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
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);
42 BrowserCompositorMac::BrowserCompositorMac()
43 : accelerated_widget_mac_(
44 new ui::AcceleratedWidgetMac(WidgetNeedsGLFinishWorkaround())),
46 accelerated_widget_mac_->accelerated_widget(),
47 content::GetContextFactory(),
48 RenderWidgetResizeHelper::Get()->task_runner()) {
49 compositor_.SetLocksWillTimeOut(false);
51 compositor_.AddObserver(this);
54 BrowserCompositorMac::~BrowserCompositorMac() {
55 compositor_.RemoveObserver(this);
58 void BrowserCompositorMac::Suspend() {
59 compositor_suspended_lock_ = compositor_.GetCompositorLock();
62 void BrowserCompositorMac::Unsuspend() {
63 compositor_suspended_lock_ = nullptr;
66 void BrowserCompositorMac::OnCompositingDidCommit(
67 ui::Compositor* compositor_that_did_commit) {
68 DCHECK_EQ(compositor_that_did_commit, compositor());
69 content::ImageTransportFactory::GetInstance()
70 ->SetCompositorSuspendedForRecycle(compositor(), false);
74 scoped_ptr<BrowserCompositorMac> BrowserCompositorMac::Create() {
75 if (g_recyclable_browser_compositor.Get())
76 return g_recyclable_browser_compositor.Get().Pass();
77 return scoped_ptr<BrowserCompositorMac>(new BrowserCompositorMac).Pass();
81 void BrowserCompositorMac::Recycle(
82 scoped_ptr<BrowserCompositorMac> compositor) {
84 content::ImageTransportFactory::GetInstance()
85 ->SetCompositorSuspendedForRecycle(compositor->compositor(), true);
87 // It is an error to have a browser compositor continue to exist after
89 CHECK(!g_has_shut_down);
91 // Make this BrowserCompositorMac recyclable for future instances.
92 g_recyclable_browser_compositor.Get().swap(compositor);
94 // If there are no placeholders allocated, destroy the recyclable
95 // BrowserCompositorMac that we just populated.
96 if (!g_placeholder_count)
97 g_recyclable_browser_compositor.Get().reset();
101 void BrowserCompositorMac::DisableRecyclingForShutdown() {
102 g_has_shut_down = true;
103 g_recyclable_browser_compositor.Get().reset();
106 ////////////////////////////////////////////////////////////////////////////////
107 // BrowserCompositorMacPlaceholder
109 BrowserCompositorMacPlaceholder::BrowserCompositorMacPlaceholder() {
110 g_placeholder_count += 1;
113 BrowserCompositorMacPlaceholder::~BrowserCompositorMacPlaceholder() {
114 DCHECK_GT(g_placeholder_count, 0u);
115 g_placeholder_count -= 1;
117 // If there are no placeholders allocated, destroy the recyclable
118 // BrowserCompositorMac.
119 if (!g_placeholder_count)
120 g_recyclable_browser_compositor.Get().reset();
123 } // namespace content