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