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 | */ |
Mike Reed | 121c2af | 2020-03-10 14:02:56 -0400 | [diff] [blame] | 7 | |
| 8 | #include "include/core/SkShader.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "src/utils/SkCanvasStack.h" |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 10 | |
| 11 | SkCanvasStack::SkCanvasStack(int width, int height) |
| 12 | : INHERITED(width, height) {} |
| 13 | |
| 14 | SkCanvasStack::~SkCanvasStack() { |
| 15 | this->removeAll(); |
| 16 | } |
| 17 | |
Mike Reed | 584ca89 | 2016-11-15 11:52:55 -0500 | [diff] [blame] | 18 | void SkCanvasStack::pushCanvas(std::unique_ptr<SkCanvas> canvas, const SkIPoint& origin) { |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 19 | if (canvas) { |
| 20 | // compute the bounds of this canvas |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 21 | const SkIRect canvasBounds = SkIRect::MakeSize(canvas->getBaseLayerSize()); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 22 | |
| 23 | // push the canvas onto the stack |
Mike Reed | 584ca89 | 2016-11-15 11:52:55 -0500 | [diff] [blame] | 24 | this->INHERITED::addCanvas(canvas.get()); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 25 | |
| 26 | // push the canvas data onto the stack |
| 27 | CanvasData* data = &fCanvasData.push_back(); |
| 28 | data->origin = origin; |
| 29 | data->requiredClip.setRect(canvasBounds); |
Mike Reed | 584ca89 | 2016-11-15 11:52:55 -0500 | [diff] [blame] | 30 | data->ownedCanvas = std::move(canvas); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 31 | |
| 32 | // subtract this region from the canvas objects already on the stack. |
| 33 | // This ensures they do not draw into the space occupied by the layers |
| 34 | // above them. |
| 35 | for (int i = fList.count() - 1; i > 0; --i) { |
| 36 | SkIRect localBounds = canvasBounds; |
| 37 | localBounds.offset(origin - fCanvasData[i-1].origin); |
| 38 | |
| 39 | fCanvasData[i-1].requiredClip.op(localBounds, SkRegion::kDifference_Op); |
bungeman@google.com | a089f03 | 2013-12-02 21:34:36 +0000 | [diff] [blame] | 40 | fList[i-1]->clipRegion(fCanvasData[i-1].requiredClip); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | SkASSERT(fList.count() == fCanvasData.count()); |
| 44 | } |
| 45 | |
| 46 | void SkCanvasStack::removeAll() { |
Mike Reed | 584ca89 | 2016-11-15 11:52:55 -0500 | [diff] [blame] | 47 | this->INHERITED::removeAll(); // call the baseclass *before* we actually delete the canvases |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 48 | fCanvasData.reset(); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Traverse all canvases (e.g. layers) the stack and ensure that they are clipped |
| 53 | * to their bounds and that the area covered by any canvas higher in the stack is |
| 54 | * also clipped out. |
| 55 | */ |
| 56 | void SkCanvasStack::clipToZOrderedBounds() { |
| 57 | SkASSERT(fList.count() == fCanvasData.count()); |
| 58 | for (int i = 0; i < fList.count(); ++i) { |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 59 | fList[i]->clipRegion(fCanvasData[i].requiredClip); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | //////////////////////////////////////////////////////////////////////////////// |
| 64 | |
| 65 | /** |
| 66 | * We need to handle setMatrix specially as it overwrites the matrix in each |
| 67 | * canvas unlike all other matrix operations (i.e. translate, scale, etc) which |
| 68 | * just pre-concatenate with the existing matrix. |
| 69 | */ |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 70 | void SkCanvasStack::didSetMatrix(const SkMatrix& matrix) { |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 71 | SkASSERT(fList.count() == fCanvasData.count()); |
| 72 | for (int i = 0; i < fList.count(); ++i) { |
| 73 | |
| 74 | SkMatrix tempMatrix = matrix; |
| 75 | tempMatrix.postTranslate(SkIntToScalar(-fCanvasData[i].origin.x()), |
| 76 | SkIntToScalar(-fCanvasData[i].origin.y())); |
| 77 | fList[i]->setMatrix(tempMatrix); |
| 78 | } |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 79 | this->SkCanvas::didSetMatrix(matrix); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 82 | void SkCanvasStack::onClipRect(const SkRect& r, SkClipOp op, ClipEdgeStyle edgeStyle) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 83 | this->INHERITED::onClipRect(r, op, edgeStyle); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 84 | this->clipToZOrderedBounds(); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 87 | void SkCanvasStack::onClipRRect(const SkRRect& rr, SkClipOp op, ClipEdgeStyle edgeStyle) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 88 | this->INHERITED::onClipRRect(rr, op, edgeStyle); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 89 | this->clipToZOrderedBounds(); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 92 | void SkCanvasStack::onClipPath(const SkPath& p, SkClipOp op, ClipEdgeStyle edgeStyle) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 93 | this->INHERITED::onClipPath(p, op, edgeStyle); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 94 | this->clipToZOrderedBounds(); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Mike Reed | 121c2af | 2020-03-10 14:02:56 -0400 | [diff] [blame] | 97 | void SkCanvasStack::onClipShader(sk_sp<SkShader> cs, SkClipOp op) { |
| 98 | this->INHERITED::onClipShader(std::move(cs), op); |
| 99 | // we don't change the "bounds" of the clip, so we don't need to update zorder |
| 100 | } |
| 101 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 102 | void SkCanvasStack::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) { |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 103 | SkASSERT(fList.count() == fCanvasData.count()); |
| 104 | for (int i = 0; i < fList.count(); ++i) { |
| 105 | SkRegion tempRegion; |
| 106 | deviceRgn.translate(-fCanvasData[i].origin.x(), |
| 107 | -fCanvasData[i].origin.y(), &tempRegion); |
| 108 | tempRegion.op(fCanvasData[i].requiredClip, SkRegion::kIntersect_Op); |
| 109 | fList[i]->clipRegion(tempRegion, op); |
| 110 | } |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 111 | this->SkCanvas::onClipRegion(deviceRgn, op); |
djsollen@google.com | 339e79f | 2013-09-04 17:16:00 +0000 | [diff] [blame] | 112 | } |