blob: 4cc7780d062996190b8557a262165da6ccd2a5a5 [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 Salomon82ddc942017-07-14 12:00:13 -040030 static std::unique_ptr<GrDrawOp> Make(sk_sp<GrFragmentProcessor> fp) {
31 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 Salomon82ddc942017-07-14 12:00:13 -040045 TestOp(sk_sp<GrFragmentProcessor> fp) : INHERITED(ClassID()), fProcessors(std::move(fp)) {
46 this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsZeroArea::kNo);
47 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050048
Brian Salomon91326c32017-08-09 16:02:19 -040049 void onPrepareDraws(Target* target) override { return; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050050
Brian Salomon82ddc942017-07-14 12:00:13 -040051 bool onCombineIfPossible(GrOp* op, const GrCaps& caps) override { return false; }
52
53 GrProcessorSet fProcessors;
54
55 typedef GrMeshDrawOp INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050056};
57
58/**
59 * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts
60 * of resources owned by child FPs.
61 */
62class TestFP : public GrFragmentProcessor {
63public:
64 struct Image {
Robert Phillips8a02f652017-05-12 14:49:16 -040065 Image(sk_sp<GrTextureProxy> proxy, GrIOType ioType) : fProxy(proxy), fIOType(ioType) {}
66 sk_sp<GrTextureProxy> fProxy;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050067 GrIOType fIOType;
68 };
69 static sk_sp<GrFragmentProcessor> Make(sk_sp<GrFragmentProcessor> child) {
70 return sk_sp<GrFragmentProcessor>(new TestFP(std::move(child)));
71 }
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040072 static sk_sp<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Brian Salomonbc6b99d2017-01-11 10:32:34 -050073 const SkTArray<sk_sp<GrBuffer>>& buffers,
74 const SkTArray<Image>& images) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040075 return sk_sp<GrFragmentProcessor>(new TestFP(proxies, buffers, images));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050076 }
77
78 const char* name() const override { return "test"; }
79
80 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
81 // We don't really care about reusing these.
82 static int32_t gKey = 0;
83 b->add32(sk_atomic_inc(&gKey));
84 }
85
Brian Salomonb17e6392017-07-28 13:41:51 -040086 sk_sp<GrFragmentProcessor> clone() const override {
Brian Salomon96271cd2017-07-31 16:27:23 -040087 return sk_sp<GrFragmentProcessor>(new TestFP(*this));
Brian Salomonb17e6392017-07-28 13:41:51 -040088 }
89
Brian Salomonbc6b99d2017-01-11 10:32:34 -050090private:
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040091 TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Robert Phillips30f9bc62017-02-22 15:28:38 -050092 const SkTArray<sk_sp<GrBuffer>>& buffers,
Brian Salomonbc6b99d2017-01-11 10:32:34 -050093 const SkTArray<Image>& images)
Brian Salomon587e08f2017-01-27 10:59:27 -050094 : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) {
Brian Salomonb17e6392017-07-28 13:41:51 -040095 this->initClassID<TestFP>();
Robert Phillips30f9bc62017-02-22 15:28:38 -050096 for (const auto& proxy : proxies) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040097 this->addTextureSampler(&fSamplers.emplace_back(proxy));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050098 }
99 for (const auto& buffer : buffers) {
100 this->addBufferAccess(&fBuffers.emplace_back(kRGBA_8888_GrPixelConfig, buffer.get()));
101 }
102 for (const Image& image : images) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400103 fImages.emplace_back(image.fProxy, image.fIOType,
104 GrSLMemoryModel::kNone, GrSLRestrict::kNo);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400105 this->addImageStorageAccess(&fImages.back());
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500106 }
107 }
108
Brian Salomon587e08f2017-01-27 10:59:27 -0500109 TestFP(sk_sp<GrFragmentProcessor> child)
110 : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400111 this->initClassID<TestFP>();
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500112 this->registerChildProcessor(std::move(child));
113 }
114
Brian Salomon96271cd2017-07-31 16:27:23 -0400115 explicit TestFP(const TestFP& that)
Brian Salomonb17e6392017-07-28 13:41:51 -0400116 : INHERITED(that.optimizationFlags()), fSamplers(4), fBuffers(4), fImages(4) {
117 this->initClassID<TestFP>();
118 for (int i = 0; i < that.fSamplers.count(); ++i) {
119 fSamplers.emplace_back(that.fSamplers[i]);
120 this->addTextureSampler(&fSamplers.back());
121 }
122 for (int i = 0; i < that.fBuffers.count(); ++i) {
123 fBuffers.emplace_back(that.fBuffers[i]);
124 this->addBufferAccess(&fBuffers.back());
125 }
126 for (int i = 0; i < that.fImages.count(); ++i) {
127 fImages.emplace_back(that.fImages[i]);
128 this->addImageStorageAccess(&fImages.back());
129 }
Brian Salomon96271cd2017-07-31 16:27:23 -0400130 for (int i = 0; i < that.numChildProcessors(); ++i) {
131 this->registerChildProcessor(that.childProcessor(i).clone());
Brian Salomonb17e6392017-07-28 13:41:51 -0400132 }
133 }
134
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500135 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
136 class TestGLSLFP : public GrGLSLFragmentProcessor {
137 public:
138 TestGLSLFP() {}
139 void emitCode(EmitArgs& args) override {
140 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
141 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
142 }
143
144 private:
145 };
146 return new TestGLSLFP();
147 }
148
149 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
150
151 GrTAllocator<TextureSampler> fSamplers;
152 GrTAllocator<BufferAccess> fBuffers;
153 GrTAllocator<ImageStorageAccess> fImages;
Brian Salomon587e08f2017-01-27 10:59:27 -0500154 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500155};
156}
157
Brian Salomonf046e152017-01-11 15:24:47 -0500158template <typename T>
159inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) {
160 *refCnt = resource->fRefCnt;
161 *readCnt = resource->fPendingReads;
162 *writeCnt = resource->fPendingWrites;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500163}
164
Robert Phillips2f493142017-03-02 18:18:38 -0500165void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500166 *refCnt = proxy->getBackingRefCnt_TestOnly();
167 *readCnt = proxy->getPendingReadCnt_TestOnly();
168 *writeCnt = proxy->getPendingWriteCnt_TestOnly();
169}
170
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500171DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
172 GrContext* context = ctxInfo.grContext();
173
Robert Phillips16d8ec62017-07-27 16:16:25 -0400174 GrSurfaceDesc desc;
175 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500176 desc.fWidth = 10;
177 desc.fHeight = 10;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400178 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500179
Brian Salomonb17e6392017-07-28 13:41:51 -0400180 for (bool makeClone : {false, true}) {
181 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
182 sk_sp<GrRenderTargetContext> renderTargetContext(
183 context->makeDeferredRenderTargetContext( SkBackingFit::kApprox, 1, 1,
184 kRGBA_8888_GrPixelConfig, nullptr));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500185 {
Brian Salomonb17e6392017-07-28 13:41:51 -0400186 bool texelBufferSupport = context->caps()->shaderCaps()->texelBufferSupport();
187 bool imageLoadStoreSupport = context->caps()->shaderCaps()->imageLoadStoreSupport();
188 sk_sp<GrTextureProxy> proxy1(
189 GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
190 desc, SkBackingFit::kExact,
191 SkBudgeted::kYes));
192 sk_sp<GrTextureProxy> proxy2
193 (GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
194 desc, SkBackingFit::kExact,
195 SkBudgeted::kYes));
196 sk_sp<GrTextureProxy> proxy3(
197 GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
198 desc, SkBackingFit::kExact,
199 SkBudgeted::kYes));
200 sk_sp<GrTextureProxy> proxy4(
201 GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
202 desc, SkBackingFit::kExact,
203 SkBudgeted::kYes));
204 sk_sp<GrBuffer> buffer(texelBufferSupport
205 ? context->resourceProvider()->createBuffer(
206 1024, GrBufferType::kTexel_GrBufferType,
207 GrAccessPattern::kStatic_GrAccessPattern, 0)
208 : nullptr);
209 {
210 SkTArray<sk_sp<GrTextureProxy>> proxies;
211 SkTArray<sk_sp<GrBuffer>> buffers;
212 SkTArray<TestFP::Image> images;
213 proxies.push_back(proxy1);
214 if (texelBufferSupport) {
215 buffers.push_back(buffer);
216 }
217 if (imageLoadStoreSupport) {
218 images.emplace_back(proxy2, GrIOType::kRead_GrIOType);
219 images.emplace_back(proxy3, GrIOType::kWrite_GrIOType);
220 images.emplace_back(proxy4, GrIOType::kRW_GrIOType);
221 }
222 auto fp = TestFP::Make(std::move(proxies), std::move(buffers),
223 std::move(images));
224 for (int i = 0; i < parentCnt; ++i) {
225 fp = TestFP::Make(std::move(fp));
226 }
227 sk_sp<GrFragmentProcessor> clone;
228 if (makeClone) {
229 clone = fp->clone();
230 }
231 std::unique_ptr<GrDrawOp> op(TestOp::Make(std::move(fp)));
232 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
233 if (clone) {
234 op = TestOp::Make(std::move(clone));
235 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
236 }
237 }
238 int refCnt, readCnt, writeCnt;
239
240 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
241 // IO counts should be double if there is a clone of the FP.
242 int ioRefMul = makeClone ? 2 : 1;
243 REPORTER_ASSERT(reporter, 1 == refCnt);
244 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
245 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
246
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500247 if (texelBufferSupport) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400248 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
249 REPORTER_ASSERT(reporter, 1 == refCnt);
250 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
251 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500252 }
Brian Salomonb17e6392017-07-28 13:41:51 -0400253
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500254 if (imageLoadStoreSupport) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400255 testingOnly_getIORefCnts(proxy2.get(), &refCnt, &readCnt, &writeCnt);
256 REPORTER_ASSERT(reporter, 1 == refCnt);
257 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
258 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
259
260 testingOnly_getIORefCnts(proxy3.get(), &refCnt, &readCnt, &writeCnt);
261 REPORTER_ASSERT(reporter, 1 == refCnt);
262 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
263 REPORTER_ASSERT(reporter, ioRefMul * 1 == writeCnt);
264
265 testingOnly_getIORefCnts(proxy4.get(), &refCnt, &readCnt, &writeCnt);
266 REPORTER_ASSERT(reporter, 1 == refCnt);
267 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
268 REPORTER_ASSERT(reporter, ioRefMul * 1 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500269 }
Brian Salomonb17e6392017-07-28 13:41:51 -0400270
271 context->flush();
272
273 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
274 REPORTER_ASSERT(reporter, 1 == refCnt);
275 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
276 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
277
278 if (texelBufferSupport) {
279 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
280 REPORTER_ASSERT(reporter, 1 == refCnt);
281 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
282 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500283 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500284
Brian Salomonb17e6392017-07-28 13:41:51 -0400285 if (texelBufferSupport) {
286 testingOnly_getIORefCnts(proxy2.get(), &refCnt, &readCnt, &writeCnt);
287 REPORTER_ASSERT(reporter, 1 == refCnt);
288 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
289 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500290
Brian Salomonb17e6392017-07-28 13:41:51 -0400291 testingOnly_getIORefCnts(proxy3.get(), &refCnt, &readCnt, &writeCnt);
292 REPORTER_ASSERT(reporter, 1 == refCnt);
293 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
294 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500295
Brian Salomonb17e6392017-07-28 13:41:51 -0400296 testingOnly_getIORefCnts(proxy4.get(), &refCnt, &readCnt, &writeCnt);
297 REPORTER_ASSERT(reporter, 1 == refCnt);
298 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
299 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
300 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500301 }
302 }
303 }
304}
Brian Salomon587e08f2017-01-27 10:59:27 -0500305
306// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
307#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
308
Brian Salomon0e05a822017-07-25 09:43:22 -0400309#include "SkCommandLineFlags.h"
310DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?");
311
312#if GR_TEST_UTILS
313
314static GrColor input_texel_color(int i, int j) {
315 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 -0500316 return GrPremulColor(color);
317}
318
Brian Salomon0e05a822017-07-25 09:43:22 -0400319static GrColor4f input_texel_color4f(int i, int j) {
320 return GrColor4f::FromGrColor(input_texel_color(i, j));
321}
Brian Salomon587e08f2017-01-27 10:59:27 -0500322
Robert Phillips296b1cc2017-03-15 10:42:12 -0400323void test_draw_op(GrRenderTargetContext* rtc, sk_sp<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500324 sk_sp<GrTextureProxy> inputDataProxy) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500325 GrPaint paint;
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400326 paint.addColorTextureProcessor(std::move(inputDataProxy), nullptr, SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500327 paint.addColorFragmentProcessor(std::move(fp));
328 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400329
Brian Salomonbaaf4392017-06-15 09:59:23 -0400330 auto op = GrRectOpFactory::MakeNonAAFill(std::move(paint), SkMatrix::I(),
331 SkRect::MakeWH(rtc->width(), rtc->height()),
332 GrAAType::kNone);
Brian Salomonac70f842017-05-08 10:43:33 -0400333 rtc->addDrawOp(GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500334}
335
Brian Salomon0e05a822017-07-25 09:43:22 -0400336/** Initializes the two test texture proxies that are available to the FP test factories. */
337bool init_test_textures(GrContext* context, SkRandom* random, sk_sp<GrTextureProxy> proxies[2]) {
338 static const int kTestTextureSize = 256;
339 GrSurfaceDesc desc;
340 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
341 desc.fWidth = kTestTextureSize;
342 desc.fHeight = kTestTextureSize;
343 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400344
Brian Salomon0e05a822017-07-25 09:43:22 -0400345 // Put premul data into the RGBA texture that the test FPs can optionally use.
346 std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]);
347 for (int y = 0; y < kTestTextureSize; ++y) {
348 for (int x = 0; x < kTestTextureSize; ++x) {
349 rgbaData[kTestTextureSize * y + x] =
350 input_texel_color(random->nextULessThan(256), random->nextULessThan(256));
351 }
352 }
353 proxies[0] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
354 rgbaData.get(), kTestTextureSize * sizeof(GrColor));
355
356 // Put random values into the alpha texture that the test FPs can optionally use.
357 desc.fConfig = kAlpha_8_GrPixelConfig;
358 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]);
359 for (int y = 0; y < kTestTextureSize; ++y) {
360 for (int x = 0; x < kTestTextureSize; ++x) {
361 alphaData[kTestTextureSize * y + x] = random->nextULessThan(256);
362 }
363 }
364 proxies[1] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
365 alphaData.get(), kTestTextureSize);
366
367 return proxies[0] && proxies[1];
368}
369
370// Creates a texture of premul colors used as the output of the fragment processor that precedes
371// the fragment processor under test. Color values are those provided by input_texel_color().
372sk_sp<GrTextureProxy> make_input_texture(GrContext* context, int width, int height) {
373 std::unique_ptr<GrColor[]> data(new GrColor[width * height]);
374 for (int y = 0; y < width; ++y) {
375 for (int x = 0; x < height; ++x) {
376 data.get()[width * y + x] = input_texel_color(x, y);
377 }
378 }
379 GrSurfaceDesc desc;
380 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
381 desc.fWidth = width;
382 desc.fHeight = height;
383 desc.fConfig = kRGBA_8888_GrPixelConfig;
384 return GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
385 data.get(), width * sizeof(GrColor));
386}
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500387DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500388 GrContext* context = ctxInfo.grContext();
Brian Salomon1c053642017-07-24 10:16:19 -0400389 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400390
391 uint32_t seed = 0;
392 if (FLAGS_randomProcessorTest) {
393 std::random_device rd;
394 seed = rd();
395 }
396 // If a non-deterministic bot fails this test, check the output to see what seed it used, then
397 // hard-code that value here:
398 SkRandom random(seed);
399
Brian Salomon0e05a822017-07-25 09:43:22 -0400400 // Make the destination context for the test.
401 static constexpr int kRenderSize = 256;
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400402 sk_sp<GrRenderTargetContext> rtc = context->makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400403 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500404
Robert Phillipse78b7252017-04-06 07:59:41 -0400405 sk_sp<GrTextureProxy> proxies[2];
Brian Salomon0e05a822017-07-25 09:43:22 -0400406 if (!init_test_textures(context, &random, proxies)) {
407 ERRORF(reporter, "Could not create test textures");
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400408 return;
409 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400410 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500411
Brian Salomon0e05a822017-07-25 09:43:22 -0400412 auto inputTexture = make_input_texture(context, kRenderSize, kRenderSize);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500413
Ben Wagner8c791942017-07-25 11:47:48 -0400414 std::unique_ptr<GrColor[]> readData(new GrColor[kRenderSize * kRenderSize]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400415 // Because processor factories configure themselves in random ways, this is not exhaustive.
Brian Salomon587e08f2017-01-27 10:59:27 -0500416 for (int i = 0; i < FPFactory::Count(); ++i) {
417 int timesToInvokeFactory = 5;
418 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
419 // on child optimizations being present.
420 sk_sp<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
421 for (int j = 0; j < fp->numChildProcessors(); ++j) {
422 // This value made a reasonable trade off between time and coverage when this test was
423 // written.
424 timesToInvokeFactory *= FPFactory::Count() / 2;
425 }
426 for (int j = 0; j < timesToInvokeFactory; ++j) {
427 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillips9bee2e52017-05-29 12:37:20 -0400428 if (!fp->instantiate(context->resourceProvider())) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400429 continue;
430 }
431
Brian Salomon587e08f2017-01-27 10:59:27 -0500432 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500433 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500434 continue;
435 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400436 test_draw_op(rtc.get(), fp, inputTexture);
437 memset(readData.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
438 rtc->readPixels(SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
439 kPremul_SkAlphaType),
440 readData.get(), 0, 0, 0);
Brian Salomon587e08f2017-01-27 10:59:27 -0500441 bool passing = true;
442 if (0) { // Useful to see what FPs are being tested.
443 SkString children;
444 for (int c = 0; c < fp->numChildProcessors(); ++c) {
445 if (!c) {
446 children.append("(");
447 }
448 children.append(fp->childProcessor(c).name());
449 children.append(c == fp->numChildProcessors() - 1 ? ")" : ", ");
450 }
451 SkDebugf("%s %s\n", fp->name(), children.c_str());
452 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400453 for (int y = 0; y < kRenderSize && passing; ++y) {
454 for (int x = 0; x < kRenderSize && passing; ++x) {
455 GrColor input = input_texel_color(x, y);
456 GrColor output = readData.get()[y * kRenderSize + x];
Brian Salomonf3b995b2017-02-15 10:22:23 -0500457 if (fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500458 // A modulating processor is allowed to modulate either the input color or
459 // just the input alpha.
460 bool legalColorModulation =
461 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
462 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
463 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
464 GrColorUnpackB(output) <= GrColorUnpackB(input);
465 bool legalAlphaModulation =
466 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
467 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
468 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
469 GrColorUnpackB(output) <= GrColorUnpackA(input);
470 if (!legalColorModulation && !legalAlphaModulation) {
471 ERRORF(reporter,
472 "\"Modulating\" processor %s made color/alpha value larger. "
Brian Osman2f3865b2017-03-15 13:35:51 -0400473 "Input: 0x%08x, Output: 0x%08x.",
Brian Salomon587e08f2017-01-27 10:59:27 -0500474 fp->name(), input, output);
475 passing = false;
476 }
477 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400478 GrColor4f input4f = input_texel_color4f(x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500479 GrColor4f output4f = GrColor4f::FromGrColor(output);
480 GrColor4f expected4f;
481 if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) {
482 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
483 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
484 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
485 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500486 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500487 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
488 ERRORF(reporter,
489 "Processor %s claimed output for const input doesn't match "
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500490 "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, "
491 "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)",
492 fp->name(), SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))),
493 kTol, input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2],
494 input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1],
495 output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0],
496 expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]);
Brian Salomon587e08f2017-01-27 10:59:27 -0500497 passing = false;
498 }
499 }
500 if (GrColorIsOpaque(input) && fp->preservesOpaqueInput() &&
501 !GrColorIsOpaque(output)) {
502 ERRORF(reporter,
503 "Processor %s claimed opaqueness is preserved but it is not. Input: "
Brian Osman2f3865b2017-03-15 13:35:51 -0400504 "0x%08x, Output: 0x%08x.",
Brian Salomon587e08f2017-01-27 10:59:27 -0500505 fp->name(), input, output);
506 passing = false;
507 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400508 if (!passing) {
Brian Osmanc35a2d42017-03-17 10:58:53 -0400509 ERRORF(reporter, "Seed: 0x%08x, Processor details: %s",
510 seed, fp->dumpInfo().c_str());
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400511 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500512 }
513 }
514 }
515 }
516}
Robert Phillips18166ee2017-06-01 12:55:44 -0400517
Brian Salomon0e05a822017-07-25 09:43:22 -0400518// Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their
519// progenitors.
520DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
521 GrContext* context = ctxInfo.grContext();
522
523 SkRandom random;
524
525 // Make the destination context for the test.
526 static constexpr int kRenderSize = 1024;
527 sk_sp<GrRenderTargetContext> rtc = context->makeDeferredRenderTargetContext(
528 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
529
530 sk_sp<GrTextureProxy> proxies[2];
531 if (!init_test_textures(context, &random, proxies)) {
532 ERRORF(reporter, "Could not create test textures");
533 return;
534 }
535 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
536
537 auto inputTexture = make_input_texture(context, kRenderSize, kRenderSize);
538 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
539 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
540 auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
541 kPremul_SkAlphaType);
542
543 // Because processor factories configure themselves in random ways, this is not exhaustive.
544 for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
545 static constexpr int kTimesToInvokeFactory = 10;
546 for (int j = 0; j < kTimesToInvokeFactory; ++j) {
547 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
548 auto clone = fp->clone();
Brian Salomon0e05a822017-07-25 09:43:22 -0400549 if (!clone) {
Brian Salomon96271cd2017-07-31 16:27:23 -0400550 ERRORF(reporter, "Clone of processor %s failed.", fp->name());
Brian Salomon0e05a822017-07-25 09:43:22 -0400551 continue;
552 }
553 if (!fp->instantiate(context->resourceProvider()) ||
554 !clone->instantiate(context->resourceProvider())) {
555 continue;
556 }
Brian Salomonce06e262017-08-01 16:23:40 -0400557 REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name()));
558 REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() ==
559 clone->compatibleWithCoverageAsAlpha());
560 REPORTER_ASSERT(reporter, fp->isEqual(*clone));
561 REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput());
562 REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() ==
563 clone->hasConstantOutputForConstantInput());
564 REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors());
565 REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords());
Brian Salomon0e05a822017-07-25 09:43:22 -0400566 // Draw with original and read back the results.
567 test_draw_op(rtc.get(), fp, inputTexture);
568 memset(readData1.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
569 rtc->readPixels(readInfo, readData1.get(), 0, 0, 0);
570
571 // Draw with clone and read back the results.
572 test_draw_op(rtc.get(), clone, inputTexture);
573 memset(readData2.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
574 rtc->readPixels(readInfo, readData2.get(), 0, 0, 0);
575
576 // Check that the results are the same.
577 bool passing = true;
578 for (int y = 0; y < kRenderSize && passing; ++y) {
579 for (int x = 0; x < kRenderSize && passing; ++x) {
580 int idx = y * kRenderSize + x;
581 if (readData1[idx] != readData2[idx]) {
582 ERRORF(reporter,
583 "Processor %s made clone produced different output. "
584 "Input color: 0x%08x, Original Output Color: 0x%08x, "
585 "Clone Output Color: 0x%08x..",
586 fp->name(), input_texel_color(x, y), readData1[idx], readData2[idx]);
587 passing = false;
588 }
589 }
590 }
591 }
592 }
593}
594
Hal Canary6f6961e2017-01-31 13:50:44 -0500595#endif // GR_TEST_UTILS
596#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
597#endif // SK_SUPPORT_GPU