blob: b6cacccb954894f54a4b54acc80100b8630a2435 [file] [log] [blame]
Brian Salomonbc6b99d2017-01-11 10:32:34 -05001/*
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
21namespace {
22class TestOp : public GrTestMeshDrawOp {
23public:
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
29private:
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 */
41class TestFP : public GrFragmentProcessor {
42public:
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
70private:
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
112template <typename D>
113inline void GrIORef<D>::testingOnly_getCounts(int* refCnt, int* readCnt, int* writeCnt) const {
114 *refCnt = fRefCnt;
115 *readCnt = fPendingReads;
116 *writeCnt = fPendingWrites;
117}
118
119DEF_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));
166 renderTargetContext->priv().testingOnly_addDrawOp(paint, GrAAType::kNone,
167 std::move(op));
168 }
169 int refCnt, readCnt, writeCnt;
170
171 texture1->testingOnly_getCounts(&refCnt, &readCnt, &writeCnt);
172 REPORTER_ASSERT(reporter, 1 == refCnt);
173 REPORTER_ASSERT(reporter, 1 == readCnt);
174 REPORTER_ASSERT(reporter, 0 == writeCnt);
175
176 if (texelBufferSupport) {
177 buffer->testingOnly_getCounts(&refCnt, &readCnt, &writeCnt);
178 REPORTER_ASSERT(reporter, 1 == refCnt);
179 REPORTER_ASSERT(reporter, 1 == readCnt);
180 REPORTER_ASSERT(reporter, 0 == writeCnt);
181 }
182
183 if (imageLoadStoreSupport) {
184 texture2->testingOnly_getCounts(&refCnt, &readCnt, &writeCnt);
185 REPORTER_ASSERT(reporter, 1 == refCnt);
186 REPORTER_ASSERT(reporter, 1 == readCnt);
187 REPORTER_ASSERT(reporter, 0 == writeCnt);
188
189 texture3->testingOnly_getCounts(&refCnt, &readCnt, &writeCnt);
190 REPORTER_ASSERT(reporter, 1 == refCnt);
191 REPORTER_ASSERT(reporter, 0 == readCnt);
192 REPORTER_ASSERT(reporter, 1 == writeCnt);
193
194 texture4->testingOnly_getCounts(&refCnt, &readCnt, &writeCnt);
195 REPORTER_ASSERT(reporter, 1 == refCnt);
196 REPORTER_ASSERT(reporter, 1 == readCnt);
197 REPORTER_ASSERT(reporter, 1 == writeCnt);
198 }
199
200 context->flush();
201
202 texture1->testingOnly_getCounts(&refCnt, &readCnt, &writeCnt);
203 REPORTER_ASSERT(reporter, 1 == refCnt);
204 REPORTER_ASSERT(reporter, 0 == readCnt);
205 REPORTER_ASSERT(reporter, 0 == writeCnt);
206
207 if (texelBufferSupport) {
208 buffer->testingOnly_getCounts(&refCnt, &readCnt, &writeCnt);
209 REPORTER_ASSERT(reporter, 1 == refCnt);
210 REPORTER_ASSERT(reporter, 0 == readCnt);
211 REPORTER_ASSERT(reporter, 0 == writeCnt);
212 }
213
214 if (texelBufferSupport) {
215 texture2->testingOnly_getCounts(&refCnt, &readCnt, &writeCnt);
216 REPORTER_ASSERT(reporter, 1 == refCnt);
217 REPORTER_ASSERT(reporter, 0 == readCnt);
218 REPORTER_ASSERT(reporter, 0 == writeCnt);
219
220 texture3->testingOnly_getCounts(&refCnt, &readCnt, &writeCnt);
221 REPORTER_ASSERT(reporter, 1 == refCnt);
222 REPORTER_ASSERT(reporter, 0 == readCnt);
223 REPORTER_ASSERT(reporter, 0 == writeCnt);
224
225 texture4->testingOnly_getCounts(&refCnt, &readCnt, &writeCnt);
226 REPORTER_ASSERT(reporter, 1 == refCnt);
227 REPORTER_ASSERT(reporter, 0 == readCnt);
228 REPORTER_ASSERT(reporter, 0 == writeCnt);
229 }
230 }
231 }
232}
233#endif