diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 5a632d24d3c9..f67926978d16 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -1242,7 +1242,7 @@ void WebContents::BeginFrameSubscription(mate::Arguments* args) { const auto view = web_contents()->GetRenderWidgetHostView(); if (view) { std::unique_ptr frame_subscriber(new FrameSubscriber( - view, callback, only_dirty)); + isolate(), view, callback, only_dirty)); view->BeginFrameSubscription(std::move(frame_subscriber)); } } diff --git a/atom/browser/api/frame_subscriber.cc b/atom/browser/api/frame_subscriber.cc index c33fa9d80f55..3840f8d98b03 100644 --- a/atom/browser/api/frame_subscriber.cc +++ b/atom/browser/api/frame_subscriber.cc @@ -15,10 +15,12 @@ namespace atom { namespace api { -FrameSubscriber::FrameSubscriber(content::RenderWidgetHostView* view, +FrameSubscriber::FrameSubscriber(v8::Isolate* isolate, + content::RenderWidgetHostView* view, const FrameCaptureCallback& callback, bool only_dirty) - : view_(view), + : isolate_(isolate), + view_(view), callback_(callback), only_dirty_(only_dirty), weak_factory_(this) { @@ -68,7 +70,23 @@ void FrameSubscriber::OnFrameDelivered(const FrameCaptureCallback& callback, if (response != content::ReadbackResponse::READBACK_SUCCESS) return; - callback.Run(gfx::Image::CreateFrom1xBitmap(bitmap), damage_rect); + v8::Locker locker(isolate_); + v8::HandleScope handle_scope(isolate_); + + size_t rgb_arr_size = bitmap.width() * bitmap.height() * + bitmap.bytesPerPixel(); + v8::MaybeLocal buffer = node::Buffer::New(isolate_, rgb_arr_size); + if (buffer.IsEmpty()) + return; + + bitmap.copyPixelsTo( + reinterpret_cast(node::Buffer::Data(buffer.ToLocalChecked())), + rgb_arr_size); + + v8::Local damage = + mate::Converter::ToV8(isolate_, damage_rect); + + callback_.Run(buffer.ToLocalChecked(), damage); } } // namespace api diff --git a/atom/browser/api/frame_subscriber.h b/atom/browser/api/frame_subscriber.h index 628c2bd7de17..f1c7f0af45e2 100644 --- a/atom/browser/api/frame_subscriber.h +++ b/atom/browser/api/frame_subscriber.h @@ -12,7 +12,7 @@ #include "content/public/browser/render_widget_host_view_frame_subscriber.h" #include "third_party/skia/include/core/SkBitmap.h" #include "ui/gfx/geometry/size.h" -#include "ui/gfx/image/image.h" +#include "v8/include/v8.h" namespace atom { @@ -21,9 +21,10 @@ namespace api { class FrameSubscriber : public content::RenderWidgetHostViewFrameSubscriber { public: using FrameCaptureCallback = - base::Callback; + base::Callback, v8::Local)>; - FrameSubscriber(content::RenderWidgetHostView* view, + FrameSubscriber(v8::Isolate* isolate, + content::RenderWidgetHostView* view, const FrameCaptureCallback& callback, bool only_dirty); @@ -38,6 +39,7 @@ class FrameSubscriber : public content::RenderWidgetHostViewFrameSubscriber { const SkBitmap& bitmap, content::ReadbackResponse response); + v8::Isolate* isolate_; content::RenderWidgetHostView* view_; FrameCaptureCallback callback_; bool only_dirty_; diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index ef75cd61e6cd..235c044c81e5 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -1063,15 +1063,18 @@ For the `mouseWheel` event, the `event` object also have following properties: * `callback` Function Begin subscribing for presentation events and captured frames, the `callback` -will be called with `callback(image, dirtyRect)` when there is a +will be called with `callback(frameBuffer, dirtyRect)` when there is a presentation event. -The `image` is a [NativeImage](native-image.md) that contains the image data of -the frame. +The `frameBuffer` is a `Buffer` that contains raw pixel data. On most machines, +the pixel data is effectively stored in 32bit BGRA format, but the actual +representation depends on the endianness of the processor (most modern +processors are little-endian, on machines with big-endian processors the data +is in 32bit ARGB format). The `dirtyRect` is an object with `x, y, width, height` properties that describes which part of the page was repainted. If `onlyDirty` is set to -`true`, `image` will only contain the repainted area. `onlyDirty` +`true`, `frameBuffer` will only contain the repainted area. `onlyDirty` defaults to `false`. #### `contents.endFrameSubscription()` diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index be56c0b60ad8..ba8c979a9f8b 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -670,12 +670,12 @@ describe('browser-window module', function () { let called = false w.loadURL('file://' + fixtures + '/api/frame-subscriber.html') w.webContents.on('dom-ready', function () { - w.webContents.beginFrameSubscription(function (image) { + w.webContents.beginFrameSubscription(function (data) { // This callback might be called twice. if (called) return called = true - assert.equal(image.isEmpty(), false) + assert.notEqual(data.length, 0) w.webContents.endFrameSubscription() done() }) @@ -686,12 +686,12 @@ describe('browser-window module', function () { let called = false w.loadURL('file://' + fixtures + '/api/frame-subscriber.html') w.webContents.on('dom-ready', function () { - w.webContents.beginFrameSubscription(true, function (image) { + w.webContents.beginFrameSubscription(true, function (data) { // This callback might be called twice. if (called) return called = true - assert.equal(image.isEmpty(), false) + assert.notEqual(data.length, 0) w.webContents.endFrameSubscription() done() })