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 "include/core/SkShader.h"
9 #include "src/utils/SkCanvasStack.h"
10 
SkCanvasStack(int width,int height)11 SkCanvasStack::SkCanvasStack(int width, int height)
12         : INHERITED(width, height) {}
13 
~SkCanvasStack()14 SkCanvasStack::~SkCanvasStack() {
15     this->removeAll();
16 }
17 
pushCanvas(std::unique_ptr<SkCanvas> canvas,const SkIPoint & origin)18 void SkCanvasStack::pushCanvas(std::unique_ptr<SkCanvas> canvas, const SkIPoint& origin) {
19     if (canvas) {
20         // compute the bounds of this canvas
21         const SkIRect canvasBounds = SkIRect::MakeSize(canvas->getBaseLayerSize());
22 
23         // push the canvas onto the stack
24         this->INHERITED::addCanvas(canvas.get());
25 
26         // push the canvas data onto the stack
27         CanvasData* data = &fCanvasData.push_back();
28         data->origin = origin;
29         data->requiredClip.setRect(canvasBounds);
30         data->ownedCanvas = std::move(canvas);
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);
40             fList[i-1]->clipRegion(fCanvasData[i-1].requiredClip);
41         }
42     }
43     SkASSERT(fList.count() == fCanvasData.count());
44 }
45 
removeAll()46 void SkCanvasStack::removeAll() {
47     this->INHERITED::removeAll();   // call the baseclass *before* we actually delete the canvases
48     fCanvasData.reset();
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  */
clipToZOrderedBounds()56 void SkCanvasStack::clipToZOrderedBounds() {
57     SkASSERT(fList.count() == fCanvasData.count());
58     for (int i = 0; i < fList.count(); ++i) {
59         fList[i]->clipRegion(fCanvasData[i].requiredClip);
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  */
didSetMatrix(const SkMatrix & matrix)70 void SkCanvasStack::didSetMatrix(const SkMatrix& matrix) {
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     }
79     this->SkCanvas::didSetMatrix(matrix);
80 }
81 
onClipRect(const SkRect & r,SkClipOp op,ClipEdgeStyle edgeStyle)82 void SkCanvasStack::onClipRect(const SkRect& r, SkClipOp op, ClipEdgeStyle edgeStyle) {
83     this->INHERITED::onClipRect(r, op, edgeStyle);
84     this->clipToZOrderedBounds();
85 }
86 
onClipRRect(const SkRRect & rr,SkClipOp op,ClipEdgeStyle edgeStyle)87 void SkCanvasStack::onClipRRect(const SkRRect& rr, SkClipOp op, ClipEdgeStyle edgeStyle) {
88     this->INHERITED::onClipRRect(rr, op, edgeStyle);
89     this->clipToZOrderedBounds();
90 }
91 
onClipPath(const SkPath & p,SkClipOp op,ClipEdgeStyle edgeStyle)92 void SkCanvasStack::onClipPath(const SkPath& p, SkClipOp op, ClipEdgeStyle edgeStyle) {
93     this->INHERITED::onClipPath(p, op, edgeStyle);
94     this->clipToZOrderedBounds();
95 }
96 
onClipShader(sk_sp<SkShader> cs,SkClipOp op)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 
onClipRegion(const SkRegion & deviceRgn,SkClipOp op)102 void SkCanvasStack::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {
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     }
111     this->SkCanvas::onClipRegion(deviceRgn, op);
112 }
113