2015-09-18 07:57:43 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/api/frame_subscriber.h"
|
|
|
|
|
2016-06-21 00:15:39 +00:00
|
|
|
#include "atom/common/native_mate_converters/gfx_converter.h"
|
2016-06-26 02:46:40 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
2016-08-26 22:30:02 +00:00
|
|
|
#include "base/bind.h"
|
2016-02-25 22:48:58 +00:00
|
|
|
#include "content/public/browser/render_widget_host.h"
|
|
|
|
|
2015-09-18 07:57:43 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
|
|
|
FrameSubscriber::FrameSubscriber(v8::Isolate* isolate,
|
2016-02-18 00:15:00 +00:00
|
|
|
content::RenderWidgetHostView* view,
|
2016-06-21 00:15:39 +00:00
|
|
|
const FrameCaptureCallback& callback,
|
2016-06-25 16:23:40 +00:00
|
|
|
bool only_dirty)
|
2016-06-26 02:46:40 +00:00
|
|
|
: isolate_(isolate),
|
|
|
|
view_(view),
|
|
|
|
callback_(callback),
|
|
|
|
only_dirty_(only_dirty),
|
|
|
|
weak_factory_(this) {
|
2015-09-18 07:57:43 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 02:44:10 +00:00
|
|
|
bool FrameSubscriber::ShouldCaptureFrame(
|
2016-06-25 16:23:40 +00:00
|
|
|
const gfx::Rect& dirty_rect,
|
2015-09-18 07:57:43 +00:00
|
|
|
base::TimeTicks present_time,
|
|
|
|
scoped_refptr<media::VideoFrame>* storage,
|
|
|
|
DeliverFrameCallback* callback) {
|
2016-02-26 01:05:57 +00:00
|
|
|
const auto host = view_ ? view_->GetRenderWidgetHost() : nullptr;
|
|
|
|
if (!view_ || !host)
|
2016-02-18 00:15:00 +00:00
|
|
|
return false;
|
|
|
|
|
2016-06-25 16:23:40 +00:00
|
|
|
if (dirty_rect.IsEmpty())
|
2016-06-21 00:15:39 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
gfx::Rect rect = gfx::Rect(view_->GetVisibleViewportSize());
|
2016-06-25 16:23:40 +00:00
|
|
|
if (only_dirty_)
|
|
|
|
rect = dirty_rect;
|
2016-02-18 00:15:00 +00:00
|
|
|
|
|
|
|
host->CopyFromBackingStore(
|
2016-06-21 00:15:39 +00:00
|
|
|
rect,
|
|
|
|
rect.size(),
|
2016-02-18 00:15:00 +00:00
|
|
|
base::Bind(&FrameSubscriber::OnFrameDelivered,
|
2016-06-21 00:15:39 +00:00
|
|
|
weak_factory_.GetWeakPtr(), callback_, rect),
|
2016-02-18 00:15:00 +00:00
|
|
|
kBGRA_8888_SkColorType);
|
|
|
|
|
|
|
|
return false;
|
2015-09-18 07:57:43 +00:00
|
|
|
}
|
|
|
|
|
2016-02-18 00:15:00 +00:00
|
|
|
void FrameSubscriber::OnFrameDelivered(const FrameCaptureCallback& callback,
|
2016-06-26 02:46:40 +00:00
|
|
|
const gfx::Rect& damage_rect,
|
|
|
|
const SkBitmap& bitmap,
|
|
|
|
content::ReadbackResponse response) {
|
2016-06-21 00:15:39 +00:00
|
|
|
if (response != content::ReadbackResponse::READBACK_SUCCESS)
|
2015-09-18 07:57:43 +00:00
|
|
|
return;
|
|
|
|
|
2015-11-14 22:59:38 +00:00
|
|
|
v8::Locker locker(isolate_);
|
|
|
|
v8::HandleScope handle_scope(isolate_);
|
|
|
|
|
2016-02-18 00:15:00 +00:00
|
|
|
size_t rgb_arr_size = bitmap.width() * bitmap.height() *
|
|
|
|
bitmap.bytesPerPixel();
|
2015-09-18 07:57:43 +00:00
|
|
|
v8::MaybeLocal<v8::Object> buffer = node::Buffer::New(isolate_, rgb_arr_size);
|
|
|
|
if (buffer.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
2016-02-18 00:15:00 +00:00
|
|
|
bitmap.copyPixelsTo(
|
2016-03-08 14:28:53 +00:00
|
|
|
reinterpret_cast<uint8_t*>(node::Buffer::Data(buffer.ToLocalChecked())),
|
2016-02-18 00:15:00 +00:00
|
|
|
rgb_arr_size);
|
2015-09-18 07:57:43 +00:00
|
|
|
|
2016-06-21 00:15:39 +00:00
|
|
|
v8::Local<v8::Value> damage =
|
2016-06-26 02:46:40 +00:00
|
|
|
mate::Converter<gfx::Rect>::ToV8(isolate_, damage_rect);
|
2016-06-21 00:15:39 +00:00
|
|
|
|
|
|
|
callback_.Run(buffer.ToLocalChecked(), damage);
|
2015-09-18 07:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|