Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f08a82b

Browse files
johnstiles-googleSkia Commit-Bot
authored and
Skia Commit-Bot
committed
Return tuple from GrConvexPolyEffect::Make denoting success or failure.
Change-Id: I1c9cd865c3fd37b5d4c911790713d9ca2283aeee Reviewed-on: https://skia-review.googlesource.com/c/skia/+/296724 Reviewed-by: Brian Osman <brianosman@google.com> Commit-Queue: John Stiles <johnstiles@google.com>
1 parent c85f1fa commit f08a82b

File tree

6 files changed

+72
-52
lines changed

6 files changed

+72
-52
lines changed

gm/convexpolyeffect.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,8 @@ class ConvexPolyEffect : public GpuGM {
126126
path->transform(m, &p);
127127

128128
GrClipEdgeType edgeType = (GrClipEdgeType) et;
129-
std::unique_ptr<GrFragmentProcessor> fp =
130-
GrConvexPolyEffect::Make(/*inputFP=*/nullptr, edgeType, p);
131-
if (!fp) {
129+
auto [success, fp] = GrConvexPolyEffect::Make(/*inputFP=*/nullptr, edgeType, p);
130+
if (!success) {
132131
continue;
133132
}
134133

@@ -166,9 +165,8 @@ class ConvexPolyEffect : public GpuGM {
166165
for (int et = 0; et < kGrClipEdgeTypeCnt; ++et) {
167166
SkRect rect = iter.get()->makeOffset(x, y);
168167
GrClipEdgeType edgeType = (GrClipEdgeType) et;
169-
std::unique_ptr<GrFragmentProcessor> fp =
170-
GrConvexPolyEffect::Make(/*inputFP=*/nullptr, edgeType, rect);
171-
if (!fp) {
168+
auto [success, fp] = GrConvexPolyEffect::Make(/*inputFP=*/nullptr, edgeType, rect);
169+
if (!success) {
172170
continue;
173171
}
174172

src/gpu/GrFragmentProcessor.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#ifndef GrFragmentProcessor_DEFINED
99
#define GrFragmentProcessor_DEFINED
1010

11+
#include <tuple>
12+
1113
#include "src/gpu/GrCoordTransform.h"
1214
#include "src/gpu/GrProcessor.h"
1315
#include "src/gpu/ops/GrOp.h"
@@ -209,6 +211,20 @@ class GrFragmentProcessor : public GrProcessor {
209211

210212
void visitProxies(const GrOp::VisitProxyFunc& func);
211213

214+
/**
215+
* Some fragment processors' Make() methods have preconditions that might not be satisfied by
216+
* the calling code. Those FPs can return a `MakeResult` from their Make() methods. If creation
217+
* succeeds, the new fragment processor is created and `success` is true. If a precondition is
218+
* not met, `success` is set to false and the input FP is returned unchanged.
219+
*/
220+
using MakeResult = std::tuple<bool /*success*/, std::unique_ptr<GrFragmentProcessor>>;
221+
static MakeResult MakeFailure(std::unique_ptr<GrFragmentProcessor> fp) {
222+
return {false, std::move(fp)};
223+
}
224+
static MakeResult MakeSuccess(std::unique_ptr<GrFragmentProcessor> fp) {
225+
return {true, std::move(fp)};
226+
}
227+
212228
// A pre-order traversal iterator over a hierarchy of FPs. It can also iterate over all the FP
213229
// hierarchies rooted in a GrPaint, GrProcessorSet, or GrPipeline. For these collections it
214230
// iterates the tree rooted at each color FP and then each coverage FP.

src/gpu/GrReducedClip.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -689,18 +689,18 @@ GrReducedClip::ClipResult GrReducedClip::addAnalyticFP(const SkPath& deviceSpace
689689

690690
if (fAnalyticFPs.empty()) {
691691
// Create our first analytic effect in the stack.
692-
auto fp = GrConvexPolyEffect::Make(/*inputFP=*/nullptr, GetClipEdgeType(invert, aa),
693-
deviceSpacePath);
694-
if (fp != nullptr) {
692+
auto [success, fp] = GrConvexPolyEffect::Make(/*inputFP=*/nullptr,
693+
GetClipEdgeType(invert, aa), deviceSpacePath);
694+
if (success) {
695695
fAnalyticFPs.push_back(std::move(fp));
696696
return ClipResult::kClipped;
697697
}
698698
} else {
699699
// Combine this analytic effect with the previous effect in the stack.
700-
auto fp = GrConvexPolyEffect::Make(&fAnalyticFPs.back(), GetClipEdgeType(invert, aa),
701-
deviceSpacePath);
702-
if (fp != nullptr) {
703-
fAnalyticFPs.back() = std::move(fp);
700+
auto [success, fp] = GrConvexPolyEffect::Make(std::move(fAnalyticFPs.back()),
701+
GetClipEdgeType(invert, aa), deviceSpacePath);
702+
fAnalyticFPs.back() = std::move(fp);
703+
if (success) {
704704
return ClipResult::kClipped;
705705
}
706706
}

src/gpu/effects/GrConvexPolyEffect.cpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,14 @@ void GrGLConvexPolyEffect::GenKey(const GrProcessor& processor, const GrShaderCa
9494

9595
//////////////////////////////////////////////////////////////////////////////
9696

97-
std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::Make(
98-
std::unique_ptr<GrFragmentProcessor>* inputFP,
97+
GrFragmentProcessor::MakeResult GrConvexPolyEffect::Make(
98+
std::unique_ptr<GrFragmentProcessor> inputFP,
9999
GrClipEdgeType type, const SkPath& path) {
100100
if (GrClipEdgeType::kHairlineAA == type) {
101-
return nullptr;
101+
return MakeFailure(std::move(inputFP));
102102
}
103-
if (path.getSegmentMasks() != SkPath::kLine_SegmentMask ||
104-
!path.isConvex()) {
105-
return nullptr;
103+
if (path.getSegmentMasks() != SkPath::kLine_SegmentMask || !path.isConvex()) {
104+
return MakeFailure(std::move(inputFP));
106105
}
107106

108107
SkPathPriv::FirstDirection dir;
@@ -111,17 +110,17 @@ std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::Make(
111110
// skip the draw or omit the clip element.
112111
if (!SkPathPriv::CheapComputeFirstDirection(path, &dir)) {
113112
if (GrProcessorEdgeTypeIsInverseFill(type)) {
114-
return GrConstColorProcessor::Make(inputFP ? std::move(*inputFP) : nullptr,
115-
SK_PMColor4fWHITE,
116-
GrConstColorProcessor::InputMode::kModulateRGBA);
113+
return MakeSuccess(
114+
GrConstColorProcessor::Make(std::move(inputFP), SK_PMColor4fWHITE,
115+
GrConstColorProcessor::InputMode::kModulateRGBA));
117116
}
118117
// This could use kIgnore instead of kModulateRGBA but it would trigger a debug print
119118
// about a coverage processor not being compatible with the alpha-as-coverage optimization.
120119
// We don't really care about this unlikely case so we just use kModulateRGBA to suppress
121120
// the print.
122-
return GrConstColorProcessor::Make(inputFP ? std::move(*inputFP) : nullptr,
123-
SK_PMColor4fTRANSPARENT,
124-
GrConstColorProcessor::InputMode::kModulateRGBA);
121+
return MakeSuccess(
122+
GrConstColorProcessor::Make(std::move(inputFP), SK_PMColor4fTRANSPARENT,
123+
GrConstColorProcessor::InputMode::kModulateRGBA));
125124
}
126125

127126
SkScalar edges[3 * kMaxEdges];
@@ -143,7 +142,7 @@ std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::Make(
143142
break;
144143
case SkPath::kLine_Verb: {
145144
if (n >= kMaxEdges) {
146-
return nullptr;
145+
return MakeFailure(std::move(inputFP));
147146
}
148147
if (pts[0] != pts[1]) {
149148
SkVector v = pts[1] - pts[0];
@@ -161,23 +160,24 @@ std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::Make(
161160
break;
162161
}
163162
default:
164-
return nullptr;
163+
return MakeFailure(std::move(inputFP));
165164
}
166165
}
167166

168167
if (path.isInverseFillType()) {
169168
type = GrInvertProcessorEdgeType(type);
170169
}
171-
return Make(inputFP, type, n, edges);
170+
return GrConvexPolyEffect::Make(std::move(inputFP), type, n, edges);
172171
}
173172

174-
std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::Make(
175-
std::unique_ptr<GrFragmentProcessor>* inputFP,
173+
GrFragmentProcessor::MakeResult GrConvexPolyEffect::Make(
174+
std::unique_ptr<GrFragmentProcessor> inputFP,
176175
GrClipEdgeType edgeType, const SkRect& rect) {
177-
if (GrClipEdgeType::kHairlineAA == edgeType){
178-
return nullptr;
176+
if (GrClipEdgeType::kHairlineAA == edgeType) {
177+
return MakeFailure(std::move(inputFP));
179178
}
180-
return GrAARectEffect::Make(inputFP ? std::move(*inputFP) : nullptr, edgeType, rect);
179+
180+
return MakeSuccess(GrAARectEffect::Make(std::move(inputFP), edgeType, rect));
181181
}
182182

183183
GrConvexPolyEffect::~GrConvexPolyEffect() {}
@@ -245,7 +245,11 @@ std::unique_ptr<GrFragmentProcessor> GrConvexPolyEffect::TestCreate(GrProcessorT
245245
do {
246246
GrClipEdgeType edgeType = static_cast<GrClipEdgeType>(
247247
d->fRandom->nextULessThan(kGrClipEdgeTypeCnt));
248-
fp = GrConvexPolyEffect::Make(/*inputFP=*/nullptr, edgeType, count, edges);
248+
auto [success, convexPolyFP] = GrConvexPolyEffect::Make(/*inputFP=*/nullptr, edgeType,
249+
count, edges);
250+
if (success) {
251+
fp = std::move(convexPolyFP);
252+
}
249253
} while (nullptr == fp);
250254
return fp;
251255
}

src/gpu/effects/GrConvexPolyEffect.h

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,27 @@ class GrConvexPolyEffect : public GrFragmentProcessor {
3535
* them in src space. There are a number of ways this could be accomplished but we'd probably
3636
* have to modify the effect/shaderbuilder interface to make it possible (e.g. give access
3737
* to the view matrix or untransformed positions in the fragment shader).
38-
*
39-
* The input fragment processor is passed as a pointer because it is only absorbed if creation
40-
* of the convex-poly effect is successful. If Make returns nullptr, the inputFP is left as-is.
4138
*/
42-
static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor>* inputFP,
43-
GrClipEdgeType edgeType,
44-
int n, const SkScalar edges[]) {
39+
static MakeResult Make(std::unique_ptr<GrFragmentProcessor> inputFP,
40+
GrClipEdgeType edgeType, int n, const SkScalar edges[]) {
4541
if (n <= 0 || n > kMaxEdges || GrClipEdgeType::kHairlineAA == edgeType) {
46-
return nullptr;
42+
return MakeFailure(std::move(inputFP));
4743
}
48-
return std::unique_ptr<GrFragmentProcessor>(
49-
new GrConvexPolyEffect(inputFP ? std::move(*inputFP) : nullptr, edgeType, n, edges));
44+
45+
return MakeSuccess(std::unique_ptr<GrFragmentProcessor>(
46+
new GrConvexPolyEffect(std::move(inputFP), edgeType, n, edges)));
5047
}
5148

5249
/**
5350
* Creates an effect that clips against the path. If the path is not a convex polygon, is
54-
* inverse filled, or has too many edges, this will return nullptr.
51+
* inverse filled, or has too many edges, creation will fail.
5552
*/
56-
static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor>*,
57-
GrClipEdgeType, const SkPath&);
53+
static MakeResult Make(std::unique_ptr<GrFragmentProcessor>, GrClipEdgeType, const SkPath&);
5854

5955
/**
6056
* Creates an effect that fills inside the rect with AA edges..
6157
*/
62-
static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor>*,
63-
GrClipEdgeType, const SkRect&);
58+
static MakeResult Make(std::unique_ptr<GrFragmentProcessor>, GrClipEdgeType, const SkRect&);
6459

6560
~GrConvexPolyEffect() override;
6661

src/gpu/effects/GrRRectEffect.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,9 @@ std::unique_ptr<GrFragmentProcessor> GrRRectEffect::Make(GrClipEdgeType edgeType
726726
const SkRRect& rrect,
727727
const GrShaderCaps& caps) {
728728
if (rrect.isRect()) {
729-
return GrConvexPolyEffect::Make(/*inputFP=*/nullptr, edgeType, rrect.getBounds());
729+
auto [success, fp] = GrConvexPolyEffect::Make(/*inputFP=*/nullptr,
730+
edgeType, rrect.getBounds());
731+
return std::move(fp);
730732
}
731733

732734
if (rrect.isOval()) {
@@ -738,7 +740,9 @@ std::unique_ptr<GrFragmentProcessor> GrRRectEffect::Make(GrClipEdgeType edgeType
738740
SkRRectPriv::GetSimpleRadii(rrect).fY < kRadiusMin) {
739741
// In this case the corners are extremely close to rectangular and we collapse the
740742
// clip to a rectangular clip.
741-
return GrConvexPolyEffect::Make(/*inputFP=*/nullptr, edgeType, rrect.getBounds());
743+
auto [success, fp] = GrConvexPolyEffect::Make(/*inputFP=*/nullptr,
744+
edgeType, rrect.getBounds());
745+
return std::move(fp);
742746
}
743747
if (SkRRectPriv::GetSimpleRadii(rrect).fX == SkRRectPriv::GetSimpleRadii(rrect).fY) {
744748
return CircularRRectEffect::Make(/*inputFP=*/nullptr, edgeType,
@@ -803,8 +807,11 @@ std::unique_ptr<GrFragmentProcessor> GrRRectEffect::Make(GrClipEdgeType edgeType
803807
}
804808
return CircularRRectEffect::Make(/*inputFP=*/nullptr, edgeType, cornerFlags, *rr);
805809
}
806-
case CircularRRectEffect::kNone_CornerFlags:
807-
return GrConvexPolyEffect::Make(/*inputFP=*/nullptr, edgeType, rrect.getBounds());
810+
case CircularRRectEffect::kNone_CornerFlags: {
811+
auto [success, fp] = GrConvexPolyEffect::Make(/*inputFP=*/nullptr,
812+
edgeType, rrect.getBounds());
813+
return std::move(fp);
814+
}
808815
default: {
809816
if (squashedRadii) {
810817
// If we got here then we squashed some but not all the radii to zero. (If all

0 commit comments

Comments
 (0)