b38edce89aeb1bfea64e06861fb3f5a40044b9a8
[platform/framework/web/crosswalk.git] / src / content / browser / compositor / browser_compositor_view_mac.mm
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.
4
5 #include "content/browser/compositor/browser_compositor_view_mac.h"
6
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"
12
13 ////////////////////////////////////////////////////////////////////////////////
14 // BrowserCompositorViewMac
15
16 namespace content {
17 namespace {
18
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;
23
24 // A spare BrowserCompositorCALayerTreeMac kept around for recycling.
25 base::LazyInstance<scoped_ptr<BrowserCompositorCALayerTreeMac>>
26   g_recyclable_ca_layer_tree;
27
28 }  // namespace
29
30 BrowserCompositorViewMac::BrowserCompositorViewMac(
31     BrowserCompositorViewMacClient* client,
32     NSView* native_view,
33     ui::Layer* ui_root_layer)
34       : client_(client),
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();
43   if (!ca_layer_tree_)
44     ca_layer_tree_.reset(new BrowserCompositorCALayerTreeMac);
45   ca_layer_tree_->SetView(this);
46 }
47
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();
52
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();
57 }
58
59 ui::Compositor* BrowserCompositorViewMac::GetCompositor() const {
60   DCHECK(ca_layer_tree_);
61   return ca_layer_tree_->compositor();
62 }
63
64 bool BrowserCompositorViewMac::HasFrameOfSize(
65     const gfx::Size& dip_size) const {
66   if (ca_layer_tree_)
67     return ca_layer_tree_->HasFrameOfSize(dip_size);
68   return false;
69 }
70
71 void BrowserCompositorViewMac::BeginPumpingFrames() {
72   if (ca_layer_tree_)
73     ca_layer_tree_->BeginPumpingFrames();
74 }
75
76 void BrowserCompositorViewMac::EndPumpingFrames() {
77   if (ca_layer_tree_)
78     ca_layer_tree_->EndPumpingFrames();
79 }
80
81 ////////////////////////////////////////////////////////////////////////////////
82 // BrowserCompositorViewPlaceholderMac
83
84 BrowserCompositorViewPlaceholderMac::BrowserCompositorViewPlaceholderMac() {
85   g_placeholder_count += 1;
86 }
87
88 BrowserCompositorViewPlaceholderMac::~BrowserCompositorViewPlaceholderMac() {
89   DCHECK_GT(g_placeholder_count, 0u);
90   g_placeholder_count -= 1;
91
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();
96 }
97
98 }  // namespace content