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 <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
| 10 #include "base/containers/adapters.h" |
10 #include "cc/base/math_util.h" | 11 #include "cc/base/math_util.h" |
11 #include "cc/playback/display_item_list.h" | 12 #include "cc/playback/display_item_list.h" |
12 #include "third_party/skia/include/utils/SkNWayCanvas.h" | 13 #include "third_party/skia/include/utils/SkNWayCanvas.h" |
13 #include "ui/gfx/geometry/rect_conversions.h" | 14 #include "ui/gfx/geometry/rect_conversions.h" |
14 #include "ui/gfx/skia_util.h" | 15 #include "ui/gfx/skia_util.h" |
15 | 16 |
16 namespace cc { | 17 namespace cc { |
17 | 18 |
18 namespace { | |
19 | |
20 SkRect MapRect(const SkMatrix& matrix, const SkRect& src) { | 19 SkRect MapRect(const SkMatrix& matrix, const SkRect& src) { |
21 SkRect dst; | 20 SkRect dst; |
22 matrix.mapRect(&dst, src); | 21 matrix.mapRect(&dst, src); |
23 return dst; | 22 return dst; |
24 } | 23 } |
25 | 24 |
26 SkSize ExtractScale(const SkMatrix& matrix) { | 25 bool ExtractScale(const SkMatrix& matrix, SkSize* scale) { |
27 SkSize scale = SkSize::Make(matrix.getScaleX(), matrix.getScaleY()); | 26 *scale = SkSize::Make(matrix.getScaleX(), matrix.getScaleY()); |
28 if (matrix.getType() & SkMatrix::kAffine_Mask) { | 27 if (matrix.getType() & SkMatrix::kAffine_Mask) { |
29 if (!matrix.decomposeScale(&scale)) | 28 if (!matrix.decomposeScale(scale)) { |
30 scale.set(1, 1); | 29 scale->set(1, 1); |
| 30 return false; |
| 31 } |
31 } | 32 } |
32 return scale; | 33 return true; |
33 } | 34 } |
34 | 35 |
| 36 bool ComputeNewRectFromPaintBounds( |
| 37 const SkRect& rect, |
| 38 const SkPaint* paint, |
| 39 const std::vector<const SkPaint*>& saved_paints, |
| 40 SkRect* paint_bounds) { |
| 41 *paint_bounds = rect; |
| 42 if (paint) { |
| 43 if (!paint->canComputeFastBounds()) |
| 44 return false; |
| 45 *paint_bounds = paint->computeFastBounds(*paint_bounds, paint_bounds); |
| 46 } |
| 47 |
| 48 for (const auto* paint : base::Reversed(saved_paints)) { |
| 49 if (!paint) |
| 50 continue; |
| 51 if (!paint->canComputeFastBounds()) |
| 52 return false; |
| 53 *paint_bounds = paint->computeFastBounds(*paint_bounds, paint_bounds); |
| 54 } |
| 55 return true; |
| 56 } |
| 57 |
| 58 namespace { |
| 59 |
35 // We're using an NWay canvas with no added canvases, so in effect | 60 // We're using an NWay canvas with no added canvases, so in effect |
36 // non-overridden functions are no-ops. | 61 // non-overridden functions are no-ops. |
37 class DiscardableImagesMetadataCanvas : public SkNWayCanvas { | 62 class DiscardableImagesMetadataCanvas : public SkNWayCanvas { |
38 public: | 63 public: |
39 DiscardableImagesMetadataCanvas( | 64 DiscardableImagesMetadataCanvas( |
40 int width, | 65 int width, |
41 int height, | 66 int height, |
42 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set) | 67 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set) |
43 : SkNWayCanvas(width, height), | 68 : SkNWayCanvas(width, height), |
44 image_set_(image_set), | 69 image_set_(image_set), |
45 canvas_bounds_(SkRect::MakeIWH(width, height)) {} | 70 canvas_bounds_(SkRect::MakeIWH(width, height)) {} |
46 | 71 |
47 protected: | 72 protected: |
48 // 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 |
49 // it. | 74 // it. |
50 void onDrawPicture(const SkPicture* picture, | 75 void onDrawPicture(const SkPicture* picture, |
51 const SkMatrix* matrix, | 76 const SkMatrix* matrix, |
52 const SkPaint* paint) override { | 77 const SkPaint* paint) override { |
53 SkCanvas::onDrawPicture(picture, matrix, paint); | 78 SkCanvas::onDrawPicture(picture, matrix, paint); |
54 } | 79 } |
55 | 80 |
56 void onDrawImage(const SkImage* image, | 81 void onDrawImage(const SkImage* image, |
57 SkScalar x, | 82 SkScalar x, |
58 SkScalar y, | 83 SkScalar y, |
59 const SkPaint* paint) override { | 84 const SkPaint* paint) override { |
60 const SkMatrix& ctm = this->getTotalMatrix(); | 85 const SkMatrix& ctm = getTotalMatrix(); |
61 AddImage(image, MapRect(ctm, SkRect::MakeXYWH(x, y, image->width(), | 86 AddImage( |
62 image->height())), | 87 image, SkRect::MakeIWH(image->width(), image->height()), |
63 ctm, paint); | 88 MapRect(ctm, SkRect::MakeXYWH(x, y, image->width(), image->height())), |
| 89 ctm, paint); |
64 } | 90 } |
65 | 91 |
66 void onDrawImageRect(const SkImage* image, | 92 void onDrawImageRect(const SkImage* image, |
67 const SkRect* src, | 93 const SkRect* src, |
68 const SkRect& dst, | 94 const SkRect& dst, |
69 const SkPaint* paint, | 95 const SkPaint* paint, |
70 SrcRectConstraint) override { | 96 SrcRectConstraint) override { |
71 const SkMatrix& ctm = this->getTotalMatrix(); | 97 const SkMatrix& ctm = getTotalMatrix(); |
72 SkRect src_storage; | 98 SkRect src_storage; |
73 if (!src) { | 99 if (!src) { |
74 src_storage = SkRect::MakeIWH(image->width(), image->height()); | 100 src_storage = SkRect::MakeIWH(image->width(), image->height()); |
75 src = &src_storage; | 101 src = &src_storage; |
76 } | 102 } |
77 SkMatrix matrix; | 103 SkMatrix matrix; |
78 matrix.setRectToRect(*src, dst, SkMatrix::kFill_ScaleToFit); | 104 matrix.setRectToRect(*src, dst, SkMatrix::kFill_ScaleToFit); |
79 matrix.postConcat(ctm); | 105 matrix.postConcat(ctm); |
80 AddImage(image, MapRect(ctm, dst), matrix, paint); | 106 AddImage(image, *src, MapRect(ctm, dst), matrix, paint); |
81 } | 107 } |
82 | 108 |
83 void onDrawImageNine(const SkImage* image, | 109 void onDrawImageNine(const SkImage* image, |
84 const SkIRect& center, | 110 const SkIRect& center, |
85 const SkRect& dst, | 111 const SkRect& dst, |
86 const SkPaint* paint) override { | 112 const SkPaint* paint) override { |
87 AddImage(image, dst, this->getTotalMatrix(), paint); | 113 // TODO(vmpstr): This should be storing 9 different images here, probably to |
| 114 // allow for sub-image scaling. |
| 115 AddImage(image, SkRect::MakeIWH(image->width(), image->height()), dst, |
| 116 getTotalMatrix(), paint); |
| 117 } |
| 118 |
| 119 SaveLayerStrategy willSaveLayer(const SkRect* rect, |
| 120 const SkPaint* paint, |
| 121 SaveFlags save_flags) override { |
| 122 saved_paints_.push_back(paint); |
| 123 return SkNWayCanvas::willSaveLayer(rect, paint, save_flags); |
| 124 } |
| 125 |
| 126 void willSave() override { |
| 127 saved_paints_.push_back(nullptr); |
| 128 return SkNWayCanvas::willSave(); |
| 129 } |
| 130 |
| 131 void willRestore() override { |
| 132 DCHECK_GT(saved_paints_.size(), 0u); |
| 133 saved_paints_.pop_back(); |
| 134 SkNWayCanvas::willRestore(); |
88 } | 135 } |
89 | 136 |
90 private: | 137 private: |
91 void AddImage(const SkImage* image, | 138 void AddImage(const SkImage* image, |
| 139 const SkRect& src_rect, |
92 const SkRect& rect, | 140 const SkRect& rect, |
93 const SkMatrix& matrix, | 141 const SkMatrix& matrix, |
94 const SkPaint* paint) { | 142 const SkPaint* paint) { |
95 if (rect.intersects(canvas_bounds_) && image->isLazyGenerated()) { | 143 if (!image->isLazyGenerated()) |
96 SkFilterQuality filter_quality = kNone_SkFilterQuality; | 144 return; |
97 if (paint) { | 145 |
98 filter_quality = paint->getFilterQuality(); | 146 SkRect paint_rect; |
99 } | 147 bool computed_paint_bounds = ComputePaintBounds(rect, paint, &paint_rect); |
100 image_set_->push_back( | 148 if (!computed_paint_bounds) { |
101 std::make_pair(DrawImage(image, ExtractScale(matrix), filter_quality), | 149 // TODO(vmpstr): UMA this case. |
102 gfx::ToEnclosingRect(gfx::SkRectToRectF(rect)))); | 150 paint_rect = canvas_bounds_; |
103 } | 151 } |
| 152 |
| 153 if (!paint_rect.intersects(canvas_bounds_)) |
| 154 return; |
| 155 |
| 156 SkFilterQuality filter_quality = kNone_SkFilterQuality; |
| 157 if (paint) { |
| 158 filter_quality = paint->getFilterQuality(); |
| 159 } |
| 160 |
| 161 SkIRect src_irect; |
| 162 src_rect.roundOut(&src_irect); |
| 163 SkSize scale; |
| 164 bool is_decomposable = ExtractScale(matrix, &scale); |
| 165 image_set_->push_back( |
| 166 std::make_pair(DrawImage(image, src_irect, scale, filter_quality, |
| 167 matrix.hasPerspective(), is_decomposable), |
| 168 gfx::ToEnclosingRect(gfx::SkRectToRectF(paint_rect)))); |
| 169 } |
| 170 |
| 171 bool ComputePaintBounds(const SkRect& rect, |
| 172 const SkPaint* paint, |
| 173 SkRect* paint_bounds) { |
| 174 return ComputeNewRectFromPaintBounds(rect, paint, saved_paints_, |
| 175 paint_bounds); |
104 } | 176 } |
105 | 177 |
106 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set_; | 178 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set_; |
107 const SkRect canvas_bounds_; | 179 const SkRect canvas_bounds_; |
| 180 std::vector<const SkPaint*> saved_paints_; |
108 }; | 181 }; |
109 | 182 |
110 } // namespace | 183 } // namespace |
111 | 184 |
112 DiscardableImageMap::DiscardableImageMap() {} | 185 DiscardableImageMap::DiscardableImageMap() {} |
113 | 186 |
114 DiscardableImageMap::~DiscardableImageMap() {} | 187 DiscardableImageMap::~DiscardableImageMap() {} |
115 | 188 |
116 scoped_ptr<SkCanvas> DiscardableImageMap::BeginGeneratingMetadata( | 189 scoped_ptr<SkCanvas> DiscardableImageMap::BeginGeneratingMetadata( |
117 const gfx::Size& bounds) { | 190 const gfx::Size& bounds) { |
(...skipping 23 matching lines...) Expand all Loading... |
141 DiscardableImageMap* image_map, | 214 DiscardableImageMap* image_map, |
142 const gfx::Size& bounds) | 215 const gfx::Size& bounds) |
143 : image_map_(image_map), | 216 : image_map_(image_map), |
144 metadata_canvas_(image_map->BeginGeneratingMetadata(bounds)) {} | 217 metadata_canvas_(image_map->BeginGeneratingMetadata(bounds)) {} |
145 | 218 |
146 DiscardableImageMap::ScopedMetadataGenerator::~ScopedMetadataGenerator() { | 219 DiscardableImageMap::ScopedMetadataGenerator::~ScopedMetadataGenerator() { |
147 image_map_->EndGeneratingMetadata(); | 220 image_map_->EndGeneratingMetadata(); |
148 } | 221 } |
149 | 222 |
150 } // namespace cc | 223 } // namespace cc |
OLD | NEW |