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

Side by Side Diff: content/browser/renderer_host/compositor_impl_android.cc

Issue 195583003: Add initial GpuMemoryBufferSurfaceTexture implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address review feedback Created 6 years, 9 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
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
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_primary_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 secondary_id) {
157 DCHECK(thread_checker_.CalledOnValidThread());
158 int primary_id = next_primary_id_++;
159 if (next_primary_id_ == INT_MAX)
160 next_primary_id_ = 1;
161
162 base::AutoLock lock(surface_textures_lock_);
163 SurfaceTextureMapKey key(primary_id, secondary_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 primary_id;
168 }
169
170 void RemoveAllSurfaceTextures(int secondary_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 == secondary_id)
176 surface_textures_.erase(it++);
177 else
178 ++it;
179 }
180 }
181
182 jobject GetSurface(int primary_id, int secondary_id) const {
183 base::AutoLock lock(surface_textures_lock_);
184 SurfaceTextureMap::const_iterator it =
185 surface_textures_.find(SurfaceTextureMapKey(primary_id, secondary_id));
186 return it == surface_textures_.end()
187 ? NULL
188 : it->second->surface.j_surface().obj();
189 }
190
191 private:
192 struct SurfaceTextureInfo {
193 explicit SurfaceTextureInfo(gfx::SurfaceTexture* surface_texture)
194 : surface_texture(surface_texture), surface(surface_texture) {}
195
196 scoped_refptr<gfx::SurfaceTexture> surface_texture;
197 gfx::ScopedJavaSurface surface;
198 };
199
200 typedef std::pair<int, int> SurfaceTextureMapKey;
201 typedef base::ScopedPtrHashMap<SurfaceTextureMapKey, SurfaceTextureInfo>
202 SurfaceTextureMap;
203 SurfaceTextureMap surface_textures_;
204 mutable base::Lock surface_textures_lock_;
205 int next_primary_id_;
206 base::ThreadChecker thread_checker_;
207 };
208 base::LazyInstance<SurfaceTextureTrackerImpl> g_surface_texture_tracker =
209 LAZY_INSTANCE_INITIALIZER;
210
134 static bool g_initialized = false; 211 static bool g_initialized = false;
135 212
136 } // anonymous namespace 213 } // anonymous namespace
137 214
138 namespace content { 215 namespace content {
139 216
140 typedef std::map<int, base::android::ScopedJavaGlobalRef<jobject> > 217 typedef std::map<int, base::android::ScopedJavaGlobalRef<jobject> >
141 SurfaceMap; 218 SurfaceMap;
142 static base::LazyInstance<SurfaceMap> 219 static base::LazyInstance<SurfaceMap>
143 g_surface_map = LAZY_INSTANCE_INITIALIZER; 220 g_surface_map = LAZY_INSTANCE_INITIALIZER;
144 static base::LazyInstance<base::Lock> g_surface_map_lock; 221 static base::LazyInstance<base::Lock> g_surface_map_lock;
145 222
146 // static 223 // static
147 Compositor* Compositor::Create(CompositorClient* client, 224 Compositor* Compositor::Create(CompositorClient* client,
148 gfx::NativeWindow root_window) { 225 gfx::NativeWindow root_window) {
149 return client ? new CompositorImpl(client, root_window) : NULL; 226 return client ? new CompositorImpl(client, root_window) : NULL;
150 } 227 }
151 228
152 // static 229 // static
153 void Compositor::Initialize() { 230 void Compositor::Initialize() {
154 DCHECK(!CompositorImpl::IsInitialized()); 231 DCHECK(!CompositorImpl::IsInitialized());
232 // SurfaceTextureTracker instance must be set before we create a GPU thread
233 // that could be using it to initialize GLImage instances.
234 gfx::SurfaceTextureTracker::InitInstance(g_surface_texture_tracker.Pointer());
155 g_initialized = true; 235 g_initialized = true;
156 } 236 }
157 237
158 // static 238 // static
159 bool CompositorImpl::IsInitialized() { 239 bool CompositorImpl::IsInitialized() {
160 return g_initialized; 240 return g_initialized;
161 } 241 }
162 242
163 // static 243 // static
164 jobject CompositorImpl::GetSurface(int surface_id) { 244 jobject CompositorImpl::GetSurface(int surface_id) {
165 base::AutoLock lock(g_surface_map_lock.Get()); 245 base::AutoLock lock(g_surface_map_lock.Get());
166 SurfaceMap* surfaces = g_surface_map.Pointer(); 246 SurfaceMap* surfaces = g_surface_map.Pointer();
167 SurfaceMap::iterator it = surfaces->find(surface_id); 247 SurfaceMap::iterator it = surfaces->find(surface_id);
168 jobject jsurface = it == surfaces->end() ? NULL : it->second.obj(); 248 jobject jsurface = it == surfaces->end() ? NULL : it->second.obj();
169 249
170 LOG_IF(WARNING, !jsurface) << "No surface for surface id " << surface_id; 250 LOG_IF(WARNING, !jsurface) << "No surface for surface id " << surface_id;
171 return jsurface; 251 return jsurface;
172 } 252 }
173 253
254 // static
255 jobject CompositorImpl::GetSurfaceTextureSurface(int primary_id,
256 int secondary_id) {
257 jobject jsurface =
258 g_surface_texture_tracker.Pointer()->GetSurface(primary_id, secondary_id);
259
260 LOG_IF(WARNING, !jsurface) << "No surface for surface texture id "
261 << primary_id << ", " << secondary_id;
262 return jsurface;
263 }
264
265 // static
266 int CompositorImpl::AllocateSurfaceTexture(int secondary_id) {
267 const int kDummyTextureId = 0;
268 scoped_refptr<gfx::SurfaceTexture> surface_texture =
269 gfx::SurfaceTexture::Create(kDummyTextureId);
270 return g_surface_texture_tracker.Pointer()->AddSurfaceTexture(
271 surface_texture.get(), secondary_id);
272 }
273
274 // static
275 void CompositorImpl::DestroyAllSurfaceTextures(int secondary_id) {
276 g_surface_texture_tracker.Pointer()->RemoveAllSurfaceTextures(secondary_id);
277 }
278
174 CompositorImpl::CompositorImpl(CompositorClient* client, 279 CompositorImpl::CompositorImpl(CompositorClient* client,
175 gfx::NativeWindow root_window) 280 gfx::NativeWindow root_window)
176 : root_layer_(cc::Layer::Create()), 281 : root_layer_(cc::Layer::Create()),
177 has_transparent_background_(false), 282 has_transparent_background_(false),
178 device_scale_factor_(1), 283 device_scale_factor_(1),
179 window_(NULL), 284 window_(NULL),
180 surface_id_(0), 285 surface_id_(0),
181 client_(client), 286 client_(client),
182 root_window_(root_window) { 287 root_window_(root_window) {
183 DCHECK(client); 288 DCHECK(client);
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 569
465 void CompositorImpl::DidCommit() { 570 void CompositorImpl::DidCommit() {
466 root_window_->OnCompositingDidCommit(); 571 root_window_->OnCompositingDidCommit();
467 } 572 }
468 573
469 void CompositorImpl::AttachLayerForReadback(scoped_refptr<cc::Layer> layer) { 574 void CompositorImpl::AttachLayerForReadback(scoped_refptr<cc::Layer> layer) {
470 root_layer_->AddChild(layer); 575 root_layer_->AddChild(layer);
471 } 576 }
472 577
473 } // namespace content 578 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698