blob: dd731e07732a86904386bc30ed1220690fe26657 [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
bsalomon@google.com67b915d2013-02-04 16:13:32 +000015#include "GrContextFactory.h"
egdaniel605dd0f2014-11-12 08:35:25 -080016#include "GrInvariantOutput.h"
egdanielc0648242014-09-22 13:17:02 -070017#include "GrOptDrawState.h"
joshualitt2c93efe2014-11-06 12:57:13 -080018#include "GrTBackendProcessorFactory.h"
19#include "GrTest.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000020#include "SkChecksum.h"
tfarina@chromium.org223137f2012-11-21 22:38:36 +000021#include "SkRandom.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000022#include "Test.h"
joshualitt2c93efe2014-11-06 12:57:13 -080023#include "effects/GrConfigConversionEffect.h"
24#include "gl/GrGLPathRendering.h"
25#include "gl/GrGpuGL.h"
26#include "gl/builders/GrGLProgramBuilder.h"
bsalomon@google.comc3841b92012-08-02 18:11:43 +000027
joshualitt65171342014-10-09 07:25:36 -070028/*
bsalomon98b33eb2014-10-15 11:05:26 -070029 * A dummy processor which just tries to insert a massive key and verify that it can retrieve the
joshualitt65171342014-10-09 07:25:36 -070030 * whole thing correctly
31 */
32static const uint32_t kMaxKeySize = 1024;
33
34class GLBigKeyProcessor;
35
36class BigKeyProcessor : public GrFragmentProcessor {
37public:
38 static GrFragmentProcessor* Create() {
bsalomon98b33eb2014-10-15 11:05:26 -070039 GR_CREATE_STATIC_PROCESSOR(gBigKeyProcessor, BigKeyProcessor, ())
joshualitt65171342014-10-09 07:25:36 -070040 return SkRef(gBigKeyProcessor);
joshualittbd769d02014-09-04 08:56:46 -070041 }
joshualitt65171342014-10-09 07:25:36 -070042
43 static const char* Name() { return "Big ol' Key"; }
44
45 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE {
46 return GrTBackendFragmentProcessorFactory<BigKeyProcessor>::getInstance();
joshualittd9097592014-10-07 08:37:36 -070047 }
joshualitt65171342014-10-09 07:25:36 -070048
49 typedef GLBigKeyProcessor GLProcessor;
50
51private:
52 BigKeyProcessor() { }
bsalomon0e08fc12014-10-15 08:19:04 -070053 virtual bool onIsEqual(const GrFragmentProcessor&) const SK_OVERRIDE { return true; }
egdaniel605dd0f2014-11-12 08:35:25 -080054 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE { }
joshualitt65171342014-10-09 07:25:36 -070055
56 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
57
58 typedef GrFragmentProcessor INHERITED;
59};
60
61GR_DEFINE_FRAGMENT_PROCESSOR_TEST(BigKeyProcessor);
62
63GrFragmentProcessor* BigKeyProcessor::TestCreate(SkRandom*,
64 GrContext*,
65 const GrDrawTargetCaps&,
66 GrTexture*[]) {
67 return BigKeyProcessor::Create();
joshualittd9097592014-10-07 08:37:36 -070068}
69
joshualitt65171342014-10-09 07:25:36 -070070class GLBigKeyProcessor : public GrGLFragmentProcessor {
71public:
72 GLBigKeyProcessor(const GrBackendProcessorFactory& factory, const GrProcessor&)
73 : INHERITED(factory) {}
joshualittd9097592014-10-07 08:37:36 -070074
joshualitt15988992014-10-09 15:04:05 -070075 virtual void emitCode(GrGLFPBuilder* builder,
joshualitt65171342014-10-09 07:25:36 -070076 const GrFragmentProcessor& fp,
77 const GrProcessorKey& key,
78 const char* outputColor,
79 const char* inputColor,
80 const TransformedCoordsArray&,
81 const TextureSamplerArray&) {
82 for (uint32_t i = 0; i < kMaxKeySize; i++) {
83 SkASSERT(key.get32(i) == i);
joshualittd9097592014-10-07 08:37:36 -070084 }
joshualitt65171342014-10-09 07:25:36 -070085 }
86
87 static void GenKey(const GrProcessor& processor, const GrGLCaps&, GrProcessorKeyBuilder* b) {
88 for (uint32_t i = 0; i < kMaxKeySize; i++) {
89 b->add32(i);
joshualittd9097592014-10-07 08:37:36 -070090 }
joshualittd9097592014-10-07 08:37:36 -070091 }
92
joshualitt65171342014-10-09 07:25:36 -070093private:
94 typedef GrGLFragmentProcessor INHERITED;
95};
96
97/*
98 * Begin test code
99 */
100static const int kRenderTargetHeight = 1;
101static const int kRenderTargetWidth = 1;
102
joshualitt2c93efe2014-11-06 12:57:13 -0800103static GrRenderTarget* random_render_target(GrContext* context,
joshualitt65171342014-10-09 07:25:36 -0700104 const GrCacheID& cacheId,
105 SkRandom* random) {
106 // setup render target
107 GrTextureParams params;
bsalomonf2703d82014-10-28 14:33:06 -0700108 GrSurfaceDesc texDesc;
joshualitt65171342014-10-09 07:25:36 -0700109 texDesc.fWidth = kRenderTargetWidth;
110 texDesc.fHeight = kRenderTargetHeight;
bsalomonf2703d82014-10-28 14:33:06 -0700111 texDesc.fFlags = kRenderTarget_GrSurfaceFlag;
joshualitt65171342014-10-09 07:25:36 -0700112 texDesc.fConfig = kRGBA_8888_GrPixelConfig;
113 texDesc.fOrigin = random->nextBool() == true ? kTopLeft_GrSurfaceOrigin :
114 kBottomLeft_GrSurfaceOrigin;
115
joshualitt2c93efe2014-11-06 12:57:13 -0800116 SkAutoTUnref<GrTexture> texture(context->findAndRefTexture(texDesc, cacheId, &params));
bsalomond27726e2014-10-12 05:40:00 -0700117 if (!texture) {
joshualitt2c93efe2014-11-06 12:57:13 -0800118 texture.reset(context->createTexture(&params, texDesc, cacheId, 0, 0));
bsalomond27726e2014-10-12 05:40:00 -0700119 if (!texture) {
joshualitt65171342014-10-09 07:25:36 -0700120 return NULL;
joshualittd9097592014-10-07 08:37:36 -0700121 }
joshualittd9097592014-10-07 08:37:36 -0700122 }
bsalomond27726e2014-10-12 05:40:00 -0700123 return SkRef(texture->asRenderTarget());
bsalomon@google.com91207482013-02-12 21:45:24 +0000124}
125
joshualitt249af152014-09-15 11:41:13 -0700126// TODO clean this up, we have to do this to test geometry processors but there has got to be
127// a better way. In the mean time, we actually fill out these generic vertex attribs below with
128// the correct vertex attribs from the GP. We have to ensure, however, we don't try to add more
joshualitt65171342014-10-09 07:25:36 -0700129// than two attributes. In addition, we 'pad' the below array with GPs up to 6 entries, 4 fixed
130// function vertex attributes and 2 GP custom attributes.
131GrVertexAttrib kGenericVertexAttribs[] = {
joshualitt249af152014-09-15 11:41:13 -0700132 { kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding },
joshualittb0a8a372014-09-23 09:50:21 -0700133 { kVec2f_GrVertexAttribType, 0, kGeometryProcessor_GrVertexAttribBinding },
joshualitt65171342014-10-09 07:25:36 -0700134 { kVec2f_GrVertexAttribType, 0, kGeometryProcessor_GrVertexAttribBinding },
135 { kVec2f_GrVertexAttribType, 0, kGeometryProcessor_GrVertexAttribBinding },
136 { kVec2f_GrVertexAttribType, 0, kGeometryProcessor_GrVertexAttribBinding },
joshualittb0a8a372014-09-23 09:50:21 -0700137 { kVec2f_GrVertexAttribType, 0, kGeometryProcessor_GrVertexAttribBinding }
joshualitt249af152014-09-15 11:41:13 -0700138};
139
140/*
141 * convert sl type to vertexattrib type, not a complete implementation, only use for debugging
142 */
joshualitt65171342014-10-09 07:25:36 -0700143static GrVertexAttribType convert_sltype_to_attribtype(GrSLType type) {
joshualitt249af152014-09-15 11:41:13 -0700144 switch (type) {
145 case kFloat_GrSLType:
146 return kFloat_GrVertexAttribType;
147 case kVec2f_GrSLType:
148 return kVec2f_GrVertexAttribType;
149 case kVec3f_GrSLType:
150 return kVec3f_GrVertexAttribType;
151 case kVec4f_GrSLType:
152 return kVec4f_GrVertexAttribType;
153 default:
154 SkFAIL("Type isn't convertible");
155 return kFloat_GrVertexAttribType;
156 }
157}
joshualitt65171342014-10-09 07:25:36 -0700158// end test hack
joshualitt249af152014-09-15 11:41:13 -0700159
joshualitt65171342014-10-09 07:25:36 -0700160static void setup_random_ff_attribute(GrVertexAttribBinding binding, GrVertexAttribType type,
161 SkRandom* random, int* attribIndex, int* runningStride) {
162 if (random->nextBool()) {
163 kGenericVertexAttribs[*attribIndex].fType = type;
164 kGenericVertexAttribs[*attribIndex].fOffset = *runningStride;
165 kGenericVertexAttribs[*attribIndex].fBinding = binding;
166 *runningStride += GrVertexAttribTypeSize(kGenericVertexAttribs[(*attribIndex)++].fType);
167 }
168}
169
joshualitt2c93efe2014-11-06 12:57:13 -0800170static void set_random_gp(GrContext* context,
171 const GrDrawTargetCaps& caps,
172 GrDrawState* ds,
173 SkRandom* random,
174 GrTexture* dummyTextures[]) {
joshualitt65171342014-10-09 07:25:36 -0700175 GrProgramElementRef<const GrGeometryProcessor> gp(
176 GrProcessorTestFactory<GrGeometryProcessor>::CreateStage(random,
joshualitt2c93efe2014-11-06 12:57:13 -0800177 context,
178 caps,
joshualitt65171342014-10-09 07:25:36 -0700179 dummyTextures));
180 SkASSERT(gp);
181
182 // we have to set dummy vertex attributes, first we setup the fixed function attributes
183 // always leave the position attribute untouched in the array
184 int attribIndex = 1;
185 int runningStride = GrVertexAttribTypeSize(kGenericVertexAttribs[0].fType);
186
187 // local coords
188 setup_random_ff_attribute(kLocalCoord_GrVertexAttribBinding, kVec2f_GrVertexAttribType,
189 random, &attribIndex, &runningStride);
190
191 // color
192 setup_random_ff_attribute(kColor_GrVertexAttribBinding, kVec4f_GrVertexAttribType,
193 random, &attribIndex, &runningStride);
194
195 // coverage
egdaniel37b4d862014-11-03 10:07:07 -0800196 setup_random_ff_attribute(kCoverage_GrVertexAttribBinding, kUByte_GrVertexAttribType,
joshualitt65171342014-10-09 07:25:36 -0700197 random, &attribIndex, &runningStride);
198
199 // Update the geometry processor attributes
200 const GrGeometryProcessor::VertexAttribArray& v = gp->getVertexAttribs();
201 int numGPAttribs = v.count();
202 SkASSERT(numGPAttribs <= GrGeometryProcessor::kMaxVertexAttribs &&
203 GrGeometryProcessor::kMaxVertexAttribs == 2);
204
205 // we actually can't overflow if kMaxVertexAttribs == 2, but GCC 4.8 wants more proof
206 int maxIndex = SK_ARRAY_COUNT(kGenericVertexAttribs);
207 for (int i = 0; i < numGPAttribs && i + attribIndex < maxIndex; i++) {
208 kGenericVertexAttribs[i + attribIndex].fType =
209 convert_sltype_to_attribtype(v[i].getType());
210 kGenericVertexAttribs[i + attribIndex].fOffset = runningStride;
211 kGenericVertexAttribs[i + attribIndex].fBinding = kGeometryProcessor_GrVertexAttribBinding;
212 runningStride += GrVertexAttribTypeSize(kGenericVertexAttribs[i + attribIndex].fType);
213 }
214
215 // update the vertex attributes with the ds
joshualitt65171342014-10-09 07:25:36 -0700216 ds->setVertexAttribs<kGenericVertexAttribs>(attribIndex + numGPAttribs, runningStride);
217 ds->setGeometryProcessor(gp);
218}
219
220static void set_random_color_coverage_stages(GrGpuGL* gpu,
joshualitt2c93efe2014-11-06 12:57:13 -0800221 GrDrawState* ds,
joshualitt65171342014-10-09 07:25:36 -0700222 int maxStages,
223 bool usePathRendering,
224 SkRandom* random,
225 GrTexture* dummyTextures[]) {
226 int numProcs = random->nextULessThan(maxStages + 1);
227 int numColorProcs = random->nextULessThan(numProcs + 1);
228
229 int currTextureCoordSet = 0;
230 for (int s = 0; s < numProcs;) {
231 GrProgramElementRef<GrFragmentProcessor> fp(
232 GrProcessorTestFactory<GrFragmentProcessor>::CreateStage(random,
233 gpu->getContext(),
234 *gpu->caps(),
235 dummyTextures));
236 SkASSERT(fp);
237
238 // don't add dst color reads to coverage stage
239 if (s >= numColorProcs && fp->willReadDstColor()) {
240 continue;
241 }
242
243 // If adding this effect would exceed the max texture coord set count then generate a
244 // new random effect.
245 if (usePathRendering && gpu->glPathRendering()->texturingMode() ==
246 GrGLPathRendering::FixedFunction_TexturingMode) {;
247 int numTransforms = fp->numTransforms();
248 if (currTextureCoordSet + numTransforms >
249 gpu->glCaps().maxFixedFunctionTextureCoords()) {
250 continue;
251 }
252 currTextureCoordSet += numTransforms;
253 }
254
255 // finally add the stage to the correct pipeline in the drawstate
joshualitt65171342014-10-09 07:25:36 -0700256 if (s < numColorProcs) {
257 ds->addColorProcessor(fp);
258 } else {
259 ds->addCoverageProcessor(fp);
260 }
261 ++s;
262 }
263}
264
265// There are only a few cases of random colors which interest us
266enum ColorMode {
267 kAllOnes_ColorMode,
268 kAllZeros_ColorMode,
269 kAlphaOne_ColorMode,
270 kRandom_ColorMode,
271 kLast_ColorMode = kRandom_ColorMode
272};
273
joshualitt2c93efe2014-11-06 12:57:13 -0800274static void set_random_color(GrDrawState* ds, SkRandom* random) {
joshualitt65171342014-10-09 07:25:36 -0700275 ColorMode colorMode = ColorMode(random->nextULessThan(kLast_ColorMode + 1));
276 GrColor color;
277 switch (colorMode) {
278 case kAllOnes_ColorMode:
279 color = GrColorPackRGBA(0xFF, 0xFF, 0xFF, 0xFF);
280 break;
281 case kAllZeros_ColorMode:
282 color = GrColorPackRGBA(0, 0, 0, 0);
283 break;
284 case kAlphaOne_ColorMode:
285 color = GrColorPackRGBA(random->nextULessThan(256),
286 random->nextULessThan(256),
287 random->nextULessThan(256),
288 0xFF);
289 break;
290 case kRandom_ColorMode:
291 uint8_t alpha = random->nextULessThan(256);
292 color = GrColorPackRGBA(random->nextRangeU(0, alpha),
293 random->nextRangeU(0, alpha),
294 random->nextRangeU(0, alpha),
295 alpha);
296 break;
297 }
298 GrColorIsPMAssert(color);
joshualitt2c93efe2014-11-06 12:57:13 -0800299 ds->setColor(color);
joshualitt65171342014-10-09 07:25:36 -0700300}
301
302// There are only a few cases of random coverages which interest us
303enum CoverageMode {
304 kZero_CoverageMode,
305 kFF_CoverageMode,
306 kRandom_CoverageMode,
307 kLast_CoverageMode = kRandom_CoverageMode
308};
309
joshualitt2c93efe2014-11-06 12:57:13 -0800310static void set_random_coverage(GrDrawState* ds, SkRandom* random) {
joshualitt65171342014-10-09 07:25:36 -0700311 CoverageMode coverageMode = CoverageMode(random->nextULessThan(kLast_CoverageMode + 1));
312 uint8_t coverage;
313 switch (coverageMode) {
314 case kZero_CoverageMode:
315 coverage = 0;
316 break;
317 case kFF_CoverageMode:
318 coverage = 0xFF;
319 break;
320 case kRandom_CoverageMode:
321 coverage = uint8_t(random->nextU());
322 break;
323 }
joshualitt2c93efe2014-11-06 12:57:13 -0800324 ds->setCoverage(coverage);
joshualitt65171342014-10-09 07:25:36 -0700325}
326
joshualitt2c93efe2014-11-06 12:57:13 -0800327static void set_random_hints(GrDrawState* ds, SkRandom* random) {
joshualitt65171342014-10-09 07:25:36 -0700328 for (int i = 1; i <= GrDrawState::kLast_Hint; i <<= 1) {
joshualitt2c93efe2014-11-06 12:57:13 -0800329 ds->setHint(GrDrawState::Hints(i), random->nextBool());
joshualitt65171342014-10-09 07:25:36 -0700330 }
331}
332
joshualitt2c93efe2014-11-06 12:57:13 -0800333static void set_random_state(GrDrawState* ds, SkRandom* random) {
joshualitt65171342014-10-09 07:25:36 -0700334 int state = 0;
joshualitt7a6184f2014-10-29 18:29:27 -0700335 for (int i = 1; i <= GrDrawState::kLast_StateBit; i <<= 1) {
joshualitt65171342014-10-09 07:25:36 -0700336 state |= random->nextBool() * i;
337 }
joshualitt2c93efe2014-11-06 12:57:13 -0800338 ds->enableState(state);
joshualitt65171342014-10-09 07:25:36 -0700339}
340
341// this function will randomly pick non-self referencing blend modes
joshualitt2c93efe2014-11-06 12:57:13 -0800342static void set_random_blend_func(GrDrawState* ds, SkRandom* random) {
joshualitt65171342014-10-09 07:25:36 -0700343 GrBlendCoeff src;
344 do {
345 src = GrBlendCoeff(random->nextRangeU(kFirstPublicGrBlendCoeff, kLastPublicGrBlendCoeff));
346 } while (GrBlendCoeffRefsSrc(src));
347
348 GrBlendCoeff dst;
349 do {
350 dst = GrBlendCoeff(random->nextRangeU(kFirstPublicGrBlendCoeff, kLastPublicGrBlendCoeff));
351 } while (GrBlendCoeffRefsDst(dst));
352
joshualitt2c93efe2014-11-06 12:57:13 -0800353 ds->setBlendFunc(src, dst);
joshualitt65171342014-10-09 07:25:36 -0700354}
355
356// right now, the only thing we seem to care about in drawState's stencil is 'doesWrite()'
joshualitt2c93efe2014-11-06 12:57:13 -0800357static void set_random_stencil(GrDrawState* ds, SkRandom* random) {
joshualitt65171342014-10-09 07:25:36 -0700358 GR_STATIC_CONST_SAME_STENCIL(kDoesWriteStencil,
359 kReplace_StencilOp,
360 kReplace_StencilOp,
361 kAlways_StencilFunc,
362 0xffff,
363 0xffff,
364 0xffff);
365 GR_STATIC_CONST_SAME_STENCIL(kDoesNotWriteStencil,
366 kKeep_StencilOp,
367 kKeep_StencilOp,
368 kNever_StencilFunc,
369 0xffff,
370 0xffff,
371 0xffff);
372
373 if (random->nextBool()) {
joshualitt2c93efe2014-11-06 12:57:13 -0800374 ds->setStencil(kDoesWriteStencil);
joshualitt65171342014-10-09 07:25:36 -0700375 } else {
joshualitt2c93efe2014-11-06 12:57:13 -0800376 ds->setStencil(kDoesNotWriteStencil);
joshualitt65171342014-10-09 07:25:36 -0700377 }
378}
joshualitt249af152014-09-15 11:41:13 -0700379
joshualitt2c93efe2014-11-06 12:57:13 -0800380bool GrDrawTarget::programUnitTest(int maxStages) {
381 GrGpuGL* gpu = static_cast<GrGpuGL*>(fContext->getGpu());
joshualitt65171342014-10-09 07:25:36 -0700382 // setup dummy textures
bsalomonf2703d82014-10-28 14:33:06 -0700383 GrSurfaceDesc dummyDesc;
384 dummyDesc.fFlags = kRenderTarget_GrSurfaceFlag;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000385 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000386 dummyDesc.fWidth = 34;
387 dummyDesc.fHeight = 18;
joshualitt2c93efe2014-11-06 12:57:13 -0800388 SkAutoTUnref<GrTexture> dummyTexture1(gpu->createTexture(dummyDesc, NULL, 0));
bsalomonf2703d82014-10-28 14:33:06 -0700389 dummyDesc.fFlags = kNone_GrSurfaceFlags;
bsalomon@google.comd4726202012-08-03 14:34:46 +0000390 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
391 dummyDesc.fWidth = 16;
392 dummyDesc.fHeight = 22;
joshualitt2c93efe2014-11-06 12:57:13 -0800393 SkAutoTUnref<GrTexture> dummyTexture2(gpu->createTexture(dummyDesc, NULL, 0));
bsalomon@google.comd4726202012-08-03 14:34:46 +0000394
bsalomone904c092014-07-17 10:50:59 -0700395 if (!dummyTexture1 || ! dummyTexture2) {
joshualitt65171342014-10-09 07:25:36 -0700396 SkDebugf("Could not allocate dummy textures");
bsalomone904c092014-07-17 10:50:59 -0700397 return false;
398 }
399
joshualitt65171342014-10-09 07:25:36 -0700400 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
401
402 // Setup texture cache id key
403 const GrCacheID::Domain glProgramsDomain = GrCacheID::GenerateDomain();
404 GrCacheID::Key key;
405 memset(&key, 0, sizeof(key));
406 key.fData32[0] = kRenderTargetWidth;
407 key.fData32[1] = kRenderTargetHeight;
408 GrCacheID glProgramsCacheID(glProgramsDomain, key);
409
410 // setup clip
joshualitt2c93efe2014-11-06 12:57:13 -0800411 SkRect screen = SkRect::MakeWH(SkIntToScalar(kRenderTargetWidth),
412 SkIntToScalar(kRenderTargetHeight));
joshualitt65171342014-10-09 07:25:36 -0700413
414 SkClipStack stack;
415 stack.clipDevRect(screen, SkRegion::kReplace_Op, false);
416
417 // wrap the SkClipStack in a GrClipData
418 GrClipData clipData;
419 clipData.fClipStack = &stack;
420 this->setClip(&clipData);
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000421
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000422 SkRandom random;
joshualitt65171342014-10-09 07:25:36 -0700423 static const int NUM_TESTS = 512;
424 for (int t = 0; t < NUM_TESTS;) {
425 // setup random render target(can fail)
joshualitt2c93efe2014-11-06 12:57:13 -0800426 SkAutoTUnref<GrRenderTarget> rt(random_render_target(fContext, glProgramsCacheID, &random));
427 if (!rt.get()) {
joshualitt65171342014-10-09 07:25:36 -0700428 SkDebugf("Could not allocate render target");
429 return false;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000430 }
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000431
joshualitt65171342014-10-09 07:25:36 -0700432 GrDrawState* ds = this->drawState();
433 ds->setRenderTarget(rt.get());
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000434
joshualitt65171342014-10-09 07:25:36 -0700435 // if path rendering we have to setup a couple of things like the draw type
joshualitt2c93efe2014-11-06 12:57:13 -0800436 bool usePathRendering = gpu->glCaps().pathRenderingSupport() && random.nextBool();
egdanielc0648242014-09-22 13:17:02 -0700437
egdanielae444962014-09-22 12:29:52 -0700438 GrGpu::DrawType drawType = usePathRendering ? GrGpu::kDrawPath_DrawType :
439 GrGpu::kDrawPoints_DrawType;
commit-bot@chromium.org8e919ad2013-10-21 14:48:23 +0000440
joshualitt65171342014-10-09 07:25:36 -0700441 // twiddle drawstate knobs randomly
egdanielae444962014-09-22 12:29:52 -0700442 bool hasGeometryProcessor = usePathRendering ? false : random.nextBool();
joshualittbd769d02014-09-04 08:56:46 -0700443 if (hasGeometryProcessor) {
joshualitt2c93efe2014-11-06 12:57:13 -0800444 set_random_gp(fContext, gpu->glCaps(), ds, &random, dummyTextures);
joshualittd9097592014-10-07 08:37:36 -0700445 }
joshualitt2c93efe2014-11-06 12:57:13 -0800446 set_random_color_coverage_stages(gpu,
447 ds,
448 maxStages - hasGeometryProcessor,
449 usePathRendering,
450 &random,
451 dummyTextures);
452 set_random_color(ds, &random);
453 set_random_coverage(ds, &random);
454 set_random_hints(ds, &random);
455 set_random_state(ds, &random);
456 set_random_blend_func(ds, &random);
457 set_random_stencil(ds, &random);
joshualittd9097592014-10-07 08:37:36 -0700458
joshualitt65171342014-10-09 07:25:36 -0700459 GrDeviceCoordTexture dstCopy;
460
461 if (!this->setupDstReadIfNecessary(&dstCopy, NULL)) {
462 SkDebugf("Couldn't setup dst read texture");
bsalomon848faf02014-07-11 10:01:02 -0700463 return false;
464 }
joshualitt79f8fae2014-10-28 17:59:26 -0700465
466 // create optimized draw state, setup readDst texture if required, and build a descriptor
467 // and program. ODS creation can fail, so we have to check
468 SkAutoTUnref<GrOptDrawState> ods(GrOptDrawState::Create(this->getDrawState(),
joshualitt2c93efe2014-11-06 12:57:13 -0800469 gpu,
joshualitt79f8fae2014-10-28 17:59:26 -0700470 &dstCopy,
471 drawType));
472 if (!ods.get()) {
473 ds->reset();
474 continue;
joshualittd9097592014-10-07 08:37:36 -0700475 }
joshualitt2c93efe2014-11-06 12:57:13 -0800476 SkAutoTUnref<GrGLProgram> program(GrGLProgramBuilder::CreateProgram(*ods, drawType, gpu));
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000477 if (NULL == program.get()) {
joshualitt65171342014-10-09 07:25:36 -0700478 SkDebugf("Failed to create program!");
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000479 return false;
480 }
joshualitt249af152014-09-15 11:41:13 -0700481
482 // We have to reset the drawstate because we might have added a gp
joshualitt65171342014-10-09 07:25:36 -0700483 ds->reset();
484
485 // because occasionally optimized drawstate creation will fail for valid reasons, we only
486 // want to increment on success
487 ++t;
bsalomon@google.comc3841b92012-08-02 18:11:43 +0000488 }
489 return true;
490}
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000491
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +0000492DEF_GPUTEST(GLPrograms, reporter, factory) {
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000493 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
494 GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type));
bsalomon49f085d2014-09-05 13:34:00 -0700495 if (context) {
bsalomon@google.com042a2862013-02-04 18:39:24 +0000496 GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu());
joshualitt9e87fa72014-10-09 13:12:35 -0700497
498 /*
499 * For the time being, we only support the test with desktop GL or for android on
500 * ARM platforms
501 * TODO When we run ES 3.00 GLSL in more places, test again
502 */
503 int maxStages;
504 if (kGL_GrGLStandard == gpu->glStandard() ||
505 kARM_GrGLVendor == gpu->ctxInfo().vendor()) {
506 maxStages = 6;
507 } else if (kTegra3_GrGLRenderer == gpu->ctxInfo().renderer() ||
508 kOther_GrGLRenderer == gpu->ctxInfo().renderer()) {
509 maxStages = 1;
510 } else {
511 return;
512 }
bsalomon@google.com042a2862013-02-04 18:39:24 +0000513#if SK_ANGLE
514 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
515 if (type == GrContextFactory::kANGLE_GLContextType) {
516 maxStages = 3;
517 }
518#endif
joshualitt2c93efe2014-11-06 12:57:13 -0800519 GrTestTarget target;
520 context->getTestTarget(&target);
521 REPORTER_ASSERT(reporter, target.target()->programUnitTest(maxStages));
bsalomon@google.com67b915d2013-02-04 16:13:32 +0000522 }
523 }
bsalomon@google.coma8e686e2011-08-16 15:45:58 +0000524}
525
bsalomon@google.comcf8fb1f2012-08-02 14:03:32 +0000526#endif