blob: 7440608d773b5f940e5206e6c4540ceb9872bcd7 [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"
robertphillips8b8f36f2016-03-02 08:53:12 -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:
robertphillips9cdb9922016-02-03 12:25:40 -080048 void emitCode(EmitArgs& args) override {
joshualitt6c891102015-05-13 08:51:49 -070049 // pass through
egdaniel4ca2e602015-11-18 08:01:26 -080050 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
bsalomonb5b60322015-09-14 12:26:33 -070051 if (args.fInputColor) {
egdaniel4ca2e602015-11-18 08:01:26 -080052 fragBuilder->codeAppendf("%s = %s;\n", args.fOutputColor, args.fInputColor);
bsalomonb5b60322015-09-14 12:26:33 -070053 } else {
egdaniel4ca2e602015-11-18 08:01:26 -080054 fragBuilder->codeAppendf("%s = vec4(1.0);\n", args.fOutputColor);
bsalomonb5b60322015-09-14 12:26:33 -070055 }
joshualitt6c891102015-05-13 08:51:49 -070056 }
joshualitt65171342014-10-09 07:25:36 -070057
jvanverthcfc18862015-04-28 08:48:20 -070058 static void GenKey(const GrProcessor& processor, const GrGLSLCaps&, GrProcessorKeyBuilder* b) {
joshualitt65171342014-10-09 07:25:36 -070059 for (uint32_t i = 0; i < kMaxKeySize; i++) {
60 b->add32(i);
joshualittd9097592014-10-07 08:37:36 -070061 }
joshualittd9097592014-10-07 08:37:36 -070062 }
63
joshualitt65171342014-10-09 07:25:36 -070064private:
egdaniel64c47282015-11-13 06:54:19 -080065 typedef GrGLSLFragmentProcessor INHERITED;
joshualitt65171342014-10-09 07:25:36 -070066};
67
joshualitteb2a6762014-12-04 11:35:33 -080068class BigKeyProcessor : public GrFragmentProcessor {
69public:
70 static GrFragmentProcessor* Create() {
bsalomon02141732015-10-21 06:57:30 -070071 return new BigKeyProcessor;
joshualitteb2a6762014-12-04 11:35:33 -080072 }
73
mtklein36352bf2015-03-25 18:17:31 -070074 const char* name() const override { return "Big Ole Key"; }
joshualitteb2a6762014-12-04 11:35:33 -080075
egdaniel57d3b032015-11-13 11:57:27 -080076 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
robertphillips9cdb9922016-02-03 12:25:40 -080077 return new GLBigKeyProcessor;
joshualitteb2a6762014-12-04 11:35:33 -080078 }
79
80private:
81 BigKeyProcessor() {
82 this->initClassID<BigKeyProcessor>();
83 }
egdaniel57d3b032015-11-13 11:57:27 -080084 virtual void onGetGLSLProcessorKey(const GrGLSLCaps& caps,
85 GrProcessorKeyBuilder* b) const override {
wangyix4b3050b2015-08-04 07:59:37 -070086 GLBigKeyProcessor::GenKey(*this, caps, b);
87 }
mtklein36352bf2015-03-25 18:17:31 -070088 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
89 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { }
joshualitteb2a6762014-12-04 11:35:33 -080090
91 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
92
93 typedef GrFragmentProcessor INHERITED;
94};
95
96GR_DEFINE_FRAGMENT_PROCESSOR_TEST(BigKeyProcessor);
97
bsalomonc21b09e2015-08-28 18:46:56 -070098const GrFragmentProcessor* BigKeyProcessor::TestCreate(GrProcessorTestData*) {
joshualitteb2a6762014-12-04 11:35:33 -080099 return BigKeyProcessor::Create();
100}
101
bsalomonb5b60322015-09-14 12:26:33 -0700102//////////////////////////////////////////////////////////////////////////////
103
104class BlockInputFragmentProcessor : public GrFragmentProcessor {
105public:
106 static GrFragmentProcessor* Create(const GrFragmentProcessor* fp) {
107 return new BlockInputFragmentProcessor(fp);
108 }
109
110 const char* name() const override { return "Block Input"; }
111
egdaniel57d3b032015-11-13 11:57:27 -0800112 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLFP; }
bsalomonb5b60322015-09-14 12:26:33 -0700113
114private:
egdaniel64c47282015-11-13 06:54:19 -0800115 class GLFP : public GrGLSLFragmentProcessor {
bsalomonb5b60322015-09-14 12:26:33 -0700116 public:
117 void emitCode(EmitArgs& args) override {
bsalomon38ddbad2015-09-24 06:00:00 -0700118 this->emitChild(0, nullptr, args);
bsalomonb5b60322015-09-14 12:26:33 -0700119 }
120
121 private:
egdaniel64c47282015-11-13 06:54:19 -0800122 typedef GrGLSLFragmentProcessor INHERITED;
bsalomonb5b60322015-09-14 12:26:33 -0700123 };
124
125 BlockInputFragmentProcessor(const GrFragmentProcessor* child) {
126 this->initClassID<BlockInputFragmentProcessor>();
127 this->registerChildProcessor(child);
128 }
129
egdaniel57d3b032015-11-13 11:57:27 -0800130 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {}
bsalomonb5b60322015-09-14 12:26:33 -0700131
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
joshualitt65171342014-10-09 07:25:36 -0700145/*
146 * Begin test code
147 */
148static const int kRenderTargetHeight = 1;
149static const int kRenderTargetWidth = 1;
150
bsalomon4061b122015-05-29 10:26:19 -0700151static GrRenderTarget* random_render_target(GrTextureProvider* textureProvider, SkRandom* random,
bsalomon4b91f762015-05-19 09:29:46 -0700152 const GrCaps* caps) {
joshualitt65171342014-10-09 07:25:36 -0700153 // setup render target
154 GrTextureParams params;
bsalomonf2703d82014-10-28 14:33:06 -0700155 GrSurfaceDesc texDesc;
joshualitt65171342014-10-09 07:25:36 -0700156 texDesc.fWidth = kRenderTargetWidth;
157 texDesc.fHeight = kRenderTargetHeight;
bsalomonf2703d82014-10-28 14:33:06 -0700158 texDesc.fFlags = kRenderTarget_GrSurfaceFlag;
joshualitt65171342014-10-09 07:25:36 -0700159 texDesc.fConfig = kRGBA_8888_GrPixelConfig;
160 texDesc.fOrigin = random->nextBool() == true ? kTopLeft_GrSurfaceOrigin :
161 kBottomLeft_GrSurfaceOrigin;
joshualitt15732062015-05-13 12:15:14 -0700162 texDesc.fSampleCnt = random->nextBool() == true ? SkTMin(4, caps->maxSampleCount()) : 0;
joshualitt6c891102015-05-13 08:51:49 -0700163
bsalomon8718aaf2015-02-19 07:24:21 -0800164 GrUniqueKey key;
165 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
joshualitt6c891102015-05-13 08:51:49 -0700166 GrUniqueKey::Builder builder(&key, kDomain, 2);
bsalomon24db3b12015-01-23 04:24:04 -0800167 builder[0] = texDesc.fOrigin;
joshualitt6c891102015-05-13 08:51:49 -0700168 builder[1] = texDesc.fSampleCnt;
bsalomon24db3b12015-01-23 04:24:04 -0800169 builder.finish();
joshualitt65171342014-10-09 07:25:36 -0700170
bsalomon4061b122015-05-29 10:26:19 -0700171 GrTexture* texture = textureProvider->findAndRefTextureByUniqueKey(key);
bsalomond27726e2014-10-12 05:40:00 -0700172 if (!texture) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800173 texture = textureProvider->createTexture(texDesc, SkBudgeted::kYes);
bsalomon37f9a262015-02-02 13:00:10 -0800174 if (texture) {
bsalomon4061b122015-05-29 10:26:19 -0700175 textureProvider->assignUniqueKeyToTexture(key, texture);
joshualittd9097592014-10-07 08:37:36 -0700176 }
joshualittd9097592014-10-07 08:37:36 -0700177 }
halcanary96fcdcc2015-08-27 07:41:13 -0700178 return texture ? texture->asRenderTarget() : nullptr;
bsalomon@google.com91207482013-02-12 21:45:24 +0000179}
180
joshualitt0067ff52015-07-08 14:26:19 -0700181static void set_random_xpf(GrPipelineBuilder* pipelineBuilder, GrProcessorTestData* d) {
bsalomonb5b60322015-09-14 12:26:33 -0700182 SkAutoTUnref<const GrXPFactory> xpf(GrProcessorTestFactory<GrXPFactory>::Create(d));
egdanielc2304142014-12-11 13:15:13 -0800183 SkASSERT(xpf);
egdaniel8dd688b2015-01-22 10:16:09 -0800184 pipelineBuilder->setXPFactory(xpf.get());
egdanielc2304142014-12-11 13:15:13 -0800185}
186
wangyix059dffa2015-09-10 06:57:05 -0700187static const GrFragmentProcessor* create_random_proc_tree(GrProcessorTestData* d,
188 int minLevels, int maxLevels) {
189 SkASSERT(1 <= minLevels);
190 SkASSERT(minLevels <= maxLevels);
191
192 // Return a leaf node if maxLevels is 1 or if we randomly chose to terminate.
193 // If returning a leaf node, make sure that it doesn't have children (e.g. another
194 // GrComposeEffect)
195 const float terminateProbability = 0.3f;
196 if (1 == minLevels) {
197 bool terminate = (1 == maxLevels) || (d->fRandom->nextF() < terminateProbability);
198 if (terminate) {
199 const GrFragmentProcessor* fp;
200 while (true) {
bsalomonb5b60322015-09-14 12:26:33 -0700201 fp = GrProcessorTestFactory<GrFragmentProcessor>::Create(d);
wangyix059dffa2015-09-10 06:57:05 -0700202 SkASSERT(fp);
203 if (0 == fp->numChildProcessors()) {
204 break;
205 }
206 fp->unref();
207 }
208 return fp;
209 }
210 }
211 // If we didn't terminate, choose either the left or right subtree to fulfill
212 // the minLevels requirement of this tree; the other child can have as few levels as it wants.
213 // Also choose a random xfer mode that's supported by CreateFrom2Procs().
214 if (minLevels > 1) {
215 --minLevels;
216 }
217 SkAutoTUnref<const GrFragmentProcessor> minLevelsChild(create_random_proc_tree(d, minLevels,
218 maxLevels - 1));
219 SkAutoTUnref<const GrFragmentProcessor> otherChild(create_random_proc_tree(d, 1,
220 maxLevels - 1));
221 SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(d->fRandom->nextRangeU(0,
222 SkXfermode::kLastCoeffMode));
223 const GrFragmentProcessor* fp;
224 if (d->fRandom->nextF() < 0.5f) {
225 fp = GrXfermodeFragmentProcessor::CreateFromTwoProcessors(minLevelsChild, otherChild, mode);
226 SkASSERT(fp);
227 } else {
228 fp = GrXfermodeFragmentProcessor::CreateFromTwoProcessors(otherChild, minLevelsChild, mode);
229 SkASSERT(fp);
230 }
231 return fp;
232}
233
joshualitt0067ff52015-07-08 14:26:19 -0700234static void set_random_color_coverage_stages(GrPipelineBuilder* pipelineBuilder,
235 GrProcessorTestData* d, int maxStages) {
wangyix059dffa2015-09-10 06:57:05 -0700236 // Randomly choose to either create a linear pipeline of procs or create one proc tree
237 const float procTreeProbability = 0.5f;
238 if (d->fRandom->nextF() < procTreeProbability) {
239 // A full tree with 5 levels (31 nodes) may exceed the max allowed length of the gl
240 // processor key; maxTreeLevels should be a number from 1 to 4 inclusive.
241 const int maxTreeLevels = 4;
bsalomonae59b772014-11-19 08:23:49 -0800242 SkAutoTUnref<const GrFragmentProcessor> fp(
wangyix059dffa2015-09-10 06:57:05 -0700243 create_random_proc_tree(d, 2, maxTreeLevels));
244 pipelineBuilder->addColorFragmentProcessor(fp);
245 } else {
246 int numProcs = d->fRandom->nextULessThan(maxStages + 1);
247 int numColorProcs = d->fRandom->nextULessThan(numProcs + 1);
joshualitt65171342014-10-09 07:25:36 -0700248
wangyix059dffa2015-09-10 06:57:05 -0700249 for (int s = 0; s < numProcs;) {
250 SkAutoTUnref<const GrFragmentProcessor> fp(
bsalomonae4738f2015-09-15 15:33:27 -0700251 GrProcessorTestFactory<GrFragmentProcessor>::Create(d));
wangyix059dffa2015-09-10 06:57:05 -0700252 SkASSERT(fp);
253
254 // finally add the stage to the correct pipeline in the drawstate
255 if (s < numColorProcs) {
256 pipelineBuilder->addColorFragmentProcessor(fp);
257 } else {
258 pipelineBuilder->addCoverageFragmentProcessor(fp);
259 }
260 ++s;
joshualitt65171342014-10-09 07:25:36 -0700261 }
joshualitt65171342014-10-09 07:25:36 -0700262 }
263}
264
egdaniel8dd688b2015-01-22 10:16:09 -0800265static void set_random_state(GrPipelineBuilder* pipelineBuilder, SkRandom* random) {
joshualitt65171342014-10-09 07:25:36 -0700266 int state = 0;
bsalomond79c5492015-04-27 10:07:04 -0700267 for (int i = 1; i <= GrPipelineBuilder::kLast_Flag; i <<= 1) {
joshualitt65171342014-10-09 07:25:36 -0700268 state |= random->nextBool() * i;
269 }
joshualitt6c891102015-05-13 08:51:49 -0700270
271 // If we don't have an MSAA rendertarget then we have to disable useHWAA
272 if ((state | GrPipelineBuilder::kHWAntialias_Flag) &&
vbuzinovdded6962015-06-12 08:59:45 -0700273 !pipelineBuilder->getRenderTarget()->isUnifiedMultisampled()) {
joshualitt6c891102015-05-13 08:51:49 -0700274 state &= ~GrPipelineBuilder::kHWAntialias_Flag;
275 }
egdaniel8dd688b2015-01-22 10:16:09 -0800276 pipelineBuilder->enableState(state);
joshualitt65171342014-10-09 07:25:36 -0700277}
278
joshualitt65171342014-10-09 07:25:36 -0700279// right now, the only thing we seem to care about in drawState's stencil is 'doesWrite()'
egdaniel8dd688b2015-01-22 10:16:09 -0800280static void set_random_stencil(GrPipelineBuilder* pipelineBuilder, SkRandom* random) {
joshualitt65171342014-10-09 07:25:36 -0700281 GR_STATIC_CONST_SAME_STENCIL(kDoesWriteStencil,
282 kReplace_StencilOp,
283 kReplace_StencilOp,
284 kAlways_StencilFunc,
285 0xffff,
286 0xffff,
287 0xffff);
288 GR_STATIC_CONST_SAME_STENCIL(kDoesNotWriteStencil,
289 kKeep_StencilOp,
290 kKeep_StencilOp,
291 kNever_StencilFunc,
292 0xffff,
293 0xffff,
294 0xffff);
295
296 if (random->nextBool()) {
egdaniel8dd688b2015-01-22 10:16:09 -0800297 pipelineBuilder->setStencil(kDoesWriteStencil);
joshualitt65171342014-10-09 07:25:36 -0700298 } else {
egdaniel8dd688b2015-01-22 10:16:09 -0800299 pipelineBuilder->setStencil(kDoesNotWriteStencil);
joshualitt65171342014-10-09 07:25:36 -0700300 }
301}
joshualitt249af152014-09-15 11:41:13 -0700302
robertphillips0dfa62c2015-11-16 06:23:31 -0800303bool GrDrawingManager::ProgramUnitTest(GrContext* context, int maxStages) {
robertphillipsa13e2022015-11-11 12:01:09 -0800304 GrDrawingManager* drawingManager = context->drawingManager();
305
joshualitt65171342014-10-09 07:25:36 -0700306 // setup dummy textures
bsalomonf2703d82014-10-28 14:33:06 -0700307 GrSurfaceDesc dummyDesc;
bsalomonbea01502015-07-16 10:00:28 -0700308 dummyDesc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000309 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000310 dummyDesc.fWidth = 34;
311 dummyDesc.fHeight = 18;
bsalomon4061b122015-05-29 10:26:19 -0700312 SkAutoTUnref<GrTexture> dummyTexture1(
bsalomon5ec26ae2016-02-25 08:33:02 -0800313 context->textureProvider()->createTexture(dummyDesc, SkBudgeted::kNo, nullptr, 0));
bsalomonf2703d82014-10-28 14:33:06 -0700314 dummyDesc.fFlags = kNone_GrSurfaceFlags;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000315 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
316 dummyDesc.fWidth = 16;
317 dummyDesc.fHeight = 22;
bsalomon4061b122015-05-29 10:26:19 -0700318 SkAutoTUnref<GrTexture> dummyTexture2(
bsalomon5ec26ae2016-02-25 08:33:02 -0800319 context->textureProvider()->createTexture(dummyDesc, SkBudgeted::kNo, nullptr, 0));
bsalomon@google.comd4726202012-08-03 14:34:46 +0000320
bsalomone904c092014-07-17 10:50:59 -0700321 if (!dummyTexture1 || ! dummyTexture2) {
joshualitt65171342014-10-09 07:25:36 -0700322 SkDebugf("Could not allocate dummy textures");
bsalomone904c092014-07-17 10:50:59 -0700323 return false;
324 }
325
joshualitt65171342014-10-09 07:25:36 -0700326 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
327
joshualitt54e0c122014-11-19 09:38:51 -0800328 // dummy scissor state
bsalomon3e791242014-12-17 13:43:13 -0800329 GrScissorState scissor;
joshualitt54e0c122014-11-19 09:38:51 -0800330
joshualitt6c891102015-05-13 08:51:49 -0700331 // wide open clip
joshualitt44701df2015-02-23 14:44:57 -0800332 GrClip clip;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000333
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000334 SkRandom random;
joshualitt6c891102015-05-13 08:51:49 -0700335 static const int NUM_TESTS = 2048;
336 for (int t = 0; t < NUM_TESTS; t++) {
joshualitt65171342014-10-09 07:25:36 -0700337 // setup random render target(can fail)
bsalomon4061b122015-05-29 10:26:19 -0700338 SkAutoTUnref<GrRenderTarget> rt(random_render_target(
robertphillipsa13e2022015-11-11 12:01:09 -0800339 context->textureProvider(), &random, context->caps()));
joshualitt2c93efe2014-11-06 12:57:13 -0800340 if (!rt.get()) {
joshualitt65171342014-10-09 07:25:36 -0700341 SkDebugf("Could not allocate render target");
342 return false;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000343 }
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000344
egdaniel8dd688b2015-01-22 10:16:09 -0800345 GrPipelineBuilder pipelineBuilder;
346 pipelineBuilder.setRenderTarget(rt.get());
joshualitt44701df2015-02-23 14:44:57 -0800347 pipelineBuilder.setClip(clip);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000348
bsalomonabd30f52015-08-13 13:34:48 -0700349 SkAutoTUnref<GrDrawBatch> batch(GrRandomDrawBatch(&random, context));
joshualitt6c891102015-05-13 08:51:49 -0700350 SkASSERT(batch);
egdanielc0648242014-09-22 13:17:02 -0700351
cdaltonc94cd7c2015-11-12 12:11:04 -0800352 GrProcessorTestData ptd(&random, context, context->caps(), rt, dummyTextures);
joshualitt0067ff52015-07-08 14:26:19 -0700353 set_random_color_coverage_stages(&pipelineBuilder, &ptd, maxStages);
354 set_random_xpf(&pipelineBuilder, &ptd);
egdaniel8dd688b2015-01-22 10:16:09 -0800355 set_random_state(&pipelineBuilder, &random);
356 set_random_stencil(&pipelineBuilder, &random);
joshualittd9097592014-10-07 08:37:36 -0700357
joshualittf5883a62016-01-13 07:47:38 -0800358 SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(rt));
359 if (!drawContext) {
360 SkDebugf("Could not allocate drawContext");
361 return false;
362 }
robertphillips0dfa62c2015-11-16 06:23:31 -0800363
robertphillips8b8f36f2016-03-02 08:53:12 -0800364 drawContext->internal_drawBatch(pipelineBuilder, batch);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000365 }
joshualitt6c891102015-05-13 08:51:49 -0700366 // Flush everything, test passes if flush is successful(ie, no asserts are hit, no crashes)
robertphillipsa13e2022015-11-11 12:01:09 -0800367 drawingManager->flush();
bsalomonb5b60322015-09-14 12:26:33 -0700368
369 // Validate that GrFPs work correctly without an input.
370 GrSurfaceDesc rtDesc;
371 rtDesc.fWidth = kRenderTargetWidth;
372 rtDesc.fHeight = kRenderTargetHeight;
373 rtDesc.fFlags = kRenderTarget_GrSurfaceFlag;
374 rtDesc.fConfig = kRGBA_8888_GrPixelConfig;
375 SkAutoTUnref<GrRenderTarget> rt(
bsalomon5ec26ae2016-02-25 08:33:02 -0800376 context->textureProvider()->createTexture(rtDesc, SkBudgeted::kNo)->asRenderTarget());
bsalomonb5b60322015-09-14 12:26:33 -0700377 int fpFactoryCnt = GrProcessorTestFactory<GrFragmentProcessor>::Count();
378 for (int i = 0; i < fpFactoryCnt; ++i) {
379 // Since FP factories internally randomize, call each 10 times.
380 for (int j = 0; j < 10; ++j) {
381 SkAutoTUnref<GrDrawBatch> batch(GrRandomDrawBatch(&random, context));
382 SkASSERT(batch);
cdaltonc94cd7c2015-11-12 12:11:04 -0800383 GrProcessorTestData ptd(&random, context, context->caps(), rt, dummyTextures);
bsalomonb5b60322015-09-14 12:26:33 -0700384 GrPipelineBuilder builder;
385 builder.setXPFactory(GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref();
386 builder.setRenderTarget(rt);
387 builder.setClip(clip);
388
389 SkAutoTUnref<const GrFragmentProcessor> fp(
390 GrProcessorTestFactory<GrFragmentProcessor>::CreateIdx(i, &ptd));
391 SkAutoTUnref<const GrFragmentProcessor> blockFP(
392 BlockInputFragmentProcessor::Create(fp));
393 builder.addColorFragmentProcessor(blockFP);
394
joshualittf5883a62016-01-13 07:47:38 -0800395 SkAutoTUnref<GrDrawContext> drawContext(context->drawContext(rt));
396 if (!drawContext) {
397 SkDebugf("Could not allocate a drawcontext");
398 return false;
399 }
robertphillips0dfa62c2015-11-16 06:23:31 -0800400
robertphillips8b8f36f2016-03-02 08:53:12 -0800401 drawContext->internal_drawBatch(builder, batch);
robertphillipsa13e2022015-11-11 12:01:09 -0800402 drawingManager->flush();
bsalomonb5b60322015-09-14 12:26:33 -0700403 }
404 }
405
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000406 return true;
407}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000408
kkinnunen3e980c32015-12-23 01:33:00 -0800409static int get_glprograms_max_stages(GrContext* context) {
410 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->getGpu());
411 /*
412 * For the time being, we only support the test with desktop GL or for android on
413 * ARM platforms
414 * TODO When we run ES 3.00 GLSL in more places, test again
415 */
416 if (kGL_GrGLStandard == gpu->glStandard() ||
417 kARM_GrGLVendor == gpu->ctxInfo().vendor()) {
418 return 6;
419 } else if (kTegra3_GrGLRenderer == gpu->ctxInfo().renderer() ||
420 kOther_GrGLRenderer == gpu->ctxInfo().renderer()) {
421 return 1;
422 }
423 return 0;
424}
425
426static void test_glprograms_native(skiatest::Reporter* reporter, GrContext* context) {
427 int maxStages = get_glprograms_max_stages(context);
428 if (maxStages == 0) {
429 return;
430 }
431 REPORTER_ASSERT(reporter, GrDrawingManager::ProgramUnitTest(context, maxStages));
432}
433
434static void test_glprograms_other_contexts(skiatest::Reporter* reporter, GrContext* context) {
435 int maxStages = get_glprograms_max_stages(context);
436#ifdef SK_BUILD_FOR_WIN
437 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE and
438 // command buffer.
439 maxStages = SkTMin(maxStages, 2);
440#endif
441 if (maxStages == 0) {
442 return;
443 }
444 REPORTER_ASSERT(reporter, GrDrawingManager::ProgramUnitTest(context, maxStages));
445}
446
447DEF_GPUTEST(GLPrograms, reporter, /*factory*/) {
bsalomon3318ee72015-03-16 11:56:29 -0700448 // Set a locale that would cause shader compilation to fail because of , as decimal separator.
449 // skbug 3330
450#ifdef SK_BUILD_FOR_WIN
451 GrAutoLocaleSetter als("sv-SE");
452#else
453 GrAutoLocaleSetter als("sv_SE.UTF-8");
454#endif
455
joshualitt6c891102015-05-13 08:51:49 -0700456 // We suppress prints to avoid spew
bsalomon682c2692015-05-22 14:01:46 -0700457 GrContextOptions opts;
joshualitt6c891102015-05-13 08:51:49 -0700458 opts.fSuppressPrints = true;
459 GrContextFactory debugFactory(opts);
kkinnunen3e980c32015-12-23 01:33:00 -0800460 skiatest::RunWithGPUTestContexts(test_glprograms_native, skiatest::kNative_GPUTestContexts,
461 reporter, &debugFactory);
462 skiatest::RunWithGPUTestContexts(test_glprograms_other_contexts,
463 skiatest::kOther_GPUTestContexts, reporter, &debugFactory);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000464}
465
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000466#endif