blob: 3bfd62e3a46aa56668b170e9b09e1789469b397d [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
Brian Salomonac70f842017-05-08 10:43:33 -040012#include <random>
Brian Salomonc65aec92017-03-09 09:03:58 -050013#include "GrClip.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050014#include "GrContext.h"
15#include "GrGpuResource.h"
16#include "GrRenderTargetContext.h"
17#include "GrRenderTargetContextPriv.h"
18#include "GrResourceProvider.h"
19#include "glsl/GrGLSLFragmentProcessor.h"
20#include "glsl/GrGLSLFragmentShaderBuilder.h"
Brian Salomon82ddc942017-07-14 12:00:13 -040021#include "ops/GrMeshDrawOp.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040022#include "ops/GrRectOpFactory.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050023
24namespace {
Brian Salomon82ddc942017-07-14 12:00:13 -040025class TestOp : public GrMeshDrawOp {
Brian Salomonbc6b99d2017-01-11 10:32:34 -050026public:
27 DEFINE_OP_CLASS_ID
28 const char* name() const override { return "TestOp"; }
29
Brian Salomon82ddc942017-07-14 12:00:13 -040030 static std::unique_ptr<GrDrawOp> Make(sk_sp<GrFragmentProcessor> fp) {
31 return std::unique_ptr<GrDrawOp>(new TestOp(std::move(fp)));
32 }
33
34 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
35
36 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
37 static constexpr GrProcessorAnalysisColor kUnknownColor;
38 GrColor overrideColor;
39 fProcessors.finalize(kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, false, caps,
40 &overrideColor);
41 return RequiresDstTexture::kNo;
Brian Salomon649a3412017-03-09 13:50:43 -050042 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050043
44private:
Brian Salomon82ddc942017-07-14 12:00:13 -040045 TestOp(sk_sp<GrFragmentProcessor> fp) : INHERITED(ClassID()), fProcessors(std::move(fp)) {
46 this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsZeroArea::kNo);
47 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050048
49 void onPrepareDraws(Target* target) const override { return; }
50
Brian Salomon82ddc942017-07-14 12:00:13 -040051 bool onCombineIfPossible(GrOp* op, const GrCaps& caps) override { return false; }
52
53 GrProcessorSet fProcessors;
54
55 typedef GrMeshDrawOp INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050056};
57
58/**
59 * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts
60 * of resources owned by child FPs.
61 */
62class TestFP : public GrFragmentProcessor {
63public:
64 struct Image {
Robert Phillips8a02f652017-05-12 14:49:16 -040065 Image(sk_sp<GrTextureProxy> proxy, GrIOType ioType) : fProxy(proxy), fIOType(ioType) {}
66 sk_sp<GrTextureProxy> fProxy;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050067 GrIOType fIOType;
68 };
69 static sk_sp<GrFragmentProcessor> Make(sk_sp<GrFragmentProcessor> child) {
70 return sk_sp<GrFragmentProcessor>(new TestFP(std::move(child)));
71 }
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040072 static sk_sp<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Brian Salomonbc6b99d2017-01-11 10:32:34 -050073 const SkTArray<sk_sp<GrBuffer>>& buffers,
74 const SkTArray<Image>& images) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040075 return sk_sp<GrFragmentProcessor>(new TestFP(proxies, buffers, images));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050076 }
77
78 const char* name() const override { return "test"; }
79
80 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
81 // We don't really care about reusing these.
82 static int32_t gKey = 0;
83 b->add32(sk_atomic_inc(&gKey));
84 }
85
Brian Salomonb17e6392017-07-28 13:41:51 -040086 sk_sp<GrFragmentProcessor> clone() const override {
87 sk_sp<GrFragmentProcessor> child;
88 if (this->numChildProcessors()) {
89 SkASSERT(1 == this->numChildProcessors());
90 child = this->childProcessor(0).clone();
91 if (!child) {
92 return nullptr;
93 }
94 }
95 return sk_sp<GrFragmentProcessor> (new TestFP(*this, std::move(child)));
96 }
97
Brian Salomonbc6b99d2017-01-11 10:32:34 -050098private:
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040099 TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500100 const SkTArray<sk_sp<GrBuffer>>& buffers,
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500101 const SkTArray<Image>& images)
Brian Salomon587e08f2017-01-27 10:59:27 -0500102 : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400103 this->initClassID<TestFP>();
Robert Phillips30f9bc62017-02-22 15:28:38 -0500104 for (const auto& proxy : proxies) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400105 this->addTextureSampler(&fSamplers.emplace_back(proxy));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500106 }
107 for (const auto& buffer : buffers) {
108 this->addBufferAccess(&fBuffers.emplace_back(kRGBA_8888_GrPixelConfig, buffer.get()));
109 }
110 for (const Image& image : images) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400111 fImages.emplace_back(image.fProxy, image.fIOType,
112 GrSLMemoryModel::kNone, GrSLRestrict::kNo);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400113 this->addImageStorageAccess(&fImages.back());
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500114 }
115 }
116
Brian Salomon587e08f2017-01-27 10:59:27 -0500117 TestFP(sk_sp<GrFragmentProcessor> child)
118 : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400119 this->initClassID<TestFP>();
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500120 this->registerChildProcessor(std::move(child));
121 }
122
Brian Salomonb17e6392017-07-28 13:41:51 -0400123 explicit TestFP(const TestFP& that, sk_sp<GrFragmentProcessor> child)
124 : INHERITED(that.optimizationFlags()), fSamplers(4), fBuffers(4), fImages(4) {
125 this->initClassID<TestFP>();
126 for (int i = 0; i < that.fSamplers.count(); ++i) {
127 fSamplers.emplace_back(that.fSamplers[i]);
128 this->addTextureSampler(&fSamplers.back());
129 }
130 for (int i = 0; i < that.fBuffers.count(); ++i) {
131 fBuffers.emplace_back(that.fBuffers[i]);
132 this->addBufferAccess(&fBuffers.back());
133 }
134 for (int i = 0; i < that.fImages.count(); ++i) {
135 fImages.emplace_back(that.fImages[i]);
136 this->addImageStorageAccess(&fImages.back());
137 }
138 if (child) {
139 this->registerChildProcessor(std::move(child));
140 }
141 }
142
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500143 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
144 class TestGLSLFP : public GrGLSLFragmentProcessor {
145 public:
146 TestGLSLFP() {}
147 void emitCode(EmitArgs& args) override {
148 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
149 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
150 }
151
152 private:
153 };
154 return new TestGLSLFP();
155 }
156
157 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
158
159 GrTAllocator<TextureSampler> fSamplers;
160 GrTAllocator<BufferAccess> fBuffers;
161 GrTAllocator<ImageStorageAccess> fImages;
Brian Salomon587e08f2017-01-27 10:59:27 -0500162 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500163};
164}
165
Brian Salomonf046e152017-01-11 15:24:47 -0500166template <typename T>
167inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) {
168 *refCnt = resource->fRefCnt;
169 *readCnt = resource->fPendingReads;
170 *writeCnt = resource->fPendingWrites;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500171}
172
Robert Phillips2f493142017-03-02 18:18:38 -0500173void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500174 *refCnt = proxy->getBackingRefCnt_TestOnly();
175 *readCnt = proxy->getPendingReadCnt_TestOnly();
176 *writeCnt = proxy->getPendingWriteCnt_TestOnly();
177}
178
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500179DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
180 GrContext* context = ctxInfo.grContext();
181
Robert Phillips16d8ec62017-07-27 16:16:25 -0400182 GrSurfaceDesc desc;
183 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500184 desc.fWidth = 10;
185 desc.fHeight = 10;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400186 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500187
Brian Salomonb17e6392017-07-28 13:41:51 -0400188 for (bool makeClone : {false, true}) {
189 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
190 sk_sp<GrRenderTargetContext> renderTargetContext(
191 context->makeDeferredRenderTargetContext( SkBackingFit::kApprox, 1, 1,
192 kRGBA_8888_GrPixelConfig, nullptr));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500193 {
Brian Salomonb17e6392017-07-28 13:41:51 -0400194 bool texelBufferSupport = context->caps()->shaderCaps()->texelBufferSupport();
195 bool imageLoadStoreSupport = context->caps()->shaderCaps()->imageLoadStoreSupport();
196 sk_sp<GrTextureProxy> proxy1(
197 GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
198 desc, SkBackingFit::kExact,
199 SkBudgeted::kYes));
200 sk_sp<GrTextureProxy> proxy2
201 (GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
202 desc, SkBackingFit::kExact,
203 SkBudgeted::kYes));
204 sk_sp<GrTextureProxy> proxy3(
205 GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
206 desc, SkBackingFit::kExact,
207 SkBudgeted::kYes));
208 sk_sp<GrTextureProxy> proxy4(
209 GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
210 desc, SkBackingFit::kExact,
211 SkBudgeted::kYes));
212 sk_sp<GrBuffer> buffer(texelBufferSupport
213 ? context->resourceProvider()->createBuffer(
214 1024, GrBufferType::kTexel_GrBufferType,
215 GrAccessPattern::kStatic_GrAccessPattern, 0)
216 : nullptr);
217 {
218 SkTArray<sk_sp<GrTextureProxy>> proxies;
219 SkTArray<sk_sp<GrBuffer>> buffers;
220 SkTArray<TestFP::Image> images;
221 proxies.push_back(proxy1);
222 if (texelBufferSupport) {
223 buffers.push_back(buffer);
224 }
225 if (imageLoadStoreSupport) {
226 images.emplace_back(proxy2, GrIOType::kRead_GrIOType);
227 images.emplace_back(proxy3, GrIOType::kWrite_GrIOType);
228 images.emplace_back(proxy4, GrIOType::kRW_GrIOType);
229 }
230 auto fp = TestFP::Make(std::move(proxies), std::move(buffers),
231 std::move(images));
232 for (int i = 0; i < parentCnt; ++i) {
233 fp = TestFP::Make(std::move(fp));
234 }
235 sk_sp<GrFragmentProcessor> clone;
236 if (makeClone) {
237 clone = fp->clone();
238 }
239 std::unique_ptr<GrDrawOp> op(TestOp::Make(std::move(fp)));
240 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
241 if (clone) {
242 op = TestOp::Make(std::move(clone));
243 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
244 }
245 }
246 int refCnt, readCnt, writeCnt;
247
248 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
249 // IO counts should be double if there is a clone of the FP.
250 int ioRefMul = makeClone ? 2 : 1;
251 REPORTER_ASSERT(reporter, 1 == refCnt);
252 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
253 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
254
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500255 if (texelBufferSupport) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400256 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
257 REPORTER_ASSERT(reporter, 1 == refCnt);
258 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
259 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500260 }
Brian Salomonb17e6392017-07-28 13:41:51 -0400261
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500262 if (imageLoadStoreSupport) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400263 testingOnly_getIORefCnts(proxy2.get(), &refCnt, &readCnt, &writeCnt);
264 REPORTER_ASSERT(reporter, 1 == refCnt);
265 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
266 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
267
268 testingOnly_getIORefCnts(proxy3.get(), &refCnt, &readCnt, &writeCnt);
269 REPORTER_ASSERT(reporter, 1 == refCnt);
270 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
271 REPORTER_ASSERT(reporter, ioRefMul * 1 == writeCnt);
272
273 testingOnly_getIORefCnts(proxy4.get(), &refCnt, &readCnt, &writeCnt);
274 REPORTER_ASSERT(reporter, 1 == refCnt);
275 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
276 REPORTER_ASSERT(reporter, ioRefMul * 1 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500277 }
Brian Salomonb17e6392017-07-28 13:41:51 -0400278
279 context->flush();
280
281 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
282 REPORTER_ASSERT(reporter, 1 == refCnt);
283 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
284 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
285
286 if (texelBufferSupport) {
287 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
288 REPORTER_ASSERT(reporter, 1 == refCnt);
289 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
290 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500291 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500292
Brian Salomonb17e6392017-07-28 13:41:51 -0400293 if (texelBufferSupport) {
294 testingOnly_getIORefCnts(proxy2.get(), &refCnt, &readCnt, &writeCnt);
295 REPORTER_ASSERT(reporter, 1 == refCnt);
296 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
297 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500298
Brian Salomonb17e6392017-07-28 13:41:51 -0400299 testingOnly_getIORefCnts(proxy3.get(), &refCnt, &readCnt, &writeCnt);
300 REPORTER_ASSERT(reporter, 1 == refCnt);
301 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
302 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500303
Brian Salomonb17e6392017-07-28 13:41:51 -0400304 testingOnly_getIORefCnts(proxy4.get(), &refCnt, &readCnt, &writeCnt);
305 REPORTER_ASSERT(reporter, 1 == refCnt);
306 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
307 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
308 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500309 }
310 }
311 }
312}
Brian Salomon587e08f2017-01-27 10:59:27 -0500313
314// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
315#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
316
Brian Salomon0e05a822017-07-25 09:43:22 -0400317#include "SkCommandLineFlags.h"
318DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?");
319
320#if GR_TEST_UTILS
321
322static GrColor input_texel_color(int i, int j) {
323 GrColor color = GrColorPackRGBA((uint8_t)j, (uint8_t)(i + j), (uint8_t)(2 * j - i), (uint8_t)i);
Brian Salomon587e08f2017-01-27 10:59:27 -0500324 return GrPremulColor(color);
325}
326
Brian Salomon0e05a822017-07-25 09:43:22 -0400327static GrColor4f input_texel_color4f(int i, int j) {
328 return GrColor4f::FromGrColor(input_texel_color(i, j));
329}
Brian Salomon587e08f2017-01-27 10:59:27 -0500330
Robert Phillips296b1cc2017-03-15 10:42:12 -0400331void test_draw_op(GrRenderTargetContext* rtc, sk_sp<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500332 sk_sp<GrTextureProxy> inputDataProxy) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500333 GrPaint paint;
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400334 paint.addColorTextureProcessor(std::move(inputDataProxy), nullptr, SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500335 paint.addColorFragmentProcessor(std::move(fp));
336 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400337
Brian Salomonbaaf4392017-06-15 09:59:23 -0400338 auto op = GrRectOpFactory::MakeNonAAFill(std::move(paint), SkMatrix::I(),
339 SkRect::MakeWH(rtc->width(), rtc->height()),
340 GrAAType::kNone);
Brian Salomonac70f842017-05-08 10:43:33 -0400341 rtc->addDrawOp(GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500342}
343
Brian Salomon0e05a822017-07-25 09:43:22 -0400344/** Initializes the two test texture proxies that are available to the FP test factories. */
345bool init_test_textures(GrContext* context, SkRandom* random, sk_sp<GrTextureProxy> proxies[2]) {
346 static const int kTestTextureSize = 256;
347 GrSurfaceDesc desc;
348 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
349 desc.fWidth = kTestTextureSize;
350 desc.fHeight = kTestTextureSize;
351 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400352
Brian Salomon0e05a822017-07-25 09:43:22 -0400353 // Put premul data into the RGBA texture that the test FPs can optionally use.
354 std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]);
355 for (int y = 0; y < kTestTextureSize; ++y) {
356 for (int x = 0; x < kTestTextureSize; ++x) {
357 rgbaData[kTestTextureSize * y + x] =
358 input_texel_color(random->nextULessThan(256), random->nextULessThan(256));
359 }
360 }
361 proxies[0] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
362 rgbaData.get(), kTestTextureSize * sizeof(GrColor));
363
364 // Put random values into the alpha texture that the test FPs can optionally use.
365 desc.fConfig = kAlpha_8_GrPixelConfig;
366 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]);
367 for (int y = 0; y < kTestTextureSize; ++y) {
368 for (int x = 0; x < kTestTextureSize; ++x) {
369 alphaData[kTestTextureSize * y + x] = random->nextULessThan(256);
370 }
371 }
372 proxies[1] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
373 alphaData.get(), kTestTextureSize);
374
375 return proxies[0] && proxies[1];
376}
377
378// Creates a texture of premul colors used as the output of the fragment processor that precedes
379// the fragment processor under test. Color values are those provided by input_texel_color().
380sk_sp<GrTextureProxy> make_input_texture(GrContext* context, int width, int height) {
381 std::unique_ptr<GrColor[]> data(new GrColor[width * height]);
382 for (int y = 0; y < width; ++y) {
383 for (int x = 0; x < height; ++x) {
384 data.get()[width * y + x] = input_texel_color(x, y);
385 }
386 }
387 GrSurfaceDesc desc;
388 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
389 desc.fWidth = width;
390 desc.fHeight = height;
391 desc.fConfig = kRGBA_8888_GrPixelConfig;
392 return GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
393 data.get(), width * sizeof(GrColor));
394}
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500395DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500396 GrContext* context = ctxInfo.grContext();
Brian Salomon1c053642017-07-24 10:16:19 -0400397 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400398
399 uint32_t seed = 0;
400 if (FLAGS_randomProcessorTest) {
401 std::random_device rd;
402 seed = rd();
403 }
404 // If a non-deterministic bot fails this test, check the output to see what seed it used, then
405 // hard-code that value here:
406 SkRandom random(seed);
407
Brian Salomon0e05a822017-07-25 09:43:22 -0400408 // Make the destination context for the test.
409 static constexpr int kRenderSize = 256;
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400410 sk_sp<GrRenderTargetContext> rtc = context->makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400411 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500412
Robert Phillipse78b7252017-04-06 07:59:41 -0400413 sk_sp<GrTextureProxy> proxies[2];
Brian Salomon0e05a822017-07-25 09:43:22 -0400414 if (!init_test_textures(context, &random, proxies)) {
415 ERRORF(reporter, "Could not create test textures");
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400416 return;
417 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400418 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500419
Brian Salomon0e05a822017-07-25 09:43:22 -0400420 auto inputTexture = make_input_texture(context, kRenderSize, kRenderSize);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500421
Ben Wagner8c791942017-07-25 11:47:48 -0400422 std::unique_ptr<GrColor[]> readData(new GrColor[kRenderSize * kRenderSize]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400423 // Because processor factories configure themselves in random ways, this is not exhaustive.
Brian Salomon587e08f2017-01-27 10:59:27 -0500424 for (int i = 0; i < FPFactory::Count(); ++i) {
425 int timesToInvokeFactory = 5;
426 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
427 // on child optimizations being present.
428 sk_sp<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
429 for (int j = 0; j < fp->numChildProcessors(); ++j) {
430 // This value made a reasonable trade off between time and coverage when this test was
431 // written.
432 timesToInvokeFactory *= FPFactory::Count() / 2;
433 }
434 for (int j = 0; j < timesToInvokeFactory; ++j) {
435 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillips9bee2e52017-05-29 12:37:20 -0400436 if (!fp->instantiate(context->resourceProvider())) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400437 continue;
438 }
439
Brian Salomon587e08f2017-01-27 10:59:27 -0500440 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500441 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500442 continue;
443 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400444 test_draw_op(rtc.get(), fp, inputTexture);
445 memset(readData.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
446 rtc->readPixels(SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
447 kPremul_SkAlphaType),
448 readData.get(), 0, 0, 0);
Brian Salomon587e08f2017-01-27 10:59:27 -0500449 bool passing = true;
450 if (0) { // Useful to see what FPs are being tested.
451 SkString children;
452 for (int c = 0; c < fp->numChildProcessors(); ++c) {
453 if (!c) {
454 children.append("(");
455 }
456 children.append(fp->childProcessor(c).name());
457 children.append(c == fp->numChildProcessors() - 1 ? ")" : ", ");
458 }
459 SkDebugf("%s %s\n", fp->name(), children.c_str());
460 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400461 for (int y = 0; y < kRenderSize && passing; ++y) {
462 for (int x = 0; x < kRenderSize && passing; ++x) {
463 GrColor input = input_texel_color(x, y);
464 GrColor output = readData.get()[y * kRenderSize + x];
Brian Salomonf3b995b2017-02-15 10:22:23 -0500465 if (fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500466 // A modulating processor is allowed to modulate either the input color or
467 // just the input alpha.
468 bool legalColorModulation =
469 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
470 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
471 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
472 GrColorUnpackB(output) <= GrColorUnpackB(input);
473 bool legalAlphaModulation =
474 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
475 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
476 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
477 GrColorUnpackB(output) <= GrColorUnpackA(input);
478 if (!legalColorModulation && !legalAlphaModulation) {
479 ERRORF(reporter,
480 "\"Modulating\" processor %s made color/alpha value larger. "
Brian Osman2f3865b2017-03-15 13:35:51 -0400481 "Input: 0x%08x, Output: 0x%08x.",
Brian Salomon587e08f2017-01-27 10:59:27 -0500482 fp->name(), input, output);
483 passing = false;
484 }
485 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400486 GrColor4f input4f = input_texel_color4f(x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500487 GrColor4f output4f = GrColor4f::FromGrColor(output);
488 GrColor4f expected4f;
489 if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) {
490 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
491 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
492 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
493 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500494 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500495 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
496 ERRORF(reporter,
497 "Processor %s claimed output for const input doesn't match "
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500498 "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, "
499 "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)",
500 fp->name(), SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))),
501 kTol, input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2],
502 input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1],
503 output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0],
504 expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]);
Brian Salomon587e08f2017-01-27 10:59:27 -0500505 passing = false;
506 }
507 }
508 if (GrColorIsOpaque(input) && fp->preservesOpaqueInput() &&
509 !GrColorIsOpaque(output)) {
510 ERRORF(reporter,
511 "Processor %s claimed opaqueness is preserved but it is not. Input: "
Brian Osman2f3865b2017-03-15 13:35:51 -0400512 "0x%08x, Output: 0x%08x.",
Brian Salomon587e08f2017-01-27 10:59:27 -0500513 fp->name(), input, output);
514 passing = false;
515 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400516 if (!passing) {
Brian Osmanc35a2d42017-03-17 10:58:53 -0400517 ERRORF(reporter, "Seed: 0x%08x, Processor details: %s",
518 seed, fp->dumpInfo().c_str());
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400519 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500520 }
521 }
522 }
523 }
524}
Robert Phillips18166ee2017-06-01 12:55:44 -0400525
Brian Salomon0e05a822017-07-25 09:43:22 -0400526// Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their
527// progenitors.
528DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
529 GrContext* context = ctxInfo.grContext();
530
531 SkRandom random;
532
533 // Make the destination context for the test.
534 static constexpr int kRenderSize = 1024;
535 sk_sp<GrRenderTargetContext> rtc = context->makeDeferredRenderTargetContext(
536 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
537
538 sk_sp<GrTextureProxy> proxies[2];
539 if (!init_test_textures(context, &random, proxies)) {
540 ERRORF(reporter, "Could not create test textures");
541 return;
542 }
543 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
544
545 auto inputTexture = make_input_texture(context, kRenderSize, kRenderSize);
546 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
547 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
548 auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
549 kPremul_SkAlphaType);
550
551 // Because processor factories configure themselves in random ways, this is not exhaustive.
552 for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
553 static constexpr int kTimesToInvokeFactory = 10;
554 for (int j = 0; j < kTimesToInvokeFactory; ++j) {
555 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
556 auto clone = fp->clone();
557 // Currently clone() is allowed to fail.
558 if (!clone) {
559 continue;
560 }
561 if (!fp->instantiate(context->resourceProvider()) ||
562 !clone->instantiate(context->resourceProvider())) {
563 continue;
564 }
565 // Draw with original and read back the results.
566 test_draw_op(rtc.get(), fp, inputTexture);
567 memset(readData1.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
568 rtc->readPixels(readInfo, readData1.get(), 0, 0, 0);
569
570 // Draw with clone and read back the results.
571 test_draw_op(rtc.get(), clone, inputTexture);
572 memset(readData2.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
573 rtc->readPixels(readInfo, readData2.get(), 0, 0, 0);
574
575 // Check that the results are the same.
576 bool passing = true;
577 for (int y = 0; y < kRenderSize && passing; ++y) {
578 for (int x = 0; x < kRenderSize && passing; ++x) {
579 int idx = y * kRenderSize + x;
580 if (readData1[idx] != readData2[idx]) {
581 ERRORF(reporter,
582 "Processor %s made clone produced different output. "
583 "Input color: 0x%08x, Original Output Color: 0x%08x, "
584 "Clone Output Color: 0x%08x..",
585 fp->name(), input_texel_color(x, y), readData1[idx], readData2[idx]);
586 passing = false;
587 }
588 }
589 }
590 }
591 }
592}
593
Hal Canary6f6961e2017-01-31 13:50:44 -0500594#endif // GR_TEST_UTILS
595#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
596#endif // SK_SUPPORT_GPU