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_
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"
24 namespace trace_event
{
31 class PictureLayerTiling
;
32 class PrioritizedTile
;
35 class CC_EXPORT PictureLayerTilingClient
{
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 bool HasValidTilePriorities() const = 0;
49 virtual bool RequiresHighResToDraw() const = 0;
52 virtual ~PictureLayerTilingClient() {}
56 TileMapKey(int x
, int y
) : index_x(x
), index_y(y
) {}
57 explicit TileMapKey(const std::pair
<int, int>& index
)
58 : index_x(index
.first
), index_y(index
.second
) {}
60 bool operator==(const TileMapKey
& other
) const {
61 return index_x
== other
.index_x
&& index_y
== other
.index_y
;
70 namespace BASE_HASH_NAMESPACE
{
72 struct hash
<cc::TileMapKey
> {
73 size_t operator()(const cc::TileMapKey
& key
) const {
74 uint16 value1
= static_cast<uint16
>(key
.index_x
);
75 uint16 value2
= static_cast<uint16
>(key
.index_y
);
76 uint32 value1_32
= value1
;
77 return (value1_32
<< 16) | value2
;
80 } // namespace BASE_HASH_NAMESPACE
84 class CC_EXPORT PictureLayerTiling
{
86 static const int kBorderTexels
= 1;
88 PictureLayerTilingClient
* client() const { return client_
; }
89 ~PictureLayerTiling();
91 static float CalculateSoonBorderDistance(
92 const gfx::Rect
& visible_rect_in_content_space
,
93 float content_to_screen_scale
);
95 // Create a tiling with no tiles. CreateTile() must be called to add some.
96 static scoped_ptr
<PictureLayerTiling
> Create(
99 scoped_refptr
<RasterSource
> raster_source
,
100 PictureLayerTilingClient
* client
,
101 size_t tiling_interest_area_padding
,
102 float skewport_target_time_in_seconds
,
103 int skewport_extrapolation_limit_in_content_pixels
);
105 void SetRasterSourceAndResize(scoped_refptr
<RasterSource
> raster_source
);
106 void Invalidate(const Region
& layer_invalidation
);
107 void CreateMissingTilesInLiveTilesRect();
108 void TakeTilesAndPropertiesFrom(PictureLayerTiling
* pending_twin
,
109 const Region
& layer_invalidation
);
111 bool IsTileRequiredForActivation(const Tile
* tile
) const;
112 bool IsTileRequiredForDraw(const Tile
* tile
) const;
114 void set_resolution(TileResolution resolution
) {
115 resolution_
= resolution
;
116 was_ever_low_resolution_
|= resolution
== LOW_RESOLUTION
;
118 TileResolution
resolution() const { return resolution_
; }
119 bool was_ever_low_resolution() const { return was_ever_low_resolution_
; }
120 void set_can_require_tiles_for_activation(bool can_require_tiles
) {
121 can_require_tiles_for_activation_
= can_require_tiles
;
124 RasterSource
* raster_source() const { return raster_source_
.get(); }
125 gfx::Size
tiling_size() const { return tiling_data_
.tiling_size(); }
126 gfx::Rect
live_tiles_rect() const { return live_tiles_rect_
; }
127 gfx::Size
tile_size() const { return tiling_data_
.max_texture_size(); }
128 float contents_scale() const { return contents_scale_
; }
129 const TilingData
* tiling_data() const { return &tiling_data_
; }
131 Tile
* TileAt(int i
, int j
) const {
132 TileMap::const_iterator iter
= tiles_
.find(TileMapKey(i
, j
));
133 return iter
== tiles_
.end() ? nullptr : iter
->second
;
136 bool has_tiles() const { return !tiles_
.empty(); }
137 // all_tiles_done() can return false negatives.
138 bool all_tiles_done() const { return all_tiles_done_
; }
139 void set_all_tiles_done(bool all_tiles_done
) {
140 all_tiles_done_
= all_tiles_done
;
143 void VerifyNoTileNeedsRaster() const {
145 for (const auto tile_pair
: tiles_
) {
146 DCHECK(!tile_pair
.second
->draw_info().NeedsRaster() ||
147 IsTileOccluded(tile_pair
.second
));
149 #endif // DCHECK_IS_ON()
152 // For testing functionality.
153 void CreateAllTilesForTesting() {
154 SetLiveTilesRect(gfx::Rect(tiling_data_
.tiling_size()));
156 const TilingData
& TilingDataForTesting() const { return tiling_data_
; }
157 std::vector
<Tile
*> AllTilesForTesting() const {
158 std::vector
<Tile
*> all_tiles
;
159 for (TileMap::const_iterator it
= tiles_
.begin(); it
!= tiles_
.end(); ++it
)
160 all_tiles
.push_back(it
->second
);
164 void UpdateAllRequiredStateForTesting() {
165 for (const auto& key_tile_pair
: tiles_
)
166 UpdateRequiredStatesOnTile(key_tile_pair
.second
);
168 std::map
<const Tile
*, PrioritizedTile
>
169 UpdateAndGetAllPrioritizedTilesForTesting() const;
171 void SetAllTilesOccludedForTesting() {
172 gfx::Rect viewport_in_layer_space
=
173 ScaleToEnclosingRect(current_visible_rect_
, 1.0f
/ contents_scale_
);
174 current_occlusion_in_layer_space_
=
175 Occlusion(gfx::Transform(),
176 SimpleEnclosedRegion(viewport_in_layer_space
),
177 SimpleEnclosedRegion(viewport_in_layer_space
));
179 const gfx::Rect
& GetCurrentVisibleRectForTesting() const {
180 return current_visible_rect_
;
182 void SetTilePriorityRectsForTesting(
183 const gfx::Rect
& visible_rect_in_content_space
,
184 const gfx::Rect
& skewport
,
185 const gfx::Rect
& soon_border_rect
,
186 const gfx::Rect
& eventually_rect
) {
187 SetTilePriorityRects(1.0f
, visible_rect_in_content_space
, skewport
,
188 soon_border_rect
, eventually_rect
, Occlusion());
191 // Iterate over all tiles to fill content_rect. Even if tiles are invalid
192 // (i.e. no valid resource) this tiling should still iterate over them.
193 // The union of all geometry_rect calls for each element iterated over should
194 // exactly equal content_rect and no two geometry_rects should intersect.
195 class CC_EXPORT CoverageIterator
{
198 CoverageIterator(const PictureLayerTiling
* tiling
,
200 const gfx::Rect
& rect
);
203 // Visible rect (no borders), always in the space of content_rect,
204 // regardless of the contents scale of the tiling.
205 gfx::Rect
geometry_rect() const;
206 // Texture rect (in texels) for geometry_rect
207 gfx::RectF
texture_rect() const;
209 Tile
* operator->() const { return current_tile_
; }
210 Tile
* operator*() const { return current_tile_
; }
212 CoverageIterator
& operator++();
213 operator bool() const { return tile_j_
<= bottom_
; }
215 int i() const { return tile_i_
; }
216 int j() const { return tile_j_
; }
219 const PictureLayerTiling
* tiling_
;
220 gfx::Rect dest_rect_
;
221 float dest_to_content_scale_
;
224 gfx::Rect current_geometry_rect_
;
232 friend class PictureLayerTiling
;
237 bool ComputeTilePriorityRects(const gfx::Rect
& viewport_in_layer_space
,
238 float ideal_contents_scale
,
239 double current_frame_time_in_seconds
,
240 const Occlusion
& occlusion_in_layer_space
);
242 void GetAllPrioritizedTilesForTracing(
243 std::vector
<PrioritizedTile
>* prioritized_tiles
) const;
244 void AsValueInto(base::trace_event::TracedValue
* array
) const;
245 size_t GPUMemoryUsageInBytes() const;
248 friend class CoverageIterator
;
249 friend class PrioritizedTile
;
250 friend class TilingSetRasterQueueAll
;
251 friend class TilingSetRasterQueueRequired
;
252 friend class TilingSetEvictionQueue
;
254 // PENDING VISIBLE RECT refers to the visible rect that will become current
255 // upon activation (ie, the pending tree's visible rect). Tiles in this
256 // region that are not part of the current visible rect are all handled
257 // here. Note that when processing a pending tree, this rect is the same as
258 // the visible rect so no tiles are processed in this case.
259 enum PriorityRectType
{
261 PENDING_VISIBLE_RECT
,
267 using TileMap
= base::ScopedPtrHashMap
<TileMapKey
, ScopedTilePtr
>;
269 struct FrameVisibleRect
{
270 gfx::Rect visible_rect_in_content_space
;
271 double frame_time_in_seconds
= 0.0;
274 PictureLayerTiling(WhichTree tree
,
275 float contents_scale
,
276 scoped_refptr
<RasterSource
> raster_source
,
277 PictureLayerTilingClient
* client
,
278 size_t tiling_interest_area_padding
,
279 float skewport_target_time_in_seconds
,
280 int skewport_extrapolation_limit_in_content_pixels
);
281 void SetLiveTilesRect(const gfx::Rect
& live_tiles_rect
);
282 void VerifyLiveTilesRect(bool is_on_recycle_tree
) const;
283 Tile
* CreateTile(int i
, int j
);
284 ScopedTilePtr
TakeTileAt(int i
, int j
);
285 // Returns true if the Tile existed and was removed from the tiling.
286 bool RemoveTileAt(int i
, int j
);
287 bool TilingMatchesTileIndices(const PictureLayerTiling
* twin
) const;
289 // Computes a skewport. The calculation extrapolates the last visible
290 // rect and the current visible rect to expand the skewport to where it
291 // would be in |skewport_target_time| seconds. Note that the skewport
292 // is guaranteed to contain the current visible rect.
293 gfx::Rect
ComputeSkewport(double current_frame_time_in_seconds
,
294 const gfx::Rect
& visible_rect_in_content_space
)
297 // Save the required data for computing tile priorities later.
298 void SetTilePriorityRects(float content_to_screen_scale_
,
299 const gfx::Rect
& visible_rect_in_content_space
,
300 const gfx::Rect
& skewport
,
301 const gfx::Rect
& soon_border_rect
,
302 const gfx::Rect
& eventually_rect
,
303 const Occlusion
& occlusion_in_layer_space
);
305 bool NeedsUpdateForFrameAtTimeAndViewport(
306 double frame_time_in_seconds
,
307 const gfx::Rect
& viewport_in_layer_space
) {
308 return frame_time_in_seconds
!=
309 visible_rect_history_
[0].frame_time_in_seconds
||
310 viewport_in_layer_space
!= last_viewport_in_layer_space_
;
312 void UpdateVisibleRectHistory(
313 double frame_time_in_seconds
,
314 const gfx::Rect
& visible_rect_in_content_space
) {
315 visible_rect_history_
[1] = visible_rect_history_
[0];
316 visible_rect_history_
[0].frame_time_in_seconds
= frame_time_in_seconds
;
317 visible_rect_history_
[0].visible_rect_in_content_space
=
318 visible_rect_in_content_space
;
319 // If we don't have a second history item, set it to the most recent one.
320 if (visible_rect_history_
[1].frame_time_in_seconds
== 0.0)
321 visible_rect_history_
[1] = visible_rect_history_
[0];
323 bool IsTileOccludedOnCurrentTree(const Tile
* tile
) const;
324 bool ShouldCreateTileAt(int i
, int j
) const;
325 bool IsTileOccluded(const Tile
* tile
) const;
326 void UpdateRequiredStatesOnTile(Tile
* tile
) const;
327 PrioritizedTile
MakePrioritizedTile(
329 PriorityRectType priority_rect_type
) const;
330 TilePriority
ComputePriorityForTile(
332 PriorityRectType priority_rect_type
) const;
333 PriorityRectType
ComputePriorityRectTypeForTile(const Tile
* tile
) const;
334 bool has_visible_rect_tiles() const { return has_visible_rect_tiles_
; }
335 bool has_skewport_rect_tiles() const { return has_skewport_rect_tiles_
; }
336 bool has_soon_border_rect_tiles() const {
337 return has_soon_border_rect_tiles_
;
339 bool has_eventually_rect_tiles() const { return has_eventually_rect_tiles_
; }
341 const gfx::Rect
& current_visible_rect() const {
342 return current_visible_rect_
;
344 gfx::Rect
pending_visible_rect() const {
345 const PictureLayerTiling
* pending_tiling
=
346 tree_
== ACTIVE_TREE
? client_
->GetPendingOrActiveTwinTiling(this)
349 return pending_tiling
->current_visible_rect();
352 const gfx::Rect
& current_skewport_rect() const {
353 return current_skewport_rect_
;
355 const gfx::Rect
& current_soon_border_rect() const {
356 return current_soon_border_rect_
;
358 const gfx::Rect
& current_eventually_rect() const {
359 return current_eventually_rect_
;
361 bool has_ever_been_updated() const {
362 return visible_rect_history_
[0].frame_time_in_seconds
!= 0.0;
364 void RemoveTilesInRegion(const Region
& layer_region
, bool recreate_tiles
);
366 const size_t tiling_interest_area_padding_
;
367 const float skewport_target_time_in_seconds_
;
368 const int skewport_extrapolation_limit_in_content_pixels_
;
371 const float contents_scale_
;
372 PictureLayerTilingClient
* const client_
;
373 const WhichTree tree_
;
374 scoped_refptr
<RasterSource
> raster_source_
;
375 TileResolution resolution_
;
376 bool was_ever_low_resolution_
;
379 TilingData tiling_data_
;
380 TileMap tiles_
; // It is not legal to have a NULL tile in the tiles_ map.
381 gfx::Rect live_tiles_rect_
;
383 gfx::Rect last_viewport_in_layer_space_
;
384 // State saved for computing velocities based upon finite differences.
385 FrameVisibleRect visible_rect_history_
[2];
387 bool can_require_tiles_for_activation_
;
389 // Iteration rects in content space.
390 gfx::Rect current_visible_rect_
;
391 gfx::Rect current_skewport_rect_
;
392 gfx::Rect current_soon_border_rect_
;
393 gfx::Rect current_eventually_rect_
;
394 // Other properties used for tile iteration and prioritization.
395 float current_content_to_screen_scale_
;
396 Occlusion current_occlusion_in_layer_space_
;
398 bool has_visible_rect_tiles_
;
399 bool has_skewport_rect_tiles_
;
400 bool has_soon_border_rect_tiles_
;
401 bool has_eventually_rect_tiles_
;
402 bool all_tiles_done_
;
405 DISALLOW_ASSIGN(PictureLayerTiling
);
410 #endif // CC_TILES_PICTURE_LAYER_TILING_H_