blob: fd9398d0c0d7ab6239dba344c465bec3aba8be49 [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
joshualitt4eaf9ce2015-04-28 13:31:18 -070015#include "GrColor.h"
Brian Osmane2f732f2016-10-03 14:23:50 -040016#include "GrColorSpaceXform.h"
bsalomon6663acf2016-05-10 09:14:17 -070017#include "SkPathEffect.h"
joshualitt4eaf9ce2015-04-28 13:31:18 -070018#include "SkRandom.h"
Florin Malita58a75642017-05-24 15:33:28 -040019#include "SkShaderBase.h"
joshualitt21279c72015-05-11 07:21:37 -070020#include "SkStrokeRec.h"
bsalomon6663acf2016-05-10 09:14:17 -070021#include "../private/SkTemplates.h"
joshualitt4eaf9ce2015-04-28 13:31:18 -070022
Brian Osman9f532a32016-10-19 11:12:09 -040023struct GrProcessorTestData;
bsalomon6663acf2016-05-10 09:14:17 -070024class GrStyle;
joshualitt4eaf9ce2015-04-28 13:31:18 -070025class SkMatrix;
joshualitt40ded322015-05-02 07:07:17 -070026class SkPath;
joshualitt3e708c52015-04-30 13:49:27 -070027class SkRRect;
joshualitt3f655f32015-04-29 10:01:22 -070028struct SkRect;
joshualitt4eaf9ce2015-04-28 13:31:18 -070029
30namespace GrTest {
31/**
bsalomon6663acf2016-05-10 09:14:17 -070032 * Helpers for use in Test functions.
joshualitt4eaf9ce2015-04-28 13:31:18 -070033 */
34const SkMatrix& TestMatrix(SkRandom*);
joshualittfa2008f2015-04-29 11:32:05 -070035const SkMatrix& TestMatrixPreservesRightAngles(SkRandom*);
joshualitt3e708c52015-04-30 13:49:27 -070036const SkMatrix& TestMatrixRectStaysRect(SkRandom*);
joshualitt2fbd4062015-05-07 13:06:41 -070037const SkMatrix& TestMatrixInvertible(SkRandom*);
robertphillips01a19502016-07-06 09:58:57 -070038const SkMatrix& TestMatrixPerspective(SkRandom*);
joshualitt3f655f32015-04-29 10:01:22 -070039const SkRect& TestRect(SkRandom*);
joshualitt6c891102015-05-13 08:51:49 -070040const SkRect& TestSquare(SkRandom*);
joshualitt3e708c52015-04-30 13:49:27 -070041const SkRRect& TestRRectSimple(SkRandom*);
joshualitt40ded322015-05-02 07:07:17 -070042const SkPath& TestPath(SkRandom*);
joshualitt8e5c1772015-05-11 08:58:52 -070043const SkPath& TestPathConvex(SkRandom*);
joshualitt21279c72015-05-11 07:21:37 -070044SkStrokeRec TestStrokeRec(SkRandom*);
bsalomon6663acf2016-05-10 09:14:17 -070045/** Creates styles with dash path effects and null path effects */
46void TestStyle(SkRandom*, GrStyle*);
Brian Osman0d9dfe92016-10-03 15:24:44 -040047sk_sp<SkColorSpace> TestColorSpace(SkRandom*);
Brian Osmane2f732f2016-10-03 14:23:50 -040048sk_sp<GrColorSpaceXform> TestColorXform(SkRandom*);
joshualitt4eaf9ce2015-04-28 13:31:18 -070049
Brian Osman9f532a32016-10-19 11:12:09 -040050class TestAsFPArgs {
51public:
52 TestAsFPArgs(GrProcessorTestData*);
Florin Malita58a75642017-05-24 15:33:28 -040053 const SkShaderBase::AsFPArgs& args() const { return fArgs; }
Brian Osman9f532a32016-10-19 11:12:09 -040054
55private:
Florin Malita58a75642017-05-24 15:33:28 -040056 SkShaderBase::AsFPArgs fArgs;
Brian Osman9f532a32016-10-19 11:12:09 -040057 SkMatrix fViewMatrixStorage;
58 sk_sp<SkColorSpace> fColorSpaceStorage;
59};
60
bsalomon6663acf2016-05-10 09:14:17 -070061// We have a simplified dash path effect here to avoid relying on SkDashPathEffect which
62// is in the optional build target effects.
63class TestDashPathEffect : public SkPathEffect {
64public:
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
74private:
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
joshualitt4eaf9ce2015-04-28 13:31:18 -070086
87static 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));
robertphillips73add932016-04-07 09:01:20 -070098 GrColor color SK_INIT_TO_AVOID_WARNING;
joshualitt4eaf9ce2015-04-28 13:31:18 -070099 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;
joshualitt3f655f32015-04-29 10:01:22 -0700112 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);
joshualitt4eaf9ce2015-04-28 13:31:18 -0700118 break;
joshualitt3f655f32015-04-29 10:01:22 -0700119 }
joshualitt4eaf9ce2015-04-28 13:31:18 -0700120 }
121 GrColorIsPMAssert(color);
122 return color;
123}
124
125static 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));
robertphillips73add932016-04-07 09:01:20 -0700134 uint8_t coverage SK_INIT_TO_AVOID_WARNING;
joshualitt4eaf9ce2015-04-28 13:31:18 -0700135 switch (colorMode) {
136 case kZero_CoverageMode:
137 coverage = 0;
tzikbc7d2352016-01-05 00:35:50 -0800138 break;
joshualitt4eaf9ce2015-04-28 13:31:18 -0700139 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
joshualitt3f655f32015-04-29 10:01:22 -0700150#endif