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/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" |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
205 ANativeWindow_release(window); | 206 ANativeWindow_release(window); |
206 { | 207 { |
207 base::AutoLock lock(g_surface_map_lock.Get()); | 208 base::AutoLock lock(g_surface_map_lock.Get()); |
208 g_surface_map.Get().insert(std::make_pair(surface_id_, j_surface)); | 209 g_surface_map.Get().insert(std::make_pair(surface_id_, j_surface)); |
209 } | 210 } |
210 } | 211 } |
211 } | 212 } |
212 | 213 |
213 void CompositorImpl::SetVisible(bool visible) { | 214 void CompositorImpl::SetVisible(bool visible) { |
214 if (!visible) { | 215 if (!visible) { |
215 ui_resource_map_.clear(); | 216 scoped_ui_resource_map_.clear(); |
216 host_.reset(); | 217 host_.reset(); |
217 client_->UIResourcesAreInvalid(); | 218 client_->UIResourcesAreInvalid(); |
218 } else if (!host_) { | 219 } else if (!host_) { |
219 cc::LayerTreeSettings settings; | 220 cc::LayerTreeSettings settings; |
220 settings.refresh_rate = 60.0; | 221 settings.refresh_rate = 60.0; |
221 settings.impl_side_painting = false; | 222 settings.impl_side_painting = false; |
222 settings.allow_antialiasing = false; | 223 settings.allow_antialiasing = false; |
223 settings.calculate_top_controls_position = false; | 224 settings.calculate_top_controls_position = false; |
224 settings.top_controls_height = 0.f; | 225 settings.top_controls_height = 0.f; |
225 settings.use_memory_management = false; | 226 settings.use_memory_management = false; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
263 if (host_) | 264 if (host_) |
264 return host_->CompositeAndReadback(pixels, rect); | 265 return host_->CompositeAndReadback(pixels, rect); |
265 else | 266 else |
266 return false; | 267 return false; |
267 } | 268 } |
268 | 269 |
269 cc::UIResourceId CompositorImpl::GenerateUIResource( | 270 cc::UIResourceId CompositorImpl::GenerateUIResource( |
270 const cc::UIResourceBitmap& bitmap) { | 271 const cc::UIResourceBitmap& bitmap) { |
271 if (!host_) | 272 if (!host_) |
272 return 0; | 273 return 0; |
273 scoped_ptr<cc::ScopedUIResource> ui_resource = | 274 |
274 cc::ScopedUIResource::Create(host_.get(), bitmap); | 275 cc::UIResourceId id = 0; |
275 cc::UIResourceId id = ui_resource->id(); | 276 if (bitmap.GetFormat() == cc::UIResourceBitmap::RGBA8) { |
276 ui_resource_map_.set(id, ui_resource.Pass()); | 277 scoped_ptr<cc::ScopedUIResource> resource = |
278 cc::ScopedUIResource::Create(host_.get(), bitmap); | |
279 id = resource->id(); | |
280 scoped_ui_resource_map_.set(id, resource.Pass()); | |
281 } else if (bitmap.GetFormat() == cc::UIResourceBitmap::ETC1) { | |
282 transient_ui_bitmap_ = make_scoped_ptr(new cc::UIResourceBitmap(bitmap)); | |
no sievers
2014/01/29 21:13:48
as discussed: an alternate 'GenerateUIResource(bas
powei
2014/01/30 00:54:12
So I opted for the boolean flag for now since aeli
| |
283 id = host_->CreateUIResource(this); | |
284 } | |
285 | |
277 return id; | 286 return id; |
278 } | 287 } |
279 | 288 |
280 void CompositorImpl::DeleteUIResource(cc::UIResourceId resource_id) { | 289 void CompositorImpl::DeleteUIResource(cc::UIResourceId resource_id) { |
281 UIResourceMap::iterator it = ui_resource_map_.find(resource_id); | 290 if (!host_) |
282 if (it != ui_resource_map_.end()) | 291 return; |
283 ui_resource_map_.erase(it); | 292 |
293 ScopedUIResourceMap::iterator it = scoped_ui_resource_map_.find(resource_id); | |
294 if (it != scoped_ui_resource_map_.end()) { | |
295 scoped_ui_resource_map_.erase(it); | |
296 } else { | |
297 host_->DeleteUIResource(resource_id); | |
298 } | |
284 } | 299 } |
285 | 300 |
286 GLuint CompositorImpl::GenerateTexture(gfx::JavaBitmap& bitmap) { | 301 cc::UIResourceBitmap CompositorImpl::GetBitmap(cc::UIResourceId resource_id, |
287 unsigned int texture_id = BuildBasicTexture(); | 302 bool resource_lost) { |
288 gpu::gles2::GLES2Interface* gl = | 303 if (transient_ui_bitmap_) |
289 ImageTransportFactoryAndroid::GetInstance()->GetContextGL(); | 304 return cc::UIResourceBitmap(*transient_ui_bitmap_.release()); |
290 if (texture_id == 0u) | |
291 return 0u; | |
292 GLenum format = GetGLFormatForBitmap(bitmap); | |
293 GLenum type = GetGLTypeForBitmap(bitmap); | |
294 | 305 |
295 gl->TexImage2D(GL_TEXTURE_2D, | 306 // When resource has been lost. |
296 0, | 307 DCHECK(resource_lost); |
297 format, | 308 SkBitmap tiny_bitmap; |
298 bitmap.size().width(), | 309 tiny_bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); |
299 bitmap.size().height(), | 310 tiny_bitmap.allocPixels(); |
300 0, | 311 tiny_bitmap.setImmutable(); |
301 format, | |
302 type, | |
303 bitmap.pixels()); | |
304 gl->ShallowFlushCHROMIUM(); | |
305 return texture_id; | |
306 } | |
307 | 312 |
308 GLuint CompositorImpl::GenerateCompressedTexture(gfx::Size& size, | 313 return cc::UIResourceBitmap(tiny_bitmap); |
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 } | 314 } |
354 | 315 |
355 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl> | 316 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl> |
356 CreateGpuProcessViewContext( | 317 CreateGpuProcessViewContext( |
357 const blink::WebGraphicsContext3D::Attributes attributes, | 318 const blink::WebGraphicsContext3D::Attributes attributes, |
358 int surface_id) { | 319 int surface_id) { |
359 BrowserGpuChannelHostFactory* factory = | 320 BrowserGpuChannelHostFactory* factory = |
360 BrowserGpuChannelHostFactory::instance(); | 321 BrowserGpuChannelHostFactory::instance(); |
361 CauseForGpuLaunch cause = | 322 CauseForGpuLaunch cause = |
362 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE; | 323 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
436 void CompositorImpl::DidPostSwapBuffers() { | 397 void CompositorImpl::DidPostSwapBuffers() { |
437 TRACE_EVENT0("compositor", "CompositorImpl::DidPostSwapBuffers"); | 398 TRACE_EVENT0("compositor", "CompositorImpl::DidPostSwapBuffers"); |
438 client_->OnSwapBuffersPosted(); | 399 client_->OnSwapBuffersPosted(); |
439 } | 400 } |
440 | 401 |
441 void CompositorImpl::DidAbortSwapBuffers() { | 402 void CompositorImpl::DidAbortSwapBuffers() { |
442 TRACE_EVENT0("compositor", "CompositorImpl::DidAbortSwapBuffers"); | 403 TRACE_EVENT0("compositor", "CompositorImpl::DidAbortSwapBuffers"); |
443 client_->OnSwapBuffersCompleted(); | 404 client_->OnSwapBuffersCompleted(); |
444 } | 405 } |
445 | 406 |
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() { | 407 void CompositorImpl::DidCommit() { |
494 root_window_->OnCompositingDidCommit(); | 408 root_window_->OnCompositingDidCommit(); |
495 } | 409 } |
496 | 410 |
497 } // namespace content | 411 } // namespace content |
OLD | NEW |