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