File with simple block types.
[chromium-blink-merge.git] / cc / trees / layer_tree_host_common.h
blob349b341f6ba9783da9465a26f1af8f70c8af4fb4
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 "ui/gfx/geometry/rect.h"
17 #include "ui/gfx/geometry/vector2d.h"
18 #include "ui/gfx/transform.h"
20 namespace cc {
22 class LayerImpl;
23 class Layer;
24 class SwapPromise;
26 class CC_EXPORT LayerTreeHostCommon {
27 public:
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 {
34 public:
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 int max_texture_size,
42 bool can_use_lcd_text,
43 bool layers_always_allowed_lcd_text,
44 bool can_render_to_separate_surface,
45 bool can_adjust_raster_scales,
46 RenderSurfaceLayerListType* render_surface_layer_list,
47 int current_render_surface_layer_list_id)
48 : root_layer(root_layer),
49 device_viewport_size(device_viewport_size),
50 device_transform(device_transform),
51 device_scale_factor(device_scale_factor),
52 page_scale_factor(page_scale_factor),
53 page_scale_application_layer(page_scale_application_layer),
54 max_texture_size(max_texture_size),
55 can_use_lcd_text(can_use_lcd_text),
56 layers_always_allowed_lcd_text(layers_always_allowed_lcd_text),
57 can_render_to_separate_surface(can_render_to_separate_surface),
58 can_adjust_raster_scales(can_adjust_raster_scales),
59 render_surface_layer_list(render_surface_layer_list),
60 current_render_surface_layer_list_id(
61 current_render_surface_layer_list_id) {}
63 LayerType* root_layer;
64 gfx::Size device_viewport_size;
65 const gfx::Transform& device_transform;
66 float device_scale_factor;
67 float page_scale_factor;
68 const LayerType* page_scale_application_layer;
69 int max_texture_size;
70 bool can_use_lcd_text;
71 bool layers_always_allowed_lcd_text;
72 bool can_render_to_separate_surface;
73 bool can_adjust_raster_scales;
74 RenderSurfaceLayerListType* render_surface_layer_list;
75 int current_render_surface_layer_list_id;
78 template <typename LayerType, typename RenderSurfaceLayerListType>
79 struct CalcDrawPropsInputsForTesting
80 : public CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType> {
81 CalcDrawPropsInputsForTesting(
82 LayerType* root_layer,
83 const gfx::Size& device_viewport_size,
84 const gfx::Transform& device_transform,
85 RenderSurfaceLayerListType* render_surface_layer_list);
86 CalcDrawPropsInputsForTesting(
87 LayerType* root_layer,
88 const gfx::Size& device_viewport_size,
89 RenderSurfaceLayerListType* render_surface_layer_list);
91 private:
92 const gfx::Transform identity_transform_;
95 typedef CalcDrawPropsInputs<Layer, RenderSurfaceLayerList>
96 CalcDrawPropsMainInputs;
97 typedef CalcDrawPropsInputsForTesting<Layer, RenderSurfaceLayerList>
98 CalcDrawPropsMainInputsForTesting;
99 static void CalculateDrawProperties(CalcDrawPropsMainInputs* inputs);
101 typedef CalcDrawPropsInputs<LayerImpl, LayerImplList> CalcDrawPropsImplInputs;
102 typedef CalcDrawPropsInputsForTesting<LayerImpl, LayerImplList>
103 CalcDrawPropsImplInputsForTesting;
104 static void CalculateDrawProperties(CalcDrawPropsImplInputs* inputs);
106 template <typename LayerType>
107 static bool RenderSurfaceContributesToTarget(LayerType*,
108 int target_surface_layer_id);
110 template <typename LayerType>
111 static void CallFunctionForSubtree(
112 LayerType* root_layer,
113 const base::Callback<void(LayerType* layer)>& function);
115 // Returns a layer with the given id if one exists in the subtree starting
116 // from the given root layer (including mask and replica layers).
117 template <typename LayerType>
118 static LayerType* FindLayerInSubtree(LayerType* root_layer, int layer_id);
120 static Layer* get_layer_as_raw_ptr(const LayerList& layers, size_t index) {
121 return layers[index].get();
124 static LayerImpl* get_layer_as_raw_ptr(const OwnedLayerImplList& layers,
125 size_t index) {
126 return layers[index];
129 static LayerImpl* get_layer_as_raw_ptr(const LayerImplList& layers,
130 size_t index) {
131 return layers[index];
134 struct ScrollUpdateInfo {
135 int layer_id;
136 // TODO(miletus) : Use ScrollOffset once LayerTreeHost/Blink fully supports
137 // franctional scroll offset.
138 gfx::Vector2d scroll_delta;
142 struct CC_EXPORT ScrollAndScaleSet {
143 ScrollAndScaleSet();
144 ~ScrollAndScaleSet();
146 std::vector<LayerTreeHostCommon::ScrollUpdateInfo> scrolls;
147 float page_scale_delta;
148 float top_controls_delta;
149 ScopedPtrVector<SwapPromise> swap_promises;
152 template <typename LayerType>
153 bool LayerTreeHostCommon::RenderSurfaceContributesToTarget(
154 LayerType* layer,
155 int target_surface_layer_id) {
156 // A layer will either contribute its own content, or its render surface's
157 // content, to the target surface. The layer contributes its surface's content
158 // when both the following are true:
159 // (1) The layer actually has a render surface, and
160 // (2) The layer's render surface is not the same as the target surface.
162 // Otherwise, the layer just contributes itself to the target surface.
164 return layer->render_surface() && layer->id() != target_surface_layer_id;
167 template <typename LayerType>
168 LayerType* LayerTreeHostCommon::FindLayerInSubtree(LayerType* root_layer,
169 int layer_id) {
170 if (!root_layer)
171 return NULL;
173 if (root_layer->id() == layer_id)
174 return root_layer;
176 if (root_layer->mask_layer() && root_layer->mask_layer()->id() == layer_id)
177 return root_layer->mask_layer();
179 if (root_layer->replica_layer() &&
180 root_layer->replica_layer()->id() == layer_id)
181 return root_layer->replica_layer();
183 for (size_t i = 0; i < root_layer->children().size(); ++i) {
184 if (LayerType* found = FindLayerInSubtree(
185 get_layer_as_raw_ptr(root_layer->children(), i), layer_id))
186 return found;
188 return NULL;
191 template <typename LayerType>
192 void LayerTreeHostCommon::CallFunctionForSubtree(
193 LayerType* root_layer,
194 const base::Callback<void(LayerType* layer)>& function) {
195 function.Run(root_layer);
197 if (LayerType* mask_layer = root_layer->mask_layer())
198 function.Run(mask_layer);
199 if (LayerType* replica_layer = root_layer->replica_layer()) {
200 function.Run(replica_layer);
201 if (LayerType* mask_layer = replica_layer->mask_layer())
202 function.Run(mask_layer);
205 for (size_t i = 0; i < root_layer->children().size(); ++i) {
206 CallFunctionForSubtree(get_layer_as_raw_ptr(root_layer->children(), i),
207 function);
211 template <typename LayerType, typename RenderSurfaceLayerListType>
212 LayerTreeHostCommon::CalcDrawPropsInputsForTesting<LayerType,
213 RenderSurfaceLayerListType>::
214 CalcDrawPropsInputsForTesting(
215 LayerType* root_layer,
216 const gfx::Size& device_viewport_size,
217 const gfx::Transform& device_transform,
218 RenderSurfaceLayerListType* render_surface_layer_list)
219 : CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType>(
220 root_layer,
221 device_viewport_size,
222 device_transform,
223 1.f,
224 1.f,
225 NULL,
226 std::numeric_limits<int>::max() / 2,
227 false,
228 false,
229 true,
230 false,
231 render_surface_layer_list,
232 0) {
233 DCHECK(root_layer);
234 DCHECK(render_surface_layer_list);
237 template <typename LayerType, typename RenderSurfaceLayerListType>
238 LayerTreeHostCommon::CalcDrawPropsInputsForTesting<LayerType,
239 RenderSurfaceLayerListType>::
240 CalcDrawPropsInputsForTesting(
241 LayerType* root_layer,
242 const gfx::Size& device_viewport_size,
243 RenderSurfaceLayerListType* render_surface_layer_list)
244 : CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType>(
245 root_layer,
246 device_viewport_size,
247 identity_transform_,
248 1.f,
249 1.f,
250 NULL,
251 std::numeric_limits<int>::max() / 2,
252 false,
253 false,
254 true,
255 false,
256 render_surface_layer_list,
257 0) {
258 DCHECK(root_layer);
259 DCHECK(render_surface_layer_list);
262 } // namespace cc
264 #endif // CC_TREES_LAYER_TREE_HOST_COMMON_H_