blob: 9a1ae28dac8cdc335e07ccd9ae30a6ece23b964d [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"
Brian Salomon2bbdcc42017-09-07 12:36:34 -040017#include "GrSamplerState.h"
bsalomon6663acf2016-05-10 09:14:17 -070018#include "SkPathEffect.h"
joshualitt4eaf9ce2015-04-28 13:31:18 -070019#include "SkRandom.h"
Florin Malita4aed1382017-05-25 10:38:07 -040020#include "SkShaderBase.h"
joshualitt21279c72015-05-11 07:21:37 -070021#include "SkStrokeRec.h"
joshualitt4eaf9ce2015-04-28 13:31:18 -070022
Brian Salomon4cbb6e62017-10-25 15:12:19 -040023class GrColorSpaceInfo;
Brian Osmand49e9462017-10-16 13:17:48 -040024class GrColorSpaceXform;
Brian Osman9f532a32016-10-19 11:12:09 -040025struct GrProcessorTestData;
bsalomon6663acf2016-05-10 09:14:17 -070026class GrStyle;
joshualitt4eaf9ce2015-04-28 13:31:18 -070027class SkMatrix;
joshualitt40ded322015-05-02 07:07:17 -070028class SkPath;
joshualitt3e708c52015-04-30 13:49:27 -070029class SkRRect;
joshualitt3f655f32015-04-29 10:01:22 -070030struct SkRect;
joshualitt4eaf9ce2015-04-28 13:31:18 -070031
32namespace GrTest {
33/**
bsalomon6663acf2016-05-10 09:14:17 -070034 * Helpers for use in Test functions.
joshualitt4eaf9ce2015-04-28 13:31:18 -070035 */
36const SkMatrix& TestMatrix(SkRandom*);
joshualittfa2008f2015-04-29 11:32:05 -070037const SkMatrix& TestMatrixPreservesRightAngles(SkRandom*);
joshualitt3e708c52015-04-30 13:49:27 -070038const SkMatrix& TestMatrixRectStaysRect(SkRandom*);
joshualitt2fbd4062015-05-07 13:06:41 -070039const SkMatrix& TestMatrixInvertible(SkRandom*);
robertphillips01a19502016-07-06 09:58:57 -070040const SkMatrix& TestMatrixPerspective(SkRandom*);
Brian Salomon2bbdcc42017-09-07 12:36:34 -040041void TestWrapModes(SkRandom*, GrSamplerState::WrapMode[2]);
joshualitt3f655f32015-04-29 10:01:22 -070042const SkRect& TestRect(SkRandom*);
joshualitt6c891102015-05-13 08:51:49 -070043const SkRect& TestSquare(SkRandom*);
joshualitt3e708c52015-04-30 13:49:27 -070044const SkRRect& TestRRectSimple(SkRandom*);
joshualitt40ded322015-05-02 07:07:17 -070045const SkPath& TestPath(SkRandom*);
joshualitt8e5c1772015-05-11 08:58:52 -070046const SkPath& TestPathConvex(SkRandom*);
joshualitt21279c72015-05-11 07:21:37 -070047SkStrokeRec TestStrokeRec(SkRandom*);
bsalomon6663acf2016-05-10 09:14:17 -070048/** Creates styles with dash path effects and null path effects */
49void TestStyle(SkRandom*, GrStyle*);
Brian Osman0d9dfe92016-10-03 15:24:44 -040050sk_sp<SkColorSpace> TestColorSpace(SkRandom*);
Brian Osmane2f732f2016-10-03 14:23:50 -040051sk_sp<GrColorSpaceXform> TestColorXform(SkRandom*);
joshualitt4eaf9ce2015-04-28 13:31:18 -070052
Brian Osman9f532a32016-10-19 11:12:09 -040053class TestAsFPArgs {
54public:
55 TestAsFPArgs(GrProcessorTestData*);
Brian Salomon4cbb6e62017-10-25 15:12:19 -040056 ~TestAsFPArgs();
Florin Malita4aed1382017-05-25 10:38:07 -040057 const SkShaderBase::AsFPArgs& args() const { return fArgs; }
Brian Osman9f532a32016-10-19 11:12:09 -040058
59private:
Florin Malita4aed1382017-05-25 10:38:07 -040060 SkShaderBase::AsFPArgs fArgs;
Brian Osman9f532a32016-10-19 11:12:09 -040061 SkMatrix fViewMatrixStorage;
Brian Salomon4cbb6e62017-10-25 15:12:19 -040062 std::unique_ptr<GrColorSpaceInfo> fColorSpaceInfoStorage;
Brian Osman9f532a32016-10-19 11:12:09 -040063};
64
bsalomon6663acf2016-05-10 09:14:17 -070065// We have a simplified dash path effect here to avoid relying on SkDashPathEffect which
66// is in the optional build target effects.
67class TestDashPathEffect : public SkPathEffect {
68public:
69 static sk_sp<SkPathEffect> Make(const SkScalar* intervals, int count, SkScalar phase) {
70 return sk_sp<SkPathEffect>(new TestDashPathEffect(intervals, count, phase));
71 }
72
73 bool filterPath(SkPath* dst, const SkPath&, SkStrokeRec* , const SkRect*) const override;
74 DashType asADash(DashInfo* info) const override;
75 Factory getFactory() const override { return nullptr; }
76 void toString(SkString*) const override {}
77
78private:
79 TestDashPathEffect(const SkScalar* intervals, int count, SkScalar phase);
80
81 int fCount;
82 SkAutoTArray<SkScalar> fIntervals;
83 SkScalar fPhase;
84 SkScalar fInitialDashLength;
85 int fInitialDashIndex;
86 SkScalar fIntervalLength;
87};
88
89} // namespace GrTest
joshualitt4eaf9ce2015-04-28 13:31:18 -070090
91static inline GrColor GrRandomColor(SkRandom* random) {
92 // There are only a few cases of random colors which interest us
93 enum ColorMode {
94 kAllOnes_ColorMode,
95 kAllZeros_ColorMode,
96 kAlphaOne_ColorMode,
97 kRandom_ColorMode,
98 kLast_ColorMode = kRandom_ColorMode
99 };
100
101 ColorMode colorMode = ColorMode(random->nextULessThan(kLast_ColorMode + 1));
robertphillips73add932016-04-07 09:01:20 -0700102 GrColor color SK_INIT_TO_AVOID_WARNING;
joshualitt4eaf9ce2015-04-28 13:31:18 -0700103 switch (colorMode) {
104 case kAllOnes_ColorMode:
105 color = GrColorPackRGBA(0xFF, 0xFF, 0xFF, 0xFF);
106 break;
107 case kAllZeros_ColorMode:
108 color = GrColorPackRGBA(0, 0, 0, 0);
109 break;
110 case kAlphaOne_ColorMode:
111 color = GrColorPackRGBA(random->nextULessThan(256),
112 random->nextULessThan(256),
113 random->nextULessThan(256),
114 0xFF);
115 break;
joshualitt3f655f32015-04-29 10:01:22 -0700116 case kRandom_ColorMode: {
117 uint8_t alpha = random->nextULessThan(256);
118 color = GrColorPackRGBA(random->nextRangeU(0, alpha),
119 random->nextRangeU(0, alpha),
120 random->nextRangeU(0, alpha),
121 alpha);
joshualitt4eaf9ce2015-04-28 13:31:18 -0700122 break;
joshualitt3f655f32015-04-29 10:01:22 -0700123 }
joshualitt4eaf9ce2015-04-28 13:31:18 -0700124 }
125 GrColorIsPMAssert(color);
126 return color;
127}
128
129static inline uint8_t GrRandomCoverage(SkRandom* random) {
130 enum CoverageMode {
131 kZero_CoverageMode,
132 kAllOnes_CoverageMode,
133 kRandom_CoverageMode,
134 kLast_CoverageMode = kRandom_CoverageMode
135 };
136
137 CoverageMode colorMode = CoverageMode(random->nextULessThan(kLast_CoverageMode + 1));
robertphillips73add932016-04-07 09:01:20 -0700138 uint8_t coverage SK_INIT_TO_AVOID_WARNING;
joshualitt4eaf9ce2015-04-28 13:31:18 -0700139 switch (colorMode) {
140 case kZero_CoverageMode:
141 coverage = 0;
tzikbc7d2352016-01-05 00:35:50 -0800142 break;
joshualitt4eaf9ce2015-04-28 13:31:18 -0700143 case kAllOnes_CoverageMode:
144 coverage = 0xff;
145 break;
146 case kRandom_CoverageMode:
147 coverage = random->nextULessThan(256);
148 break;
149 }
150 return coverage;
151}
152
153#endif
joshualitt3f655f32015-04-29 10:01:22 -0700154#endif