OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 #include "cc/resources/active_picture_layer_tiling.h" | |
6 | |
7 #include "ui/gfx/geometry/point_conversions.h" | |
8 #include "ui/gfx/geometry/rect_conversions.h" | |
9 #include "ui/gfx/geometry/safe_integer_conversions.h" | |
10 #include "ui/gfx/geometry/size_conversions.h" | |
11 | |
12 namespace cc { | |
13 | |
14 scoped_ptr<PictureLayerTiling> ActivePictureLayerTiling::Create( | |
15 float contents_scale, | |
16 scoped_refptr<RasterSource> raster_source, | |
17 PictureLayerTilingClient* client, | |
18 size_t max_tiles_for_interest_area, | |
19 float skewport_target_time_in_seconds, | |
20 int skewport_extrapolation_limit_in_content_pixels) { | |
21 return make_scoped_ptr(new ActivePictureLayerTiling( | |
22 contents_scale, raster_source, client, max_tiles_for_interest_area, | |
23 skewport_target_time_in_seconds, | |
24 skewport_extrapolation_limit_in_content_pixels)); | |
25 } | |
26 | |
27 ActivePictureLayerTiling::ActivePictureLayerTiling( | |
28 float contents_scale, | |
29 scoped_refptr<RasterSource> raster_source, | |
30 PictureLayerTilingClient* client, | |
31 size_t max_tiles_for_interest_area, | |
32 float skewport_target_time_in_seconds, | |
33 int skewport_extrapolation_limit_in_content_pixels) | |
34 : PictureLayerTiling(contents_scale, | |
35 raster_source, | |
36 client, | |
37 max_tiles_for_interest_area, | |
38 skewport_target_time_in_seconds, | |
39 skewport_extrapolation_limit_in_content_pixels) { | |
40 } | |
41 | |
42 ActivePictureLayerTiling::~ActivePictureLayerTiling() { | |
43 } | |
44 | |
45 void ActivePictureLayerTiling::SetRasterSourceAndResize( | |
46 const scoped_refptr<RasterSource> raster_source) { | |
47 DCHECK(!raster_source->IsSolidColor()); | |
48 gfx::Size old_layer_bounds = raster_source_->GetSize(); | |
49 raster_source_ = raster_source; | |
50 gfx::Size new_layer_bounds = raster_source_->GetSize(); | |
51 gfx::Size content_bounds = | |
52 gfx::ToCeiledSize(gfx::ScaleSize(new_layer_bounds, contents_scale_)); | |
53 gfx::Size tile_size = client_->CalculateTileSize(content_bounds); | |
54 | |
55 if (tile_size != tiling_data_.max_texture_size()) { | |
56 tiling_data_.SetTilingSize(content_bounds); | |
57 tiling_data_.SetMaxTextureSize(tile_size); | |
58 // When the tile size changes, the TilingData positions no longer work | |
59 // as valid keys to the TileMap, so just drop all tiles and clear the live | |
60 // tiles rect. | |
61 Reset(); | |
62 return; | |
63 } | |
64 | |
65 if (old_layer_bounds == new_layer_bounds) | |
66 return; | |
67 | |
68 // The SetLiveTilesRect() method would drop tiles outside the new bounds, | |
69 // but may do so incorrectly if resizing the tiling causes the number of | |
70 // tiles in the tiling_data_ to change. | |
71 gfx::Rect content_rect(content_bounds); | |
72 int before_left = tiling_data_.TileXIndexFromSrcCoord(live_tiles_rect_.x()); | |
73 int before_top = tiling_data_.TileYIndexFromSrcCoord(live_tiles_rect_.y()); | |
74 int before_right = | |
75 tiling_data_.TileXIndexFromSrcCoord(live_tiles_rect_.right() - 1); | |
76 int before_bottom = | |
77 tiling_data_.TileYIndexFromSrcCoord(live_tiles_rect_.bottom() - 1); | |
78 | |
79 // The live_tiles_rect_ is clamped to stay within the tiling size as we | |
80 // change it. | |
81 live_tiles_rect_.Intersect(content_rect); | |
82 tiling_data_.SetTilingSize(content_bounds); | |
83 | |
84 int after_right = -1; | |
85 int after_bottom = -1; | |
86 if (!live_tiles_rect_.IsEmpty()) { | |
87 after_right = | |
88 tiling_data_.TileXIndexFromSrcCoord(live_tiles_rect_.right() - 1); | |
89 after_bottom = | |
90 tiling_data_.TileYIndexFromSrcCoord(live_tiles_rect_.bottom() - 1); | |
91 } | |
92 | |
93 // Drop tiles outside the new layer bounds if the layer shrank. | |
94 for (int i = after_right + 1; i <= before_right; ++i) { | |
95 for (int j = before_top; j <= before_bottom; ++j) | |
96 RemoveTileAt(i, j); | |
97 } | |
98 for (int i = before_left; i <= after_right; ++i) { | |
99 for (int j = after_bottom + 1; j <= before_bottom; ++j) | |
100 RemoveTileAt(i, j); | |
101 } | |
102 | |
103 CreateMissingTilesInLiveTilesRect(); | |
104 } | |
105 | |
106 void ActivePictureLayerTiling::Invalidate(const Region& layer_invalidation) { | |
107 // We only invalidate the active tiling when it's orphaned: it has no pending | |
108 // twin, so it's slated for removal in the future. | |
109 DCHECK(!client_->GetPendingOrActiveTwinTiling(this)); | |
110 if (live_tiles_rect_.IsEmpty()) | |
111 return; | |
112 std::vector<TileMapKey> new_tile_keys; | |
113 gfx::Rect expanded_live_tiles_rect = | |
114 tiling_data_.ExpandRectIgnoringBordersToTileBounds(live_tiles_rect_); | |
115 for (Region::Iterator iter(layer_invalidation); iter.has_rect(); | |
116 iter.next()) { | |
117 gfx::Rect layer_rect = iter.rect(); | |
118 gfx::Rect content_rect = | |
119 gfx::ScaleToEnclosingRect(layer_rect, contents_scale_); | |
120 // Consider tiles inside the live tiles rect even if only their border | |
121 // pixels intersect the invalidation. But don't consider tiles outside | |
122 // the live tiles rect with the same conditions, as they won't exist. | |
123 int border_pixels = tiling_data_.border_texels(); | |
124 content_rect.Inset(-border_pixels, -border_pixels); | |
125 // Avoid needless work by not bothering to invalidate where there aren't | |
126 // tiles. | |
127 content_rect.Intersect(expanded_live_tiles_rect); | |
128 if (content_rect.IsEmpty()) | |
129 continue; | |
130 // Since the content_rect includes border pixels already, don't include | |
131 // borders when iterating to avoid double counting them. | |
132 bool include_borders = false; | |
133 for ( | |
134 TilingData::Iterator iter(&tiling_data_, content_rect, include_borders); | |
135 iter; ++iter) { | |
136 if (RemoveTileAt(iter.index_x(), iter.index_y())) | |
137 new_tile_keys.push_back(iter.index()); | |
138 } | |
139 } | |
140 | |
141 for (const auto& key : new_tile_keys) | |
142 CreateTile(key.first, key.second); | |
143 } | |
144 | |
145 void ActivePictureLayerTiling::SetRasterSourceOnTiles() { | |
146 for (auto& tile_pair : tiles_) | |
147 tile_pair.second->set_raster_source(raster_source_.get()); | |
148 } | |
149 | |
150 void ActivePictureLayerTiling::CreateMissingTilesInLiveTilesRect() { | |
151 bool include_borders = false; | |
152 for (TilingData::Iterator iter(&tiling_data_, live_tiles_rect_, | |
153 include_borders); | |
154 iter; ++iter) { | |
155 TileMapKey key = iter.index(); | |
156 TileMap::iterator find = tiles_.find(key); | |
157 if (find != tiles_.end()) | |
158 continue; | |
159 CreateTile(key.first, key.second); | |
160 } | |
161 | |
162 VerifyLiveTilesRect(false); | |
163 } | |
164 | |
165 void ActivePictureLayerTiling::TakeTilesAndPropertiesFrom( | |
166 PictureLayerTiling* pending_twin) { | |
167 SetRasterSourceAndResize(pending_twin->raster_source_); | |
168 resolution_ = pending_twin->resolution_; | |
169 SetLiveTilesRect(pending_twin->live_tiles_rect()); | |
170 | |
171 for (const auto& tile_pair : pending_twin->tiles_) | |
172 tiles_[tile_pair.first] = tile_pair.second; | |
173 pending_twin->tiles_.clear(); | |
174 | |
175 VerifyLiveTilesRect(false); | |
176 | |
177 SetTilePriorityRects(pending_twin->current_content_to_screen_scale_, | |
178 pending_twin->current_visible_rect_, | |
179 pending_twin->current_skewport_rect_, | |
180 pending_twin->current_soon_border_rect_, | |
181 pending_twin->current_eventually_rect_, | |
182 pending_twin->current_occlusion_in_layer_space_); | |
183 } | |
184 | |
185 bool ActivePictureLayerTiling::IsTileOccluded(const Tile* tile) const { | |
186 if (!IsTileOccludedOnActiveTree(tile)) | |
187 return false; | |
188 | |
189 // If we decide that the tile is occluded, but the same tile would not be | |
enne (OOO)
2015/04/02 21:45:34
Can you help me understand this logic in the land
vmpstr
2015/04/02 22:33:26
This tile exists on the active tree and the questi
| |
190 // occluded with a pending viewport, then we have to treat this tile as | |
191 // unoccluded. | |
192 const PictureLayerTiling* pending_twin = | |
193 client_->GetPendingOrActiveTwinTiling(this); | |
194 if (pending_twin) { | |
195 // If there's a pending tile in the same position. Or if the pending twin | |
196 // would have to be creating all tiles, then we don't need to worry about | |
197 // occlusion on the twin. | |
198 if (!TilingMatchesTileIndecies(pending_twin) || | |
199 pending_twin->TileAt(tile->tiling_i_index(), tile->tiling_j_index())) { | |
200 return true; | |
201 } | |
202 return pending_twin->IsTileOccluded(tile); | |
203 } | |
204 return true; | |
205 } | |
206 | |
207 bool ActivePictureLayerTiling::IsTileRequiredForActivation( | |
208 const Tile* tile) const { | |
209 const PictureLayerTiling* pending_twin = | |
210 client_->GetPendingOrActiveTwinTiling(this); | |
211 // If we don't have a pending tree, or the pending tree will overwrite the | |
212 // given tile, then it is not required for activation. | |
213 if (!pending_twin || !TilingMatchesTileIndecies(pending_twin) || | |
214 pending_twin->TileAt(tile->tiling_i_index(), tile->tiling_j_index())) { | |
215 return false; | |
216 } | |
217 // Otherwise, ask the pending twin if this tile is required for activation. | |
218 return pending_twin->IsTileRequiredForActivation(tile); | |
219 } | |
220 | |
221 bool ActivePictureLayerTiling::IsTileRequiredForDraw(const Tile* tile) const { | |
222 if (resolution_ != HIGH_RESOLUTION) | |
223 return false; | |
224 | |
225 bool tile_is_visible = current_visible_rect_.Intersects(tile->content_rect()); | |
226 if (!tile_is_visible) | |
227 return false; | |
228 | |
229 if (IsTileOccludedOnActiveTree(tile)) | |
230 return false; | |
231 return true; | |
232 } | |
233 | |
234 bool ActivePictureLayerTiling::IsTileOccludedOnActiveTree( | |
enne (OOO)
2015/04/02 21:45:34
This looks a lot like PendingPictureLayerTiling::I
vmpstr
2015/04/02 22:33:26
Yep, I think this can be moved to PictureLayerTili
| |
235 const Tile* tile) const { | |
236 if (!current_occlusion_in_layer_space_.HasOcclusion()) | |
237 return false; | |
238 gfx::Rect tile_query_rect = | |
239 gfx::IntersectRects(tile->content_rect(), current_visible_rect_); | |
240 // Explicitly check if the tile is outside the viewport. If so, we need to | |
241 // return false, since occlusion for this tile is unknown. | |
242 if (tile_query_rect.IsEmpty()) | |
243 return false; | |
244 | |
245 if (contents_scale_ != 1.f) { | |
246 tile_query_rect = | |
247 gfx::ScaleToEnclosingRect(tile_query_rect, 1.f / contents_scale_); | |
248 } | |
249 return current_occlusion_in_layer_space_.IsOccluded(tile_query_rect); | |
250 } | |
251 | |
252 void ActivePictureLayerTiling::SetLiveTilesRect( | |
253 const gfx::Rect& new_live_tiles_rect) { | |
254 DCHECK(new_live_tiles_rect.IsEmpty() || | |
255 gfx::Rect(tiling_size()).Contains(new_live_tiles_rect)) | |
256 << "tiling_size: " << tiling_size().ToString() | |
257 << " new_live_tiles_rect: " << new_live_tiles_rect.ToString(); | |
258 if (live_tiles_rect_ == new_live_tiles_rect) | |
259 return; | |
260 | |
261 // Iterate to delete all tiles outside of our new live_tiles rect. | |
262 for (TilingData::DifferenceIterator iter(&tiling_data_, live_tiles_rect_, | |
263 new_live_tiles_rect); | |
264 iter; ++iter) { | |
265 RemoveTileAt(iter.index_x(), iter.index_y()); | |
266 } | |
267 | |
268 // Iterate to allocate new tiles for all regions with newly exposed area. | |
269 for (TilingData::DifferenceIterator iter(&tiling_data_, new_live_tiles_rect, | |
270 live_tiles_rect_); | |
271 iter; ++iter) { | |
272 TileMapKey key(iter.index()); | |
273 CreateTile(key.first, key.second); | |
274 } | |
275 | |
276 live_tiles_rect_ = new_live_tiles_rect; | |
277 VerifyLiveTilesRect(false); | |
278 } | |
279 | |
280 } // namespace cc | |
OLD | NEW |