Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / gpu / compositor_output_surface.cc
blobbdeefe0a8fc98208ccd3b0deea0e018a61cb0c64
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_sync_channel.h"
24 namespace {
25 // There are several compositor surfaces in a process, but they share the same
26 // compositor thread, so we use a simple int here to track prefer-smoothness.
27 int g_prefer_smoothness_count = 0;
28 } // namespace
30 namespace content {
32 CompositorOutputSurface::CompositorOutputSurface(
33 int32 routing_id,
34 uint32 output_surface_id,
35 const scoped_refptr<ContextProviderCommandBuffer>& context_provider,
36 const scoped_refptr<ContextProviderCommandBuffer>& worker_context_provider,
37 scoped_ptr<cc::SoftwareOutputDevice> software_device,
38 scoped_refptr<FrameSwapMessageQueue> swap_frame_message_queue,
39 bool use_swap_compositor_frame_message)
40 : OutputSurface(context_provider,
41 worker_context_provider,
42 software_device.Pass()),
43 output_surface_id_(output_surface_id),
44 use_swap_compositor_frame_message_(use_swap_compositor_frame_message),
45 output_surface_filter_(
46 RenderThreadImpl::current()->compositor_message_filter()),
47 frame_swap_message_queue_(swap_frame_message_queue),
48 routing_id_(routing_id),
49 prefers_smoothness_(false),
50 #if defined(OS_WIN)
51 // TODO(epenner): Implement PlatformThread::CurrentHandle() on windows.
52 main_thread_handle_(base::PlatformThreadHandle()),
53 #else
54 main_thread_handle_(base::PlatformThread::CurrentHandle()),
55 #endif
56 layout_test_mode_(RenderThreadImpl::current()->layout_test_mode()),
57 weak_ptrs_(this) {
58 DCHECK(output_surface_filter_.get());
59 DCHECK(frame_swap_message_queue_.get());
60 DetachFromThread();
61 message_sender_ = RenderThreadImpl::current()->sync_message_filter();
62 DCHECK(message_sender_.get());
63 if (OutputSurface::software_device())
64 capabilities_.max_frames_pending = 1;
67 CompositorOutputSurface::~CompositorOutputSurface() {
68 DCHECK(CalledOnValidThread());
69 if (!HasClient())
70 return;
71 UpdateSmoothnessTakesPriority(false);
72 if (output_surface_proxy_.get())
73 output_surface_proxy_->ClearOutputSurface();
74 output_surface_filter_->RemoveHandlerOnCompositorThread(
75 routing_id_,
76 output_surface_filter_handler_);
79 bool CompositorOutputSurface::BindToClient(
80 cc::OutputSurfaceClient* client) {
81 DCHECK(CalledOnValidThread());
83 if (!cc::OutputSurface::BindToClient(client))
84 return false;
86 output_surface_proxy_ = new CompositorOutputSurfaceProxy(this);
87 output_surface_filter_handler_ =
88 base::Bind(&CompositorOutputSurfaceProxy::OnMessageReceived,
89 output_surface_proxy_);
90 output_surface_filter_->AddHandlerOnCompositorThread(
91 routing_id_,
92 output_surface_filter_handler_);
94 if (!context_provider()) {
95 // Without a GPU context, the memory policy otherwise wouldn't be set.
96 client->SetMemoryPolicy(cc::ManagedMemoryPolicy(
97 128 * 1024 * 1024,
98 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE,
99 base::SharedMemory::GetHandleLimit() / 3));
102 return true;
105 void CompositorOutputSurface::ShortcutSwapAck(
106 uint32 output_surface_id,
107 scoped_ptr<cc::GLFrameData> gl_frame_data,
108 scoped_ptr<cc::SoftwareFrameData> software_frame_data) {
109 if (!layout_test_previous_frame_ack_) {
110 layout_test_previous_frame_ack_.reset(new cc::CompositorFrameAck);
111 layout_test_previous_frame_ack_->gl_frame_data.reset(new cc::GLFrameData);
114 OnSwapAck(output_surface_id, *layout_test_previous_frame_ack_);
116 layout_test_previous_frame_ack_->gl_frame_data = gl_frame_data.Pass();
117 layout_test_previous_frame_ack_->last_software_frame_id =
118 software_frame_data ? software_frame_data->id : 0;
121 void CompositorOutputSurface::SwapBuffers(cc::CompositorFrame* frame) {
122 DCHECK(use_swap_compositor_frame_message_);
123 if (layout_test_mode_) {
124 // This code path is here to support layout tests that are currently
125 // doing a readback in the renderer instead of the browser. So they
126 // are using deprecated code paths in the renderer and don't need to
127 // actually swap anything to the browser. We shortcut the swap to the
128 // browser here and just ack directly within the renderer process.
129 // Once crbug.com/311404 is fixed, this can be removed.
131 // This would indicate that crbug.com/311404 is being fixed, and this
132 // block needs to be removed.
133 DCHECK(!frame->delegated_frame_data);
135 base::Closure closure =
136 base::Bind(&CompositorOutputSurface::ShortcutSwapAck,
137 weak_ptrs_.GetWeakPtr(),
138 output_surface_id_,
139 base::Passed(&frame->gl_frame_data),
140 base::Passed(&frame->software_frame_data));
142 if (context_provider()) {
143 gpu::gles2::GLES2Interface* context = context_provider()->ContextGL();
144 context->Flush();
145 uint32 sync_point = context->InsertSyncPointCHROMIUM();
146 context_provider()->ContextSupport()->SignalSyncPoint(sync_point,
147 closure);
148 } else {
149 base::MessageLoopProxy::current()->PostTask(FROM_HERE, closure);
151 client_->DidSwapBuffers();
152 return;
153 } else {
155 ScopedVector<IPC::Message> messages;
156 std::vector<IPC::Message> messages_to_deliver_with_frame;
157 scoped_ptr<FrameSwapMessageQueue::SendMessageScope> send_message_scope =
158 frame_swap_message_queue_->AcquireSendMessageScope();
159 frame_swap_message_queue_->DrainMessages(&messages);
160 FrameSwapMessageQueue::TransferMessages(messages,
161 &messages_to_deliver_with_frame);
162 Send(new ViewHostMsg_SwapCompositorFrame(routing_id_,
163 output_surface_id_,
164 *frame,
165 messages_to_deliver_with_frame));
166 // ~send_message_scope.
168 client_->DidSwapBuffers();
172 void CompositorOutputSurface::OnMessageReceived(const IPC::Message& message) {
173 DCHECK(CalledOnValidThread());
174 if (!HasClient())
175 return;
176 IPC_BEGIN_MESSAGE_MAP(CompositorOutputSurface, message)
177 IPC_MESSAGE_HANDLER(ViewMsg_UpdateVSyncParameters,
178 OnUpdateVSyncParametersFromBrowser);
179 IPC_MESSAGE_HANDLER(ViewMsg_SwapCompositorFrameAck, OnSwapAck);
180 IPC_MESSAGE_HANDLER(ViewMsg_ReclaimCompositorResources, OnReclaimResources);
181 IPC_END_MESSAGE_MAP()
184 void CompositorOutputSurface::OnUpdateVSyncParametersFromBrowser(
185 base::TimeTicks timebase,
186 base::TimeDelta interval) {
187 DCHECK(CalledOnValidThread());
188 CommitVSyncParameters(timebase, interval);
191 void CompositorOutputSurface::OnSwapAck(uint32 output_surface_id,
192 const cc::CompositorFrameAck& ack) {
193 // Ignore message if it's a stale one coming from a different output surface
194 // (e.g. after a lost context).
195 if (output_surface_id != output_surface_id_)
196 return;
197 ReclaimResources(&ack);
198 client_->DidSwapBuffersComplete();
201 void CompositorOutputSurface::OnReclaimResources(
202 uint32 output_surface_id,
203 const cc::CompositorFrameAck& ack) {
204 // Ignore message if it's a stale one coming from a different output surface
205 // (e.g. after a lost context).
206 if (output_surface_id != output_surface_id_)
207 return;
208 ReclaimResources(&ack);
211 bool CompositorOutputSurface::Send(IPC::Message* message) {
212 return message_sender_->Send(message);
215 namespace {
216 #if defined(OS_ANDROID)
217 void SetThreadPriorityToIdle(base::PlatformThreadHandle handle) {
218 base::PlatformThread::SetThreadPriority(handle,
219 base::ThreadPriority::BACKGROUND);
221 void SetThreadPriorityToDefault(base::PlatformThreadHandle handle) {
222 base::PlatformThread::SetThreadPriority(handle,
223 base::ThreadPriority::NORMAL);
225 #else
226 void SetThreadPriorityToIdle(base::PlatformThreadHandle handle) {}
227 void SetThreadPriorityToDefault(base::PlatformThreadHandle handle) {}
228 #endif
231 void CompositorOutputSurface::UpdateSmoothnessTakesPriority(
232 bool prefers_smoothness) {
233 #ifndef NDEBUG
234 // If we use different compositor threads, we need to
235 // use an atomic int to track prefer smoothness count.
236 base::PlatformThreadId g_last_thread = base::PlatformThread::CurrentId();
237 DCHECK_EQ(g_last_thread, base::PlatformThread::CurrentId());
238 #endif
239 if (prefers_smoothness_ == prefers_smoothness)
240 return;
241 // If this is the first surface to start preferring smoothness,
242 // Throttle the main thread's priority.
243 if (prefers_smoothness_ == false &&
244 ++g_prefer_smoothness_count == 1) {
245 SetThreadPriorityToIdle(main_thread_handle_);
247 // If this is the last surface to stop preferring smoothness,
248 // Reset the main thread's priority to the default.
249 if (prefers_smoothness_ == true &&
250 --g_prefer_smoothness_count == 0) {
251 SetThreadPriorityToDefault(main_thread_handle_);
253 prefers_smoothness_ = prefers_smoothness;
256 } // namespace content