blob: c3298d03d5faf315b429cdcf71e96c232675985e [file] [log] [blame]
[email protected]1842fe22012-08-13 23:24:351// 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.
4
5#include "content/renderer/gpu/compositor_output_surface.h"
6
[email protected]56f33732013-03-05 04:41:497#include "base/command_line.h"
skyostil2d3b5bd2015-05-27 15:40:598#include "base/location.h"
9#include "base/single_thread_task_runner.h"
10#include "base/thread_task_runner_handle.h"
[email protected]7f0d825f2013-03-18 07:24:3011#include "cc/output/compositor_frame.h"
12#include "cc/output/compositor_frame_ack.h"
[email protected]e15c3d732013-09-24 07:30:2713#include "cc/output/managed_memory_policy.h"
[email protected]7f0d825f2013-03-18 07:24:3014#include "cc/output/output_surface_client.h"
[email protected]3ae68c52013-04-12 06:10:0515#include "content/common/gpu/client/command_buffer_proxy_impl.h"
[email protected]0634cdd42013-08-16 00:46:0916#include "content/common/gpu/client/context_provider_command_buffer.h"
[email protected]3ae68c52013-04-12 06:10:0517#include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
[email protected]1842fe22012-08-13 23:24:3518#include "content/common/view_messages.h"
[email protected]56f33732013-03-05 04:41:4919#include "content/public/common/content_switches.h"
[email protected]586871b2014-07-22 17:05:1120#include "content/renderer/gpu/frame_swap_message_queue.h"
[email protected]1842fe22012-08-13 23:24:3521#include "content/renderer/render_thread_impl.h"
[email protected]758efb02014-04-05 07:53:4522#include "gpu/command_buffer/client/context_support.h"
[email protected]a770f2a2014-01-07 22:29:2323#include "gpu/command_buffer/client/gles2_interface.h"
[email protected]1842fe22012-08-13 23:24:3524#include "ipc/ipc_sync_channel.h"
[email protected]1842fe22012-08-13 23:24:3525
[email protected]e9ff79c2012-10-19 21:31:2626namespace content {
27
[email protected]1842fe22012-08-13 23:24:3528CompositorOutputSurface::CompositorOutputSurface(
29 int32 routing_id,
[email protected]53b4cc12013-07-18 23:02:3030 uint32 output_surface_id,
[email protected]0634cdd42013-08-16 00:46:0931 const scoped_refptr<ContextProviderCommandBuffer>& context_provider,
caseqff9c74c2015-02-10 14:56:2932 const scoped_refptr<ContextProviderCommandBuffer>& worker_context_provider,
[email protected]0634cdd42013-08-16 00:46:0933 scoped_ptr<cc::SoftwareOutputDevice> software_device,
[email protected]586871b2014-07-22 17:05:1134 scoped_refptr<FrameSwapMessageQueue> swap_frame_message_queue,
[email protected]36e5ff12013-06-11 12:19:2935 bool use_swap_compositor_frame_message)
caseqff9c74c2015-02-10 14:56:2936 : OutputSurface(context_provider,
37 worker_context_provider,
38 software_device.Pass()),
[email protected]53b4cc12013-07-18 23:02:3039 output_surface_id_(output_surface_id),
[email protected]36e5ff12013-06-11 12:19:2940 use_swap_compositor_frame_message_(use_swap_compositor_frame_message),
[email protected]79a1be52013-02-13 23:59:5041 output_surface_filter_(
simonhonga7e3ac42014-11-11 20:50:2242 RenderThreadImpl::current()->compositor_message_filter()),
[email protected]586871b2014-07-22 17:05:1143 frame_swap_message_queue_(swap_frame_message_queue),
[email protected]7d4481d12012-09-24 21:26:4744 routing_id_(routing_id),
toyoshima28154722015-06-26 00:02:1245#if defined(OS_ANDROID)
[email protected]34351cca2013-01-23 03:22:1546 prefers_smoothness_(false),
toyoshima28154722015-06-26 00:02:1247 main_thread_runner_(base::MessageLoop::current()->task_runner()),
[email protected]8d14e56b2013-07-12 23:22:3648#endif
[email protected]758efb02014-04-05 07:53:4549 layout_test_mode_(RenderThreadImpl::current()->layout_test_mode()),
50 weak_ptrs_(this) {
[email protected]fc72bb12013-06-02 21:13:4651 DCHECK(output_surface_filter_.get());
[email protected]586871b2014-07-22 17:05:1152 DCHECK(frame_swap_message_queue_.get());
[email protected]1842fe22012-08-13 23:24:3553 DetachFromThread();
sunnyps39c5af242015-06-08 22:07:3354 capabilities_.max_frames_pending = 1;
[email protected]2462b7f02013-04-23 02:12:0255 message_sender_ = RenderThreadImpl::current()->sync_message_filter();
[email protected]fc72bb12013-06-02 21:13:4656 DCHECK(message_sender_.get());
[email protected]1842fe22012-08-13 23:24:3557}
58
59CompositorOutputSurface::~CompositorOutputSurface() {
60 DCHECK(CalledOnValidThread());
[email protected]049fc7a2013-06-18 12:32:3561 if (!HasClient())
[email protected]1842fe22012-08-13 23:24:3562 return;
[email protected]34351cca2013-01-23 03:22:1563 UpdateSmoothnessTakesPriority(false);
[email protected]fc72bb12013-06-02 21:13:4664 if (output_surface_proxy_.get())
[email protected]79a1be52013-02-13 23:59:5065 output_surface_proxy_->ClearOutputSurface();
simonhonga7e3ac42014-11-11 20:50:2266 output_surface_filter_->RemoveHandlerOnCompositorThread(
67 routing_id_,
68 output_surface_filter_handler_);
[email protected]1842fe22012-08-13 23:24:3569}
70
[email protected]a46f32932012-12-07 21:43:1671bool CompositorOutputSurface::BindToClient(
72 cc::OutputSurfaceClient* client) {
[email protected]1842fe22012-08-13 23:24:3573 DCHECK(CalledOnValidThread());
[email protected]1842fe22012-08-13 23:24:3574
[email protected]79a1be52013-02-13 23:59:5075 if (!cc::OutputSurface::BindToClient(client))
76 return false;
[email protected]1842fe22012-08-13 23:24:3577
[email protected]d4b437ef2012-10-03 06:03:3978 output_surface_proxy_ = new CompositorOutputSurfaceProxy(this);
simonhonga7e3ac42014-11-11 20:50:2279 output_surface_filter_handler_ =
[email protected]d4b437ef2012-10-03 06:03:3980 base::Bind(&CompositorOutputSurfaceProxy::OnMessageReceived,
simonhonga7e3ac42014-11-11 20:50:2281 output_surface_proxy_);
82 output_surface_filter_->AddHandlerOnCompositorThread(
83 routing_id_,
84 output_surface_filter_handler_);
[email protected]1842fe22012-08-13 23:24:3585
[email protected]e15c3d732013-09-24 07:30:2786 if (!context_provider()) {
87 // Without a GPU context, the memory policy otherwise wouldn't be set.
88 client->SetMemoryPolicy(cc::ManagedMemoryPolicy(
[email protected]23b7297f2014-05-21 22:26:2489 128 * 1024 * 1024,
[email protected]f44d5552013-10-29 04:56:2990 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE,
[email protected]721532e2b2014-07-16 05:57:4591 base::SharedMemory::GetHandleLimit() / 3));
[email protected]e15c3d732013-09-24 07:30:2792 }
93
[email protected]1842fe22012-08-13 23:24:3594 return true;
95}
96
[email protected]758efb02014-04-05 07:53:4597void CompositorOutputSurface::ShortcutSwapAck(
98 uint32 output_surface_id,
jbauman95f741352015-08-07 02:10:3499 scoped_ptr<cc::GLFrameData> gl_frame_data) {
[email protected]758efb02014-04-05 07:53:45100 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);
103 }
104
105 OnSwapAck(output_surface_id, *layout_test_previous_frame_ack_);
106
107 layout_test_previous_frame_ack_->gl_frame_data = gl_frame_data.Pass();
[email protected]758efb02014-04-05 07:53:45108}
109
[email protected]36e5ff12013-06-11 12:19:29110void CompositorOutputSurface::SwapBuffers(cc::CompositorFrame* frame) {
sievers4941650f2014-10-07 17:21:31111 DCHECK(use_swap_compositor_frame_message_);
112 if (layout_test_mode_) {
[email protected]758efb02014-04-05 07:53:45113 // 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.
119
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);
123
jbauman95f741352015-08-07 02:10:34124 base::Closure closure = base::Bind(
125 &CompositorOutputSurface::ShortcutSwapAck, weak_ptrs_.GetWeakPtr(),
126 output_surface_id_, base::Passed(&frame->gl_frame_data));
[email protected]758efb02014-04-05 07:53:45127
[email protected]e6367342014-06-21 02:52:31128 if (context_provider()) {
129 gpu::gles2::GLES2Interface* context = context_provider()->ContextGL();
[email protected]758efb02014-04-05 07:53:45130 context->Flush();
131 uint32 sync_point = context->InsertSyncPointCHROMIUM();
[email protected]e6367342014-06-21 02:52:31132 context_provider()->ContextSupport()->SignalSyncPoint(sync_point,
133 closure);
[email protected]758efb02014-04-05 07:53:45134 } else {
skyostil2d3b5bd2015-05-27 15:40:59135 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, closure);
[email protected]758efb02014-04-05 07:53:45136 }
[email protected]3dc0c772014-04-26 10:06:05137 client_->DidSwapBuffers();
[email protected]758efb02014-04-05 07:53:45138 return;
sievers4941650f2014-10-07 17:21:31139 } else {
[email protected]586871b2014-07-22 17:05:11140 {
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.
153 }
[email protected]3dc0c772014-04-26 10:06:05154 client_->DidSwapBuffers();
[email protected]36e5ff12013-06-11 12:19:29155 }
[email protected]3ae68c52013-04-12 06:10:05156}
157
[email protected]1842fe22012-08-13 23:24:35158void CompositorOutputSurface::OnMessageReceived(const IPC::Message& message) {
159 DCHECK(CalledOnValidThread());
[email protected]049fc7a2013-06-18 12:32:35160 if (!HasClient())
[email protected]b75ce46a02012-08-15 22:27:06161 return;
[email protected]1842fe22012-08-13 23:24:35162 IPC_BEGIN_MESSAGE_MAP(CompositorOutputSurface, message)
[email protected]2bd1fcf02014-02-12 22:35:53163 IPC_MESSAGE_HANDLER(ViewMsg_UpdateVSyncParameters,
164 OnUpdateVSyncParametersFromBrowser);
[email protected]534f9aa2013-03-03 21:58:16165 IPC_MESSAGE_HANDLER(ViewMsg_SwapCompositorFrameAck, OnSwapAck);
[email protected]a7335e0b2013-09-18 09:34:51166 IPC_MESSAGE_HANDLER(ViewMsg_ReclaimCompositorResources, OnReclaimResources);
[email protected]1842fe22012-08-13 23:24:35167 IPC_END_MESSAGE_MAP()
168}
169
[email protected]2bd1fcf02014-02-12 22:35:53170void CompositorOutputSurface::OnUpdateVSyncParametersFromBrowser(
171 base::TimeTicks timebase,
172 base::TimeDelta interval) {
[email protected]1842fe22012-08-13 23:24:35173 DCHECK(CalledOnValidThread());
[email protected]2bd1fcf02014-02-12 22:35:53174 CommitVSyncParameters(timebase, interval);
[email protected]1842fe22012-08-13 23:24:35175}
[email protected]e9ff79c2012-10-19 21:31:26176
[email protected]53b4cc12013-07-18 23:02:30177void 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;
[email protected]a7335e0b2013-09-18 09:34:51183 ReclaimResources(&ack);
[email protected]3dc0c772014-04-26 10:06:05184 client_->DidSwapBuffersComplete();
[email protected]a7335e0b2013-09-18 09:34:51185}
186
187void 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);
[email protected]534f9aa2013-03-03 21:58:16195}
196
[email protected]bf189f62012-12-18 03:42:11197bool CompositorOutputSurface::Send(IPC::Message* message) {
[email protected]2462b7f02013-04-23 02:12:02198 return message_sender_->Send(message);
[email protected]bf189f62012-12-18 03:42:11199}
200
[email protected]34351cca2013-01-23 03:22:15201#if defined(OS_ANDROID)
toyoshima28154722015-06-26 00:02:12202namespace {
203void SetThreadPriorityToIdle() {
toyoshim7c0e1952015-07-14 09:42:03204 base::PlatformThread::SetCurrentThreadPriority(
205 base::ThreadPriority::BACKGROUND);
[email protected]34351cca2013-01-23 03:22:15206}
toyoshima28154722015-06-26 00:02:12207void SetThreadPriorityToDefault() {
toyoshim7c0e1952015-07-14 09:42:03208 base::PlatformThread::SetCurrentThreadPriority(base::ThreadPriority::NORMAL);
toyoshima28154722015-06-26 00:02:12209}
210} // namespace
211#endif
[email protected]34351cca2013-01-23 03:22:15212
213void CompositorOutputSurface::UpdateSmoothnessTakesPriority(
214 bool prefers_smoothness) {
toyoshima28154722015-06-26 00:02:12215#if defined(OS_ANDROID)
[email protected]34351cca2013-01-23 03:22:15216 if (prefers_smoothness_ == prefers_smoothness)
217 return;
[email protected]34351cca2013-01-23 03:22:15218 prefers_smoothness_ = prefers_smoothness;
toyoshima28154722015-06-26 00:02:12219 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));
225 }
226#endif
[email protected]34351cca2013-01-23 03:22:15227}
228
[email protected]e9ff79c2012-10-19 21:31:26229} // namespace content