joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 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 | |
joshualitt | 3f655f3 | 2015-04-29 10:01:22 -0700 | [diff] [blame] | 11 | #include "SkTypes.h" |
| 12 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 13 | #if GR_TEST_UTILS |
joshualitt | 3f655f3 | 2015-04-29 10:01:22 -0700 | [diff] [blame] | 14 | |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 15 | #include "../private/SkTemplates.h" |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 16 | #include "GrColor.h" |
Mike Reed | e3429e6 | 2018-01-19 11:43:34 -0500 | [diff] [blame] | 17 | #include "GrFPArgs.h" |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 18 | #include "GrSamplerState.h" |
Hal Canary | 50dbc09 | 2018-06-12 14:50:37 -0400 | [diff] [blame] | 19 | #include "SkMacros.h" |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 20 | #include "SkPathEffect.h" |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 21 | #include "SkRandom.h" |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 22 | #include "SkShaderBase.h" |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 23 | #include "SkStrokeRec.h" |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 24 | |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 25 | class GrColorSpaceInfo; |
Brian Osman | d49e946 | 2017-10-16 13:17:48 -0400 | [diff] [blame] | 26 | class GrColorSpaceXform; |
Brian Osman | 9f532a3 | 2016-10-19 11:12:09 -0400 | [diff] [blame] | 27 | struct GrProcessorTestData; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 28 | class GrStyle; |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 29 | class SkMatrix; |
joshualitt | 40ded32 | 2015-05-02 07:07:17 -0700 | [diff] [blame] | 30 | class SkPath; |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 31 | class SkRRect; |
joshualitt | 3f655f3 | 2015-04-29 10:01:22 -0700 | [diff] [blame] | 32 | struct SkRect; |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 33 | |
| 34 | namespace GrTest { |
| 35 | /** |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 36 | * Helpers for use in Test functions. |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 37 | */ |
| 38 | const SkMatrix& TestMatrix(SkRandom*); |
joshualitt | fa2008f | 2015-04-29 11:32:05 -0700 | [diff] [blame] | 39 | const SkMatrix& TestMatrixPreservesRightAngles(SkRandom*); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 40 | const SkMatrix& TestMatrixRectStaysRect(SkRandom*); |
joshualitt | 2fbd406 | 2015-05-07 13:06:41 -0700 | [diff] [blame] | 41 | const SkMatrix& TestMatrixInvertible(SkRandom*); |
robertphillips | 01a1950 | 2016-07-06 09:58:57 -0700 | [diff] [blame] | 42 | const SkMatrix& TestMatrixPerspective(SkRandom*); |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 43 | void TestWrapModes(SkRandom*, GrSamplerState::WrapMode[2]); |
joshualitt | 3f655f3 | 2015-04-29 10:01:22 -0700 | [diff] [blame] | 44 | const SkRect& TestRect(SkRandom*); |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 45 | const SkRect& TestSquare(SkRandom*); |
joshualitt | 3e708c5 | 2015-04-30 13:49:27 -0700 | [diff] [blame] | 46 | const SkRRect& TestRRectSimple(SkRandom*); |
joshualitt | 40ded32 | 2015-05-02 07:07:17 -0700 | [diff] [blame] | 47 | const SkPath& TestPath(SkRandom*); |
joshualitt | 8e5c177 | 2015-05-11 08:58:52 -0700 | [diff] [blame] | 48 | const SkPath& TestPathConvex(SkRandom*); |
joshualitt | 21279c7 | 2015-05-11 07:21:37 -0700 | [diff] [blame] | 49 | SkStrokeRec TestStrokeRec(SkRandom*); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 50 | /** Creates styles with dash path effects and null path effects */ |
| 51 | void TestStyle(SkRandom*, GrStyle*); |
Brian Osman | 0d9dfe9 | 2016-10-03 15:24:44 -0400 | [diff] [blame] | 52 | sk_sp<SkColorSpace> TestColorSpace(SkRandom*); |
Brian Osman | e2f732f | 2016-10-03 14:23:50 -0400 | [diff] [blame] | 53 | sk_sp<GrColorSpaceXform> TestColorXform(SkRandom*); |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 54 | |
Brian Osman | 9f532a3 | 2016-10-19 11:12:09 -0400 | [diff] [blame] | 55 | class TestAsFPArgs { |
| 56 | public: |
| 57 | TestAsFPArgs(GrProcessorTestData*); |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 58 | ~TestAsFPArgs(); |
Mike Reed | e3429e6 | 2018-01-19 11:43:34 -0500 | [diff] [blame] | 59 | const GrFPArgs& args() const { return fArgs; } |
Brian Osman | 9f532a3 | 2016-10-19 11:12:09 -0400 | [diff] [blame] | 60 | |
| 61 | private: |
Brian Osman | 9f532a3 | 2016-10-19 11:12:09 -0400 | [diff] [blame] | 62 | SkMatrix fViewMatrixStorage; |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 63 | std::unique_ptr<GrColorSpaceInfo> fColorSpaceInfoStorage; |
Mike Reed | 3bc266b | 2018-01-20 22:24:41 +0000 | [diff] [blame] | 64 | GrFPArgs fArgs; |
Brian Osman | 9f532a3 | 2016-10-19 11:12:09 -0400 | [diff] [blame] | 65 | }; |
| 66 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 67 | // We have a simplified dash path effect here to avoid relying on SkDashPathEffect which |
| 68 | // is in the optional build target effects. |
| 69 | class TestDashPathEffect : public SkPathEffect { |
| 70 | public: |
| 71 | static sk_sp<SkPathEffect> Make(const SkScalar* intervals, int count, SkScalar phase) { |
| 72 | return sk_sp<SkPathEffect>(new TestDashPathEffect(intervals, count, phase)); |
| 73 | } |
| 74 | |
| 75 | bool filterPath(SkPath* dst, const SkPath&, SkStrokeRec* , const SkRect*) const override; |
| 76 | DashType asADash(DashInfo* info) const override; |
| 77 | Factory getFactory() const override { return nullptr; } |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 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 |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 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)); |
robertphillips | 73add93 | 2016-04-07 09:01:20 -0700 | [diff] [blame] | 103 | GrColor color SK_INIT_TO_AVOID_WARNING; |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 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; |
joshualitt | 3f655f3 | 2015-04-29 10:01:22 -0700 | [diff] [blame] | 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); |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 123 | break; |
joshualitt | 3f655f3 | 2015-04-29 10:01:22 -0700 | [diff] [blame] | 124 | } |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 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)); |
robertphillips | 73add93 | 2016-04-07 09:01:20 -0700 | [diff] [blame] | 139 | uint8_t coverage SK_INIT_TO_AVOID_WARNING; |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 140 | switch (colorMode) { |
| 141 | case kZero_CoverageMode: |
| 142 | coverage = 0; |
tzik | bc7d235 | 2016-01-05 00:35:50 -0800 | [diff] [blame] | 143 | break; |
joshualitt | 4eaf9ce | 2015-04-28 13:31:18 -0700 | [diff] [blame] | 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 |
joshualitt | 3f655f3 | 2015-04-29 10:01:22 -0700 | [diff] [blame] | 155 | #endif |