Adding status to swap buffers completion
[chromium-blink-merge.git] / content / browser / renderer_host / compositor_impl_android.cc
blobf5ec1e5e42ebd45c37ad32764eaacc1e6bc91fed
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/thread_task_runner_handle.h"
22 #include "base/threading/simple_thread.h"
23 #include "base/threading/thread.h"
24 #include "base/threading/thread_checker.h"
25 #include "cc/base/switches.h"
26 #include "cc/input/input_handler.h"
27 #include "cc/layers/layer.h"
28 #include "cc/output/compositor_frame.h"
29 #include "cc/output/context_provider.h"
30 #include "cc/output/output_surface.h"
31 #include "cc/output/output_surface_client.h"
32 #include "cc/raster/task_graph_runner.h"
33 #include "cc/scheduler/begin_frame_source.h"
34 #include "cc/surfaces/onscreen_display_client.h"
35 #include "cc/surfaces/surface_display_output_surface.h"
36 #include "cc/surfaces/surface_id_allocator.h"
37 #include "cc/surfaces/surface_manager.h"
38 #include "cc/trees/layer_tree_host.h"
39 #include "cc/trees/layer_tree_settings.h"
40 #include "content/browser/android/child_process_launcher_android.h"
41 #include "content/browser/gpu/browser_gpu_channel_host_factory.h"
42 #include "content/browser/gpu/browser_gpu_memory_buffer_manager.h"
43 #include "content/browser/gpu/compositor_util.h"
44 #include "content/browser/gpu/gpu_surface_tracker.h"
45 #include "content/browser/renderer_host/render_widget_host_impl.h"
46 #include "content/common/gpu/client/command_buffer_proxy_impl.h"
47 #include "content/common/gpu/client/context_provider_command_buffer.h"
48 #include "content/common/gpu/client/gl_helper.h"
49 #include "content/common/gpu/client/gpu_channel_host.h"
50 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
51 #include "content/common/gpu/gpu_process_launch_causes.h"
52 #include "content/common/host_shared_bitmap_manager.h"
53 #include "content/public/browser/android/compositor.h"
54 #include "content/public/browser/android/compositor_client.h"
55 #include "gpu/blink/webgraphicscontext3d_in_process_command_buffer_impl.h"
56 #include "gpu/command_buffer/client/context_support.h"
57 #include "gpu/command_buffer/client/gles2_interface.h"
58 #include "third_party/khronos/GLES2/gl2.h"
59 #include "third_party/khronos/GLES2/gl2ext.h"
60 #include "third_party/skia/include/core/SkMallocPixelRef.h"
61 #include "ui/android/window_android.h"
62 #include "ui/gfx/android/device_display_info.h"
63 #include "ui/gfx/swap_result.h"
65 namespace content {
67 namespace {
69 const unsigned int kMaxSwapBuffers = 2U;
71 // Used to override capabilities_.adjust_deadline_for_parent to false
72 class OutputSurfaceWithoutParent : public cc::OutputSurface {
73 public:
74 OutputSurfaceWithoutParent(
75 const scoped_refptr<ContextProviderCommandBuffer>& context_provider,
76 base::WeakPtr<CompositorImpl> compositor_impl)
77 : cc::OutputSurface(context_provider),
78 swap_buffers_completion_callback_(
79 base::Bind(&OutputSurfaceWithoutParent::OnSwapBuffersCompleted,
80 base::Unretained(this))) {
81 capabilities_.adjust_deadline_for_parent = false;
82 capabilities_.max_frames_pending = 2;
83 compositor_impl_ = compositor_impl;
84 main_thread_ = base::MessageLoopProxy::current();
87 void SwapBuffers(cc::CompositorFrame* frame) override {
88 GetCommandBufferProxy()->SetLatencyInfo(frame->metadata.latency_info);
89 DCHECK(frame->gl_frame_data->sub_buffer_rect ==
90 gfx::Rect(frame->gl_frame_data->size));
91 context_provider_->ContextSupport()->Swap();
92 client_->DidSwapBuffers();
95 bool BindToClient(cc::OutputSurfaceClient* client) override {
96 if (!OutputSurface::BindToClient(client))
97 return false;
99 GetCommandBufferProxy()->SetSwapBuffersCompletionCallback(
100 swap_buffers_completion_callback_.callback());
102 main_thread_->PostTask(
103 FROM_HERE,
104 base::Bind(&CompositorImpl::PopulateGpuCapabilities,
105 compositor_impl_,
106 context_provider_->ContextCapabilities().gpu));
108 return true;
111 private:
112 CommandBufferProxyImpl* GetCommandBufferProxy() {
113 ContextProviderCommandBuffer* provider_command_buffer =
114 static_cast<content::ContextProviderCommandBuffer*>(
115 context_provider_.get());
116 CommandBufferProxyImpl* command_buffer_proxy =
117 provider_command_buffer->GetCommandBufferProxy();
118 DCHECK(command_buffer_proxy);
119 return command_buffer_proxy;
122 void OnSwapBuffersCompleted(const std::vector<ui::LatencyInfo>& latency_info,
123 gfx::SwapResult result) {
124 RenderWidgetHostImpl::CompositorFrameDrawn(latency_info);
125 OutputSurface::OnSwapBuffersComplete();
128 base::CancelableCallback<void(const std::vector<ui::LatencyInfo>&,
129 gfx::SwapResult)>
130 swap_buffers_completion_callback_;
132 scoped_refptr<base::MessageLoopProxy> main_thread_;
133 base::WeakPtr<CompositorImpl> compositor_impl_;
136 static bool g_initialized = false;
138 bool g_use_surface_manager = false;
139 base::LazyInstance<cc::SurfaceManager> g_surface_manager =
140 LAZY_INSTANCE_INITIALIZER;
143 int g_surface_id_namespace = 0;
145 class SingleThreadTaskGraphRunner
146 : public cc::TaskGraphRunner,
147 public base::DelegateSimpleThread::Delegate {
148 public:
149 SingleThreadTaskGraphRunner()
150 : worker_thread_(this, "CompositorTileWorker1") {
151 worker_thread_.Start();
152 worker_thread_.SetThreadPriority(base::ThreadPriority::BACKGROUND);
155 ~SingleThreadTaskGraphRunner() override {
156 Shutdown();
157 worker_thread_.Join();
160 private:
161 // Overridden from base::DelegateSimpleThread::Delegate:
162 void Run() override { cc::TaskGraphRunner::Run(); }
164 base::DelegateSimpleThread worker_thread_;
167 base::LazyInstance<SingleThreadTaskGraphRunner> g_task_graph_runner =
168 LAZY_INSTANCE_INITIALIZER;
170 base::LazyInstance<cc::LayerSettings> g_layer_settings =
171 LAZY_INSTANCE_INITIALIZER;
173 } // anonymous namespace
175 // static
176 Compositor* Compositor::Create(CompositorClient* client,
177 gfx::NativeWindow root_window) {
178 return client ? new CompositorImpl(client, root_window) : NULL;
181 // static
182 void Compositor::Initialize() {
183 DCHECK(!CompositorImpl::IsInitialized());
184 g_initialized = true;
185 g_use_surface_manager = UseSurfacesEnabled();
188 // static
189 const cc::LayerSettings& Compositor::LayerSettings() {
190 return g_layer_settings.Get();
193 // static
194 void Compositor::SetLayerSettings(const cc::LayerSettings& settings) {
195 g_layer_settings.Get() = settings;
198 // static
199 bool CompositorImpl::IsInitialized() {
200 return g_initialized;
203 // static
204 cc::SurfaceManager* CompositorImpl::GetSurfaceManager() {
205 if (!g_use_surface_manager)
206 return nullptr;
207 return g_surface_manager.Pointer();
210 // static
211 scoped_ptr<cc::SurfaceIdAllocator> CompositorImpl::CreateSurfaceIdAllocator() {
212 return make_scoped_ptr(new cc::SurfaceIdAllocator(++g_surface_id_namespace));
215 CompositorImpl::CompositorImpl(CompositorClient* client,
216 gfx::NativeWindow root_window)
217 : root_layer_(cc::Layer::Create(Compositor::LayerSettings())),
218 resource_manager_(&ui_resource_provider_),
219 surface_id_allocator_(CreateSurfaceIdAllocator()),
220 has_transparent_background_(false),
221 device_scale_factor_(1),
222 window_(NULL),
223 surface_id_(0),
224 client_(client),
225 root_window_(root_window),
226 did_post_swapbuffers_(false),
227 ignore_schedule_composite_(false),
228 needs_composite_(false),
229 needs_animate_(false),
230 will_composite_immediately_(false),
231 composite_on_vsync_trigger_(DO_NOT_COMPOSITE),
232 pending_swapbuffers_(0U),
233 num_successive_context_creation_failures_(0),
234 output_surface_request_pending_(false),
235 weak_factory_(this) {
236 DCHECK(client);
237 DCHECK(root_window);
238 root_window->AttachCompositor(this);
241 CompositorImpl::~CompositorImpl() {
242 root_window_->DetachCompositor();
243 // Clean-up any surface references.
244 SetSurface(NULL);
247 void CompositorImpl::PostComposite(CompositingTrigger trigger) {
248 DCHECK(needs_composite_);
249 DCHECK(trigger == COMPOSITE_IMMEDIATELY || trigger == COMPOSITE_EVENTUALLY);
251 if (will_composite_immediately_ ||
252 (trigger == COMPOSITE_EVENTUALLY && WillComposite())) {
253 // We will already composite soon enough.
254 DCHECK(WillComposite());
255 return;
258 if (DidCompositeThisFrame()) {
259 DCHECK(!WillCompositeThisFrame());
260 if (composite_on_vsync_trigger_ != COMPOSITE_IMMEDIATELY) {
261 composite_on_vsync_trigger_ = trigger;
262 root_window_->RequestVSyncUpdate();
264 DCHECK(WillComposite());
265 return;
268 base::TimeDelta delay;
269 if (trigger == COMPOSITE_IMMEDIATELY) {
270 will_composite_immediately_ = true;
271 composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
272 } else {
273 DCHECK(!WillComposite());
274 const base::TimeDelta estimated_composite_time = vsync_period_ / 4;
275 const base::TimeTicks now = base::TimeTicks::Now();
277 if (!last_vsync_.is_null() && (now - last_vsync_) < vsync_period_) {
278 base::TimeTicks next_composite =
279 last_vsync_ + vsync_period_ - estimated_composite_time;
280 if (next_composite < now) {
281 // It's too late, we will reschedule composite as needed on the next
282 // vsync.
283 composite_on_vsync_trigger_ = COMPOSITE_EVENTUALLY;
284 root_window_->RequestVSyncUpdate();
285 DCHECK(WillComposite());
286 return;
289 delay = next_composite - now;
292 TRACE_EVENT2("cc,benchmark", "CompositorImpl::PostComposite",
293 "trigger", trigger,
294 "delay", delay.InMillisecondsF());
296 DCHECK(composite_on_vsync_trigger_ == DO_NOT_COMPOSITE);
297 if (current_composite_task_)
298 current_composite_task_->Cancel();
300 // Unretained because we cancel the task on shutdown.
301 current_composite_task_.reset(new base::CancelableClosure(
302 base::Bind(&CompositorImpl::Composite, base::Unretained(this), trigger)));
303 base::MessageLoop::current()->PostDelayedTask(
304 FROM_HERE, current_composite_task_->callback(), delay);
307 void CompositorImpl::Composite(CompositingTrigger trigger) {
308 if (trigger == COMPOSITE_IMMEDIATELY)
309 will_composite_immediately_ = false;
311 DCHECK(host_);
312 DCHECK(trigger == COMPOSITE_IMMEDIATELY || trigger == COMPOSITE_EVENTUALLY);
313 DCHECK(needs_composite_);
314 DCHECK(!DidCompositeThisFrame());
316 DCHECK_LE(pending_swapbuffers_, kMaxSwapBuffers);
317 // Swap Ack accounting is unreliable if the OutputSurface was lost.
318 // In that case still attempt to composite, which will cause creation of a
319 // new OutputSurface and reset pending_swapbuffers_.
320 if (pending_swapbuffers_ == kMaxSwapBuffers &&
321 !host_->output_surface_lost()) {
322 TRACE_EVENT0("compositor", "CompositorImpl_SwapLimit");
323 return;
326 // Reset state before Layout+Composite since that might create more
327 // requests to Composite that we need to respect.
328 needs_composite_ = false;
330 // Only allow compositing once per vsync.
331 current_composite_task_->Cancel();
332 DCHECK(DidCompositeThisFrame() && !WillComposite());
334 // Ignore ScheduleComposite() from layer tree changes during layout and
335 // animation updates that will already be reflected in the current frame
336 // we are about to draw.
337 ignore_schedule_composite_ = true;
339 const base::TimeTicks frame_time = base::TimeTicks::Now();
340 if (needs_animate_) {
341 needs_animate_ = false;
342 root_window_->Animate(frame_time);
344 ignore_schedule_composite_ = false;
346 did_post_swapbuffers_ = false;
347 host_->Composite(frame_time);
348 if (did_post_swapbuffers_)
349 pending_swapbuffers_++;
351 // Need to track vsync to avoid compositing more than once per frame.
352 root_window_->RequestVSyncUpdate();
355 ui::UIResourceProvider& CompositorImpl::GetUIResourceProvider() {
356 return ui_resource_provider_;
359 ui::ResourceManager& CompositorImpl::GetResourceManager() {
360 return resource_manager_;
363 void CompositorImpl::SetRootLayer(scoped_refptr<cc::Layer> root_layer) {
364 if (subroot_layer_.get()) {
365 subroot_layer_->RemoveFromParent();
366 subroot_layer_ = NULL;
368 if (root_layer.get()) {
369 subroot_layer_ = root_layer;
370 root_layer_->AddChild(root_layer);
374 void CompositorImpl::SetWindowSurface(ANativeWindow* window) {
375 GpuSurfaceTracker* tracker = GpuSurfaceTracker::Get();
377 if (window_) {
378 tracker->RemoveSurface(surface_id_);
379 ANativeWindow_release(window_);
380 window_ = NULL;
381 surface_id_ = 0;
382 SetVisible(false);
385 if (window) {
386 window_ = window;
387 ANativeWindow_acquire(window);
388 surface_id_ = tracker->AddSurfaceForNativeWidget(window);
389 tracker->SetSurfaceHandle(
390 surface_id_,
391 gfx::GLSurfaceHandle(gfx::kNullPluginWindow, gfx::NATIVE_DIRECT));
392 SetVisible(true);
396 void CompositorImpl::SetSurface(jobject surface) {
397 JNIEnv* env = base::android::AttachCurrentThread();
398 base::android::ScopedJavaLocalRef<jobject> j_surface(env, surface);
400 // First, cleanup any existing surface references.
401 if (surface_id_)
402 UnregisterViewSurface(surface_id_);
403 SetWindowSurface(NULL);
405 // Now, set the new surface if we have one.
406 ANativeWindow* window = NULL;
407 if (surface) {
408 // Note: This ensures that any local references used by
409 // ANativeWindow_fromSurface are released immediately. This is needed as a
410 // workaround for https://code.google.com/p/android/issues/detail?id=68174
411 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
412 window = ANativeWindow_fromSurface(env, surface);
414 if (window) {
415 SetWindowSurface(window);
416 ANativeWindow_release(window);
417 RegisterViewSurface(surface_id_, j_surface.obj());
421 void CompositorImpl::CreateLayerTreeHost() {
422 DCHECK(!host_);
423 DCHECK(!WillCompositeThisFrame());
424 needs_composite_ = false;
425 pending_swapbuffers_ = 0;
426 cc::LayerTreeSettings settings;
427 settings.renderer_settings.refresh_rate = 60.0;
428 settings.renderer_settings.allow_antialiasing = false;
429 settings.renderer_settings.highp_threshold_min = 2048;
430 settings.impl_side_painting = true;
431 settings.use_zero_copy = true;
433 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
434 settings.initial_debug_state.SetRecordRenderingStats(
435 command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking));
436 settings.initial_debug_state.show_fps_counter =
437 command_line->HasSwitch(cc::switches::kUIShowFPSCounter);
438 // TODO(enne): Update this this compositor to use the scheduler.
439 settings.single_thread_proxy_scheduler = false;
441 cc::LayerTreeHost::InitParams params;
442 params.client = this;
443 params.shared_bitmap_manager = HostSharedBitmapManager::current();
444 params.gpu_memory_buffer_manager = BrowserGpuMemoryBufferManager::current();
445 params.task_graph_runner = g_task_graph_runner.Pointer();
446 params.main_task_runner = base::ThreadTaskRunnerHandle::Get();
447 params.settings = &settings;
448 host_ = cc::LayerTreeHost::CreateSingleThreaded(this, &params);
449 host_->SetRootLayer(root_layer_);
451 host_->SetVisible(true);
452 host_->SetLayerTreeHostClientReady();
453 host_->SetViewportSize(size_);
454 host_->set_has_transparent_background(has_transparent_background_);
455 host_->SetDeviceScaleFactor(device_scale_factor_);
457 if (needs_animate_)
458 host_->SetNeedsAnimate();
461 void CompositorImpl::SetVisible(bool visible) {
462 TRACE_EVENT1("cc", "CompositorImpl::SetVisible", "visible", visible);
463 if (!visible) {
464 DCHECK(host_);
465 // Look for any layers that were attached to the root for readback
466 // and are waiting for Composite() to happen.
467 bool readback_pending = false;
468 for (size_t i = 0; i < root_layer_->children().size(); ++i) {
469 if (root_layer_->children()[i]->HasCopyRequest()) {
470 readback_pending = true;
471 break;
474 if (readback_pending) {
475 ignore_schedule_composite_ = true;
476 host_->Composite(base::TimeTicks::Now());
477 ignore_schedule_composite_ = false;
479 if (WillComposite())
480 CancelComposite();
481 ui_resource_provider_.SetLayerTreeHost(NULL);
482 host_.reset();
483 establish_gpu_channel_timeout_.Stop();
484 output_surface_request_pending_ = false;
485 display_client_.reset();
486 if (current_composite_task_) {
487 current_composite_task_->Cancel();
488 current_composite_task_.reset();
490 } else if (!host_) {
491 CreateLayerTreeHost();
492 ui_resource_provider_.SetLayerTreeHost(host_.get());
494 root_window_->OnVisibilityChanged(visible);
497 void CompositorImpl::setDeviceScaleFactor(float factor) {
498 device_scale_factor_ = factor;
499 if (host_)
500 host_->SetDeviceScaleFactor(factor);
503 void CompositorImpl::SetWindowBounds(const gfx::Size& size) {
504 if (size_ == size)
505 return;
507 size_ = size;
508 if (host_)
509 host_->SetViewportSize(size);
510 if (display_client_)
511 display_client_->display()->Resize(size);
512 root_layer_->SetBounds(size);
515 void CompositorImpl::SetHasTransparentBackground(bool flag) {
516 has_transparent_background_ = flag;
517 if (host_)
518 host_->set_has_transparent_background(flag);
521 void CompositorImpl::SetNeedsComposite() {
522 if (!host_.get())
523 return;
524 DCHECK(!needs_composite_ || WillComposite());
526 needs_composite_ = true;
527 PostComposite(COMPOSITE_IMMEDIATELY);
530 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
531 CreateGpuProcessViewContext(
532 const scoped_refptr<GpuChannelHost>& gpu_channel_host,
533 const blink::WebGraphicsContext3D::Attributes attributes,
534 int surface_id) {
535 GURL url("chrome://gpu/Compositor::createContext3D");
536 static const size_t kBytesPerPixel = 4;
537 gfx::DeviceDisplayInfo display_info;
538 size_t full_screen_texture_size_in_bytes =
539 display_info.GetDisplayHeight() *
540 display_info.GetDisplayWidth() *
541 kBytesPerPixel;
542 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits limits;
543 limits.command_buffer_size = 64 * 1024;
544 limits.start_transfer_buffer_size = 64 * 1024;
545 limits.min_transfer_buffer_size = 64 * 1024;
546 limits.max_transfer_buffer_size = std::min(
547 3 * full_screen_texture_size_in_bytes, kDefaultMaxTransferBufferSize);
548 limits.mapped_memory_reclaim_limit = 2 * 1024 * 1024;
549 bool lose_context_when_out_of_memory = true;
550 return make_scoped_ptr(
551 new WebGraphicsContext3DCommandBufferImpl(surface_id,
552 url,
553 gpu_channel_host.get(),
554 attributes,
555 lose_context_when_out_of_memory,
556 limits,
557 NULL));
560 void CompositorImpl::Layout() {
561 ignore_schedule_composite_ = true;
562 client_->Layout();
563 ignore_schedule_composite_ = false;
566 void CompositorImpl::OnGpuChannelEstablished() {
567 establish_gpu_channel_timeout_.Stop();
568 CreateOutputSurface();
571 void CompositorImpl::OnGpuChannelTimeout() {
572 LOG(FATAL) << "Timed out waiting for GPU channel.";
575 void CompositorImpl::RequestNewOutputSurface() {
576 output_surface_request_pending_ = true;
578 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || defined(SYZYASAN)
579 const int64 kGpuChannelTimeoutInSeconds = 30;
580 #else
581 const int64 kGpuChannelTimeoutInSeconds = 10;
582 #endif
584 BrowserGpuChannelHostFactory* factory =
585 BrowserGpuChannelHostFactory::instance();
586 if (!factory->GetGpuChannel() || factory->GetGpuChannel()->IsLost()) {
587 factory->EstablishGpuChannel(
588 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE,
589 base::Bind(&CompositorImpl::OnGpuChannelEstablished,
590 weak_factory_.GetWeakPtr()));
591 establish_gpu_channel_timeout_.Start(
592 FROM_HERE, base::TimeDelta::FromSeconds(kGpuChannelTimeoutInSeconds),
593 this, &CompositorImpl::OnGpuChannelTimeout);
594 return;
597 CreateOutputSurface();
600 void CompositorImpl::DidInitializeOutputSurface() {
601 num_successive_context_creation_failures_ = 0;
602 output_surface_request_pending_ = false;
605 void CompositorImpl::DidFailToInitializeOutputSurface() {
606 LOG(ERROR) << "Failed to init OutputSurface for compositor.";
607 LOG_IF(FATAL, ++num_successive_context_creation_failures_ >= 2)
608 << "Too many context creation failures. Giving up... ";
609 RequestNewOutputSurface();
612 void CompositorImpl::CreateOutputSurface() {
613 // We might have had a request from a LayerTreeHost that was then
614 // deleted.
615 if (!output_surface_request_pending_)
616 return;
618 blink::WebGraphicsContext3D::Attributes attrs;
619 attrs.shareResources = true;
620 attrs.noAutomaticFlushes = true;
621 pending_swapbuffers_ = 0;
623 DCHECK(window_);
624 DCHECK(surface_id_);
626 BrowserGpuChannelHostFactory* factory =
627 BrowserGpuChannelHostFactory::instance();
628 // This channel might be lost (and even if it isn't right now, it might
629 // still get marked as lost from the IO thread, at any point in time really).
630 // But from here on just try and always lead to either
631 // DidInitializeOutputSurface() or DidFailToInitializeOutputSurface().
632 scoped_refptr<GpuChannelHost> gpu_channel_host(factory->GetGpuChannel());
633 scoped_refptr<ContextProviderCommandBuffer> context_provider(
634 ContextProviderCommandBuffer::Create(
635 CreateGpuProcessViewContext(gpu_channel_host, attrs, surface_id_),
636 BROWSER_COMPOSITOR_ONSCREEN_CONTEXT));
637 DCHECK(context_provider.get());
639 scoped_ptr<cc::OutputSurface> real_output_surface(
640 new OutputSurfaceWithoutParent(context_provider,
641 weak_factory_.GetWeakPtr()));
643 cc::SurfaceManager* manager = GetSurfaceManager();
644 if (manager) {
645 display_client_.reset(new cc::OnscreenDisplayClient(
646 real_output_surface.Pass(), manager, HostSharedBitmapManager::current(),
647 BrowserGpuMemoryBufferManager::current(),
648 host_->settings().renderer_settings,
649 base::MessageLoopProxy::current()));
650 scoped_ptr<cc::SurfaceDisplayOutputSurface> surface_output_surface(
651 new cc::SurfaceDisplayOutputSurface(
652 manager, surface_id_allocator_.get(), context_provider));
654 display_client_->set_surface_output_surface(surface_output_surface.get());
655 surface_output_surface->set_display_client(display_client_.get());
656 display_client_->display()->Resize(size_);
657 host_->SetOutputSurface(surface_output_surface.Pass());
658 } else {
659 host_->SetOutputSurface(real_output_surface.Pass());
663 void CompositorImpl::PopulateGpuCapabilities(
664 gpu::Capabilities gpu_capabilities) {
665 ui_resource_provider_.SetSupportsETC1NonPowerOfTwo(
666 gpu_capabilities.texture_format_etc1_npot);
669 void CompositorImpl::ScheduleComposite() {
670 DCHECK(!needs_composite_ || WillComposite());
671 if (ignore_schedule_composite_)
672 return;
674 needs_composite_ = true;
675 // We currently expect layer tree invalidations at most once per frame
676 // during normal operation and therefore try to composite immediately
677 // to minimize latency.
678 PostComposite(COMPOSITE_IMMEDIATELY);
681 void CompositorImpl::ScheduleAnimation() {
682 DCHECK(!needs_composite_ || WillComposite());
683 needs_animate_ = true;
685 if (needs_composite_)
686 return;
688 TRACE_EVENT0("cc", "CompositorImpl::ScheduleAnimation");
689 needs_composite_ = true;
690 PostComposite(COMPOSITE_EVENTUALLY);
693 void CompositorImpl::DidPostSwapBuffers() {
694 TRACE_EVENT0("compositor", "CompositorImpl::DidPostSwapBuffers");
695 did_post_swapbuffers_ = true;
698 void CompositorImpl::DidCompleteSwapBuffers() {
699 TRACE_EVENT0("compositor", "CompositorImpl::DidCompleteSwapBuffers");
700 DCHECK_GT(pending_swapbuffers_, 0U);
701 if (pending_swapbuffers_-- == kMaxSwapBuffers && needs_composite_)
702 PostComposite(COMPOSITE_IMMEDIATELY);
703 client_->OnSwapBuffersCompleted(pending_swapbuffers_);
706 void CompositorImpl::DidAbortSwapBuffers() {
707 TRACE_EVENT0("compositor", "CompositorImpl::DidAbortSwapBuffers");
708 // This really gets called only once from
709 // SingleThreadProxy::DidLoseOutputSurfaceOnImplThread() when the
710 // context was lost.
711 ScheduleComposite();
712 client_->OnSwapBuffersCompleted(0);
715 void CompositorImpl::DidCommit() {
716 root_window_->OnCompositingDidCommit();
719 void CompositorImpl::AttachLayerForReadback(scoped_refptr<cc::Layer> layer) {
720 root_layer_->AddChild(layer);
723 void CompositorImpl::RequestCopyOfOutputOnRootLayer(
724 scoped_ptr<cc::CopyOutputRequest> request) {
725 root_layer_->RequestCopyOfOutput(request.Pass());
728 void CompositorImpl::OnVSync(base::TimeTicks frame_time,
729 base::TimeDelta vsync_period) {
730 vsync_period_ = vsync_period;
731 last_vsync_ = frame_time;
733 if (WillCompositeThisFrame()) {
734 // We somehow missed the last vsync interval, so reschedule for deadline.
735 // We cannot schedule immediately, or will get us out-of-phase with new
736 // renderer frames.
737 CancelComposite();
738 composite_on_vsync_trigger_ = COMPOSITE_EVENTUALLY;
739 } else {
740 current_composite_task_.reset();
743 DCHECK(!DidCompositeThisFrame() && !WillCompositeThisFrame());
744 if (composite_on_vsync_trigger_ != DO_NOT_COMPOSITE) {
745 CompositingTrigger trigger = composite_on_vsync_trigger_;
746 composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
747 PostComposite(trigger);
751 void CompositorImpl::SetNeedsAnimate() {
752 needs_animate_ = true;
753 if (!host_)
754 return;
756 host_->SetNeedsAnimate();
759 } // namespace content