djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 9 | SkCanvasStack::SkCanvasStack(int width, int height) |
| 10 | : INHERITED(width, height) {} |
| 11 | |
| 12 | SkCanvasStack::~SkCanvasStack() { |
| 13 | this->removeAll(); |
| 14 | } |
| 15 | |
| 16 | void 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.com | a089f03 | 2013-12-02 21:34:36 +0000 | [diff] [blame] | 37 | fList[i-1]->clipRegion(fCanvasData[i-1].requiredClip); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | SkASSERT(fList.count() == fCanvasData.count()); |
| 41 | } |
| 42 | |
| 43 | void 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 | */ |
| 53 | void 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.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 67 | void SkCanvasStack::didSetMatrix(const SkMatrix& matrix) { |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 68 | 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.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 76 | this->SkCanvas::didSetMatrix(matrix); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 77 | } |
| 78 | |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 79 | void SkCanvasStack::onClipRect(const SkRect& r, SkRegion::Op op, ClipEdgeStyle edgeStyle) { |
| 80 | this->INHERITED::onClipRect(r, op, edgeStyle); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 81 | this->clipToZOrderedBounds(); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 82 | } |
| 83 | |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 84 | void SkCanvasStack::onClipRRect(const SkRRect& rr, SkRegion::Op op, ClipEdgeStyle edgeStyle) { |
| 85 | this->INHERITED::onClipRRect(rr, op, edgeStyle); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 86 | this->clipToZOrderedBounds(); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 87 | } |
| 88 | |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 89 | void SkCanvasStack::onClipPath(const SkPath& p, SkRegion::Op op, ClipEdgeStyle edgeStyle) { |
| 90 | this->INHERITED::onClipPath(p, op, edgeStyle); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 91 | this->clipToZOrderedBounds(); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 92 | } |
| 93 | |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 94 | void SkCanvasStack::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) { |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 95 | 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.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 103 | this->SkCanvas::onClipRegion(deviceRgn, op); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 104 | } |