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 "cc/trees/property_tree.h"
17 #include "ui/gfx/geometry/rect.h"
18 #include "ui/gfx/geometry/vector2d.h"
19 #include "ui/gfx/transform.h"
28 class CC_EXPORT LayerTreeHostCommon
{
30 static gfx::Rect
CalculateVisibleRect(const gfx::Rect
& target_surface_rect
,
31 const gfx::Rect
& layer_bound_rect
,
32 const gfx::Transform
& transform
);
34 struct CC_EXPORT CalcDrawPropsMainInputs
{
36 CalcDrawPropsMainInputs(Layer
* root_layer
,
37 const gfx::Size
& device_viewport_size
,
38 const gfx::Transform
& device_transform
,
39 float device_scale_factor
,
40 float page_scale_factor
,
41 const Layer
* page_scale_layer
,
42 const Layer
* inner_viewport_scroll_layer
,
43 const Layer
* outer_viewport_scroll_layer
);
44 CalcDrawPropsMainInputs(Layer
* root_layer
,
45 const gfx::Size
& device_viewport_size
,
46 const gfx::Transform
& device_transform
);
47 CalcDrawPropsMainInputs(Layer
* root_layer
,
48 const gfx::Size
& device_viewport_size
);
50 gfx::Size device_viewport_size
;
51 gfx::Transform device_transform
;
52 float device_scale_factor
;
53 float page_scale_factor
;
54 const Layer
* page_scale_layer
;
55 const Layer
* inner_viewport_scroll_layer
;
56 const Layer
* outer_viewport_scroll_layer
;
59 struct CC_EXPORT CalcDrawPropsImplInputs
{
61 CalcDrawPropsImplInputs(
62 LayerImpl
* root_layer
,
63 const gfx::Size
& device_viewport_size
,
64 const gfx::Transform
& device_transform
,
65 float device_scale_factor
,
66 float page_scale_factor
,
67 const LayerImpl
* page_scale_layer
,
68 const LayerImpl
* inner_viewport_scroll_layer
,
69 const LayerImpl
* outer_viewport_scroll_layer
,
70 const gfx::Vector2dF
& elastic_overscroll
,
71 const LayerImpl
* elastic_overscroll_application_layer
,
73 bool can_use_lcd_text
,
74 bool layers_always_allowed_lcd_text
,
75 bool can_render_to_separate_surface
,
76 bool can_adjust_raster_scales
,
77 bool verify_property_trees
,
78 LayerImplList
* render_surface_layer_list
,
79 int current_render_surface_layer_list_id
,
80 PropertyTrees
* property_trees
);
82 LayerImpl
* root_layer
;
83 gfx::Size device_viewport_size
;
84 gfx::Transform device_transform
;
85 float device_scale_factor
;
86 float page_scale_factor
;
87 const LayerImpl
* page_scale_layer
;
88 const LayerImpl
* inner_viewport_scroll_layer
;
89 const LayerImpl
* outer_viewport_scroll_layer
;
90 gfx::Vector2dF elastic_overscroll
;
91 const LayerImpl
* elastic_overscroll_application_layer
;
93 bool can_use_lcd_text
;
94 bool layers_always_allowed_lcd_text
;
95 bool can_render_to_separate_surface
;
96 bool can_adjust_raster_scales
;
97 bool verify_property_trees
;
98 LayerImplList
* render_surface_layer_list
;
99 int current_render_surface_layer_list_id
;
100 PropertyTrees
* property_trees
;
103 struct CC_EXPORT CalcDrawPropsImplInputsForTesting
104 : public CalcDrawPropsImplInputs
{
105 CalcDrawPropsImplInputsForTesting(LayerImpl
* root_layer
,
106 const gfx::Size
& device_viewport_size
,
107 const gfx::Transform
& device_transform
,
108 LayerImplList
* render_surface_layer_list
);
109 CalcDrawPropsImplInputsForTesting(LayerImpl
* root_layer
,
110 const gfx::Size
& device_viewport_size
,
111 LayerImplList
* render_surface_layer_list
);
114 static void UpdateRenderSurfaces(Layer
* root_layer
,
115 bool can_render_to_separate_surface
,
116 const gfx::Transform
& transform
,
117 bool preserves_2d_axis_alignment
);
118 static void UpdateRenderSurface(Layer
* layer
,
119 bool can_render_to_separate_surface
,
120 gfx::Transform
* transform
,
121 bool* animation_preserves_axis_alignment
);
122 static void CalculateDrawProperties(CalcDrawPropsMainInputs
* inputs
);
123 static void PreCalculateMetaInformation(Layer
* root_layer
);
124 static void PreCalculateMetaInformationForTesting(LayerImpl
* root_layer
);
125 static void PreCalculateMetaInformationForTesting(Layer
* root_layer
);
127 static void CalculateDrawProperties(CalcDrawPropsImplInputs
* inputs
);
128 static void CalculateDrawProperties(
129 CalcDrawPropsImplInputsForTesting
* inputs
);
131 template <typename LayerType
>
132 static bool RenderSurfaceContributesToTarget(LayerType
*,
133 int target_surface_layer_id
);
135 template <typename LayerType
, typename Function
>
136 static void CallFunctionForSubtree(LayerType
* layer
,
137 const Function
& function
);
139 // Returns a layer with the given id if one exists in the subtree starting
140 // from the given root layer (including mask and replica layers).
141 template <typename LayerType
>
142 static LayerType
* FindLayerInSubtree(LayerType
* root_layer
, int layer_id
);
144 static Layer
* get_layer_as_raw_ptr(const LayerList
& layers
, size_t index
) {
145 return layers
[index
].get();
148 static LayerImpl
* get_layer_as_raw_ptr(const OwnedLayerImplList
& layers
,
150 return layers
[index
];
153 static LayerImpl
* get_layer_as_raw_ptr(const LayerImplList
& layers
,
155 return layers
[index
];
158 struct ScrollUpdateInfo
{
160 // TODO(miletus): Use ScrollOffset once LayerTreeHost/Blink fully supports
161 // franctional scroll offset.
162 gfx::Vector2d scroll_delta
;
166 struct CC_EXPORT ScrollAndScaleSet
{
168 ~ScrollAndScaleSet();
170 std::vector
<LayerTreeHostCommon::ScrollUpdateInfo
> scrolls
;
171 float page_scale_delta
;
172 gfx::Vector2dF elastic_overscroll_delta
;
173 float top_controls_delta
;
174 ScopedPtrVector
<SwapPromise
> swap_promises
;
177 template <typename LayerType
>
178 bool LayerTreeHostCommon::RenderSurfaceContributesToTarget(
180 int target_surface_layer_id
) {
181 // A layer will either contribute its own content, or its render surface's
182 // content, to the target surface. The layer contributes its surface's content
183 // when both the following are true:
184 // (1) The layer actually has a render surface and rendering into that
186 // (2) The layer's render surface is not the same as the target surface.
188 // Otherwise, the layer just contributes itself to the target surface.
190 return layer
->render_target() == layer
&&
191 layer
->id() != target_surface_layer_id
;
194 template <typename LayerType
>
195 LayerType
* LayerTreeHostCommon::FindLayerInSubtree(LayerType
* root_layer
,
200 if (root_layer
->id() == layer_id
)
203 if (root_layer
->mask_layer() && root_layer
->mask_layer()->id() == layer_id
)
204 return root_layer
->mask_layer();
206 if (root_layer
->replica_layer() &&
207 root_layer
->replica_layer()->id() == layer_id
)
208 return root_layer
->replica_layer();
210 for (size_t i
= 0; i
< root_layer
->children().size(); ++i
) {
211 if (LayerType
* found
= FindLayerInSubtree(
212 get_layer_as_raw_ptr(root_layer
->children(), i
), layer_id
))
218 template <typename LayerType
, typename Function
>
219 void LayerTreeHostCommon::CallFunctionForSubtree(LayerType
* layer
,
220 const Function
& function
) {
223 if (LayerType
* mask_layer
= layer
->mask_layer())
224 function(mask_layer
);
225 if (LayerType
* replica_layer
= layer
->replica_layer()) {
226 function(replica_layer
);
227 if (LayerType
* mask_layer
= replica_layer
->mask_layer())
228 function(mask_layer
);
231 for (size_t i
= 0; i
< layer
->children().size(); ++i
) {
232 CallFunctionForSubtree(get_layer_as_raw_ptr(layer
->children(), i
),
237 CC_EXPORT PropertyTrees
* GetPropertyTrees(Layer
* layer
);
238 CC_EXPORT PropertyTrees
* GetPropertyTrees(LayerImpl
* layer
);
242 #endif // CC_TREES_LAYER_TREE_HOST_COMMON_H_