blob: 3933e649d8d6c2a9f9a6657f44fd979da3614b39 [file] [log] [blame]
bsalomon@google.coma8e686e2011-08-16 15:45:58 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
bsalomon@google.comd4726202012-08-03 14:34:46 +00009// This is a GPU-backend specific test. It relies on static intializers to work
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +000010
bsalomon@google.com2a48c3a2012-08-03 14:54:45 +000011#include "SkTypes.h"
12
13#if SK_SUPPORT_GPU && SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
14
bsalomon6f7f2012015-03-16 14:00:52 -070015#include "GrAutoLocaleSetter.h"
joshualitt6c891102015-05-13 08:51:49 -070016#include "GrBatchTest.h"
bsalomon@google.com67b915d2013-02-04 16:13:32 +000017#include "GrContextFactory.h"
joshualittf5883a62016-01-13 07:47:38 -080018#include "GrDrawContext.h"
robertphillipsa13e2022015-11-11 12:01:09 -080019#include "GrDrawingManager.h"
egdaniel605dd0f2014-11-12 08:35:25 -080020#include "GrInvariantOutput.h"
egdaniel8dd688b2015-01-22 10:16:09 -080021#include "GrPipeline.h"
bsalomon4061b122015-05-29 10:26:19 -070022#include "GrResourceProvider.h"
joshualitt2c93efe2014-11-06 12:57:13 -080023#include "GrTest.h"
egdaniel95131432014-12-09 11:15:43 -080024#include "GrXferProcessor.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000025#include "SkChecksum.h"
tfarina@chromium.org223137f2012-11-21 22:38:36 +000026#include "SkRandom.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000027#include "Test.h"
joshualitt74417822015-08-07 11:42:16 -070028
bsalomon16b99132015-08-13 14:55:50 -070029#include "batches/GrDrawBatch.h"
joshualitt74417822015-08-07 11:42:16 -070030
joshualitt2c93efe2014-11-06 12:57:13 -080031#include "effects/GrConfigConversionEffect.h"
egdaniel95131432014-12-09 11:15:43 -080032#include "effects/GrPorterDuffXferProcessor.h"
wangyix059dffa2015-09-10 06:57:05 -070033#include "effects/GrXfermodeFragmentProcessor.h"
joshualitt74417822015-08-07 11:42:16 -070034
jvanverth39edf762014-12-22 11:44:19 -080035#include "gl/GrGLGpu.h"
egdaniel64c47282015-11-13 06:54:19 -080036#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080037#include "glsl/GrGLSLFragmentShaderBuilder.h"
38#include "glsl/GrGLSLProgramBuilder.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000039
joshualitt65171342014-10-09 07:25:36 -070040/*
bsalomon98b33eb2014-10-15 11:05:26 -070041 * A dummy processor which just tries to insert a massive key and verify that it can retrieve the
joshualitt65171342014-10-09 07:25:36 -070042 * whole thing correctly
43 */
44static const uint32_t kMaxKeySize = 1024;
45
egdaniel64c47282015-11-13 06:54:19 -080046class GLBigKeyProcessor : public GrGLSLFragmentProcessor {
joshualitt65171342014-10-09 07:25:36 -070047public:
joshualitteb2a6762014-12-04 11:35:33 -080048 GLBigKeyProcessor(const GrProcessor&) {}
joshualittd9097592014-10-07 08:37:36 -070049
wangyix7c157a92015-07-22 15:08:53 -070050 virtual void emitCode(EmitArgs& args) override {
joshualitt6c891102015-05-13 08:51:49 -070051 // pass through
egdaniel4ca2e602015-11-18 08:01:26 -080052 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
bsalomonb5b60322015-09-14 12:26:33 -070053 if (args.fInputColor) {
egdaniel4ca2e602015-11-18 08:01:26 -080054 fragBuilder->codeAppendf("%s = %s;\n", args.fOutputColor, args.fInputColor);
bsalomonb5b60322015-09-14 12:26:33 -070055 } else {
egdaniel4ca2e602015-11-18 08:01:26 -080056 fragBuilder->codeAppendf("%s = vec4(1.0);\n", args.fOutputColor);
bsalomonb5b60322015-09-14 12:26:33 -070057 }
joshualitt6c891102015-05-13 08:51:49 -070058 }
joshualitt65171342014-10-09 07:25:36 -070059
jvanverthcfc18862015-04-28 08:48:20 -070060 static void GenKey(const GrProcessor& processor, const GrGLSLCaps&, GrProcessorKeyBuilder* b) {
joshualitt65171342014-10-09 07:25:36 -070061 for (uint32_t i = 0; i < kMaxKeySize; i++) {
62 b->add32(i);
joshualittd9097592014-10-07 08:37:36 -070063 }
joshualittd9097592014-10-07 08:37:36 -070064 }
65
joshualitt65171342014-10-09 07:25:36 -070066private:
egdaniel64c47282015-11-13 06:54:19 -080067 typedef GrGLSLFragmentProcessor INHERITED;
joshualitt65171342014-10-09 07:25:36 -070068};
69
joshualitteb2a6762014-12-04 11:35:33 -080070class BigKeyProcessor : public GrFragmentProcessor {
71public:
72 static GrFragmentProcessor* Create() {
bsalomon02141732015-10-21 06:57:30 -070073 return new BigKeyProcessor;
joshualitteb2a6762014-12-04 11:35:33 -080074 }
75
mtklein36352bf2015-03-25 18:17:31 -070076 const char* name() const override { return "Big Ole Key"; }
joshualitteb2a6762014-12-04 11:35:33 -080077
egdaniel57d3b032015-11-13 11:57:27 -080078 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
halcanary385fe4d2015-08-26 13:07:48 -070079 return new GLBigKeyProcessor(*this);
joshualitteb2a6762014-12-04 11:35:33 -080080 }
81
82private:
83 BigKeyProcessor() {
84 this->initClassID<BigKeyProcessor>();
85 }
egdaniel57d3b032015-11-13 11:57:27 -080086 virtual void onGetGLSLProcessorKey(const GrGLSLCaps& caps,
87 GrProcessorKeyBuilder* b) const override {
wangyix4b3050b2015-08-04 07:59:37 -070088 GLBigKeyProcessor::GenKey(*this, caps, b);
89 }
mtklein36352bf2015-03-25 18:17:31 -070090 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
91 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { }
joshualitteb2a6762014-12-04 11:35:33 -080092
93 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
94
95 typedef GrFragmentProcessor INHERITED;
96};
97
98GR_DEFINE_FRAGMENT_PROCESSOR_TEST(BigKeyProcessor);
99
bsalomonc21b09e2015-08-28 18:46:56 -0700100const GrFragmentProcessor* BigKeyProcessor::TestCreate(GrProcessorTestData*) {
joshualitteb2a6762014-12-04 11:35:33 -0800101 return BigKeyProcessor::Create();
102}
103
bsalomonb5b60322015-09-14 12:26:33 -0700104//////////////////////////////////////////////////////////////////////////////
105
106class BlockInputFragmentProcessor : public GrFragmentProcessor {
107public:
108 static GrFragmentProcessor* Create(const GrFragmentProcessor* fp) {
109 return new BlockInputFragmentProcessor(fp);
110 }
111
112 const char* name() const override { return "Block Input"; }
113
egdaniel57d3b032015-11-13 11:57:27 -0800114 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLFP; }
bsalomonb5b60322015-09-14 12:26:33 -0700115
116private:
egdaniel64c47282015-11-13 06:54:19 -0800117 class GLFP : public GrGLSLFragmentProcessor {
bsalomonb5b60322015-09-14 12:26:33 -0700118 public:
119 void emitCode(EmitArgs& args) override {
bsalomon38ddbad2015-09-24 06:00:00 -0700120 this->emitChild(0, nullptr, args);
bsalomonb5b60322015-09-14 12:26:33 -0700121 }
122
123 private:
egdaniel64c47282015-11-13 06:54:19 -0800124 typedef GrGLSLFragmentProcessor INHERITED;
bsalomonb5b60322015-09-14 12:26:33 -0700125 };
126
127 BlockInputFragmentProcessor(const GrFragmentProcessor* child) {
128 this->initClassID<BlockInputFragmentProcessor>();
129 this->registerChildProcessor(child);
130 }
131
egdaniel57d3b032015-11-13 11:57:27 -0800132 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {}
bsalomonb5b60322015-09-14 12:26:33 -0700133
134 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
135
136 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
137 inout->setToOther(kRGBA_GrColorComponentFlags, GrColor_WHITE,
138 GrInvariantOutput::kWillNot_ReadInput);
139 this->childProcessor(0).computeInvariantOutput(inout);
140 }
141
142 typedef GrFragmentProcessor INHERITED;
143};
144
145//////////////////////////////////////////////////////////////////////////////
146
joshualitt65171342014-10-09 07:25:36 -0700147/*
148 * Begin test code
149 */
150static const int kRenderTargetHeight = 1;
151static const int kRenderTargetWidth = 1;
152
bsalomon4061b122015-05-29 10:26:19 -0700153static GrRenderTarget* random_render_target(GrTextureProvider* textureProvider, SkRandom* random,
bsalomon4b91f762015-05-19 09:29:46 -0700154 const GrCaps* caps) {
joshualitt65171342014-10-09 07:25:36 -0700155 // setup render target
156 GrTextureParams params;
bsalomonf2703d82014-10-28 14:33:06 -0700157 GrSurfaceDesc texDesc;
joshualitt65171342014-10-09 07:25:36 -0700158 texDesc.fWidth = kRenderTargetWidth;
159 texDesc.fHeight = kRenderTargetHeight;
bsalomonf2703d82014-10-28 14:33:06 -0700160 texDesc.fFlags = kRenderTarget_GrSurfaceFlag;
joshualitt65171342014-10-09 07:25:36 -0700161 texDesc.fConfig = kRGBA_8888_GrPixelConfig;
162 texDesc.fOrigin = random->nextBool() == true ? kTopLeft_GrSurfaceOrigin :
163 kBottomLeft_GrSurfaceOrigin;
joshualitt15732062015-05-13 12:15:14 -0700164 texDesc.fSampleCnt = random->nextBool() == true ? SkTMin(4, caps->maxSampleCount()) : 0;
joshualitt6c891102015-05-13 08:51:49 -0700165
bsalomon8718aaf2015-02-19 07:24:21 -0800166 GrUniqueKey key;
167 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
joshualitt6c891102015-05-13 08:51:49 -0700168 GrUniqueKey::Builder builder(&key, kDomain, 2);
bsalomon24db3b12015-01-23 04:24:04 -0800169 builder[0] = texDesc.fOrigin;
joshualitt6c891102015-05-13 08:51:49 -0700170 builder[1] = texDesc.fSampleCnt;
bsalomon24db3b12015-01-23 04:24:04 -0800171 builder.finish();
joshualitt65171342014-10-09 07:25:36 -0700172
bsalomon4061b122015-05-29 10:26:19 -0700173 GrTexture* texture = textureProvider->findAndRefTextureByUniqueKey(key);
bsalomond27726e2014-10-12 05:40:00 -0700174 if (!texture) {
bsalomon4061b122015-05-29 10:26:19 -0700175 texture = textureProvider->createTexture(texDesc, true);
bsalomon37f9a262015-02-02 13:00:10 -0800176 if (texture) {
bsalomon4061b122015-05-29 10:26:19 -0700177 textureProvider->assignUniqueKeyToTexture(key, texture);
joshualittd9097592014-10-07 08:37:36 -0700178 }
joshualittd9097592014-10-07 08:37:36 -0700179 }
halcanary96fcdcc2015-08-27 07:41:13 -0700180 return texture ? texture->asRenderTarget() : nullptr;
bsalomon@google.com91207482013-02-12 21:45:24 +0000181}
182
joshualitt0067ff52015-07-08 14:26:19 -0700183static void set_random_xpf(GrPipelineBuilder* pipelineBuilder, GrProcessorTestData* d) {
bsalomonb5b60322015-09-14 12:26:33 -0700184 SkAutoTUnref<const GrXPFactory> xpf(GrProcessorTestFactory<GrXPFactory>::Create(d));
egdanielc2304142014-12-11 13:15:13 -0800185 SkASSERT(xpf);
egdaniel8dd688b2015-01-22 10:16:09 -0800186 pipelineBuilder->setXPFactory(xpf.get());
egdanielc2304142014-12-11 13:15:13 -0800187}
188
wangyix059dffa2015-09-10 06:57:05 -0700189static const GrFragmentProcessor* create_random_proc_tree(GrProcessorTestData* d,
190 int minLevels, int maxLevels) {
191 SkASSERT(1 <= minLevels);
192 SkASSERT(minLevels <= maxLevels);
193
194 // Return a leaf node if maxLevels is 1 or if we randomly chose to terminate.
195 // If returning a leaf node, make sure that it doesn't have children (e.g. another
196 // GrComposeEffect)
197 const float terminateProbability = 0.3f;
198 if (1 == minLevels) {
199 bool terminate = (1 == maxLevels) || (d->fRandom->nextF() < terminateProbability);
200 if (terminate) {
201 const GrFragmentProcessor* fp;
202 while (true) {
bsalomonb5b60322015-09-14 12:26:33 -0700203 fp = GrProcessorTestFactory<GrFragmentProcessor>::Create(d);
wangyix059dffa2015-09-10 06:57:05 -0700204 SkASSERT(fp);
205 if (0 == fp->numChildProcessors()) {
206 break;
207 }
208 fp->unref();
209 }
210 return fp;
211 }
212 }
213 // If we didn't terminate, choose either the left or right subtree to fulfill
214 // the minLevels requirement of this tree; the other child can have as few levels as it wants.
215 // Also choose a random xfer mode that's supported by CreateFrom2Procs().
216 if (minLevels > 1) {
217 --minLevels;
218 }
219 SkAutoTUnref<const GrFragmentProcessor> minLevelsChild(create_random_proc_tree(d, minLevels,
220 maxLevels - 1));
221 SkAutoTUnref<const GrFragmentProcessor> otherChild(create_random_proc_tree(d, 1,
222 maxLevels - 1));
223 SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(d->fRandom->nextRangeU(0,
224 SkXfermode::kLastCoeffMode));
225 const GrFragmentProcessor* fp;
226 if (d->fRandom->nextF() < 0.5f) {
227 fp = GrXfermodeFragmentProcessor::CreateFromTwoProcessors(minLevelsChild, otherChild, mode);
228 SkASSERT(fp);
229 } else {
230 fp = GrXfermodeFragmentProcessor::CreateFromTwoProcessors(otherChild, minLevelsChild, mode);
231 SkASSERT(fp);
232 }
233 return fp;
234}
235
joshualitt0067ff52015-07-08 14:26:19 -0700236static void set_random_color_coverage_stages(GrPipelineBuilder* pipelineBuilder,
237 GrProcessorTestData* d, int maxStages) {
wangyix059dffa2015-09-10 06:57:05 -0700238 // Randomly choose to either create a linear pipeline of procs or create one proc tree
239 const float procTreeProbability = 0.5f;
240 if (d->fRandom->nextF() < procTreeProbability) {
241 // A full tree with 5 levels (31 nodes) may exceed the max allowed length of the gl
242 // processor key; maxTreeLevels should be a number from 1 to 4 inclusive.
243 const int maxTreeLevels = 4;
bsalomonae59b772014-11-19 08:23:49 -0800244 SkAutoTUnref<const GrFragmentProcessor> fp(
wangyix059dffa2015-09-10 06:57:05 -0700245 create_random_proc_tree(d, 2, maxTreeLevels));
246 pipelineBuilder->addColorFragmentProcessor(fp);
247 } else {
248 int numProcs = d->fRandom->nextULessThan(maxStages + 1);
249 int numColorProcs = d->fRandom->nextULessThan(numProcs + 1);
joshualitt65171342014-10-09 07:25:36 -0700250
wangyix059dffa2015-09-10 06:57:05 -0700251 for (int s = 0; s < numProcs;) {
252 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomonae4738f2015-09-15 15:33:27 -0700253 GrProcessorTestFactory<GrFragmentProcessor>::Create(d));
wangyix059dffa2015-09-10 06:57:05 -0700254 SkASSERT(fp);
255
256 // finally add the stage to the correct pipeline in the drawstate
257 if (s < numColorProcs) {
258 pipelineBuilder->addColorFragmentProcessor(fp);
259 } else {
260 pipelineBuilder->addCoverageFragmentProcessor(fp);
261 }
262 ++s;
joshualitt65171342014-10-09 07:25:36 -0700263 }
joshualitt65171342014-10-09 07:25:36 -0700264 }
265}
266
egdaniel8dd688b2015-01-22 10:16:09 -0800267static void set_random_state(GrPipelineBuilder* pipelineBuilder, SkRandom* random) {
joshualitt65171342014-10-09 07:25:36 -0700268 int state = 0;
bsalomond79c5492015-04-27 10:07:04 -0700269 for (int i = 1; i <= GrPipelineBuilder::kLast_Flag; i <<= 1) {
joshualitt65171342014-10-09 07:25:36 -0700270 state |= random->nextBool() * i;
271 }
joshualitt6c891102015-05-13 08:51:49 -0700272
273 // If we don't have an MSAA rendertarget then we have to disable useHWAA
274 if ((state | GrPipelineBuilder::kHWAntialias_Flag) &&
vbuzinovdded6962015-06-12 08:59:45 -0700275 !pipelineBuilder->getRenderTarget()->isUnifiedMultisampled()) {
joshualitt6c891102015-05-13 08:51:49 -0700276 state &= ~GrPipelineBuilder::kHWAntialias_Flag;
277 }
egdaniel8dd688b2015-01-22 10:16:09 -0800278 pipelineBuilder->enableState(state);
joshualitt65171342014-10-09 07:25:36 -0700279}
280
joshualitt65171342014-10-09 07:25:36 -0700281// right now, the only thing we seem to care about in drawState's stencil is 'doesWrite()'
egdaniel8dd688b2015-01-22 10:16:09 -0800282static void set_random_stencil(GrPipelineBuilder* pipelineBuilder, SkRandom* random) {
joshualitt65171342014-10-09 07:25:36 -0700283 GR_STATIC_CONST_SAME_STENCIL(kDoesWriteStencil,
284 kReplace_StencilOp,
285 kReplace_StencilOp,
286 kAlways_StencilFunc,
287 0xffff,
288 0xffff,
289 0xffff);
290 GR_STATIC_CONST_SAME_STENCIL(kDoesNotWriteStencil,
291 kKeep_StencilOp,
292 kKeep_StencilOp,
293 kNever_StencilFunc,
294 0xffff,
295 0xffff,
296 0xffff);
297
298 if (random->nextBool()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800299 pipelineBuilder->setStencil(kDoesWriteStencil);
joshualitt65171342014-10-09 07:25:36 -0700300 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800301 pipelineBuilder->setStencil(kDoesNotWriteStencil);
joshualitt65171342014-10-09 07:25:36 -0700302 }
303}
joshualitt249af152014-09-15 11:41:13 -0700304
robertphillips0dfa62c2015-11-16 06:23:31 -0800305bool GrDrawingManager::ProgramUnitTest(GrContext* context, int maxStages) {
robertphillipsa13e2022015-11-11 12:01:09 -0800306 GrDrawingManager* drawingManager = context->drawingManager();
307
joshualitt65171342014-10-09 07:25:36 -0700308 // setup dummy textures
bsalomonf2703d82014-10-28 14:33:06 -0700309 GrSurfaceDesc dummyDesc;
bsalomonbea01502015-07-16 10:00:28 -0700310 dummyDesc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000311 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000312 dummyDesc.fWidth = 34;
313 dummyDesc.fHeight = 18;
bsalomon4061b122015-05-29 10:26:19 -0700314 SkAutoTUnref<GrTexture> dummyTexture1(
halcanary96fcdcc2015-08-27 07:41:13 -0700315 context->textureProvider()->createTexture(dummyDesc, false, nullptr, 0));
bsalomonf2703d82014-10-28 14:33:06 -0700316 dummyDesc.fFlags = kNone_GrSurfaceFlags;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000317 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
318 dummyDesc.fWidth = 16;
319 dummyDesc.fHeight = 22;
bsalomon4061b122015-05-29 10:26:19 -0700320 SkAutoTUnref<GrTexture> dummyTexture2(
halcanary96fcdcc2015-08-27 07:41:13 -0700321 context->textureProvider()->createTexture(dummyDesc, false, nullptr, 0));
bsalomon@google.comd4726202012-08-03 14:34:46 +0000322
bsalomone904c092014-07-17 10:50:59 -0700323 if (!dummyTexture1 || ! dummyTexture2) {
joshualitt65171342014-10-09 07:25:36 -0700324 SkDebugf("Could not allocate dummy textures");
bsalomone904c092014-07-17 10:50:59 -0700325 return false;
326 }
327
joshualitt65171342014-10-09 07:25:36 -0700328 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
329
joshualitt54e0c122014-11-19 09:38:51 -0800330 // dummy scissor state
bsalomon3e791242014-12-17 13:43:13 -0800331 GrScissorState scissor;
joshualitt54e0c122014-11-19 09:38:51 -0800332
joshualitt6c891102015-05-13 08:51:49 -0700333 // wide open clip
joshualitt44701df2015-02-23 14:44:57 -0800334 GrClip clip;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000335
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000336 SkRandom random;
joshualitt6c891102015-05-13 08:51:49 -0700337 static const int NUM_TESTS = 2048;
338 for (int t = 0; t < NUM_TESTS; t++) {
joshualitt65171342014-10-09 07:25:36 -0700339 // setup random render target(can fail)
bsalomon4061b122015-05-29 10:26:19 -0700340 SkAutoTUnref<GrRenderTarget> rt(random_render_target(
robertphillipsa13e2022015-11-11 12:01:09 -0800341 context->textureProvider(), &random, context->caps()));
joshualitt2c93efe2014-11-06 12:57:13 -0800342 if (!rt.get()) {
joshualitt65171342014-10-09 07:25:36 -0700343 SkDebugf("Could not allocate render target");
344 return false;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000345 }
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000346
egdaniel8dd688b2015-01-22 10:16:09 -0800347 GrPipelineBuilder pipelineBuilder;
348 pipelineBuilder.setRenderTarget(rt.get());
joshualitt44701df2015-02-23 14:44:57 -0800349 pipelineBuilder.setClip(clip);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000350
bsalomonabd30f52015-08-13 13:34:48 -0700351 SkAutoTUnref<GrDrawBatch> batch(GrRandomDrawBatch(&random, context));
joshualitt6c891102015-05-13 08:51:49 -0700352 SkASSERT(batch);
egdanielc0648242014-09-22 13:17:02 -0700353
cdaltonc94cd7c2015-11-12 12:11:04 -0800354 GrProcessorTestData ptd(&random, context, context->caps(), rt, dummyTextures);
joshualitt0067ff52015-07-08 14:26:19 -0700355 set_random_color_coverage_stages(&pipelineBuilder, &ptd, maxStages);
356 set_random_xpf(&pipelineBuilder, &ptd);
egdaniel8dd688b2015-01-22 10:16:09 -0800357 set_random_state(&pipelineBuilder, &random);
358 set_random_stencil(&pipelineBuilder, &random);
joshualittd9097592014-10-07 08:37:36 -0700359
joshualittf5883a62016-01-13 07:47:38 -0800360 SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(rt));
361 if (!drawContext) {
362 SkDebugf("Could not allocate drawContext");
363 return false;
364 }
robertphillips0dfa62c2015-11-16 06:23:31 -0800365
joshualittf5883a62016-01-13 07:47:38 -0800366 drawContext->internal_drawBatch(pipelineBuilder, batch);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000367 }
joshualitt6c891102015-05-13 08:51:49 -0700368 // Flush everything, test passes if flush is successful(ie, no asserts are hit, no crashes)
robertphillipsa13e2022015-11-11 12:01:09 -0800369 drawingManager->flush();
bsalomonb5b60322015-09-14 12:26:33 -0700370
371 // Validate that GrFPs work correctly without an input.
372 GrSurfaceDesc rtDesc;
373 rtDesc.fWidth = kRenderTargetWidth;
374 rtDesc.fHeight = kRenderTargetHeight;
375 rtDesc.fFlags = kRenderTarget_GrSurfaceFlag;
376 rtDesc.fConfig = kRGBA_8888_GrPixelConfig;
377 SkAutoTUnref<GrRenderTarget> rt(
robertphillipsa13e2022015-11-11 12:01:09 -0800378 context->textureProvider()->createTexture(rtDesc, false)->asRenderTarget());
bsalomonb5b60322015-09-14 12:26:33 -0700379 int fpFactoryCnt = GrProcessorTestFactory<GrFragmentProcessor>::Count();
380 for (int i = 0; i < fpFactoryCnt; ++i) {
381 // Since FP factories internally randomize, call each 10 times.
382 for (int j = 0; j < 10; ++j) {
383 SkAutoTUnref<GrDrawBatch> batch(GrRandomDrawBatch(&random, context));
384 SkASSERT(batch);
cdaltonc94cd7c2015-11-12 12:11:04 -0800385 GrProcessorTestData ptd(&random, context, context->caps(), rt, dummyTextures);
bsalomonb5b60322015-09-14 12:26:33 -0700386 GrPipelineBuilder builder;
387 builder.setXPFactory(GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref();
388 builder.setRenderTarget(rt);
389 builder.setClip(clip);
390
391 SkAutoTUnref<const GrFragmentProcessor> fp(
392 GrProcessorTestFactory<GrFragmentProcessor>::CreateIdx(i, &ptd));
393 SkAutoTUnref<const GrFragmentProcessor> blockFP(
394 BlockInputFragmentProcessor::Create(fp));
395 builder.addColorFragmentProcessor(blockFP);
396
joshualittf5883a62016-01-13 07:47:38 -0800397 SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(rt));
398 if (!drawContext) {
399 SkDebugf("Could not allocate a drawcontext");
400 return false;
401 }
robertphillips0dfa62c2015-11-16 06:23:31 -0800402
joshualittf5883a62016-01-13 07:47:38 -0800403 drawContext->internal_drawBatch(builder, batch);
robertphillipsa13e2022015-11-11 12:01:09 -0800404 drawingManager->flush();
bsalomonb5b60322015-09-14 12:26:33 -0700405 }
406 }
407
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000408 return true;
409}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000410
kkinnunen3e980c32015-12-23 01:33:00 -0800411static int get_glprograms_max_stages(GrContext* context) {
412 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->getGpu());
413 /*
414 * For the time being, we only support the test with desktop GL or for android on
415 * ARM platforms
416 * TODO When we run ES 3.00 GLSL in more places, test again
417 */
418 if (kGL_GrGLStandard == gpu->glStandard() ||
419 kARM_GrGLVendor == gpu->ctxInfo().vendor()) {
420 return 6;
421 } else if (kTegra3_GrGLRenderer == gpu->ctxInfo().renderer() ||
422 kOther_GrGLRenderer == gpu->ctxInfo().renderer()) {
423 return 1;
424 }
425 return 0;
426}
427
428static void test_glprograms_native(skiatest::Reporter* reporter, GrContext* context) {
429 int maxStages = get_glprograms_max_stages(context);
430 if (maxStages == 0) {
431 return;
432 }
433 REPORTER_ASSERT(reporter, GrDrawingManager::ProgramUnitTest(context, maxStages));
434}
435
436static void test_glprograms_other_contexts(skiatest::Reporter* reporter, GrContext* context) {
437 int maxStages = get_glprograms_max_stages(context);
438#ifdef SK_BUILD_FOR_WIN
439 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE and
440 // command buffer.
441 maxStages = SkTMin(maxStages, 2);
442#endif
443 if (maxStages == 0) {
444 return;
445 }
446 REPORTER_ASSERT(reporter, GrDrawingManager::ProgramUnitTest(context, maxStages));
447}
448
449DEF_GPUTEST(GLPrograms, reporter, /*factory*/) {
bsalomon3318ee72015-03-16 11:56:29 -0700450 // Set a locale that would cause shader compilation to fail because of , as decimal separator.
451 // skbug 3330
452#ifdef SK_BUILD_FOR_WIN
453 GrAutoLocaleSetter als("sv-SE");
454#else
455 GrAutoLocaleSetter als("sv_SE.UTF-8");
456#endif
457
joshualitt6c891102015-05-13 08:51:49 -0700458 // We suppress prints to avoid spew
bsalomon682c2692015-05-22 14:01:46 -0700459 GrContextOptions opts;
joshualitt6c891102015-05-13 08:51:49 -0700460 opts.fSuppressPrints = true;
461 GrContextFactory debugFactory(opts);
kkinnunen3e980c32015-12-23 01:33:00 -0800462 skiatest::RunWithGPUTestContexts(test_glprograms_native, skiatest::kNative_GPUTestContexts,
463 reporter, &debugFactory);
464 skiatest::RunWithGPUTestContexts(test_glprograms_other_contexts,
465 skiatest::kOther_GPUTestContexts, reporter, &debugFactory);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000466}
467
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000468#endif