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" | |
42 #include "ui/base/android/window_android.h" | 41 #include "ui/base/android/window_android.h" |
43 #include "ui/gfx/android/device_display_info.h" | 42 #include "ui/gfx/android/device_display_info.h" |
44 #include "ui/gfx/android/java_bitmap.h" | 43 #include "ui/gfx/android/java_bitmap.h" |
45 #include "ui/gfx/frame_time.h" | 44 #include "ui/gfx/frame_time.h" |
46 #include "webkit/common/gpu/context_provider_in_process.h" | 45 #include "webkit/common/gpu/context_provider_in_process.h" |
47 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.
h" | 46 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.
h" |
48 | 47 |
49 namespace gfx { | 48 namespace gfx { |
50 class JavaBitmap; | 49 class JavaBitmap; |
51 } | 50 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 context_provider_.get()); | 83 context_provider_.get()); |
85 content::CommandBufferProxyImpl* command_buffer_proxy = | 84 content::CommandBufferProxyImpl* command_buffer_proxy = |
86 provider_command_buffer->GetCommandBufferProxy(); | 85 provider_command_buffer->GetCommandBufferProxy(); |
87 DCHECK(command_buffer_proxy); | 86 DCHECK(command_buffer_proxy); |
88 command_buffer_proxy->SetLatencyInfo(frame->metadata.latency_info); | 87 command_buffer_proxy->SetLatencyInfo(frame->metadata.latency_info); |
89 | 88 |
90 OutputSurface::SwapBuffers(frame); | 89 OutputSurface::SwapBuffers(frame); |
91 } | 90 } |
92 }; | 91 }; |
93 | 92 |
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 | |
134 static bool g_initialized = false; | 93 static bool g_initialized = false; |
135 | 94 |
136 } // anonymous namespace | 95 } // anonymous namespace |
137 | 96 |
138 namespace content { | 97 namespace content { |
139 | 98 |
140 typedef std::map<int, base::android::ScopedJavaGlobalRef<jobject> > | 99 typedef std::map<int, base::android::ScopedJavaGlobalRef<jobject> > |
141 SurfaceMap; | 100 SurfaceMap; |
142 static base::LazyInstance<SurfaceMap> | 101 static base::LazyInstance<SurfaceMap> |
143 g_surface_map = LAZY_INSTANCE_INITIALIZER; | 102 g_surface_map = LAZY_INSTANCE_INITIALIZER; |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 host_->set_has_transparent_background(flag); | 259 host_->set_has_transparent_background(flag); |
301 } | 260 } |
302 | 261 |
303 bool CompositorImpl::CompositeAndReadback(void *pixels, const gfx::Rect& rect) { | 262 bool CompositorImpl::CompositeAndReadback(void *pixels, const gfx::Rect& rect) { |
304 if (host_) | 263 if (host_) |
305 return host_->CompositeAndReadback(pixels, rect); | 264 return host_->CompositeAndReadback(pixels, rect); |
306 else | 265 else |
307 return false; | 266 return false; |
308 } | 267 } |
309 | 268 |
310 cc::UIResourceId CompositorImpl::GenerateUIResourceFromUIResourceBitmap( | 269 cc::UIResourceId CompositorImpl::GenerateUIResource( |
311 const cc::UIResourceBitmap& bitmap, | 270 const cc::UIResourceBitmap& bitmap) { |
312 bool is_transient) { | |
313 if (!host_) | 271 if (!host_) |
314 return 0; | 272 return 0; |
315 | 273 scoped_ptr<cc::ScopedUIResource> ui_resource = |
316 cc::UIResourceId id = 0; | 274 cc::ScopedUIResource::Create(host_.get(), bitmap); |
317 scoped_ptr<cc::UIResourceClient> resource; | 275 cc::UIResourceId id = ui_resource->id(); |
318 if (is_transient) { | 276 ui_resource_map_.set(id, ui_resource.Pass()); |
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()); | |
331 return id; | 277 return id; |
332 } | 278 } |
333 | 279 |
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 | |
360 void CompositorImpl::DeleteUIResource(cc::UIResourceId resource_id) { | 280 void CompositorImpl::DeleteUIResource(cc::UIResourceId resource_id) { |
361 UIResourceMap::iterator it = ui_resource_map_.find(resource_id); | 281 UIResourceMap::iterator it = ui_resource_map_.find(resource_id); |
362 if (it != ui_resource_map_.end()) | 282 if (it != ui_resource_map_.end()) |
363 ui_resource_map_.erase(it); | 283 ui_resource_map_.erase(it); |
364 } | 284 } |
365 | 285 |
| 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 |
366 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl> | 355 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl> |
367 CreateGpuProcessViewContext( | 356 CreateGpuProcessViewContext( |
368 const blink::WebGraphicsContext3D::Attributes attributes, | 357 const blink::WebGraphicsContext3D::Attributes attributes, |
369 int surface_id) { | 358 int surface_id) { |
370 BrowserGpuChannelHostFactory* factory = | 359 BrowserGpuChannelHostFactory* factory = |
371 BrowserGpuChannelHostFactory::instance(); | 360 BrowserGpuChannelHostFactory::instance(); |
372 CauseForGpuLaunch cause = | 361 CauseForGpuLaunch cause = |
373 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE; | 362 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE; |
374 scoped_refptr<GpuChannelHost> gpu_channel_host( | 363 scoped_refptr<GpuChannelHost> gpu_channel_host( |
375 factory->EstablishGpuChannelSync(cause)); | 364 factory->EstablishGpuChannelSync(cause)); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
447 void CompositorImpl::DidPostSwapBuffers() { | 436 void CompositorImpl::DidPostSwapBuffers() { |
448 TRACE_EVENT0("compositor", "CompositorImpl::DidPostSwapBuffers"); | 437 TRACE_EVENT0("compositor", "CompositorImpl::DidPostSwapBuffers"); |
449 client_->OnSwapBuffersPosted(); | 438 client_->OnSwapBuffersPosted(); |
450 } | 439 } |
451 | 440 |
452 void CompositorImpl::DidAbortSwapBuffers() { | 441 void CompositorImpl::DidAbortSwapBuffers() { |
453 TRACE_EVENT0("compositor", "CompositorImpl::DidAbortSwapBuffers"); | 442 TRACE_EVENT0("compositor", "CompositorImpl::DidAbortSwapBuffers"); |
454 client_->OnSwapBuffersCompleted(); | 443 client_->OnSwapBuffersCompleted(); |
455 } | 444 } |
456 | 445 |
| 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 |
457 void CompositorImpl::DidCommit() { | 493 void CompositorImpl::DidCommit() { |
458 root_window_->OnCompositingDidCommit(); | 494 root_window_->OnCompositingDidCommit(); |
459 } | 495 } |
460 | 496 |
461 } // namespace content | 497 } // namespace content |
OLD | NEW |