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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | cc/playback/discardable_image_map_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cc/playback/discardable_image_map.cc
diff --git a/cc/playback/discardable_image_map.cc b/cc/playback/discardable_image_map.cc
index 840de37a95686e3cd38a9d5066cf0b1646ff868a..822d49b6e4ca93097c12af58ed3548ad7e024007 100644
--- a/cc/playback/discardable_image_map.cc
+++ b/cc/playback/discardable_image_map.cc
@@ -24,6 +24,33 @@ SkRect MapRect(const SkMatrix& matrix, const SkRect& src) {
return dst;
}
+// Returns a rect clamped to |max_size|. Note that |paint_rect| should intersect
+// or be contained by a rect defined by (0, 0) and |max_size|.
+gfx::Rect SafeClampPaintRectToSize(const SkRect& paint_rect,
+ const gfx::Size& max_size) {
+ // bounds_rect.x() + bounds_rect.width() (aka bounds_rect.right()) might
+ // overflow integer bounds, so do custom intersect, since gfx::Rect::Intersect
+ // uses bounds_rect.right().
+ gfx::RectF bounds_rect = gfx::SkRectToRectF(paint_rect);
+ float x_offset_if_negative = bounds_rect.x() < 0.f ? bounds_rect.x() : 0.f;
+ float y_offset_if_negative = bounds_rect.y() < 0.f ? bounds_rect.y() : 0.f;
+ bounds_rect.set_x(std::max(0.f, bounds_rect.x()));
+ bounds_rect.set_y(std::max(0.f, bounds_rect.y()));
+
+ // Verify that the rects intersect or that bound_rect is contained by
+ // max_size.
+ DCHECK_GE(bounds_rect.width(), -x_offset_if_negative);
+ DCHECK_GE(bounds_rect.height(), -y_offset_if_negative);
+ DCHECK_GE(max_size.width(), bounds_rect.x());
+ DCHECK_GE(max_size.height(), bounds_rect.y());
+
+ bounds_rect.set_width(std::min(bounds_rect.width() + x_offset_if_negative,
+ max_size.width() - bounds_rect.x()));
+ bounds_rect.set_height(std::min(bounds_rect.height() + y_offset_if_negative,
+ max_size.height() - bounds_rect.y()));
+ return gfx::ToEnclosingRect(bounds_rect);
+}
+
namespace {
// We're using an NWay canvas with no added canvases, so in effect
@@ -36,7 +63,8 @@ class DiscardableImagesMetadataCanvas : public SkNWayCanvas {
std::vector<std::pair<DrawImage, gfx::Rect>>* image_set)
: SkNWayCanvas(width, height),
image_set_(image_set),
- canvas_bounds_(SkRect::MakeIWH(width, height)) {}
+ canvas_bounds_(SkRect::MakeIWH(width, height)),
+ canvas_size_(width, height) {}
protected:
// we need to "undo" the behavior of SkNWayCanvas, which will try to forward
@@ -146,11 +174,12 @@ class DiscardableImagesMetadataCanvas : public SkNWayCanvas {
src_rect.roundOut(&src_irect);
image_set_->push_back(std::make_pair(
DrawImage(std::move(image), src_irect, filter_quality, matrix),
- gfx::ToEnclosingRect(gfx::SkRectToRectF(paint_rect))));
+ SafeClampPaintRectToSize(paint_rect, canvas_size_)));
}
std::vector<std::pair<DrawImage, gfx::Rect>>* image_set_;
const SkRect canvas_bounds_;
+ const gfx::Size canvas_size_;
std::vector<SkPaint> saved_paints_;
};
« 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