1 // Copyright (c) 2012 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/renderer/gpu/compositor_output_surface.h"
7 #include "base/command_line.h"
8 #include "base/message_loop/message_loop_proxy.h"
9 #include "cc/output/compositor_frame.h"
10 #include "cc/output/compositor_frame_ack.h"
11 #include "cc/output/managed_memory_policy.h"
12 #include "cc/output/output_surface_client.h"
13 #include "content/common/gpu/client/command_buffer_proxy_impl.h"
14 #include "content/common/gpu/client/context_provider_command_buffer.h"
15 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
16 #include "content/common/view_messages.h"
17 #include "content/public/common/content_switches.h"
18 #include "content/renderer/render_thread_impl.h"
19 #include "gpu/command_buffer/client/gles2_interface.h"
20 #include "ipc/ipc_forwarding_message_filter.h"
21 #include "ipc/ipc_sync_channel.h"
24 // There are several compositor surfaces in a process, but they share the same
25 // compositor thread, so we use a simple int here to track prefer-smoothness.
26 int g_prefer_smoothness_count
= 0;
31 //------------------------------------------------------------------------------
34 IPC::ForwardingMessageFilter
* CompositorOutputSurface::CreateFilter(
35 base::TaskRunner
* target_task_runner
)
37 uint32 messages_to_filter
[] = {
38 ViewMsg_UpdateVSyncParameters::ID
,
39 ViewMsg_SwapCompositorFrameAck::ID
,
40 ViewMsg_ReclaimCompositorResources::ID
,
41 #if defined(OS_ANDROID)
42 ViewMsg_BeginFrame::ID
46 return new IPC::ForwardingMessageFilter(
47 messages_to_filter
, arraysize(messages_to_filter
),
51 CompositorOutputSurface::CompositorOutputSurface(
53 uint32 output_surface_id
,
54 const scoped_refptr
<ContextProviderCommandBuffer
>& context_provider
,
55 scoped_ptr
<cc::SoftwareOutputDevice
> software_device
,
56 bool use_swap_compositor_frame_message
)
57 : OutputSurface(context_provider
, software_device
.Pass()),
58 output_surface_id_(output_surface_id
),
59 use_swap_compositor_frame_message_(use_swap_compositor_frame_message
),
60 output_surface_filter_(
61 RenderThreadImpl::current()->compositor_output_surface_filter()),
62 routing_id_(routing_id
),
63 prefers_smoothness_(false),
65 // TODO(epenner): Implement PlatformThread::CurrentHandle() on windows.
66 main_thread_handle_(base::PlatformThreadHandle())
68 main_thread_handle_(base::PlatformThread::CurrentHandle())
71 DCHECK(output_surface_filter_
.get());
73 message_sender_
= RenderThreadImpl::current()->sync_message_filter();
74 DCHECK(message_sender_
.get());
75 if (OutputSurface::software_device())
76 capabilities_
.max_frames_pending
= 1;
79 CompositorOutputSurface::~CompositorOutputSurface() {
80 DCHECK(CalledOnValidThread());
81 SetNeedsBeginImplFrame(false);
84 UpdateSmoothnessTakesPriority(false);
85 if (output_surface_proxy_
.get())
86 output_surface_proxy_
->ClearOutputSurface();
87 output_surface_filter_
->RemoveRoute(routing_id_
);
90 bool CompositorOutputSurface::BindToClient(
91 cc::OutputSurfaceClient
* client
) {
92 DCHECK(CalledOnValidThread());
94 if (!cc::OutputSurface::BindToClient(client
))
97 output_surface_proxy_
= new CompositorOutputSurfaceProxy(this);
98 output_surface_filter_
->AddRoute(
100 base::Bind(&CompositorOutputSurfaceProxy::OnMessageReceived
,
101 output_surface_proxy_
));
103 if (!context_provider()) {
104 // Without a GPU context, the memory policy otherwise wouldn't be set.
105 client
->SetMemoryPolicy(cc::ManagedMemoryPolicy(
107 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE
,
108 cc::ManagedMemoryPolicy::kDefaultNumResourcesLimit
));
114 void CompositorOutputSurface::SwapBuffers(cc::CompositorFrame
* frame
) {
115 if (use_swap_compositor_frame_message_
) {
116 Send(new ViewHostMsg_SwapCompositorFrame(routing_id_
,
123 if (frame
->gl_frame_data
) {
124 ContextProviderCommandBuffer
* provider_command_buffer
=
125 static_cast<ContextProviderCommandBuffer
*>(context_provider_
.get());
126 CommandBufferProxyImpl
* command_buffer_proxy
=
127 provider_command_buffer
->GetCommandBufferProxy();
128 DCHECK(command_buffer_proxy
);
129 context_provider_
->ContextGL()->ShallowFlushCHROMIUM();
130 command_buffer_proxy
->SetLatencyInfo(frame
->metadata
.latency_info
);
133 OutputSurface::SwapBuffers(frame
);
136 void CompositorOutputSurface::OnMessageReceived(const IPC::Message
& message
) {
137 DCHECK(CalledOnValidThread());
140 IPC_BEGIN_MESSAGE_MAP(CompositorOutputSurface
, message
)
141 IPC_MESSAGE_HANDLER(ViewMsg_UpdateVSyncParameters
, OnUpdateVSyncParameters
);
142 IPC_MESSAGE_HANDLER(ViewMsg_SwapCompositorFrameAck
, OnSwapAck
);
143 IPC_MESSAGE_HANDLER(ViewMsg_ReclaimCompositorResources
, OnReclaimResources
);
144 #if defined(OS_ANDROID)
145 IPC_MESSAGE_HANDLER(ViewMsg_BeginFrame
, OnBeginImplFrame
);
147 IPC_END_MESSAGE_MAP()
150 void CompositorOutputSurface::OnUpdateVSyncParameters(
151 base::TimeTicks timebase
, base::TimeDelta interval
) {
152 DCHECK(CalledOnValidThread());
153 OnVSyncParametersChanged(timebase
, interval
);
156 #if defined(OS_ANDROID)
157 void CompositorOutputSurface::SetNeedsBeginImplFrame(bool enable
) {
158 DCHECK(CalledOnValidThread());
159 if (needs_begin_impl_frame_
!= enable
)
160 Send(new ViewHostMsg_SetNeedsBeginFrame(routing_id_
, enable
));
161 OutputSurface::SetNeedsBeginImplFrame(enable
);
164 void CompositorOutputSurface::OnBeginImplFrame(const cc::BeginFrameArgs
& args
) {
165 DCHECK(CalledOnValidThread());
166 BeginImplFrame(args
);
168 #endif // defined(OS_ANDROID)
170 void CompositorOutputSurface::OnSwapAck(uint32 output_surface_id
,
171 const cc::CompositorFrameAck
& ack
) {
172 // Ignore message if it's a stale one coming from a different output surface
173 // (e.g. after a lost context).
174 if (output_surface_id
!= output_surface_id_
)
176 ReclaimResources(&ack
);
177 OnSwapBuffersComplete();
180 void CompositorOutputSurface::OnReclaimResources(
181 uint32 output_surface_id
,
182 const cc::CompositorFrameAck
& ack
) {
183 // Ignore message if it's a stale one coming from a different output surface
184 // (e.g. after a lost context).
185 if (output_surface_id
!= output_surface_id_
)
187 ReclaimResources(&ack
);
190 bool CompositorOutputSurface::Send(IPC::Message
* message
) {
191 return message_sender_
->Send(message
);
195 #if defined(OS_ANDROID)
196 void SetThreadPriorityToIdle(base::PlatformThreadHandle handle
) {
197 base::PlatformThread::SetThreadPriority(
198 handle
, base::kThreadPriority_Background
);
200 void SetThreadPriorityToDefault(base::PlatformThreadHandle handle
) {
201 base::PlatformThread::SetThreadPriority(
202 handle
, base::kThreadPriority_Normal
);
205 void SetThreadPriorityToIdle(base::PlatformThreadHandle handle
) {}
206 void SetThreadPriorityToDefault(base::PlatformThreadHandle handle
) {}
210 void CompositorOutputSurface::UpdateSmoothnessTakesPriority(
211 bool prefers_smoothness
) {
213 // If we use different compositor threads, we need to
214 // use an atomic int to track prefer smoothness count.
215 base::PlatformThreadId g_last_thread
= base::PlatformThread::CurrentId();
216 DCHECK_EQ(g_last_thread
, base::PlatformThread::CurrentId());
218 if (prefers_smoothness_
== prefers_smoothness
)
220 // If this is the first surface to start preferring smoothness,
221 // Throttle the main thread's priority.
222 if (prefers_smoothness_
== false &&
223 ++g_prefer_smoothness_count
== 1) {
224 SetThreadPriorityToIdle(main_thread_handle_
);
226 // If this is the last surface to stop preferring smoothness,
227 // Reset the main thread's priority to the default.
228 if (prefers_smoothness_
== true &&
229 --g_prefer_smoothness_count
== 0) {
230 SetThreadPriorityToDefault(main_thread_handle_
);
232 prefers_smoothness_
= prefers_smoothness
;
235 } // namespace content