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 */
didSetM44(const SkM44 & mx)70 void SkCanvasStack::didSetM44(const SkM44& mx) {
71 SkASSERT(fList.count() == fCanvasData.count());
72 for (int i = 0; i < fList.count(); ++i) {
73 fList[i]->setMatrix(SkM44::Translate(SkIntToScalar(-fCanvasData[i].origin.x()),
74 SkIntToScalar(-fCanvasData[i].origin.y())) * mx);
75 }
76 this->SkCanvas::didSetM44(mx);
77 }
78
onClipRect(const SkRect & r,SkClipOp op,ClipEdgeStyle edgeStyle)79 void SkCanvasStack::onClipRect(const SkRect& r, SkClipOp op, ClipEdgeStyle edgeStyle) {
80 this->INHERITED::onClipRect(r, op, edgeStyle);
81 this->clipToZOrderedBounds();
82 }
83
onClipRRect(const SkRRect & rr,SkClipOp op,ClipEdgeStyle edgeStyle)84 void SkCanvasStack::onClipRRect(const SkRRect& rr, SkClipOp op, ClipEdgeStyle edgeStyle) {
85 this->INHERITED::onClipRRect(rr, op, edgeStyle);
86 this->clipToZOrderedBounds();
87 }
88
onClipPath(const SkPath & p,SkClipOp op,ClipEdgeStyle edgeStyle)89 void SkCanvasStack::onClipPath(const SkPath& p, SkClipOp op, ClipEdgeStyle edgeStyle) {
90 this->INHERITED::onClipPath(p, op, edgeStyle);
91 this->clipToZOrderedBounds();
92 }
93
onClipShader(sk_sp<SkShader> cs,SkClipOp op)94 void SkCanvasStack::onClipShader(sk_sp<SkShader> cs, SkClipOp op) {
95 this->INHERITED::onClipShader(std::move(cs), op);
96 // we don't change the "bounds" of the clip, so we don't need to update zorder
97 }
98
onClipRegion(const SkRegion & deviceRgn,SkClipOp op)99 void SkCanvasStack::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {
100 SkASSERT(fList.count() == fCanvasData.count());
101 for (int i = 0; i < fList.count(); ++i) {
102 SkRegion tempRegion;
103 deviceRgn.translate(-fCanvasData[i].origin.x(),
104 -fCanvasData[i].origin.y(), &tempRegion);
105 tempRegion.op(fCanvasData[i].requiredClip, SkRegion::kIntersect_Op);
106 fList[i]->clipRegion(tempRegion, op);
107 }
108 this->SkCanvas::onClipRegion(deviceRgn, op);
109 }
110