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/command_line.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/lazy_instance.h"
16 #include "base/logging.h"
17 #include "base/single_thread_task_runner.h"
18 #include "base/synchronization/lock.h"
19 #include "base/threading/thread.h"
20 #include "base/threading/thread_checker.h"
21 #include "cc/base/switches.h"
22 #include "cc/input/input_handler.h"
23 #include "cc/layers/layer.h"
24 #include "cc/output/compositor_frame.h"
25 #include "cc/output/context_provider.h"
26 #include "cc/output/output_surface.h"
27 #include "cc/trees/layer_tree_host.h"
28 #include "content/browser/android/child_process_launcher_android.h"
29 #include "content/browser/gpu/browser_gpu_channel_host_factory.h"
30 #include "content/browser/gpu/gpu_surface_tracker.h"
31 #include "content/common/gpu/client/command_buffer_proxy_impl.h"
32 #include "content/common/gpu/client/context_provider_command_buffer.h"
33 #include "content/common/gpu/client/gl_helper.h"
34 #include "content/common/gpu/client/gpu_channel_host.h"
35 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
36 #include "content/common/gpu/gpu_process_launch_causes.h"
37 #include "content/common/host_shared_bitmap_manager.h"
38 #include "content/public/browser/android/compositor_client.h"
39 #include "gpu/command_buffer/client/gles2_interface.h"
40 #include "third_party/khronos/GLES2/gl2.h"
41 #include "third_party/khronos/GLES2/gl2ext.h"
42 #include "third_party/skia/include/core/SkMallocPixelRef.h"
43 #include "ui/base/android/window_android.h"
44 #include "ui/gfx/android/device_display_info.h"
45 #include "ui/gfx/frame_time.h"
46 #include "ui/gl/android/surface_texture.h"
47 #include "ui/gl/android/surface_texture_tracker.h"
48 #include "webkit/common/gpu/context_provider_in_process.h"
49 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
53 const unsigned int kMaxSwapBuffers = 2U;
55 // Used to override capabilities_.adjust_deadline_for_parent to false
56 class OutputSurfaceWithoutParent : public cc::OutputSurface {
58 OutputSurfaceWithoutParent(const scoped_refptr<
59 content::ContextProviderCommandBuffer>& context_provider)
60 : cc::OutputSurface(context_provider) {
61 capabilities_.adjust_deadline_for_parent = false;
64 virtual void SwapBuffers(cc::CompositorFrame* frame) OVERRIDE {
65 content::ContextProviderCommandBuffer* provider_command_buffer =
66 static_cast<content::ContextProviderCommandBuffer*>(
67 context_provider_.get());
68 content::CommandBufferProxyImpl* command_buffer_proxy =
69 provider_command_buffer->GetCommandBufferProxy();
70 DCHECK(command_buffer_proxy);
71 command_buffer_proxy->SetLatencyInfo(frame->metadata.latency_info);
73 OutputSurface::SwapBuffers(frame);
77 class SurfaceTextureTrackerImpl : public gfx::SurfaceTextureTracker {
79 SurfaceTextureTrackerImpl() : next_surface_texture_id_(1) {
80 thread_checker_.DetachFromThread();
83 // Overridden from gfx::SurfaceTextureTracker:
84 virtual scoped_refptr<gfx::SurfaceTexture> AcquireSurfaceTexture(
86 int secondary_id) OVERRIDE {
87 base::AutoLock lock(surface_textures_lock_);
88 SurfaceTextureMapKey key(primary_id, secondary_id);
89 SurfaceTextureMap::iterator it = surface_textures_.find(key);
90 if (it == surface_textures_.end())
91 return scoped_refptr<gfx::SurfaceTexture>();
92 scoped_refptr<gfx::SurfaceTexture> surface_texture = it->second;
93 surface_textures_.erase(it);
94 return surface_texture;
97 int AddSurfaceTexture(gfx::SurfaceTexture* surface_texture,
98 int child_process_id) {
99 DCHECK(thread_checker_.CalledOnValidThread());
100 int surface_texture_id = next_surface_texture_id_++;
101 if (next_surface_texture_id_ == INT_MAX)
102 next_surface_texture_id_ = 1;
104 base::AutoLock lock(surface_textures_lock_);
105 SurfaceTextureMapKey key(surface_texture_id, child_process_id);
106 DCHECK(surface_textures_.find(key) == surface_textures_.end());
107 surface_textures_[key] = surface_texture;
108 content::RegisterChildProcessSurfaceTexture(
111 surface_texture->j_surface_texture().obj());
112 return surface_texture_id;
115 void RemoveAllSurfaceTextures(int child_process_id) {
116 DCHECK(thread_checker_.CalledOnValidThread());
117 base::AutoLock lock(surface_textures_lock_);
118 SurfaceTextureMap::iterator it = surface_textures_.begin();
119 while (it != surface_textures_.end()) {
120 if (it->first.second == child_process_id) {
121 content::UnregisterChildProcessSurfaceTexture(it->first.first,
123 surface_textures_.erase(it++);
131 typedef std::pair<int, int> SurfaceTextureMapKey;
132 typedef base::hash_map<SurfaceTextureMapKey,
133 scoped_refptr<gfx::SurfaceTexture> >
135 SurfaceTextureMap surface_textures_;
136 mutable base::Lock surface_textures_lock_;
137 int next_surface_texture_id_;
138 base::ThreadChecker thread_checker_;
140 base::LazyInstance<SurfaceTextureTrackerImpl> g_surface_texture_tracker =
141 LAZY_INSTANCE_INITIALIZER;
143 static bool g_initialized = false;
145 } // anonymous namespace
150 Compositor* Compositor::Create(CompositorClient* client,
151 gfx::NativeWindow root_window) {
152 return client ? new CompositorImpl(client, root_window) : NULL;
156 void Compositor::Initialize() {
157 DCHECK(!CompositorImpl::IsInitialized());
158 // SurfaceTextureTracker instance must be set before we create a GPU thread
159 // that could be using it to initialize GLImage instances.
160 gfx::SurfaceTextureTracker::InitInstance(g_surface_texture_tracker.Pointer());
161 g_initialized = true;
165 bool CompositorImpl::IsInitialized() {
166 return g_initialized;
170 int CompositorImpl::CreateSurfaceTexture(int child_process_id) {
171 // Note: this needs to be 0 as the surface texture implemenation will take
172 // ownership of the texture and call glDeleteTextures when the GPU service
173 // attaches the surface texture to a real texture id. glDeleteTextures
174 // silently ignores 0.
175 const int kDummyTextureId = 0;
176 scoped_refptr<gfx::SurfaceTexture> surface_texture =
177 gfx::SurfaceTexture::Create(kDummyTextureId);
178 return g_surface_texture_tracker.Pointer()->AddSurfaceTexture(
179 surface_texture.get(), child_process_id);
183 void CompositorImpl::DestroyAllSurfaceTextures(int child_process_id) {
184 g_surface_texture_tracker.Pointer()->RemoveAllSurfaceTextures(
188 CompositorImpl::CompositorImpl(CompositorClient* client,
189 gfx::NativeWindow root_window)
190 : root_layer_(cc::Layer::Create()),
191 has_transparent_background_(false),
192 device_scale_factor_(1),
196 root_window_(root_window),
197 did_post_swapbuffers_(false),
198 ignore_schedule_composite_(false),
199 needs_composite_(false),
200 needs_animate_(false),
201 will_composite_immediately_(false),
202 composite_on_vsync_trigger_(DO_NOT_COMPOSITE),
203 pending_swapbuffers_(0U),
204 weak_factory_(this) {
207 ImageTransportFactoryAndroid::AddObserver(this);
208 root_window->AttachCompositor(this);
211 CompositorImpl::~CompositorImpl() {
212 root_window_->DetachCompositor();
213 ImageTransportFactoryAndroid::RemoveObserver(this);
214 // Clean-up any surface references.
218 void CompositorImpl::PostComposite(CompositingTrigger trigger) {
219 DCHECK(needs_composite_);
220 DCHECK(trigger == COMPOSITE_IMMEDIATELY || trigger == COMPOSITE_EVENTUALLY);
222 if (will_composite_immediately_ ||
223 (trigger == COMPOSITE_EVENTUALLY && WillComposite())) {
224 // We will already composite soon enough.
225 DCHECK(WillComposite());
229 if (DidCompositeThisFrame()) {
230 DCHECK(!WillCompositeThisFrame());
231 if (composite_on_vsync_trigger_ != COMPOSITE_IMMEDIATELY) {
232 composite_on_vsync_trigger_ = trigger;
233 root_window_->RequestVSyncUpdate();
235 DCHECK(WillComposite());
239 base::TimeDelta delay;
240 if (trigger == COMPOSITE_IMMEDIATELY) {
241 will_composite_immediately_ = true;
242 composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
244 DCHECK(!WillComposite());
245 const base::TimeDelta estimated_composite_time = vsync_period_ / 4;
246 const base::TimeTicks now = base::TimeTicks::Now();
248 if (!last_vsync_.is_null() && (now - last_vsync_) < vsync_period_) {
249 base::TimeTicks next_composite =
250 last_vsync_ + vsync_period_ - estimated_composite_time;
251 if (next_composite < now) {
252 // It's too late, we will reschedule composite as needed on the next
254 composite_on_vsync_trigger_ = COMPOSITE_EVENTUALLY;
255 root_window_->RequestVSyncUpdate();
256 DCHECK(WillComposite());
260 delay = next_composite - now;
263 TRACE_EVENT2("cc", "CompositorImpl::PostComposite",
265 "delay", delay.InMillisecondsF());
267 DCHECK(composite_on_vsync_trigger_ == DO_NOT_COMPOSITE);
268 if (current_composite_task_)
269 current_composite_task_->Cancel();
271 // Unretained because we cancel the task on shutdown.
272 current_composite_task_.reset(new base::CancelableClosure(
273 base::Bind(&CompositorImpl::Composite, base::Unretained(this), trigger)));
274 base::MessageLoop::current()->PostDelayedTask(
275 FROM_HERE, current_composite_task_->callback(), delay);
278 void CompositorImpl::Composite(CompositingTrigger trigger) {
279 BrowserGpuChannelHostFactory* factory =
280 BrowserGpuChannelHostFactory::instance();
281 if (!factory->GetGpuChannel() || factory->GetGpuChannel()->IsLost()) {
282 CauseForGpuLaunch cause =
283 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE;
284 factory->EstablishGpuChannel(
286 base::Bind(&CompositorImpl::OnGpuChannelEstablished,
287 weak_factory_.GetWeakPtr()));
292 DCHECK(trigger == COMPOSITE_IMMEDIATELY || trigger == COMPOSITE_EVENTUALLY);
293 DCHECK(needs_composite_);
294 DCHECK(!DidCompositeThisFrame());
296 if (trigger == COMPOSITE_IMMEDIATELY)
297 will_composite_immediately_ = false;
299 DCHECK_LE(pending_swapbuffers_, kMaxSwapBuffers);
300 if (pending_swapbuffers_ == kMaxSwapBuffers) {
301 TRACE_EVENT0("compositor", "CompositorImpl_SwapLimit");
305 // Reset state before Layout+Composite since that might create more
306 // requests to Composite that we need to respect.
307 needs_composite_ = false;
309 // Only allow compositing once per vsync.
310 current_composite_task_->Cancel();
311 DCHECK(DidCompositeThisFrame() && !WillComposite());
313 // Ignore ScheduleComposite() from layer tree changes during layout and
314 // animation updates that will already be reflected in the current frame
315 // we are about to draw.
316 ignore_schedule_composite_ = true;
319 const base::TimeTicks frame_time = gfx::FrameTime::Now();
320 if (needs_animate_) {
321 needs_animate_ = false;
322 root_window_->Animate(frame_time);
324 ignore_schedule_composite_ = false;
326 did_post_swapbuffers_ = false;
327 host_->Composite(frame_time);
328 if (did_post_swapbuffers_)
329 pending_swapbuffers_++;
331 // Need to track vsync to avoid compositing more than once per frame.
332 root_window_->RequestVSyncUpdate();
335 void CompositorImpl::OnGpuChannelEstablished() {
339 UIResourceProvider& CompositorImpl::GetUIResourceProvider() {
340 return ui_resource_provider_;
343 ui::SystemUIResourceManager& CompositorImpl::GetSystemUIResourceManager() {
344 return ui_resource_provider_.GetSystemUIResourceManager();
347 void CompositorImpl::SetRootLayer(scoped_refptr<cc::Layer> root_layer) {
348 if (subroot_layer_) {
349 subroot_layer_->RemoveFromParent();
350 subroot_layer_ = NULL;
353 subroot_layer_ = root_layer;
354 root_layer_->AddChild(root_layer);
358 void CompositorImpl::SetWindowSurface(ANativeWindow* window) {
359 GpuSurfaceTracker* tracker = GpuSurfaceTracker::Get();
362 tracker->RemoveSurface(surface_id_);
363 ANativeWindow_release(window_);
371 ANativeWindow_acquire(window);
372 surface_id_ = tracker->AddSurfaceForNativeWidget(window);
373 tracker->SetSurfaceHandle(
375 gfx::GLSurfaceHandle(gfx::kNullPluginWindow, gfx::NATIVE_DIRECT));
380 void CompositorImpl::SetSurface(jobject surface) {
381 JNIEnv* env = base::android::AttachCurrentThread();
382 base::android::ScopedJavaLocalRef<jobject> j_surface(env, surface);
384 // First, cleanup any existing surface references.
386 content::UnregisterViewSurface(surface_id_);
387 SetWindowSurface(NULL);
389 // Now, set the new surface if we have one.
390 ANativeWindow* window = NULL;
392 // Note: This ensures that any local references used by
393 // ANativeWindow_fromSurface are released immediately. This is needed as a
394 // workaround for https://code.google.com/p/android/issues/detail?id=68174
395 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
396 window = ANativeWindow_fromSurface(env, surface);
399 SetWindowSurface(window);
400 ANativeWindow_release(window);
401 content::RegisterViewSurface(surface_id_, j_surface.obj());
405 void CompositorImpl::SetVisible(bool visible) {
408 // Look for any layers that were attached to the root for readback
409 // and are waiting for Composite() to happen.
410 bool readback_pending = false;
411 for (size_t i = 0; i < root_layer_->children().size(); ++i) {
412 if (root_layer_->children()[i]->HasCopyRequest()) {
413 readback_pending = true;
417 if (readback_pending) {
418 ignore_schedule_composite_ = true;
419 host_->Composite(base::TimeTicks::Now());
420 ignore_schedule_composite_ = false;
424 ui_resource_provider_.SetLayerTreeHost(NULL);
427 DCHECK(!WillComposite());
428 needs_composite_ = false;
429 needs_animate_ = false;
430 pending_swapbuffers_ = 0;
431 cc::LayerTreeSettings settings;
432 settings.refresh_rate = 60.0;
433 settings.impl_side_painting = false;
434 settings.allow_antialiasing = false;
435 settings.calculate_top_controls_position = false;
436 settings.top_controls_height = 0.f;
437 settings.highp_threshold_min = 2048;
439 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
440 settings.initial_debug_state.SetRecordRenderingStats(
441 command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking));
442 settings.initial_debug_state.show_fps_counter =
443 command_line->HasSwitch(cc::switches::kUIShowFPSCounter);
445 host_ = cc::LayerTreeHost::CreateSingleThreaded(
448 HostSharedBitmapManager::current(),
450 base::MessageLoopProxy::current());
451 host_->SetRootLayer(root_layer_);
453 host_->SetVisible(true);
454 host_->SetLayerTreeHostClientReady();
455 host_->SetViewportSize(size_);
456 host_->set_has_transparent_background(has_transparent_background_);
457 host_->SetDeviceScaleFactor(device_scale_factor_);
458 ui_resource_provider_.SetLayerTreeHost(host_.get());
462 void CompositorImpl::setDeviceScaleFactor(float factor) {
463 device_scale_factor_ = factor;
465 host_->SetDeviceScaleFactor(factor);
468 void CompositorImpl::SetWindowBounds(const gfx::Size& size) {
474 host_->SetViewportSize(size);
475 root_layer_->SetBounds(size);
478 void CompositorImpl::SetHasTransparentBackground(bool flag) {
479 has_transparent_background_ = flag;
481 host_->set_has_transparent_background(flag);
484 void CompositorImpl::SetNeedsComposite() {
487 DCHECK(!needs_composite_ || WillComposite());
489 needs_composite_ = true;
490 PostComposite(COMPOSITE_IMMEDIATELY);
493 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
494 CreateGpuProcessViewContext(
495 const scoped_refptr<GpuChannelHost>& gpu_channel_host,
496 const blink::WebGraphicsContext3D::Attributes attributes,
498 DCHECK(gpu_channel_host);
500 GURL url("chrome://gpu/Compositor::createContext3D");
501 static const size_t kBytesPerPixel = 4;
502 gfx::DeviceDisplayInfo display_info;
503 size_t full_screen_texture_size_in_bytes =
504 display_info.GetDisplayHeight() *
505 display_info.GetDisplayWidth() *
507 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits limits;
508 limits.command_buffer_size = 64 * 1024;
509 limits.start_transfer_buffer_size = 64 * 1024;
510 limits.min_transfer_buffer_size = 64 * 1024;
511 limits.max_transfer_buffer_size = std::min(
512 3 * full_screen_texture_size_in_bytes, kDefaultMaxTransferBufferSize);
513 limits.mapped_memory_reclaim_limit = 2 * 1024 * 1024;
514 bool lose_context_when_out_of_memory = true;
515 return make_scoped_ptr(
516 new WebGraphicsContext3DCommandBufferImpl(surface_id,
518 gpu_channel_host.get(),
520 lose_context_when_out_of_memory,
525 void CompositorImpl::Layout() {
526 // TODO: If we get this callback from the SingleThreadProxy, we need
527 // to stop calling it ourselves in CompositorImpl::Composite().
532 scoped_ptr<cc::OutputSurface> CompositorImpl::CreateOutputSurface(
534 blink::WebGraphicsContext3D::Attributes attrs;
535 attrs.shareResources = true;
536 attrs.noAutomaticFlushes = true;
537 pending_swapbuffers_ = 0;
542 scoped_refptr<ContextProviderCommandBuffer> context_provider;
543 BrowserGpuChannelHostFactory* factory =
544 BrowserGpuChannelHostFactory::instance();
545 scoped_refptr<GpuChannelHost> gpu_channel_host = factory->GetGpuChannel();
546 if (gpu_channel_host && !gpu_channel_host->IsLost()) {
547 context_provider = ContextProviderCommandBuffer::Create(
548 CreateGpuProcessViewContext(gpu_channel_host, attrs, surface_id_),
549 "BrowserCompositor");
551 if (!context_provider.get()) {
552 LOG(ERROR) << "Failed to create 3D context for compositor.";
553 return scoped_ptr<cc::OutputSurface>();
556 return scoped_ptr<cc::OutputSurface>(
557 new OutputSurfaceWithoutParent(context_provider));
560 void CompositorImpl::OnLostResources() {
561 client_->DidLoseResources();
562 ui_resource_provider_.UIResourcesAreInvalid();
565 void CompositorImpl::ScheduleComposite() {
566 DCHECK(!needs_composite_ || WillComposite());
567 if (ignore_schedule_composite_)
570 needs_composite_ = true;
571 // We currently expect layer tree invalidations at most once per frame
572 // during normal operation and therefore try to composite immediately
573 // to minimize latency.
574 PostComposite(COMPOSITE_IMMEDIATELY);
577 void CompositorImpl::ScheduleAnimation() {
578 DCHECK(!needs_animate_ || needs_composite_);
579 DCHECK(!needs_composite_ || WillComposite());
580 needs_animate_ = true;
582 if (needs_composite_)
585 TRACE_EVENT0("cc", "CompositorImpl::ScheduleAnimation");
586 needs_composite_ = true;
587 PostComposite(COMPOSITE_EVENTUALLY);
590 void CompositorImpl::DidPostSwapBuffers() {
591 TRACE_EVENT0("compositor", "CompositorImpl::DidPostSwapBuffers");
592 did_post_swapbuffers_ = true;
595 void CompositorImpl::DidCompleteSwapBuffers() {
596 TRACE_EVENT0("compositor", "CompositorImpl::DidCompleteSwapBuffers");
597 DCHECK_GT(pending_swapbuffers_, 0U);
598 if (pending_swapbuffers_-- == kMaxSwapBuffers && needs_composite_)
599 PostComposite(COMPOSITE_IMMEDIATELY);
600 client_->OnSwapBuffersCompleted(pending_swapbuffers_);
603 void CompositorImpl::DidAbortSwapBuffers() {
604 TRACE_EVENT0("compositor", "CompositorImpl::DidAbortSwapBuffers");
605 // This really gets called only once from
606 // SingleThreadProxy::DidLoseOutputSurfaceOnImplThread() when the
608 client_->OnSwapBuffersCompleted(0);
611 void CompositorImpl::DidCommit() {
612 root_window_->OnCompositingDidCommit();
615 void CompositorImpl::AttachLayerForReadback(scoped_refptr<cc::Layer> layer) {
616 root_layer_->AddChild(layer);
619 void CompositorImpl::RequestCopyOfOutputOnRootLayer(
620 scoped_ptr<cc::CopyOutputRequest> request) {
621 root_layer_->RequestCopyOfOutput(request.Pass());
624 void CompositorImpl::OnVSync(base::TimeTicks frame_time,
625 base::TimeDelta vsync_period) {
626 vsync_period_ = vsync_period;
627 last_vsync_ = frame_time;
629 if (WillCompositeThisFrame()) {
630 // We somehow missed the last vsync interval, so reschedule for deadline.
631 // We cannot schedule immediately, or will get us out-of-phase with new
634 composite_on_vsync_trigger_ = COMPOSITE_EVENTUALLY;
636 current_composite_task_.reset();
639 DCHECK(!DidCompositeThisFrame() && !WillCompositeThisFrame());
640 if (composite_on_vsync_trigger_ != DO_NOT_COMPOSITE) {
641 CompositingTrigger trigger = composite_on_vsync_trigger_;
642 composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
643 PostComposite(trigger);
647 void CompositorImpl::SetNeedsAnimate() {
651 host_->SetNeedsAnimate();
654 } // namespace content