Include all dupe types (event when value is zero) in scan stats.
[chromium-blink-merge.git] / content / renderer / gpu / compositor_output_surface.cc
blob3695a188705d7cc76a8db05ce5a918b1b2cc53e8
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/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "cc/output/compositor_frame.h"
12 #include "cc/output/compositor_frame_ack.h"
13 #include "cc/output/managed_memory_policy.h"
14 #include "cc/output/output_surface_client.h"
15 #include "content/common/gpu/client/command_buffer_proxy_impl.h"
16 #include "content/common/gpu/client/context_provider_command_buffer.h"
17 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
18 #include "content/common/view_messages.h"
19 #include "content/public/common/content_switches.h"
20 #include "content/renderer/gpu/frame_swap_message_queue.h"
21 #include "content/renderer/render_thread_impl.h"
22 #include "gpu/command_buffer/client/context_support.h"
23 #include "gpu/command_buffer/client/gles2_interface.h"
24 #include "ipc/ipc_sync_channel.h"
26 namespace {
27 // There are several compositor surfaces in a process, but they share the same
28 // compositor thread, so we use a simple int here to track prefer-smoothness.
29 int g_prefer_smoothness_count = 0;
30 } // namespace
32 namespace content {
34 CompositorOutputSurface::CompositorOutputSurface(
35 int32 routing_id,
36 uint32 output_surface_id,
37 const scoped_refptr<ContextProviderCommandBuffer>& context_provider,
38 const scoped_refptr<ContextProviderCommandBuffer>& worker_context_provider,
39 scoped_ptr<cc::SoftwareOutputDevice> software_device,
40 scoped_refptr<FrameSwapMessageQueue> swap_frame_message_queue,
41 bool use_swap_compositor_frame_message)
42 : OutputSurface(context_provider,
43 worker_context_provider,
44 software_device.Pass()),
45 output_surface_id_(output_surface_id),
46 use_swap_compositor_frame_message_(use_swap_compositor_frame_message),
47 output_surface_filter_(
48 RenderThreadImpl::current()->compositor_message_filter()),
49 frame_swap_message_queue_(swap_frame_message_queue),
50 routing_id_(routing_id),
51 prefers_smoothness_(false),
52 #if defined(OS_WIN)
53 // TODO(epenner): Implement PlatformThread::CurrentHandle() on windows.
54 main_thread_handle_(base::PlatformThreadHandle()),
55 #else
56 main_thread_handle_(base::PlatformThread::CurrentHandle()),
57 #endif
58 layout_test_mode_(RenderThreadImpl::current()->layout_test_mode()),
59 weak_ptrs_(this) {
60 DCHECK(output_surface_filter_.get());
61 DCHECK(frame_swap_message_queue_.get());
62 DetachFromThread();
63 message_sender_ = RenderThreadImpl::current()->sync_message_filter();
64 DCHECK(message_sender_.get());
65 if (OutputSurface::software_device())
66 capabilities_.max_frames_pending = 1;
69 CompositorOutputSurface::~CompositorOutputSurface() {
70 DCHECK(CalledOnValidThread());
71 if (!HasClient())
72 return;
73 UpdateSmoothnessTakesPriority(false);
74 if (output_surface_proxy_.get())
75 output_surface_proxy_->ClearOutputSurface();
76 output_surface_filter_->RemoveHandlerOnCompositorThread(
77 routing_id_,
78 output_surface_filter_handler_);
81 bool CompositorOutputSurface::BindToClient(
82 cc::OutputSurfaceClient* client) {
83 DCHECK(CalledOnValidThread());
85 if (!cc::OutputSurface::BindToClient(client))
86 return false;
88 output_surface_proxy_ = new CompositorOutputSurfaceProxy(this);
89 output_surface_filter_handler_ =
90 base::Bind(&CompositorOutputSurfaceProxy::OnMessageReceived,
91 output_surface_proxy_);
92 output_surface_filter_->AddHandlerOnCompositorThread(
93 routing_id_,
94 output_surface_filter_handler_);
96 if (!context_provider()) {
97 // Without a GPU context, the memory policy otherwise wouldn't be set.
98 client->SetMemoryPolicy(cc::ManagedMemoryPolicy(
99 128 * 1024 * 1024,
100 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE,
101 base::SharedMemory::GetHandleLimit() / 3));
104 return true;
107 void CompositorOutputSurface::ShortcutSwapAck(
108 uint32 output_surface_id,
109 scoped_ptr<cc::GLFrameData> gl_frame_data,
110 scoped_ptr<cc::SoftwareFrameData> software_frame_data) {
111 if (!layout_test_previous_frame_ack_) {
112 layout_test_previous_frame_ack_.reset(new cc::CompositorFrameAck);
113 layout_test_previous_frame_ack_->gl_frame_data.reset(new cc::GLFrameData);
116 OnSwapAck(output_surface_id, *layout_test_previous_frame_ack_);
118 layout_test_previous_frame_ack_->gl_frame_data = gl_frame_data.Pass();
119 layout_test_previous_frame_ack_->last_software_frame_id =
120 software_frame_data ? software_frame_data->id : 0;
123 void CompositorOutputSurface::SwapBuffers(cc::CompositorFrame* frame) {
124 DCHECK(use_swap_compositor_frame_message_);
125 if (layout_test_mode_) {
126 // This code path is here to support layout tests that are currently
127 // doing a readback in the renderer instead of the browser. So they
128 // are using deprecated code paths in the renderer and don't need to
129 // actually swap anything to the browser. We shortcut the swap to the
130 // browser here and just ack directly within the renderer process.
131 // Once crbug.com/311404 is fixed, this can be removed.
133 // This would indicate that crbug.com/311404 is being fixed, and this
134 // block needs to be removed.
135 DCHECK(!frame->delegated_frame_data);
137 base::Closure closure =
138 base::Bind(&CompositorOutputSurface::ShortcutSwapAck,
139 weak_ptrs_.GetWeakPtr(),
140 output_surface_id_,
141 base::Passed(&frame->gl_frame_data),
142 base::Passed(&frame->software_frame_data));
144 if (context_provider()) {
145 gpu::gles2::GLES2Interface* context = context_provider()->ContextGL();
146 context->Flush();
147 uint32 sync_point = context->InsertSyncPointCHROMIUM();
148 context_provider()->ContextSupport()->SignalSyncPoint(sync_point,
149 closure);
150 } else {
151 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, closure);
153 client_->DidSwapBuffers();
154 return;
155 } else {
157 ScopedVector<IPC::Message> messages;
158 std::vector<IPC::Message> messages_to_deliver_with_frame;
159 scoped_ptr<FrameSwapMessageQueue::SendMessageScope> send_message_scope =
160 frame_swap_message_queue_->AcquireSendMessageScope();
161 frame_swap_message_queue_->DrainMessages(&messages);
162 FrameSwapMessageQueue::TransferMessages(messages,
163 &messages_to_deliver_with_frame);
164 Send(new ViewHostMsg_SwapCompositorFrame(routing_id_,
165 output_surface_id_,
166 *frame,
167 messages_to_deliver_with_frame));
168 // ~send_message_scope.
170 client_->DidSwapBuffers();
174 void CompositorOutputSurface::OnMessageReceived(const IPC::Message& message) {
175 DCHECK(CalledOnValidThread());
176 if (!HasClient())
177 return;
178 IPC_BEGIN_MESSAGE_MAP(CompositorOutputSurface, message)
179 IPC_MESSAGE_HANDLER(ViewMsg_UpdateVSyncParameters,
180 OnUpdateVSyncParametersFromBrowser);
181 IPC_MESSAGE_HANDLER(ViewMsg_SwapCompositorFrameAck, OnSwapAck);
182 IPC_MESSAGE_HANDLER(ViewMsg_ReclaimCompositorResources, OnReclaimResources);
183 IPC_END_MESSAGE_MAP()
186 void CompositorOutputSurface::OnUpdateVSyncParametersFromBrowser(
187 base::TimeTicks timebase,
188 base::TimeDelta interval) {
189 DCHECK(CalledOnValidThread());
190 CommitVSyncParameters(timebase, interval);
193 void CompositorOutputSurface::OnSwapAck(uint32 output_surface_id,
194 const cc::CompositorFrameAck& ack) {
195 // Ignore message if it's a stale one coming from a different output surface
196 // (e.g. after a lost context).
197 if (output_surface_id != output_surface_id_)
198 return;
199 ReclaimResources(&ack);
200 client_->DidSwapBuffersComplete();
203 void CompositorOutputSurface::OnReclaimResources(
204 uint32 output_surface_id,
205 const cc::CompositorFrameAck& ack) {
206 // Ignore message if it's a stale one coming from a different output surface
207 // (e.g. after a lost context).
208 if (output_surface_id != output_surface_id_)
209 return;
210 ReclaimResources(&ack);
213 bool CompositorOutputSurface::Send(IPC::Message* message) {
214 return message_sender_->Send(message);
217 namespace {
218 #if defined(OS_ANDROID)
219 void SetThreadPriorityToIdle(base::PlatformThreadHandle handle) {
220 base::PlatformThread::SetThreadPriority(handle,
221 base::ThreadPriority::BACKGROUND);
223 void SetThreadPriorityToDefault(base::PlatformThreadHandle handle) {
224 base::PlatformThread::SetThreadPriority(handle,
225 base::ThreadPriority::NORMAL);
227 #else
228 void SetThreadPriorityToIdle(base::PlatformThreadHandle handle) {}
229 void SetThreadPriorityToDefault(base::PlatformThreadHandle handle) {}
230 #endif
233 void CompositorOutputSurface::UpdateSmoothnessTakesPriority(
234 bool prefers_smoothness) {
235 #ifndef NDEBUG
236 // If we use different compositor threads, we need to
237 // use an atomic int to track prefer smoothness count.
238 base::PlatformThreadId g_last_thread = base::PlatformThread::CurrentId();
239 DCHECK_EQ(g_last_thread, base::PlatformThread::CurrentId());
240 #endif
241 if (prefers_smoothness_ == prefers_smoothness)
242 return;
243 // If this is the first surface to start preferring smoothness,
244 // Throttle the main thread's priority.
245 if (prefers_smoothness_ == false &&
246 ++g_prefer_smoothness_count == 1) {
247 SetThreadPriorityToIdle(main_thread_handle_);
249 // If this is the last surface to stop preferring smoothness,
250 // Reset the main thread's priority to the default.
251 if (prefers_smoothness_ == true &&
252 --g_prefer_smoothness_count == 0) {
253 SetThreadPriorityToDefault(main_thread_handle_);
255 prefers_smoothness_ = prefers_smoothness;
258 } // namespace content