blob: ff31e49619724a11fcf999baa6037d8120c8f7dc [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
247DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
248 // This tests code under development but not used in skia lib. Leaving this disabled until
249 // some platform-specific issues are addressed.
250 if (1) {
251 return;
252 }
253 GrContext* context = ctxInfo.grContext();
254 using FPFactory = GrProcessorTestFactory<GrFragmentProcessor>;
255 SkRandom random;
256 sk_sp<GrRenderTargetContext> rtc = context->makeRenderTargetContext(
257 SkBackingFit::kExact, 256, 256, kRGBA_8888_GrPixelConfig, nullptr);
258 GrSurfaceDesc desc;
259 desc.fWidth = 256;
260 desc.fHeight = 256;
261 desc.fFlags = kRenderTarget_GrSurfaceFlag;
262 desc.fConfig = kRGBA_8888_GrPixelConfig;
263 sk_sp<GrTexture> tex0(context->textureProvider()->createTexture(desc, SkBudgeted::kYes));
264 desc.fConfig = kAlpha_8_GrPixelConfig;
265 sk_sp<GrTexture> tex1(context->textureProvider()->createTexture(desc, SkBudgeted::kYes));
266 GrTexture* textures[] = {tex0.get(), tex1.get()};
267 GrProcessorTestData testData(&random, context, rtc.get(), textures);
268
269 std::unique_ptr<GrColor> data(new GrColor[256 * 256]);
270 for (int y = 0; y < 256; ++y) {
271 for (int x = 0; x < 256; ++x) {
272 data.get()[256 * y + x] = texel_color(x, y);
273 }
274 }
275 desc.fConfig = kRGBA_8888_GrPixelConfig;
276 sk_sp<GrTexture> dataTexture(context->textureProvider()->createTexture(
277 desc, SkBudgeted::kYes, data.get(), 256 * sizeof(GrColor)));
278
279 // Because processors factories configure themselves in random ways, this is not exhaustive.
280 for (int i = 0; i < FPFactory::Count(); ++i) {
281 int timesToInvokeFactory = 5;
282 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
283 // on child optimizations being present.
284 sk_sp<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
285 for (int j = 0; j < fp->numChildProcessors(); ++j) {
286 // This value made a reasonable trade off between time and coverage when this test was
287 // written.
288 timesToInvokeFactory *= FPFactory::Count() / 2;
289 }
290 for (int j = 0; j < timesToInvokeFactory; ++j) {
291 fp = FPFactory::MakeIdx(i, &testData);
292 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
293 !fp->modulatesInput()) {
294 continue;
295 }
296 GrPaint paint;
297 paint.addColorTextureProcessor(dataTexture.get(), nullptr, SkMatrix::I());
298 paint.addColorFragmentProcessor(fp);
299 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
300 rtc->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
301 SkRect::MakeWH(256.f, 256.f));
302 memset(data.get(), 0x0, sizeof(GrColor) * 256 * 256);
303 rtc->readPixels(
304 SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
305 data.get(), 0, 0, 0);
306 bool passing = true;
307 if (0) { // Useful to see what FPs are being tested.
308 SkString children;
309 for (int c = 0; c < fp->numChildProcessors(); ++c) {
310 if (!c) {
311 children.append("(");
312 }
313 children.append(fp->childProcessor(c).name());
314 children.append(c == fp->numChildProcessors() - 1 ? ")" : ", ");
315 }
316 SkDebugf("%s %s\n", fp->name(), children.c_str());
317 }
318 for (int y = 0; y < 256 && passing; ++y) {
319 for (int x = 0; x < 256 && passing; ++x) {
320 GrColor input = texel_color(x, y);
321 GrColor output = data.get()[y * 256 + x];
322 if (fp->modulatesInput()) {
323 // A modulating processor is allowed to modulate either the input color or
324 // just the input alpha.
325 bool legalColorModulation =
326 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
327 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
328 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
329 GrColorUnpackB(output) <= GrColorUnpackB(input);
330 bool legalAlphaModulation =
331 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
332 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
333 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
334 GrColorUnpackB(output) <= GrColorUnpackA(input);
335 if (!legalColorModulation && !legalAlphaModulation) {
336 ERRORF(reporter,
337 "\"Modulating\" processor %s made color/alpha value larger. "
338 "Input: 0x%0x8, Output: 0x%08x.",
339 fp->name(), input, output);
340 passing = false;
341 }
342 }
343 GrColor4f input4f = texel_color4f(x, y);
344 GrColor4f output4f = GrColor4f::FromGrColor(output);
345 GrColor4f expected4f;
346 if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) {
347 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
348 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
349 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
350 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
351 static constexpr float kTol = 3 / 255.f;
352 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
353 ERRORF(reporter,
354 "Processor %s claimed output for const input doesn't match "
355 "actual output.",
356 fp->name());
357 passing = false;
358 }
359 }
360 if (GrColorIsOpaque(input) && fp->preservesOpaqueInput() &&
361 !GrColorIsOpaque(output)) {
362 ERRORF(reporter,
363 "Processor %s claimed opaqueness is preserved but it is not. Input: "
364 "0x%0x8, Output: 0x%08x.",
365 fp->name(), input, output);
366 passing = false;
367 }
368 }
369 }
370 }
371 }
372}
373#endif
374
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500375#endif