blob: 3916436cfc8996226efbb2915dc9cc4d93a7859e [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) {
Brian Salomon82ddc942017-07-14 12:00:13 -040032 return std::unique_ptr<GrDrawOp>(new TestOp(std::move(fp)));
33 }
34
Robert Phillips5f567c72017-09-14 08:27:37 -040035 const char* name() const override { return "TestOp"; }
36
Robert Phillipsf1748f52017-09-14 14:11:24 -040037 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips5f567c72017-09-14 08:27:37 -040038 fProcessors.visitProxies(func);
39 }
40
Brian Salomon82ddc942017-07-14 12:00:13 -040041 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
42
Brian Osman9a725dd2017-09-20 09:53:22 -040043 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
44 GrPixelConfigIsClamped dstIsClamped) override {
Brian Salomon82ddc942017-07-14 12:00:13 -040045 static constexpr GrProcessorAnalysisColor kUnknownColor;
46 GrColor overrideColor;
47 fProcessors.finalize(kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, false, caps,
Brian Osman9a725dd2017-09-20 09:53:22 -040048 dstIsClamped, &overrideColor);
Brian Salomon82ddc942017-07-14 12:00:13 -040049 return RequiresDstTexture::kNo;
Brian Salomon649a3412017-03-09 13:50:43 -050050 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050051
52private:
Robert Phillips7c525e62018-06-12 10:11:12 -040053 friend class ::GrOpMemoryPool; // for ctor
54
Brian Salomonaff329b2017-08-11 09:40:37 -040055 TestOp(std::unique_ptr<GrFragmentProcessor> fp)
56 : INHERITED(ClassID()), fProcessors(std::move(fp)) {
Brian Salomon82ddc942017-07-14 12:00:13 -040057 this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsZeroArea::kNo);
58 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050059
Brian Salomon91326c32017-08-09 16:02:19 -040060 void onPrepareDraws(Target* target) override { return; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050061
Brian Salomon82ddc942017-07-14 12:00:13 -040062 bool onCombineIfPossible(GrOp* op, const GrCaps& caps) override { return false; }
63
64 GrProcessorSet fProcessors;
65
66 typedef GrMeshDrawOp INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050067};
68
69/**
70 * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts
71 * of resources owned by child FPs.
72 */
73class TestFP : public GrFragmentProcessor {
74public:
Brian Salomonaff329b2017-08-11 09:40:37 -040075 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) {
76 return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child)));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050077 }
Brian Salomonaff329b2017-08-11 09:40:37 -040078 static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Brian Salomon559f5562017-11-15 14:28:33 -050079 const SkTArray<sk_sp<GrBuffer>>& buffers) {
80 return std::unique_ptr<GrFragmentProcessor>(new TestFP(proxies, buffers));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050081 }
82
83 const char* name() const override { return "test"; }
84
85 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
86 // We don't really care about reusing these.
87 static int32_t gKey = 0;
88 b->add32(sk_atomic_inc(&gKey));
89 }
90
Brian Salomonaff329b2017-08-11 09:40:37 -040091 std::unique_ptr<GrFragmentProcessor> clone() const override {
92 return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this));
Brian Salomonb17e6392017-07-28 13:41:51 -040093 }
94
Brian Salomonbc6b99d2017-01-11 10:32:34 -050095private:
Brian Salomon559f5562017-11-15 14:28:33 -050096 TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies, const SkTArray<sk_sp<GrBuffer>>& buffers)
97 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4), fBuffers(4) {
Robert Phillips30f9bc62017-02-22 15:28:38 -050098 for (const auto& proxy : proxies) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040099 this->addTextureSampler(&fSamplers.emplace_back(proxy));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500100 }
101 for (const auto& buffer : buffers) {
102 this->addBufferAccess(&fBuffers.emplace_back(kRGBA_8888_GrPixelConfig, buffer.get()));
103 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500104 }
105
Brian Salomonaff329b2017-08-11 09:40:37 -0400106 TestFP(std::unique_ptr<GrFragmentProcessor> child)
Brian Salomon559f5562017-11-15 14:28:33 -0500107 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4), fBuffers(4) {
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500108 this->registerChildProcessor(std::move(child));
109 }
110
Brian Salomon96271cd2017-07-31 16:27:23 -0400111 explicit TestFP(const TestFP& that)
Brian Salomon559f5562017-11-15 14:28:33 -0500112 : INHERITED(kTestFP_ClassID, that.optimizationFlags()), fSamplers(4), fBuffers(4) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400113 for (int i = 0; i < that.fSamplers.count(); ++i) {
114 fSamplers.emplace_back(that.fSamplers[i]);
115 this->addTextureSampler(&fSamplers.back());
116 }
117 for (int i = 0; i < that.fBuffers.count(); ++i) {
118 fBuffers.emplace_back(that.fBuffers[i]);
119 this->addBufferAccess(&fBuffers.back());
120 }
Brian Salomon96271cd2017-07-31 16:27:23 -0400121 for (int i = 0; i < that.numChildProcessors(); ++i) {
122 this->registerChildProcessor(that.childProcessor(i).clone());
Brian Salomonb17e6392017-07-28 13:41:51 -0400123 }
124 }
125
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500126 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
127 class TestGLSLFP : public GrGLSLFragmentProcessor {
128 public:
129 TestGLSLFP() {}
130 void emitCode(EmitArgs& args) override {
131 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
132 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
133 }
134
135 private:
136 };
137 return new TestGLSLFP();
138 }
139
140 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
141
142 GrTAllocator<TextureSampler> fSamplers;
143 GrTAllocator<BufferAccess> fBuffers;
Brian Salomon587e08f2017-01-27 10:59:27 -0500144 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500145};
146}
147
Brian Salomonf046e152017-01-11 15:24:47 -0500148template <typename T>
149inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) {
150 *refCnt = resource->fRefCnt;
151 *readCnt = resource->fPendingReads;
152 *writeCnt = resource->fPendingWrites;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500153}
154
Robert Phillips2f493142017-03-02 18:18:38 -0500155void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500156 *refCnt = proxy->getBackingRefCnt_TestOnly();
157 *readCnt = proxy->getPendingReadCnt_TestOnly();
158 *writeCnt = proxy->getPendingWriteCnt_TestOnly();
159}
160
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500161DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
162 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500163 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500164 GrResourceProvider* resourceProvider = context->contextPriv().resourceProvider();
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500165
Robert Phillips16d8ec62017-07-27 16:16:25 -0400166 GrSurfaceDesc desc;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500167 desc.fWidth = 10;
168 desc.fHeight = 10;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400169 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500170
Brian Salomonb17e6392017-07-28 13:41:51 -0400171 for (bool makeClone : {false, true}) {
172 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
173 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500174 context->contextPriv().makeDeferredRenderTargetContext(
175 SkBackingFit::kApprox, 1, 1,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500176 kRGBA_8888_GrPixelConfig, nullptr));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500177 {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400178 bool texelBufferSupport =
179 context->contextPriv().caps()->shaderCaps()->texelBufferSupport();
Brian Salomon2a4f9832018-03-03 22:43:43 -0500180 sk_sp<GrTextureProxy> proxy1 = proxyProvider->createProxy(
181 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
182 sk_sp<GrTextureProxy> proxy2 = proxyProvider->createProxy(
183 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
184 sk_sp<GrTextureProxy> proxy3 = proxyProvider->createProxy(
185 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
186 sk_sp<GrTextureProxy> proxy4 = proxyProvider->createProxy(
187 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
Brian Salomonb17e6392017-07-28 13:41:51 -0400188 sk_sp<GrBuffer> buffer(texelBufferSupport
Robert Phillips6be756b2018-01-16 15:07:54 -0500189 ? resourceProvider->createBuffer(
Brian Salomonb17e6392017-07-28 13:41:51 -0400190 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 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400208 std::unique_ptr<GrDrawOp> op(TestOp::Make(context, std::move(fp)));
Brian Salomonb17e6392017-07-28 13:41:51 -0400209 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
210 if (clone) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400211 op = TestOp::Make(context, std::move(clone));
Brian Salomonb17e6392017-07-28 13:41:51 -0400212 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
Robert Phillips7c525e62018-06-12 10:11:12 -0400283void test_draw_op(GrContext* context,
284 GrRenderTargetContext* rtc,
285 std::unique_ptr<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500286 sk_sp<GrTextureProxy> inputDataProxy) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500287 GrPaint paint;
Brian Osman2240be92017-10-18 13:15:13 -0400288 paint.addColorTextureProcessor(std::move(inputDataProxy), SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500289 paint.addColorFragmentProcessor(std::move(fp));
290 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400291
Robert Phillips7c525e62018-06-12 10:11:12 -0400292 auto op = GrRectOpFactory::MakeNonAAFill(context, std::move(paint), SkMatrix::I(),
Brian Salomonbaaf4392017-06-15 09:59:23 -0400293 SkRect::MakeWH(rtc->width(), rtc->height()),
294 GrAAType::kNone);
Brian Salomonac70f842017-05-08 10:43:33 -0400295 rtc->addDrawOp(GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500296}
297
Brian Salomon0e05a822017-07-25 09:43:22 -0400298/** Initializes the two test texture proxies that are available to the FP test factories. */
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500299bool init_test_textures(GrProxyProvider* proxyProvider, SkRandom* random,
300 sk_sp<GrTextureProxy> proxies[2]) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400301 static const int kTestTextureSize = 256;
302 GrSurfaceDesc desc;
Brian Salomon0e05a822017-07-25 09:43:22 -0400303 desc.fWidth = kTestTextureSize;
304 desc.fHeight = kTestTextureSize;
305 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400306
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500307 {
308 // Put premul data into the RGBA texture that the test FPs can optionally use.
309 std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]);
310 for (int y = 0; y < kTestTextureSize; ++y) {
311 for (int x = 0; x < kTestTextureSize; ++x) {
312 rgbaData[kTestTextureSize * y + x] =
313 input_texel_color(random->nextULessThan(256), random->nextULessThan(256));
314 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400315 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400316
Brian Salomon58389b92018-03-07 13:01:25 -0500317 proxies[0] = proxyProvider->createTextureProxy(desc, SkBudgeted::kYes, rgbaData.get(),
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500318 kTestTextureSize * sizeof(GrColor));
Brian Salomon0e05a822017-07-25 09:43:22 -0400319 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500320
321 {
322 // Put random values into the alpha texture that the test FPs can optionally use.
323 desc.fConfig = kAlpha_8_GrPixelConfig;
324 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]);
325 for (int y = 0; y < kTestTextureSize; ++y) {
326 for (int x = 0; x < kTestTextureSize; ++x) {
327 alphaData[kTestTextureSize * y + x] = random->nextULessThan(256);
328 }
329 }
330
Brian Salomon58389b92018-03-07 13:01:25 -0500331 proxies[1] = proxyProvider->createTextureProxy(desc, SkBudgeted::kYes, alphaData.get(),
Brian Salomon2a4f9832018-03-03 22:43:43 -0500332 kTestTextureSize);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500333 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400334
335 return proxies[0] && proxies[1];
336}
337
338// Creates a texture of premul colors used as the output of the fragment processor that precedes
339// the fragment processor under test. Color values are those provided by input_texel_color().
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500340sk_sp<GrTextureProxy> make_input_texture(GrProxyProvider* proxyProvider, int width, int height) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400341 std::unique_ptr<GrColor[]> data(new GrColor[width * height]);
342 for (int y = 0; y < width; ++y) {
343 for (int x = 0; x < height; ++x) {
344 data.get()[width * y + x] = input_texel_color(x, y);
345 }
346 }
347 GrSurfaceDesc desc;
Brian Salomon0e05a822017-07-25 09:43:22 -0400348 desc.fWidth = width;
349 desc.fHeight = height;
350 desc.fConfig = kRGBA_8888_GrPixelConfig;
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500351
Brian Salomon58389b92018-03-07 13:01:25 -0500352 return proxyProvider->createTextureProxy(desc, SkBudgeted::kYes, data.get(),
353 width * sizeof(GrColor));
Brian Salomon0e05a822017-07-25 09:43:22 -0400354}
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500355
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500356DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500357 GrContext* context = ctxInfo.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500358 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500359 auto resourceProvider = context->contextPriv().resourceProvider();
Brian Salomon1c053642017-07-24 10:16:19 -0400360 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400361
362 uint32_t seed = 0;
363 if (FLAGS_randomProcessorTest) {
364 std::random_device rd;
365 seed = rd();
366 }
367 // If a non-deterministic bot fails this test, check the output to see what seed it used, then
368 // hard-code that value here:
369 SkRandom random(seed);
370
Brian Salomon0e05a822017-07-25 09:43:22 -0400371 // Make the destination context for the test.
372 static constexpr int kRenderSize = 256;
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500373 sk_sp<GrRenderTargetContext> rtc = context->contextPriv().makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400374 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500375
Robert Phillipse78b7252017-04-06 07:59:41 -0400376 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500377 if (!init_test_textures(proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400378 ERRORF(reporter, "Could not create test textures");
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400379 return;
380 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400381 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500382
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500383 auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500384
Ben Wagner8c791942017-07-25 11:47:48 -0400385 std::unique_ptr<GrColor[]> readData(new GrColor[kRenderSize * kRenderSize]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400386 // Because processor factories configure themselves in random ways, this is not exhaustive.
Brian Salomon587e08f2017-01-27 10:59:27 -0500387 for (int i = 0; i < FPFactory::Count(); ++i) {
388 int timesToInvokeFactory = 5;
389 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
390 // on child optimizations being present.
Brian Salomonaff329b2017-08-11 09:40:37 -0400391 std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
Brian Salomon587e08f2017-01-27 10:59:27 -0500392 for (int j = 0; j < fp->numChildProcessors(); ++j) {
393 // This value made a reasonable trade off between time and coverage when this test was
394 // written.
395 timesToInvokeFactory *= FPFactory::Count() / 2;
396 }
397 for (int j = 0; j < timesToInvokeFactory; ++j) {
398 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillips6be756b2018-01-16 15:07:54 -0500399 if (!fp->instantiate(resourceProvider)) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400400 continue;
401 }
402
Brian Salomon587e08f2017-01-27 10:59:27 -0500403 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500404 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500405 continue;
406 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400407
408 // Since we transfer away ownership of the original FP, we make a clone.
409 auto clone = fp->clone();
410
Robert Phillips7c525e62018-06-12 10:11:12 -0400411 test_draw_op(context, rtc.get(), std::move(fp), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400412 memset(readData.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
413 rtc->readPixels(SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
414 kPremul_SkAlphaType),
415 readData.get(), 0, 0, 0);
Brian Salomon587e08f2017-01-27 10:59:27 -0500416 bool passing = true;
417 if (0) { // Useful to see what FPs are being tested.
418 SkString children;
Brian Salomonaff329b2017-08-11 09:40:37 -0400419 for (int c = 0; c < clone->numChildProcessors(); ++c) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500420 if (!c) {
421 children.append("(");
422 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400423 children.append(clone->name());
424 children.append(c == clone->numChildProcessors() - 1 ? ")" : ", ");
Brian Salomon587e08f2017-01-27 10:59:27 -0500425 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400426 SkDebugf("%s %s\n", clone->name(), children.c_str());
Brian Salomon587e08f2017-01-27 10:59:27 -0500427 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400428 for (int y = 0; y < kRenderSize && passing; ++y) {
429 for (int x = 0; x < kRenderSize && passing; ++x) {
430 GrColor input = input_texel_color(x, y);
431 GrColor output = readData.get()[y * kRenderSize + x];
Brian Salomonaff329b2017-08-11 09:40:37 -0400432 if (clone->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500433 // A modulating processor is allowed to modulate either the input color or
434 // just the input alpha.
435 bool legalColorModulation =
436 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
437 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
438 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
439 GrColorUnpackB(output) <= GrColorUnpackB(input);
440 bool legalAlphaModulation =
441 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
442 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
443 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
444 GrColorUnpackB(output) <= GrColorUnpackA(input);
445 if (!legalColorModulation && !legalAlphaModulation) {
446 ERRORF(reporter,
447 "\"Modulating\" processor %s made color/alpha value larger. "
Greg Daniel10ed2432017-12-01 16:19:43 -0500448 "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).",
449 clone->name(), input, output, x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500450 passing = false;
451 }
452 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400453 GrColor4f input4f = input_texel_color4f(x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500454 GrColor4f output4f = GrColor4f::FromGrColor(output);
455 GrColor4f expected4f;
Brian Salomonaff329b2017-08-11 09:40:37 -0400456 if (clone->hasConstantOutputForConstantInput(input4f, &expected4f)) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500457 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
458 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
459 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
460 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500461 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500462 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
463 ERRORF(reporter,
464 "Processor %s claimed output for const input doesn't match "
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500465 "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, "
466 "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)",
Brian Salomonf61711a2018-02-16 10:44:28 -0500467 clone->name(),
468 SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))), kTol,
469 input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2],
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500470 input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1],
471 output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0],
472 expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]);
Brian Salomon587e08f2017-01-27 10:59:27 -0500473 passing = false;
474 }
475 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400476 if (GrColorIsOpaque(input) && clone->preservesOpaqueInput() &&
Brian Salomon587e08f2017-01-27 10:59:27 -0500477 !GrColorIsOpaque(output)) {
478 ERRORF(reporter,
479 "Processor %s claimed opaqueness is preserved but it is not. Input: "
Brian Osman2f3865b2017-03-15 13:35:51 -0400480 "0x%08x, Output: 0x%08x.",
Brian Salomonaff329b2017-08-11 09:40:37 -0400481 clone->name(), input, output);
Brian Salomon587e08f2017-01-27 10:59:27 -0500482 passing = false;
483 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400484 if (!passing) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400485 ERRORF(reporter, "Seed: 0x%08x, Processor details: %s", seed,
486 clone->dumpInfo().c_str());
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400487 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500488 }
489 }
490 }
491 }
492}
Robert Phillips18166ee2017-06-01 12:55:44 -0400493
Brian Salomon0e05a822017-07-25 09:43:22 -0400494// Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their
495// progenitors.
496DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
497 GrContext* context = ctxInfo.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500498 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500499 auto resourceProvider = context->contextPriv().resourceProvider();
Brian Salomon0e05a822017-07-25 09:43:22 -0400500
501 SkRandom random;
502
503 // Make the destination context for the test.
504 static constexpr int kRenderSize = 1024;
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500505 sk_sp<GrRenderTargetContext> rtc = context->contextPriv().makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400506 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
507
508 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500509 if (!init_test_textures(proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400510 ERRORF(reporter, "Could not create test textures");
511 return;
512 }
513 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
514
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500515 auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize);
Brian Salomon0e05a822017-07-25 09:43:22 -0400516 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
517 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
518 auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
519 kPremul_SkAlphaType);
520
521 // Because processor factories configure themselves in random ways, this is not exhaustive.
522 for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
523 static constexpr int kTimesToInvokeFactory = 10;
524 for (int j = 0; j < kTimesToInvokeFactory; ++j) {
525 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
526 auto clone = fp->clone();
Brian Salomon0e05a822017-07-25 09:43:22 -0400527 if (!clone) {
Brian Salomon96271cd2017-07-31 16:27:23 -0400528 ERRORF(reporter, "Clone of processor %s failed.", fp->name());
Brian Salomon0e05a822017-07-25 09:43:22 -0400529 continue;
530 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400531 const char* name = fp->name();
Robert Phillips6be756b2018-01-16 15:07:54 -0500532 if (!fp->instantiate(resourceProvider) || !clone->instantiate(resourceProvider)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400533 continue;
534 }
Brian Salomonce06e262017-08-01 16:23:40 -0400535 REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name()));
536 REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() ==
537 clone->compatibleWithCoverageAsAlpha());
538 REPORTER_ASSERT(reporter, fp->isEqual(*clone));
539 REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput());
540 REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() ==
541 clone->hasConstantOutputForConstantInput());
542 REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors());
543 REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords());
Brian Salomon0e05a822017-07-25 09:43:22 -0400544 // Draw with original and read back the results.
Robert Phillips7c525e62018-06-12 10:11:12 -0400545 test_draw_op(context, rtc.get(), std::move(fp), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400546 memset(readData1.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
547 rtc->readPixels(readInfo, readData1.get(), 0, 0, 0);
548
549 // Draw with clone and read back the results.
Robert Phillips7c525e62018-06-12 10:11:12 -0400550 test_draw_op(context, rtc.get(), std::move(clone), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400551 memset(readData2.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
552 rtc->readPixels(readInfo, readData2.get(), 0, 0, 0);
553
554 // Check that the results are the same.
555 bool passing = true;
556 for (int y = 0; y < kRenderSize && passing; ++y) {
557 for (int x = 0; x < kRenderSize && passing; ++x) {
558 int idx = y * kRenderSize + x;
559 if (readData1[idx] != readData2[idx]) {
560 ERRORF(reporter,
561 "Processor %s made clone produced different output. "
562 "Input color: 0x%08x, Original Output Color: 0x%08x, "
563 "Clone Output Color: 0x%08x..",
Brian Salomonaff329b2017-08-11 09:40:37 -0400564 name, input_texel_color(x, y), readData1[idx], readData2[idx]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400565 passing = false;
566 }
567 }
568 }
569 }
570 }
571}
572
Hal Canary6f6961e2017-01-31 13:50:44 -0500573#endif // GR_TEST_UTILS
574#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS