• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Chromium Embedded Framework Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "libcef/browser/osr/host_display_client_osr.h"
6 
7 #include <utility>
8 
9 #include "libcef/browser/osr/render_widget_host_view_osr.h"
10 
11 #include "base/memory/shared_memory_mapping.h"
12 #include "components/viz/common/resources/resource_format.h"
13 #include "components/viz/common/resources/resource_sizes.h"
14 #include "mojo/public/cpp/system/platform_handle.h"
15 #include "services/viz/privileged/mojom/compositing/layered_window_updater.mojom.h"
16 #include "skia/ext/platform_canvas.h"
17 #include "third_party/skia/include/core/SkColor.h"
18 #include "third_party/skia/include/core/SkRect.h"
19 #include "third_party/skia/src/core/SkDevice.h"
20 #include "ui/gfx/skia_util.h"
21 
22 #if BUILDFLAG(IS_WIN)
23 #include "skia/ext/skia_utils_win.h"
24 #endif
25 
26 class CefLayeredWindowUpdaterOSR : public viz::mojom::LayeredWindowUpdater {
27  public:
28   CefLayeredWindowUpdaterOSR(
29       CefRenderWidgetHostViewOSR* const view,
30       mojo::PendingReceiver<viz::mojom::LayeredWindowUpdater> receiver);
31 
32   CefLayeredWindowUpdaterOSR(const CefLayeredWindowUpdaterOSR&) = delete;
33   CefLayeredWindowUpdaterOSR& operator=(const CefLayeredWindowUpdaterOSR&) =
34       delete;
35 
36   ~CefLayeredWindowUpdaterOSR() override;
37 
38   void SetActive(bool active);
39   const void* GetPixelMemory() const;
40   gfx::Size GetPixelSize() const;
41 
42   // viz::mojom::LayeredWindowUpdater implementation.
43   void OnAllocatedSharedMemory(const gfx::Size& pixel_size,
44                                base::UnsafeSharedMemoryRegion region) override;
45   void Draw(const gfx::Rect& damage_rect, DrawCallback draw_callback) override;
46 
47  private:
48   CefRenderWidgetHostViewOSR* const view_;
49   mojo::Receiver<viz::mojom::LayeredWindowUpdater> receiver_;
50   bool active_ = false;
51   base::WritableSharedMemoryMapping shared_memory_;
52   gfx::Size pixel_size_;
53 };
54 
CefLayeredWindowUpdaterOSR(CefRenderWidgetHostViewOSR * const view,mojo::PendingReceiver<viz::mojom::LayeredWindowUpdater> receiver)55 CefLayeredWindowUpdaterOSR::CefLayeredWindowUpdaterOSR(
56     CefRenderWidgetHostViewOSR* const view,
57     mojo::PendingReceiver<viz::mojom::LayeredWindowUpdater> receiver)
58     : view_(view), receiver_(this, std::move(receiver)) {}
59 
60 CefLayeredWindowUpdaterOSR::~CefLayeredWindowUpdaterOSR() = default;
61 
SetActive(bool active)62 void CefLayeredWindowUpdaterOSR::SetActive(bool active) {
63   active_ = active;
64 }
65 
GetPixelMemory() const66 const void* CefLayeredWindowUpdaterOSR::GetPixelMemory() const {
67   return shared_memory_.memory();
68 }
69 
GetPixelSize() const70 gfx::Size CefLayeredWindowUpdaterOSR::GetPixelSize() const {
71   return pixel_size_;
72 }
73 
OnAllocatedSharedMemory(const gfx::Size & pixel_size,base::UnsafeSharedMemoryRegion region)74 void CefLayeredWindowUpdaterOSR::OnAllocatedSharedMemory(
75     const gfx::Size& pixel_size,
76     base::UnsafeSharedMemoryRegion region) {
77   // Make sure |pixel_size| is sane.
78   size_t expected_bytes;
79   bool size_result = viz::ResourceSizes::MaybeSizeInBytes(
80       pixel_size, viz::ResourceFormat::RGBA_8888, &expected_bytes);
81   if (!size_result)
82     return;
83 
84   pixel_size_ = pixel_size;
85   shared_memory_ = region.Map();
86   DCHECK(shared_memory_.IsValid());
87 }
88 
Draw(const gfx::Rect & damage_rect,DrawCallback draw_callback)89 void CefLayeredWindowUpdaterOSR::Draw(const gfx::Rect& damage_rect,
90                                       DrawCallback draw_callback) {
91   if (active_) {
92     const void* memory = GetPixelMemory();
93     if (memory) {
94       view_->OnPaint(damage_rect, pixel_size_, memory);
95     } else {
96       LOG(WARNING) << "Failed to read pixels";
97     }
98   }
99 
100   std::move(draw_callback).Run();
101 }
102 
CefHostDisplayClientOSR(CefRenderWidgetHostViewOSR * const view,gfx::AcceleratedWidget widget)103 CefHostDisplayClientOSR::CefHostDisplayClientOSR(
104     CefRenderWidgetHostViewOSR* const view,
105     gfx::AcceleratedWidget widget)
106     : viz::HostDisplayClient(widget), view_(view) {}
107 
~CefHostDisplayClientOSR()108 CefHostDisplayClientOSR::~CefHostDisplayClientOSR() {}
109 
SetActive(bool active)110 void CefHostDisplayClientOSR::SetActive(bool active) {
111   active_ = active;
112   if (layered_window_updater_) {
113     layered_window_updater_->SetActive(active_);
114   }
115 }
116 
GetPixelMemory() const117 const void* CefHostDisplayClientOSR::GetPixelMemory() const {
118   return layered_window_updater_ ? layered_window_updater_->GetPixelMemory()
119                                  : nullptr;
120 }
121 
GetPixelSize() const122 gfx::Size CefHostDisplayClientOSR::GetPixelSize() const {
123   return layered_window_updater_ ? layered_window_updater_->GetPixelSize()
124                                  : gfx::Size{};
125 }
126 
UseProxyOutputDevice(UseProxyOutputDeviceCallback callback)127 void CefHostDisplayClientOSR::UseProxyOutputDevice(
128     UseProxyOutputDeviceCallback callback) {
129   std::move(callback).Run(true);
130 }
131 
CreateLayeredWindowUpdater(mojo::PendingReceiver<viz::mojom::LayeredWindowUpdater> receiver)132 void CefHostDisplayClientOSR::CreateLayeredWindowUpdater(
133     mojo::PendingReceiver<viz::mojom::LayeredWindowUpdater> receiver) {
134   layered_window_updater_ =
135       std::make_unique<CefLayeredWindowUpdaterOSR>(view_, std::move(receiver));
136   layered_window_updater_->SetActive(active_);
137 }
138 
139 #if BUILDFLAG(IS_LINUX)
DidCompleteSwapWithNewSize(const gfx::Size & size)140 void CefHostDisplayClientOSR::DidCompleteSwapWithNewSize(
141     const gfx::Size& size) {}
142 #endif
143