blob: ba1079a7546016d2dc6b982fac54576e51aaed6d [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
Ben Wagnerf1344ac2019-05-10 15:01:53 -04008// This is a GPU-backend specific test.
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +00009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkTypes.h"
bsalomon@google.com2a48c3a2012-08-03 14:54:45 +000011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/SkChecksum.h"
13#include "include/utils/SkRandom.h"
14#include "src/gpu/GrAutoLocaleSetter.h"
15#include "src/gpu/GrContextPriv.h"
16#include "src/gpu/GrDrawOpTest.h"
17#include "src/gpu/GrDrawingManager.h"
18#include "src/gpu/GrPipeline.h"
19#include "src/gpu/GrRenderTargetContextPriv.h"
20#include "src/gpu/GrXferProcessor.h"
21#include "tests/Test.h"
22#include "tools/gpu/GrContextFactory.h"
joshualitt74417822015-08-07 11:42:16 -070023
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/ops/GrDrawOp.h"
joshualitt74417822015-08-07 11:42:16 -070025
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/effects/GrPorterDuffXferProcessor.h"
27#include "src/gpu/effects/GrXfermodeFragmentProcessor.h"
28#include "src/gpu/effects/generated/GrConfigConversionEffect.h"
joshualitt74417822015-08-07 11:42:16 -070029
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/gl/GrGLGpu.h"
31#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
32#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
33#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000034
joshualitt65171342014-10-09 07:25:36 -070035/*
bsalomon98b33eb2014-10-15 11:05:26 -070036 * A dummy processor which just tries to insert a massive key and verify that it can retrieve the
joshualitt65171342014-10-09 07:25:36 -070037 * whole thing correctly
38 */
39static const uint32_t kMaxKeySize = 1024;
40
egdaniel64c47282015-11-13 06:54:19 -080041class GLBigKeyProcessor : public GrGLSLFragmentProcessor {
joshualitt65171342014-10-09 07:25:36 -070042public:
robertphillips9cdb9922016-02-03 12:25:40 -080043 void emitCode(EmitArgs& args) override {
joshualitt6c891102015-05-13 08:51:49 -070044 // pass through
egdaniel4ca2e602015-11-18 08:01:26 -080045 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
bsalomonb5b60322015-09-14 12:26:33 -070046 if (args.fInputColor) {
egdaniel4ca2e602015-11-18 08:01:26 -080047 fragBuilder->codeAppendf("%s = %s;\n", args.fOutputColor, args.fInputColor);
bsalomonb5b60322015-09-14 12:26:33 -070048 } else {
egdaniel4ca2e602015-11-18 08:01:26 -080049 fragBuilder->codeAppendf("%s = vec4(1.0);\n", args.fOutputColor);
bsalomonb5b60322015-09-14 12:26:33 -070050 }
joshualitt6c891102015-05-13 08:51:49 -070051 }
joshualitt65171342014-10-09 07:25:36 -070052
Brian Salomon94efbf52016-11-29 13:43:05 -050053 static void GenKey(const GrProcessor&, const GrShaderCaps&, GrProcessorKeyBuilder* b) {
joshualitt65171342014-10-09 07:25:36 -070054 for (uint32_t i = 0; i < kMaxKeySize; i++) {
55 b->add32(i);
joshualittd9097592014-10-07 08:37:36 -070056 }
joshualittd9097592014-10-07 08:37:36 -070057 }
58
joshualitt65171342014-10-09 07:25:36 -070059private:
egdaniel64c47282015-11-13 06:54:19 -080060 typedef GrGLSLFragmentProcessor INHERITED;
joshualitt65171342014-10-09 07:25:36 -070061};
62
joshualitteb2a6762014-12-04 11:35:33 -080063class BigKeyProcessor : public GrFragmentProcessor {
64public:
Brian Salomonaff329b2017-08-11 09:40:37 -040065 static std::unique_ptr<GrFragmentProcessor> Make() {
66 return std::unique_ptr<GrFragmentProcessor>(new BigKeyProcessor);
joshualitteb2a6762014-12-04 11:35:33 -080067 }
68
mtklein36352bf2015-03-25 18:17:31 -070069 const char* name() const override { return "Big Ole Key"; }
joshualitteb2a6762014-12-04 11:35:33 -080070
egdaniel57d3b032015-11-13 11:57:27 -080071 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
robertphillips9cdb9922016-02-03 12:25:40 -080072 return new GLBigKeyProcessor;
joshualitteb2a6762014-12-04 11:35:33 -080073 }
74
Brian Salomonaff329b2017-08-11 09:40:37 -040075 std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(); }
Brian Salomonb17e6392017-07-28 13:41:51 -040076
joshualitteb2a6762014-12-04 11:35:33 -080077private:
Ethan Nicholasabff9562017-10-09 10:54:08 -040078 BigKeyProcessor() : INHERITED(kBigKeyProcessor_ClassID, kNone_OptimizationFlags) { }
Brian Salomon94efbf52016-11-29 13:43:05 -050079 virtual void onGetGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -080080 GrProcessorKeyBuilder* b) const override {
wangyix4b3050b2015-08-04 07:59:37 -070081 GLBigKeyProcessor::GenKey(*this, caps, b);
82 }
mtklein36352bf2015-03-25 18:17:31 -070083 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
joshualitteb2a6762014-12-04 11:35:33 -080084
Brian Salomon0c26a9d2017-07-06 10:09:38 -040085 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
joshualitteb2a6762014-12-04 11:35:33 -080086
87 typedef GrFragmentProcessor INHERITED;
88};
89
90GR_DEFINE_FRAGMENT_PROCESSOR_TEST(BigKeyProcessor);
91
Hal Canary6f6961e2017-01-31 13:50:44 -050092#if GR_TEST_UTILS
Brian Salomonaff329b2017-08-11 09:40:37 -040093std::unique_ptr<GrFragmentProcessor> BigKeyProcessor::TestCreate(GrProcessorTestData*) {
bungeman06ca8ec2016-06-09 08:01:03 -070094 return BigKeyProcessor::Make();
joshualitteb2a6762014-12-04 11:35:33 -080095}
Hal Canary6f6961e2017-01-31 13:50:44 -050096#endif
joshualitteb2a6762014-12-04 11:35:33 -080097
bsalomonb5b60322015-09-14 12:26:33 -070098//////////////////////////////////////////////////////////////////////////////
99
100class BlockInputFragmentProcessor : public GrFragmentProcessor {
101public:
Brian Salomonaff329b2017-08-11 09:40:37 -0400102 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp) {
103 return std::unique_ptr<GrFragmentProcessor>(new BlockInputFragmentProcessor(std::move(fp)));
bsalomonb5b60322015-09-14 12:26:33 -0700104 }
105
106 const char* name() const override { return "Block Input"; }
107
egdaniel57d3b032015-11-13 11:57:27 -0800108 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new GLFP; }
bsalomonb5b60322015-09-14 12:26:33 -0700109
Brian Salomonaff329b2017-08-11 09:40:37 -0400110 std::unique_ptr<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -0400111 return Make(this->childProcessor(0).clone());
Brian Salomonb17e6392017-07-28 13:41:51 -0400112 }
113
bsalomonb5b60322015-09-14 12:26:33 -0700114private:
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 {
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000118 this->emitChild(0, 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
Brian Salomonaff329b2017-08-11 09:40:37 -0400125 BlockInputFragmentProcessor(std::unique_ptr<GrFragmentProcessor> child)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400126 : INHERITED(kBlockInputFragmentProcessor_ClassID, kNone_OptimizationFlags) {
bungeman06ca8ec2016-06-09 08:01:03 -0700127 this->registerChildProcessor(std::move(child));
bsalomonb5b60322015-09-14 12:26:33 -0700128 }
129
Brian Salomon94efbf52016-11-29 13:43:05 -0500130 void onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override {}
bsalomonb5b60322015-09-14 12:26:33 -0700131
132 bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
133
bsalomonb5b60322015-09-14 12:26:33 -0700134 typedef GrFragmentProcessor INHERITED;
135};
136
137//////////////////////////////////////////////////////////////////////////////
138
joshualitt65171342014-10-09 07:25:36 -0700139/*
140 * Begin test code
141 */
142static const int kRenderTargetHeight = 1;
143static const int kRenderTargetWidth = 1;
144
Brian Osman11052242016-10-27 14:47:55 -0400145static sk_sp<GrRenderTargetContext> random_render_target_context(GrContext* context,
146 SkRandom* random,
147 const GrCaps* caps) {
robertphillips82ec6e52016-05-19 14:01:05 -0700148 GrSurfaceOrigin origin = random->nextBool() ? kTopLeft_GrSurfaceOrigin
149 : kBottomLeft_GrSurfaceOrigin;
joshualitt6c891102015-05-13 08:51:49 -0700150
Greg Daniel5c96db82019-07-09 14:06:58 -0400151 GrColorType ct = GrColorType::kRGBA_8888;
152 const GrBackendFormat format = caps->getBackendFormatFromColorType(ct);
Greg Daniel4065d452018-11-16 15:43:41 -0500153
Greg Daniel5c96db82019-07-09 14:06:58 -0400154 int sampleCnt = random->nextBool() ? caps->getRenderTargetSampleCount(2, ct, format) : 1;
Robert Phillipsd8f79a22019-06-24 13:25:42 -0400155 // Above could be 0 if msaa isn't supported.
156 sampleCnt = SkTMax(1, sampleCnt);
157
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500158 sk_sp<GrRenderTargetContext> renderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -0400159 context->priv().makeDeferredRenderTargetContext(SkBackingFit::kExact,
Brian Salomond6287472019-06-24 15:50:07 -0400160 kRenderTargetWidth,
161 kRenderTargetHeight,
Brian Salomond6287472019-06-24 15:50:07 -0400162 GrColorType::kRGBA_8888,
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);
Ben Wagnerf1344ac2019-05-10 15:01:53 -0400190 if (!fp) {
191 return nullptr;
192 }
wangyix059dffa2015-09-10 06:57:05 -0700193 if (0 == fp->numChildProcessors()) {
194 break;
195 }
wangyix059dffa2015-09-10 06:57:05 -0700196 }
197 return fp;
198 }
199 }
200 // If we didn't terminate, choose either the left or right subtree to fulfill
201 // the minLevels requirement of this tree; the other child can have as few levels as it wants.
Brian Salomona12c1532017-02-13 12:41:44 -0500202 // Also choose a random xfer mode.
wangyix059dffa2015-09-10 06:57:05 -0700203 if (minLevels > 1) {
204 --minLevels;
205 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400206 auto minLevelsChild = create_random_proc_tree(d, minLevels, maxLevels - 1);
207 std::unique_ptr<GrFragmentProcessor> otherChild(create_random_proc_tree(d, 1, maxLevels - 1));
Ben Wagnerf1344ac2019-05-10 15:01:53 -0400208 if (!minLevelsChild || !otherChild) {
209 return nullptr;
210 }
Mike Reed7d954ad2016-10-28 15:42:34 -0400211 SkBlendMode mode = static_cast<SkBlendMode>(d->fRandom->nextRangeU(0,
Brian Salomona12c1532017-02-13 12:41:44 -0500212 (int)SkBlendMode::kLastMode));
Brian Salomonaff329b2017-08-11 09:40:37 -0400213 std::unique_ptr<GrFragmentProcessor> fp;
wangyix059dffa2015-09-10 06:57:05 -0700214 if (d->fRandom->nextF() < 0.5f) {
bungeman06ca8ec2016-06-09 08:01:03 -0700215 fp = GrXfermodeFragmentProcessor::MakeFromTwoProcessors(std::move(minLevelsChild),
216 std::move(otherChild), mode);
wangyix059dffa2015-09-10 06:57:05 -0700217 SkASSERT(fp);
218 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700219 fp = GrXfermodeFragmentProcessor::MakeFromTwoProcessors(std::move(otherChild),
220 std::move(minLevelsChild), mode);
wangyix059dffa2015-09-10 06:57:05 -0700221 SkASSERT(fp);
222 }
223 return fp;
224}
225
robertphillips28a838e2016-06-23 14:07:00 -0700226static void set_random_color_coverage_stages(GrPaint* paint,
227 GrProcessorTestData* d,
Greg Daniel78325c12017-06-19 16:39:13 -0400228 int maxStages,
229 int maxTreeLevels) {
wangyix059dffa2015-09-10 06:57:05 -0700230 // Randomly choose to either create a linear pipeline of procs or create one proc tree
231 const float procTreeProbability = 0.5f;
232 if (d->fRandom->nextF() < procTreeProbability) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400233 std::unique_ptr<GrFragmentProcessor> fp(create_random_proc_tree(d, 2, maxTreeLevels));
Robert Phillips1c9686b2017-06-30 08:40:28 -0400234 if (fp) {
235 paint->addColorFragmentProcessor(std::move(fp));
236 }
wangyix059dffa2015-09-10 06:57:05 -0700237 } else {
238 int numProcs = d->fRandom->nextULessThan(maxStages + 1);
239 int numColorProcs = d->fRandom->nextULessThan(numProcs + 1);
joshualitt65171342014-10-09 07:25:36 -0700240
Ben Wagnerf1344ac2019-05-10 15:01:53 -0400241 for (int s = 0; s < numProcs; ++s) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400242 std::unique_ptr<GrFragmentProcessor> fp(GrFragmentProcessorTestFactory::Make(d));
Ben Wagnerf1344ac2019-05-10 15:01:53 -0400243 if (!fp) {
244 continue;
245 }
wangyix059dffa2015-09-10 06:57:05 -0700246 // finally add the stage to the correct pipeline in the drawstate
247 if (s < numColorProcs) {
robertphillips28a838e2016-06-23 14:07:00 -0700248 paint->addColorFragmentProcessor(std::move(fp));
wangyix059dffa2015-09-10 06:57:05 -0700249 } else {
robertphillips28a838e2016-06-23 14:07:00 -0700250 paint->addCoverageFragmentProcessor(std::move(fp));
wangyix059dffa2015-09-10 06:57:05 -0700251 }
joshualitt65171342014-10-09 07:25:36 -0700252 }
joshualitt65171342014-10-09 07:25:36 -0700253 }
254}
255
Hal Canary6f6961e2017-01-31 13:50:44 -0500256#endif
joshualitt249af152014-09-15 11:41:13 -0700257
Hal Canary6f6961e2017-01-31 13:50:44 -0500258#if !GR_TEST_UTILS
259bool GrDrawingManager::ProgramUnitTest(GrContext*, int) { return true; }
260#else
Greg Daniel78325c12017-06-19 16:39:13 -0400261bool GrDrawingManager::ProgramUnitTest(GrContext* context, int maxStages, int maxLevels) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500262 GrDrawingManager* drawingManager = context->priv().drawingManager();
263 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
robertphillipsa13e2022015-11-11 12:01:09 -0800264
Robert Phillipse78b7252017-04-06 07:59:41 -0400265 sk_sp<GrTextureProxy> proxies[2];
266
joshualitt65171342014-10-09 07:25:36 -0700267 // setup dummy textures
Brian Salomon4687bdd2019-05-09 16:28:04 -0400268 GrMipMapped mipMapped = GrMipMapped(context->priv().caps()->mipMapSupport());
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500269 {
270 GrSurfaceDesc dummyDesc;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500271 dummyDesc.fWidth = 34;
272 dummyDesc.fHeight = 18;
273 dummyDesc.fConfig = kRGBA_8888_GrPixelConfig;
Greg Daniel4065d452018-11-16 15:43:41 -0500274 const GrBackendFormat format =
Greg Daniel627d0532019-07-08 16:48:14 -0400275 context->priv().caps()->getBackendFormatFromColorType(GrColorType::kRGBA_8888);
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400276 proxies[0] = proxyProvider->createProxy(
277 format, dummyDesc, GrRenderable::kYes, kBottomLeft_GrSurfaceOrigin, mipMapped,
278 SkBackingFit::kExact, SkBudgeted::kNo, GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500279 }
280 {
281 GrSurfaceDesc dummyDesc;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500282 dummyDesc.fWidth = 16;
283 dummyDesc.fHeight = 22;
284 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
Greg Daniel4065d452018-11-16 15:43:41 -0500285 const GrBackendFormat format =
Greg Daniel627d0532019-07-08 16:48:14 -0400286 context->priv().caps()->getBackendFormatFromColorType(GrColorType::kAlpha_8);
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400287 proxies[1] = proxyProvider->createProxy(
288 format, dummyDesc, GrRenderable::kNo, kTopLeft_GrSurfaceOrigin, mipMapped,
289 SkBackingFit::kExact, SkBudgeted::kNo, GrInternalSurfaceFlags::kNone);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500290 }
bsalomon@google.comd4726202012-08-03 14:34:46 +0000291
Robert Phillipse78b7252017-04-06 07:59:41 -0400292 if (!proxies[0] || !proxies[1]) {
joshualitt65171342014-10-09 07:25:36 -0700293 SkDebugf("Could not allocate dummy textures");
bsalomone904c092014-07-17 10:50:59 -0700294 return false;
295 }
296
Brian Salomond818ebf2018-07-02 14:08:49 +0000297 // dummy scissor state
298 GrScissorState scissor;
299
Brian Osman9f772a42017-07-10 13:03:50 -0400300 SkRandom random;
bsalomonf3261af2016-04-05 10:57:13 -0700301 static const int NUM_TESTS = 1024;
joshualitt6c891102015-05-13 08:51:49 -0700302 for (int t = 0; t < NUM_TESTS; t++) {
joshualitt65171342014-10-09 07:25:36 -0700303 // setup random render target(can fail)
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400304 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips9da87e02019-02-04 13:26:26 -0500305 random_render_target_context(context, &random, context->priv().caps()));
Brian Osman11052242016-10-27 14:47:55 -0400306 if (!renderTargetContext) {
307 SkDebugf("Could not allocate renderTargetContext");
joshualittf5883a62016-01-13 07:47:38 -0800308 return false;
309 }
robertphillips0dfa62c2015-11-16 06:23:31 -0800310
Brian Salomon17726632017-05-12 14:09:46 -0400311 GrPaint paint;
Robert Phillipse78b7252017-04-06 07:59:41 -0400312 GrProcessorTestData ptd(&random, context, renderTargetContext.get(), proxies);
Greg Daniel78325c12017-06-19 16:39:13 -0400313 set_random_color_coverage_stages(&paint, &ptd, maxStages, maxLevels);
Brian Salomon17726632017-05-12 14:09:46 -0400314 set_random_xpf(&paint, &ptd);
Brian Salomon17726632017-05-12 14:09:46 -0400315 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)
Greg Daniel797efca2019-05-09 14:04:20 -0400318 drawingManager->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(),
319 GrPrepareForExternalIORequests());
bsalomonb5b60322015-09-14 12:26:33 -0700320
321 // Validate that GrFPs work correctly without an input.
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500322 sk_sp<GrRenderTargetContext> renderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -0400323 context->priv().makeDeferredRenderTargetContext(SkBackingFit::kExact,
Brian Salomond6287472019-06-24 15:50:07 -0400324 kRenderTargetWidth,
325 kRenderTargetHeight,
Brian Salomond6287472019-06-24 15:50:07 -0400326 GrColorType::kRGBA_8888,
327 nullptr));
Brian Osman11052242016-10-27 14:47:55 -0400328 if (!renderTargetContext) {
329 SkDebugf("Could not allocate a renderTargetContext");
robertphillips82ec6e52016-05-19 14:01:05 -0700330 return false;
331 }
332
Brian Salomon1c053642017-07-24 10:16:19 -0400333 int fpFactoryCnt = GrFragmentProcessorTestFactory::Count();
bsalomonb5b60322015-09-14 12:26:33 -0700334 for (int i = 0; i < fpFactoryCnt; ++i) {
335 // Since FP factories internally randomize, call each 10 times.
336 for (int j = 0; j < 10; ++j) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400337 GrProcessorTestData ptd(&random, context, renderTargetContext.get(), proxies);
bsalomonb5b60322015-09-14 12:26:33 -0700338
Brian Salomon17726632017-05-12 14:09:46 -0400339 GrPaint paint;
340 paint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
Brian Salomonaff329b2017-08-11 09:40:37 -0400341 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &ptd);
342 auto blockFP = BlockInputFragmentProcessor::Make(std::move(fp));
Brian Salomon17726632017-05-12 14:09:46 -0400343 paint.addColorFragmentProcessor(std::move(blockFP));
344 GrDrawRandomOp(&random, renderTargetContext.get(), std::move(paint));
Brian Salomonf9a1fdf2019-05-09 10:30:12 -0400345 drawingManager->flush(nullptr, 0, SkSurface::BackendSurfaceAccess::kNoAccess,
Greg Daniel797efca2019-05-09 14:04:20 -0400346 GrFlushInfo(), GrPrepareForExternalIORequests());
bsalomonb5b60322015-09-14 12:26:33 -0700347 }
348 }
349
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000350 return true;
351}
Hal Canary6f6961e2017-01-31 13:50:44 -0500352#endif
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000353
Greg Danield895ca62017-06-19 14:39:43 -0400354static int get_glprograms_max_stages(const sk_gpu_test::ContextInfo& ctxInfo) {
355 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500356 GrGLGpu* gpu = static_cast<GrGLGpu*>(context->priv().getGpu());
Brian Salomone334c592017-05-15 11:00:58 -0400357 int maxStages = 6;
358 if (kGLES_GrGLStandard == gpu->glStandard()) {
359 // We've had issues with driver crashes and HW limits being exceeded with many effects on
360 // Android devices. We have passes on ARM devices with the default number of stages.
361 // TODO When we run ES 3.00 GLSL in more places, test again
362#ifdef SK_BUILD_FOR_ANDROID
363 if (kARM_GrGLVendor != gpu->ctxInfo().vendor()) {
364 maxStages = 1;
365 }
kkinnunen3e980c32015-12-23 01:33:00 -0800366#endif
Brian Salomone334c592017-05-15 11:00:58 -0400367 // On iOS we can exceed the maximum number of varyings. http://skbug.com/6627.
Brian Osman4e1868c2017-05-26 15:56:32 -0400368#ifdef SK_BUILD_FOR_IOS
Brian Salomone334c592017-05-15 11:00:58 -0400369 maxStages = 3;
370#endif
371 }
Greg Danield895ca62017-06-19 14:39:43 -0400372 if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D9_ES2_ContextType ||
373 ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES2_ContextType) {
374 // On Angle D3D we will hit a limit of out variables if we use too many stages.
375 maxStages = 3;
376 }
Brian Salomone334c592017-05-15 11:00:58 -0400377 return maxStages;
378}
379
Greg Daniel78325c12017-06-19 16:39:13 -0400380static int get_glprograms_max_levels(const sk_gpu_test::ContextInfo& ctxInfo) {
381 // A full tree with 5 levels (31 nodes) may cause a program that exceeds shader limits
382 // (e.g. uniform or varying limits); maxTreeLevels should be a number from 1 to 4 inclusive.
383 int maxTreeLevels = 4;
384 // On iOS we can exceed the maximum number of varyings. http://skbug.com/6627.
385#ifdef SK_BUILD_FOR_IOS
386 maxTreeLevels = 2;
387#endif
388 if (ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D9_ES2_ContextType ||
389 ctxInfo.type() == sk_gpu_test::GrContextFactory::kANGLE_D3D11_ES2_ContextType) {
390 // On Angle D3D we will hit a limit of out variables if we use too many stages.
391 maxTreeLevels = 2;
392 }
393 return maxTreeLevels;
394}
395
Brian Salomone334c592017-05-15 11:00:58 -0400396static void test_glprograms(skiatest::Reporter* reporter, const sk_gpu_test::ContextInfo& ctxInfo) {
Greg Danield895ca62017-06-19 14:39:43 -0400397 int maxStages = get_glprograms_max_stages(ctxInfo);
kkinnunen3e980c32015-12-23 01:33:00 -0800398 if (maxStages == 0) {
399 return;
400 }
Greg Daniel78325c12017-06-19 16:39:13 -0400401 int maxLevels = get_glprograms_max_levels(ctxInfo);
402 if (maxLevels == 0) {
403 return;
404 }
Brian Osman9f772a42017-07-10 13:03:50 -0400405
Greg Daniel78325c12017-06-19 16:39:13 -0400406 REPORTER_ASSERT(reporter, GrDrawingManager::ProgramUnitTest(ctxInfo.grContext(), maxStages,
407 maxLevels));
kkinnunen3e980c32015-12-23 01:33:00 -0800408}
409
Brian Salomondcfca432017-11-15 15:48:03 -0500410DEF_GPUTEST(GLPrograms, reporter, options) {
bsalomon3318ee72015-03-16 11:56:29 -0700411 // Set a locale that would cause shader compilation to fail because of , as decimal separator.
412 // skbug 3330
413#ifdef SK_BUILD_FOR_WIN
414 GrAutoLocaleSetter als("sv-SE");
415#else
416 GrAutoLocaleSetter als("sv_SE.UTF-8");
417#endif
418
joshualitt6c891102015-05-13 08:51:49 -0700419 // We suppress prints to avoid spew
Brian Salomondcfca432017-11-15 15:48:03 -0500420 GrContextOptions opts = options;
joshualitt6c891102015-05-13 08:51:49 -0700421 opts.fSuppressPrints = true;
bsalomon3724e572016-03-30 18:56:19 -0700422 sk_gpu_test::GrContextFactory debugFactory(opts);
Brian Salomone334c592017-05-15 11:00:58 -0400423 skiatest::RunWithGPUTestContexts(test_glprograms, &skiatest::IsRenderingGLContextType, reporter,
Brian Salomondcfca432017-11-15 15:48:03 -0500424 opts);
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000425}
426