Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(268)

Side by Side Diff: content/browser/android/in_process/synchronous_compositor_factory_impl.cc

Issue 102963010: Move SynchronousCompositorFactoryImpl into separate h/cc files (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: move a TODO comment Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 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_factory_impl .h"
6
7 #include "content/browser/android/in_process/synchronous_compositor_output_surfa ce.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "gpu/command_buffer/client/gl_in_process_context.h"
10 #include "ui/gl/android/surface_texture.h"
11 #include "ui/gl/gl_surface.h"
12 #include "webkit/common/gpu/context_provider_in_process.h"
13 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl. h"
14
15 namespace content {
16
17 namespace {
18
19 class VideoContextProvider
20 : public StreamTextureFactorySynchronousImpl::ContextProvider {
21 public:
22 VideoContextProvider(
23 const scoped_refptr<cc::ContextProvider>& context_provider,
24 gpu::GLInProcessContext* gl_in_process_context)
25 : context_provider_(context_provider),
26 gl_in_process_context_(gl_in_process_context) {}
27
28 virtual scoped_refptr<gfx::SurfaceTexture> GetSurfaceTexture(
29 uint32 stream_id) OVERRIDE {
30 return gl_in_process_context_->GetSurfaceTexture(stream_id);
31 }
32
33 virtual blink::WebGraphicsContext3D* Context3d() OVERRIDE {
34 return context_provider_->Context3d();
35 }
36
37 private:
38 friend class base::RefCountedThreadSafe<VideoContextProvider>;
39 virtual ~VideoContextProvider() {}
40
41 scoped_refptr<cc::ContextProvider> context_provider_;
42 gpu::GLInProcessContext* gl_in_process_context_;
43
44 DISALLOW_COPY_AND_ASSIGN(VideoContextProvider);
45 };
46
47 } // namespace
48
49 using webkit::gpu::WebGraphicsContext3DInProcessCommandBufferImpl;
50
51 SynchronousCompositorFactoryImpl::SynchronousCompositorFactoryImpl()
52 : wrapped_gl_context_for_main_thread_(NULL), num_hardware_compositors_(0) {
Ted C 2014/01/07 00:31:09 does num_hardware_compositors_ need to go on the s
boliu 2014/01/07 00:39:33 Not sure, grey area in the style guide, and clang-
53 SynchronousCompositorFactory::SetInstance(this);
54 }
55
56 SynchronousCompositorFactoryImpl::~SynchronousCompositorFactoryImpl() {}
57
58 scoped_refptr<base::MessageLoopProxy>
59 SynchronousCompositorFactoryImpl::GetCompositorMessageLoop() {
60 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);
61 }
62
63 scoped_ptr<cc::OutputSurface>
64 SynchronousCompositorFactoryImpl::CreateOutputSurface(int routing_id) {
65 scoped_ptr<SynchronousCompositorOutputSurface> output_surface(
66 new SynchronousCompositorOutputSurface(routing_id));
67 return output_surface.PassAs<cc::OutputSurface>();
68 }
69
70 InputHandlerManagerClient*
71 SynchronousCompositorFactoryImpl::GetInputHandlerManagerClient() {
72 return synchronous_input_event_filter();
73 }
74
75 scoped_refptr<cc::ContextProvider>
76 SynchronousCompositorFactoryImpl::GetOffscreenContextProviderForMainThread() {
77 // This check only guarantees the main thread context is created after
78 // a compositor did successfully initialize hardware draw in the past.
79 // In particular this does not guarantee that the main thread context
80 // will fail creation when all compositors release hardware draw.
81 bool failed = !CanCreateMainThreadContext();
82 if (!failed &&
83 (!offscreen_context_for_main_thread_.get() ||
84 offscreen_context_for_main_thread_->DestroyedOnMainThread())) {
85 offscreen_context_for_main_thread_ =
86 webkit::gpu::ContextProviderInProcess::Create(
87 CreateOffscreenContext(),
88 "Compositor-Offscreen");
89 failed = !offscreen_context_for_main_thread_.get() ||
90 !offscreen_context_for_main_thread_->BindToCurrentThread();
91 }
92
93 if (failed) {
94 offscreen_context_for_main_thread_ = NULL;
95 wrapped_gl_context_for_main_thread_ = NULL;
96 }
97 return offscreen_context_for_main_thread_;
98 }
99
100 // This is called on both renderer main thread (offscreen context creation
101 // path shared between cross-process and in-process platforms) and renderer
102 // compositor impl thread (InitializeHwDraw) in order to support Android
103 // WebView synchronously enable and disable hardware mode multiple times in
104 // the same task. This is ok because in-process WGC3D creation may happen on
105 // any thread and is lightweight.
106 scoped_refptr<cc::ContextProvider> SynchronousCompositorFactoryImpl::
107 GetOffscreenContextProviderForCompositorThread() {
108 base::AutoLock lock(offscreen_context_for_compositor_thread_lock_);
109 if (!offscreen_context_for_compositor_thread_.get() ||
110 offscreen_context_for_compositor_thread_->DestroyedOnMainThread()) {
111 offscreen_context_for_compositor_thread_ =
112 webkit::gpu::ContextProviderInProcess::CreateOffscreen();
113 }
114 return offscreen_context_for_compositor_thread_;
115 }
116
117 scoped_ptr<StreamTextureFactory>
118 SynchronousCompositorFactoryImpl::CreateStreamTextureFactory(int view_id) {
119 scoped_ptr<StreamTextureFactorySynchronousImpl> factory(
120 new StreamTextureFactorySynchronousImpl(
121 base::Bind(&SynchronousCompositorFactoryImpl::
122 TryCreateStreamTextureFactory,
123 base::Unretained(this)),
124 view_id));
125 return factory.PassAs<StreamTextureFactory>();
126 }
127
128 void SynchronousCompositorFactoryImpl::CompositorInitializedHardwareDraw() {
129 base::AutoLock lock(num_hardware_compositor_lock_);
130 num_hardware_compositors_++;
131 }
132
133 void SynchronousCompositorFactoryImpl::CompositorReleasedHardwareDraw() {
134 bool should_release_resources = false;
135 {
136 base::AutoLock lock(num_hardware_compositor_lock_);
137 DCHECK_GT(num_hardware_compositors_, 0u);
138 num_hardware_compositors_--;
139 should_release_resources = num_hardware_compositors_ == 0u;
140 }
141 if (should_release_resources)
142 ReleaseGlobalHardwareResources();
143 }
144
145 void SynchronousCompositorFactoryImpl::ReleaseGlobalHardwareResources() {
146 {
147 base::AutoLock lock(offscreen_context_for_compositor_thread_lock_);
148 offscreen_context_for_compositor_thread_ = NULL;
149 }
150
151 // TODO(boliu): Properly clean up command buffer server of main thread
152 // context here.
153 }
154
155 bool SynchronousCompositorFactoryImpl::CanCreateMainThreadContext() {
156 base::AutoLock lock(num_hardware_compositor_lock_);
157 return num_hardware_compositors_ > 0;
158 }
159
160 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider>
161 SynchronousCompositorFactoryImpl::TryCreateStreamTextureFactory() {
162 scoped_refptr<StreamTextureFactorySynchronousImpl::ContextProvider>
163 context_provider;
164 if (CanCreateMainThreadContext() &&
165 GetOffscreenContextProviderForMainThread()) {
166 DCHECK(offscreen_context_for_main_thread_);
167 DCHECK(wrapped_gl_context_for_main_thread_);
168 context_provider =
169 new VideoContextProvider(offscreen_context_for_main_thread_,
170 wrapped_gl_context_for_main_thread_);
171 }
172 return context_provider;
173 }
174
175 // TODO(boliu): Deduplicate this with synchronous_compositor_output_surface.cc.
176 scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>
177 SynchronousCompositorFactoryImpl::CreateOffscreenContext() {
178 if (!gfx::GLSurface::InitializeOneOff())
179 return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>();
180
181 const gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu;
182
183 blink::WebGraphicsContext3D::Attributes attributes;
184 attributes.antialias = false;
185 attributes.shareResources = true;
186 attributes.noAutomaticFlushes = true;
187
188 gpu::GLInProcessContextAttribs in_process_attribs;
189 WebGraphicsContext3DInProcessCommandBufferImpl::ConvertAttributes(
190 attributes, &in_process_attribs);
191 scoped_ptr<gpu::GLInProcessContext> context(
192 gpu::GLInProcessContext::CreateContext(true,
193 NULL,
194 gfx::Size(1, 1),
195 attributes.shareResources,
196 in_process_attribs,
197 gpu_preference));
198
199 wrapped_gl_context_for_main_thread_ = context.get();
200 if (!context.get())
201 return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>();
202
203 return scoped_ptr<WebGraphicsContext3DInProcessCommandBufferImpl>(
204 WebGraphicsContext3DInProcessCommandBufferImpl::WrapContext(
205 context.Pass(), attributes));
206 }
207
208 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698