| 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 <algorithm> | |
| 6 | |
| 7 #include "cc/base/region.h" | |
| 8 #include "cc/picture_pile.h" | |
| 9 #include "cc/picture_pile_impl.h" | |
| 10 | |
| 11 namespace { | |
| 12 // Maximum number of pictures that can overlap before we collapse them into | |
| 13 // a larger one. | |
| 14 const int kMaxOverlapping = 2; | |
| 15 // Maximum percentage area of the base picture another picture in the picture | |
| 16 // list can be. If higher, we destroy the list and recreate from scratch. | |
| 17 const float kResetThreshold = 0.7f; | |
| 18 // Layout pixel buffer around the visible layer rect to record. Any base | |
| 19 // picture that intersects the visible layer rect expanded by this distance | |
| 20 // will be recorded. | |
| 21 const int kPixelDistanceToRecord = 8000; | |
| 22 } | |
| 23 | |
| 24 namespace cc { | |
| 25 | |
| 26 PicturePile::PicturePile() { | |
| 27 } | |
| 28 | |
| 29 PicturePile::~PicturePile() { | |
| 30 } | |
| 31 | |
| 32 void PicturePile::Update( | |
| 33 ContentLayerClient* painter, | |
| 34 SkColor background_color, | |
| 35 const Region& invalidation, | |
| 36 gfx::Rect visible_layer_rect, | |
| 37 RenderingStats* stats) { | |
| 38 background_color_ = background_color; | |
| 39 | |
| 40 gfx::Rect interest_rect = visible_layer_rect; | |
| 41 interest_rect.Inset( | |
| 42 -kPixelDistanceToRecord, | |
| 43 -kPixelDistanceToRecord, | |
| 44 -kPixelDistanceToRecord, | |
| 45 -kPixelDistanceToRecord); | |
| 46 for (Region::Iterator i(invalidation); i.has_rect(); i.next()) { | |
| 47 // Inflate all recordings from invalidations with a margin so that when | |
| 48 // scaled down to at least min_contents_scale, any final pixel touched by an | |
| 49 // invalidation can be fully rasterized by this picture. | |
| 50 gfx::Rect inflated_invalidation = i.rect(); | |
| 51 inflated_invalidation.Inset( | |
| 52 -buffer_pixels(), | |
| 53 -buffer_pixels(), | |
| 54 -buffer_pixels(), | |
| 55 -buffer_pixels()); | |
| 56 // Split this inflated invalidation across tile boundaries and apply it | |
| 57 // to all tiles that it touches. | |
| 58 for (TilingData::Iterator iter(&tiling_, inflated_invalidation); | |
| 59 iter; ++iter) { | |
| 60 gfx::Rect tile = | |
| 61 tiling_.TileBoundsWithBorder(iter.index_x(), iter.index_y()); | |
| 62 if (!tile.Intersects(interest_rect)) { | |
| 63 // This invalidation touches a tile outside the interest rect, so | |
| 64 // just remove the entire picture list. | |
| 65 picture_list_map_.erase(iter.index()); | |
| 66 continue; | |
| 67 } | |
| 68 | |
| 69 gfx::Rect tile_invalidation = | |
| 70 gfx::IntersectRects(inflated_invalidation, tile); | |
| 71 if (tile_invalidation.IsEmpty()) | |
| 72 continue; | |
| 73 PictureListMap::iterator find = picture_list_map_.find(iter.index()); | |
| 74 if (find == picture_list_map_.end()) | |
| 75 continue; | |
| 76 PictureList& pic_list = find->second; | |
| 77 // Leave empty pic_lists empty in case there are multiple invalidations. | |
| 78 if (!pic_list.empty()) | |
| 79 InvalidateRect(pic_list, tile_invalidation); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 // Walk through all pictures in the rect of interest and record. | |
| 84 for (TilingData::Iterator iter(&tiling_, interest_rect); iter; ++iter) { | |
| 85 // Create a picture in this list if it doesn't exist. | |
| 86 PictureList& pic_list = picture_list_map_[iter.index()]; | |
| 87 if (pic_list.empty()) { | |
| 88 // Inflate the base picture with a margin, similar to invalidations, so | |
| 89 // that when scaled down to at least min_contents_scale, the enclosed | |
| 90 // rect still includes content all the way to the edge of the layer. | |
| 91 gfx::Rect tile = tiling_.TileBounds(iter.index_x(), iter.index_y()); | |
| 92 tile.Inset( | |
| 93 -buffer_pixels(), | |
| 94 -buffer_pixels(), | |
| 95 -buffer_pixels(), | |
| 96 -buffer_pixels()); | |
| 97 scoped_refptr<Picture> base_picture = Picture::Create(tile); | |
| 98 pic_list.push_back(base_picture); | |
| 99 } | |
| 100 | |
| 101 for (PictureList::iterator pic = pic_list.begin(); | |
| 102 pic != pic_list.end(); ++pic) { | |
| 103 if (!(*pic)->HasRecording()) { | |
| 104 (*pic)->Record(painter, stats, tile_grid_info_); | |
| 105 (*pic)->CloneForDrawing(num_raster_threads_); | |
| 106 } | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 UpdateRecordedRegion(); | |
| 111 } | |
| 112 | |
| 113 class FullyContainedPredicate { | |
| 114 public: | |
| 115 FullyContainedPredicate(gfx::Rect rect) : layer_rect_(rect) {} | |
| 116 bool operator()(const scoped_refptr<Picture>& picture) { | |
| 117 return layer_rect_.Contains(picture->LayerRect()); | |
| 118 } | |
| 119 gfx::Rect layer_rect_; | |
| 120 }; | |
| 121 | |
| 122 void PicturePile::InvalidateRect( | |
| 123 PictureList& picture_list, | |
| 124 gfx::Rect invalidation) { | |
| 125 DCHECK(!picture_list.empty()); | |
| 126 | |
| 127 std::vector<PictureList::iterator> overlaps; | |
| 128 for (PictureList::iterator i = picture_list.begin(); | |
| 129 i != picture_list.end(); ++i) { | |
| 130 if ((*i)->LayerRect().Contains(invalidation) && !(*i)->HasRecording()) | |
| 131 return; | |
| 132 if ((*i)->LayerRect().Intersects(invalidation) && i != picture_list.begin()) | |
| 133 overlaps.push_back(i); | |
| 134 } | |
| 135 | |
| 136 gfx::Rect picture_rect = invalidation; | |
| 137 if (overlaps.size() >= kMaxOverlapping) { | |
| 138 for (size_t j = 0; j < overlaps.size(); j++) | |
| 139 picture_rect.Union((*overlaps[j])->LayerRect()); | |
| 140 } | |
| 141 | |
| 142 Picture* base_picture = picture_list.front(); | |
| 143 int max_pixels = kResetThreshold * base_picture->LayerRect().size().GetArea(); | |
| 144 if (picture_rect.size().GetArea() > max_pixels) { | |
| 145 // This picture list will be entirely recreated, so clear it. | |
| 146 picture_list.clear(); | |
| 147 return; | |
| 148 } | |
| 149 | |
| 150 FullyContainedPredicate pred(picture_rect); | |
| 151 picture_list.erase(std::remove_if(picture_list.begin(), | |
| 152 picture_list.end(), | |
| 153 pred), | |
| 154 picture_list.end()); | |
| 155 picture_list.push_back(Picture::Create(picture_rect)); | |
| 156 } | |
| 157 | |
| 158 } // namespace cc | |
| OLD | NEW |