Add ICU message format support
[chromium-blink-merge.git] / cc / trees / layer_tree_host_common.h
blobbf62111acbe70039cb9b1afa5f1348adae5175d3
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_
8 #include <limits>
9 #include <vector>
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"
21 namespace cc {
23 class LayerImpl;
24 class Layer;
25 class SwapPromise;
26 class PropertyTrees;
28 class CC_EXPORT LayerTreeHostCommon {
29 public:
30 static gfx::Rect CalculateVisibleRect(const gfx::Rect& target_surface_rect,
31 const gfx::Rect& layer_bound_rect,
32 const gfx::Transform& transform);
34 template <typename LayerType, typename RenderSurfaceLayerListType>
35 struct CalcDrawPropsInputs {
36 public:
37 CalcDrawPropsInputs(LayerType* root_layer,
38 const gfx::Size& device_viewport_size,
39 const gfx::Transform& device_transform,
40 float device_scale_factor,
41 float page_scale_factor,
42 const LayerType* page_scale_layer,
43 const LayerType* inner_viewport_scroll_layer,
44 const LayerType* outer_viewport_scroll_layer,
45 const gfx::Vector2dF& elastic_overscroll,
46 const LayerType* elastic_overscroll_application_layer,
47 int max_texture_size,
48 bool can_use_lcd_text,
49 bool layers_always_allowed_lcd_text,
50 bool can_render_to_separate_surface,
51 bool can_adjust_raster_scales,
52 bool verify_property_trees,
53 RenderSurfaceLayerListType* render_surface_layer_list,
54 int current_render_surface_layer_list_id,
55 PropertyTrees* property_trees)
56 : root_layer(root_layer),
57 device_viewport_size(device_viewport_size),
58 device_transform(device_transform),
59 device_scale_factor(device_scale_factor),
60 page_scale_factor(page_scale_factor),
61 page_scale_layer(page_scale_layer),
62 inner_viewport_scroll_layer(inner_viewport_scroll_layer),
63 outer_viewport_scroll_layer(outer_viewport_scroll_layer),
64 elastic_overscroll(elastic_overscroll),
65 elastic_overscroll_application_layer(
66 elastic_overscroll_application_layer),
67 max_texture_size(max_texture_size),
68 can_use_lcd_text(can_use_lcd_text),
69 layers_always_allowed_lcd_text(layers_always_allowed_lcd_text),
70 can_render_to_separate_surface(can_render_to_separate_surface),
71 can_adjust_raster_scales(can_adjust_raster_scales),
72 verify_property_trees(verify_property_trees),
73 render_surface_layer_list(render_surface_layer_list),
74 current_render_surface_layer_list_id(
75 current_render_surface_layer_list_id),
76 property_trees(property_trees) {}
78 LayerType* root_layer;
79 gfx::Size device_viewport_size;
80 gfx::Transform device_transform;
81 float device_scale_factor;
82 float page_scale_factor;
83 const LayerType* page_scale_layer;
84 const LayerType* inner_viewport_scroll_layer;
85 const LayerType* outer_viewport_scroll_layer;
86 gfx::Vector2dF elastic_overscroll;
87 const LayerType* elastic_overscroll_application_layer;
88 int max_texture_size;
89 bool can_use_lcd_text;
90 bool layers_always_allowed_lcd_text;
91 bool can_render_to_separate_surface;
92 bool can_adjust_raster_scales;
93 bool verify_property_trees;
94 RenderSurfaceLayerListType* render_surface_layer_list;
95 int current_render_surface_layer_list_id;
96 PropertyTrees* property_trees;
99 template <typename LayerType, typename RenderSurfaceLayerListType>
100 struct CalcDrawPropsInputsForTesting
101 : public CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType> {
102 CalcDrawPropsInputsForTesting(
103 LayerType* root_layer,
104 const gfx::Size& device_viewport_size,
105 const gfx::Transform& device_transform,
106 RenderSurfaceLayerListType* render_surface_layer_list);
107 CalcDrawPropsInputsForTesting(
108 LayerType* root_layer,
109 const gfx::Size& device_viewport_size,
110 RenderSurfaceLayerListType* render_surface_layer_list);
113 typedef CalcDrawPropsInputs<Layer, RenderSurfaceLayerList>
114 CalcDrawPropsMainInputs;
115 typedef CalcDrawPropsInputsForTesting<Layer, RenderSurfaceLayerList>
116 CalcDrawPropsMainInputsForTesting;
117 static void UpdateRenderSurfaces(Layer* root_layer,
118 bool can_render_to_separate_surface,
119 const gfx::Transform& transform,
120 bool preserves_2d_axis_alignment);
121 static void UpdateRenderSurface(Layer* layer,
122 bool can_render_to_separate_surface,
123 gfx::Transform* transform,
124 bool* animation_preserves_axis_alignment);
125 static void CalculateDrawProperties(CalcDrawPropsMainInputs* inputs);
126 static void PreCalculateMetaInformation(Layer* root_layer);
127 static void PreCalculateMetaInformationForTesting(LayerImpl* root_layer);
128 static void PreCalculateMetaInformationForTesting(Layer* root_layer);
130 typedef CalcDrawPropsInputs<LayerImpl, LayerImplList> CalcDrawPropsImplInputs;
131 typedef CalcDrawPropsInputsForTesting<LayerImpl, LayerImplList>
132 CalcDrawPropsImplInputsForTesting;
133 static void CalculateDrawProperties(CalcDrawPropsImplInputs* inputs);
134 static void CalculateDrawProperties(
135 CalcDrawPropsImplInputsForTesting* inputs);
137 template <typename LayerType>
138 static bool RenderSurfaceContributesToTarget(LayerType*,
139 int target_surface_layer_id);
141 template <typename LayerType, typename Function>
142 static void CallFunctionForSubtree(LayerType* layer,
143 const Function& function);
145 // Returns a layer with the given id if one exists in the subtree starting
146 // from the given root layer (including mask and replica layers).
147 template <typename LayerType>
148 static LayerType* FindLayerInSubtree(LayerType* root_layer, int layer_id);
150 static Layer* get_layer_as_raw_ptr(const LayerList& layers, size_t index) {
151 return layers[index].get();
154 static LayerImpl* get_layer_as_raw_ptr(const OwnedLayerImplList& layers,
155 size_t index) {
156 return layers[index];
159 static LayerImpl* get_layer_as_raw_ptr(const LayerImplList& layers,
160 size_t index) {
161 return layers[index];
164 struct ScrollUpdateInfo {
165 int layer_id;
166 // TODO(miletus): Use ScrollOffset once LayerTreeHost/Blink fully supports
167 // franctional scroll offset.
168 gfx::Vector2d scroll_delta;
172 struct CC_EXPORT ScrollAndScaleSet {
173 ScrollAndScaleSet();
174 ~ScrollAndScaleSet();
176 std::vector<LayerTreeHostCommon::ScrollUpdateInfo> scrolls;
177 float page_scale_delta;
178 gfx::Vector2dF elastic_overscroll_delta;
179 float top_controls_delta;
180 ScopedPtrVector<SwapPromise> swap_promises;
183 template <typename LayerType>
184 bool LayerTreeHostCommon::RenderSurfaceContributesToTarget(
185 LayerType* layer,
186 int target_surface_layer_id) {
187 // A layer will either contribute its own content, or its render surface's
188 // content, to the target surface. The layer contributes its surface's content
189 // when both the following are true:
190 // (1) The layer actually has a render surface and rendering into that
191 // surface, and
192 // (2) The layer's render surface is not the same as the target surface.
194 // Otherwise, the layer just contributes itself to the target surface.
196 return layer->render_target() == layer &&
197 layer->id() != target_surface_layer_id;
200 template <typename LayerType>
201 LayerType* LayerTreeHostCommon::FindLayerInSubtree(LayerType* root_layer,
202 int layer_id) {
203 if (!root_layer)
204 return NULL;
206 if (root_layer->id() == layer_id)
207 return root_layer;
209 if (root_layer->mask_layer() && root_layer->mask_layer()->id() == layer_id)
210 return root_layer->mask_layer();
212 if (root_layer->replica_layer() &&
213 root_layer->replica_layer()->id() == layer_id)
214 return root_layer->replica_layer();
216 for (size_t i = 0; i < root_layer->children().size(); ++i) {
217 if (LayerType* found = FindLayerInSubtree(
218 get_layer_as_raw_ptr(root_layer->children(), i), layer_id))
219 return found;
221 return NULL;
224 template <typename LayerType, typename Function>
225 void LayerTreeHostCommon::CallFunctionForSubtree(LayerType* layer,
226 const Function& function) {
227 function(layer);
229 if (LayerType* mask_layer = layer->mask_layer())
230 function(mask_layer);
231 if (LayerType* replica_layer = layer->replica_layer()) {
232 function(replica_layer);
233 if (LayerType* mask_layer = replica_layer->mask_layer())
234 function(mask_layer);
237 for (size_t i = 0; i < layer->children().size(); ++i) {
238 CallFunctionForSubtree(get_layer_as_raw_ptr(layer->children(), i),
239 function);
243 CC_EXPORT PropertyTrees* GetPropertyTrees(Layer* layer);
244 CC_EXPORT PropertyTrees* GetPropertyTrees(LayerImpl* layer);
246 template <typename LayerType, typename RenderSurfaceLayerListType>
247 LayerTreeHostCommon::CalcDrawPropsInputsForTesting<LayerType,
248 RenderSurfaceLayerListType>::
249 CalcDrawPropsInputsForTesting(
250 LayerType* root_layer,
251 const gfx::Size& device_viewport_size,
252 const gfx::Transform& device_transform,
253 RenderSurfaceLayerListType* render_surface_layer_list)
254 : CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType>(
255 root_layer,
256 device_viewport_size,
257 device_transform,
258 1.f,
259 1.f,
260 NULL,
261 NULL,
262 NULL,
263 gfx::Vector2dF(),
264 NULL,
265 std::numeric_limits<int>::max() / 2,
266 false,
267 false,
268 true,
269 false,
270 true,
271 render_surface_layer_list,
273 GetPropertyTrees(root_layer)) {
274 DCHECK(root_layer);
275 DCHECK(render_surface_layer_list);
278 template <typename LayerType, typename RenderSurfaceLayerListType>
279 LayerTreeHostCommon::CalcDrawPropsInputsForTesting<LayerType,
280 RenderSurfaceLayerListType>::
281 CalcDrawPropsInputsForTesting(
282 LayerType* root_layer,
283 const gfx::Size& device_viewport_size,
284 RenderSurfaceLayerListType* render_surface_layer_list)
285 : CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType>(
286 root_layer,
287 device_viewport_size,
288 gfx::Transform(),
289 1.f,
290 1.f,
291 NULL,
292 NULL,
293 NULL,
294 gfx::Vector2dF(),
295 NULL,
296 std::numeric_limits<int>::max() / 2,
297 false,
298 false,
299 true,
300 false,
301 true,
302 render_surface_layer_list,
304 GetPropertyTrees(root_layer)) {
305 DCHECK(root_layer);
306 DCHECK(render_surface_layer_list);
309 } // namespace cc
311 #endif // CC_TREES_LAYER_TREE_HOST_COMMON_H_