Remove aura enum from DesktopMediaID to fix desktop mirroring audio (CrOS).
[chromium-blink-merge.git] / content / renderer / gpu / compositor_output_surface.cc
blobd1ad1cc2a2ed8348416bc1610a1f6106add0dbc2
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 scoped_ptr<cc::SoftwareFrameData> software_frame_data) {
101 if (!layout_test_previous_frame_ack_) {
102 layout_test_previous_frame_ack_.reset(new cc::CompositorFrameAck);
103 layout_test_previous_frame_ack_->gl_frame_data.reset(new cc::GLFrameData);
106 OnSwapAck(output_surface_id, *layout_test_previous_frame_ack_);
108 layout_test_previous_frame_ack_->gl_frame_data = gl_frame_data.Pass();
109 layout_test_previous_frame_ack_->last_software_frame_id =
110 software_frame_data ? software_frame_data->id : 0;
113 void CompositorOutputSurface::SwapBuffers(cc::CompositorFrame* frame) {
114 DCHECK(use_swap_compositor_frame_message_);
115 if (layout_test_mode_) {
116 // This code path is here to support layout tests that are currently
117 // doing a readback in the renderer instead of the browser. So they
118 // are using deprecated code paths in the renderer and don't need to
119 // actually swap anything to the browser. We shortcut the swap to the
120 // browser here and just ack directly within the renderer process.
121 // Once crbug.com/311404 is fixed, this can be removed.
123 // This would indicate that crbug.com/311404 is being fixed, and this
124 // block needs to be removed.
125 DCHECK(!frame->delegated_frame_data);
127 base::Closure closure =
128 base::Bind(&CompositorOutputSurface::ShortcutSwapAck,
129 weak_ptrs_.GetWeakPtr(),
130 output_surface_id_,
131 base::Passed(&frame->gl_frame_data),
132 base::Passed(&frame->software_frame_data));
134 if (context_provider()) {
135 gpu::gles2::GLES2Interface* context = context_provider()->ContextGL();
136 context->Flush();
137 uint32 sync_point = context->InsertSyncPointCHROMIUM();
138 context_provider()->ContextSupport()->SignalSyncPoint(sync_point,
139 closure);
140 } else {
141 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, closure);
143 client_->DidSwapBuffers();
144 return;
145 } else {
147 ScopedVector<IPC::Message> messages;
148 std::vector<IPC::Message> messages_to_deliver_with_frame;
149 scoped_ptr<FrameSwapMessageQueue::SendMessageScope> send_message_scope =
150 frame_swap_message_queue_->AcquireSendMessageScope();
151 frame_swap_message_queue_->DrainMessages(&messages);
152 FrameSwapMessageQueue::TransferMessages(messages,
153 &messages_to_deliver_with_frame);
154 Send(new ViewHostMsg_SwapCompositorFrame(routing_id_,
155 output_surface_id_,
156 *frame,
157 messages_to_deliver_with_frame));
158 // ~send_message_scope.
160 client_->DidSwapBuffers();
164 void CompositorOutputSurface::OnMessageReceived(const IPC::Message& message) {
165 DCHECK(CalledOnValidThread());
166 if (!HasClient())
167 return;
168 IPC_BEGIN_MESSAGE_MAP(CompositorOutputSurface, message)
169 IPC_MESSAGE_HANDLER(ViewMsg_UpdateVSyncParameters,
170 OnUpdateVSyncParametersFromBrowser);
171 IPC_MESSAGE_HANDLER(ViewMsg_SwapCompositorFrameAck, OnSwapAck);
172 IPC_MESSAGE_HANDLER(ViewMsg_ReclaimCompositorResources, OnReclaimResources);
173 IPC_END_MESSAGE_MAP()
176 void CompositorOutputSurface::OnUpdateVSyncParametersFromBrowser(
177 base::TimeTicks timebase,
178 base::TimeDelta interval) {
179 DCHECK(CalledOnValidThread());
180 CommitVSyncParameters(timebase, interval);
183 void CompositorOutputSurface::OnSwapAck(uint32 output_surface_id,
184 const cc::CompositorFrameAck& ack) {
185 // Ignore message if it's a stale one coming from a different output surface
186 // (e.g. after a lost context).
187 if (output_surface_id != output_surface_id_)
188 return;
189 ReclaimResources(&ack);
190 client_->DidSwapBuffersComplete();
193 void CompositorOutputSurface::OnReclaimResources(
194 uint32 output_surface_id,
195 const cc::CompositorFrameAck& ack) {
196 // Ignore message if it's a stale one coming from a different output surface
197 // (e.g. after a lost context).
198 if (output_surface_id != output_surface_id_)
199 return;
200 ReclaimResources(&ack);
203 bool CompositorOutputSurface::Send(IPC::Message* message) {
204 return message_sender_->Send(message);
207 #if defined(OS_ANDROID)
208 namespace {
209 void SetThreadPriorityToIdle() {
210 base::PlatformThread::SetCurrentThreadPriority(
211 base::ThreadPriority::BACKGROUND);
213 void SetThreadPriorityToDefault() {
214 base::PlatformThread::SetCurrentThreadPriority(base::ThreadPriority::NORMAL);
216 } // namespace
217 #endif
219 void CompositorOutputSurface::UpdateSmoothnessTakesPriority(
220 bool prefers_smoothness) {
221 #if defined(OS_ANDROID)
222 if (prefers_smoothness_ == prefers_smoothness)
223 return;
224 prefers_smoothness_ = prefers_smoothness;
225 if (prefers_smoothness) {
226 main_thread_runner_->PostTask(FROM_HERE,
227 base::Bind(&SetThreadPriorityToIdle));
228 } else {
229 main_thread_runner_->PostTask(FROM_HERE,
230 base::Bind(&SetThreadPriorityToDefault));
232 #endif
235 } // namespace content