blob: ec157f9003552c501a4c66251a03a7c962e9b5e8 [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
49 void onPrepareDraws(Target* target) const override { return; }
50
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 Salomonbc6b99d2017-01-11 10:32:34 -050086private:
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040087 TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Robert Phillips30f9bc62017-02-22 15:28:38 -050088 const SkTArray<sk_sp<GrBuffer>>& buffers,
Brian Salomonbc6b99d2017-01-11 10:32:34 -050089 const SkTArray<Image>& images)
Brian Salomon587e08f2017-01-27 10:59:27 -050090 : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) {
Robert Phillips30f9bc62017-02-22 15:28:38 -050091 for (const auto& proxy : proxies) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040092 this->addTextureSampler(&fSamplers.emplace_back(proxy));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050093 }
94 for (const auto& buffer : buffers) {
95 this->addBufferAccess(&fBuffers.emplace_back(kRGBA_8888_GrPixelConfig, buffer.get()));
96 }
97 for (const Image& image : images) {
Robert Phillips8a02f652017-05-12 14:49:16 -040098 fImages.emplace_back(image.fProxy, image.fIOType,
99 GrSLMemoryModel::kNone, GrSLRestrict::kNo);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400100 this->addImageStorageAccess(&fImages.back());
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500101 }
102 }
103
Brian Salomon587e08f2017-01-27 10:59:27 -0500104 TestFP(sk_sp<GrFragmentProcessor> child)
105 : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) {
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500106 this->registerChildProcessor(std::move(child));
107 }
108
109 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
110 class TestGLSLFP : public GrGLSLFragmentProcessor {
111 public:
112 TestGLSLFP() {}
113 void emitCode(EmitArgs& args) override {
114 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
115 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
116 }
117
118 private:
119 };
120 return new TestGLSLFP();
121 }
122
123 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
124
125 GrTAllocator<TextureSampler> fSamplers;
126 GrTAllocator<BufferAccess> fBuffers;
127 GrTAllocator<ImageStorageAccess> fImages;
Brian Salomon587e08f2017-01-27 10:59:27 -0500128 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500129};
130}
131
Brian Salomonf046e152017-01-11 15:24:47 -0500132template <typename T>
133inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) {
134 *refCnt = resource->fRefCnt;
135 *readCnt = resource->fPendingReads;
136 *writeCnt = resource->fPendingWrites;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500137}
138
Robert Phillips2f493142017-03-02 18:18:38 -0500139void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500140 *refCnt = proxy->getBackingRefCnt_TestOnly();
141 *readCnt = proxy->getPendingReadCnt_TestOnly();
142 *writeCnt = proxy->getPendingWriteCnt_TestOnly();
143}
144
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500145DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
146 GrContext* context = ctxInfo.grContext();
147
Robert Phillips16d8ec62017-07-27 16:16:25 -0400148 GrSurfaceDesc desc;
149 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500150 desc.fWidth = 10;
151 desc.fHeight = 10;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400152 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500153
154 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400155 sk_sp<GrRenderTargetContext> renderTargetContext(context->makeDeferredRenderTargetContext(
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500156 SkBackingFit::kApprox, 1, 1, kRGBA_8888_GrPixelConfig, nullptr));
157 {
158 bool texelBufferSupport = context->caps()->shaderCaps()->texelBufferSupport();
159 bool imageLoadStoreSupport = context->caps()->shaderCaps()->imageLoadStoreSupport();
Brian Osman32342f02017-03-04 08:12:46 -0500160 sk_sp<GrTextureProxy> proxy1(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
Robert Phillips8a02f652017-05-12 14:49:16 -0400161 desc, SkBackingFit::kExact,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500162 SkBudgeted::kYes));
Robert Phillips8a02f652017-05-12 14:49:16 -0400163 sk_sp<GrTextureProxy> proxy2(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
164 desc, SkBackingFit::kExact,
165 SkBudgeted::kYes));
166 sk_sp<GrTextureProxy> proxy3(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
167 desc, SkBackingFit::kExact,
168 SkBudgeted::kYes));
169 sk_sp<GrTextureProxy> proxy4(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
170 desc, SkBackingFit::kExact,
171 SkBudgeted::kYes));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500172 sk_sp<GrBuffer> buffer(texelBufferSupport
173 ? context->resourceProvider()->createBuffer(
174 1024, GrBufferType::kTexel_GrBufferType,
175 GrAccessPattern::kStatic_GrAccessPattern, 0)
176 : nullptr);
177 {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500178 SkTArray<sk_sp<GrTextureProxy>> proxies;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500179 SkTArray<sk_sp<GrBuffer>> buffers;
180 SkTArray<TestFP::Image> images;
Robert Phillips2f493142017-03-02 18:18:38 -0500181 proxies.push_back(proxy1);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500182 if (texelBufferSupport) {
183 buffers.push_back(buffer);
184 }
185 if (imageLoadStoreSupport) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400186 images.emplace_back(proxy2, GrIOType::kRead_GrIOType);
187 images.emplace_back(proxy3, GrIOType::kWrite_GrIOType);
188 images.emplace_back(proxy4, GrIOType::kRW_GrIOType);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500189 }
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400190 auto fp = TestFP::Make(std::move(proxies), std::move(buffers), std::move(images));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500191 for (int i = 0; i < parentCnt; ++i) {
192 fp = TestFP::Make(std::move(fp));
193 }
Brian Salomon82ddc942017-07-14 12:00:13 -0400194 std::unique_ptr<GrDrawOp> op(TestOp::Make(std::move(fp)));
195 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500196 }
197 int refCnt, readCnt, writeCnt;
198
Robert Phillips30f9bc62017-02-22 15:28:38 -0500199 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500200 REPORTER_ASSERT(reporter, 1 == refCnt);
201 REPORTER_ASSERT(reporter, 1 == readCnt);
202 REPORTER_ASSERT(reporter, 0 == writeCnt);
203
204 if (texelBufferSupport) {
Brian Salomonf046e152017-01-11 15:24:47 -0500205 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500206 REPORTER_ASSERT(reporter, 1 == refCnt);
207 REPORTER_ASSERT(reporter, 1 == readCnt);
208 REPORTER_ASSERT(reporter, 0 == writeCnt);
209 }
210
211 if (imageLoadStoreSupport) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400212 testingOnly_getIORefCnts(proxy2.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500213 REPORTER_ASSERT(reporter, 1 == refCnt);
214 REPORTER_ASSERT(reporter, 1 == readCnt);
215 REPORTER_ASSERT(reporter, 0 == writeCnt);
216
Robert Phillips8a02f652017-05-12 14:49:16 -0400217 testingOnly_getIORefCnts(proxy3.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500218 REPORTER_ASSERT(reporter, 1 == refCnt);
219 REPORTER_ASSERT(reporter, 0 == readCnt);
220 REPORTER_ASSERT(reporter, 1 == writeCnt);
221
Robert Phillips8a02f652017-05-12 14:49:16 -0400222 testingOnly_getIORefCnts(proxy4.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500223 REPORTER_ASSERT(reporter, 1 == refCnt);
224 REPORTER_ASSERT(reporter, 1 == readCnt);
225 REPORTER_ASSERT(reporter, 1 == writeCnt);
226 }
227
228 context->flush();
229
Robert Phillips30f9bc62017-02-22 15:28:38 -0500230 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500231 REPORTER_ASSERT(reporter, 1 == refCnt);
232 REPORTER_ASSERT(reporter, 0 == readCnt);
233 REPORTER_ASSERT(reporter, 0 == writeCnt);
234
235 if (texelBufferSupport) {
Brian Salomonf046e152017-01-11 15:24:47 -0500236 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500237 REPORTER_ASSERT(reporter, 1 == refCnt);
238 REPORTER_ASSERT(reporter, 0 == readCnt);
239 REPORTER_ASSERT(reporter, 0 == writeCnt);
240 }
241
242 if (texelBufferSupport) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400243 testingOnly_getIORefCnts(proxy2.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500244 REPORTER_ASSERT(reporter, 1 == refCnt);
245 REPORTER_ASSERT(reporter, 0 == readCnt);
246 REPORTER_ASSERT(reporter, 0 == writeCnt);
247
Robert Phillips8a02f652017-05-12 14:49:16 -0400248 testingOnly_getIORefCnts(proxy3.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500249 REPORTER_ASSERT(reporter, 1 == refCnt);
250 REPORTER_ASSERT(reporter, 0 == readCnt);
251 REPORTER_ASSERT(reporter, 0 == writeCnt);
252
Robert Phillips8a02f652017-05-12 14:49:16 -0400253 testingOnly_getIORefCnts(proxy4.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500254 REPORTER_ASSERT(reporter, 1 == refCnt);
255 REPORTER_ASSERT(reporter, 0 == readCnt);
256 REPORTER_ASSERT(reporter, 0 == writeCnt);
257 }
258 }
259 }
260}
Brian Salomon587e08f2017-01-27 10:59:27 -0500261
262// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
263#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
264
Brian Salomon0e05a822017-07-25 09:43:22 -0400265#include "SkCommandLineFlags.h"
266DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?");
267
268#if GR_TEST_UTILS
269
270static GrColor input_texel_color(int i, int j) {
271 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 -0500272 return GrPremulColor(color);
273}
274
Brian Salomon0e05a822017-07-25 09:43:22 -0400275static GrColor4f input_texel_color4f(int i, int j) {
276 return GrColor4f::FromGrColor(input_texel_color(i, j));
277}
Brian Salomon587e08f2017-01-27 10:59:27 -0500278
Robert Phillips296b1cc2017-03-15 10:42:12 -0400279void test_draw_op(GrRenderTargetContext* rtc, sk_sp<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500280 sk_sp<GrTextureProxy> inputDataProxy) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500281 GrPaint paint;
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400282 paint.addColorTextureProcessor(std::move(inputDataProxy), nullptr, SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500283 paint.addColorFragmentProcessor(std::move(fp));
284 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400285
Brian Salomonbaaf4392017-06-15 09:59:23 -0400286 auto op = GrRectOpFactory::MakeNonAAFill(std::move(paint), SkMatrix::I(),
287 SkRect::MakeWH(rtc->width(), rtc->height()),
288 GrAAType::kNone);
Brian Salomonac70f842017-05-08 10:43:33 -0400289 rtc->addDrawOp(GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500290}
291
Brian Salomon0e05a822017-07-25 09:43:22 -0400292/** Initializes the two test texture proxies that are available to the FP test factories. */
293bool init_test_textures(GrContext* context, SkRandom* random, sk_sp<GrTextureProxy> proxies[2]) {
294 static const int kTestTextureSize = 256;
295 GrSurfaceDesc desc;
296 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
297 desc.fWidth = kTestTextureSize;
298 desc.fHeight = kTestTextureSize;
299 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400300
Brian Salomon0e05a822017-07-25 09:43:22 -0400301 // Put premul data into the RGBA texture that the test FPs can optionally use.
302 std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]);
303 for (int y = 0; y < kTestTextureSize; ++y) {
304 for (int x = 0; x < kTestTextureSize; ++x) {
305 rgbaData[kTestTextureSize * y + x] =
306 input_texel_color(random->nextULessThan(256), random->nextULessThan(256));
307 }
308 }
309 proxies[0] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
310 rgbaData.get(), kTestTextureSize * sizeof(GrColor));
311
312 // Put random values into the alpha texture that the test FPs can optionally use.
313 desc.fConfig = kAlpha_8_GrPixelConfig;
314 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]);
315 for (int y = 0; y < kTestTextureSize; ++y) {
316 for (int x = 0; x < kTestTextureSize; ++x) {
317 alphaData[kTestTextureSize * y + x] = random->nextULessThan(256);
318 }
319 }
320 proxies[1] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
321 alphaData.get(), kTestTextureSize);
322
323 return proxies[0] && proxies[1];
324}
325
326// Creates a texture of premul colors used as the output of the fragment processor that precedes
327// the fragment processor under test. Color values are those provided by input_texel_color().
328sk_sp<GrTextureProxy> make_input_texture(GrContext* context, int width, int height) {
329 std::unique_ptr<GrColor[]> data(new GrColor[width * height]);
330 for (int y = 0; y < width; ++y) {
331 for (int x = 0; x < height; ++x) {
332 data.get()[width * y + x] = input_texel_color(x, y);
333 }
334 }
335 GrSurfaceDesc desc;
336 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
337 desc.fWidth = width;
338 desc.fHeight = height;
339 desc.fConfig = kRGBA_8888_GrPixelConfig;
340 return GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
341 data.get(), width * sizeof(GrColor));
342}
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500343DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500344 GrContext* context = ctxInfo.grContext();
Brian Salomon1c053642017-07-24 10:16:19 -0400345 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400346
347 uint32_t seed = 0;
348 if (FLAGS_randomProcessorTest) {
349 std::random_device rd;
350 seed = rd();
351 }
352 // If a non-deterministic bot fails this test, check the output to see what seed it used, then
353 // hard-code that value here:
354 SkRandom random(seed);
355
Brian Salomon0e05a822017-07-25 09:43:22 -0400356 // Make the destination context for the test.
357 static constexpr int kRenderSize = 256;
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400358 sk_sp<GrRenderTargetContext> rtc = context->makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400359 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500360
Robert Phillipse78b7252017-04-06 07:59:41 -0400361 sk_sp<GrTextureProxy> proxies[2];
Brian Salomon0e05a822017-07-25 09:43:22 -0400362 if (!init_test_textures(context, &random, proxies)) {
363 ERRORF(reporter, "Could not create test textures");
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400364 return;
365 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400366 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500367
Brian Salomon0e05a822017-07-25 09:43:22 -0400368 auto inputTexture = make_input_texture(context, kRenderSize, kRenderSize);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500369
Ben Wagner8c791942017-07-25 11:47:48 -0400370 std::unique_ptr<GrColor[]> readData(new GrColor[kRenderSize * kRenderSize]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400371 // Because processor factories configure themselves in random ways, this is not exhaustive.
Brian Salomon587e08f2017-01-27 10:59:27 -0500372 for (int i = 0; i < FPFactory::Count(); ++i) {
373 int timesToInvokeFactory = 5;
374 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
375 // on child optimizations being present.
376 sk_sp<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
377 for (int j = 0; j < fp->numChildProcessors(); ++j) {
378 // This value made a reasonable trade off between time and coverage when this test was
379 // written.
380 timesToInvokeFactory *= FPFactory::Count() / 2;
381 }
382 for (int j = 0; j < timesToInvokeFactory; ++j) {
383 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillips9bee2e52017-05-29 12:37:20 -0400384 if (!fp->instantiate(context->resourceProvider())) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400385 continue;
386 }
387
Brian Salomon587e08f2017-01-27 10:59:27 -0500388 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500389 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500390 continue;
391 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400392 test_draw_op(rtc.get(), fp, inputTexture);
393 memset(readData.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
394 rtc->readPixels(SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
395 kPremul_SkAlphaType),
396 readData.get(), 0, 0, 0);
Brian Salomon587e08f2017-01-27 10:59:27 -0500397 bool passing = true;
398 if (0) { // Useful to see what FPs are being tested.
399 SkString children;
400 for (int c = 0; c < fp->numChildProcessors(); ++c) {
401 if (!c) {
402 children.append("(");
403 }
404 children.append(fp->childProcessor(c).name());
405 children.append(c == fp->numChildProcessors() - 1 ? ")" : ", ");
406 }
407 SkDebugf("%s %s\n", fp->name(), children.c_str());
408 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400409 for (int y = 0; y < kRenderSize && passing; ++y) {
410 for (int x = 0; x < kRenderSize && passing; ++x) {
411 GrColor input = input_texel_color(x, y);
412 GrColor output = readData.get()[y * kRenderSize + x];
Brian Salomonf3b995b2017-02-15 10:22:23 -0500413 if (fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500414 // A modulating processor is allowed to modulate either the input color or
415 // just the input alpha.
416 bool legalColorModulation =
417 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
418 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
419 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
420 GrColorUnpackB(output) <= GrColorUnpackB(input);
421 bool legalAlphaModulation =
422 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
423 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
424 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
425 GrColorUnpackB(output) <= GrColorUnpackA(input);
426 if (!legalColorModulation && !legalAlphaModulation) {
427 ERRORF(reporter,
428 "\"Modulating\" processor %s made color/alpha value larger. "
Brian Osman2f3865b2017-03-15 13:35:51 -0400429 "Input: 0x%08x, Output: 0x%08x.",
Brian Salomon587e08f2017-01-27 10:59:27 -0500430 fp->name(), input, output);
431 passing = false;
432 }
433 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400434 GrColor4f input4f = input_texel_color4f(x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500435 GrColor4f output4f = GrColor4f::FromGrColor(output);
436 GrColor4f expected4f;
437 if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) {
438 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
439 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
440 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
441 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500442 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500443 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
444 ERRORF(reporter,
445 "Processor %s claimed output for const input doesn't match "
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500446 "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, "
447 "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)",
448 fp->name(), SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))),
449 kTol, input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2],
450 input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1],
451 output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0],
452 expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]);
Brian Salomon587e08f2017-01-27 10:59:27 -0500453 passing = false;
454 }
455 }
456 if (GrColorIsOpaque(input) && fp->preservesOpaqueInput() &&
457 !GrColorIsOpaque(output)) {
458 ERRORF(reporter,
459 "Processor %s claimed opaqueness is preserved but it is not. Input: "
Brian Osman2f3865b2017-03-15 13:35:51 -0400460 "0x%08x, Output: 0x%08x.",
Brian Salomon587e08f2017-01-27 10:59:27 -0500461 fp->name(), input, output);
462 passing = false;
463 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400464 if (!passing) {
Brian Osmanc35a2d42017-03-17 10:58:53 -0400465 ERRORF(reporter, "Seed: 0x%08x, Processor details: %s",
466 seed, fp->dumpInfo().c_str());
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400467 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500468 }
469 }
470 }
471 }
472}
Robert Phillips18166ee2017-06-01 12:55:44 -0400473
Brian Salomon0e05a822017-07-25 09:43:22 -0400474// Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their
475// progenitors.
476DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
477 GrContext* context = ctxInfo.grContext();
478
479 SkRandom random;
480
481 // Make the destination context for the test.
482 static constexpr int kRenderSize = 1024;
483 sk_sp<GrRenderTargetContext> rtc = context->makeDeferredRenderTargetContext(
484 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
485
486 sk_sp<GrTextureProxy> proxies[2];
487 if (!init_test_textures(context, &random, proxies)) {
488 ERRORF(reporter, "Could not create test textures");
489 return;
490 }
491 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
492
493 auto inputTexture = make_input_texture(context, kRenderSize, kRenderSize);
494 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
495 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
496 auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
497 kPremul_SkAlphaType);
498
499 // Because processor factories configure themselves in random ways, this is not exhaustive.
500 for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
501 static constexpr int kTimesToInvokeFactory = 10;
502 for (int j = 0; j < kTimesToInvokeFactory; ++j) {
503 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
504 auto clone = fp->clone();
505 // Currently clone() is allowed to fail.
506 if (!clone) {
507 continue;
508 }
509 if (!fp->instantiate(context->resourceProvider()) ||
510 !clone->instantiate(context->resourceProvider())) {
511 continue;
512 }
513 // Draw with original and read back the results.
514 test_draw_op(rtc.get(), fp, inputTexture);
515 memset(readData1.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
516 rtc->readPixels(readInfo, readData1.get(), 0, 0, 0);
517
518 // Draw with clone and read back the results.
519 test_draw_op(rtc.get(), clone, inputTexture);
520 memset(readData2.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
521 rtc->readPixels(readInfo, readData2.get(), 0, 0, 0);
522
523 // Check that the results are the same.
524 bool passing = true;
525 for (int y = 0; y < kRenderSize && passing; ++y) {
526 for (int x = 0; x < kRenderSize && passing; ++x) {
527 int idx = y * kRenderSize + x;
528 if (readData1[idx] != readData2[idx]) {
529 ERRORF(reporter,
530 "Processor %s made clone produced different output. "
531 "Input color: 0x%08x, Original Output Color: 0x%08x, "
532 "Clone Output Color: 0x%08x..",
533 fp->name(), input_texel_color(x, y), readData1[idx], readData2[idx]);
534 passing = false;
535 }
536 }
537 }
538 }
539 }
540}
541
Hal Canary6f6961e2017-01-31 13:50:44 -0500542#endif // GR_TEST_UTILS
543#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
544#endif // SK_SUPPORT_GPU