[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors. All rights reserved. |
[email protected] | 0fb2500 | 2012-10-12 07:20:02 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 4 | |
[email protected] | 556fd29 | 2013-03-18 08:03:04 | [diff] [blame] | 5 | #ifndef CC_TREES_LAYER_TREE_HOST_COMMON_H_ |
| 6 | #define CC_TREES_LAYER_TREE_HOST_COMMON_H_ |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 7 | |
[email protected] | 7aad55f | 2013-07-26 11:25:53 | [diff] [blame] | 8 | #include <limits> |
[email protected] | bf691c2 | 2013-03-26 21:15:06 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
[email protected] | 65319e20 | 2013-06-29 01:11:58 | [diff] [blame] | 11 | #include "base/bind.h" |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
[email protected] | 681ccff | 2013-03-18 06:13:52 | [diff] [blame] | 13 | #include "cc/base/cc_export.h" |
| 14 | #include "cc/base/scoped_ptr_vector.h" |
[email protected] | 50761e9 | 2013-03-29 20:51:28 | [diff] [blame] | 15 | #include "cc/layers/layer_lists.h" |
[email protected] | aad0a007 | 2012-11-01 18:15:58 | [diff] [blame] | 16 | #include "ui/gfx/rect.h" |
[email protected] | c8686a0 | 2012-11-27 08:29:00 | [diff] [blame] | 17 | #include "ui/gfx/transform.h" |
[email protected] | c9c1ebe | 2012-11-05 20:46:13 | [diff] [blame] | 18 | #include "ui/gfx/vector2d.h" |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 19 | |
| 20 | namespace cc { |
| 21 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 22 | class LayerImpl; |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 23 | class Layer; |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 24 | |
[email protected] | 52347c84 | 2012-11-02 21:06:20 | [diff] [blame] | 25 | class CC_EXPORT LayerTreeHostCommon { |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 26 | public: |
[email protected] | 0023fc7 | 2014-01-10 20:05:06 | [diff] [blame] | 27 | static gfx::Rect CalculateVisibleRect(const gfx::Rect& target_surface_rect, |
| 28 | const gfx::Rect& layer_bound_rect, |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 29 | const gfx::Transform& transform); |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 30 | |
[email protected] | 7aad55f | 2013-07-26 11:25:53 | [diff] [blame] | 31 | template <typename LayerType, typename RenderSurfaceLayerListType> |
| 32 | struct CalcDrawPropsInputs { |
| 33 | public: |
| 34 | CalcDrawPropsInputs(LayerType* root_layer, |
[email protected] | 64348ea | 2014-01-29 22:58:26 | [diff] [blame] | 35 | const gfx::Size& device_viewport_size, |
[email protected] | 7aad55f | 2013-07-26 11:25:53 | [diff] [blame] | 36 | const gfx::Transform& device_transform, |
| 37 | float device_scale_factor, |
| 38 | float page_scale_factor, |
[email protected] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 39 | const LayerType* page_scale_application_layer, |
[email protected] | 7aad55f | 2013-07-26 11:25:53 | [diff] [blame] | 40 | int max_texture_size, |
| 41 | bool can_use_lcd_text, |
[email protected] | 4594871 | 2013-09-27 02:46:48 | [diff] [blame] | 42 | bool can_render_to_separate_surface, |
[email protected] | 7aad55f | 2013-07-26 11:25:53 | [diff] [blame] | 43 | 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] | 4594871 | 2013-09-27 02:46:48 | [diff] [blame] | 53 | can_render_to_separate_surface(can_render_to_separate_surface), |
[email protected] | 7aad55f | 2013-07-26 11:25:53 | [diff] [blame] | 54 | 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] | 8428be2 | 2013-10-23 07:37:44 | [diff] [blame] | 62 | const LayerType* page_scale_application_layer; |
[email protected] | 7aad55f | 2013-07-26 11:25:53 | [diff] [blame] | 63 | int max_texture_size; |
| 64 | bool can_use_lcd_text; |
[email protected] | 4594871 | 2013-09-27 02:46:48 | [diff] [blame] | 65 | bool can_render_to_separate_surface; |
[email protected] | 7aad55f | 2013-07-26 11:25:53 | [diff] [blame] | 66 | 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] | 64348ea | 2014-01-29 22:58:26 | [diff] [blame] | 75 | const gfx::Size& device_viewport_size, |
[email protected] | 7aad55f | 2013-07-26 11:25:53 | [diff] [blame] | 76 | const gfx::Transform& device_transform, |
| 77 | RenderSurfaceLayerListType* render_surface_layer_list); |
| 78 | CalcDrawPropsInputsForTesting( |
| 79 | LayerType* root_layer, |
[email protected] | 64348ea | 2014-01-29 22:58:26 | [diff] [blame] | 80 | const gfx::Size& device_viewport_size, |
[email protected] | 7aad55f | 2013-07-26 11:25:53 | [diff] [blame] | 81 | 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] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 97 | |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 98 | // Performs hit testing for a given render_surface_layer_list. |
[email protected] | 05ba53c | 2014-04-16 21:22:51 | [diff] [blame] | 99 | 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] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 104 | static LayerImpl* FindLayerThatIsHitByPoint( |
[email protected] | 14bc5d68 | 2014-01-17 07:26:47 | [diff] [blame] | 105 | const gfx::PointF& screen_space_point, |
[email protected] | 50761e9 | 2013-03-29 20:51:28 | [diff] [blame] | 106 | const LayerImplList& render_surface_layer_list); |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 107 | |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 108 | static LayerImpl* FindLayerThatIsHitByPointInTouchHandlerRegion( |
[email protected] | 14bc5d68 | 2014-01-17 07:26:47 | [diff] [blame] | 109 | const gfx::PointF& screen_space_point, |
[email protected] | 50761e9 | 2013-03-29 20:51:28 | [diff] [blame] | 110 | const LayerImplList& render_surface_layer_list); |
[email protected] | 2f1acc26 | 2012-11-16 21:42:22 | [diff] [blame] | 111 | |
[email protected] | 14bc5d68 | 2014-01-17 07:26:47 | [diff] [blame] | 112 | static bool LayerHasTouchEventHandlersAt( |
| 113 | const gfx::PointF& screen_space_point, |
| 114 | LayerImpl* layer_impl); |
[email protected] | 35d2edd2 | 2012-12-13 02:19:05 | [diff] [blame] | 115 | |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 116 | template <typename LayerType> |
| 117 | static bool RenderSurfaceContributesToTarget(LayerType*, |
| 118 | int target_surface_layer_id); |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 119 | |
[email protected] | 65319e20 | 2013-06-29 01:11:58 | [diff] [blame] | 120 | template <typename LayerType> |
| 121 | static void CallFunctionForSubtree( |
| 122 | LayerType* root_layer, |
| 123 | const base::Callback<void(LayerType* layer)>& function); |
| 124 | |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 125 | // 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] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 129 | |
[email protected] | 61a1b401 | 2014-04-28 18:11:27 | [diff] [blame] | 130 | static Layer* get_layer_as_raw_ptr(const LayerList& layers, size_t index) { |
| 131 | return layers[index].get(); |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 132 | } |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 133 | |
[email protected] | 61a1b401 | 2014-04-28 18:11:27 | [diff] [blame] | 134 | static LayerImpl* get_layer_as_raw_ptr(const OwnedLayerImplList& layers, |
[email protected] | 7644fa2 | 2014-04-28 12:20:33 | [diff] [blame] | 135 | size_t index) { |
[email protected] | 61a1b401 | 2014-04-28 18:11:27 | [diff] [blame] | 136 | 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] | 7644fa2 | 2014-04-28 12:20:33 | [diff] [blame] | 142 | } |
| 143 | |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 144 | struct ScrollUpdateInfo { |
| 145 | int layer_id; |
| 146 | gfx::Vector2d scroll_delta; |
| 147 | }; |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 148 | }; |
| 149 | |
[email protected] | 52347c84 | 2012-11-02 21:06:20 | [diff] [blame] | 150 | struct CC_EXPORT ScrollAndScaleSet { |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 151 | ScrollAndScaleSet(); |
| 152 | ~ScrollAndScaleSet(); |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 153 | |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 154 | std::vector<LayerTreeHostCommon::ScrollUpdateInfo> scrolls; |
| 155 | float page_scale_delta; |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 156 | }; |
| 157 | |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 158 | template <typename LayerType> |
| 159 | bool 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] | ed511b8d | 2013-03-25 03:29:29 | [diff] [blame] | 165 | // (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] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 167 | // |
| 168 | // Otherwise, the layer just contributes itself to the target surface. |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 169 | |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 170 | return layer->render_surface() && layer->id() != target_surface_layer_id; |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 171 | } |
| 172 | |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 173 | template <typename LayerType> |
| 174 | LayerType* LayerTreeHostCommon::FindLayerInSubtree(LayerType* root_layer, |
| 175 | int layer_id) { |
[email protected] | a90fac7 | 2013-06-06 18:56:13 | [diff] [blame] | 176 | if (!root_layer) |
| 177 | return NULL; |
| 178 | |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 179 | if (root_layer->id() == layer_id) |
| 180 | return root_layer; |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 181 | |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 182 | if (root_layer->mask_layer() && root_layer->mask_layer()->id() == layer_id) |
| 183 | return root_layer->mask_layer(); |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 184 | |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 185 | if (root_layer->replica_layer() && |
| 186 | root_layer->replica_layer()->id() == layer_id) |
| 187 | return root_layer->replica_layer(); |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 188 | |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 189 | for (size_t i = 0; i < root_layer->children().size(); ++i) { |
| 190 | if (LayerType* found = FindLayerInSubtree( |
[email protected] | 61a1b401 | 2014-04-28 18:11:27 | [diff] [blame] | 191 | get_layer_as_raw_ptr(root_layer->children(), i), layer_id)) |
[email protected] | 6ba91412 | 2013-03-22 16:26:39 | [diff] [blame] | 192 | return found; |
| 193 | } |
| 194 | return NULL; |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 195 | } |
| 196 | |
[email protected] | 65319e20 | 2013-06-29 01:11:58 | [diff] [blame] | 197 | template <typename LayerType> |
| 198 | void 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] | 61a1b401 | 2014-04-28 18:11:27 | [diff] [blame] | 212 | CallFunctionForSubtree(get_layer_as_raw_ptr(root_layer->children(), i), |
[email protected] | 65319e20 | 2013-06-29 01:11:58 | [diff] [blame] | 213 | function); |
| 214 | } |
| 215 | } |
| 216 | |
[email protected] | 7aad55f | 2013-07-26 11:25:53 | [diff] [blame] | 217 | template <typename LayerType, typename RenderSurfaceLayerListType> |
| 218 | LayerTreeHostCommon::CalcDrawPropsInputsForTesting<LayerType, |
| 219 | RenderSurfaceLayerListType>:: |
| 220 | CalcDrawPropsInputsForTesting( |
| 221 | LayerType* root_layer, |
[email protected] | 64348ea | 2014-01-29 22:58:26 | [diff] [blame] | 222 | const gfx::Size& device_viewport_size, |
[email protected] | 7aad55f | 2013-07-26 11:25:53 | [diff] [blame] | 223 | 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] | 4594871 | 2013-09-27 02:46:48 | [diff] [blame] | 234 | true, |
[email protected] | 7aad55f | 2013-07-26 11:25:53 | [diff] [blame] | 235 | false, |
| 236 | render_surface_layer_list) { |
| 237 | DCHECK(root_layer); |
| 238 | DCHECK(render_surface_layer_list); |
| 239 | } |
| 240 | |
| 241 | template <typename LayerType, typename RenderSurfaceLayerListType> |
| 242 | LayerTreeHostCommon::CalcDrawPropsInputsForTesting<LayerType, |
| 243 | RenderSurfaceLayerListType>:: |
| 244 | CalcDrawPropsInputsForTesting( |
| 245 | LayerType* root_layer, |
[email protected] | 64348ea | 2014-01-29 22:58:26 | [diff] [blame] | 246 | const gfx::Size& device_viewport_size, |
[email protected] | 7aad55f | 2013-07-26 11:25:53 | [diff] [blame] | 247 | 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] | 4594871 | 2013-09-27 02:46:48 | [diff] [blame] | 257 | true, |
[email protected] | 7aad55f | 2013-07-26 11:25:53 | [diff] [blame] | 258 | false, |
| 259 | render_surface_layer_list) { |
| 260 | DCHECK(root_layer); |
| 261 | DCHECK(render_surface_layer_list); |
| 262 | } |
| 263 | |
[email protected] | e17f307 | 2012-10-26 22:15:34 | [diff] [blame] | 264 | } // namespace cc |
[email protected] | cd57cc5a | 2012-10-12 22:43:41 | [diff] [blame] | 265 | |
[email protected] | 556fd29 | 2013-03-18 08:03:04 | [diff] [blame] | 266 | #endif // CC_TREES_LAYER_TREE_HOST_COMMON_H_ |