OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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/browser/android/in_process/synchronous_compositor_impl.h" |
| 6 |
| 7 #include "base/lazy_instance.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "content/public/browser/android/synchronous_compositor_client.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/render_process_host.h" |
| 12 #include "content/public/browser/render_view_host.h" |
| 13 #include "content/renderer/android/synchronous_compositor_factory.h" |
| 14 |
| 15 namespace content { |
| 16 |
| 17 namespace { |
| 18 |
| 19 int GetInProcessRendererId() { |
| 20 content::RenderProcessHost::iterator it = |
| 21 content::RenderProcessHost::AllHostsIterator(); |
| 22 if (it.IsAtEnd()) { |
| 23 // There should always be one RPH in single process mode. |
| 24 NOTREACHED(); |
| 25 return 0; |
| 26 } |
| 27 |
| 28 int id = it.GetCurrentValue()->GetID(); |
| 29 it.Advance(); |
| 30 DCHECK(it.IsAtEnd()); // Not multiprocess compatible. |
| 31 return id; |
| 32 } |
| 33 |
| 34 class SynchronousCompositorFactoryImpl : public SynchronousCompositorFactory { |
| 35 public: |
| 36 SynchronousCompositorFactoryImpl() { |
| 37 SynchronousCompositorFactory::SetInstance(this); |
| 38 } |
| 39 |
| 40 // SynchronousCompositorFactory |
| 41 virtual scoped_refptr<base::MessageLoopProxy> |
| 42 GetCompositorMessageLoop() OVERRIDE { |
| 43 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); |
| 44 } |
| 45 |
| 46 virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface( |
| 47 int routing_id) OVERRIDE { |
| 48 scoped_ptr<SynchronousCompositorOutputSurface> output_surface( |
| 49 new SynchronousCompositorOutputSurface(routing_id)); |
| 50 return output_surface.PassAs<cc::OutputSurface>(); |
| 51 } |
| 52 }; |
| 53 |
| 54 base::LazyInstance<SynchronousCompositorFactoryImpl>::Leaky g_factory = |
| 55 LAZY_INSTANCE_INITIALIZER; |
| 56 |
| 57 } // namespace |
| 58 |
| 59 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SynchronousCompositorImpl); |
| 60 |
| 61 // static |
| 62 SynchronousCompositorImpl* SynchronousCompositorImpl::FromRoutingID( |
| 63 int routing_id) { |
| 64 RenderViewHost* rvh = RenderViewHost::FromID(GetInProcessRendererId(), |
| 65 routing_id); |
| 66 if (!rvh) |
| 67 return NULL; |
| 68 WebContents* contents = WebContents::FromRenderViewHost(rvh); |
| 69 if (!contents) |
| 70 return NULL; |
| 71 return FromWebContents(contents); |
| 72 } |
| 73 |
| 74 SynchronousCompositorImpl::SynchronousCompositorImpl(WebContents* contents) |
| 75 : compositor_client_(NULL), |
| 76 output_surface_(NULL), |
| 77 contents_(contents) { |
| 78 } |
| 79 |
| 80 SynchronousCompositorImpl::~SynchronousCompositorImpl() { |
| 81 if (compositor_client_) |
| 82 compositor_client_->DidDestroyCompositor(this); |
| 83 } |
| 84 |
| 85 bool SynchronousCompositorImpl::IsHwReady() { |
| 86 DCHECK(CalledOnValidThread()); |
| 87 DCHECK(output_surface_); |
| 88 |
| 89 return output_surface_->IsHwReady(); |
| 90 } |
| 91 |
| 92 void SynchronousCompositorImpl::SetClient( |
| 93 SynchronousCompositorClient* compositor_client) { |
| 94 DCHECK(CalledOnValidThread()); |
| 95 compositor_client_ = compositor_client; |
| 96 } |
| 97 |
| 98 bool SynchronousCompositorImpl::DemandDrawSw(SkCanvas* canvas) { |
| 99 DCHECK(CalledOnValidThread()); |
| 100 DCHECK(output_surface_); |
| 101 |
| 102 return output_surface_->DemandDrawSw(canvas); |
| 103 } |
| 104 |
| 105 bool SynchronousCompositorImpl::DemandDrawHw( |
| 106 gfx::Size view_size, |
| 107 const gfx::Transform& transform, |
| 108 gfx::Rect damage_area) { |
| 109 DCHECK(CalledOnValidThread()); |
| 110 DCHECK(output_surface_); |
| 111 |
| 112 return output_surface_->DemandDrawHw(view_size, transform, damage_area); |
| 113 } |
| 114 |
| 115 void SynchronousCompositorImpl::DidBindOutputSurface( |
| 116 SynchronousCompositorOutputSurface* output_surface) { |
| 117 DCHECK(CalledOnValidThread()); |
| 118 output_surface_ = output_surface; |
| 119 if (compositor_client_) |
| 120 compositor_client_->DidInitializeCompositor(this); |
| 121 } |
| 122 |
| 123 void SynchronousCompositorImpl::DidDestroySynchronousOutputSurface( |
| 124 SynchronousCompositorOutputSurface* output_surface) { |
| 125 DCHECK(CalledOnValidThread()); |
| 126 // Allow for transient hand-over when two output surfaces may refer to |
| 127 // a single delegate. |
| 128 if (output_surface_ == output_surface) { |
| 129 output_surface_ = NULL; |
| 130 if (compositor_client_) |
| 131 compositor_client_->DidDestroyCompositor(this); |
| 132 compositor_client_ = NULL; |
| 133 } |
| 134 } |
| 135 |
| 136 void SynchronousCompositorImpl::SetContinuousInvalidate(bool enable) { |
| 137 DCHECK(CalledOnValidThread()); |
| 138 if (compositor_client_) |
| 139 compositor_client_->SetContinuousInvalidate(enable); |
| 140 } |
| 141 |
| 142 // Not using base::NonThreadSafe as we want to enforce a more exacting threading |
| 143 // requirement: SynchronousCompositorImpl() must only be used on the UI thread. |
| 144 bool SynchronousCompositorImpl::CalledOnValidThread() const { |
| 145 return BrowserThread::CurrentlyOn(BrowserThread::UI); |
| 146 } |
| 147 |
| 148 // static |
| 149 void SynchronousCompositor::SetClientForWebContents( |
| 150 WebContents* contents, |
| 151 SynchronousCompositorClient* client) { |
| 152 DCHECK(contents); |
| 153 if (client) { |
| 154 g_factory.Get(); // Ensure it's initialized. |
| 155 SynchronousCompositorImpl::CreateForWebContents(contents); |
| 156 } |
| 157 if (SynchronousCompositorImpl* instance = |
| 158 SynchronousCompositorImpl::FromWebContents(contents)) { |
| 159 instance->SetClient(client); |
| 160 } |
| 161 } |
| 162 |
| 163 } // namespace content |
OLD | NEW |