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
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/compositor_impl_android.h"
7 #include <android/bitmap.h>
8 #include <android/native_window_jni.h>
10 #include "base/android/jni_android.h"
11 #include "base/android/scoped_java_ref.h"
12 #include "base/bind.h"
13 #include "base/cancelable_callback.h"
14 #include "base/command_line.h"
15 #include "base/containers/hash_tables.h"
16 #include "base/lazy_instance.h"
17 #include "base/logging.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/single_thread_task_runner.h"
20 #include "base/synchronization/lock.h"
21 #include "base/threading/thread.h"
22 #include "base/threading/thread_checker.h"
23 #include "cc/base/switches.h"
24 #include "cc/input/input_handler.h"
25 #include "cc/layers/layer.h"
26 #include "cc/output/compositor_frame.h"
27 #include "cc/output/context_provider.h"
28 #include "cc/output/output_surface.h"
29 #include "cc/output/output_surface_client.h"
30 #include "cc/trees/layer_tree_host.h"
31 #include "content/browser/android/child_process_launcher_android.h"
32 #include "content/browser/gpu/browser_gpu_channel_host_factory.h"
33 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h"
34 #include "content/browser/gpu/gpu_surface_tracker.h"
35 #include "content/browser/renderer_host/render_widget_host_impl.h"
36 #include "content/common/gpu/client/command_buffer_proxy_impl.h"
37 #include "content/common/gpu/client/context_provider_command_buffer.h"
38 #include "content/common/gpu/client/gl_helper.h"
39 #include "content/common/gpu/client/gpu_channel_host.h"
40 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
41 #include "content/common/gpu/gpu_process_launch_causes.h"
42 #include "content/common/host_shared_bitmap_manager.h"
43 #include "content/public/browser/android/compositor_client.h"
44 #include "gpu/command_buffer/client/context_support.h"
45 #include "gpu/command_buffer/client/gles2_interface.h"
46 #include "third_party/khronos/GLES2/gl2.h"
47 #include "third_party/khronos/GLES2/gl2ext.h"
48 #include "third_party/skia/include/core/SkMallocPixelRef.h"
49 #include "ui/base/android/window_android.h"
50 #include "ui/gfx/android/device_display_info.h"
51 #include "ui/gfx/frame_time.h"
52 #include "webkit/common/gpu/context_provider_in_process.h"
53 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
59 const unsigned int kMaxSwapBuffers
= 2U;
61 // Used to override capabilities_.adjust_deadline_for_parent to false
62 class OutputSurfaceWithoutParent
: public cc::OutputSurface
{
64 OutputSurfaceWithoutParent(
65 const scoped_refptr
<ContextProviderCommandBuffer
>& context_provider
,
66 base::WeakPtr
<CompositorImpl
> compositor_impl
)
67 : cc::OutputSurface(context_provider
),
68 swap_buffers_completion_callback_(
69 base::Bind(&OutputSurfaceWithoutParent::OnSwapBuffersCompleted
,
70 base::Unretained(this))) {
71 capabilities_
.adjust_deadline_for_parent
= false;
72 compositor_impl_
= compositor_impl
;
73 main_thread_
= base::MessageLoopProxy::current();
76 virtual void SwapBuffers(cc::CompositorFrame
* frame
) override
{
77 for (size_t i
= 0; i
< frame
->metadata
.latency_info
.size(); i
++) {
78 frame
->metadata
.latency_info
[i
].AddLatencyNumber(
79 ui::INPUT_EVENT_BROWSER_SWAP_BUFFER_COMPONENT
, 0, 0);
82 GetCommandBufferProxy()->SetLatencyInfo(frame
->metadata
.latency_info
);
83 DCHECK(frame
->gl_frame_data
->sub_buffer_rect
==
84 gfx::Rect(frame
->gl_frame_data
->size
));
85 context_provider_
->ContextSupport()->Swap();
86 client_
->DidSwapBuffers();
89 virtual bool BindToClient(cc::OutputSurfaceClient
* client
) override
{
90 if (!OutputSurface::BindToClient(client
))
93 GetCommandBufferProxy()->SetSwapBuffersCompletionCallback(
94 swap_buffers_completion_callback_
.callback());
96 main_thread_
->PostTask(
98 base::Bind(&CompositorImpl::PopulateGpuCapabilities
,
100 context_provider_
->ContextCapabilities().gpu
));
106 CommandBufferProxyImpl
* GetCommandBufferProxy() {
107 ContextProviderCommandBuffer
* provider_command_buffer
=
108 static_cast<content::ContextProviderCommandBuffer
*>(
109 context_provider_
.get());
110 CommandBufferProxyImpl
* command_buffer_proxy
=
111 provider_command_buffer
->GetCommandBufferProxy();
112 DCHECK(command_buffer_proxy
);
113 return command_buffer_proxy
;
116 void OnSwapBuffersCompleted(
117 const std::vector
<ui::LatencyInfo
>& latency_info
) {
118 RenderWidgetHostImpl::CompositorFrameDrawn(latency_info
);
119 OutputSurface::OnSwapBuffersComplete();
122 base::CancelableCallback
<void(const std::vector
<ui::LatencyInfo
>&)>
123 swap_buffers_completion_callback_
;
125 scoped_refptr
<base::MessageLoopProxy
> main_thread_
;
126 base::WeakPtr
<CompositorImpl
> compositor_impl_
;
129 static bool g_initialized
= false;
131 } // anonymous namespace
134 Compositor
* Compositor::Create(CompositorClient
* client
,
135 gfx::NativeWindow root_window
) {
136 return client
? new CompositorImpl(client
, root_window
) : NULL
;
140 void Compositor::Initialize() {
141 DCHECK(!CompositorImpl::IsInitialized());
142 g_initialized
= true;
146 bool CompositorImpl::IsInitialized() {
147 return g_initialized
;
150 CompositorImpl::CompositorImpl(CompositorClient
* client
,
151 gfx::NativeWindow root_window
)
152 : root_layer_(cc::Layer::Create()),
153 has_transparent_background_(false),
154 device_scale_factor_(1),
158 root_window_(root_window
),
159 did_post_swapbuffers_(false),
160 ignore_schedule_composite_(false),
161 needs_composite_(false),
162 needs_animate_(false),
163 will_composite_immediately_(false),
164 composite_on_vsync_trigger_(DO_NOT_COMPOSITE
),
165 pending_swapbuffers_(0U),
166 weak_factory_(this) {
169 ImageTransportFactoryAndroid::AddObserver(this);
170 root_window
->AttachCompositor(this);
173 CompositorImpl::~CompositorImpl() {
174 root_window_
->DetachCompositor();
175 ImageTransportFactoryAndroid::RemoveObserver(this);
176 // Clean-up any surface references.
180 void CompositorImpl::PostComposite(CompositingTrigger trigger
) {
181 DCHECK(needs_composite_
);
182 DCHECK(trigger
== COMPOSITE_IMMEDIATELY
|| trigger
== COMPOSITE_EVENTUALLY
);
184 if (will_composite_immediately_
||
185 (trigger
== COMPOSITE_EVENTUALLY
&& WillComposite())) {
186 // We will already composite soon enough.
187 DCHECK(WillComposite());
191 if (DidCompositeThisFrame()) {
192 DCHECK(!WillCompositeThisFrame());
193 if (composite_on_vsync_trigger_
!= COMPOSITE_IMMEDIATELY
) {
194 composite_on_vsync_trigger_
= trigger
;
195 root_window_
->RequestVSyncUpdate();
197 DCHECK(WillComposite());
201 base::TimeDelta delay
;
202 if (trigger
== COMPOSITE_IMMEDIATELY
) {
203 will_composite_immediately_
= true;
204 composite_on_vsync_trigger_
= DO_NOT_COMPOSITE
;
206 DCHECK(!WillComposite());
207 const base::TimeDelta estimated_composite_time
= vsync_period_
/ 4;
208 const base::TimeTicks now
= base::TimeTicks::Now();
210 if (!last_vsync_
.is_null() && (now
- last_vsync_
) < vsync_period_
) {
211 base::TimeTicks next_composite
=
212 last_vsync_
+ vsync_period_
- estimated_composite_time
;
213 if (next_composite
< now
) {
214 // It's too late, we will reschedule composite as needed on the next
216 composite_on_vsync_trigger_
= COMPOSITE_EVENTUALLY
;
217 root_window_
->RequestVSyncUpdate();
218 DCHECK(WillComposite());
222 delay
= next_composite
- now
;
225 TRACE_EVENT2("cc", "CompositorImpl::PostComposite",
227 "delay", delay
.InMillisecondsF());
229 DCHECK(composite_on_vsync_trigger_
== DO_NOT_COMPOSITE
);
230 if (current_composite_task_
)
231 current_composite_task_
->Cancel();
233 // Unretained because we cancel the task on shutdown.
234 current_composite_task_
.reset(new base::CancelableClosure(
235 base::Bind(&CompositorImpl::Composite
, base::Unretained(this), trigger
)));
236 base::MessageLoop::current()->PostDelayedTask(
237 FROM_HERE
, current_composite_task_
->callback(), delay
);
240 void CompositorImpl::Composite(CompositingTrigger trigger
) {
241 BrowserGpuChannelHostFactory
* factory
=
242 BrowserGpuChannelHostFactory::instance();
243 if (!factory
->GetGpuChannel() || factory
->GetGpuChannel()->IsLost()) {
244 CauseForGpuLaunch cause
=
245 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE
;
246 factory
->EstablishGpuChannel(cause
,
247 base::Bind(&CompositorImpl::ScheduleComposite
,
248 weak_factory_
.GetWeakPtr()));
253 DCHECK(trigger
== COMPOSITE_IMMEDIATELY
|| trigger
== COMPOSITE_EVENTUALLY
);
254 DCHECK(needs_composite_
);
255 DCHECK(!DidCompositeThisFrame());
257 if (trigger
== COMPOSITE_IMMEDIATELY
)
258 will_composite_immediately_
= false;
260 DCHECK_LE(pending_swapbuffers_
, kMaxSwapBuffers
);
261 if (pending_swapbuffers_
== kMaxSwapBuffers
) {
262 TRACE_EVENT0("compositor", "CompositorImpl_SwapLimit");
266 // Reset state before Layout+Composite since that might create more
267 // requests to Composite that we need to respect.
268 needs_composite_
= false;
270 // Only allow compositing once per vsync.
271 current_composite_task_
->Cancel();
272 DCHECK(DidCompositeThisFrame() && !WillComposite());
274 // Ignore ScheduleComposite() from layer tree changes during layout and
275 // animation updates that will already be reflected in the current frame
276 // we are about to draw.
277 ignore_schedule_composite_
= true;
279 const base::TimeTicks frame_time
= gfx::FrameTime::Now();
280 if (needs_animate_
) {
281 needs_animate_
= false;
282 root_window_
->Animate(frame_time
);
284 ignore_schedule_composite_
= false;
286 did_post_swapbuffers_
= false;
287 host_
->Composite(frame_time
);
288 if (did_post_swapbuffers_
)
289 pending_swapbuffers_
++;
291 // Need to track vsync to avoid compositing more than once per frame.
292 root_window_
->RequestVSyncUpdate();
295 UIResourceProvider
& CompositorImpl::GetUIResourceProvider() {
296 return ui_resource_provider_
;
299 ui::SystemUIResourceManager
& CompositorImpl::GetSystemUIResourceManager() {
300 return ui_resource_provider_
.GetSystemUIResourceManager();
303 void CompositorImpl::SetRootLayer(scoped_refptr
<cc::Layer
> root_layer
) {
304 if (subroot_layer_
.get()) {
305 subroot_layer_
->RemoveFromParent();
306 subroot_layer_
= NULL
;
308 if (root_layer
.get()) {
309 subroot_layer_
= root_layer
;
310 root_layer_
->AddChild(root_layer
);
314 void CompositorImpl::SetWindowSurface(ANativeWindow
* window
) {
315 GpuSurfaceTracker
* tracker
= GpuSurfaceTracker::Get();
318 tracker
->RemoveSurface(surface_id_
);
319 ANativeWindow_release(window_
);
327 ANativeWindow_acquire(window
);
328 surface_id_
= tracker
->AddSurfaceForNativeWidget(window
);
329 tracker
->SetSurfaceHandle(
331 gfx::GLSurfaceHandle(gfx::kNullPluginWindow
, gfx::NATIVE_DIRECT
));
336 void CompositorImpl::SetSurface(jobject surface
) {
337 JNIEnv
* env
= base::android::AttachCurrentThread();
338 base::android::ScopedJavaLocalRef
<jobject
> j_surface(env
, surface
);
340 // First, cleanup any existing surface references.
342 UnregisterViewSurface(surface_id_
);
343 SetWindowSurface(NULL
);
345 // Now, set the new surface if we have one.
346 ANativeWindow
* window
= NULL
;
348 // Note: This ensures that any local references used by
349 // ANativeWindow_fromSurface are released immediately. This is needed as a
350 // workaround for https://code.google.com/p/android/issues/detail?id=68174
351 base::android::ScopedJavaLocalFrame
scoped_local_reference_frame(env
);
352 window
= ANativeWindow_fromSurface(env
, surface
);
355 SetWindowSurface(window
);
356 ANativeWindow_release(window
);
357 RegisterViewSurface(surface_id_
, j_surface
.obj());
361 void CompositorImpl::SetVisible(bool visible
) {
364 // Look for any layers that were attached to the root for readback
365 // and are waiting for Composite() to happen.
366 bool readback_pending
= false;
367 for (size_t i
= 0; i
< root_layer_
->children().size(); ++i
) {
368 if (root_layer_
->children()[i
]->HasCopyRequest()) {
369 readback_pending
= true;
373 if (readback_pending
) {
374 ignore_schedule_composite_
= true;
375 host_
->Composite(base::TimeTicks::Now());
376 ignore_schedule_composite_
= false;
380 ui_resource_provider_
.SetLayerTreeHost(NULL
);
383 DCHECK(!WillComposite());
384 needs_composite_
= false;
385 pending_swapbuffers_
= 0;
386 cc::LayerTreeSettings settings
;
387 settings
.refresh_rate
= 60.0;
388 settings
.impl_side_painting
= false;
389 settings
.allow_antialiasing
= false;
390 settings
.calculate_top_controls_position
= false;
391 settings
.top_controls_height
= 0.f
;
392 settings
.highp_threshold_min
= 2048;
394 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
395 settings
.initial_debug_state
.SetRecordRenderingStats(
396 command_line
->HasSwitch(cc::switches::kEnableGpuBenchmarking
));
397 settings
.initial_debug_state
.show_fps_counter
=
398 command_line
->HasSwitch(cc::switches::kUIShowFPSCounter
);
399 // TODO(enne): Update this this compositor to use the scheduler.
400 settings
.single_thread_proxy_scheduler
= false;
402 host_
= cc::LayerTreeHost::CreateSingleThreaded(
405 HostSharedBitmapManager::current(),
406 BrowserGpuMemoryBufferManager::current(),
408 base::MessageLoopProxy::current());
409 host_
->SetRootLayer(root_layer_
);
411 host_
->SetVisible(true);
412 host_
->SetLayerTreeHostClientReady();
413 host_
->SetViewportSize(size_
);
414 host_
->set_has_transparent_background(has_transparent_background_
);
415 host_
->SetDeviceScaleFactor(device_scale_factor_
);
416 ui_resource_provider_
.SetLayerTreeHost(host_
.get());
420 void CompositorImpl::setDeviceScaleFactor(float factor
) {
421 device_scale_factor_
= factor
;
423 host_
->SetDeviceScaleFactor(factor
);
426 void CompositorImpl::SetWindowBounds(const gfx::Size
& size
) {
432 host_
->SetViewportSize(size
);
433 root_layer_
->SetBounds(size
);
436 void CompositorImpl::SetHasTransparentBackground(bool flag
) {
437 has_transparent_background_
= flag
;
439 host_
->set_has_transparent_background(flag
);
442 void CompositorImpl::SetNeedsComposite() {
445 DCHECK(!needs_composite_
|| WillComposite());
447 needs_composite_
= true;
448 PostComposite(COMPOSITE_IMMEDIATELY
);
451 static scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
>
452 CreateGpuProcessViewContext(
453 const scoped_refptr
<GpuChannelHost
>& gpu_channel_host
,
454 const blink::WebGraphicsContext3D::Attributes attributes
,
456 DCHECK(gpu_channel_host
.get());
458 GURL
url("chrome://gpu/Compositor::createContext3D");
459 static const size_t kBytesPerPixel
= 4;
460 gfx::DeviceDisplayInfo display_info
;
461 size_t full_screen_texture_size_in_bytes
=
462 display_info
.GetDisplayHeight() *
463 display_info
.GetDisplayWidth() *
465 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits limits
;
466 limits
.command_buffer_size
= 64 * 1024;
467 limits
.start_transfer_buffer_size
= 64 * 1024;
468 limits
.min_transfer_buffer_size
= 64 * 1024;
469 limits
.max_transfer_buffer_size
= std::min(
470 3 * full_screen_texture_size_in_bytes
, kDefaultMaxTransferBufferSize
);
471 limits
.mapped_memory_reclaim_limit
= 2 * 1024 * 1024;
472 bool lose_context_when_out_of_memory
= true;
473 return make_scoped_ptr(
474 new WebGraphicsContext3DCommandBufferImpl(surface_id
,
476 gpu_channel_host
.get(),
478 lose_context_when_out_of_memory
,
483 void CompositorImpl::Layout() {
484 ignore_schedule_composite_
= true;
486 ignore_schedule_composite_
= false;
489 void CompositorImpl::RequestNewOutputSurface(bool fallback
) {
490 BrowserGpuChannelHostFactory
* factory
=
491 BrowserGpuChannelHostFactory::instance();
492 if (!factory
->GetGpuChannel() || factory
->GetGpuChannel()->IsLost()) {
493 CauseForGpuLaunch cause
=
494 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE
;
495 factory
->EstablishGpuChannel(
497 base::Bind(&CompositorImpl::CreateOutputSurface
,
498 weak_factory_
.GetWeakPtr(),
503 CreateOutputSurface(fallback
);
506 void CompositorImpl::CreateOutputSurface(bool fallback
) {
507 blink::WebGraphicsContext3D::Attributes attrs
;
508 attrs
.shareResources
= true;
509 attrs
.noAutomaticFlushes
= true;
510 pending_swapbuffers_
= 0;
515 scoped_refptr
<ContextProviderCommandBuffer
> context_provider
;
516 BrowserGpuChannelHostFactory
* factory
=
517 BrowserGpuChannelHostFactory::instance();
518 scoped_refptr
<GpuChannelHost
> gpu_channel_host
= factory
->GetGpuChannel();
519 if (gpu_channel_host
.get() && !gpu_channel_host
->IsLost()) {
520 context_provider
= ContextProviderCommandBuffer::Create(
521 CreateGpuProcessViewContext(gpu_channel_host
, attrs
, surface_id_
),
522 "BrowserCompositor");
524 if (!context_provider
.get()) {
525 LOG(ERROR
) << "Failed to create 3D context for compositor.";
526 host_
->SetOutputSurface(scoped_ptr
<cc::OutputSurface
>());
530 host_
->SetOutputSurface(
531 scoped_ptr
<cc::OutputSurface
>(new OutputSurfaceWithoutParent(
532 context_provider
, weak_factory_
.GetWeakPtr())));
535 void CompositorImpl::PopulateGpuCapabilities(
536 gpu::Capabilities gpu_capabilities
) {
537 ui_resource_provider_
.SetSupportsETC1NonPowerOfTwo(
538 gpu_capabilities
.texture_format_etc1_npot
);
541 void CompositorImpl::OnLostResources() {
542 client_
->DidLoseResources();
545 void CompositorImpl::ScheduleComposite() {
546 DCHECK(!needs_composite_
|| WillComposite());
547 if (ignore_schedule_composite_
)
550 needs_composite_
= true;
551 // We currently expect layer tree invalidations at most once per frame
552 // during normal operation and therefore try to composite immediately
553 // to minimize latency.
554 PostComposite(COMPOSITE_IMMEDIATELY
);
557 void CompositorImpl::ScheduleAnimation() {
558 DCHECK(!needs_composite_
|| WillComposite());
559 needs_animate_
= true;
561 if (needs_composite_
)
564 TRACE_EVENT0("cc", "CompositorImpl::ScheduleAnimation");
565 needs_composite_
= true;
566 PostComposite(COMPOSITE_EVENTUALLY
);
569 void CompositorImpl::DidPostSwapBuffers() {
570 TRACE_EVENT0("compositor", "CompositorImpl::DidPostSwapBuffers");
571 did_post_swapbuffers_
= true;
574 void CompositorImpl::DidCompleteSwapBuffers() {
575 TRACE_EVENT0("compositor", "CompositorImpl::DidCompleteSwapBuffers");
576 DCHECK_GT(pending_swapbuffers_
, 0U);
577 if (pending_swapbuffers_
-- == kMaxSwapBuffers
&& needs_composite_
)
578 PostComposite(COMPOSITE_IMMEDIATELY
);
579 client_
->OnSwapBuffersCompleted(pending_swapbuffers_
);
582 void CompositorImpl::DidAbortSwapBuffers() {
583 TRACE_EVENT0("compositor", "CompositorImpl::DidAbortSwapBuffers");
584 // This really gets called only once from
585 // SingleThreadProxy::DidLoseOutputSurfaceOnImplThread() when the
588 client_
->OnSwapBuffersCompleted(0);
591 void CompositorImpl::DidCommit() {
592 root_window_
->OnCompositingDidCommit();
595 void CompositorImpl::AttachLayerForReadback(scoped_refptr
<cc::Layer
> layer
) {
596 root_layer_
->AddChild(layer
);
599 void CompositorImpl::RequestCopyOfOutputOnRootLayer(
600 scoped_ptr
<cc::CopyOutputRequest
> request
) {
601 root_layer_
->RequestCopyOfOutput(request
.Pass());
604 void CompositorImpl::OnVSync(base::TimeTicks frame_time
,
605 base::TimeDelta vsync_period
) {
606 vsync_period_
= vsync_period
;
607 last_vsync_
= frame_time
;
609 if (WillCompositeThisFrame()) {
610 // We somehow missed the last vsync interval, so reschedule for deadline.
611 // We cannot schedule immediately, or will get us out-of-phase with new
614 composite_on_vsync_trigger_
= COMPOSITE_EVENTUALLY
;
616 current_composite_task_
.reset();
619 DCHECK(!DidCompositeThisFrame() && !WillCompositeThisFrame());
620 if (composite_on_vsync_trigger_
!= DO_NOT_COMPOSITE
) {
621 CompositingTrigger trigger
= composite_on_vsync_trigger_
;
622 composite_on_vsync_trigger_
= DO_NOT_COMPOSITE
;
623 PostComposite(trigger
);
627 void CompositorImpl::SetNeedsAnimate() {
631 host_
->SetNeedsAnimate();
634 } // namespace content