Remove NavigationRequestInfo::is_showing.
[chromium-blink-merge.git] / cc / layers / tiled_layer.cc
blob4a960a84f5261e2b4ff0fb4db8c079fb9f181dfa
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"
7 #include <algorithm>
8 #include <vector>
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"
24 namespace cc {
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 {
36 public:
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
53 // pushed.
54 void ResetUpdateState() {
55 update_rect = gfx::Rect();
56 occluded = false;
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();
70 gfx::Rect dirty_rect;
71 gfx::Rect update_rect;
72 bool partial_update;
73 bool valid_for_frame;
74 bool occluded;
76 private:
77 explicit UpdatableTile(scoped_ptr<LayerUpdater::Resource> updater_resource)
78 : partial_update(false),
79 valid_for_frame(false),
80 occluded(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),
91 skips_draw_(false),
92 failed_update_(false),
93 tiling_option_(AUTO_TILE) {
94 tiler_ =
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;
130 bool is_tiled;
131 if (tiling_option_ == ALWAYS_TILE)
132 is_tiled = true;
133 else if (tiling_option_ == NEVER_TILE)
134 is_tiled = false;
135 else
136 is_tiled = auto_tiled;
138 gfx::Size requested_size = is_tiled ? tile_size : content_bounds();
139 const int max_size =
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)
149 return;
150 tiler_->SetTilingSize(new_tiling_size);
152 // Invalidate any areas that the new bounds exposes.
153 Region new_region =
154 SubtractRegions(gfx::Rect(new_tiling_size), gfx::Rect(old_tiling_size));
155 for (Region::Iterator new_rects(new_region); new_rects.has_rect();
156 new_rects.next())
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() {
181 if (Updater())
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();
200 ++iter) {
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.
205 if (!tile)
206 continue;
208 if (!tile->managed_resource()->have_backing_texture()) {
209 // Evicted tiles get deleted from both layers
210 invalid_tiles.push_back(tile);
211 continue;
214 if (!tile->valid_for_frame) {
215 // Invalidated tiles are set so they can get different debug colors.
216 tiled_layer->PushInvalidTile(i, j);
217 continue;
220 tiled_layer->PushTileProperties(
223 tile->managed_resource()->resource_id(),
224 tile->opaque_rect(),
225 tile->managed_resource()->contents_swizzled());
227 for (std::vector<UpdatableTile*>::const_iterator iter = invalid_tiles.begin();
228 iter != invalid_tiles.end();
229 ++iter)
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())
240 return NULL;
241 return layer_tree_host()->contents_texture_manager();
244 const PrioritizedResource* TiledLayer::ResourceAtForTesting(int i,
245 int j) const {
246 UpdatableTile* tile = TileAt(i, j);
247 if (!tile)
248 return NULL;
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();
257 ++iter) {
258 UpdatableTile* tile = static_cast<UpdatableTile*>(iter->second);
259 // TODO(enne): This should not ever be null.
260 if (!tile)
261 continue;
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.
286 CHECK(added_tile);
287 CHECK(TileAt(i, j));
289 return added_tile;
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) {
298 UpdateBounds();
299 if (tiler_->is_empty() || content_rect.IsEmpty() || skips_draw_)
300 return;
302 for (LayerTilingData::TileMap::const_iterator iter = tiler_->tiles().begin();
303 iter != tiler_->tiles().end();
304 ++iter) {
305 UpdatableTile* tile = static_cast<UpdatableTile*>(iter->second);
306 DCHECK(tile);
307 // TODO(enne): This should not ever be null.
308 if (!tile)
309 continue;
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,
323 int top,
324 int right,
325 int bottom,
326 ResourceUpdateQueue* queue,
327 const OcclusionTracker<Layer>* occlusion,
328 bool* updated) {
329 CreateUpdaterIfNeeded();
331 bool ignore_occlusions = !occlusion;
332 if (!HaveTexturesForTiles(left, top, right, bottom, ignore_occlusions)) {
333 failed_update_ = true;
334 return false;
337 gfx::Rect update_rect;
338 gfx::Rect paint_rect;
339 MarkTilesForUpdate(
340 &update_rect, &paint_rect, left, top, right, bottom, ignore_occlusions);
342 if (paint_rect.IsEmpty())
343 return true;
345 *updated = true;
346 UpdateTileTextures(
347 update_rect, paint_rect, left, top, right, bottom, queue, occlusion);
348 return true;
351 void TiledLayer::MarkOcclusionsAndRequestTextures(
352 int left,
353 int top,
354 int right,
355 int bottom,
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.
364 if (!tile)
365 continue;
366 // Did ResetUpdateState get skipped? Are we doing more than one occlusion
367 // pass?
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 &&
372 occlusion->Occluded(
373 render_target(), visible_tile_rect, draw_transform())) {
374 tile->occluded = true;
375 occluded_tile_count++;
376 } else {
377 succeeded &= tile->managed_resource()->RequestLate();
383 bool TiledLayer::HaveTexturesForTiles(int left,
384 int top,
385 int right,
386 int bottom,
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.
393 if (!tile)
394 continue;
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)
403 continue;
405 if (!tile->managed_resource()->can_acquire_backing_texture())
406 return false;
409 return true;
412 void TiledLayer::MarkTilesForUpdate(gfx::Rect* update_rect,
413 gfx::Rect* paint_rect,
414 int left,
415 int top,
416 int right,
417 int bottom,
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.
424 if (!tile)
425 continue;
426 if (tile->occluded && !ignore_occlusions)
427 continue;
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;
444 } else {
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,
458 int left,
459 int top,
460 int right,
461 int bottom,
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.
466 float width_scale =
467 paint_properties().bounds.width() /
468 static_cast<float>(content_bounds().width());
469 float height_scale =
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,
482 tiler_->tile_size(),
483 1.f / width_scale,
484 1.f / height_scale,
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.
492 if (!tile)
493 continue;
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
498 // to update_rect.
499 gfx::Rect dirty_rect = tile->update_rect;
500 if (dirty_rect.IsEmpty())
501 continue;
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
505 // opaque.
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())
534 continue;
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())
562 return false;
563 gfx::Size viewport_size =
564 layer_tree_host() ? layer_tree_host()->device_viewport_size()
565 : gfx::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();
573 namespace {
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,
579 bool draws_to_root,
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);
596 } // namespace
598 void TiledLayer::SetTexturePriorities(const PriorityCalculator& priority_calc) {
599 UpdateBounds();
600 ResetUpdateState();
601 UpdateScrollPrediction();
603 if (tiler_->has_empty_bounds())
604 return;
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) {
619 if (!TileAt(i, j))
620 CreateTile(i, j);
625 // Now update priorities on all tiles we have in the layer, no matter where
626 // they are.
627 for (LayerTilingData::TileMap::const_iterator iter = tiler_->tiles().begin();
628 iter != tiler_->tiles().end();
629 ++iter) {
630 UpdatableTile* tile = static_cast<UpdatableTile*>(iter->second);
631 // TODO(enne): This should not ever be null.
632 if (!tile)
633 continue;
634 gfx::Rect tile_rect = tiler_->TileRect(tile);
635 SetPriorityForTexture(predicted_visible_rect_,
636 tile_rect,
637 draws_to_root,
638 small_animated_layer,
639 tile->managed_resource());
643 SimpleEnclosedRegion TiledLayer::VisibleContentOpaqueRegion() const {
644 if (skips_draw_)
645 return SimpleEnclosedRegion();
646 if (contents_opaque())
647 return SimpleEnclosedRegion(visible_content_rect());
648 return tiler_->OpaqueRegionInContentRect(visible_content_rect());
651 void TiledLayer::ResetUpdateState() {
652 skips_draw_ = false;
653 failed_update_ = false;
655 LayerTilingData::TileMap::const_iterator end = tiler_->tiles().end();
656 for (LayerTilingData::TileMap::const_iterator iter = tiler_->tiles().begin();
657 iter != end;
658 ++iter) {
659 UpdatableTile* tile = static_cast<UpdatableTile*>(iter->second);
660 // TODO(enne): This should not ever be null.
661 if (!tile)
662 continue;
663 tile->ResetUpdateState();
667 namespace {
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
702 // bounds.
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_,
725 true);
727 updated |= ContentsScalingLayer::Update(queue, occlusion);
728 UpdateBounds();
731 if (tiler_->has_empty_bounds() || !DrawsContent())
732 return false;
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()),
740 &left,
741 &top,
742 &right,
743 &bottom);
744 UpdateTiles(left, top, right, bottom, queue, NULL, &updated);
745 if (updated)
746 return 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())
753 return updated;
755 // Visible painting. First occlude visible tiles and paint the non-occluded
756 // tiles.
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);
763 if (skips_draw_)
764 tiler_->reset();
765 if (skips_draw_ || updated)
766 return true;
768 // If we have already painting everything visible. Do some pre-painting while
769 // idle.
770 gfx::Rect idle_paint_content_rect = IdlePaintRect();
771 if (idle_paint_content_rect.IsEmpty())
772 return updated;
774 // Prepaint anything that was occluded but inside the layer's visible region.
775 if (!UpdateTiles(left, top, right, bottom, queue, NULL, &updated) ||
776 updated)
777 return updated;
779 int prepaint_left, prepaint_top, prepaint_right, prepaint_bottom;
780 tiler_->ContentRectToTileIndices(idle_paint_content_rect,
781 &prepaint_left,
782 &prepaint_top,
783 &prepaint_right,
784 &prepaint_bottom);
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
788 // first.
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,
799 -minor_delta };
800 for (int i = 0; i < 4; i++) {
801 if (deltas[i].y() > 0) {
802 while (bottom < prepaint_bottom) {
803 ++bottom;
804 if (!UpdateTiles(
805 left, bottom, right, bottom, queue, NULL, &updated) ||
806 updated)
807 return updated;
810 if (deltas[i].y() < 0) {
811 while (top > prepaint_top) {
812 --top;
813 if (!UpdateTiles(
814 left, top, right, top, queue, NULL, &updated) ||
815 updated)
816 return updated;
819 if (deltas[i].x() < 0) {
820 while (left > prepaint_left) {
821 --left;
822 if (!UpdateTiles(
823 left, top, left, bottom, queue, NULL, &updated) ||
824 updated)
825 return updated;
828 if (deltas[i].x() > 0) {
829 while (right < prepaint_right) {
830 ++right;
831 if (!UpdateTiles(
832 right, top, right, bottom, queue, NULL, &updated) ||
833 updated)
834 return updated;
838 return 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();
845 ++iter) {
846 UpdatableTile* tile = static_cast<UpdatableTile*>(iter->second);
847 if (!tile)
848 continue;
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())
858 return false;
860 gfx::Rect idle_paint_content_rect = IdlePaintRect();
861 if (idle_paint_content_rect.IsEmpty())
862 return false;
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?
872 if (!tile)
873 continue;
875 bool updated = !tile->update_rect.IsEmpty();
876 bool can_acquire =
877 tile->managed_resource()->can_acquire_backing_texture();
878 bool dirty =
879 tile->is_dirty() || !tile->managed_resource()->have_backing_texture();
880 if (!updated && can_acquire && dirty)
881 return true;
884 return false;
887 gfx::Rect TiledLayer::IdlePaintRect() {
888 // Don't inflate an empty rect.
889 if (visible_content_rect().IsEmpty())
890 return gfx::Rect();
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;
901 } // namespace cc