Trace background/visibility events
[chromium-blink-merge.git] / content / browser / renderer_host / compositor_impl_android.cc
blob853684f802e4b05a7edfb449be179f096b5c34aa
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/simple_thread.h"
22 #include "base/threading/thread.h"
23 #include "base/threading/thread_checker.h"
24 #include "cc/base/switches.h"
25 #include "cc/input/input_handler.h"
26 #include "cc/layers/layer.h"
27 #include "cc/output/compositor_frame.h"
28 #include "cc/output/context_provider.h"
29 #include "cc/output/output_surface.h"
30 #include "cc/output/output_surface_client.h"
31 #include "cc/resources/task_graph_runner.h"
32 #include "cc/scheduler/begin_frame_source.h"
33 #include "cc/surfaces/onscreen_display_client.h"
34 #include "cc/surfaces/surface_display_output_surface.h"
35 #include "cc/surfaces/surface_id_allocator.h"
36 #include "cc/surfaces/surface_manager.h"
37 #include "cc/trees/layer_tree_host.h"
38 #include "content/browser/android/child_process_launcher_android.h"
39 #include "content/browser/gpu/browser_gpu_channel_host_factory.h"
40 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h"
41 #include "content/browser/gpu/compositor_util.h"
42 #include "content/browser/gpu/gpu_surface_tracker.h"
43 #include "content/browser/renderer_host/render_widget_host_impl.h"
44 #include "content/common/gpu/client/command_buffer_proxy_impl.h"
45 #include "content/common/gpu/client/context_provider_command_buffer.h"
46 #include "content/common/gpu/client/gl_helper.h"
47 #include "content/common/gpu/client/gpu_channel_host.h"
48 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
49 #include "content/common/gpu/gpu_process_launch_causes.h"
50 #include "content/common/host_shared_bitmap_manager.h"
51 #include "content/public/browser/android/compositor_client.h"
52 #include "gpu/blink/webgraphicscontext3d_in_process_command_buffer_impl.h"
53 #include "gpu/command_buffer/client/context_support.h"
54 #include "gpu/command_buffer/client/gles2_interface.h"
55 #include "third_party/khronos/GLES2/gl2.h"
56 #include "third_party/khronos/GLES2/gl2ext.h"
57 #include "third_party/skia/include/core/SkMallocPixelRef.h"
58 #include "ui/android/window_android.h"
59 #include "ui/gfx/android/device_display_info.h"
60 #include "ui/gfx/frame_time.h"
61 #include "webkit/common/gpu/context_provider_in_process.h"
63 namespace content {
65 namespace {
67 const unsigned int kMaxSwapBuffers = 2U;
69 // Used to override capabilities_.adjust_deadline_for_parent to false
70 class OutputSurfaceWithoutParent : public cc::OutputSurface {
71 public:
72 OutputSurfaceWithoutParent(
73 const scoped_refptr<ContextProviderCommandBuffer>& context_provider,
74 base::WeakPtr<CompositorImpl> compositor_impl)
75 : cc::OutputSurface(context_provider),
76 swap_buffers_completion_callback_(
77 base::Bind(&OutputSurfaceWithoutParent::OnSwapBuffersCompleted,
78 base::Unretained(this))) {
79 capabilities_.adjust_deadline_for_parent = false;
80 capabilities_.max_frames_pending = 2;
81 compositor_impl_ = compositor_impl;
82 main_thread_ = base::MessageLoopProxy::current();
85 void SwapBuffers(cc::CompositorFrame* frame) override {
86 GetCommandBufferProxy()->SetLatencyInfo(frame->metadata.latency_info);
87 DCHECK(frame->gl_frame_data->sub_buffer_rect ==
88 gfx::Rect(frame->gl_frame_data->size));
89 context_provider_->ContextSupport()->Swap();
90 client_->DidSwapBuffers();
93 bool BindToClient(cc::OutputSurfaceClient* client) override {
94 if (!OutputSurface::BindToClient(client))
95 return false;
97 GetCommandBufferProxy()->SetSwapBuffersCompletionCallback(
98 swap_buffers_completion_callback_.callback());
100 main_thread_->PostTask(
101 FROM_HERE,
102 base::Bind(&CompositorImpl::PopulateGpuCapabilities,
103 compositor_impl_,
104 context_provider_->ContextCapabilities().gpu));
106 return true;
109 private:
110 CommandBufferProxyImpl* GetCommandBufferProxy() {
111 ContextProviderCommandBuffer* provider_command_buffer =
112 static_cast<content::ContextProviderCommandBuffer*>(
113 context_provider_.get());
114 CommandBufferProxyImpl* command_buffer_proxy =
115 provider_command_buffer->GetCommandBufferProxy();
116 DCHECK(command_buffer_proxy);
117 return command_buffer_proxy;
120 void OnSwapBuffersCompleted(
121 const std::vector<ui::LatencyInfo>& latency_info) {
122 RenderWidgetHostImpl::CompositorFrameDrawn(latency_info);
123 OutputSurface::OnSwapBuffersComplete();
126 base::CancelableCallback<void(const std::vector<ui::LatencyInfo>&)>
127 swap_buffers_completion_callback_;
129 scoped_refptr<base::MessageLoopProxy> main_thread_;
130 base::WeakPtr<CompositorImpl> compositor_impl_;
133 static bool g_initialized = false;
135 bool g_use_surface_manager = false;
136 base::LazyInstance<cc::SurfaceManager> g_surface_manager =
137 LAZY_INSTANCE_INITIALIZER;
140 int g_surface_id_namespace = 0;
142 class SingleThreadTaskGraphRunner
143 : public cc::TaskGraphRunner,
144 public base::DelegateSimpleThread::Delegate {
145 public:
146 SingleThreadTaskGraphRunner()
147 : worker_thread_(this, "CompositorTileWorker1") {
148 worker_thread_.Start();
149 worker_thread_.SetThreadPriority(base::kThreadPriority_Background);
152 ~SingleThreadTaskGraphRunner() override {
153 Shutdown();
154 worker_thread_.Join();
157 private:
158 // Overridden from base::DelegateSimpleThread::Delegate:
159 void Run() override { cc::TaskGraphRunner::Run(); }
161 base::DelegateSimpleThread worker_thread_;
164 base::LazyInstance<SingleThreadTaskGraphRunner> g_task_graph_runner =
165 LAZY_INSTANCE_INITIALIZER;
167 } // anonymous namespace
169 // static
170 Compositor* Compositor::Create(CompositorClient* client,
171 gfx::NativeWindow root_window) {
172 return client ? new CompositorImpl(client, root_window) : NULL;
175 // static
176 void Compositor::Initialize() {
177 DCHECK(!CompositorImpl::IsInitialized());
178 g_initialized = true;
179 g_use_surface_manager = UseSurfacesEnabled();
182 // static
183 bool CompositorImpl::IsInitialized() {
184 return g_initialized;
187 // static
188 cc::SurfaceManager* CompositorImpl::GetSurfaceManager() {
189 if (!g_use_surface_manager)
190 return nullptr;
191 return g_surface_manager.Pointer();
194 // static
195 scoped_ptr<cc::SurfaceIdAllocator> CompositorImpl::CreateSurfaceIdAllocator() {
196 return make_scoped_ptr(new cc::SurfaceIdAllocator(++g_surface_id_namespace));
199 CompositorImpl::CompositorImpl(CompositorClient* client,
200 gfx::NativeWindow root_window)
201 : root_layer_(cc::Layer::Create()),
202 resource_manager_(&ui_resource_provider_),
203 surface_id_allocator_(CreateSurfaceIdAllocator()),
204 has_transparent_background_(false),
205 device_scale_factor_(1),
206 window_(NULL),
207 surface_id_(0),
208 client_(client),
209 root_window_(root_window),
210 did_post_swapbuffers_(false),
211 ignore_schedule_composite_(false),
212 needs_composite_(false),
213 needs_animate_(false),
214 will_composite_immediately_(false),
215 composite_on_vsync_trigger_(DO_NOT_COMPOSITE),
216 pending_swapbuffers_(0U),
217 num_successive_context_creation_failures_(0),
218 output_surface_request_pending_(false),
219 weak_factory_(this) {
220 DCHECK(client);
221 DCHECK(root_window);
222 root_window->AttachCompositor(this);
225 CompositorImpl::~CompositorImpl() {
226 root_window_->DetachCompositor();
227 // Clean-up any surface references.
228 SetSurface(NULL);
231 void CompositorImpl::PostComposite(CompositingTrigger trigger) {
232 DCHECK(needs_composite_);
233 DCHECK(trigger == COMPOSITE_IMMEDIATELY || trigger == COMPOSITE_EVENTUALLY);
235 if (will_composite_immediately_ ||
236 (trigger == COMPOSITE_EVENTUALLY && WillComposite())) {
237 // We will already composite soon enough.
238 DCHECK(WillComposite());
239 return;
242 if (DidCompositeThisFrame()) {
243 DCHECK(!WillCompositeThisFrame());
244 if (composite_on_vsync_trigger_ != COMPOSITE_IMMEDIATELY) {
245 composite_on_vsync_trigger_ = trigger;
246 root_window_->RequestVSyncUpdate();
248 DCHECK(WillComposite());
249 return;
252 base::TimeDelta delay;
253 if (trigger == COMPOSITE_IMMEDIATELY) {
254 will_composite_immediately_ = true;
255 composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
256 } else {
257 DCHECK(!WillComposite());
258 const base::TimeDelta estimated_composite_time = vsync_period_ / 4;
259 const base::TimeTicks now = base::TimeTicks::Now();
261 if (!last_vsync_.is_null() && (now - last_vsync_) < vsync_period_) {
262 base::TimeTicks next_composite =
263 last_vsync_ + vsync_period_ - estimated_composite_time;
264 if (next_composite < now) {
265 // It's too late, we will reschedule composite as needed on the next
266 // vsync.
267 composite_on_vsync_trigger_ = COMPOSITE_EVENTUALLY;
268 root_window_->RequestVSyncUpdate();
269 DCHECK(WillComposite());
270 return;
273 delay = next_composite - now;
276 TRACE_EVENT2("cc,benchmark", "CompositorImpl::PostComposite",
277 "trigger", trigger,
278 "delay", delay.InMillisecondsF());
280 DCHECK(composite_on_vsync_trigger_ == DO_NOT_COMPOSITE);
281 if (current_composite_task_)
282 current_composite_task_->Cancel();
284 // Unretained because we cancel the task on shutdown.
285 current_composite_task_.reset(new base::CancelableClosure(
286 base::Bind(&CompositorImpl::Composite, base::Unretained(this), trigger)));
287 base::MessageLoop::current()->PostDelayedTask(
288 FROM_HERE, current_composite_task_->callback(), delay);
291 void CompositorImpl::Composite(CompositingTrigger trigger) {
292 if (trigger == COMPOSITE_IMMEDIATELY)
293 will_composite_immediately_ = false;
295 DCHECK(host_);
296 DCHECK(trigger == COMPOSITE_IMMEDIATELY || trigger == COMPOSITE_EVENTUALLY);
297 DCHECK(needs_composite_);
298 DCHECK(!DidCompositeThisFrame());
300 DCHECK_LE(pending_swapbuffers_, kMaxSwapBuffers);
301 // Swap Ack accounting is unreliable if the OutputSurface was lost.
302 // In that case still attempt to composite, which will cause creation of a
303 // new OutputSurface and reset pending_swapbuffers_.
304 if (pending_swapbuffers_ == kMaxSwapBuffers &&
305 !host_->output_surface_lost()) {
306 TRACE_EVENT0("compositor", "CompositorImpl_SwapLimit");
307 return;
310 // Reset state before Layout+Composite since that might create more
311 // requests to Composite that we need to respect.
312 needs_composite_ = false;
314 // Only allow compositing once per vsync.
315 current_composite_task_->Cancel();
316 DCHECK(DidCompositeThisFrame() && !WillComposite());
318 // Ignore ScheduleComposite() from layer tree changes during layout and
319 // animation updates that will already be reflected in the current frame
320 // we are about to draw.
321 ignore_schedule_composite_ = true;
323 const base::TimeTicks frame_time = gfx::FrameTime::Now();
324 if (needs_animate_) {
325 needs_animate_ = false;
326 root_window_->Animate(frame_time);
328 ignore_schedule_composite_ = false;
330 did_post_swapbuffers_ = false;
331 host_->Composite(frame_time);
332 if (did_post_swapbuffers_)
333 pending_swapbuffers_++;
335 // Need to track vsync to avoid compositing more than once per frame.
336 root_window_->RequestVSyncUpdate();
339 ui::UIResourceProvider& CompositorImpl::GetUIResourceProvider() {
340 return ui_resource_provider_;
343 ui::ResourceManager& CompositorImpl::GetResourceManager() {
344 return resource_manager_;
347 void CompositorImpl::SetRootLayer(scoped_refptr<cc::Layer> root_layer) {
348 if (subroot_layer_.get()) {
349 subroot_layer_->RemoveFromParent();
350 subroot_layer_ = NULL;
352 if (root_layer.get()) {
353 subroot_layer_ = root_layer;
354 root_layer_->AddChild(root_layer);
358 void CompositorImpl::SetWindowSurface(ANativeWindow* window) {
359 GpuSurfaceTracker* tracker = GpuSurfaceTracker::Get();
361 if (window_) {
362 tracker->RemoveSurface(surface_id_);
363 ANativeWindow_release(window_);
364 window_ = NULL;
365 surface_id_ = 0;
366 SetVisible(false);
369 if (window) {
370 window_ = window;
371 ANativeWindow_acquire(window);
372 surface_id_ = tracker->AddSurfaceForNativeWidget(window);
373 tracker->SetSurfaceHandle(
374 surface_id_,
375 gfx::GLSurfaceHandle(gfx::kNullPluginWindow, gfx::NATIVE_DIRECT));
376 SetVisible(true);
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.
385 if (surface_id_)
386 UnregisterViewSurface(surface_id_);
387 SetWindowSurface(NULL);
389 // Now, set the new surface if we have one.
390 ANativeWindow* window = NULL;
391 if (surface) {
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);
398 if (window) {
399 SetWindowSurface(window);
400 ANativeWindow_release(window);
401 RegisterViewSurface(surface_id_, j_surface.obj());
405 void CompositorImpl::CreateLayerTreeHost() {
406 DCHECK(!host_);
407 DCHECK(!WillCompositeThisFrame());
408 needs_composite_ = false;
409 pending_swapbuffers_ = 0;
410 cc::LayerTreeSettings settings;
411 settings.renderer_settings.refresh_rate = 60.0;
412 settings.renderer_settings.allow_antialiasing = false;
413 settings.renderer_settings.highp_threshold_min = 2048;
414 settings.impl_side_painting = true;
416 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
417 settings.initial_debug_state.SetRecordRenderingStats(
418 command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking));
419 settings.initial_debug_state.show_fps_counter =
420 command_line->HasSwitch(cc::switches::kUIShowFPSCounter);
421 // TODO(enne): Update this this compositor to use the scheduler.
422 settings.single_thread_proxy_scheduler = false;
424 host_ = cc::LayerTreeHost::CreateSingleThreaded(
425 this, this, HostSharedBitmapManager::current(),
426 BrowserGpuMemoryBufferManager::current(), g_task_graph_runner.Pointer(),
427 settings, base::MessageLoopProxy::current(), nullptr);
428 host_->SetRootLayer(root_layer_);
430 host_->SetVisible(true);
431 host_->SetLayerTreeHostClientReady();
432 host_->SetViewportSize(size_);
433 host_->set_has_transparent_background(has_transparent_background_);
434 host_->SetDeviceScaleFactor(device_scale_factor_);
436 if (needs_animate_)
437 host_->SetNeedsAnimate();
440 void CompositorImpl::SetVisible(bool visible) {
441 TRACE_EVENT1("cc", "CompositorImpl::SetVisible", "visible", visible);
442 if (!visible) {
443 DCHECK(host_);
444 // Look for any layers that were attached to the root for readback
445 // and are waiting for Composite() to happen.
446 bool readback_pending = false;
447 for (size_t i = 0; i < root_layer_->children().size(); ++i) {
448 if (root_layer_->children()[i]->HasCopyRequest()) {
449 readback_pending = true;
450 break;
453 if (readback_pending) {
454 ignore_schedule_composite_ = true;
455 host_->Composite(base::TimeTicks::Now());
456 ignore_schedule_composite_ = false;
458 if (WillComposite())
459 CancelComposite();
460 ui_resource_provider_.SetLayerTreeHost(NULL);
461 host_.reset();
462 establish_gpu_channel_timeout_.Stop();
463 output_surface_request_pending_ = false;
464 display_client_.reset();
465 if (current_composite_task_) {
466 current_composite_task_->Cancel();
467 current_composite_task_.reset();
469 } else if (!host_) {
470 CreateLayerTreeHost();
471 ui_resource_provider_.SetLayerTreeHost(host_.get());
473 root_window_->OnVisibilityChanged(visible);
476 void CompositorImpl::setDeviceScaleFactor(float factor) {
477 device_scale_factor_ = factor;
478 if (host_)
479 host_->SetDeviceScaleFactor(factor);
482 void CompositorImpl::SetWindowBounds(const gfx::Size& size) {
483 if (size_ == size)
484 return;
486 size_ = size;
487 if (host_)
488 host_->SetViewportSize(size);
489 if (display_client_)
490 display_client_->display()->Resize(size);
491 root_layer_->SetBounds(size);
494 void CompositorImpl::SetHasTransparentBackground(bool flag) {
495 has_transparent_background_ = flag;
496 if (host_)
497 host_->set_has_transparent_background(flag);
500 void CompositorImpl::SetNeedsComposite() {
501 if (!host_.get())
502 return;
503 DCHECK(!needs_composite_ || WillComposite());
505 needs_composite_ = true;
506 PostComposite(COMPOSITE_IMMEDIATELY);
509 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
510 CreateGpuProcessViewContext(
511 const scoped_refptr<GpuChannelHost>& gpu_channel_host,
512 const blink::WebGraphicsContext3D::Attributes attributes,
513 int surface_id) {
514 GURL url("chrome://gpu/Compositor::createContext3D");
515 static const size_t kBytesPerPixel = 4;
516 gfx::DeviceDisplayInfo display_info;
517 size_t full_screen_texture_size_in_bytes =
518 display_info.GetDisplayHeight() *
519 display_info.GetDisplayWidth() *
520 kBytesPerPixel;
521 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits limits;
522 limits.command_buffer_size = 64 * 1024;
523 limits.start_transfer_buffer_size = 64 * 1024;
524 limits.min_transfer_buffer_size = 64 * 1024;
525 limits.max_transfer_buffer_size = std::min(
526 3 * full_screen_texture_size_in_bytes, kDefaultMaxTransferBufferSize);
527 limits.mapped_memory_reclaim_limit = 2 * 1024 * 1024;
528 bool lose_context_when_out_of_memory = true;
529 return make_scoped_ptr(
530 new WebGraphicsContext3DCommandBufferImpl(surface_id,
531 url,
532 gpu_channel_host.get(),
533 attributes,
534 lose_context_when_out_of_memory,
535 limits,
536 NULL));
539 void CompositorImpl::Layout() {
540 ignore_schedule_composite_ = true;
541 client_->Layout();
542 ignore_schedule_composite_ = false;
545 void CompositorImpl::OnGpuChannelEstablished() {
546 establish_gpu_channel_timeout_.Stop();
547 CreateOutputSurface();
550 void CompositorImpl::OnGpuChannelTimeout() {
551 LOG(FATAL) << "Timed out waiting for GPU channel.";
554 void CompositorImpl::RequestNewOutputSurface() {
555 output_surface_request_pending_ = true;
557 BrowserGpuChannelHostFactory* factory =
558 BrowserGpuChannelHostFactory::instance();
559 if (!factory->GetGpuChannel() || factory->GetGpuChannel()->IsLost()) {
560 factory->EstablishGpuChannel(
561 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE,
562 base::Bind(&CompositorImpl::OnGpuChannelEstablished,
563 weak_factory_.GetWeakPtr()));
564 establish_gpu_channel_timeout_.Start(
565 FROM_HERE, base::TimeDelta::FromSeconds(7), this,
566 &CompositorImpl::OnGpuChannelTimeout);
567 return;
570 CreateOutputSurface();
573 void CompositorImpl::DidInitializeOutputSurface() {
574 num_successive_context_creation_failures_ = 0;
575 output_surface_request_pending_ = false;
578 void CompositorImpl::DidFailToInitializeOutputSurface() {
579 LOG(ERROR) << "Failed to init OutputSurface for compositor.";
580 LOG_IF(FATAL, ++num_successive_context_creation_failures_ >= 2)
581 << "Too many context creation failures. Giving up... ";
582 RequestNewOutputSurface();
585 void CompositorImpl::CreateOutputSurface() {
586 // We might have had a request from a LayerTreeHost that was then
587 // deleted.
588 if (!output_surface_request_pending_)
589 return;
591 blink::WebGraphicsContext3D::Attributes attrs;
592 attrs.shareResources = true;
593 attrs.noAutomaticFlushes = true;
594 pending_swapbuffers_ = 0;
596 DCHECK(window_);
597 DCHECK(surface_id_);
599 BrowserGpuChannelHostFactory* factory =
600 BrowserGpuChannelHostFactory::instance();
601 // This channel might be lost (and even if it isn't right now, it might
602 // still get marked as lost from the IO thread, at any point in time really).
603 // But from here on just try and always lead to either
604 // DidInitializeOutputSurface() or DidFailToInitializeOutputSurface().
605 scoped_refptr<GpuChannelHost> gpu_channel_host(factory->GetGpuChannel());
606 scoped_refptr<ContextProviderCommandBuffer> context_provider(
607 ContextProviderCommandBuffer::Create(
608 CreateGpuProcessViewContext(gpu_channel_host, attrs, surface_id_),
609 "BrowserCompositor"));
610 DCHECK(context_provider.get());
612 scoped_ptr<cc::OutputSurface> real_output_surface(
613 new OutputSurfaceWithoutParent(context_provider,
614 weak_factory_.GetWeakPtr()));
616 cc::SurfaceManager* manager = GetSurfaceManager();
617 if (manager) {
618 display_client_.reset(new cc::OnscreenDisplayClient(
619 real_output_surface.Pass(), manager, HostSharedBitmapManager::current(),
620 BrowserGpuMemoryBufferManager::current(),
621 host_->settings().renderer_settings,
622 base::MessageLoopProxy::current()));
623 scoped_ptr<cc::SurfaceDisplayOutputSurface> surface_output_surface(
624 new cc::SurfaceDisplayOutputSurface(
625 manager, surface_id_allocator_.get(), context_provider));
627 display_client_->set_surface_output_surface(surface_output_surface.get());
628 surface_output_surface->set_display_client(display_client_.get());
629 display_client_->display()->Resize(size_);
630 host_->SetOutputSurface(surface_output_surface.Pass());
631 } else {
632 host_->SetOutputSurface(real_output_surface.Pass());
636 void CompositorImpl::PopulateGpuCapabilities(
637 gpu::Capabilities gpu_capabilities) {
638 ui_resource_provider_.SetSupportsETC1NonPowerOfTwo(
639 gpu_capabilities.texture_format_etc1_npot);
642 void CompositorImpl::ScheduleComposite() {
643 DCHECK(!needs_composite_ || WillComposite());
644 if (ignore_schedule_composite_)
645 return;
647 needs_composite_ = true;
648 // We currently expect layer tree invalidations at most once per frame
649 // during normal operation and therefore try to composite immediately
650 // to minimize latency.
651 PostComposite(COMPOSITE_IMMEDIATELY);
654 void CompositorImpl::ScheduleAnimation() {
655 DCHECK(!needs_composite_ || WillComposite());
656 needs_animate_ = true;
658 if (needs_composite_)
659 return;
661 TRACE_EVENT0("cc", "CompositorImpl::ScheduleAnimation");
662 needs_composite_ = true;
663 PostComposite(COMPOSITE_EVENTUALLY);
666 void CompositorImpl::DidPostSwapBuffers() {
667 TRACE_EVENT0("compositor", "CompositorImpl::DidPostSwapBuffers");
668 did_post_swapbuffers_ = true;
671 void CompositorImpl::DidCompleteSwapBuffers() {
672 TRACE_EVENT0("compositor", "CompositorImpl::DidCompleteSwapBuffers");
673 DCHECK_GT(pending_swapbuffers_, 0U);
674 if (pending_swapbuffers_-- == kMaxSwapBuffers && needs_composite_)
675 PostComposite(COMPOSITE_IMMEDIATELY);
676 client_->OnSwapBuffersCompleted(pending_swapbuffers_);
679 void CompositorImpl::DidAbortSwapBuffers() {
680 TRACE_EVENT0("compositor", "CompositorImpl::DidAbortSwapBuffers");
681 // This really gets called only once from
682 // SingleThreadProxy::DidLoseOutputSurfaceOnImplThread() when the
683 // context was lost.
684 ScheduleComposite();
685 client_->OnSwapBuffersCompleted(0);
688 void CompositorImpl::DidCommit() {
689 root_window_->OnCompositingDidCommit();
692 void CompositorImpl::AttachLayerForReadback(scoped_refptr<cc::Layer> layer) {
693 root_layer_->AddChild(layer);
696 void CompositorImpl::RequestCopyOfOutputOnRootLayer(
697 scoped_ptr<cc::CopyOutputRequest> request) {
698 root_layer_->RequestCopyOfOutput(request.Pass());
701 void CompositorImpl::OnVSync(base::TimeTicks frame_time,
702 base::TimeDelta vsync_period) {
703 vsync_period_ = vsync_period;
704 last_vsync_ = frame_time;
706 if (WillCompositeThisFrame()) {
707 // We somehow missed the last vsync interval, so reschedule for deadline.
708 // We cannot schedule immediately, or will get us out-of-phase with new
709 // renderer frames.
710 CancelComposite();
711 composite_on_vsync_trigger_ = COMPOSITE_EVENTUALLY;
712 } else {
713 current_composite_task_.reset();
716 DCHECK(!DidCompositeThisFrame() && !WillCompositeThisFrame());
717 if (composite_on_vsync_trigger_ != DO_NOT_COMPOSITE) {
718 CompositingTrigger trigger = composite_on_vsync_trigger_;
719 composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
720 PostComposite(trigger);
724 void CompositorImpl::SetNeedsAnimate() {
725 needs_animate_ = true;
726 if (!host_)
727 return;
729 host_->SetNeedsAnimate();
732 } // namespace content