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