Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / renderer / gpu / compositor_output_surface.cc
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.
4
5 #include "content/renderer/gpu/compositor_output_surface.h"
6
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_forwarding_message_filter.h"
23 #include "ipc/ipc_sync_channel.h"
24
25 namespace {
26 // There are several compositor surfaces in a process, but they share the same
27 // compositor thread, so we use a simple int here to track prefer-smoothness.
28 int g_prefer_smoothness_count = 0;
29 } // namespace
30
31 namespace content {
32
33 //------------------------------------------------------------------------------
34
35 // static
36 IPC::ForwardingMessageFilter* CompositorOutputSurface::CreateFilter(
37     base::TaskRunner* target_task_runner)
38 {
39   uint32 messages_to_filter[] = {
40     ViewMsg_UpdateVSyncParameters::ID,
41     ViewMsg_SwapCompositorFrameAck::ID,
42     ViewMsg_ReclaimCompositorResources::ID,
43 #if defined(OS_ANDROID)
44     ViewMsg_BeginFrame::ID
45 #endif
46   };
47
48   return new IPC::ForwardingMessageFilter(
49       messages_to_filter, arraysize(messages_to_filter),
50       target_task_runner);
51 }
52
53 CompositorOutputSurface::CompositorOutputSurface(
54     int32 routing_id,
55     uint32 output_surface_id,
56     const scoped_refptr<ContextProviderCommandBuffer>& context_provider,
57     scoped_ptr<cc::SoftwareOutputDevice> software_device,
58     scoped_refptr<FrameSwapMessageQueue> swap_frame_message_queue,
59     bool use_swap_compositor_frame_message)
60     : OutputSurface(context_provider, software_device.Pass()),
61       output_surface_id_(output_surface_id),
62       use_swap_compositor_frame_message_(use_swap_compositor_frame_message),
63       output_surface_filter_(
64           RenderThreadImpl::current()->compositor_output_surface_filter()),
65       frame_swap_message_queue_(swap_frame_message_queue),
66       routing_id_(routing_id),
67       prefers_smoothness_(false),
68 #if defined(OS_WIN)
69       // TODO(epenner): Implement PlatformThread::CurrentHandle() on windows.
70       main_thread_handle_(base::PlatformThreadHandle()),
71 #else
72       main_thread_handle_(base::PlatformThread::CurrentHandle()),
73 #endif
74       layout_test_mode_(RenderThreadImpl::current()->layout_test_mode()),
75       weak_ptrs_(this) {
76   DCHECK(output_surface_filter_.get());
77   DCHECK(frame_swap_message_queue_.get());
78   DetachFromThread();
79   message_sender_ = RenderThreadImpl::current()->sync_message_filter();
80   DCHECK(message_sender_.get());
81   if (OutputSurface::software_device())
82     capabilities_.max_frames_pending = 1;
83 }
84
85 CompositorOutputSurface::~CompositorOutputSurface() {
86   DCHECK(CalledOnValidThread());
87   SetNeedsBeginFrame(false);
88   if (!HasClient())
89     return;
90   UpdateSmoothnessTakesPriority(false);
91   if (output_surface_proxy_.get())
92     output_surface_proxy_->ClearOutputSurface();
93   output_surface_filter_->RemoveRoute(routing_id_);
94 }
95
96 bool CompositorOutputSurface::BindToClient(
97     cc::OutputSurfaceClient* client) {
98   DCHECK(CalledOnValidThread());
99
100   if (!cc::OutputSurface::BindToClient(client))
101     return false;
102
103   output_surface_proxy_ = new CompositorOutputSurfaceProxy(this);
104   output_surface_filter_->AddRoute(
105       routing_id_,
106       base::Bind(&CompositorOutputSurfaceProxy::OnMessageReceived,
107                  output_surface_proxy_));
108
109   if (!context_provider()) {
110     // Without a GPU context, the memory policy otherwise wouldn't be set.
111     client->SetMemoryPolicy(cc::ManagedMemoryPolicy(
112         128 * 1024 * 1024,
113         gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE,
114         base::SharedMemory::GetHandleLimit() / 3));
115   }
116
117   return true;
118 }
119
120 void CompositorOutputSurface::ShortcutSwapAck(
121     uint32 output_surface_id,
122     scoped_ptr<cc::GLFrameData> gl_frame_data,
123     scoped_ptr<cc::SoftwareFrameData> software_frame_data) {
124   if (!layout_test_previous_frame_ack_) {
125     layout_test_previous_frame_ack_.reset(new cc::CompositorFrameAck);
126     layout_test_previous_frame_ack_->gl_frame_data.reset(new cc::GLFrameData);
127   }
128
129   OnSwapAck(output_surface_id, *layout_test_previous_frame_ack_);
130
131   layout_test_previous_frame_ack_->gl_frame_data = gl_frame_data.Pass();
132   layout_test_previous_frame_ack_->last_software_frame_id =
133       software_frame_data ? software_frame_data->id : 0;
134 }
135
136 void CompositorOutputSurface::SwapBuffers(cc::CompositorFrame* frame) {
137   if (layout_test_mode_ && use_swap_compositor_frame_message_) {
138     // This code path is here to support layout tests that are currently
139     // doing a readback in the renderer instead of the browser. So they
140     // are using deprecated code paths in the renderer and don't need to
141     // actually swap anything to the browser. We shortcut the swap to the
142     // browser here and just ack directly within the renderer process.
143     // Once crbug.com/311404 is fixed, this can be removed.
144
145     // This would indicate that crbug.com/311404 is being fixed, and this
146     // block needs to be removed.
147     DCHECK(!frame->delegated_frame_data);
148
149     base::Closure closure =
150         base::Bind(&CompositorOutputSurface::ShortcutSwapAck,
151                    weak_ptrs_.GetWeakPtr(),
152                    output_surface_id_,
153                    base::Passed(&frame->gl_frame_data),
154                    base::Passed(&frame->software_frame_data));
155
156     if (context_provider()) {
157       gpu::gles2::GLES2Interface* context = context_provider()->ContextGL();
158       context->Flush();
159       uint32 sync_point = context->InsertSyncPointCHROMIUM();
160       context_provider()->ContextSupport()->SignalSyncPoint(sync_point,
161                                                             closure);
162     } else {
163       base::MessageLoopProxy::current()->PostTask(FROM_HERE, closure);
164     }
165     client_->DidSwapBuffers();
166     return;
167   }
168
169   if (use_swap_compositor_frame_message_) {
170     {
171       ScopedVector<IPC::Message> messages;
172       std::vector<IPC::Message> messages_to_deliver_with_frame;
173       scoped_ptr<FrameSwapMessageQueue::SendMessageScope> send_message_scope =
174           frame_swap_message_queue_->AcquireSendMessageScope();
175       frame_swap_message_queue_->DrainMessages(&messages);
176       FrameSwapMessageQueue::TransferMessages(messages,
177                                               &messages_to_deliver_with_frame);
178       Send(new ViewHostMsg_SwapCompositorFrame(routing_id_,
179                                                output_surface_id_,
180                                                *frame,
181                                                messages_to_deliver_with_frame));
182       // ~send_message_scope.
183     }
184     client_->DidSwapBuffers();
185     return;
186   }
187
188   if (frame->gl_frame_data) {
189     context_provider()->ContextGL()->ShallowFlushCHROMIUM();
190     ContextProviderCommandBuffer* provider_command_buffer =
191         static_cast<ContextProviderCommandBuffer*>(context_provider().get());
192     CommandBufferProxyImpl* command_buffer_proxy =
193         provider_command_buffer->GetCommandBufferProxy();
194     DCHECK(command_buffer_proxy);
195     command_buffer_proxy->SetLatencyInfo(frame->metadata.latency_info);
196   }
197
198   OutputSurface::SwapBuffers(frame);
199 }
200
201 void CompositorOutputSurface::OnMessageReceived(const IPC::Message& message) {
202   DCHECK(CalledOnValidThread());
203   if (!HasClient())
204     return;
205   IPC_BEGIN_MESSAGE_MAP(CompositorOutputSurface, message)
206     IPC_MESSAGE_HANDLER(ViewMsg_UpdateVSyncParameters,
207                         OnUpdateVSyncParametersFromBrowser);
208     IPC_MESSAGE_HANDLER(ViewMsg_SwapCompositorFrameAck, OnSwapAck);
209     IPC_MESSAGE_HANDLER(ViewMsg_ReclaimCompositorResources, OnReclaimResources);
210 #if defined(OS_ANDROID)
211     IPC_MESSAGE_HANDLER(ViewMsg_BeginFrame, OnBeginFrame);
212 #endif
213   IPC_END_MESSAGE_MAP()
214 }
215
216 void CompositorOutputSurface::OnUpdateVSyncParametersFromBrowser(
217     base::TimeTicks timebase,
218     base::TimeDelta interval) {
219   DCHECK(CalledOnValidThread());
220   CommitVSyncParameters(timebase, interval);
221 }
222
223 #if defined(OS_ANDROID)
224 void CompositorOutputSurface::SetNeedsBeginFrame(bool enable) {
225   DCHECK(CalledOnValidThread());
226   Send(new ViewHostMsg_SetNeedsBeginFrame(routing_id_, enable));
227 }
228
229 void CompositorOutputSurface::OnBeginFrame(const cc::BeginFrameArgs& args) {
230   DCHECK(CalledOnValidThread());
231   client_->BeginFrame(args);
232 }
233 #endif  // defined(OS_ANDROID)
234
235 void CompositorOutputSurface::OnSwapAck(uint32 output_surface_id,
236                                         const cc::CompositorFrameAck& ack) {
237   // Ignore message if it's a stale one coming from a different output surface
238   // (e.g. after a lost context).
239   if (output_surface_id != output_surface_id_)
240     return;
241   ReclaimResources(&ack);
242   client_->DidSwapBuffersComplete();
243 }
244
245 void CompositorOutputSurface::OnReclaimResources(
246     uint32 output_surface_id,
247     const cc::CompositorFrameAck& ack) {
248   // Ignore message if it's a stale one coming from a different output surface
249   // (e.g. after a lost context).
250   if (output_surface_id != output_surface_id_)
251     return;
252   ReclaimResources(&ack);
253 }
254
255 bool CompositorOutputSurface::Send(IPC::Message* message) {
256   return message_sender_->Send(message);
257 }
258
259 namespace {
260 #if defined(OS_ANDROID)
261   void SetThreadPriorityToIdle(base::PlatformThreadHandle handle) {
262     base::PlatformThread::SetThreadPriority(
263        handle, base::kThreadPriority_Background);
264   }
265   void SetThreadPriorityToDefault(base::PlatformThreadHandle handle) {
266     base::PlatformThread::SetThreadPriority(
267        handle, base::kThreadPriority_Normal);
268   }
269 #else
270   void SetThreadPriorityToIdle(base::PlatformThreadHandle handle) {}
271   void SetThreadPriorityToDefault(base::PlatformThreadHandle handle) {}
272 #endif
273 }
274
275 void CompositorOutputSurface::UpdateSmoothnessTakesPriority(
276     bool prefers_smoothness) {
277 #ifndef NDEBUG
278   // If we use different compositor threads, we need to
279   // use an atomic int to track prefer smoothness count.
280   base::PlatformThreadId g_last_thread = base::PlatformThread::CurrentId();
281   DCHECK_EQ(g_last_thread, base::PlatformThread::CurrentId());
282 #endif
283   if (prefers_smoothness_ == prefers_smoothness)
284     return;
285   // If this is the first surface to start preferring smoothness,
286   // Throttle the main thread's priority.
287   if (prefers_smoothness_ == false &&
288       ++g_prefer_smoothness_count == 1) {
289     SetThreadPriorityToIdle(main_thread_handle_);
290   }
291   // If this is the last surface to stop preferring smoothness,
292   // Reset the main thread's priority to the default.
293   if (prefers_smoothness_ == true &&
294       --g_prefer_smoothness_count == 0) {
295     SetThreadPriorityToDefault(main_thread_handle_);
296   }
297   prefers_smoothness_ = prefers_smoothness;
298 }
299
300 }  // namespace content