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