2019-07-16 22:09:04 +02:00
|
|
|
// Copyright 2019 The Chromium Embedded Framework 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 "libcef/browser/osr/host_display_client_osr.h"
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "libcef/browser/osr/render_widget_host_view_osr.h"
|
|
|
|
|
2020-02-10 18:10:17 +01:00
|
|
|
#include "base/memory/shared_memory_mapping.h"
|
2019-07-16 22:09:04 +02:00
|
|
|
#include "components/viz/common/resources/resource_sizes.h"
|
|
|
|
#include "mojo/public/cpp/system/platform_handle.h"
|
2019-10-01 15:55:16 +02:00
|
|
|
#include "services/viz/privileged/mojom/compositing/layered_window_updater.mojom.h"
|
2019-07-16 22:09:04 +02:00
|
|
|
#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"
|
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2019-07-16 22:09:04 +02:00
|
|
|
#include "skia/ext/skia_utils_win.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class CefLayeredWindowUpdaterOSR : public viz::mojom::LayeredWindowUpdater {
|
|
|
|
public:
|
2019-11-12 17:11:44 +01:00
|
|
|
CefLayeredWindowUpdaterOSR(
|
|
|
|
CefRenderWidgetHostViewOSR* const view,
|
|
|
|
mojo::PendingReceiver<viz::mojom::LayeredWindowUpdater> receiver);
|
2021-12-06 21:40:25 +01:00
|
|
|
|
|
|
|
CefLayeredWindowUpdaterOSR(const CefLayeredWindowUpdaterOSR&) = delete;
|
|
|
|
CefLayeredWindowUpdaterOSR& operator=(const CefLayeredWindowUpdaterOSR&) =
|
|
|
|
delete;
|
|
|
|
|
2019-07-16 22:09:04 +02:00
|
|
|
~CefLayeredWindowUpdaterOSR() override;
|
|
|
|
|
|
|
|
void SetActive(bool active);
|
|
|
|
const void* GetPixelMemory() const;
|
|
|
|
gfx::Size GetPixelSize() const;
|
|
|
|
|
|
|
|
// viz::mojom::LayeredWindowUpdater implementation.
|
2019-09-04 17:13:32 +02:00
|
|
|
void OnAllocatedSharedMemory(const gfx::Size& pixel_size,
|
|
|
|
base::UnsafeSharedMemoryRegion region) override;
|
2019-07-16 22:09:04 +02:00
|
|
|
void Draw(const gfx::Rect& damage_rect, DrawCallback draw_callback) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
CefRenderWidgetHostViewOSR* const view_;
|
2019-11-12 17:11:44 +01:00
|
|
|
mojo::Receiver<viz::mojom::LayeredWindowUpdater> receiver_;
|
2019-07-16 22:09:04 +02:00
|
|
|
bool active_ = false;
|
2019-09-04 17:13:32 +02:00
|
|
|
base::WritableSharedMemoryMapping shared_memory_;
|
2019-07-16 22:09:04 +02:00
|
|
|
gfx::Size pixel_size_;
|
|
|
|
};
|
|
|
|
|
|
|
|
CefLayeredWindowUpdaterOSR::CefLayeredWindowUpdaterOSR(
|
|
|
|
CefRenderWidgetHostViewOSR* const view,
|
2019-11-12 17:11:44 +01:00
|
|
|
mojo::PendingReceiver<viz::mojom::LayeredWindowUpdater> receiver)
|
|
|
|
: view_(view), receiver_(this, std::move(receiver)) {}
|
2019-07-16 22:09:04 +02:00
|
|
|
|
|
|
|
CefLayeredWindowUpdaterOSR::~CefLayeredWindowUpdaterOSR() = default;
|
|
|
|
|
|
|
|
void CefLayeredWindowUpdaterOSR::SetActive(bool active) {
|
|
|
|
active_ = active;
|
|
|
|
}
|
|
|
|
|
|
|
|
const void* CefLayeredWindowUpdaterOSR::GetPixelMemory() const {
|
|
|
|
return shared_memory_.memory();
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Size CefLayeredWindowUpdaterOSR::GetPixelSize() const {
|
|
|
|
return pixel_size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefLayeredWindowUpdaterOSR::OnAllocatedSharedMemory(
|
|
|
|
const gfx::Size& pixel_size,
|
2019-09-04 17:13:32 +02:00
|
|
|
base::UnsafeSharedMemoryRegion region) {
|
2019-07-16 22:09:04 +02:00
|
|
|
// Make sure |pixel_size| is sane.
|
|
|
|
size_t expected_bytes;
|
2024-01-05 18:14:58 +01:00
|
|
|
if (!viz::ResourceSizes::MaybeSizeInBytes(
|
|
|
|
pixel_size, viz::SinglePlaneFormat::kRGBA_8888, &expected_bytes)) {
|
|
|
|
DLOG(ERROR) << "OnAllocatedSharedMemory with size that overflows";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto mapping = region.Map();
|
|
|
|
if (!mapping.IsValid()) {
|
|
|
|
DLOG(ERROR) << "Shared memory mapping failed.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mapping.size() < expected_bytes) {
|
|
|
|
DLOG(ERROR) << "Shared memory size was less than expected.";
|
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
|
|
|
|
|
|
|
pixel_size_ = pixel_size;
|
2024-01-05 18:14:58 +01:00
|
|
|
shared_memory_ = std::move(mapping);
|
2019-07-16 22:09:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefLayeredWindowUpdaterOSR::Draw(const gfx::Rect& damage_rect,
|
|
|
|
DrawCallback draw_callback) {
|
|
|
|
if (active_) {
|
|
|
|
const void* memory = GetPixelMemory();
|
|
|
|
if (memory) {
|
|
|
|
view_->OnPaint(damage_rect, pixel_size_, memory);
|
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Failed to read pixels";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::move(draw_callback).Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefHostDisplayClientOSR::CefHostDisplayClientOSR(
|
|
|
|
CefRenderWidgetHostViewOSR* const view,
|
|
|
|
gfx::AcceleratedWidget widget)
|
|
|
|
: viz::HostDisplayClient(widget), view_(view) {}
|
|
|
|
|
2024-01-20 23:48:57 +01:00
|
|
|
CefHostDisplayClientOSR::~CefHostDisplayClientOSR() = default;
|
2019-07-16 22:09:04 +02:00
|
|
|
|
|
|
|
void CefHostDisplayClientOSR::SetActive(bool active) {
|
|
|
|
active_ = active;
|
|
|
|
if (layered_window_updater_) {
|
|
|
|
layered_window_updater_->SetActive(active_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const void* CefHostDisplayClientOSR::GetPixelMemory() const {
|
|
|
|
return layered_window_updater_ ? layered_window_updater_->GetPixelMemory()
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Size CefHostDisplayClientOSR::GetPixelSize() const {
|
|
|
|
return layered_window_updater_ ? layered_window_updater_->GetPixelSize()
|
|
|
|
: gfx::Size{};
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefHostDisplayClientOSR::UseProxyOutputDevice(
|
|
|
|
UseProxyOutputDeviceCallback callback) {
|
|
|
|
std::move(callback).Run(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefHostDisplayClientOSR::CreateLayeredWindowUpdater(
|
2019-11-12 17:11:44 +01:00
|
|
|
mojo::PendingReceiver<viz::mojom::LayeredWindowUpdater> receiver) {
|
2019-07-16 22:09:04 +02:00
|
|
|
layered_window_updater_ =
|
2019-11-12 17:11:44 +01:00
|
|
|
std::make_unique<CefLayeredWindowUpdaterOSR>(view_, std::move(receiver));
|
2019-07-16 22:09:04 +02:00
|
|
|
layered_window_updater_->SetActive(active_);
|
|
|
|
}
|
2019-11-12 17:11:44 +01:00
|
|
|
|
2024-02-22 19:36:15 +01:00
|
|
|
#if BUILDFLAG(IS_LINUX) && BUILDFLAG(IS_OZONE_X11)
|
2019-11-12 17:11:44 +01:00
|
|
|
void CefHostDisplayClientOSR::DidCompleteSwapWithNewSize(
|
|
|
|
const gfx::Size& size) {}
|
|
|
|
#endif
|