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
SkCanvasStack(int width,int height)9 SkCanvasStack::SkCanvasStack(int width, int height)
10 : INHERITED(width, height) {}
11
~SkCanvasStack()12 SkCanvasStack::~SkCanvasStack() {
13 this->removeAll();
14 }
15
pushCanvas(std::unique_ptr<SkCanvas> canvas,const SkIPoint & origin)16 void SkCanvasStack::pushCanvas(std::unique_ptr<SkCanvas> canvas, const SkIPoint& origin) {
17 if (canvas) {
18 // compute the bounds of this canvas
19 const SkIRect canvasBounds = SkIRect::MakeSize(canvas->getBaseLayerSize());
20
21 // push the canvas onto the stack
22 this->INHERITED::addCanvas(canvas.get());
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 data->ownedCanvas = std::move(canvas);
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);
38 fList[i-1]->clipRegion(fCanvasData[i-1].requiredClip);
39 }
40 }
41 SkASSERT(fList.count() == fCanvasData.count());
42 }
43
removeAll()44 void SkCanvasStack::removeAll() {
45 this->INHERITED::removeAll(); // call the baseclass *before* we actually delete the canvases
46 fCanvasData.reset();
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 */
clipToZOrderedBounds()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);
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 */
didSetMatrix(const SkMatrix & matrix)68 void SkCanvasStack::didSetMatrix(const SkMatrix& matrix) {
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 }
77 this->SkCanvas::didSetMatrix(matrix);
78 }
79
onClipRect(const SkRect & r,SkClipOp op,ClipEdgeStyle edgeStyle)80 void SkCanvasStack::onClipRect(const SkRect& r, SkClipOp op, ClipEdgeStyle edgeStyle) {
81 this->INHERITED::onClipRect(r, op, edgeStyle);
82 this->clipToZOrderedBounds();
83 }
84
onClipRRect(const SkRRect & rr,SkClipOp op,ClipEdgeStyle edgeStyle)85 void SkCanvasStack::onClipRRect(const SkRRect& rr, SkClipOp op, ClipEdgeStyle edgeStyle) {
86 this->INHERITED::onClipRRect(rr, op, edgeStyle);
87 this->clipToZOrderedBounds();
88 }
89
onClipPath(const SkPath & p,SkClipOp op,ClipEdgeStyle edgeStyle)90 void SkCanvasStack::onClipPath(const SkPath& p, SkClipOp op, ClipEdgeStyle edgeStyle) {
91 this->INHERITED::onClipPath(p, op, edgeStyle);
92 this->clipToZOrderedBounds();
93 }
94
onClipRegion(const SkRegion & deviceRgn,SkClipOp op)95 void SkCanvasStack::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {
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 }
104 this->SkCanvas::onClipRegion(deviceRgn, op);
105 }
106