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