Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(459)

Side by Side Diff: cc/playback/discardable_image_map.cc

Issue 2229603002: cc: Do a safe intersect when gathering images. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | cc/playback/discardable_image_map_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "cc/base/math_util.h" 13 #include "cc/base/math_util.h"
14 #include "cc/playback/display_item_list.h" 14 #include "cc/playback/display_item_list.h"
15 #include "third_party/skia/include/utils/SkNWayCanvas.h" 15 #include "third_party/skia/include/utils/SkNWayCanvas.h"
16 #include "ui/gfx/geometry/rect_conversions.h" 16 #include "ui/gfx/geometry/rect_conversions.h"
17 #include "ui/gfx/skia_util.h" 17 #include "ui/gfx/skia_util.h"
18 18
19 namespace cc { 19 namespace cc {
20 20
21 SkRect MapRect(const SkMatrix& matrix, const SkRect& src) { 21 SkRect MapRect(const SkMatrix& matrix, const SkRect& src) {
22 SkRect dst; 22 SkRect dst;
23 matrix.mapRect(&dst, src); 23 matrix.mapRect(&dst, src);
24 return dst; 24 return dst;
25 } 25 }
26 26
27 // Returns a rect clamped to |max_size|. Note that |paint_rect| should intersect
enne (OOO) 2016/08/09 18:23:42 Can you DCHECK this too just for sanity's sake? A
28 // a rect defined by (0, 0) and |max_size|.
29 gfx::Rect SafeClampPaintRectToSize(const SkRect& paint_rect,
30 const gfx::Size& max_size) {
31 // bounds_rect.x() + bounds_rect.width() (aka bounds_rect.right()) might
32 // overflow integer bounds, so do custom intersect, since gfx::Rect::Intersect
33 // uses bounds_rect.right().
34 gfx::RectF bounds_rect = gfx::SkRectToRectF(paint_rect);
35 bounds_rect.set_x(std::max(0.f, bounds_rect.x()));
36 bounds_rect.set_y(std::max(0.f, bounds_rect.y()));
37 bounds_rect.set_width(
38 std::min(bounds_rect.width(), max_size.width() - bounds_rect.x()));
39 bounds_rect.set_height(
40 std::min(bounds_rect.height(), max_size.height() - bounds_rect.y()));
41 return gfx::ToEnclosingRect(bounds_rect);
42 }
43
27 namespace { 44 namespace {
28 45
29 // We're using an NWay canvas with no added canvases, so in effect 46 // We're using an NWay canvas with no added canvases, so in effect
30 // non-overridden functions are no-ops. 47 // non-overridden functions are no-ops.
31 class DiscardableImagesMetadataCanvas : public SkNWayCanvas { 48 class DiscardableImagesMetadataCanvas : public SkNWayCanvas {
32 public: 49 public:
33 DiscardableImagesMetadataCanvas( 50 DiscardableImagesMetadataCanvas(
34 int width, 51 int width,
35 int height, 52 int height,
36 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set) 53 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set)
37 : SkNWayCanvas(width, height), 54 : SkNWayCanvas(width, height),
38 image_set_(image_set), 55 image_set_(image_set),
39 canvas_bounds_(SkRect::MakeIWH(width, height)) {} 56 canvas_bounds_(SkRect::MakeIWH(width, height)),
57 canvas_size_(width, height) {}
40 58
41 protected: 59 protected:
42 // we need to "undo" the behavior of SkNWayCanvas, which will try to forward 60 // we need to "undo" the behavior of SkNWayCanvas, which will try to forward
43 // it. 61 // it.
44 void onDrawPicture(const SkPicture* picture, 62 void onDrawPicture(const SkPicture* picture,
45 const SkMatrix* matrix, 63 const SkMatrix* matrix,
46 const SkPaint* paint) override { 64 const SkPaint* paint) override {
47 SkCanvas::onDrawPicture(picture, matrix, paint); 65 SkCanvas::onDrawPicture(picture, matrix, paint);
48 } 66 }
49 67
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 157
140 SkFilterQuality filter_quality = kNone_SkFilterQuality; 158 SkFilterQuality filter_quality = kNone_SkFilterQuality;
141 if (paint) { 159 if (paint) {
142 filter_quality = paint->getFilterQuality(); 160 filter_quality = paint->getFilterQuality();
143 } 161 }
144 162
145 SkIRect src_irect; 163 SkIRect src_irect;
146 src_rect.roundOut(&src_irect); 164 src_rect.roundOut(&src_irect);
147 image_set_->push_back(std::make_pair( 165 image_set_->push_back(std::make_pair(
148 DrawImage(std::move(image), src_irect, filter_quality, matrix), 166 DrawImage(std::move(image), src_irect, filter_quality, matrix),
149 gfx::ToEnclosingRect(gfx::SkRectToRectF(paint_rect)))); 167 SafeClampPaintRectToSize(paint_rect, canvas_size_)));
150 } 168 }
151 169
152 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set_; 170 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set_;
153 const SkRect canvas_bounds_; 171 const SkRect canvas_bounds_;
172 const gfx::Size canvas_size_;
154 std::vector<SkPaint> saved_paints_; 173 std::vector<SkPaint> saved_paints_;
155 }; 174 };
156 175
157 } // namespace 176 } // namespace
158 177
159 DiscardableImageMap::DiscardableImageMap() {} 178 DiscardableImageMap::DiscardableImageMap() {}
160 179
161 DiscardableImageMap::~DiscardableImageMap() {} 180 DiscardableImageMap::~DiscardableImageMap() {}
162 181
163 sk_sp<SkCanvas> DiscardableImageMap::BeginGeneratingMetadata( 182 sk_sp<SkCanvas> DiscardableImageMap::BeginGeneratingMetadata(
(...skipping 24 matching lines...) Expand all
188 DiscardableImageMap* image_map, 207 DiscardableImageMap* image_map,
189 const gfx::Size& bounds) 208 const gfx::Size& bounds)
190 : image_map_(image_map), 209 : image_map_(image_map),
191 metadata_canvas_(image_map->BeginGeneratingMetadata(bounds)) {} 210 metadata_canvas_(image_map->BeginGeneratingMetadata(bounds)) {}
192 211
193 DiscardableImageMap::ScopedMetadataGenerator::~ScopedMetadataGenerator() { 212 DiscardableImageMap::ScopedMetadataGenerator::~ScopedMetadataGenerator() {
194 image_map_->EndGeneratingMetadata(); 213 image_map_->EndGeneratingMetadata();
195 } 214 }
196 215
197 } // namespace cc 216 } // namespace cc
OLDNEW
« no previous file with comments | « no previous file | cc/playback/discardable_image_map_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698