Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 8 | #include "SkTypes.h" |
| 9 | #include "Test.h" |
| 10 | |
| 11 | #if SK_SUPPORT_GPU |
| 12 | #include "GrContext.h" |
| 13 | #include "GrGpuResource.h" |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 14 | #include "GrPipelineBuilder.h" |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 15 | #include "GrRenderTargetContext.h" |
| 16 | #include "GrRenderTargetContextPriv.h" |
| 17 | #include "GrResourceProvider.h" |
| 18 | #include "glsl/GrGLSLFragmentProcessor.h" |
| 19 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 20 | #include "ops/GrNonAAFillRectOp.h" |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 21 | #include "ops/GrTestMeshDrawOp.h" |
| 22 | |
| 23 | namespace { |
| 24 | class TestOp : public GrTestMeshDrawOp { |
| 25 | public: |
| 26 | DEFINE_OP_CLASS_ID |
| 27 | const char* name() const override { return "TestOp"; } |
| 28 | |
| 29 | static std::unique_ptr<GrDrawOp> Make() { return std::unique_ptr<GrDrawOp>(new TestOp); } |
| 30 | |
| 31 | private: |
| 32 | TestOp() : INHERITED(ClassID(), SkRect::MakeWH(100, 100), 0xFFFFFFFF) {} |
| 33 | |
| 34 | void onPrepareDraws(Target* target) const override { return; } |
| 35 | |
| 36 | typedef GrTestMeshDrawOp INHERITED; |
| 37 | }; |
| 38 | |
| 39 | /** |
| 40 | * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts |
| 41 | * of resources owned by child FPs. |
| 42 | */ |
| 43 | class TestFP : public GrFragmentProcessor { |
| 44 | public: |
| 45 | struct Image { |
| 46 | Image(sk_sp<GrTexture> texture, GrIOType ioType) : fTexture(texture), fIOType(ioType) {} |
| 47 | sk_sp<GrTexture> fTexture; |
| 48 | GrIOType fIOType; |
| 49 | }; |
| 50 | static sk_sp<GrFragmentProcessor> Make(sk_sp<GrFragmentProcessor> child) { |
| 51 | return sk_sp<GrFragmentProcessor>(new TestFP(std::move(child))); |
| 52 | } |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 53 | static sk_sp<GrFragmentProcessor> Make(GrContext* context, |
| 54 | const SkTArray<sk_sp<GrTextureProxy>>& proxies, |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 55 | const SkTArray<sk_sp<GrBuffer>>& buffers, |
| 56 | const SkTArray<Image>& images) { |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 57 | return sk_sp<GrFragmentProcessor>(new TestFP(context, proxies, buffers, images)); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | const char* name() const override { return "test"; } |
| 61 | |
| 62 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
| 63 | // We don't really care about reusing these. |
| 64 | static int32_t gKey = 0; |
| 65 | b->add32(sk_atomic_inc(&gKey)); |
| 66 | } |
| 67 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 68 | private: |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 69 | TestFP(GrContext* context, |
| 70 | const SkTArray<sk_sp<GrTextureProxy>>& proxies, |
| 71 | const SkTArray<sk_sp<GrBuffer>>& buffers, |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 72 | const SkTArray<Image>& images) |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 73 | : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) { |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 74 | for (const auto& proxy : proxies) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 75 | this->addTextureSampler(&fSamplers.emplace_back(context->resourceProvider(), proxy)); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 76 | } |
| 77 | for (const auto& buffer : buffers) { |
| 78 | this->addBufferAccess(&fBuffers.emplace_back(kRGBA_8888_GrPixelConfig, buffer.get())); |
| 79 | } |
| 80 | for (const Image& image : images) { |
| 81 | this->addImageStorageAccess(&fImages.emplace_back( |
| 82 | image.fTexture, image.fIOType, GrSLMemoryModel::kNone, GrSLRestrict::kNo)); |
| 83 | } |
| 84 | } |
| 85 | |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 86 | TestFP(sk_sp<GrFragmentProcessor> child) |
| 87 | : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) { |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 88 | this->registerChildProcessor(std::move(child)); |
| 89 | } |
| 90 | |
| 91 | virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| 92 | class TestGLSLFP : public GrGLSLFragmentProcessor { |
| 93 | public: |
| 94 | TestGLSLFP() {} |
| 95 | void emitCode(EmitArgs& args) override { |
| 96 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 97 | fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor); |
| 98 | } |
| 99 | |
| 100 | private: |
| 101 | }; |
| 102 | return new TestGLSLFP(); |
| 103 | } |
| 104 | |
| 105 | bool onIsEqual(const GrFragmentProcessor&) const override { return false; } |
| 106 | |
| 107 | GrTAllocator<TextureSampler> fSamplers; |
| 108 | GrTAllocator<BufferAccess> fBuffers; |
| 109 | GrTAllocator<ImageStorageAccess> fImages; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 110 | typedef GrFragmentProcessor INHERITED; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 111 | }; |
| 112 | } |
| 113 | |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 114 | template <typename T> |
| 115 | inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) { |
| 116 | *refCnt = resource->fRefCnt; |
| 117 | *readCnt = resource->fPendingReads; |
| 118 | *writeCnt = resource->fPendingWrites; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 119 | } |
| 120 | |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 121 | void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) { |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 122 | *refCnt = proxy->getBackingRefCnt_TestOnly(); |
| 123 | *readCnt = proxy->getPendingReadCnt_TestOnly(); |
| 124 | *writeCnt = proxy->getPendingWriteCnt_TestOnly(); |
| 125 | } |
| 126 | |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 127 | DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) { |
| 128 | GrContext* context = ctxInfo.grContext(); |
| 129 | |
| 130 | GrTextureDesc desc; |
| 131 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 132 | desc.fWidth = 10; |
| 133 | desc.fHeight = 10; |
| 134 | |
| 135 | for (int parentCnt = 0; parentCnt < 2; parentCnt++) { |
| 136 | sk_sp<GrRenderTargetContext> renderTargetContext(context->makeRenderTargetContext( |
| 137 | SkBackingFit::kApprox, 1, 1, kRGBA_8888_GrPixelConfig, nullptr)); |
| 138 | { |
| 139 | bool texelBufferSupport = context->caps()->shaderCaps()->texelBufferSupport(); |
| 140 | bool imageLoadStoreSupport = context->caps()->shaderCaps()->imageLoadStoreSupport(); |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 141 | sk_sp<GrTextureProxy> proxy1(GrSurfaceProxy::MakeDeferred(context->resourceProvider(), |
Robert Phillips | 7928e76 | 2017-02-28 16:30:28 -0500 | [diff] [blame] | 142 | *context->caps(), desc, |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 143 | SkBackingFit::kExact, |
| 144 | SkBudgeted::kYes)); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 145 | sk_sp<GrTexture> texture2( |
| 146 | context->resourceProvider()->createTexture(desc, SkBudgeted::kYes)); |
| 147 | sk_sp<GrTexture> texture3( |
| 148 | context->resourceProvider()->createTexture(desc, SkBudgeted::kYes)); |
| 149 | sk_sp<GrTexture> texture4( |
| 150 | context->resourceProvider()->createTexture(desc, SkBudgeted::kYes)); |
| 151 | sk_sp<GrBuffer> buffer(texelBufferSupport |
| 152 | ? context->resourceProvider()->createBuffer( |
| 153 | 1024, GrBufferType::kTexel_GrBufferType, |
| 154 | GrAccessPattern::kStatic_GrAccessPattern, 0) |
| 155 | : nullptr); |
| 156 | { |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 157 | SkTArray<sk_sp<GrTextureProxy>> proxies; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 158 | SkTArray<sk_sp<GrBuffer>> buffers; |
| 159 | SkTArray<TestFP::Image> images; |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 160 | proxies.push_back(proxy1); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 161 | if (texelBufferSupport) { |
| 162 | buffers.push_back(buffer); |
| 163 | } |
| 164 | if (imageLoadStoreSupport) { |
| 165 | images.emplace_back(texture2, GrIOType::kRead_GrIOType); |
| 166 | images.emplace_back(texture3, GrIOType::kWrite_GrIOType); |
| 167 | images.emplace_back(texture4, GrIOType::kRW_GrIOType); |
| 168 | } |
| 169 | std::unique_ptr<GrDrawOp> op(TestOp::Make()); |
| 170 | GrPaint paint; |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 171 | auto fp = TestFP::Make(context, |
| 172 | std::move(proxies), std::move(buffers), std::move(images)); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 173 | for (int i = 0; i < parentCnt; ++i) { |
| 174 | fp = TestFP::Make(std::move(fp)); |
| 175 | } |
| 176 | paint.addColorFragmentProcessor(std::move(fp)); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 177 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(paint), GrAAType::kNone, |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 178 | std::move(op)); |
| 179 | } |
| 180 | int refCnt, readCnt, writeCnt; |
| 181 | |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 182 | testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 183 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 184 | REPORTER_ASSERT(reporter, 1 == readCnt); |
| 185 | REPORTER_ASSERT(reporter, 0 == writeCnt); |
| 186 | |
| 187 | if (texelBufferSupport) { |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 188 | testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 189 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 190 | REPORTER_ASSERT(reporter, 1 == readCnt); |
| 191 | REPORTER_ASSERT(reporter, 0 == writeCnt); |
| 192 | } |
| 193 | |
| 194 | if (imageLoadStoreSupport) { |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 195 | testingOnly_getIORefCnts(texture2.get(), &refCnt, &readCnt, &writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 196 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 197 | REPORTER_ASSERT(reporter, 1 == readCnt); |
| 198 | REPORTER_ASSERT(reporter, 0 == writeCnt); |
| 199 | |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 200 | testingOnly_getIORefCnts(texture3.get(), &refCnt, &readCnt, &writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 201 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 202 | REPORTER_ASSERT(reporter, 0 == readCnt); |
| 203 | REPORTER_ASSERT(reporter, 1 == writeCnt); |
| 204 | |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 205 | testingOnly_getIORefCnts(texture4.get(), &refCnt, &readCnt, &writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 206 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 207 | REPORTER_ASSERT(reporter, 1 == readCnt); |
| 208 | REPORTER_ASSERT(reporter, 1 == writeCnt); |
| 209 | } |
| 210 | |
| 211 | context->flush(); |
| 212 | |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 213 | testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 214 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 215 | REPORTER_ASSERT(reporter, 0 == readCnt); |
| 216 | REPORTER_ASSERT(reporter, 0 == writeCnt); |
| 217 | |
| 218 | if (texelBufferSupport) { |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 219 | testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 220 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 221 | REPORTER_ASSERT(reporter, 0 == readCnt); |
| 222 | REPORTER_ASSERT(reporter, 0 == writeCnt); |
| 223 | } |
| 224 | |
| 225 | if (texelBufferSupport) { |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 226 | testingOnly_getIORefCnts(texture2.get(), &refCnt, &readCnt, &writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 227 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 228 | REPORTER_ASSERT(reporter, 0 == readCnt); |
| 229 | REPORTER_ASSERT(reporter, 0 == writeCnt); |
| 230 | |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 231 | testingOnly_getIORefCnts(texture3.get(), &refCnt, &readCnt, &writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 232 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 233 | REPORTER_ASSERT(reporter, 0 == readCnt); |
| 234 | REPORTER_ASSERT(reporter, 0 == writeCnt); |
| 235 | |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 236 | testingOnly_getIORefCnts(texture4.get(), &refCnt, &readCnt, &writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 237 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 238 | REPORTER_ASSERT(reporter, 0 == readCnt); |
| 239 | REPORTER_ASSERT(reporter, 0 == writeCnt); |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 244 | |
| 245 | // This test uses the random GrFragmentProcessor test factory, which relies on static initializers. |
| 246 | #if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 247 | |
| 248 | static GrColor texel_color(int i, int j) { |
| 249 | SkASSERT((unsigned)i < 256 && (unsigned)j < 256); |
| 250 | GrColor color = GrColorPackRGBA(j, (uint8_t)(i + j), (uint8_t)(2 * j - i), i); |
| 251 | return GrPremulColor(color); |
| 252 | } |
| 253 | |
| 254 | static GrColor4f texel_color4f(int i, int j) { return GrColor4f::FromGrColor(texel_color(i, j)); } |
| 255 | |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 256 | void test_draw_op(GrContext* context, GrRenderTargetContext* rtc, sk_sp<GrFragmentProcessor> fp, |
| 257 | sk_sp<GrTextureProxy> inputDataProxy) { |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 258 | GrPaint paint; |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 259 | paint.addColorTextureProcessor(context, std::move(inputDataProxy), nullptr, SkMatrix::I()); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 260 | paint.addColorFragmentProcessor(std::move(fp)); |
| 261 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 262 | GrPipelineBuilder pb(std::move(paint), GrAAType::kNone); |
| 263 | auto op = |
| 264 | GrNonAAFillRectOp::Make(GrColor_WHITE, SkMatrix::I(), |
| 265 | SkRect::MakeWH(rtc->width(), rtc->height()), nullptr, nullptr); |
| 266 | rtc->addDrawOp(pb, GrNoClip(), std::move(op)); |
| 267 | } |
| 268 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 269 | #if GR_TEST_UTILS |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 270 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 271 | GrContext* context = ctxInfo.grContext(); |
| 272 | using FPFactory = GrProcessorTestFactory<GrFragmentProcessor>; |
| 273 | SkRandom random; |
| 274 | sk_sp<GrRenderTargetContext> rtc = context->makeRenderTargetContext( |
| 275 | SkBackingFit::kExact, 256, 256, kRGBA_8888_GrPixelConfig, nullptr); |
| 276 | GrSurfaceDesc desc; |
| 277 | desc.fWidth = 256; |
| 278 | desc.fHeight = 256; |
| 279 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 280 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 281 | |
| 282 | // Put premul data into the RGBA texture that the test FPs can optionally use. |
| 283 | std::unique_ptr<GrColor[]> rgbaData(new GrColor[256 * 256]); |
| 284 | for (int y = 0; y < 256; ++y) { |
| 285 | for (int x = 0; x < 256; ++x) { |
| 286 | rgbaData.get()[256 * y + x] = |
| 287 | texel_color(random.nextULessThan(256), random.nextULessThan(256)); |
| 288 | } |
| 289 | } |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 290 | sk_sp<GrTexture> tex0(context->resourceProvider()->createTexture( |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 291 | desc, SkBudgeted::kYes, rgbaData.get(), 256 * sizeof(GrColor))); |
| 292 | |
| 293 | // Put random values into the alpha texture that the test FPs can optionally use. |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 294 | desc.fConfig = kAlpha_8_GrPixelConfig; |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 295 | std::unique_ptr<uint8_t[]> alphaData(new uint8_t[256 * 256]); |
| 296 | for (int y = 0; y < 256; ++y) { |
| 297 | for (int x = 0; x < 256; ++x) { |
| 298 | alphaData.get()[256 * y + x] = random.nextULessThan(256); |
| 299 | } |
| 300 | } |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 301 | sk_sp<GrTexture> tex1(context->resourceProvider()->createTexture(desc, SkBudgeted::kYes, |
| 302 | alphaData.get(), 256)); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 303 | GrTexture* textures[] = {tex0.get(), tex1.get()}; |
| 304 | GrProcessorTestData testData(&random, context, rtc.get(), textures); |
| 305 | |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 306 | // Use a different array of premul colors for the output of the fragment processor that preceeds |
| 307 | // the fragment processor under test. |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 308 | for (int y = 0; y < 256; ++y) { |
| 309 | for (int x = 0; x < 256; ++x) { |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 310 | rgbaData.get()[256 * y + x] = texel_color(x, y); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 314 | |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 315 | sk_sp<GrTextureProxy> dataProxy = GrSurfaceProxy::MakeDeferred(*context->caps(), |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 316 | context->resourceProvider(), |
Robert Phillips | 30f9bc6 | 2017-02-22 15:28:38 -0500 | [diff] [blame] | 317 | desc, SkBudgeted::kYes, |
| 318 | rgbaData.get(), |
| 319 | 256 * sizeof(GrColor)); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 320 | |
| 321 | // Because processors factories configure themselves in random ways, this is not exhaustive. |
| 322 | for (int i = 0; i < FPFactory::Count(); ++i) { |
| 323 | int timesToInvokeFactory = 5; |
| 324 | // Increase the number of attempts if the FP has child FPs since optimizations likely depend |
| 325 | // on child optimizations being present. |
| 326 | sk_sp<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData); |
| 327 | for (int j = 0; j < fp->numChildProcessors(); ++j) { |
| 328 | // This value made a reasonable trade off between time and coverage when this test was |
| 329 | // written. |
| 330 | timesToInvokeFactory *= FPFactory::Count() / 2; |
| 331 | } |
| 332 | for (int j = 0; j < timesToInvokeFactory; ++j) { |
| 333 | fp = FPFactory::MakeIdx(i, &testData); |
| 334 | if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() && |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 335 | !fp->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 336 | continue; |
| 337 | } |
Robert Phillips | 2f49314 | 2017-03-02 18:18:38 -0500 | [diff] [blame] | 338 | test_draw_op(context, rtc.get(), fp, dataProxy); |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 339 | memset(rgbaData.get(), 0x0, sizeof(GrColor) * 256 * 256); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 340 | rtc->readPixels( |
| 341 | SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType), |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 342 | rgbaData.get(), 0, 0, 0); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 343 | bool passing = true; |
| 344 | if (0) { // Useful to see what FPs are being tested. |
| 345 | SkString children; |
| 346 | for (int c = 0; c < fp->numChildProcessors(); ++c) { |
| 347 | if (!c) { |
| 348 | children.append("("); |
| 349 | } |
| 350 | children.append(fp->childProcessor(c).name()); |
| 351 | children.append(c == fp->numChildProcessors() - 1 ? ")" : ", "); |
| 352 | } |
| 353 | SkDebugf("%s %s\n", fp->name(), children.c_str()); |
| 354 | } |
| 355 | for (int y = 0; y < 256 && passing; ++y) { |
| 356 | for (int x = 0; x < 256 && passing; ++x) { |
| 357 | GrColor input = texel_color(x, y); |
Brian Salomon | d1a8bdf | 2017-02-10 12:39:26 -0500 | [diff] [blame] | 358 | GrColor output = rgbaData.get()[y * 256 + x]; |
Brian Salomon | f3b995b | 2017-02-15 10:22:23 -0500 | [diff] [blame] | 359 | if (fp->compatibleWithCoverageAsAlpha()) { |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 360 | // A modulating processor is allowed to modulate either the input color or |
| 361 | // just the input alpha. |
| 362 | bool legalColorModulation = |
| 363 | GrColorUnpackA(output) <= GrColorUnpackA(input) && |
| 364 | GrColorUnpackR(output) <= GrColorUnpackR(input) && |
| 365 | GrColorUnpackG(output) <= GrColorUnpackG(input) && |
| 366 | GrColorUnpackB(output) <= GrColorUnpackB(input); |
| 367 | bool legalAlphaModulation = |
| 368 | GrColorUnpackA(output) <= GrColorUnpackA(input) && |
| 369 | GrColorUnpackR(output) <= GrColorUnpackA(input) && |
| 370 | GrColorUnpackG(output) <= GrColorUnpackA(input) && |
| 371 | GrColorUnpackB(output) <= GrColorUnpackA(input); |
| 372 | if (!legalColorModulation && !legalAlphaModulation) { |
| 373 | ERRORF(reporter, |
| 374 | "\"Modulating\" processor %s made color/alpha value larger. " |
| 375 | "Input: 0x%0x8, Output: 0x%08x.", |
| 376 | fp->name(), input, output); |
| 377 | passing = false; |
| 378 | } |
| 379 | } |
| 380 | GrColor4f input4f = texel_color4f(x, y); |
| 381 | GrColor4f output4f = GrColor4f::FromGrColor(output); |
| 382 | GrColor4f expected4f; |
| 383 | if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) { |
| 384 | float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]); |
| 385 | float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]); |
| 386 | float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]); |
| 387 | float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]); |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 388 | static constexpr float kTol = 4 / 255.f; |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 389 | if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) { |
| 390 | ERRORF(reporter, |
| 391 | "Processor %s claimed output for const input doesn't match " |
Brian Salomon | 5d4cd9e | 2017-02-09 11:16:46 -0500 | [diff] [blame] | 392 | "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, " |
| 393 | "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)", |
| 394 | fp->name(), SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))), |
| 395 | kTol, input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2], |
| 396 | input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1], |
| 397 | output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0], |
| 398 | expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 399 | passing = false; |
| 400 | } |
| 401 | } |
| 402 | if (GrColorIsOpaque(input) && fp->preservesOpaqueInput() && |
| 403 | !GrColorIsOpaque(output)) { |
| 404 | ERRORF(reporter, |
| 405 | "Processor %s claimed opaqueness is preserved but it is not. Input: " |
| 406 | "0x%0x8, Output: 0x%08x.", |
| 407 | fp->name(), input, output); |
| 408 | passing = false; |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 415 | #endif // GR_TEST_UTILS |
| 416 | #endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS |
| 417 | #endif // SK_SUPPORT_GPU |