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

Unified Diff: src/utils/SkCanvasStack.cpp

Issue 23865004: Add SkCanvasStack and update the Canvas utilities to use it. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: comments Created 7 years, 3 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
Index: src/utils/SkCanvasStack.cpp
diff --git a/src/utils/SkCanvasStack.cpp b/src/utils/SkCanvasStack.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..88e07cf51933a5010012cbefce774952cdf99281
--- /dev/null
+++ b/src/utils/SkCanvasStack.cpp
@@ -0,0 +1,108 @@
+
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+#include "SkCanvasStack.h"
+
+SkCanvasStack::SkCanvasStack(int width, int height)
+ : INHERITED(width, height) {}
+
+SkCanvasStack::~SkCanvasStack() {
+ this->removeAll();
+}
+
+void SkCanvasStack::pushCanvas(SkCanvas* canvas, const SkIPoint& origin) {
+ if (canvas) {
+ // compute the bounds of this canvas
+ const SkIRect canvasBounds = SkIRect::MakeSize(canvas->getDeviceSize());
+
+ // push the canvas onto the stack
+ this->INHERITED::addCanvas(canvas);
+
+ // push the canvas data onto the stack
+ CanvasData* data = &fCanvasData.push_back();
+ data->origin = origin;
+ data->requiredClip.setRect(canvasBounds);
+
+ // subtract this region from the canvas objects already on the stack.
+ // This ensures they do not draw into the space occupied by the layers
+ // above them.
+ for (int i = fList.count() - 1; i > 0; --i) {
+ SkIRect localBounds = canvasBounds;
+ localBounds.offset(origin - fCanvasData[i-1].origin);
+
+ fCanvasData[i-1].requiredClip.op(localBounds, SkRegion::kDifference_Op);
+ fList[i-i]->clipRegion(fCanvasData[i-1].requiredClip);
bungeman-skia 2013/12/02 18:34:18 i-i
+ }
+ }
+ SkASSERT(fList.count() == fCanvasData.count());
+}
+
+void SkCanvasStack::removeAll() {
+ fCanvasData.reset();
+ this->INHERITED::removeAll();
+}
+
+/**
+ * Traverse all canvases (e.g. layers) the stack and ensure that they are clipped
+ * to their bounds and that the area covered by any canvas higher in the stack is
+ * also clipped out.
+ */
+void SkCanvasStack::clipToZOrderedBounds() {
+ SkASSERT(fList.count() == fCanvasData.count());
+ for (int i = 0; i < fList.count(); ++i) {
+ fList[i]->clipRegion(fCanvasData[i].requiredClip, SkRegion::kIntersect_Op);
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+/**
+ * We need to handle setMatrix specially as it overwrites the matrix in each
+ * canvas unlike all other matrix operations (i.e. translate, scale, etc) which
+ * just pre-concatenate with the existing matrix.
+ */
+void SkCanvasStack::setMatrix(const SkMatrix& matrix) {
+ SkASSERT(fList.count() == fCanvasData.count());
+ for (int i = 0; i < fList.count(); ++i) {
+
+ SkMatrix tempMatrix = matrix;
+ tempMatrix.postTranslate(SkIntToScalar(-fCanvasData[i].origin.x()),
+ SkIntToScalar(-fCanvasData[i].origin.y()));
+ fList[i]->setMatrix(tempMatrix);
+ }
+ this->SkCanvas::setMatrix(matrix);
+}
+
+bool SkCanvasStack::clipRect(const SkRect& r, SkRegion::Op op, bool aa) {
+ bool result = this->INHERITED::clipRect(r, op, aa);
+ clipToZOrderedBounds();
reed1 2013/09/04 15:00:00 nit: this->clipToZOrderedBounds()
+ return result;
+}
+
+bool SkCanvasStack::clipRRect(const SkRRect& rr, SkRegion::Op op, bool aa) {
+ bool result = this->INHERITED::clipRRect(rr, op, aa);
+ clipToZOrderedBounds();
+ return result;
+}
+
+bool SkCanvasStack::clipPath(const SkPath& p, SkRegion::Op op, bool aa) {
+ bool result = this->INHERITED::clipPath(p, op, aa);
+ clipToZOrderedBounds();
+ return result;
+}
+
+bool SkCanvasStack::clipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
+ SkASSERT(fList.count() == fCanvasData.count());
reed1 2013/09/04 15:00:00 perhaps small comment here: // This loop replaces
+ for (int i = 0; i < fList.count(); ++i) {
+ SkRegion tempRegion;
+ deviceRgn.translate(-fCanvasData[i].origin.x(),
+ -fCanvasData[i].origin.y(), &tempRegion);
+ tempRegion.op(fCanvasData[i].requiredClip, SkRegion::kIntersect_Op);
+ fList[i]->clipRegion(tempRegion, op);
+ }
+ return this->SkCanvas::clipRegion(deviceRgn, op);
+}

Powered by Google App Engine
This is Rietveld 408576698