blob: d6d3635025746306f9e67c19137c3e7b06591faf [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"
17#include "GrRenderTargetContext.h"
18#include "GrRenderTargetContextPriv.h"
19#include "GrResourceProvider.h"
20#include "glsl/GrGLSLFragmentProcessor.h"
21#include "glsl/GrGLSLFragmentShaderBuilder.h"
Brian Salomon82ddc942017-07-14 12:00:13 -040022#include "ops/GrMeshDrawOp.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040023#include "ops/GrRectOpFactory.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050024
25namespace {
Brian Salomon82ddc942017-07-14 12:00:13 -040026class TestOp : public GrMeshDrawOp {
Brian Salomonbc6b99d2017-01-11 10:32:34 -050027public:
28 DEFINE_OP_CLASS_ID
Brian Salomonaff329b2017-08-11 09:40:37 -040029 static std::unique_ptr<GrDrawOp> Make(std::unique_ptr<GrFragmentProcessor> fp) {
Brian Salomon82ddc942017-07-14 12:00:13 -040030 return std::unique_ptr<GrDrawOp>(new TestOp(std::move(fp)));
31 }
32
Robert Phillips5f567c72017-09-14 08:27:37 -040033 const char* name() const override { return "TestOp"; }
34
Robert Phillipsf1748f52017-09-14 14:11:24 -040035 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips5f567c72017-09-14 08:27:37 -040036 fProcessors.visitProxies(func);
37 }
38
Brian Salomon82ddc942017-07-14 12:00:13 -040039 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
40
Brian Osman9a725dd2017-09-20 09:53:22 -040041 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
42 GrPixelConfigIsClamped dstIsClamped) override {
Brian Salomon82ddc942017-07-14 12:00:13 -040043 static constexpr GrProcessorAnalysisColor kUnknownColor;
44 GrColor overrideColor;
45 fProcessors.finalize(kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, false, caps,
Brian Osman9a725dd2017-09-20 09:53:22 -040046 dstIsClamped, &overrideColor);
Brian Salomon82ddc942017-07-14 12:00:13 -040047 return RequiresDstTexture::kNo;
Brian Salomon649a3412017-03-09 13:50:43 -050048 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050049
50private:
Brian Salomonaff329b2017-08-11 09:40:37 -040051 TestOp(std::unique_ptr<GrFragmentProcessor> fp)
52 : INHERITED(ClassID()), fProcessors(std::move(fp)) {
Brian Salomon82ddc942017-07-14 12:00:13 -040053 this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsZeroArea::kNo);
54 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050055
Brian Salomon91326c32017-08-09 16:02:19 -040056 void onPrepareDraws(Target* target) override { return; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050057
Brian Salomon82ddc942017-07-14 12:00:13 -040058 bool onCombineIfPossible(GrOp* op, const GrCaps& caps) override { return false; }
59
60 GrProcessorSet fProcessors;
61
62 typedef GrMeshDrawOp INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050063};
64
65/**
66 * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts
67 * of resources owned by child FPs.
68 */
69class TestFP : public GrFragmentProcessor {
70public:
Brian Salomonaff329b2017-08-11 09:40:37 -040071 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) {
72 return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child)));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050073 }
Brian Salomonaff329b2017-08-11 09:40:37 -040074 static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Brian Salomon559f5562017-11-15 14:28:33 -050075 const SkTArray<sk_sp<GrBuffer>>& buffers) {
76 return std::unique_ptr<GrFragmentProcessor>(new TestFP(proxies, buffers));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050077 }
78
79 const char* name() const override { return "test"; }
80
81 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
82 // We don't really care about reusing these.
83 static int32_t gKey = 0;
84 b->add32(sk_atomic_inc(&gKey));
85 }
86
Brian Salomonaff329b2017-08-11 09:40:37 -040087 std::unique_ptr<GrFragmentProcessor> clone() const override {
88 return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this));
Brian Salomonb17e6392017-07-28 13:41:51 -040089 }
90
Brian Salomonbc6b99d2017-01-11 10:32:34 -050091private:
Brian Salomon559f5562017-11-15 14:28:33 -050092 TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies, const SkTArray<sk_sp<GrBuffer>>& buffers)
93 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4), fBuffers(4) {
Robert Phillips30f9bc62017-02-22 15:28:38 -050094 for (const auto& proxy : proxies) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040095 this->addTextureSampler(&fSamplers.emplace_back(proxy));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050096 }
97 for (const auto& buffer : buffers) {
98 this->addBufferAccess(&fBuffers.emplace_back(kRGBA_8888_GrPixelConfig, buffer.get()));
99 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500100 }
101
Brian Salomonaff329b2017-08-11 09:40:37 -0400102 TestFP(std::unique_ptr<GrFragmentProcessor> child)
Brian Salomon559f5562017-11-15 14:28:33 -0500103 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4), fBuffers(4) {
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500104 this->registerChildProcessor(std::move(child));
105 }
106
Brian Salomon96271cd2017-07-31 16:27:23 -0400107 explicit TestFP(const TestFP& that)
Brian Salomon559f5562017-11-15 14:28:33 -0500108 : INHERITED(kTestFP_ClassID, that.optimizationFlags()), fSamplers(4), fBuffers(4) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400109 for (int i = 0; i < that.fSamplers.count(); ++i) {
110 fSamplers.emplace_back(that.fSamplers[i]);
111 this->addTextureSampler(&fSamplers.back());
112 }
113 for (int i = 0; i < that.fBuffers.count(); ++i) {
114 fBuffers.emplace_back(that.fBuffers[i]);
115 this->addBufferAccess(&fBuffers.back());
116 }
Brian Salomon96271cd2017-07-31 16:27:23 -0400117 for (int i = 0; i < that.numChildProcessors(); ++i) {
118 this->registerChildProcessor(that.childProcessor(i).clone());
Brian Salomonb17e6392017-07-28 13:41:51 -0400119 }
120 }
121
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500122 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
123 class TestGLSLFP : public GrGLSLFragmentProcessor {
124 public:
125 TestGLSLFP() {}
126 void emitCode(EmitArgs& args) override {
127 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
128 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
129 }
130
131 private:
132 };
133 return new TestGLSLFP();
134 }
135
136 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
137
138 GrTAllocator<TextureSampler> fSamplers;
139 GrTAllocator<BufferAccess> fBuffers;
Brian Salomon587e08f2017-01-27 10:59:27 -0500140 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500141};
142}
143
Brian Salomonf046e152017-01-11 15:24:47 -0500144template <typename T>
145inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) {
146 *refCnt = resource->fRefCnt;
147 *readCnt = resource->fPendingReads;
148 *writeCnt = resource->fPendingWrites;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500149}
150
Robert Phillips2f493142017-03-02 18:18:38 -0500151void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500152 *refCnt = proxy->getBackingRefCnt_TestOnly();
153 *readCnt = proxy->getPendingReadCnt_TestOnly();
154 *writeCnt = proxy->getPendingWriteCnt_TestOnly();
155}
156
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500157DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
158 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500159 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500160
Robert Phillips16d8ec62017-07-27 16:16:25 -0400161 GrSurfaceDesc desc;
162 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500163 desc.fWidth = 10;
164 desc.fHeight = 10;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400165 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500166
Brian Salomonb17e6392017-07-28 13:41:51 -0400167 for (bool makeClone : {false, true}) {
168 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
169 sk_sp<GrRenderTargetContext> renderTargetContext(
170 context->makeDeferredRenderTargetContext( SkBackingFit::kApprox, 1, 1,
171 kRGBA_8888_GrPixelConfig, nullptr));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500172 {
Brian Salomonb17e6392017-07-28 13:41:51 -0400173 bool texelBufferSupport = context->caps()->shaderCaps()->texelBufferSupport();
Brian Salomonb17e6392017-07-28 13:41:51 -0400174 sk_sp<GrTextureProxy> proxy1(
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500175 GrSurfaceProxy::MakeDeferred(proxyProvider,
Brian Salomonb17e6392017-07-28 13:41:51 -0400176 desc, SkBackingFit::kExact,
177 SkBudgeted::kYes));
178 sk_sp<GrTextureProxy> proxy2
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500179 (GrSurfaceProxy::MakeDeferred(proxyProvider,
Brian Salomonb17e6392017-07-28 13:41:51 -0400180 desc, SkBackingFit::kExact,
181 SkBudgeted::kYes));
182 sk_sp<GrTextureProxy> proxy3(
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500183 GrSurfaceProxy::MakeDeferred(proxyProvider,
Brian Salomonb17e6392017-07-28 13:41:51 -0400184 desc, SkBackingFit::kExact,
185 SkBudgeted::kYes));
186 sk_sp<GrTextureProxy> proxy4(
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500187 GrSurfaceProxy::MakeDeferred(proxyProvider,
Brian Salomonb17e6392017-07-28 13:41:51 -0400188 desc, SkBackingFit::kExact,
189 SkBudgeted::kYes));
190 sk_sp<GrBuffer> buffer(texelBufferSupport
191 ? context->resourceProvider()->createBuffer(
192 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 }
210 std::unique_ptr<GrDrawOp> op(TestOp::Make(std::move(fp)));
211 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
212 if (clone) {
213 op = TestOp::Make(std::move(clone));
214 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
Brian Salomonaff329b2017-08-11 09:40:37 -0400285void test_draw_op(GrRenderTargetContext* rtc, 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
Brian Salomonbaaf4392017-06-15 09:59:23 -0400292 auto op = GrRectOpFactory::MakeNonAAFill(std::move(paint), SkMatrix::I(),
293 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. */
299bool init_test_textures(GrContext* context, SkRandom* random, sk_sp<GrTextureProxy> proxies[2]) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500300 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
301
Brian Salomon0e05a822017-07-25 09:43:22 -0400302 static const int kTestTextureSize = 256;
303 GrSurfaceDesc desc;
304 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
305 desc.fWidth = kTestTextureSize;
306 desc.fHeight = kTestTextureSize;
307 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400308
Brian Salomon0e05a822017-07-25 09:43:22 -0400309 // Put premul data into the RGBA texture that the test FPs can optionally use.
310 std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]);
311 for (int y = 0; y < kTestTextureSize; ++y) {
312 for (int x = 0; x < kTestTextureSize; ++x) {
313 rgbaData[kTestTextureSize * y + x] =
314 input_texel_color(random->nextULessThan(256), random->nextULessThan(256));
315 }
316 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500317 proxies[0] = GrSurfaceProxy::MakeDeferred(proxyProvider, desc, SkBudgeted::kYes,
Brian Salomon0e05a822017-07-25 09:43:22 -0400318 rgbaData.get(), kTestTextureSize * sizeof(GrColor));
319
320 // Put random values into the alpha texture that the test FPs can optionally use.
321 desc.fConfig = kAlpha_8_GrPixelConfig;
322 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]);
323 for (int y = 0; y < kTestTextureSize; ++y) {
324 for (int x = 0; x < kTestTextureSize; ++x) {
325 alphaData[kTestTextureSize * y + x] = random->nextULessThan(256);
326 }
327 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500328 proxies[1] = GrSurfaceProxy::MakeDeferred(proxyProvider, desc, SkBudgeted::kYes,
Brian Salomon0e05a822017-07-25 09:43:22 -0400329 alphaData.get(), kTestTextureSize);
330
331 return proxies[0] && proxies[1];
332}
333
334// Creates a texture of premul colors used as the output of the fragment processor that precedes
335// the fragment processor under test. Color values are those provided by input_texel_color().
336sk_sp<GrTextureProxy> make_input_texture(GrContext* context, int width, int height) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500337 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
338
Brian Salomon0e05a822017-07-25 09:43:22 -0400339 std::unique_ptr<GrColor[]> data(new GrColor[width * height]);
340 for (int y = 0; y < width; ++y) {
341 for (int x = 0; x < height; ++x) {
342 data.get()[width * y + x] = input_texel_color(x, y);
343 }
344 }
345 GrSurfaceDesc desc;
346 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
347 desc.fWidth = width;
348 desc.fHeight = height;
349 desc.fConfig = kRGBA_8888_GrPixelConfig;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500350 return GrSurfaceProxy::MakeDeferred(proxyProvider, desc, SkBudgeted::kYes,
Brian Salomon0e05a822017-07-25 09:43:22 -0400351 data.get(), width * sizeof(GrColor));
352}
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500353DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500354 GrContext* context = ctxInfo.grContext();
Brian Salomon1c053642017-07-24 10:16:19 -0400355 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400356
357 uint32_t seed = 0;
358 if (FLAGS_randomProcessorTest) {
359 std::random_device rd;
360 seed = rd();
361 }
362 // If a non-deterministic bot fails this test, check the output to see what seed it used, then
363 // hard-code that value here:
364 SkRandom random(seed);
365
Brian Salomon0e05a822017-07-25 09:43:22 -0400366 // Make the destination context for the test.
367 static constexpr int kRenderSize = 256;
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400368 sk_sp<GrRenderTargetContext> rtc = context->makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400369 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500370
Robert Phillipse78b7252017-04-06 07:59:41 -0400371 sk_sp<GrTextureProxy> proxies[2];
Brian Salomon0e05a822017-07-25 09:43:22 -0400372 if (!init_test_textures(context, &random, proxies)) {
373 ERRORF(reporter, "Could not create test textures");
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400374 return;
375 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400376 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500377
Brian Salomon0e05a822017-07-25 09:43:22 -0400378 auto inputTexture = make_input_texture(context, kRenderSize, kRenderSize);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500379
Ben Wagner8c791942017-07-25 11:47:48 -0400380 std::unique_ptr<GrColor[]> readData(new GrColor[kRenderSize * kRenderSize]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400381 // Because processor factories configure themselves in random ways, this is not exhaustive.
Brian Salomon587e08f2017-01-27 10:59:27 -0500382 for (int i = 0; i < FPFactory::Count(); ++i) {
383 int timesToInvokeFactory = 5;
384 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
385 // on child optimizations being present.
Brian Salomonaff329b2017-08-11 09:40:37 -0400386 std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
Brian Salomon587e08f2017-01-27 10:59:27 -0500387 for (int j = 0; j < fp->numChildProcessors(); ++j) {
388 // This value made a reasonable trade off between time and coverage when this test was
389 // written.
390 timesToInvokeFactory *= FPFactory::Count() / 2;
391 }
392 for (int j = 0; j < timesToInvokeFactory; ++j) {
393 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillips9bee2e52017-05-29 12:37:20 -0400394 if (!fp->instantiate(context->resourceProvider())) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400395 continue;
396 }
397
Brian Salomon587e08f2017-01-27 10:59:27 -0500398 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500399 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500400 continue;
401 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400402
403 // Since we transfer away ownership of the original FP, we make a clone.
404 auto clone = fp->clone();
405
406 test_draw_op(rtc.get(), std::move(fp), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400407 memset(readData.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
408 rtc->readPixels(SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
409 kPremul_SkAlphaType),
410 readData.get(), 0, 0, 0);
Brian Salomon587e08f2017-01-27 10:59:27 -0500411 bool passing = true;
412 if (0) { // Useful to see what FPs are being tested.
413 SkString children;
Brian Salomonaff329b2017-08-11 09:40:37 -0400414 for (int c = 0; c < clone->numChildProcessors(); ++c) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500415 if (!c) {
416 children.append("(");
417 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400418 children.append(clone->name());
419 children.append(c == clone->numChildProcessors() - 1 ? ")" : ", ");
Brian Salomon587e08f2017-01-27 10:59:27 -0500420 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400421 SkDebugf("%s %s\n", clone->name(), children.c_str());
Brian Salomon587e08f2017-01-27 10:59:27 -0500422 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400423 for (int y = 0; y < kRenderSize && passing; ++y) {
424 for (int x = 0; x < kRenderSize && passing; ++x) {
425 GrColor input = input_texel_color(x, y);
426 GrColor output = readData.get()[y * kRenderSize + x];
Brian Salomonaff329b2017-08-11 09:40:37 -0400427 if (clone->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500428 // A modulating processor is allowed to modulate either the input color or
429 // just the input alpha.
430 bool legalColorModulation =
431 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
432 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
433 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
434 GrColorUnpackB(output) <= GrColorUnpackB(input);
435 bool legalAlphaModulation =
436 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
437 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
438 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
439 GrColorUnpackB(output) <= GrColorUnpackA(input);
440 if (!legalColorModulation && !legalAlphaModulation) {
441 ERRORF(reporter,
442 "\"Modulating\" processor %s made color/alpha value larger. "
Greg Daniel10ed2432017-12-01 16:19:43 -0500443 "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).",
444 clone->name(), input, output, x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500445 passing = false;
446 }
447 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400448 GrColor4f input4f = input_texel_color4f(x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500449 GrColor4f output4f = GrColor4f::FromGrColor(output);
450 GrColor4f expected4f;
Brian Salomonaff329b2017-08-11 09:40:37 -0400451 if (clone->hasConstantOutputForConstantInput(input4f, &expected4f)) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500452 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
453 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
454 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
455 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500456 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500457 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
458 ERRORF(reporter,
459 "Processor %s claimed output for const input doesn't match "
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500460 "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, "
461 "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)",
462 fp->name(), SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))),
463 kTol, input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2],
464 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();
492
493 SkRandom random;
494
495 // Make the destination context for the test.
496 static constexpr int kRenderSize = 1024;
497 sk_sp<GrRenderTargetContext> rtc = context->makeDeferredRenderTargetContext(
498 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
499
500 sk_sp<GrTextureProxy> proxies[2];
501 if (!init_test_textures(context, &random, proxies)) {
502 ERRORF(reporter, "Could not create test textures");
503 return;
504 }
505 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
506
507 auto inputTexture = make_input_texture(context, kRenderSize, kRenderSize);
508 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
509 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
510 auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
511 kPremul_SkAlphaType);
512
513 // Because processor factories configure themselves in random ways, this is not exhaustive.
514 for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
515 static constexpr int kTimesToInvokeFactory = 10;
516 for (int j = 0; j < kTimesToInvokeFactory; ++j) {
517 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
518 auto clone = fp->clone();
Brian Salomon0e05a822017-07-25 09:43:22 -0400519 if (!clone) {
Brian Salomon96271cd2017-07-31 16:27:23 -0400520 ERRORF(reporter, "Clone of processor %s failed.", fp->name());
Brian Salomon0e05a822017-07-25 09:43:22 -0400521 continue;
522 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400523 const char* name = fp->name();
Brian Salomon0e05a822017-07-25 09:43:22 -0400524 if (!fp->instantiate(context->resourceProvider()) ||
525 !clone->instantiate(context->resourceProvider())) {
526 continue;
527 }
Brian Salomonce06e262017-08-01 16:23:40 -0400528 REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name()));
529 REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() ==
530 clone->compatibleWithCoverageAsAlpha());
531 REPORTER_ASSERT(reporter, fp->isEqual(*clone));
532 REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput());
533 REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() ==
534 clone->hasConstantOutputForConstantInput());
535 REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors());
536 REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords());
Brian Salomon0e05a822017-07-25 09:43:22 -0400537 // Draw with original and read back the results.
Brian Salomonaff329b2017-08-11 09:40:37 -0400538 test_draw_op(rtc.get(), std::move(fp), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400539 memset(readData1.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
540 rtc->readPixels(readInfo, readData1.get(), 0, 0, 0);
541
542 // Draw with clone and read back the results.
Brian Salomonaff329b2017-08-11 09:40:37 -0400543 test_draw_op(rtc.get(), std::move(clone), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400544 memset(readData2.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
545 rtc->readPixels(readInfo, readData2.get(), 0, 0, 0);
546
547 // Check that the results are the same.
548 bool passing = true;
549 for (int y = 0; y < kRenderSize && passing; ++y) {
550 for (int x = 0; x < kRenderSize && passing; ++x) {
551 int idx = y * kRenderSize + x;
552 if (readData1[idx] != readData2[idx]) {
553 ERRORF(reporter,
554 "Processor %s made clone produced different output. "
555 "Input color: 0x%08x, Original Output Color: 0x%08x, "
556 "Clone Output Color: 0x%08x..",
Brian Salomonaff329b2017-08-11 09:40:37 -0400557 name, input_texel_color(x, y), readData1[idx], readData2[idx]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400558 passing = false;
559 }
560 }
561 }
562 }
563 }
564}
565
Hal Canary6f6961e2017-01-31 13:50:44 -0500566#endif // GR_TEST_UTILS
567#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
568#endif // SK_SUPPORT_GPU