Roll src/third_party/WebKit 5bc05e9:3d59927 (svn 202625:202627)
[chromium-blink-merge.git] / cc / tiles / picture_layer_tiling.h
blob08380b1d78c8c22b1d0d3d9a6f6ba07fdb0166f0
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.
5 #ifndef CC_TILES_PICTURE_LAYER_TILING_H_
6 #define CC_TILES_PICTURE_LAYER_TILING_H_
8 #include <map>
9 #include <utility>
10 #include <vector>
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/tiles/tile.h"
19 #include "cc/tiles/tile_priority.h"
20 #include "cc/trees/occlusion.h"
21 #include "ui/gfx/geometry/rect.h"
23 namespace base {
24 namespace trace_event {
25 class TracedValue;
29 namespace cc {
31 class PictureLayerTiling;
32 class PrioritizedTile;
33 class RasterSource;
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(const Tile::CreateInfo& info) = 0;
40 virtual gfx::Size CalculateTileSize(
41 const gfx::Size& content_bounds) const = 0;
42 // This invalidation region defines the area (if any, it can by null) that
43 // tiles can not be shared between pending and active trees.
44 virtual const Region* GetPendingInvalidation() = 0;
45 virtual const PictureLayerTiling* GetPendingOrActiveTwinTiling(
46 const PictureLayerTiling* tiling) const = 0;
47 virtual bool HasValidTilePriorities() const = 0;
48 virtual bool RequiresHighResToDraw() const = 0;
50 protected:
51 virtual ~PictureLayerTilingClient() {}
54 struct TileMapKey {
55 TileMapKey(int x, int y) : index_x(x), index_y(y) {}
56 explicit TileMapKey(const std::pair<int, int>& index)
57 : index_x(index.first), index_y(index.second) {}
59 bool operator==(const TileMapKey& other) const {
60 return index_x == other.index_x && index_y == other.index_y;
63 int index_x;
64 int index_y;
67 } // namespace cc
69 namespace BASE_HASH_NAMESPACE {
70 template <>
71 struct hash<cc::TileMapKey> {
72 size_t operator()(const cc::TileMapKey& key) const {
73 uint16 value1 = static_cast<uint16>(key.index_x);
74 uint16 value2 = static_cast<uint16>(key.index_y);
75 uint32 value1_32 = value1;
76 return (value1_32 << 16) | value2;
79 } // namespace BASE_HASH_NAMESPACE
81 namespace cc {
83 class CC_EXPORT PictureLayerTiling {
84 public:
85 static const int kBorderTexels = 1;
87 PictureLayerTilingClient* client() const { return client_; }
88 ~PictureLayerTiling();
90 static float CalculateSoonBorderDistance(
91 const gfx::Rect& visible_rect_in_content_space,
92 float content_to_screen_scale);
94 // Create a tiling with no tiles. CreateTile() must be called to add some.
95 static scoped_ptr<PictureLayerTiling> Create(
96 WhichTree tree,
97 float contents_scale,
98 scoped_refptr<RasterSource> raster_source,
99 PictureLayerTilingClient* client,
100 size_t tiling_interest_area_padding,
101 float skewport_target_time_in_seconds,
102 int skewport_extrapolation_limit_in_content_pixels);
104 void SetRasterSourceAndResize(scoped_refptr<RasterSource> raster_source);
105 void Invalidate(const Region& layer_invalidation);
106 void CreateMissingTilesInLiveTilesRect();
107 void TakeTilesAndPropertiesFrom(PictureLayerTiling* pending_twin,
108 const Region& layer_invalidation);
110 bool IsTileRequiredForActivation(const Tile* tile) const;
111 bool IsTileRequiredForDraw(const Tile* tile) const;
113 void set_resolution(TileResolution resolution) {
114 resolution_ = resolution;
115 may_contain_low_resolution_tiles_ |= resolution == LOW_RESOLUTION;
117 TileResolution resolution() const { return resolution_; }
118 bool may_contain_low_resolution_tiles() const {
119 return may_contain_low_resolution_tiles_;
121 void reset_may_contain_low_resolution_tiles() {
122 may_contain_low_resolution_tiles_ = false;
124 void set_can_require_tiles_for_activation(bool can_require_tiles) {
125 can_require_tiles_for_activation_ = can_require_tiles;
128 RasterSource* raster_source() const { return raster_source_.get(); }
129 gfx::Size tiling_size() const { return tiling_data_.tiling_size(); }
130 gfx::Rect live_tiles_rect() const { return live_tiles_rect_; }
131 gfx::Size tile_size() const { return tiling_data_.max_texture_size(); }
132 float contents_scale() const { return contents_scale_; }
133 const TilingData* tiling_data() const { return &tiling_data_; }
135 Tile* TileAt(int i, int j) const {
136 TileMap::const_iterator iter = tiles_.find(TileMapKey(i, j));
137 return iter == tiles_.end() ? nullptr : iter->second;
140 bool has_tiles() const { return !tiles_.empty(); }
141 // all_tiles_done() can return false negatives.
142 bool all_tiles_done() const { return all_tiles_done_; }
143 void set_all_tiles_done(bool all_tiles_done) {
144 all_tiles_done_ = all_tiles_done;
147 void VerifyNoTileNeedsRaster() const {
148 #if DCHECK_IS_ON()
149 for (const auto tile_pair : tiles_) {
150 DCHECK(!tile_pair.second->draw_info().NeedsRaster() ||
151 IsTileOccluded(tile_pair.second));
153 #endif // DCHECK_IS_ON()
156 // For testing functionality.
157 void CreateAllTilesForTesting() {
158 SetLiveTilesRect(gfx::Rect(tiling_data_.tiling_size()));
160 const TilingData& TilingDataForTesting() const { return tiling_data_; }
161 std::vector<Tile*> AllTilesForTesting() const {
162 std::vector<Tile*> all_tiles;
163 for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end(); ++it)
164 all_tiles.push_back(it->second);
165 return all_tiles;
168 void UpdateAllRequiredStateForTesting() {
169 for (const auto& key_tile_pair : tiles_)
170 UpdateRequiredStatesOnTile(key_tile_pair.second);
172 std::map<const Tile*, PrioritizedTile>
173 UpdateAndGetAllPrioritizedTilesForTesting() const;
175 void SetAllTilesOccludedForTesting() {
176 gfx::Rect viewport_in_layer_space =
177 ScaleToEnclosingRect(current_visible_rect_, 1.0f / contents_scale_);
178 current_occlusion_in_layer_space_ =
179 Occlusion(gfx::Transform(),
180 SimpleEnclosedRegion(viewport_in_layer_space),
181 SimpleEnclosedRegion(viewport_in_layer_space));
183 const gfx::Rect& GetCurrentVisibleRectForTesting() const {
184 return current_visible_rect_;
186 void SetTilePriorityRectsForTesting(
187 const gfx::Rect& visible_rect_in_content_space,
188 const gfx::Rect& skewport,
189 const gfx::Rect& soon_border_rect,
190 const gfx::Rect& eventually_rect) {
191 SetTilePriorityRects(1.0f, visible_rect_in_content_space, skewport,
192 soon_border_rect, eventually_rect, Occlusion());
195 // Iterate over all tiles to fill content_rect. Even if tiles are invalid
196 // (i.e. no valid resource) this tiling should still iterate over them.
197 // The union of all geometry_rect calls for each element iterated over should
198 // exactly equal content_rect and no two geometry_rects should intersect.
199 class CC_EXPORT CoverageIterator {
200 public:
201 CoverageIterator();
202 CoverageIterator(const PictureLayerTiling* tiling,
203 float dest_scale,
204 const gfx::Rect& rect);
205 ~CoverageIterator();
207 // Visible rect (no borders), always in the space of content_rect,
208 // regardless of the contents scale of the tiling.
209 gfx::Rect geometry_rect() const;
210 // Texture rect (in texels) for geometry_rect
211 gfx::RectF texture_rect() const;
213 Tile* operator->() const { return current_tile_; }
214 Tile* operator*() const { return current_tile_; }
216 CoverageIterator& operator++();
217 operator bool() const { return tile_j_ <= bottom_; }
219 int i() const { return tile_i_; }
220 int j() const { return tile_j_; }
222 private:
223 const PictureLayerTiling* tiling_;
224 gfx::Rect dest_rect_;
225 float dest_to_content_scale_;
227 Tile* current_tile_;
228 gfx::Rect current_geometry_rect_;
229 int tile_i_;
230 int tile_j_;
231 int left_;
232 int top_;
233 int right_;
234 int bottom_;
236 friend class PictureLayerTiling;
239 void Reset();
241 bool ComputeTilePriorityRects(const gfx::Rect& viewport_in_layer_space,
242 float ideal_contents_scale,
243 double current_frame_time_in_seconds,
244 const Occlusion& occlusion_in_layer_space);
246 void GetAllPrioritizedTilesForTracing(
247 std::vector<PrioritizedTile>* prioritized_tiles) const;
248 void AsValueInto(base::trace_event::TracedValue* array) const;
249 size_t GPUMemoryUsageInBytes() const;
251 protected:
252 friend class CoverageIterator;
253 friend class PrioritizedTile;
254 friend class TilingSetRasterQueueAll;
255 friend class TilingSetRasterQueueRequired;
256 friend class TilingSetEvictionQueue;
258 // PENDING VISIBLE RECT refers to the visible rect that will become current
259 // upon activation (ie, the pending tree's visible rect). Tiles in this
260 // region that are not part of the current visible rect are all handled
261 // here. Note that when processing a pending tree, this rect is the same as
262 // the visible rect so no tiles are processed in this case.
263 enum PriorityRectType {
264 VISIBLE_RECT,
265 PENDING_VISIBLE_RECT,
266 SKEWPORT_RECT,
267 SOON_BORDER_RECT,
268 EVENTUALLY_RECT
271 using TileMap = base::ScopedPtrHashMap<TileMapKey, ScopedTilePtr>;
273 struct FrameVisibleRect {
274 gfx::Rect visible_rect_in_content_space;
275 double frame_time_in_seconds = 0.0;
278 PictureLayerTiling(WhichTree tree,
279 float contents_scale,
280 scoped_refptr<RasterSource> raster_source,
281 PictureLayerTilingClient* client,
282 size_t tiling_interest_area_padding,
283 float skewport_target_time_in_seconds,
284 int skewport_extrapolation_limit_in_content_pixels);
285 void SetLiveTilesRect(const gfx::Rect& live_tiles_rect);
286 void VerifyLiveTilesRect(bool is_on_recycle_tree) const;
287 Tile* CreateTile(const Tile::CreateInfo& info);
288 ScopedTilePtr TakeTileAt(int i, int j);
289 // Returns true if the Tile existed and was removed from the tiling.
290 bool RemoveTileAt(int i, int j);
291 bool TilingMatchesTileIndices(const PictureLayerTiling* twin) const;
293 // Computes a skewport. The calculation extrapolates the last visible
294 // rect and the current visible rect to expand the skewport to where it
295 // would be in |skewport_target_time| seconds. Note that the skewport
296 // is guaranteed to contain the current visible rect.
297 gfx::Rect ComputeSkewport(double current_frame_time_in_seconds,
298 const gfx::Rect& visible_rect_in_content_space)
299 const;
301 // Save the required data for computing tile priorities later.
302 void SetTilePriorityRects(float content_to_screen_scale_,
303 const gfx::Rect& visible_rect_in_content_space,
304 const gfx::Rect& skewport,
305 const gfx::Rect& soon_border_rect,
306 const gfx::Rect& eventually_rect,
307 const Occlusion& occlusion_in_layer_space);
309 bool NeedsUpdateForFrameAtTimeAndViewport(
310 double frame_time_in_seconds,
311 const gfx::Rect& viewport_in_layer_space) {
312 return frame_time_in_seconds !=
313 visible_rect_history_[0].frame_time_in_seconds ||
314 viewport_in_layer_space != last_viewport_in_layer_space_;
316 void UpdateVisibleRectHistory(
317 double frame_time_in_seconds,
318 const gfx::Rect& visible_rect_in_content_space) {
319 visible_rect_history_[1] = visible_rect_history_[0];
320 visible_rect_history_[0].frame_time_in_seconds = frame_time_in_seconds;
321 visible_rect_history_[0].visible_rect_in_content_space =
322 visible_rect_in_content_space;
323 // If we don't have a second history item, set it to the most recent one.
324 if (visible_rect_history_[1].frame_time_in_seconds == 0.0)
325 visible_rect_history_[1] = visible_rect_history_[0];
327 bool IsTileOccludedOnCurrentTree(const Tile* tile) const;
328 Tile::CreateInfo CreateInfoForTile(int i, int j) const;
329 bool ShouldCreateTileAt(const Tile::CreateInfo& info) const;
330 bool IsTileOccluded(const Tile* tile) const;
331 void UpdateRequiredStatesOnTile(Tile* tile) const;
332 PrioritizedTile MakePrioritizedTile(
333 Tile* tile,
334 PriorityRectType priority_rect_type) const;
335 TilePriority ComputePriorityForTile(
336 const Tile* tile,
337 PriorityRectType priority_rect_type) const;
338 PriorityRectType ComputePriorityRectTypeForTile(const Tile* tile) const;
339 bool has_visible_rect_tiles() const { return has_visible_rect_tiles_; }
340 bool has_skewport_rect_tiles() const { return has_skewport_rect_tiles_; }
341 bool has_soon_border_rect_tiles() const {
342 return has_soon_border_rect_tiles_;
344 bool has_eventually_rect_tiles() const { return has_eventually_rect_tiles_; }
346 const gfx::Rect& current_visible_rect() const {
347 return current_visible_rect_;
349 gfx::Rect pending_visible_rect() const {
350 const PictureLayerTiling* pending_tiling =
351 tree_ == ACTIVE_TREE ? client_->GetPendingOrActiveTwinTiling(this)
352 : this;
353 if (pending_tiling)
354 return pending_tiling->current_visible_rect();
355 return gfx::Rect();
357 const gfx::Rect& current_skewport_rect() const {
358 return current_skewport_rect_;
360 const gfx::Rect& current_soon_border_rect() const {
361 return current_soon_border_rect_;
363 const gfx::Rect& current_eventually_rect() const {
364 return current_eventually_rect_;
366 bool has_ever_been_updated() const {
367 return visible_rect_history_[0].frame_time_in_seconds != 0.0;
369 void RemoveTilesInRegion(const Region& layer_region, bool recreate_tiles);
371 const size_t tiling_interest_area_padding_;
372 const float skewport_target_time_in_seconds_;
373 const int skewport_extrapolation_limit_in_content_pixels_;
375 // Given properties.
376 const float contents_scale_;
377 PictureLayerTilingClient* const client_;
378 const WhichTree tree_;
379 scoped_refptr<RasterSource> raster_source_;
380 TileResolution resolution_;
381 bool may_contain_low_resolution_tiles_;
383 // Internal data.
384 TilingData tiling_data_;
385 TileMap tiles_; // It is not legal to have a NULL tile in the tiles_ map.
386 gfx::Rect live_tiles_rect_;
388 gfx::Rect last_viewport_in_layer_space_;
389 // State saved for computing velocities based upon finite differences.
390 FrameVisibleRect visible_rect_history_[2];
392 bool can_require_tiles_for_activation_;
394 // Iteration rects in content space.
395 gfx::Rect current_visible_rect_;
396 gfx::Rect current_skewport_rect_;
397 gfx::Rect current_soon_border_rect_;
398 gfx::Rect current_eventually_rect_;
399 // Other properties used for tile iteration and prioritization.
400 float current_content_to_screen_scale_;
401 Occlusion current_occlusion_in_layer_space_;
403 bool has_visible_rect_tiles_;
404 bool has_skewport_rect_tiles_;
405 bool has_soon_border_rect_tiles_;
406 bool has_eventually_rect_tiles_;
407 bool all_tiles_done_;
409 private:
410 DISALLOW_ASSIGN(PictureLayerTiling);
413 } // namespace cc
415 #endif // CC_TILES_PICTURE_LAYER_TILING_H_