Home | History | Annotate | Download | only in gpu
      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/render_thread_impl.h"
     19 #include "ipc/ipc_forwarding_message_filter.h"
     20 #include "ipc/ipc_sync_channel.h"
     21 
     22 namespace {
     23 // There are several compositor surfaces in a process, but they share the same
     24 // compositor thread, so we use a simple int here to track prefer-smoothness.
     25 int g_prefer_smoothness_count = 0;
     26 } // namespace
     27 
     28 namespace content {
     29 
     30 //------------------------------------------------------------------------------
     31 
     32 // static
     33 IPC::ForwardingMessageFilter* CompositorOutputSurface::CreateFilter(
     34     base::TaskRunner* target_task_runner)
     35 {
     36   uint32 messages_to_filter[] = {
     37     ViewMsg_UpdateVSyncParameters::ID,
     38     ViewMsg_SwapCompositorFrameAck::ID,
     39     ViewMsg_ReclaimCompositorResources::ID,
     40 #if defined(OS_ANDROID)
     41     ViewMsg_BeginFrame::ID
     42 #endif
     43   };
     44 
     45   return new IPC::ForwardingMessageFilter(
     46       messages_to_filter, arraysize(messages_to_filter),
     47       target_task_runner);
     48 }
     49 
     50 CompositorOutputSurface::CompositorOutputSurface(
     51     int32 routing_id,
     52     uint32 output_surface_id,
     53     const scoped_refptr<ContextProviderCommandBuffer>& context_provider,
     54     scoped_ptr<cc::SoftwareOutputDevice> software_device,
     55     bool use_swap_compositor_frame_message)
     56     : OutputSurface(context_provider, software_device.Pass()),
     57       output_surface_id_(output_surface_id),
     58       use_swap_compositor_frame_message_(use_swap_compositor_frame_message),
     59       output_surface_filter_(
     60           RenderThreadImpl::current()->compositor_output_surface_filter()),
     61       routing_id_(routing_id),
     62       prefers_smoothness_(false),
     63 #if defined(OS_WIN)
     64       // TODO(epenner): Implement PlatformThread::CurrentHandle() on windows.
     65       main_thread_handle_(base::PlatformThreadHandle())
     66 #else
     67       main_thread_handle_(base::PlatformThread::CurrentHandle())
     68 #endif
     69 {
     70   DCHECK(output_surface_filter_.get());
     71   DetachFromThread();
     72   message_sender_ = RenderThreadImpl::current()->sync_message_filter();
     73   DCHECK(message_sender_.get());
     74   if (OutputSurface::software_device())
     75     capabilities_.max_frames_pending = 1;
     76 }
     77 
     78 CompositorOutputSurface::~CompositorOutputSurface() {
     79   DCHECK(CalledOnValidThread());
     80   SetNeedsBeginImplFrame(false);
     81   if (!HasClient())
     82     return;
     83   UpdateSmoothnessTakesPriority(false);
     84   if (output_surface_proxy_.get())
     85     output_surface_proxy_->ClearOutputSurface();
     86   output_surface_filter_->RemoveRoute(routing_id_);
     87 }
     88 
     89 bool CompositorOutputSurface::BindToClient(
     90     cc::OutputSurfaceClient* client) {
     91   DCHECK(CalledOnValidThread());
     92 
     93   if (!cc::OutputSurface::BindToClient(client))
     94     return false;
     95 
     96   output_surface_proxy_ = new CompositorOutputSurfaceProxy(this);
     97   output_surface_filter_->AddRoute(
     98       routing_id_,
     99       base::Bind(&CompositorOutputSurfaceProxy::OnMessageReceived,
    100                  output_surface_proxy_));
    101 
    102   if (!context_provider()) {
    103     // Without a GPU context, the memory policy otherwise wouldn't be set.
    104     client->SetMemoryPolicy(cc::ManagedMemoryPolicy(
    105         64 * 1024 * 1024,
    106         gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE,
    107         cc::ManagedMemoryPolicy::kDefaultNumResourcesLimit));
    108   }
    109 
    110   return true;
    111 }
    112 
    113 void CompositorOutputSurface::SwapBuffers(cc::CompositorFrame* frame) {
    114   if (use_swap_compositor_frame_message_) {
    115     Send(new ViewHostMsg_SwapCompositorFrame(routing_id_,
    116                                              output_surface_id_,
    117                                              *frame));
    118     DidSwapBuffers();
    119     return;
    120   }
    121 
    122   if (frame->gl_frame_data) {
    123     WebGraphicsContext3DCommandBufferImpl* command_buffer_context =
    124         static_cast<WebGraphicsContext3DCommandBufferImpl*>(
    125             context_provider_->Context3d());
    126     CommandBufferProxyImpl* command_buffer_proxy =
    127         command_buffer_context->GetCommandBufferProxy();
    128     DCHECK(command_buffer_proxy);
    129     context_provider_->Context3d()->shallowFlushCHROMIUM();
    130     command_buffer_proxy->SetLatencyInfo(frame->metadata.latency_info);
    131   }
    132 
    133   OutputSurface::SwapBuffers(frame);
    134 }
    135 
    136 void CompositorOutputSurface::OnMessageReceived(const IPC::Message& message) {
    137   DCHECK(CalledOnValidThread());
    138   if (!HasClient())
    139     return;
    140   IPC_BEGIN_MESSAGE_MAP(CompositorOutputSurface, message)
    141     IPC_MESSAGE_HANDLER(ViewMsg_UpdateVSyncParameters, OnUpdateVSyncParameters);
    142     IPC_MESSAGE_HANDLER(ViewMsg_SwapCompositorFrameAck, OnSwapAck);
    143     IPC_MESSAGE_HANDLER(ViewMsg_ReclaimCompositorResources, OnReclaimResources);
    144 #if defined(OS_ANDROID)
    145     IPC_MESSAGE_HANDLER(ViewMsg_BeginFrame, OnBeginImplFrame);
    146 #endif
    147   IPC_END_MESSAGE_MAP()
    148 }
    149 
    150 void CompositorOutputSurface::OnUpdateVSyncParameters(
    151     base::TimeTicks timebase, base::TimeDelta interval) {
    152   DCHECK(CalledOnValidThread());
    153   OnVSyncParametersChanged(timebase, interval);
    154 }
    155 
    156 #if defined(OS_ANDROID)
    157 void CompositorOutputSurface::SetNeedsBeginImplFrame(bool enable) {
    158   DCHECK(CalledOnValidThread());
    159   if (needs_begin_impl_frame_ != enable)
    160     Send(new ViewHostMsg_SetNeedsBeginFrame(routing_id_, enable));
    161   OutputSurface::SetNeedsBeginImplFrame(enable);
    162 }
    163 
    164 void CompositorOutputSurface::OnBeginImplFrame(const cc::BeginFrameArgs& args) {
    165   DCHECK(CalledOnValidThread());
    166   BeginImplFrame(args);
    167 }
    168 #endif  // defined(OS_ANDROID)
    169 
    170 void CompositorOutputSurface::OnSwapAck(uint32 output_surface_id,
    171                                         const cc::CompositorFrameAck& ack) {
    172   // Ignore message if it's a stale one coming from a different output surface
    173   // (e.g. after a lost context).
    174   if (output_surface_id != output_surface_id_)
    175     return;
    176   ReclaimResources(&ack);
    177   OnSwapBuffersComplete();
    178 }
    179 
    180 void CompositorOutputSurface::OnReclaimResources(
    181     uint32 output_surface_id,
    182     const cc::CompositorFrameAck& ack) {
    183   // Ignore message if it's a stale one coming from a different output surface
    184   // (e.g. after a lost context).
    185   if (output_surface_id != output_surface_id_)
    186     return;
    187   ReclaimResources(&ack);
    188 }
    189 
    190 bool CompositorOutputSurface::Send(IPC::Message* message) {
    191   return message_sender_->Send(message);
    192 }
    193 
    194 namespace {
    195 #if defined(OS_ANDROID)
    196   void SetThreadPriorityToIdle(base::PlatformThreadHandle handle) {
    197     base::PlatformThread::SetThreadPriority(
    198        handle, base::kThreadPriority_Background);
    199   }
    200   void SetThreadPriorityToDefault(base::PlatformThreadHandle handle) {
    201     base::PlatformThread::SetThreadPriority(
    202        handle, base::kThreadPriority_Normal);
    203   }
    204 #else
    205   void SetThreadPriorityToIdle(base::PlatformThreadHandle handle) {}
    206   void SetThreadPriorityToDefault(base::PlatformThreadHandle handle) {}
    207 #endif
    208 }
    209 
    210 void CompositorOutputSurface::UpdateSmoothnessTakesPriority(
    211     bool prefers_smoothness) {
    212 #ifndef NDEBUG
    213   // If we use different compositor threads, we need to
    214   // use an atomic int to track prefer smoothness count.
    215   base::PlatformThreadId g_last_thread = base::PlatformThread::CurrentId();
    216   DCHECK_EQ(g_last_thread, base::PlatformThread::CurrentId());
    217 #endif
    218   if (prefers_smoothness_ == prefers_smoothness)
    219     return;
    220   // If this is the first surface to start preferring smoothness,
    221   // Throttle the main thread's priority.
    222   if (prefers_smoothness_ == false &&
    223       ++g_prefer_smoothness_count == 1) {
    224     SetThreadPriorityToIdle(main_thread_handle_);
    225   }
    226   // If this is the last surface to stop preferring smoothness,
    227   // Reset the main thread's priority to the default.
    228   if (prefers_smoothness_ == true &&
    229       --g_prefer_smoothness_count == 0) {
    230     SetThreadPriorityToDefault(main_thread_handle_);
    231   }
    232   prefers_smoothness_ = prefers_smoothness;
    233 }
    234 
    235 }  // namespace content
    236