bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 8 | // This is a GPU-backend specific test. It relies on static intializers to work |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 9 | |
bsalomon@google.com | 2a48c3a | 2012-08-03 14:54:45 +0000 | [diff] [blame] | 10 | #include "SkTypes.h" |
| 11 | |
| 12 | #if SK_SUPPORT_GPU && SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 13 | |
bsalomon | 6f7f201 | 2015-03-16 14:00:52 -0700 | [diff] [blame] | 14 | #include "GrAutoLocaleSetter.h" |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 15 | #include "GrContextFactory.h" |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 16 | #include "GrContextPriv.h" |
Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame] | 17 | #include "GrDrawOpTest.h" |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 18 | #include "GrDrawingManager.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 19 | #include "GrInvariantOutput.h" |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 20 | #include "GrPipeline.h" |
Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame] | 21 | #include "GrRenderTargetContextPriv.h" |
bsalomon | 4061b12 | 2015-05-29 10:26:19 -0700 | [diff] [blame] | 22 | #include "GrResourceProvider.h" |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 23 | #include "GrTest.h" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 24 | #include "GrXferProcessor.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 25 | #include "SkChecksum.h" |
tfarina@chromium.org | 223137f | 2012-11-21 22:38:36 +0000 | [diff] [blame] | 26 | #include "SkRandom.h" |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 27 | #include "Test.h" |
joshualitt | 7441782 | 2015-08-07 11:42:16 -0700 | [diff] [blame] | 28 | |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 29 | #include "ops/GrDrawOp.h" |
joshualitt | 7441782 | 2015-08-07 11:42:16 -0700 | [diff] [blame] | 30 | |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 31 | #include "effects/GrConfigConversionEffect.h" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 32 | #include "effects/GrPorterDuffXferProcessor.h" |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 33 | #include "effects/GrXfermodeFragmentProcessor.h" |
joshualitt | 7441782 | 2015-08-07 11:42:16 -0700 | [diff] [blame] | 34 | |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 35 | #include "gl/GrGLGpu.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 36 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 37 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 38 | #include "glsl/GrGLSLProgramBuilder.h" |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 39 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 40 | /* |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 41 | * A dummy processor which just tries to insert a massive key and verify that it can retrieve the |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 42 | * whole thing correctly |
| 43 | */ |
| 44 | static const uint32_t kMaxKeySize = 1024; |
| 45 | |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 46 | class GLBigKeyProcessor : public GrGLSLFragmentProcessor { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 47 | public: |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 48 | void emitCode(EmitArgs& args) override { |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 49 | // pass through |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 50 | GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 51 | if (args.fInputColor) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 52 | fragBuilder->codeAppendf("%s = %s;\n", args.fOutputColor, args.fInputColor); |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 53 | } else { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 54 | fragBuilder->codeAppendf("%s = vec4(1.0);\n", args.fOutputColor); |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 55 | } |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 56 | } |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 57 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 58 | static void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder* b) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 59 | for (uint32_t i = 0; i < kMaxKeySize; i++) { |
| 60 | b->add32(i); |
joshualitt | d909759 | 2014-10-07 08:37:36 -0700 | [diff] [blame] | 61 | } |
joshualitt | d909759 | 2014-10-07 08:37:36 -0700 | [diff] [blame] | 62 | } |
| 63 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 64 | private: |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 65 | typedef GrGLSLFragmentProcessor INHERITED; |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 68 | class BigKeyProcessor : public GrFragmentProcessor { |
| 69 | public: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 70 | static sk_sp<GrFragmentProcessor> Make() { |
| 71 | return sk_sp<GrFragmentProcessor>(new BigKeyProcessor); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 72 | } |
| 73 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 74 | const char* name() const override { return "Big Ole Key"; } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 75 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 76 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
robertphillips | 9cdb992 | 2016-02-03 12:25:40 -0800 | [diff] [blame] | 77 | return new GLBigKeyProcessor; |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | private: |
| 81 | BigKeyProcessor() { |
| 82 | this->initClassID<BigKeyProcessor>(); |
| 83 | } |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 84 | virtual void onGetGLSLProcessorKey(const GrShaderCaps& caps, |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 85 | GrProcessorKeyBuilder* b) const override { |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 86 | GLBigKeyProcessor::GenKey(*this, caps, b); |
| 87 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 88 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
| 89 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override { } |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 90 | |
| 91 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 92 | |
| 93 | typedef GrFragmentProcessor INHERITED; |
| 94 | }; |
| 95 | |
| 96 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(BigKeyProcessor); |
| 97 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 98 | sk_sp<GrFragmentProcessor> BigKeyProcessor::TestCreate(GrProcessorTestData*) { |
| 99 | return BigKeyProcessor::Make(); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 100 | } |
| 101 | |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 102 | ////////////////////////////////////////////////////////////////////////////// |
| 103 | |
| 104 | class BlockInputFragmentProcessor : public GrFragmentProcessor { |
| 105 | public: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 106 | static sk_sp<GrFragmentProcessor> Make(sk_sp<GrFragmentProcessor> fp) { |
| 107 | return sk_sp<GrFragmentProcessor>(new BlockInputFragmentProcessor(fp)); |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | const char* name() const override { return "Block Input"; } |
| 111 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 112 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLFP; } |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 113 | |
| 114 | private: |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 115 | class GLFP : public GrGLSLFragmentProcessor { |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 116 | public: |
| 117 | void emitCode(EmitArgs& args) override { |
bsalomon | 38ddbad | 2015-09-24 06:00:00 -0700 | [diff] [blame] | 118 | this->emitChild(0, nullptr, args); |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | private: |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 122 | typedef GrGLSLFragmentProcessor INHERITED; |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 125 | BlockInputFragmentProcessor(sk_sp<GrFragmentProcessor> child) { |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 126 | this->initClassID<BlockInputFragmentProcessor>(); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 127 | this->registerChildProcessor(std::move(child)); |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 130 | void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override {} |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 131 | |
| 132 | bool onIsEqual(const GrFragmentProcessor&) const override { return true; } |
| 133 | |
| 134 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
| 135 | inout->setToOther(kRGBA_GrColorComponentFlags, GrColor_WHITE, |
| 136 | GrInvariantOutput::kWillNot_ReadInput); |
| 137 | this->childProcessor(0).computeInvariantOutput(inout); |
| 138 | } |
| 139 | |
| 140 | typedef GrFragmentProcessor INHERITED; |
| 141 | }; |
| 142 | |
| 143 | ////////////////////////////////////////////////////////////////////////////// |
| 144 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 145 | /* |
| 146 | * Begin test code |
| 147 | */ |
| 148 | static const int kRenderTargetHeight = 1; |
| 149 | static const int kRenderTargetWidth = 1; |
| 150 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 151 | static sk_sp<GrRenderTargetContext> random_render_target_context(GrContext* context, |
| 152 | SkRandom* random, |
| 153 | const GrCaps* caps) { |
robertphillips | 82ec6e5 | 2016-05-19 14:01:05 -0700 | [diff] [blame] | 154 | GrSurfaceOrigin origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin |
| 155 | : kBottomLeft_GrSurfaceOrigin; |
| 156 | int sampleCnt = random->nextBool() ? SkTMin(4, caps->maxSampleCount()) : 0; |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 157 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 158 | sk_sp<GrRenderTargetContext> renderTargetContext(context->makeRenderTargetContext( |
| 159 | SkBackingFit::kExact, |
| 160 | kRenderTargetWidth, |
| 161 | kRenderTargetHeight, |
| 162 | kRGBA_8888_GrPixelConfig, |
| 163 | nullptr, |
| 164 | sampleCnt, |
| 165 | origin)); |
| 166 | return renderTargetContext; |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 167 | } |
| 168 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 169 | static void set_random_xpf(GrPaint* paint, GrProcessorTestData* d) { |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 170 | paint->setXPFactory(GrXPFactoryTestFactory::Get(d)); |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 171 | } |
| 172 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 173 | static sk_sp<GrFragmentProcessor> create_random_proc_tree(GrProcessorTestData* d, |
| 174 | int minLevels, int maxLevels) { |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 175 | SkASSERT(1 <= minLevels); |
| 176 | SkASSERT(minLevels <= maxLevels); |
| 177 | |
| 178 | // Return a leaf node if maxLevels is 1 or if we randomly chose to terminate. |
| 179 | // If returning a leaf node, make sure that it doesn't have children (e.g. another |
| 180 | // GrComposeEffect) |
| 181 | const float terminateProbability = 0.3f; |
| 182 | if (1 == minLevels) { |
| 183 | bool terminate = (1 == maxLevels) || (d->fRandom->nextF() < terminateProbability); |
| 184 | if (terminate) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 185 | sk_sp<GrFragmentProcessor> fp; |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 186 | while (true) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 187 | fp = GrProcessorTestFactory<GrFragmentProcessor>::Make(d); |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 188 | SkASSERT(fp); |
| 189 | if (0 == fp->numChildProcessors()) { |
| 190 | break; |
| 191 | } |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 192 | } |
| 193 | return fp; |
| 194 | } |
| 195 | } |
| 196 | // If we didn't terminate, choose either the left or right subtree to fulfill |
| 197 | // the minLevels requirement of this tree; the other child can have as few levels as it wants. |
| 198 | // Also choose a random xfer mode that's supported by CreateFrom2Procs(). |
| 199 | if (minLevels > 1) { |
| 200 | --minLevels; |
| 201 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 202 | sk_sp<GrFragmentProcessor> minLevelsChild(create_random_proc_tree(d, minLevels, maxLevels - 1)); |
| 203 | sk_sp<GrFragmentProcessor> otherChild(create_random_proc_tree(d, 1, maxLevels - 1)); |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 204 | SkBlendMode mode = static_cast<SkBlendMode>(d->fRandom->nextRangeU(0, |
| 205 | (int)SkBlendMode::kLastCoeffMode)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 206 | sk_sp<GrFragmentProcessor> fp; |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 207 | if (d->fRandom->nextF() < 0.5f) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 208 | fp = GrXfermodeFragmentProcessor::MakeFromTwoProcessors(std::move(minLevelsChild), |
| 209 | std::move(otherChild), mode); |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 210 | SkASSERT(fp); |
| 211 | } else { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 212 | fp = GrXfermodeFragmentProcessor::MakeFromTwoProcessors(std::move(otherChild), |
| 213 | std::move(minLevelsChild), mode); |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 214 | SkASSERT(fp); |
| 215 | } |
| 216 | return fp; |
| 217 | } |
| 218 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 219 | static void set_random_color_coverage_stages(GrPaint* paint, |
| 220 | GrProcessorTestData* d, |
| 221 | int maxStages) { |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 222 | // Randomly choose to either create a linear pipeline of procs or create one proc tree |
| 223 | const float procTreeProbability = 0.5f; |
| 224 | if (d->fRandom->nextF() < procTreeProbability) { |
| 225 | // A full tree with 5 levels (31 nodes) may exceed the max allowed length of the gl |
| 226 | // processor key; maxTreeLevels should be a number from 1 to 4 inclusive. |
| 227 | const int maxTreeLevels = 4; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 228 | sk_sp<GrFragmentProcessor> fp(create_random_proc_tree(d, 2, maxTreeLevels)); |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 229 | paint->addColorFragmentProcessor(std::move(fp)); |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 230 | } else { |
| 231 | int numProcs = d->fRandom->nextULessThan(maxStages + 1); |
| 232 | int numColorProcs = d->fRandom->nextULessThan(numProcs + 1); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 233 | |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 234 | for (int s = 0; s < numProcs;) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 235 | sk_sp<GrFragmentProcessor> fp(GrProcessorTestFactory<GrFragmentProcessor>::Make(d)); |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 236 | SkASSERT(fp); |
| 237 | |
| 238 | // finally add the stage to the correct pipeline in the drawstate |
| 239 | if (s < numColorProcs) { |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 240 | paint->addColorFragmentProcessor(std::move(fp)); |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 241 | } else { |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 242 | paint->addCoverageFragmentProcessor(std::move(fp)); |
wangyix | 059dffa | 2015-09-10 06:57:05 -0700 | [diff] [blame] | 243 | } |
| 244 | ++s; |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 245 | } |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 249 | static bool set_random_state(GrPaint* paint, SkRandom* random) { |
| 250 | if (random->nextBool()) { |
| 251 | paint->setDisableOutputConversionToSRGB(true); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 252 | } |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 253 | if (random->nextBool()) { |
| 254 | paint->setAllowSRGBInputs(true); |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 255 | } |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 256 | return random->nextBool(); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 257 | } |
| 258 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 259 | // right now, the only thing we seem to care about in drawState's stencil is 'doesWrite()' |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 260 | static const GrUserStencilSettings* get_random_stencil(SkRandom* random) { |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 261 | static constexpr GrUserStencilSettings kDoesWriteStencil( |
| 262 | GrUserStencilSettings::StaticInit< |
| 263 | 0xffff, |
| 264 | GrUserStencilTest::kAlways, |
| 265 | 0xffff, |
| 266 | GrUserStencilOp::kReplace, |
| 267 | GrUserStencilOp::kReplace, |
| 268 | 0xffff>() |
| 269 | ); |
| 270 | static constexpr GrUserStencilSettings kDoesNotWriteStencil( |
| 271 | GrUserStencilSettings::StaticInit< |
| 272 | 0xffff, |
| 273 | GrUserStencilTest::kNever, |
| 274 | 0xffff, |
| 275 | GrUserStencilOp::kKeep, |
| 276 | GrUserStencilOp::kKeep, |
| 277 | 0xffff>() |
| 278 | ); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 279 | |
| 280 | if (random->nextBool()) { |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 281 | return &kDoesWriteStencil; |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 282 | } else { |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 283 | return &kDoesNotWriteStencil; |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 286 | |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 287 | bool GrDrawingManager::ProgramUnitTest(GrContext* context, int maxStages) { |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 288 | GrDrawingManager* drawingManager = context->contextPriv().drawingManager(); |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 289 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 290 | // setup dummy textures |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 291 | GrSurfaceDesc dummyDesc; |
bsalomon | bea0150 | 2015-07-16 10:00:28 -0700 | [diff] [blame] | 292 | dummyDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
Brian Osman | 777b563 | 2016-10-14 09:16:21 -0400 | [diff] [blame] | 293 | dummyDesc.fConfig = kRGBA_8888_GrPixelConfig; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 294 | dummyDesc.fWidth = 34; |
| 295 | dummyDesc.fHeight = 18; |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 296 | sk_sp<GrTexture> dummyTexture1( |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 297 | context->textureProvider()->createTexture(dummyDesc, SkBudgeted::kNo, nullptr, 0)); |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 298 | dummyDesc.fFlags = kNone_GrSurfaceFlags; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 299 | dummyDesc.fConfig = kAlpha_8_GrPixelConfig; |
| 300 | dummyDesc.fWidth = 16; |
| 301 | dummyDesc.fHeight = 22; |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 302 | sk_sp<GrTexture> dummyTexture2( |
bsalomon | 5ec26ae | 2016-02-25 08:33:02 -0800 | [diff] [blame] | 303 | context->textureProvider()->createTexture(dummyDesc, SkBudgeted::kNo, nullptr, 0)); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 304 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 305 | if (!dummyTexture1 || ! dummyTexture2) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 306 | SkDebugf("Could not allocate dummy textures"); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 307 | return false; |
| 308 | } |
| 309 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 310 | GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()}; |
| 311 | |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 312 | // dummy scissor state |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 313 | GrScissorState scissor; |
joshualitt | 54e0c12 | 2014-11-19 09:38:51 -0800 | [diff] [blame] | 314 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 315 | SkRandom random; |
bsalomon | f3261af | 2016-04-05 10:57:13 -0700 | [diff] [blame] | 316 | static const int NUM_TESTS = 1024; |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 317 | for (int t = 0; t < NUM_TESTS; t++) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 318 | // setup random render target(can fail) |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 319 | sk_sp<GrRenderTargetContext> renderTargetContext(random_render_target_context( |
| 320 | context, &random, context->caps())); |
| 321 | if (!renderTargetContext) { |
| 322 | SkDebugf("Could not allocate renderTargetContext"); |
joshualitt | f5883a6 | 2016-01-13 07:47:38 -0800 | [diff] [blame] | 323 | return false; |
| 324 | } |
robertphillips | 0dfa62c | 2015-11-16 06:23:31 -0800 | [diff] [blame] | 325 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 326 | GrPaint grPaint; |
robertphillips | 82ec6e5 | 2016-05-19 14:01:05 -0700 | [diff] [blame] | 327 | |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 328 | std::unique_ptr<GrDrawOp> op(GrRandomDrawOp(&random, context)); |
Brian Salomon | 1951f3d | 2016-12-09 16:07:12 -0500 | [diff] [blame] | 329 | SkASSERT(op); |
robertphillips | 82ec6e5 | 2016-05-19 14:01:05 -0700 | [diff] [blame] | 330 | |
| 331 | GrProcessorTestData ptd(&random, context, context->caps(), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 332 | renderTargetContext.get(), dummyTextures); |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 333 | set_random_color_coverage_stages(&grPaint, &ptd, maxStages); |
| 334 | set_random_xpf(&grPaint, &ptd); |
| 335 | bool snapToCenters = set_random_state(&grPaint, &random); |
| 336 | const GrUserStencilSettings* uss = get_random_stencil(&random); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 337 | // We don't use kHW because we will hit an assertion if the render target is not |
| 338 | // multisampled |
| 339 | static constexpr GrAAType kAATypes[] = {GrAAType::kNone, GrAAType::kCoverage}; |
| 340 | GrAAType aaType = kAATypes[random.nextULessThan(SK_ARRAY_COUNT(kAATypes))]; |
robertphillips | 82ec6e5 | 2016-05-19 14:01:05 -0700 | [diff] [blame] | 341 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 342 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(grPaint), aaType, std::move(op), |
| 343 | uss, snapToCenters); |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 344 | } |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 345 | // Flush everything, test passes if flush is successful(ie, no asserts are hit, no crashes) |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 346 | drawingManager->flush(); |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 347 | |
| 348 | // Validate that GrFPs work correctly without an input. |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 349 | sk_sp<GrRenderTargetContext> renderTargetContext(context->makeRenderTargetContext( |
| 350 | SkBackingFit::kExact, |
| 351 | kRenderTargetWidth, |
| 352 | kRenderTargetHeight, |
| 353 | kRGBA_8888_GrPixelConfig, |
| 354 | nullptr)); |
| 355 | if (!renderTargetContext) { |
| 356 | SkDebugf("Could not allocate a renderTargetContext"); |
robertphillips | 82ec6e5 | 2016-05-19 14:01:05 -0700 | [diff] [blame] | 357 | return false; |
| 358 | } |
| 359 | |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 360 | int fpFactoryCnt = GrProcessorTestFactory<GrFragmentProcessor>::Count(); |
| 361 | for (int i = 0; i < fpFactoryCnt; ++i) { |
| 362 | // Since FP factories internally randomize, call each 10 times. |
| 363 | for (int j = 0; j < 10; ++j) { |
Brian Salomon | f833478 | 2017-01-03 09:42:58 -0500 | [diff] [blame] | 364 | std::unique_ptr<GrDrawOp> op(GrRandomDrawOp(&random, context)); |
Brian Salomon | 1951f3d | 2016-12-09 16:07:12 -0500 | [diff] [blame] | 365 | SkASSERT(op); |
robertphillips | 82ec6e5 | 2016-05-19 14:01:05 -0700 | [diff] [blame] | 366 | GrProcessorTestData ptd(&random, context, context->caps(), |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 367 | renderTargetContext.get(), dummyTextures); |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 368 | GrPaint grPaint; |
Brian Salomon | a163392 | 2017-01-09 11:46:10 -0500 | [diff] [blame] | 369 | grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc)); |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 370 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 371 | sk_sp<GrFragmentProcessor> fp( |
| 372 | GrProcessorTestFactory<GrFragmentProcessor>::MakeIdx(i, &ptd)); |
| 373 | sk_sp<GrFragmentProcessor> blockFP( |
| 374 | BlockInputFragmentProcessor::Make(std::move(fp))); |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 375 | grPaint.addColorFragmentProcessor(std::move(blockFP)); |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 376 | |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 377 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(grPaint), GrAAType::kNone, |
Brian Salomon | 1951f3d | 2016-12-09 16:07:12 -0500 | [diff] [blame] | 378 | std::move(op)); |
robertphillips | a13e202 | 2015-11-11 12:01:09 -0800 | [diff] [blame] | 379 | drawingManager->flush(); |
bsalomon | b5b6032 | 2015-09-14 12:26:33 -0700 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 383 | return true; |
| 384 | } |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 385 | |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 386 | static int get_glprograms_max_stages(GrContext* context) { |
| 387 | GrGLGpu* gpu = static_cast<GrGLGpu*>(context->getGpu()); |
| 388 | /* |
| 389 | * For the time being, we only support the test with desktop GL or for android on |
| 390 | * ARM platforms |
| 391 | * TODO When we run ES 3.00 GLSL in more places, test again |
| 392 | */ |
| 393 | if (kGL_GrGLStandard == gpu->glStandard() || |
| 394 | kARM_GrGLVendor == gpu->ctxInfo().vendor()) { |
| 395 | return 6; |
| 396 | } else if (kTegra3_GrGLRenderer == gpu->ctxInfo().renderer() || |
| 397 | kOther_GrGLRenderer == gpu->ctxInfo().renderer()) { |
| 398 | return 1; |
| 399 | } |
| 400 | return 0; |
| 401 | } |
| 402 | |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 403 | static void test_glprograms_native(skiatest::Reporter* reporter, |
| 404 | const sk_gpu_test::ContextInfo& ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 405 | int maxStages = get_glprograms_max_stages(ctxInfo.grContext()); |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 406 | if (maxStages == 0) { |
| 407 | return; |
| 408 | } |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 409 | REPORTER_ASSERT(reporter, GrDrawingManager::ProgramUnitTest(ctxInfo.grContext(), maxStages)); |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 410 | } |
| 411 | |
bsalomon | f2f1c17 | 2016-04-05 12:59:06 -0700 | [diff] [blame] | 412 | static void test_glprograms_other_contexts( |
| 413 | skiatest::Reporter* reporter, |
| 414 | const sk_gpu_test::ContextInfo& ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 415 | int maxStages = get_glprograms_max_stages(ctxInfo.grContext()); |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 416 | #ifdef SK_BUILD_FOR_WIN |
| 417 | // Some long shaders run out of temporary registers in the D3D compiler on ANGLE and |
| 418 | // command buffer. |
| 419 | maxStages = SkTMin(maxStages, 2); |
| 420 | #endif |
| 421 | if (maxStages == 0) { |
| 422 | return; |
| 423 | } |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 424 | REPORTER_ASSERT(reporter, GrDrawingManager::ProgramUnitTest(ctxInfo.grContext(), maxStages)); |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 425 | } |
| 426 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 427 | static bool is_native_gl_context_type(sk_gpu_test::GrContextFactory::ContextType type) { |
| 428 | return type == sk_gpu_test::GrContextFactory::kNativeGL_ContextType; |
| 429 | } |
| 430 | |
| 431 | static bool is_other_rendering_gl_context_type(sk_gpu_test::GrContextFactory::ContextType type) { |
| 432 | return !is_native_gl_context_type(type) && |
| 433 | kOpenGL_GrBackend == sk_gpu_test::GrContextFactory::ContextTypeBackend(type) && |
| 434 | sk_gpu_test::GrContextFactory::IsRenderingContext(type); |
| 435 | } |
| 436 | |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 437 | DEF_GPUTEST(GLPrograms, reporter, /*factory*/) { |
bsalomon | 3318ee7 | 2015-03-16 11:56:29 -0700 | [diff] [blame] | 438 | // Set a locale that would cause shader compilation to fail because of , as decimal separator. |
| 439 | // skbug 3330 |
| 440 | #ifdef SK_BUILD_FOR_WIN |
| 441 | GrAutoLocaleSetter als("sv-SE"); |
| 442 | #else |
| 443 | GrAutoLocaleSetter als("sv_SE.UTF-8"); |
| 444 | #endif |
| 445 | |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 446 | // We suppress prints to avoid spew |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 447 | GrContextOptions opts; |
joshualitt | 6c89110 | 2015-05-13 08:51:49 -0700 | [diff] [blame] | 448 | opts.fSuppressPrints = true; |
bsalomon | 3724e57 | 2016-03-30 18:56:19 -0700 | [diff] [blame] | 449 | sk_gpu_test::GrContextFactory debugFactory(opts); |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 450 | skiatest::RunWithGPUTestContexts(test_glprograms_native, &is_native_gl_context_type, |
kkinnunen | 3e980c3 | 2015-12-23 01:33:00 -0800 | [diff] [blame] | 451 | reporter, &debugFactory); |
| 452 | skiatest::RunWithGPUTestContexts(test_glprograms_other_contexts, |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 453 | &is_other_rendering_gl_context_type, reporter, &debugFactory); |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 454 | } |
| 455 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 456 | #endif |