Debian Codegraph @

src/core/SkClipStackDevice.cpp

raw
1 /*
2 * Copyright 2017 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/core/SkClipStackDevice.h"
9
10 #include "include/core/SkImageInfo.h"
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkPathBuilder.h"
14 #include "include/core/SkPoint.h"
15 #include "include/core/SkRegion.h"
16 #include "include/core/SkShader.h"
17 #include "include/private/base/SkAssert.h"
18 #include "src/core/SkMatrixPriv.h"
19
20 #include <utility>
21
22 class SkRRect;
23 enum class SkClipOp;
24
25 SkIRect SkClipStackDevice::devClipBounds() const {
26 SkIRect r = fClipStack.bounds(this->imageInfo().bounds()).roundOut();
27 if (!r.isEmpty()) {
28 SkASSERT(this->imageInfo().bounds().contains(r));
29 }
30 return r;
31 }
32
33 ///////////////////////////////////////////////////////////////////////////////////////////////////
34
35 void SkClipStackDevice::pushClipStack() {
36 fClipStack.save();
37 }
38
39 void SkClipStackDevice::popClipStack() {
40 fClipStack.restore();
41 }
42
43 void SkClipStackDevice::clipRect(const SkRect& rect, SkClipOp op, bool aa) {
44 fClipStack.clipRect(rect, this->localToDevice(), op, aa);
45 }
46
47 void SkClipStackDevice::clipRRect(const SkRRect& rrect, SkClipOp op, bool aa) {
48 fClipStack.clipRRect(rrect, this->localToDevice(), op, aa);
49 }
50
51 void SkClipStackDevice::clipPath(const SkPath& path, SkClipOp op, bool aa) {
52 fClipStack.clipPath(path, this->localToDevice(), op, aa);
53 }
54
55 void SkClipStackDevice::onClipShader(sk_sp<SkShader> shader) {
56 fClipStack.clipShader(std::move(shader));
57 }
58
59 void SkClipStackDevice::clipRegion(const SkRegion& rgn, SkClipOp op) {
60 SkIPoint origin = this->getOrigin();
61 SkRegion tmp;
62 SkPathBuilder builder;
63 rgn.addBoundaryPath(&builder);
64 builder.transform(SkMatrix::Translate(-origin));
65 fClipStack.clipPath(builder.detach(), SkMatrix::I(), op, false);
66 }
67
68 void SkClipStackDevice::replaceClip(const SkIRect& rect) {
69 SkRect deviceRect = SkMatrixPriv::MapRect(this->globalToDevice(), SkRect::Make(rect));
70 fClipStack.replaceClip(deviceRect, /*doAA=*/false);
71 }
72
73 bool SkClipStackDevice::isClipAntiAliased() const {
74 SkClipStack::B2TIter iter(fClipStack);
75 const SkClipStack::Element* element;
76
77 while ((element = iter.next()) != nullptr) {
78 if (element->isAA()) {
79 return true;
80 }
81 }
82 return false;
83 }
84
85 bool SkClipStackDevice::isClipWideOpen() const {
86 return fClipStack.quickContains(SkRect::MakeIWH(this->width(), this->height()));
87 }
88
89 bool SkClipStackDevice::isClipEmpty() const {
90 return fClipStack.isEmpty(SkIRect::MakeWH(this->width(), this->height()));
91 }
92
93 bool SkClipStackDevice::isClipRect() const {
94 if (this->isClipWideOpen()) {
95 return true;
96 } else if (this->isClipEmpty()) {
97 return false;
98 }
99
100 SkClipStack::BoundsType boundType;
101 bool isIntersectionOfRects;
102 SkRect bounds;
103 fClipStack.getBounds(&bounds, &boundType, &isIntersectionOfRects);
104 return isIntersectionOfRects && boundType == SkClipStack::kNormal_BoundsType;
105 }
106
107 void SkClipStackDevice::android_utils_clipAsRgn(SkRegion* rgn) const {
108 SkClipStack::BoundsType boundType;
109 bool isIntersectionOfRects;
110 SkRect bounds;
111 fClipStack.getBounds(&bounds, &boundType, &isIntersectionOfRects);
112 if (isIntersectionOfRects && SkClipStack::kNormal_BoundsType == boundType) {
113 rgn->setRect(bounds.round());
114 } else {
115 SkRegion boundsRgn({0, 0, this->width(), this->height()});
116
117 *rgn = boundsRgn;
118 SkClipStack::B2TIter iter(fClipStack);
119 while (auto elem = iter.next()) {
120 SkPath tmpPath = elem->asDeviceSpacePath();
121 SkRegion tmpRgn;
122 tmpRgn.setPath(tmpPath, boundsRgn);
123 if (elem->isReplaceOp()) {
124 // All replace elements are rectangles
125 // TODO: SkClipStack can be simplified to be I,D,R ops now, which means element
126 // iteration can be from top of the stack to the most recent replace element.
127 // When that's done, this loop will be simplifiable.
128 rgn->setRect(elem->getDeviceSpaceRect().round());
129 } else {
130 rgn->op(tmpRgn, static_cast<SkRegion::Op>(elem->getOp()));
131 }
132 }
133 }
134 }