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.
4
5 #ifndef CC_TREES_LAYER_TREE_HOST_COMMON_H_
6 #define CC_TREES_LAYER_TREE_HOST_COMMON_H_
7
8 #include <limits>
9 #include <vector>
10
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"
19
20 namespace cc {
21
22 class LayerImpl;
23 class Layer;
24
25 class CC_EXPORT LayerTreeHostCommon {
26 public:
27 static gfx::Rect CalculateVisibleRect(const gfx::Rect& target_surface_rect,
28 const gfx::Rect& layer_bound_rect,
29 const gfx::Transform& transform);
30
31 template <typename LayerType, typename RenderSurfaceLayerListType>
32 struct CalcDrawPropsInputs {
33 public:
CalcDrawPropsInputsCalcDrawPropsInputs34 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,
40 int max_texture_size,
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) {}
59
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;
66 int max_texture_size;
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;
72 };
73
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);
86
87 private:
88 const gfx::Transform identity_transform_;
89 };
90
91 typedef CalcDrawPropsInputs<Layer, RenderSurfaceLayerList>
92 CalcDrawPropsMainInputs;
93 typedef CalcDrawPropsInputsForTesting<Layer, RenderSurfaceLayerList>
94 CalcDrawPropsMainInputsForTesting;
95 static void CalculateDrawProperties(CalcDrawPropsMainInputs* inputs);
96
97 typedef CalcDrawPropsInputs<LayerImpl, LayerImplList> CalcDrawPropsImplInputs;
98 typedef CalcDrawPropsInputsForTesting<LayerImpl, LayerImplList>
99 CalcDrawPropsImplInputsForTesting;
100 static void CalculateDrawProperties(CalcDrawPropsImplInputs* inputs);
101
102 template <typename LayerType>
103 static bool RenderSurfaceContributesToTarget(LayerType*,
104 int target_surface_layer_id);
105
106 template <typename LayerType>
107 static void CallFunctionForSubtree(
108 LayerType* root_layer,
109 const base::Callback<void(LayerType* layer)>& function);
110
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);
115
get_layer_as_raw_ptr(const LayerList & layers,size_t index)116 static Layer* get_layer_as_raw_ptr(const LayerList& layers, size_t index) {
117 return layers[index].get();
118 }
119
get_layer_as_raw_ptr(const OwnedLayerImplList & layers,size_t index)120 static LayerImpl* get_layer_as_raw_ptr(const OwnedLayerImplList& layers,
121 size_t index) {
122 return layers[index];
123 }
124
get_layer_as_raw_ptr(const LayerImplList & layers,size_t index)125 static LayerImpl* get_layer_as_raw_ptr(const LayerImplList& layers,
126 size_t index) {
127 return layers[index];
128 }
129
130 struct ScrollUpdateInfo {
131 int layer_id;
132 gfx::Vector2d scroll_delta;
133 };
134 };
135
136 struct CC_EXPORT ScrollAndScaleSet {
137 ScrollAndScaleSet();
138 ~ScrollAndScaleSet();
139
140 std::vector<LayerTreeHostCommon::ScrollUpdateInfo> scrolls;
141 float page_scale_delta;
142 };
143
144 template <typename LayerType>
RenderSurfaceContributesToTarget(LayerType * layer,int target_surface_layer_id)145 bool LayerTreeHostCommon::RenderSurfaceContributesToTarget(
146 LayerType* layer,
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.
153 //
154 // Otherwise, the layer just contributes itself to the target surface.
155
156 return layer->render_surface() && layer->id() != target_surface_layer_id;
157 }
158
159 template <typename LayerType>
FindLayerInSubtree(LayerType * root_layer,int layer_id)160 LayerType* LayerTreeHostCommon::FindLayerInSubtree(LayerType* root_layer,
161 int layer_id) {
162 if (!root_layer)
163 return NULL;
164
165 if (root_layer->id() == layer_id)
166 return root_layer;
167
168 if (root_layer->mask_layer() && root_layer->mask_layer()->id() == layer_id)
169 return root_layer->mask_layer();
170
171 if (root_layer->replica_layer() &&
172 root_layer->replica_layer()->id() == layer_id)
173 return root_layer->replica_layer();
174
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))
178 return found;
179 }
180 return NULL;
181 }
182
183 template <typename LayerType>
CallFunctionForSubtree(LayerType * root_layer,const base::Callback<void (LayerType * layer)> & function)184 void LayerTreeHostCommon::CallFunctionForSubtree(
185 LayerType* root_layer,
186 const base::Callback<void(LayerType* layer)>& function) {
187 function.Run(root_layer);
188
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);
195 }
196
197 for (size_t i = 0; i < root_layer->children().size(); ++i) {
198 CallFunctionForSubtree(get_layer_as_raw_ptr(root_layer->children(), i),
199 function);
200 }
201 }
202
203 template <typename LayerType, typename RenderSurfaceLayerListType>
204 LayerTreeHostCommon::CalcDrawPropsInputsForTesting<LayerType,
205 RenderSurfaceLayerListType>::
CalcDrawPropsInputsForTesting(LayerType * root_layer,const gfx::Size & device_viewport_size,const gfx::Transform & device_transform,RenderSurfaceLayerListType * render_surface_layer_list)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>(
212 root_layer,
213 device_viewport_size,
214 device_transform,
215 1.f,
216 1.f,
217 NULL,
218 std::numeric_limits<int>::max() / 2,
219 false,
220 true,
221 false,
222 render_surface_layer_list,
223 0) {
224 DCHECK(root_layer);
225 DCHECK(render_surface_layer_list);
226 }
227
228 template <typename LayerType, typename RenderSurfaceLayerListType>
229 LayerTreeHostCommon::CalcDrawPropsInputsForTesting<LayerType,
230 RenderSurfaceLayerListType>::
CalcDrawPropsInputsForTesting(LayerType * root_layer,const gfx::Size & device_viewport_size,RenderSurfaceLayerListType * render_surface_layer_list)231 CalcDrawPropsInputsForTesting(
232 LayerType* root_layer,
233 const gfx::Size& device_viewport_size,
234 RenderSurfaceLayerListType* render_surface_layer_list)
235 : CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType>(
236 root_layer,
237 device_viewport_size,
238 identity_transform_,
239 1.f,
240 1.f,
241 NULL,
242 std::numeric_limits<int>::max() / 2,
243 false,
244 true,
245 false,
246 render_surface_layer_list,
247 0) {
248 DCHECK(root_layer);
249 DCHECK(render_surface_layer_list);
250 }
251
252 } // namespace cc
253
254 #endif // CC_TREES_LAYER_TREE_HOST_COMMON_H_
255