Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / compositor / browser_compositor_view_mac.mm
blob322183d0570ff20a19ef6871219263b4ee653609
1 // Copyright 2014 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/compositor/browser_compositor_view_mac.h"
7 #include "base/lazy_instance.h"
8 #include "base/trace_event/trace_event.h"
9 #include "content/browser/compositor/image_transport_factory.h"
10 #include "content/browser/gpu/gpu_data_manager_impl.h"
11 #include "content/browser/renderer_host/render_widget_resize_helper.h"
12 #include "content/public/browser/context_factory.h"
13 #include "gpu/config/gpu_driver_bug_workaround_type.h"
14 #include "ui/accelerated_widget_mac/accelerated_widget_mac.h"
16 ////////////////////////////////////////////////////////////////////////////////
17 // BrowserCompositorMac
19 namespace content {
21 namespace {
23 // Set when no browser compositors should remain alive.
24 bool g_has_shut_down = false;
26 // The number of placeholder objects allocated. If this reaches zero, then
27 // the BrowserCompositorMac being held on to for recycling,
28 // |g_recyclable_browser_compositor|, will be freed.
29 uint32 g_placeholder_count = 0;
31 // A spare BrowserCompositorMac kept around for recycling.
32 base::LazyInstance<scoped_ptr<BrowserCompositorMac>>
33   g_recyclable_browser_compositor;
35 bool WidgetNeedsGLFinishWorkaround() {
36   return GpuDataManagerImpl::GetInstance()->IsDriverBugWorkaroundActive(
37       gpu::FORCE_GL_FINISH_AFTER_COMPOSITING);
40 }  // namespace
42 BrowserCompositorMac::BrowserCompositorMac()
43     : accelerated_widget_mac_(
44           new ui::AcceleratedWidgetMac(WidgetNeedsGLFinishWorkaround())),
45       compositor_(
46           accelerated_widget_mac_->accelerated_widget(),
47           content::GetContextFactory(),
48           RenderWidgetResizeHelper::Get()->task_runner()) {
49   compositor_.SetLocksWillTimeOut(false);
50   Suspend();
53 BrowserCompositorMac::~BrowserCompositorMac() {}
55 void BrowserCompositorMac::Suspend() {
56   compositor_suspended_lock_ = compositor_.GetCompositorLock();
59 void BrowserCompositorMac::Unsuspend() {
60   compositor_suspended_lock_ = nullptr;
63 // static
64 scoped_ptr<BrowserCompositorMac> BrowserCompositorMac::Create() {
65   if (g_recyclable_browser_compositor.Get())
66     return g_recyclable_browser_compositor.Get().Pass();
67   return scoped_ptr<BrowserCompositorMac>(new BrowserCompositorMac).Pass();
70 // static
71 void BrowserCompositorMac::Recycle(
72     scoped_ptr<BrowserCompositorMac> compositor) {
73   DCHECK(compositor);
74   content::ImageTransportFactory::GetInstance()->OnCompositorRecycled(
75       compositor->compositor());
77   // It is an error to have a browser compositor continue to exist after
78   // shutdown.
79   CHECK(!g_has_shut_down);
81   // Make this BrowserCompositorMac recyclable for future instances.
82   g_recyclable_browser_compositor.Get().swap(compositor);
84   // If there are no placeholders allocated, destroy the recyclable
85   // BrowserCompositorMac that we just populated.
86   if (!g_placeholder_count)
87     g_recyclable_browser_compositor.Get().reset();
90 // static
91 void BrowserCompositorMac::DisableRecyclingForShutdown() {
92   g_has_shut_down = true;
93   g_recyclable_browser_compositor.Get().reset();
96 ////////////////////////////////////////////////////////////////////////////////
97 // BrowserCompositorMacPlaceholder
99 BrowserCompositorMacPlaceholder::BrowserCompositorMacPlaceholder() {
100   g_placeholder_count += 1;
103 BrowserCompositorMacPlaceholder::~BrowserCompositorMacPlaceholder() {
104   DCHECK_GT(g_placeholder_count, 0u);
105   g_placeholder_count -= 1;
107   // If there are no placeholders allocated, destroy the recyclable
108   // BrowserCompositorMac.
109   if (!g_placeholder_count)
110     g_recyclable_browser_compositor.Get().reset();
113 }  // namespace content