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()) {
51 BrowserCompositorMac::~BrowserCompositorMac() {}
54 scoped_ptr<BrowserCompositorMac> BrowserCompositorMac::Create() {
55 if (g_recyclable_browser_compositor.Get())
56 return g_recyclable_browser_compositor.Get().Pass();
57 return scoped_ptr<BrowserCompositorMac>(new BrowserCompositorMac).Pass();
61 void BrowserCompositorMac::Recycle(
62 scoped_ptr<BrowserCompositorMac> compositor) {
64 content::ImageTransportFactory::GetInstance()->OnCompositorRecycled(
65 compositor->compositor());
67 // It is an error to have a browser compositor continue to exist after
69 CHECK(!g_has_shut_down);
71 // Make this BrowserCompositorMac recyclable for future instances.
72 g_recyclable_browser_compositor.Get().swap(compositor);
74 // If there are no placeholders allocated, destroy the recyclable
75 // BrowserCompositorMac that we just populated.
76 if (!g_placeholder_count)
77 g_recyclable_browser_compositor.Get().reset();
81 void BrowserCompositorMac::DisableRecyclingForShutdown() {
82 g_has_shut_down = true;
83 g_recyclable_browser_compositor.Get().reset();
86 ////////////////////////////////////////////////////////////////////////////////
87 // BrowserCompositorMacPlaceholder
89 BrowserCompositorMacPlaceholder::BrowserCompositorMacPlaceholder() {
90 g_placeholder_count += 1;
93 BrowserCompositorMacPlaceholder::~BrowserCompositorMacPlaceholder() {
94 DCHECK_GT(g_placeholder_count, 0u);
95 g_placeholder_count -= 1;
97 // If there are no placeholders allocated, destroy the recyclable
98 // BrowserCompositorMac.
99 if (!g_placeholder_count)
100 g_recyclable_browser_compositor.Get().reset();
103 } // namespace content