blob: e52070b7d2cca09e9c7fd727d92a119ee96f8bf1 [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
Brian Salomonac70f842017-05-08 10:43:33 -040011#include <random>
Brian Salomonc65aec92017-03-09 09:03:58 -050012#include "GrClip.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050013#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050014#include "GrContextPriv.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050015#include "GrGpuResource.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040016#include "GrMemoryPool.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
Robert Phillips7c525e62018-06-12 10:11:12 -040030 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
31 std::unique_ptr<GrFragmentProcessor> fp) {
Robert Phillipsc994a932018-06-19 13:09:54 -040032 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
33
34 return pool->allocate<TestOp>(std::move(fp));
Brian Salomon82ddc942017-07-14 12:00:13 -040035 }
36
Robert Phillips5f567c72017-09-14 08:27:37 -040037 const char* name() const override { return "TestOp"; }
38
Robert Phillipsf1748f52017-09-14 14:11:24 -040039 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips5f567c72017-09-14 08:27:37 -040040 fProcessors.visitProxies(func);
41 }
42
Brian Salomon82ddc942017-07-14 12:00:13 -040043 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
44
Brian Osman9a725dd2017-09-20 09:53:22 -040045 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
46 GrPixelConfigIsClamped dstIsClamped) override {
Brian Salomon82ddc942017-07-14 12:00:13 -040047 static constexpr GrProcessorAnalysisColor kUnknownColor;
48 GrColor overrideColor;
49 fProcessors.finalize(kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, false, caps,
Brian Osman9a725dd2017-09-20 09:53:22 -040050 dstIsClamped, &overrideColor);
Brian Salomon82ddc942017-07-14 12:00:13 -040051 return RequiresDstTexture::kNo;
Brian Salomon649a3412017-03-09 13:50:43 -050052 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050053
54private:
Robert Phillips7c525e62018-06-12 10:11:12 -040055 friend class ::GrOpMemoryPool; // for ctor
56
Brian Salomonaff329b2017-08-11 09:40:37 -040057 TestOp(std::unique_ptr<GrFragmentProcessor> fp)
58 : INHERITED(ClassID()), fProcessors(std::move(fp)) {
Brian Salomon82ddc942017-07-14 12:00:13 -040059 this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsZeroArea::kNo);
60 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050061
Brian Salomon91326c32017-08-09 16:02:19 -040062 void onPrepareDraws(Target* target) override { return; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050063
Brian Salomon82ddc942017-07-14 12:00:13 -040064 bool onCombineIfPossible(GrOp* op, const GrCaps& caps) override { return false; }
65
66 GrProcessorSet fProcessors;
67
68 typedef GrMeshDrawOp INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050069};
70
71/**
72 * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts
73 * of resources owned by child FPs.
74 */
75class TestFP : public GrFragmentProcessor {
76public:
Brian Salomonaff329b2017-08-11 09:40:37 -040077 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) {
78 return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child)));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050079 }
Brian Salomonaff329b2017-08-11 09:40:37 -040080 static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Brian Salomon559f5562017-11-15 14:28:33 -050081 const SkTArray<sk_sp<GrBuffer>>& buffers) {
82 return std::unique_ptr<GrFragmentProcessor>(new TestFP(proxies, buffers));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050083 }
84
85 const char* name() const override { return "test"; }
86
87 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
88 // We don't really care about reusing these.
89 static int32_t gKey = 0;
90 b->add32(sk_atomic_inc(&gKey));
91 }
92
Brian Salomonaff329b2017-08-11 09:40:37 -040093 std::unique_ptr<GrFragmentProcessor> clone() const override {
94 return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this));
Brian Salomonb17e6392017-07-28 13:41:51 -040095 }
96
Brian Salomonbc6b99d2017-01-11 10:32:34 -050097private:
Brian Salomon559f5562017-11-15 14:28:33 -050098 TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies, const SkTArray<sk_sp<GrBuffer>>& buffers)
99 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4), fBuffers(4) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500100 for (const auto& proxy : proxies) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400101 this->addTextureSampler(&fSamplers.emplace_back(proxy));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500102 }
103 for (const auto& buffer : buffers) {
104 this->addBufferAccess(&fBuffers.emplace_back(kRGBA_8888_GrPixelConfig, buffer.get()));
105 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500106 }
107
Brian Salomonaff329b2017-08-11 09:40:37 -0400108 TestFP(std::unique_ptr<GrFragmentProcessor> child)
Brian Salomon559f5562017-11-15 14:28:33 -0500109 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4), fBuffers(4) {
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500110 this->registerChildProcessor(std::move(child));
111 }
112
Brian Salomon96271cd2017-07-31 16:27:23 -0400113 explicit TestFP(const TestFP& that)
Brian Salomon559f5562017-11-15 14:28:33 -0500114 : INHERITED(kTestFP_ClassID, that.optimizationFlags()), fSamplers(4), fBuffers(4) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400115 for (int i = 0; i < that.fSamplers.count(); ++i) {
116 fSamplers.emplace_back(that.fSamplers[i]);
117 this->addTextureSampler(&fSamplers.back());
118 }
119 for (int i = 0; i < that.fBuffers.count(); ++i) {
120 fBuffers.emplace_back(that.fBuffers[i]);
121 this->addBufferAccess(&fBuffers.back());
122 }
Brian Salomon96271cd2017-07-31 16:27:23 -0400123 for (int i = 0; i < that.numChildProcessors(); ++i) {
124 this->registerChildProcessor(that.childProcessor(i).clone());
Brian Salomonb17e6392017-07-28 13:41:51 -0400125 }
126 }
127
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500128 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
129 class TestGLSLFP : public GrGLSLFragmentProcessor {
130 public:
131 TestGLSLFP() {}
132 void emitCode(EmitArgs& args) override {
133 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
134 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
135 }
136
137 private:
138 };
139 return new TestGLSLFP();
140 }
141
142 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
143
144 GrTAllocator<TextureSampler> fSamplers;
145 GrTAllocator<BufferAccess> fBuffers;
Brian Salomon587e08f2017-01-27 10:59:27 -0500146 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500147};
148}
149
Brian Salomonf046e152017-01-11 15:24:47 -0500150template <typename T>
151inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) {
152 *refCnt = resource->fRefCnt;
153 *readCnt = resource->fPendingReads;
154 *writeCnt = resource->fPendingWrites;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500155}
156
Robert Phillips2f493142017-03-02 18:18:38 -0500157void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500158 *refCnt = proxy->getBackingRefCnt_TestOnly();
159 *readCnt = proxy->getPendingReadCnt_TestOnly();
160 *writeCnt = proxy->getPendingWriteCnt_TestOnly();
161}
162
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500163DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
164 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500165 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500166 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500167
Robert Phillips16d8ec62017-07-27 16:16:25 -0400168 GrSurfaceDesc desc;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500169 desc.fWidth = 10;
170 desc.fHeight = 10;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400171 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500172
Brian Salomonb17e6392017-07-28 13:41:51 -0400173 for (bool makeClone : {false, true}) {
174 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
175 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500176 context->contextPriv().makeDeferredRenderTargetContext(
177 SkBackingFit::kApprox, 1, 1,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500178 kRGBA_8888_GrPixelConfig, nullptr));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500179 {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400180 bool texelBufferSupport =
181 context->contextPriv().caps()->shaderCaps()->texelBufferSupport();
Brian Salomon2a4f9832018-03-03 22:43:43 -0500182 sk_sp<GrTextureProxy> proxy1 = proxyProvider->createProxy(
183 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
184 sk_sp<GrTextureProxy> proxy2 = proxyProvider->createProxy(
185 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
186 sk_sp<GrTextureProxy> proxy3 = proxyProvider->createProxy(
187 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
188 sk_sp<GrTextureProxy> proxy4 = proxyProvider->createProxy(
189 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
Brian Salomonb17e6392017-07-28 13:41:51 -0400190 sk_sp<GrBuffer> buffer(texelBufferSupport
Robert Phillips6be756b2018-01-16 15:07:54 -0500191 ? resourceProvider->createBuffer(
Brian Salomonb17e6392017-07-28 13:41:51 -0400192 1024, GrBufferType::kTexel_GrBufferType,
193 GrAccessPattern::kStatic_GrAccessPattern, 0)
194 : nullptr);
195 {
196 SkTArray<sk_sp<GrTextureProxy>> proxies;
197 SkTArray<sk_sp<GrBuffer>> buffers;
Brian Salomonb17e6392017-07-28 13:41:51 -0400198 proxies.push_back(proxy1);
199 if (texelBufferSupport) {
200 buffers.push_back(buffer);
201 }
Brian Salomon559f5562017-11-15 14:28:33 -0500202 auto fp = TestFP::Make(std::move(proxies), std::move(buffers));
Brian Salomonb17e6392017-07-28 13:41:51 -0400203 for (int i = 0; i < parentCnt; ++i) {
204 fp = TestFP::Make(std::move(fp));
205 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400206 std::unique_ptr<GrFragmentProcessor> clone;
Brian Salomonb17e6392017-07-28 13:41:51 -0400207 if (makeClone) {
208 clone = fp->clone();
209 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400210 std::unique_ptr<GrDrawOp> op(TestOp::Make(context, std::move(fp)));
Brian Salomonb17e6392017-07-28 13:41:51 -0400211 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
212 if (clone) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400213 op = TestOp::Make(context, std::move(clone));
Brian Salomonb17e6392017-07-28 13:41:51 -0400214 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
215 }
216 }
217 int refCnt, readCnt, writeCnt;
218
219 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
220 // IO counts should be double if there is a clone of the FP.
221 int ioRefMul = makeClone ? 2 : 1;
222 REPORTER_ASSERT(reporter, 1 == refCnt);
223 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
224 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
225
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500226 if (texelBufferSupport) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400227 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
228 REPORTER_ASSERT(reporter, 1 == refCnt);
229 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
230 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500231 }
Brian Salomonb17e6392017-07-28 13:41:51 -0400232
Brian Salomonb17e6392017-07-28 13:41:51 -0400233 context->flush();
234
235 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
236 REPORTER_ASSERT(reporter, 1 == refCnt);
237 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
238 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
239
240 if (texelBufferSupport) {
241 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
242 REPORTER_ASSERT(reporter, 1 == refCnt);
243 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
244 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500245 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500246
Brian Salomonb17e6392017-07-28 13:41:51 -0400247 if (texelBufferSupport) {
248 testingOnly_getIORefCnts(proxy2.get(), &refCnt, &readCnt, &writeCnt);
249 REPORTER_ASSERT(reporter, 1 == refCnt);
250 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
251 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500252
Brian Salomonb17e6392017-07-28 13:41:51 -0400253 testingOnly_getIORefCnts(proxy3.get(), &refCnt, &readCnt, &writeCnt);
254 REPORTER_ASSERT(reporter, 1 == refCnt);
255 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
256 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500257
Brian Salomonb17e6392017-07-28 13:41:51 -0400258 testingOnly_getIORefCnts(proxy4.get(), &refCnt, &readCnt, &writeCnt);
259 REPORTER_ASSERT(reporter, 1 == refCnt);
260 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
261 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
262 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500263 }
264 }
265 }
266}
Brian Salomon587e08f2017-01-27 10:59:27 -0500267
268// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
269#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
270
Brian Salomon0e05a822017-07-25 09:43:22 -0400271#include "SkCommandLineFlags.h"
272DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?");
273
274#if GR_TEST_UTILS
275
276static GrColor input_texel_color(int i, int j) {
277 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 -0500278 return GrPremulColor(color);
279}
280
Brian Salomon0e05a822017-07-25 09:43:22 -0400281static GrColor4f input_texel_color4f(int i, int j) {
282 return GrColor4f::FromGrColor(input_texel_color(i, j));
283}
Brian Salomon587e08f2017-01-27 10:59:27 -0500284
Robert Phillips7c525e62018-06-12 10:11:12 -0400285void test_draw_op(GrContext* context,
286 GrRenderTargetContext* rtc,
287 std::unique_ptr<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500288 sk_sp<GrTextureProxy> inputDataProxy) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500289 GrPaint paint;
Brian Osman2240be92017-10-18 13:15:13 -0400290 paint.addColorTextureProcessor(std::move(inputDataProxy), SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500291 paint.addColorFragmentProcessor(std::move(fp));
292 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400293
Robert Phillips7c525e62018-06-12 10:11:12 -0400294 auto op = GrRectOpFactory::MakeNonAAFill(context, std::move(paint), SkMatrix::I(),
Brian Salomonbaaf4392017-06-15 09:59:23 -0400295 SkRect::MakeWH(rtc->width(), rtc->height()),
296 GrAAType::kNone);
Brian Salomonac70f842017-05-08 10:43:33 -0400297 rtc->addDrawOp(GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500298}
299
Brian Salomon0e05a822017-07-25 09:43:22 -0400300/** Initializes the two test texture proxies that are available to the FP test factories. */
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500301bool init_test_textures(GrProxyProvider* proxyProvider, SkRandom* random,
302 sk_sp<GrTextureProxy> proxies[2]) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400303 static const int kTestTextureSize = 256;
304 GrSurfaceDesc desc;
Brian Salomon0e05a822017-07-25 09:43:22 -0400305 desc.fWidth = kTestTextureSize;
306 desc.fHeight = kTestTextureSize;
307 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400308
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500309 {
310 // Put premul data into the RGBA texture that the test FPs can optionally use.
311 std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]);
312 for (int y = 0; y < kTestTextureSize; ++y) {
313 for (int x = 0; x < kTestTextureSize; ++x) {
314 rgbaData[kTestTextureSize * y + x] =
315 input_texel_color(random->nextULessThan(256), random->nextULessThan(256));
316 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400317 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400318
Brian Salomon58389b92018-03-07 13:01:25 -0500319 proxies[0] = proxyProvider->createTextureProxy(desc, SkBudgeted::kYes, rgbaData.get(),
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500320 kTestTextureSize * sizeof(GrColor));
Brian Salomon0e05a822017-07-25 09:43:22 -0400321 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500322
323 {
324 // Put random values into the alpha texture that the test FPs can optionally use.
325 desc.fConfig = kAlpha_8_GrPixelConfig;
326 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]);
327 for (int y = 0; y < kTestTextureSize; ++y) {
328 for (int x = 0; x < kTestTextureSize; ++x) {
329 alphaData[kTestTextureSize * y + x] = random->nextULessThan(256);
330 }
331 }
332
Brian Salomon58389b92018-03-07 13:01:25 -0500333 proxies[1] = proxyProvider->createTextureProxy(desc, SkBudgeted::kYes, alphaData.get(),
Brian Salomon2a4f9832018-03-03 22:43:43 -0500334 kTestTextureSize);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500335 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400336
337 return proxies[0] && proxies[1];
338}
339
340// Creates a texture of premul colors used as the output of the fragment processor that precedes
341// the fragment processor under test. Color values are those provided by input_texel_color().
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500342sk_sp<GrTextureProxy> make_input_texture(GrProxyProvider* proxyProvider, int width, int height) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400343 std::unique_ptr<GrColor[]> data(new GrColor[width * height]);
344 for (int y = 0; y < width; ++y) {
345 for (int x = 0; x < height; ++x) {
346 data.get()[width * y + x] = input_texel_color(x, y);
347 }
348 }
349 GrSurfaceDesc desc;
Brian Salomon0e05a822017-07-25 09:43:22 -0400350 desc.fWidth = width;
351 desc.fHeight = height;
352 desc.fConfig = kRGBA_8888_GrPixelConfig;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500353
Brian Salomon58389b92018-03-07 13:01:25 -0500354 return proxyProvider->createTextureProxy(desc, SkBudgeted::kYes, data.get(),
355 width * sizeof(GrColor));
Brian Salomon0e05a822017-07-25 09:43:22 -0400356}
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500357
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500358DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500359 GrContext* context = ctxInfo.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500360 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500361 auto resourceProvider = context->contextPriv().resourceProvider();
Brian Salomon1c053642017-07-24 10:16:19 -0400362 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400363
364 uint32_t seed = 0;
365 if (FLAGS_randomProcessorTest) {
366 std::random_device rd;
367 seed = rd();
368 }
369 // If a non-deterministic bot fails this test, check the output to see what seed it used, then
370 // hard-code that value here:
371 SkRandom random(seed);
372
Brian Salomon0e05a822017-07-25 09:43:22 -0400373 // Make the destination context for the test.
374 static constexpr int kRenderSize = 256;
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500375 sk_sp<GrRenderTargetContext> rtc = context->contextPriv().makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400376 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500377
Robert Phillipse78b7252017-04-06 07:59:41 -0400378 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500379 if (!init_test_textures(proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400380 ERRORF(reporter, "Could not create test textures");
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400381 return;
382 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400383 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500384
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500385 auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500386
Ben Wagner8c791942017-07-25 11:47:48 -0400387 std::unique_ptr<GrColor[]> readData(new GrColor[kRenderSize * kRenderSize]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400388 // Because processor factories configure themselves in random ways, this is not exhaustive.
Brian Salomon587e08f2017-01-27 10:59:27 -0500389 for (int i = 0; i < FPFactory::Count(); ++i) {
390 int timesToInvokeFactory = 5;
391 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
392 // on child optimizations being present.
Brian Salomonaff329b2017-08-11 09:40:37 -0400393 std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
Brian Salomon587e08f2017-01-27 10:59:27 -0500394 for (int j = 0; j < fp->numChildProcessors(); ++j) {
395 // This value made a reasonable trade off between time and coverage when this test was
396 // written.
397 timesToInvokeFactory *= FPFactory::Count() / 2;
398 }
399 for (int j = 0; j < timesToInvokeFactory; ++j) {
400 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillips6be756b2018-01-16 15:07:54 -0500401 if (!fp->instantiate(resourceProvider)) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400402 continue;
403 }
404
Brian Salomon587e08f2017-01-27 10:59:27 -0500405 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500406 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500407 continue;
408 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400409
410 // Since we transfer away ownership of the original FP, we make a clone.
411 auto clone = fp->clone();
412
Robert Phillips7c525e62018-06-12 10:11:12 -0400413 test_draw_op(context, rtc.get(), std::move(fp), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400414 memset(readData.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
415 rtc->readPixels(SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
416 kPremul_SkAlphaType),
417 readData.get(), 0, 0, 0);
Brian Salomon587e08f2017-01-27 10:59:27 -0500418 bool passing = true;
419 if (0) { // Useful to see what FPs are being tested.
420 SkString children;
Brian Salomonaff329b2017-08-11 09:40:37 -0400421 for (int c = 0; c < clone->numChildProcessors(); ++c) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500422 if (!c) {
423 children.append("(");
424 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400425 children.append(clone->name());
426 children.append(c == clone->numChildProcessors() - 1 ? ")" : ", ");
Brian Salomon587e08f2017-01-27 10:59:27 -0500427 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400428 SkDebugf("%s %s\n", clone->name(), children.c_str());
Brian Salomon587e08f2017-01-27 10:59:27 -0500429 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400430 for (int y = 0; y < kRenderSize && passing; ++y) {
431 for (int x = 0; x < kRenderSize && passing; ++x) {
432 GrColor input = input_texel_color(x, y);
433 GrColor output = readData.get()[y * kRenderSize + x];
Brian Salomonaff329b2017-08-11 09:40:37 -0400434 if (clone->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500435 // A modulating processor is allowed to modulate either the input color or
436 // just the input alpha.
437 bool legalColorModulation =
438 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
439 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
440 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
441 GrColorUnpackB(output) <= GrColorUnpackB(input);
442 bool legalAlphaModulation =
443 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
444 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
445 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
446 GrColorUnpackB(output) <= GrColorUnpackA(input);
447 if (!legalColorModulation && !legalAlphaModulation) {
448 ERRORF(reporter,
449 "\"Modulating\" processor %s made color/alpha value larger. "
Greg Daniel10ed2432017-12-01 16:19:43 -0500450 "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).",
451 clone->name(), input, output, x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500452 passing = false;
453 }
454 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400455 GrColor4f input4f = input_texel_color4f(x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500456 GrColor4f output4f = GrColor4f::FromGrColor(output);
457 GrColor4f expected4f;
Brian Salomonaff329b2017-08-11 09:40:37 -0400458 if (clone->hasConstantOutputForConstantInput(input4f, &expected4f)) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500459 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
460 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
461 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
462 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500463 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500464 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
465 ERRORF(reporter,
466 "Processor %s claimed output for const input doesn't match "
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500467 "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, "
468 "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)",
Brian Salomonf61711a2018-02-16 10:44:28 -0500469 clone->name(),
470 SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))), kTol,
471 input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2],
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500472 input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1],
473 output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0],
474 expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]);
Brian Salomon587e08f2017-01-27 10:59:27 -0500475 passing = false;
476 }
477 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400478 if (GrColorIsOpaque(input) && clone->preservesOpaqueInput() &&
Brian Salomon587e08f2017-01-27 10:59:27 -0500479 !GrColorIsOpaque(output)) {
480 ERRORF(reporter,
481 "Processor %s claimed opaqueness is preserved but it is not. Input: "
Brian Osman2f3865b2017-03-15 13:35:51 -0400482 "0x%08x, Output: 0x%08x.",
Brian Salomonaff329b2017-08-11 09:40:37 -0400483 clone->name(), input, output);
Brian Salomon587e08f2017-01-27 10:59:27 -0500484 passing = false;
485 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400486 if (!passing) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400487 ERRORF(reporter, "Seed: 0x%08x, Processor details: %s", seed,
488 clone->dumpInfo().c_str());
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400489 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500490 }
491 }
492 }
493 }
494}
Robert Phillips18166ee2017-06-01 12:55:44 -0400495
Brian Salomon0e05a822017-07-25 09:43:22 -0400496// Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their
497// progenitors.
498DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
499 GrContext* context = ctxInfo.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500500 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500501 auto resourceProvider = context->contextPriv().resourceProvider();
Brian Salomon0e05a822017-07-25 09:43:22 -0400502
503 SkRandom random;
504
505 // Make the destination context for the test.
506 static constexpr int kRenderSize = 1024;
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500507 sk_sp<GrRenderTargetContext> rtc = context->contextPriv().makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400508 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
509
510 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500511 if (!init_test_textures(proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400512 ERRORF(reporter, "Could not create test textures");
513 return;
514 }
515 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
516
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500517 auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize);
Brian Salomon0e05a822017-07-25 09:43:22 -0400518 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
519 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
520 auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
521 kPremul_SkAlphaType);
522
523 // Because processor factories configure themselves in random ways, this is not exhaustive.
524 for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
525 static constexpr int kTimesToInvokeFactory = 10;
526 for (int j = 0; j < kTimesToInvokeFactory; ++j) {
527 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
528 auto clone = fp->clone();
Brian Salomon0e05a822017-07-25 09:43:22 -0400529 if (!clone) {
Brian Salomon96271cd2017-07-31 16:27:23 -0400530 ERRORF(reporter, "Clone of processor %s failed.", fp->name());
Brian Salomon0e05a822017-07-25 09:43:22 -0400531 continue;
532 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400533 const char* name = fp->name();
Robert Phillips6be756b2018-01-16 15:07:54 -0500534 if (!fp->instantiate(resourceProvider) || !clone->instantiate(resourceProvider)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400535 continue;
536 }
Brian Salomonce06e262017-08-01 16:23:40 -0400537 REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name()));
538 REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() ==
539 clone->compatibleWithCoverageAsAlpha());
540 REPORTER_ASSERT(reporter, fp->isEqual(*clone));
541 REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput());
542 REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() ==
543 clone->hasConstantOutputForConstantInput());
544 REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors());
545 REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords());
Brian Salomon0e05a822017-07-25 09:43:22 -0400546 // Draw with original and read back the results.
Robert Phillips7c525e62018-06-12 10:11:12 -0400547 test_draw_op(context, rtc.get(), std::move(fp), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400548 memset(readData1.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
549 rtc->readPixels(readInfo, readData1.get(), 0, 0, 0);
550
551 // Draw with clone and read back the results.
Robert Phillips7c525e62018-06-12 10:11:12 -0400552 test_draw_op(context, rtc.get(), std::move(clone), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400553 memset(readData2.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
554 rtc->readPixels(readInfo, readData2.get(), 0, 0, 0);
555
556 // Check that the results are the same.
557 bool passing = true;
558 for (int y = 0; y < kRenderSize && passing; ++y) {
559 for (int x = 0; x < kRenderSize && passing; ++x) {
560 int idx = y * kRenderSize + x;
561 if (readData1[idx] != readData2[idx]) {
562 ERRORF(reporter,
563 "Processor %s made clone produced different output. "
564 "Input color: 0x%08x, Original Output Color: 0x%08x, "
565 "Clone Output Color: 0x%08x..",
Brian Salomonaff329b2017-08-11 09:40:37 -0400566 name, input_texel_color(x, y), readData1[idx], readData2[idx]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400567 passing = false;
568 }
569 }
570 }
571 }
572 }
573}
574
Hal Canary6f6961e2017-01-31 13:50:44 -0500575#endif // GR_TEST_UTILS
576#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS