1 // Copyright 2012 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 #ifndef CC_TREES_OCCLUSION_TRACKER_H_
6 #define CC_TREES_OCCLUSION_TRACKER_H_
10 #include "base/basictypes.h"
11 #include "cc/base/cc_export.h"
12 #include "cc/base/region.h"
13 #include "cc/layers/layer_iterator.h"
14 #include "ui/gfx/rect.h"
17 class OverdrawMetrics
;
19 class RenderSurfaceImpl
;
23 // This class is used to track occlusion of layers while traversing them in a
24 // front-to-back order. As each layer is visited, one of the methods in this
25 // class is called to notify it about the current target surface. Then,
26 // occlusion in the content space of the current layer may be queried, via
27 // methods such as Occluded() and UnoccludedContentRect(). If the current layer
28 // owns a RenderSurfaceImpl, then occlusion on that RenderSurfaceImpl may also
29 // be queried via surfaceOccluded() and surfaceUnoccludedContentRect(). Finally,
30 // once finished with the layer, occlusion behind the layer should be marked by
31 // calling MarkOccludedBehindLayer().
32 template <typename LayerType
, typename RenderSurfaceType
>
33 class CC_EXPORT OcclusionTrackerBase
{
35 OcclusionTrackerBase(gfx::Rect screen_space_clip_rect
,
36 bool record_metrics_for_frame
);
37 ~OcclusionTrackerBase();
39 // Called at the beginning of each step in the LayerIterator's front-to-back
40 // traversal. If |prevent_occlusion| is true, the layer will be considered
42 void EnterLayer(const LayerIteratorPosition
<LayerType
>& layer_iterator
,
43 bool prevent_occlusion
);
44 // Called at the end of each step in the LayerIterator's front-to-back
46 void LeaveLayer(const LayerIteratorPosition
<LayerType
>& layer_iterator
);
48 // Returns true if the given rect in content space for a layer is fully
49 // occluded in either screen space or the layer's target surface.
50 // |render_target| is the contributing layer's render target, and
51 // |draw_transform| and |impl_draw_transform_is_unknown| are relative to that.
52 bool Occluded(const LayerType
* render_target
,
53 gfx::Rect content_rect
,
54 const gfx::Transform
& draw_transform
,
55 bool impl_draw_transform_is_unknown
) const;
57 // Gives an unoccluded sub-rect of |content_rect| in the content space of a
58 // layer. Used when considering occlusion for a layer that paints/draws
59 // something. |render_target| is the contributing layer's render target, and
60 // |draw_transform| and |impl_draw_transform_is_unknown| are relative to that.
61 gfx::Rect
UnoccludedContentRect(
62 const LayerType
* render_target
,
63 gfx::Rect content_rect
,
64 const gfx::Transform
& draw_transform
,
65 bool impl_draw_transform_is_unknown
) const;
67 // Gives an unoccluded sub-rect of |content_rect| in the content space of the
68 // render_target owned by the layer. Used when considering occlusion for a
69 // contributing surface that is rendering into another target.
70 gfx::Rect
UnoccludedContributingSurfaceContentRect(
71 const LayerType
* layer
,
73 gfx::Rect content_rect
) const;
75 // Report operations for recording overdraw metrics.
76 OverdrawMetrics
* overdraw_metrics() const {
77 return overdraw_metrics_
.get();
80 // Gives the region of the screen that is not occluded by something opaque.
81 Region
ComputeVisibleRegionInScreen() const {
82 DCHECK(!stack_
.back().target
->parent());
83 return SubtractRegions(screen_space_clip_rect_
,
84 stack_
.back().occlusion_from_inside_target
);
87 void set_minimum_tracking_size(gfx::Size size
) {
88 minimum_tracking_size_
= size
;
91 // The following is used for visualization purposes.
92 void set_occluding_screen_space_rects_container(
93 std::vector
<gfx::Rect
>* rects
) {
94 occluding_screen_space_rects_
= rects
;
96 void set_non_occluding_screen_space_rects_container(
97 std::vector
<gfx::Rect
>* rects
) {
98 non_occluding_screen_space_rects_
= rects
;
103 StackObject() : target(0) {}
104 explicit StackObject(const LayerType
* target
) : target(target
) {}
105 const LayerType
* target
;
106 Region occlusion_from_outside_target
;
107 Region occlusion_from_inside_target
;
110 // The stack holds occluded regions for subtrees in the
111 // RenderSurfaceImpl-Layer tree, so that when we leave a subtree we may apply
112 // a mask to it, but not to the parts outside the subtree.
113 // - The first time we see a new subtree under a target, we add that target to
114 // the top of the stack. This can happen as a layer representing itself, or as
116 // - When we visit a target surface, we apply its mask to its subtree, which
117 // is at the top of the stack.
118 // - When we visit a layer representing itself, we add its occlusion to the
119 // current subtree, which is at the top of the stack.
120 // - When we visit a layer representing a contributing surface, the current
121 // target will never be the top of the stack since we just came from the
122 // contributing surface.
123 // We merge the occlusion at the top of the stack with the new current
124 // subtree. This new target is pushed onto the stack if not already there.
125 std::vector
<StackObject
> stack_
;
128 // Called when visiting a layer representing itself. If the target was not
129 // already current, then this indicates we have entered a new surface subtree.
130 void EnterRenderTarget(const LayerType
* new_target
);
132 // Called when visiting a layer representing a target surface. This indicates
133 // we have visited all the layers within the surface, and we may perform any
134 // surface-wide operations.
135 void FinishedRenderTarget(const LayerType
* finished_target
);
137 // Called when visiting a layer representing a contributing surface. This
138 // indicates that we are leaving our current surface, and entering the new
139 // one. We then perform any operations required for merging results from the
140 // child subtree into its parent.
141 void LeaveToRenderTarget(const LayerType
* new_target
);
143 // Add the layer's occlusion to the tracked state.
144 void MarkOccludedBehindLayer(const LayerType
* layer
);
146 gfx::Rect screen_space_clip_rect_
;
147 scoped_ptr
<class OverdrawMetrics
> overdraw_metrics_
;
148 gfx::Size minimum_tracking_size_
;
149 bool prevent_occlusion_
;
151 // This is used for visualizing the occlusion tracking process.
152 std::vector
<gfx::Rect
>* occluding_screen_space_rects_
;
153 std::vector
<gfx::Rect
>* non_occluding_screen_space_rects_
;
155 DISALLOW_COPY_AND_ASSIGN(OcclusionTrackerBase
);
158 typedef OcclusionTrackerBase
<Layer
, RenderSurface
> OcclusionTracker
;
159 typedef OcclusionTrackerBase
<LayerImpl
, RenderSurfaceImpl
> OcclusionTrackerImpl
;
160 #if !defined(COMPILER_MSVC)
161 extern template class OcclusionTrackerBase
<Layer
, RenderSurface
>;
162 extern template class OcclusionTrackerBase
<LayerImpl
, RenderSurfaceImpl
>;
167 #endif // CC_TREES_OCCLUSION_TRACKER_H_