1 // Copyright 2013 The Chromium 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.
5 #include "content/browser/android/in_process/synchronous_compositor_output_surface.h"
7 #include "base/auto_reset.h"
8 #include "base/logging.h"
9 #include "cc/output/compositor_frame.h"
10 #include "cc/output/context_provider.h"
11 #include "cc/output/output_surface_client.h"
12 #include "cc/output/software_output_device.h"
13 #include "content/browser/android/in_process/synchronous_compositor_external_begin_frame_source.h"
14 #include "content/browser/android/in_process/synchronous_compositor_impl.h"
15 #include "content/browser/android/in_process/synchronous_compositor_registry.h"
16 #include "content/browser/gpu/compositor_util.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/renderer/gpu/frame_swap_message_queue.h"
19 #include "gpu/command_buffer/client/gles2_interface.h"
20 #include "gpu/command_buffer/common/gpu_memory_allocation.h"
21 #include "third_party/skia/include/core/SkCanvas.h"
22 #include "ui/gfx/geometry/rect_conversions.h"
23 #include "ui/gfx/skia_util.h"
24 #include "ui/gfx/transform.h"
30 // Do not limit number of resources, so use an unrealistically high value.
31 const size_t kNumResourcesLimit
= 10 * 1000 * 1000;
35 class SynchronousCompositorOutputSurface::SoftwareDevice
36 : public cc::SoftwareOutputDevice
{
38 SoftwareDevice(SynchronousCompositorOutputSurface
* surface
)
41 virtual void Resize(const gfx::Size
& pixel_size
,
42 float scale_factor
) override
{
43 // Intentional no-op: canvas size is controlled by the embedder.
45 virtual SkCanvas
* BeginPaint(const gfx::Rect
& damage_rect
) override
{
46 if (!surface_
->current_sw_canvas_
) {
47 NOTREACHED() << "BeginPaint with no canvas set";
50 LOG_IF(WARNING
, surface_
->frame_holder_
.get())
51 << "Mutliple calls to BeginPaint per frame";
52 return surface_
->current_sw_canvas_
;
54 virtual void EndPaint(cc::SoftwareFrameData
* frame_data
) override
{
56 virtual void CopyToPixels(const gfx::Rect
& rect
, void* pixels
) override
{
61 SynchronousCompositorOutputSurface
* surface_
;
62 SkCanvas null_canvas_
;
64 DISALLOW_COPY_AND_ASSIGN(SoftwareDevice
);
67 SynchronousCompositorOutputSurface::SynchronousCompositorOutputSurface(
69 scoped_refptr
<FrameSwapMessageQueue
> frame_swap_message_queue
)
71 scoped_ptr
<cc::SoftwareOutputDevice
>(new SoftwareDevice(this))),
72 routing_id_(routing_id
),
74 current_sw_canvas_(nullptr),
76 output_surface_client_(nullptr),
77 frame_swap_message_queue_(frame_swap_message_queue
),
78 begin_frame_source_(nullptr) {
79 capabilities_
.deferred_gl_initialization
= true;
80 capabilities_
.draw_and_swap_full_viewport_every_frame
= true;
81 capabilities_
.adjust_deadline_for_parent
= false;
82 capabilities_
.delegated_rendering
= true;
83 capabilities_
.max_frames_pending
= 1;
84 memory_policy_
.priority_cutoff_when_visible
=
85 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE
;
88 SynchronousCompositorOutputSurface::~SynchronousCompositorOutputSurface() {
89 DCHECK(CalledOnValidThread());
91 SynchronousCompositorRegistry::GetInstance()->UnregisterOutputSurface(
94 DCHECK(!begin_frame_source_
);
97 bool SynchronousCompositorOutputSurface::BindToClient(
98 cc::OutputSurfaceClient
* surface_client
) {
99 DCHECK(CalledOnValidThread());
100 if (!cc::OutputSurface::BindToClient(surface_client
))
103 output_surface_client_
= surface_client
;
104 output_surface_client_
->SetMemoryPolicy(memory_policy_
);
106 SynchronousCompositorRegistry::GetInstance()->RegisterOutputSurface(
113 void SynchronousCompositorOutputSurface::Reshape(
114 const gfx::Size
& size
, float scale_factor
) {
115 // Intentional no-op: surface size is controlled by the embedder.
118 void SynchronousCompositorOutputSurface::SwapBuffers(
119 cc::CompositorFrame
* frame
) {
120 DCHECK(CalledOnValidThread());
122 frame_holder_
.reset(new cc::CompositorFrame
);
123 frame
->AssignTo(frame_holder_
.get());
125 client_
->DidSwapBuffers();
128 void SynchronousCompositorOutputSurface::SetBeginFrameSource(
129 SynchronousCompositorExternalBeginFrameSource
* begin_frame_source
) {
130 begin_frame_source_
= begin_frame_source
;
134 void AdjustTransform(gfx::Transform
* transform
, gfx::Rect viewport
) {
135 // CC's draw origin starts at the viewport.
136 transform
->matrix().postTranslate(-viewport
.x(), -viewport
.y(), 0);
140 bool SynchronousCompositorOutputSurface::InitializeHwDraw(
141 scoped_refptr
<cc::ContextProvider
> onscreen_context_provider
) {
142 DCHECK(CalledOnValidThread());
144 DCHECK(!context_provider_
.get());
146 return InitializeAndSetContext3d(onscreen_context_provider
);
149 void SynchronousCompositorOutputSurface::ReleaseHwDraw() {
150 DCHECK(CalledOnValidThread());
151 cc::OutputSurface::ReleaseGL();
154 scoped_ptr
<cc::CompositorFrame
>
155 SynchronousCompositorOutputSurface::DemandDrawHw(
156 gfx::Size surface_size
,
157 const gfx::Transform
& transform
,
160 gfx::Rect viewport_rect_for_tile_priority
,
161 const gfx::Transform
& transform_for_tile_priority
) {
162 DCHECK(CalledOnValidThread());
164 DCHECK(context_provider_
.get());
166 surface_size_
= surface_size
;
167 InvokeComposite(transform
,
170 viewport_rect_for_tile_priority
,
171 transform_for_tile_priority
,
174 return frame_holder_
.Pass();
177 scoped_ptr
<cc::CompositorFrame
>
178 SynchronousCompositorOutputSurface::DemandDrawSw(SkCanvas
* canvas
) {
179 DCHECK(CalledOnValidThread());
181 DCHECK(!current_sw_canvas_
);
182 base::AutoReset
<SkCanvas
*> canvas_resetter(¤t_sw_canvas_
, canvas
);
185 canvas
->getClipDeviceBounds(&canvas_clip
);
186 gfx::Rect clip
= gfx::SkIRectToRect(canvas_clip
);
188 gfx::Transform
transform(gfx::Transform::kSkipInitialization
);
189 transform
.matrix() = canvas
->getTotalMatrix(); // Converts 3x3 matrix to 4x4.
191 surface_size_
= gfx::Size(canvas
->getDeviceSize().width(),
192 canvas
->getDeviceSize().height());
194 // Pass in the cached hw viewport and transform for tile priority to avoid
195 // tile thrashing when the WebView is alternating between hardware and
197 InvokeComposite(transform
,
200 cached_hw_viewport_rect_for_tile_priority_
,
201 cached_hw_transform_for_tile_priority_
,
204 return frame_holder_
.Pass();
207 void SynchronousCompositorOutputSurface::InvokeComposite(
208 const gfx::Transform
& transform
,
211 gfx::Rect viewport_rect_for_tile_priority
,
212 gfx::Transform transform_for_tile_priority
,
213 bool hardware_draw
) {
214 DCHECK(!frame_holder_
.get());
215 DCHECK(begin_frame_source_
);
217 gfx::Transform adjusted_transform
= transform
;
218 AdjustTransform(&adjusted_transform
, viewport
);
219 SetExternalDrawConstraints(adjusted_transform
,
222 viewport_rect_for_tile_priority
,
223 transform_for_tile_priority
,
225 SetNeedsRedrawRect(gfx::Rect(viewport
.size()));
227 begin_frame_source_
->BeginFrame();
229 // After software draws (which might move the viewport arbitrarily), restore
230 // the previous hardware viewport to allow CC's tile manager to prioritize
233 cached_hw_transform_
= adjusted_transform
;
234 cached_hw_viewport_
= viewport
;
235 cached_hw_clip_
= clip
;
236 cached_hw_viewport_rect_for_tile_priority_
=
237 viewport_rect_for_tile_priority
;
238 cached_hw_transform_for_tile_priority_
= transform_for_tile_priority
;
240 bool resourceless_software_draw
= false;
241 SetExternalDrawConstraints(cached_hw_transform_
,
244 cached_hw_viewport_rect_for_tile_priority_
,
245 cached_hw_transform_for_tile_priority_
,
246 resourceless_software_draw
);
249 if (frame_holder_
.get())
250 client_
->DidSwapBuffersComplete();
253 void SynchronousCompositorOutputSurface::ReturnResources(
254 const cc::CompositorFrameAck
& frame_ack
) {
255 ReclaimResources(&frame_ack
);
258 void SynchronousCompositorOutputSurface::SetMemoryPolicy(size_t bytes_limit
) {
259 DCHECK(CalledOnValidThread());
260 memory_policy_
.bytes_limit_when_visible
= bytes_limit
;
261 memory_policy_
.num_resources_limit
= kNumResourcesLimit
;
263 if (output_surface_client_
)
264 output_surface_client_
->SetMemoryPolicy(memory_policy_
);
267 void SynchronousCompositorOutputSurface::SetTreeActivationCallback(
268 const base::Closure
& callback
) {
270 client_
->SetTreeActivationCallback(callback
);
273 void SynchronousCompositorOutputSurface::GetMessagesToDeliver(
274 ScopedVector
<IPC::Message
>* messages
) {
275 DCHECK(CalledOnValidThread());
276 scoped_ptr
<FrameSwapMessageQueue::SendMessageScope
> send_message_scope
=
277 frame_swap_message_queue_
->AcquireSendMessageScope();
278 frame_swap_message_queue_
->DrainMessages(messages
);
281 // Not using base::NonThreadSafe as we want to enforce a more exacting threading
282 // requirement: SynchronousCompositorOutputSurface() must only be used on the UI
284 bool SynchronousCompositorOutputSurface::CalledOnValidThread() const {
285 return BrowserThread::CurrentlyOn(BrowserThread::UI
);
288 } // namespace content