| 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 "base/containers/adapters.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "cc/base/math_util.h" | 14 #include "cc/base/math_util.h" |
| 15 #include "cc/playback/display_item_list.h" | 15 #include "cc/playback/display_item_list.h" |
| 16 #include "third_party/skia/include/utils/SkNWayCanvas.h" | 16 #include "skia/ext/cdl_no_draw_canvas.h" |
| 17 #include "skia/ext/cdl_paint.h" |
| 18 #include "third_party/skia/include/core/SkCanvas.h" |
| 17 #include "ui/gfx/geometry/rect_conversions.h" | 19 #include "ui/gfx/geometry/rect_conversions.h" |
| 18 #include "ui/gfx/skia_util.h" | 20 #include "ui/gfx/skia_util.h" |
| 19 | 21 |
| 20 namespace cc { | 22 namespace cc { |
| 21 | 23 |
| 22 SkRect MapRect(const SkMatrix& matrix, const SkRect& src) { | 24 SkRect MapRect(const SkMatrix& matrix, const SkRect& src) { |
| 23 SkRect dst; | 25 SkRect dst; |
| 24 matrix.mapRect(&dst, src); | 26 matrix.mapRect(&dst, src); |
| 25 return dst; | 27 return dst; |
| 26 } | 28 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 49 max_size.width() - bounds_rect.x())); | 51 max_size.width() - bounds_rect.x())); |
| 50 bounds_rect.set_height(std::min(bounds_rect.height() + y_offset_if_negative, | 52 bounds_rect.set_height(std::min(bounds_rect.height() + y_offset_if_negative, |
| 51 max_size.height() - bounds_rect.y())); | 53 max_size.height() - bounds_rect.y())); |
| 52 return gfx::ToEnclosingRect(bounds_rect); | 54 return gfx::ToEnclosingRect(bounds_rect); |
| 53 } | 55 } |
| 54 | 56 |
| 55 namespace { | 57 namespace { |
| 56 | 58 |
| 57 // We're using an NWay canvas with no added canvases, so in effect | 59 // We're using an NWay canvas with no added canvases, so in effect |
| 58 // non-overridden functions are no-ops. | 60 // non-overridden functions are no-ops. |
| 59 class DiscardableImagesMetadataCanvas : public SkNWayCanvas { | 61 class DiscardableImagesMetadataCanvas : public CdlNoDrawCanvas { |
| 60 public: | 62 public: |
| 61 DiscardableImagesMetadataCanvas( | 63 DiscardableImagesMetadataCanvas( |
| 62 int width, | 64 int width, |
| 63 int height, | 65 int height, |
| 64 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set) | 66 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set) |
| 65 : SkNWayCanvas(width, height), | 67 : CdlNoDrawCanvas(width, height), |
| 66 image_set_(image_set), | 68 image_set_(image_set), |
| 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 CdlNoDrawCanvas, which will ignore |
| 72 // it. | 74 // onDrawPicture(). |
| 73 void onDrawPicture(const SkPicture* picture, | 75 void onDrawPicture(const CdlPicture* picture, |
| 74 const SkMatrix* matrix, | 76 const SkMatrix* matrix, |
| 75 const SkPaint* paint) override { | 77 const CdlPaint* paint) override { |
| 76 SkCanvas::onDrawPicture(picture, matrix, paint); | 78 CdlCanvas::onDrawPicture(picture, matrix, paint); |
| 77 } | 79 } |
| 78 | 80 |
| 79 void onDrawImage(const SkImage* image, | 81 void onDrawImage(const SkImage* image, |
| 80 SkScalar x, | 82 SkScalar x, |
| 81 SkScalar y, | 83 SkScalar y, |
| 82 const SkPaint* paint) override { | 84 const CdlPaint* paint) override { |
| 83 const SkMatrix& ctm = getTotalMatrix(); | 85 const SkMatrix& ctm = getTotalMatrix(); |
| 84 AddImage( | 86 AddImage( |
| 85 sk_ref_sp(image), SkRect::MakeIWH(image->width(), image->height()), | 87 sk_ref_sp(image), SkRect::MakeIWH(image->width(), image->height()), |
| 86 MapRect(ctm, SkRect::MakeXYWH(x, y, image->width(), image->height())), | 88 MapRect(ctm, SkRect::MakeXYWH(x, y, image->width(), image->height())), |
| 87 ctm, paint); | 89 ctm, paint); |
| 88 } | 90 } |
| 89 | 91 |
| 90 void onDrawImageRect(const SkImage* image, | 92 void onDrawImageRect(const SkImage* image, |
| 91 const SkRect* src, | 93 const SkRect* src, |
| 92 const SkRect& dst, | 94 const SkRect& dst, |
| 93 const SkPaint* paint, | 95 const CdlPaint* paint, |
| 94 SrcRectConstraint) override { | 96 SkCanvas::SrcRectConstraint) override { |
| 95 const SkMatrix& ctm = getTotalMatrix(); | 97 const SkMatrix& ctm = getTotalMatrix(); |
| 96 SkRect src_storage; | 98 SkRect src_storage; |
| 97 if (!src) { | 99 if (!src) { |
| 98 src_storage = SkRect::MakeIWH(image->width(), image->height()); | 100 src_storage = SkRect::MakeIWH(image->width(), image->height()); |
| 99 src = &src_storage; | 101 src = &src_storage; |
| 100 } | 102 } |
| 101 SkMatrix matrix; | 103 SkMatrix matrix; |
| 102 matrix.setRectToRect(*src, dst, SkMatrix::kFill_ScaleToFit); | 104 matrix.setRectToRect(*src, dst, SkMatrix::kFill_ScaleToFit); |
| 103 matrix.postConcat(ctm); | 105 matrix.postConcat(ctm); |
| 104 AddImage(sk_ref_sp(image), *src, MapRect(ctm, dst), matrix, paint); | 106 AddImage(sk_ref_sp(image), *src, MapRect(ctm, dst), matrix, paint); |
| 105 } | 107 } |
| 106 | 108 |
| 107 void onDrawImageNine(const SkImage* image, | 109 #if CDL_ENABLED |
| 108 const SkIRect& center, | 110 int onSaveLayer(const SaveLayerRec& rec) override { |
| 109 const SkRect& dst, | 111 saved_paints_.push_back(*rec.fPaint); |
| 110 const SkPaint* paint) override { | 112 return CdlNoDrawCanvas::onSaveLayer(rec); |
| 111 // No cc embedder issues image nine calls. | |
| 112 NOTREACHED(); | |
| 113 } | 113 } |
| 114 | 114 |
| 115 int onSave() override { |
| 116 saved_paints_.push_back(CdlPaint()); |
| 117 return CdlNoDrawCanvas::onSave(); |
| 118 } |
| 119 |
| 120 void onRestore() override { |
| 121 DCHECK_GT(saved_paints_.size(), 0u); |
| 122 saved_paints_.pop_back(); |
| 123 CdlNoDrawCanvas::onRestore(); |
| 124 } |
| 125 #else |
| 115 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec& rec) override { | 126 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec& rec) override { |
| 116 saved_paints_.push_back(*rec.fPaint); | 127 saved_paints_.push_back(*rec.fPaint); |
| 117 return SkNWayCanvas::getSaveLayerStrategy(rec); | 128 return CdlNoDrawCanvas::getSaveLayerStrategy(rec); |
| 118 } | 129 } |
| 119 | 130 |
| 120 void willSave() override { | 131 void willSave() override { |
| 121 saved_paints_.push_back(SkPaint()); | 132 saved_paints_.push_back(SkPaint()); |
| 122 return SkNWayCanvas::willSave(); | 133 return CdlNoDrawCanvas::willSave(); |
| 123 } | 134 } |
| 124 | 135 |
| 125 void willRestore() override { | 136 void willRestore() override { |
| 126 DCHECK_GT(saved_paints_.size(), 0u); | 137 DCHECK_GT(saved_paints_.size(), 0u); |
| 127 saved_paints_.pop_back(); | 138 saved_paints_.pop_back(); |
| 128 SkNWayCanvas::willRestore(); | 139 CdlNoDrawCanvas::willRestore(); |
| 129 } | 140 } |
| 141 #endif |
| 130 | 142 |
| 131 private: | 143 private: |
| 132 bool ComputePaintBounds(const SkRect& rect, | 144 bool ComputePaintBounds(const SkRect& rect, |
| 133 const SkPaint* current_paint, | 145 const CdlPaint* current_paint, |
| 134 SkRect* paint_bounds) { | 146 SkRect* paint_bounds) { |
| 135 *paint_bounds = rect; | 147 *paint_bounds = rect; |
| 136 if (current_paint) { | 148 if (current_paint) { |
| 137 if (!current_paint->canComputeFastBounds()) | 149 if (!current_paint->canComputeFastBounds()) |
| 138 return false; | 150 return false; |
| 139 *paint_bounds = | 151 *paint_bounds = |
| 140 current_paint->computeFastBounds(*paint_bounds, paint_bounds); | 152 current_paint->computeFastBounds(*paint_bounds, paint_bounds); |
| 141 } | 153 } |
| 142 | 154 |
| 143 for (const auto& paint : base::Reversed(saved_paints_)) { | 155 for (const auto& paint : base::Reversed(saved_paints_)) { |
| 144 if (!paint.canComputeFastBounds()) | 156 if (!paint.canComputeFastBounds()) |
| 145 return false; | 157 return false; |
| 146 *paint_bounds = paint.computeFastBounds(*paint_bounds, paint_bounds); | 158 *paint_bounds = paint.computeFastBounds(*paint_bounds, paint_bounds); |
| 147 } | 159 } |
| 148 return true; | 160 return true; |
| 149 } | 161 } |
| 150 | 162 |
| 151 void AddImage(sk_sp<const SkImage> image, | 163 void AddImage(sk_sp<const SkImage> image, |
| 152 const SkRect& src_rect, | 164 const SkRect& src_rect, |
| 153 const SkRect& rect, | 165 const SkRect& rect, |
| 154 const SkMatrix& matrix, | 166 const SkMatrix& matrix, |
| 155 const SkPaint* paint) { | 167 const CdlPaint* paint) { |
| 156 if (!image->isLazyGenerated()) | 168 if (!image->isLazyGenerated()) |
| 157 return; | 169 return; |
| 158 | 170 |
| 159 SkRect paint_rect; | 171 SkRect paint_rect; |
| 160 bool computed_paint_bounds = ComputePaintBounds(rect, paint, &paint_rect); | 172 bool computed_paint_bounds = ComputePaintBounds(rect, paint, &paint_rect); |
| 161 if (!computed_paint_bounds) { | 173 if (!computed_paint_bounds) { |
| 162 // TODO(vmpstr): UMA this case. | 174 // TODO(vmpstr): UMA this case. |
| 163 paint_rect = canvas_bounds_; | 175 paint_rect = canvas_bounds_; |
| 164 } | 176 } |
| 165 | 177 |
| 166 if (!paint_rect.intersects(canvas_bounds_)) | 178 if (!paint_rect.intersects(canvas_bounds_)) |
| 167 return; | 179 return; |
| 168 | 180 |
| 169 SkFilterQuality filter_quality = kNone_SkFilterQuality; | 181 SkFilterQuality filter_quality = kNone_SkFilterQuality; |
| 170 if (paint) { | 182 if (paint) { |
| 171 filter_quality = paint->getFilterQuality(); | 183 filter_quality = paint->getFilterQuality(); |
| 172 } | 184 } |
| 173 | 185 |
| 174 SkIRect src_irect; | 186 SkIRect src_irect; |
| 175 src_rect.roundOut(&src_irect); | 187 src_rect.roundOut(&src_irect); |
| 176 image_set_->push_back(std::make_pair( | 188 image_set_->push_back(std::make_pair( |
| 177 DrawImage(std::move(image), src_irect, filter_quality, matrix), | 189 DrawImage(std::move(image), src_irect, filter_quality, matrix), |
| 178 SafeClampPaintRectToSize(paint_rect, canvas_size_))); | 190 SafeClampPaintRectToSize(paint_rect, canvas_size_))); |
| 179 } | 191 } |
| 180 | 192 |
| 181 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set_; | 193 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set_; |
| 182 const SkRect canvas_bounds_; | 194 const SkRect canvas_bounds_; |
| 183 const gfx::Size canvas_size_; | 195 const gfx::Size canvas_size_; |
| 184 std::vector<SkPaint> saved_paints_; | 196 std::vector<CdlPaint> saved_paints_; |
| 185 }; | 197 }; |
| 186 | 198 |
| 187 } // namespace | 199 } // namespace |
| 188 | 200 |
| 189 DiscardableImageMap::DiscardableImageMap() {} | 201 DiscardableImageMap::DiscardableImageMap() {} |
| 190 | 202 |
| 191 DiscardableImageMap::~DiscardableImageMap() {} | 203 DiscardableImageMap::~DiscardableImageMap() {} |
| 192 | 204 |
| 193 std::unique_ptr<SkCanvas> DiscardableImageMap::BeginGeneratingMetadata( | 205 std::unique_ptr<CdlCanvas> DiscardableImageMap::BeginGeneratingMetadata( |
| 194 const gfx::Size& bounds) { | 206 const gfx::Size& bounds) { |
| 195 DCHECK(all_images_.empty()); | 207 DCHECK(all_images_.empty()); |
| 208 // TODO(cdl): switch to using std::unique_ptrs? |
| 196 return base::MakeUnique<DiscardableImagesMetadataCanvas>( | 209 return base::MakeUnique<DiscardableImagesMetadataCanvas>( |
| 197 bounds.width(), bounds.height(), &all_images_); | 210 bounds.width(), bounds.height(), &all_images_); |
| 198 } | 211 } |
| 199 | 212 |
| 200 void DiscardableImageMap::EndGeneratingMetadata() { | 213 void DiscardableImageMap::EndGeneratingMetadata() { |
| 201 images_rtree_.Build(all_images_, | 214 images_rtree_.Build(all_images_, |
| 202 [](const std::pair<DrawImage, gfx::Rect>& image) { | 215 [](const std::pair<DrawImage, gfx::Rect>& image) { |
| 203 return image.second; | 216 return image.second; |
| 204 }); | 217 }); |
| 205 } | 218 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 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 |