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 |
(...skipping 20 matching lines...) Expand all Loading... |
31 #include "content/common/gpu/client/context_provider_command_buffer.h" | 31 #include "content/common/gpu/client/context_provider_command_buffer.h" |
32 #include "content/common/gpu/client/gl_helper.h" | 32 #include "content/common/gpu/client/gl_helper.h" |
33 #include "content/common/gpu/client/gpu_channel_host.h" | 33 #include "content/common/gpu/client/gpu_channel_host.h" |
34 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" | 34 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" |
35 #include "content/common/gpu/gpu_process_launch_causes.h" | 35 #include "content/common/gpu/gpu_process_launch_causes.h" |
36 #include "content/public/browser/android/compositor_client.h" | 36 #include "content/public/browser/android/compositor_client.h" |
37 #include "content/public/common/content_switches.h" | 37 #include "content/public/common/content_switches.h" |
38 #include "gpu/command_buffer/client/gles2_interface.h" | 38 #include "gpu/command_buffer/client/gles2_interface.h" |
39 #include "third_party/khronos/GLES2/gl2.h" | 39 #include "third_party/khronos/GLES2/gl2.h" |
40 #include "third_party/khronos/GLES2/gl2ext.h" | 40 #include "third_party/khronos/GLES2/gl2ext.h" |
| 41 #include "third_party/skia/include/core/SkMallocPixelRef.h" |
41 #include "ui/base/android/window_android.h" | 42 #include "ui/base/android/window_android.h" |
42 #include "ui/gfx/android/device_display_info.h" | 43 #include "ui/gfx/android/device_display_info.h" |
43 #include "ui/gfx/android/java_bitmap.h" | 44 #include "ui/gfx/android/java_bitmap.h" |
44 #include "ui/gfx/frame_time.h" | 45 #include "ui/gfx/frame_time.h" |
45 #include "webkit/common/gpu/context_provider_in_process.h" | 46 #include "webkit/common/gpu/context_provider_in_process.h" |
46 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.
h" | 47 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.
h" |
47 | 48 |
48 namespace gfx { | 49 namespace gfx { |
49 class JavaBitmap; | 50 class JavaBitmap; |
50 } | 51 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 context_provider_.get()); | 84 context_provider_.get()); |
84 content::CommandBufferProxyImpl* command_buffer_proxy = | 85 content::CommandBufferProxyImpl* command_buffer_proxy = |
85 provider_command_buffer->GetCommandBufferProxy(); | 86 provider_command_buffer->GetCommandBufferProxy(); |
86 DCHECK(command_buffer_proxy); | 87 DCHECK(command_buffer_proxy); |
87 command_buffer_proxy->SetLatencyInfo(frame->metadata.latency_info); | 88 command_buffer_proxy->SetLatencyInfo(frame->metadata.latency_info); |
88 | 89 |
89 OutputSurface::SwapBuffers(frame); | 90 OutputSurface::SwapBuffers(frame); |
90 } | 91 } |
91 }; | 92 }; |
92 | 93 |
| 94 class TransientUIResource : public cc::ScopedUIResource { |
| 95 public: |
| 96 static scoped_ptr<TransientUIResource> Create( |
| 97 cc::LayerTreeHost* host, |
| 98 const cc::UIResourceBitmap& bitmap) { |
| 99 return make_scoped_ptr(new TransientUIResource(host, bitmap)); |
| 100 } |
| 101 |
| 102 virtual cc::UIResourceBitmap GetBitmap(cc::UIResourceId uid, |
| 103 bool resource_lost) OVERRIDE { |
| 104 if (!retrieved_) { |
| 105 cc::UIResourceBitmap old_bitmap(bitmap_); |
| 106 |
| 107 // Return a place holder for all following calls to GetBitmap. |
| 108 SkBitmap tiny_bitmap; |
| 109 SkCanvas canvas(tiny_bitmap); |
| 110 tiny_bitmap.setConfig( |
| 111 SkBitmap::kARGB_8888_Config, 1, 1, 0, kOpaque_SkAlphaType); |
| 112 tiny_bitmap.allocPixels(); |
| 113 canvas.drawColor(SK_ColorWHITE); |
| 114 tiny_bitmap.setImmutable(); |
| 115 |
| 116 // Release our reference of the true bitmap. |
| 117 bitmap_ = cc::UIResourceBitmap(tiny_bitmap); |
| 118 |
| 119 retrieved_ = true; |
| 120 return old_bitmap; |
| 121 } |
| 122 return bitmap_; |
| 123 } |
| 124 |
| 125 protected: |
| 126 TransientUIResource(cc::LayerTreeHost* host, |
| 127 const cc::UIResourceBitmap& bitmap) |
| 128 : cc::ScopedUIResource(host, bitmap), retrieved_(false) {} |
| 129 |
| 130 private: |
| 131 bool retrieved_; |
| 132 }; |
| 133 |
93 static bool g_initialized = false; | 134 static bool g_initialized = false; |
94 | 135 |
95 } // anonymous namespace | 136 } // anonymous namespace |
96 | 137 |
97 namespace content { | 138 namespace content { |
98 | 139 |
99 typedef std::map<int, base::android::ScopedJavaGlobalRef<jobject> > | 140 typedef std::map<int, base::android::ScopedJavaGlobalRef<jobject> > |
100 SurfaceMap; | 141 SurfaceMap; |
101 static base::LazyInstance<SurfaceMap> | 142 static base::LazyInstance<SurfaceMap> |
102 g_surface_map = LAZY_INSTANCE_INITIALIZER; | 143 g_surface_map = LAZY_INSTANCE_INITIALIZER; |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 host_->set_has_transparent_background(flag); | 300 host_->set_has_transparent_background(flag); |
260 } | 301 } |
261 | 302 |
262 bool CompositorImpl::CompositeAndReadback(void *pixels, const gfx::Rect& rect) { | 303 bool CompositorImpl::CompositeAndReadback(void *pixels, const gfx::Rect& rect) { |
263 if (host_) | 304 if (host_) |
264 return host_->CompositeAndReadback(pixels, rect); | 305 return host_->CompositeAndReadback(pixels, rect); |
265 else | 306 else |
266 return false; | 307 return false; |
267 } | 308 } |
268 | 309 |
269 cc::UIResourceId CompositorImpl::GenerateUIResource( | 310 cc::UIResourceId CompositorImpl::GenerateUIResourceFromUIResourceBitmap( |
270 const cc::UIResourceBitmap& bitmap) { | 311 const cc::UIResourceBitmap& bitmap, |
| 312 bool is_transient) { |
271 if (!host_) | 313 if (!host_) |
272 return 0; | 314 return 0; |
273 scoped_ptr<cc::ScopedUIResource> ui_resource = | 315 |
274 cc::ScopedUIResource::Create(host_.get(), bitmap); | 316 cc::UIResourceId id = 0; |
275 cc::UIResourceId id = ui_resource->id(); | 317 scoped_ptr<cc::UIResourceClient> resource; |
276 ui_resource_map_.set(id, ui_resource.Pass()); | 318 if (is_transient) { |
| 319 scoped_ptr<TransientUIResource> transient_resource = |
| 320 TransientUIResource::Create(host_.get(), bitmap); |
| 321 id = transient_resource->id(); |
| 322 resource = transient_resource.Pass(); |
| 323 } else { |
| 324 scoped_ptr<cc::ScopedUIResource> scoped_resource = |
| 325 cc::ScopedUIResource::Create(host_.get(), bitmap); |
| 326 id = scoped_resource->id(); |
| 327 resource = scoped_resource.Pass(); |
| 328 } |
| 329 |
| 330 ui_resource_map_.set(id, resource.Pass()); |
277 return id; | 331 return id; |
278 } | 332 } |
279 | 333 |
| 334 cc::UIResourceId CompositorImpl::GenerateUIResource(const SkBitmap& bitmap, |
| 335 bool is_transient) { |
| 336 return GenerateUIResourceFromUIResourceBitmap(cc::UIResourceBitmap(bitmap), |
| 337 is_transient); |
| 338 } |
| 339 |
| 340 cc::UIResourceId CompositorImpl::GenerateCompressedUIResource( |
| 341 const gfx::Size& size, |
| 342 void* pixels, |
| 343 bool is_transient) { |
| 344 DCHECK_LT(0, size.width()); |
| 345 DCHECK_LT(0, size.height()); |
| 346 DCHECK_EQ(0, size.width() % 4); |
| 347 DCHECK_EQ(0, size.height() % 4); |
| 348 |
| 349 size_t data_size = size.width() * size.height() / 2; |
| 350 SkImageInfo info = {size.width(), size.height() / 2, kAlpha_8_SkColorType, |
| 351 kPremul_SkAlphaType}; |
| 352 skia::RefPtr<SkMallocPixelRef> etc1_pixel_ref = |
| 353 skia::AdoptRef(SkMallocPixelRef::NewAllocate(info, 0, 0)); |
| 354 memcpy(etc1_pixel_ref->getAddr(), pixels, data_size); |
| 355 etc1_pixel_ref->setImmutable(); |
| 356 return GenerateUIResourceFromUIResourceBitmap( |
| 357 cc::UIResourceBitmap(etc1_pixel_ref, size), is_transient); |
| 358 } |
| 359 |
280 void CompositorImpl::DeleteUIResource(cc::UIResourceId resource_id) { | 360 void CompositorImpl::DeleteUIResource(cc::UIResourceId resource_id) { |
281 UIResourceMap::iterator it = ui_resource_map_.find(resource_id); | 361 UIResourceMap::iterator it = ui_resource_map_.find(resource_id); |
282 if (it != ui_resource_map_.end()) | 362 if (it != ui_resource_map_.end()) |
283 ui_resource_map_.erase(it); | 363 ui_resource_map_.erase(it); |
284 } | 364 } |
285 | 365 |
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> | 366 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl> |
356 CreateGpuProcessViewContext( | 367 CreateGpuProcessViewContext( |
357 const blink::WebGraphicsContext3D::Attributes attributes, | 368 const blink::WebGraphicsContext3D::Attributes attributes, |
358 int surface_id) { | 369 int surface_id) { |
359 BrowserGpuChannelHostFactory* factory = | 370 BrowserGpuChannelHostFactory* factory = |
360 BrowserGpuChannelHostFactory::instance(); | 371 BrowserGpuChannelHostFactory::instance(); |
361 CauseForGpuLaunch cause = | 372 CauseForGpuLaunch cause = |
362 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE; | 373 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE; |
363 scoped_refptr<GpuChannelHost> gpu_channel_host( | 374 scoped_refptr<GpuChannelHost> gpu_channel_host( |
364 factory->EstablishGpuChannelSync(cause)); | 375 factory->EstablishGpuChannelSync(cause)); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 void CompositorImpl::DidPostSwapBuffers() { | 447 void CompositorImpl::DidPostSwapBuffers() { |
437 TRACE_EVENT0("compositor", "CompositorImpl::DidPostSwapBuffers"); | 448 TRACE_EVENT0("compositor", "CompositorImpl::DidPostSwapBuffers"); |
438 client_->OnSwapBuffersPosted(); | 449 client_->OnSwapBuffersPosted(); |
439 } | 450 } |
440 | 451 |
441 void CompositorImpl::DidAbortSwapBuffers() { | 452 void CompositorImpl::DidAbortSwapBuffers() { |
442 TRACE_EVENT0("compositor", "CompositorImpl::DidAbortSwapBuffers"); | 453 TRACE_EVENT0("compositor", "CompositorImpl::DidAbortSwapBuffers"); |
443 client_->OnSwapBuffersCompleted(); | 454 client_->OnSwapBuffersCompleted(); |
444 } | 455 } |
445 | 456 |
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() { | 457 void CompositorImpl::DidCommit() { |
494 root_window_->OnCompositingDidCommit(); | 458 root_window_->OnCompositingDidCommit(); |
495 } | 459 } |
496 | 460 |
497 } // namespace content | 461 } // namespace content |
OLD | NEW |