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" |
20 #include "cc/base/switches.h" | 21 #include "cc/base/switches.h" |
21 #include "cc/input/input_handler.h" | 22 #include "cc/input/input_handler.h" |
22 #include "cc/layers/layer.h" | 23 #include "cc/layers/layer.h" |
23 #include "cc/output/compositor_frame.h" | 24 #include "cc/output/compositor_frame.h" |
24 #include "cc/output/context_provider.h" | 25 #include "cc/output/context_provider.h" |
(...skipping 11 matching lines...) Expand all Loading... | |
36 #include "content/common/gpu/gpu_process_launch_causes.h" | 37 #include "content/common/gpu/gpu_process_launch_causes.h" |
37 #include "content/public/browser/android/compositor_client.h" | 38 #include "content/public/browser/android/compositor_client.h" |
38 #include "gpu/command_buffer/client/gles2_interface.h" | 39 #include "gpu/command_buffer/client/gles2_interface.h" |
39 #include "third_party/khronos/GLES2/gl2.h" | 40 #include "third_party/khronos/GLES2/gl2.h" |
40 #include "third_party/khronos/GLES2/gl2ext.h" | 41 #include "third_party/khronos/GLES2/gl2ext.h" |
41 #include "third_party/skia/include/core/SkMallocPixelRef.h" | 42 #include "third_party/skia/include/core/SkMallocPixelRef.h" |
42 #include "ui/base/android/window_android.h" | 43 #include "ui/base/android/window_android.h" |
43 #include "ui/gfx/android/device_display_info.h" | 44 #include "ui/gfx/android/device_display_info.h" |
44 #include "ui/gfx/android/java_bitmap.h" | 45 #include "ui/gfx/android/java_bitmap.h" |
45 #include "ui/gfx/frame_time.h" | 46 #include "ui/gfx/frame_time.h" |
47 #include "ui/gl/android/scoped_java_surface.h" | |
48 #include "ui/gl/android/surface_texture.h" | |
49 #include "ui/gl/android/surface_texture_tracker.h" | |
46 #include "webkit/common/gpu/context_provider_in_process.h" | 50 #include "webkit/common/gpu/context_provider_in_process.h" |
47 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl. h" | 51 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl. h" |
48 | 52 |
49 namespace gfx { | 53 namespace gfx { |
50 class JavaBitmap; | 54 class JavaBitmap; |
51 } | 55 } |
52 | 56 |
53 namespace { | 57 namespace { |
54 | 58 |
55 // Used for drawing directly to the screen. Bypasses resizing and swaps. | 59 // 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 | 128 |
125 protected: | 129 protected: |
126 TransientUIResource(cc::LayerTreeHost* host, | 130 TransientUIResource(cc::LayerTreeHost* host, |
127 const cc::UIResourceBitmap& bitmap) | 131 const cc::UIResourceBitmap& bitmap) |
128 : cc::ScopedUIResource(host, bitmap), retrieved_(false) {} | 132 : cc::ScopedUIResource(host, bitmap), retrieved_(false) {} |
129 | 133 |
130 private: | 134 private: |
131 bool retrieved_; | 135 bool retrieved_; |
132 }; | 136 }; |
133 | 137 |
138 class SurfaceTextureTrackerImpl : public gfx::SurfaceTextureTracker { | |
139 public: | |
140 SurfaceTextureTrackerImpl() : next_surface_texture_id_(1) {} | |
141 | |
142 // Overridden from gfx::SurfaceTextureTracker: | |
143 virtual scoped_refptr<gfx::SurfaceTexture> AcquireSurfaceTexture( | |
144 int surface_texture_id) OVERRIDE { | |
145 base::AutoLock lock(surface_textures_lock_); | |
146 for (SurfaceTextureMap::iterator it = surface_textures_.begin(); | |
147 it != surface_textures_.end(); | |
148 ++it) { | |
149 if (it->first.second == surface_texture_id) { | |
150 scoped_ptr<SurfaceTextureInfo> info = | |
151 surface_textures_.take_and_erase(it); | |
152 DCHECK(info); | |
153 return info->surface_texture; | |
154 } | |
155 } | |
156 return NULL; | |
157 } | |
158 | |
159 int AddSurfaceTexture(gfx::SurfaceTexture* surface_texture, | |
no sievers
2014/03/14 01:24:51
How do they ever get deleted? :)
reveman
2014/03/14 19:44:53
Removed using AcquireSurfaceTexture() above, which
| |
160 int render_process_id) { | |
161 int surface_texture_id = next_surface_texture_id_++; | |
162 if (next_surface_texture_id_ == INT_MAX) | |
163 next_surface_texture_id_ = 1; | |
164 | |
165 base::AutoLock lock(surface_textures_lock_); | |
no sievers
2014/03/14 01:24:51
You should hold the lock when modifying next_surfa
reveman
2014/03/14 19:44:53
This should always be called from browser IO threa
| |
166 SurfaceTextureMapKey key(render_process_id, surface_texture_id); | |
167 DCHECK(surface_textures_.find(key) == surface_textures_.end()); | |
168 surface_textures_.set( | |
169 key, make_scoped_ptr(new SurfaceTextureInfo(surface_texture))); | |
170 return surface_texture_id; | |
171 } | |
172 | |
173 jobject GetSurface(int render_process_id, int surface_texture_id) const { | |
174 base::AutoLock lock(surface_textures_lock_); | |
175 SurfaceTextureMap::const_iterator it = surface_textures_.find( | |
176 SurfaceTextureMapKey(render_process_id, surface_texture_id)); | |
177 return it == surface_textures_.end() | |
178 ? NULL | |
179 : it->second->surface.j_surface().obj(); | |
180 } | |
181 | |
182 private: | |
183 struct SurfaceTextureInfo { | |
184 explicit SurfaceTextureInfo(gfx::SurfaceTexture* surface_texture) | |
185 : surface_texture(surface_texture), surface(surface_texture) {} | |
186 | |
187 scoped_refptr<gfx::SurfaceTexture> surface_texture; | |
188 gfx::ScopedJavaSurface surface; | |
189 }; | |
190 | |
191 typedef std::pair<int, int> SurfaceTextureMapKey; | |
192 typedef base::ScopedPtrHashMap<SurfaceTextureMapKey, SurfaceTextureInfo> | |
193 SurfaceTextureMap; | |
194 SurfaceTextureMap surface_textures_; | |
195 mutable base::Lock surface_textures_lock_; | |
196 int next_surface_texture_id_; | |
197 }; | |
198 base::LazyInstance<SurfaceTextureTrackerImpl> g_surface_texture_tracker = | |
199 LAZY_INSTANCE_INITIALIZER; | |
200 | |
134 static bool g_initialized = false; | 201 static bool g_initialized = false; |
135 | 202 |
136 } // anonymous namespace | 203 } // anonymous namespace |
137 | 204 |
138 namespace content { | 205 namespace content { |
139 | 206 |
140 typedef std::map<int, base::android::ScopedJavaGlobalRef<jobject> > | 207 typedef std::map<int, base::android::ScopedJavaGlobalRef<jobject> > |
141 SurfaceMap; | 208 SurfaceMap; |
142 static base::LazyInstance<SurfaceMap> | 209 static base::LazyInstance<SurfaceMap> |
143 g_surface_map = LAZY_INSTANCE_INITIALIZER; | 210 g_surface_map = LAZY_INSTANCE_INITIALIZER; |
144 static base::LazyInstance<base::Lock> g_surface_map_lock; | 211 static base::LazyInstance<base::Lock> g_surface_map_lock; |
145 | 212 |
146 // static | 213 // static |
147 Compositor* Compositor::Create(CompositorClient* client, | 214 Compositor* Compositor::Create(CompositorClient* client, |
148 gfx::NativeWindow root_window) { | 215 gfx::NativeWindow root_window) { |
149 return client ? new CompositorImpl(client, root_window) : NULL; | 216 return client ? new CompositorImpl(client, root_window) : NULL; |
150 } | 217 } |
151 | 218 |
152 // static | 219 // static |
153 void Compositor::Initialize() { | 220 void Compositor::Initialize() { |
154 DCHECK(!CompositorImpl::IsInitialized()); | 221 DCHECK(!CompositorImpl::IsInitialized()); |
222 // SurfaceTextureTracker instance must be set before we create a GPU thread | |
223 // that could be using it to initialize GLImage instances. | |
224 gfx::SurfaceTextureTracker::InitInstance(g_surface_texture_tracker.Pointer()); | |
155 g_initialized = true; | 225 g_initialized = true; |
156 } | 226 } |
157 | 227 |
158 // static | 228 // static |
159 bool CompositorImpl::IsInitialized() { | 229 bool CompositorImpl::IsInitialized() { |
160 return g_initialized; | 230 return g_initialized; |
161 } | 231 } |
162 | 232 |
163 // static | 233 // static |
164 jobject CompositorImpl::GetSurface(int surface_id) { | 234 jobject CompositorImpl::GetSurface(int surface_id) { |
165 base::AutoLock lock(g_surface_map_lock.Get()); | 235 base::AutoLock lock(g_surface_map_lock.Get()); |
166 SurfaceMap* surfaces = g_surface_map.Pointer(); | 236 SurfaceMap* surfaces = g_surface_map.Pointer(); |
167 SurfaceMap::iterator it = surfaces->find(surface_id); | 237 SurfaceMap::iterator it = surfaces->find(surface_id); |
168 jobject jsurface = it == surfaces->end() ? NULL : it->second.obj(); | 238 jobject jsurface = it == surfaces->end() ? NULL : it->second.obj(); |
169 | 239 |
170 LOG_IF(WARNING, !jsurface) << "No surface for surface id " << surface_id; | 240 LOG_IF(WARNING, !jsurface) << "No surface for surface id " << surface_id; |
171 return jsurface; | 241 return jsurface; |
172 } | 242 } |
173 | 243 |
244 // static | |
245 jobject CompositorImpl::GetSurfaceTextureSurface(int render_process_id, | |
246 int surface_texture_id) { | |
247 jobject jsurface = g_surface_texture_tracker.Pointer()->GetSurface( | |
248 render_process_id, surface_texture_id); | |
249 | |
250 LOG_IF(WARNING, !jsurface) << "No surface for surface texture id " | |
251 << surface_texture_id; | |
252 return jsurface; | |
253 } | |
254 | |
255 // static | |
256 int CompositorImpl::AllocateSurfaceTexture(int render_process_id) { | |
257 const int kDummyTextureId = 0; | |
258 scoped_refptr<gfx::SurfaceTexture> surface_texture = | |
259 gfx::SurfaceTexture::Create(kDummyTextureId); | |
260 return g_surface_texture_tracker.Pointer()->AddSurfaceTexture( | |
261 surface_texture.get(), render_process_id); | |
262 } | |
263 | |
174 CompositorImpl::CompositorImpl(CompositorClient* client, | 264 CompositorImpl::CompositorImpl(CompositorClient* client, |
175 gfx::NativeWindow root_window) | 265 gfx::NativeWindow root_window) |
176 : root_layer_(cc::Layer::Create()), | 266 : root_layer_(cc::Layer::Create()), |
177 has_transparent_background_(false), | 267 has_transparent_background_(false), |
178 device_scale_factor_(1), | 268 device_scale_factor_(1), |
179 window_(NULL), | 269 window_(NULL), |
180 surface_id_(0), | 270 surface_id_(0), |
181 client_(client), | 271 client_(client), |
182 root_window_(root_window) { | 272 root_window_(root_window) { |
183 DCHECK(client); | 273 DCHECK(client); |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
460 void CompositorImpl::DidAbortSwapBuffers() { | 550 void CompositorImpl::DidAbortSwapBuffers() { |
461 TRACE_EVENT0("compositor", "CompositorImpl::DidAbortSwapBuffers"); | 551 TRACE_EVENT0("compositor", "CompositorImpl::DidAbortSwapBuffers"); |
462 client_->OnSwapBuffersCompleted(); | 552 client_->OnSwapBuffersCompleted(); |
463 } | 553 } |
464 | 554 |
465 void CompositorImpl::DidCommit() { | 555 void CompositorImpl::DidCommit() { |
466 root_window_->OnCompositingDidCommit(); | 556 root_window_->OnCompositingDidCommit(); |
467 } | 557 } |
468 | 558 |
469 } // namespace content | 559 } // namespace content |
OLD | NEW |