blob: de9a713ccb6b0f44488490c4d1f1d30e23104f6d [file] [log] [blame]
[email protected]cd57cc5a2012-10-12 22:43:411// Copyright 2011 The Chromium Authors. All rights reserved.
[email protected]0fb25002012-10-12 07:20:022// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]cd57cc5a2012-10-12 22:43:414
[email protected]556fd292013-03-18 08:03:045#ifndef CC_TREES_LAYER_TREE_HOST_COMMON_H_
6#define CC_TREES_LAYER_TREE_HOST_COMMON_H_
[email protected]cd57cc5a2012-10-12 22:43:417
[email protected]7aad55f2013-07-26 11:25:538#include <limits>
[email protected]bf691c22013-03-26 21:15:069#include <vector>
10
[email protected]65319e202013-06-29 01:11:5811#include "base/bind.h"
[email protected]cd57cc5a2012-10-12 22:43:4112#include "base/memory/ref_counted.h"
[email protected]681ccff2013-03-18 06:13:5213#include "cc/base/cc_export.h"
14#include "cc/base/scoped_ptr_vector.h"
[email protected]50761e92013-03-29 20:51:2815#include "cc/layers/layer_lists.h"
[email protected]aad0a0072012-11-01 18:15:5816#include "ui/gfx/rect.h"
[email protected]c8686a02012-11-27 08:29:0017#include "ui/gfx/transform.h"
[email protected]c9c1ebe2012-11-05 20:46:1318#include "ui/gfx/vector2d.h"
[email protected]cd57cc5a2012-10-12 22:43:4119
20namespace cc {
21
[email protected]96baf3e2012-10-22 23:09:5522class LayerImpl;
[email protected]96baf3e2012-10-22 23:09:5523class Layer;
[email protected]cd57cc5a2012-10-12 22:43:4124
[email protected]52347c842012-11-02 21:06:2025class CC_EXPORT LayerTreeHostCommon {
[email protected]6ba914122013-03-22 16:26:3926 public:
[email protected]0023fc72014-01-10 20:05:0627 static gfx::Rect CalculateVisibleRect(const gfx::Rect& target_surface_rect,
28 const gfx::Rect& layer_bound_rect,
[email protected]6ba914122013-03-22 16:26:3929 const gfx::Transform& transform);
[email protected]cd57cc5a2012-10-12 22:43:4130
[email protected]7aad55f2013-07-26 11:25:5331 template <typename LayerType, typename RenderSurfaceLayerListType>
32 struct CalcDrawPropsInputs {
33 public:
34 CalcDrawPropsInputs(LayerType* root_layer,
[email protected]64348ea2014-01-29 22:58:2635 const gfx::Size& device_viewport_size,
[email protected]7aad55f2013-07-26 11:25:5336 const gfx::Transform& device_transform,
37 float device_scale_factor,
38 float page_scale_factor,
[email protected]8428be22013-10-23 07:37:4439 const LayerType* page_scale_application_layer,
[email protected]7aad55f2013-07-26 11:25:5340 int max_texture_size,
41 bool can_use_lcd_text,
[email protected]45948712013-09-27 02:46:4842 bool can_render_to_separate_surface,
[email protected]7aad55f2013-07-26 11:25:5343 bool can_adjust_raster_scales,
44 RenderSurfaceLayerListType* render_surface_layer_list)
45 : root_layer(root_layer),
46 device_viewport_size(device_viewport_size),
47 device_transform(device_transform),
48 device_scale_factor(device_scale_factor),
49 page_scale_factor(page_scale_factor),
50 page_scale_application_layer(page_scale_application_layer),
51 max_texture_size(max_texture_size),
52 can_use_lcd_text(can_use_lcd_text),
[email protected]45948712013-09-27 02:46:4853 can_render_to_separate_surface(can_render_to_separate_surface),
[email protected]7aad55f2013-07-26 11:25:5354 can_adjust_raster_scales(can_adjust_raster_scales),
55 render_surface_layer_list(render_surface_layer_list) {}
56
57 LayerType* root_layer;
58 gfx::Size device_viewport_size;
59 const gfx::Transform& device_transform;
60 float device_scale_factor;
61 float page_scale_factor;
[email protected]8428be22013-10-23 07:37:4462 const LayerType* page_scale_application_layer;
[email protected]7aad55f2013-07-26 11:25:5363 int max_texture_size;
64 bool can_use_lcd_text;
[email protected]45948712013-09-27 02:46:4865 bool can_render_to_separate_surface;
[email protected]7aad55f2013-07-26 11:25:5366 bool can_adjust_raster_scales;
67 RenderSurfaceLayerListType* render_surface_layer_list;
68 };
69
70 template <typename LayerType, typename RenderSurfaceLayerListType>
71 struct CalcDrawPropsInputsForTesting
72 : public CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType> {
73 CalcDrawPropsInputsForTesting(
74 LayerType* root_layer,
[email protected]64348ea2014-01-29 22:58:2675 const gfx::Size& device_viewport_size,
[email protected]7aad55f2013-07-26 11:25:5376 const gfx::Transform& device_transform,
77 RenderSurfaceLayerListType* render_surface_layer_list);
78 CalcDrawPropsInputsForTesting(
79 LayerType* root_layer,
[email protected]64348ea2014-01-29 22:58:2680 const gfx::Size& device_viewport_size,
[email protected]7aad55f2013-07-26 11:25:5381 RenderSurfaceLayerListType* render_surface_layer_list);
82
83 private:
84 const gfx::Transform identity_transform_;
85 };
86
87 typedef CalcDrawPropsInputs<Layer, RenderSurfaceLayerList>
88 CalcDrawPropsMainInputs;
89 typedef CalcDrawPropsInputsForTesting<Layer, RenderSurfaceLayerList>
90 CalcDrawPropsMainInputsForTesting;
91 static void CalculateDrawProperties(CalcDrawPropsMainInputs* inputs);
92
93 typedef CalcDrawPropsInputs<LayerImpl, LayerImplList> CalcDrawPropsImplInputs;
94 typedef CalcDrawPropsInputsForTesting<LayerImpl, LayerImplList>
95 CalcDrawPropsImplInputsForTesting;
96 static void CalculateDrawProperties(CalcDrawPropsImplInputs* inputs);
[email protected]cd57cc5a2012-10-12 22:43:4197
[email protected]6ba914122013-03-22 16:26:3998 // Performs hit testing for a given render_surface_layer_list.
[email protected]05ba53c2014-04-16 21:22:5199 static LayerImpl* FindFirstScrollingLayerThatIsHitByPoint(
100 const gfx::PointF& screen_space_point,
101 const LayerImplList& render_surface_layer_list);
102
103 // Performs hit testing for a given render_surface_layer_list.
[email protected]6ba914122013-03-22 16:26:39104 static LayerImpl* FindLayerThatIsHitByPoint(
[email protected]14bc5d682014-01-17 07:26:47105 const gfx::PointF& screen_space_point,
[email protected]50761e92013-03-29 20:51:28106 const LayerImplList& render_surface_layer_list);
[email protected]cd57cc5a2012-10-12 22:43:41107
[email protected]6ba914122013-03-22 16:26:39108 static LayerImpl* FindLayerThatIsHitByPointInTouchHandlerRegion(
[email protected]14bc5d682014-01-17 07:26:47109 const gfx::PointF& screen_space_point,
[email protected]50761e92013-03-29 20:51:28110 const LayerImplList& render_surface_layer_list);
[email protected]2f1acc262012-11-16 21:42:22111
[email protected]14bc5d682014-01-17 07:26:47112 static bool LayerHasTouchEventHandlersAt(
113 const gfx::PointF& screen_space_point,
114 LayerImpl* layer_impl);
[email protected]35d2edd22012-12-13 02:19:05115
[email protected]6ba914122013-03-22 16:26:39116 template <typename LayerType>
117 static bool RenderSurfaceContributesToTarget(LayerType*,
118 int target_surface_layer_id);
[email protected]cd57cc5a2012-10-12 22:43:41119
[email protected]65319e202013-06-29 01:11:58120 template <typename LayerType>
121 static void CallFunctionForSubtree(
122 LayerType* root_layer,
123 const base::Callback<void(LayerType* layer)>& function);
124
[email protected]6ba914122013-03-22 16:26:39125 // Returns a layer with the given id if one exists in the subtree starting
126 // from the given root layer (including mask and replica layers).
127 template <typename LayerType>
128 static LayerType* FindLayerInSubtree(LayerType* root_layer, int layer_id);
[email protected]cd57cc5a2012-10-12 22:43:41129
[email protected]61a1b4012014-04-28 18:11:27130 static Layer* get_layer_as_raw_ptr(const LayerList& layers, size_t index) {
131 return layers[index].get();
[email protected]6ba914122013-03-22 16:26:39132 }
[email protected]cd57cc5a2012-10-12 22:43:41133
[email protected]61a1b4012014-04-28 18:11:27134 static LayerImpl* get_layer_as_raw_ptr(const OwnedLayerImplList& layers,
[email protected]7644fa22014-04-28 12:20:33135 size_t index) {
[email protected]61a1b4012014-04-28 18:11:27136 return layers[index];
137 }
138
139 static LayerImpl* get_layer_as_raw_ptr(const LayerImplList& layers,
140 size_t index) {
141 return layers[index];
[email protected]7644fa22014-04-28 12:20:33142 }
143
[email protected]6ba914122013-03-22 16:26:39144 struct ScrollUpdateInfo {
145 int layer_id;
146 gfx::Vector2d scroll_delta;
147 };
[email protected]cd57cc5a2012-10-12 22:43:41148};
149
[email protected]52347c842012-11-02 21:06:20150struct CC_EXPORT ScrollAndScaleSet {
[email protected]6ba914122013-03-22 16:26:39151 ScrollAndScaleSet();
152 ~ScrollAndScaleSet();
[email protected]cd57cc5a2012-10-12 22:43:41153
[email protected]6ba914122013-03-22 16:26:39154 std::vector<LayerTreeHostCommon::ScrollUpdateInfo> scrolls;
155 float page_scale_delta;
[email protected]cd57cc5a2012-10-12 22:43:41156};
157
[email protected]6ba914122013-03-22 16:26:39158template <typename LayerType>
159bool LayerTreeHostCommon::RenderSurfaceContributesToTarget(
160 LayerType* layer,
161 int target_surface_layer_id) {
162 // A layer will either contribute its own content, or its render surface's
163 // content, to the target surface. The layer contributes its surface's content
164 // when both the following are true:
[email protected]ed511b8d2013-03-25 03:29:29165 // (1) The layer actually has a render surface, and
166 // (2) The layer's render surface is not the same as the target surface.
[email protected]6ba914122013-03-22 16:26:39167 //
168 // Otherwise, the layer just contributes itself to the target surface.
[email protected]cd57cc5a2012-10-12 22:43:41169
[email protected]6ba914122013-03-22 16:26:39170 return layer->render_surface() && layer->id() != target_surface_layer_id;
[email protected]cd57cc5a2012-10-12 22:43:41171}
172
[email protected]6ba914122013-03-22 16:26:39173template <typename LayerType>
174LayerType* LayerTreeHostCommon::FindLayerInSubtree(LayerType* root_layer,
175 int layer_id) {
[email protected]a90fac72013-06-06 18:56:13176 if (!root_layer)
177 return NULL;
178
[email protected]6ba914122013-03-22 16:26:39179 if (root_layer->id() == layer_id)
180 return root_layer;
[email protected]cd57cc5a2012-10-12 22:43:41181
[email protected]6ba914122013-03-22 16:26:39182 if (root_layer->mask_layer() && root_layer->mask_layer()->id() == layer_id)
183 return root_layer->mask_layer();
[email protected]cd57cc5a2012-10-12 22:43:41184
[email protected]6ba914122013-03-22 16:26:39185 if (root_layer->replica_layer() &&
186 root_layer->replica_layer()->id() == layer_id)
187 return root_layer->replica_layer();
[email protected]cd57cc5a2012-10-12 22:43:41188
[email protected]6ba914122013-03-22 16:26:39189 for (size_t i = 0; i < root_layer->children().size(); ++i) {
190 if (LayerType* found = FindLayerInSubtree(
[email protected]61a1b4012014-04-28 18:11:27191 get_layer_as_raw_ptr(root_layer->children(), i), layer_id))
[email protected]6ba914122013-03-22 16:26:39192 return found;
193 }
194 return NULL;
[email protected]cd57cc5a2012-10-12 22:43:41195}
196
[email protected]65319e202013-06-29 01:11:58197template <typename LayerType>
198void LayerTreeHostCommon::CallFunctionForSubtree(
199 LayerType* root_layer,
200 const base::Callback<void(LayerType* layer)>& function) {
201 function.Run(root_layer);
202
203 if (LayerType* mask_layer = root_layer->mask_layer())
204 function.Run(mask_layer);
205 if (LayerType* replica_layer = root_layer->replica_layer()) {
206 function.Run(replica_layer);
207 if (LayerType* mask_layer = replica_layer->mask_layer())
208 function.Run(mask_layer);
209 }
210
211 for (size_t i = 0; i < root_layer->children().size(); ++i) {
[email protected]61a1b4012014-04-28 18:11:27212 CallFunctionForSubtree(get_layer_as_raw_ptr(root_layer->children(), i),
[email protected]65319e202013-06-29 01:11:58213 function);
214 }
215}
216
[email protected]7aad55f2013-07-26 11:25:53217template <typename LayerType, typename RenderSurfaceLayerListType>
218LayerTreeHostCommon::CalcDrawPropsInputsForTesting<LayerType,
219 RenderSurfaceLayerListType>::
220 CalcDrawPropsInputsForTesting(
221 LayerType* root_layer,
[email protected]64348ea2014-01-29 22:58:26222 const gfx::Size& device_viewport_size,
[email protected]7aad55f2013-07-26 11:25:53223 const gfx::Transform& device_transform,
224 RenderSurfaceLayerListType* render_surface_layer_list)
225 : CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType>(
226 root_layer,
227 device_viewport_size,
228 device_transform,
229 1.f,
230 1.f,
231 NULL,
232 std::numeric_limits<int>::max() / 2,
233 false,
[email protected]45948712013-09-27 02:46:48234 true,
[email protected]7aad55f2013-07-26 11:25:53235 false,
236 render_surface_layer_list) {
237 DCHECK(root_layer);
238 DCHECK(render_surface_layer_list);
239}
240
241template <typename LayerType, typename RenderSurfaceLayerListType>
242LayerTreeHostCommon::CalcDrawPropsInputsForTesting<LayerType,
243 RenderSurfaceLayerListType>::
244 CalcDrawPropsInputsForTesting(
245 LayerType* root_layer,
[email protected]64348ea2014-01-29 22:58:26246 const gfx::Size& device_viewport_size,
[email protected]7aad55f2013-07-26 11:25:53247 RenderSurfaceLayerListType* render_surface_layer_list)
248 : CalcDrawPropsInputs<LayerType, RenderSurfaceLayerListType>(
249 root_layer,
250 device_viewport_size,
251 identity_transform_,
252 1.f,
253 1.f,
254 NULL,
255 std::numeric_limits<int>::max() / 2,
256 false,
[email protected]45948712013-09-27 02:46:48257 true,
[email protected]7aad55f2013-07-26 11:25:53258 false,
259 render_surface_layer_list) {
260 DCHECK(root_layer);
261 DCHECK(render_surface_layer_list);
262}
263
[email protected]e17f3072012-10-26 22:15:34264} // namespace cc
[email protected]cd57cc5a2012-10-12 22:43:41265
[email protected]556fd292013-03-18 08:03:04266#endif // CC_TREES_LAYER_TREE_HOST_COMMON_H_