1 /*
2 * Copyright 2015 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 #ifndef GrTestUtils_DEFINED
9 #define GrTestUtils_DEFINED
10
11 #include "include/core/SkTypes.h"
12
13 #if GR_TEST_UTILS
14
15 #include "include/core/SkPathEffect.h"
16 #include "include/core/SkStrokeRec.h"
17 #include "include/private/SkMacros.h"
18 #include "include/private/SkTemplates.h"
19 #include "include/utils/SkRandom.h"
20 #include "src/core/SkMatrixProvider.h"
21 #include "src/gpu/GrColor.h"
22 #include "src/gpu/GrFPArgs.h"
23 #include "src/gpu/GrSamplerState.h"
24 #include "src/shaders/SkShaderBase.h"
25
26 class GrColorInfo;
27 class GrColorSpaceXform;
28 class GrProcessorTestData;
29 class GrStyle;
30 class SkMatrix;
31 class SkPath;
32 class SkRRect;
33 struct SkRect;
34
35 namespace GrTest {
36 /**
37 * Helpers for use in Test functions.
38 */
39 const SkMatrix& TestMatrix(SkRandom*);
40 const SkMatrix& TestMatrixPreservesRightAngles(SkRandom*);
41 const SkMatrix& TestMatrixRectStaysRect(SkRandom*);
42 const SkMatrix& TestMatrixInvertible(SkRandom*);
43 const SkMatrix& TestMatrixPerspective(SkRandom*);
44 void TestWrapModes(SkRandom*, GrSamplerState::WrapMode[2]);
45 const SkRect& TestRect(SkRandom*);
46 const SkRect& TestSquare(SkRandom*);
47 const SkRRect& TestRRectSimple(SkRandom*);
48 const SkPath& TestPath(SkRandom*);
49 const SkPath& TestPathConvex(SkRandom*);
50 SkStrokeRec TestStrokeRec(SkRandom*);
51 /** Creates styles with dash path effects and null path effects */
52 void TestStyle(SkRandom*, GrStyle*);
53 sk_sp<SkColorSpace> TestColorSpace(SkRandom*);
54 sk_sp<GrColorSpaceXform> TestColorXform(SkRandom*);
55
56 class TestAsFPArgs {
57 public:
58 TestAsFPArgs(GrProcessorTestData*);
59 ~TestAsFPArgs();
args()60 const GrFPArgs& args() const { return fArgs; }
61
62 private:
63 SkSimpleMatrixProvider fMatrixProvider;
64 std::unique_ptr<GrColorInfo> fColorInfoStorage;
65 GrFPArgs fArgs;
66 };
67
68 // We have a simplified dash path effect here to avoid relying on SkDashPathEffect which
69 // is in the optional build target effects.
70 class TestDashPathEffect : public SkPathEffect {
71 public:
Make(const SkScalar * intervals,int count,SkScalar phase)72 static sk_sp<SkPathEffect> Make(const SkScalar* intervals, int count, SkScalar phase) {
73 return sk_sp<SkPathEffect>(new TestDashPathEffect(intervals, count, phase));
74 }
75
getFactory()76 Factory getFactory() const override { return nullptr; }
getTypeName()77 const char* getTypeName() const override { return nullptr; }
78
79 protected:
80 bool onFilterPath(SkPath* dst, const SkPath&, SkStrokeRec* , const SkRect*) const override;
81 DashType onAsADash(DashInfo* info) const override;
82
83 private:
84 TestDashPathEffect(const SkScalar* intervals, int count, SkScalar phase);
85
computeFastBounds(SkRect * bounds)86 bool computeFastBounds(SkRect* bounds) const override { return true; }
87
88 int fCount;
89 SkAutoTArray<SkScalar> fIntervals;
90 SkScalar fPhase;
91 SkScalar fInitialDashLength;
92 int fInitialDashIndex;
93 SkScalar fIntervalLength;
94 };
95
96 } // namespace GrTest
97
GrRandomColor(SkRandom * random)98 static inline GrColor GrRandomColor(SkRandom* random) {
99 // There are only a few cases of random colors which interest us
100 enum ColorMode {
101 kAllOnes_ColorMode,
102 kAllZeros_ColorMode,
103 kAlphaOne_ColorMode,
104 kRandom_ColorMode,
105 kLast_ColorMode = kRandom_ColorMode
106 };
107
108 ColorMode colorMode = ColorMode(random->nextULessThan(kLast_ColorMode + 1));
109 GrColor color SK_INIT_TO_AVOID_WARNING;
110 switch (colorMode) {
111 case kAllOnes_ColorMode:
112 color = GrColorPackRGBA(0xFF, 0xFF, 0xFF, 0xFF);
113 break;
114 case kAllZeros_ColorMode:
115 color = GrColorPackRGBA(0, 0, 0, 0);
116 break;
117 case kAlphaOne_ColorMode:
118 color = GrColorPackRGBA(random->nextULessThan(256),
119 random->nextULessThan(256),
120 random->nextULessThan(256),
121 0xFF);
122 break;
123 case kRandom_ColorMode: {
124 uint8_t alpha = random->nextULessThan(256);
125 color = GrColorPackRGBA(random->nextRangeU(0, alpha),
126 random->nextRangeU(0, alpha),
127 random->nextRangeU(0, alpha),
128 alpha);
129 break;
130 }
131 }
132 return color;
133 }
134
GrRandomCoverage(SkRandom * random)135 static inline uint8_t GrRandomCoverage(SkRandom* random) {
136 enum CoverageMode {
137 kZero_CoverageMode,
138 kAllOnes_CoverageMode,
139 kRandom_CoverageMode,
140 kLast_CoverageMode = kRandom_CoverageMode
141 };
142
143 CoverageMode colorMode = CoverageMode(random->nextULessThan(kLast_CoverageMode + 1));
144 uint8_t coverage SK_INIT_TO_AVOID_WARNING;
145 switch (colorMode) {
146 case kZero_CoverageMode:
147 coverage = 0;
148 break;
149 case kAllOnes_CoverageMode:
150 coverage = 0xff;
151 break;
152 case kRandom_CoverageMode:
153 coverage = random->nextULessThan(256);
154 break;
155 }
156 return coverage;
157 }
158
159 #endif
160 #endif
161