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