blob: 64c766204393abac1b2d44aa3fd94ed5de0a1232 [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
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; }
bsalomon6663acf2016-05-10 09:14:17 -070078
79private:
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
joshualitt4eaf9ce2015-04-28 13:31:18 -070091
92static 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));
robertphillips73add932016-04-07 09:01:20 -0700103 GrColor color SK_INIT_TO_AVOID_WARNING;
joshualitt4eaf9ce2015-04-28 13:31:18 -0700104 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;
joshualitt3f655f32015-04-29 10:01:22 -0700117 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);
joshualitt4eaf9ce2015-04-28 13:31:18 -0700123 break;
joshualitt3f655f32015-04-29 10:01:22 -0700124 }
joshualitt4eaf9ce2015-04-28 13:31:18 -0700125 }
126 GrColorIsPMAssert(color);
127 return color;
128}
129
130static 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));
robertphillips73add932016-04-07 09:01:20 -0700139 uint8_t coverage SK_INIT_TO_AVOID_WARNING;
joshualitt4eaf9ce2015-04-28 13:31:18 -0700140 switch (colorMode) {
141 case kZero_CoverageMode:
142 coverage = 0;
tzikbc7d2352016-01-05 00:35:50 -0800143 break;
joshualitt4eaf9ce2015-04-28 13:31:18 -0700144 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
joshualitt3f655f32015-04-29 10:01:22 -0700155#endif