blob: a5a483dac9c2f438dd5599475efffd66636a1304 [file] [log] [blame]
djsollen@google.com339e79f2013-09-04 17:16:00 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#include "SkCanvasStack.h"
8
9SkCanvasStack::SkCanvasStack(int width, int height)
10 : INHERITED(width, height) {}
11
12SkCanvasStack::~SkCanvasStack() {
13 this->removeAll();
14}
15
16void SkCanvasStack::pushCanvas(SkCanvas* canvas, const SkIPoint& origin) {
17 if (canvas) {
18 // compute the bounds of this canvas
19 const SkIRect canvasBounds = SkIRect::MakeSize(canvas->getDeviceSize());
20
21 // push the canvas onto the stack
22 this->INHERITED::addCanvas(canvas);
23
24 // push the canvas data onto the stack
25 CanvasData* data = &fCanvasData.push_back();
26 data->origin = origin;
27 data->requiredClip.setRect(canvasBounds);
28
29 // subtract this region from the canvas objects already on the stack.
30 // This ensures they do not draw into the space occupied by the layers
31 // above them.
32 for (int i = fList.count() - 1; i > 0; --i) {
33 SkIRect localBounds = canvasBounds;
34 localBounds.offset(origin - fCanvasData[i-1].origin);
35
36 fCanvasData[i-1].requiredClip.op(localBounds, SkRegion::kDifference_Op);
bungeman@google.coma089f032013-12-02 21:34:36 +000037 fList[i-1]->clipRegion(fCanvasData[i-1].requiredClip);
djsollen@google.com339e79f2013-09-04 17:16:00 +000038 }
39 }
40 SkASSERT(fList.count() == fCanvasData.count());
41}
42
43void SkCanvasStack::removeAll() {
44 fCanvasData.reset();
45 this->INHERITED::removeAll();
46}
47
48/**
49 * Traverse all canvases (e.g. layers) the stack and ensure that they are clipped
50 * to their bounds and that the area covered by any canvas higher in the stack is
51 * also clipped out.
52 */
53void SkCanvasStack::clipToZOrderedBounds() {
54 SkASSERT(fList.count() == fCanvasData.count());
55 for (int i = 0; i < fList.count(); ++i) {
56 fList[i]->clipRegion(fCanvasData[i].requiredClip, SkRegion::kIntersect_Op);
57 }
58}
59
60////////////////////////////////////////////////////////////////////////////////
61
62/**
63 * We need to handle setMatrix specially as it overwrites the matrix in each
64 * canvas unlike all other matrix operations (i.e. translate, scale, etc) which
65 * just pre-concatenate with the existing matrix.
66 */
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +000067void SkCanvasStack::didSetMatrix(const SkMatrix& matrix) {
djsollen@google.com339e79f2013-09-04 17:16:00 +000068 SkASSERT(fList.count() == fCanvasData.count());
69 for (int i = 0; i < fList.count(); ++i) {
70
71 SkMatrix tempMatrix = matrix;
72 tempMatrix.postTranslate(SkIntToScalar(-fCanvasData[i].origin.x()),
73 SkIntToScalar(-fCanvasData[i].origin.y()));
74 fList[i]->setMatrix(tempMatrix);
75 }
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +000076 this->SkCanvas::didSetMatrix(matrix);
djsollen@google.com339e79f2013-09-04 17:16:00 +000077}
78
robertphillips@google.com8f90a892014-02-28 18:19:39 +000079void SkCanvasStack::onClipRect(const SkRect& r, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
80 this->INHERITED::onClipRect(r, op, edgeStyle);
djsollen@google.com339e79f2013-09-04 17:16:00 +000081 this->clipToZOrderedBounds();
djsollen@google.com339e79f2013-09-04 17:16:00 +000082}
83
robertphillips@google.com8f90a892014-02-28 18:19:39 +000084void SkCanvasStack::onClipRRect(const SkRRect& rr, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
85 this->INHERITED::onClipRRect(rr, op, edgeStyle);
djsollen@google.com339e79f2013-09-04 17:16:00 +000086 this->clipToZOrderedBounds();
djsollen@google.com339e79f2013-09-04 17:16:00 +000087}
88
robertphillips@google.com8f90a892014-02-28 18:19:39 +000089void SkCanvasStack::onClipPath(const SkPath& p, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
90 this->INHERITED::onClipPath(p, op, edgeStyle);
djsollen@google.com339e79f2013-09-04 17:16:00 +000091 this->clipToZOrderedBounds();
djsollen@google.com339e79f2013-09-04 17:16:00 +000092}
93
robertphillips@google.com8f90a892014-02-28 18:19:39 +000094void SkCanvasStack::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
djsollen@google.com339e79f2013-09-04 17:16:00 +000095 SkASSERT(fList.count() == fCanvasData.count());
96 for (int i = 0; i < fList.count(); ++i) {
97 SkRegion tempRegion;
98 deviceRgn.translate(-fCanvasData[i].origin.x(),
99 -fCanvasData[i].origin.y(), &tempRegion);
100 tempRegion.op(fCanvasData[i].requiredClip, SkRegion::kIntersect_Op);
101 fList[i]->clipRegion(tempRegion, op);
102 }
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000103 this->SkCanvas::onClipRegion(deviceRgn, op);
djsollen@google.com339e79f2013-09-04 17:16:00 +0000104}