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_mac.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 content::GetContextFactory(),
47 RenderWidgetResizeHelper::Get()->task_runner()) {
48 compositor_.SetAcceleratedWidgetAndStartCompositor(
49 accelerated_widget_mac_->accelerated_widget());
50 compositor_.SetLocksWillTimeOut(false);
52 compositor_.AddObserver(this);
55 BrowserCompositorMac::~BrowserCompositorMac() {
56 compositor_.RemoveObserver(this);
59 void BrowserCompositorMac::Suspend() {
60 compositor_suspended_lock_ = compositor_.GetCompositorLock();
63 void BrowserCompositorMac::Unsuspend() {
64 compositor_suspended_lock_ = nullptr;
67 void BrowserCompositorMac::OnCompositingDidCommit(
68 ui::Compositor* compositor_that_did_commit) {
69 DCHECK_EQ(compositor_that_did_commit, compositor());
70 content::ImageTransportFactory::GetInstance()
71 ->SetCompositorSuspendedForRecycle(compositor(), false);
75 scoped_ptr<BrowserCompositorMac> BrowserCompositorMac::Create() {
76 if (g_recyclable_browser_compositor.Get())
77 return g_recyclable_browser_compositor.Get().Pass();
78 return scoped_ptr<BrowserCompositorMac>(new BrowserCompositorMac).Pass();
82 void BrowserCompositorMac::Recycle(
83 scoped_ptr<BrowserCompositorMac> compositor) {
85 content::ImageTransportFactory::GetInstance()
86 ->SetCompositorSuspendedForRecycle(compositor->compositor(), true);
88 // It is an error to have a browser compositor continue to exist after
90 CHECK(!g_has_shut_down);
92 // Make this BrowserCompositorMac recyclable for future instances.
93 g_recyclable_browser_compositor.Get().swap(compositor);
95 // If there are no placeholders allocated, destroy the recyclable
96 // BrowserCompositorMac that we just populated.
97 if (!g_placeholder_count)
98 g_recyclable_browser_compositor.Get().reset();
102 void BrowserCompositorMac::DisableRecyclingForShutdown() {
103 g_has_shut_down = true;
104 g_recyclable_browser_compositor.Get().reset();
107 ////////////////////////////////////////////////////////////////////////////////
108 // BrowserCompositorMacPlaceholder
110 BrowserCompositorMacPlaceholder::BrowserCompositorMacPlaceholder() {
111 g_placeholder_count += 1;
114 BrowserCompositorMacPlaceholder::~BrowserCompositorMacPlaceholder() {
115 DCHECK_GT(g_placeholder_count, 0u);
116 g_placeholder_count -= 1;
118 // If there are no placeholders allocated, destroy the recyclable
119 // BrowserCompositorMac.
120 if (!g_placeholder_count)
121 g_recyclable_browser_compositor.Get().reset();
124 } // namespace content