Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / browser / renderer_host / compositor_impl_android.cc
blob744a54023e3a47b497e2bc25a0708edded495b3c
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 "content/public/common/content_switches.h"
56 #include "gpu/blink/webgraphicscontext3d_in_process_command_buffer_impl.h"
57 #include "gpu/command_buffer/client/context_support.h"
58 #include "gpu/command_buffer/client/gles2_interface.h"
59 #include "third_party/khronos/GLES2/gl2.h"
60 #include "third_party/khronos/GLES2/gl2ext.h"
61 #include "third_party/skia/include/core/SkMallocPixelRef.h"
62 #include "ui/android/window_android.h"
63 #include "ui/gfx/android/device_display_info.h"
64 #include "ui/gfx/swap_result.h"
66 namespace content {
68 namespace {
70 const unsigned int kMaxSwapBuffers = 2U;
72 // Used to override capabilities_.adjust_deadline_for_parent to false
73 class OutputSurfaceWithoutParent : public cc::OutputSurface {
74 public:
75 OutputSurfaceWithoutParent(
76 const scoped_refptr<ContextProviderCommandBuffer>& context_provider,
77 const base::Callback<void(gpu::Capabilities)>&
78 populate_gpu_capabilities_callback)
79 : cc::OutputSurface(context_provider),
80 populate_gpu_capabilities_callback_(populate_gpu_capabilities_callback),
81 swap_buffers_completion_callback_(
82 base::Bind(&OutputSurfaceWithoutParent::OnSwapBuffersCompleted,
83 base::Unretained(this))) {
84 capabilities_.adjust_deadline_for_parent = false;
85 capabilities_.max_frames_pending = 2;
88 void SwapBuffers(cc::CompositorFrame* frame) override {
89 GetCommandBufferProxy()->SetLatencyInfo(frame->metadata.latency_info);
90 DCHECK(frame->gl_frame_data->sub_buffer_rect ==
91 gfx::Rect(frame->gl_frame_data->size));
92 context_provider_->ContextSupport()->Swap();
93 client_->DidSwapBuffers();
96 bool BindToClient(cc::OutputSurfaceClient* client) override {
97 if (!OutputSurface::BindToClient(client))
98 return false;
100 GetCommandBufferProxy()->SetSwapBuffersCompletionCallback(
101 swap_buffers_completion_callback_.callback());
103 populate_gpu_capabilities_callback_.Run(
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(const std::vector<ui::LatencyInfo>& latency_info,
121 gfx::SwapResult result) {
122 RenderWidgetHostImpl::CompositorFrameDrawn(latency_info);
123 OutputSurface::OnSwapBuffersComplete();
126 base::Callback<void(gpu::Capabilities)> populate_gpu_capabilities_callback_;
127 base::CancelableCallback<void(const std::vector<ui::LatencyInfo>&,
128 gfx::SwapResult)>
129 swap_buffers_completion_callback_;
132 static bool g_initialized = false;
134 bool g_use_surface_manager = false;
135 base::LazyInstance<cc::SurfaceManager> g_surface_manager =
136 LAZY_INSTANCE_INITIALIZER;
139 int g_surface_id_namespace = 0;
141 class SingleThreadTaskGraphRunner
142 : public cc::TaskGraphRunner,
143 public base::DelegateSimpleThread::Delegate {
144 public:
145 SingleThreadTaskGraphRunner()
146 : worker_thread_(
147 this,
148 "CompositorTileWorker1",
149 base::SimpleThread::Options(base::ThreadPriority::BACKGROUND)) {
150 worker_thread_.Start();
153 ~SingleThreadTaskGraphRunner() override {
154 Shutdown();
155 worker_thread_.Join();
158 private:
159 // Overridden from base::DelegateSimpleThread::Delegate:
160 void Run() override { cc::TaskGraphRunner::Run(); }
162 base::DelegateSimpleThread worker_thread_;
165 base::LazyInstance<SingleThreadTaskGraphRunner> g_task_graph_runner =
166 LAZY_INSTANCE_INITIALIZER;
168 base::LazyInstance<cc::LayerSettings> g_layer_settings =
169 LAZY_INSTANCE_INITIALIZER;
171 } // anonymous namespace
173 // static
174 Compositor* Compositor::Create(CompositorClient* client,
175 gfx::NativeWindow root_window) {
176 return client ? new CompositorImpl(client, root_window) : NULL;
179 // static
180 void Compositor::Initialize() {
181 DCHECK(!CompositorImpl::IsInitialized());
182 g_initialized = true;
183 g_use_surface_manager = UseSurfacesEnabled();
186 // static
187 const cc::LayerSettings& Compositor::LayerSettings() {
188 return g_layer_settings.Get();
191 // static
192 void Compositor::SetLayerSettings(const cc::LayerSettings& settings) {
193 g_layer_settings.Get() = settings;
196 // static
197 bool CompositorImpl::IsInitialized() {
198 return g_initialized;
201 // static
202 cc::SurfaceManager* CompositorImpl::GetSurfaceManager() {
203 if (!g_use_surface_manager)
204 return nullptr;
205 return g_surface_manager.Pointer();
208 // static
209 scoped_ptr<cc::SurfaceIdAllocator> CompositorImpl::CreateSurfaceIdAllocator() {
210 scoped_ptr<cc::SurfaceIdAllocator> allocator(
211 new cc::SurfaceIdAllocator(++g_surface_id_namespace));
212 cc::SurfaceManager* manager = GetSurfaceManager();
213 DCHECK(manager);
214 allocator->RegisterSurfaceIdNamespace(manager);
215 return allocator.Pass();
218 CompositorImpl::CompositorImpl(CompositorClient* client,
219 gfx::NativeWindow root_window)
220 : root_layer_(cc::Layer::Create(Compositor::LayerSettings())),
221 resource_manager_(&ui_resource_provider_),
222 surface_id_allocator_(GetSurfaceManager() ? CreateSurfaceIdAllocator()
223 : nullptr),
224 has_transparent_background_(false),
225 device_scale_factor_(1),
226 window_(NULL),
227 surface_id_(0),
228 client_(client),
229 root_window_(root_window),
230 did_post_swapbuffers_(false),
231 ignore_schedule_composite_(false),
232 needs_composite_(false),
233 needs_animate_(false),
234 will_composite_immediately_(false),
235 composite_on_vsync_trigger_(DO_NOT_COMPOSITE),
236 pending_swapbuffers_(0U),
237 num_successive_context_creation_failures_(0),
238 output_surface_request_pending_(false),
239 weak_factory_(this) {
240 DCHECK(client);
241 DCHECK(root_window);
242 root_window->AttachCompositor(this);
245 CompositorImpl::~CompositorImpl() {
246 root_window_->DetachCompositor();
247 // Clean-up any surface references.
248 SetSurface(NULL);
251 void CompositorImpl::PostComposite(CompositingTrigger trigger) {
252 DCHECK(needs_composite_);
253 DCHECK(trigger == COMPOSITE_IMMEDIATELY || trigger == COMPOSITE_EVENTUALLY);
255 if (will_composite_immediately_ ||
256 (trigger == COMPOSITE_EVENTUALLY && WillComposite())) {
257 // We will already composite soon enough.
258 DCHECK(WillComposite());
259 return;
262 if (DidCompositeThisFrame()) {
263 DCHECK(!WillCompositeThisFrame());
264 if (composite_on_vsync_trigger_ != COMPOSITE_IMMEDIATELY) {
265 composite_on_vsync_trigger_ = trigger;
266 root_window_->RequestVSyncUpdate();
268 DCHECK(WillComposite());
269 return;
272 base::TimeDelta delay;
273 if (trigger == COMPOSITE_IMMEDIATELY) {
274 will_composite_immediately_ = true;
275 composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
276 } else {
277 DCHECK(!WillComposite());
278 const base::TimeDelta estimated_composite_time = vsync_period_ / 4;
279 const base::TimeTicks now = base::TimeTicks::Now();
281 if (!last_vsync_.is_null() && (now - last_vsync_) < vsync_period_) {
282 base::TimeTicks next_composite =
283 last_vsync_ + vsync_period_ - estimated_composite_time;
284 if (next_composite < now) {
285 // It's too late, we will reschedule composite as needed on the next
286 // vsync.
287 composite_on_vsync_trigger_ = COMPOSITE_EVENTUALLY;
288 root_window_->RequestVSyncUpdate();
289 DCHECK(WillComposite());
290 return;
293 delay = next_composite - now;
296 TRACE_EVENT2("cc,benchmark", "CompositorImpl::PostComposite",
297 "trigger", trigger,
298 "delay", delay.InMillisecondsF());
300 DCHECK(composite_on_vsync_trigger_ == DO_NOT_COMPOSITE);
301 if (current_composite_task_)
302 current_composite_task_->Cancel();
304 // Unretained because we cancel the task on shutdown.
305 current_composite_task_.reset(new base::CancelableClosure(
306 base::Bind(&CompositorImpl::Composite, base::Unretained(this), trigger)));
307 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
308 FROM_HERE, current_composite_task_->callback(), delay);
311 void CompositorImpl::Composite(CompositingTrigger trigger) {
312 if (trigger == COMPOSITE_IMMEDIATELY)
313 will_composite_immediately_ = false;
315 DCHECK(host_);
316 DCHECK(trigger == COMPOSITE_IMMEDIATELY || trigger == COMPOSITE_EVENTUALLY);
317 DCHECK(needs_composite_);
318 DCHECK(!DidCompositeThisFrame());
320 DCHECK_LE(pending_swapbuffers_, kMaxSwapBuffers);
321 // Swap Ack accounting is unreliable if the OutputSurface was lost.
322 // In that case still attempt to composite, which will cause creation of a
323 // new OutputSurface and reset pending_swapbuffers_.
324 if (pending_swapbuffers_ == kMaxSwapBuffers &&
325 !host_->output_surface_lost()) {
326 TRACE_EVENT0("compositor", "CompositorImpl_SwapLimit");
327 return;
330 // Reset state before Layout+Composite since that might create more
331 // requests to Composite that we need to respect.
332 needs_composite_ = false;
334 // Only allow compositing once per vsync.
335 current_composite_task_->Cancel();
336 DCHECK(DidCompositeThisFrame() && !WillComposite());
338 // Ignore ScheduleComposite() from layer tree changes during layout and
339 // animation updates that will already be reflected in the current frame
340 // we are about to draw.
341 ignore_schedule_composite_ = true;
343 const base::TimeTicks frame_time = base::TimeTicks::Now();
344 if (needs_animate_) {
345 needs_animate_ = false;
346 root_window_->Animate(frame_time);
348 ignore_schedule_composite_ = false;
350 did_post_swapbuffers_ = false;
351 host_->Composite(frame_time);
352 if (did_post_swapbuffers_)
353 pending_swapbuffers_++;
355 // Need to track vsync to avoid compositing more than once per frame.
356 root_window_->RequestVSyncUpdate();
359 ui::UIResourceProvider& CompositorImpl::GetUIResourceProvider() {
360 return ui_resource_provider_;
363 ui::ResourceManager& CompositorImpl::GetResourceManager() {
364 return resource_manager_;
367 void CompositorImpl::SetRootLayer(scoped_refptr<cc::Layer> root_layer) {
368 if (subroot_layer_.get()) {
369 subroot_layer_->RemoveFromParent();
370 subroot_layer_ = NULL;
372 if (root_layer.get()) {
373 subroot_layer_ = root_layer;
374 root_layer_->AddChild(root_layer);
378 void CompositorImpl::SetWindowSurface(ANativeWindow* window) {
379 GpuSurfaceTracker* tracker = GpuSurfaceTracker::Get();
381 if (window_) {
382 tracker->RemoveSurface(surface_id_);
383 ANativeWindow_release(window_);
384 window_ = NULL;
385 surface_id_ = 0;
386 SetVisible(false);
389 if (window) {
390 window_ = window;
391 ANativeWindow_acquire(window);
392 surface_id_ = tracker->AddSurfaceForNativeWidget(window);
393 tracker->SetSurfaceHandle(
394 surface_id_,
395 gfx::GLSurfaceHandle(gfx::kNullPluginWindow, gfx::NATIVE_DIRECT));
396 SetVisible(true);
400 void CompositorImpl::SetSurface(jobject surface) {
401 JNIEnv* env = base::android::AttachCurrentThread();
402 base::android::ScopedJavaLocalRef<jobject> j_surface(env, surface);
404 // First, cleanup any existing surface references.
405 if (surface_id_)
406 UnregisterViewSurface(surface_id_);
407 SetWindowSurface(NULL);
409 // Now, set the new surface if we have one.
410 ANativeWindow* window = NULL;
411 if (surface) {
412 // Note: This ensures that any local references used by
413 // ANativeWindow_fromSurface are released immediately. This is needed as a
414 // workaround for https://code.google.com/p/android/issues/detail?id=68174
415 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
416 window = ANativeWindow_fromSurface(env, surface);
418 if (window) {
419 SetWindowSurface(window);
420 ANativeWindow_release(window);
421 RegisterViewSurface(surface_id_, j_surface.obj());
425 void CompositorImpl::CreateLayerTreeHost() {
426 DCHECK(!host_);
427 DCHECK(!WillCompositeThisFrame());
428 needs_composite_ = false;
429 pending_swapbuffers_ = 0;
430 cc::LayerTreeSettings settings;
431 settings.renderer_settings.refresh_rate = 60.0;
432 settings.renderer_settings.allow_antialiasing = false;
433 settings.renderer_settings.highp_threshold_min = 2048;
434 settings.use_zero_copy = true;
435 settings.use_one_copy = false;
436 settings.use_display_lists = true;
438 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
439 settings.initial_debug_state.SetRecordRenderingStats(
440 command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking));
441 settings.initial_debug_state.show_fps_counter =
442 command_line->HasSwitch(cc::switches::kUIShowFPSCounter);
443 // TODO(enne): Update this this compositor to use the scheduler.
444 settings.single_thread_proxy_scheduler = false;
446 if (command_line->HasSwitch(
447 switches::kEnableAndroidCompositorAnimationTimelines))
448 settings.use_compositor_animation_timelines = true;
450 cc::LayerTreeHost::InitParams params;
451 params.client = this;
452 params.shared_bitmap_manager = HostSharedBitmapManager::current();
453 params.gpu_memory_buffer_manager = BrowserGpuMemoryBufferManager::current();
454 params.task_graph_runner = g_task_graph_runner.Pointer();
455 params.main_task_runner = base::ThreadTaskRunnerHandle::Get();
456 params.settings = &settings;
457 host_ = cc::LayerTreeHost::CreateSingleThreaded(this, &params);
458 host_->SetRootLayer(root_layer_);
460 host_->SetVisible(true);
461 host_->SetLayerTreeHostClientReady();
462 host_->SetViewportSize(size_);
463 host_->set_has_transparent_background(has_transparent_background_);
464 host_->SetDeviceScaleFactor(device_scale_factor_);
466 if (needs_animate_)
467 host_->SetNeedsAnimate();
470 void CompositorImpl::SetVisible(bool visible) {
471 TRACE_EVENT1("cc", "CompositorImpl::SetVisible", "visible", visible);
472 if (!visible) {
473 DCHECK(host_);
474 // Look for any layers that were attached to the root for readback
475 // and are waiting for Composite() to happen.
476 bool readback_pending = false;
477 for (size_t i = 0; i < root_layer_->children().size(); ++i) {
478 if (root_layer_->children()[i]->HasCopyRequest()) {
479 readback_pending = true;
480 break;
483 if (readback_pending) {
484 ignore_schedule_composite_ = true;
485 host_->Composite(base::TimeTicks::Now());
486 ignore_schedule_composite_ = false;
488 if (WillComposite())
489 CancelComposite();
490 ui_resource_provider_.SetLayerTreeHost(NULL);
491 host_.reset();
492 establish_gpu_channel_timeout_.Stop();
493 output_surface_request_pending_ = false;
494 display_client_.reset();
495 if (current_composite_task_) {
496 current_composite_task_->Cancel();
497 current_composite_task_.reset();
499 } else if (!host_) {
500 CreateLayerTreeHost();
501 ui_resource_provider_.SetLayerTreeHost(host_.get());
505 void CompositorImpl::setDeviceScaleFactor(float factor) {
506 device_scale_factor_ = factor;
507 if (host_)
508 host_->SetDeviceScaleFactor(factor);
511 void CompositorImpl::SetWindowBounds(const gfx::Size& size) {
512 if (size_ == size)
513 return;
515 size_ = size;
516 if (host_)
517 host_->SetViewportSize(size);
518 if (display_client_)
519 display_client_->display()->Resize(size);
520 root_layer_->SetBounds(size);
523 void CompositorImpl::SetHasTransparentBackground(bool flag) {
524 has_transparent_background_ = flag;
525 if (host_)
526 host_->set_has_transparent_background(flag);
529 void CompositorImpl::SetNeedsComposite() {
530 if (!host_.get())
531 return;
532 DCHECK(!needs_composite_ || WillComposite());
534 needs_composite_ = true;
535 PostComposite(COMPOSITE_IMMEDIATELY);
538 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
539 CreateGpuProcessViewContext(
540 const scoped_refptr<GpuChannelHost>& gpu_channel_host,
541 const blink::WebGraphicsContext3D::Attributes attributes,
542 int surface_id) {
543 GURL url("chrome://gpu/Compositor::createContext3D");
544 static const size_t kBytesPerPixel = 4;
545 gfx::DeviceDisplayInfo display_info;
546 size_t full_screen_texture_size_in_bytes =
547 display_info.GetDisplayHeight() *
548 display_info.GetDisplayWidth() *
549 kBytesPerPixel;
550 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits limits;
551 limits.command_buffer_size = 64 * 1024;
552 limits.start_transfer_buffer_size = 64 * 1024;
553 limits.min_transfer_buffer_size = 64 * 1024;
554 limits.max_transfer_buffer_size = std::min(
555 3 * full_screen_texture_size_in_bytes, kDefaultMaxTransferBufferSize);
556 limits.mapped_memory_reclaim_limit = 2 * 1024 * 1024;
557 bool lose_context_when_out_of_memory = true;
558 return make_scoped_ptr(
559 new WebGraphicsContext3DCommandBufferImpl(surface_id,
560 url,
561 gpu_channel_host.get(),
562 attributes,
563 lose_context_when_out_of_memory,
564 limits,
565 NULL));
568 void CompositorImpl::Layout() {
569 ignore_schedule_composite_ = true;
570 client_->Layout();
571 ignore_schedule_composite_ = false;
574 void CompositorImpl::OnGpuChannelEstablished() {
575 establish_gpu_channel_timeout_.Stop();
576 CreateOutputSurface();
579 void CompositorImpl::OnGpuChannelTimeout() {
580 LOG(FATAL) << "Timed out waiting for GPU channel.";
583 void CompositorImpl::RequestNewOutputSurface() {
584 output_surface_request_pending_ = true;
586 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || defined(SYZYASAN)
587 const int64 kGpuChannelTimeoutInSeconds = 30;
588 #else
589 const int64 kGpuChannelTimeoutInSeconds = 10;
590 #endif
592 BrowserGpuChannelHostFactory* factory =
593 BrowserGpuChannelHostFactory::instance();
594 if (!factory->GetGpuChannel() || factory->GetGpuChannel()->IsLost()) {
595 factory->EstablishGpuChannel(
596 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE,
597 base::Bind(&CompositorImpl::OnGpuChannelEstablished,
598 weak_factory_.GetWeakPtr()));
599 establish_gpu_channel_timeout_.Start(
600 FROM_HERE, base::TimeDelta::FromSeconds(kGpuChannelTimeoutInSeconds),
601 this, &CompositorImpl::OnGpuChannelTimeout);
602 return;
605 CreateOutputSurface();
608 void CompositorImpl::DidInitializeOutputSurface() {
609 num_successive_context_creation_failures_ = 0;
610 output_surface_request_pending_ = false;
613 void CompositorImpl::DidFailToInitializeOutputSurface() {
614 LOG(ERROR) << "Failed to init OutputSurface for compositor.";
615 LOG_IF(FATAL, ++num_successive_context_creation_failures_ >= 2)
616 << "Too many context creation failures. Giving up... ";
617 RequestNewOutputSurface();
620 void CompositorImpl::CreateOutputSurface() {
621 // We might have had a request from a LayerTreeHost that was then
622 // deleted.
623 if (!output_surface_request_pending_)
624 return;
626 blink::WebGraphicsContext3D::Attributes attrs;
627 attrs.shareResources = true;
628 attrs.noAutomaticFlushes = true;
629 pending_swapbuffers_ = 0;
631 DCHECK(window_);
632 DCHECK(surface_id_);
634 BrowserGpuChannelHostFactory* factory =
635 BrowserGpuChannelHostFactory::instance();
636 // This channel might be lost (and even if it isn't right now, it might
637 // still get marked as lost from the IO thread, at any point in time really).
638 // But from here on just try and always lead to either
639 // DidInitializeOutputSurface() or DidFailToInitializeOutputSurface().
640 scoped_refptr<GpuChannelHost> gpu_channel_host(factory->GetGpuChannel());
641 scoped_refptr<ContextProviderCommandBuffer> context_provider(
642 ContextProviderCommandBuffer::Create(
643 CreateGpuProcessViewContext(gpu_channel_host, attrs, surface_id_),
644 BROWSER_COMPOSITOR_ONSCREEN_CONTEXT));
645 DCHECK(context_provider.get());
647 scoped_ptr<cc::OutputSurface> real_output_surface(
648 new OutputSurfaceWithoutParent(
649 context_provider, base::Bind(&CompositorImpl::PopulateGpuCapabilities,
650 base::Unretained(this))));
652 cc::SurfaceManager* manager = GetSurfaceManager();
653 if (manager) {
654 display_client_.reset(new cc::OnscreenDisplayClient(
655 real_output_surface.Pass(), manager, HostSharedBitmapManager::current(),
656 BrowserGpuMemoryBufferManager::current(),
657 host_->settings().renderer_settings,
658 base::ThreadTaskRunnerHandle::Get()));
659 scoped_ptr<cc::SurfaceDisplayOutputSurface> surface_output_surface(
660 new cc::SurfaceDisplayOutputSurface(
661 manager, surface_id_allocator_.get(), context_provider));
663 display_client_->set_surface_output_surface(surface_output_surface.get());
664 surface_output_surface->set_display_client(display_client_.get());
665 display_client_->display()->Resize(size_);
666 host_->SetOutputSurface(surface_output_surface.Pass());
667 } else {
668 host_->SetOutputSurface(real_output_surface.Pass());
672 void CompositorImpl::PopulateGpuCapabilities(
673 gpu::Capabilities gpu_capabilities) {
674 ui_resource_provider_.SetSupportsETC1NonPowerOfTwo(
675 gpu_capabilities.texture_format_etc1_npot);
678 void CompositorImpl::ScheduleComposite() {
679 DCHECK(!needs_composite_ || WillComposite());
680 if (ignore_schedule_composite_)
681 return;
683 needs_composite_ = true;
684 // We currently expect layer tree invalidations at most once per frame
685 // during normal operation and therefore try to composite immediately
686 // to minimize latency.
687 PostComposite(COMPOSITE_IMMEDIATELY);
690 void CompositorImpl::ScheduleAnimation() {
691 DCHECK(!needs_composite_ || WillComposite());
692 needs_animate_ = true;
694 if (needs_composite_)
695 return;
697 TRACE_EVENT0("cc", "CompositorImpl::ScheduleAnimation");
698 needs_composite_ = true;
699 PostComposite(COMPOSITE_EVENTUALLY);
702 void CompositorImpl::DidPostSwapBuffers() {
703 TRACE_EVENT0("compositor", "CompositorImpl::DidPostSwapBuffers");
704 did_post_swapbuffers_ = true;
707 void CompositorImpl::DidCompleteSwapBuffers() {
708 TRACE_EVENT0("compositor", "CompositorImpl::DidCompleteSwapBuffers");
709 DCHECK_GT(pending_swapbuffers_, 0U);
710 if (pending_swapbuffers_-- == kMaxSwapBuffers && needs_composite_)
711 PostComposite(COMPOSITE_IMMEDIATELY);
712 client_->OnSwapBuffersCompleted(pending_swapbuffers_);
715 void CompositorImpl::DidAbortSwapBuffers() {
716 TRACE_EVENT0("compositor", "CompositorImpl::DidAbortSwapBuffers");
717 // This really gets called only once from
718 // SingleThreadProxy::DidLoseOutputSurfaceOnImplThread() when the
719 // context was lost.
720 ScheduleComposite();
721 client_->OnSwapBuffersCompleted(0);
724 void CompositorImpl::DidCommit() {
725 root_window_->OnCompositingDidCommit();
728 void CompositorImpl::AttachLayerForReadback(scoped_refptr<cc::Layer> layer) {
729 root_layer_->AddChild(layer);
732 void CompositorImpl::RequestCopyOfOutputOnRootLayer(
733 scoped_ptr<cc::CopyOutputRequest> request) {
734 root_layer_->RequestCopyOfOutput(request.Pass());
737 void CompositorImpl::OnVSync(base::TimeTicks frame_time,
738 base::TimeDelta vsync_period) {
739 vsync_period_ = vsync_period;
740 last_vsync_ = frame_time;
742 if (WillCompositeThisFrame()) {
743 // We somehow missed the last vsync interval, so reschedule for deadline.
744 // We cannot schedule immediately, or will get us out-of-phase with new
745 // renderer frames.
746 CancelComposite();
747 composite_on_vsync_trigger_ = COMPOSITE_EVENTUALLY;
748 } else {
749 current_composite_task_.reset();
752 DCHECK(!DidCompositeThisFrame() && !WillCompositeThisFrame());
753 if (composite_on_vsync_trigger_ != DO_NOT_COMPOSITE) {
754 CompositingTrigger trigger = composite_on_vsync_trigger_;
755 composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
756 PostComposite(trigger);
760 void CompositorImpl::SetNeedsAnimate() {
761 needs_animate_ = true;
762 if (!host_)
763 return;
765 host_->SetNeedsAnimate();
768 } // namespace content