2019-07-16 22:09:04 +02:00
|
|
|
// Copyright 2014 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "cef/libcef/browser/osr/software_output_device_proxy.h"
|
|
|
|
|
2020-02-10 18:10:17 +01:00
|
|
|
#include "base/memory/shared_memory_mapping.h"
|
2019-11-12 17:11:44 +01:00
|
|
|
#include "base/trace_event/trace_event.h"
|
2019-07-16 22:09:04 +02:00
|
|
|
#include "components/viz/common/resources/resource_sizes.h"
|
2022-03-26 02:12:30 +01:00
|
|
|
#include "mojo/public/cpp/bindings/pending_remote.h"
|
2019-07-16 22:09:04 +02:00
|
|
|
#include "mojo/public/cpp/system/platform_handle.h"
|
|
|
|
#include "skia/ext/platform_canvas.h"
|
|
|
|
#include "third_party/skia/include/core/SkCanvas.h"
|
|
|
|
#include "ui/gfx/skia_util.h"
|
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2019-07-16 22:09:04 +02:00
|
|
|
#include <windows.h>
|
|
|
|
#include "skia/ext/skia_utils_win.h"
|
|
|
|
#include "ui/gfx/gdi_util.h"
|
|
|
|
#include "ui/gfx/win/hwnd_util.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace viz {
|
|
|
|
|
|
|
|
SoftwareOutputDeviceProxy::~SoftwareOutputDeviceProxy() = default;
|
|
|
|
|
|
|
|
SoftwareOutputDeviceProxy::SoftwareOutputDeviceProxy(
|
2022-03-26 02:12:30 +01:00
|
|
|
mojo::PendingRemote<mojom::LayeredWindowUpdater> layered_window_updater)
|
2019-07-16 22:09:04 +02:00
|
|
|
: layered_window_updater_(std::move(layered_window_updater)) {
|
|
|
|
DCHECK(layered_window_updater_.is_bound());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoftwareOutputDeviceProxy::OnSwapBuffers(
|
2023-01-03 00:34:43 +01:00
|
|
|
SwapBuffersCallback swap_ack_callback,
|
2023-01-30 18:43:54 +01:00
|
|
|
gfx::FrameData data) {
|
2019-07-16 22:09:04 +02:00
|
|
|
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
|
|
|
|
DCHECK(swap_ack_callback_.is_null());
|
|
|
|
|
|
|
|
// We aren't waiting on DrawAck() and can immediately run the callback.
|
|
|
|
if (!waiting_on_draw_ack_) {
|
|
|
|
task_runner_->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
base::BindOnce(std::move(swap_ack_callback), viewport_pixel_size_));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
swap_ack_callback_ =
|
|
|
|
base::BindOnce(std::move(swap_ack_callback), viewport_pixel_size_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoftwareOutputDeviceProxy::Resize(const gfx::Size& viewport_pixel_size,
|
|
|
|
float scale_factor) {
|
|
|
|
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
|
|
|
|
DCHECK(!in_paint_);
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (viewport_pixel_size_ == viewport_pixel_size) {
|
2019-07-16 22:09:04 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-07-16 22:09:04 +02:00
|
|
|
|
|
|
|
viewport_pixel_size_ = viewport_pixel_size;
|
|
|
|
|
|
|
|
canvas_.reset();
|
|
|
|
|
|
|
|
size_t required_bytes;
|
2023-06-26 12:13:38 +02:00
|
|
|
if (!ResourceSizes::MaybeSizeInBytes(viewport_pixel_size_,
|
|
|
|
SinglePlaneFormat::kRGBA_8888,
|
|
|
|
&required_bytes)) {
|
2019-07-16 22:09:04 +02:00
|
|
|
DLOG(ERROR) << "Invalid viewport size " << viewport_pixel_size_.ToString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-04 17:13:32 +02:00
|
|
|
base::UnsafeSharedMemoryRegion region =
|
|
|
|
base::UnsafeSharedMemoryRegion::Create(required_bytes);
|
|
|
|
if (!region.IsValid()) {
|
|
|
|
DLOG(ERROR) << "Failed to allocate " << required_bytes << " bytes";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if !BUILDFLAG(IS_WIN)
|
2020-04-14 21:31:00 +02:00
|
|
|
auto shm = base::ReadOnlySharedMemoryRegion::Create(required_bytes);
|
2019-07-16 22:09:04 +02:00
|
|
|
if (!shm.IsValid()) {
|
|
|
|
DLOG(ERROR) << "Failed to allocate " << required_bytes << " bytes";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-04 17:13:32 +02:00
|
|
|
shm_ = region.Map();
|
2019-07-16 22:09:04 +02:00
|
|
|
if (!shm_.IsValid()) {
|
|
|
|
DLOG(ERROR) << "Failed to map " << required_bytes << " bytes";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas_ = skia::CreatePlatformCanvasWithPixels(
|
|
|
|
viewport_pixel_size_.width(), viewport_pixel_size_.height(), false,
|
|
|
|
static_cast<uint8_t*>(shm_.memory()), skia::CRASH_ON_FAILURE);
|
|
|
|
#else
|
|
|
|
canvas_ = skia::CreatePlatformCanvasWithSharedSection(
|
|
|
|
viewport_pixel_size_.width(), viewport_pixel_size_.height(), false,
|
2019-09-04 17:13:32 +02:00
|
|
|
region.GetPlatformHandle(), skia::CRASH_ON_FAILURE);
|
2019-07-16 22:09:04 +02:00
|
|
|
#endif
|
|
|
|
|
2019-09-04 17:13:32 +02:00
|
|
|
// Transfer region ownership to the browser process.
|
2019-07-16 22:09:04 +02:00
|
|
|
layered_window_updater_->OnAllocatedSharedMemory(viewport_pixel_size_,
|
2019-09-04 17:13:32 +02:00
|
|
|
std::move(region));
|
2019-07-16 22:09:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SkCanvas* SoftwareOutputDeviceProxy::BeginPaint(const gfx::Rect& damage_rect) {
|
|
|
|
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
|
|
|
|
DCHECK(!in_paint_);
|
|
|
|
|
|
|
|
damage_rect_ = damage_rect;
|
|
|
|
in_paint_ = true;
|
|
|
|
|
|
|
|
return canvas_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoftwareOutputDeviceProxy::EndPaint() {
|
|
|
|
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
|
|
|
|
DCHECK(in_paint_);
|
|
|
|
DCHECK(!waiting_on_draw_ack_);
|
|
|
|
|
|
|
|
in_paint_ = false;
|
|
|
|
|
|
|
|
gfx::Rect intersected_damage_rect = damage_rect_;
|
|
|
|
intersected_damage_rect.Intersect(gfx::Rect(viewport_pixel_size_));
|
2023-01-02 23:59:03 +01:00
|
|
|
if (intersected_damage_rect.IsEmpty()) {
|
2019-07-16 22:09:04 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-07-16 22:09:04 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!canvas_) {
|
2019-07-16 22:09:04 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-07-16 22:09:04 +02:00
|
|
|
|
|
|
|
layered_window_updater_->Draw(
|
|
|
|
damage_rect_, base::BindOnce(&SoftwareOutputDeviceProxy::DrawAck,
|
|
|
|
base::Unretained(this)));
|
|
|
|
waiting_on_draw_ack_ = true;
|
|
|
|
|
|
|
|
TRACE_EVENT_ASYNC_BEGIN0("viz", "SoftwareOutputDeviceProxy::Draw", this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoftwareOutputDeviceProxy::DrawAck() {
|
|
|
|
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
|
|
|
|
DCHECK(waiting_on_draw_ack_);
|
|
|
|
DCHECK(!swap_ack_callback_.is_null());
|
|
|
|
|
|
|
|
TRACE_EVENT_ASYNC_END0("viz", "SoftwareOutputDeviceProxy::Draw", this);
|
|
|
|
|
|
|
|
waiting_on_draw_ack_ = false;
|
|
|
|
std::move(swap_ack_callback_).Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace viz
|