OLD | NEW |
| (Empty) |
1 // Copyright 2012 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. | |
4 | |
5 #ifndef CC_RESOURCES_PICTURE_LAYER_TILING_H_ | |
6 #define CC_RESOURCES_PICTURE_LAYER_TILING_H_ | |
7 | |
8 #include <map> | |
9 #include <utility> | |
10 #include <vector> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/containers/scoped_ptr_hash_map.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "cc/base/cc_export.h" | |
16 #include "cc/base/region.h" | |
17 #include "cc/base/tiling_data.h" | |
18 #include "cc/resources/tile.h" | |
19 #include "cc/resources/tile_priority.h" | |
20 #include "cc/trees/occlusion.h" | |
21 #include "ui/gfx/geometry/rect.h" | |
22 | |
23 namespace base { | |
24 namespace trace_event { | |
25 class TracedValue; | |
26 } | |
27 } | |
28 | |
29 namespace cc { | |
30 | |
31 class PictureLayerTiling; | |
32 class PrioritizedTile; | |
33 class RasterSource; | |
34 | |
35 class CC_EXPORT PictureLayerTilingClient { | |
36 public: | |
37 // Create a tile at the given content_rect (in the contents scale of the | |
38 // tiling) This might return null if the client cannot create such a tile. | |
39 virtual ScopedTilePtr CreateTile(float contents_scale, | |
40 const gfx::Rect& content_rect) = 0; | |
41 virtual gfx::Size CalculateTileSize( | |
42 const gfx::Size& content_bounds) const = 0; | |
43 // This invalidation region defines the area (if any, it can by null) that | |
44 // tiles can not be shared between pending and active trees. | |
45 virtual const Region* GetPendingInvalidation() = 0; | |
46 virtual const PictureLayerTiling* GetPendingOrActiveTwinTiling( | |
47 const PictureLayerTiling* tiling) const = 0; | |
48 virtual TilePriority::PriorityBin GetMaxTilePriorityBin() const = 0; | |
49 virtual bool RequiresHighResToDraw() const = 0; | |
50 | |
51 protected: | |
52 virtual ~PictureLayerTilingClient() {} | |
53 }; | |
54 | |
55 class CC_EXPORT PictureLayerTiling { | |
56 public: | |
57 static const int kBorderTexels = 1; | |
58 | |
59 PictureLayerTilingClient* client() const { return client_; } | |
60 ~PictureLayerTiling(); | |
61 | |
62 static float CalculateSoonBorderDistance( | |
63 const gfx::Rect& visible_rect_in_content_space, | |
64 float content_to_screen_scale); | |
65 | |
66 // Create a tiling with no tiles. CreateTile() must be called to add some. | |
67 static scoped_ptr<PictureLayerTiling> Create( | |
68 WhichTree tree, | |
69 float contents_scale, | |
70 scoped_refptr<RasterSource> raster_source, | |
71 PictureLayerTilingClient* client, | |
72 float tiling_interest_area_viewport_multiplier, | |
73 float skewport_target_time_in_seconds, | |
74 int skewport_extrapolation_limit_in_content_pixels); | |
75 | |
76 void SetRasterSourceAndResize(scoped_refptr<RasterSource> raster_source); | |
77 void Invalidate(const Region& layer_invalidation); | |
78 void CreateMissingTilesInLiveTilesRect(); | |
79 void TakeTilesAndPropertiesFrom(PictureLayerTiling* pending_twin, | |
80 const Region& layer_invalidation); | |
81 | |
82 bool IsTileRequiredForActivation(const Tile* tile) const; | |
83 bool IsTileRequiredForDraw(const Tile* tile) const; | |
84 | |
85 void set_resolution(TileResolution resolution) { resolution_ = resolution; } | |
86 TileResolution resolution() const { return resolution_; } | |
87 void set_can_require_tiles_for_activation(bool can_require_tiles) { | |
88 can_require_tiles_for_activation_ = can_require_tiles; | |
89 } | |
90 | |
91 RasterSource* raster_source() const { return raster_source_.get(); } | |
92 gfx::Size tiling_size() const { return tiling_data_.tiling_size(); } | |
93 gfx::Rect live_tiles_rect() const { return live_tiles_rect_; } | |
94 gfx::Size tile_size() const { return tiling_data_.max_texture_size(); } | |
95 float contents_scale() const { return contents_scale_; } | |
96 const TilingData* tiling_data() const { return &tiling_data_; } | |
97 | |
98 Tile* TileAt(int i, int j) const { | |
99 TileMap::const_iterator iter = tiles_.find(TileMapKey(i, j)); | |
100 return iter == tiles_.end() ? nullptr : iter->second; | |
101 } | |
102 | |
103 bool has_tiles() const { return !tiles_.empty(); } | |
104 | |
105 // For testing functionality. | |
106 void CreateAllTilesForTesting() { | |
107 SetLiveTilesRect(gfx::Rect(tiling_data_.tiling_size())); | |
108 } | |
109 const TilingData& TilingDataForTesting() const { return tiling_data_; } | |
110 std::vector<Tile*> AllTilesForTesting() const { | |
111 std::vector<Tile*> all_tiles; | |
112 for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end(); ++it) | |
113 all_tiles.push_back(it->second); | |
114 return all_tiles; | |
115 } | |
116 | |
117 void UpdateAllRequiredStateForTesting() { | |
118 for (const auto& key_tile_pair : tiles_) | |
119 UpdateRequiredStatesOnTile(key_tile_pair.second); | |
120 } | |
121 std::map<const Tile*, PrioritizedTile> | |
122 UpdateAndGetAllPrioritizedTilesForTesting() const; | |
123 | |
124 void SetAllTilesOccludedForTesting() { | |
125 gfx::Rect viewport_in_layer_space = | |
126 ScaleToEnclosingRect(current_visible_rect_, 1.0f / contents_scale_); | |
127 current_occlusion_in_layer_space_ = | |
128 Occlusion(gfx::Transform(), | |
129 SimpleEnclosedRegion(viewport_in_layer_space), | |
130 SimpleEnclosedRegion(viewport_in_layer_space)); | |
131 } | |
132 const gfx::Rect& GetCurrentVisibleRectForTesting() const { | |
133 return current_visible_rect_; | |
134 } | |
135 | |
136 // Iterate over all tiles to fill content_rect. Even if tiles are invalid | |
137 // (i.e. no valid resource) this tiling should still iterate over them. | |
138 // The union of all geometry_rect calls for each element iterated over should | |
139 // exactly equal content_rect and no two geometry_rects should intersect. | |
140 class CC_EXPORT CoverageIterator { | |
141 public: | |
142 CoverageIterator(); | |
143 CoverageIterator(const PictureLayerTiling* tiling, | |
144 float dest_scale, | |
145 const gfx::Rect& rect); | |
146 ~CoverageIterator(); | |
147 | |
148 // Visible rect (no borders), always in the space of content_rect, | |
149 // regardless of the contents scale of the tiling. | |
150 gfx::Rect geometry_rect() const; | |
151 // Texture rect (in texels) for geometry_rect | |
152 gfx::RectF texture_rect() const; | |
153 | |
154 Tile* operator->() const { return current_tile_; } | |
155 Tile* operator*() const { return current_tile_; } | |
156 | |
157 CoverageIterator& operator++(); | |
158 operator bool() const { return tile_j_ <= bottom_; } | |
159 | |
160 int i() const { return tile_i_; } | |
161 int j() const { return tile_j_; } | |
162 | |
163 private: | |
164 const PictureLayerTiling* tiling_; | |
165 gfx::Rect dest_rect_; | |
166 float dest_to_content_scale_; | |
167 | |
168 Tile* current_tile_; | |
169 gfx::Rect current_geometry_rect_; | |
170 int tile_i_; | |
171 int tile_j_; | |
172 int left_; | |
173 int top_; | |
174 int right_; | |
175 int bottom_; | |
176 | |
177 friend class PictureLayerTiling; | |
178 }; | |
179 | |
180 void Reset(); | |
181 | |
182 bool ComputeTilePriorityRects(const gfx::Rect& viewport_in_layer_space, | |
183 float ideal_contents_scale, | |
184 double current_frame_time_in_seconds, | |
185 const Occlusion& occlusion_in_layer_space); | |
186 | |
187 void GetAllPrioritizedTilesForTracing( | |
188 std::vector<PrioritizedTile>* prioritized_tiles) const; | |
189 void AsValueInto(base::trace_event::TracedValue* array) const; | |
190 size_t GPUMemoryUsageInBytes() const; | |
191 | |
192 struct RectExpansionCache { | |
193 RectExpansionCache(); | |
194 | |
195 gfx::Rect previous_start; | |
196 gfx::Rect previous_bounds; | |
197 gfx::Rect previous_result; | |
198 int64 previous_target; | |
199 }; | |
200 | |
201 static | |
202 gfx::Rect ExpandRectEquallyToAreaBoundedBy( | |
203 const gfx::Rect& starting_rect, | |
204 int64 target_area, | |
205 const gfx::Rect& bounding_rect, | |
206 RectExpansionCache* cache); | |
207 | |
208 protected: | |
209 friend class CoverageIterator; | |
210 friend class PrioritizedTile; | |
211 friend class TilingSetRasterQueueAll; | |
212 friend class TilingSetRasterQueueRequired; | |
213 friend class TilingSetEvictionQueue; | |
214 | |
215 // PENDING VISIBLE RECT refers to the visible rect that will become current | |
216 // upon activation (ie, the pending tree's visible rect). Tiles in this | |
217 // region that are not part of the current visible rect are all handled | |
218 // here. Note that when processing a pending tree, this rect is the same as | |
219 // the visible rect so no tiles are processed in this case. | |
220 enum PriorityRectType { | |
221 VISIBLE_RECT, | |
222 PENDING_VISIBLE_RECT, | |
223 SKEWPORT_RECT, | |
224 SOON_BORDER_RECT, | |
225 EVENTUALLY_RECT | |
226 }; | |
227 | |
228 using TileMapKey = std::pair<int, int>; | |
229 using TileMap = base::ScopedPtrHashMap<TileMapKey, ScopedTilePtr>; | |
230 | |
231 struct FrameVisibleRect { | |
232 gfx::Rect visible_rect_in_content_space; | |
233 double frame_time_in_seconds = 0.0; | |
234 }; | |
235 | |
236 PictureLayerTiling(WhichTree tree, | |
237 float contents_scale, | |
238 scoped_refptr<RasterSource> raster_source, | |
239 PictureLayerTilingClient* client, | |
240 float tiling_interest_area_viewport_multiplier, | |
241 float skewport_target_time_in_seconds, | |
242 int skewport_extrapolation_limit_in_content_pixels); | |
243 void SetLiveTilesRect(const gfx::Rect& live_tiles_rect); | |
244 void VerifyLiveTilesRect(bool is_on_recycle_tree) const; | |
245 Tile* CreateTile(int i, int j); | |
246 // Returns true if the Tile existed and was removed from the tiling. | |
247 bool RemoveTileAt(int i, int j); | |
248 bool TilingMatchesTileIndices(const PictureLayerTiling* twin) const; | |
249 | |
250 // Computes a skewport. The calculation extrapolates the last visible | |
251 // rect and the current visible rect to expand the skewport to where it | |
252 // would be in |skewport_target_time| seconds. Note that the skewport | |
253 // is guaranteed to contain the current visible rect. | |
254 gfx::Rect ComputeSkewport(double current_frame_time_in_seconds, | |
255 const gfx::Rect& visible_rect_in_content_space) | |
256 const; | |
257 | |
258 // Save the required data for computing tile priorities later. | |
259 void SetTilePriorityRects(float content_to_screen_scale_, | |
260 const gfx::Rect& visible_rect_in_content_space, | |
261 const gfx::Rect& skewport, | |
262 const gfx::Rect& soon_border_rect, | |
263 const gfx::Rect& eventually_rect, | |
264 const Occlusion& occlusion_in_layer_space); | |
265 | |
266 bool NeedsUpdateForFrameAtTimeAndViewport( | |
267 double frame_time_in_seconds, | |
268 const gfx::Rect& viewport_in_layer_space) { | |
269 return frame_time_in_seconds != | |
270 visible_rect_history_[0].frame_time_in_seconds || | |
271 viewport_in_layer_space != last_viewport_in_layer_space_; | |
272 } | |
273 void UpdateVisibleRectHistory( | |
274 double frame_time_in_seconds, | |
275 const gfx::Rect& visible_rect_in_content_space) { | |
276 visible_rect_history_[1] = visible_rect_history_[0]; | |
277 visible_rect_history_[0].frame_time_in_seconds = frame_time_in_seconds; | |
278 visible_rect_history_[0].visible_rect_in_content_space = | |
279 visible_rect_in_content_space; | |
280 // If we don't have a second history item, set it to the most recent one. | |
281 if (visible_rect_history_[1].frame_time_in_seconds == 0.0) | |
282 visible_rect_history_[1] = visible_rect_history_[0]; | |
283 } | |
284 bool IsTileOccludedOnCurrentTree(const Tile* tile) const; | |
285 bool ShouldCreateTileAt(int i, int j) const; | |
286 bool IsTileOccluded(const Tile* tile) const; | |
287 void UpdateRequiredStatesOnTile(Tile* tile) const; | |
288 PrioritizedTile MakePrioritizedTile( | |
289 Tile* tile, | |
290 PriorityRectType priority_rect_type) const; | |
291 TilePriority ComputePriorityForTile( | |
292 const Tile* tile, | |
293 PriorityRectType priority_rect_type) const; | |
294 PriorityRectType ComputePriorityRectTypeForTile(const Tile* tile) const; | |
295 bool has_visible_rect_tiles() const { return has_visible_rect_tiles_; } | |
296 bool has_skewport_rect_tiles() const { return has_skewport_rect_tiles_; } | |
297 bool has_soon_border_rect_tiles() const { | |
298 return has_soon_border_rect_tiles_; | |
299 } | |
300 bool has_eventually_rect_tiles() const { return has_eventually_rect_tiles_; } | |
301 | |
302 const gfx::Rect& current_visible_rect() const { | |
303 return current_visible_rect_; | |
304 } | |
305 gfx::Rect pending_visible_rect() const { | |
306 const PictureLayerTiling* pending_tiling = | |
307 tree_ == ACTIVE_TREE ? client_->GetPendingOrActiveTwinTiling(this) | |
308 : this; | |
309 if (pending_tiling) | |
310 return pending_tiling->current_visible_rect(); | |
311 return gfx::Rect(); | |
312 } | |
313 const gfx::Rect& current_skewport_rect() const { | |
314 return current_skewport_rect_; | |
315 } | |
316 const gfx::Rect& current_soon_border_rect() const { | |
317 return current_soon_border_rect_; | |
318 } | |
319 const gfx::Rect& current_eventually_rect() const { | |
320 return current_eventually_rect_; | |
321 } | |
322 bool has_ever_been_updated() const { | |
323 return visible_rect_history_[0].frame_time_in_seconds != 0.0; | |
324 } | |
325 void RemoveTilesInRegion(const Region& layer_region, bool recreate_tiles); | |
326 | |
327 const float tiling_interest_area_viewport_multiplier_; | |
328 const float skewport_target_time_in_seconds_; | |
329 const int skewport_extrapolation_limit_in_content_pixels_; | |
330 | |
331 // Given properties. | |
332 const float contents_scale_; | |
333 PictureLayerTilingClient* const client_; | |
334 const WhichTree tree_; | |
335 scoped_refptr<RasterSource> raster_source_; | |
336 TileResolution resolution_; | |
337 | |
338 // Internal data. | |
339 TilingData tiling_data_; | |
340 TileMap tiles_; // It is not legal to have a NULL tile in the tiles_ map. | |
341 gfx::Rect live_tiles_rect_; | |
342 | |
343 gfx::Rect last_viewport_in_layer_space_; | |
344 // State saved for computing velocities based upon finite differences. | |
345 FrameVisibleRect visible_rect_history_[2]; | |
346 | |
347 bool can_require_tiles_for_activation_; | |
348 | |
349 // Iteration rects in content space. | |
350 gfx::Rect current_visible_rect_; | |
351 gfx::Rect current_skewport_rect_; | |
352 gfx::Rect current_soon_border_rect_; | |
353 gfx::Rect current_eventually_rect_; | |
354 // Other properties used for tile iteration and prioritization. | |
355 float current_content_to_screen_scale_; | |
356 Occlusion current_occlusion_in_layer_space_; | |
357 | |
358 bool has_visible_rect_tiles_; | |
359 bool has_skewport_rect_tiles_; | |
360 bool has_soon_border_rect_tiles_; | |
361 bool has_eventually_rect_tiles_; | |
362 | |
363 private: | |
364 DISALLOW_ASSIGN(PictureLayerTiling); | |
365 | |
366 RectExpansionCache expansion_cache_; | |
367 }; | |
368 | |
369 } // namespace cc | |
370 | |
371 #endif // CC_RESOURCES_PICTURE_LAYER_TILING_H_ | |
OLD | NEW |