| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/playback/discardable_image_map.h" | 5 #include "cc/playback/discardable_image_map.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 | 53 |
| 54 namespace { | 54 namespace { |
| 55 | 55 |
| 56 // We're using an NWay canvas with no added canvases, so in effect | 56 // We're using an NWay canvas with no added canvases, so in effect |
| 57 // non-overridden functions are no-ops. | 57 // non-overridden functions are no-ops. |
| 58 class DiscardableImagesMetadataCanvas : public SkNWayCanvas { | 58 class DiscardableImagesMetadataCanvas : public SkNWayCanvas { |
| 59 public: | 59 public: |
| 60 DiscardableImagesMetadataCanvas( | 60 DiscardableImagesMetadataCanvas( |
| 61 int width, | 61 int width, |
| 62 int height, | 62 int height, |
| 63 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set) | 63 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set, |
| 64 ImageToRegionMap* image_to_region) |
| 64 : SkNWayCanvas(width, height), | 65 : SkNWayCanvas(width, height), |
| 65 image_set_(image_set), | 66 image_set_(image_set), |
| 67 image_to_region_(image_to_region), |
| 66 canvas_bounds_(SkRect::MakeIWH(width, height)), | 68 canvas_bounds_(SkRect::MakeIWH(width, height)), |
| 67 canvas_size_(width, height) {} | 69 canvas_size_(width, height) {} |
| 68 | 70 |
| 69 protected: | 71 protected: |
| 70 // we need to "undo" the behavior of SkNWayCanvas, which will try to forward | 72 // we need to "undo" the behavior of SkNWayCanvas, which will try to forward |
| 71 // it. | 73 // it. |
| 72 void onDrawPicture(const SkPicture* picture, | 74 void onDrawPicture(const SkPicture* picture, |
| 73 const SkMatrix* matrix, | 75 const SkMatrix* matrix, |
| 74 const SkPaint* paint) override { | 76 const SkPaint* paint) override { |
| 75 SkCanvas::onDrawPicture(picture, matrix, paint); | 77 SkCanvas::onDrawPicture(picture, matrix, paint); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 if (!paint_rect.intersects(canvas_bounds_)) | 167 if (!paint_rect.intersects(canvas_bounds_)) |
| 166 return; | 168 return; |
| 167 | 169 |
| 168 SkFilterQuality filter_quality = kNone_SkFilterQuality; | 170 SkFilterQuality filter_quality = kNone_SkFilterQuality; |
| 169 if (paint) { | 171 if (paint) { |
| 170 filter_quality = paint->getFilterQuality(); | 172 filter_quality = paint->getFilterQuality(); |
| 171 } | 173 } |
| 172 | 174 |
| 173 SkIRect src_irect; | 175 SkIRect src_irect; |
| 174 src_rect.roundOut(&src_irect); | 176 src_rect.roundOut(&src_irect); |
| 177 gfx::Rect image_rect = SafeClampPaintRectToSize(paint_rect, canvas_size_); |
| 178 |
| 179 Region& region = (*image_to_region_)[image->uniqueID()]; |
| 180 region.Union(image_rect); |
| 181 |
| 175 image_set_->push_back(std::make_pair( | 182 image_set_->push_back(std::make_pair( |
| 176 DrawImage(std::move(image), src_irect, filter_quality, matrix), | 183 DrawImage(std::move(image), src_irect, filter_quality, matrix), |
| 177 SafeClampPaintRectToSize(paint_rect, canvas_size_))); | 184 image_rect)); |
| 178 } | 185 } |
| 179 | 186 |
| 180 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set_; | 187 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set_; |
| 188 ImageToRegionMap* image_to_region_; |
| 181 const SkRect canvas_bounds_; | 189 const SkRect canvas_bounds_; |
| 182 const gfx::Size canvas_size_; | 190 const gfx::Size canvas_size_; |
| 183 std::vector<SkPaint> saved_paints_; | 191 std::vector<SkPaint> saved_paints_; |
| 184 }; | 192 }; |
| 185 | 193 |
| 186 } // namespace | 194 } // namespace |
| 187 | 195 |
| 188 DiscardableImageMap::DiscardableImageMap() {} | 196 DiscardableImageMap::DiscardableImageMap() {} |
| 189 | 197 |
| 190 DiscardableImageMap::~DiscardableImageMap() {} | 198 DiscardableImageMap::~DiscardableImageMap() {} |
| 191 | 199 |
| 192 sk_sp<SkCanvas> DiscardableImageMap::BeginGeneratingMetadata( | 200 sk_sp<SkCanvas> DiscardableImageMap::BeginGeneratingMetadata( |
| 193 const gfx::Size& bounds) { | 201 const gfx::Size& bounds) { |
| 194 DCHECK(all_images_.empty()); | 202 DCHECK(all_images_.empty()); |
| 195 return sk_make_sp<DiscardableImagesMetadataCanvas>( | 203 return sk_make_sp<DiscardableImagesMetadataCanvas>( |
| 196 bounds.width(), bounds.height(), &all_images_); | 204 bounds.width(), bounds.height(), &all_images_, &image_to_region_); |
| 197 } | 205 } |
| 198 | 206 |
| 199 void DiscardableImageMap::EndGeneratingMetadata() { | 207 void DiscardableImageMap::EndGeneratingMetadata() { |
| 200 images_rtree_.Build(all_images_, | 208 images_rtree_.Build(all_images_, |
| 201 [](const std::pair<DrawImage, gfx::Rect>& image) { | 209 [](const std::pair<DrawImage, gfx::Rect>& image) { |
| 202 return image.second; | 210 return image.second; |
| 203 }); | 211 }); |
| 204 } | 212 } |
| 205 | 213 |
| 206 void DiscardableImageMap::GetDiscardableImagesInRect( | 214 void DiscardableImageMap::GetDiscardableImagesInRect( |
| 207 const gfx::Rect& rect, | 215 const gfx::Rect& rect, |
| 208 const gfx::SizeF& raster_scales, | 216 const gfx::SizeF& raster_scales, |
| 209 std::vector<DrawImage>* images) const { | 217 std::vector<DrawImage>* images) const { |
| 210 std::vector<size_t> indices; | 218 std::vector<size_t> indices; |
| 211 images_rtree_.Search(rect, &indices); | 219 images_rtree_.Search(rect, &indices); |
| 212 for (size_t index : indices) | 220 for (size_t index : indices) |
| 213 images->push_back(all_images_[index].first.ApplyScale(raster_scales)); | 221 images->push_back(all_images_[index].first.ApplyScale(raster_scales)); |
| 214 } | 222 } |
| 215 | 223 |
| 224 Region DiscardableImageMap::GetRegionForImage(const SkImage* image) const { |
| 225 const auto& it = image_to_region_.find(image->uniqueID()); |
| 226 return it == image_to_region_.end() ? Region() : it->second; |
| 227 } |
| 228 |
| 216 DiscardableImageMap::ScopedMetadataGenerator::ScopedMetadataGenerator( | 229 DiscardableImageMap::ScopedMetadataGenerator::ScopedMetadataGenerator( |
| 217 DiscardableImageMap* image_map, | 230 DiscardableImageMap* image_map, |
| 218 const gfx::Size& bounds) | 231 const gfx::Size& bounds) |
| 219 : image_map_(image_map), | 232 : image_map_(image_map), |
| 220 metadata_canvas_(image_map->BeginGeneratingMetadata(bounds)) {} | 233 metadata_canvas_(image_map->BeginGeneratingMetadata(bounds)) {} |
| 221 | 234 |
| 222 DiscardableImageMap::ScopedMetadataGenerator::~ScopedMetadataGenerator() { | 235 DiscardableImageMap::ScopedMetadataGenerator::~ScopedMetadataGenerator() { |
| 223 image_map_->EndGeneratingMetadata(); | 236 image_map_->EndGeneratingMetadata(); |
| 224 } | 237 } |
| 225 | 238 |
| 226 } // namespace cc | 239 } // namespace cc |
| OLD | NEW |