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 void Resize(const gfx::Size
& pixel_size
, float scale_factor
) override
{
42 // Intentional no-op: canvas size is controlled by the embedder.
44 SkCanvas
* BeginPaint(const gfx::Rect
& damage_rect
) override
{
45 if (!surface_
->current_sw_canvas_
) {
46 NOTREACHED() << "BeginPaint with no canvas set";
49 LOG_IF(WARNING
, surface_
->frame_holder_
.get())
50 << "Mutliple calls to BeginPaint per frame";
51 return surface_
->current_sw_canvas_
;
53 void EndPaint(cc::SoftwareFrameData
* frame_data
) override
{}
54 void CopyToPixels(const gfx::Rect
& rect
, void* pixels
) override
{
59 SynchronousCompositorOutputSurface
* surface_
;
60 SkCanvas null_canvas_
;
62 DISALLOW_COPY_AND_ASSIGN(SoftwareDevice
);
65 SynchronousCompositorOutputSurface::SynchronousCompositorOutputSurface(
67 scoped_refptr
<FrameSwapMessageQueue
> frame_swap_message_queue
)
69 scoped_ptr
<cc::SoftwareOutputDevice
>(new SoftwareDevice(this))),
70 routing_id_(routing_id
),
72 current_sw_canvas_(nullptr),
74 frame_swap_message_queue_(frame_swap_message_queue
) {
75 capabilities_
.deferred_gl_initialization
= true;
76 capabilities_
.draw_and_swap_full_viewport_every_frame
= true;
77 capabilities_
.adjust_deadline_for_parent
= false;
78 capabilities_
.delegated_rendering
= true;
79 capabilities_
.max_frames_pending
= 1;
80 memory_policy_
.priority_cutoff_when_visible
=
81 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE
;
84 SynchronousCompositorOutputSurface::~SynchronousCompositorOutputSurface() {
85 DCHECK(CalledOnValidThread());
87 SynchronousCompositorRegistry::GetInstance()->UnregisterOutputSurface(
92 bool SynchronousCompositorOutputSurface::BindToClient(
93 cc::OutputSurfaceClient
* surface_client
) {
94 DCHECK(CalledOnValidThread());
95 if (!cc::OutputSurface::BindToClient(surface_client
))
98 client_
->SetMemoryPolicy(memory_policy_
);
100 SynchronousCompositorRegistry::GetInstance()->RegisterOutputSurface(
107 void SynchronousCompositorOutputSurface::SetCompositor(
108 SynchronousCompositorImpl
* compositor
) {
109 DCHECK(CalledOnValidThread());
110 compositor_
= compositor
;
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::Invalidate() {
129 DCHECK(CalledOnValidThread());
130 compositor_
->PostInvalidate();
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 scoped_refptr
<cc::ContextProvider
> worker_context_provider
) {
143 DCHECK(CalledOnValidThread());
145 DCHECK(!context_provider_
.get());
147 return InitializeAndSetContext3d(onscreen_context_provider
,
148 worker_context_provider
);
151 void SynchronousCompositorOutputSurface::ReleaseHwDraw() {
152 DCHECK(CalledOnValidThread());
153 cc::OutputSurface::ReleaseGL();
156 scoped_ptr
<cc::CompositorFrame
>
157 SynchronousCompositorOutputSurface::DemandDrawHw(
158 gfx::Size surface_size
,
159 const gfx::Transform
& transform
,
162 gfx::Rect viewport_rect_for_tile_priority
,
163 const gfx::Transform
& transform_for_tile_priority
) {
164 DCHECK(CalledOnValidThread());
166 DCHECK(context_provider_
.get());
168 surface_size_
= surface_size
;
169 InvokeComposite(transform
,
172 viewport_rect_for_tile_priority
,
173 transform_for_tile_priority
,
176 return frame_holder_
.Pass();
179 scoped_ptr
<cc::CompositorFrame
>
180 SynchronousCompositorOutputSurface::DemandDrawSw(SkCanvas
* canvas
) {
181 DCHECK(CalledOnValidThread());
183 DCHECK(!current_sw_canvas_
);
185 base::AutoReset
<SkCanvas
*> canvas_resetter(¤t_sw_canvas_
, canvas
);
188 canvas
->getClipDeviceBounds(&canvas_clip
);
189 gfx::Rect clip
= gfx::SkIRectToRect(canvas_clip
);
191 gfx::Transform
transform(gfx::Transform::kSkipInitialization
);
192 transform
.matrix() = canvas
->getTotalMatrix(); // Converts 3x3 matrix to 4x4.
194 surface_size_
= gfx::Size(canvas
->getDeviceSize().width(),
195 canvas
->getDeviceSize().height());
197 // Pass in the cached hw viewport and transform for tile priority to avoid
198 // tile thrashing when the WebView is alternating between hardware and
200 InvokeComposite(transform
,
203 cached_hw_viewport_rect_for_tile_priority_
,
204 cached_hw_transform_for_tile_priority_
,
207 return frame_holder_
.Pass();
210 void SynchronousCompositorOutputSurface::InvokeComposite(
211 const gfx::Transform
& transform
,
214 gfx::Rect viewport_rect_for_tile_priority
,
215 gfx::Transform transform_for_tile_priority
,
216 bool hardware_draw
) {
217 DCHECK(!frame_holder_
.get());
219 gfx::Transform adjusted_transform
= transform
;
220 AdjustTransform(&adjusted_transform
, viewport
);
221 SetExternalDrawConstraints(adjusted_transform
,
224 viewport_rect_for_tile_priority
,
225 transform_for_tile_priority
,
227 SetNeedsRedrawRect(gfx::Rect(viewport
.size()));
231 // After software draws (which might move the viewport arbitrarily), restore
232 // the previous hardware viewport to allow CC's tile manager to prioritize
235 cached_hw_transform_
= adjusted_transform
;
236 cached_hw_viewport_
= viewport
;
237 cached_hw_clip_
= clip
;
238 cached_hw_viewport_rect_for_tile_priority_
=
239 viewport_rect_for_tile_priority
;
240 cached_hw_transform_for_tile_priority_
= transform_for_tile_priority
;
242 bool resourceless_software_draw
= false;
243 SetExternalDrawConstraints(cached_hw_transform_
,
246 cached_hw_viewport_rect_for_tile_priority_
,
247 cached_hw_transform_for_tile_priority_
,
248 resourceless_software_draw
);
251 if (frame_holder_
.get())
252 client_
->DidSwapBuffersComplete();
255 void SynchronousCompositorOutputSurface::ReturnResources(
256 const cc::CompositorFrameAck
& frame_ack
) {
257 ReclaimResources(&frame_ack
);
260 void SynchronousCompositorOutputSurface::SetMemoryPolicy(size_t bytes_limit
) {
261 DCHECK(CalledOnValidThread());
262 memory_policy_
.bytes_limit_when_visible
= bytes_limit
;
263 memory_policy_
.num_resources_limit
= kNumResourcesLimit
;
266 client_
->SetMemoryPolicy(memory_policy_
);
269 void SynchronousCompositorOutputSurface::SetTreeActivationCallback(
270 const base::Closure
& callback
) {
272 client_
->SetTreeActivationCallback(callback
);
275 void SynchronousCompositorOutputSurface::GetMessagesToDeliver(
276 ScopedVector
<IPC::Message
>* messages
) {
277 DCHECK(CalledOnValidThread());
278 scoped_ptr
<FrameSwapMessageQueue::SendMessageScope
> send_message_scope
=
279 frame_swap_message_queue_
->AcquireSendMessageScope();
280 frame_swap_message_queue_
->DrainMessages(messages
);
283 // Not using base::NonThreadSafe as we want to enforce a more exacting threading
284 // requirement: SynchronousCompositorOutputSurface() must only be used on the UI
286 bool SynchronousCompositorOutputSurface::CalledOnValidThread() const {
287 return BrowserThread::CurrentlyOn(BrowserThread::UI
);
290 } // namespace content