Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / skia / ext / discardable_image_utils.cc
blobf447ff92cbcec652fb74aeaf4ad600f5be77d4bc
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
3 // found in the LICENSE file.
5 #include "skia/ext/discardable_image_utils.h"
7 #include <algorithm>
9 #include "third_party/skia/include/core/SkCanvas.h"
10 #include "third_party/skia/include/core/SkImage.h"
11 #include "third_party/skia/include/utils/SkNWayCanvas.h"
13 namespace skia {
15 namespace {
17 class DiscardableImageSet {
18 public:
19 DiscardableImageSet(std::vector<DiscardableImageUtils::PositionImage>* images)
20 : images_(images) {}
22 void Add(const SkImage* image,
23 const SkRect& rect,
24 const SkMatrix& matrix,
25 SkFilterQuality filter_quality) {
26 // We should only be saving discardable pixel refs.
27 SkASSERT(image->isLazyGenerated());
29 DiscardableImageUtils::PositionImage position_image;
30 position_image.image = image;
31 position_image.image_rect = rect;
32 position_image.matrix = matrix;
33 position_image.filter_quality = filter_quality;
34 images_->push_back(position_image);
37 private:
38 std::vector<DiscardableImageUtils::PositionImage>* images_;
41 SkRect MapRect(const SkMatrix& matrix, const SkRect& src) {
42 SkRect dst;
43 matrix.mapRect(&dst, src);
44 return dst;
47 class GatherDiscardableImageCanvas : public SkNWayCanvas {
48 public:
49 GatherDiscardableImageCanvas(int width,
50 int height,
51 DiscardableImageSet* image_set)
52 : SkNWayCanvas(width, height),
53 image_set_(image_set),
54 canvas_bounds_(SkRect::MakeIWH(width, height)) {}
56 protected:
57 // we need to "undo" the behavio of SkNWayCanvas, which will try to forward
58 // it.
59 void onDrawPicture(const SkPicture* picture,
60 const SkMatrix* matrix,
61 const SkPaint* paint) override {
62 SkCanvas::onDrawPicture(picture, matrix, paint);
65 void onDrawImage(const SkImage* image,
66 SkScalar x,
67 SkScalar y,
68 const SkPaint* paint) override {
69 const SkMatrix& ctm = this->getTotalMatrix();
70 AddImage(image, MapRect(ctm, SkRect::MakeXYWH(x, y, image->width(),
71 image->height())),
72 ctm, paint);
75 void onDrawImageRect(const SkImage* image,
76 const SkRect* src,
77 const SkRect& dst,
78 const SkPaint* paint,
79 SrcRectConstraint) override {
80 const SkMatrix& ctm = this->getTotalMatrix();
81 SkRect src_storage;
82 if (!src) {
83 src_storage = SkRect::MakeIWH(image->width(), image->height());
84 src = &src_storage;
86 SkMatrix matrix;
87 matrix.setRectToRect(*src, dst, SkMatrix::kFill_ScaleToFit);
88 matrix.postConcat(ctm);
89 AddImage(image, MapRect(ctm, dst), matrix, paint);
92 void onDrawImageNine(const SkImage* image,
93 const SkIRect& center,
94 const SkRect& dst,
95 const SkPaint* paint) override {
96 AddImage(image, dst, this->getTotalMatrix(), paint);
99 private:
100 DiscardableImageSet* image_set_;
101 const SkRect canvas_bounds_;
103 void AddImage(const SkImage* image,
104 const SkRect& rect,
105 const SkMatrix& matrix,
106 const SkPaint* paint) {
107 if (rect.intersects(canvas_bounds_) && image->isLazyGenerated()) {
108 SkFilterQuality filter_quality = kNone_SkFilterQuality;
109 if (paint) {
110 filter_quality = paint->getFilterQuality();
112 image_set_->Add(image, rect, matrix, filter_quality);
117 } // namespace
119 void DiscardableImageUtils::GatherDiscardableImages(
120 SkPicture* picture,
121 std::vector<PositionImage>* images) {
122 images->clear();
123 DiscardableImageSet image_set(images);
125 SkIRect picture_ibounds = picture->cullRect().roundOut();
126 // Use right/bottom as the size so that we don't need a translate and, as a
127 // result, the information is returned relative to the picture's origin.
128 GatherDiscardableImageCanvas canvas(picture_ibounds.right(),
129 picture_ibounds.bottom(), &image_set);
131 canvas.drawPicture(picture);
134 } // namespace skia