Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GrTestUtils.h
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 "SkPathEffect.h"
17 #include "SkRandom.h"
18 #include "SkStrokeRec.h"
19 #include "../private/SkTemplates.h"
20 
21 class GrStyle;
22 class SkMatrix;
23 class SkPath;
24 class SkRRect;
25 struct SkRect;
26 
27 namespace GrTest {
31 const SkMatrix& TestMatrix(SkRandom*);
32 const SkMatrix& TestMatrixPreservesRightAngles(SkRandom*);
33 const SkMatrix& TestMatrixRectStaysRect(SkRandom*);
34 const SkMatrix& TestMatrixInvertible(SkRandom*);
35 const SkMatrix& TestMatrixPerspective(SkRandom*);
36 const SkRect& TestRect(SkRandom*);
37 const SkRect& TestSquare(SkRandom*);
38 const SkRRect& TestRRectSimple(SkRandom*);
39 const SkPath& TestPath(SkRandom*);
40 const SkPath& TestPathConvex(SkRandom*);
41 SkStrokeRec TestStrokeRec(SkRandom*);
43 void TestStyle(SkRandom*, GrStyle*);
44 
45 // We have a simplified dash path effect here to avoid relying on SkDashPathEffect which
46 // is in the optional build target effects.
47 class TestDashPathEffect : public SkPathEffect {
48 public:
49  static sk_sp<SkPathEffect> Make(const SkScalar* intervals, int count, SkScalar phase) {
50  return sk_sp<SkPathEffect>(new TestDashPathEffect(intervals, count, phase));
51  }
52 
53  bool filterPath(SkPath* dst, const SkPath&, SkStrokeRec* , const SkRect*) const override;
54  DashType asADash(DashInfo* info) const override;
55  Factory getFactory() const override { return nullptr; }
56  void toString(SkString*) const override {}
57 
58 private:
59  TestDashPathEffect(const SkScalar* intervals, int count, SkScalar phase);
60 
61  int fCount;
62  SkAutoTArray<SkScalar> fIntervals;
63  SkScalar fPhase;
64  SkScalar fInitialDashLength;
65  int fInitialDashIndex;
66  SkScalar fIntervalLength;
67 };
68 
69 } // namespace GrTest
70 
71 static inline GrColor GrRandomColor(SkRandom* random) {
72  // There are only a few cases of random colors which interest us
73  enum ColorMode {
74  kAllOnes_ColorMode,
75  kAllZeros_ColorMode,
76  kAlphaOne_ColorMode,
77  kRandom_ColorMode,
78  kLast_ColorMode = kRandom_ColorMode
79  };
80 
81  ColorMode colorMode = ColorMode(random->nextULessThan(kLast_ColorMode + 1));
82  GrColor color SK_INIT_TO_AVOID_WARNING;
83  switch (colorMode) {
84  case kAllOnes_ColorMode:
85  color = GrColorPackRGBA(0xFF, 0xFF, 0xFF, 0xFF);
86  break;
87  case kAllZeros_ColorMode:
88  color = GrColorPackRGBA(0, 0, 0, 0);
89  break;
90  case kAlphaOne_ColorMode:
91  color = GrColorPackRGBA(random->nextULessThan(256),
92  random->nextULessThan(256),
93  random->nextULessThan(256),
94  0xFF);
95  break;
96  case kRandom_ColorMode: {
97  uint8_t alpha = random->nextULessThan(256);
98  color = GrColorPackRGBA(random->nextRangeU(0, alpha),
99  random->nextRangeU(0, alpha),
100  random->nextRangeU(0, alpha),
101  alpha);
102  break;
103  }
104  }
105  GrColorIsPMAssert(color);
106  return color;
107 }
108 
109 static inline uint8_t GrRandomCoverage(SkRandom* random) {
110  enum CoverageMode {
111  kZero_CoverageMode,
112  kAllOnes_CoverageMode,
113  kRandom_CoverageMode,
114  kLast_CoverageMode = kRandom_CoverageMode
115  };
116 
117  CoverageMode colorMode = CoverageMode(random->nextULessThan(kLast_CoverageMode + 1));
118  uint8_t coverage SK_INIT_TO_AVOID_WARNING;
119  switch (colorMode) {
120  case kZero_CoverageMode:
121  coverage = 0;
122  break;
123  case kAllOnes_CoverageMode:
124  coverage = 0xff;
125  break;
126  case kRandom_CoverageMode:
127  coverage = random->nextULessThan(256);
128  break;
129  }
130  return coverage;
131 }
132 
133 #endif
134 #endif
The SkRRect class represents a rounded rect with a potentially different radii for each corner...
Definition: SkRRect.h:48
The SkPath class encapsulates compound (multiple contour) geometric paths consisting of straight line...
Definition: SkPath.h:27
SkPathEffect is the base class for objects in the SkPaint that affect the geometry of a drawing primi...
Definition: SkPathEffect.h:29
Definition: SkStrokeRec.h:16
The SkMatrix class holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:26
Definition: SkRect.h:390
Light weight class for managing strings.
Definition: SkString.h:121