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" |
| 14 | #include "GrRenderTargetContext.h" |
| 15 | #include "GrRenderTargetContextPriv.h" |
| 16 | #include "GrResourceProvider.h" |
| 17 | #include "glsl/GrGLSLFragmentProcessor.h" |
| 18 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 19 | #include "ops/GrTestMeshDrawOp.h" |
| 20 | |
| 21 | namespace { |
| 22 | class TestOp : public GrTestMeshDrawOp { |
| 23 | public: |
| 24 | DEFINE_OP_CLASS_ID |
| 25 | const char* name() const override { return "TestOp"; } |
| 26 | |
| 27 | static std::unique_ptr<GrDrawOp> Make() { return std::unique_ptr<GrDrawOp>(new TestOp); } |
| 28 | |
| 29 | private: |
| 30 | TestOp() : INHERITED(ClassID(), SkRect::MakeWH(100, 100), 0xFFFFFFFF) {} |
| 31 | |
| 32 | void onPrepareDraws(Target* target) const override { return; } |
| 33 | |
| 34 | typedef GrTestMeshDrawOp INHERITED; |
| 35 | }; |
| 36 | |
| 37 | /** |
| 38 | * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts |
| 39 | * of resources owned by child FPs. |
| 40 | */ |
| 41 | class TestFP : public GrFragmentProcessor { |
| 42 | public: |
| 43 | struct Image { |
| 44 | Image(sk_sp<GrTexture> texture, GrIOType ioType) : fTexture(texture), fIOType(ioType) {} |
| 45 | sk_sp<GrTexture> fTexture; |
| 46 | GrIOType fIOType; |
| 47 | }; |
| 48 | static sk_sp<GrFragmentProcessor> Make(sk_sp<GrFragmentProcessor> child) { |
| 49 | return sk_sp<GrFragmentProcessor>(new TestFP(std::move(child))); |
| 50 | } |
| 51 | static sk_sp<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTexture>>& textures, |
| 52 | const SkTArray<sk_sp<GrBuffer>>& buffers, |
| 53 | const SkTArray<Image>& images) { |
| 54 | return sk_sp<GrFragmentProcessor>(new TestFP(textures, buffers, images)); |
| 55 | } |
| 56 | |
| 57 | const char* name() const override { return "test"; } |
| 58 | |
| 59 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
| 60 | // We don't really care about reusing these. |
| 61 | static int32_t gKey = 0; |
| 62 | b->add32(sk_atomic_inc(&gKey)); |
| 63 | } |
| 64 | |
| 65 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
| 66 | // We don't care about optimizing these processors. |
| 67 | inout->setToUnknown(GrInvariantOutput::kWill_ReadInput); |
| 68 | } |
| 69 | |
| 70 | private: |
| 71 | TestFP(const SkTArray<sk_sp<GrTexture>>& textures, const SkTArray<sk_sp<GrBuffer>>& buffers, |
| 72 | const SkTArray<Image>& images) |
| 73 | : fSamplers(4), fBuffers(4), fImages(4) { |
| 74 | for (const auto& texture : textures) { |
| 75 | this->addTextureSampler(&fSamplers.emplace_back(texture.get())); |
| 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 | |
| 86 | TestFP(sk_sp<GrFragmentProcessor> child) : fSamplers(4), fBuffers(4), fImages(4) { |
| 87 | this->registerChildProcessor(std::move(child)); |
| 88 | } |
| 89 | |
| 90 | virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { |
| 91 | class TestGLSLFP : public GrGLSLFragmentProcessor { |
| 92 | public: |
| 93 | TestGLSLFP() {} |
| 94 | void emitCode(EmitArgs& args) override { |
| 95 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 96 | fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor); |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | }; |
| 101 | return new TestGLSLFP(); |
| 102 | } |
| 103 | |
| 104 | bool onIsEqual(const GrFragmentProcessor&) const override { return false; } |
| 105 | |
| 106 | GrTAllocator<TextureSampler> fSamplers; |
| 107 | GrTAllocator<BufferAccess> fBuffers; |
| 108 | GrTAllocator<ImageStorageAccess> fImages; |
| 109 | }; |
| 110 | } |
| 111 | |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 112 | template <typename T> |
| 113 | inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) { |
| 114 | *refCnt = resource->fRefCnt; |
| 115 | *readCnt = resource->fPendingReads; |
| 116 | *writeCnt = resource->fPendingWrites; |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) { |
| 120 | GrContext* context = ctxInfo.grContext(); |
| 121 | |
| 122 | GrTextureDesc desc; |
| 123 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 124 | desc.fWidth = 10; |
| 125 | desc.fHeight = 10; |
| 126 | |
| 127 | for (int parentCnt = 0; parentCnt < 2; parentCnt++) { |
| 128 | sk_sp<GrRenderTargetContext> renderTargetContext(context->makeRenderTargetContext( |
| 129 | SkBackingFit::kApprox, 1, 1, kRGBA_8888_GrPixelConfig, nullptr)); |
| 130 | { |
| 131 | bool texelBufferSupport = context->caps()->shaderCaps()->texelBufferSupport(); |
| 132 | bool imageLoadStoreSupport = context->caps()->shaderCaps()->imageLoadStoreSupport(); |
| 133 | sk_sp<GrTexture> texture1( |
| 134 | context->resourceProvider()->createTexture(desc, SkBudgeted::kYes)); |
| 135 | sk_sp<GrTexture> texture2( |
| 136 | context->resourceProvider()->createTexture(desc, SkBudgeted::kYes)); |
| 137 | sk_sp<GrTexture> texture3( |
| 138 | context->resourceProvider()->createTexture(desc, SkBudgeted::kYes)); |
| 139 | sk_sp<GrTexture> texture4( |
| 140 | context->resourceProvider()->createTexture(desc, SkBudgeted::kYes)); |
| 141 | sk_sp<GrBuffer> buffer(texelBufferSupport |
| 142 | ? context->resourceProvider()->createBuffer( |
| 143 | 1024, GrBufferType::kTexel_GrBufferType, |
| 144 | GrAccessPattern::kStatic_GrAccessPattern, 0) |
| 145 | : nullptr); |
| 146 | { |
| 147 | SkTArray<sk_sp<GrTexture>> textures; |
| 148 | SkTArray<sk_sp<GrBuffer>> buffers; |
| 149 | SkTArray<TestFP::Image> images; |
| 150 | textures.push_back(texture1); |
| 151 | if (texelBufferSupport) { |
| 152 | buffers.push_back(buffer); |
| 153 | } |
| 154 | if (imageLoadStoreSupport) { |
| 155 | images.emplace_back(texture2, GrIOType::kRead_GrIOType); |
| 156 | images.emplace_back(texture3, GrIOType::kWrite_GrIOType); |
| 157 | images.emplace_back(texture4, GrIOType::kRW_GrIOType); |
| 158 | } |
| 159 | std::unique_ptr<GrDrawOp> op(TestOp::Make()); |
| 160 | GrPaint paint; |
| 161 | auto fp = TestFP::Make(std::move(textures), std::move(buffers), std::move(images)); |
| 162 | for (int i = 0; i < parentCnt; ++i) { |
| 163 | fp = TestFP::Make(std::move(fp)); |
| 164 | } |
| 165 | paint.addColorFragmentProcessor(std::move(fp)); |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 166 | renderTargetContext->priv().testingOnly_addDrawOp(std::move(paint), GrAAType::kNone, |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 167 | std::move(op)); |
| 168 | } |
| 169 | int refCnt, readCnt, writeCnt; |
| 170 | |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 171 | testingOnly_getIORefCnts(texture1.get(), &refCnt, &readCnt, &writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 172 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 173 | REPORTER_ASSERT(reporter, 1 == readCnt); |
| 174 | REPORTER_ASSERT(reporter, 0 == writeCnt); |
| 175 | |
| 176 | if (texelBufferSupport) { |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 177 | testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 178 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 179 | REPORTER_ASSERT(reporter, 1 == readCnt); |
| 180 | REPORTER_ASSERT(reporter, 0 == writeCnt); |
| 181 | } |
| 182 | |
| 183 | if (imageLoadStoreSupport) { |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 184 | testingOnly_getIORefCnts(texture2.get(), &refCnt, &readCnt, &writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 185 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 186 | REPORTER_ASSERT(reporter, 1 == readCnt); |
| 187 | REPORTER_ASSERT(reporter, 0 == writeCnt); |
| 188 | |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 189 | testingOnly_getIORefCnts(texture3.get(), &refCnt, &readCnt, &writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 190 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 191 | REPORTER_ASSERT(reporter, 0 == readCnt); |
| 192 | REPORTER_ASSERT(reporter, 1 == writeCnt); |
| 193 | |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 194 | testingOnly_getIORefCnts(texture4.get(), &refCnt, &readCnt, &writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 195 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 196 | REPORTER_ASSERT(reporter, 1 == readCnt); |
| 197 | REPORTER_ASSERT(reporter, 1 == writeCnt); |
| 198 | } |
| 199 | |
| 200 | context->flush(); |
| 201 | |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 202 | testingOnly_getIORefCnts(texture1.get(), &refCnt, &readCnt, &writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 203 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 204 | REPORTER_ASSERT(reporter, 0 == readCnt); |
| 205 | REPORTER_ASSERT(reporter, 0 == writeCnt); |
| 206 | |
| 207 | if (texelBufferSupport) { |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 208 | testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 209 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 210 | REPORTER_ASSERT(reporter, 0 == readCnt); |
| 211 | REPORTER_ASSERT(reporter, 0 == writeCnt); |
| 212 | } |
| 213 | |
| 214 | if (texelBufferSupport) { |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 215 | testingOnly_getIORefCnts(texture2.get(), &refCnt, &readCnt, &writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 216 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 217 | REPORTER_ASSERT(reporter, 0 == readCnt); |
| 218 | REPORTER_ASSERT(reporter, 0 == writeCnt); |
| 219 | |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 220 | testingOnly_getIORefCnts(texture3.get(), &refCnt, &readCnt, &writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 221 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 222 | REPORTER_ASSERT(reporter, 0 == readCnt); |
| 223 | REPORTER_ASSERT(reporter, 0 == writeCnt); |
| 224 | |
Brian Salomon | f046e15 | 2017-01-11 15:24:47 -0500 | [diff] [blame] | 225 | testingOnly_getIORefCnts(texture4.get(), &refCnt, &readCnt, &writeCnt); |
Brian Salomon | bc6b99d | 2017-01-11 10:32:34 -0500 | [diff] [blame] | 226 | REPORTER_ASSERT(reporter, 1 == refCnt); |
| 227 | REPORTER_ASSERT(reporter, 0 == readCnt); |
| 228 | REPORTER_ASSERT(reporter, 0 == writeCnt); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | #endif |