Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / cc / trees / layer_tree_host_common.h
blobc86c4aa2c788a58820e1464923a07bcc706af6a2
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 ScopedPtrVector<SwapPromise> swap_promises;
146 template <typename LayerType>
147 bool LayerTreeHostCommon::RenderSurfaceContributesToTarget(
148 LayerType* layer,
149 int target_surface_layer_id) {
150 // A layer will either contribute its own content, or its render surface's
151 // content, to the target surface. The layer contributes its surface's content
152 // when both the following are true:
153 // (1) The layer actually has a render surface, and
154 // (2) The layer's render surface is not the same as the target surface.
156 // Otherwise, the layer just contributes itself to the target surface.
158 return layer->render_surface() && layer->id() != target_surface_layer_id;
161 template <typename LayerType>
162 LayerType* LayerTreeHostCommon::FindLayerInSubtree(LayerType* root_layer,
163 int layer_id) {
164 if (!root_layer)
165 return NULL;
167 if (root_layer->id() == layer_id)
168 return root_layer;
170 if (root_layer->mask_layer() && root_layer->mask_layer()->id() == layer_id)
171 return root_layer->mask_layer();
173 if (root_layer->replica_layer() &&
174 root_layer->replica_layer()->id() == layer_id)
175 return root_layer->replica_layer();
177 for (size_t i = 0; i < root_layer->children().size(); ++i) {
178 if (LayerType* found = FindLayerInSubtree(
179 get_layer_as_raw_ptr(root_layer->children(), i), layer_id))
180 return found;
182 return NULL;
185 template <typename LayerType>
186 void LayerTreeHostCommon::CallFunctionForSubtree(
187 LayerType* root_layer,
188 const base::Callback<void(LayerType* layer)>& function) {
189 function.Run(root_layer);
191 if (LayerType* mask_layer = root_layer->mask_layer())
192 function.Run(mask_layer);
193 if (LayerType* replica_layer = root_layer->replica_layer()) {
194 function.Run(replica_layer);
195 if (LayerType* mask_layer = replica_layer->mask_layer())
196 function.Run(mask_layer);
199 for (size_t i = 0; i < root_layer->children().size(); ++i) {
200 CallFunctionForSubtree(get_layer_as_raw_ptr(root_layer->children(), i),
201 function);
205 template <typename LayerType, typename RenderSurfaceLayerListType>
206 LayerTreeHostCommon::CalcDrawPropsInputsForTesting<LayerType,
207 RenderSurfaceLayerListType>::
208 CalcDrawPropsInputsForTesting(
209 LayerType* root_layer,
210 const gfx::Size& device_viewport_size,
211 const gfx::Transform& device_transform,
212 RenderSurfaceLayerListType* render_surface_layer_list)
213 : CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType>(
214 root_layer,
215 device_viewport_size,
216 device_transform,
217 1.f,
218 1.f,
219 NULL,
220 std::numeric_limits<int>::max() / 2,
221 false,
222 true,
223 false,
224 render_surface_layer_list,
225 0) {
226 DCHECK(root_layer);
227 DCHECK(render_surface_layer_list);
230 template <typename LayerType, typename RenderSurfaceLayerListType>
231 LayerTreeHostCommon::CalcDrawPropsInputsForTesting<LayerType,
232 RenderSurfaceLayerListType>::
233 CalcDrawPropsInputsForTesting(
234 LayerType* root_layer,
235 const gfx::Size& device_viewport_size,
236 RenderSurfaceLayerListType* render_surface_layer_list)
237 : CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType>(
238 root_layer,
239 device_viewport_size,
240 identity_transform_,
241 1.f,
242 1.f,
243 NULL,
244 std::numeric_limits<int>::max() / 2,
245 false,
246 true,
247 false,
248 render_surface_layer_list,
249 0) {
250 DCHECK(root_layer);
251 DCHECK(render_surface_layer_list);
254 } // namespace cc
256 #endif // CC_TREES_LAYER_TREE_HOST_COMMON_H_