Chromium Code Reviews| 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 more. | |
| 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 // SynchronousCompositorFactory | |
| 37 virtual scoped_refptr<base::MessageLoopProxy> | |
| 38 GetCompositorMessageLoop() OVERRIDE { | |
| 39 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); | |
| 40 } | |
| 41 | |
| 42 virtual scoped_ptr<cc::OutputSurface> CreateOutputSurface( | |
| 43 int routing_id) OVERRIDE { | |
| 44 scoped_ptr<SynchronousCompositorOutputSurface> output_surface( | |
| 45 new SynchronousCompositorOutputSurface(routing_id)); | |
| 46 return output_surface.PassAs<cc::OutputSurface>(); | |
| 47 } | |
| 48 }; | |
| 49 | |
| 50 base::LazyInstance<SynchronousCompositorFactoryImpl> g_factory = | |
| 51 LAZY_INSTANCE_INITIALIZER; | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SynchronousCompositorImpl); | |
| 56 | |
| 57 void SynchronousCompositorImpl::InitFactory() { | |
| 58 SynchronousCompositorFactory::SetInstance(g_factory.Pointer()); | |
| 59 } | |
| 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); | |
|
mkosiba (inactive)
2013/05/30 11:05:29
nit: it's a bit hard to figure out that this metho
| |
| 72 } | |
| 73 | |
| 74 // static | |
| 75 SynchronousCompositorImpl* SynchronousCompositorImpl::GetOrCreateForWebContents( | |
| 76 WebContents* contents) { | |
| 77 SynchronousCompositorImpl* instance = FromWebContents(contents); | |
|
mkosiba (inactive)
2013/05/30 11:05:29
same here
| |
| 78 if (instance) | |
| 79 return instance; | |
| 80 CreateForWebContents(contents); | |
|
mkosiba (inactive)
2013/05/30 11:05:29
and here
| |
| 81 return FromWebContents(contents); | |
|
joth
2013/05/30 19:18:05
and here?
IKWYM about taking some effort to read
| |
| 82 } | |
| 83 | |
| 84 SynchronousCompositorImpl::SynchronousCompositorImpl(WebContents* contents) | |
| 85 : compositor_client_(NULL), | |
| 86 output_surface_(NULL), | |
| 87 contents_(contents) { | |
| 88 } | |
| 89 | |
| 90 SynchronousCompositorImpl::~SynchronousCompositorImpl() { | |
| 91 if (compositor_client_) | |
| 92 compositor_client_->DidDestroyCompositor(this); | |
| 93 } | |
| 94 | |
| 95 bool SynchronousCompositorImpl::IsHwReady() { | |
| 96 DCHECK(CalledOnValidThread()); | |
| 97 DCHECK(output_surface_); | |
| 98 | |
| 99 return output_surface_->IsHwReady(); | |
| 100 } | |
| 101 | |
| 102 void SynchronousCompositorImpl::SetClient( | |
| 103 SynchronousCompositorClient* compositor_client) { | |
| 104 DCHECK(CalledOnValidThread()); | |
| 105 compositor_client_ = compositor_client; | |
| 106 } | |
| 107 | |
| 108 bool SynchronousCompositorImpl::DemandDrawSw(SkCanvas* canvas) { | |
| 109 DCHECK(CalledOnValidThread()); | |
| 110 DCHECK(output_surface_); | |
| 111 | |
| 112 return output_surface_->DemandDrawSw(canvas); | |
| 113 } | |
| 114 | |
| 115 bool SynchronousCompositorImpl::DemandDrawHw( | |
| 116 gfx::Size view_size, | |
| 117 const gfx::Transform& transform, | |
| 118 gfx::Rect damage_area) { | |
| 119 DCHECK(CalledOnValidThread()); | |
| 120 DCHECK(output_surface_); | |
| 121 | |
| 122 return output_surface_->DemandDrawHw(view_size, transform, damage_area); | |
| 123 } | |
| 124 | |
| 125 void SynchronousCompositorImpl::DidBindOutputSurface( | |
| 126 SynchronousCompositorOutputSurface* output_surface) { | |
| 127 DCHECK(CalledOnValidThread()); | |
| 128 output_surface_ = output_surface; | |
| 129 if (compositor_client_) | |
| 130 compositor_client_->DidInitializeCompositor(this); | |
| 131 } | |
| 132 | |
| 133 void SynchronousCompositorImpl::DidDestroySynchronousOutputSurface( | |
| 134 SynchronousCompositorOutputSurface* output_surface) { | |
| 135 DCHECK(CalledOnValidThread()); | |
| 136 // Allow for transient hand-over when two output surfaces may refer to | |
| 137 // a single delegate. | |
| 138 if (output_surface_ == output_surface) { | |
| 139 output_surface_ = NULL; | |
| 140 if (compositor_client_) | |
| 141 compositor_client_->DidDestroyCompositor(this); | |
| 142 compositor_client_ = NULL; | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 void SynchronousCompositorImpl::SetContinuousInvalidate(bool enable) { | |
| 147 DCHECK(CalledOnValidThread()); | |
| 148 if (compositor_client_) | |
| 149 compositor_client_->SetContinuousInvalidate(enable); | |
| 150 } | |
| 151 | |
| 152 // Not using base::NonThreadSafe as we want to enforce a more exacting threading | |
| 153 // requirement: SynchronousCompositorImpl() must only be used on the UI thread. | |
| 154 bool SynchronousCompositorImpl::CalledOnValidThread() const { | |
| 155 return BrowserThread::CurrentlyOn(BrowserThread::UI); | |
| 156 } | |
| 157 | |
| 158 // static | |
| 159 void SynchronousCompositor::Init() { | |
| 160 SynchronousCompositorImpl::InitFactory(); | |
| 161 } | |
| 162 | |
| 163 // static | |
| 164 SynchronousCompositor* SynchronousCompositor::GetOrCreateForWebContents( | |
| 165 WebContents* contents) { | |
| 166 return SynchronousCompositorImpl::GetOrCreateForWebContents(contents); | |
| 167 } | |
| 168 | |
| 169 } // namespace content | |
| OLD | NEW |