1 // Copyright 2011 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 #include "cc/layers/tiled_layer.h"
10 #include "base/auto_reset.h"
11 #include "base/basictypes.h"
12 #include "build/build_config.h"
13 #include "cc/base/simple_enclosed_region.h"
14 #include "cc/layers/layer_impl.h"
15 #include "cc/layers/tiled_layer_impl.h"
16 #include "cc/resources/layer_updater.h"
17 #include "cc/resources/prioritized_resource.h"
18 #include "cc/resources/priority_calculator.h"
19 #include "cc/trees/layer_tree_host.h"
20 #include "cc/trees/occlusion_tracker.h"
21 #include "third_party/khronos/GLES2/gl2.h"
22 #include "ui/gfx/rect_conversions.h"
26 // Maximum predictive expansion of the visible area.
27 static const int kMaxPredictiveTilesCount
= 2;
29 // Number of rows/columns of tiles to pre-paint.
30 // We should increase these further as all textures are
31 // prioritized and we insure performance doesn't suffer.
32 static const int kPrepaintRows
= 4;
33 static const int kPrepaintColumns
= 2;
35 class UpdatableTile
: public LayerTilingData::Tile
{
37 static scoped_ptr
<UpdatableTile
> Create(
38 scoped_ptr
<LayerUpdater::Resource
> updater_resource
) {
39 return make_scoped_ptr(new UpdatableTile(updater_resource
.Pass()));
42 LayerUpdater::Resource
* updater_resource() { return updater_resource_
.get(); }
43 PrioritizedResource
* managed_resource() {
44 return updater_resource_
->texture();
47 bool is_dirty() const { return !dirty_rect
.IsEmpty(); }
49 // Reset update state for the current frame. This should occur before painting
50 // for all layers. Since painting one layer can invalidate another layer after
51 // it has already painted, mark all non-dirty tiles as valid before painting
52 // such that invalidations during painting won't prevent them from being
54 void ResetUpdateState() {
55 update_rect
= gfx::Rect();
57 partial_update
= false;
58 valid_for_frame
= !is_dirty();
61 // This promises to update the tile and therefore also guarantees the tile
62 // will be valid for this frame. dirty_rect is copied into update_rect so we
63 // can continue to track re-entrant invalidations that occur during painting.
64 void MarkForUpdate() {
65 valid_for_frame
= true;
66 update_rect
= dirty_rect
;
67 dirty_rect
= gfx::Rect();
71 gfx::Rect update_rect
;
77 explicit UpdatableTile(scoped_ptr
<LayerUpdater::Resource
> updater_resource
)
78 : partial_update(false),
79 valid_for_frame(false),
81 updater_resource_(updater_resource
.Pass()) {}
83 scoped_ptr
<LayerUpdater::Resource
> updater_resource_
;
85 DISALLOW_COPY_AND_ASSIGN(UpdatableTile
);
88 TiledLayer::TiledLayer()
89 : ContentsScalingLayer(),
90 texture_format_(RGBA_8888
),
92 failed_update_(false),
93 tiling_option_(AUTO_TILE
) {
95 LayerTilingData::Create(gfx::Size(), LayerTilingData::HAS_BORDER_TEXELS
);
98 TiledLayer::~TiledLayer() {}
100 scoped_ptr
<LayerImpl
> TiledLayer::CreateLayerImpl(LayerTreeImpl
* tree_impl
) {
101 return TiledLayerImpl::Create(tree_impl
, id()).PassAs
<LayerImpl
>();
104 void TiledLayer::UpdateTileSizeAndTilingOption() {
105 DCHECK(layer_tree_host());
107 gfx::Size default_tile_size
= layer_tree_host()->settings().default_tile_size
;
108 gfx::Size max_untiled_layer_size
=
109 layer_tree_host()->settings().max_untiled_layer_size
;
110 int layer_width
= content_bounds().width();
111 int layer_height
= content_bounds().height();
113 gfx::Size
tile_size(std::min(default_tile_size
.width(), layer_width
),
114 std::min(default_tile_size
.height(), layer_height
));
116 // Tile if both dimensions large, or any one dimension large and the other
117 // extends into a second tile but the total layer area isn't larger than that
118 // of the largest possible untiled layer. This heuristic allows for long
119 // skinny layers (e.g. scrollbars) that are Nx1 tiles to minimize wasted
120 // texture space but still avoids creating very large tiles.
121 bool any_dimension_large
= layer_width
> max_untiled_layer_size
.width() ||
122 layer_height
> max_untiled_layer_size
.height();
123 bool any_dimension_one_tile
=
124 (layer_width
<= default_tile_size
.width() ||
125 layer_height
<= default_tile_size
.height()) &&
126 (layer_width
* layer_height
) <= (max_untiled_layer_size
.width() *
127 max_untiled_layer_size
.height());
128 bool auto_tiled
= any_dimension_large
&& !any_dimension_one_tile
;
131 if (tiling_option_
== ALWAYS_TILE
)
133 else if (tiling_option_
== NEVER_TILE
)
136 is_tiled
= auto_tiled
;
138 gfx::Size requested_size
= is_tiled
? tile_size
: content_bounds();
140 layer_tree_host()->GetRendererCapabilities().max_texture_size
;
141 requested_size
.SetToMin(gfx::Size(max_size
, max_size
));
142 SetTileSize(requested_size
);
145 void TiledLayer::UpdateBounds() {
146 gfx::Size old_tiling_size
= tiler_
->tiling_size();
147 gfx::Size new_tiling_size
= content_bounds();
148 if (old_tiling_size
== new_tiling_size
)
150 tiler_
->SetTilingSize(new_tiling_size
);
152 // Invalidate any areas that the new bounds exposes.
154 SubtractRegions(gfx::Rect(new_tiling_size
), gfx::Rect(old_tiling_size
));
155 for (Region::Iterator
new_rects(new_region
); new_rects
.has_rect();
157 InvalidateContentRect(new_rects
.rect());
158 UpdateDrawsContent(HasDrawableContent());
161 void TiledLayer::SetTileSize(const gfx::Size
& size
) {
162 tiler_
->SetTileSize(size
);
163 UpdateDrawsContent(HasDrawableContent());
166 void TiledLayer::SetBorderTexelOption(
167 LayerTilingData::BorderTexelOption border_texel_option
) {
168 tiler_
->SetBorderTexelOption(border_texel_option
);
169 UpdateDrawsContent(HasDrawableContent());
172 bool TiledLayer::HasDrawableContent() const {
173 bool has_more_than_one_tile
=
174 (tiler_
->num_tiles_x() > 1) || (tiler_
->num_tiles_y() > 1);
176 return !(tiling_option_
== NEVER_TILE
&& has_more_than_one_tile
) &&
177 ContentsScalingLayer::HasDrawableContent();
180 void TiledLayer::ReduceMemoryUsage() {
182 Updater()->ReduceMemoryUsage();
185 void TiledLayer::SetIsMask(bool is_mask
) {
186 set_tiling_option(is_mask
? NEVER_TILE
: AUTO_TILE
);
189 void TiledLayer::PushPropertiesTo(LayerImpl
* layer
) {
190 ContentsScalingLayer::PushPropertiesTo(layer
);
192 TiledLayerImpl
* tiled_layer
= static_cast<TiledLayerImpl
*>(layer
);
194 tiled_layer
->set_skips_draw(skips_draw_
);
195 tiled_layer
->SetTilingData(*tiler_
);
196 std::vector
<UpdatableTile
*> invalid_tiles
;
198 for (LayerTilingData::TileMap::const_iterator iter
= tiler_
->tiles().begin();
199 iter
!= tiler_
->tiles().end();
201 int i
= iter
->first
.first
;
202 int j
= iter
->first
.second
;
203 UpdatableTile
* tile
= static_cast<UpdatableTile
*>(iter
->second
);
204 // TODO(enne): This should not ever be null.
208 if (!tile
->managed_resource()->have_backing_texture()) {
209 // Evicted tiles get deleted from both layers
210 invalid_tiles
.push_back(tile
);
214 if (!tile
->valid_for_frame
) {
215 // Invalidated tiles are set so they can get different debug colors.
216 tiled_layer
->PushInvalidTile(i
, j
);
220 tiled_layer
->PushTileProperties(
223 tile
->managed_resource()->resource_id(),
225 tile
->managed_resource()->contents_swizzled());
227 for (std::vector
<UpdatableTile
*>::const_iterator iter
= invalid_tiles
.begin();
228 iter
!= invalid_tiles
.end();
230 tiler_
->TakeTile((*iter
)->i(), (*iter
)->j());
232 // TiledLayer must push properties every frame, since viewport state and
233 // occlusion from anywhere in the tree can change what the layer decides to
234 // push to the impl tree.
235 needs_push_properties_
= true;
238 PrioritizedResourceManager
* TiledLayer::ResourceManager() {
239 if (!layer_tree_host())
241 return layer_tree_host()->contents_texture_manager();
244 const PrioritizedResource
* TiledLayer::ResourceAtForTesting(int i
,
246 UpdatableTile
* tile
= TileAt(i
, j
);
249 return tile
->managed_resource();
252 void TiledLayer::SetLayerTreeHost(LayerTreeHost
* host
) {
253 if (host
&& host
!= layer_tree_host()) {
254 for (LayerTilingData::TileMap::const_iterator
255 iter
= tiler_
->tiles().begin();
256 iter
!= tiler_
->tiles().end();
258 UpdatableTile
* tile
= static_cast<UpdatableTile
*>(iter
->second
);
259 // TODO(enne): This should not ever be null.
262 tile
->managed_resource()->SetTextureManager(
263 host
->contents_texture_manager());
266 ContentsScalingLayer::SetLayerTreeHost(host
);
269 UpdatableTile
* TiledLayer::TileAt(int i
, int j
) const {
270 return static_cast<UpdatableTile
*>(tiler_
->TileAt(i
, j
));
273 UpdatableTile
* TiledLayer::CreateTile(int i
, int j
) {
274 CreateUpdaterIfNeeded();
276 scoped_ptr
<UpdatableTile
> tile(
277 UpdatableTile::Create(Updater()->CreateResource(ResourceManager())));
278 tile
->managed_resource()->SetDimensions(tiler_
->tile_size(), texture_format_
);
280 UpdatableTile
* added_tile
= tile
.get();
281 tiler_
->AddTile(tile
.PassAs
<LayerTilingData::Tile
>(), i
, j
);
283 added_tile
->dirty_rect
= tiler_
->TileRect(added_tile
);
285 // Temporary diagnostic crash.
292 void TiledLayer::SetNeedsDisplayRect(const gfx::RectF
& dirty_rect
) {
293 InvalidateContentRect(LayerRectToContentRect(dirty_rect
));
294 ContentsScalingLayer::SetNeedsDisplayRect(dirty_rect
);
297 void TiledLayer::InvalidateContentRect(const gfx::Rect
& content_rect
) {
299 if (tiler_
->is_empty() || content_rect
.IsEmpty() || skips_draw_
)
302 for (LayerTilingData::TileMap::const_iterator iter
= tiler_
->tiles().begin();
303 iter
!= tiler_
->tiles().end();
305 UpdatableTile
* tile
= static_cast<UpdatableTile
*>(iter
->second
);
307 // TODO(enne): This should not ever be null.
310 gfx::Rect bound
= tiler_
->TileRect(tile
);
311 bound
.Intersect(content_rect
);
312 tile
->dirty_rect
.Union(bound
);
316 // Returns true if tile is dirty and only part of it needs to be updated.
317 bool TiledLayer::TileOnlyNeedsPartialUpdate(UpdatableTile
* tile
) {
318 return !tile
->dirty_rect
.Contains(tiler_
->TileRect(tile
)) &&
319 tile
->managed_resource()->have_backing_texture();
322 bool TiledLayer::UpdateTiles(int left
,
326 ResourceUpdateQueue
* queue
,
327 const OcclusionTracker
<Layer
>* occlusion
,
329 CreateUpdaterIfNeeded();
331 bool ignore_occlusions
= !occlusion
;
332 if (!HaveTexturesForTiles(left
, top
, right
, bottom
, ignore_occlusions
)) {
333 failed_update_
= true;
337 gfx::Rect update_rect
;
338 gfx::Rect paint_rect
;
340 &update_rect
, &paint_rect
, left
, top
, right
, bottom
, ignore_occlusions
);
342 if (paint_rect
.IsEmpty())
347 update_rect
, paint_rect
, left
, top
, right
, bottom
, queue
, occlusion
);
351 void TiledLayer::MarkOcclusionsAndRequestTextures(
356 const OcclusionTracker
<Layer
>* occlusion
) {
357 int occluded_tile_count
= 0;
358 bool succeeded
= true;
359 for (int j
= top
; j
<= bottom
; ++j
) {
360 for (int i
= left
; i
<= right
; ++i
) {
361 UpdatableTile
* tile
= TileAt(i
, j
);
362 DCHECK(tile
); // Did SetTexturePriorities get skipped?
363 // TODO(enne): This should not ever be null.
366 // Did ResetUpdateState get skipped? Are we doing more than one occlusion
368 DCHECK(!tile
->occluded
);
369 gfx::Rect visible_tile_rect
= gfx::IntersectRects(
370 tiler_
->tile_bounds(i
, j
), visible_content_rect());
371 if (!draw_transform_is_animating() && occlusion
&&
373 render_target(), visible_tile_rect
, draw_transform())) {
374 tile
->occluded
= true;
375 occluded_tile_count
++;
377 succeeded
&= tile
->managed_resource()->RequestLate();
383 bool TiledLayer::HaveTexturesForTiles(int left
,
387 bool ignore_occlusions
) {
388 for (int j
= top
; j
<= bottom
; ++j
) {
389 for (int i
= left
; i
<= right
; ++i
) {
390 UpdatableTile
* tile
= TileAt(i
, j
);
391 DCHECK(tile
); // Did SetTexturePriorites get skipped?
392 // TODO(enne): This should not ever be null.
396 // Ensure the entire tile is dirty if we don't have the texture.
397 if (!tile
->managed_resource()->have_backing_texture())
398 tile
->dirty_rect
= tiler_
->TileRect(tile
);
400 // If using occlusion and the visible region of the tile is occluded,
401 // don't reserve a texture or update the tile.
402 if (tile
->occluded
&& !ignore_occlusions
)
405 if (!tile
->managed_resource()->can_acquire_backing_texture())
412 void TiledLayer::MarkTilesForUpdate(gfx::Rect
* update_rect
,
413 gfx::Rect
* paint_rect
,
418 bool ignore_occlusions
) {
419 for (int j
= top
; j
<= bottom
; ++j
) {
420 for (int i
= left
; i
<= right
; ++i
) {
421 UpdatableTile
* tile
= TileAt(i
, j
);
422 DCHECK(tile
); // Did SetTexturePriorites get skipped?
423 // TODO(enne): This should not ever be null.
426 if (tile
->occluded
&& !ignore_occlusions
)
429 // Prepare update rect from original dirty rects.
430 update_rect
->Union(tile
->dirty_rect
);
432 // TODO(reveman): Decide if partial update should be allowed based on cost
433 // of update. https://bugs.webkit.org/show_bug.cgi?id=77376
434 if (tile
->is_dirty() &&
435 !layer_tree_host()->AlwaysUsePartialTextureUpdates()) {
436 // If we get a partial update, we use the same texture, otherwise return
437 // the current texture backing, so we don't update visible textures
438 // non-atomically. If the current backing is in-use, it won't be
439 // deleted until after the commit as the texture manager will not allow
440 // deletion or recycling of in-use textures.
441 if (TileOnlyNeedsPartialUpdate(tile
) &&
442 layer_tree_host()->RequestPartialTextureUpdate()) {
443 tile
->partial_update
= true;
445 tile
->dirty_rect
= tiler_
->TileRect(tile
);
446 tile
->managed_resource()->ReturnBackingTexture();
450 paint_rect
->Union(tile
->dirty_rect
);
451 tile
->MarkForUpdate();
456 void TiledLayer::UpdateTileTextures(const gfx::Rect
& update_rect
,
457 const gfx::Rect
& paint_rect
,
462 ResourceUpdateQueue
* queue
,
463 const OcclusionTracker
<Layer
>* occlusion
) {
464 // The update_rect should be in layer space. So we have to convert the
465 // paint_rect from content space to layer space.
467 paint_properties().bounds
.width() /
468 static_cast<float>(content_bounds().width());
470 paint_properties().bounds
.height() /
471 static_cast<float>(content_bounds().height());
472 update_rect_
= gfx::ScaleRect(update_rect
, width_scale
, height_scale
);
474 // Calling PrepareToUpdate() calls into WebKit to paint, which may have the
475 // side effect of disabling compositing, which causes our reference to the
476 // texture updater to be deleted. However, we can't free the memory backing
477 // the SkCanvas until the paint finishes, so we grab a local reference here to
478 // hold the updater alive until the paint completes.
479 scoped_refptr
<LayerUpdater
> protector(Updater());
480 gfx::Rect painted_opaque_rect
;
481 Updater()->PrepareToUpdate(paint_rect
,
485 &painted_opaque_rect
);
487 for (int j
= top
; j
<= bottom
; ++j
) {
488 for (int i
= left
; i
<= right
; ++i
) {
489 UpdatableTile
* tile
= TileAt(i
, j
);
490 DCHECK(tile
); // Did SetTexturePriorites get skipped?
491 // TODO(enne): This should not ever be null.
495 gfx::Rect tile_rect
= tiler_
->tile_bounds(i
, j
);
497 // Use update_rect as the above loop copied the dirty rect for this frame
499 gfx::Rect dirty_rect
= tile
->update_rect
;
500 if (dirty_rect
.IsEmpty())
503 // Save what was painted opaque in the tile. Keep the old area if the
504 // paint didn't touch it, and didn't paint some other part of the tile
506 gfx::Rect tile_painted_rect
= gfx::IntersectRects(tile_rect
, paint_rect
);
507 gfx::Rect tile_painted_opaque_rect
=
508 gfx::IntersectRects(tile_rect
, painted_opaque_rect
);
509 if (!tile_painted_rect
.IsEmpty()) {
510 gfx::Rect paint_inside_tile_opaque_rect
=
511 gfx::IntersectRects(tile
->opaque_rect(), tile_painted_rect
);
512 bool paint_inside_tile_opaque_rect_is_non_opaque
=
513 !paint_inside_tile_opaque_rect
.IsEmpty() &&
514 !tile_painted_opaque_rect
.Contains(paint_inside_tile_opaque_rect
);
515 bool opaque_paint_not_inside_tile_opaque_rect
=
516 !tile_painted_opaque_rect
.IsEmpty() &&
517 !tile
->opaque_rect().Contains(tile_painted_opaque_rect
);
519 if (paint_inside_tile_opaque_rect_is_non_opaque
||
520 opaque_paint_not_inside_tile_opaque_rect
)
521 tile
->set_opaque_rect(tile_painted_opaque_rect
);
524 // source_rect starts as a full-sized tile with border texels included.
525 gfx::Rect source_rect
= tiler_
->TileRect(tile
);
526 source_rect
.Intersect(dirty_rect
);
527 // Paint rect not guaranteed to line up on tile boundaries, so
528 // make sure that source_rect doesn't extend outside of it.
529 source_rect
.Intersect(paint_rect
);
531 tile
->update_rect
= source_rect
;
533 if (source_rect
.IsEmpty())
536 const gfx::Point anchor
= tiler_
->TileRect(tile
).origin();
538 // Calculate tile-space rectangle to upload into.
539 gfx::Vector2d dest_offset
= source_rect
.origin() - anchor
;
540 CHECK_GE(dest_offset
.x(), 0);
541 CHECK_GE(dest_offset
.y(), 0);
543 // Offset from paint rectangle to this tile's dirty rectangle.
544 gfx::Vector2d paint_offset
= source_rect
.origin() - paint_rect
.origin();
545 CHECK_GE(paint_offset
.x(), 0);
546 CHECK_GE(paint_offset
.y(), 0);
547 CHECK_LE(paint_offset
.x() + source_rect
.width(), paint_rect
.width());
548 CHECK_LE(paint_offset
.y() + source_rect
.height(), paint_rect
.height());
550 tile
->updater_resource()->Update(
551 queue
, source_rect
, dest_offset
, tile
->partial_update
);
556 // This picks a small animated layer to be anything less than one viewport. This
557 // is specifically for page transitions which are viewport-sized layers. The
558 // extra tile of padding is due to these layers being slightly larger than the
559 // viewport in some cases.
560 bool TiledLayer::IsSmallAnimatedLayer() const {
561 if (!draw_transform_is_animating() && !screen_space_transform_is_animating())
563 gfx::Size viewport_size
=
564 layer_tree_host() ? layer_tree_host()->device_viewport_size()
566 gfx::Rect
content_rect(content_bounds());
567 return content_rect
.width() <=
568 viewport_size
.width() + tiler_
->tile_size().width() &&
569 content_rect
.height() <=
570 viewport_size
.height() + tiler_
->tile_size().height();
574 // TODO(epenner): Remove this and make this based on distance once distance can
575 // be calculated for offscreen layers. For now, prioritize all small animated
576 // layers after 512 pixels of pre-painting.
577 void SetPriorityForTexture(const gfx::Rect
& visible_rect
,
578 const gfx::Rect
& tile_rect
,
580 bool is_small_animated_layer
,
581 PrioritizedResource
* texture
) {
582 int priority
= PriorityCalculator::LowestPriority();
583 if (!visible_rect
.IsEmpty()) {
584 priority
= PriorityCalculator::PriorityFromDistance(
585 visible_rect
, tile_rect
, draws_to_root
);
588 if (is_small_animated_layer
) {
589 priority
= PriorityCalculator::max_priority(
590 priority
, PriorityCalculator::SmallAnimatedLayerMinPriority());
593 if (priority
!= PriorityCalculator::LowestPriority())
594 texture
->set_request_priority(priority
);
598 void TiledLayer::SetTexturePriorities(const PriorityCalculator
& priority_calc
) {
601 UpdateScrollPrediction();
603 if (tiler_
->has_empty_bounds())
606 bool draws_to_root
= !render_target()->parent();
607 bool small_animated_layer
= IsSmallAnimatedLayer();
609 // Minimally create the tiles in the desired pre-paint rect.
610 gfx::Rect create_tiles_rect
= IdlePaintRect();
611 if (small_animated_layer
)
612 create_tiles_rect
= gfx::Rect(content_bounds());
613 if (!create_tiles_rect
.IsEmpty()) {
614 int left
, top
, right
, bottom
;
615 tiler_
->ContentRectToTileIndices(
616 create_tiles_rect
, &left
, &top
, &right
, &bottom
);
617 for (int j
= top
; j
<= bottom
; ++j
) {
618 for (int i
= left
; i
<= right
; ++i
) {
625 // Now update priorities on all tiles we have in the layer, no matter where
627 for (LayerTilingData::TileMap::const_iterator iter
= tiler_
->tiles().begin();
628 iter
!= tiler_
->tiles().end();
630 UpdatableTile
* tile
= static_cast<UpdatableTile
*>(iter
->second
);
631 // TODO(enne): This should not ever be null.
634 gfx::Rect tile_rect
= tiler_
->TileRect(tile
);
635 SetPriorityForTexture(predicted_visible_rect_
,
638 small_animated_layer
,
639 tile
->managed_resource());
643 SimpleEnclosedRegion
TiledLayer::VisibleContentOpaqueRegion() const {
645 return SimpleEnclosedRegion();
646 if (contents_opaque())
647 return SimpleEnclosedRegion(visible_content_rect());
648 return tiler_
->OpaqueRegionInContentRect(visible_content_rect());
651 void TiledLayer::ResetUpdateState() {
653 failed_update_
= false;
655 LayerTilingData::TileMap::const_iterator end
= tiler_
->tiles().end();
656 for (LayerTilingData::TileMap::const_iterator iter
= tiler_
->tiles().begin();
659 UpdatableTile
* tile
= static_cast<UpdatableTile
*>(iter
->second
);
660 // TODO(enne): This should not ever be null.
663 tile
->ResetUpdateState();
668 gfx::Rect
ExpandRectByDelta(const gfx::Rect
& rect
, const gfx::Vector2d
& delta
) {
669 int width
= rect
.width() + std::abs(delta
.x());
670 int height
= rect
.height() + std::abs(delta
.y());
671 int x
= rect
.x() + ((delta
.x() < 0) ? delta
.x() : 0);
672 int y
= rect
.y() + ((delta
.y() < 0) ? delta
.y() : 0);
673 return gfx::Rect(x
, y
, width
, height
);
677 void TiledLayer::UpdateScrollPrediction() {
678 // This scroll prediction is very primitive and should be replaced by a
679 // a recursive calculation on all layers which uses actual scroll/animation
680 // velocities. To insure this doesn't miss-predict, we only use it to predict
681 // the visible_rect if:
682 // - content_bounds() hasn't changed.
683 // - visible_rect.size() hasn't changed.
684 // These two conditions prevent rotations, scales, pinch-zooms etc. where
685 // the prediction would be incorrect.
686 gfx::Vector2d delta
= visible_content_rect().CenterPoint() -
687 previous_visible_rect_
.CenterPoint();
688 predicted_scroll_
= -delta
;
689 predicted_visible_rect_
= visible_content_rect();
690 if (previous_content_bounds_
== content_bounds() &&
691 previous_visible_rect_
.size() == visible_content_rect().size()) {
692 // Only expand the visible rect in the major scroll direction, to prevent
693 // massive paints due to diagonal scrolls.
694 gfx::Vector2d major_scroll_delta
=
695 (std::abs(delta
.x()) > std::abs(delta
.y())) ?
696 gfx::Vector2d(delta
.x(), 0) :
697 gfx::Vector2d(0, delta
.y());
698 predicted_visible_rect_
=
699 ExpandRectByDelta(visible_content_rect(), major_scroll_delta
);
701 // Bound the prediction to prevent unbounded paints, and clamp to content
703 gfx::Rect bound
= visible_content_rect();
704 bound
.Inset(-tiler_
->tile_size().width() * kMaxPredictiveTilesCount
,
705 -tiler_
->tile_size().height() * kMaxPredictiveTilesCount
);
706 bound
.Intersect(gfx::Rect(content_bounds()));
707 predicted_visible_rect_
.Intersect(bound
);
709 previous_content_bounds_
= content_bounds();
710 previous_visible_rect_
= visible_content_rect();
713 bool TiledLayer::Update(ResourceUpdateQueue
* queue
,
714 const OcclusionTracker
<Layer
>* occlusion
) {
715 DCHECK(!skips_draw_
&& !failed_update_
); // Did ResetUpdateState get skipped?
717 // Tiled layer always causes commits to wait for activation, as it does
718 // not support pending trees.
719 SetNextCommitWaitsForActivation();
721 bool updated
= false;
724 base::AutoReset
<bool> ignore_set_needs_commit(&ignore_set_needs_commit_
,
727 updated
|= ContentsScalingLayer::Update(queue
, occlusion
);
731 if (tiler_
->has_empty_bounds() || !DrawsContent())
734 // Animation pre-paint. If the layer is small, try to paint it all
735 // immediately whether or not it is occluded, to avoid paint/upload
736 // hiccups while it is animating.
737 if (IsSmallAnimatedLayer()) {
738 int left
, top
, right
, bottom
;
739 tiler_
->ContentRectToTileIndices(gfx::Rect(content_bounds()),
744 UpdateTiles(left
, top
, right
, bottom
, queue
, NULL
, &updated
);
747 // This was an attempt to paint the entire layer so if we fail it's okay,
748 // just fallback on painting visible etc. below.
749 failed_update_
= false;
752 if (predicted_visible_rect_
.IsEmpty())
755 // Visible painting. First occlude visible tiles and paint the non-occluded
757 int left
, top
, right
, bottom
;
758 tiler_
->ContentRectToTileIndices(
759 predicted_visible_rect_
, &left
, &top
, &right
, &bottom
);
760 MarkOcclusionsAndRequestTextures(left
, top
, right
, bottom
, occlusion
);
761 skips_draw_
= !UpdateTiles(
762 left
, top
, right
, bottom
, queue
, occlusion
, &updated
);
765 if (skips_draw_
|| updated
)
768 // If we have already painting everything visible. Do some pre-painting while
770 gfx::Rect idle_paint_content_rect
= IdlePaintRect();
771 if (idle_paint_content_rect
.IsEmpty())
774 // Prepaint anything that was occluded but inside the layer's visible region.
775 if (!UpdateTiles(left
, top
, right
, bottom
, queue
, NULL
, &updated
) ||
779 int prepaint_left
, prepaint_top
, prepaint_right
, prepaint_bottom
;
780 tiler_
->ContentRectToTileIndices(idle_paint_content_rect
,
786 // Then expand outwards one row/column at a time until we find a dirty
787 // row/column to update. Increment along the major and minor scroll directions
789 gfx::Vector2d delta
= -predicted_scroll_
;
790 delta
= gfx::Vector2d(delta
.x() == 0 ? 1 : delta
.x(),
791 delta
.y() == 0 ? 1 : delta
.y());
792 gfx::Vector2d major_delta
=
793 (std::abs(delta
.x()) > std::abs(delta
.y())) ? gfx::Vector2d(delta
.x(), 0)
794 : gfx::Vector2d(0, delta
.y());
795 gfx::Vector2d minor_delta
=
796 (std::abs(delta
.x()) <= std::abs(delta
.y())) ? gfx::Vector2d(delta
.x(), 0)
797 : gfx::Vector2d(0, delta
.y());
798 gfx::Vector2d deltas
[4] = { major_delta
, minor_delta
, -major_delta
,
800 for (int i
= 0; i
< 4; i
++) {
801 if (deltas
[i
].y() > 0) {
802 while (bottom
< prepaint_bottom
) {
805 left
, bottom
, right
, bottom
, queue
, NULL
, &updated
) ||
810 if (deltas
[i
].y() < 0) {
811 while (top
> prepaint_top
) {
814 left
, top
, right
, top
, queue
, NULL
, &updated
) ||
819 if (deltas
[i
].x() < 0) {
820 while (left
> prepaint_left
) {
823 left
, top
, left
, bottom
, queue
, NULL
, &updated
) ||
828 if (deltas
[i
].x() > 0) {
829 while (right
< prepaint_right
) {
832 right
, top
, right
, bottom
, queue
, NULL
, &updated
) ||
841 void TiledLayer::OnOutputSurfaceCreated() {
842 // Ensure that all textures are of the right format.
843 for (LayerTilingData::TileMap::const_iterator iter
= tiler_
->tiles().begin();
844 iter
!= tiler_
->tiles().end();
846 UpdatableTile
* tile
= static_cast<UpdatableTile
*>(iter
->second
);
849 PrioritizedResource
* resource
= tile
->managed_resource();
850 resource
->SetDimensions(resource
->size(), texture_format_
);
854 bool TiledLayer::NeedsIdlePaint() {
855 // Don't trigger more paints if we failed (as we'll just fail again).
856 if (failed_update_
|| visible_content_rect().IsEmpty() ||
857 tiler_
->has_empty_bounds() || !DrawsContent())
860 gfx::Rect idle_paint_content_rect
= IdlePaintRect();
861 if (idle_paint_content_rect
.IsEmpty())
864 int left
, top
, right
, bottom
;
865 tiler_
->ContentRectToTileIndices(
866 idle_paint_content_rect
, &left
, &top
, &right
, &bottom
);
868 for (int j
= top
; j
<= bottom
; ++j
) {
869 for (int i
= left
; i
<= right
; ++i
) {
870 UpdatableTile
* tile
= TileAt(i
, j
);
871 DCHECK(tile
); // Did SetTexturePriorities get skipped?
875 bool updated
= !tile
->update_rect
.IsEmpty();
877 tile
->managed_resource()->can_acquire_backing_texture();
879 tile
->is_dirty() || !tile
->managed_resource()->have_backing_texture();
880 if (!updated
&& can_acquire
&& dirty
)
887 gfx::Rect
TiledLayer::IdlePaintRect() {
888 // Don't inflate an empty rect.
889 if (visible_content_rect().IsEmpty())
892 gfx::Rect prepaint_rect
= visible_content_rect();
893 prepaint_rect
.Inset(-tiler_
->tile_size().width() * kPrepaintColumns
,
894 -tiler_
->tile_size().height() * kPrepaintRows
);
895 gfx::Rect
content_rect(content_bounds());
896 prepaint_rect
.Intersect(content_rect
);
898 return prepaint_rect
;