| 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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 | 55 |
| 56 namespace { | 56 namespace { |
| 57 | 57 |
| 58 // We're using an NWay canvas with no added canvases, so in effect | 58 // We're using an NWay canvas with no added canvases, so in effect |
| 59 // non-overridden functions are no-ops. | 59 // non-overridden functions are no-ops. |
| 60 class DiscardableImagesMetadataCanvas : public SkNWayCanvas { | 60 class DiscardableImagesMetadataCanvas : public SkNWayCanvas { |
| 61 public: | 61 public: |
| 62 DiscardableImagesMetadataCanvas( | 62 DiscardableImagesMetadataCanvas( |
| 63 int width, | 63 int width, |
| 64 int height, | 64 int height, |
| 65 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set) | 65 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set, |
| 66 std::unordered_map<ImageId, gfx::Rect>* image_id_to_rect) |
| 66 : SkNWayCanvas(width, height), | 67 : SkNWayCanvas(width, height), |
| 67 image_set_(image_set), | 68 image_set_(image_set), |
| 69 image_id_to_rect_(image_id_to_rect), |
| 68 canvas_bounds_(SkRect::MakeIWH(width, height)), | 70 canvas_bounds_(SkRect::MakeIWH(width, height)), |
| 69 canvas_size_(width, height) {} | 71 canvas_size_(width, height) {} |
| 70 | 72 |
| 71 protected: | 73 protected: |
| 72 // we need to "undo" the behavior of SkNWayCanvas, which will try to forward | 74 // we need to "undo" the behavior of SkNWayCanvas, which will try to forward |
| 73 // it. | 75 // it. |
| 74 void onDrawPicture(const SkPicture* picture, | 76 void onDrawPicture(const SkPicture* picture, |
| 75 const SkMatrix* matrix, | 77 const SkMatrix* matrix, |
| 76 const SkPaint* paint) override { | 78 const SkPaint* paint) override { |
| 77 SkCanvas::onDrawPicture(picture, matrix, paint); | 79 SkCanvas::onDrawPicture(picture, matrix, paint); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 if (!paint_rect.intersects(canvas_bounds_)) | 193 if (!paint_rect.intersects(canvas_bounds_)) |
| 192 return; | 194 return; |
| 193 | 195 |
| 194 SkFilterQuality filter_quality = kNone_SkFilterQuality; | 196 SkFilterQuality filter_quality = kNone_SkFilterQuality; |
| 195 if (paint) { | 197 if (paint) { |
| 196 filter_quality = paint->getFilterQuality(); | 198 filter_quality = paint->getFilterQuality(); |
| 197 } | 199 } |
| 198 | 200 |
| 199 SkIRect src_irect; | 201 SkIRect src_irect; |
| 200 src_rect.roundOut(&src_irect); | 202 src_rect.roundOut(&src_irect); |
| 203 gfx::Rect image_rect = SafeClampPaintRectToSize(paint_rect, canvas_size_); |
| 204 |
| 205 (*image_id_to_rect_)[image->uniqueID()].Union(image_rect); |
| 201 image_set_->push_back(std::make_pair( | 206 image_set_->push_back(std::make_pair( |
| 202 DrawImage(std::move(image), src_irect, filter_quality, matrix), | 207 DrawImage(std::move(image), src_irect, filter_quality, matrix), |
| 203 SafeClampPaintRectToSize(paint_rect, canvas_size_))); | 208 image_rect)); |
| 204 } | 209 } |
| 205 | 210 |
| 206 // Currently this function only handles extracting images from SkImageShaders | 211 // Currently this function only handles extracting images from SkImageShaders |
| 207 // embedded in SkPaints. Other embedded image cases, such as SkPictures, | 212 // embedded in SkPaints. Other embedded image cases, such as SkPictures, |
| 208 // are not yet handled. | 213 // are not yet handled. |
| 209 void AddPaintImage(const SkRect& rect, const SkPaint& paint) { | 214 void AddPaintImage(const SkRect& rect, const SkPaint& paint) { |
| 210 SkShader* shader = paint.getShader(); | 215 SkShader* shader = paint.getShader(); |
| 211 if (shader) { | 216 if (shader) { |
| 212 SkMatrix matrix; | 217 SkMatrix matrix; |
| 213 SkShader::TileMode xy[2]; | 218 SkShader::TileMode xy[2]; |
| 214 SkImage* image = shader->isAImage(&matrix, xy); | 219 SkImage* image = shader->isAImage(&matrix, xy); |
| 215 if (image) { | 220 if (image) { |
| 216 const SkMatrix& ctm = getTotalMatrix(); | 221 const SkMatrix& ctm = getTotalMatrix(); |
| 217 matrix.postConcat(ctm); | 222 matrix.postConcat(ctm); |
| 218 // TODO(ericrk): Handle cases where we only need a sub-rect from the | 223 // TODO(ericrk): Handle cases where we only need a sub-rect from the |
| 219 // image. crbug.com/671821 | 224 // image. crbug.com/671821 |
| 220 AddImage(sk_ref_sp(image), SkRect::MakeFromIRect(image->bounds()), | 225 AddImage(sk_ref_sp(image), SkRect::MakeFromIRect(image->bounds()), |
| 221 MapRect(ctm, rect), matrix, &paint); | 226 MapRect(ctm, rect), matrix, &paint); |
| 222 } | 227 } |
| 223 } | 228 } |
| 224 } | 229 } |
| 225 | 230 |
| 226 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set_; | 231 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set_; |
| 232 std::unordered_map<ImageId, gfx::Rect>* image_id_to_rect_; |
| 227 const SkRect canvas_bounds_; | 233 const SkRect canvas_bounds_; |
| 228 const gfx::Size canvas_size_; | 234 const gfx::Size canvas_size_; |
| 229 std::vector<SkPaint> saved_paints_; | 235 std::vector<SkPaint> saved_paints_; |
| 230 }; | 236 }; |
| 231 | 237 |
| 232 } // namespace | 238 } // namespace |
| 233 | 239 |
| 234 DiscardableImageMap::DiscardableImageMap() {} | 240 DiscardableImageMap::DiscardableImageMap() {} |
| 235 | 241 |
| 236 DiscardableImageMap::~DiscardableImageMap() {} | 242 DiscardableImageMap::~DiscardableImageMap() {} |
| 237 | 243 |
| 238 std::unique_ptr<SkCanvas> DiscardableImageMap::BeginGeneratingMetadata( | 244 std::unique_ptr<SkCanvas> DiscardableImageMap::BeginGeneratingMetadata( |
| 239 const gfx::Size& bounds) { | 245 const gfx::Size& bounds) { |
| 240 DCHECK(all_images_.empty()); | 246 DCHECK(all_images_.empty()); |
| 241 return base::MakeUnique<DiscardableImagesMetadataCanvas>( | 247 return base::MakeUnique<DiscardableImagesMetadataCanvas>( |
| 242 bounds.width(), bounds.height(), &all_images_); | 248 bounds.width(), bounds.height(), &all_images_, &image_id_to_rect_); |
| 243 } | 249 } |
| 244 | 250 |
| 245 void DiscardableImageMap::EndGeneratingMetadata() { | 251 void DiscardableImageMap::EndGeneratingMetadata() { |
| 246 images_rtree_.Build(all_images_, | 252 images_rtree_.Build(all_images_, |
| 247 [](const std::pair<DrawImage, gfx::Rect>& image) { | 253 [](const std::pair<DrawImage, gfx::Rect>& image) { |
| 248 return image.second; | 254 return image.second; |
| 249 }); | 255 }); |
| 250 } | 256 } |
| 251 | 257 |
| 252 void DiscardableImageMap::GetDiscardableImagesInRect( | 258 void DiscardableImageMap::GetDiscardableImagesInRect( |
| 253 const gfx::Rect& rect, | 259 const gfx::Rect& rect, |
| 254 float contents_scale, | 260 float contents_scale, |
| 255 std::vector<DrawImage>* images) const { | 261 std::vector<DrawImage>* images) const { |
| 256 std::vector<size_t> indices; | 262 std::vector<size_t> indices; |
| 257 images_rtree_.Search(rect, &indices); | 263 images_rtree_.Search(rect, &indices); |
| 258 for (size_t index : indices) | 264 for (size_t index : indices) |
| 259 images->push_back(all_images_[index].first.ApplyScale(contents_scale)); | 265 images->push_back(all_images_[index].first.ApplyScale(contents_scale)); |
| 260 } | 266 } |
| 261 | 267 |
| 268 gfx::Rect DiscardableImageMap::GetRectForImage(ImageId image_id) const { |
| 269 const auto& it = image_id_to_rect_.find(image_id); |
| 270 return it == image_id_to_rect_.end() ? gfx::Rect() : it->second; |
| 271 } |
| 272 |
| 262 DiscardableImageMap::ScopedMetadataGenerator::ScopedMetadataGenerator( | 273 DiscardableImageMap::ScopedMetadataGenerator::ScopedMetadataGenerator( |
| 263 DiscardableImageMap* image_map, | 274 DiscardableImageMap* image_map, |
| 264 const gfx::Size& bounds) | 275 const gfx::Size& bounds) |
| 265 : image_map_(image_map), | 276 : image_map_(image_map), |
| 266 metadata_canvas_(image_map->BeginGeneratingMetadata(bounds)) {} | 277 metadata_canvas_(image_map->BeginGeneratingMetadata(bounds)) {} |
| 267 | 278 |
| 268 DiscardableImageMap::ScopedMetadataGenerator::~ScopedMetadataGenerator() { | 279 DiscardableImageMap::ScopedMetadataGenerator::~ScopedMetadataGenerator() { |
| 269 image_map_->EndGeneratingMetadata(); | 280 image_map_->EndGeneratingMetadata(); |
| 270 } | 281 } |
| 271 | 282 |
| 272 } // namespace cc | 283 } // namespace cc |
| OLD | NEW |