blob: 1d393d6b7928c80c93cee2b6414854bda882add9 [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
Brian Salomonaff329b2017-08-11 09:40:37 -040028 static std::unique_ptr<GrDrawOp> Make(std::unique_ptr<GrFragmentProcessor> fp) {
Brian Salomon82ddc942017-07-14 12:00:13 -040029 return std::unique_ptr<GrDrawOp>(new TestOp(std::move(fp)));
30 }
31
Robert Phillips5f567c72017-09-14 08:27:37 -040032 const char* name() const override { return "TestOp"; }
33
Robert Phillipsf1748f52017-09-14 14:11:24 -040034 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips5f567c72017-09-14 08:27:37 -040035 fProcessors.visitProxies(func);
36 }
37
Brian Salomon82ddc942017-07-14 12:00:13 -040038 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
39
Brian Osman9a725dd2017-09-20 09:53:22 -040040 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
41 GrPixelConfigIsClamped dstIsClamped) override {
Brian Salomon82ddc942017-07-14 12:00:13 -040042 static constexpr GrProcessorAnalysisColor kUnknownColor;
43 GrColor overrideColor;
44 fProcessors.finalize(kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, false, caps,
Brian Osman9a725dd2017-09-20 09:53:22 -040045 dstIsClamped, &overrideColor);
Brian Salomon82ddc942017-07-14 12:00:13 -040046 return RequiresDstTexture::kNo;
Brian Salomon649a3412017-03-09 13:50:43 -050047 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050048
49private:
Brian Salomonaff329b2017-08-11 09:40:37 -040050 TestOp(std::unique_ptr<GrFragmentProcessor> fp)
51 : INHERITED(ClassID()), fProcessors(std::move(fp)) {
Brian Salomon82ddc942017-07-14 12:00:13 -040052 this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsZeroArea::kNo);
53 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050054
Brian Salomon91326c32017-08-09 16:02:19 -040055 void onPrepareDraws(Target* target) override { return; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050056
Brian Salomon82ddc942017-07-14 12:00:13 -040057 bool onCombineIfPossible(GrOp* op, const GrCaps& caps) override { return false; }
58
59 GrProcessorSet fProcessors;
60
61 typedef GrMeshDrawOp INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050062};
63
64/**
65 * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts
66 * of resources owned by child FPs.
67 */
68class TestFP : public GrFragmentProcessor {
69public:
Brian Salomonaff329b2017-08-11 09:40:37 -040070 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) {
71 return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child)));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050072 }
Brian Salomonaff329b2017-08-11 09:40:37 -040073 static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Brian Salomon559f5562017-11-15 14:28:33 -050074 const SkTArray<sk_sp<GrBuffer>>& buffers) {
75 return std::unique_ptr<GrFragmentProcessor>(new TestFP(proxies, buffers));
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 Salomonaff329b2017-08-11 09:40:37 -040086 std::unique_ptr<GrFragmentProcessor> clone() const override {
87 return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this));
Brian Salomonb17e6392017-07-28 13:41:51 -040088 }
89
Brian Salomonbc6b99d2017-01-11 10:32:34 -050090private:
Brian Salomon559f5562017-11-15 14:28:33 -050091 TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies, const SkTArray<sk_sp<GrBuffer>>& buffers)
92 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4), fBuffers(4) {
Robert Phillips30f9bc62017-02-22 15:28:38 -050093 for (const auto& proxy : proxies) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040094 this->addTextureSampler(&fSamplers.emplace_back(proxy));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050095 }
96 for (const auto& buffer : buffers) {
97 this->addBufferAccess(&fBuffers.emplace_back(kRGBA_8888_GrPixelConfig, buffer.get()));
98 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050099 }
100
Brian Salomonaff329b2017-08-11 09:40:37 -0400101 TestFP(std::unique_ptr<GrFragmentProcessor> child)
Brian Salomon559f5562017-11-15 14:28:33 -0500102 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4), fBuffers(4) {
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500103 this->registerChildProcessor(std::move(child));
104 }
105
Brian Salomon96271cd2017-07-31 16:27:23 -0400106 explicit TestFP(const TestFP& that)
Brian Salomon559f5562017-11-15 14:28:33 -0500107 : INHERITED(kTestFP_ClassID, that.optimizationFlags()), fSamplers(4), fBuffers(4) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400108 for (int i = 0; i < that.fSamplers.count(); ++i) {
109 fSamplers.emplace_back(that.fSamplers[i]);
110 this->addTextureSampler(&fSamplers.back());
111 }
112 for (int i = 0; i < that.fBuffers.count(); ++i) {
113 fBuffers.emplace_back(that.fBuffers[i]);
114 this->addBufferAccess(&fBuffers.back());
115 }
Brian Salomon96271cd2017-07-31 16:27:23 -0400116 for (int i = 0; i < that.numChildProcessors(); ++i) {
117 this->registerChildProcessor(that.childProcessor(i).clone());
Brian Salomonb17e6392017-07-28 13:41:51 -0400118 }
119 }
120
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500121 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
122 class TestGLSLFP : public GrGLSLFragmentProcessor {
123 public:
124 TestGLSLFP() {}
125 void emitCode(EmitArgs& args) override {
126 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
127 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
128 }
129
130 private:
131 };
132 return new TestGLSLFP();
133 }
134
135 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
136
137 GrTAllocator<TextureSampler> fSamplers;
138 GrTAllocator<BufferAccess> fBuffers;
Brian Salomon587e08f2017-01-27 10:59:27 -0500139 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500140};
141}
142
Brian Salomonf046e152017-01-11 15:24:47 -0500143template <typename T>
144inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) {
145 *refCnt = resource->fRefCnt;
146 *readCnt = resource->fPendingReads;
147 *writeCnt = resource->fPendingWrites;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500148}
149
Robert Phillips2f493142017-03-02 18:18:38 -0500150void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500151 *refCnt = proxy->getBackingRefCnt_TestOnly();
152 *readCnt = proxy->getPendingReadCnt_TestOnly();
153 *writeCnt = proxy->getPendingWriteCnt_TestOnly();
154}
155
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500156DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
157 GrContext* context = ctxInfo.grContext();
158
Robert Phillips16d8ec62017-07-27 16:16:25 -0400159 GrSurfaceDesc desc;
160 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500161 desc.fWidth = 10;
162 desc.fHeight = 10;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400163 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500164
Brian Salomonb17e6392017-07-28 13:41:51 -0400165 for (bool makeClone : {false, true}) {
166 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
167 sk_sp<GrRenderTargetContext> renderTargetContext(
168 context->makeDeferredRenderTargetContext( SkBackingFit::kApprox, 1, 1,
169 kRGBA_8888_GrPixelConfig, nullptr));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500170 {
Brian Salomonb17e6392017-07-28 13:41:51 -0400171 bool texelBufferSupport = context->caps()->shaderCaps()->texelBufferSupport();
Brian Salomonb17e6392017-07-28 13:41:51 -0400172 sk_sp<GrTextureProxy> proxy1(
173 GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
174 desc, SkBackingFit::kExact,
175 SkBudgeted::kYes));
176 sk_sp<GrTextureProxy> proxy2
177 (GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
178 desc, SkBackingFit::kExact,
179 SkBudgeted::kYes));
180 sk_sp<GrTextureProxy> proxy3(
181 GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
182 desc, SkBackingFit::kExact,
183 SkBudgeted::kYes));
184 sk_sp<GrTextureProxy> proxy4(
185 GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
186 desc, SkBackingFit::kExact,
187 SkBudgeted::kYes));
188 sk_sp<GrBuffer> buffer(texelBufferSupport
189 ? context->resourceProvider()->createBuffer(
190 1024, GrBufferType::kTexel_GrBufferType,
191 GrAccessPattern::kStatic_GrAccessPattern, 0)
192 : nullptr);
193 {
194 SkTArray<sk_sp<GrTextureProxy>> proxies;
195 SkTArray<sk_sp<GrBuffer>> buffers;
Brian Salomonb17e6392017-07-28 13:41:51 -0400196 proxies.push_back(proxy1);
197 if (texelBufferSupport) {
198 buffers.push_back(buffer);
199 }
Brian Salomon559f5562017-11-15 14:28:33 -0500200 auto fp = TestFP::Make(std::move(proxies), std::move(buffers));
Brian Salomonb17e6392017-07-28 13:41:51 -0400201 for (int i = 0; i < parentCnt; ++i) {
202 fp = TestFP::Make(std::move(fp));
203 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400204 std::unique_ptr<GrFragmentProcessor> clone;
Brian Salomonb17e6392017-07-28 13:41:51 -0400205 if (makeClone) {
206 clone = fp->clone();
207 }
208 std::unique_ptr<GrDrawOp> op(TestOp::Make(std::move(fp)));
209 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
210 if (clone) {
211 op = TestOp::Make(std::move(clone));
212 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
213 }
214 }
215 int refCnt, readCnt, writeCnt;
216
217 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
218 // IO counts should be double if there is a clone of the FP.
219 int ioRefMul = makeClone ? 2 : 1;
220 REPORTER_ASSERT(reporter, 1 == refCnt);
221 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
222 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
223
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500224 if (texelBufferSupport) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400225 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
226 REPORTER_ASSERT(reporter, 1 == refCnt);
227 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
228 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500229 }
Brian Salomonb17e6392017-07-28 13:41:51 -0400230
Brian Salomonb17e6392017-07-28 13:41:51 -0400231 context->flush();
232
233 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
234 REPORTER_ASSERT(reporter, 1 == refCnt);
235 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
236 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
237
238 if (texelBufferSupport) {
239 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
240 REPORTER_ASSERT(reporter, 1 == refCnt);
241 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
242 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500243 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500244
Brian Salomonb17e6392017-07-28 13:41:51 -0400245 if (texelBufferSupport) {
246 testingOnly_getIORefCnts(proxy2.get(), &refCnt, &readCnt, &writeCnt);
247 REPORTER_ASSERT(reporter, 1 == refCnt);
248 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
249 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500250
Brian Salomonb17e6392017-07-28 13:41:51 -0400251 testingOnly_getIORefCnts(proxy3.get(), &refCnt, &readCnt, &writeCnt);
252 REPORTER_ASSERT(reporter, 1 == refCnt);
253 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
254 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500255
Brian Salomonb17e6392017-07-28 13:41:51 -0400256 testingOnly_getIORefCnts(proxy4.get(), &refCnt, &readCnt, &writeCnt);
257 REPORTER_ASSERT(reporter, 1 == refCnt);
258 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
259 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
260 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500261 }
262 }
263 }
264}
Brian Salomon587e08f2017-01-27 10:59:27 -0500265
266// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
267#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
268
Brian Salomon0e05a822017-07-25 09:43:22 -0400269#include "SkCommandLineFlags.h"
270DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?");
271
272#if GR_TEST_UTILS
273
274static GrColor input_texel_color(int i, int j) {
275 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 -0500276 return GrPremulColor(color);
277}
278
Brian Salomon0e05a822017-07-25 09:43:22 -0400279static GrColor4f input_texel_color4f(int i, int j) {
280 return GrColor4f::FromGrColor(input_texel_color(i, j));
281}
Brian Salomon587e08f2017-01-27 10:59:27 -0500282
Brian Salomonaff329b2017-08-11 09:40:37 -0400283void test_draw_op(GrRenderTargetContext* rtc, std::unique_ptr<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500284 sk_sp<GrTextureProxy> inputDataProxy) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500285 GrPaint paint;
Brian Osman2240be92017-10-18 13:15:13 -0400286 paint.addColorTextureProcessor(std::move(inputDataProxy), SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500287 paint.addColorFragmentProcessor(std::move(fp));
288 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400289
Brian Salomonbaaf4392017-06-15 09:59:23 -0400290 auto op = GrRectOpFactory::MakeNonAAFill(std::move(paint), SkMatrix::I(),
291 SkRect::MakeWH(rtc->width(), rtc->height()),
292 GrAAType::kNone);
Brian Salomonac70f842017-05-08 10:43:33 -0400293 rtc->addDrawOp(GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500294}
295
Brian Salomon0e05a822017-07-25 09:43:22 -0400296/** Initializes the two test texture proxies that are available to the FP test factories. */
297bool init_test_textures(GrContext* context, SkRandom* random, sk_sp<GrTextureProxy> proxies[2]) {
298 static const int kTestTextureSize = 256;
299 GrSurfaceDesc desc;
300 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
301 desc.fWidth = kTestTextureSize;
302 desc.fHeight = kTestTextureSize;
303 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400304
Brian Salomon0e05a822017-07-25 09:43:22 -0400305 // Put premul data into the RGBA texture that the test FPs can optionally use.
306 std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]);
307 for (int y = 0; y < kTestTextureSize; ++y) {
308 for (int x = 0; x < kTestTextureSize; ++x) {
309 rgbaData[kTestTextureSize * y + x] =
310 input_texel_color(random->nextULessThan(256), random->nextULessThan(256));
311 }
312 }
313 proxies[0] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
314 rgbaData.get(), kTestTextureSize * sizeof(GrColor));
315
316 // Put random values into the alpha texture that the test FPs can optionally use.
317 desc.fConfig = kAlpha_8_GrPixelConfig;
318 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]);
319 for (int y = 0; y < kTestTextureSize; ++y) {
320 for (int x = 0; x < kTestTextureSize; ++x) {
321 alphaData[kTestTextureSize * y + x] = random->nextULessThan(256);
322 }
323 }
324 proxies[1] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
325 alphaData.get(), kTestTextureSize);
326
327 return proxies[0] && proxies[1];
328}
329
330// Creates a texture of premul colors used as the output of the fragment processor that precedes
331// the fragment processor under test. Color values are those provided by input_texel_color().
332sk_sp<GrTextureProxy> make_input_texture(GrContext* context, int width, int height) {
333 std::unique_ptr<GrColor[]> data(new GrColor[width * height]);
334 for (int y = 0; y < width; ++y) {
335 for (int x = 0; x < height; ++x) {
336 data.get()[width * y + x] = input_texel_color(x, y);
337 }
338 }
339 GrSurfaceDesc desc;
340 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
341 desc.fWidth = width;
342 desc.fHeight = height;
343 desc.fConfig = kRGBA_8888_GrPixelConfig;
344 return GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
345 data.get(), width * sizeof(GrColor));
346}
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500347DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500348 GrContext* context = ctxInfo.grContext();
Brian Salomon1c053642017-07-24 10:16:19 -0400349 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400350
351 uint32_t seed = 0;
352 if (FLAGS_randomProcessorTest) {
353 std::random_device rd;
354 seed = rd();
355 }
356 // If a non-deterministic bot fails this test, check the output to see what seed it used, then
357 // hard-code that value here:
358 SkRandom random(seed);
359
Brian Salomon0e05a822017-07-25 09:43:22 -0400360 // Make the destination context for the test.
361 static constexpr int kRenderSize = 256;
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400362 sk_sp<GrRenderTargetContext> rtc = context->makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400363 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500364
Robert Phillipse78b7252017-04-06 07:59:41 -0400365 sk_sp<GrTextureProxy> proxies[2];
Brian Salomon0e05a822017-07-25 09:43:22 -0400366 if (!init_test_textures(context, &random, proxies)) {
367 ERRORF(reporter, "Could not create test textures");
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400368 return;
369 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400370 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500371
Brian Salomon0e05a822017-07-25 09:43:22 -0400372 auto inputTexture = make_input_texture(context, kRenderSize, kRenderSize);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500373
Ben Wagner8c791942017-07-25 11:47:48 -0400374 std::unique_ptr<GrColor[]> readData(new GrColor[kRenderSize * kRenderSize]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400375 // Because processor factories configure themselves in random ways, this is not exhaustive.
Brian Salomon587e08f2017-01-27 10:59:27 -0500376 for (int i = 0; i < FPFactory::Count(); ++i) {
377 int timesToInvokeFactory = 5;
378 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
379 // on child optimizations being present.
Brian Salomonaff329b2017-08-11 09:40:37 -0400380 std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
Brian Salomon587e08f2017-01-27 10:59:27 -0500381 for (int j = 0; j < fp->numChildProcessors(); ++j) {
382 // This value made a reasonable trade off between time and coverage when this test was
383 // written.
384 timesToInvokeFactory *= FPFactory::Count() / 2;
385 }
386 for (int j = 0; j < timesToInvokeFactory; ++j) {
387 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillips9bee2e52017-05-29 12:37:20 -0400388 if (!fp->instantiate(context->resourceProvider())) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400389 continue;
390 }
391
Brian Salomon587e08f2017-01-27 10:59:27 -0500392 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500393 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500394 continue;
395 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400396
397 // Since we transfer away ownership of the original FP, we make a clone.
398 auto clone = fp->clone();
399
400 test_draw_op(rtc.get(), std::move(fp), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400401 memset(readData.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
402 rtc->readPixels(SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
403 kPremul_SkAlphaType),
404 readData.get(), 0, 0, 0);
Brian Salomon587e08f2017-01-27 10:59:27 -0500405 bool passing = true;
406 if (0) { // Useful to see what FPs are being tested.
407 SkString children;
Brian Salomonaff329b2017-08-11 09:40:37 -0400408 for (int c = 0; c < clone->numChildProcessors(); ++c) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500409 if (!c) {
410 children.append("(");
411 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400412 children.append(clone->name());
413 children.append(c == clone->numChildProcessors() - 1 ? ")" : ", ");
Brian Salomon587e08f2017-01-27 10:59:27 -0500414 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400415 SkDebugf("%s %s\n", clone->name(), children.c_str());
Brian Salomon587e08f2017-01-27 10:59:27 -0500416 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400417 for (int y = 0; y < kRenderSize && passing; ++y) {
418 for (int x = 0; x < kRenderSize && passing; ++x) {
419 GrColor input = input_texel_color(x, y);
420 GrColor output = readData.get()[y * kRenderSize + x];
Brian Salomonaff329b2017-08-11 09:40:37 -0400421 if (clone->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500422 // A modulating processor is allowed to modulate either the input color or
423 // just the input alpha.
424 bool legalColorModulation =
425 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
426 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
427 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
428 GrColorUnpackB(output) <= GrColorUnpackB(input);
429 bool legalAlphaModulation =
430 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
431 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
432 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
433 GrColorUnpackB(output) <= GrColorUnpackA(input);
434 if (!legalColorModulation && !legalAlphaModulation) {
435 ERRORF(reporter,
436 "\"Modulating\" processor %s made color/alpha value larger. "
Greg Daniel10ed2432017-12-01 16:19:43 -0500437 "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).",
438 clone->name(), input, output, x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500439 passing = false;
440 }
441 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400442 GrColor4f input4f = input_texel_color4f(x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500443 GrColor4f output4f = GrColor4f::FromGrColor(output);
444 GrColor4f expected4f;
Brian Salomonaff329b2017-08-11 09:40:37 -0400445 if (clone->hasConstantOutputForConstantInput(input4f, &expected4f)) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500446 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
447 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
448 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
449 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500450 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500451 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
452 ERRORF(reporter,
453 "Processor %s claimed output for const input doesn't match "
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500454 "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, "
455 "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)",
456 fp->name(), SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))),
457 kTol, input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2],
458 input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1],
459 output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0],
460 expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]);
Brian Salomon587e08f2017-01-27 10:59:27 -0500461 passing = false;
462 }
463 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400464 if (GrColorIsOpaque(input) && clone->preservesOpaqueInput() &&
Brian Salomon587e08f2017-01-27 10:59:27 -0500465 !GrColorIsOpaque(output)) {
466 ERRORF(reporter,
467 "Processor %s claimed opaqueness is preserved but it is not. Input: "
Brian Osman2f3865b2017-03-15 13:35:51 -0400468 "0x%08x, Output: 0x%08x.",
Brian Salomonaff329b2017-08-11 09:40:37 -0400469 clone->name(), input, output);
Brian Salomon587e08f2017-01-27 10:59:27 -0500470 passing = false;
471 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400472 if (!passing) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400473 ERRORF(reporter, "Seed: 0x%08x, Processor details: %s", seed,
474 clone->dumpInfo().c_str());
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400475 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500476 }
477 }
478 }
479 }
480}
Robert Phillips18166ee2017-06-01 12:55:44 -0400481
Brian Salomon0e05a822017-07-25 09:43:22 -0400482// Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their
483// progenitors.
484DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
485 GrContext* context = ctxInfo.grContext();
486
487 SkRandom random;
488
489 // Make the destination context for the test.
490 static constexpr int kRenderSize = 1024;
491 sk_sp<GrRenderTargetContext> rtc = context->makeDeferredRenderTargetContext(
492 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
493
494 sk_sp<GrTextureProxy> proxies[2];
495 if (!init_test_textures(context, &random, proxies)) {
496 ERRORF(reporter, "Could not create test textures");
497 return;
498 }
499 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
500
501 auto inputTexture = make_input_texture(context, kRenderSize, kRenderSize);
502 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
503 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
504 auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
505 kPremul_SkAlphaType);
506
507 // Because processor factories configure themselves in random ways, this is not exhaustive.
508 for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
509 static constexpr int kTimesToInvokeFactory = 10;
510 for (int j = 0; j < kTimesToInvokeFactory; ++j) {
511 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
512 auto clone = fp->clone();
Brian Salomon0e05a822017-07-25 09:43:22 -0400513 if (!clone) {
Brian Salomon96271cd2017-07-31 16:27:23 -0400514 ERRORF(reporter, "Clone of processor %s failed.", fp->name());
Brian Salomon0e05a822017-07-25 09:43:22 -0400515 continue;
516 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400517 const char* name = fp->name();
Brian Salomon0e05a822017-07-25 09:43:22 -0400518 if (!fp->instantiate(context->resourceProvider()) ||
519 !clone->instantiate(context->resourceProvider())) {
520 continue;
521 }
Brian Salomonce06e262017-08-01 16:23:40 -0400522 REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name()));
523 REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() ==
524 clone->compatibleWithCoverageAsAlpha());
525 REPORTER_ASSERT(reporter, fp->isEqual(*clone));
526 REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput());
527 REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() ==
528 clone->hasConstantOutputForConstantInput());
529 REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors());
530 REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords());
Brian Salomon0e05a822017-07-25 09:43:22 -0400531 // Draw with original and read back the results.
Brian Salomonaff329b2017-08-11 09:40:37 -0400532 test_draw_op(rtc.get(), std::move(fp), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400533 memset(readData1.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
534 rtc->readPixels(readInfo, readData1.get(), 0, 0, 0);
535
536 // Draw with clone and read back the results.
Brian Salomonaff329b2017-08-11 09:40:37 -0400537 test_draw_op(rtc.get(), std::move(clone), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400538 memset(readData2.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
539 rtc->readPixels(readInfo, readData2.get(), 0, 0, 0);
540
541 // Check that the results are the same.
542 bool passing = true;
543 for (int y = 0; y < kRenderSize && passing; ++y) {
544 for (int x = 0; x < kRenderSize && passing; ++x) {
545 int idx = y * kRenderSize + x;
546 if (readData1[idx] != readData2[idx]) {
547 ERRORF(reporter,
548 "Processor %s made clone produced different output. "
549 "Input color: 0x%08x, Original Output Color: 0x%08x, "
550 "Clone Output Color: 0x%08x..",
Brian Salomonaff329b2017-08-11 09:40:37 -0400551 name, input_texel_color(x, y), readData1[idx], readData2[idx]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400552 passing = false;
553 }
554 }
555 }
556 }
557 }
558}
559
Hal Canary6f6961e2017-01-31 13:50:44 -0500560#endif // GR_TEST_UTILS
561#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
562#endif // SK_SUPPORT_GPU