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 #include "cc/layers/picture_layer_impl.h" | |
6 | |
7 #include <algorithm> | |
8 #include <cmath> | |
9 #include <limits> | |
10 #include <set> | |
11 | |
12 #include "base/time/time.h" | |
13 #include "base/trace_event/trace_event_argument.h" | |
14 #include "cc/base/math_util.h" | |
15 #include "cc/base/util.h" | |
16 #include "cc/debug/debug_colors.h" | |
17 #include "cc/debug/micro_benchmark_impl.h" | |
18 #include "cc/debug/traced_value.h" | |
19 #include "cc/layers/append_quads_data.h" | |
20 #include "cc/layers/solid_color_layer_impl.h" | |
21 #include "cc/output/begin_frame_args.h" | |
22 #include "cc/quads/checkerboard_draw_quad.h" | |
23 #include "cc/quads/debug_border_draw_quad.h" | |
24 #include "cc/quads/picture_draw_quad.h" | |
25 #include "cc/quads/solid_color_draw_quad.h" | |
26 #include "cc/quads/tile_draw_quad.h" | |
27 #include "cc/resources/tile_manager.h" | |
28 #include "cc/resources/tiling_set_raster_queue_all.h" | |
29 #include "cc/trees/layer_tree_impl.h" | |
30 #include "cc/trees/occlusion.h" | |
31 #include "ui/gfx/geometry/quad_f.h" | |
32 #include "ui/gfx/geometry/rect_conversions.h" | |
33 #include "ui/gfx/geometry/size_conversions.h" | |
34 | |
35 namespace { | |
36 // This must be > 1 as we multiply or divide by this to find a new raster | |
37 // scale during pinch. | |
38 const float kMaxScaleRatioDuringPinch = 2.0f; | |
39 | |
40 // When creating a new tiling during pinch, snap to an existing | |
41 // tiling's scale if the desired scale is within this ratio. | |
42 const float kSnapToExistingTilingRatio = 1.2f; | |
43 | |
44 // Even for really wide viewports, at some point GPU raster should use | |
45 // less than 4 tiles to fill the viewport. This is set to 256 as a | |
46 // sane minimum for now, but we might want to tune this for low-end. | |
47 const int kMinHeightForGpuRasteredTile = 256; | |
48 | |
49 // When making odd-sized tiles, round them up to increase the chances | |
50 // of using the same tile size. | |
51 const int kTileRoundUp = 64; | |
52 | |
53 } // namespace | |
54 | |
55 namespace cc { | |
56 | |
57 PictureLayerImpl::Pair::Pair() : active(nullptr), pending(nullptr) { | |
58 } | |
59 | |
60 PictureLayerImpl::Pair::Pair(PictureLayerImpl* active_layer, | |
61 PictureLayerImpl* pending_layer) | |
62 : active(active_layer), pending(pending_layer) { | |
63 } | |
64 | |
65 PictureLayerImpl::Pair::~Pair() { | |
66 } | |
67 | |
68 PictureLayerImpl::PictureLayerImpl( | |
69 LayerTreeImpl* tree_impl, | |
70 int id, | |
71 bool is_mask, | |
72 scoped_refptr<SyncedScrollOffset> scroll_offset) | |
73 : LayerImpl(tree_impl, id, scroll_offset), | |
74 twin_layer_(nullptr), | |
75 tilings_(CreatePictureLayerTilingSet()), | |
76 ideal_page_scale_(0.f), | |
77 ideal_device_scale_(0.f), | |
78 ideal_source_scale_(0.f), | |
79 ideal_contents_scale_(0.f), | |
80 raster_page_scale_(0.f), | |
81 raster_device_scale_(0.f), | |
82 raster_source_scale_(0.f), | |
83 raster_contents_scale_(0.f), | |
84 low_res_raster_contents_scale_(0.f), | |
85 raster_source_scale_is_fixed_(false), | |
86 was_screen_space_transform_animating_(false), | |
87 only_used_low_res_last_append_quads_(false), | |
88 is_mask_(is_mask), | |
89 nearest_neighbor_(false) { | |
90 layer_tree_impl()->RegisterPictureLayerImpl(this); | |
91 } | |
92 | |
93 PictureLayerImpl::~PictureLayerImpl() { | |
94 if (twin_layer_) | |
95 twin_layer_->twin_layer_ = nullptr; | |
96 layer_tree_impl()->UnregisterPictureLayerImpl(this); | |
97 } | |
98 | |
99 const char* PictureLayerImpl::LayerTypeAsString() const { | |
100 return "cc::PictureLayerImpl"; | |
101 } | |
102 | |
103 scoped_ptr<LayerImpl> PictureLayerImpl::CreateLayerImpl( | |
104 LayerTreeImpl* tree_impl) { | |
105 return PictureLayerImpl::Create(tree_impl, id(), is_mask_, | |
106 synced_scroll_offset()); | |
107 } | |
108 | |
109 void PictureLayerImpl::PushPropertiesTo(LayerImpl* base_layer) { | |
110 PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer); | |
111 DCHECK_EQ(layer_impl->is_mask_, is_mask_); | |
112 | |
113 LayerImpl::PushPropertiesTo(base_layer); | |
114 | |
115 // Twin relationships should never change once established. | |
116 DCHECK_IMPLIES(twin_layer_, twin_layer_ == layer_impl); | |
117 DCHECK_IMPLIES(twin_layer_, layer_impl->twin_layer_ == this); | |
118 // The twin relationship does not need to exist before the first | |
119 // PushPropertiesTo from pending to active layer since before that the active | |
120 // layer can not have a pile or tilings, it has only been created and inserted | |
121 // into the tree at that point. | |
122 twin_layer_ = layer_impl; | |
123 layer_impl->twin_layer_ = this; | |
124 | |
125 layer_impl->SetNearestNeighbor(nearest_neighbor_); | |
126 | |
127 // Solid color layers have no tilings. | |
128 DCHECK_IMPLIES(raster_source_->IsSolidColor(), tilings_->num_tilings() == 0); | |
129 // The pending tree should only have a high res (and possibly low res) tiling. | |
130 DCHECK_LE(tilings_->num_tilings(), | |
131 layer_tree_impl()->create_low_res_tiling() ? 2u : 1u); | |
132 | |
133 layer_impl->set_gpu_raster_max_texture_size(gpu_raster_max_texture_size_); | |
134 layer_impl->UpdateRasterSource(raster_source_, &invalidation_, | |
135 tilings_.get()); | |
136 DCHECK(invalidation_.IsEmpty()); | |
137 | |
138 // After syncing a solid color layer, the active layer has no tilings. | |
139 DCHECK_IMPLIES(raster_source_->IsSolidColor(), | |
140 layer_impl->tilings_->num_tilings() == 0); | |
141 | |
142 layer_impl->raster_page_scale_ = raster_page_scale_; | |
143 layer_impl->raster_device_scale_ = raster_device_scale_; | |
144 layer_impl->raster_source_scale_ = raster_source_scale_; | |
145 layer_impl->raster_contents_scale_ = raster_contents_scale_; | |
146 layer_impl->low_res_raster_contents_scale_ = low_res_raster_contents_scale_; | |
147 | |
148 layer_impl->SanityCheckTilingState(); | |
149 | |
150 // We always need to push properties. | |
151 // See http://crbug.com/303943 | |
152 // TODO(danakj): Stop always pushing properties since we don't swap tilings. | |
153 needs_push_properties_ = true; | |
154 } | |
155 | |
156 void PictureLayerImpl::AppendQuads(RenderPass* render_pass, | |
157 AppendQuadsData* append_quads_data) { | |
158 // The bounds and the pile size may differ if the pile wasn't updated (ie. | |
159 // PictureLayer::Update didn't happen). In that case the pile will be empty. | |
160 DCHECK_IMPLIES(!raster_source_->GetSize().IsEmpty(), | |
161 bounds() == raster_source_->GetSize()) | |
162 << " bounds " << bounds().ToString() << " pile " | |
163 << raster_source_->GetSize().ToString(); | |
164 | |
165 SharedQuadState* shared_quad_state = | |
166 render_pass->CreateAndAppendSharedQuadState(); | |
167 | |
168 if (raster_source_->IsSolidColor()) { | |
169 PopulateSharedQuadState(shared_quad_state); | |
170 | |
171 AppendDebugBorderQuad( | |
172 render_pass, bounds(), shared_quad_state, append_quads_data); | |
173 | |
174 SolidColorLayerImpl::AppendSolidQuads( | |
175 render_pass, draw_properties().occlusion_in_content_space, | |
176 shared_quad_state, visible_content_rect(), | |
177 raster_source_->GetSolidColor(), append_quads_data); | |
178 return; | |
179 } | |
180 | |
181 float max_contents_scale = MaximumTilingContentsScale(); | |
182 PopulateScaledSharedQuadState(shared_quad_state, max_contents_scale); | |
183 Occlusion scaled_occlusion = | |
184 draw_properties() | |
185 .occlusion_in_content_space.GetOcclusionWithGivenDrawTransform( | |
186 shared_quad_state->content_to_target_transform); | |
187 | |
188 if (current_draw_mode_ == DRAW_MODE_RESOURCELESS_SOFTWARE) { | |
189 AppendDebugBorderQuad( | |
190 render_pass, shared_quad_state->content_bounds, shared_quad_state, | |
191 append_quads_data, DebugColors::DirectPictureBorderColor(), | |
192 DebugColors::DirectPictureBorderWidth(layer_tree_impl())); | |
193 | |
194 gfx::Rect geometry_rect = shared_quad_state->visible_content_rect; | |
195 gfx::Rect opaque_rect = contents_opaque() ? geometry_rect : gfx::Rect(); | |
196 gfx::Rect visible_geometry_rect = | |
197 scaled_occlusion.GetUnoccludedContentRect(geometry_rect); | |
198 if (visible_geometry_rect.IsEmpty()) | |
199 return; | |
200 | |
201 gfx::Rect quad_content_rect = shared_quad_state->visible_content_rect; | |
202 gfx::Size texture_size = quad_content_rect.size(); | |
203 gfx::RectF texture_rect = gfx::RectF(texture_size); | |
204 | |
205 PictureDrawQuad* quad = | |
206 render_pass->CreateAndAppendDrawQuad<PictureDrawQuad>(); | |
207 quad->SetNew(shared_quad_state, geometry_rect, opaque_rect, | |
208 visible_geometry_rect, texture_rect, texture_size, | |
209 nearest_neighbor_, RGBA_8888, quad_content_rect, | |
210 max_contents_scale, raster_source_); | |
211 return; | |
212 } | |
213 | |
214 AppendDebugBorderQuad(render_pass, shared_quad_state->content_bounds, | |
215 shared_quad_state, append_quads_data); | |
216 | |
217 if (ShowDebugBorders()) { | |
218 for (PictureLayerTilingSet::CoverageIterator iter( | |
219 tilings_.get(), max_contents_scale, | |
220 shared_quad_state->visible_content_rect, ideal_contents_scale_); | |
221 iter; ++iter) { | |
222 SkColor color; | |
223 float width; | |
224 if (*iter && iter->IsReadyToDraw()) { | |
225 TileDrawInfo::Mode mode = iter->draw_info().mode(); | |
226 if (mode == TileDrawInfo::SOLID_COLOR_MODE) { | |
227 color = DebugColors::SolidColorTileBorderColor(); | |
228 width = DebugColors::SolidColorTileBorderWidth(layer_tree_impl()); | |
229 } else if (mode == TileDrawInfo::OOM_MODE) { | |
230 color = DebugColors::OOMTileBorderColor(); | |
231 width = DebugColors::OOMTileBorderWidth(layer_tree_impl()); | |
232 } else if (iter.resolution() == HIGH_RESOLUTION) { | |
233 color = DebugColors::HighResTileBorderColor(); | |
234 width = DebugColors::HighResTileBorderWidth(layer_tree_impl()); | |
235 } else if (iter.resolution() == LOW_RESOLUTION) { | |
236 color = DebugColors::LowResTileBorderColor(); | |
237 width = DebugColors::LowResTileBorderWidth(layer_tree_impl()); | |
238 } else if (iter->contents_scale() > max_contents_scale) { | |
239 color = DebugColors::ExtraHighResTileBorderColor(); | |
240 width = DebugColors::ExtraHighResTileBorderWidth(layer_tree_impl()); | |
241 } else { | |
242 color = DebugColors::ExtraLowResTileBorderColor(); | |
243 width = DebugColors::ExtraLowResTileBorderWidth(layer_tree_impl()); | |
244 } | |
245 } else { | |
246 color = DebugColors::MissingTileBorderColor(); | |
247 width = DebugColors::MissingTileBorderWidth(layer_tree_impl()); | |
248 } | |
249 | |
250 DebugBorderDrawQuad* debug_border_quad = | |
251 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>(); | |
252 gfx::Rect geometry_rect = iter.geometry_rect(); | |
253 gfx::Rect visible_geometry_rect = geometry_rect; | |
254 debug_border_quad->SetNew(shared_quad_state, | |
255 geometry_rect, | |
256 visible_geometry_rect, | |
257 color, | |
258 width); | |
259 } | |
260 } | |
261 | |
262 // Keep track of the tilings that were used so that tilings that are | |
263 // unused can be considered for removal. | |
264 last_append_quads_tilings_.clear(); | |
265 | |
266 // Ignore missing tiles outside of viewport for tile priority. This is | |
267 // normally the same as draw viewport but can be independently overridden by | |
268 // embedders like Android WebView with SetExternalDrawConstraints. | |
269 gfx::Rect scaled_viewport_for_tile_priority = gfx::ScaleToEnclosingRect( | |
270 viewport_rect_for_tile_priority_in_content_space_, max_contents_scale); | |
271 | |
272 size_t missing_tile_count = 0u; | |
273 size_t on_demand_missing_tile_count = 0u; | |
274 only_used_low_res_last_append_quads_ = true; | |
275 for (PictureLayerTilingSet::CoverageIterator iter( | |
276 tilings_.get(), max_contents_scale, | |
277 shared_quad_state->visible_content_rect, ideal_contents_scale_); | |
278 iter; ++iter) { | |
279 gfx::Rect geometry_rect = iter.geometry_rect(); | |
280 gfx::Rect opaque_rect = contents_opaque() ? geometry_rect : gfx::Rect(); | |
281 gfx::Rect visible_geometry_rect = | |
282 scaled_occlusion.GetUnoccludedContentRect(geometry_rect); | |
283 if (visible_geometry_rect.IsEmpty()) | |
284 continue; | |
285 | |
286 append_quads_data->visible_content_area += | |
287 visible_geometry_rect.width() * visible_geometry_rect.height(); | |
288 | |
289 bool has_draw_quad = false; | |
290 if (*iter && iter->IsReadyToDraw()) { | |
291 const TileDrawInfo& draw_info = iter->draw_info(); | |
292 switch (draw_info.mode()) { | |
293 case TileDrawInfo::RESOURCE_MODE: { | |
294 gfx::RectF texture_rect = iter.texture_rect(); | |
295 | |
296 // The raster_contents_scale_ is the best scale that the layer is | |
297 // trying to produce, even though it may not be ideal. Since that's | |
298 // the best the layer can promise in the future, consider those as | |
299 // complete. But if a tile is ideal scale, we don't want to consider | |
300 // it incomplete and trying to replace it with a tile at a worse | |
301 // scale. | |
302 if (iter->contents_scale() != raster_contents_scale_ && | |
303 iter->contents_scale() != ideal_contents_scale_ && | |
304 geometry_rect.Intersects(scaled_viewport_for_tile_priority)) { | |
305 append_quads_data->num_incomplete_tiles++; | |
306 } | |
307 | |
308 TileDrawQuad* quad = | |
309 render_pass->CreateAndAppendDrawQuad<TileDrawQuad>(); | |
310 quad->SetNew(shared_quad_state, geometry_rect, opaque_rect, | |
311 visible_geometry_rect, draw_info.resource_id(), | |
312 texture_rect, draw_info.resource_size(), | |
313 draw_info.contents_swizzled(), nearest_neighbor_); | |
314 has_draw_quad = true; | |
315 break; | |
316 } | |
317 case TileDrawInfo::SOLID_COLOR_MODE: { | |
318 SolidColorDrawQuad* quad = | |
319 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>(); | |
320 quad->SetNew(shared_quad_state, geometry_rect, visible_geometry_rect, | |
321 draw_info.solid_color(), false); | |
322 has_draw_quad = true; | |
323 break; | |
324 } | |
325 case TileDrawInfo::OOM_MODE: | |
326 break; // Checkerboard. | |
327 } | |
328 } | |
329 | |
330 if (!has_draw_quad) { | |
331 if (draw_checkerboard_for_missing_tiles()) { | |
332 CheckerboardDrawQuad* quad = | |
333 render_pass->CreateAndAppendDrawQuad<CheckerboardDrawQuad>(); | |
334 SkColor color = DebugColors::DefaultCheckerboardColor(); | |
335 quad->SetNew(shared_quad_state, geometry_rect, visible_geometry_rect, | |
336 color, draw_properties().device_scale_factor); | |
337 } else { | |
338 SkColor color = SafeOpaqueBackgroundColor(); | |
339 SolidColorDrawQuad* quad = | |
340 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>(); | |
341 quad->SetNew(shared_quad_state, | |
342 geometry_rect, | |
343 visible_geometry_rect, | |
344 color, | |
345 false); | |
346 } | |
347 | |
348 if (geometry_rect.Intersects(scaled_viewport_for_tile_priority)) { | |
349 append_quads_data->num_missing_tiles++; | |
350 ++missing_tile_count; | |
351 } | |
352 append_quads_data->approximated_visible_content_area += | |
353 visible_geometry_rect.width() * visible_geometry_rect.height(); | |
354 continue; | |
355 } | |
356 | |
357 if (iter.resolution() != HIGH_RESOLUTION) { | |
358 append_quads_data->approximated_visible_content_area += | |
359 visible_geometry_rect.width() * visible_geometry_rect.height(); | |
360 } | |
361 | |
362 // If we have a draw quad, but it's not low resolution, then | |
363 // mark that we've used something other than low res to draw. | |
364 if (iter.resolution() != LOW_RESOLUTION) | |
365 only_used_low_res_last_append_quads_ = false; | |
366 | |
367 if (last_append_quads_tilings_.empty() || | |
368 last_append_quads_tilings_.back() != iter.CurrentTiling()) { | |
369 last_append_quads_tilings_.push_back(iter.CurrentTiling()); | |
370 } | |
371 } | |
372 | |
373 if (missing_tile_count) { | |
374 TRACE_EVENT_INSTANT2("cc", | |
375 "PictureLayerImpl::AppendQuads checkerboard", | |
376 TRACE_EVENT_SCOPE_THREAD, | |
377 "missing_tile_count", | |
378 missing_tile_count, | |
379 "on_demand_missing_tile_count", | |
380 on_demand_missing_tile_count); | |
381 } | |
382 | |
383 // Aggressively remove any tilings that are not seen to save memory. Note | |
384 // that this is at the expense of doing cause more frequent re-painting. A | |
385 // better scheme would be to maintain a tighter visible_content_rect for the | |
386 // finer tilings. | |
387 CleanUpTilingsOnActiveLayer(last_append_quads_tilings_); | |
388 } | |
389 | |
390 bool PictureLayerImpl::UpdateTiles(bool resourceless_software_draw) { | |
391 DCHECK_EQ(1.f, contents_scale_x()); | |
392 DCHECK_EQ(1.f, contents_scale_y()); | |
393 | |
394 if (!resourceless_software_draw) { | |
395 visible_rect_for_tile_priority_ = visible_content_rect(); | |
396 } | |
397 | |
398 if (!CanHaveTilings()) { | |
399 ideal_page_scale_ = 0.f; | |
400 ideal_device_scale_ = 0.f; | |
401 ideal_contents_scale_ = 0.f; | |
402 ideal_source_scale_ = 0.f; | |
403 SanityCheckTilingState(); | |
404 return false; | |
405 } | |
406 | |
407 // Remove any non-ideal tilings that were not used last time we generated | |
408 // quads to save memory and processing time. Note that pending tree should | |
409 // only have one or two tilings (high and low res), so only clean up the | |
410 // active layer. This cleans it up here in case AppendQuads didn't run. | |
411 // If it did run, this would not remove any additional tilings. | |
412 if (layer_tree_impl()->IsActiveTree()) | |
413 CleanUpTilingsOnActiveLayer(last_append_quads_tilings_); | |
414 | |
415 UpdateIdealScales(); | |
416 | |
417 if (!raster_contents_scale_ || ShouldAdjustRasterScale()) { | |
418 RecalculateRasterScales(); | |
419 AddTilingsForRasterScale(); | |
420 } | |
421 | |
422 DCHECK(raster_page_scale_); | |
423 DCHECK(raster_device_scale_); | |
424 DCHECK(raster_source_scale_); | |
425 DCHECK(raster_contents_scale_); | |
426 DCHECK(low_res_raster_contents_scale_); | |
427 | |
428 was_screen_space_transform_animating_ = | |
429 draw_properties().screen_space_transform_is_animating; | |
430 | |
431 if (draw_transform_is_animating()) | |
432 raster_source_->SetShouldAttemptToUseDistanceFieldText(); | |
433 | |
434 double current_frame_time_in_seconds = | |
435 (layer_tree_impl()->CurrentBeginFrameArgs().frame_time - | |
436 base::TimeTicks()).InSecondsF(); | |
437 UpdateViewportRectForTilePriorityInContentSpace(); | |
438 | |
439 // The tiling set can require tiles for activation any of the following | |
440 // conditions are true: | |
441 // - This layer produced a high-res or non-ideal-res tile last frame. | |
442 // - We're in requires high res to draw mode. | |
443 // - We're not in smoothness takes priority mode. | |
444 // To put different, the tiling set can't require tiles for activation if | |
445 // we're in smoothness mode and only used low-res or checkerboard to draw last | |
446 // frame and we don't need high res to draw. | |
447 // | |
448 // The reason for this is that we should be able to activate sooner and get a | |
449 // more up to date recording, so we don't run out of recording on the active | |
450 // tree. | |
451 bool can_require_tiles_for_activation = | |
452 !only_used_low_res_last_append_quads_ || RequiresHighResToDraw() || | |
453 !layer_tree_impl()->SmoothnessTakesPriority(); | |
454 | |
455 static const Occlusion kEmptyOcclusion; | |
456 const Occlusion& occlusion_in_content_space = | |
457 layer_tree_impl()->settings().use_occlusion_for_tile_prioritization | |
458 ? draw_properties().occlusion_in_content_space | |
459 : kEmptyOcclusion; | |
460 | |
461 // Pass |occlusion_in_content_space| for |occlusion_in_layer_space| since | |
462 // they are the same space in picture layer, as contents scale is always 1. | |
463 bool updated = tilings_->UpdateTilePriorities( | |
464 viewport_rect_for_tile_priority_in_content_space_, ideal_contents_scale_, | |
465 current_frame_time_in_seconds, occlusion_in_content_space, | |
466 can_require_tiles_for_activation); | |
467 return updated; | |
468 } | |
469 | |
470 void PictureLayerImpl::UpdateViewportRectForTilePriorityInContentSpace() { | |
471 // If visible_rect_for_tile_priority_ is empty or | |
472 // viewport_rect_for_tile_priority is set to be different from the device | |
473 // viewport, try to inverse project the viewport into layer space and use | |
474 // that. Otherwise just use visible_rect_for_tile_priority_ | |
475 gfx::Rect visible_rect_in_content_space = visible_rect_for_tile_priority_; | |
476 gfx::Rect viewport_rect_for_tile_priority = | |
477 layer_tree_impl()->ViewportRectForTilePriority(); | |
478 if (visible_rect_in_content_space.IsEmpty() || | |
479 layer_tree_impl()->DeviceViewport() != viewport_rect_for_tile_priority) { | |
480 gfx::Transform view_to_layer(gfx::Transform::kSkipInitialization); | |
481 if (screen_space_transform().GetInverse(&view_to_layer)) { | |
482 // Transform from view space to content space. | |
483 visible_rect_in_content_space = | |
484 gfx::ToEnclosingRect(MathUtil::ProjectClippedRect( | |
485 view_to_layer, viewport_rect_for_tile_priority)); | |
486 } | |
487 } | |
488 viewport_rect_for_tile_priority_in_content_space_ = | |
489 visible_rect_in_content_space; | |
490 } | |
491 | |
492 PictureLayerImpl* PictureLayerImpl::GetPendingOrActiveTwinLayer() const { | |
493 if (!twin_layer_ || !twin_layer_->IsOnActiveOrPendingTree()) | |
494 return nullptr; | |
495 return twin_layer_; | |
496 } | |
497 | |
498 PictureLayerImpl* PictureLayerImpl::GetRecycledTwinLayer() const { | |
499 if (!twin_layer_ || twin_layer_->IsOnActiveOrPendingTree()) | |
500 return nullptr; | |
501 return twin_layer_; | |
502 } | |
503 | |
504 void PictureLayerImpl::UpdateRasterSource( | |
505 scoped_refptr<RasterSource> raster_source, | |
506 Region* new_invalidation, | |
507 const PictureLayerTilingSet* pending_set) { | |
508 // The bounds and the pile size may differ if the pile wasn't updated (ie. | |
509 // PictureLayer::Update didn't happen). In that case the pile will be empty. | |
510 DCHECK_IMPLIES(!raster_source->GetSize().IsEmpty(), | |
511 bounds() == raster_source->GetSize()) | |
512 << " bounds " << bounds().ToString() << " pile " | |
513 << raster_source->GetSize().ToString(); | |
514 | |
515 // The |raster_source_| is initially null, so have to check for that for the | |
516 // first frame. | |
517 bool could_have_tilings = raster_source_.get() && CanHaveTilings(); | |
518 raster_source_.swap(raster_source); | |
519 | |
520 // The |new_invalidation| must be cleared before updating tilings since they | |
521 // access the invalidation through the PictureLayerTilingClient interface. | |
522 invalidation_.Clear(); | |
523 invalidation_.Swap(new_invalidation); | |
524 | |
525 bool can_have_tilings = CanHaveTilings(); | |
526 DCHECK_IMPLIES( | |
527 pending_set, | |
528 can_have_tilings == GetPendingOrActiveTwinLayer()->CanHaveTilings()); | |
529 | |
530 // Need to call UpdateTiles again if CanHaveTilings changed. | |
531 if (could_have_tilings != can_have_tilings) | |
532 layer_tree_impl()->set_needs_update_draw_properties(); | |
533 | |
534 if (!can_have_tilings) { | |
535 RemoveAllTilings(); | |
536 return; | |
537 } | |
538 | |
539 // We could do this after doing UpdateTiles, which would avoid doing this for | |
540 // tilings that are going to disappear on the pending tree (if scale changed). | |
541 // But that would also be more complicated, so we just do it here for now. | |
542 tilings_->UpdateTilingsToCurrentRasterSource( | |
543 raster_source_, pending_set, invalidation_, MinimumContentsScale(), | |
544 MaximumContentsScale()); | |
545 } | |
546 | |
547 void PictureLayerImpl::UpdateCanUseLCDTextAfterCommit() { | |
548 // This function is only allowed to be called after commit, due to it not | |
549 // being smart about sharing tiles and because otherwise it would cause | |
550 // flashes by switching out tiles in place that may be currently on screen. | |
551 DCHECK(layer_tree_impl()->IsSyncTree()); | |
552 | |
553 // Don't allow the LCD text state to change once disabled. | |
554 if (!RasterSourceUsesLCDText()) | |
555 return; | |
556 if (can_use_lcd_text() == RasterSourceUsesLCDText()) | |
557 return; | |
558 | |
559 // Raster sources are considered const, so in order to update the state | |
560 // a new one must be created and all tiles recreated. | |
561 scoped_refptr<RasterSource> new_raster_source = | |
562 raster_source_->CreateCloneWithoutLCDText(); | |
563 // Synthetically invalidate everything. | |
564 gfx::Rect bounds_rect(bounds()); | |
565 Region invalidation(bounds_rect); | |
566 UpdateRasterSource(new_raster_source, &invalidation, nullptr); | |
567 SetUpdateRect(bounds_rect); | |
568 | |
569 DCHECK(!RasterSourceUsesLCDText()); | |
570 } | |
571 | |
572 bool PictureLayerImpl::RasterSourceUsesLCDText() const { | |
573 return raster_source_ ? raster_source_->CanUseLCDText() | |
574 : layer_tree_impl()->settings().can_use_lcd_text; | |
575 } | |
576 | |
577 void PictureLayerImpl::NotifyTileStateChanged(const Tile* tile) { | |
578 if (layer_tree_impl()->IsActiveTree()) { | |
579 gfx::RectF layer_damage_rect = | |
580 gfx::ScaleRect(tile->content_rect(), 1.f / tile->contents_scale()); | |
581 AddDamageRect(layer_damage_rect); | |
582 } | |
583 } | |
584 | |
585 void PictureLayerImpl::DidBeginTracing() { | |
586 raster_source_->DidBeginTracing(); | |
587 } | |
588 | |
589 void PictureLayerImpl::ReleaseResources() { | |
590 // Recreate tilings with new settings, since some of those might change when | |
591 // we release resources. | |
592 tilings_ = nullptr; | |
593 ResetRasterScale(); | |
594 } | |
595 | |
596 void PictureLayerImpl::RecreateResources() { | |
597 tilings_ = CreatePictureLayerTilingSet(); | |
598 | |
599 // To avoid an edge case after lost context where the tree is up to date but | |
600 // the tilings have not been managed, request an update draw properties | |
601 // to force tilings to get managed. | |
602 layer_tree_impl()->set_needs_update_draw_properties(); | |
603 } | |
604 | |
605 skia::RefPtr<SkPicture> PictureLayerImpl::GetPicture() { | |
606 return raster_source_->GetFlattenedPicture(); | |
607 } | |
608 | |
609 Region PictureLayerImpl::GetInvalidationRegion() { | |
610 // |invalidation_| gives the invalidation contained in the source frame, but | |
611 // is not cleared after drawing from the layer. However, update_rect() is | |
612 // cleared once the invalidation is drawn, which is useful for debugging | |
613 // visualizations. This method intersects the two to give a more exact | |
614 // representation of what was invalidated that is cleared after drawing. | |
615 return IntersectRegions(invalidation_, update_rect()); | |
616 } | |
617 | |
618 scoped_refptr<Tile> PictureLayerImpl::CreateTile( | |
619 float contents_scale, | |
620 const gfx::Rect& content_rect) { | |
621 int flags = 0; | |
622 | |
623 // We don't handle solid color masks, so we shouldn't bother analyzing those. | |
624 // Otherwise, always analyze to maximize memory savings. | |
625 if (!is_mask_) | |
626 flags = Tile::USE_PICTURE_ANALYSIS; | |
627 | |
628 return layer_tree_impl()->tile_manager()->CreateTile( | |
629 raster_source_.get(), content_rect.size(), content_rect, contents_scale, | |
630 id(), layer_tree_impl()->source_frame_number(), flags); | |
631 } | |
632 | |
633 const Region* PictureLayerImpl::GetPendingInvalidation() { | |
634 if (layer_tree_impl()->IsPendingTree()) | |
635 return &invalidation_; | |
636 if (layer_tree_impl()->IsRecycleTree()) | |
637 return nullptr; | |
638 DCHECK(layer_tree_impl()->IsActiveTree()); | |
639 if (PictureLayerImpl* twin_layer = GetPendingOrActiveTwinLayer()) | |
640 return &twin_layer->invalidation_; | |
641 return nullptr; | |
642 } | |
643 | |
644 const PictureLayerTiling* PictureLayerImpl::GetPendingOrActiveTwinTiling( | |
645 const PictureLayerTiling* tiling) const { | |
646 PictureLayerImpl* twin_layer = GetPendingOrActiveTwinLayer(); | |
647 if (!twin_layer) | |
648 return nullptr; | |
649 return twin_layer->tilings_->FindTilingWithScale(tiling->contents_scale()); | |
650 } | |
651 | |
652 PictureLayerTiling* PictureLayerImpl::GetRecycledTwinTiling( | |
653 const PictureLayerTiling* tiling) { | |
654 PictureLayerImpl* recycled_twin = GetRecycledTwinLayer(); | |
655 if (!recycled_twin || !recycled_twin->tilings_) | |
656 return nullptr; | |
657 return recycled_twin->tilings_->FindTilingWithScale(tiling->contents_scale()); | |
658 } | |
659 | |
660 TilePriority::PriorityBin PictureLayerImpl::GetMaxTilePriorityBin() const { | |
661 if (!HasValidTilePriorities()) | |
662 return TilePriority::EVENTUALLY; | |
663 return TilePriority::NOW; | |
664 } | |
665 | |
666 bool PictureLayerImpl::RequiresHighResToDraw() const { | |
667 return layer_tree_impl()->RequiresHighResToDraw(); | |
668 } | |
669 | |
670 gfx::Rect PictureLayerImpl::GetEnclosingRectInTargetSpace() const { | |
671 return GetScaledEnclosingRectInTargetSpace(MaximumTilingContentsScale()); | |
672 } | |
673 | |
674 gfx::Size PictureLayerImpl::CalculateTileSize( | |
675 const gfx::Size& content_bounds) const { | |
676 int max_texture_size = | |
677 layer_tree_impl()->resource_provider()->max_texture_size(); | |
678 | |
679 if (is_mask_) { | |
680 // Masks are not tiled, so if we can't cover the whole mask with one tile, | |
681 // we shouldn't have such a tiling at all. | |
682 DCHECK_LE(content_bounds.width(), max_texture_size); | |
683 DCHECK_LE(content_bounds.height(), max_texture_size); | |
684 return content_bounds; | |
685 } | |
686 | |
687 int default_tile_width = 0; | |
688 int default_tile_height = 0; | |
689 if (layer_tree_impl()->use_gpu_rasterization()) { | |
690 // For GPU rasterization, we pick an ideal tile size using the viewport | |
691 // so we don't need any settings. The current approach uses 4 tiles | |
692 // to cover the viewport vertically. | |
693 int viewport_width = gpu_raster_max_texture_size_.width(); | |
694 int viewport_height = gpu_raster_max_texture_size_.height(); | |
695 default_tile_width = viewport_width; | |
696 // Also, increase the height proportionally as the width decreases, and | |
697 // pad by our border texels to make the tiles exactly match the viewport. | |
698 int divisor = 4; | |
699 if (content_bounds.width() <= viewport_width / 2) | |
700 divisor = 2; | |
701 if (content_bounds.width() <= viewport_width / 4) | |
702 divisor = 1; | |
703 default_tile_height = RoundUp(viewport_height, divisor) / divisor; | |
704 default_tile_height += 2 * PictureLayerTiling::kBorderTexels; | |
705 default_tile_height = | |
706 std::max(default_tile_height, kMinHeightForGpuRasteredTile); | |
707 } else { | |
708 // For CPU rasterization we use tile-size settings. | |
709 const LayerTreeSettings& settings = layer_tree_impl()->settings(); | |
710 int max_untiled_content_width = settings.max_untiled_layer_size.width(); | |
711 int max_untiled_content_height = settings.max_untiled_layer_size.height(); | |
712 default_tile_width = settings.default_tile_size.width(); | |
713 default_tile_height = settings.default_tile_size.height(); | |
714 | |
715 // If the content width is small, increase tile size vertically. | |
716 // If the content height is small, increase tile size horizontally. | |
717 // If both are less than the untiled-size, use a single tile. | |
718 if (content_bounds.width() < default_tile_width) | |
719 default_tile_height = max_untiled_content_height; | |
720 if (content_bounds.height() < default_tile_height) | |
721 default_tile_width = max_untiled_content_width; | |
722 if (content_bounds.width() < max_untiled_content_width && | |
723 content_bounds.height() < max_untiled_content_height) { | |
724 default_tile_height = max_untiled_content_height; | |
725 default_tile_width = max_untiled_content_width; | |
726 } | |
727 } | |
728 | |
729 int tile_width = default_tile_width; | |
730 int tile_height = default_tile_height; | |
731 | |
732 // Clamp the tile width/height to the content width/height to save space. | |
733 if (content_bounds.width() < default_tile_width) { | |
734 tile_width = std::min(tile_width, content_bounds.width()); | |
735 tile_width = RoundUp(tile_width, kTileRoundUp); | |
736 tile_width = std::min(tile_width, default_tile_width); | |
737 } | |
738 if (content_bounds.height() < default_tile_height) { | |
739 tile_height = std::min(tile_height, content_bounds.height()); | |
740 tile_height = RoundUp(tile_height, kTileRoundUp); | |
741 tile_height = std::min(tile_height, default_tile_height); | |
742 } | |
743 | |
744 // Under no circumstance should we be larger than the max texture size. | |
745 tile_width = std::min(tile_width, max_texture_size); | |
746 tile_height = std::min(tile_height, max_texture_size); | |
747 return gfx::Size(tile_width, tile_height); | |
748 } | |
749 | |
750 void PictureLayerImpl::GetContentsResourceId( | |
751 ResourceProvider::ResourceId* resource_id, | |
752 gfx::Size* resource_size) const { | |
753 // The bounds and the pile size may differ if the pile wasn't updated (ie. | |
754 // PictureLayer::Update didn't happen). In that case the pile will be empty. | |
755 DCHECK_IMPLIES(!raster_source_->GetSize().IsEmpty(), | |
756 bounds() == raster_source_->GetSize()) | |
757 << " bounds " << bounds().ToString() << " pile " | |
758 << raster_source_->GetSize().ToString(); | |
759 gfx::Rect content_rect(bounds()); | |
760 PictureLayerTilingSet::CoverageIterator iter( | |
761 tilings_.get(), 1.f, content_rect, ideal_contents_scale_); | |
762 | |
763 // Mask resource not ready yet. | |
764 if (!iter || !*iter) { | |
765 *resource_id = 0; | |
766 return; | |
767 } | |
768 | |
769 // Masks only supported if they fit on exactly one tile. | |
770 DCHECK(iter.geometry_rect() == content_rect) | |
771 << "iter rect " << iter.geometry_rect().ToString() << " content rect " | |
772 << content_rect.ToString(); | |
773 | |
774 const TileDrawInfo& draw_info = iter->draw_info(); | |
775 if (!draw_info.IsReadyToDraw() || | |
776 draw_info.mode() != TileDrawInfo::RESOURCE_MODE) { | |
777 *resource_id = 0; | |
778 return; | |
779 } | |
780 | |
781 *resource_id = draw_info.resource_id(); | |
782 *resource_size = draw_info.resource_size(); | |
783 } | |
784 | |
785 void PictureLayerImpl::SetNearestNeighbor(bool nearest_neighbor) { | |
786 if (nearest_neighbor_ == nearest_neighbor) | |
787 return; | |
788 | |
789 nearest_neighbor_ = nearest_neighbor; | |
790 NoteLayerPropertyChanged(); | |
791 } | |
792 | |
793 PictureLayerTiling* PictureLayerImpl::AddTiling(float contents_scale) { | |
794 DCHECK(CanHaveTilings()); | |
795 DCHECK_GE(contents_scale, MinimumContentsScale()); | |
796 DCHECK_LE(contents_scale, MaximumContentsScale()); | |
797 DCHECK(raster_source_->HasRecordings()); | |
798 return tilings_->AddTiling(contents_scale, raster_source_); | |
799 } | |
800 | |
801 void PictureLayerImpl::RemoveAllTilings() { | |
802 tilings_->RemoveAllTilings(); | |
803 // If there are no tilings, then raster scales are no longer meaningful. | |
804 ResetRasterScale(); | |
805 } | |
806 | |
807 void PictureLayerImpl::AddTilingsForRasterScale() { | |
808 // Reset all resolution enums on tilings, we'll be setting new values in this | |
809 // function. | |
810 tilings_->MarkAllTilingsNonIdeal(); | |
811 | |
812 PictureLayerTiling* high_res = | |
813 tilings_->FindTilingWithScale(raster_contents_scale_); | |
814 // We always need a high res tiling, so create one if it doesn't exist. | |
815 if (!high_res) | |
816 high_res = AddTiling(raster_contents_scale_); | |
817 | |
818 // Try and find a low res tiling. | |
819 PictureLayerTiling* low_res = nullptr; | |
820 if (raster_contents_scale_ == low_res_raster_contents_scale_) | |
821 low_res = high_res; | |
822 else | |
823 low_res = tilings_->FindTilingWithScale(low_res_raster_contents_scale_); | |
824 | |
825 // Only create new low res tilings when the transform is static. This | |
826 // prevents wastefully creating a paired low res tiling for every new high res | |
827 // tiling during a pinch or a CSS animation. | |
828 bool can_have_low_res = layer_tree_impl()->create_low_res_tiling(); | |
829 bool needs_low_res = !low_res; | |
830 bool is_pinching = layer_tree_impl()->PinchGestureActive(); | |
831 bool is_animating = draw_properties().screen_space_transform_is_animating; | |
832 if (can_have_low_res && needs_low_res && !is_pinching && !is_animating) | |
833 low_res = AddTiling(low_res_raster_contents_scale_); | |
834 | |
835 // Set low-res if we have one. | |
836 if (low_res && low_res != high_res) | |
837 low_res->set_resolution(LOW_RESOLUTION); | |
838 | |
839 // Make sure we always have one high-res (even if high == low). | |
840 high_res->set_resolution(HIGH_RESOLUTION); | |
841 | |
842 if (layer_tree_impl()->IsPendingTree()) { | |
843 // On the pending tree, drop any tilings that are non-ideal since we don't | |
844 // need them to activate anyway. | |
845 tilings_->RemoveNonIdealTilings(); | |
846 } | |
847 | |
848 SanityCheckTilingState(); | |
849 } | |
850 | |
851 bool PictureLayerImpl::ShouldAdjustRasterScale() const { | |
852 if (was_screen_space_transform_animating_ != | |
853 draw_properties().screen_space_transform_is_animating) | |
854 return true; | |
855 | |
856 if (draw_properties().screen_space_transform_is_animating && | |
857 raster_contents_scale_ != ideal_contents_scale_ && | |
858 ShouldAdjustRasterScaleDuringScaleAnimations()) | |
859 return true; | |
860 | |
861 bool is_pinching = layer_tree_impl()->PinchGestureActive(); | |
862 if (is_pinching && raster_page_scale_) { | |
863 // We change our raster scale when it is: | |
864 // - Higher than ideal (need a lower-res tiling available) | |
865 // - Too far from ideal (need a higher-res tiling available) | |
866 float ratio = ideal_page_scale_ / raster_page_scale_; | |
867 if (raster_page_scale_ > ideal_page_scale_ || | |
868 ratio > kMaxScaleRatioDuringPinch) | |
869 return true; | |
870 } | |
871 | |
872 if (!is_pinching) { | |
873 // When not pinching, match the ideal page scale factor. | |
874 if (raster_page_scale_ != ideal_page_scale_) | |
875 return true; | |
876 } | |
877 | |
878 // Always match the ideal device scale factor. | |
879 if (raster_device_scale_ != ideal_device_scale_) | |
880 return true; | |
881 | |
882 // When the source scale changes we want to match it, but not when animating | |
883 // or when we've fixed the scale in place. | |
884 if (!draw_properties().screen_space_transform_is_animating && | |
885 !raster_source_scale_is_fixed_ && | |
886 raster_source_scale_ != ideal_source_scale_) | |
887 return true; | |
888 | |
889 if (raster_contents_scale_ > MaximumContentsScale()) | |
890 return true; | |
891 if (raster_contents_scale_ < MinimumContentsScale()) | |
892 return true; | |
893 | |
894 return false; | |
895 } | |
896 | |
897 void PictureLayerImpl::RecalculateRasterScales() { | |
898 float old_raster_contents_scale = raster_contents_scale_; | |
899 float old_raster_page_scale = raster_page_scale_; | |
900 float old_raster_source_scale = raster_source_scale_; | |
901 | |
902 raster_device_scale_ = ideal_device_scale_; | |
903 raster_page_scale_ = ideal_page_scale_; | |
904 raster_source_scale_ = ideal_source_scale_; | |
905 raster_contents_scale_ = ideal_contents_scale_; | |
906 | |
907 // If we're not animating, or leaving an animation, and the | |
908 // ideal_source_scale_ changes, then things are unpredictable, and we fix | |
909 // the raster_source_scale_ in place. | |
910 if (old_raster_source_scale && | |
911 !draw_properties().screen_space_transform_is_animating && | |
912 !was_screen_space_transform_animating_ && | |
913 old_raster_source_scale != ideal_source_scale_) | |
914 raster_source_scale_is_fixed_ = true; | |
915 | |
916 // TODO(danakj): Adjust raster source scale closer to ideal source scale at | |
917 // a throttled rate. Possibly make use of invalidation_.IsEmpty() on pending | |
918 // tree. This will allow CSS scale changes to get re-rastered at an | |
919 // appropriate rate. (crbug.com/413636) | |
920 if (raster_source_scale_is_fixed_) { | |
921 raster_contents_scale_ /= raster_source_scale_; | |
922 raster_source_scale_ = 1.f; | |
923 } | |
924 | |
925 // During pinch we completely ignore the current ideal scale, and just use | |
926 // a multiple of the previous scale. | |
927 bool is_pinching = layer_tree_impl()->PinchGestureActive(); | |
928 if (is_pinching && old_raster_contents_scale) { | |
929 // See ShouldAdjustRasterScale: | |
930 // - When zooming out, preemptively create new tiling at lower resolution. | |
931 // - When zooming in, approximate ideal using multiple of kMaxScaleRatio. | |
932 bool zooming_out = old_raster_page_scale > ideal_page_scale_; | |
933 float desired_contents_scale = old_raster_contents_scale; | |
934 if (zooming_out) { | |
935 while (desired_contents_scale > ideal_contents_scale_) | |
936 desired_contents_scale /= kMaxScaleRatioDuringPinch; | |
937 } else { | |
938 while (desired_contents_scale < ideal_contents_scale_) | |
939 desired_contents_scale *= kMaxScaleRatioDuringPinch; | |
940 } | |
941 raster_contents_scale_ = tilings_->GetSnappedContentsScale( | |
942 desired_contents_scale, kSnapToExistingTilingRatio); | |
943 raster_page_scale_ = | |
944 raster_contents_scale_ / raster_device_scale_ / raster_source_scale_; | |
945 } | |
946 | |
947 // If we're not re-rasterizing during animation, rasterize at the maximum | |
948 // scale that will occur during the animation, if the maximum scale is | |
949 // known. However we want to avoid excessive memory use. If the scale is | |
950 // smaller than what we would choose otherwise, then it's always better off | |
951 // for us memory-wise. But otherwise, we don't choose a scale at which this | |
952 // layer's rastered content would become larger than the viewport. | |
953 if (draw_properties().screen_space_transform_is_animating && | |
954 !ShouldAdjustRasterScaleDuringScaleAnimations()) { | |
955 bool can_raster_at_maximum_scale = false; | |
956 // TODO(ajuma): If we need to deal with scale-down animations starting right | |
957 // as a layer gets promoted, then we'd want to have the | |
958 // |starting_animation_contents_scale| passed in here as a separate draw | |
959 // property so we could try use that when the max is too large. | |
960 // See crbug.com/422341. | |
961 float maximum_scale = draw_properties().maximum_animation_contents_scale; | |
962 if (maximum_scale) { | |
963 gfx::Size bounds_at_maximum_scale = gfx::ToCeiledSize( | |
964 gfx::ScaleSize(raster_source_->GetSize(), maximum_scale)); | |
965 int64 maximum_area = static_cast<int64>(bounds_at_maximum_scale.width()) * | |
966 static_cast<int64>(bounds_at_maximum_scale.height()); | |
967 gfx::Size viewport = layer_tree_impl()->device_viewport_size(); | |
968 int64 viewport_area = static_cast<int64>(viewport.width()) * | |
969 static_cast<int64>(viewport.height()); | |
970 if (maximum_area <= viewport_area) | |
971 can_raster_at_maximum_scale = true; | |
972 } | |
973 // Use the computed scales for the raster scale directly, do not try to use | |
974 // the ideal scale here. The current ideal scale may be way too large in the | |
975 // case of an animation with scale, and will be constantly changing. | |
976 if (can_raster_at_maximum_scale) | |
977 raster_contents_scale_ = maximum_scale; | |
978 else | |
979 raster_contents_scale_ = 1.f * ideal_page_scale_ * ideal_device_scale_; | |
980 } | |
981 | |
982 raster_contents_scale_ = | |
983 std::max(raster_contents_scale_, MinimumContentsScale()); | |
984 raster_contents_scale_ = | |
985 std::min(raster_contents_scale_, MaximumContentsScale()); | |
986 DCHECK_GE(raster_contents_scale_, MinimumContentsScale()); | |
987 DCHECK_LE(raster_contents_scale_, MaximumContentsScale()); | |
988 | |
989 // If this layer would create zero or one tiles at this content scale, | |
990 // don't create a low res tiling. | |
991 gfx::Size raster_bounds = gfx::ToCeiledSize( | |
992 gfx::ScaleSize(raster_source_->GetSize(), raster_contents_scale_)); | |
993 gfx::Size tile_size = CalculateTileSize(raster_bounds); | |
994 bool tile_covers_bounds = tile_size.width() >= raster_bounds.width() && | |
995 tile_size.height() >= raster_bounds.height(); | |
996 if (tile_size.IsEmpty() || tile_covers_bounds) { | |
997 low_res_raster_contents_scale_ = raster_contents_scale_; | |
998 return; | |
999 } | |
1000 | |
1001 float low_res_factor = | |
1002 layer_tree_impl()->settings().low_res_contents_scale_factor; | |
1003 low_res_raster_contents_scale_ = | |
1004 std::max(raster_contents_scale_ * low_res_factor, MinimumContentsScale()); | |
1005 DCHECK_LE(low_res_raster_contents_scale_, raster_contents_scale_); | |
1006 DCHECK_GE(low_res_raster_contents_scale_, MinimumContentsScale()); | |
1007 DCHECK_LE(low_res_raster_contents_scale_, MaximumContentsScale()); | |
1008 } | |
1009 | |
1010 void PictureLayerImpl::CleanUpTilingsOnActiveLayer( | |
1011 const std::vector<PictureLayerTiling*>& used_tilings) { | |
1012 DCHECK(layer_tree_impl()->IsActiveTree()); | |
1013 if (tilings_->num_tilings() == 0) | |
1014 return; | |
1015 | |
1016 float min_acceptable_high_res_scale = std::min( | |
1017 raster_contents_scale_, ideal_contents_scale_); | |
1018 float max_acceptable_high_res_scale = std::max( | |
1019 raster_contents_scale_, ideal_contents_scale_); | |
1020 | |
1021 PictureLayerImpl* twin = GetPendingOrActiveTwinLayer(); | |
1022 if (twin && twin->CanHaveTilings()) { | |
1023 min_acceptable_high_res_scale = std::min( | |
1024 min_acceptable_high_res_scale, | |
1025 std::min(twin->raster_contents_scale_, twin->ideal_contents_scale_)); | |
1026 max_acceptable_high_res_scale = std::max( | |
1027 max_acceptable_high_res_scale, | |
1028 std::max(twin->raster_contents_scale_, twin->ideal_contents_scale_)); | |
1029 } | |
1030 | |
1031 PictureLayerTilingSet* twin_set = twin ? twin->tilings_.get() : nullptr; | |
1032 PictureLayerImpl* recycled_twin = GetRecycledTwinLayer(); | |
1033 PictureLayerTilingSet* recycled_twin_set = | |
1034 recycled_twin ? recycled_twin->tilings_.get() : nullptr; | |
1035 | |
1036 tilings_->CleanUpTilings(min_acceptable_high_res_scale, | |
1037 max_acceptable_high_res_scale, used_tilings, | |
1038 layer_tree_impl()->create_low_res_tiling(), twin_set, | |
1039 recycled_twin_set); | |
1040 | |
1041 if (recycled_twin_set && recycled_twin_set->num_tilings() == 0) | |
1042 recycled_twin->ResetRasterScale(); | |
1043 | |
1044 DCHECK_GT(tilings_->num_tilings(), 0u); | |
1045 SanityCheckTilingState(); | |
1046 } | |
1047 | |
1048 float PictureLayerImpl::MinimumContentsScale() const { | |
1049 float setting_min = layer_tree_impl()->settings().minimum_contents_scale; | |
1050 | |
1051 // If the contents scale is less than 1 / width (also for height), | |
1052 // then it will end up having less than one pixel of content in that | |
1053 // dimension. Bump the minimum contents scale up in this case to prevent | |
1054 // this from happening. | |
1055 int min_dimension = std::min(raster_source_->GetSize().width(), | |
1056 raster_source_->GetSize().height()); | |
1057 if (!min_dimension) | |
1058 return setting_min; | |
1059 | |
1060 return std::max(1.f / min_dimension, setting_min); | |
1061 } | |
1062 | |
1063 float PictureLayerImpl::MaximumContentsScale() const { | |
1064 // Masks can not have tilings that would become larger than the | |
1065 // max_texture_size since they use a single tile for the entire | |
1066 // tiling. Other layers can have tilings of any scale. | |
1067 if (!is_mask_) | |
1068 return std::numeric_limits<float>::max(); | |
1069 | |
1070 int max_texture_size = | |
1071 layer_tree_impl()->resource_provider()->max_texture_size(); | |
1072 float max_scale_width = | |
1073 static_cast<float>(max_texture_size) / bounds().width(); | |
1074 float max_scale_height = | |
1075 static_cast<float>(max_texture_size) / bounds().height(); | |
1076 float max_scale = std::min(max_scale_width, max_scale_height); | |
1077 // We require that multiplying the layer size by the contents scale and | |
1078 // ceiling produces a value <= |max_texture_size|. Because for large layer | |
1079 // sizes floating point ambiguity may crop up, making the result larger or | |
1080 // smaller than expected, we use a slightly smaller floating point value for | |
1081 // the scale, to help ensure that the resulting content bounds will never end | |
1082 // up larger than |max_texture_size|. | |
1083 return nextafterf(max_scale, 0.f); | |
1084 } | |
1085 | |
1086 void PictureLayerImpl::ResetRasterScale() { | |
1087 raster_page_scale_ = 0.f; | |
1088 raster_device_scale_ = 0.f; | |
1089 raster_source_scale_ = 0.f; | |
1090 raster_contents_scale_ = 0.f; | |
1091 low_res_raster_contents_scale_ = 0.f; | |
1092 raster_source_scale_is_fixed_ = false; | |
1093 } | |
1094 | |
1095 bool PictureLayerImpl::CanHaveTilings() const { | |
1096 if (raster_source_->IsSolidColor()) | |
1097 return false; | |
1098 if (!DrawsContent()) | |
1099 return false; | |
1100 if (!raster_source_->HasRecordings()) | |
1101 return false; | |
1102 // If the |raster_source_| has a recording it should have non-empty bounds. | |
1103 DCHECK(!raster_source_->GetSize().IsEmpty()); | |
1104 if (MaximumContentsScale() < MinimumContentsScale()) | |
1105 return false; | |
1106 return true; | |
1107 } | |
1108 | |
1109 void PictureLayerImpl::SanityCheckTilingState() const { | |
1110 #if DCHECK_IS_ON() | |
1111 // Recycle tree doesn't have any restrictions. | |
1112 if (layer_tree_impl()->IsRecycleTree()) | |
1113 return; | |
1114 | |
1115 if (!CanHaveTilings()) { | |
1116 DCHECK_EQ(0u, tilings_->num_tilings()); | |
1117 return; | |
1118 } | |
1119 if (tilings_->num_tilings() == 0) | |
1120 return; | |
1121 | |
1122 // We should only have one high res tiling. | |
1123 DCHECK_EQ(1, tilings_->NumHighResTilings()); | |
1124 #endif | |
1125 } | |
1126 | |
1127 bool PictureLayerImpl::ShouldAdjustRasterScaleDuringScaleAnimations() const { | |
1128 return layer_tree_impl()->use_gpu_rasterization(); | |
1129 } | |
1130 | |
1131 float PictureLayerImpl::MaximumTilingContentsScale() const { | |
1132 float max_contents_scale = tilings_->GetMaximumContentsScale(); | |
1133 return std::max(max_contents_scale, MinimumContentsScale()); | |
1134 } | |
1135 | |
1136 scoped_ptr<PictureLayerTilingSet> | |
1137 PictureLayerImpl::CreatePictureLayerTilingSet() { | |
1138 const LayerTreeSettings& settings = layer_tree_impl()->settings(); | |
1139 return PictureLayerTilingSet::Create( | |
1140 this, settings.max_tiles_for_interest_area, | |
1141 layer_tree_impl()->use_gpu_rasterization() | |
1142 ? settings.gpu_rasterization_skewport_target_time_in_seconds | |
1143 : settings.skewport_target_time_in_seconds, | |
1144 settings.skewport_extrapolation_limit_in_content_pixels); | |
1145 } | |
1146 | |
1147 void PictureLayerImpl::UpdateIdealScales() { | |
1148 DCHECK(CanHaveTilings()); | |
1149 | |
1150 float min_contents_scale = MinimumContentsScale(); | |
1151 DCHECK_GT(min_contents_scale, 0.f); | |
1152 float min_page_scale = layer_tree_impl()->min_page_scale_factor(); | |
1153 DCHECK_GT(min_page_scale, 0.f); | |
1154 float min_device_scale = 1.f; | |
1155 float min_source_scale = | |
1156 min_contents_scale / min_page_scale / min_device_scale; | |
1157 | |
1158 float ideal_page_scale = draw_properties().page_scale_factor; | |
1159 float ideal_device_scale = draw_properties().device_scale_factor; | |
1160 float ideal_source_scale = draw_properties().ideal_contents_scale / | |
1161 ideal_page_scale / ideal_device_scale; | |
1162 ideal_contents_scale_ = | |
1163 std::max(draw_properties().ideal_contents_scale, min_contents_scale); | |
1164 ideal_page_scale_ = draw_properties().page_scale_factor; | |
1165 ideal_device_scale_ = draw_properties().device_scale_factor; | |
1166 ideal_source_scale_ = std::max(ideal_source_scale, min_source_scale); | |
1167 } | |
1168 | |
1169 void PictureLayerImpl::GetDebugBorderProperties( | |
1170 SkColor* color, | |
1171 float* width) const { | |
1172 *color = DebugColors::TiledContentLayerBorderColor(); | |
1173 *width = DebugColors::TiledContentLayerBorderWidth(layer_tree_impl()); | |
1174 } | |
1175 | |
1176 void PictureLayerImpl::GetAllTilesAndPrioritiesForTracing( | |
1177 std::map<const Tile*, TilePriority>* tile_map) const { | |
1178 if (!tilings_) | |
1179 return; | |
1180 tilings_->GetAllTilesAndPrioritiesForTracing(tile_map); | |
1181 } | |
1182 | |
1183 void PictureLayerImpl::AsValueInto( | |
1184 base::trace_event::TracedValue* state) const { | |
1185 LayerImpl::AsValueInto(state); | |
1186 state->SetDouble("ideal_contents_scale", ideal_contents_scale_); | |
1187 state->SetDouble("geometry_contents_scale", MaximumTilingContentsScale()); | |
1188 state->BeginArray("tilings"); | |
1189 tilings_->AsValueInto(state); | |
1190 state->EndArray(); | |
1191 | |
1192 MathUtil::AddToTracedValue("tile_priority_rect", | |
1193 viewport_rect_for_tile_priority_in_content_space_, | |
1194 state); | |
1195 MathUtil::AddToTracedValue("visible_rect", visible_content_rect(), state); | |
1196 | |
1197 state->BeginArray("pictures"); | |
1198 raster_source_->AsValueInto(state); | |
1199 state->EndArray(); | |
1200 | |
1201 state->BeginArray("invalidation"); | |
1202 invalidation_.AsValueInto(state); | |
1203 state->EndArray(); | |
1204 | |
1205 state->BeginArray("coverage_tiles"); | |
1206 for (PictureLayerTilingSet::CoverageIterator iter( | |
1207 tilings_.get(), 1.f, gfx::Rect(raster_source_->GetSize()), | |
1208 ideal_contents_scale_); | |
1209 iter; ++iter) { | |
1210 state->BeginDictionary(); | |
1211 | |
1212 MathUtil::AddToTracedValue("geometry_rect", iter.geometry_rect(), state); | |
1213 | |
1214 if (*iter) | |
1215 TracedValue::SetIDRef(*iter, state, "tile"); | |
1216 | |
1217 state->EndDictionary(); | |
1218 } | |
1219 state->EndArray(); | |
1220 } | |
1221 | |
1222 size_t PictureLayerImpl::GPUMemoryUsageInBytes() const { | |
1223 return tilings_->GPUMemoryUsageInBytes(); | |
1224 } | |
1225 | |
1226 void PictureLayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) { | |
1227 benchmark->RunOnLayer(this); | |
1228 } | |
1229 | |
1230 WhichTree PictureLayerImpl::GetTree() const { | |
1231 return layer_tree_impl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE; | |
1232 } | |
1233 | |
1234 bool PictureLayerImpl::IsOnActiveOrPendingTree() const { | |
1235 return !layer_tree_impl()->IsRecycleTree(); | |
1236 } | |
1237 | |
1238 bool PictureLayerImpl::HasValidTilePriorities() const { | |
1239 return IsOnActiveOrPendingTree() && IsDrawnRenderSurfaceLayerListMember(); | |
1240 } | |
1241 | |
1242 } // namespace cc | |
OLD | NEW |