Enable gaia services for enterprise autotests.
[chromium-blink-merge.git] / cc / trees / layer_tree_host_common.h
blob1f4717257bb4a44fdc1f6b657645a84847fc2912
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/rect.h"
17 #include "ui/gfx/transform.h"
18 #include "ui/gfx/vector2d.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 can_render_to_separate_surface,
44 bool can_adjust_raster_scales,
45 RenderSurfaceLayerListType* render_surface_layer_list,
46 int current_render_surface_layer_list_id)
47 : root_layer(root_layer),
48 device_viewport_size(device_viewport_size),
49 device_transform(device_transform),
50 device_scale_factor(device_scale_factor),
51 page_scale_factor(page_scale_factor),
52 page_scale_application_layer(page_scale_application_layer),
53 max_texture_size(max_texture_size),
54 can_use_lcd_text(can_use_lcd_text),
55 can_render_to_separate_surface(can_render_to_separate_surface),
56 can_adjust_raster_scales(can_adjust_raster_scales),
57 render_surface_layer_list(render_surface_layer_list),
58 current_render_surface_layer_list_id(
59 current_render_surface_layer_list_id) {}
61 LayerType* root_layer;
62 gfx::Size device_viewport_size;
63 const gfx::Transform& device_transform;
64 float device_scale_factor;
65 float page_scale_factor;
66 const LayerType* page_scale_application_layer;
67 int max_texture_size;
68 bool can_use_lcd_text;
69 bool can_render_to_separate_surface;
70 bool can_adjust_raster_scales;
71 RenderSurfaceLayerListType* render_surface_layer_list;
72 int current_render_surface_layer_list_id;
75 template <typename LayerType, typename RenderSurfaceLayerListType>
76 struct CalcDrawPropsInputsForTesting
77 : public CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType> {
78 CalcDrawPropsInputsForTesting(
79 LayerType* root_layer,
80 const gfx::Size& device_viewport_size,
81 const gfx::Transform& device_transform,
82 RenderSurfaceLayerListType* render_surface_layer_list);
83 CalcDrawPropsInputsForTesting(
84 LayerType* root_layer,
85 const gfx::Size& device_viewport_size,
86 RenderSurfaceLayerListType* render_surface_layer_list);
88 private:
89 const gfx::Transform identity_transform_;
92 typedef CalcDrawPropsInputs<Layer, RenderSurfaceLayerList>
93 CalcDrawPropsMainInputs;
94 typedef CalcDrawPropsInputsForTesting<Layer, RenderSurfaceLayerList>
95 CalcDrawPropsMainInputsForTesting;
96 static void CalculateDrawProperties(CalcDrawPropsMainInputs* inputs);
98 typedef CalcDrawPropsInputs<LayerImpl, LayerImplList> CalcDrawPropsImplInputs;
99 typedef CalcDrawPropsInputsForTesting<LayerImpl, LayerImplList>
100 CalcDrawPropsImplInputsForTesting;
101 static void CalculateDrawProperties(CalcDrawPropsImplInputs* inputs);
103 template <typename LayerType>
104 static bool RenderSurfaceContributesToTarget(LayerType*,
105 int target_surface_layer_id);
107 template <typename LayerType>
108 static void CallFunctionForSubtree(
109 LayerType* root_layer,
110 const base::Callback<void(LayerType* layer)>& function);
112 // Returns a layer with the given id if one exists in the subtree starting
113 // from the given root layer (including mask and replica layers).
114 template <typename LayerType>
115 static LayerType* FindLayerInSubtree(LayerType* root_layer, int layer_id);
117 static Layer* get_layer_as_raw_ptr(const LayerList& layers, size_t index) {
118 return layers[index].get();
121 static LayerImpl* get_layer_as_raw_ptr(const OwnedLayerImplList& layers,
122 size_t index) {
123 return layers[index];
126 static LayerImpl* get_layer_as_raw_ptr(const LayerImplList& layers,
127 size_t index) {
128 return layers[index];
131 struct ScrollUpdateInfo {
132 int layer_id;
133 gfx::Vector2d scroll_delta;
137 struct CC_EXPORT ScrollAndScaleSet {
138 ScrollAndScaleSet();
139 ~ScrollAndScaleSet();
141 std::vector<LayerTreeHostCommon::ScrollUpdateInfo> scrolls;
142 float page_scale_delta;
143 float top_controls_delta;
144 ScopedPtrVector<SwapPromise> swap_promises;
147 template <typename LayerType>
148 bool LayerTreeHostCommon::RenderSurfaceContributesToTarget(
149 LayerType* layer,
150 int target_surface_layer_id) {
151 // A layer will either contribute its own content, or its render surface's
152 // content, to the target surface. The layer contributes its surface's content
153 // when both the following are true:
154 // (1) The layer actually has a render surface, and
155 // (2) The layer's render surface is not the same as the target surface.
157 // Otherwise, the layer just contributes itself to the target surface.
159 return layer->render_surface() && layer->id() != target_surface_layer_id;
162 template <typename LayerType>
163 LayerType* LayerTreeHostCommon::FindLayerInSubtree(LayerType* root_layer,
164 int layer_id) {
165 if (!root_layer)
166 return NULL;
168 if (root_layer->id() == layer_id)
169 return root_layer;
171 if (root_layer->mask_layer() && root_layer->mask_layer()->id() == layer_id)
172 return root_layer->mask_layer();
174 if (root_layer->replica_layer() &&
175 root_layer->replica_layer()->id() == layer_id)
176 return root_layer->replica_layer();
178 for (size_t i = 0; i < root_layer->children().size(); ++i) {
179 if (LayerType* found = FindLayerInSubtree(
180 get_layer_as_raw_ptr(root_layer->children(), i), layer_id))
181 return found;
183 return NULL;
186 template <typename LayerType>
187 void LayerTreeHostCommon::CallFunctionForSubtree(
188 LayerType* root_layer,
189 const base::Callback<void(LayerType* layer)>& function) {
190 function.Run(root_layer);
192 if (LayerType* mask_layer = root_layer->mask_layer())
193 function.Run(mask_layer);
194 if (LayerType* replica_layer = root_layer->replica_layer()) {
195 function.Run(replica_layer);
196 if (LayerType* mask_layer = replica_layer->mask_layer())
197 function.Run(mask_layer);
200 for (size_t i = 0; i < root_layer->children().size(); ++i) {
201 CallFunctionForSubtree(get_layer_as_raw_ptr(root_layer->children(), i),
202 function);
206 template <typename LayerType, typename RenderSurfaceLayerListType>
207 LayerTreeHostCommon::CalcDrawPropsInputsForTesting<LayerType,
208 RenderSurfaceLayerListType>::
209 CalcDrawPropsInputsForTesting(
210 LayerType* root_layer,
211 const gfx::Size& device_viewport_size,
212 const gfx::Transform& device_transform,
213 RenderSurfaceLayerListType* render_surface_layer_list)
214 : CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType>(
215 root_layer,
216 device_viewport_size,
217 device_transform,
218 1.f,
219 1.f,
220 NULL,
221 std::numeric_limits<int>::max() / 2,
222 false,
223 true,
224 false,
225 render_surface_layer_list,
226 0) {
227 DCHECK(root_layer);
228 DCHECK(render_surface_layer_list);
231 template <typename LayerType, typename RenderSurfaceLayerListType>
232 LayerTreeHostCommon::CalcDrawPropsInputsForTesting<LayerType,
233 RenderSurfaceLayerListType>::
234 CalcDrawPropsInputsForTesting(
235 LayerType* root_layer,
236 const gfx::Size& device_viewport_size,
237 RenderSurfaceLayerListType* render_surface_layer_list)
238 : CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType>(
239 root_layer,
240 device_viewport_size,
241 identity_transform_,
242 1.f,
243 1.f,
244 NULL,
245 std::numeric_limits<int>::max() / 2,
246 false,
247 true,
248 false,
249 render_surface_layer_list,
250 0) {
251 DCHECK(root_layer);
252 DCHECK(render_surface_layer_list);
255 } // namespace cc
257 #endif // CC_TREES_LAYER_TREE_HOST_COMMON_H_