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/debug/trace_event.h"
8 #include "base/lazy_instance.h"
9 #include "content/browser/gpu/gpu_process_host_ui_shim.h"
10 #include "content/browser/compositor/browser_compositor_ca_layer_tree_mac.h"
11 #include "content/common/gpu/gpu_messages.h"
13 ////////////////////////////////////////////////////////////////////////////////
14 // BrowserCompositorViewMac
19 // The number of placeholder objects allocated. If this reaches zero, then
20 // the BrowserCompositorCALayerTreeMac being held on to for recycling,
21 // |g_recyclable_ca_layer_tree|, will be freed.
22 uint32 g_placeholder_count = 0;
24 // A spare BrowserCompositorCALayerTreeMac kept around for recycling.
25 base::LazyInstance<scoped_ptr<BrowserCompositorCALayerTreeMac>>
26 g_recyclable_ca_layer_tree;
30 BrowserCompositorViewMac::BrowserCompositorViewMac(
31 BrowserCompositorViewMacClient* client,
33 ui::Layer* ui_root_layer)
35 native_view_(native_view),
36 ui_root_layer_(ui_root_layer) {
37 // Try to use the recyclable BrowserCompositorCALayerTreeMac if there is one,
38 // otherwise allocate a new one.
39 // TODO(ccameron): If there exists a frame in flight (swap has been called
40 // by the compositor, but the frame has not arrived from the GPU process
41 // yet), then that frame may inappropriately flash in the new view.
42 ca_layer_tree_ = g_recyclable_ca_layer_tree.Get().Pass();
44 ca_layer_tree_.reset(new BrowserCompositorCALayerTreeMac);
45 ca_layer_tree_->SetView(this);
48 BrowserCompositorViewMac::~BrowserCompositorViewMac() {
49 // Make this BrowserCompositorCALayerTreeMac recyclable for future instances.
50 ca_layer_tree_->ResetView();
51 g_recyclable_ca_layer_tree.Get() = ca_layer_tree_.Pass();
53 // If there are no placeholders allocated, destroy the recyclable
54 // BrowserCompositorCALayerTreeMac that we just populated.
55 if (!g_placeholder_count)
56 g_recyclable_ca_layer_tree.Get().reset();
59 ui::Compositor* BrowserCompositorViewMac::GetCompositor() const {
60 DCHECK(ca_layer_tree_);
61 return ca_layer_tree_->compositor();
64 bool BrowserCompositorViewMac::HasFrameOfSize(
65 const gfx::Size& dip_size) const {
67 return ca_layer_tree_->HasFrameOfSize(dip_size);
71 void BrowserCompositorViewMac::BeginPumpingFrames() {
73 ca_layer_tree_->BeginPumpingFrames();
76 void BrowserCompositorViewMac::EndPumpingFrames() {
78 ca_layer_tree_->EndPumpingFrames();
81 ////////////////////////////////////////////////////////////////////////////////
82 // BrowserCompositorViewPlaceholderMac
84 BrowserCompositorViewPlaceholderMac::BrowserCompositorViewPlaceholderMac() {
85 g_placeholder_count += 1;
88 BrowserCompositorViewPlaceholderMac::~BrowserCompositorViewPlaceholderMac() {
89 DCHECK_GT(g_placeholder_count, 0u);
90 g_placeholder_count -= 1;
92 // If there are no placeholders allocated, destroy the recyclable
93 // BrowserCompositorCALayerTreeMac.
94 if (!g_placeholder_count)
95 g_recyclable_ca_layer_tree.Get().reset();
98 } // namespace content