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