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 #ifdef GR_TEST_UTILS
14 
15 #include "GrColor.h"
16 #include "SkRandom.h"
17 #include "SkStrokeRec.h"
18 
19 class GrStrokeInfo;
20 class SkMatrix;
21 class SkPath;
22 class SkRRect;
23 struct SkRect;
24 
25 namespace GrTest {
26 /**
27  * A helper for use in Test functions.
28  */
29 const SkMatrix& TestMatrix(SkRandom*);
30 const SkMatrix& TestMatrixPreservesRightAngles(SkRandom*);
31 const SkMatrix& TestMatrixRectStaysRect(SkRandom*);
32 const SkMatrix& TestMatrixInvertible(SkRandom*);
33 const SkRect& TestRect(SkRandom*);
34 const SkRect& TestSquare(SkRandom*);
35 const SkRRect& TestRRectSimple(SkRandom*);
36 const SkPath& TestPath(SkRandom*);
37 const SkPath& TestPathConvex(SkRandom*);
38 SkStrokeRec TestStrokeRec(SkRandom*);
39 GrStrokeInfo TestStrokeInfo(SkRandom*);
40 
41 }
42 
GrRandomColor(SkRandom * random)43 static inline GrColor GrRandomColor(SkRandom* random) {
44     // There are only a few cases of random colors which interest us
45     enum ColorMode {
46         kAllOnes_ColorMode,
47         kAllZeros_ColorMode,
48         kAlphaOne_ColorMode,
49         kRandom_ColorMode,
50         kLast_ColorMode = kRandom_ColorMode
51     };
52 
53     ColorMode colorMode = ColorMode(random->nextULessThan(kLast_ColorMode + 1));
54     GrColor color;
55     switch (colorMode) {
56         case kAllOnes_ColorMode:
57             color = GrColorPackRGBA(0xFF, 0xFF, 0xFF, 0xFF);
58             break;
59         case kAllZeros_ColorMode:
60             color = GrColorPackRGBA(0, 0, 0, 0);
61             break;
62         case kAlphaOne_ColorMode:
63             color = GrColorPackRGBA(random->nextULessThan(256),
64                                     random->nextULessThan(256),
65                                     random->nextULessThan(256),
66                                     0xFF);
67             break;
68         case kRandom_ColorMode: {
69                 uint8_t alpha = random->nextULessThan(256);
70                 color = GrColorPackRGBA(random->nextRangeU(0, alpha),
71                                         random->nextRangeU(0, alpha),
72                                         random->nextRangeU(0, alpha),
73                                         alpha);
74             break;
75         }
76     }
77     GrColorIsPMAssert(color);
78     return color;
79 }
80 
GrRandomCoverage(SkRandom * random)81 static inline uint8_t GrRandomCoverage(SkRandom* random) {
82     enum CoverageMode {
83         kZero_CoverageMode,
84         kAllOnes_CoverageMode,
85         kRandom_CoverageMode,
86         kLast_CoverageMode = kRandom_CoverageMode
87     };
88 
89     CoverageMode colorMode = CoverageMode(random->nextULessThan(kLast_CoverageMode + 1));
90     uint8_t coverage;
91     switch (colorMode) {
92         case kZero_CoverageMode:
93             coverage = 0;
94             break;
95         case kAllOnes_CoverageMode:
96             coverage = 0xff;
97             break;
98         case kRandom_CoverageMode:
99             coverage = random->nextULessThan(256);
100             break;
101     }
102     return coverage;
103 }
104 
105 #endif
106 #endif
107