bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 1 | |
| 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.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 9 | // This is a GPU-backend specific test. It relies on static intializers to work |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 10 | |
bsalomon@google.com | 2a48c3a | 2012-08-03 14:54:45 +0000 | [diff] [blame] | 11 | #include "SkTypes.h" |
| 12 | |
| 13 | #if SK_SUPPORT_GPU && SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 14 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 15 | #include "GrContextFactory.h" |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 16 | #include "GrInvariantOutput.h" |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 17 | #include "GrOptDrawState.h" |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 18 | #include "GrTBackendProcessorFactory.h" |
| 19 | #include "GrTest.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 20 | #include "SkChecksum.h" |
tfarina@chromium.org | 223137f | 2012-11-21 22:38:36 +0000 | [diff] [blame] | 21 | #include "SkRandom.h" |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 22 | #include "Test.h" |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 23 | #include "effects/GrConfigConversionEffect.h" |
| 24 | #include "gl/GrGLPathRendering.h" |
| 25 | #include "gl/GrGpuGL.h" |
| 26 | #include "gl/builders/GrGLProgramBuilder.h" |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 27 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 28 | /* |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 29 | * A dummy processor which just tries to insert a massive key and verify that it can retrieve the |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 30 | * whole thing correctly |
| 31 | */ |
| 32 | static const uint32_t kMaxKeySize = 1024; |
| 33 | |
| 34 | class GLBigKeyProcessor; |
| 35 | |
| 36 | class BigKeyProcessor : public GrFragmentProcessor { |
| 37 | public: |
| 38 | static GrFragmentProcessor* Create() { |
bsalomon | 98b33eb | 2014-10-15 11:05:26 -0700 | [diff] [blame] | 39 | GR_CREATE_STATIC_PROCESSOR(gBigKeyProcessor, BigKeyProcessor, ()) |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 40 | return SkRef(gBigKeyProcessor); |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 41 | } |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 42 | |
| 43 | static const char* Name() { return "Big ol' Key"; } |
| 44 | |
| 45 | virtual const GrBackendFragmentProcessorFactory& getFactory() const SK_OVERRIDE { |
| 46 | return GrTBackendFragmentProcessorFactory<BigKeyProcessor>::getInstance(); |
joshualitt | d909759 | 2014-10-07 08:37:36 -0700 | [diff] [blame] | 47 | } |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 48 | |
| 49 | typedef GLBigKeyProcessor GLProcessor; |
| 50 | |
| 51 | private: |
| 52 | BigKeyProcessor() { } |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 53 | virtual bool onIsEqual(const GrFragmentProcessor&) const SK_OVERRIDE { return true; } |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 54 | virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE { } |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 55 | |
| 56 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 57 | |
| 58 | typedef GrFragmentProcessor INHERITED; |
| 59 | }; |
| 60 | |
| 61 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(BigKeyProcessor); |
| 62 | |
| 63 | GrFragmentProcessor* BigKeyProcessor::TestCreate(SkRandom*, |
| 64 | GrContext*, |
| 65 | const GrDrawTargetCaps&, |
| 66 | GrTexture*[]) { |
| 67 | return BigKeyProcessor::Create(); |
joshualitt | d909759 | 2014-10-07 08:37:36 -0700 | [diff] [blame] | 68 | } |
| 69 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 70 | class GLBigKeyProcessor : public GrGLFragmentProcessor { |
| 71 | public: |
| 72 | GLBigKeyProcessor(const GrBackendProcessorFactory& factory, const GrProcessor&) |
| 73 | : INHERITED(factory) {} |
joshualitt | d909759 | 2014-10-07 08:37:36 -0700 | [diff] [blame] | 74 | |
joshualitt | 1598899 | 2014-10-09 15:04:05 -0700 | [diff] [blame] | 75 | virtual void emitCode(GrGLFPBuilder* builder, |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 76 | 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); |
joshualitt | d909759 | 2014-10-07 08:37:36 -0700 | [diff] [blame] | 84 | } |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 85 | } |
| 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); |
joshualitt | d909759 | 2014-10-07 08:37:36 -0700 | [diff] [blame] | 90 | } |
joshualitt | d909759 | 2014-10-07 08:37:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 93 | private: |
| 94 | typedef GrGLFragmentProcessor INHERITED; |
| 95 | }; |
| 96 | |
| 97 | /* |
| 98 | * Begin test code |
| 99 | */ |
| 100 | static const int kRenderTargetHeight = 1; |
| 101 | static const int kRenderTargetWidth = 1; |
| 102 | |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 103 | static GrRenderTarget* random_render_target(GrContext* context, |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 104 | const GrCacheID& cacheId, |
| 105 | SkRandom* random) { |
| 106 | // setup render target |
| 107 | GrTextureParams params; |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 108 | GrSurfaceDesc texDesc; |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 109 | texDesc.fWidth = kRenderTargetWidth; |
| 110 | texDesc.fHeight = kRenderTargetHeight; |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 111 | texDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 112 | texDesc.fConfig = kRGBA_8888_GrPixelConfig; |
| 113 | texDesc.fOrigin = random->nextBool() == true ? kTopLeft_GrSurfaceOrigin : |
| 114 | kBottomLeft_GrSurfaceOrigin; |
| 115 | |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 116 | SkAutoTUnref<GrTexture> texture(context->findAndRefTexture(texDesc, cacheId, ¶ms)); |
bsalomon | d27726e | 2014-10-12 05:40:00 -0700 | [diff] [blame] | 117 | if (!texture) { |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 118 | texture.reset(context->createTexture(¶ms, texDesc, cacheId, 0, 0)); |
bsalomon | d27726e | 2014-10-12 05:40:00 -0700 | [diff] [blame] | 119 | if (!texture) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 120 | return NULL; |
joshualitt | d909759 | 2014-10-07 08:37:36 -0700 | [diff] [blame] | 121 | } |
joshualitt | d909759 | 2014-10-07 08:37:36 -0700 | [diff] [blame] | 122 | } |
bsalomon | d27726e | 2014-10-12 05:40:00 -0700 | [diff] [blame] | 123 | return SkRef(texture->asRenderTarget()); |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 124 | } |
| 125 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 126 | // 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 |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 129 | // 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. |
| 131 | GrVertexAttrib kGenericVertexAttribs[] = { |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 132 | { kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding }, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 133 | { kVec2f_GrVertexAttribType, 0, kGeometryProcessor_GrVertexAttribBinding }, |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 134 | { kVec2f_GrVertexAttribType, 0, kGeometryProcessor_GrVertexAttribBinding }, |
| 135 | { kVec2f_GrVertexAttribType, 0, kGeometryProcessor_GrVertexAttribBinding }, |
| 136 | { kVec2f_GrVertexAttribType, 0, kGeometryProcessor_GrVertexAttribBinding }, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 137 | { kVec2f_GrVertexAttribType, 0, kGeometryProcessor_GrVertexAttribBinding } |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | /* |
| 141 | * convert sl type to vertexattrib type, not a complete implementation, only use for debugging |
| 142 | */ |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 143 | static GrVertexAttribType convert_sltype_to_attribtype(GrSLType type) { |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 144 | 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 | } |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 158 | // end test hack |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 159 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 160 | static 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 | |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 170 | static void set_random_gp(GrContext* context, |
| 171 | const GrDrawTargetCaps& caps, |
| 172 | GrDrawState* ds, |
| 173 | SkRandom* random, |
| 174 | GrTexture* dummyTextures[]) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 175 | GrProgramElementRef<const GrGeometryProcessor> gp( |
| 176 | GrProcessorTestFactory<GrGeometryProcessor>::CreateStage(random, |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 177 | context, |
| 178 | caps, |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 179 | 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 |
egdaniel | 37b4d86 | 2014-11-03 10:07:07 -0800 | [diff] [blame] | 196 | setup_random_ff_attribute(kCoverage_GrVertexAttribBinding, kUByte_GrVertexAttribType, |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 197 | 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 |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 216 | ds->setVertexAttribs<kGenericVertexAttribs>(attribIndex + numGPAttribs, runningStride); |
| 217 | ds->setGeometryProcessor(gp); |
| 218 | } |
| 219 | |
| 220 | static void set_random_color_coverage_stages(GrGpuGL* gpu, |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 221 | GrDrawState* ds, |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 222 | 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 |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 256 | 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 |
| 266 | enum ColorMode { |
| 267 | kAllOnes_ColorMode, |
| 268 | kAllZeros_ColorMode, |
| 269 | kAlphaOne_ColorMode, |
| 270 | kRandom_ColorMode, |
| 271 | kLast_ColorMode = kRandom_ColorMode |
| 272 | }; |
| 273 | |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 274 | static void set_random_color(GrDrawState* ds, SkRandom* random) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 275 | 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); |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 299 | ds->setColor(color); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | // There are only a few cases of random coverages which interest us |
| 303 | enum CoverageMode { |
| 304 | kZero_CoverageMode, |
| 305 | kFF_CoverageMode, |
| 306 | kRandom_CoverageMode, |
| 307 | kLast_CoverageMode = kRandom_CoverageMode |
| 308 | }; |
| 309 | |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 310 | static void set_random_coverage(GrDrawState* ds, SkRandom* random) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 311 | 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 | } |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 324 | ds->setCoverage(coverage); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 325 | } |
| 326 | |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 327 | static void set_random_hints(GrDrawState* ds, SkRandom* random) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 328 | for (int i = 1; i <= GrDrawState::kLast_Hint; i <<= 1) { |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 329 | ds->setHint(GrDrawState::Hints(i), random->nextBool()); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 333 | static void set_random_state(GrDrawState* ds, SkRandom* random) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 334 | int state = 0; |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 335 | for (int i = 1; i <= GrDrawState::kLast_StateBit; i <<= 1) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 336 | state |= random->nextBool() * i; |
| 337 | } |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 338 | ds->enableState(state); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | // this function will randomly pick non-self referencing blend modes |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 342 | static void set_random_blend_func(GrDrawState* ds, SkRandom* random) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 343 | 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 | |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 353 | ds->setBlendFunc(src, dst); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | // right now, the only thing we seem to care about in drawState's stencil is 'doesWrite()' |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 357 | static void set_random_stencil(GrDrawState* ds, SkRandom* random) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 358 | 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()) { |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 374 | ds->setStencil(kDoesWriteStencil); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 375 | } else { |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 376 | ds->setStencil(kDoesNotWriteStencil); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 379 | |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 380 | bool GrDrawTarget::programUnitTest(int maxStages) { |
| 381 | GrGpuGL* gpu = static_cast<GrGpuGL*>(fContext->getGpu()); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 382 | // setup dummy textures |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 383 | GrSurfaceDesc dummyDesc; |
| 384 | dummyDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 385 | dummyDesc.fConfig = kSkia8888_GrPixelConfig; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 386 | dummyDesc.fWidth = 34; |
| 387 | dummyDesc.fHeight = 18; |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 388 | SkAutoTUnref<GrTexture> dummyTexture1(gpu->createTexture(dummyDesc, NULL, 0)); |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 389 | dummyDesc.fFlags = kNone_GrSurfaceFlags; |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 390 | dummyDesc.fConfig = kAlpha_8_GrPixelConfig; |
| 391 | dummyDesc.fWidth = 16; |
| 392 | dummyDesc.fHeight = 22; |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 393 | SkAutoTUnref<GrTexture> dummyTexture2(gpu->createTexture(dummyDesc, NULL, 0)); |
bsalomon@google.com | d472620 | 2012-08-03 14:34:46 +0000 | [diff] [blame] | 394 | |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 395 | if (!dummyTexture1 || ! dummyTexture2) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 396 | SkDebugf("Could not allocate dummy textures"); |
bsalomon | e904c09 | 2014-07-17 10:50:59 -0700 | [diff] [blame] | 397 | return false; |
| 398 | } |
| 399 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 400 | 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 |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 411 | SkRect screen = SkRect::MakeWH(SkIntToScalar(kRenderTargetWidth), |
| 412 | SkIntToScalar(kRenderTargetHeight)); |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 413 | |
| 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.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 421 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 422 | SkRandom random; |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 423 | static const int NUM_TESTS = 512; |
| 424 | for (int t = 0; t < NUM_TESTS;) { |
| 425 | // setup random render target(can fail) |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 426 | SkAutoTUnref<GrRenderTarget> rt(random_render_target(fContext, glProgramsCacheID, &random)); |
| 427 | if (!rt.get()) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 428 | SkDebugf("Could not allocate render target"); |
| 429 | return false; |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 430 | } |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 431 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 432 | GrDrawState* ds = this->drawState(); |
| 433 | ds->setRenderTarget(rt.get()); |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 434 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 435 | // if path rendering we have to setup a couple of things like the draw type |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 436 | bool usePathRendering = gpu->glCaps().pathRenderingSupport() && random.nextBool(); |
egdaniel | c064824 | 2014-09-22 13:17:02 -0700 | [diff] [blame] | 437 | |
egdaniel | ae44496 | 2014-09-22 12:29:52 -0700 | [diff] [blame] | 438 | GrGpu::DrawType drawType = usePathRendering ? GrGpu::kDrawPath_DrawType : |
| 439 | GrGpu::kDrawPoints_DrawType; |
commit-bot@chromium.org | 8e919ad | 2013-10-21 14:48:23 +0000 | [diff] [blame] | 440 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 441 | // twiddle drawstate knobs randomly |
egdaniel | ae44496 | 2014-09-22 12:29:52 -0700 | [diff] [blame] | 442 | bool hasGeometryProcessor = usePathRendering ? false : random.nextBool(); |
joshualitt | bd769d0 | 2014-09-04 08:56:46 -0700 | [diff] [blame] | 443 | if (hasGeometryProcessor) { |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 444 | set_random_gp(fContext, gpu->glCaps(), ds, &random, dummyTextures); |
joshualitt | d909759 | 2014-10-07 08:37:36 -0700 | [diff] [blame] | 445 | } |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 446 | 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); |
joshualitt | d909759 | 2014-10-07 08:37:36 -0700 | [diff] [blame] | 458 | |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 459 | GrDeviceCoordTexture dstCopy; |
| 460 | |
| 461 | if (!this->setupDstReadIfNecessary(&dstCopy, NULL)) { |
| 462 | SkDebugf("Couldn't setup dst read texture"); |
bsalomon | 848faf0 | 2014-07-11 10:01:02 -0700 | [diff] [blame] | 463 | return false; |
| 464 | } |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 465 | |
| 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(), |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 469 | gpu, |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 470 | &dstCopy, |
| 471 | drawType)); |
| 472 | if (!ods.get()) { |
| 473 | ds->reset(); |
| 474 | continue; |
joshualitt | d909759 | 2014-10-07 08:37:36 -0700 | [diff] [blame] | 475 | } |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 476 | SkAutoTUnref<GrGLProgram> program(GrGLProgramBuilder::CreateProgram(*ods, drawType, gpu)); |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 477 | if (NULL == program.get()) { |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 478 | SkDebugf("Failed to create program!"); |
bsalomon@google.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 479 | return false; |
| 480 | } |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 481 | |
| 482 | // We have to reset the drawstate because we might have added a gp |
joshualitt | 6517134 | 2014-10-09 07:25:36 -0700 | [diff] [blame] | 483 | 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.com | c3841b9 | 2012-08-02 18:11:43 +0000 | [diff] [blame] | 488 | } |
| 489 | return true; |
| 490 | } |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 491 | |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 492 | DEF_GPUTEST(GLPrograms, reporter, factory) { |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 493 | for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
| 494 | GrContext* context = factory->get(static_cast<GrContextFactory::GLContextType>(type)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 495 | if (context) { |
bsalomon@google.com | 042a286 | 2013-02-04 18:39:24 +0000 | [diff] [blame] | 496 | GrGpuGL* gpu = static_cast<GrGpuGL*>(context->getGpu()); |
joshualitt | 9e87fa7 | 2014-10-09 13:12:35 -0700 | [diff] [blame] | 497 | |
| 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.com | 042a286 | 2013-02-04 18:39:24 +0000 | [diff] [blame] | 513 | #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 |
joshualitt | 2c93efe | 2014-11-06 12:57:13 -0800 | [diff] [blame] | 519 | GrTestTarget target; |
| 520 | context->getTestTarget(&target); |
| 521 | REPORTER_ASSERT(reporter, target.target()->programUnitTest(maxStages)); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 524 | } |
| 525 | |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 526 | #endif |