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/geometry/rect.h"
17 #include "ui/gfx/geometry/vector2d.h"
18 #include "ui/gfx/transform.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
,
41 const gfx::Vector2dF
& elastic_overscroll
,
42 const LayerType
* elastic_overscroll_application_layer
,
44 bool can_use_lcd_text
,
45 bool layers_always_allowed_lcd_text
,
46 bool can_render_to_separate_surface
,
47 bool can_adjust_raster_scales
,
48 bool verify_property_trees
,
49 RenderSurfaceLayerListType
* render_surface_layer_list
,
50 int current_render_surface_layer_list_id
)
51 : root_layer(root_layer
),
52 device_viewport_size(device_viewport_size
),
53 device_transform(device_transform
),
54 device_scale_factor(device_scale_factor
),
55 page_scale_factor(page_scale_factor
),
56 page_scale_application_layer(page_scale_application_layer
),
57 elastic_overscroll(elastic_overscroll
),
58 elastic_overscroll_application_layer(
59 elastic_overscroll_application_layer
),
60 max_texture_size(max_texture_size
),
61 can_use_lcd_text(can_use_lcd_text
),
62 layers_always_allowed_lcd_text(layers_always_allowed_lcd_text
),
63 can_render_to_separate_surface(can_render_to_separate_surface
),
64 can_adjust_raster_scales(can_adjust_raster_scales
),
65 verify_property_trees(verify_property_trees
),
66 render_surface_layer_list(render_surface_layer_list
),
67 current_render_surface_layer_list_id(
68 current_render_surface_layer_list_id
) {}
70 LayerType
* root_layer
;
71 gfx::Size device_viewport_size
;
72 const gfx::Transform
& device_transform
;
73 float device_scale_factor
;
74 float page_scale_factor
;
75 const LayerType
* page_scale_application_layer
;
76 gfx::Vector2dF elastic_overscroll
;
77 const LayerType
* elastic_overscroll_application_layer
;
79 bool can_use_lcd_text
;
80 bool layers_always_allowed_lcd_text
;
81 bool can_render_to_separate_surface
;
82 bool can_adjust_raster_scales
;
83 bool verify_property_trees
;
84 RenderSurfaceLayerListType
* render_surface_layer_list
;
85 int current_render_surface_layer_list_id
;
88 template <typename LayerType
, typename RenderSurfaceLayerListType
>
89 struct CalcDrawPropsInputsForTesting
90 : public CalcDrawPropsInputs
<LayerType
, RenderSurfaceLayerListType
> {
91 CalcDrawPropsInputsForTesting(
92 LayerType
* root_layer
,
93 const gfx::Size
& device_viewport_size
,
94 const gfx::Transform
& device_transform
,
95 RenderSurfaceLayerListType
* render_surface_layer_list
);
96 CalcDrawPropsInputsForTesting(
97 LayerType
* root_layer
,
98 const gfx::Size
& device_viewport_size
,
99 RenderSurfaceLayerListType
* render_surface_layer_list
);
102 const gfx::Transform identity_transform_
;
105 typedef CalcDrawPropsInputs
<Layer
, RenderSurfaceLayerList
>
106 CalcDrawPropsMainInputs
;
107 typedef CalcDrawPropsInputsForTesting
<Layer
, RenderSurfaceLayerList
>
108 CalcDrawPropsMainInputsForTesting
;
109 static void UpdateRenderSurfaces(Layer
* root_layer
,
110 bool can_render_to_separate_surface
,
111 const gfx::Transform
& transform
,
112 bool preserves_2d_axis_alignment
);
113 static void UpdateRenderSurface(Layer
* layer
,
114 bool can_render_to_separate_surface
,
115 gfx::Transform
* transform
,
116 bool* animation_preserves_axis_alignment
);
117 static void CalculateDrawProperties(CalcDrawPropsMainInputs
* inputs
);
119 typedef CalcDrawPropsInputs
<LayerImpl
, LayerImplList
> CalcDrawPropsImplInputs
;
120 typedef CalcDrawPropsInputsForTesting
<LayerImpl
, LayerImplList
>
121 CalcDrawPropsImplInputsForTesting
;
122 static void CalculateDrawProperties(CalcDrawPropsImplInputs
* inputs
);
124 template <typename LayerType
>
125 static bool RenderSurfaceContributesToTarget(LayerType
*,
126 int target_surface_layer_id
);
128 template <typename LayerType
>
129 static void CallFunctionForSubtree(
130 LayerType
* root_layer
,
131 const base::Callback
<void(LayerType
* layer
)>& function
);
133 // Returns a layer with the given id if one exists in the subtree starting
134 // from the given root layer (including mask and replica layers).
135 template <typename LayerType
>
136 static LayerType
* FindLayerInSubtree(LayerType
* root_layer
, int layer_id
);
138 static Layer
* get_layer_as_raw_ptr(const LayerList
& layers
, size_t index
) {
139 return layers
[index
].get();
142 static LayerImpl
* get_layer_as_raw_ptr(const OwnedLayerImplList
& layers
,
144 return layers
[index
];
147 static LayerImpl
* get_layer_as_raw_ptr(const LayerImplList
& layers
,
149 return layers
[index
];
152 struct ScrollUpdateInfo
{
154 // TODO(miletus) : Use ScrollOffset once LayerTreeHost/Blink fully supports
155 // franctional scroll offset.
156 gfx::Vector2d scroll_delta
;
160 struct CC_EXPORT ScrollAndScaleSet
{
162 ~ScrollAndScaleSet();
164 std::vector
<LayerTreeHostCommon::ScrollUpdateInfo
> scrolls
;
165 float page_scale_delta
;
166 gfx::Vector2dF elastic_overscroll_delta
;
167 float top_controls_delta
;
168 ScopedPtrVector
<SwapPromise
> swap_promises
;
171 template <typename LayerType
>
172 bool LayerTreeHostCommon::RenderSurfaceContributesToTarget(
174 int target_surface_layer_id
) {
175 // A layer will either contribute its own content, or its render surface's
176 // content, to the target surface. The layer contributes its surface's content
177 // when both the following are true:
178 // (1) The layer actually has a render surface, and
179 // (2) The layer's render surface is not the same as the target surface.
181 // Otherwise, the layer just contributes itself to the target surface.
183 return layer
->render_surface() && layer
->id() != target_surface_layer_id
;
186 template <typename LayerType
>
187 LayerType
* LayerTreeHostCommon::FindLayerInSubtree(LayerType
* root_layer
,
192 if (root_layer
->id() == layer_id
)
195 if (root_layer
->mask_layer() && root_layer
->mask_layer()->id() == layer_id
)
196 return root_layer
->mask_layer();
198 if (root_layer
->replica_layer() &&
199 root_layer
->replica_layer()->id() == layer_id
)
200 return root_layer
->replica_layer();
202 for (size_t i
= 0; i
< root_layer
->children().size(); ++i
) {
203 if (LayerType
* found
= FindLayerInSubtree(
204 get_layer_as_raw_ptr(root_layer
->children(), i
), layer_id
))
210 template <typename LayerType
>
211 void LayerTreeHostCommon::CallFunctionForSubtree(
212 LayerType
* root_layer
,
213 const base::Callback
<void(LayerType
* layer
)>& function
) {
214 function
.Run(root_layer
);
216 if (LayerType
* mask_layer
= root_layer
->mask_layer())
217 function
.Run(mask_layer
);
218 if (LayerType
* replica_layer
= root_layer
->replica_layer()) {
219 function
.Run(replica_layer
);
220 if (LayerType
* mask_layer
= replica_layer
->mask_layer())
221 function
.Run(mask_layer
);
224 for (size_t i
= 0; i
< root_layer
->children().size(); ++i
) {
225 CallFunctionForSubtree(get_layer_as_raw_ptr(root_layer
->children(), i
),
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 const gfx::Transform
& device_transform
,
237 RenderSurfaceLayerListType
* render_surface_layer_list
)
238 : CalcDrawPropsInputs
<LayerType
, RenderSurfaceLayerListType
>(
240 device_viewport_size
,
247 std::numeric_limits
<int>::max() / 2,
253 render_surface_layer_list
,
256 DCHECK(render_surface_layer_list
);
259 template <typename LayerType
, typename RenderSurfaceLayerListType
>
260 LayerTreeHostCommon::CalcDrawPropsInputsForTesting
<LayerType
,
261 RenderSurfaceLayerListType
>::
262 CalcDrawPropsInputsForTesting(
263 LayerType
* root_layer
,
264 const gfx::Size
& device_viewport_size
,
265 RenderSurfaceLayerListType
* render_surface_layer_list
)
266 : CalcDrawPropsInputs
<LayerType
, RenderSurfaceLayerListType
>(
268 device_viewport_size
,
275 std::numeric_limits
<int>::max() / 2,
281 render_surface_layer_list
,
284 DCHECK(render_surface_layer_list
);
289 #endif // CC_TREES_LAYER_TREE_HOST_COMMON_H_