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