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