blob: e3639fbce6c05a0c93f9e99e71c9ae45d796f843 [file] [log] [blame]
bsalomon@google.coma8e686e2011-08-16 15:45:58 +00001/*
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.comd4726202012-08-03 14:34:46 +00008// This is a GPU-backend specific test. It relies on static intializers to work
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00009
bsalomon@google.com2a48c3a2012-08-03 14:54:45 +000010#include "SkTypes.h"
11
12#if SK_SUPPORT_GPU && SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
13
bsalomon6f7f2012015-03-16 14:00:52 -070014#include "GrAutoLocaleSetter.h"
bsalomon@google.com67b915d2013-02-04 16:13:32 +000015#include "GrContextFactory.h"
csmartdaltonbde96c62016-08-31 12:54:46 -070016#include "GrContextPriv.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050017#include "GrDrawOpTest.h"
robertphillipsa13e2022015-11-11 12:01:09 -080018#include "GrDrawingManager.h"
egdaniel8dd688b2015-01-22 10:16:09 -080019#include "GrPipeline.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050020#include "GrRenderTargetContextPriv.h"
joshualitt2c93efe2014-11-06 12:57:13 -080021#include "GrTest.h"
egdaniel95131432014-12-09 11:15:43 -080022#include "GrXferProcessor.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000023#include "SkChecksum.h"
tfarina@chromium.org223137f2012-11-21 22:38:36 +000024#include "SkRandom.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000025#include "Test.h"
joshualitt74417822015-08-07 11:42:16 -070026
Brian Salomon89527432016-12-16 09:52:16 -050027#include "ops/GrDrawOp.h"
joshualitt74417822015-08-07 11:42:16 -070028
joshualitt2c93efe2014-11-06 12:57:13 -080029#include "effects/GrConfigConversionEffect.h"
egdaniel95131432014-12-09 11:15:43 -080030#include "effects/GrPorterDuffXferProcessor.h"
wangyix059dffa2015-09-10 06:57:05 -070031#include "effects/GrXfermodeFragmentProcessor.h"
joshualitt74417822015-08-07 11:42:16 -070032
jvanverth39edf762014-12-22 11:44:19 -080033#include "gl/GrGLGpu.h"
egdaniel64c47282015-11-13 06:54:19 -080034#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080035#include "glsl/GrGLSLFragmentShaderBuilder.h"
36#include "glsl/GrGLSLProgramBuilder.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000037
joshualitt65171342014-10-09 07:25:36 -070038/*
bsalomon98b33eb2014-10-15 11:05:26 -070039 * A dummy processor which just tries to insert a massive key and verify that it can retrieve the
joshualitt65171342014-10-09 07:25:36 -070040 * whole thing correctly
41 */
42static const uint32_t kMaxKeySize = 1024;
43
egdaniel64c47282015-11-13 06:54:19 -080044class GLBigKeyProcessor : public GrGLSLFragmentProcessor {
joshualitt65171342014-10-09 07:25:36 -070045public:
robertphillips9cdb9922016-02-03 12:25:40 -080046 void emitCode(EmitArgs& args) override {
joshualitt6c891102015-05-13 08:51:49 -070047 // pass through
egdaniel4ca2e602015-11-18 08:01:26 -080048 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
bsalomonb5b60322015-09-14 12:26:33 -070049 if (args.fInputColor) {
egdaniel4ca2e602015-11-18 08:01:26 -080050 fragBuilder->codeAppendf("%s = %s;\n", args.fOutputColor, args.fInputColor);
bsalomonb5b60322015-09-14 12:26:33 -070051 } else {
egdaniel4ca2e602015-11-18 08:01:26 -080052 fragBuilder->codeAppendf("%s = vec4(1.0);\n", args.fOutputColor);
bsalomonb5b60322015-09-14 12:26:33 -070053 }
joshualitt6c891102015-05-13 08:51:49 -070054 }
joshualitt65171342014-10-09 07:25:36 -070055
Brian Salomon94efbf52016-11-29 13:43:05 -050056 static void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder* b) {
joshualitt65171342014-10-09 07:25:36 -070057 for (uint32_t i = 0; i < kMaxKeySize; i++) {
58 b->add32(i);
joshualittd9097592014-10-07 08:37:36 -070059 }
joshualittd9097592014-10-07 08:37:36 -070060 }
61
joshualitt65171342014-10-09 07:25:36 -070062private:
egdaniel64c47282015-11-13 06:54:19 -080063 typedef GrGLSLFragmentProcessor INHERITED;
joshualitt65171342014-10-09 07:25:36 -070064};
65
joshualitteb2a6762014-12-04 11:35:33 -080066class BigKeyProcessor : public GrFragmentProcessor {
67public:
Brian Salomonaff329b2017-08-11 09:40:37 -040068 static std::unique_ptr<GrFragmentProcessor> Make() {
69 return std::unique_ptr<GrFragmentProcessor>(new BigKeyProcessor);
joshualitteb2a6762014-12-04 11:35:33 -080070 }
71
mtklein36352bf2015-03-25 18:17:31 -070072 const char* name() const override { return "Big Ole Key"; }
joshualitteb2a6762014-12-04 11:35:33 -080073
egdaniel57d3b032015-11-13 11:57:27 -080074 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
robertphillips9cdb9922016-02-03 12:25:40 -080075 return new GLBigKeyProcessor;
joshualitteb2a6762014-12-04 11:35:33 -080076 }
77
Brian Salomonaff329b2017-08-11 09:40:37 -040078 std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(); }
Brian Salomonb17e6392017-07-28 13:41:51 -040079
joshualitteb2a6762014-12-04 11:35:33 -080080private:
Ethan Nicholasabff9562017-10-09 10:54:08 -040081 BigKeyProcessor() : INHERITED(kBigKeyProcessor_ClassID, kNone_OptimizationFlags) { }
Brian Salomon94efbf52016-11-29 13:43:05 -050082 virtual void onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -080083 GrProcessorKeyBuilder* b) const override {
wangyix4b3050b2015-08-04 07:59:37 -070084 GLBigKeyProcessor::GenKey(*this, caps, b);
85 }
mtklein36352bf2015-03-25 18:17:31 -070086 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
joshualitteb2a6762014-12-04 11:35:33 -080087
Brian Salomon0c26a9d2017-07-06 10:09:38 -040088 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
joshualitteb2a6762014-12-04 11:35:33 -080089
90 typedef GrFragmentProcessor INHERITED;
91};
92
93GR_DEFINE_FRAGMENT_PROCESSOR_TEST(BigKeyProcessor);
94
Hal Canary6f6961e2017-01-31 13:50:44 -050095#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -040096std::unique_ptr<GrFragmentProcessor> BigKeyProcessor::TestCreate(GrProcessorTestData*) {
bungeman06ca8ec2016-06-09 08:01:03 -070097 return BigKeyProcessor::Make();
joshualitteb2a6762014-12-04 11:35:33 -080098}
Hal Canary6f6961e2017-01-31 13:50:44 -050099#endif
joshualitteb2a6762014-12-04 11:35:33 -0800100
bsalomonb5b60322015-09-14 12:26:33 -0700101//////////////////////////////////////////////////////////////////////////////
102
103class BlockInputFragmentProcessor : public GrFragmentProcessor {
104public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400105 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp) {
106 return std::unique_ptr<GrFragmentProcessor>(new BlockInputFragmentProcessor(std::move(fp)));
bsalomonb5b60322015-09-14 12:26:33 -0700107 }
108
109 const char* name() const override { return "Block Input"; }
110
egdaniel57d3b032015-11-13 11:57:27 -0800111 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLFP; }
bsalomonb5b60322015-09-14 12:26:33 -0700112
Brian Salomonaff329b2017-08-11 09:40:37 -0400113 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400114 return Make(this->childProcessor(0).clone());
Brian Salomonb17e6392017-07-28 13:41:51 -0400115 }
116
bsalomonb5b60322015-09-14 12:26:33 -0700117private:
egdaniel64c47282015-11-13 06:54:19 -0800118 class GLFP : public GrGLSLFragmentProcessor {
bsalomonb5b60322015-09-14 12:26:33 -0700119 public:
120 void emitCode(EmitArgs& args) override {
Ethan Nicholas2983f402017-05-08 09:36:08 -0400121 this->emitChild(0, args);
bsalomonb5b60322015-09-14 12:26:33 -0700122 }
123
124 private:
egdaniel64c47282015-11-13 06:54:19 -0800125 typedef GrGLSLFragmentProcessor INHERITED;
bsalomonb5b60322015-09-14 12:26:33 -0700126 };
127
Brian Salomonaff329b2017-08-11 09:40:37 -0400128 BlockInputFragmentProcessor(std::unique_ptr<GrFragmentProcessor> child)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400129 : INHERITED(kBlockInputFragmentProcessor_ClassID, kNone_OptimizationFlags) {
bungeman06ca8ec2016-06-09 08:01:03 -0700130 this->registerChildProcessor(std::move(child));
bsalomonb5b60322015-09-14 12:26:33 -0700131 }
132
Brian Salomon94efbf52016-11-29 13:43:05 -0500133 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override {}
bsalomonb5b60322015-09-14 12:26:33 -0700134
135 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
136
bsalomonb5b60322015-09-14 12:26:33 -0700137 typedef GrFragmentProcessor INHERITED;
138};
139
140//////////////////////////////////////////////////////////////////////////////
141
joshualitt65171342014-10-09 07:25:36 -0700142/*
143 * Begin test code
144 */
145static const int kRenderTargetHeight = 1;
146static const int kRenderTargetWidth = 1;
147
Brian Osman11052242016-10-27 14:47:55 -0400148static sk_sp<GrRenderTargetContext> random_render_target_context(GrContext* context,
149 SkRandom* random,
150 const GrCaps* caps) {
robertphillips82ec6e52016-05-19 14:01:05 -0700151 GrSurfaceOrigin origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin
152 : kBottomLeft_GrSurfaceOrigin;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500153 int sampleCnt =
154 random->nextBool() ? caps->getRenderTargetSampleCount(2, kRGBA_8888_GrPixelConfig) : 1;
155 // Above could be 0 if msaa isn't supported.
156 sampleCnt = SkTMax(1, sampleCnt);
joshualitt6c891102015-05-13 08:51:49 -0700157
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500158 sk_sp<GrRenderTargetContext> renderTargetContext(
159 context->contextPriv().makeDeferredRenderTargetContext(SkBackingFit::kExact,
160 kRenderTargetWidth,
161 kRenderTargetHeight,
162 kRGBA_8888_GrPixelConfig,
163 nullptr,
164 sampleCnt,
165 GrMipMapped::kNo,
166 origin));
Brian Osman11052242016-10-27 14:47:55 -0400167 return renderTargetContext;
bsalomon@google.com91207482013-02-12 21:45:24 +0000168}
169
Hal Canary6f6961e2017-01-31 13:50:44 -0500170#if GR_TEST_UTILS
robertphillips28a838e2016-06-23 14:07:00 -0700171static void set_random_xpf(GrPaint* paint, GrProcessorTestData* d) {
Brian Salomona1633922017-01-09 11:46:10 -0500172 paint->setXPFactory(GrXPFactoryTestFactory::Get(d));
egdanielc2304142014-12-11 13:15:13 -0800173}
174
Brian Salomonaff329b2017-08-11 09:40:37 -0400175static std::unique_ptr<GrFragmentProcessor> create_random_proc_tree(GrProcessorTestData* d,
176 int minLevels, int maxLevels) {
wangyix059dffa2015-09-10 06:57:05 -0700177 SkASSERT(1 <= minLevels);
178 SkASSERT(minLevels <= maxLevels);
179
180 // Return a leaf node if maxLevels is 1 or if we randomly chose to terminate.
181 // If returning a leaf node, make sure that it doesn't have children (e.g. another
182 // GrComposeEffect)
183 const float terminateProbability = 0.3f;
184 if (1 == minLevels) {
185 bool terminate = (1 == maxLevels) || (d->fRandom->nextF() < terminateProbability);
186 if (terminate) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400187 std::unique_ptr<GrFragmentProcessor> fp;
wangyix059dffa2015-09-10 06:57:05 -0700188 while (true) {
Brian Salomon1c053642017-07-24 10:16:19 -0400189 fp = GrFragmentProcessorTestFactory::Make(d);
wangyix059dffa2015-09-10 06:57:05 -0700190 SkASSERT(fp);
191 if (0 == fp->numChildProcessors()) {
192 break;
193 }
wangyix059dffa2015-09-10 06:57:05 -0700194 }
195 return fp;
196 }
197 }
198 // If we didn't terminate, choose either the left or right subtree to fulfill
199 // the minLevels requirement of this tree; the other child can have as few levels as it wants.
Brian Salomona12c1532017-02-13 12:41:44 -0500200 // Also choose a random xfer mode.
wangyix059dffa2015-09-10 06:57:05 -0700201 if (minLevels > 1) {
202 --minLevels;
203 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400204 auto minLevelsChild = create_random_proc_tree(d, minLevels, maxLevels - 1);
205 std::unique_ptr<GrFragmentProcessor> otherChild(create_random_proc_tree(d, 1, maxLevels - 1));
Mike Reed7d954ad2016-10-28 15:42:34 -0400206 SkBlendMode mode = static_cast<SkBlendMode>(d->fRandom->nextRangeU(0,
Brian Salomona12c1532017-02-13 12:41:44 -0500207 (int)SkBlendMode::kLastMode));
Brian Salomonaff329b2017-08-11 09:40:37 -0400208 std::unique_ptr<GrFragmentProcessor> fp;
wangyix059dffa2015-09-10 06:57:05 -0700209 if (d->fRandom->nextF() < 0.5f) {
bungeman06ca8ec2016-06-09 08:01:03 -0700210 fp = GrXfermodeFragmentProcessor::MakeFromTwoProcessors(std::move(minLevelsChild),
211 std::move(otherChild), mode);
wangyix059dffa2015-09-10 06:57:05 -0700212 SkASSERT(fp);
213 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700214 fp = GrXfermodeFragmentProcessor::MakeFromTwoProcessors(std::move(otherChild),
215 std::move(minLevelsChild), mode);
wangyix059dffa2015-09-10 06:57:05 -0700216 SkASSERT(fp);
217 }
218 return fp;
219}
220
robertphillips28a838e2016-06-23 14:07:00 -0700221static void set_random_color_coverage_stages(GrPaint* paint,
222 GrProcessorTestData* d,
Greg Daniel78325c12017-06-19 16:39:13 -0400223 int maxStages,
224 int maxTreeLevels) {
wangyix059dffa2015-09-10 06:57:05 -0700225 // Randomly choose to either create a linear pipeline of procs or create one proc tree
226 const float procTreeProbability = 0.5f;
227 if (d->fRandom->nextF() < procTreeProbability) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400228 std::unique_ptr<GrFragmentProcessor> fp(create_random_proc_tree(d, 2, maxTreeLevels));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400229 if (fp) {
230 paint->addColorFragmentProcessor(std::move(fp));
231 }
wangyix059dffa2015-09-10 06:57:05 -0700232 } else {
233 int numProcs = d->fRandom->nextULessThan(maxStages + 1);
234 int numColorProcs = d->fRandom->nextULessThan(numProcs + 1);
joshualitt65171342014-10-09 07:25:36 -0700235
wangyix059dffa2015-09-10 06:57:05 -0700236 for (int s = 0; s < numProcs;) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400237 std::unique_ptr<GrFragmentProcessor> fp(GrFragmentProcessorTestFactory::Make(d));
wangyix059dffa2015-09-10 06:57:05 -0700238 SkASSERT(fp);
239
240 // finally add the stage to the correct pipeline in the drawstate
241 if (s < numColorProcs) {
robertphillips28a838e2016-06-23 14:07:00 -0700242 paint->addColorFragmentProcessor(std::move(fp));
wangyix059dffa2015-09-10 06:57:05 -0700243 } else {
robertphillips28a838e2016-06-23 14:07:00 -0700244 paint->addCoverageFragmentProcessor(std::move(fp));
wangyix059dffa2015-09-10 06:57:05 -0700245 }
246 ++s;
joshualitt65171342014-10-09 07:25:36 -0700247 }
joshualitt65171342014-10-09 07:25:36 -0700248 }
249}
250
Brian Salomon17726632017-05-12 14:09:46 -0400251static void set_random_state(GrPaint* paint, SkRandom* random) {
robertphillips28a838e2016-06-23 14:07:00 -0700252 if (random->nextBool()) {
253 paint->setDisableOutputConversionToSRGB(true);
joshualitt65171342014-10-09 07:25:36 -0700254 }
robertphillips28a838e2016-06-23 14:07:00 -0700255 if (random->nextBool()) {
256 paint->setAllowSRGBInputs(true);
joshualitt6c891102015-05-13 08:51:49 -0700257 }
joshualitt65171342014-10-09 07:25:36 -0700258}
259
Hal Canary6f6961e2017-01-31 13:50:44 -0500260#endif
joshualitt249af152014-09-15 11:41:13 -0700261
Hal Canary6f6961e2017-01-31 13:50:44 -0500262#if !GR_TEST_UTILS
263bool GrDrawingManager::ProgramUnitTest(GrContext*, int) { return true; }
264#else
Greg Daniel78325c12017-06-19 16:39:13 -0400265bool GrDrawingManager::ProgramUnitTest(GrContext* context, int maxStages, int maxLevels) {
csmartdaltonbde96c62016-08-31 12:54:46 -0700266 GrDrawingManager* drawingManager = context->contextPriv().drawingManager();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500267 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
robertphillipsa13e2022015-11-11 12:01:09 -0800268
Robert Phillipse78b7252017-04-06 07:59:41 -0400269 sk_sp<GrTextureProxy> proxies[2];
270
joshualitt65171342014-10-09 07:25:36 -0700271 // setup dummy textures
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500272 {
273 GrSurfaceDesc dummyDesc;
274 dummyDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500275 dummyDesc.fWidth = 34;
276 dummyDesc.fHeight = 18;
277 dummyDesc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomon2a4f9832018-03-03 22:43:43 -0500278 proxies[0] = proxyProvider->createProxy(dummyDesc, kBottomLeft_GrSurfaceOrigin,
279 SkBackingFit::kExact, SkBudgeted::kNo);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500280 }
281 {
282 GrSurfaceDesc dummyDesc;
283 dummyDesc.fFlags = kNone_GrSurfaceFlags;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500284 dummyDesc.fWidth = 16;
285 dummyDesc.fHeight = 22;
286 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
Brian Salomon2a4f9832018-03-03 22:43:43 -0500287 proxies[1] = proxyProvider->createProxy(dummyDesc, kTopLeft_GrSurfaceOrigin,
288 SkBackingFit::kExact, SkBudgeted::kNo);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500289 }
bsalomon@google.comd4726202012-08-03 14:34:46 +0000290
Robert Phillipse78b7252017-04-06 07:59:41 -0400291 if (!proxies[0] || !proxies[1]) {
joshualitt65171342014-10-09 07:25:36 -0700292 SkDebugf("Could not allocate dummy textures");
bsalomone904c092014-07-17 10:50:59 -0700293 return false;
294 }
295
joshualitt54e0c122014-11-19 09:38:51 -0800296 // dummy scissor state
bsalomon3e791242014-12-17 13:43:13 -0800297 GrScissorState scissor;
joshualitt54e0c122014-11-19 09:38:51 -0800298
Brian Osman9f772a42017-07-10 13:03:50 -0400299 SkRandom random;
bsalomonf3261af2016-04-05 10:57:13 -0700300 static const int NUM_TESTS = 1024;
joshualitt6c891102015-05-13 08:51:49 -0700301 for (int t = 0; t < NUM_TESTS; t++) {
joshualitt65171342014-10-09 07:25:36 -0700302 // setup random render target(can fail)
Brian Osman11052242016-10-27 14:47:55 -0400303 sk_sp<GrRenderTargetContext> renderTargetContext(random_render_target_context(
304 context, &random, context->caps()));
305 if (!renderTargetContext) {
306 SkDebugf("Could not allocate renderTargetContext");
joshualittf5883a62016-01-13 07:47:38 -0800307 return false;
308 }
robertphillips0dfa62c2015-11-16 06:23:31 -0800309
Brian Salomon17726632017-05-12 14:09:46 -0400310 GrPaint paint;
Robert Phillipse78b7252017-04-06 07:59:41 -0400311 GrProcessorTestData ptd(&random, context, renderTargetContext.get(), proxies);
Greg Daniel78325c12017-06-19 16:39:13 -0400312 set_random_color_coverage_stages(&paint, &ptd, maxStages, maxLevels);
Brian Salomon17726632017-05-12 14:09:46 -0400313 set_random_xpf(&paint, &ptd);
314 set_random_state(&paint, &random);
315 GrDrawRandomOp(&random, renderTargetContext.get(), std::move(paint));
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000316 }
joshualitt6c891102015-05-13 08:51:49 -0700317 // Flush everything, test passes if flush is successful(ie, no asserts are hit, no crashes)
Robert Phillips7ee385e2017-03-30 08:02:11 -0400318 drawingManager->flush(nullptr);
bsalomonb5b60322015-09-14 12:26:33 -0700319
320 // Validate that GrFPs work correctly without an input.
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500321 sk_sp<GrRenderTargetContext> renderTargetContext(
322 context->contextPriv().makeDeferredRenderTargetContext(SkBackingFit::kExact,
323 kRenderTargetWidth,
324 kRenderTargetHeight,
325 kRGBA_8888_GrPixelConfig,
326 nullptr));
Brian Osman11052242016-10-27 14:47:55 -0400327 if (!renderTargetContext) {
328 SkDebugf("Could not allocate a renderTargetContext");
robertphillips82ec6e52016-05-19 14:01:05 -0700329 return false;
330 }
331
Brian Salomon1c053642017-07-24 10:16:19 -0400332 int fpFactoryCnt = GrFragmentProcessorTestFactory::Count();
bsalomonb5b60322015-09-14 12:26:33 -0700333 for (int i = 0; i < fpFactoryCnt; ++i) {
334 // Since FP factories internally randomize, call each 10 times.
335 for (int j = 0; j < 10; ++j) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400336 GrProcessorTestData ptd(&random, context, renderTargetContext.get(), proxies);
bsalomonb5b60322015-09-14 12:26:33 -0700337
Brian Salomon17726632017-05-12 14:09:46 -0400338 GrPaint paint;
339 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Brian Salomonaff329b2017-08-11 09:40:37 -0400340 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &ptd);
341 auto blockFP = BlockInputFragmentProcessor::Make(std::move(fp));
Brian Salomon17726632017-05-12 14:09:46 -0400342 paint.addColorFragmentProcessor(std::move(blockFP));
343 GrDrawRandomOp(&random, renderTargetContext.get(), std::move(paint));
Robert Phillips7ee385e2017-03-30 08:02:11 -0400344 drawingManager->flush(nullptr);
bsalomonb5b60322015-09-14 12:26:33 -0700345 }
346 }
347
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000348 return true;
349}
Hal Canary6f6961e2017-01-31 13:50:44 -0500350#endif
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000351
Greg Danield895ca62017-06-19 14:39:43 -0400352static int get_glprograms_max_stages(const sk_gpu_test::ContextInfo& ctxInfo) {
353 GrContext* context = ctxInfo.grContext();
Robert Phillipsf35fd8d2018-01-22 10:48:15 -0500354 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->contextPriv().getGpu());
Brian Salomone334c592017-05-15 11:00:58 -0400355 int maxStages = 6;
356 if (kGLES_GrGLStandard == gpu->glStandard()) {
357 // We've had issues with driver crashes and HW limits being exceeded with many effects on
358 // Android devices. We have passes on ARM devices with the default number of stages.
359 // TODO When we run ES 3.00 GLSL in more places, test again
360#ifdef SK_BUILD_FOR_ANDROID
361 if (kARM_GrGLVendor != gpu->ctxInfo().vendor()) {
362 maxStages = 1;
363 }
kkinnunen3e980c32015-12-23 01:33:00 -0800364#endif
Brian Salomone334c592017-05-15 11:00:58 -0400365 // On iOS we can exceed the maximum number of varyings. http://skbug.com/6627.
Brian Osman4e1868c2017-05-26 15:56:32 -0400366#ifdef SK_BUILD_FOR_IOS
Brian Salomone334c592017-05-15 11:00:58 -0400367 maxStages = 3;
368#endif
369 }
Greg Danield895ca62017-06-19 14:39:43 -0400370 if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D9_ES2_ContextType ||
371 ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES2_ContextType) {
372 // On Angle D3D we will hit a limit of out variables if we use too many stages.
373 maxStages = 3;
374 }
Brian Salomone334c592017-05-15 11:00:58 -0400375 return maxStages;
376}
377
Greg Daniel78325c12017-06-19 16:39:13 -0400378static int get_glprograms_max_levels(const sk_gpu_test::ContextInfo& ctxInfo) {
379 // A full tree with 5 levels (31 nodes) may cause a program that exceeds shader limits
380 // (e.g. uniform or varying limits); maxTreeLevels should be a number from 1 to 4 inclusive.
381 int maxTreeLevels = 4;
382 // On iOS we can exceed the maximum number of varyings. http://skbug.com/6627.
383#ifdef SK_BUILD_FOR_IOS
384 maxTreeLevels = 2;
385#endif
386 if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D9_ES2_ContextType ||
387 ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES2_ContextType) {
388 // On Angle D3D we will hit a limit of out variables if we use too many stages.
389 maxTreeLevels = 2;
390 }
391 return maxTreeLevels;
392}
393
Brian Salomone334c592017-05-15 11:00:58 -0400394static void test_glprograms(skiatest::Reporter* reporter, const sk_gpu_test::ContextInfo& ctxInfo) {
Greg Danield895ca62017-06-19 14:39:43 -0400395 int maxStages = get_glprograms_max_stages(ctxInfo);
kkinnunen3e980c32015-12-23 01:33:00 -0800396 if (maxStages == 0) {
397 return;
398 }
Greg Daniel78325c12017-06-19 16:39:13 -0400399 int maxLevels = get_glprograms_max_levels(ctxInfo);
400 if (maxLevels == 0) {
401 return;
402 }
Brian Osman9f772a42017-07-10 13:03:50 -0400403
Greg Daniel78325c12017-06-19 16:39:13 -0400404 REPORTER_ASSERT(reporter, GrDrawingManager::ProgramUnitTest(ctxInfo.grContext(), maxStages,
405 maxLevels));
kkinnunen3e980c32015-12-23 01:33:00 -0800406}
407
Brian Salomondcfca432017-11-15 15:48:03 -0500408DEF_GPUTEST(GLPrograms, reporter, options) {
bsalomon3318ee72015-03-16 11:56:29 -0700409 // Set a locale that would cause shader compilation to fail because of , as decimal separator.
410 // skbug 3330
411#ifdef SK_BUILD_FOR_WIN
412 GrAutoLocaleSetter als("sv-SE");
413#else
414 GrAutoLocaleSetter als("sv_SE.UTF-8");
415#endif
416
joshualitt6c891102015-05-13 08:51:49 -0700417 // We suppress prints to avoid spew
Brian Salomondcfca432017-11-15 15:48:03 -0500418 GrContextOptions opts = options;
joshualitt6c891102015-05-13 08:51:49 -0700419 opts.fSuppressPrints = true;
bsalomon3724e572016-03-30 18:56:19 -0700420 sk_gpu_test::GrContextFactory debugFactory(opts);
Brian Salomone334c592017-05-15 11:00:58 -0400421 skiatest::RunWithGPUTestContexts(test_glprograms, &skiatest::IsRenderingGLContextType, reporter,
Brian Salomondcfca432017-11-15 15:48:03 -0500422 opts);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000423}
424
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000425#endif