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

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

Issue 287593002: android: content::UIResourceProvider and content::UIResourceClientAndroid (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressed dtrainor comments Created 6 years, 7 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 9
10 #include "base/android/jni_android.h" 10 #include "base/android/jni_android.h"
11 #include "base/android/scoped_java_ref.h" 11 #include "base/android/scoped_java_ref.h"
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/containers/hash_tables.h" 14 #include "base/containers/hash_tables.h"
15 #include "base/lazy_instance.h" 15 #include "base/lazy_instance.h"
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/single_thread_task_runner.h" 17 #include "base/single_thread_task_runner.h"
18 #include "base/synchronization/lock.h" 18 #include "base/synchronization/lock.h"
19 #include "base/threading/thread.h" 19 #include "base/threading/thread.h"
20 #include "base/threading/thread_checker.h" 20 #include "base/threading/thread_checker.h"
21 #include "cc/base/switches.h" 21 #include "cc/base/switches.h"
22 #include "cc/input/input_handler.h" 22 #include "cc/input/input_handler.h"
23 #include "cc/layers/layer.h" 23 #include "cc/layers/layer.h"
24 #include "cc/output/compositor_frame.h" 24 #include "cc/output/compositor_frame.h"
25 #include "cc/output/context_provider.h" 25 #include "cc/output/context_provider.h"
26 #include "cc/output/output_surface.h" 26 #include "cc/output/output_surface.h"
27 #include "cc/resources/scoped_ui_resource.h"
28 #include "cc/resources/ui_resource_bitmap.h"
29 #include "cc/trees/layer_tree_host.h" 27 #include "cc/trees/layer_tree_host.h"
30 #include "content/browser/android/child_process_launcher_android.h" 28 #include "content/browser/android/child_process_launcher_android.h"
31 #include "content/browser/gpu/browser_gpu_channel_host_factory.h" 29 #include "content/browser/gpu/browser_gpu_channel_host_factory.h"
32 #include "content/browser/gpu/gpu_surface_tracker.h" 30 #include "content/browser/gpu/gpu_surface_tracker.h"
33 #include "content/common/gpu/client/command_buffer_proxy_impl.h" 31 #include "content/common/gpu/client/command_buffer_proxy_impl.h"
34 #include "content/common/gpu/client/context_provider_command_buffer.h" 32 #include "content/common/gpu/client/context_provider_command_buffer.h"
35 #include "content/common/gpu/client/gl_helper.h" 33 #include "content/common/gpu/client/gl_helper.h"
36 #include "content/common/gpu/client/gpu_channel_host.h" 34 #include "content/common/gpu/client/gpu_channel_host.h"
37 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" 35 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
38 #include "content/common/gpu/gpu_process_launch_causes.h" 36 #include "content/common/gpu/gpu_process_launch_causes.h"
39 #include "content/common/host_shared_bitmap_manager.h" 37 #include "content/common/host_shared_bitmap_manager.h"
40 #include "content/public/browser/android/compositor_client.h" 38 #include "content/public/browser/android/compositor_client.h"
41 #include "gpu/command_buffer/client/gles2_interface.h" 39 #include "gpu/command_buffer/client/gles2_interface.h"
42 #include "third_party/khronos/GLES2/gl2.h" 40 #include "third_party/khronos/GLES2/gl2.h"
43 #include "third_party/khronos/GLES2/gl2ext.h" 41 #include "third_party/khronos/GLES2/gl2ext.h"
44 #include "third_party/skia/include/core/SkMallocPixelRef.h" 42 #include "third_party/skia/include/core/SkMallocPixelRef.h"
45 #include "ui/base/android/window_android.h" 43 #include "ui/base/android/window_android.h"
46 #include "ui/gfx/android/device_display_info.h" 44 #include "ui/gfx/android/device_display_info.h"
47 #include "ui/gfx/android/java_bitmap.h"
48 #include "ui/gfx/frame_time.h" 45 #include "ui/gfx/frame_time.h"
49 #include "ui/gl/android/surface_texture.h" 46 #include "ui/gl/android/surface_texture.h"
50 #include "ui/gl/android/surface_texture_tracker.h" 47 #include "ui/gl/android/surface_texture_tracker.h"
51 #include "webkit/common/gpu/context_provider_in_process.h" 48 #include "webkit/common/gpu/context_provider_in_process.h"
52 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl. h" 49 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl. h"
53 50
54 namespace gfx {
55 class JavaBitmap;
56 }
57
58 namespace { 51 namespace {
59 52
60 // Used to override capabilities_.adjust_deadline_for_parent to false 53 // Used to override capabilities_.adjust_deadline_for_parent to false
61 class OutputSurfaceWithoutParent : public cc::OutputSurface { 54 class OutputSurfaceWithoutParent : public cc::OutputSurface {
62 public: 55 public:
63 OutputSurfaceWithoutParent(const scoped_refptr< 56 OutputSurfaceWithoutParent(const scoped_refptr<
64 content::ContextProviderCommandBuffer>& context_provider) 57 content::ContextProviderCommandBuffer>& context_provider)
65 : cc::OutputSurface(context_provider) { 58 : cc::OutputSurface(context_provider) {
66 capabilities_.adjust_deadline_for_parent = false; 59 capabilities_.adjust_deadline_for_parent = false;
67 } 60 }
68 61
69 virtual void SwapBuffers(cc::CompositorFrame* frame) OVERRIDE { 62 virtual void SwapBuffers(cc::CompositorFrame* frame) OVERRIDE {
70 content::ContextProviderCommandBuffer* provider_command_buffer = 63 content::ContextProviderCommandBuffer* provider_command_buffer =
71 static_cast<content::ContextProviderCommandBuffer*>( 64 static_cast<content::ContextProviderCommandBuffer*>(
72 context_provider_.get()); 65 context_provider_.get());
73 content::CommandBufferProxyImpl* command_buffer_proxy = 66 content::CommandBufferProxyImpl* command_buffer_proxy =
74 provider_command_buffer->GetCommandBufferProxy(); 67 provider_command_buffer->GetCommandBufferProxy();
75 DCHECK(command_buffer_proxy); 68 DCHECK(command_buffer_proxy);
76 command_buffer_proxy->SetLatencyInfo(frame->metadata.latency_info); 69 command_buffer_proxy->SetLatencyInfo(frame->metadata.latency_info);
77 70
78 OutputSurface::SwapBuffers(frame); 71 OutputSurface::SwapBuffers(frame);
79 } 72 }
80 }; 73 };
81 74
82 class TransientUIResource : public cc::ScopedUIResource {
83 public:
84 static scoped_ptr<TransientUIResource> Create(
85 cc::LayerTreeHost* host,
86 const cc::UIResourceBitmap& bitmap) {
87 return make_scoped_ptr(new TransientUIResource(host, bitmap));
88 }
89
90 virtual cc::UIResourceBitmap GetBitmap(cc::UIResourceId uid,
91 bool resource_lost) OVERRIDE {
92 if (!retrieved_) {
93 cc::UIResourceBitmap old_bitmap(bitmap_);
94
95 // Return a place holder for all following calls to GetBitmap.
96 SkBitmap tiny_bitmap;
97 SkCanvas canvas(tiny_bitmap);
98 tiny_bitmap.setConfig(
99 SkBitmap::kARGB_8888_Config, 1, 1, 0, kOpaque_SkAlphaType);
100 tiny_bitmap.allocPixels();
101 canvas.drawColor(SK_ColorWHITE);
102 tiny_bitmap.setImmutable();
103
104 // Release our reference of the true bitmap.
105 bitmap_ = cc::UIResourceBitmap(tiny_bitmap);
106
107 retrieved_ = true;
108 return old_bitmap;
109 }
110 return bitmap_;
111 }
112
113 protected:
114 TransientUIResource(cc::LayerTreeHost* host,
115 const cc::UIResourceBitmap& bitmap)
116 : cc::ScopedUIResource(host, bitmap), retrieved_(false) {}
117
118 private:
119 bool retrieved_;
120 };
121
122 class SurfaceTextureTrackerImpl : public gfx::SurfaceTextureTracker { 75 class SurfaceTextureTrackerImpl : public gfx::SurfaceTextureTracker {
123 public: 76 public:
124 SurfaceTextureTrackerImpl() : next_surface_texture_id_(1) { 77 SurfaceTextureTrackerImpl() : next_surface_texture_id_(1) {
125 thread_checker_.DetachFromThread(); 78 thread_checker_.DetachFromThread();
126 } 79 }
127 80
128 // Overridden from gfx::SurfaceTextureTracker: 81 // Overridden from gfx::SurfaceTextureTracker:
129 virtual scoped_refptr<gfx::SurfaceTexture> AcquireSurfaceTexture( 82 virtual scoped_refptr<gfx::SurfaceTexture> AcquireSurfaceTexture(
130 int primary_id, 83 int primary_id,
131 int secondary_id) OVERRIDE { 84 int secondary_id) OVERRIDE {
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 ImageTransportFactoryAndroid::RemoveObserver(this); 203 ImageTransportFactoryAndroid::RemoveObserver(this);
251 // Clean-up any surface references. 204 // Clean-up any surface references.
252 SetSurface(NULL); 205 SetSurface(NULL);
253 } 206 }
254 207
255 void CompositorImpl::Composite() { 208 void CompositorImpl::Composite() {
256 if (host_) 209 if (host_)
257 host_->Composite(gfx::FrameTime::Now()); 210 host_->Composite(gfx::FrameTime::Now());
258 } 211 }
259 212
213 UIResourceProvider* CompositorImpl::GetUIResourceProvider() {
214 return &ui_resource_provider_;
215 }
216
260 void CompositorImpl::SetRootLayer(scoped_refptr<cc::Layer> root_layer) { 217 void CompositorImpl::SetRootLayer(scoped_refptr<cc::Layer> root_layer) {
261 root_layer_->RemoveAllChildren(); 218 root_layer_->RemoveAllChildren();
262 if (root_layer) 219 if (root_layer)
263 root_layer_->AddChild(root_layer); 220 root_layer_->AddChild(root_layer);
264 } 221 }
265 222
266 void CompositorImpl::SetWindowSurface(ANativeWindow* window) { 223 void CompositorImpl::SetWindowSurface(ANativeWindow* window) {
267 GpuSurfaceTracker* tracker = GpuSurfaceTracker::Get(); 224 GpuSurfaceTracker* tracker = GpuSurfaceTracker::Get();
268 225
269 if (window_) { 226 if (window_) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 } 262 }
306 if (window) { 263 if (window) {
307 SetWindowSurface(window); 264 SetWindowSurface(window);
308 ANativeWindow_release(window); 265 ANativeWindow_release(window);
309 content::RegisterViewSurface(surface_id_, j_surface.obj()); 266 content::RegisterViewSurface(surface_id_, j_surface.obj());
310 } 267 }
311 } 268 }
312 269
313 void CompositorImpl::SetVisible(bool visible) { 270 void CompositorImpl::SetVisible(bool visible) {
314 if (!visible) { 271 if (!visible) {
315 ui_resource_map_.clear(); 272 ui_resource_provider_.SetLayerTreeHost(NULL);
316 host_.reset(); 273 host_.reset();
317 client_->UIResourcesAreInvalid();
318 } else if (!host_) { 274 } else if (!host_) {
319 cc::LayerTreeSettings settings; 275 cc::LayerTreeSettings settings;
320 settings.refresh_rate = 60.0; 276 settings.refresh_rate = 60.0;
321 settings.impl_side_painting = false; 277 settings.impl_side_painting = false;
322 settings.allow_antialiasing = false; 278 settings.allow_antialiasing = false;
323 settings.calculate_top_controls_position = false; 279 settings.calculate_top_controls_position = false;
324 settings.top_controls_height = 0.f; 280 settings.top_controls_height = 0.f;
325 settings.highp_threshold_min = 2048; 281 settings.highp_threshold_min = 2048;
326 282
327 CommandLine* command_line = CommandLine::ForCurrentProcess(); 283 CommandLine* command_line = CommandLine::ForCurrentProcess();
328 settings.initial_debug_state.SetRecordRenderingStats( 284 settings.initial_debug_state.SetRecordRenderingStats(
329 command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking)); 285 command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking));
330 settings.initial_debug_state.show_fps_counter = 286 settings.initial_debug_state.show_fps_counter =
331 command_line->HasSwitch(cc::switches::kUIShowFPSCounter); 287 command_line->HasSwitch(cc::switches::kUIShowFPSCounter);
332 288
333 host_ = cc::LayerTreeHost::CreateSingleThreaded( 289 host_ = cc::LayerTreeHost::CreateSingleThreaded(
334 this, this, HostSharedBitmapManager::current(), settings); 290 this, this, HostSharedBitmapManager::current(), settings);
335 host_->SetRootLayer(root_layer_); 291 host_->SetRootLayer(root_layer_);
336 292
337 host_->SetVisible(true); 293 host_->SetVisible(true);
338 host_->SetLayerTreeHostClientReady(); 294 host_->SetLayerTreeHostClientReady();
339 host_->SetViewportSize(size_); 295 host_->SetViewportSize(size_);
340 host_->set_has_transparent_background(has_transparent_background_); 296 host_->set_has_transparent_background(has_transparent_background_);
341 host_->SetDeviceScaleFactor(device_scale_factor_); 297 host_->SetDeviceScaleFactor(device_scale_factor_);
342 // Need to recreate the UI resources because a new LayerTreeHost has been 298 ui_resource_provider_.SetLayerTreeHost(host_.get());
343 // created.
344 client_->DidLoseUIResources();
345 } 299 }
346 } 300 }
347 301
348 void CompositorImpl::setDeviceScaleFactor(float factor) { 302 void CompositorImpl::setDeviceScaleFactor(float factor) {
349 device_scale_factor_ = factor; 303 device_scale_factor_ = factor;
350 if (host_) 304 if (host_)
351 host_->SetDeviceScaleFactor(factor); 305 host_->SetDeviceScaleFactor(factor);
352 } 306 }
353 307
354 void CompositorImpl::SetWindowBounds(const gfx::Size& size) { 308 void CompositorImpl::SetWindowBounds(const gfx::Size& size) {
355 if (size_ == size) 309 if (size_ == size)
356 return; 310 return;
357 311
358 size_ = size; 312 size_ = size;
359 if (host_) 313 if (host_)
360 host_->SetViewportSize(size); 314 host_->SetViewportSize(size);
361 root_layer_->SetBounds(size); 315 root_layer_->SetBounds(size);
362 } 316 }
363 317
364 void CompositorImpl::SetHasTransparentBackground(bool flag) { 318 void CompositorImpl::SetHasTransparentBackground(bool flag) {
365 has_transparent_background_ = flag; 319 has_transparent_background_ = flag;
366 if (host_) 320 if (host_)
367 host_->set_has_transparent_background(flag); 321 host_->set_has_transparent_background(flag);
368 } 322 }
369 323
370 bool CompositorImpl::CompositeAndReadback(void *pixels, const gfx::Rect& rect) { 324 bool CompositorImpl::CompositeAndReadback(void *pixels, const gfx::Rect& rect) {
371 return false; 325 return false;
372 } 326 }
373 327
374 cc::UIResourceId CompositorImpl::GenerateUIResourceFromUIResourceBitmap(
375 const cc::UIResourceBitmap& bitmap,
376 bool is_transient) {
377 if (!host_)
378 return 0;
379
380 cc::UIResourceId id = 0;
381 scoped_ptr<cc::UIResourceClient> resource;
382 if (is_transient) {
383 scoped_ptr<TransientUIResource> transient_resource =
384 TransientUIResource::Create(host_.get(), bitmap);
385 id = transient_resource->id();
386 resource = transient_resource.Pass();
387 } else {
388 scoped_ptr<cc::ScopedUIResource> scoped_resource =
389 cc::ScopedUIResource::Create(host_.get(), bitmap);
390 id = scoped_resource->id();
391 resource = scoped_resource.Pass();
392 }
393
394 ui_resource_map_.set(id, resource.Pass());
395 return id;
396 }
397
398 cc::UIResourceId CompositorImpl::GenerateUIResource(const SkBitmap& bitmap,
399 bool is_transient) {
400 return GenerateUIResourceFromUIResourceBitmap(cc::UIResourceBitmap(bitmap),
401 is_transient);
402 }
403
404 cc::UIResourceId CompositorImpl::GenerateCompressedUIResource(
405 const gfx::Size& size,
406 void* pixels,
407 bool is_transient) {
408 DCHECK_LT(0, size.width());
409 DCHECK_LT(0, size.height());
410 DCHECK_EQ(0, size.width() % 4);
411 DCHECK_EQ(0, size.height() % 4);
412
413 size_t data_size = size.width() * size.height() / 2;
414 SkImageInfo info = {size.width(), size.height() / 2, kAlpha_8_SkColorType,
415 kPremul_SkAlphaType};
416 skia::RefPtr<SkMallocPixelRef> etc1_pixel_ref =
417 skia::AdoptRef(SkMallocPixelRef::NewAllocate(info, 0, 0));
418 memcpy(etc1_pixel_ref->getAddr(), pixels, data_size);
419 etc1_pixel_ref->setImmutable();
420 return GenerateUIResourceFromUIResourceBitmap(
421 cc::UIResourceBitmap(etc1_pixel_ref, size), is_transient);
422 }
423
424 void CompositorImpl::DeleteUIResource(cc::UIResourceId resource_id) {
425 UIResourceMap::iterator it = ui_resource_map_.find(resource_id);
426 if (it != ui_resource_map_.end())
427 ui_resource_map_.erase(it);
428 }
429
430 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl> 328 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
431 CreateGpuProcessViewContext( 329 CreateGpuProcessViewContext(
432 const blink::WebGraphicsContext3D::Attributes attributes, 330 const blink::WebGraphicsContext3D::Attributes attributes,
433 int surface_id) { 331 int surface_id) {
434 BrowserGpuChannelHostFactory* factory = 332 BrowserGpuChannelHostFactory* factory =
435 BrowserGpuChannelHostFactory::instance(); 333 BrowserGpuChannelHostFactory::instance();
436 CauseForGpuLaunch cause = 334 CauseForGpuLaunch cause =
437 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE; 335 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE;
438 scoped_refptr<GpuChannelHost> gpu_channel_host( 336 scoped_refptr<GpuChannelHost> gpu_channel_host(
439 factory->EstablishGpuChannelSync(cause)); 337 factory->EstablishGpuChannelSync(cause));
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 if (!context_provider.get()) { 378 if (!context_provider.get()) {
481 LOG(ERROR) << "Failed to create 3D context for compositor."; 379 LOG(ERROR) << "Failed to create 3D context for compositor.";
482 return scoped_ptr<cc::OutputSurface>(); 380 return scoped_ptr<cc::OutputSurface>();
483 } 381 }
484 382
485 return scoped_ptr<cc::OutputSurface>( 383 return scoped_ptr<cc::OutputSurface>(
486 new OutputSurfaceWithoutParent(context_provider)); 384 new OutputSurfaceWithoutParent(context_provider));
487 } 385 }
488 386
489 void CompositorImpl::OnLostResources() { 387 void CompositorImpl::OnLostResources() {
490 client_->DidLoseResources(); 388 client_->DidLoseResources();
no sievers 2014/05/16 20:12:19 I think we need to signal UIResourcesAreInvalid()
powei 2014/05/19 18:49:46 Done.
491 } 389 }
492 390
493 void CompositorImpl::DidCompleteSwapBuffers() { 391 void CompositorImpl::DidCompleteSwapBuffers() {
494 client_->OnSwapBuffersCompleted(); 392 client_->OnSwapBuffersCompleted();
495 } 393 }
496 394
497 void CompositorImpl::ScheduleComposite() { 395 void CompositorImpl::ScheduleComposite() {
498 client_->ScheduleComposite(); 396 client_->ScheduleComposite();
499 } 397 }
500 398
(...skipping 13 matching lines...) Expand all
514 412
515 void CompositorImpl::DidCommit() { 413 void CompositorImpl::DidCommit() {
516 root_window_->OnCompositingDidCommit(); 414 root_window_->OnCompositingDidCommit();
517 } 415 }
518 416
519 void CompositorImpl::AttachLayerForReadback(scoped_refptr<cc::Layer> layer) { 417 void CompositorImpl::AttachLayerForReadback(scoped_refptr<cc::Layer> layer) {
520 root_layer_->AddChild(layer); 418 root_layer_->AddChild(layer);
521 } 419 }
522 420
523 } // namespace content 421 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698