[ServiceWorker] Implement WebServiceWorkerContextClient::openWindow().
[chromium-blink-merge.git] / content / renderer / gpu / compositor_output_surface.cc
blob4e2874c053f8656f8903640aaef7480686aba412
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 scoped_ptr<cc::SoftwareOutputDevice> software_device,
37 scoped_refptr<FrameSwapMessageQueue> swap_frame_message_queue,
38 bool use_swap_compositor_frame_message)
39 : OutputSurface(context_provider, software_device.Pass()),
40 output_surface_id_(output_surface_id),
41 use_swap_compositor_frame_message_(use_swap_compositor_frame_message),
42 output_surface_filter_(
43 RenderThreadImpl::current()->compositor_message_filter()),
44 frame_swap_message_queue_(swap_frame_message_queue),
45 routing_id_(routing_id),
46 prefers_smoothness_(false),
47 #if defined(OS_WIN)
48 // TODO(epenner): Implement PlatformThread::CurrentHandle() on windows.
49 main_thread_handle_(base::PlatformThreadHandle()),
50 #else
51 main_thread_handle_(base::PlatformThread::CurrentHandle()),
52 #endif
53 layout_test_mode_(RenderThreadImpl::current()->layout_test_mode()),
54 weak_ptrs_(this) {
55 DCHECK(output_surface_filter_.get());
56 DCHECK(frame_swap_message_queue_.get());
57 DetachFromThread();
58 message_sender_ = RenderThreadImpl::current()->sync_message_filter();
59 DCHECK(message_sender_.get());
60 if (OutputSurface::software_device())
61 capabilities_.max_frames_pending = 1;
64 CompositorOutputSurface::~CompositorOutputSurface() {
65 DCHECK(CalledOnValidThread());
66 if (!HasClient())
67 return;
68 UpdateSmoothnessTakesPriority(false);
69 if (output_surface_proxy_.get())
70 output_surface_proxy_->ClearOutputSurface();
71 output_surface_filter_->RemoveHandlerOnCompositorThread(
72 routing_id_,
73 output_surface_filter_handler_);
76 bool CompositorOutputSurface::BindToClient(
77 cc::OutputSurfaceClient* client) {
78 DCHECK(CalledOnValidThread());
80 if (!cc::OutputSurface::BindToClient(client))
81 return false;
83 output_surface_proxy_ = new CompositorOutputSurfaceProxy(this);
84 output_surface_filter_handler_ =
85 base::Bind(&CompositorOutputSurfaceProxy::OnMessageReceived,
86 output_surface_proxy_);
87 output_surface_filter_->AddHandlerOnCompositorThread(
88 routing_id_,
89 output_surface_filter_handler_);
91 if (!context_provider()) {
92 // Without a GPU context, the memory policy otherwise wouldn't be set.
93 client->SetMemoryPolicy(cc::ManagedMemoryPolicy(
94 128 * 1024 * 1024,
95 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE,
96 base::SharedMemory::GetHandleLimit() / 3));
99 return true;
102 void CompositorOutputSurface::ShortcutSwapAck(
103 uint32 output_surface_id,
104 scoped_ptr<cc::GLFrameData> gl_frame_data,
105 scoped_ptr<cc::SoftwareFrameData> software_frame_data) {
106 if (!layout_test_previous_frame_ack_) {
107 layout_test_previous_frame_ack_.reset(new cc::CompositorFrameAck);
108 layout_test_previous_frame_ack_->gl_frame_data.reset(new cc::GLFrameData);
111 OnSwapAck(output_surface_id, *layout_test_previous_frame_ack_);
113 layout_test_previous_frame_ack_->gl_frame_data = gl_frame_data.Pass();
114 layout_test_previous_frame_ack_->last_software_frame_id =
115 software_frame_data ? software_frame_data->id : 0;
118 void CompositorOutputSurface::SwapBuffers(cc::CompositorFrame* frame) {
119 DCHECK(use_swap_compositor_frame_message_);
120 if (layout_test_mode_) {
121 // This code path is here to support layout tests that are currently
122 // doing a readback in the renderer instead of the browser. So they
123 // are using deprecated code paths in the renderer and don't need to
124 // actually swap anything to the browser. We shortcut the swap to the
125 // browser here and just ack directly within the renderer process.
126 // Once crbug.com/311404 is fixed, this can be removed.
128 // This would indicate that crbug.com/311404 is being fixed, and this
129 // block needs to be removed.
130 DCHECK(!frame->delegated_frame_data);
132 base::Closure closure =
133 base::Bind(&CompositorOutputSurface::ShortcutSwapAck,
134 weak_ptrs_.GetWeakPtr(),
135 output_surface_id_,
136 base::Passed(&frame->gl_frame_data),
137 base::Passed(&frame->software_frame_data));
139 if (context_provider()) {
140 gpu::gles2::GLES2Interface* context = context_provider()->ContextGL();
141 context->Flush();
142 uint32 sync_point = context->InsertSyncPointCHROMIUM();
143 context_provider()->ContextSupport()->SignalSyncPoint(sync_point,
144 closure);
145 } else {
146 base::MessageLoopProxy::current()->PostTask(FROM_HERE, closure);
148 client_->DidSwapBuffers();
149 return;
150 } else {
152 ScopedVector<IPC::Message> messages;
153 std::vector<IPC::Message> messages_to_deliver_with_frame;
154 scoped_ptr<FrameSwapMessageQueue::SendMessageScope> send_message_scope =
155 frame_swap_message_queue_->AcquireSendMessageScope();
156 frame_swap_message_queue_->DrainMessages(&messages);
157 FrameSwapMessageQueue::TransferMessages(messages,
158 &messages_to_deliver_with_frame);
159 Send(new ViewHostMsg_SwapCompositorFrame(routing_id_,
160 output_surface_id_,
161 *frame,
162 messages_to_deliver_with_frame));
163 // ~send_message_scope.
165 client_->DidSwapBuffers();
169 void CompositorOutputSurface::OnMessageReceived(const IPC::Message& message) {
170 DCHECK(CalledOnValidThread());
171 if (!HasClient())
172 return;
173 IPC_BEGIN_MESSAGE_MAP(CompositorOutputSurface, message)
174 IPC_MESSAGE_HANDLER(ViewMsg_UpdateVSyncParameters,
175 OnUpdateVSyncParametersFromBrowser);
176 IPC_MESSAGE_HANDLER(ViewMsg_SwapCompositorFrameAck, OnSwapAck);
177 IPC_MESSAGE_HANDLER(ViewMsg_ReclaimCompositorResources, OnReclaimResources);
178 IPC_END_MESSAGE_MAP()
181 void CompositorOutputSurface::OnUpdateVSyncParametersFromBrowser(
182 base::TimeTicks timebase,
183 base::TimeDelta interval) {
184 DCHECK(CalledOnValidThread());
185 CommitVSyncParameters(timebase, interval);
188 void CompositorOutputSurface::OnSwapAck(uint32 output_surface_id,
189 const cc::CompositorFrameAck& ack) {
190 // Ignore message if it's a stale one coming from a different output surface
191 // (e.g. after a lost context).
192 if (output_surface_id != output_surface_id_)
193 return;
194 ReclaimResources(&ack);
195 client_->DidSwapBuffersComplete();
198 void CompositorOutputSurface::OnReclaimResources(
199 uint32 output_surface_id,
200 const cc::CompositorFrameAck& ack) {
201 // Ignore message if it's a stale one coming from a different output surface
202 // (e.g. after a lost context).
203 if (output_surface_id != output_surface_id_)
204 return;
205 ReclaimResources(&ack);
208 bool CompositorOutputSurface::Send(IPC::Message* message) {
209 return message_sender_->Send(message);
212 namespace {
213 #if defined(OS_ANDROID)
214 void SetThreadPriorityToIdle(base::PlatformThreadHandle handle) {
215 base::PlatformThread::SetThreadPriority(
216 handle, base::kThreadPriority_Background);
218 void SetThreadPriorityToDefault(base::PlatformThreadHandle handle) {
219 base::PlatformThread::SetThreadPriority(
220 handle, base::kThreadPriority_Normal);
222 #else
223 void SetThreadPriorityToIdle(base::PlatformThreadHandle handle) {}
224 void SetThreadPriorityToDefault(base::PlatformThreadHandle handle) {}
225 #endif
228 void CompositorOutputSurface::UpdateSmoothnessTakesPriority(
229 bool prefers_smoothness) {
230 #ifndef NDEBUG
231 // If we use different compositor threads, we need to
232 // use an atomic int to track prefer smoothness count.
233 base::PlatformThreadId g_last_thread = base::PlatformThread::CurrentId();
234 DCHECK_EQ(g_last_thread, base::PlatformThread::CurrentId());
235 #endif
236 if (prefers_smoothness_ == prefers_smoothness)
237 return;
238 // If this is the first surface to start preferring smoothness,
239 // Throttle the main thread's priority.
240 if (prefers_smoothness_ == false &&
241 ++g_prefer_smoothness_count == 1) {
242 SetThreadPriorityToIdle(main_thread_handle_);
244 // If this is the last surface to stop preferring smoothness,
245 // Reset the main thread's priority to the default.
246 if (prefers_smoothness_ == true &&
247 --g_prefer_smoothness_count == 0) {
248 SetThreadPriorityToDefault(main_thread_handle_);
250 prefers_smoothness_ = prefers_smoothness;
253 } // namespace content