Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / renderer / gpu / compositor_output_surface.cc
blobc3298d03d5faf315b429cdcf71e96c232675985e
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 content {
28 CompositorOutputSurface::CompositorOutputSurface(
29 int32 routing_id,
30 uint32 output_surface_id,
31 const scoped_refptr<ContextProviderCommandBuffer>& context_provider,
32 const scoped_refptr<ContextProviderCommandBuffer>& worker_context_provider,
33 scoped_ptr<cc::SoftwareOutputDevice> software_device,
34 scoped_refptr<FrameSwapMessageQueue> swap_frame_message_queue,
35 bool use_swap_compositor_frame_message)
36 : OutputSurface(context_provider,
37 worker_context_provider,
38 software_device.Pass()),
39 output_surface_id_(output_surface_id),
40 use_swap_compositor_frame_message_(use_swap_compositor_frame_message),
41 output_surface_filter_(
42 RenderThreadImpl::current()->compositor_message_filter()),
43 frame_swap_message_queue_(swap_frame_message_queue),
44 routing_id_(routing_id),
45 #if defined(OS_ANDROID)
46 prefers_smoothness_(false),
47 main_thread_runner_(base::MessageLoop::current()->task_runner()),
48 #endif
49 layout_test_mode_(RenderThreadImpl::current()->layout_test_mode()),
50 weak_ptrs_(this) {
51 DCHECK(output_surface_filter_.get());
52 DCHECK(frame_swap_message_queue_.get());
53 DetachFromThread();
54 capabilities_.max_frames_pending = 1;
55 message_sender_ = RenderThreadImpl::current()->sync_message_filter();
56 DCHECK(message_sender_.get());
59 CompositorOutputSurface::~CompositorOutputSurface() {
60 DCHECK(CalledOnValidThread());
61 if (!HasClient())
62 return;
63 UpdateSmoothnessTakesPriority(false);
64 if (output_surface_proxy_.get())
65 output_surface_proxy_->ClearOutputSurface();
66 output_surface_filter_->RemoveHandlerOnCompositorThread(
67 routing_id_,
68 output_surface_filter_handler_);
71 bool CompositorOutputSurface::BindToClient(
72 cc::OutputSurfaceClient* client) {
73 DCHECK(CalledOnValidThread());
75 if (!cc::OutputSurface::BindToClient(client))
76 return false;
78 output_surface_proxy_ = new CompositorOutputSurfaceProxy(this);
79 output_surface_filter_handler_ =
80 base::Bind(&CompositorOutputSurfaceProxy::OnMessageReceived,
81 output_surface_proxy_);
82 output_surface_filter_->AddHandlerOnCompositorThread(
83 routing_id_,
84 output_surface_filter_handler_);
86 if (!context_provider()) {
87 // Without a GPU context, the memory policy otherwise wouldn't be set.
88 client->SetMemoryPolicy(cc::ManagedMemoryPolicy(
89 128 * 1024 * 1024,
90 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE,
91 base::SharedMemory::GetHandleLimit() / 3));
94 return true;
97 void CompositorOutputSurface::ShortcutSwapAck(
98 uint32 output_surface_id,
99 scoped_ptr<cc::GLFrameData> gl_frame_data) {
100 if (!layout_test_previous_frame_ack_) {
101 layout_test_previous_frame_ack_.reset(new cc::CompositorFrameAck);
102 layout_test_previous_frame_ack_->gl_frame_data.reset(new cc::GLFrameData);
105 OnSwapAck(output_surface_id, *layout_test_previous_frame_ack_);
107 layout_test_previous_frame_ack_->gl_frame_data = gl_frame_data.Pass();
110 void CompositorOutputSurface::SwapBuffers(cc::CompositorFrame* frame) {
111 DCHECK(use_swap_compositor_frame_message_);
112 if (layout_test_mode_) {
113 // This code path is here to support layout tests that are currently
114 // doing a readback in the renderer instead of the browser. So they
115 // are using deprecated code paths in the renderer and don't need to
116 // actually swap anything to the browser. We shortcut the swap to the
117 // browser here and just ack directly within the renderer process.
118 // Once crbug.com/311404 is fixed, this can be removed.
120 // This would indicate that crbug.com/311404 is being fixed, and this
121 // block needs to be removed.
122 DCHECK(!frame->delegated_frame_data);
124 base::Closure closure = base::Bind(
125 &CompositorOutputSurface::ShortcutSwapAck, weak_ptrs_.GetWeakPtr(),
126 output_surface_id_, base::Passed(&frame->gl_frame_data));
128 if (context_provider()) {
129 gpu::gles2::GLES2Interface* context = context_provider()->ContextGL();
130 context->Flush();
131 uint32 sync_point = context->InsertSyncPointCHROMIUM();
132 context_provider()->ContextSupport()->SignalSyncPoint(sync_point,
133 closure);
134 } else {
135 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, closure);
137 client_->DidSwapBuffers();
138 return;
139 } else {
141 ScopedVector<IPC::Message> messages;
142 std::vector<IPC::Message> messages_to_deliver_with_frame;
143 scoped_ptr<FrameSwapMessageQueue::SendMessageScope> send_message_scope =
144 frame_swap_message_queue_->AcquireSendMessageScope();
145 frame_swap_message_queue_->DrainMessages(&messages);
146 FrameSwapMessageQueue::TransferMessages(messages,
147 &messages_to_deliver_with_frame);
148 Send(new ViewHostMsg_SwapCompositorFrame(routing_id_,
149 output_surface_id_,
150 *frame,
151 messages_to_deliver_with_frame));
152 // ~send_message_scope.
154 client_->DidSwapBuffers();
158 void CompositorOutputSurface::OnMessageReceived(const IPC::Message& message) {
159 DCHECK(CalledOnValidThread());
160 if (!HasClient())
161 return;
162 IPC_BEGIN_MESSAGE_MAP(CompositorOutputSurface, message)
163 IPC_MESSAGE_HANDLER(ViewMsg_UpdateVSyncParameters,
164 OnUpdateVSyncParametersFromBrowser);
165 IPC_MESSAGE_HANDLER(ViewMsg_SwapCompositorFrameAck, OnSwapAck);
166 IPC_MESSAGE_HANDLER(ViewMsg_ReclaimCompositorResources, OnReclaimResources);
167 IPC_END_MESSAGE_MAP()
170 void CompositorOutputSurface::OnUpdateVSyncParametersFromBrowser(
171 base::TimeTicks timebase,
172 base::TimeDelta interval) {
173 DCHECK(CalledOnValidThread());
174 CommitVSyncParameters(timebase, interval);
177 void CompositorOutputSurface::OnSwapAck(uint32 output_surface_id,
178 const cc::CompositorFrameAck& ack) {
179 // Ignore message if it's a stale one coming from a different output surface
180 // (e.g. after a lost context).
181 if (output_surface_id != output_surface_id_)
182 return;
183 ReclaimResources(&ack);
184 client_->DidSwapBuffersComplete();
187 void CompositorOutputSurface::OnReclaimResources(
188 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);
197 bool CompositorOutputSurface::Send(IPC::Message* message) {
198 return message_sender_->Send(message);
201 #if defined(OS_ANDROID)
202 namespace {
203 void SetThreadPriorityToIdle() {
204 base::PlatformThread::SetCurrentThreadPriority(
205 base::ThreadPriority::BACKGROUND);
207 void SetThreadPriorityToDefault() {
208 base::PlatformThread::SetCurrentThreadPriority(base::ThreadPriority::NORMAL);
210 } // namespace
211 #endif
213 void CompositorOutputSurface::UpdateSmoothnessTakesPriority(
214 bool prefers_smoothness) {
215 #if defined(OS_ANDROID)
216 if (prefers_smoothness_ == prefers_smoothness)
217 return;
218 prefers_smoothness_ = prefers_smoothness;
219 if (prefers_smoothness) {
220 main_thread_runner_->PostTask(FROM_HERE,
221 base::Bind(&SetThreadPriorityToIdle));
222 } else {
223 main_thread_runner_->PostTask(FROM_HERE,
224 base::Bind(&SetThreadPriorityToDefault));
226 #endif
229 } // namespace content