Android: Fix null onscreen surface
[chromium-blink-merge.git] / content / browser / renderer_host / compositor_impl_android.cc
blob358ac519981ee5bc746eae1e660a48a9977d032a
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 CompositorImpl::VSyncObserver {
75 public:
76 OutputSurfaceWithoutParent(
77 CompositorImpl* compositor,
78 const scoped_refptr<ContextProviderCommandBuffer>& context_provider,
79 const base::Callback<void(gpu::Capabilities)>&
80 populate_gpu_capabilities_callback)
81 : cc::OutputSurface(context_provider),
82 compositor_(compositor),
83 populate_gpu_capabilities_callback_(populate_gpu_capabilities_callback),
84 swap_buffers_completion_callback_(
85 base::Bind(&OutputSurfaceWithoutParent::OnSwapBuffersCompleted,
86 base::Unretained(this))) {
87 capabilities_.adjust_deadline_for_parent = false;
88 capabilities_.max_frames_pending = 2;
91 ~OutputSurfaceWithoutParent() override { compositor_->RemoveObserver(this); }
93 void SwapBuffers(cc::CompositorFrame* frame) override {
94 GetCommandBufferProxy()->SetLatencyInfo(frame->metadata.latency_info);
95 DCHECK(frame->gl_frame_data->sub_buffer_rect ==
96 gfx::Rect(frame->gl_frame_data->size));
97 context_provider_->ContextSupport()->Swap();
98 client_->DidSwapBuffers();
101 bool BindToClient(cc::OutputSurfaceClient* client) override {
102 if (!OutputSurface::BindToClient(client))
103 return false;
105 GetCommandBufferProxy()->SetSwapBuffersCompletionCallback(
106 swap_buffers_completion_callback_.callback());
108 populate_gpu_capabilities_callback_.Run(
109 context_provider_->ContextCapabilities().gpu);
110 compositor_->AddObserver(this);
112 return true;
115 private:
116 CommandBufferProxyImpl* GetCommandBufferProxy() {
117 ContextProviderCommandBuffer* provider_command_buffer =
118 static_cast<content::ContextProviderCommandBuffer*>(
119 context_provider_.get());
120 CommandBufferProxyImpl* command_buffer_proxy =
121 provider_command_buffer->GetCommandBufferProxy();
122 DCHECK(command_buffer_proxy);
123 return command_buffer_proxy;
126 void OnSwapBuffersCompleted(const std::vector<ui::LatencyInfo>& latency_info,
127 gfx::SwapResult result) {
128 RenderWidgetHostImpl::CompositorFrameDrawn(latency_info);
129 OutputSurface::OnSwapBuffersComplete();
132 void OnUpdateVSyncParameters(base::TimeTicks timebase,
133 base::TimeDelta interval) override {
134 CommitVSyncParameters(timebase, interval);
137 CompositorImpl* compositor_;
138 base::Callback<void(gpu::Capabilities)> populate_gpu_capabilities_callback_;
139 base::CancelableCallback<void(const std::vector<ui::LatencyInfo>&,
140 gfx::SwapResult)>
141 swap_buffers_completion_callback_;
144 static bool g_initialized = false;
146 bool g_use_surface_manager = false;
147 base::LazyInstance<cc::SurfaceManager> g_surface_manager =
148 LAZY_INSTANCE_INITIALIZER;
151 int g_surface_id_namespace = 0;
153 class SingleThreadTaskGraphRunner
154 : public cc::TaskGraphRunner,
155 public base::DelegateSimpleThread::Delegate {
156 public:
157 SingleThreadTaskGraphRunner()
158 : worker_thread_(
159 this,
160 "CompositorTileWorker1",
161 base::SimpleThread::Options(base::ThreadPriority::BACKGROUND)) {
162 worker_thread_.Start();
165 ~SingleThreadTaskGraphRunner() override {
166 Shutdown();
167 worker_thread_.Join();
170 private:
171 // Overridden from base::DelegateSimpleThread::Delegate:
172 void Run() override { cc::TaskGraphRunner::Run(); }
174 base::DelegateSimpleThread worker_thread_;
177 base::LazyInstance<SingleThreadTaskGraphRunner> g_task_graph_runner =
178 LAZY_INSTANCE_INITIALIZER;
180 base::LazyInstance<cc::LayerSettings> g_layer_settings =
181 LAZY_INSTANCE_INITIALIZER;
183 } // anonymous namespace
185 // static
186 Compositor* Compositor::Create(CompositorClient* client,
187 gfx::NativeWindow root_window) {
188 return client ? new CompositorImpl(client, root_window) : NULL;
191 // static
192 void Compositor::Initialize() {
193 DCHECK(!CompositorImpl::IsInitialized());
194 g_initialized = true;
195 g_use_surface_manager = UseSurfacesEnabled();
198 // static
199 const cc::LayerSettings& Compositor::LayerSettings() {
200 return g_layer_settings.Get();
203 // static
204 void Compositor::SetLayerSettings(const cc::LayerSettings& settings) {
205 g_layer_settings.Get() = settings;
208 // static
209 bool CompositorImpl::IsInitialized() {
210 return g_initialized;
213 // static
214 cc::SurfaceManager* CompositorImpl::GetSurfaceManager() {
215 if (!g_use_surface_manager)
216 return nullptr;
217 return g_surface_manager.Pointer();
220 // static
221 scoped_ptr<cc::SurfaceIdAllocator> CompositorImpl::CreateSurfaceIdAllocator() {
222 scoped_ptr<cc::SurfaceIdAllocator> allocator(
223 new cc::SurfaceIdAllocator(++g_surface_id_namespace));
224 cc::SurfaceManager* manager = GetSurfaceManager();
225 DCHECK(manager);
226 allocator->RegisterSurfaceIdNamespace(manager);
227 return allocator.Pass();
230 CompositorImpl::CompositorImpl(CompositorClient* client,
231 gfx::NativeWindow root_window)
232 : root_layer_(cc::Layer::Create(Compositor::LayerSettings())),
233 resource_manager_(&ui_resource_provider_),
234 surface_id_allocator_(GetSurfaceManager() ? CreateSurfaceIdAllocator()
235 : nullptr),
236 has_transparent_background_(false),
237 device_scale_factor_(1),
238 window_(NULL),
239 surface_id_(0),
240 client_(client),
241 root_window_(root_window),
242 did_post_swapbuffers_(false),
243 ignore_schedule_composite_(false),
244 needs_composite_(false),
245 needs_animate_(false),
246 will_composite_immediately_(false),
247 composite_on_vsync_trigger_(DO_NOT_COMPOSITE),
248 pending_swapbuffers_(0U),
249 num_successive_context_creation_failures_(0),
250 output_surface_request_pending_(false),
251 weak_factory_(this) {
252 DCHECK(client);
253 DCHECK(root_window);
254 root_window->AttachCompositor(this);
257 CompositorImpl::~CompositorImpl() {
258 root_window_->DetachCompositor();
259 // Clean-up any surface references.
260 SetSurface(NULL);
263 void CompositorImpl::PostComposite(CompositingTrigger trigger) {
264 DCHECK(needs_composite_);
265 DCHECK(trigger == COMPOSITE_IMMEDIATELY || trigger == COMPOSITE_EVENTUALLY);
267 if (will_composite_immediately_ ||
268 (trigger == COMPOSITE_EVENTUALLY && WillComposite())) {
269 // We will already composite soon enough.
270 DCHECK(WillComposite());
271 return;
274 if (DidCompositeThisFrame()) {
275 DCHECK(!WillCompositeThisFrame());
276 if (composite_on_vsync_trigger_ != COMPOSITE_IMMEDIATELY) {
277 composite_on_vsync_trigger_ = trigger;
278 root_window_->RequestVSyncUpdate();
280 DCHECK(WillComposite());
281 return;
284 base::TimeDelta delay;
285 if (trigger == COMPOSITE_IMMEDIATELY) {
286 will_composite_immediately_ = true;
287 composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
288 } else {
289 DCHECK(!WillComposite());
290 const base::TimeDelta estimated_composite_time = vsync_period_ / 4;
291 const base::TimeTicks now = base::TimeTicks::Now();
293 if (!last_vsync_.is_null() && (now - last_vsync_) < vsync_period_) {
294 base::TimeTicks next_composite =
295 last_vsync_ + vsync_period_ - estimated_composite_time;
296 if (next_composite < now) {
297 // It's too late, we will reschedule composite as needed on the next
298 // vsync.
299 composite_on_vsync_trigger_ = COMPOSITE_EVENTUALLY;
300 root_window_->RequestVSyncUpdate();
301 DCHECK(WillComposite());
302 return;
305 delay = next_composite - now;
308 TRACE_EVENT2("cc,benchmark", "CompositorImpl::PostComposite",
309 "trigger", trigger,
310 "delay", delay.InMillisecondsF());
312 DCHECK(composite_on_vsync_trigger_ == DO_NOT_COMPOSITE);
313 if (current_composite_task_)
314 current_composite_task_->Cancel();
316 // Unretained because we cancel the task on shutdown.
317 current_composite_task_.reset(new base::CancelableClosure(
318 base::Bind(&CompositorImpl::Composite, base::Unretained(this), trigger)));
319 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
320 FROM_HERE, current_composite_task_->callback(), delay);
323 void CompositorImpl::Composite(CompositingTrigger trigger) {
324 if (trigger == COMPOSITE_IMMEDIATELY)
325 will_composite_immediately_ = false;
327 DCHECK(host_);
328 DCHECK(trigger == COMPOSITE_IMMEDIATELY || trigger == COMPOSITE_EVENTUALLY);
329 DCHECK(needs_composite_);
330 DCHECK(!DidCompositeThisFrame());
332 DCHECK_LE(pending_swapbuffers_, kMaxSwapBuffers);
333 // Swap Ack accounting is unreliable if the OutputSurface was lost.
334 // In that case still attempt to composite, which will cause creation of a
335 // new OutputSurface and reset pending_swapbuffers_.
336 if (pending_swapbuffers_ == kMaxSwapBuffers &&
337 !host_->output_surface_lost()) {
338 TRACE_EVENT0("compositor", "CompositorImpl_SwapLimit");
339 return;
342 // Reset state before Layout+Composite since that might create more
343 // requests to Composite that we need to respect.
344 needs_composite_ = false;
346 // Only allow compositing once per vsync.
347 current_composite_task_->Cancel();
348 DCHECK(DidCompositeThisFrame() && !WillComposite());
350 // Ignore ScheduleComposite() from layer tree changes during layout and
351 // animation updates that will already be reflected in the current frame
352 // we are about to draw.
353 ignore_schedule_composite_ = true;
355 const base::TimeTicks frame_time = base::TimeTicks::Now();
356 if (needs_animate_) {
357 needs_animate_ = false;
358 root_window_->Animate(frame_time);
360 ignore_schedule_composite_ = false;
362 did_post_swapbuffers_ = false;
363 host_->Composite(frame_time);
364 if (did_post_swapbuffers_)
365 pending_swapbuffers_++;
367 // Need to track vsync to avoid compositing more than once per frame.
368 root_window_->RequestVSyncUpdate();
371 ui::UIResourceProvider& CompositorImpl::GetUIResourceProvider() {
372 return ui_resource_provider_;
375 ui::ResourceManager& CompositorImpl::GetResourceManager() {
376 return resource_manager_;
379 void CompositorImpl::SetRootLayer(scoped_refptr<cc::Layer> root_layer) {
380 if (subroot_layer_.get()) {
381 subroot_layer_->RemoveFromParent();
382 subroot_layer_ = NULL;
384 if (root_layer.get()) {
385 subroot_layer_ = root_layer;
386 root_layer_->AddChild(root_layer);
390 void CompositorImpl::SetWindowSurface(ANativeWindow* window) {
391 GpuSurfaceTracker* tracker = GpuSurfaceTracker::Get();
393 if (window_) {
394 // Shut down GL context before unregistering surface.
395 SetVisible(false);
396 tracker->RemoveSurface(surface_id_);
397 ANativeWindow_release(window_);
398 window_ = NULL;
399 surface_id_ = 0;
402 if (window) {
403 window_ = window;
404 ANativeWindow_acquire(window);
405 surface_id_ = tracker->AddSurfaceForNativeWidget(window);
406 tracker->SetSurfaceHandle(
407 surface_id_,
408 gfx::GLSurfaceHandle(gfx::kNullPluginWindow, gfx::NATIVE_DIRECT));
409 SetVisible(true);
413 void CompositorImpl::SetSurface(jobject surface) {
414 JNIEnv* env = base::android::AttachCurrentThread();
415 base::android::ScopedJavaLocalRef<jobject> j_surface(env, surface);
417 // First, shut down the GL context.
418 int surface_id = surface_id_;
419 SetWindowSurface(NULL);
420 // Then, cleanup any existing surface references.
421 if (surface_id)
422 UnregisterViewSurface(surface_id);
424 // Now, set the new surface if we have one.
425 ANativeWindow* window = NULL;
426 if (surface) {
427 // Note: This ensures that any local references used by
428 // ANativeWindow_fromSurface are released immediately. This is needed as a
429 // workaround for https://code.google.com/p/android/issues/detail?id=68174
430 base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
431 window = ANativeWindow_fromSurface(env, surface);
433 if (window) {
434 SetWindowSurface(window);
435 ANativeWindow_release(window);
436 RegisterViewSurface(surface_id_, j_surface.obj());
440 void CompositorImpl::CreateLayerTreeHost() {
441 DCHECK(!host_);
442 DCHECK(!WillCompositeThisFrame());
443 needs_composite_ = false;
444 pending_swapbuffers_ = 0;
445 cc::LayerTreeSettings settings;
446 settings.renderer_settings.refresh_rate = 60.0;
447 settings.renderer_settings.allow_antialiasing = false;
448 settings.renderer_settings.highp_threshold_min = 2048;
449 settings.use_zero_copy = true;
450 settings.use_one_copy = false;
451 settings.use_display_lists = true;
453 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
454 settings.initial_debug_state.SetRecordRenderingStats(
455 command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking));
456 settings.initial_debug_state.show_fps_counter =
457 command_line->HasSwitch(cc::switches::kUIShowFPSCounter);
458 // TODO(enne): Update this this compositor to use the scheduler.
459 settings.single_thread_proxy_scheduler = false;
461 if (command_line->HasSwitch(
462 switches::kEnableAndroidCompositorAnimationTimelines))
463 settings.use_compositor_animation_timelines = true;
465 cc::LayerTreeHost::InitParams params;
466 params.client = this;
467 params.shared_bitmap_manager = HostSharedBitmapManager::current();
468 params.gpu_memory_buffer_manager = BrowserGpuMemoryBufferManager::current();
469 params.task_graph_runner = g_task_graph_runner.Pointer();
470 params.main_task_runner = base::ThreadTaskRunnerHandle::Get();
471 params.settings = &settings;
472 host_ = cc::LayerTreeHost::CreateSingleThreaded(this, &params);
473 host_->SetRootLayer(root_layer_);
475 host_->SetVisible(true);
476 host_->SetLayerTreeHostClientReady();
477 host_->SetViewportSize(size_);
478 host_->set_has_transparent_background(has_transparent_background_);
479 host_->SetDeviceScaleFactor(device_scale_factor_);
481 if (needs_animate_)
482 host_->SetNeedsAnimate();
485 void CompositorImpl::SetVisible(bool visible) {
486 TRACE_EVENT1("cc", "CompositorImpl::SetVisible", "visible", visible);
487 if (!visible) {
488 DCHECK(host_);
489 // Look for any layers that were attached to the root for readback
490 // and are waiting for Composite() to happen.
491 bool readback_pending = false;
492 for (size_t i = 0; i < root_layer_->children().size(); ++i) {
493 if (root_layer_->children()[i]->HasCopyRequest()) {
494 readback_pending = true;
495 break;
498 if (readback_pending) {
499 ignore_schedule_composite_ = true;
500 host_->Composite(base::TimeTicks::Now());
501 ignore_schedule_composite_ = false;
503 if (WillComposite())
504 CancelComposite();
505 ui_resource_provider_.SetLayerTreeHost(NULL);
506 host_.reset();
507 establish_gpu_channel_timeout_.Stop();
508 output_surface_request_pending_ = false;
509 display_client_.reset();
510 if (current_composite_task_) {
511 current_composite_task_->Cancel();
512 current_composite_task_.reset();
514 } else if (!host_) {
515 CreateLayerTreeHost();
516 ui_resource_provider_.SetLayerTreeHost(host_.get());
520 void CompositorImpl::setDeviceScaleFactor(float factor) {
521 device_scale_factor_ = factor;
522 if (host_)
523 host_->SetDeviceScaleFactor(factor);
526 void CompositorImpl::SetWindowBounds(const gfx::Size& size) {
527 if (size_ == size)
528 return;
530 size_ = size;
531 if (host_)
532 host_->SetViewportSize(size);
533 if (display_client_)
534 display_client_->display()->Resize(size);
535 root_layer_->SetBounds(size);
538 void CompositorImpl::SetHasTransparentBackground(bool flag) {
539 has_transparent_background_ = flag;
540 if (host_)
541 host_->set_has_transparent_background(flag);
544 void CompositorImpl::SetNeedsComposite() {
545 if (!host_.get())
546 return;
547 DCHECK(!needs_composite_ || WillComposite());
549 needs_composite_ = true;
550 PostComposite(COMPOSITE_IMMEDIATELY);
553 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
554 CreateGpuProcessViewContext(
555 const scoped_refptr<GpuChannelHost>& gpu_channel_host,
556 const blink::WebGraphicsContext3D::Attributes attributes,
557 int surface_id) {
558 GURL url("chrome://gpu/Compositor::createContext3D");
559 static const size_t kBytesPerPixel = 4;
560 gfx::DeviceDisplayInfo display_info;
561 size_t full_screen_texture_size_in_bytes =
562 display_info.GetDisplayHeight() *
563 display_info.GetDisplayWidth() *
564 kBytesPerPixel;
565 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits limits;
566 limits.command_buffer_size = 64 * 1024;
567 limits.start_transfer_buffer_size = 64 * 1024;
568 limits.min_transfer_buffer_size = 64 * 1024;
569 limits.max_transfer_buffer_size = std::min(
570 3 * full_screen_texture_size_in_bytes, kDefaultMaxTransferBufferSize);
571 limits.mapped_memory_reclaim_limit = 2 * 1024 * 1024;
572 bool lose_context_when_out_of_memory = true;
573 return make_scoped_ptr(
574 new WebGraphicsContext3DCommandBufferImpl(surface_id,
575 url,
576 gpu_channel_host.get(),
577 attributes,
578 lose_context_when_out_of_memory,
579 limits,
580 NULL));
583 void CompositorImpl::Layout() {
584 ignore_schedule_composite_ = true;
585 client_->Layout();
586 ignore_schedule_composite_ = false;
589 void CompositorImpl::OnGpuChannelEstablished() {
590 establish_gpu_channel_timeout_.Stop();
591 CreateOutputSurface();
594 void CompositorImpl::OnGpuChannelTimeout() {
595 LOG(FATAL) << "Timed out waiting for GPU channel.";
598 void CompositorImpl::RequestNewOutputSurface() {
599 output_surface_request_pending_ = true;
601 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || defined(SYZYASAN)
602 const int64 kGpuChannelTimeoutInSeconds = 30;
603 #else
604 const int64 kGpuChannelTimeoutInSeconds = 10;
605 #endif
607 BrowserGpuChannelHostFactory* factory =
608 BrowserGpuChannelHostFactory::instance();
609 if (!factory->GetGpuChannel() || factory->GetGpuChannel()->IsLost()) {
610 factory->EstablishGpuChannel(
611 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE,
612 base::Bind(&CompositorImpl::OnGpuChannelEstablished,
613 weak_factory_.GetWeakPtr()));
614 establish_gpu_channel_timeout_.Start(
615 FROM_HERE, base::TimeDelta::FromSeconds(kGpuChannelTimeoutInSeconds),
616 this, &CompositorImpl::OnGpuChannelTimeout);
617 return;
620 CreateOutputSurface();
623 void CompositorImpl::DidInitializeOutputSurface() {
624 num_successive_context_creation_failures_ = 0;
625 output_surface_request_pending_ = false;
628 void CompositorImpl::DidFailToInitializeOutputSurface() {
629 LOG(ERROR) << "Failed to init OutputSurface for compositor.";
630 LOG_IF(FATAL, ++num_successive_context_creation_failures_ >= 2)
631 << "Too many context creation failures. Giving up... ";
632 RequestNewOutputSurface();
635 void CompositorImpl::CreateOutputSurface() {
636 // We might have had a request from a LayerTreeHost that was then
637 // deleted.
638 if (!output_surface_request_pending_)
639 return;
641 blink::WebGraphicsContext3D::Attributes attrs;
642 attrs.shareResources = true;
643 attrs.noAutomaticFlushes = true;
644 pending_swapbuffers_ = 0;
646 DCHECK(window_);
647 DCHECK(surface_id_);
649 BrowserGpuChannelHostFactory* factory =
650 BrowserGpuChannelHostFactory::instance();
651 // This channel might be lost (and even if it isn't right now, it might
652 // still get marked as lost from the IO thread, at any point in time really).
653 // But from here on just try and always lead to either
654 // DidInitializeOutputSurface() or DidFailToInitializeOutputSurface().
655 scoped_refptr<GpuChannelHost> gpu_channel_host(factory->GetGpuChannel());
656 scoped_refptr<ContextProviderCommandBuffer> context_provider(
657 ContextProviderCommandBuffer::Create(
658 CreateGpuProcessViewContext(gpu_channel_host, attrs, surface_id_),
659 BROWSER_COMPOSITOR_ONSCREEN_CONTEXT));
660 DCHECK(context_provider.get());
662 scoped_ptr<cc::OutputSurface> real_output_surface(
663 new OutputSurfaceWithoutParent(
664 this, context_provider,
665 base::Bind(&CompositorImpl::PopulateGpuCapabilities,
666 base::Unretained(this))));
668 cc::SurfaceManager* manager = GetSurfaceManager();
669 if (manager) {
670 display_client_.reset(new cc::OnscreenDisplayClient(
671 real_output_surface.Pass(), manager, HostSharedBitmapManager::current(),
672 BrowserGpuMemoryBufferManager::current(),
673 host_->settings().renderer_settings,
674 base::ThreadTaskRunnerHandle::Get()));
675 scoped_ptr<cc::SurfaceDisplayOutputSurface> surface_output_surface(
676 new cc::SurfaceDisplayOutputSurface(
677 manager, surface_id_allocator_.get(), context_provider));
679 display_client_->set_surface_output_surface(surface_output_surface.get());
680 surface_output_surface->set_display_client(display_client_.get());
681 display_client_->display()->Resize(size_);
682 host_->SetOutputSurface(surface_output_surface.Pass());
683 } else {
684 host_->SetOutputSurface(real_output_surface.Pass());
688 void CompositorImpl::PopulateGpuCapabilities(
689 gpu::Capabilities gpu_capabilities) {
690 ui_resource_provider_.SetSupportsETC1NonPowerOfTwo(
691 gpu_capabilities.texture_format_etc1_npot);
694 void CompositorImpl::AddObserver(VSyncObserver* observer) {
695 observer_list_.AddObserver(observer);
698 void CompositorImpl::RemoveObserver(VSyncObserver* observer) {
699 observer_list_.RemoveObserver(observer);
702 void CompositorImpl::ScheduleComposite() {
703 DCHECK(!needs_composite_ || WillComposite());
704 if (ignore_schedule_composite_)
705 return;
707 needs_composite_ = true;
708 // We currently expect layer tree invalidations at most once per frame
709 // during normal operation and therefore try to composite immediately
710 // to minimize latency.
711 PostComposite(COMPOSITE_IMMEDIATELY);
714 void CompositorImpl::ScheduleAnimation() {
715 DCHECK(!needs_composite_ || WillComposite());
716 needs_animate_ = true;
718 if (needs_composite_)
719 return;
721 TRACE_EVENT0("cc", "CompositorImpl::ScheduleAnimation");
722 needs_composite_ = true;
723 PostComposite(COMPOSITE_EVENTUALLY);
726 void CompositorImpl::DidPostSwapBuffers() {
727 TRACE_EVENT0("compositor", "CompositorImpl::DidPostSwapBuffers");
728 did_post_swapbuffers_ = true;
731 void CompositorImpl::DidCompleteSwapBuffers() {
732 TRACE_EVENT0("compositor", "CompositorImpl::DidCompleteSwapBuffers");
733 DCHECK_GT(pending_swapbuffers_, 0U);
734 if (pending_swapbuffers_-- == kMaxSwapBuffers && needs_composite_)
735 PostComposite(COMPOSITE_IMMEDIATELY);
736 client_->OnSwapBuffersCompleted(pending_swapbuffers_);
739 void CompositorImpl::DidAbortSwapBuffers() {
740 TRACE_EVENT0("compositor", "CompositorImpl::DidAbortSwapBuffers");
741 // This really gets called only once from
742 // SingleThreadProxy::DidLoseOutputSurfaceOnImplThread() when the
743 // context was lost.
744 ScheduleComposite();
745 client_->OnSwapBuffersCompleted(0);
748 void CompositorImpl::DidCommit() {
749 root_window_->OnCompositingDidCommit();
752 void CompositorImpl::AttachLayerForReadback(scoped_refptr<cc::Layer> layer) {
753 root_layer_->AddChild(layer);
756 void CompositorImpl::RequestCopyOfOutputOnRootLayer(
757 scoped_ptr<cc::CopyOutputRequest> request) {
758 root_layer_->RequestCopyOfOutput(request.Pass());
761 void CompositorImpl::OnVSync(base::TimeTicks frame_time,
762 base::TimeDelta vsync_period) {
763 vsync_period_ = vsync_period;
764 last_vsync_ = frame_time;
766 if (WillCompositeThisFrame()) {
767 // We somehow missed the last vsync interval, so reschedule for deadline.
768 // We cannot schedule immediately, or will get us out-of-phase with new
769 // renderer frames.
770 CancelComposite();
771 composite_on_vsync_trigger_ = COMPOSITE_EVENTUALLY;
772 } else {
773 current_composite_task_.reset();
776 DCHECK(!DidCompositeThisFrame() && !WillCompositeThisFrame());
777 if (composite_on_vsync_trigger_ != DO_NOT_COMPOSITE) {
778 CompositingTrigger trigger = composite_on_vsync_trigger_;
779 composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
780 PostComposite(trigger);
783 FOR_EACH_OBSERVER(VSyncObserver, observer_list_,
784 OnUpdateVSyncParameters(frame_time, vsync_period));
787 void CompositorImpl::SetNeedsAnimate() {
788 needs_animate_ = true;
789 if (!host_)
790 return;
792 host_->SetNeedsAnimate();
795 } // namespace content