blob: 79c85408a2bc8923c8cd114776a1e25f446e84e1 [file] [log] [blame]
joshualitt4eaf9ce2015-04-28 13:31:18 -07001/*
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
joshualitt3f655f32015-04-29 10:01:22 -070011#include "SkTypes.h"
12
Hal Canary6f6961e2017-01-31 13:50:44 -050013#if GR_TEST_UTILS
joshualitt3f655f32015-04-29 10:01:22 -070014
Brian Salomon2bbdcc42017-09-07 12:36:34 -040015#include "../private/SkTemplates.h"
joshualitt4eaf9ce2015-04-28 13:31:18 -070016#include "GrColor.h"
Mike Reede3429e62018-01-19 11:43:34 -050017#include "GrFPArgs.h"
Brian Salomon2bbdcc42017-09-07 12:36:34 -040018#include "GrSamplerState.h"
Hal Canary50dbc092018-06-12 14:50:37 -040019#include "SkMacros.h"
bsalomon6663acf2016-05-10 09:14:17 -070020#include "SkPathEffect.h"
joshualitt4eaf9ce2015-04-28 13:31:18 -070021#include "SkRandom.h"
Florin Malita4aed1382017-05-25 10:38:07 -040022#include "SkShaderBase.h"
joshualitt21279c72015-05-11 07:21:37 -070023#include "SkStrokeRec.h"
joshualitt4eaf9ce2015-04-28 13:31:18 -070024
Brian Salomon4cbb6e62017-10-25 15:12:19 -040025class GrColorSpaceInfo;
Brian Osmand49e9462017-10-16 13:17:48 -040026class GrColorSpaceXform;
Brian Osman9f532a32016-10-19 11:12:09 -040027struct GrProcessorTestData;
bsalomon6663acf2016-05-10 09:14:17 -070028class GrStyle;
joshualitt4eaf9ce2015-04-28 13:31:18 -070029class SkMatrix;
joshualitt40ded322015-05-02 07:07:17 -070030class SkPath;
joshualitt3e708c52015-04-30 13:49:27 -070031class SkRRect;
joshualitt3f655f32015-04-29 10:01:22 -070032struct SkRect;
joshualitt4eaf9ce2015-04-28 13:31:18 -070033
34namespace GrTest {
35/**
bsalomon6663acf2016-05-10 09:14:17 -070036 * Helpers for use in Test functions.
joshualitt4eaf9ce2015-04-28 13:31:18 -070037 */
38const SkMatrix& TestMatrix(SkRandom*);
joshualittfa2008f2015-04-29 11:32:05 -070039const SkMatrix& TestMatrixPreservesRightAngles(SkRandom*);
joshualitt3e708c52015-04-30 13:49:27 -070040const SkMatrix& TestMatrixRectStaysRect(SkRandom*);
joshualitt2fbd4062015-05-07 13:06:41 -070041const SkMatrix& TestMatrixInvertible(SkRandom*);
robertphillips01a19502016-07-06 09:58:57 -070042const SkMatrix& TestMatrixPerspective(SkRandom*);
Brian Salomon2bbdcc42017-09-07 12:36:34 -040043void TestWrapModes(SkRandom*, GrSamplerState::WrapMode[2]);
joshualitt3f655f32015-04-29 10:01:22 -070044const SkRect& TestRect(SkRandom*);
joshualitt6c891102015-05-13 08:51:49 -070045const SkRect& TestSquare(SkRandom*);
joshualitt3e708c52015-04-30 13:49:27 -070046const SkRRect& TestRRectSimple(SkRandom*);
joshualitt40ded322015-05-02 07:07:17 -070047const SkPath& TestPath(SkRandom*);
joshualitt8e5c1772015-05-11 08:58:52 -070048const SkPath& TestPathConvex(SkRandom*);
joshualitt21279c72015-05-11 07:21:37 -070049SkStrokeRec TestStrokeRec(SkRandom*);
bsalomon6663acf2016-05-10 09:14:17 -070050/** Creates styles with dash path effects and null path effects */
51void TestStyle(SkRandom*, GrStyle*);
Brian Osman0d9dfe92016-10-03 15:24:44 -040052sk_sp<SkColorSpace> TestColorSpace(SkRandom*);
Brian Osmane2f732f2016-10-03 14:23:50 -040053sk_sp<GrColorSpaceXform> TestColorXform(SkRandom*);
joshualitt4eaf9ce2015-04-28 13:31:18 -070054
Brian Osman9f532a32016-10-19 11:12:09 -040055class TestAsFPArgs {
56public:
57 TestAsFPArgs(GrProcessorTestData*);
Brian Salomon4cbb6e62017-10-25 15:12:19 -040058 ~TestAsFPArgs();
Mike Reede3429e62018-01-19 11:43:34 -050059 const GrFPArgs& args() const { return fArgs; }
Brian Osman9f532a32016-10-19 11:12:09 -040060
61private:
Brian Osman9f532a32016-10-19 11:12:09 -040062 SkMatrix fViewMatrixStorage;
Brian Salomon4cbb6e62017-10-25 15:12:19 -040063 std::unique_ptr<GrColorSpaceInfo> fColorSpaceInfoStorage;
Mike Reed3bc266b2018-01-20 22:24:41 +000064 GrFPArgs fArgs;
Brian Osman9f532a32016-10-19 11:12:09 -040065};
66
bsalomon6663acf2016-05-10 09:14:17 -070067// We have a simplified dash path effect here to avoid relying on SkDashPathEffect which
68// is in the optional build target effects.
69class TestDashPathEffect : public SkPathEffect {
70public:
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
bsalomon6663acf2016-05-10 09:14:17 -070075 Factory getFactory() const override { return nullptr; }
Mike Klein4fee3232018-10-18 17:27:16 -040076 const char* getTypeName() const override { return nullptr; }
bsalomon6663acf2016-05-10 09:14:17 -070077
Mike Reed6d10f8b2018-08-16 13:22:16 -040078protected:
79 bool onFilterPath(SkPath* dst, const SkPath&, SkStrokeRec* , const SkRect*) const override;
80 DashType onAsADash(DashInfo* info) const override;
81
bsalomon6663acf2016-05-10 09:14:17 -070082private:
83 TestDashPathEffect(const SkScalar* intervals, int count, SkScalar phase);
84
85 int fCount;
86 SkAutoTArray<SkScalar> fIntervals;
87 SkScalar fPhase;
88 SkScalar fInitialDashLength;
89 int fInitialDashIndex;
90 SkScalar fIntervalLength;
91};
92
93} // namespace GrTest
joshualitt4eaf9ce2015-04-28 13:31:18 -070094
95static inline GrColor GrRandomColor(SkRandom* random) {
96 // There are only a few cases of random colors which interest us
97 enum ColorMode {
98 kAllOnes_ColorMode,
99 kAllZeros_ColorMode,
100 kAlphaOne_ColorMode,
101 kRandom_ColorMode,
102 kLast_ColorMode = kRandom_ColorMode
103 };
104
105 ColorMode colorMode = ColorMode(random->nextULessThan(kLast_ColorMode + 1));
robertphillips73add932016-04-07 09:01:20 -0700106 GrColor color SK_INIT_TO_AVOID_WARNING;
joshualitt4eaf9ce2015-04-28 13:31:18 -0700107 switch (colorMode) {
108 case kAllOnes_ColorMode:
109 color = GrColorPackRGBA(0xFF, 0xFF, 0xFF, 0xFF);
110 break;
111 case kAllZeros_ColorMode:
112 color = GrColorPackRGBA(0, 0, 0, 0);
113 break;
114 case kAlphaOne_ColorMode:
115 color = GrColorPackRGBA(random->nextULessThan(256),
116 random->nextULessThan(256),
117 random->nextULessThan(256),
118 0xFF);
119 break;
joshualitt3f655f32015-04-29 10:01:22 -0700120 case kRandom_ColorMode: {
121 uint8_t alpha = random->nextULessThan(256);
122 color = GrColorPackRGBA(random->nextRangeU(0, alpha),
123 random->nextRangeU(0, alpha),
124 random->nextRangeU(0, alpha),
125 alpha);
joshualitt4eaf9ce2015-04-28 13:31:18 -0700126 break;
joshualitt3f655f32015-04-29 10:01:22 -0700127 }
joshualitt4eaf9ce2015-04-28 13:31:18 -0700128 }
joshualitt4eaf9ce2015-04-28 13:31:18 -0700129 return color;
130}
131
132static inline uint8_t GrRandomCoverage(SkRandom* random) {
133 enum CoverageMode {
134 kZero_CoverageMode,
135 kAllOnes_CoverageMode,
136 kRandom_CoverageMode,
137 kLast_CoverageMode = kRandom_CoverageMode
138 };
139
140 CoverageMode colorMode = CoverageMode(random->nextULessThan(kLast_CoverageMode + 1));
robertphillips73add932016-04-07 09:01:20 -0700141 uint8_t coverage SK_INIT_TO_AVOID_WARNING;
joshualitt4eaf9ce2015-04-28 13:31:18 -0700142 switch (colorMode) {
143 case kZero_CoverageMode:
144 coverage = 0;
tzikbc7d2352016-01-05 00:35:50 -0800145 break;
joshualitt4eaf9ce2015-04-28 13:31:18 -0700146 case kAllOnes_CoverageMode:
147 coverage = 0xff;
148 break;
149 case kRandom_CoverageMode:
150 coverage = random->nextULessThan(256);
151 break;
152 }
153 return coverage;
154}
155
156#endif
joshualitt3f655f32015-04-29 10:01:22 -0700157#endif