blob: 161192e487e95ea05ddc4f1e613328bdab23dd7b [file] [log] [blame]
Brian Salomonbc6b99d2017-01-11 10:32:34 -05001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkTypes.h"
9#include "Test.h"
10
11#if SK_SUPPORT_GPU
Brian Salomonac70f842017-05-08 10:43:33 -040012#include <random>
Brian Salomonc65aec92017-03-09 09:03:58 -050013#include "GrClip.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050014#include "GrContext.h"
15#include "GrGpuResource.h"
16#include "GrRenderTargetContext.h"
17#include "GrRenderTargetContextPriv.h"
18#include "GrResourceProvider.h"
19#include "glsl/GrGLSLFragmentProcessor.h"
20#include "glsl/GrGLSLFragmentShaderBuilder.h"
Brian Salomon82ddc942017-07-14 12:00:13 -040021#include "ops/GrMeshDrawOp.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040022#include "ops/GrRectOpFactory.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050023
24namespace {
Brian Salomon82ddc942017-07-14 12:00:13 -040025class TestOp : public GrMeshDrawOp {
Brian Salomonbc6b99d2017-01-11 10:32:34 -050026public:
27 DEFINE_OP_CLASS_ID
28 const char* name() const override { return "TestOp"; }
29
Brian Salomonaff329b2017-08-11 09:40:37 -040030 static std::unique_ptr<GrDrawOp> Make(std::unique_ptr<GrFragmentProcessor> fp) {
Brian Salomon82ddc942017-07-14 12:00:13 -040031 return std::unique_ptr<GrDrawOp>(new TestOp(std::move(fp)));
32 }
33
34 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
35
36 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
37 static constexpr GrProcessorAnalysisColor kUnknownColor;
38 GrColor overrideColor;
39 fProcessors.finalize(kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, false, caps,
40 &overrideColor);
41 return RequiresDstTexture::kNo;
Brian Salomon649a3412017-03-09 13:50:43 -050042 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050043
44private:
Brian Salomonaff329b2017-08-11 09:40:37 -040045 TestOp(std::unique_ptr<GrFragmentProcessor> fp)
46 : INHERITED(ClassID()), fProcessors(std::move(fp)) {
Brian Salomon82ddc942017-07-14 12:00:13 -040047 this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsZeroArea::kNo);
48 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050049
Brian Salomon91326c32017-08-09 16:02:19 -040050 void onPrepareDraws(Target* target) override { return; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050051
Brian Salomon82ddc942017-07-14 12:00:13 -040052 bool onCombineIfPossible(GrOp* op, const GrCaps& caps) override { return false; }
53
54 GrProcessorSet fProcessors;
55
56 typedef GrMeshDrawOp INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050057};
58
59/**
60 * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts
61 * of resources owned by child FPs.
62 */
63class TestFP : public GrFragmentProcessor {
64public:
65 struct Image {
Robert Phillips8a02f652017-05-12 14:49:16 -040066 Image(sk_sp<GrTextureProxy> proxy, GrIOType ioType) : fProxy(proxy), fIOType(ioType) {}
67 sk_sp<GrTextureProxy> fProxy;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050068 GrIOType fIOType;
69 };
Brian Salomonaff329b2017-08-11 09:40:37 -040070 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) {
71 return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child)));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050072 }
Brian Salomonaff329b2017-08-11 09:40:37 -040073 static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
74 const SkTArray<sk_sp<GrBuffer>>& buffers,
75 const SkTArray<Image>& images) {
76 return std::unique_ptr<GrFragmentProcessor>(new TestFP(proxies, buffers, images));
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:
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040092 TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Robert Phillips30f9bc62017-02-22 15:28:38 -050093 const SkTArray<sk_sp<GrBuffer>>& buffers,
Brian Salomonbc6b99d2017-01-11 10:32:34 -050094 const SkTArray<Image>& images)
Brian Salomon587e08f2017-01-27 10:59:27 -050095 : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) {
Brian Salomonb17e6392017-07-28 13:41:51 -040096 this->initClassID<TestFP>();
Robert Phillips30f9bc62017-02-22 15:28:38 -050097 for (const auto& proxy : proxies) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040098 this->addTextureSampler(&fSamplers.emplace_back(proxy));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050099 }
100 for (const auto& buffer : buffers) {
101 this->addBufferAccess(&fBuffers.emplace_back(kRGBA_8888_GrPixelConfig, buffer.get()));
102 }
103 for (const Image& image : images) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400104 fImages.emplace_back(image.fProxy, image.fIOType,
105 GrSLMemoryModel::kNone, GrSLRestrict::kNo);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400106 this->addImageStorageAccess(&fImages.back());
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500107 }
108 }
109
Brian Salomonaff329b2017-08-11 09:40:37 -0400110 TestFP(std::unique_ptr<GrFragmentProcessor> child)
Brian Salomon587e08f2017-01-27 10:59:27 -0500111 : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400112 this->initClassID<TestFP>();
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500113 this->registerChildProcessor(std::move(child));
114 }
115
Brian Salomon96271cd2017-07-31 16:27:23 -0400116 explicit TestFP(const TestFP& that)
Brian Salomonb17e6392017-07-28 13:41:51 -0400117 : INHERITED(that.optimizationFlags()), fSamplers(4), fBuffers(4), fImages(4) {
118 this->initClassID<TestFP>();
119 for (int i = 0; i < that.fSamplers.count(); ++i) {
120 fSamplers.emplace_back(that.fSamplers[i]);
121 this->addTextureSampler(&fSamplers.back());
122 }
123 for (int i = 0; i < that.fBuffers.count(); ++i) {
124 fBuffers.emplace_back(that.fBuffers[i]);
125 this->addBufferAccess(&fBuffers.back());
126 }
127 for (int i = 0; i < that.fImages.count(); ++i) {
128 fImages.emplace_back(that.fImages[i]);
129 this->addImageStorageAccess(&fImages.back());
130 }
Brian Salomon96271cd2017-07-31 16:27:23 -0400131 for (int i = 0; i < that.numChildProcessors(); ++i) {
132 this->registerChildProcessor(that.childProcessor(i).clone());
Brian Salomonb17e6392017-07-28 13:41:51 -0400133 }
134 }
135
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500136 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
137 class TestGLSLFP : public GrGLSLFragmentProcessor {
138 public:
139 TestGLSLFP() {}
140 void emitCode(EmitArgs& args) override {
141 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
142 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
143 }
144
145 private:
146 };
147 return new TestGLSLFP();
148 }
149
150 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
151
152 GrTAllocator<TextureSampler> fSamplers;
153 GrTAllocator<BufferAccess> fBuffers;
154 GrTAllocator<ImageStorageAccess> fImages;
Brian Salomon587e08f2017-01-27 10:59:27 -0500155 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500156};
157}
158
Brian Salomonf046e152017-01-11 15:24:47 -0500159template <typename T>
160inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) {
161 *refCnt = resource->fRefCnt;
162 *readCnt = resource->fPendingReads;
163 *writeCnt = resource->fPendingWrites;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500164}
165
Robert Phillips2f493142017-03-02 18:18:38 -0500166void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500167 *refCnt = proxy->getBackingRefCnt_TestOnly();
168 *readCnt = proxy->getPendingReadCnt_TestOnly();
169 *writeCnt = proxy->getPendingWriteCnt_TestOnly();
170}
171
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500172DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
173 GrContext* context = ctxInfo.grContext();
174
Robert Phillips16d8ec62017-07-27 16:16:25 -0400175 GrSurfaceDesc desc;
176 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500177 desc.fWidth = 10;
178 desc.fHeight = 10;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400179 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500180
Brian Salomonb17e6392017-07-28 13:41:51 -0400181 for (bool makeClone : {false, true}) {
182 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
183 sk_sp<GrRenderTargetContext> renderTargetContext(
184 context->makeDeferredRenderTargetContext( SkBackingFit::kApprox, 1, 1,
185 kRGBA_8888_GrPixelConfig, nullptr));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500186 {
Brian Salomonb17e6392017-07-28 13:41:51 -0400187 bool texelBufferSupport = context->caps()->shaderCaps()->texelBufferSupport();
188 bool imageLoadStoreSupport = context->caps()->shaderCaps()->imageLoadStoreSupport();
189 sk_sp<GrTextureProxy> proxy1(
190 GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
191 desc, SkBackingFit::kExact,
192 SkBudgeted::kYes));
193 sk_sp<GrTextureProxy> proxy2
194 (GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
195 desc, SkBackingFit::kExact,
196 SkBudgeted::kYes));
197 sk_sp<GrTextureProxy> proxy3(
198 GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
199 desc, SkBackingFit::kExact,
200 SkBudgeted::kYes));
201 sk_sp<GrTextureProxy> proxy4(
202 GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
203 desc, SkBackingFit::kExact,
204 SkBudgeted::kYes));
205 sk_sp<GrBuffer> buffer(texelBufferSupport
206 ? context->resourceProvider()->createBuffer(
207 1024, GrBufferType::kTexel_GrBufferType,
208 GrAccessPattern::kStatic_GrAccessPattern, 0)
209 : nullptr);
210 {
211 SkTArray<sk_sp<GrTextureProxy>> proxies;
212 SkTArray<sk_sp<GrBuffer>> buffers;
213 SkTArray<TestFP::Image> images;
214 proxies.push_back(proxy1);
215 if (texelBufferSupport) {
216 buffers.push_back(buffer);
217 }
218 if (imageLoadStoreSupport) {
219 images.emplace_back(proxy2, GrIOType::kRead_GrIOType);
220 images.emplace_back(proxy3, GrIOType::kWrite_GrIOType);
221 images.emplace_back(proxy4, GrIOType::kRW_GrIOType);
222 }
223 auto fp = TestFP::Make(std::move(proxies), std::move(buffers),
224 std::move(images));
225 for (int i = 0; i < parentCnt; ++i) {
226 fp = TestFP::Make(std::move(fp));
227 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400228 std::unique_ptr<GrFragmentProcessor> clone;
Brian Salomonb17e6392017-07-28 13:41:51 -0400229 if (makeClone) {
230 clone = fp->clone();
231 }
232 std::unique_ptr<GrDrawOp> op(TestOp::Make(std::move(fp)));
233 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
234 if (clone) {
235 op = TestOp::Make(std::move(clone));
236 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
237 }
238 }
239 int refCnt, readCnt, writeCnt;
240
241 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
242 // IO counts should be double if there is a clone of the FP.
243 int ioRefMul = makeClone ? 2 : 1;
244 REPORTER_ASSERT(reporter, 1 == refCnt);
245 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
246 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
247
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500248 if (texelBufferSupport) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400249 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
250 REPORTER_ASSERT(reporter, 1 == refCnt);
251 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
252 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500253 }
Brian Salomonb17e6392017-07-28 13:41:51 -0400254
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500255 if (imageLoadStoreSupport) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400256 testingOnly_getIORefCnts(proxy2.get(), &refCnt, &readCnt, &writeCnt);
257 REPORTER_ASSERT(reporter, 1 == refCnt);
258 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
259 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
260
261 testingOnly_getIORefCnts(proxy3.get(), &refCnt, &readCnt, &writeCnt);
262 REPORTER_ASSERT(reporter, 1 == refCnt);
263 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
264 REPORTER_ASSERT(reporter, ioRefMul * 1 == writeCnt);
265
266 testingOnly_getIORefCnts(proxy4.get(), &refCnt, &readCnt, &writeCnt);
267 REPORTER_ASSERT(reporter, 1 == refCnt);
268 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
269 REPORTER_ASSERT(reporter, ioRefMul * 1 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500270 }
Brian Salomonb17e6392017-07-28 13:41:51 -0400271
272 context->flush();
273
274 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
275 REPORTER_ASSERT(reporter, 1 == refCnt);
276 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
277 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
278
279 if (texelBufferSupport) {
280 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
281 REPORTER_ASSERT(reporter, 1 == refCnt);
282 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
283 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500284 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500285
Brian Salomonb17e6392017-07-28 13:41:51 -0400286 if (texelBufferSupport) {
287 testingOnly_getIORefCnts(proxy2.get(), &refCnt, &readCnt, &writeCnt);
288 REPORTER_ASSERT(reporter, 1 == refCnt);
289 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
290 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500291
Brian Salomonb17e6392017-07-28 13:41:51 -0400292 testingOnly_getIORefCnts(proxy3.get(), &refCnt, &readCnt, &writeCnt);
293 REPORTER_ASSERT(reporter, 1 == refCnt);
294 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
295 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500296
Brian Salomonb17e6392017-07-28 13:41:51 -0400297 testingOnly_getIORefCnts(proxy4.get(), &refCnt, &readCnt, &writeCnt);
298 REPORTER_ASSERT(reporter, 1 == refCnt);
299 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
300 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
301 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500302 }
303 }
304 }
305}
Brian Salomon587e08f2017-01-27 10:59:27 -0500306
307// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
308#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
309
Brian Salomon0e05a822017-07-25 09:43:22 -0400310#include "SkCommandLineFlags.h"
311DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?");
312
313#if GR_TEST_UTILS
314
315static GrColor input_texel_color(int i, int j) {
316 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 -0500317 return GrPremulColor(color);
318}
319
Brian Salomon0e05a822017-07-25 09:43:22 -0400320static GrColor4f input_texel_color4f(int i, int j) {
321 return GrColor4f::FromGrColor(input_texel_color(i, j));
322}
Brian Salomon587e08f2017-01-27 10:59:27 -0500323
Brian Salomonaff329b2017-08-11 09:40:37 -0400324void test_draw_op(GrRenderTargetContext* rtc, std::unique_ptr<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500325 sk_sp<GrTextureProxy> inputDataProxy) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500326 GrPaint paint;
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400327 paint.addColorTextureProcessor(std::move(inputDataProxy), nullptr, SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500328 paint.addColorFragmentProcessor(std::move(fp));
329 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400330
Brian Salomonbaaf4392017-06-15 09:59:23 -0400331 auto op = GrRectOpFactory::MakeNonAAFill(std::move(paint), SkMatrix::I(),
332 SkRect::MakeWH(rtc->width(), rtc->height()),
333 GrAAType::kNone);
Brian Salomonac70f842017-05-08 10:43:33 -0400334 rtc->addDrawOp(GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500335}
336
Brian Salomon0e05a822017-07-25 09:43:22 -0400337/** Initializes the two test texture proxies that are available to the FP test factories. */
338bool init_test_textures(GrContext* context, SkRandom* random, sk_sp<GrTextureProxy> proxies[2]) {
339 static const int kTestTextureSize = 256;
340 GrSurfaceDesc desc;
341 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
342 desc.fWidth = kTestTextureSize;
343 desc.fHeight = kTestTextureSize;
344 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400345
Brian Salomon0e05a822017-07-25 09:43:22 -0400346 // Put premul data into the RGBA texture that the test FPs can optionally use.
347 std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]);
348 for (int y = 0; y < kTestTextureSize; ++y) {
349 for (int x = 0; x < kTestTextureSize; ++x) {
350 rgbaData[kTestTextureSize * y + x] =
351 input_texel_color(random->nextULessThan(256), random->nextULessThan(256));
352 }
353 }
354 proxies[0] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
355 rgbaData.get(), kTestTextureSize * sizeof(GrColor));
356
357 // Put random values into the alpha texture that the test FPs can optionally use.
358 desc.fConfig = kAlpha_8_GrPixelConfig;
359 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]);
360 for (int y = 0; y < kTestTextureSize; ++y) {
361 for (int x = 0; x < kTestTextureSize; ++x) {
362 alphaData[kTestTextureSize * y + x] = random->nextULessThan(256);
363 }
364 }
365 proxies[1] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
366 alphaData.get(), kTestTextureSize);
367
368 return proxies[0] && proxies[1];
369}
370
371// Creates a texture of premul colors used as the output of the fragment processor that precedes
372// the fragment processor under test. Color values are those provided by input_texel_color().
373sk_sp<GrTextureProxy> make_input_texture(GrContext* context, int width, int height) {
374 std::unique_ptr<GrColor[]> data(new GrColor[width * height]);
375 for (int y = 0; y < width; ++y) {
376 for (int x = 0; x < height; ++x) {
377 data.get()[width * y + x] = input_texel_color(x, y);
378 }
379 }
380 GrSurfaceDesc desc;
381 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
382 desc.fWidth = width;
383 desc.fHeight = height;
384 desc.fConfig = kRGBA_8888_GrPixelConfig;
385 return GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
386 data.get(), width * sizeof(GrColor));
387}
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500388DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500389 GrContext* context = ctxInfo.grContext();
Brian Salomon1c053642017-07-24 10:16:19 -0400390 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400391
392 uint32_t seed = 0;
393 if (FLAGS_randomProcessorTest) {
394 std::random_device rd;
395 seed = rd();
396 }
397 // If a non-deterministic bot fails this test, check the output to see what seed it used, then
398 // hard-code that value here:
399 SkRandom random(seed);
400
Brian Salomon0e05a822017-07-25 09:43:22 -0400401 // Make the destination context for the test.
402 static constexpr int kRenderSize = 256;
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400403 sk_sp<GrRenderTargetContext> rtc = context->makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400404 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500405
Robert Phillipse78b7252017-04-06 07:59:41 -0400406 sk_sp<GrTextureProxy> proxies[2];
Brian Salomon0e05a822017-07-25 09:43:22 -0400407 if (!init_test_textures(context, &random, proxies)) {
408 ERRORF(reporter, "Could not create test textures");
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400409 return;
410 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400411 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500412
Brian Salomon0e05a822017-07-25 09:43:22 -0400413 auto inputTexture = make_input_texture(context, kRenderSize, kRenderSize);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500414
Ben Wagner8c791942017-07-25 11:47:48 -0400415 std::unique_ptr<GrColor[]> readData(new GrColor[kRenderSize * kRenderSize]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400416 // Because processor factories configure themselves in random ways, this is not exhaustive.
Brian Salomon587e08f2017-01-27 10:59:27 -0500417 for (int i = 0; i < FPFactory::Count(); ++i) {
418 int timesToInvokeFactory = 5;
419 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
420 // on child optimizations being present.
Brian Salomonaff329b2017-08-11 09:40:37 -0400421 std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
Brian Salomon587e08f2017-01-27 10:59:27 -0500422 for (int j = 0; j < fp->numChildProcessors(); ++j) {
423 // This value made a reasonable trade off between time and coverage when this test was
424 // written.
425 timesToInvokeFactory *= FPFactory::Count() / 2;
426 }
427 for (int j = 0; j < timesToInvokeFactory; ++j) {
428 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillips9bee2e52017-05-29 12:37:20 -0400429 if (!fp->instantiate(context->resourceProvider())) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400430 continue;
431 }
432
Brian Salomon587e08f2017-01-27 10:59:27 -0500433 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500434 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500435 continue;
436 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400437
438 // Since we transfer away ownership of the original FP, we make a clone.
439 auto clone = fp->clone();
440
441 test_draw_op(rtc.get(), std::move(fp), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400442 memset(readData.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
443 rtc->readPixels(SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
444 kPremul_SkAlphaType),
445 readData.get(), 0, 0, 0);
Brian Salomon587e08f2017-01-27 10:59:27 -0500446 bool passing = true;
447 if (0) { // Useful to see what FPs are being tested.
448 SkString children;
Brian Salomonaff329b2017-08-11 09:40:37 -0400449 for (int c = 0; c < clone->numChildProcessors(); ++c) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500450 if (!c) {
451 children.append("(");
452 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400453 children.append(clone->name());
454 children.append(c == clone->numChildProcessors() - 1 ? ")" : ", ");
Brian Salomon587e08f2017-01-27 10:59:27 -0500455 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400456 SkDebugf("%s %s\n", clone->name(), children.c_str());
Brian Salomon587e08f2017-01-27 10:59:27 -0500457 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400458 for (int y = 0; y < kRenderSize && passing; ++y) {
459 for (int x = 0; x < kRenderSize && passing; ++x) {
460 GrColor input = input_texel_color(x, y);
461 GrColor output = readData.get()[y * kRenderSize + x];
Brian Salomonaff329b2017-08-11 09:40:37 -0400462 if (clone->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500463 // A modulating processor is allowed to modulate either the input color or
464 // just the input alpha.
465 bool legalColorModulation =
466 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
467 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
468 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
469 GrColorUnpackB(output) <= GrColorUnpackB(input);
470 bool legalAlphaModulation =
471 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
472 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
473 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
474 GrColorUnpackB(output) <= GrColorUnpackA(input);
475 if (!legalColorModulation && !legalAlphaModulation) {
476 ERRORF(reporter,
477 "\"Modulating\" processor %s made color/alpha value larger. "
Brian Osman2f3865b2017-03-15 13:35:51 -0400478 "Input: 0x%08x, Output: 0x%08x.",
Brian Salomonaff329b2017-08-11 09:40:37 -0400479 clone->name(), input, output);
Brian Salomon587e08f2017-01-27 10:59:27 -0500480 passing = false;
481 }
482 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400483 GrColor4f input4f = input_texel_color4f(x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500484 GrColor4f output4f = GrColor4f::FromGrColor(output);
485 GrColor4f expected4f;
Brian Salomonaff329b2017-08-11 09:40:37 -0400486 if (clone->hasConstantOutputForConstantInput(input4f, &expected4f)) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500487 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
488 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
489 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
490 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500491 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500492 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
493 ERRORF(reporter,
494 "Processor %s claimed output for const input doesn't match "
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500495 "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, "
496 "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)",
497 fp->name(), SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))),
498 kTol, input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2],
499 input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1],
500 output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0],
501 expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]);
Brian Salomon587e08f2017-01-27 10:59:27 -0500502 passing = false;
503 }
504 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400505 if (GrColorIsOpaque(input) && clone->preservesOpaqueInput() &&
Brian Salomon587e08f2017-01-27 10:59:27 -0500506 !GrColorIsOpaque(output)) {
507 ERRORF(reporter,
508 "Processor %s claimed opaqueness is preserved but it is not. Input: "
Brian Osman2f3865b2017-03-15 13:35:51 -0400509 "0x%08x, Output: 0x%08x.",
Brian Salomonaff329b2017-08-11 09:40:37 -0400510 clone->name(), input, output);
Brian Salomon587e08f2017-01-27 10:59:27 -0500511 passing = false;
512 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400513 if (!passing) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400514 ERRORF(reporter, "Seed: 0x%08x, Processor details: %s", seed,
515 clone->dumpInfo().c_str());
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400516 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500517 }
518 }
519 }
520 }
521}
Robert Phillips18166ee2017-06-01 12:55:44 -0400522
Brian Salomon0e05a822017-07-25 09:43:22 -0400523// Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their
524// progenitors.
525DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
526 GrContext* context = ctxInfo.grContext();
527
528 SkRandom random;
529
530 // Make the destination context for the test.
531 static constexpr int kRenderSize = 1024;
532 sk_sp<GrRenderTargetContext> rtc = context->makeDeferredRenderTargetContext(
533 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
534
535 sk_sp<GrTextureProxy> proxies[2];
536 if (!init_test_textures(context, &random, proxies)) {
537 ERRORF(reporter, "Could not create test textures");
538 return;
539 }
540 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
541
542 auto inputTexture = make_input_texture(context, kRenderSize, kRenderSize);
543 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
544 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
545 auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
546 kPremul_SkAlphaType);
547
548 // Because processor factories configure themselves in random ways, this is not exhaustive.
549 for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
550 static constexpr int kTimesToInvokeFactory = 10;
551 for (int j = 0; j < kTimesToInvokeFactory; ++j) {
552 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
553 auto clone = fp->clone();
Brian Salomon0e05a822017-07-25 09:43:22 -0400554 if (!clone) {
Brian Salomon96271cd2017-07-31 16:27:23 -0400555 ERRORF(reporter, "Clone of processor %s failed.", fp->name());
Brian Salomon0e05a822017-07-25 09:43:22 -0400556 continue;
557 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400558 const char* name = fp->name();
Brian Salomon0e05a822017-07-25 09:43:22 -0400559 if (!fp->instantiate(context->resourceProvider()) ||
560 !clone->instantiate(context->resourceProvider())) {
561 continue;
562 }
Brian Salomonce06e262017-08-01 16:23:40 -0400563 REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name()));
564 REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() ==
565 clone->compatibleWithCoverageAsAlpha());
566 REPORTER_ASSERT(reporter, fp->isEqual(*clone));
567 REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput());
568 REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() ==
569 clone->hasConstantOutputForConstantInput());
570 REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors());
571 REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords());
Brian Salomon0e05a822017-07-25 09:43:22 -0400572 // Draw with original and read back the results.
Brian Salomonaff329b2017-08-11 09:40:37 -0400573 test_draw_op(rtc.get(), std::move(fp), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400574 memset(readData1.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
575 rtc->readPixels(readInfo, readData1.get(), 0, 0, 0);
576
577 // Draw with clone and read back the results.
Brian Salomonaff329b2017-08-11 09:40:37 -0400578 test_draw_op(rtc.get(), std::move(clone), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400579 memset(readData2.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
580 rtc->readPixels(readInfo, readData2.get(), 0, 0, 0);
581
582 // Check that the results are the same.
583 bool passing = true;
584 for (int y = 0; y < kRenderSize && passing; ++y) {
585 for (int x = 0; x < kRenderSize && passing; ++x) {
586 int idx = y * kRenderSize + x;
587 if (readData1[idx] != readData2[idx]) {
588 ERRORF(reporter,
589 "Processor %s made clone produced different output. "
590 "Input color: 0x%08x, Original Output Color: 0x%08x, "
591 "Clone Output Color: 0x%08x..",
Brian Salomonaff329b2017-08-11 09:40:37 -0400592 name, input_texel_color(x, y), readData1[idx], readData2[idx]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400593 passing = false;
594 }
595 }
596 }
597 }
598 }
599}
600
Hal Canary6f6961e2017-01-31 13:50:44 -0500601#endif // GR_TEST_UTILS
602#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
603#endif // SK_SUPPORT_GPU