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"
26 class CC_EXPORT LayerTreeHostCommon
{
28 static gfx::Rect
CalculateVisibleRect(const gfx::Rect
& target_surface_rect
,
29 const gfx::Rect
& layer_bound_rect
,
30 const gfx::Transform
& transform
);
32 template <typename LayerType
, typename RenderSurfaceLayerListType
>
33 struct CalcDrawPropsInputs
{
35 CalcDrawPropsInputs(LayerType
* root_layer
,
36 const gfx::Size
& device_viewport_size
,
37 const gfx::Transform
& device_transform
,
38 float device_scale_factor
,
39 float page_scale_factor
,
40 const LayerType
* page_scale_application_layer
,
42 bool can_use_lcd_text
,
43 bool can_render_to_separate_surface
,
44 bool can_adjust_raster_scales
,
45 RenderSurfaceLayerListType
* render_surface_layer_list
,
46 int current_render_surface_layer_list_id
)
47 : root_layer(root_layer
),
48 device_viewport_size(device_viewport_size
),
49 device_transform(device_transform
),
50 device_scale_factor(device_scale_factor
),
51 page_scale_factor(page_scale_factor
),
52 page_scale_application_layer(page_scale_application_layer
),
53 max_texture_size(max_texture_size
),
54 can_use_lcd_text(can_use_lcd_text
),
55 can_render_to_separate_surface(can_render_to_separate_surface
),
56 can_adjust_raster_scales(can_adjust_raster_scales
),
57 render_surface_layer_list(render_surface_layer_list
),
58 current_render_surface_layer_list_id(
59 current_render_surface_layer_list_id
) {}
61 LayerType
* root_layer
;
62 gfx::Size device_viewport_size
;
63 const gfx::Transform
& device_transform
;
64 float device_scale_factor
;
65 float page_scale_factor
;
66 const LayerType
* page_scale_application_layer
;
68 bool can_use_lcd_text
;
69 bool can_render_to_separate_surface
;
70 bool can_adjust_raster_scales
;
71 RenderSurfaceLayerListType
* render_surface_layer_list
;
72 int current_render_surface_layer_list_id
;
75 template <typename LayerType
, typename RenderSurfaceLayerListType
>
76 struct CalcDrawPropsInputsForTesting
77 : public CalcDrawPropsInputs
<LayerType
, RenderSurfaceLayerListType
> {
78 CalcDrawPropsInputsForTesting(
79 LayerType
* root_layer
,
80 const gfx::Size
& device_viewport_size
,
81 const gfx::Transform
& device_transform
,
82 RenderSurfaceLayerListType
* render_surface_layer_list
);
83 CalcDrawPropsInputsForTesting(
84 LayerType
* root_layer
,
85 const gfx::Size
& device_viewport_size
,
86 RenderSurfaceLayerListType
* render_surface_layer_list
);
89 const gfx::Transform identity_transform_
;
92 typedef CalcDrawPropsInputs
<Layer
, RenderSurfaceLayerList
>
93 CalcDrawPropsMainInputs
;
94 typedef CalcDrawPropsInputsForTesting
<Layer
, RenderSurfaceLayerList
>
95 CalcDrawPropsMainInputsForTesting
;
96 static void CalculateDrawProperties(CalcDrawPropsMainInputs
* inputs
);
98 typedef CalcDrawPropsInputs
<LayerImpl
, LayerImplList
> CalcDrawPropsImplInputs
;
99 typedef CalcDrawPropsInputsForTesting
<LayerImpl
, LayerImplList
>
100 CalcDrawPropsImplInputsForTesting
;
101 static void CalculateDrawProperties(CalcDrawPropsImplInputs
* inputs
);
103 template <typename LayerType
>
104 static bool RenderSurfaceContributesToTarget(LayerType
*,
105 int target_surface_layer_id
);
107 template <typename LayerType
>
108 static void CallFunctionForSubtree(
109 LayerType
* root_layer
,
110 const base::Callback
<void(LayerType
* layer
)>& function
);
112 // Returns a layer with the given id if one exists in the subtree starting
113 // from the given root layer (including mask and replica layers).
114 template <typename LayerType
>
115 static LayerType
* FindLayerInSubtree(LayerType
* root_layer
, int layer_id
);
117 static Layer
* get_layer_as_raw_ptr(const LayerList
& layers
, size_t index
) {
118 return layers
[index
].get();
121 static LayerImpl
* get_layer_as_raw_ptr(const OwnedLayerImplList
& layers
,
123 return layers
[index
];
126 static LayerImpl
* get_layer_as_raw_ptr(const LayerImplList
& layers
,
128 return layers
[index
];
131 struct ScrollUpdateInfo
{
133 gfx::Vector2d scroll_delta
;
137 struct CC_EXPORT ScrollAndScaleSet
{
139 ~ScrollAndScaleSet();
141 std::vector
<LayerTreeHostCommon::ScrollUpdateInfo
> scrolls
;
142 float page_scale_delta
;
143 ScopedPtrVector
<SwapPromise
> swap_promises
;
146 template <typename LayerType
>
147 bool LayerTreeHostCommon::RenderSurfaceContributesToTarget(
149 int target_surface_layer_id
) {
150 // A layer will either contribute its own content, or its render surface's
151 // content, to the target surface. The layer contributes its surface's content
152 // when both the following are true:
153 // (1) The layer actually has a render surface, and
154 // (2) The layer's render surface is not the same as the target surface.
156 // Otherwise, the layer just contributes itself to the target surface.
158 return layer
->render_surface() && layer
->id() != target_surface_layer_id
;
161 template <typename LayerType
>
162 LayerType
* LayerTreeHostCommon::FindLayerInSubtree(LayerType
* root_layer
,
167 if (root_layer
->id() == layer_id
)
170 if (root_layer
->mask_layer() && root_layer
->mask_layer()->id() == layer_id
)
171 return root_layer
->mask_layer();
173 if (root_layer
->replica_layer() &&
174 root_layer
->replica_layer()->id() == layer_id
)
175 return root_layer
->replica_layer();
177 for (size_t i
= 0; i
< root_layer
->children().size(); ++i
) {
178 if (LayerType
* found
= FindLayerInSubtree(
179 get_layer_as_raw_ptr(root_layer
->children(), i
), layer_id
))
185 template <typename LayerType
>
186 void LayerTreeHostCommon::CallFunctionForSubtree(
187 LayerType
* root_layer
,
188 const base::Callback
<void(LayerType
* layer
)>& function
) {
189 function
.Run(root_layer
);
191 if (LayerType
* mask_layer
= root_layer
->mask_layer())
192 function
.Run(mask_layer
);
193 if (LayerType
* replica_layer
= root_layer
->replica_layer()) {
194 function
.Run(replica_layer
);
195 if (LayerType
* mask_layer
= replica_layer
->mask_layer())
196 function
.Run(mask_layer
);
199 for (size_t i
= 0; i
< root_layer
->children().size(); ++i
) {
200 CallFunctionForSubtree(get_layer_as_raw_ptr(root_layer
->children(), i
),
205 template <typename LayerType
, typename RenderSurfaceLayerListType
>
206 LayerTreeHostCommon::CalcDrawPropsInputsForTesting
<LayerType
,
207 RenderSurfaceLayerListType
>::
208 CalcDrawPropsInputsForTesting(
209 LayerType
* root_layer
,
210 const gfx::Size
& device_viewport_size
,
211 const gfx::Transform
& device_transform
,
212 RenderSurfaceLayerListType
* render_surface_layer_list
)
213 : CalcDrawPropsInputs
<LayerType
, RenderSurfaceLayerListType
>(
215 device_viewport_size
,
220 std::numeric_limits
<int>::max() / 2,
224 render_surface_layer_list
,
227 DCHECK(render_surface_layer_list
);
230 template <typename LayerType
, typename RenderSurfaceLayerListType
>
231 LayerTreeHostCommon::CalcDrawPropsInputsForTesting
<LayerType
,
232 RenderSurfaceLayerListType
>::
233 CalcDrawPropsInputsForTesting(
234 LayerType
* root_layer
,
235 const gfx::Size
& device_viewport_size
,
236 RenderSurfaceLayerListType
* render_surface_layer_list
)
237 : CalcDrawPropsInputs
<LayerType
, RenderSurfaceLayerListType
>(
239 device_viewport_size
,
244 std::numeric_limits
<int>::max() / 2,
248 render_surface_layer_list
,
251 DCHECK(render_surface_layer_list
);
256 #endif // CC_TREES_LAYER_TREE_HOST_COMMON_H_