blob: 9acf185b9b2e5036819906860072595f8e61ae0e [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
Brian Salomonaff329b2017-08-11 09:40:37 -040028 static std::unique_ptr<GrDrawOp> Make(std::unique_ptr<GrFragmentProcessor> fp) {
Brian Salomon82ddc942017-07-14 12:00:13 -040029 return std::unique_ptr<GrDrawOp>(new TestOp(std::move(fp)));
30 }
31
Robert Phillips5f567c72017-09-14 08:27:37 -040032 const char* name() const override { return "TestOp"; }
33
Robert Phillipsf1748f52017-09-14 14:11:24 -040034 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips5f567c72017-09-14 08:27:37 -040035 fProcessors.visitProxies(func);
36 }
37
Brian Salomon82ddc942017-07-14 12:00:13 -040038 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
39
Brian Osman9a725dd2017-09-20 09:53:22 -040040 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
41 GrPixelConfigIsClamped dstIsClamped) override {
Brian Salomon82ddc942017-07-14 12:00:13 -040042 static constexpr GrProcessorAnalysisColor kUnknownColor;
43 GrColor overrideColor;
44 fProcessors.finalize(kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, false, caps,
Brian Osman9a725dd2017-09-20 09:53:22 -040045 dstIsClamped, &overrideColor);
Brian Salomon82ddc942017-07-14 12:00:13 -040046 return RequiresDstTexture::kNo;
Brian Salomon649a3412017-03-09 13:50:43 -050047 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050048
49private:
Brian Salomonaff329b2017-08-11 09:40:37 -040050 TestOp(std::unique_ptr<GrFragmentProcessor> fp)
51 : INHERITED(ClassID()), fProcessors(std::move(fp)) {
Brian Salomon82ddc942017-07-14 12:00:13 -040052 this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsZeroArea::kNo);
53 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050054
Brian Salomon91326c32017-08-09 16:02:19 -040055 void onPrepareDraws(Target* target) override { return; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050056
Brian Salomon82ddc942017-07-14 12:00:13 -040057 bool onCombineIfPossible(GrOp* op, const GrCaps& caps) override { return false; }
58
59 GrProcessorSet fProcessors;
60
61 typedef GrMeshDrawOp INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050062};
63
64/**
65 * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts
66 * of resources owned by child FPs.
67 */
68class TestFP : public GrFragmentProcessor {
69public:
70 struct Image {
Robert Phillips8a02f652017-05-12 14:49:16 -040071 Image(sk_sp<GrTextureProxy> proxy, GrIOType ioType) : fProxy(proxy), fIOType(ioType) {}
72 sk_sp<GrTextureProxy> fProxy;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050073 GrIOType fIOType;
74 };
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,
79 const SkTArray<sk_sp<GrBuffer>>& buffers,
80 const SkTArray<Image>& images) {
81 return std::unique_ptr<GrFragmentProcessor>(new TestFP(proxies, buffers, images));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050082 }
83
84 const char* name() const override { return "test"; }
85
86 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
87 // We don't really care about reusing these.
88 static int32_t gKey = 0;
89 b->add32(sk_atomic_inc(&gKey));
90 }
91
Brian Salomonaff329b2017-08-11 09:40:37 -040092 std::unique_ptr<GrFragmentProcessor> clone() const override {
93 return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this));
Brian Salomonb17e6392017-07-28 13:41:51 -040094 }
95
Brian Salomonbc6b99d2017-01-11 10:32:34 -050096private:
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040097 TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Robert Phillips30f9bc62017-02-22 15:28:38 -050098 const SkTArray<sk_sp<GrBuffer>>& buffers,
Brian Salomonbc6b99d2017-01-11 10:32:34 -050099 const SkTArray<Image>& images)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400100 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4), fBuffers(4),
101 fImages(4) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500102 for (const auto& proxy : proxies) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400103 this->addTextureSampler(&fSamplers.emplace_back(proxy));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500104 }
105 for (const auto& buffer : buffers) {
106 this->addBufferAccess(&fBuffers.emplace_back(kRGBA_8888_GrPixelConfig, buffer.get()));
107 }
108 for (const Image& image : images) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400109 fImages.emplace_back(image.fProxy, image.fIOType,
110 GrSLMemoryModel::kNone, GrSLRestrict::kNo);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400111 this->addImageStorageAccess(&fImages.back());
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500112 }
113 }
114
Brian Salomonaff329b2017-08-11 09:40:37 -0400115 TestFP(std::unique_ptr<GrFragmentProcessor> child)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400116 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4), fBuffers(4),
117 fImages(4) {
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500118 this->registerChildProcessor(std::move(child));
119 }
120
Brian Salomon96271cd2017-07-31 16:27:23 -0400121 explicit TestFP(const TestFP& that)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400122 : INHERITED(kTestFP_ClassID, that.optimizationFlags()), fSamplers(4), fBuffers(4),
123 fImages(4) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400124 for (int i = 0; i < that.fSamplers.count(); ++i) {
125 fSamplers.emplace_back(that.fSamplers[i]);
126 this->addTextureSampler(&fSamplers.back());
127 }
128 for (int i = 0; i < that.fBuffers.count(); ++i) {
129 fBuffers.emplace_back(that.fBuffers[i]);
130 this->addBufferAccess(&fBuffers.back());
131 }
132 for (int i = 0; i < that.fImages.count(); ++i) {
133 fImages.emplace_back(that.fImages[i]);
134 this->addImageStorageAccess(&fImages.back());
135 }
Brian Salomon96271cd2017-07-31 16:27:23 -0400136 for (int i = 0; i < that.numChildProcessors(); ++i) {
137 this->registerChildProcessor(that.childProcessor(i).clone());
Brian Salomonb17e6392017-07-28 13:41:51 -0400138 }
139 }
140
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500141 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
142 class TestGLSLFP : public GrGLSLFragmentProcessor {
143 public:
144 TestGLSLFP() {}
145 void emitCode(EmitArgs& args) override {
146 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
147 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
148 }
149
150 private:
151 };
152 return new TestGLSLFP();
153 }
154
155 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
156
157 GrTAllocator<TextureSampler> fSamplers;
158 GrTAllocator<BufferAccess> fBuffers;
159 GrTAllocator<ImageStorageAccess> fImages;
Brian Salomon587e08f2017-01-27 10:59:27 -0500160 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500161};
162}
163
Brian Salomonf046e152017-01-11 15:24:47 -0500164template <typename T>
165inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) {
166 *refCnt = resource->fRefCnt;
167 *readCnt = resource->fPendingReads;
168 *writeCnt = resource->fPendingWrites;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500169}
170
Robert Phillips2f493142017-03-02 18:18:38 -0500171void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500172 *refCnt = proxy->getBackingRefCnt_TestOnly();
173 *readCnt = proxy->getPendingReadCnt_TestOnly();
174 *writeCnt = proxy->getPendingWriteCnt_TestOnly();
175}
176
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500177DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
178 GrContext* context = ctxInfo.grContext();
179
Robert Phillips16d8ec62017-07-27 16:16:25 -0400180 GrSurfaceDesc desc;
181 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500182 desc.fWidth = 10;
183 desc.fHeight = 10;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400184 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500185
Brian Salomonb17e6392017-07-28 13:41:51 -0400186 for (bool makeClone : {false, true}) {
187 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
188 sk_sp<GrRenderTargetContext> renderTargetContext(
189 context->makeDeferredRenderTargetContext( SkBackingFit::kApprox, 1, 1,
190 kRGBA_8888_GrPixelConfig, nullptr));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500191 {
Brian Salomonb17e6392017-07-28 13:41:51 -0400192 bool texelBufferSupport = context->caps()->shaderCaps()->texelBufferSupport();
193 bool imageLoadStoreSupport = context->caps()->shaderCaps()->imageLoadStoreSupport();
194 sk_sp<GrTextureProxy> proxy1(
195 GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
196 desc, SkBackingFit::kExact,
197 SkBudgeted::kYes));
198 sk_sp<GrTextureProxy> proxy2
199 (GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
200 desc, SkBackingFit::kExact,
201 SkBudgeted::kYes));
202 sk_sp<GrTextureProxy> proxy3(
203 GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
204 desc, SkBackingFit::kExact,
205 SkBudgeted::kYes));
206 sk_sp<GrTextureProxy> proxy4(
207 GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
208 desc, SkBackingFit::kExact,
209 SkBudgeted::kYes));
210 sk_sp<GrBuffer> buffer(texelBufferSupport
211 ? context->resourceProvider()->createBuffer(
212 1024, GrBufferType::kTexel_GrBufferType,
213 GrAccessPattern::kStatic_GrAccessPattern, 0)
214 : nullptr);
215 {
216 SkTArray<sk_sp<GrTextureProxy>> proxies;
217 SkTArray<sk_sp<GrBuffer>> buffers;
218 SkTArray<TestFP::Image> images;
219 proxies.push_back(proxy1);
220 if (texelBufferSupport) {
221 buffers.push_back(buffer);
222 }
223 if (imageLoadStoreSupport) {
224 images.emplace_back(proxy2, GrIOType::kRead_GrIOType);
225 images.emplace_back(proxy3, GrIOType::kWrite_GrIOType);
226 images.emplace_back(proxy4, GrIOType::kRW_GrIOType);
227 }
228 auto fp = TestFP::Make(std::move(proxies), std::move(buffers),
229 std::move(images));
230 for (int i = 0; i < parentCnt; ++i) {
231 fp = TestFP::Make(std::move(fp));
232 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400233 std::unique_ptr<GrFragmentProcessor> clone;
Brian Salomonb17e6392017-07-28 13:41:51 -0400234 if (makeClone) {
235 clone = fp->clone();
236 }
237 std::unique_ptr<GrDrawOp> op(TestOp::Make(std::move(fp)));
238 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
239 if (clone) {
240 op = TestOp::Make(std::move(clone));
241 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
242 }
243 }
244 int refCnt, readCnt, writeCnt;
245
246 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
247 // IO counts should be double if there is a clone of the FP.
248 int ioRefMul = makeClone ? 2 : 1;
249 REPORTER_ASSERT(reporter, 1 == refCnt);
250 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
251 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
252
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500253 if (texelBufferSupport) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400254 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
255 REPORTER_ASSERT(reporter, 1 == refCnt);
256 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
257 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500258 }
Brian Salomonb17e6392017-07-28 13:41:51 -0400259
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500260 if (imageLoadStoreSupport) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400261 testingOnly_getIORefCnts(proxy2.get(), &refCnt, &readCnt, &writeCnt);
262 REPORTER_ASSERT(reporter, 1 == refCnt);
263 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
264 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
265
266 testingOnly_getIORefCnts(proxy3.get(), &refCnt, &readCnt, &writeCnt);
267 REPORTER_ASSERT(reporter, 1 == refCnt);
268 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
269 REPORTER_ASSERT(reporter, ioRefMul * 1 == writeCnt);
270
271 testingOnly_getIORefCnts(proxy4.get(), &refCnt, &readCnt, &writeCnt);
272 REPORTER_ASSERT(reporter, 1 == refCnt);
273 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
274 REPORTER_ASSERT(reporter, ioRefMul * 1 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500275 }
Brian Salomonb17e6392017-07-28 13:41:51 -0400276
277 context->flush();
278
279 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
280 REPORTER_ASSERT(reporter, 1 == refCnt);
281 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
282 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
283
284 if (texelBufferSupport) {
285 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
286 REPORTER_ASSERT(reporter, 1 == refCnt);
287 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
288 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500289 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500290
Brian Salomonb17e6392017-07-28 13:41:51 -0400291 if (texelBufferSupport) {
292 testingOnly_getIORefCnts(proxy2.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(proxy3.get(), &refCnt, &readCnt, &writeCnt);
298 REPORTER_ASSERT(reporter, 1 == refCnt);
299 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
300 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500301
Brian Salomonb17e6392017-07-28 13:41:51 -0400302 testingOnly_getIORefCnts(proxy4.get(), &refCnt, &readCnt, &writeCnt);
303 REPORTER_ASSERT(reporter, 1 == refCnt);
304 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
305 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
306 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500307 }
308 }
309 }
310}
Brian Salomon587e08f2017-01-27 10:59:27 -0500311
312// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
313#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
314
Brian Salomon0e05a822017-07-25 09:43:22 -0400315#include "SkCommandLineFlags.h"
316DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?");
317
318#if GR_TEST_UTILS
319
320static GrColor input_texel_color(int i, int j) {
321 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 -0500322 return GrPremulColor(color);
323}
324
Brian Salomon0e05a822017-07-25 09:43:22 -0400325static GrColor4f input_texel_color4f(int i, int j) {
326 return GrColor4f::FromGrColor(input_texel_color(i, j));
327}
Brian Salomon587e08f2017-01-27 10:59:27 -0500328
Brian Salomonaff329b2017-08-11 09:40:37 -0400329void test_draw_op(GrRenderTargetContext* rtc, std::unique_ptr<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500330 sk_sp<GrTextureProxy> inputDataProxy) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500331 GrPaint paint;
Brian Osman2240be92017-10-18 13:15:13 -0400332 paint.addColorTextureProcessor(std::move(inputDataProxy), SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500333 paint.addColorFragmentProcessor(std::move(fp));
334 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400335
Brian Salomonbaaf4392017-06-15 09:59:23 -0400336 auto op = GrRectOpFactory::MakeNonAAFill(std::move(paint), SkMatrix::I(),
337 SkRect::MakeWH(rtc->width(), rtc->height()),
338 GrAAType::kNone);
Brian Salomonac70f842017-05-08 10:43:33 -0400339 rtc->addDrawOp(GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500340}
341
Brian Salomon0e05a822017-07-25 09:43:22 -0400342/** Initializes the two test texture proxies that are available to the FP test factories. */
343bool init_test_textures(GrContext* context, SkRandom* random, sk_sp<GrTextureProxy> proxies[2]) {
344 static const int kTestTextureSize = 256;
345 GrSurfaceDesc desc;
346 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
347 desc.fWidth = kTestTextureSize;
348 desc.fHeight = kTestTextureSize;
349 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400350
Brian Salomon0e05a822017-07-25 09:43:22 -0400351 // Put premul data into the RGBA texture that the test FPs can optionally use.
352 std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]);
353 for (int y = 0; y < kTestTextureSize; ++y) {
354 for (int x = 0; x < kTestTextureSize; ++x) {
355 rgbaData[kTestTextureSize * y + x] =
356 input_texel_color(random->nextULessThan(256), random->nextULessThan(256));
357 }
358 }
359 proxies[0] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
360 rgbaData.get(), kTestTextureSize * sizeof(GrColor));
361
362 // Put random values into the alpha texture that the test FPs can optionally use.
363 desc.fConfig = kAlpha_8_GrPixelConfig;
364 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]);
365 for (int y = 0; y < kTestTextureSize; ++y) {
366 for (int x = 0; x < kTestTextureSize; ++x) {
367 alphaData[kTestTextureSize * y + x] = random->nextULessThan(256);
368 }
369 }
370 proxies[1] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
371 alphaData.get(), kTestTextureSize);
372
373 return proxies[0] && proxies[1];
374}
375
376// Creates a texture of premul colors used as the output of the fragment processor that precedes
377// the fragment processor under test. Color values are those provided by input_texel_color().
378sk_sp<GrTextureProxy> make_input_texture(GrContext* context, int width, int height) {
379 std::unique_ptr<GrColor[]> data(new GrColor[width * height]);
380 for (int y = 0; y < width; ++y) {
381 for (int x = 0; x < height; ++x) {
382 data.get()[width * y + x] = input_texel_color(x, y);
383 }
384 }
385 GrSurfaceDesc desc;
386 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
387 desc.fWidth = width;
388 desc.fHeight = height;
389 desc.fConfig = kRGBA_8888_GrPixelConfig;
390 return GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
391 data.get(), width * sizeof(GrColor));
392}
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500393DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500394 GrContext* context = ctxInfo.grContext();
Brian Salomon1c053642017-07-24 10:16:19 -0400395 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400396
397 uint32_t seed = 0;
398 if (FLAGS_randomProcessorTest) {
399 std::random_device rd;
400 seed = rd();
401 }
402 // If a non-deterministic bot fails this test, check the output to see what seed it used, then
403 // hard-code that value here:
404 SkRandom random(seed);
405
Brian Salomon0e05a822017-07-25 09:43:22 -0400406 // Make the destination context for the test.
407 static constexpr int kRenderSize = 256;
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400408 sk_sp<GrRenderTargetContext> rtc = context->makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400409 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500410
Robert Phillipse78b7252017-04-06 07:59:41 -0400411 sk_sp<GrTextureProxy> proxies[2];
Brian Salomon0e05a822017-07-25 09:43:22 -0400412 if (!init_test_textures(context, &random, proxies)) {
413 ERRORF(reporter, "Could not create test textures");
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400414 return;
415 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400416 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500417
Brian Salomon0e05a822017-07-25 09:43:22 -0400418 auto inputTexture = make_input_texture(context, kRenderSize, kRenderSize);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500419
Ben Wagner8c791942017-07-25 11:47:48 -0400420 std::unique_ptr<GrColor[]> readData(new GrColor[kRenderSize * kRenderSize]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400421 // Because processor factories configure themselves in random ways, this is not exhaustive.
Brian Salomon587e08f2017-01-27 10:59:27 -0500422 for (int i = 0; i < FPFactory::Count(); ++i) {
423 int timesToInvokeFactory = 5;
424 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
425 // on child optimizations being present.
Brian Salomonaff329b2017-08-11 09:40:37 -0400426 std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
Brian Salomon587e08f2017-01-27 10:59:27 -0500427 for (int j = 0; j < fp->numChildProcessors(); ++j) {
428 // This value made a reasonable trade off between time and coverage when this test was
429 // written.
430 timesToInvokeFactory *= FPFactory::Count() / 2;
431 }
432 for (int j = 0; j < timesToInvokeFactory; ++j) {
433 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillips9bee2e52017-05-29 12:37:20 -0400434 if (!fp->instantiate(context->resourceProvider())) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400435 continue;
436 }
437
Brian Salomon587e08f2017-01-27 10:59:27 -0500438 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500439 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500440 continue;
441 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400442
443 // Since we transfer away ownership of the original FP, we make a clone.
444 auto clone = fp->clone();
445
446 test_draw_op(rtc.get(), std::move(fp), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400447 memset(readData.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
448 rtc->readPixels(SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
449 kPremul_SkAlphaType),
450 readData.get(), 0, 0, 0);
Brian Salomon587e08f2017-01-27 10:59:27 -0500451 bool passing = true;
452 if (0) { // Useful to see what FPs are being tested.
453 SkString children;
Brian Salomonaff329b2017-08-11 09:40:37 -0400454 for (int c = 0; c < clone->numChildProcessors(); ++c) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500455 if (!c) {
456 children.append("(");
457 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400458 children.append(clone->name());
459 children.append(c == clone->numChildProcessors() - 1 ? ")" : ", ");
Brian Salomon587e08f2017-01-27 10:59:27 -0500460 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400461 SkDebugf("%s %s\n", clone->name(), children.c_str());
Brian Salomon587e08f2017-01-27 10:59:27 -0500462 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400463 for (int y = 0; y < kRenderSize && passing; ++y) {
464 for (int x = 0; x < kRenderSize && passing; ++x) {
465 GrColor input = input_texel_color(x, y);
466 GrColor output = readData.get()[y * kRenderSize + x];
Brian Salomonaff329b2017-08-11 09:40:37 -0400467 if (clone->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500468 // A modulating processor is allowed to modulate either the input color or
469 // just the input alpha.
470 bool legalColorModulation =
471 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
472 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
473 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
474 GrColorUnpackB(output) <= GrColorUnpackB(input);
475 bool legalAlphaModulation =
476 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
477 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
478 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
479 GrColorUnpackB(output) <= GrColorUnpackA(input);
480 if (!legalColorModulation && !legalAlphaModulation) {
481 ERRORF(reporter,
482 "\"Modulating\" processor %s made color/alpha value larger. "
Brian Osman2f3865b2017-03-15 13:35:51 -0400483 "Input: 0x%08x, Output: 0x%08x.",
Brian Salomonaff329b2017-08-11 09:40:37 -0400484 clone->name(), input, output);
Brian Salomon587e08f2017-01-27 10:59:27 -0500485 passing = false;
486 }
487 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400488 GrColor4f input4f = input_texel_color4f(x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500489 GrColor4f output4f = GrColor4f::FromGrColor(output);
490 GrColor4f expected4f;
Brian Salomonaff329b2017-08-11 09:40:37 -0400491 if (clone->hasConstantOutputForConstantInput(input4f, &expected4f)) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500492 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
493 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
494 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
495 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500496 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500497 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
498 ERRORF(reporter,
499 "Processor %s claimed output for const input doesn't match "
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500500 "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, "
501 "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)",
502 fp->name(), SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))),
503 kTol, input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2],
504 input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1],
505 output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0],
506 expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]);
Brian Salomon587e08f2017-01-27 10:59:27 -0500507 passing = false;
508 }
509 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400510 if (GrColorIsOpaque(input) && clone->preservesOpaqueInput() &&
Brian Salomon587e08f2017-01-27 10:59:27 -0500511 !GrColorIsOpaque(output)) {
512 ERRORF(reporter,
513 "Processor %s claimed opaqueness is preserved but it is not. Input: "
Brian Osman2f3865b2017-03-15 13:35:51 -0400514 "0x%08x, Output: 0x%08x.",
Brian Salomonaff329b2017-08-11 09:40:37 -0400515 clone->name(), input, output);
Brian Salomon587e08f2017-01-27 10:59:27 -0500516 passing = false;
517 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400518 if (!passing) {
Brian Salomonaff329b2017-08-11 09:40:37 -0400519 ERRORF(reporter, "Seed: 0x%08x, Processor details: %s", seed,
520 clone->dumpInfo().c_str());
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400521 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500522 }
523 }
524 }
525 }
526}
Robert Phillips18166ee2017-06-01 12:55:44 -0400527
Brian Salomon0e05a822017-07-25 09:43:22 -0400528// Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their
529// progenitors.
530DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
531 GrContext* context = ctxInfo.grContext();
532
533 SkRandom random;
534
535 // Make the destination context for the test.
536 static constexpr int kRenderSize = 1024;
537 sk_sp<GrRenderTargetContext> rtc = context->makeDeferredRenderTargetContext(
538 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
539
540 sk_sp<GrTextureProxy> proxies[2];
541 if (!init_test_textures(context, &random, proxies)) {
542 ERRORF(reporter, "Could not create test textures");
543 return;
544 }
545 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
546
547 auto inputTexture = make_input_texture(context, kRenderSize, kRenderSize);
548 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
549 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
550 auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
551 kPremul_SkAlphaType);
552
553 // Because processor factories configure themselves in random ways, this is not exhaustive.
554 for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
555 static constexpr int kTimesToInvokeFactory = 10;
556 for (int j = 0; j < kTimesToInvokeFactory; ++j) {
557 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
558 auto clone = fp->clone();
Brian Salomon0e05a822017-07-25 09:43:22 -0400559 if (!clone) {
Brian Salomon96271cd2017-07-31 16:27:23 -0400560 ERRORF(reporter, "Clone of processor %s failed.", fp->name());
Brian Salomon0e05a822017-07-25 09:43:22 -0400561 continue;
562 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400563 const char* name = fp->name();
Brian Salomon0e05a822017-07-25 09:43:22 -0400564 if (!fp->instantiate(context->resourceProvider()) ||
565 !clone->instantiate(context->resourceProvider())) {
566 continue;
567 }
Brian Salomonce06e262017-08-01 16:23:40 -0400568 REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name()));
569 REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() ==
570 clone->compatibleWithCoverageAsAlpha());
571 REPORTER_ASSERT(reporter, fp->isEqual(*clone));
572 REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput());
573 REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() ==
574 clone->hasConstantOutputForConstantInput());
575 REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors());
576 REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords());
Brian Salomon0e05a822017-07-25 09:43:22 -0400577 // Draw with original and read back the results.
Brian Salomonaff329b2017-08-11 09:40:37 -0400578 test_draw_op(rtc.get(), std::move(fp), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400579 memset(readData1.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
580 rtc->readPixels(readInfo, readData1.get(), 0, 0, 0);
581
582 // Draw with clone and read back the results.
Brian Salomonaff329b2017-08-11 09:40:37 -0400583 test_draw_op(rtc.get(), std::move(clone), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400584 memset(readData2.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
585 rtc->readPixels(readInfo, readData2.get(), 0, 0, 0);
586
587 // Check that the results are the same.
588 bool passing = true;
589 for (int y = 0; y < kRenderSize && passing; ++y) {
590 for (int x = 0; x < kRenderSize && passing; ++x) {
591 int idx = y * kRenderSize + x;
592 if (readData1[idx] != readData2[idx]) {
593 ERRORF(reporter,
594 "Processor %s made clone produced different output. "
595 "Input color: 0x%08x, Original Output Color: 0x%08x, "
596 "Clone Output Color: 0x%08x..",
Brian Salomonaff329b2017-08-11 09:40:37 -0400597 name, input_texel_color(x, y), readData1[idx], readData2[idx]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400598 passing = false;
599 }
600 }
601 }
602 }
603 }
604}
605
Hal Canary6f6961e2017-01-31 13:50:44 -0500606#endif // GR_TEST_UTILS
607#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
608#endif // SK_SUPPORT_GPU