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

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

Issue 143803004: android: Migrate old content readback to use async readback (and delegated renderer) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressed comments Created 6 years, 10 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
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/callback_helpers.h"
14 #include "base/command_line.h" 15 #include "base/command_line.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/input/input_handler.h" 21 #include "cc/input/input_handler.h"
21 #include "cc/layers/layer.h" 22 #include "cc/layers/layer.h"
22 #include "cc/output/compositor_frame.h" 23 #include "cc/output/compositor_frame.h"
23 #include "cc/output/context_provider.h" 24 #include "cc/output/context_provider.h"
24 #include "cc/output/output_surface.h" 25 #include "cc/output/output_surface.h"
25 #include "cc/resources/scoped_ui_resource.h" 26 #include "cc/resources/scoped_ui_resource.h"
26 #include "cc/resources/ui_resource_bitmap.h" 27 #include "cc/resources/ui_resource_bitmap.h"
27 #include "cc/trees/layer_tree_host.h" 28 #include "cc/trees/layer_tree_host.h"
28 #include "content/browser/gpu/browser_gpu_channel_host_factory.h" 29 #include "content/browser/gpu/browser_gpu_channel_host_factory.h"
29 #include "content/browser/gpu/gpu_surface_tracker.h" 30 #include "content/browser/gpu/gpu_surface_tracker.h"
30 #include "content/common/gpu/client/command_buffer_proxy_impl.h" 31 #include "content/common/gpu/client/command_buffer_proxy_impl.h"
31 #include "content/common/gpu/client/context_provider_command_buffer.h" 32 #include "content/common/gpu/client/context_provider_command_buffer.h"
32 #include "content/common/gpu/client/gl_helper.h" 33 #include "content/common/gpu/client/gl_helper.h"
33 #include "content/common/gpu/client/gpu_channel_host.h" 34 #include "content/common/gpu/client/gpu_channel_host.h"
34 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" 35 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
35 #include "content/common/gpu/gpu_process_launch_causes.h" 36 #include "content/common/gpu/gpu_process_launch_causes.h"
36 #include "content/public/browser/android/compositor_client.h" 37 #include "content/public/browser/android/compositor_client.h"
37 #include "content/public/common/content_switches.h" 38 #include "content/public/common/content_switches.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"
42 #include "third_party/skia/include/core/SkMallocPixelRef.h"
41 #include "ui/base/android/window_android.h" 43 #include "ui/base/android/window_android.h"
42 #include "ui/gfx/android/device_display_info.h" 44 #include "ui/gfx/android/device_display_info.h"
43 #include "ui/gfx/android/java_bitmap.h" 45 #include "ui/gfx/android/java_bitmap.h"
44 #include "ui/gfx/frame_time.h" 46 #include "ui/gfx/frame_time.h"
45 #include "webkit/common/gpu/context_provider_in_process.h" 47 #include "webkit/common/gpu/context_provider_in_process.h"
46 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl. h" 48 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl. h"
47 49
48 namespace gfx { 50 namespace gfx {
49 class JavaBitmap; 51 class JavaBitmap;
50 } 52 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 context_provider_.get()); 85 context_provider_.get());
84 content::CommandBufferProxyImpl* command_buffer_proxy = 86 content::CommandBufferProxyImpl* command_buffer_proxy =
85 provider_command_buffer->GetCommandBufferProxy(); 87 provider_command_buffer->GetCommandBufferProxy();
86 DCHECK(command_buffer_proxy); 88 DCHECK(command_buffer_proxy);
87 command_buffer_proxy->SetLatencyInfo(frame->metadata.latency_info); 89 command_buffer_proxy->SetLatencyInfo(frame->metadata.latency_info);
88 90
89 OutputSurface::SwapBuffers(frame); 91 OutputSurface::SwapBuffers(frame);
90 } 92 }
91 }; 93 };
92 94
95 class TransientUIResource : public cc::ScopedUIResource {
96 public:
97 static scoped_ptr<TransientUIResource> Create(
98 cc::LayerTreeHost* host,
99 const cc::UIResourceBitmap& bitmap) {
100 return make_scoped_ptr(new TransientUIResource(host, bitmap));
101 }
102
103 virtual cc::UIResourceBitmap GetBitmap(cc::UIResourceId uid,
104 bool resource_lost) OVERRIDE {
aelias_OOO_until_Jul13 2014/01/31 05:50:10 nit: indent
powei 2014/02/01 01:30:49 Done.
105 if (!retrieved_) {
106 cc::UIResourceBitmap old_bitmap(bitmap_);
107
108 // Return a place holder for all following calls to GetBitmap.
109 SkBitmap tiny_bitmap;
110 tiny_bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
aelias_OOO_until_Jul13 2014/01/31 05:50:10 , kOpaque_SkAlphaType
powei 2014/02/01 01:30:49 Done.
111 tiny_bitmap.allocPixels();
aelias_OOO_until_Jul13 2014/01/31 05:50:10 This bitmap may actually be displayed on the scree
powei 2014/02/01 01:30:49 Done.
112 tiny_bitmap.setImmutable();
113 // Release our reference of the true bitmap.
114 bitmap_ = cc::UIResourceBitmap(tiny_bitmap);
115
116 retrieved_ = true;
117 return old_bitmap;
118 }
119 return bitmap_;
120 }
121
122 protected:
123 TransientUIResource(cc::LayerTreeHost* host,
124 const cc::UIResourceBitmap& bitmap)
125 : cc::ScopedUIResource(host, bitmap), retrieved_(false) {}
126
127 private:
128 bool retrieved_;
129 };
130
93 static bool g_initialized = false; 131 static bool g_initialized = false;
94 132
95 } // anonymous namespace 133 } // anonymous namespace
96 134
97 namespace content { 135 namespace content {
98 136
99 typedef std::map<int, base::android::ScopedJavaGlobalRef<jobject> > 137 typedef std::map<int, base::android::ScopedJavaGlobalRef<jobject> >
100 SurfaceMap; 138 SurfaceMap;
101 static base::LazyInstance<SurfaceMap> 139 static base::LazyInstance<SurfaceMap>
102 g_surface_map = LAZY_INSTANCE_INITIALIZER; 140 g_surface_map = LAZY_INSTANCE_INITIALIZER;
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 } 298 }
261 299
262 bool CompositorImpl::CompositeAndReadback(void *pixels, const gfx::Rect& rect) { 300 bool CompositorImpl::CompositeAndReadback(void *pixels, const gfx::Rect& rect) {
263 if (host_) 301 if (host_)
264 return host_->CompositeAndReadback(pixels, rect); 302 return host_->CompositeAndReadback(pixels, rect);
265 else 303 else
266 return false; 304 return false;
267 } 305 }
268 306
269 cc::UIResourceId CompositorImpl::GenerateUIResource( 307 cc::UIResourceId CompositorImpl::GenerateUIResource(
270 const cc::UIResourceBitmap& bitmap) { 308 const cc::UIResourceBitmap& bitmap,
309 bool is_transient) {
271 if (!host_) 310 if (!host_)
272 return 0; 311 return 0;
273 scoped_ptr<cc::ScopedUIResource> ui_resource = 312
274 cc::ScopedUIResource::Create(host_.get(), bitmap); 313 cc::UIResourceId id = 0;
275 cc::UIResourceId id = ui_resource->id(); 314 scoped_ptr<cc::UIResourceClient> resource;
276 ui_resource_map_.set(id, ui_resource.Pass()); 315 if (is_transient) {
316 scoped_ptr<TransientUIResource> transient_resource =
317 TransientUIResource::Create(host_.get(), bitmap);
318 id = transient_resource->id();
319 resource = transient_resource.Pass();
320 } else {
321 scoped_ptr<cc::ScopedUIResource> scoped_resource =
322 cc::ScopedUIResource::Create(host_.get(), bitmap);
323 id = scoped_resource->id();
324 resource = scoped_resource.Pass();
325 }
326
327 ui_resource_map_.set(id, resource.Pass());
277 return id; 328 return id;
278 } 329 }
279 330
331 cc::UIResourceId CompositorImpl::GenerateCompressedUIResource(
332 gfx::Size& size,
aelias_OOO_until_Jul13 2014/01/31 05:50:10 const gfx::Size&
powei 2014/02/01 01:30:49 Done.
333 int data_size,
aelias_OOO_until_Jul13 2014/01/31 05:50:10 Please use size_t instead of int.
powei 2014/02/01 01:30:49 Done.
334 void* data,
aelias_OOO_until_Jul13 2014/01/31 05:50:10 void* pixels
powei 2014/02/01 01:30:49 Done.
335 bool is_transient) {
336 SkImageInfo info = {size.width(), size.height(), kPMColor_SkColorType,
337 kPremul_SkAlphaType};
338 skia::RefPtr<SkMallocPixelRef> etc1_pixel_ref =
339 skia::AdoptRef(SkMallocPixelRef::NewAllocate(info, 0, 0));
340 memcpy(etc1_pixel_ref->getAddr(), data, data_size);
341 etc1_pixel_ref->setImmutable();
342 return GenerateUIResource(cc::UIResourceBitmap(etc1_pixel_ref), is_transient);
343 }
344
280 void CompositorImpl::DeleteUIResource(cc::UIResourceId resource_id) { 345 void CompositorImpl::DeleteUIResource(cc::UIResourceId resource_id) {
281 UIResourceMap::iterator it = ui_resource_map_.find(resource_id); 346 UIResourceMap::iterator it = ui_resource_map_.find(resource_id);
282 if (it != ui_resource_map_.end()) 347 if (it != ui_resource_map_.end())
283 ui_resource_map_.erase(it); 348 ui_resource_map_.erase(it);
284 } 349 }
285 350
286 GLuint CompositorImpl::GenerateTexture(gfx::JavaBitmap& bitmap) {
287 unsigned int texture_id = BuildBasicTexture();
288 gpu::gles2::GLES2Interface* gl =
289 ImageTransportFactoryAndroid::GetInstance()->GetContextGL();
290 if (texture_id == 0u)
291 return 0u;
292 GLenum format = GetGLFormatForBitmap(bitmap);
293 GLenum type = GetGLTypeForBitmap(bitmap);
294
295 gl->TexImage2D(GL_TEXTURE_2D,
296 0,
297 format,
298 bitmap.size().width(),
299 bitmap.size().height(),
300 0,
301 format,
302 type,
303 bitmap.pixels());
304 gl->ShallowFlushCHROMIUM();
305 return texture_id;
306 }
307
308 GLuint CompositorImpl::GenerateCompressedTexture(gfx::Size& size,
309 int data_size,
310 void* data) {
311 unsigned int texture_id = BuildBasicTexture();
312 gpu::gles2::GLES2Interface* gl =
313 ImageTransportFactoryAndroid::GetInstance()->GetContextGL();
314 if (texture_id == 0u)
315 return 0u;
316 gl->CompressedTexImage2D(GL_TEXTURE_2D,
317 0,
318 GL_ETC1_RGB8_OES,
319 size.width(),
320 size.height(),
321 0,
322 data_size,
323 data);
324 gl->ShallowFlushCHROMIUM();
325 return texture_id;
326 }
327
328 void CompositorImpl::DeleteTexture(GLuint texture_id) {
329 gpu::gles2::GLES2Interface* gl =
330 ImageTransportFactoryAndroid::GetInstance()->GetContextGL();
331 gl->DeleteTextures(1, &texture_id);
332 gl->ShallowFlushCHROMIUM();
333 }
334
335 bool CompositorImpl::CopyTextureToBitmap(GLuint texture_id,
336 gfx::JavaBitmap& bitmap) {
337 return CopyTextureToBitmap(texture_id, gfx::Rect(bitmap.size()), bitmap);
338 }
339
340 bool CompositorImpl::CopyTextureToBitmap(GLuint texture_id,
341 const gfx::Rect& sub_rect,
342 gfx::JavaBitmap& bitmap) {
343 // The sub_rect should match the bitmap size.
344 DCHECK(bitmap.size() == sub_rect.size());
345 if (bitmap.size() != sub_rect.size() || texture_id == 0) return false;
346
347 GLHelper* helper = ImageTransportFactoryAndroid::GetInstance()->GetGLHelper();
348 helper->ReadbackTextureSync(texture_id,
349 sub_rect,
350 static_cast<unsigned char*> (bitmap.pixels()),
351 SkBitmap::kARGB_8888_Config);
352 return true;
353 }
354
355 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl> 351 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
356 CreateGpuProcessViewContext( 352 CreateGpuProcessViewContext(
357 const blink::WebGraphicsContext3D::Attributes attributes, 353 const blink::WebGraphicsContext3D::Attributes attributes,
358 int surface_id) { 354 int surface_id) {
359 BrowserGpuChannelHostFactory* factory = 355 BrowserGpuChannelHostFactory* factory =
360 BrowserGpuChannelHostFactory::instance(); 356 BrowserGpuChannelHostFactory::instance();
361 CauseForGpuLaunch cause = 357 CauseForGpuLaunch cause =
362 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE; 358 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE;
363 scoped_refptr<GpuChannelHost> gpu_channel_host( 359 scoped_refptr<GpuChannelHost> gpu_channel_host(
364 factory->EstablishGpuChannelSync(cause)); 360 factory->EstablishGpuChannelSync(cause));
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 void CompositorImpl::DidPostSwapBuffers() { 432 void CompositorImpl::DidPostSwapBuffers() {
437 TRACE_EVENT0("compositor", "CompositorImpl::DidPostSwapBuffers"); 433 TRACE_EVENT0("compositor", "CompositorImpl::DidPostSwapBuffers");
438 client_->OnSwapBuffersPosted(); 434 client_->OnSwapBuffersPosted();
439 } 435 }
440 436
441 void CompositorImpl::DidAbortSwapBuffers() { 437 void CompositorImpl::DidAbortSwapBuffers() {
442 TRACE_EVENT0("compositor", "CompositorImpl::DidAbortSwapBuffers"); 438 TRACE_EVENT0("compositor", "CompositorImpl::DidAbortSwapBuffers");
443 client_->OnSwapBuffersCompleted(); 439 client_->OnSwapBuffersCompleted();
444 } 440 }
445 441
446 GLuint CompositorImpl::BuildBasicTexture() {
447 gpu::gles2::GLES2Interface* gl =
448 ImageTransportFactoryAndroid::GetInstance()->GetContextGL();
449 GLuint texture_id = 0u;
450 gl->GenTextures(1, &texture_id);
451 gl->BindTexture(GL_TEXTURE_2D, texture_id);
452 gl->TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
453 gl->TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
454 gl->TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
455 gl->TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
456 return texture_id;
457 }
458
459 GLenum CompositorImpl::GetGLFormatForBitmap(gfx::JavaBitmap& bitmap) {
460 switch (bitmap.format()) {
461 case ANDROID_BITMAP_FORMAT_A_8:
462 return GL_ALPHA;
463 break;
464 case ANDROID_BITMAP_FORMAT_RGBA_4444:
465 return GL_RGBA;
466 break;
467 case ANDROID_BITMAP_FORMAT_RGBA_8888:
468 return GL_RGBA;
469 break;
470 case ANDROID_BITMAP_FORMAT_RGB_565:
471 default:
472 return GL_RGB;
473 }
474 }
475
476 GLenum CompositorImpl::GetGLTypeForBitmap(gfx::JavaBitmap& bitmap) {
477 switch (bitmap.format()) {
478 case ANDROID_BITMAP_FORMAT_A_8:
479 return GL_UNSIGNED_BYTE;
480 break;
481 case ANDROID_BITMAP_FORMAT_RGBA_4444:
482 return GL_UNSIGNED_SHORT_4_4_4_4;
483 break;
484 case ANDROID_BITMAP_FORMAT_RGBA_8888:
485 return GL_UNSIGNED_BYTE;
486 break;
487 case ANDROID_BITMAP_FORMAT_RGB_565:
488 default:
489 return GL_UNSIGNED_SHORT_5_6_5;
490 }
491 }
492
493 void CompositorImpl::DidCommit() { 442 void CompositorImpl::DidCommit() {
494 root_window_->OnCompositingDidCommit(); 443 root_window_->OnCompositingDidCommit();
495 } 444 }
496 445
497 } // namespace content 446 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698