OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/renderer_host/compositor_impl_android.h" | 5 #include "content/browser/renderer_host/compositor_impl_android.h" |
6 | 6 |
7 #include <android/bitmap.h> | 7 #include <android/bitmap.h> |
8 #include <android/native_window_jni.h> | 8 #include <android/native_window_jni.h> |
9 #include <map> | 9 #include <map> |
10 | 10 |
11 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
12 #include "base/android/scoped_java_ref.h" | 12 #include "base/android/scoped_java_ref.h" |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
15 #include "base/containers/scoped_ptr_hash_map.h" | |
15 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
16 #include "base/logging.h" | 17 #include "base/logging.h" |
17 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
18 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
19 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
21 #include "base/threading/thread_checker.h" | |
20 #include "cc/base/switches.h" | 22 #include "cc/base/switches.h" |
21 #include "cc/input/input_handler.h" | 23 #include "cc/input/input_handler.h" |
22 #include "cc/layers/layer.h" | 24 #include "cc/layers/layer.h" |
23 #include "cc/output/compositor_frame.h" | 25 #include "cc/output/compositor_frame.h" |
24 #include "cc/output/context_provider.h" | 26 #include "cc/output/context_provider.h" |
25 #include "cc/output/output_surface.h" | 27 #include "cc/output/output_surface.h" |
26 #include "cc/resources/scoped_ui_resource.h" | 28 #include "cc/resources/scoped_ui_resource.h" |
27 #include "cc/resources/ui_resource_bitmap.h" | 29 #include "cc/resources/ui_resource_bitmap.h" |
28 #include "cc/trees/layer_tree_host.h" | 30 #include "cc/trees/layer_tree_host.h" |
29 #include "content/browser/gpu/browser_gpu_channel_host_factory.h" | 31 #include "content/browser/gpu/browser_gpu_channel_host_factory.h" |
30 #include "content/browser/gpu/gpu_surface_tracker.h" | 32 #include "content/browser/gpu/gpu_surface_tracker.h" |
31 #include "content/common/gpu/client/command_buffer_proxy_impl.h" | 33 #include "content/common/gpu/client/command_buffer_proxy_impl.h" |
32 #include "content/common/gpu/client/context_provider_command_buffer.h" | 34 #include "content/common/gpu/client/context_provider_command_buffer.h" |
33 #include "content/common/gpu/client/gl_helper.h" | 35 #include "content/common/gpu/client/gl_helper.h" |
34 #include "content/common/gpu/client/gpu_channel_host.h" | 36 #include "content/common/gpu/client/gpu_channel_host.h" |
35 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" | 37 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" |
36 #include "content/common/gpu/gpu_process_launch_causes.h" | 38 #include "content/common/gpu/gpu_process_launch_causes.h" |
37 #include "content/public/browser/android/compositor_client.h" | 39 #include "content/public/browser/android/compositor_client.h" |
38 #include "gpu/command_buffer/client/gles2_interface.h" | 40 #include "gpu/command_buffer/client/gles2_interface.h" |
39 #include "third_party/khronos/GLES2/gl2.h" | 41 #include "third_party/khronos/GLES2/gl2.h" |
40 #include "third_party/khronos/GLES2/gl2ext.h" | 42 #include "third_party/khronos/GLES2/gl2ext.h" |
41 #include "third_party/skia/include/core/SkMallocPixelRef.h" | 43 #include "third_party/skia/include/core/SkMallocPixelRef.h" |
42 #include "ui/base/android/window_android.h" | 44 #include "ui/base/android/window_android.h" |
43 #include "ui/gfx/android/device_display_info.h" | 45 #include "ui/gfx/android/device_display_info.h" |
44 #include "ui/gfx/android/java_bitmap.h" | 46 #include "ui/gfx/android/java_bitmap.h" |
45 #include "ui/gfx/frame_time.h" | 47 #include "ui/gfx/frame_time.h" |
48 #include "ui/gl/android/scoped_java_surface.h" | |
49 #include "ui/gl/android/surface_texture.h" | |
50 #include "ui/gl/android/surface_texture_tracker.h" | |
46 #include "webkit/common/gpu/context_provider_in_process.h" | 51 #include "webkit/common/gpu/context_provider_in_process.h" |
47 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl. h" | 52 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl. h" |
48 | 53 |
49 namespace gfx { | 54 namespace gfx { |
50 class JavaBitmap; | 55 class JavaBitmap; |
51 } | 56 } |
52 | 57 |
53 namespace { | 58 namespace { |
54 | 59 |
55 // Used for drawing directly to the screen. Bypasses resizing and swaps. | 60 // Used for drawing directly to the screen. Bypasses resizing and swaps. |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
124 | 129 |
125 protected: | 130 protected: |
126 TransientUIResource(cc::LayerTreeHost* host, | 131 TransientUIResource(cc::LayerTreeHost* host, |
127 const cc::UIResourceBitmap& bitmap) | 132 const cc::UIResourceBitmap& bitmap) |
128 : cc::ScopedUIResource(host, bitmap), retrieved_(false) {} | 133 : cc::ScopedUIResource(host, bitmap), retrieved_(false) {} |
129 | 134 |
130 private: | 135 private: |
131 bool retrieved_; | 136 bool retrieved_; |
132 }; | 137 }; |
133 | 138 |
139 class SurfaceTextureTrackerImpl : public gfx::SurfaceTextureTracker { | |
140 public: | |
141 SurfaceTextureTrackerImpl() : next_surface_texture_id_(1) { | |
142 thread_checker_.DetachFromThread(); | |
143 } | |
144 | |
145 // Overridden from gfx::SurfaceTextureTracker: | |
146 virtual scoped_refptr<gfx::SurfaceTexture> AcquireSurfaceTexture( | |
147 int primary_id, | |
148 int secondary_id) OVERRIDE { | |
149 base::AutoLock lock(surface_textures_lock_); | |
150 scoped_ptr<SurfaceTextureInfo> info = | |
151 surface_textures_.take_and_erase(SurfaceTextureMapKey( | |
152 primary_id, static_cast<base::ProcessHandle>(secondary_id))); | |
153 return info ? info->surface_texture : NULL; | |
154 } | |
155 | |
156 int AddSurfaceTexture(gfx::SurfaceTexture* surface_texture, | |
157 base::ProcessHandle process_handle) { | |
158 DCHECK(thread_checker_.CalledOnValidThread()); | |
159 int surface_texture_id = next_surface_texture_id_++; | |
160 if (next_surface_texture_id_ == INT_MAX) | |
161 next_surface_texture_id_ = 1; | |
162 | |
163 base::AutoLock lock(surface_textures_lock_); | |
164 SurfaceTextureMapKey key(surface_texture_id, process_handle); | |
165 DCHECK(surface_textures_.find(key) == surface_textures_.end()); | |
166 surface_textures_.set( | |
167 key, make_scoped_ptr(new SurfaceTextureInfo(surface_texture))); | |
168 return surface_texture_id; | |
169 } | |
170 | |
171 void RemoveAllSurfaceTextures(base::ProcessHandle process_handle) { | |
172 DCHECK(thread_checker_.CalledOnValidThread()); | |
173 base::AutoLock lock(surface_textures_lock_); | |
174 SurfaceTextureMap::iterator it = surface_textures_.begin(); | |
175 while (it != surface_textures_.end()) { | |
176 if (it->first.second == process_handle) | |
177 surface_textures_.erase(it++); | |
178 else | |
179 ++it; | |
180 } | |
181 } | |
182 | |
183 jobject GetSurface(int surface_texture_id, | |
184 base::ProcessHandle process_handle) const { | |
185 base::AutoLock lock(surface_textures_lock_); | |
186 SurfaceTextureMap::const_iterator it = surface_textures_.find( | |
187 SurfaceTextureMapKey(surface_texture_id, process_handle)); | |
188 return it == surface_textures_.end() | |
189 ? NULL | |
190 : it->second->surface.j_surface().obj(); | |
piman
2014/03/27 01:07:25
Is the jobject returned thread safe? What if Remov
reveman
2014/03/27 14:48:11
Good call. We probably have to take a ref here and
no sievers
2014/03/27 18:25:25
There are a few things:
- The SurfaceTexture nati
| |
191 } | |
192 | |
193 private: | |
194 struct SurfaceTextureInfo { | |
195 explicit SurfaceTextureInfo(gfx::SurfaceTexture* surface_texture) | |
196 : surface_texture(surface_texture), surface(surface_texture) {} | |
197 | |
198 scoped_refptr<gfx::SurfaceTexture> surface_texture; | |
199 gfx::ScopedJavaSurface surface; | |
200 }; | |
201 | |
202 typedef std::pair<int, base::ProcessHandle> SurfaceTextureMapKey; | |
203 typedef base::ScopedPtrHashMap<SurfaceTextureMapKey, SurfaceTextureInfo> | |
204 SurfaceTextureMap; | |
205 SurfaceTextureMap surface_textures_; | |
206 mutable base::Lock surface_textures_lock_; | |
207 int next_surface_texture_id_; | |
208 base::ThreadChecker thread_checker_; | |
209 }; | |
210 base::LazyInstance<SurfaceTextureTrackerImpl> g_surface_texture_tracker = | |
211 LAZY_INSTANCE_INITIALIZER; | |
212 | |
134 static bool g_initialized = false; | 213 static bool g_initialized = false; |
135 | 214 |
136 } // anonymous namespace | 215 } // anonymous namespace |
137 | 216 |
138 namespace content { | 217 namespace content { |
139 | 218 |
140 typedef std::map<int, base::android::ScopedJavaGlobalRef<jobject> > | 219 typedef std::map<int, base::android::ScopedJavaGlobalRef<jobject> > |
141 SurfaceMap; | 220 SurfaceMap; |
142 static base::LazyInstance<SurfaceMap> | 221 static base::LazyInstance<SurfaceMap> |
143 g_surface_map = LAZY_INSTANCE_INITIALIZER; | 222 g_surface_map = LAZY_INSTANCE_INITIALIZER; |
144 static base::LazyInstance<base::Lock> g_surface_map_lock; | 223 static base::LazyInstance<base::Lock> g_surface_map_lock; |
145 | 224 |
146 // static | 225 // static |
147 Compositor* Compositor::Create(CompositorClient* client, | 226 Compositor* Compositor::Create(CompositorClient* client, |
148 gfx::NativeWindow root_window) { | 227 gfx::NativeWindow root_window) { |
149 return client ? new CompositorImpl(client, root_window) : NULL; | 228 return client ? new CompositorImpl(client, root_window) : NULL; |
150 } | 229 } |
151 | 230 |
152 // static | 231 // static |
153 void Compositor::Initialize() { | 232 void Compositor::Initialize() { |
154 DCHECK(!CompositorImpl::IsInitialized()); | 233 DCHECK(!CompositorImpl::IsInitialized()); |
234 // SurfaceTextureTracker instance must be set before we create a GPU thread | |
235 // that could be using it to initialize GLImage instances. | |
236 gfx::SurfaceTextureTracker::InitInstance(g_surface_texture_tracker.Pointer()); | |
155 g_initialized = true; | 237 g_initialized = true; |
156 } | 238 } |
157 | 239 |
158 // static | 240 // static |
159 bool CompositorImpl::IsInitialized() { | 241 bool CompositorImpl::IsInitialized() { |
160 return g_initialized; | 242 return g_initialized; |
161 } | 243 } |
162 | 244 |
163 // static | 245 // static |
164 jobject CompositorImpl::GetSurface(int surface_id) { | 246 jobject CompositorImpl::GetSurface(int surface_id) { |
165 base::AutoLock lock(g_surface_map_lock.Get()); | 247 base::AutoLock lock(g_surface_map_lock.Get()); |
166 SurfaceMap* surfaces = g_surface_map.Pointer(); | 248 SurfaceMap* surfaces = g_surface_map.Pointer(); |
167 SurfaceMap::iterator it = surfaces->find(surface_id); | 249 SurfaceMap::iterator it = surfaces->find(surface_id); |
168 jobject jsurface = it == surfaces->end() ? NULL : it->second.obj(); | 250 jobject jsurface = it == surfaces->end() ? NULL : it->second.obj(); |
169 | 251 |
170 LOG_IF(WARNING, !jsurface) << "No surface for surface id " << surface_id; | 252 LOG_IF(WARNING, !jsurface) << "No surface for surface id " << surface_id; |
171 return jsurface; | 253 return jsurface; |
172 } | 254 } |
173 | 255 |
256 // static | |
257 jobject CompositorImpl::GetSurfaceTextureSurface( | |
258 int surface_texture_id, | |
259 base::ProcessHandle process_handle) { | |
260 jobject jsurface = g_surface_texture_tracker.Pointer()->GetSurface( | |
261 surface_texture_id, process_handle); | |
262 | |
263 LOG_IF(WARNING, !jsurface) << "No surface for surface texture id " | |
264 << surface_texture_id; | |
265 return jsurface; | |
266 } | |
267 | |
268 // static | |
269 int CompositorImpl::AllocateSurfaceTexture(base::ProcessHandle process_handle) { | |
270 const int kDummyTextureId = 0; | |
271 scoped_refptr<gfx::SurfaceTexture> surface_texture = | |
272 gfx::SurfaceTexture::Create(kDummyTextureId); | |
273 return g_surface_texture_tracker.Pointer()->AddSurfaceTexture( | |
274 surface_texture.get(), process_handle); | |
275 } | |
276 | |
277 // static | |
278 void CompositorImpl::DestroyAllSurfaceTextures( | |
279 base::ProcessHandle process_handle) { | |
280 g_surface_texture_tracker.Pointer()->RemoveAllSurfaceTextures(process_handle); | |
281 } | |
282 | |
174 CompositorImpl::CompositorImpl(CompositorClient* client, | 283 CompositorImpl::CompositorImpl(CompositorClient* client, |
175 gfx::NativeWindow root_window) | 284 gfx::NativeWindow root_window) |
176 : root_layer_(cc::Layer::Create()), | 285 : root_layer_(cc::Layer::Create()), |
177 has_transparent_background_(false), | 286 has_transparent_background_(false), |
178 device_scale_factor_(1), | 287 device_scale_factor_(1), |
179 window_(NULL), | 288 window_(NULL), |
180 surface_id_(0), | 289 surface_id_(0), |
181 client_(client), | 290 client_(client), |
182 root_window_(root_window) { | 291 root_window_(root_window) { |
183 DCHECK(client); | 292 DCHECK(client); |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
464 | 573 |
465 void CompositorImpl::DidCommit() { | 574 void CompositorImpl::DidCommit() { |
466 root_window_->OnCompositingDidCommit(); | 575 root_window_->OnCompositingDidCommit(); |
467 } | 576 } |
468 | 577 |
469 void CompositorImpl::AttachLayerForReadback(scoped_refptr<cc::Layer> layer) { | 578 void CompositorImpl::AttachLayerForReadback(scoped_refptr<cc::Layer> layer) { |
470 root_layer_->AddChild(layer); | 579 root_layer_->AddChild(layer); |
471 } | 580 } |
472 | 581 |
473 } // namespace content | 582 } // namespace content |
OLD | NEW |