electron/atom/browser/api/frame_subscriber.cc

122 lines
3.7 KiB
C++
Raw Normal View History

// 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"
#include "base/bind.h"
#include "atom/common/native_mate_converters/gfx_converter.h"
2016-06-26 02:46:40 +00:00
#include "atom/common/node_includes.h"
#include "content/public/browser/render_widget_host.h"
2016-07-05 19:33:22 +00:00
#include <iostream>
#include "content/browser/compositor/image_transport_factory.h"
#include "cc/surfaces/surface_manager.h"
#include "cc/surfaces/surface.h"
#include "cc/output/copy_output_request.h"
namespace atom {
namespace api {
2016-07-05 19:33:22 +00:00
FrameSubscriberRenderWidgetHostView::FrameSubscriberRenderWidgetHostView() {
std::cout << "FrameSubscriberRenderWidgetHostView" << std::endl;
}
FrameSubscriberRenderWidgetHostView::~FrameSubscriberRenderWidgetHostView() {
std::cout << "~FrameSubscriberRenderWidgetHostView" << std::endl;
}
FrameSubscriber::FrameSubscriber(v8::Isolate* isolate,
content::RenderWidgetHostView* view,
const FrameCaptureCallback& callback,
bool only_dirty)
2016-06-26 02:46:40 +00:00
: isolate_(isolate),
view_(view),
callback_(callback),
only_dirty_(only_dirty),
weak_factory_(this) {
}
bool FrameSubscriber::ShouldCaptureFrame(
const gfx::Rect& dirty_rect,
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;
2016-07-05 19:33:22 +00:00
2016-02-26 01:05:57 +00:00
if (!view_ || !host)
return false;
2016-07-05 19:33:22 +00:00
/*if (dirty_rect.IsEmpty())
return false;*/
gfx::Rect rect = gfx::Rect(view_->GetVisibleViewportSize());
if (only_dirty_)
rect = dirty_rect;
2016-07-05 19:33:22 +00:00
/*host->CopyFromBackingStore(
rect,
rect.size(),
base::Bind(&FrameSubscriber::OnFrameDelivered,
weak_factory_.GetWeakPtr(), callback_, rect),
2016-07-05 19:33:22 +00:00
kBGRA_8888_SkColorType);*/
2016-07-05 19:33:22 +00:00
const auto base = reinterpret_cast<content::RenderWidgetHostViewBase*>(
view_);
content::ImageTransportFactory* itf =
content::ImageTransportFactory::GetInstance();
cc::SurfaceManager* sfm = itf->GetSurfaceManager();
cc::Surface* surface = sfm->GetSurfaceForId(base->SurfaceIdForTesting());
std::unique_ptr<cc::CopyOutputRequest> request =
cc::CopyOutputRequest::CreateBitmapRequest(
base::Bind(&FrameSubscriber::ReadbackResultAsBitmap,
base::Unretained(this)));
2016-07-18 14:16:23 +00:00
// surface->RequestCopyOfOutput(std::move(request));
std::cout << request.get() << surface << std::endl;
return false;
}
2016-07-05 19:33:22 +00:00
void FrameSubscriber::ReadbackResultAsBitmap(
std::unique_ptr<cc::CopyOutputResult> result) {
std::unique_ptr<SkBitmap> bitmap = result->TakeBitmap();
SkAutoPixmapUnlock res;
if (!bitmap->requestLock(&res))
return;
std::cout << res.pixmap().addr() << std::endl;
}
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) {
if (response != content::ReadbackResponse::READBACK_SUCCESS)
return;
v8::Locker locker(isolate_);
v8::HandleScope handle_scope(isolate_);
size_t rgb_arr_size = bitmap.width() * bitmap.height() *
bitmap.bytesPerPixel();
v8::MaybeLocal<v8::Object> buffer = node::Buffer::New(isolate_, rgb_arr_size);
if (buffer.IsEmpty())
return;
bitmap.copyPixelsTo(
2016-03-08 14:28:53 +00:00
reinterpret_cast<uint8_t*>(node::Buffer::Data(buffer.ToLocalChecked())),
rgb_arr_size);
v8::Local<v8::Value> damage =
2016-06-26 02:46:40 +00:00
mate::Converter<gfx::Rect>::ToV8(isolate_, damage_rect);
callback_.Run(buffer.ToLocalChecked(), damage);
}
} // namespace api
} // namespace atom