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/gpu/frame_swap_message_queue.h"
19 #include "content/renderer/render_thread_impl.h"
20 #include "gpu/command_buffer/client/context_support.h"
21 #include "gpu/command_buffer/client/gles2_interface.h"
22 #include "ipc/ipc_forwarding_message_filter.h"
23 #include "ipc/ipc_sync_channel.h"
26 // There are several compositor surfaces in a process, but they share the same
27 // compositor thread, so we use a simple int here to track prefer-smoothness.
28 int g_prefer_smoothness_count = 0;
33 //------------------------------------------------------------------------------
36 IPC::ForwardingMessageFilter* CompositorOutputSurface::CreateFilter(
37 base::TaskRunner* target_task_runner)
39 uint32 messages_to_filter[] = {
40 ViewMsg_UpdateVSyncParameters::ID,
41 ViewMsg_SwapCompositorFrameAck::ID,
42 ViewMsg_ReclaimCompositorResources::ID,
43 #if defined(OS_ANDROID)
44 ViewMsg_BeginFrame::ID
48 return new IPC::ForwardingMessageFilter(
49 messages_to_filter, arraysize(messages_to_filter),
53 CompositorOutputSurface::CompositorOutputSurface(
55 uint32 output_surface_id,
56 const scoped_refptr<ContextProviderCommandBuffer>& context_provider,
57 scoped_ptr<cc::SoftwareOutputDevice> software_device,
58 scoped_refptr<FrameSwapMessageQueue> swap_frame_message_queue,
59 bool use_swap_compositor_frame_message)
60 : OutputSurface(context_provider, software_device.Pass()),
61 output_surface_id_(output_surface_id),
62 use_swap_compositor_frame_message_(use_swap_compositor_frame_message),
63 output_surface_filter_(
64 RenderThreadImpl::current()->compositor_output_surface_filter()),
65 frame_swap_message_queue_(swap_frame_message_queue),
66 routing_id_(routing_id),
67 prefers_smoothness_(false),
69 // TODO(epenner): Implement PlatformThread::CurrentHandle() on windows.
70 main_thread_handle_(base::PlatformThreadHandle()),
72 main_thread_handle_(base::PlatformThread::CurrentHandle()),
74 layout_test_mode_(RenderThreadImpl::current()->layout_test_mode()),
76 DCHECK(output_surface_filter_.get());
77 DCHECK(frame_swap_message_queue_.get());
79 message_sender_ = RenderThreadImpl::current()->sync_message_filter();
80 DCHECK(message_sender_.get());
81 if (OutputSurface::software_device())
82 capabilities_.max_frames_pending = 1;
85 CompositorOutputSurface::~CompositorOutputSurface() {
86 DCHECK(CalledOnValidThread());
87 SetNeedsBeginFrame(false);
90 UpdateSmoothnessTakesPriority(false);
91 if (output_surface_proxy_.get())
92 output_surface_proxy_->ClearOutputSurface();
93 output_surface_filter_->RemoveRoute(routing_id_);
96 bool CompositorOutputSurface::BindToClient(
97 cc::OutputSurfaceClient* client) {
98 DCHECK(CalledOnValidThread());
100 if (!cc::OutputSurface::BindToClient(client))
103 output_surface_proxy_ = new CompositorOutputSurfaceProxy(this);
104 output_surface_filter_->AddRoute(
106 base::Bind(&CompositorOutputSurfaceProxy::OnMessageReceived,
107 output_surface_proxy_));
109 if (!context_provider()) {
110 // Without a GPU context, the memory policy otherwise wouldn't be set.
111 client->SetMemoryPolicy(cc::ManagedMemoryPolicy(
113 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE,
114 base::SharedMemory::GetHandleLimit() / 3));
120 void CompositorOutputSurface::ShortcutSwapAck(
121 uint32 output_surface_id,
122 scoped_ptr<cc::GLFrameData> gl_frame_data,
123 scoped_ptr<cc::SoftwareFrameData> software_frame_data) {
124 if (!layout_test_previous_frame_ack_) {
125 layout_test_previous_frame_ack_.reset(new cc::CompositorFrameAck);
126 layout_test_previous_frame_ack_->gl_frame_data.reset(new cc::GLFrameData);
129 OnSwapAck(output_surface_id, *layout_test_previous_frame_ack_);
131 layout_test_previous_frame_ack_->gl_frame_data = gl_frame_data.Pass();
132 layout_test_previous_frame_ack_->last_software_frame_id =
133 software_frame_data ? software_frame_data->id : 0;
136 void CompositorOutputSurface::SwapBuffers(cc::CompositorFrame* frame) {
137 DCHECK(use_swap_compositor_frame_message_);
138 if (layout_test_mode_) {
139 // This code path is here to support layout tests that are currently
140 // doing a readback in the renderer instead of the browser. So they
141 // are using deprecated code paths in the renderer and don't need to
142 // actually swap anything to the browser. We shortcut the swap to the
143 // browser here and just ack directly within the renderer process.
144 // Once crbug.com/311404 is fixed, this can be removed.
146 // This would indicate that crbug.com/311404 is being fixed, and this
147 // block needs to be removed.
148 DCHECK(!frame->delegated_frame_data);
150 base::Closure closure =
151 base::Bind(&CompositorOutputSurface::ShortcutSwapAck,
152 weak_ptrs_.GetWeakPtr(),
154 base::Passed(&frame->gl_frame_data),
155 base::Passed(&frame->software_frame_data));
157 if (context_provider()) {
158 gpu::gles2::GLES2Interface* context = context_provider()->ContextGL();
160 uint32 sync_point = context->InsertSyncPointCHROMIUM();
161 context_provider()->ContextSupport()->SignalSyncPoint(sync_point,
164 base::MessageLoopProxy::current()->PostTask(FROM_HERE, closure);
166 client_->DidSwapBuffers();
170 ScopedVector<IPC::Message> messages;
171 std::vector<IPC::Message> messages_to_deliver_with_frame;
172 scoped_ptr<FrameSwapMessageQueue::SendMessageScope> send_message_scope =
173 frame_swap_message_queue_->AcquireSendMessageScope();
174 frame_swap_message_queue_->DrainMessages(&messages);
175 FrameSwapMessageQueue::TransferMessages(messages,
176 &messages_to_deliver_with_frame);
177 Send(new ViewHostMsg_SwapCompositorFrame(routing_id_,
180 messages_to_deliver_with_frame));
181 // ~send_message_scope.
183 client_->DidSwapBuffers();
187 void CompositorOutputSurface::OnMessageReceived(const IPC::Message& message) {
188 DCHECK(CalledOnValidThread());
191 IPC_BEGIN_MESSAGE_MAP(CompositorOutputSurface, message)
192 IPC_MESSAGE_HANDLER(ViewMsg_UpdateVSyncParameters,
193 OnUpdateVSyncParametersFromBrowser);
194 IPC_MESSAGE_HANDLER(ViewMsg_SwapCompositorFrameAck, OnSwapAck);
195 IPC_MESSAGE_HANDLER(ViewMsg_ReclaimCompositorResources, OnReclaimResources);
196 #if defined(OS_ANDROID)
197 IPC_MESSAGE_HANDLER(ViewMsg_BeginFrame, OnBeginFrame);
199 IPC_END_MESSAGE_MAP()
202 void CompositorOutputSurface::OnUpdateVSyncParametersFromBrowser(
203 base::TimeTicks timebase,
204 base::TimeDelta interval) {
205 DCHECK(CalledOnValidThread());
206 CommitVSyncParameters(timebase, interval);
209 #if defined(OS_ANDROID)
210 void CompositorOutputSurface::SetNeedsBeginFrame(bool enable) {
211 DCHECK(CalledOnValidThread());
212 Send(new ViewHostMsg_SetNeedsBeginFrame(routing_id_, enable));
215 void CompositorOutputSurface::OnBeginFrame(const cc::BeginFrameArgs& args) {
216 DCHECK(CalledOnValidThread());
217 client_->BeginFrame(args);
219 #endif // defined(OS_ANDROID)
221 void CompositorOutputSurface::OnSwapAck(uint32 output_surface_id,
222 const cc::CompositorFrameAck& ack) {
223 // Ignore message if it's a stale one coming from a different output surface
224 // (e.g. after a lost context).
225 if (output_surface_id != output_surface_id_)
227 ReclaimResources(&ack);
228 client_->DidSwapBuffersComplete();
231 void CompositorOutputSurface::OnReclaimResources(
232 uint32 output_surface_id,
233 const cc::CompositorFrameAck& ack) {
234 // Ignore message if it's a stale one coming from a different output surface
235 // (e.g. after a lost context).
236 if (output_surface_id != output_surface_id_)
238 ReclaimResources(&ack);
241 bool CompositorOutputSurface::Send(IPC::Message* message) {
242 return message_sender_->Send(message);
246 #if defined(OS_ANDROID)
247 void SetThreadPriorityToIdle(base::PlatformThreadHandle handle) {
248 base::PlatformThread::SetThreadPriority(
249 handle, base::kThreadPriority_Background);
251 void SetThreadPriorityToDefault(base::PlatformThreadHandle handle) {
252 base::PlatformThread::SetThreadPriority(
253 handle, base::kThreadPriority_Normal);
256 void SetThreadPriorityToIdle(base::PlatformThreadHandle handle) {}
257 void SetThreadPriorityToDefault(base::PlatformThreadHandle handle) {}
261 void CompositorOutputSurface::UpdateSmoothnessTakesPriority(
262 bool prefers_smoothness) {
264 // If we use different compositor threads, we need to
265 // use an atomic int to track prefer smoothness count.
266 base::PlatformThreadId g_last_thread = base::PlatformThread::CurrentId();
267 DCHECK_EQ(g_last_thread, base::PlatformThread::CurrentId());
269 if (prefers_smoothness_ == prefers_smoothness)
271 // If this is the first surface to start preferring smoothness,
272 // Throttle the main thread's priority.
273 if (prefers_smoothness_ == false &&
274 ++g_prefer_smoothness_count == 1) {
275 SetThreadPriorityToIdle(main_thread_handle_);
277 // If this is the last surface to stop preferring smoothness,
278 // Reset the main thread's priority to the default.
279 if (prefers_smoothness_ == true &&
280 --g_prefer_smoothness_count == 0) {
281 SetThreadPriorityToDefault(main_thread_handle_);
283 prefers_smoothness_ = prefers_smoothness;
286 } // namespace content