blob: a23b40ab884bfbe7ea3e82369bc6275e573038c2 [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.
Brian Salomon5f13fba2017-01-23 14:35:25 -050067 inout->setToUnknown();
Brian Salomonbc6b99d2017-01-11 10:32:34 -050068 }
69
70private:
71 TestFP(const SkTArray<sk_sp<GrTexture>>& textures, const SkTArray<sk_sp<GrBuffer>>& buffers,
72 const SkTArray<Image>& images)
Brian Salomon587e08f2017-01-27 10:59:27 -050073 : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) {
Brian Salomonbc6b99d2017-01-11 10:32:34 -050074 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
Brian Salomon587e08f2017-01-27 10:59:27 -050086 TestFP(sk_sp<GrFragmentProcessor> child)
87 : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) {
Brian Salomonbc6b99d2017-01-11 10:32:34 -050088 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 Salomon587e08f2017-01-27 10:59:27 -0500110 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500111};
112}
113
Brian Salomonf046e152017-01-11 15:24:47 -0500114template <typename T>
115inline 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 Salomonbc6b99d2017-01-11 10:32:34 -0500119}
120
121DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
122 GrContext* context = ctxInfo.grContext();
123
124 GrTextureDesc desc;
125 desc.fConfig = kRGBA_8888_GrPixelConfig;
126 desc.fWidth = 10;
127 desc.fHeight = 10;
128
129 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
130 sk_sp<GrRenderTargetContext> renderTargetContext(context->makeRenderTargetContext(
131 SkBackingFit::kApprox, 1, 1, kRGBA_8888_GrPixelConfig, nullptr));
132 {
133 bool texelBufferSupport = context->caps()->shaderCaps()->texelBufferSupport();
134 bool imageLoadStoreSupport = context->caps()->shaderCaps()->imageLoadStoreSupport();
135 sk_sp<GrTexture> texture1(
136 context->resourceProvider()->createTexture(desc, SkBudgeted::kYes));
137 sk_sp<GrTexture> texture2(
138 context->resourceProvider()->createTexture(desc, SkBudgeted::kYes));
139 sk_sp<GrTexture> texture3(
140 context->resourceProvider()->createTexture(desc, SkBudgeted::kYes));
141 sk_sp<GrTexture> texture4(
142 context->resourceProvider()->createTexture(desc, SkBudgeted::kYes));
143 sk_sp<GrBuffer> buffer(texelBufferSupport
144 ? context->resourceProvider()->createBuffer(
145 1024, GrBufferType::kTexel_GrBufferType,
146 GrAccessPattern::kStatic_GrAccessPattern, 0)
147 : nullptr);
148 {
149 SkTArray<sk_sp<GrTexture>> textures;
150 SkTArray<sk_sp<GrBuffer>> buffers;
151 SkTArray<TestFP::Image> images;
152 textures.push_back(texture1);
153 if (texelBufferSupport) {
154 buffers.push_back(buffer);
155 }
156 if (imageLoadStoreSupport) {
157 images.emplace_back(texture2, GrIOType::kRead_GrIOType);
158 images.emplace_back(texture3, GrIOType::kWrite_GrIOType);
159 images.emplace_back(texture4, GrIOType::kRW_GrIOType);
160 }
161 std::unique_ptr<GrDrawOp> op(TestOp::Make());
162 GrPaint paint;
163 auto fp = TestFP::Make(std::move(textures), std::move(buffers), std::move(images));
164 for (int i = 0; i < parentCnt; ++i) {
165 fp = TestFP::Make(std::move(fp));
166 }
167 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomon82f44312017-01-11 13:42:54 -0500168 renderTargetContext->priv().testingOnly_addDrawOp(std::move(paint), GrAAType::kNone,
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500169 std::move(op));
170 }
171 int refCnt, readCnt, writeCnt;
172
Brian Salomonf046e152017-01-11 15:24:47 -0500173 testingOnly_getIORefCnts(texture1.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500174 REPORTER_ASSERT(reporter, 1 == refCnt);
175 REPORTER_ASSERT(reporter, 1 == readCnt);
176 REPORTER_ASSERT(reporter, 0 == writeCnt);
177
178 if (texelBufferSupport) {
Brian Salomonf046e152017-01-11 15:24:47 -0500179 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500180 REPORTER_ASSERT(reporter, 1 == refCnt);
181 REPORTER_ASSERT(reporter, 1 == readCnt);
182 REPORTER_ASSERT(reporter, 0 == writeCnt);
183 }
184
185 if (imageLoadStoreSupport) {
Brian Salomonf046e152017-01-11 15:24:47 -0500186 testingOnly_getIORefCnts(texture2.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500187 REPORTER_ASSERT(reporter, 1 == refCnt);
188 REPORTER_ASSERT(reporter, 1 == readCnt);
189 REPORTER_ASSERT(reporter, 0 == writeCnt);
190
Brian Salomonf046e152017-01-11 15:24:47 -0500191 testingOnly_getIORefCnts(texture3.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500192 REPORTER_ASSERT(reporter, 1 == refCnt);
193 REPORTER_ASSERT(reporter, 0 == readCnt);
194 REPORTER_ASSERT(reporter, 1 == writeCnt);
195
Brian Salomonf046e152017-01-11 15:24:47 -0500196 testingOnly_getIORefCnts(texture4.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500197 REPORTER_ASSERT(reporter, 1 == refCnt);
198 REPORTER_ASSERT(reporter, 1 == readCnt);
199 REPORTER_ASSERT(reporter, 1 == writeCnt);
200 }
201
202 context->flush();
203
Brian Salomonf046e152017-01-11 15:24:47 -0500204 testingOnly_getIORefCnts(texture1.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500205 REPORTER_ASSERT(reporter, 1 == refCnt);
206 REPORTER_ASSERT(reporter, 0 == readCnt);
207 REPORTER_ASSERT(reporter, 0 == writeCnt);
208
209 if (texelBufferSupport) {
Brian Salomonf046e152017-01-11 15:24:47 -0500210 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500211 REPORTER_ASSERT(reporter, 1 == refCnt);
212 REPORTER_ASSERT(reporter, 0 == readCnt);
213 REPORTER_ASSERT(reporter, 0 == writeCnt);
214 }
215
216 if (texelBufferSupport) {
Brian Salomonf046e152017-01-11 15:24:47 -0500217 testingOnly_getIORefCnts(texture2.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500218 REPORTER_ASSERT(reporter, 1 == refCnt);
219 REPORTER_ASSERT(reporter, 0 == readCnt);
220 REPORTER_ASSERT(reporter, 0 == writeCnt);
221
Brian Salomonf046e152017-01-11 15:24:47 -0500222 testingOnly_getIORefCnts(texture3.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500223 REPORTER_ASSERT(reporter, 1 == refCnt);
224 REPORTER_ASSERT(reporter, 0 == readCnt);
225 REPORTER_ASSERT(reporter, 0 == writeCnt);
226
Brian Salomonf046e152017-01-11 15:24:47 -0500227 testingOnly_getIORefCnts(texture4.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500228 REPORTER_ASSERT(reporter, 1 == refCnt);
229 REPORTER_ASSERT(reporter, 0 == readCnt);
230 REPORTER_ASSERT(reporter, 0 == writeCnt);
231 }
232 }
233 }
234}
Brian Salomon587e08f2017-01-27 10:59:27 -0500235
236// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
237#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
238
239static GrColor texel_color(int i, int j) {
240 SkASSERT((unsigned)i < 256 && (unsigned)j < 256);
241 GrColor color = GrColorPackRGBA(j, (uint8_t)(i + j), (uint8_t)(2 * j - i), i);
242 return GrPremulColor(color);
243}
244
245static GrColor4f texel_color4f(int i, int j) { return GrColor4f::FromGrColor(texel_color(i, j)); }
246
Hal Canary6f6961e2017-01-31 13:50:44 -0500247#if GR_TEST_UTILS
Brian Salomon587e08f2017-01-27 10:59:27 -0500248DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
249 // This tests code under development but not used in skia lib. Leaving this disabled until
250 // some platform-specific issues are addressed.
251 if (1) {
252 return;
253 }
254 GrContext* context = ctxInfo.grContext();
255 using FPFactory = GrProcessorTestFactory<GrFragmentProcessor>;
256 SkRandom random;
257 sk_sp<GrRenderTargetContext> rtc = context->makeRenderTargetContext(
258 SkBackingFit::kExact, 256, 256, kRGBA_8888_GrPixelConfig, nullptr);
259 GrSurfaceDesc desc;
260 desc.fWidth = 256;
261 desc.fHeight = 256;
262 desc.fFlags = kRenderTarget_GrSurfaceFlag;
263 desc.fConfig = kRGBA_8888_GrPixelConfig;
264 sk_sp<GrTexture> tex0(context->textureProvider()->createTexture(desc, SkBudgeted::kYes));
265 desc.fConfig = kAlpha_8_GrPixelConfig;
266 sk_sp<GrTexture> tex1(context->textureProvider()->createTexture(desc, SkBudgeted::kYes));
267 GrTexture* textures[] = {tex0.get(), tex1.get()};
268 GrProcessorTestData testData(&random, context, rtc.get(), textures);
269
270 std::unique_ptr<GrColor> data(new GrColor[256 * 256]);
271 for (int y = 0; y < 256; ++y) {
272 for (int x = 0; x < 256; ++x) {
273 data.get()[256 * y + x] = texel_color(x, y);
274 }
275 }
276 desc.fConfig = kRGBA_8888_GrPixelConfig;
277 sk_sp<GrTexture> dataTexture(context->textureProvider()->createTexture(
278 desc, SkBudgeted::kYes, data.get(), 256 * sizeof(GrColor)));
279
280 // Because processors factories configure themselves in random ways, this is not exhaustive.
281 for (int i = 0; i < FPFactory::Count(); ++i) {
282 int timesToInvokeFactory = 5;
283 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
284 // on child optimizations being present.
285 sk_sp<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
286 for (int j = 0; j < fp->numChildProcessors(); ++j) {
287 // This value made a reasonable trade off between time and coverage when this test was
288 // written.
289 timesToInvokeFactory *= FPFactory::Count() / 2;
290 }
291 for (int j = 0; j < timesToInvokeFactory; ++j) {
292 fp = FPFactory::MakeIdx(i, &testData);
293 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
294 !fp->modulatesInput()) {
295 continue;
296 }
297 GrPaint paint;
298 paint.addColorTextureProcessor(dataTexture.get(), nullptr, SkMatrix::I());
299 paint.addColorFragmentProcessor(fp);
300 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
301 rtc->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
302 SkRect::MakeWH(256.f, 256.f));
303 memset(data.get(), 0x0, sizeof(GrColor) * 256 * 256);
304 rtc->readPixels(
305 SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
306 data.get(), 0, 0, 0);
307 bool passing = true;
308 if (0) { // Useful to see what FPs are being tested.
309 SkString children;
310 for (int c = 0; c < fp->numChildProcessors(); ++c) {
311 if (!c) {
312 children.append("(");
313 }
314 children.append(fp->childProcessor(c).name());
315 children.append(c == fp->numChildProcessors() - 1 ? ")" : ", ");
316 }
317 SkDebugf("%s %s\n", fp->name(), children.c_str());
318 }
319 for (int y = 0; y < 256 && passing; ++y) {
320 for (int x = 0; x < 256 && passing; ++x) {
321 GrColor input = texel_color(x, y);
322 GrColor output = data.get()[y * 256 + x];
323 if (fp->modulatesInput()) {
324 // A modulating processor is allowed to modulate either the input color or
325 // just the input alpha.
326 bool legalColorModulation =
327 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
328 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
329 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
330 GrColorUnpackB(output) <= GrColorUnpackB(input);
331 bool legalAlphaModulation =
332 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
333 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
334 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
335 GrColorUnpackB(output) <= GrColorUnpackA(input);
336 if (!legalColorModulation && !legalAlphaModulation) {
337 ERRORF(reporter,
338 "\"Modulating\" processor %s made color/alpha value larger. "
339 "Input: 0x%0x8, Output: 0x%08x.",
340 fp->name(), input, output);
341 passing = false;
342 }
343 }
344 GrColor4f input4f = texel_color4f(x, y);
345 GrColor4f output4f = GrColor4f::FromGrColor(output);
346 GrColor4f expected4f;
347 if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) {
348 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
349 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
350 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
351 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
352 static constexpr float kTol = 3 / 255.f;
353 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
354 ERRORF(reporter,
355 "Processor %s claimed output for const input doesn't match "
356 "actual output.",
357 fp->name());
358 passing = false;
359 }
360 }
361 if (GrColorIsOpaque(input) && fp->preservesOpaqueInput() &&
362 !GrColorIsOpaque(output)) {
363 ERRORF(reporter,
364 "Processor %s claimed opaqueness is preserved but it is not. Input: "
365 "0x%0x8, Output: 0x%08x.",
366 fp->name(), input, output);
367 passing = false;
368 }
369 }
370 }
371 }
372 }
373}
Hal Canary6f6961e2017-01-31 13:50:44 -0500374#endif // GR_TEST_UTILS
375#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
376#endif // SK_SUPPORT_GPU