8000 fix: port OSR code to new viz compositor codepath by brenca · Pull Request #17538 · electron/electron · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: port OSR code to new viz compositor codepath #17538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -632,11 +632,13 @@ static_library("electron_lib") {

if (enable_osr) {
sources += [
"atom/browser/osr/osr_output_device.cc",
"atom/browser/osr/osr_output_device.h",
"atom/browser/osr/osr_host_display_client.cc",
"ato 8000 m/browser/osr/osr_host_display_client.h",
"atom/browser/osr/osr_host_display_client_mac.mm",
"atom/browser/osr/osr_render_widget_host_view.cc",
"atom/browser/osr/osr_render_widget_host_view.h",
"atom/browser/osr/osr_render_widget_host_view_mac.mm",
"atom/browser/osr/osr_video_consumer.cc",
"atom/browser/osr/osr_video_consumer.h",
"atom/browser/osr/osr_view_proxy.cc",
"atom/browser/osr/osr_view_proxy.h",
"atom/browser/osr/osr_web_contents_view.cc",
Expand Down Expand Up @@ -811,6 +813,11 @@ if (is_mac) {
"ServiceManagement.framework",
"StoreKit.framework",
]

if (enable_osr) {
libs += [ "IOSurface.framework" ]
}

ldflags = [
"-F",
rebase_path("external_binaries", root_build_dir),
Expand Down
1 change: 0 additions & 1 deletion atom/browser/api/atom_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
#include "ui/events/base_event_utils.h"

#if BUILDFLAG(ENABLE_OSR)
#include "atom/browser/osr/osr_output_device.h"
#include "atom/browser/osr/osr_render_widget_host_view.h"
#include "atom/browser/osr/osr_web_contents_view.h"
#endif
Expand Down
5 changes: 4 additions & 1 deletion atom/browser/native_window_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,10 @@ static bool FromV8(v8::Isolate* isolate,
namespace {

bool IsFramelessWindow(NSView* view) {
NativeWindow* window = [static_cast<AtomNSWindow*>([view window]) shell];
NSWindow* nswindow = [view window];
if (![nswindow respondsToSelector:@selector(shell)])
return false;
NativeWindow* window = [static_cast<AtomNSWindow*>(nswindow) shell];
return window && !window->has_frame();
}

Expand Down
121 changes: 121 additions & 0 deletions atom/browser/osr/osr_host_display_client.cc
AE27
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "atom/browser/osr/osr_host_display_client.h"

#include <utility>

#include "components/viz/common/resources/resource_format.h"
#include "components/viz/common/resources/resource_sizes.h"
#include "mojo/public/cpp/system/platform_handle.h"
#include "skia/ext/platform_canvas.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkRect.h"
#include "third_party/skia/src/core/SkDevice.h"
#include "ui/gfx/skia_util.h"

#if defined(OS_WIN)
#include "skia/ext/skia_utils_win.h"
#endif

namespace atom {

LayeredWindowUpdater::LayeredWindowUpdater(
viz::mojom::LayeredWindowUpdaterRequest request,
OnPaintCallback callback)
: callback_(callback), binding_(this, std::move(request)) {}

LayeredWindowUpdater::~LayeredWindowUpdater() = default;

void LayeredWindowUpdater::SetActive(bool active) {
active_ = active;
}

void LayeredWindowUpdater::OnAllocatedSharedMemory(
const gfx::Size& pixel_size,
mojo::ScopedSharedBufferHandle scoped_buffer_handle) {
canvas_.reset();

// Make sure |pixel_size| is sane.
size_t expected_bytes;
bool size_result = viz::ResourceSizes::MaybeSizeInBytes(
pixel_size, viz::ResourceFormat::RGBA_8888, &expected_bytes);
if (!size_result)
return;

#if defined(WIN32)
base::SharedMemoryHandle shm_handle;
size_t required_bytes;
MojoResult unwrap_result = mojo::UnwrapSharedMemoryHandle(
std::move(scoped_buffer_handle), &shm_handle, &required_bytes, nullptr);
if (unwrap_result != MOJO_RESULT_OK)
return;

base::SharedMemory shm(shm_handle, false);
if (!shm.Map(required_bytes)) {
DLOG(ERROR) << "Failed to map " << required_bytes << " bytes";
return;
}

canvas_ = skia::CreatePlatformCanvasWithSharedSection(
pixel_size.width(), pixel_size.height(), false, shm.handle().GetHandle(),
skia::CRASH_ON_FAILURE);
#else
auto shm =
mojo::UnwrapWritableSharedMemoryRegion(std::move(scoped_buffer_handle));
if (!shm.IsValid()) {
DLOG(ERROR) << "Failed to unwrap shared memory region";
return;
}

shm_mapping_ = shm.Map();
if (!shm_mapping_.IsValid()) {
DLOG(ERROR) << "Failed to map shared memory region";
return;
}

canvas_ = skia::CreatePlatformCanvasWithPixels(
pixel_size.width(), pixel_size.height(), false,
static_cast<uint8_t*>(shm_mapping_.memory()), skia::CRASH_ON_FAILURE);
#endif
}

void LayeredWindowUpdater::Draw(const gfx::Rect& damage_rect,
DrawCallback draw_callback) {
SkPixmap pixmap;
SkBitmap bitmap;

if (active_ && canvas_->peekPixels(&pixmap)) {
bitmap.installPixels(pixmap);
callback_.Run(damage_rect, bitmap);
}

std::move(draw_callback).Run();
}

OffScreenHostDisplayClient::OffScreenHostDisplayClient(
gfx::AcceleratedWidget widget,
OnPaintCallback callback)
: viz::HostDisplayClient(widget), callback_(callback) {}
OffScreenHostDisplayClient::~OffScreenHostDisplayClient() {}

void OffScreenHostDisplayClient::SetActive(bool active) {
active_ = active;
if (layered_window_updater_) {
layered_window_updater_->SetActive(active_);
}
}

void OffScreenHostDisplayClient::IsOffscreen(IsOffscreenCallback callback) {
std::move(callback).Run(true);
}

void OffScreenHostDisplayClient::CreateLayeredWindowUpdater(
viz::mojom::LayeredWindowUpdaterRequest request) {
layered_window_updater_ =
std::make_unique<LayeredWindowUpdater>(std::move(request), callback_);
layered_window_updater_->SetActive(active_);
}

} // namespace atom
77 changes: 77 additions & 0 deletions atom/browser/osr/osr_host_display_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#ifndef ATOM_BROWSER_OSR_OSR_HOST_DISPLAY_CLIENT_H_
#define ATOM_BROWSER_OSR_OSR_HOST_DISPLAY_CLIENT_H_

#include <memory>

#include "base/callback.h"
#include "base/memory/shared_memory.h"
#include "components/viz/host/host_display_client.h"
#include "services/viz/privileged/interfaces/compositing/layered_window_updater.mojom.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "ui/gfx/native_widget_types.h"

namespace atom {

typedef base::Callback<void(const gfx::Rect&, const SkBitmap&)> OnPaintCallback;

class LayeredWindowUpdater : public viz::mojom::LayeredWindowUpdater {
public:
explicit LayeredWindowUpdater(viz::mojom::LayeredWindowUpdaterRequest request,
OnPaintCallback callback);
~LayeredWindowUpdater() override;

void SetActive(bool active);

// viz::mojom::LayeredWindowUpdater implementation.
void OnAllocatedSharedMemory(
const gfx::Size& pixel_size,
mojo::ScopedSharedBufferHandle scoped_buffer_handle) override;
void Draw(const gfx::Rect& damage_rect, DrawCallback draw_callback) override;

private:
OnPaintCallback callback_;
mojo::Binding<viz::mojom::LayeredWindowUpdater> binding_;
std::unique_ptr<SkCanvas> canvas_;
bool active_ = false;

#if !defined(WIN32)
base::WritableSharedMemoryMapping shm_mapping_;
#endif

DISALLOW_COPY_AND_ASSIGN(LayeredWindowUpdater);
};

class OffScreenHostDisplayClient : public viz::HostDisplayClient {
public:
explicit OffScreenHostDisplayClient(gfx::AcceleratedWidget widget,
OnPaintCallback callback);
~OffScreenHostDisplayClient() override;

void SetActive(bool active);

private:
void IsOffscreen(IsOffscreenCallback callback) override;

#if defined(OS_MACOSX)
void OnDisplayReceivedCALayerParams(
const gfx::CALayerParams& ca_layer_params) override;
#endif

void CreateLayeredWindowUpdater(
viz::mojom::LayeredWindowUpdaterRequest request) override;

std::unique_ptr<LayeredWindowUpdater> layered_window_updater_;
OnPaintCallback callback_;
bool active_ = false;

DISALLOW_COPY_AND_ASSIGN(OffScreenHostDisplayClient);
};

} // namespace atom

#endif // ATOM_BROWSER_OSR_OSR_HOST_DISPLAY_CLIENT_H_
35 changes: 35 additions & 0 deletions atom/browser/osr/osr_host_display_client_mac.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "atom/browser/osr/osr_host_display_client.h"

#include <IOSurface/IOSurface.h>

namespace atom {

void OffScreenHostDisplayClient::OnDisplayReceivedCALayerParams(
const gfx::CALayerParams& ca_layer_params) {
if (!ca_layer_params.is_empty) {
base::ScopedCFTypeRef<IOSurfaceRef> io_surface(
IOSurfaceLookupFromMachPort(ca_layer_params.io_surface_mach_port));

gfx::Size pixel_size_ = ca_layer_params.pixel_size;
void* pixels = static_cast<void*>(IOSurfaceGetBaseAddress(io_surface));
size_t stride = IOSurfaceGetBytesPerRow(io_surface);

struct IOSurfacePinner {
base::ScopedCFTypeRef<IOSurfaceRef> io_surface;
};

SkBitmap bitmap;
bitmap.installPixels(
SkImageInfo::MakeN32(pixel_size_.width(), pixel_size_.height(),
kPremul_SkAlphaType),
pixels, stride);
bitmap.setImmutable();
callback_.Run(ca_layer_params.damage, bitmap);
}
}

} // namespace atom
101 changes: 0 additions & 101 deletions atom/browser/osr/osr_output_device.cc

This file was deleted.

Loading
0