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