aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--gyp/utils.gyp2
-rw-r--r--include/utils/SkNWayCanvas.h9
-rw-r--r--src/utils/SkCanvasStack.cpp108
-rw-r--r--src/utils/SkCanvasStack.h52
-rw-r--r--src/utils/SkCanvasStateUtils.cpp9
-rw-r--r--tests/CanvasStateTest.cpp91
6 files changed, 262 insertions, 9 deletions
diff --git a/gyp/utils.gyp b/gyp/utils.gyp
index df12b79b97..d3bac16600 100644
--- a/gyp/utils.gyp
+++ b/gyp/utils.gyp
@@ -66,6 +66,8 @@
'../src/utils/SkBitSet.h',
'../src/utils/SkBoundaryPatch.cpp',
'../src/utils/SkCamera.cpp',
+ '../src/utils/SkCanvasStack.h',
+ '../src/utils/SkCanvasStack.cpp',
'../src/utils/SkCanvasStateUtils.cpp',
'../src/utils/SkCubicInterval.cpp',
'../src/utils/SkCullPoints.cpp',
diff --git a/include/utils/SkNWayCanvas.h b/include/utils/SkNWayCanvas.h
index 3bb2c97232..c48bcab3c4 100644
--- a/include/utils/SkNWayCanvas.h
+++ b/include/utils/SkNWayCanvas.h
@@ -16,9 +16,9 @@ public:
SkNWayCanvas(int width, int height);
virtual ~SkNWayCanvas();
- void addCanvas(SkCanvas*);
- void removeCanvas(SkCanvas*);
- void removeAll();
+ virtual void addCanvas(SkCanvas*);
+ virtual void removeCanvas(SkCanvas*);
+ virtual void removeAll();
///////////////////////////////////////////////////////////////////////////
// These are forwarded to the N canvases we're referencing
@@ -79,11 +79,12 @@ public:
virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE;
virtual void endCommentGroup() SK_OVERRIDE;
-private:
+protected:
SkTDArray<SkCanvas*> fList;
class Iter;
+private:
typedef SkCanvas INHERITED;
};
diff --git a/src/utils/SkCanvasStack.cpp b/src/utils/SkCanvasStack.cpp
new file mode 100644
index 0000000000..db5a8b2740
--- /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);
+ }
+ }
+ 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);
+ this->clipToZOrderedBounds();
+ return result;
+}
+
+bool SkCanvasStack::clipRRect(const SkRRect& rr, SkRegion::Op op, bool aa) {
+ bool result = this->INHERITED::clipRRect(rr, op, aa);
+ this->clipToZOrderedBounds();
+ return result;
+}
+
+bool SkCanvasStack::clipPath(const SkPath& p, SkRegion::Op op, bool aa) {
+ bool result = this->INHERITED::clipPath(p, op, aa);
+ this->clipToZOrderedBounds();
+ return result;
+}
+
+bool SkCanvasStack::clipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
+ SkASSERT(fList.count() == fCanvasData.count());
+ 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);
+}
diff --git a/src/utils/SkCanvasStack.h b/src/utils/SkCanvasStack.h
new file mode 100644
index 0000000000..2137d308b8
--- /dev/null
+++ b/src/utils/SkCanvasStack.h
@@ -0,0 +1,52 @@
+
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkCanvasStack_DEFINED
+#define SkCanvasStack_DEFINED
+
+#include "SkNWayCanvas.h"
+#include "SkTArray.h"
+
+class SkCanvasStack : public SkNWayCanvas {
+public:
+ SkCanvasStack(int width, int height);
+ virtual ~SkCanvasStack();
+
+ void pushCanvas(SkCanvas* canvas, const SkIPoint& origin);
+ virtual void removeAll() SK_OVERRIDE;
+
+ /*
+ * The following add/remove canvas methods are overrides from SkNWayCanvas
+ * that do not make sense in the context of our CanvasStack, but since we
+ * can share most of the other implementation of NWay we override those
+ * methods to be no-ops.
+ */
+ virtual void addCanvas(SkCanvas*) SK_OVERRIDE { SkASSERT(!"Invalid Op"); }
+ virtual void removeCanvas(SkCanvas*) SK_OVERRIDE { SkASSERT(!"Invalid Op"); }
+
+ virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
+ virtual bool clipRect(const SkRect&, SkRegion::Op, bool) SK_OVERRIDE;
+ virtual bool clipRRect(const SkRRect&, SkRegion::Op, bool) SK_OVERRIDE;
+ virtual bool clipPath(const SkPath&, SkRegion::Op, bool) SK_OVERRIDE;
+ virtual bool clipRegion(const SkRegion& deviceRgn,
+ SkRegion::Op) SK_OVERRIDE;
+
+private:
+ void clipToZOrderedBounds();
+
+ struct CanvasData {
+ SkIPoint origin;
+ SkRegion requiredClip;
+ };
+
+ SkTArray<CanvasData> fCanvasData;
+
+ typedef SkNWayCanvas INHERITED;
+};
+
+#endif
diff --git a/src/utils/SkCanvasStateUtils.cpp b/src/utils/SkCanvasStateUtils.cpp
index c4232c2f5b..cd79a44f50 100644
--- a/src/utils/SkCanvasStateUtils.cpp
+++ b/src/utils/SkCanvasStateUtils.cpp
@@ -9,7 +9,7 @@
#include "SkBitmapDevice.h"
#include "SkCanvas.h"
-#include "SkNWayCanvas.h"
+#include "SkCanvasStack.h"
#include "SkWriter32.h"
#define CANVAS_STATE_VERSION 1
@@ -316,18 +316,19 @@ SkCanvas* SkCanvasStateUtils::CreateFromCanvasState(const SkCanvasState* state)
return NULL;
}
- SkAutoTUnref<SkNWayCanvas> canvas(SkNEW_ARGS(SkNWayCanvas, (state->width, state->height)));
+ SkAutoTUnref<SkCanvasStack> canvas(SkNEW_ARGS(SkCanvasStack, (state->width, state->height)));
// setup the matrix and clip on the n-way canvas
setup_canvas_from_MC_state(state->mcState, canvas);
// Iterate over the layers and add them to the n-way canvas
- for (int i = 0; i < state->layerCount; ++i) {
+ for (int i = state->layerCount - 1; i >= 0; --i) {
SkAutoTUnref<SkCanvas> canvasLayer(create_canvas_from_canvas_layer(state->layers[i]));
if (!canvasLayer.get()) {
return NULL;
}
- canvas->addCanvas(canvasLayer.get());
+ canvas->pushCanvas(canvasLayer.get(), SkIPoint::Make(state->layers[i].x,
+ state->layers[i].y));
}
return canvas.detach();
diff --git a/tests/CanvasStateTest.cpp b/tests/CanvasStateTest.cpp
index c6756a732e..604c4948db 100644
--- a/tests/CanvasStateTest.cpp
+++ b/tests/CanvasStateTest.cpp
@@ -16,7 +16,6 @@
#include "SkRRect.h"
static void test_complex_layers(skiatest::Reporter* reporter) {
-
const int WIDTH = 400;
const int HEIGHT = 400;
const int SPACER = 10;
@@ -91,6 +90,95 @@ static void test_complex_layers(skiatest::Reporter* reporter) {
////////////////////////////////////////////////////////////////////////////////
+static void test_complex_clips(skiatest::Reporter* reporter) {
+
+ const int WIDTH = 400;
+ const int HEIGHT = 400;
+ const SkScalar SPACER = SkIntToScalar(10);
+
+ SkRect layerRect = SkRect::MakeWH(SkIntToScalar(WIDTH), SkIntToScalar(HEIGHT / 4));
+ layerRect.inset(2*SPACER, 2*SPACER);
+
+ SkRect clipRect = layerRect;
+ clipRect.fRight = clipRect.fLeft + (clipRect.width() / 2) - (2*SPACER);
+ clipRect.outset(SPACER, SPACER);
+
+ SkIRect regionBounds;
+ clipRect.roundIn(&regionBounds);
+ regionBounds.offset(clipRect.width() + (2*SPACER), 0);
+
+ SkIRect regionInterior = regionBounds;
+ regionInterior.inset(SPACER*3, SPACER*3);
+
+ SkRegion clipRegion;
+ clipRegion.setRect(regionBounds);
+ clipRegion.op(regionInterior, SkRegion::kDifference_Op);
+
+
+ const SkRegion::Op clipOps[] = { SkRegion::kIntersect_Op,
+ SkRegion::kIntersect_Op,
+ SkRegion::kReplace_Op,
+ };
+ const SkCanvas::SaveFlags flags[] = { SkCanvas::kARGB_NoClipLayer_SaveFlag,
+ SkCanvas::kARGB_ClipLayer_SaveFlag,
+ SkCanvas::kARGB_NoClipLayer_SaveFlag,
+ };
+ REPORTER_ASSERT(reporter, sizeof(clipOps) == sizeof(flags));
+ const int layerCombinations = sizeof(flags) / sizeof(SkCanvas::SaveFlags);
+
+ SkBitmap bitmaps[2];
+ for (int i = 0; i < 2; ++i) {
+ bitmaps[i].setConfig(SkBitmap::kARGB_8888_Config, WIDTH, HEIGHT);
+ bitmaps[i].allocPixels();
+
+ SkCanvas canvas(bitmaps[i]);
+
+ canvas.drawColor(SK_ColorRED);
+
+ SkRegion localRegion = clipRegion;
+
+ for (int j = 0; j < layerCombinations; ++j) {
+ canvas.saveLayerAlpha(&layerRect, 128, flags[j]);
+
+ SkCanvasState* state = NULL;
+ SkCanvas* tmpCanvas = NULL;
+ if (i) {
+ state = SkCanvasStateUtils::CaptureCanvasState(&canvas);
+ REPORTER_ASSERT(reporter, state);
+ tmpCanvas = SkCanvasStateUtils::CreateFromCanvasState(state);
+ REPORTER_ASSERT(reporter, tmpCanvas);
+ } else {
+ tmpCanvas = SkRef(&canvas);
+ }
+
+ tmpCanvas->save();
+ tmpCanvas->clipRect(clipRect, clipOps[j]);
+ tmpCanvas->drawColor(SK_ColorBLUE);
+ tmpCanvas->restore();
+
+ tmpCanvas->clipRegion(localRegion, clipOps[j]);
+ tmpCanvas->drawColor(SK_ColorBLUE);
+
+ tmpCanvas->unref();
+ SkCanvasStateUtils::ReleaseCanvasState(state);
+
+ canvas.restore();
+
+ // translate the canvas and region for the next iteration
+ canvas.translate(0, 2*(layerRect.height() + SPACER));
+ localRegion.translate(0, 2*(layerRect.height() + SPACER));
+ }
+ }
+
+ // now we memcmp the two bitmaps
+ REPORTER_ASSERT(reporter, bitmaps[0].getSize() == bitmaps[1].getSize());
+ REPORTER_ASSERT(reporter, !memcmp(bitmaps[0].getPixels(),
+ bitmaps[1].getPixels(),
+ bitmaps[0].getSize()));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
class TestDrawFilter : public SkDrawFilter {
public:
virtual bool filter(SkPaint*, Type) SK_OVERRIDE { return true; }
@@ -134,6 +222,7 @@ static void test_soft_clips(skiatest::Reporter* reporter) {
static void test_canvas_state_utils(skiatest::Reporter* reporter) {
test_complex_layers(reporter);
+ test_complex_clips(reporter);
test_draw_filters(reporter);
test_soft_clips(reporter);
}