DevTools: cut host and port from webSocketDebuggerUrl in addition to ws:// prefix
[chromium-blink-merge.git] / content / browser / renderer_host / compositor_impl_android.cc
blob9440da58aa1993f57caa7e4bece56aef9edc957f
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;
437 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
438 settings.initial_debug_state.SetRecordRenderingStats(
439 command_line->HasSwitch(cc::switches::kEnableGpuBenchmarking));
440 settings.initial_debug_state.show_fps_counter =
441 command_line->HasSwitch(cc::switches::kUIShowFPSCounter);
442 // TODO(enne): Update this this compositor to use the scheduler.
443 settings.single_thread_proxy_scheduler = false;
445 if (command_line->HasSwitch(
446 switches::kEnableAndroidCompositorAnimationTimelines))
447 settings.use_compositor_animation_timelines = true;
449 cc::LayerTreeHost::InitParams params;
450 params.client = this;
451 params.shared_bitmap_manager = HostSharedBitmapManager::current();
452 params.gpu_memory_buffer_manager = BrowserGpuMemoryBufferManager::current();
453 params.task_graph_runner = g_task_graph_runner.Pointer();
454 params.main_task_runner = base::ThreadTaskRunnerHandle::Get();
455 params.settings = &settings;
456 host_ = cc::LayerTreeHost::CreateSingleThreaded(this, &params);
457 host_->SetRootLayer(root_layer_);
459 host_->SetVisible(true);
460 host_->SetLayerTreeHostClientReady();
461 host_->SetViewportSize(size_);
462 host_->set_has_transparent_background(has_transparent_background_);
463 host_->SetDeviceScaleFactor(device_scale_factor_);
465 if (needs_animate_)
466 host_->SetNeedsAnimate();
469 void CompositorImpl::SetVisible(bool visible) {
470 TRACE_EVENT1("cc", "CompositorImpl::SetVisible", "visible", visible);
471 if (!visible) {
472 DCHECK(host_);
473 // Look for any layers that were attached to the root for readback
474 // and are waiting for Composite() to happen.
475 bool readback_pending = false;
476 for (size_t i = 0; i < root_layer_->children().size(); ++i) {
477 if (root_layer_->children()[i]->HasCopyRequest()) {
478 readback_pending = true;
479 break;
482 if (readback_pending) {
483 ignore_schedule_composite_ = true;
484 host_->Composite(base::TimeTicks::Now());
485 ignore_schedule_composite_ = false;
487 if (WillComposite())
488 CancelComposite();
489 ui_resource_provider_.SetLayerTreeHost(NULL);
490 host_.reset();
491 establish_gpu_channel_timeout_.Stop();
492 output_surface_request_pending_ = false;
493 display_client_.reset();
494 if (current_composite_task_) {
495 current_composite_task_->Cancel();
496 current_composite_task_.reset();
498 } else if (!host_) {
499 CreateLayerTreeHost();
500 ui_resource_provider_.SetLayerTreeHost(host_.get());
502 // TODO(jdduke): Consider using View.onWindowVisibilityChange() to drive
503 // WindowAndroid visibility updates. CompositorImpl::SetVisible changes can
504 // occur while the visibile surface is temporarily changing, e.g., when
505 // transitioning to fullscreen video. See crbug.com/471262.
506 root_window_->OnVisibilityChanged(visible);
509 void CompositorImpl::setDeviceScaleFactor(float factor) {
510 device_scale_factor_ = factor;
511 if (host_)
512 host_->SetDeviceScaleFactor(factor);
515 void CompositorImpl::SetWindowBounds(const gfx::Size& size) {
516 if (size_ == size)
517 return;
519 size_ = size;
520 if (host_)
521 host_->SetViewportSize(size);
522 if (display_client_)
523 display_client_->display()->Resize(size);
524 root_layer_->SetBounds(size);
527 void CompositorImpl::SetHasTransparentBackground(bool flag) {
528 has_transparent_background_ = flag;
529 if (host_)
530 host_->set_has_transparent_background(flag);
533 void CompositorImpl::SetNeedsComposite() {
534 if (!host_.get())
535 return;
536 DCHECK(!needs_composite_ || WillComposite());
538 needs_composite_ = true;
539 PostComposite(COMPOSITE_IMMEDIATELY);
542 static scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
543 CreateGpuProcessViewContext(
544 const scoped_refptr<GpuChannelHost>& gpu_channel_host,
545 const blink::WebGraphicsContext3D::Attributes attributes,
546 int surface_id) {
547 GURL url("chrome://gpu/Compositor::createContext3D");
548 static const size_t kBytesPerPixel = 4;
549 gfx::DeviceDisplayInfo display_info;
550 size_t full_screen_texture_size_in_bytes =
551 display_info.GetDisplayHeight() *
552 display_info.GetDisplayWidth() *
553 kBytesPerPixel;
554 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits limits;
555 limits.command_buffer_size = 64 * 1024;
556 limits.start_transfer_buffer_size = 64 * 1024;
557 limits.min_transfer_buffer_size = 64 * 1024;
558 limits.max_transfer_buffer_size = std::min(
559 3 * full_screen_texture_size_in_bytes, kDefaultMaxTransferBufferSize);
560 limits.mapped_memory_reclaim_limit = 2 * 1024 * 1024;
561 bool lose_context_when_out_of_memory = true;
562 return make_scoped_ptr(
563 new WebGraphicsContext3DCommandBufferImpl(surface_id,
564 url,
565 gpu_channel_host.get(),
566 attributes,
567 lose_context_when_out_of_memory,
568 limits,
569 NULL));
572 void CompositorImpl::Layout() {
573 ignore_schedule_composite_ = true;
574 client_->Layout();
575 ignore_schedule_composite_ = false;
578 void CompositorImpl::OnGpuChannelEstablished() {
579 establish_gpu_channel_timeout_.Stop();
580 CreateOutputSurface();
583 void CompositorImpl::OnGpuChannelTimeout() {
584 LOG(FATAL) << "Timed out waiting for GPU channel.";
587 void CompositorImpl::RequestNewOutputSurface() {
588 output_surface_request_pending_ = true;
590 #if defined(ADDRESS_SANITIZER) || defined(THREAD_SANITIZER) || defined(SYZYASAN)
591 const int64 kGpuChannelTimeoutInSeconds = 30;
592 #else
593 const int64 kGpuChannelTimeoutInSeconds = 10;
594 #endif
596 BrowserGpuChannelHostFactory* factory =
597 BrowserGpuChannelHostFactory::instance();
598 if (!factory->GetGpuChannel() || factory->GetGpuChannel()->IsLost()) {
599 factory->EstablishGpuChannel(
600 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE,
601 base::Bind(&CompositorImpl::OnGpuChannelEstablished,
602 weak_factory_.GetWeakPtr()));
603 establish_gpu_channel_timeout_.Start(
604 FROM_HERE, base::TimeDelta::FromSeconds(kGpuChannelTimeoutInSeconds),
605 this, &CompositorImpl::OnGpuChannelTimeout);
606 return;
609 CreateOutputSurface();
612 void CompositorImpl::DidInitializeOutputSurface() {
613 num_successive_context_creation_failures_ = 0;
614 output_surface_request_pending_ = false;
617 void CompositorImpl::DidFailToInitializeOutputSurface() {
618 LOG(ERROR) << "Failed to init OutputSurface for compositor.";
619 LOG_IF(FATAL, ++num_successive_context_creation_failures_ >= 2)
620 << "Too many context creation failures. Giving up... ";
621 RequestNewOutputSurface();
624 void CompositorImpl::CreateOutputSurface() {
625 // We might have had a request from a LayerTreeHost that was then
626 // deleted.
627 if (!output_surface_request_pending_)
628 return;
630 blink::WebGraphicsContext3D::Attributes attrs;
631 attrs.shareResources = true;
632 attrs.noAutomaticFlushes = true;
633 pending_swapbuffers_ = 0;
635 DCHECK(window_);
636 DCHECK(surface_id_);
638 BrowserGpuChannelHostFactory* factory =
639 BrowserGpuChannelHostFactory::instance();
640 // This channel might be lost (and even if it isn't right now, it might
641 // still get marked as lost from the IO thread, at any point in time really).
642 // But from here on just try and always lead to either
643 // DidInitializeOutputSurface() or DidFailToInitializeOutputSurface().
644 scoped_refptr<GpuChannelHost> gpu_channel_host(factory->GetGpuChannel());
645 scoped_refptr<ContextProviderCommandBuffer> context_provider(
646 ContextProviderCommandBuffer::Create(
647 CreateGpuProcessViewContext(gpu_channel_host, attrs, surface_id_),
648 BROWSER_COMPOSITOR_ONSCREEN_CONTEXT));
649 DCHECK(context_provider.get());
651 scoped_ptr<cc::OutputSurface> real_output_surface(
652 new OutputSurfaceWithoutParent(
653 context_provider, base::Bind(&CompositorImpl::PopulateGpuCapabilities,
654 base::Unretained(this))));
656 cc::SurfaceManager* manager = GetSurfaceManager();
657 if (manager) {
658 display_client_.reset(new cc::OnscreenDisplayClient(
659 real_output_surface.Pass(), manager, HostSharedBitmapManager::current(),
660 BrowserGpuMemoryBufferManager::current(),
661 host_->settings().renderer_settings,
662 base::ThreadTaskRunnerHandle::Get()));
663 scoped_ptr<cc::SurfaceDisplayOutputSurface> surface_output_surface(
664 new cc::SurfaceDisplayOutputSurface(
665 manager, surface_id_allocator_.get(), context_provider));
667 display_client_->set_surface_output_surface(surface_output_surface.get());
668 surface_output_surface->set_display_client(display_client_.get());
669 display_client_->display()->Resize(size_);
670 host_->SetOutputSurface(surface_output_surface.Pass());
671 } else {
672 host_->SetOutputSurface(real_output_surface.Pass());
676 void CompositorImpl::PopulateGpuCapabilities(
677 gpu::Capabilities gpu_capabilities) {
678 ui_resource_provider_.SetSupportsETC1NonPowerOfTwo(
679 gpu_capabilities.texture_format_etc1_npot);
682 void CompositorImpl::ScheduleComposite() {
683 DCHECK(!needs_composite_ || WillComposite());
684 if (ignore_schedule_composite_)
685 return;
687 needs_composite_ = true;
688 // We currently expect layer tree invalidations at most once per frame
689 // during normal operation and therefore try to composite immediately
690 // to minimize latency.
691 PostComposite(COMPOSITE_IMMEDIATELY);
694 void CompositorImpl::ScheduleAnimation() {
695 DCHECK(!needs_composite_ || WillComposite());
696 needs_animate_ = true;
698 if (needs_composite_)
699 return;
701 TRACE_EVENT0("cc", "CompositorImpl::ScheduleAnimation");
702 needs_composite_ = true;
703 PostComposite(COMPOSITE_EVENTUALLY);
706 void CompositorImpl::DidPostSwapBuffers() {
707 TRACE_EVENT0("compositor", "CompositorImpl::DidPostSwapBuffers");
708 did_post_swapbuffers_ = true;
711 void CompositorImpl::DidCompleteSwapBuffers() {
712 TRACE_EVENT0("compositor", "CompositorImpl::DidCompleteSwapBuffers");
713 DCHECK_GT(pending_swapbuffers_, 0U);
714 if (pending_swapbuffers_-- == kMaxSwapBuffers && needs_composite_)
715 PostComposite(COMPOSITE_IMMEDIATELY);
716 client_->OnSwapBuffersCompleted(pending_swapbuffers_);
719 void CompositorImpl::DidAbortSwapBuffers() {
720 TRACE_EVENT0("compositor", "CompositorImpl::DidAbortSwapBuffers");
721 // This really gets called only once from
722 // SingleThreadProxy::DidLoseOutputSurfaceOnImplThread() when the
723 // context was lost.
724 ScheduleComposite();
725 client_->OnSwapBuffersCompleted(0);
728 void CompositorImpl::DidCommit() {
729 root_window_->OnCompositingDidCommit();
732 void CompositorImpl::AttachLayerForReadback(scoped_refptr<cc::Layer> layer) {
733 root_layer_->AddChild(layer);
736 void CompositorImpl::RequestCopyOfOutputOnRootLayer(
737 scoped_ptr<cc::CopyOutputRequest> request) {
738 root_layer_->RequestCopyOfOutput(request.Pass());
741 void CompositorImpl::OnVSync(base::TimeTicks frame_time,
742 base::TimeDelta vsync_period) {
743 vsync_period_ = vsync_period;
744 last_vsync_ = frame_time;
746 if (WillCompositeThisFrame()) {
747 // We somehow missed the last vsync interval, so reschedule for deadline.
748 // We cannot schedule immediately, or will get us out-of-phase with new
749 // renderer frames.
750 CancelComposite();
751 composite_on_vsync_trigger_ = COMPOSITE_EVENTUALLY;
752 } else {
753 current_composite_task_.reset();
756 DCHECK(!DidCompositeThisFrame() && !WillCompositeThisFrame());
757 if (composite_on_vsync_trigger_ != DO_NOT_COMPOSITE) {
758 CompositingTrigger trigger = composite_on_vsync_trigger_;
759 composite_on_vsync_trigger_ = DO_NOT_COMPOSITE;
760 PostComposite(trigger);
764 void CompositorImpl::SetNeedsAnimate() {
765 needs_animate_ = true;
766 if (!host_)
767 return;
769 host_->SetNeedsAnimate();
772 } // namespace content