3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 // This is a GPU-backend specific test. It relies on static intializers to work
13 #if SK_SUPPORT_GPU && SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
15 #include "GrTBackendProcessorFactory.h"
16 #include "GrContextFactory.h"
17 #include "GrOptDrawState.h"
18 #include "effects/GrConfigConversionEffect.h"
19 #include "gl/builders/GrGLProgramBuilder.h"
20 #include "gl/GrGLPathRendering.h"
21 #include "gl/GrGpuGL.h"
22 #include "SkChecksum.h"
27 * A dummy processor which just tries to insert a massive key and verify that it can retrieve the
28 * whole thing correctly
30 static const uint32_t kMaxKeySize = 1024;
32 class GLBigKeyProcessor;
34 class BigKeyProcessor : public GrFragmentProcessor {
36 static GrFragmentProcessor* Create() {
37 GR_CREATE_STATIC_PROCESSOR(gBigKeyProcessor, BigKeyProcessor, ())
38 return SkRef(gBigKeyProcessor);
41 static const char* Name() { return "Big ol' Key"; }
43 virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE {
44 return GrTBackendFragmentProcessorFactory<BigKeyProcessor>::getInstance();
47 typedef GLBigKeyProcessor GLProcessor;
51 virtual bool onIsEqual(const GrFragmentProcessor&) const SK_OVERRIDE { return true; }
52 virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE { }
54 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
56 typedef GrFragmentProcessor INHERITED;
59 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(BigKeyProcessor);
61 GrFragmentProcessor* BigKeyProcessor::TestCreate(SkRandom*,
63 const GrDrawTargetCaps&,
65 return BigKeyProcessor::Create();
68 class GLBigKeyProcessor : public GrGLFragmentProcessor {
70 GLBigKeyProcessor(const GrBackendProcessorFactory& factory, const GrProcessor&)
71 : INHERITED(factory) {}
73 virtual void emitCode(GrGLFPBuilder* builder,
74 const GrFragmentProcessor& fp,
75 const GrProcessorKey& key,
76 const char* outputColor,
77 const char* inputColor,
78 const TransformedCoordsArray&,
79 const TextureSamplerArray&) {
80 for (uint32_t i = 0; i < kMaxKeySize; i++) {
81 SkASSERT(key.get32(i) == i);
85 static void GenKey(const GrProcessor& processor, const GrGLCaps&, GrProcessorKeyBuilder* b) {
86 for (uint32_t i = 0; i < kMaxKeySize; i++) {
92 typedef GrGLFragmentProcessor INHERITED;
98 static const int kRenderTargetHeight = 1;
99 static const int kRenderTargetWidth = 1;
101 static GrRenderTarget* random_render_target(GrGpuGL* gpu,
102 const GrCacheID& cacheId,
104 // setup render target
105 GrTextureParams params;
106 GrSurfaceDesc texDesc;
107 texDesc.fWidth = kRenderTargetWidth;
108 texDesc.fHeight = kRenderTargetHeight;
109 texDesc.fFlags = kRenderTarget_GrSurfaceFlag;
110 texDesc.fConfig = kRGBA_8888_GrPixelConfig;
111 texDesc.fOrigin = random->nextBool() == true ? kTopLeft_GrSurfaceOrigin :
112 kBottomLeft_GrSurfaceOrigin;
114 SkAutoTUnref<GrTexture> texture(
115 gpu->getContext()->findAndRefTexture(texDesc, cacheId, ¶ms));
117 texture.reset(gpu->getContext()->createTexture(¶ms, texDesc, cacheId, 0, 0));
122 return SkRef(texture->asRenderTarget());
125 // TODO clean this up, we have to do this to test geometry processors but there has got to be
126 // a better way. In the mean time, we actually fill out these generic vertex attribs below with
127 // the correct vertex attribs from the GP. We have to ensure, however, we don't try to add more
128 // than two attributes. In addition, we 'pad' the below array with GPs up to 6 entries, 4 fixed
129 // function vertex attributes and 2 GP custom attributes.
130 GrVertexAttrib kGenericVertexAttribs[] = {
131 { kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding },
132 { kVec2f_GrVertexAttribType, 0, kGeometryProcessor_GrVertexAttribBinding },
133 { kVec2f_GrVertexAttribType, 0, kGeometryProcessor_GrVertexAttribBinding },
134 { kVec2f_GrVertexAttribType, 0, kGeometryProcessor_GrVertexAttribBinding },
135 { kVec2f_GrVertexAttribType, 0, kGeometryProcessor_GrVertexAttribBinding },
136 { kVec2f_GrVertexAttribType, 0, kGeometryProcessor_GrVertexAttribBinding }
140 * convert sl type to vertexattrib type, not a complete implementation, only use for debugging
142 static GrVertexAttribType convert_sltype_to_attribtype(GrSLType type) {
144 case kFloat_GrSLType:
145 return kFloat_GrVertexAttribType;
146 case kVec2f_GrSLType:
147 return kVec2f_GrVertexAttribType;
148 case kVec3f_GrSLType:
149 return kVec3f_GrVertexAttribType;
150 case kVec4f_GrSLType:
151 return kVec4f_GrVertexAttribType;
153 SkFAIL("Type isn't convertible");
154 return kFloat_GrVertexAttribType;
159 static void setup_random_ff_attribute(GrVertexAttribBinding binding, GrVertexAttribType type,
160 SkRandom* random, int* attribIndex, int* runningStride) {
161 if (random->nextBool()) {
162 kGenericVertexAttribs[*attribIndex].fType = type;
163 kGenericVertexAttribs[*attribIndex].fOffset = *runningStride;
164 kGenericVertexAttribs[*attribIndex].fBinding = binding;
165 *runningStride += GrVertexAttribTypeSize(kGenericVertexAttribs[(*attribIndex)++].fType);
169 static void set_random_gp(GrGpuGL* gpu, SkRandom* random, GrTexture* dummyTextures[]) {
170 GrProgramElementRef<const GrGeometryProcessor> gp(
171 GrProcessorTestFactory<GrGeometryProcessor>::CreateStage(random,
177 // we have to set dummy vertex attributes, first we setup the fixed function attributes
178 // always leave the position attribute untouched in the array
180 int runningStride = GrVertexAttribTypeSize(kGenericVertexAttribs[0].fType);
183 setup_random_ff_attribute(kLocalCoord_GrVertexAttribBinding, kVec2f_GrVertexAttribType,
184 random, &attribIndex, &runningStride);
187 setup_random_ff_attribute(kColor_GrVertexAttribBinding, kVec4f_GrVertexAttribType,
188 random, &attribIndex, &runningStride);
191 setup_random_ff_attribute(kCoverage_GrVertexAttribBinding, kUByte_GrVertexAttribType,
192 random, &attribIndex, &runningStride);
194 // Update the geometry processor attributes
195 const GrGeometryProcessor::VertexAttribArray& v = gp->getVertexAttribs();
196 int numGPAttribs = v.count();
197 SkASSERT(numGPAttribs <= GrGeometryProcessor::kMaxVertexAttribs &&
198 GrGeometryProcessor::kMaxVertexAttribs == 2);
200 // we actually can't overflow if kMaxVertexAttribs == 2, but GCC 4.8 wants more proof
201 int maxIndex = SK_ARRAY_COUNT(kGenericVertexAttribs);
202 for (int i = 0; i < numGPAttribs && i + attribIndex < maxIndex; i++) {
203 kGenericVertexAttribs[i + attribIndex].fType =
204 convert_sltype_to_attribtype(v[i].getType());
205 kGenericVertexAttribs[i + attribIndex].fOffset = runningStride;
206 kGenericVertexAttribs[i + attribIndex].fBinding = kGeometryProcessor_GrVertexAttribBinding;
207 runningStride += GrVertexAttribTypeSize(kGenericVertexAttribs[i + attribIndex].fType);
210 // update the vertex attributes with the ds
211 GrDrawState* ds = gpu->drawState();
212 ds->setVertexAttribs<kGenericVertexAttribs>(attribIndex + numGPAttribs, runningStride);
213 ds->setGeometryProcessor(gp);
216 static void set_random_color_coverage_stages(GrGpuGL* gpu,
218 bool usePathRendering,
220 GrTexture* dummyTextures[]) {
221 int numProcs = random->nextULessThan(maxStages + 1);
222 int numColorProcs = random->nextULessThan(numProcs + 1);
224 int currTextureCoordSet = 0;
225 for (int s = 0; s < numProcs;) {
226 GrProgramElementRef<GrFragmentProcessor> fp(
227 GrProcessorTestFactory<GrFragmentProcessor>::CreateStage(random,
233 // don't add dst color reads to coverage stage
234 if (s >= numColorProcs && fp->willReadDstColor()) {
238 // If adding this effect would exceed the max texture coord set count then generate a
239 // new random effect.
240 if (usePathRendering && gpu->glPathRendering()->texturingMode() ==
241 GrGLPathRendering::FixedFunction_TexturingMode) {;
242 int numTransforms = fp->numTransforms();
243 if (currTextureCoordSet + numTransforms >
244 gpu->glCaps().maxFixedFunctionTextureCoords()) {
247 currTextureCoordSet += numTransforms;
250 // finally add the stage to the correct pipeline in the drawstate
251 GrDrawState* ds = gpu->drawState();
252 if (s < numColorProcs) {
253 ds->addColorProcessor(fp);
255 ds->addCoverageProcessor(fp);
261 // There are only a few cases of random colors which interest us
267 kLast_ColorMode = kRandom_ColorMode
270 static void set_random_color(GrGpuGL* gpu, SkRandom* random) {
271 ColorMode colorMode = ColorMode(random->nextULessThan(kLast_ColorMode + 1));
274 case kAllOnes_ColorMode:
275 color = GrColorPackRGBA(0xFF, 0xFF, 0xFF, 0xFF);
277 case kAllZeros_ColorMode:
278 color = GrColorPackRGBA(0, 0, 0, 0);
280 case kAlphaOne_ColorMode:
281 color = GrColorPackRGBA(random->nextULessThan(256),
282 random->nextULessThan(256),
283 random->nextULessThan(256),
286 case kRandom_ColorMode:
287 uint8_t alpha = random->nextULessThan(256);
288 color = GrColorPackRGBA(random->nextRangeU(0, alpha),
289 random->nextRangeU(0, alpha),
290 random->nextRangeU(0, alpha),
294 GrColorIsPMAssert(color);
295 gpu->drawState()->setColor(color);
298 // There are only a few cases of random coverages which interest us
302 kRandom_CoverageMode,
303 kLast_CoverageMode = kRandom_CoverageMode
306 static void set_random_coverage(GrGpuGL* gpu, SkRandom* random) {
307 CoverageMode coverageMode = CoverageMode(random->nextULessThan(kLast_CoverageMode + 1));
309 switch (coverageMode) {
310 case kZero_CoverageMode:
313 case kFF_CoverageMode:
316 case kRandom_CoverageMode:
317 coverage = uint8_t(random->nextU());
320 gpu->drawState()->setCoverage(coverage);
323 static void set_random_hints(GrGpuGL* gpu, SkRandom* random) {
324 for (int i = 1; i <= GrDrawState::kLast_Hint; i <<= 1) {
325 gpu->drawState()->setHint(GrDrawState::Hints(i), random->nextBool());
329 static void set_random_state(GrGpuGL* gpu, SkRandom* random) {
331 for (int i = 1; i <= GrDrawState::kLast_StateBit; i <<= 1) {
332 state |= random->nextBool() * i;
334 gpu->drawState()->enableState(state);
337 // this function will randomly pick non-self referencing blend modes
338 static void set_random_blend_func(GrGpuGL* gpu, SkRandom* random) {
341 src = GrBlendCoeff(random->nextRangeU(kFirstPublicGrBlendCoeff, kLastPublicGrBlendCoeff));
342 } while (GrBlendCoeffRefsSrc(src));
346 dst = GrBlendCoeff(random->nextRangeU(kFirstPublicGrBlendCoeff, kLastPublicGrBlendCoeff));
347 } while (GrBlendCoeffRefsDst(dst));
349 gpu->drawState()->setBlendFunc(src, dst);
352 // right now, the only thing we seem to care about in drawState's stencil is 'doesWrite()'
353 static void set_random_stencil(GrGpuGL* gpu, SkRandom* random) {
354 GR_STATIC_CONST_SAME_STENCIL(kDoesWriteStencil,
361 GR_STATIC_CONST_SAME_STENCIL(kDoesNotWriteStencil,
369 if (random->nextBool()) {
370 gpu->drawState()->setStencil(kDoesWriteStencil);
372 gpu->drawState()->setStencil(kDoesNotWriteStencil);
376 bool GrGpuGL::programUnitTest(int maxStages) {
377 // setup dummy textures
378 GrSurfaceDesc dummyDesc;
379 dummyDesc.fFlags = kRenderTarget_GrSurfaceFlag;
380 dummyDesc.fConfig = kSkia8888_GrPixelConfig;
381 dummyDesc.fWidth = 34;
382 dummyDesc.fHeight = 18;
383 SkAutoTUnref<GrTexture> dummyTexture1(this->createTexture(dummyDesc, NULL, 0));
384 dummyDesc.fFlags = kNone_GrSurfaceFlags;
385 dummyDesc.fConfig = kAlpha_8_GrPixelConfig;
386 dummyDesc.fWidth = 16;
387 dummyDesc.fHeight = 22;
388 SkAutoTUnref<GrTexture> dummyTexture2(this->createTexture(dummyDesc, NULL, 0));
390 if (!dummyTexture1 || ! dummyTexture2) {
391 SkDebugf("Could not allocate dummy textures");
395 GrTexture* dummyTextures[] = {dummyTexture1.get(), dummyTexture2.get()};
397 // Setup texture cache id key
398 const GrCacheID::Domain glProgramsDomain = GrCacheID::GenerateDomain();
400 memset(&key, 0, sizeof(key));
401 key.fData32[0] = kRenderTargetWidth;
402 key.fData32[1] = kRenderTargetHeight;
403 GrCacheID glProgramsCacheID(glProgramsDomain, key);
407 SkRect::MakeWH(SkIntToScalar(kRenderTargetWidth), SkIntToScalar(kRenderTargetHeight));
410 stack.clipDevRect(screen, SkRegion::kReplace_Op, false);
412 // wrap the SkClipStack in a GrClipData
414 clipData.fClipStack = &stack;
415 this->setClip(&clipData);
418 static const int NUM_TESTS = 512;
419 for (int t = 0; t < NUM_TESTS;) {
420 // setup random render target(can fail)
421 SkAutoTUnref<GrRenderTarget> rt(random_render_target(this, glProgramsCacheID, &random));
423 SkDebugf("Could not allocate render target");
427 GrDrawState* ds = this->drawState();
428 ds->setRenderTarget(rt.get());
430 // if path rendering we have to setup a couple of things like the draw type
431 bool usePathRendering = this->glCaps().pathRenderingSupport() && random.nextBool();
433 GrGpu::DrawType drawType = usePathRendering ? GrGpu::kDrawPath_DrawType :
434 GrGpu::kDrawPoints_DrawType;
436 // twiddle drawstate knobs randomly
437 bool hasGeometryProcessor = usePathRendering ? false : random.nextBool();
438 if (hasGeometryProcessor) {
439 set_random_gp(this, &random, dummyTextures);
441 set_random_color_coverage_stages(this, maxStages - hasGeometryProcessor, usePathRendering,
442 &random, dummyTextures);
443 set_random_color(this, &random);
444 set_random_coverage(this, &random);
445 set_random_hints(this, &random);
446 set_random_state(this, &random);
447 set_random_blend_func(this, &random);
448 set_random_stencil(this, &random);
450 GrDeviceCoordTexture dstCopy;
452 if (!this->setupDstReadIfNecessary(&dstCopy, NULL)) {
453 SkDebugf("Couldn't setup dst read texture");
457 // create optimized draw state, setup readDst texture if required, and build a descriptor
458 // and program. ODS creation can fail, so we have to check
459 SkAutoTUnref<GrOptDrawState> ods(GrOptDrawState::Create(this->getDrawState(),
467 SkAutoTUnref<GrGLProgram> program(GrGLProgramBuilder::CreateProgram(*ods, drawType, this));
468 if (NULL == program.get()) {
469 SkDebugf("Failed to create program!");
473 // We have to reset the drawstate because we might have added a gp
476 // because occasionally optimized drawstate creation will fail for valid reasons, we only
477 // want to increment on success
483 DEF_GPUTEST(GLPrograms, reporter, factory) {
484 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
485 GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type));
487 GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu());
490 * For the time being, we only support the test with desktop GL or for android on
492 * TODO When we run ES 3.00 GLSL in more places, test again
495 if (kGL_GrGLStandard == gpu->glStandard() ||
496 kARM_GrGLVendor == gpu->ctxInfo().vendor()) {
498 } else if (kTegra3_GrGLRenderer == gpu->ctxInfo().renderer() ||
499 kOther_GrGLRenderer == gpu->ctxInfo().renderer()) {
505 // Some long shaders run out of temporary registers in the D3D compiler on ANGLE.
506 if (type == GrContextFactory::kANGLE_GLContextType) {
510 REPORTER_ASSERT(reporter, gpu->programUnitTest(maxStages));