1 // Copyright 2011 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_LAYER_TREE_HOST_COMMON_H_
6 #define CC_TREES_LAYER_TREE_HOST_COMMON_H_
11 #include "base/bind.h"
12 #include "base/memory/ref_counted.h"
13 #include "cc/base/cc_export.h"
14 #include "cc/base/scoped_ptr_vector.h"
15 #include "cc/layers/layer_lists.h"
16 #include "ui/gfx/rect.h"
17 #include "ui/gfx/transform.h"
18 #include "ui/gfx/vector2d.h"
25 class CC_EXPORT LayerTreeHostCommon
{
27 static gfx::Rect
CalculateVisibleRect(const gfx::Rect
& target_surface_rect
,
28 const gfx::Rect
& layer_bound_rect
,
29 const gfx::Transform
& transform
);
31 template <typename LayerType
, typename RenderSurfaceLayerListType
>
32 struct CalcDrawPropsInputs
{
34 CalcDrawPropsInputs(LayerType
* root_layer
,
35 const gfx::Size
& device_viewport_size
,
36 const gfx::Transform
& device_transform
,
37 float device_scale_factor
,
38 float page_scale_factor
,
39 const LayerType
* page_scale_application_layer
,
41 bool can_use_lcd_text
,
42 bool can_render_to_separate_surface
,
43 bool can_adjust_raster_scales
,
44 RenderSurfaceLayerListType
* render_surface_layer_list
,
45 int current_render_surface_layer_list_id
)
46 : root_layer(root_layer
),
47 device_viewport_size(device_viewport_size
),
48 device_transform(device_transform
),
49 device_scale_factor(device_scale_factor
),
50 page_scale_factor(page_scale_factor
),
51 page_scale_application_layer(page_scale_application_layer
),
52 max_texture_size(max_texture_size
),
53 can_use_lcd_text(can_use_lcd_text
),
54 can_render_to_separate_surface(can_render_to_separate_surface
),
55 can_adjust_raster_scales(can_adjust_raster_scales
),
56 render_surface_layer_list(render_surface_layer_list
),
57 current_render_surface_layer_list_id(
58 current_render_surface_layer_list_id
) {}
60 LayerType
* root_layer
;
61 gfx::Size device_viewport_size
;
62 const gfx::Transform
& device_transform
;
63 float device_scale_factor
;
64 float page_scale_factor
;
65 const LayerType
* page_scale_application_layer
;
67 bool can_use_lcd_text
;
68 bool can_render_to_separate_surface
;
69 bool can_adjust_raster_scales
;
70 RenderSurfaceLayerListType
* render_surface_layer_list
;
71 int current_render_surface_layer_list_id
;
74 template <typename LayerType
, typename RenderSurfaceLayerListType
>
75 struct CalcDrawPropsInputsForTesting
76 : public CalcDrawPropsInputs
<LayerType
, RenderSurfaceLayerListType
> {
77 CalcDrawPropsInputsForTesting(
78 LayerType
* root_layer
,
79 const gfx::Size
& device_viewport_size
,
80 const gfx::Transform
& device_transform
,
81 RenderSurfaceLayerListType
* render_surface_layer_list
);
82 CalcDrawPropsInputsForTesting(
83 LayerType
* root_layer
,
84 const gfx::Size
& device_viewport_size
,
85 RenderSurfaceLayerListType
* render_surface_layer_list
);
88 const gfx::Transform identity_transform_
;
91 typedef CalcDrawPropsInputs
<Layer
, RenderSurfaceLayerList
>
92 CalcDrawPropsMainInputs
;
93 typedef CalcDrawPropsInputsForTesting
<Layer
, RenderSurfaceLayerList
>
94 CalcDrawPropsMainInputsForTesting
;
95 static void CalculateDrawProperties(CalcDrawPropsMainInputs
* inputs
);
97 typedef CalcDrawPropsInputs
<LayerImpl
, LayerImplList
> CalcDrawPropsImplInputs
;
98 typedef CalcDrawPropsInputsForTesting
<LayerImpl
, LayerImplList
>
99 CalcDrawPropsImplInputsForTesting
;
100 static void CalculateDrawProperties(CalcDrawPropsImplInputs
* inputs
);
102 template <typename LayerType
>
103 static bool RenderSurfaceContributesToTarget(LayerType
*,
104 int target_surface_layer_id
);
106 template <typename LayerType
>
107 static void CallFunctionForSubtree(
108 LayerType
* root_layer
,
109 const base::Callback
<void(LayerType
* layer
)>& function
);
111 // Returns a layer with the given id if one exists in the subtree starting
112 // from the given root layer (including mask and replica layers).
113 template <typename LayerType
>
114 static LayerType
* FindLayerInSubtree(LayerType
* root_layer
, int layer_id
);
116 static Layer
* get_layer_as_raw_ptr(const LayerList
& layers
, size_t index
) {
117 return layers
[index
].get();
120 static LayerImpl
* get_layer_as_raw_ptr(const OwnedLayerImplList
& layers
,
122 return layers
[index
];
125 static LayerImpl
* get_layer_as_raw_ptr(const LayerImplList
& layers
,
127 return layers
[index
];
130 struct ScrollUpdateInfo
{
132 gfx::Vector2d scroll_delta
;
136 struct CC_EXPORT ScrollAndScaleSet
{
138 ~ScrollAndScaleSet();
140 std::vector
<LayerTreeHostCommon::ScrollUpdateInfo
> scrolls
;
141 float page_scale_delta
;
144 template <typename LayerType
>
145 bool LayerTreeHostCommon::RenderSurfaceContributesToTarget(
147 int target_surface_layer_id
) {
148 // A layer will either contribute its own content, or its render surface's
149 // content, to the target surface. The layer contributes its surface's content
150 // when both the following are true:
151 // (1) The layer actually has a render surface, and
152 // (2) The layer's render surface is not the same as the target surface.
154 // Otherwise, the layer just contributes itself to the target surface.
156 return layer
->render_surface() && layer
->id() != target_surface_layer_id
;
159 template <typename LayerType
>
160 LayerType
* LayerTreeHostCommon::FindLayerInSubtree(LayerType
* root_layer
,
165 if (root_layer
->id() == layer_id
)
168 if (root_layer
->mask_layer() && root_layer
->mask_layer()->id() == layer_id
)
169 return root_layer
->mask_layer();
171 if (root_layer
->replica_layer() &&
172 root_layer
->replica_layer()->id() == layer_id
)
173 return root_layer
->replica_layer();
175 for (size_t i
= 0; i
< root_layer
->children().size(); ++i
) {
176 if (LayerType
* found
= FindLayerInSubtree(
177 get_layer_as_raw_ptr(root_layer
->children(), i
), layer_id
))
183 template <typename LayerType
>
184 void LayerTreeHostCommon::CallFunctionForSubtree(
185 LayerType
* root_layer
,
186 const base::Callback
<void(LayerType
* layer
)>& function
) {
187 function
.Run(root_layer
);
189 if (LayerType
* mask_layer
= root_layer
->mask_layer())
190 function
.Run(mask_layer
);
191 if (LayerType
* replica_layer
= root_layer
->replica_layer()) {
192 function
.Run(replica_layer
);
193 if (LayerType
* mask_layer
= replica_layer
->mask_layer())
194 function
.Run(mask_layer
);
197 for (size_t i
= 0; i
< root_layer
->children().size(); ++i
) {
198 CallFunctionForSubtree(get_layer_as_raw_ptr(root_layer
->children(), i
),
203 template <typename LayerType
, typename RenderSurfaceLayerListType
>
204 LayerTreeHostCommon::CalcDrawPropsInputsForTesting
<LayerType
,
205 RenderSurfaceLayerListType
>::
206 CalcDrawPropsInputsForTesting(
207 LayerType
* root_layer
,
208 const gfx::Size
& device_viewport_size
,
209 const gfx::Transform
& device_transform
,
210 RenderSurfaceLayerListType
* render_surface_layer_list
)
211 : CalcDrawPropsInputs
<LayerType
, RenderSurfaceLayerListType
>(
213 device_viewport_size
,
218 std::numeric_limits
<int>::max() / 2,
222 render_surface_layer_list
,
225 DCHECK(render_surface_layer_list
);
228 template <typename LayerType
, typename RenderSurfaceLayerListType
>
229 LayerTreeHostCommon::CalcDrawPropsInputsForTesting
<LayerType
,
230 RenderSurfaceLayerListType
>::
231 CalcDrawPropsInputsForTesting(
232 LayerType
* root_layer
,
233 const gfx::Size
& device_viewport_size
,
234 RenderSurfaceLayerListType
* render_surface_layer_list
)
235 : CalcDrawPropsInputs
<LayerType
, RenderSurfaceLayerListType
>(
237 device_viewport_size
,
242 std::numeric_limits
<int>::max() / 2,
246 render_surface_layer_list
,
249 DCHECK(render_surface_layer_list
);
254 #endif // CC_TREES_LAYER_TREE_HOST_COMMON_H_