sandbox/linux/bpf_dsl: eliminate implicit dependency on C++ compiler behavior
[chromium-blink-merge.git] / content / browser / renderer_host / compositor_impl_android.cc
blob146e041d81e98e3b9557912cbf47cd9d454aa260
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;
451 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
452 settings.initial_debug_state.SetRecordRenderingStats(
453 command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking));
454 settings.initial_debug_state.show_fps_counter =
455 command_line->HasSwitch(cc::switches::kUIShowFPSCounter);
456 // TODO(enne): Update this this compositor to use the scheduler.
457 settings.single_thread_proxy_scheduler = false;
459 if (command_line->HasSwitch(
460 switches::kEnableAndroidCompositorAnimationTimelines))
461 settings.use_compositor_animation_timelines = true;
463 cc::LayerTreeHost::InitParams params;
464 params.client = this;
465 params.shared_bitmap_manager = HostSharedBitmapManager::current();
466 params.gpu_memory_buffer_manager = BrowserGpuMemoryBufferManager::current();
467 params.task_graph_runner = g_task_graph_runner.Pointer();
468 params.main_task_runner = base::ThreadTaskRunnerHandle::Get();
469 params.settings = &settings;
470 host_ = cc::LayerTreeHost::CreateSingleThreaded(this, &params);
471 host_->SetRootLayer(root_layer_);
473 host_->SetVisible(true);
474 host_->SetLayerTreeHostClientReady();
475 host_->SetViewportSize(size_);
476 host_->set_has_transparent_background(has_transparent_background_);
477 host_->SetDeviceScaleFactor(device_scale_factor_);
479 if (needs_animate_)
480 host_->SetNeedsAnimate();
483 void CompositorImpl::SetVisible(bool visible) {
484 TRACE_EVENT1("cc", "CompositorImpl::SetVisible", "visible", visible);
485 if (!visible) {
486 DCHECK(host_);
487 // Look for any layers that were attached to the root for readback
488 // and are waiting for Composite() to happen.
489 bool readback_pending = false;
490 for (size_t i = 0; i < root_layer_->children().size(); ++i) {
491 if (root_layer_->children()[i]->HasCopyRequest()) {
492 readback_pending = true;
493 break;
496 if (readback_pending) {
497 ignore_schedule_composite_ = true;
498 host_->Composite(base::TimeTicks::Now());
499 ignore_schedule_composite_ = false;
501 if (WillComposite())
502 CancelComposite();
503 ui_resource_provider_.SetLayerTreeHost(NULL);
504 host_.reset();
505 establish_gpu_channel_timeout_.Stop();
506 output_surface_request_pending_ = false;
507 display_client_.reset();
508 if (current_composite_task_) {
509 current_composite_task_->Cancel();
510 current_composite_task_.reset();
512 } else if (!host_) {
513 CreateLayerTreeHost();
514 ui_resource_provider_.SetLayerTreeHost(host_.get());
518 void CompositorImpl::setDeviceScaleFactor(float factor) {
519 device_scale_factor_ = factor;
520 if (host_)
521 host_->SetDeviceScaleFactor(factor);
524 void CompositorImpl::SetWindowBounds(const gfx::Size& size) {
525 if (size_ == size)
526 return;
528 size_ = size;
529 if (host_)
530 host_->SetViewportSize(size);
531 if (display_client_)
532 display_client_->display()->Resize(size);
533 root_layer_->SetBounds(size);
536 void CompositorImpl::SetHasTransparentBackground(bool flag) {
537 has_transparent_background_ = flag;
538 if (host_)
539 host_->set_has_transparent_background(flag);
542 void CompositorImpl::SetNeedsComposite() {
543 if (!host_.get())
544 return;
545 DCHECK(!needs_composite_ || WillComposite());
547 needs_composite_ = true;
548 PostComposite(COMPOSITE_IMMEDIATELY);
551 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
552 CreateGpuProcessViewContext(
553 const scoped_refptr<GpuChannelHost>& gpu_channel_host,
554 const blink::WebGraphicsContext3D::Attributes attributes,
555 int surface_id) {
556 GURL url("chrome://gpu/Compositor::createContext3D");
557 static const size_t kBytesPerPixel = 4;
558 gfx::DeviceDisplayInfo display_info;
559 size_t full_screen_texture_size_in_bytes =
560 display_info.GetDisplayHeight() *
561 display_info.GetDisplayWidth() *
562 kBytesPerPixel;
563 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits limits;
564 limits.command_buffer_size = 64 * 1024;
565 limits.start_transfer_buffer_size = 64 * 1024;
566 limits.min_transfer_buffer_size = 64 * 1024;
567 limits.max_transfer_buffer_size = std::min(
568 3 * full_screen_texture_size_in_bytes, kDefaultMaxTransferBufferSize);
569 limits.mapped_memory_reclaim_limit = 2 * 1024 * 1024;
570 bool lose_context_when_out_of_memory = true;
571 return make_scoped_ptr(
572 new WebGraphicsContext3DCommandBufferImpl(surface_id,
573 url,
574 gpu_channel_host.get(),
575 attributes,
576 lose_context_when_out_of_memory,
577 limits,
578 NULL));
581 void CompositorImpl::Layout() {
582 ignore_schedule_composite_ = true;
583 client_->Layout();
584 ignore_schedule_composite_ = false;
587 void CompositorImpl::OnGpuChannelEstablished() {
588 establish_gpu_channel_timeout_.Stop();
589 CreateOutputSurface();
592 void CompositorImpl::OnGpuChannelTimeout() {
593 LOG(FATAL) << "Timed out waiting for GPU channel.";
596 void CompositorImpl::RequestNewOutputSurface() {
597 output_surface_request_pending_ = true;
599 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || defined(SYZYASAN)
600 const int64 kGpuChannelTimeoutInSeconds = 30;
601 #else
602 const int64 kGpuChannelTimeoutInSeconds = 10;
603 #endif
605 BrowserGpuChannelHostFactory* factory =
606 BrowserGpuChannelHostFactory::instance();
607 if (!factory->GetGpuChannel() || factory->GetGpuChannel()->IsLost()) {
608 factory->EstablishGpuChannel(
609 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE,
610 base::Bind(&CompositorImpl::OnGpuChannelEstablished,
611 weak_factory_.GetWeakPtr()));
612 establish_gpu_channel_timeout_.Start(
613 FROM_HERE, base::TimeDelta::FromSeconds(kGpuChannelTimeoutInSeconds),
614 this, &CompositorImpl::OnGpuChannelTimeout);
615 return;
618 CreateOutputSurface();
621 void CompositorImpl::DidInitializeOutputSurface() {
622 num_successive_context_creation_failures_ = 0;
623 output_surface_request_pending_ = false;
626 void CompositorImpl::DidFailToInitializeOutputSurface() {
627 LOG(ERROR) << "Failed to init OutputSurface for compositor.";
628 LOG_IF(FATAL, ++num_successive_context_creation_failures_ >= 2)
629 << "Too many context creation failures. Giving up... ";
630 RequestNewOutputSurface();
633 void CompositorImpl::CreateOutputSurface() {
634 // We might have had a request from a LayerTreeHost that was then
635 // deleted.
636 if (!output_surface_request_pending_)
637 return;
639 blink::WebGraphicsContext3D::Attributes attrs;
640 attrs.shareResources = true;
641 attrs.noAutomaticFlushes = true;
642 pending_swapbuffers_ = 0;
644 DCHECK(window_);
645 DCHECK(surface_id_);
647 BrowserGpuChannelHostFactory* factory =
648 BrowserGpuChannelHostFactory::instance();
649 // This channel might be lost (and even if it isn't right now, it might
650 // still get marked as lost from the IO thread, at any point in time really).
651 // But from here on just try and always lead to either
652 // DidInitializeOutputSurface() or DidFailToInitializeOutputSurface().
653 scoped_refptr<GpuChannelHost> gpu_channel_host(factory->GetGpuChannel());
654 scoped_refptr<ContextProviderCommandBuffer> context_provider(
655 ContextProviderCommandBuffer::Create(
656 CreateGpuProcessViewContext(gpu_channel_host, attrs, surface_id_),
657 BROWSER_COMPOSITOR_ONSCREEN_CONTEXT));
658 DCHECK(context_provider.get());
660 scoped_ptr<cc::OutputSurface> real_output_surface(
661 new OutputSurfaceWithoutParent(
662 this, context_provider,
663 base::Bind(&CompositorImpl::PopulateGpuCapabilities,
664 base::Unretained(this))));
666 cc::SurfaceManager* manager = GetSurfaceManager();
667 if (manager) {
668 display_client_.reset(new cc::OnscreenDisplayClient(
669 real_output_surface.Pass(), manager, HostSharedBitmapManager::current(),
670 BrowserGpuMemoryBufferManager::current(),
671 host_->settings().renderer_settings,
672 base::ThreadTaskRunnerHandle::Get()));
673 scoped_ptr<cc::SurfaceDisplayOutputSurface> surface_output_surface(
674 new cc::SurfaceDisplayOutputSurface(
675 manager, surface_id_allocator_.get(), context_provider, nullptr));
677 display_client_->set_surface_output_surface(surface_output_surface.get());
678 surface_output_surface->set_display_client(display_client_.get());
679 display_client_->display()->Resize(size_);
680 host_->SetOutputSurface(surface_output_surface.Pass());
681 } else {
682 host_->SetOutputSurface(real_output_surface.Pass());
686 void CompositorImpl::PopulateGpuCapabilities(
687 gpu::Capabilities gpu_capabilities) {
688 ui_resource_provider_.SetSupportsETC1NonPowerOfTwo(
689 gpu_capabilities.texture_format_etc1_npot);
692 void CompositorImpl::AddObserver(VSyncObserver* observer) {
693 observer_list_.AddObserver(observer);
696 void CompositorImpl::RemoveObserver(VSyncObserver* observer) {
697 observer_list_.RemoveObserver(observer);
700 void CompositorImpl::ScheduleComposite() {
701 DCHECK(!needs_composite_ || WillComposite());
702 if (ignore_schedule_composite_)
703 return;
705 needs_composite_ = true;
706 // We currently expect layer tree invalidations at most once per frame
707 // during normal operation and therefore try to composite immediately
708 // to minimize latency.
709 PostComposite(COMPOSITE_IMMEDIATELY);
712 void CompositorImpl::ScheduleAnimation() {
713 DCHECK(!needs_composite_ || WillComposite());
714 needs_animate_ = true;
716 if (needs_composite_)
717 return;
719 TRACE_EVENT0("cc", "CompositorImpl::ScheduleAnimation");
720 needs_composite_ = true;
721 PostComposite(COMPOSITE_EVENTUALLY);
724 void CompositorImpl::DidPostSwapBuffers() {
725 TRACE_EVENT0("compositor", "CompositorImpl::DidPostSwapBuffers");
726 did_post_swapbuffers_ = true;
729 void CompositorImpl::DidCompleteSwapBuffers() {
730 TRACE_EVENT0("compositor", "CompositorImpl::DidCompleteSwapBuffers");
731 DCHECK_GT(pending_swapbuffers_, 0U);
732 if (pending_swapbuffers_-- == kMaxSwapBuffers && needs_composite_)
733 PostComposite(COMPOSITE_IMMEDIATELY);
734 client_->OnSwapBuffersCompleted(pending_swapbuffers_);
737 void CompositorImpl::DidAbortSwapBuffers() {
738 TRACE_EVENT0("compositor", "CompositorImpl::DidAbortSwapBuffers");
739 // This really gets called only once from
740 // SingleThreadProxy::DidLoseOutputSurfaceOnImplThread() when the
741 // context was lost.
742 ScheduleComposite();
743 client_->OnSwapBuffersCompleted(0);
746 void CompositorImpl::DidCommit() {
747 root_window_->OnCompositingDidCommit();
750 void CompositorImpl::AttachLayerForReadback(scoped_refptr<cc::Layer> layer) {
751 root_layer_->AddChild(layer);
754 void CompositorImpl::RequestCopyOfOutputOnRootLayer(
755 scoped_ptr<cc::CopyOutputRequest> request) {
756 root_layer_->RequestCopyOfOutput(request.Pass());
759 void CompositorImpl::OnVSync(base::TimeTicks frame_time,
760 base::TimeDelta vsync_period) {
761 vsync_period_ = vsync_period;
762 last_vsync_ = frame_time;
764 if (WillCompositeThisFrame()) {
765 // We somehow missed the last vsync interval, so reschedule for deadline.
766 // We cannot schedule immediately, or will get us out-of-phase with new
767 // renderer frames.
768 CancelComposite();
769 composite_on_vsync_trigger_ = COMPOSITE_EVENTUALLY;
770 } else {
771 current_composite_task_.reset();
774 DCHECK(!DidCompositeThisFrame() && !WillCompositeThisFrame());
775 if (composite_on_vsync_trigger_ != DO_NOT_COMPOSITE) {
776 CompositingTrigger trigger = composite_on_vsync_trigger_;
777 composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
778 PostComposite(trigger);
781 FOR_EACH_OBSERVER(VSyncObserver, observer_list_,
782 OnUpdateVSyncParameters(frame_time, vsync_period));
785 void CompositorImpl::SetNeedsAnimate() {
786 needs_animate_ = true;
787 if (!host_)
788 return;
790 host_->SetNeedsAnimate();
793 } // namespace content