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_PICTURE_LAYER_TILING_H_
6 #define CC_PICTURE_LAYER_TILING_H_
8 #include "base/basictypes.h"
9 #include "base/hash_tables.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "cc/cc_export.h"
12 #include "cc/hash_pair.h"
13 #include "cc/region.h"
15 #include "cc/tile_priority.h"
16 #include "cc/tiling_data.h"
17 #include "ui/gfx/rect.h"
21 class PictureLayerTiling
;
23 class PictureLayerTilingClient
{
25 // Create a tile at the given content_rect (in the contents scale of the
26 // tiling) This might return null if the client cannot create such a tile.
27 virtual scoped_refptr
<Tile
> CreateTile(
28 PictureLayerTiling
* tiling
,
29 gfx::Rect content_rect
) = 0;
30 virtual void UpdatePile(Tile
* tile
) = 0;
31 virtual gfx::Size
CalculateTileSize(
32 gfx::Size current_tile_size
,
33 gfx::Size content_bounds
) = 0;
36 class CC_EXPORT PictureLayerTiling
{
38 ~PictureLayerTiling();
40 // Create a tiling with no tiles. CreateTiles must be called to add some.
41 static scoped_ptr
<PictureLayerTiling
> Create(float contents_scale
);
42 scoped_ptr
<PictureLayerTiling
> Clone() const;
44 gfx::Size
layer_bounds() const { return layer_bounds_
; }
45 void SetLayerBounds(gfx::Size layer_bounds
);
46 void Invalidate(const Region
& layer_invalidation
);
48 // Add any tiles that intersect with |layer_rect|. If any tiles already
49 // exist, then this leaves them as-is.
50 void CreateTilesFromLayerRect(gfx::Rect layer_rect
);
52 void SetClient(PictureLayerTilingClient
* client
);
53 void set_resolution(TileResolution resolution
) { resolution_
= resolution
; }
54 TileResolution
resolution() const { return resolution_
; }
56 gfx::Rect
ContentRect() const;
57 gfx::SizeF
ContentSizeF() const;
58 float contents_scale() const { return contents_scale_
; }
60 std::vector
<Tile
*> AllTilesForTesting() const {
61 std::vector
<Tile
*> all_tiles
;
62 for (TileMap::const_iterator it
= tiles_
.begin();
63 it
!= tiles_
.end(); ++it
)
64 all_tiles
.push_back(it
->second
);
68 enum LayerDeviceAlignment
{
69 LayerDeviceAlignmentUnknown
,
71 LayerNotAlignedToDevice
,
74 // Iterate over all tiles to fill content_rect. Even if tiles are invalid
75 // (i.e. no valid resource) this tiling should still iterate over them.
76 // The union of all geometry_rect calls for each element iterated over should
77 // exactly equal content_rect and no two geometry_rects should intersect.
78 class CC_EXPORT Iterator
{
81 Iterator(const PictureLayerTiling
* tiling
,
84 LayerDeviceAlignment layerDeviceAlignment
);
87 // Visible rect (no borders), always in the space of content_rect,
88 // regardless of the contents scale of the tiling.
89 gfx::Rect
geometry_rect() const;
90 // Texture rect (in texels) for geometry_rect
91 gfx::RectF
texture_rect() const;
92 gfx::Size
texture_size() const;
94 // Full rect (including borders) of the current tile, always in the space
95 // of content_rect, regardless of the contents scale of the tiling.
96 gfx::Rect
full_tile_geometry_rect() const;
98 Tile
* operator->() const { return current_tile_
; }
99 Tile
* operator*() const { return current_tile_
; }
101 Iterator
& operator++();
102 operator bool() const { return tile_j_
<= bottom_
; }
105 const PictureLayerTiling
* tiling_
;
106 gfx::Rect dest_rect_
;
107 float dest_to_content_scale_x_
;
108 float dest_to_content_scale_y_
;
111 gfx::Rect current_geometry_rect_
;
119 friend class PictureLayerTiling
;
122 Region
OpaqueRegionInContentRect(const gfx::Rect
&) const;
124 void Reset() { return tiles_
.clear(); }
126 void UpdateTilePriorities(
128 gfx::Size device_viewport
,
129 const gfx::RectF
& viewport_in_layer_space
,
130 gfx::Size last_layer_bounds
,
131 gfx::Size current_layer_bounds
,
132 gfx::Size last_layer_content_bounds
,
133 gfx::Size current_layer_content_bounds
,
134 float last_layer_contents_scale
,
135 float current_layer_contents_scale
,
136 const gfx::Transform
& last_screen_transform
,
137 const gfx::Transform
& current_screen_transform
,
138 int current_source_frame_number
,
139 double current_frame_time
,
140 bool store_screen_space_quads_on_tiles
);
142 // Copies the src_tree priority into the dst_tree priority for all tiles.
143 // The src_tree priority is reset to the lowest priority possible. This
144 // also updates the pile on each tile to be the current client's pile.
145 void DidBecomeActive();
147 scoped_ptr
<base::Value
> AsValue() const;
150 typedef std::pair
<int, int> TileMapKey
;
151 typedef base::hash_map
<TileMapKey
, scoped_refptr
<Tile
> > TileMap
;
153 PictureLayerTiling(float contents_scale
);
154 Tile
* TileAt(int, int) const;
155 void CreateTilesFromContentRect(gfx::Rect layer_rect
);
156 void CreateTile(int i
, int j
);
158 PictureLayerTilingClient
* client_
;
159 float contents_scale_
;
160 gfx::Size layer_bounds_
;
161 gfx::Rect last_prioritized_rect_
;
162 // It is not legal to have a NULL tile in the tiles_ map.
164 TilingData tiling_data_
;
165 TileResolution resolution_
;
166 int last_source_frame_number_
;
167 double last_impl_frame_time_
;
169 friend class Iterator
;
174 #endif // CC_PICTURE_LAYER_TILING_H_