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 = surface_textures_.take_and_erase( |
| 151 SurfaceTextureMapKey(primary_id, secondary_id)); |
| 152 return info ? info->surface_texture : NULL; |
| 153 } |
| 154 |
| 155 int AddSurfaceTexture(gfx::SurfaceTexture* surface_texture, |
| 156 int child_process_id) { |
| 157 DCHECK(thread_checker_.CalledOnValidThread()); |
| 158 int surface_texture_id = next_surface_texture_id_++; |
| 159 if (next_surface_texture_id_ == INT_MAX) |
| 160 next_surface_texture_id_ = 1; |
| 161 |
| 162 base::AutoLock lock(surface_textures_lock_); |
| 163 SurfaceTextureMapKey key(surface_texture_id, child_process_id); |
| 164 DCHECK(surface_textures_.find(key) == surface_textures_.end()); |
| 165 surface_textures_.set( |
| 166 key, make_scoped_ptr(new SurfaceTextureInfo(surface_texture))); |
| 167 return surface_texture_id; |
| 168 } |
| 169 |
| 170 void RemoveAllSurfaceTextures(int child_process_id) { |
| 171 DCHECK(thread_checker_.CalledOnValidThread()); |
| 172 base::AutoLock lock(surface_textures_lock_); |
| 173 SurfaceTextureMap::iterator it = surface_textures_.begin(); |
| 174 while (it != surface_textures_.end()) { |
| 175 if (it->first.second == child_process_id) |
| 176 surface_textures_.erase(it++); |
| 177 else |
| 178 ++it; |
| 179 } |
| 180 } |
| 181 |
| 182 base::android::ScopedJavaLocalRef<jobject> GetSurface( |
| 183 int surface_texture_id, |
| 184 int child_process_id) const { |
| 185 base::AutoLock lock(surface_textures_lock_); |
| 186 SurfaceTextureMap::const_iterator it = surface_textures_.find( |
| 187 SurfaceTextureMapKey(surface_texture_id, child_process_id)); |
| 188 return it == surface_textures_.end() |
| 189 ? base::android::ScopedJavaLocalRef<jobject>() |
| 190 : base::android::ScopedJavaLocalRef<jobject>( |
| 191 it->second->surface.j_surface()); |
| 192 } |
| 193 |
| 194 private: |
| 195 struct SurfaceTextureInfo { |
| 196 explicit SurfaceTextureInfo(gfx::SurfaceTexture* surface_texture) |
| 197 : surface_texture(surface_texture), surface(surface_texture) {} |
| 198 |
| 199 scoped_refptr<gfx::SurfaceTexture> surface_texture; |
| 200 gfx::ScopedJavaSurface surface; |
| 201 }; |
| 202 |
| 203 typedef std::pair<int, int> SurfaceTextureMapKey; |
| 204 typedef base::ScopedPtrHashMap<SurfaceTextureMapKey, SurfaceTextureInfo> |
| 205 SurfaceTextureMap; |
| 206 SurfaceTextureMap surface_textures_; |
| 207 mutable base::Lock surface_textures_lock_; |
| 208 int next_surface_texture_id_; |
| 209 base::ThreadChecker thread_checker_; |
| 210 }; |
| 211 base::LazyInstance<SurfaceTextureTrackerImpl> g_surface_texture_tracker = |
| 212 LAZY_INSTANCE_INITIALIZER; |
| 213 |
134 static bool g_initialized = false; | 214 static bool g_initialized = false; |
135 | 215 |
136 } // anonymous namespace | 216 } // anonymous namespace |
137 | 217 |
138 namespace content { | 218 namespace content { |
139 | 219 |
140 typedef std::map<int, base::android::ScopedJavaGlobalRef<jobject> > | 220 typedef std::map<int, base::android::ScopedJavaGlobalRef<jobject> > |
141 SurfaceMap; | 221 SurfaceMap; |
142 static base::LazyInstance<SurfaceMap> | 222 static base::LazyInstance<SurfaceMap> |
143 g_surface_map = LAZY_INSTANCE_INITIALIZER; | 223 g_surface_map = LAZY_INSTANCE_INITIALIZER; |
144 static base::LazyInstance<base::Lock> g_surface_map_lock; | 224 static base::LazyInstance<base::Lock> g_surface_map_lock; |
145 | 225 |
146 // static | 226 // static |
147 Compositor* Compositor::Create(CompositorClient* client, | 227 Compositor* Compositor::Create(CompositorClient* client, |
148 gfx::NativeWindow root_window) { | 228 gfx::NativeWindow root_window) { |
149 return client ? new CompositorImpl(client, root_window) : NULL; | 229 return client ? new CompositorImpl(client, root_window) : NULL; |
150 } | 230 } |
151 | 231 |
152 // static | 232 // static |
153 void Compositor::Initialize() { | 233 void Compositor::Initialize() { |
154 DCHECK(!CompositorImpl::IsInitialized()); | 234 DCHECK(!CompositorImpl::IsInitialized()); |
| 235 // SurfaceTextureTracker instance must be set before we create a GPU thread |
| 236 // that could be using it to initialize GLImage instances. |
| 237 gfx::SurfaceTextureTracker::InitInstance(g_surface_texture_tracker.Pointer()); |
155 g_initialized = true; | 238 g_initialized = true; |
156 } | 239 } |
157 | 240 |
158 // static | 241 // static |
159 bool CompositorImpl::IsInitialized() { | 242 bool CompositorImpl::IsInitialized() { |
160 return g_initialized; | 243 return g_initialized; |
161 } | 244 } |
162 | 245 |
163 // static | 246 // static |
164 jobject CompositorImpl::GetSurface(int surface_id) { | 247 base::android::ScopedJavaLocalRef<jobject> CompositorImpl::GetSurface( |
| 248 int surface_id) { |
165 base::AutoLock lock(g_surface_map_lock.Get()); | 249 base::AutoLock lock(g_surface_map_lock.Get()); |
166 SurfaceMap* surfaces = g_surface_map.Pointer(); | 250 SurfaceMap* surfaces = g_surface_map.Pointer(); |
167 SurfaceMap::iterator it = surfaces->find(surface_id); | 251 SurfaceMap::iterator it = surfaces->find(surface_id); |
168 jobject jsurface = it == surfaces->end() ? NULL : it->second.obj(); | 252 base::android::ScopedJavaLocalRef<jobject> jsurface( |
| 253 it == surfaces->end() |
| 254 ? base::android::ScopedJavaLocalRef<jobject>() |
| 255 : base::android::ScopedJavaLocalRef<jobject>(it->second)); |
169 | 256 |
170 LOG_IF(WARNING, !jsurface) << "No surface for surface id " << surface_id; | 257 LOG_IF(WARNING, !jsurface.is_null()) << "No surface for surface id " |
| 258 << surface_id; |
171 return jsurface; | 259 return jsurface; |
172 } | 260 } |
173 | 261 |
| 262 // static |
| 263 base::android::ScopedJavaLocalRef<jobject> |
| 264 CompositorImpl::GetSurfaceTextureSurface(int surface_texture_id, |
| 265 int child_process_id) { |
| 266 base::android::ScopedJavaLocalRef<jobject> jsurface( |
| 267 g_surface_texture_tracker.Pointer()->GetSurface(surface_texture_id, |
| 268 child_process_id)); |
| 269 |
| 270 LOG_IF(WARNING, jsurface.is_null()) << "No surface for surface texture id " |
| 271 << surface_texture_id; |
| 272 return jsurface; |
| 273 } |
| 274 |
| 275 // static |
| 276 int CompositorImpl::CreateSurfaceTexture(int child_process_id) { |
| 277 // Note: this needs to be 0 as the surface texture implemenation will take |
| 278 // ownership of the texture and call glDeleteTextures when the GPU service |
| 279 // attaches the surface texture to a real texture id. glDeleteTextures |
| 280 // silently ignores 0. |
| 281 const int kDummyTextureId = 0; |
| 282 scoped_refptr<gfx::SurfaceTexture> surface_texture = |
| 283 gfx::SurfaceTexture::Create(kDummyTextureId); |
| 284 return g_surface_texture_tracker.Pointer()->AddSurfaceTexture( |
| 285 surface_texture.get(), child_process_id); |
| 286 } |
| 287 |
| 288 // static |
| 289 void CompositorImpl::DestroyAllSurfaceTextures(int child_process_id) { |
| 290 g_surface_texture_tracker.Pointer()->RemoveAllSurfaceTextures( |
| 291 child_process_id); |
| 292 } |
| 293 |
174 CompositorImpl::CompositorImpl(CompositorClient* client, | 294 CompositorImpl::CompositorImpl(CompositorClient* client, |
175 gfx::NativeWindow root_window) | 295 gfx::NativeWindow root_window) |
176 : root_layer_(cc::Layer::Create()), | 296 : root_layer_(cc::Layer::Create()), |
177 has_transparent_background_(false), | 297 has_transparent_background_(false), |
178 device_scale_factor_(1), | 298 device_scale_factor_(1), |
179 window_(NULL), | 299 window_(NULL), |
180 surface_id_(0), | 300 surface_id_(0), |
181 client_(client), | 301 client_(client), |
182 root_window_(root_window) { | 302 root_window_(root_window) { |
183 DCHECK(client); | 303 DCHECK(client); |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 | 584 |
465 void CompositorImpl::DidCommit() { | 585 void CompositorImpl::DidCommit() { |
466 root_window_->OnCompositingDidCommit(); | 586 root_window_->OnCompositingDidCommit(); |
467 } | 587 } |
468 | 588 |
469 void CompositorImpl::AttachLayerForReadback(scoped_refptr<cc::Layer> layer) { | 589 void CompositorImpl::AttachLayerForReadback(scoped_refptr<cc::Layer> layer) { |
470 root_layer_->AddChild(layer); | 590 root_layer_->AddChild(layer); |
471 } | 591 } |
472 | 592 |
473 } // namespace content | 593 } // namespace content |
OLD | NEW |