blob: d4350595b789ef04881d91d91629686cb8a39d0a [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"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050015#include "GrContextPriv.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050016#include "GrGpuResource.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050017#include "GrProxyProvider.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050018#include "GrRenderTargetContext.h"
19#include "GrRenderTargetContextPriv.h"
20#include "GrResourceProvider.h"
21#include "glsl/GrGLSLFragmentProcessor.h"
22#include "glsl/GrGLSLFragmentShaderBuilder.h"
Brian Salomon82ddc942017-07-14 12:00:13 -040023#include "ops/GrMeshDrawOp.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040024#include "ops/GrRectOpFactory.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050025
26namespace {
Brian Salomon82ddc942017-07-14 12:00:13 -040027class TestOp : public GrMeshDrawOp {
Brian Salomonbc6b99d2017-01-11 10:32:34 -050028public:
29 DEFINE_OP_CLASS_ID
Brian Salomonaff329b2017-08-11 09:40:37 -040030 static std::unique_ptr<GrDrawOp> Make(std::unique_ptr<GrFragmentProcessor> fp) {
Brian Salomon82ddc942017-07-14 12:00:13 -040031 return std::unique_ptr<GrDrawOp>(new TestOp(std::move(fp)));
32 }
33
Robert Phillips5f567c72017-09-14 08:27:37 -040034 const char* name() const override { return "TestOp"; }
35
Robert Phillipsf1748f52017-09-14 14:11:24 -040036 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips5f567c72017-09-14 08:27:37 -040037 fProcessors.visitProxies(func);
38 }
39
Brian Salomon82ddc942017-07-14 12:00:13 -040040 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
41
Brian Osman9a725dd2017-09-20 09:53:22 -040042 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
43 GrPixelConfigIsClamped dstIsClamped) override {
Brian Salomon82ddc942017-07-14 12:00:13 -040044 static constexpr GrProcessorAnalysisColor kUnknownColor;
45 GrColor overrideColor;
46 fProcessors.finalize(kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, false, caps,
Brian Osman9a725dd2017-09-20 09:53:22 -040047 dstIsClamped, &overrideColor);
Brian Salomon82ddc942017-07-14 12:00:13 -040048 return RequiresDstTexture::kNo;
Brian Salomon649a3412017-03-09 13:50:43 -050049 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050050
51private:
Brian Salomonaff329b2017-08-11 09:40:37 -040052 TestOp(std::unique_ptr<GrFragmentProcessor> fp)
53 : INHERITED(ClassID()), fProcessors(std::move(fp)) {
Brian Salomon82ddc942017-07-14 12:00:13 -040054 this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsZeroArea::kNo);
55 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050056
Brian Salomon91326c32017-08-09 16:02:19 -040057 void onPrepareDraws(Target* target) override { return; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050058
Brian Salomon82ddc942017-07-14 12:00:13 -040059 bool onCombineIfPossible(GrOp* op, const GrCaps& caps) override { return false; }
60
61 GrProcessorSet fProcessors;
62
63 typedef GrMeshDrawOp INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050064};
65
66/**
67 * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts
68 * of resources owned by child FPs.
69 */
70class TestFP : public GrFragmentProcessor {
71public:
Brian Salomonaff329b2017-08-11 09:40:37 -040072 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) {
73 return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child)));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050074 }
Brian Salomonaff329b2017-08-11 09:40:37 -040075 static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Brian Salomon559f5562017-11-15 14:28:33 -050076 const SkTArray<sk_sp<GrBuffer>>& buffers) {
77 return std::unique_ptr<GrFragmentProcessor>(new TestFP(proxies, buffers));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050078 }
79
80 const char* name() const override { return "test"; }
81
82 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
83 // We don't really care about reusing these.
84 static int32_t gKey = 0;
85 b->add32(sk_atomic_inc(&gKey));
86 }
87
Brian Salomonaff329b2017-08-11 09:40:37 -040088 std::unique_ptr<GrFragmentProcessor> clone() const override {
89 return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this));
Brian Salomonb17e6392017-07-28 13:41:51 -040090 }
91
Brian Salomonbc6b99d2017-01-11 10:32:34 -050092private:
Brian Salomon559f5562017-11-15 14:28:33 -050093 TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies, const SkTArray<sk_sp<GrBuffer>>& buffers)
94 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4), fBuffers(4) {
Robert Phillips30f9bc62017-02-22 15:28:38 -050095 for (const auto& proxy : proxies) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040096 this->addTextureSampler(&fSamplers.emplace_back(proxy));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050097 }
98 for (const auto& buffer : buffers) {
99 this->addBufferAccess(&fBuffers.emplace_back(kRGBA_8888_GrPixelConfig, buffer.get()));
100 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500101 }
102
Brian Salomonaff329b2017-08-11 09:40:37 -0400103 TestFP(std::unique_ptr<GrFragmentProcessor> child)
Brian Salomon559f5562017-11-15 14:28:33 -0500104 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4), fBuffers(4) {
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500105 this->registerChildProcessor(std::move(child));
106 }
107
Brian Salomon96271cd2017-07-31 16:27:23 -0400108 explicit TestFP(const TestFP& that)
Brian Salomon559f5562017-11-15 14:28:33 -0500109 : INHERITED(kTestFP_ClassID, that.optimizationFlags()), fSamplers(4), fBuffers(4) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400110 for (int i = 0; i < that.fSamplers.count(); ++i) {
111 fSamplers.emplace_back(that.fSamplers[i]);
112 this->addTextureSampler(&fSamplers.back());
113 }
114 for (int i = 0; i < that.fBuffers.count(); ++i) {
115 fBuffers.emplace_back(that.fBuffers[i]);
116 this->addBufferAccess(&fBuffers.back());
117 }
Brian Salomon96271cd2017-07-31 16:27:23 -0400118 for (int i = 0; i < that.numChildProcessors(); ++i) {
119 this->registerChildProcessor(that.childProcessor(i).clone());
Brian Salomonb17e6392017-07-28 13:41:51 -0400120 }
121 }
122
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500123 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
124 class TestGLSLFP : public GrGLSLFragmentProcessor {
125 public:
126 TestGLSLFP() {}
127 void emitCode(EmitArgs& args) override {
128 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
129 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
130 }
131
132 private:
133 };
134 return new TestGLSLFP();
135 }
136
137 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
138
139 GrTAllocator<TextureSampler> fSamplers;
140 GrTAllocator<BufferAccess> fBuffers;
Brian Salomon587e08f2017-01-27 10:59:27 -0500141 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500142};
143}
144
Brian Salomonf046e152017-01-11 15:24:47 -0500145template <typename T>
146inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) {
147 *refCnt = resource->fRefCnt;
148 *readCnt = resource->fPendingReads;
149 *writeCnt = resource->fPendingWrites;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500150}
151
Robert Phillips2f493142017-03-02 18:18:38 -0500152void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500153 *refCnt = proxy->getBackingRefCnt_TestOnly();
154 *readCnt = proxy->getPendingReadCnt_TestOnly();
155 *writeCnt = proxy->getPendingWriteCnt_TestOnly();
156}
157
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500158DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
159 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500160 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500161 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500162
Robert Phillips16d8ec62017-07-27 16:16:25 -0400163 GrSurfaceDesc desc;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500164 desc.fWidth = 10;
165 desc.fHeight = 10;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400166 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500167
Brian Salomonb17e6392017-07-28 13:41:51 -0400168 for (bool makeClone : {false, true}) {
169 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
170 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500171 context->contextPriv().makeDeferredRenderTargetContext(
172 SkBackingFit::kApprox, 1, 1,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500173 kRGBA_8888_GrPixelConfig, nullptr));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500174 {
Brian Salomonb17e6392017-07-28 13:41:51 -0400175 bool texelBufferSupport = context->caps()->shaderCaps()->texelBufferSupport();
Brian Salomon2a4f9832018-03-03 22:43:43 -0500176 sk_sp<GrTextureProxy> proxy1 = proxyProvider->createProxy(
177 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
178 sk_sp<GrTextureProxy> proxy2 = proxyProvider->createProxy(
179 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
180 sk_sp<GrTextureProxy> proxy3 = proxyProvider->createProxy(
181 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
182 sk_sp<GrTextureProxy> proxy4 = proxyProvider->createProxy(
183 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
Brian Salomonb17e6392017-07-28 13:41:51 -0400184 sk_sp<GrBuffer> buffer(texelBufferSupport
Robert Phillips6be756b2018-01-16 15:07:54 -0500185 ? resourceProvider->createBuffer(
Brian Salomonb17e6392017-07-28 13:41:51 -0400186 1024, GrBufferType::kTexel_GrBufferType,
187 GrAccessPattern::kStatic_GrAccessPattern, 0)
188 : nullptr);
189 {
190 SkTArray<sk_sp<GrTextureProxy>> proxies;
191 SkTArray<sk_sp<GrBuffer>> buffers;
Brian Salomonb17e6392017-07-28 13:41:51 -0400192 proxies.push_back(proxy1);
193 if (texelBufferSupport) {
194 buffers.push_back(buffer);
195 }
Brian Salomon559f5562017-11-15 14:28:33 -0500196 auto fp = TestFP::Make(std::move(proxies), std::move(buffers));
Brian Salomonb17e6392017-07-28 13:41:51 -0400197 for (int i = 0; i < parentCnt; ++i) {
198 fp = TestFP::Make(std::move(fp));
199 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400200 std::unique_ptr<GrFragmentProcessor> clone;
Brian Salomonb17e6392017-07-28 13:41:51 -0400201 if (makeClone) {
202 clone = fp->clone();
203 }
204 std::unique_ptr<GrDrawOp> op(TestOp::Make(std::move(fp)));
205 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
206 if (clone) {
207 op = TestOp::Make(std::move(clone));
208 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
209 }
210 }
211 int refCnt, readCnt, writeCnt;
212
213 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
214 // IO counts should be double if there is a clone of the FP.
215 int ioRefMul = makeClone ? 2 : 1;
216 REPORTER_ASSERT(reporter, 1 == refCnt);
217 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
218 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
219
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500220 if (texelBufferSupport) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400221 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
222 REPORTER_ASSERT(reporter, 1 == refCnt);
223 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
224 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500225 }
Brian Salomonb17e6392017-07-28 13:41:51 -0400226
Brian Salomonb17e6392017-07-28 13:41:51 -0400227 context->flush();
228
229 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
230 REPORTER_ASSERT(reporter, 1 == refCnt);
231 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
232 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
233
234 if (texelBufferSupport) {
235 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
236 REPORTER_ASSERT(reporter, 1 == refCnt);
237 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
238 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500239 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500240
Brian Salomonb17e6392017-07-28 13:41:51 -0400241 if (texelBufferSupport) {
242 testingOnly_getIORefCnts(proxy2.get(), &refCnt, &readCnt, &writeCnt);
243 REPORTER_ASSERT(reporter, 1 == refCnt);
244 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
245 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500246
Brian Salomonb17e6392017-07-28 13:41:51 -0400247 testingOnly_getIORefCnts(proxy3.get(), &refCnt, &readCnt, &writeCnt);
248 REPORTER_ASSERT(reporter, 1 == refCnt);
249 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
250 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500251
Brian Salomonb17e6392017-07-28 13:41:51 -0400252 testingOnly_getIORefCnts(proxy4.get(), &refCnt, &readCnt, &writeCnt);
253 REPORTER_ASSERT(reporter, 1 == refCnt);
254 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
255 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
256 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500257 }
258 }
259 }
260}
Brian Salomon587e08f2017-01-27 10:59:27 -0500261
262// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
263#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
264
Brian Salomon0e05a822017-07-25 09:43:22 -0400265#include "SkCommandLineFlags.h"
266DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?");
267
268#if GR_TEST_UTILS
269
270static GrColor input_texel_color(int i, int j) {
271 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 -0500272 return GrPremulColor(color);
273}
274
Brian Salomon0e05a822017-07-25 09:43:22 -0400275static GrColor4f input_texel_color4f(int i, int j) {
276 return GrColor4f::FromGrColor(input_texel_color(i, j));
277}
Brian Salomon587e08f2017-01-27 10:59:27 -0500278
Brian Salomonaff329b2017-08-11 09:40:37 -0400279void test_draw_op(GrRenderTargetContext* rtc, std::unique_ptr<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500280 sk_sp<GrTextureProxy> inputDataProxy) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500281 GrPaint paint;
Brian Osman2240be92017-10-18 13:15:13 -0400282 paint.addColorTextureProcessor(std::move(inputDataProxy), SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500283 paint.addColorFragmentProcessor(std::move(fp));
284 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400285
Brian Salomonbaaf4392017-06-15 09:59:23 -0400286 auto op = GrRectOpFactory::MakeNonAAFill(std::move(paint), SkMatrix::I(),
287 SkRect::MakeWH(rtc->width(), rtc->height()),
288 GrAAType::kNone);
Brian Salomonac70f842017-05-08 10:43:33 -0400289 rtc->addDrawOp(GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500290}
291
Brian Salomon0e05a822017-07-25 09:43:22 -0400292/** Initializes the two test texture proxies that are available to the FP test factories. */
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500293bool init_test_textures(GrProxyProvider* proxyProvider, SkRandom* random,
294 sk_sp<GrTextureProxy> proxies[2]) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400295 static const int kTestTextureSize = 256;
296 GrSurfaceDesc desc;
Brian Salomon0e05a822017-07-25 09:43:22 -0400297 desc.fWidth = kTestTextureSize;
298 desc.fHeight = kTestTextureSize;
299 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400300
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500301 {
302 // Put premul data into the RGBA texture that the test FPs can optionally use.
303 std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]);
304 for (int y = 0; y < kTestTextureSize; ++y) {
305 for (int x = 0; x < kTestTextureSize; ++x) {
306 rgbaData[kTestTextureSize * y + x] =
307 input_texel_color(random->nextULessThan(256), random->nextULessThan(256));
308 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400309 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400310
Brian Salomon58389b92018-03-07 13:01:25 -0500311 proxies[0] = proxyProvider->createTextureProxy(desc, SkBudgeted::kYes, rgbaData.get(),
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500312 kTestTextureSize * sizeof(GrColor));
Brian Salomon0e05a822017-07-25 09:43:22 -0400313 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500314
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
Brian Salomon58389b92018-03-07 13:01:25 -0500325 proxies[1] = proxyProvider->createTextureProxy(desc, SkBudgeted::kYes, alphaData.get(),
Brian Salomon2a4f9832018-03-03 22:43:43 -0500326 kTestTextureSize);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500327 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400328
329 return proxies[0] && proxies[1];
330}
331
332// Creates a texture of premul colors used as the output of the fragment processor that precedes
333// the fragment processor under test. Color values are those provided by input_texel_color().
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500334sk_sp<GrTextureProxy> make_input_texture(GrProxyProvider* proxyProvider, int width, int height) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400335 std::unique_ptr<GrColor[]> data(new GrColor[width * height]);
336 for (int y = 0; y < width; ++y) {
337 for (int x = 0; x < height; ++x) {
338 data.get()[width * y + x] = input_texel_color(x, y);
339 }
340 }
341 GrSurfaceDesc desc;
Brian Salomon0e05a822017-07-25 09:43:22 -0400342 desc.fWidth = width;
343 desc.fHeight = height;
344 desc.fConfig = kRGBA_8888_GrPixelConfig;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500345
Brian Salomon58389b92018-03-07 13:01:25 -0500346 return proxyProvider->createTextureProxy(desc, SkBudgeted::kYes, data.get(),
347 width * sizeof(GrColor));
Brian Salomon0e05a822017-07-25 09:43:22 -0400348}
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500349
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500350DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500351 GrContext* context = ctxInfo.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500352 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500353 auto resourceProvider = context->contextPriv().resourceProvider();
Brian Salomon1c053642017-07-24 10:16:19 -0400354 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400355
356 uint32_t seed = 0;
357 if (FLAGS_randomProcessorTest) {
358 std::random_device rd;
359 seed = rd();
360 }
361 // If a non-deterministic bot fails this test, check the output to see what seed it used, then
362 // hard-code that value here:
363 SkRandom random(seed);
364
Brian Salomon0e05a822017-07-25 09:43:22 -0400365 // Make the destination context for the test.
366 static constexpr int kRenderSize = 256;
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500367 sk_sp<GrRenderTargetContext> rtc = context->contextPriv().makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400368 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500369
Robert Phillipse78b7252017-04-06 07:59:41 -0400370 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500371 if (!init_test_textures(proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400372 ERRORF(reporter, "Could not create test textures");
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400373 return;
374 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400375 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500376
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500377 auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500378
Ben Wagner8c791942017-07-25 11:47:48 -0400379 std::unique_ptr<GrColor[]> readData(new GrColor[kRenderSize * kRenderSize]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400380 // Because processor factories configure themselves in random ways, this is not exhaustive.
Brian Salomon587e08f2017-01-27 10:59:27 -0500381 for (int i = 0; i < FPFactory::Count(); ++i) {
382 int timesToInvokeFactory = 5;
383 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
384 // on child optimizations being present.
Brian Salomonaff329b2017-08-11 09:40:37 -0400385 std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
Brian Salomon587e08f2017-01-27 10:59:27 -0500386 for (int j = 0; j < fp->numChildProcessors(); ++j) {
387 // This value made a reasonable trade off between time and coverage when this test was
388 // written.
389 timesToInvokeFactory *= FPFactory::Count() / 2;
390 }
391 for (int j = 0; j < timesToInvokeFactory; ++j) {
392 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillips6be756b2018-01-16 15:07:54 -0500393 if (!fp->instantiate(resourceProvider)) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400394 continue;
395 }
396
Brian Salomon587e08f2017-01-27 10:59:27 -0500397 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500398 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500399 continue;
400 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400401
402 // Since we transfer away ownership of the original FP, we make a clone.
403 auto clone = fp->clone();
404
405 test_draw_op(rtc.get(), std::move(fp), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400406 memset(readData.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
407 rtc->readPixels(SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
408 kPremul_SkAlphaType),
409 readData.get(), 0, 0, 0);
Brian Salomon587e08f2017-01-27 10:59:27 -0500410 bool passing = true;
411 if (0) { // Useful to see what FPs are being tested.
412 SkString children;
Brian Salomonaff329b2017-08-11 09:40:37 -0400413 for (int c = 0; c < clone->numChildProcessors(); ++c) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500414 if (!c) {
415 children.append("(");
416 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400417 children.append(clone->name());
418 children.append(c == clone->numChildProcessors() - 1 ? ")" : ", ");
Brian Salomon587e08f2017-01-27 10:59:27 -0500419 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400420 SkDebugf("%s %s\n", clone->name(), children.c_str());
Brian Salomon587e08f2017-01-27 10:59:27 -0500421 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400422 for (int y = 0; y < kRenderSize && passing; ++y) {
423 for (int x = 0; x < kRenderSize && passing; ++x) {
424 GrColor input = input_texel_color(x, y);
425 GrColor output = readData.get()[y * kRenderSize + x];
Brian Salomonaff329b2017-08-11 09:40:37 -0400426 if (clone->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500427 // A modulating processor is allowed to modulate either the input color or
428 // just the input alpha.
429 bool legalColorModulation =
430 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
431 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
432 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
433 GrColorUnpackB(output) <= GrColorUnpackB(input);
434 bool legalAlphaModulation =
435 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
436 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
437 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
438 GrColorUnpackB(output) <= GrColorUnpackA(input);
439 if (!legalColorModulation && !legalAlphaModulation) {
440 ERRORF(reporter,
441 "\"Modulating\" processor %s made color/alpha value larger. "
Greg Daniel10ed2432017-12-01 16:19:43 -0500442 "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).",
443 clone->name(), input, output, x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500444 passing = false;
445 }
446 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400447 GrColor4f input4f = input_texel_color4f(x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500448 GrColor4f output4f = GrColor4f::FromGrColor(output);
449 GrColor4f expected4f;
Brian Salomonaff329b2017-08-11 09:40:37 -0400450 if (clone->hasConstantOutputForConstantInput(input4f, &expected4f)) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500451 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
452 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
453 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
454 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500455 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500456 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
457 ERRORF(reporter,
458 "Processor %s claimed output for const input doesn't match "
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500459 "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, "
460 "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)",
Brian Salomonf61711a2018-02-16 10:44:28 -0500461 clone->name(),
462 SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))), kTol,
463 input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2],
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500464 input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1],
465 output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0],
466 expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]);
Brian Salomon587e08f2017-01-27 10:59:27 -0500467 passing = false;
468 }
469 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400470 if (GrColorIsOpaque(input) && clone->preservesOpaqueInput() &&
Brian Salomon587e08f2017-01-27 10:59:27 -0500471 !GrColorIsOpaque(output)) {
472 ERRORF(reporter,
473 "Processor %s claimed opaqueness is preserved but it is not. Input: "
Brian Osman2f3865b2017-03-15 13:35:51 -0400474 "0x%08x, Output: 0x%08x.",
Brian Salomonaff329b2017-08-11 09:40:37 -0400475 clone->name(), input, output);
Brian Salomon587e08f2017-01-27 10:59:27 -0500476 passing = false;
477 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400478 if (!passing) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400479 ERRORF(reporter, "Seed: 0x%08x, Processor details: %s", seed,
480 clone->dumpInfo().c_str());
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400481 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500482 }
483 }
484 }
485 }
486}
Robert Phillips18166ee2017-06-01 12:55:44 -0400487
Brian Salomon0e05a822017-07-25 09:43:22 -0400488// Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their
489// progenitors.
490DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
491 GrContext* context = ctxInfo.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500492 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500493 auto resourceProvider = context->contextPriv().resourceProvider();
Brian Salomon0e05a822017-07-25 09:43:22 -0400494
495 SkRandom random;
496
497 // Make the destination context for the test.
498 static constexpr int kRenderSize = 1024;
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500499 sk_sp<GrRenderTargetContext> rtc = context->contextPriv().makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400500 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
501
502 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500503 if (!init_test_textures(proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400504 ERRORF(reporter, "Could not create test textures");
505 return;
506 }
507 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
508
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500509 auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize);
Brian Salomon0e05a822017-07-25 09:43:22 -0400510 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
511 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
512 auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
513 kPremul_SkAlphaType);
514
515 // Because processor factories configure themselves in random ways, this is not exhaustive.
516 for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
517 static constexpr int kTimesToInvokeFactory = 10;
518 for (int j = 0; j < kTimesToInvokeFactory; ++j) {
519 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
520 auto clone = fp->clone();
Brian Salomon0e05a822017-07-25 09:43:22 -0400521 if (!clone) {
Brian Salomon96271cd2017-07-31 16:27:23 -0400522 ERRORF(reporter, "Clone of processor %s failed.", fp->name());
Brian Salomon0e05a822017-07-25 09:43:22 -0400523 continue;
524 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400525 const char* name = fp->name();
Robert Phillips6be756b2018-01-16 15:07:54 -0500526 if (!fp->instantiate(resourceProvider) || !clone->instantiate(resourceProvider)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400527 continue;
528 }
Brian Salomonce06e262017-08-01 16:23:40 -0400529 REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name()));
530 REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() ==
531 clone->compatibleWithCoverageAsAlpha());
532 REPORTER_ASSERT(reporter, fp->isEqual(*clone));
533 REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput());
534 REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() ==
535 clone->hasConstantOutputForConstantInput());
536 REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors());
537 REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords());
Brian Salomon0e05a822017-07-25 09:43:22 -0400538 // Draw with original and read back the results.
Brian Salomonaff329b2017-08-11 09:40:37 -0400539 test_draw_op(rtc.get(), std::move(fp), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400540 memset(readData1.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
541 rtc->readPixels(readInfo, readData1.get(), 0, 0, 0);
542
543 // Draw with clone and read back the results.
Brian Salomonaff329b2017-08-11 09:40:37 -0400544 test_draw_op(rtc.get(), std::move(clone), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400545 memset(readData2.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
546 rtc->readPixels(readInfo, readData2.get(), 0, 0, 0);
547
548 // Check that the results are the same.
549 bool passing = true;
550 for (int y = 0; y < kRenderSize && passing; ++y) {
551 for (int x = 0; x < kRenderSize && passing; ++x) {
552 int idx = y * kRenderSize + x;
553 if (readData1[idx] != readData2[idx]) {
554 ERRORF(reporter,
555 "Processor %s made clone produced different output. "
556 "Input color: 0x%08x, Original Output Color: 0x%08x, "
557 "Clone Output Color: 0x%08x..",
Brian Salomonaff329b2017-08-11 09:40:37 -0400558 name, input_texel_color(x, y), readData1[idx], readData2[idx]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400559 passing = false;
560 }
561 }
562 }
563 }
564 }
565}
566
Hal Canary6f6961e2017-01-31 13:50:44 -0500567#endif // GR_TEST_UTILS
568#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
569#endif // SK_SUPPORT_GPU