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

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
28 // or be contained by 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 float x_offset_if_negative = bounds_rect.x() < 0.f ? bounds_rect.x() : 0.f;
36 float y_offset_if_negative = bounds_rect.y() < 0.f ? bounds_rect.y() : 0.f;
37 bounds_rect.set_x(std::max(0.f, bounds_rect.x()));
38 bounds_rect.set_y(std::max(0.f, bounds_rect.y()));
39
40 // Verify that the rects intersect or that bound_rect is contained by
41 // max_size.
42 DCHECK_GE(bounds_rect.width(), -x_offset_if_negative);
43 DCHECK_GE(bounds_rect.height(), -y_offset_if_negative);
44 DCHECK_GE(max_size.width(), bounds_rect.x());
45 DCHECK_GE(max_size.height(), bounds_rect.y());
46
47 bounds_rect.set_width(std::min(bounds_rect.width() + x_offset_if_negative,
48 max_size.width() - bounds_rect.x()));
49 bounds_rect.set_height(std::min(bounds_rect.height() + y_offset_if_negative,
50 max_size.height() - bounds_rect.y()));
51 return gfx::ToEnclosingRect(bounds_rect);
52 }
53
27 namespace { 54 namespace {
28 55
29 // We're using an NWay canvas with no added canvases, so in effect 56 // We're using an NWay canvas with no added canvases, so in effect
30 // non-overridden functions are no-ops. 57 // non-overridden functions are no-ops.
31 class DiscardableImagesMetadataCanvas : public SkNWayCanvas { 58 class DiscardableImagesMetadataCanvas : public SkNWayCanvas {
32 public: 59 public:
33 DiscardableImagesMetadataCanvas( 60 DiscardableImagesMetadataCanvas(
34 int width, 61 int width,
35 int height, 62 int height,
36 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set) 63 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set)
37 : SkNWayCanvas(width, height), 64 : SkNWayCanvas(width, height),
38 image_set_(image_set), 65 image_set_(image_set),
39 canvas_bounds_(SkRect::MakeIWH(width, height)) {} 66 canvas_bounds_(SkRect::MakeIWH(width, height)),
67 canvas_size_(width, height) {}
40 68
41 protected: 69 protected:
42 // we need to "undo" the behavior of SkNWayCanvas, which will try to forward 70 // we need to "undo" the behavior of SkNWayCanvas, which will try to forward
43 // it. 71 // it.
44 void onDrawPicture(const SkPicture* picture, 72 void onDrawPicture(const SkPicture* picture,
45 const SkMatrix* matrix, 73 const SkMatrix* matrix,
46 const SkPaint* paint) override { 74 const SkPaint* paint) override {
47 SkCanvas::onDrawPicture(picture, matrix, paint); 75 SkCanvas::onDrawPicture(picture, matrix, paint);
48 } 76 }
49 77
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 167
140 SkFilterQuality filter_quality = kNone_SkFilterQuality; 168 SkFilterQuality filter_quality = kNone_SkFilterQuality;
141 if (paint) { 169 if (paint) {
142 filter_quality = paint->getFilterQuality(); 170 filter_quality = paint->getFilterQuality();
143 } 171 }
144 172
145 SkIRect src_irect; 173 SkIRect src_irect;
146 src_rect.roundOut(&src_irect); 174 src_rect.roundOut(&src_irect);
147 image_set_->push_back(std::make_pair( 175 image_set_->push_back(std::make_pair(
148 DrawImage(std::move(image), src_irect, filter_quality, matrix), 176 DrawImage(std::move(image), src_irect, filter_quality, matrix),
149 gfx::ToEnclosingRect(gfx::SkRectToRectF(paint_rect)))); 177 SafeClampPaintRectToSize(paint_rect, canvas_size_)));
150 } 178 }
151 179
152 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set_; 180 std::vector<std::pair<DrawImage, gfx::Rect>>* image_set_;
153 const SkRect canvas_bounds_; 181 const SkRect canvas_bounds_;
182 const gfx::Size canvas_size_;
154 std::vector<SkPaint> saved_paints_; 183 std::vector<SkPaint> saved_paints_;
155 }; 184 };
156 185
157 } // namespace 186 } // namespace
158 187
159 DiscardableImageMap::DiscardableImageMap() {} 188 DiscardableImageMap::DiscardableImageMap() {}
160 189
161 DiscardableImageMap::~DiscardableImageMap() {} 190 DiscardableImageMap::~DiscardableImageMap() {}
162 191
163 sk_sp<SkCanvas> DiscardableImageMap::BeginGeneratingMetadata( 192 sk_sp<SkCanvas> DiscardableImageMap::BeginGeneratingMetadata(
(...skipping 24 matching lines...) Expand all
188 DiscardableImageMap* image_map, 217 DiscardableImageMap* image_map,
189 const gfx::Size& bounds) 218 const gfx::Size& bounds)
190 : image_map_(image_map), 219 : image_map_(image_map),
191 metadata_canvas_(image_map->BeginGeneratingMetadata(bounds)) {} 220 metadata_canvas_(image_map->BeginGeneratingMetadata(bounds)) {}
192 221
193 DiscardableImageMap::ScopedMetadataGenerator::~ScopedMetadataGenerator() { 222 DiscardableImageMap::ScopedMetadataGenerator::~ScopedMetadataGenerator() {
194 image_map_->EndGeneratingMetadata(); 223 image_map_->EndGeneratingMetadata();
195 } 224 }
196 225
197 } // namespace cc 226 } // 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