• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkTypes.h"
12 
13 #if GR_TEST_UTILS
14 
15 #include "../private/SkTemplates.h"
16 #include "GrColor.h"
17 #include "GrFPArgs.h"
18 #include "GrSamplerState.h"
19 #include "SkMacros.h"
20 #include "SkPathEffect.h"
21 #include "SkRandom.h"
22 #include "SkShaderBase.h"
23 #include "SkStrokeRec.h"
24 
25 class GrColorSpaceInfo;
26 class GrColorSpaceXform;
27 struct GrProcessorTestData;
28 class GrStyle;
29 class SkMatrix;
30 class SkPath;
31 class SkRRect;
32 struct SkRect;
33 
34 namespace GrTest {
35 /**
36  * Helpers for use in Test functions.
37  */
38 const SkMatrix& TestMatrix(SkRandom*);
39 const SkMatrix& TestMatrixPreservesRightAngles(SkRandom*);
40 const SkMatrix& TestMatrixRectStaysRect(SkRandom*);
41 const SkMatrix& TestMatrixInvertible(SkRandom*);
42 const SkMatrix& TestMatrixPerspective(SkRandom*);
43 void TestWrapModes(SkRandom*, GrSamplerState::WrapMode[2]);
44 const SkRect& TestRect(SkRandom*);
45 const SkRect& TestSquare(SkRandom*);
46 const SkRRect& TestRRectSimple(SkRandom*);
47 const SkPath& TestPath(SkRandom*);
48 const SkPath& TestPathConvex(SkRandom*);
49 SkStrokeRec TestStrokeRec(SkRandom*);
50 /** Creates styles with dash path effects and null path effects */
51 void TestStyle(SkRandom*, GrStyle*);
52 sk_sp<SkColorSpace> TestColorSpace(SkRandom*);
53 sk_sp<GrColorSpaceXform> TestColorXform(SkRandom*);
54 
55 class TestAsFPArgs {
56 public:
57     TestAsFPArgs(GrProcessorTestData*);
58     ~TestAsFPArgs();
args()59     const GrFPArgs& args() const { return fArgs; }
60 
61 private:
62     SkMatrix fViewMatrixStorage;
63     std::unique_ptr<GrColorSpaceInfo> fColorSpaceInfoStorage;
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.
69 class TestDashPathEffect : public SkPathEffect {
70 public:
Make(const SkScalar * intervals,int count,SkScalar phase)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 
getFactory()75     Factory getFactory() const override { return nullptr; }
getTypeName()76     const char* getTypeName() const override { return nullptr; }
77 
78 protected:
79     bool onFilterPath(SkPath* dst, const SkPath&, SkStrokeRec* , const SkRect*) const override;
80     DashType onAsADash(DashInfo* info) const override;
81 
82 private:
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 
GrRandomColor(SkRandom * random)95 static 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 
GrRandomCoverage(SkRandom * random)132 static 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