blob: c9eb1f38a0ffbc91262ee1a9808078ebbb2fa2b8 [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
148 GrTextureDesc desc;
149 desc.fConfig = kRGBA_8888_GrPixelConfig;
150 desc.fWidth = 10;
151 desc.fHeight = 10;
152
153 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400154 sk_sp<GrRenderTargetContext> renderTargetContext(context->makeDeferredRenderTargetContext(
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500155 SkBackingFit::kApprox, 1, 1, kRGBA_8888_GrPixelConfig, nullptr));
156 {
157 bool texelBufferSupport = context->caps()->shaderCaps()->texelBufferSupport();
158 bool imageLoadStoreSupport = context->caps()->shaderCaps()->imageLoadStoreSupport();
Brian Osman32342f02017-03-04 08:12:46 -0500159 sk_sp<GrTextureProxy> proxy1(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
Robert Phillips8a02f652017-05-12 14:49:16 -0400160 desc, SkBackingFit::kExact,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500161 SkBudgeted::kYes));
Robert Phillips8a02f652017-05-12 14:49:16 -0400162 sk_sp<GrTextureProxy> proxy2(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
163 desc, SkBackingFit::kExact,
164 SkBudgeted::kYes));
165 sk_sp<GrTextureProxy> proxy3(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
166 desc, SkBackingFit::kExact,
167 SkBudgeted::kYes));
168 sk_sp<GrTextureProxy> proxy4(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
169 desc, SkBackingFit::kExact,
170 SkBudgeted::kYes));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500171 sk_sp<GrBuffer> buffer(texelBufferSupport
172 ? context->resourceProvider()->createBuffer(
173 1024, GrBufferType::kTexel_GrBufferType,
174 GrAccessPattern::kStatic_GrAccessPattern, 0)
175 : nullptr);
176 {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500177 SkTArray<sk_sp<GrTextureProxy>> proxies;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500178 SkTArray<sk_sp<GrBuffer>> buffers;
179 SkTArray<TestFP::Image> images;
Robert Phillips2f493142017-03-02 18:18:38 -0500180 proxies.push_back(proxy1);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500181 if (texelBufferSupport) {
182 buffers.push_back(buffer);
183 }
184 if (imageLoadStoreSupport) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400185 images.emplace_back(proxy2, GrIOType::kRead_GrIOType);
186 images.emplace_back(proxy3, GrIOType::kWrite_GrIOType);
187 images.emplace_back(proxy4, GrIOType::kRW_GrIOType);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500188 }
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400189 auto fp = TestFP::Make(std::move(proxies), std::move(buffers), std::move(images));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500190 for (int i = 0; i < parentCnt; ++i) {
191 fp = TestFP::Make(std::move(fp));
192 }
Brian Salomon82ddc942017-07-14 12:00:13 -0400193 std::unique_ptr<GrDrawOp> op(TestOp::Make(std::move(fp)));
194 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500195 }
196 int refCnt, readCnt, writeCnt;
197
Robert Phillips30f9bc62017-02-22 15:28:38 -0500198 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500199 REPORTER_ASSERT(reporter, 1 == refCnt);
200 REPORTER_ASSERT(reporter, 1 == readCnt);
201 REPORTER_ASSERT(reporter, 0 == writeCnt);
202
203 if (texelBufferSupport) {
Brian Salomonf046e152017-01-11 15:24:47 -0500204 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500205 REPORTER_ASSERT(reporter, 1 == refCnt);
206 REPORTER_ASSERT(reporter, 1 == readCnt);
207 REPORTER_ASSERT(reporter, 0 == writeCnt);
208 }
209
210 if (imageLoadStoreSupport) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400211 testingOnly_getIORefCnts(proxy2.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500212 REPORTER_ASSERT(reporter, 1 == refCnt);
213 REPORTER_ASSERT(reporter, 1 == readCnt);
214 REPORTER_ASSERT(reporter, 0 == writeCnt);
215
Robert Phillips8a02f652017-05-12 14:49:16 -0400216 testingOnly_getIORefCnts(proxy3.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500217 REPORTER_ASSERT(reporter, 1 == refCnt);
218 REPORTER_ASSERT(reporter, 0 == readCnt);
219 REPORTER_ASSERT(reporter, 1 == writeCnt);
220
Robert Phillips8a02f652017-05-12 14:49:16 -0400221 testingOnly_getIORefCnts(proxy4.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500222 REPORTER_ASSERT(reporter, 1 == refCnt);
223 REPORTER_ASSERT(reporter, 1 == readCnt);
224 REPORTER_ASSERT(reporter, 1 == writeCnt);
225 }
226
227 context->flush();
228
Robert Phillips30f9bc62017-02-22 15:28:38 -0500229 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500230 REPORTER_ASSERT(reporter, 1 == refCnt);
231 REPORTER_ASSERT(reporter, 0 == readCnt);
232 REPORTER_ASSERT(reporter, 0 == writeCnt);
233
234 if (texelBufferSupport) {
Brian Salomonf046e152017-01-11 15:24:47 -0500235 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500236 REPORTER_ASSERT(reporter, 1 == refCnt);
237 REPORTER_ASSERT(reporter, 0 == readCnt);
238 REPORTER_ASSERT(reporter, 0 == writeCnt);
239 }
240
241 if (texelBufferSupport) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400242 testingOnly_getIORefCnts(proxy2.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500243 REPORTER_ASSERT(reporter, 1 == refCnt);
244 REPORTER_ASSERT(reporter, 0 == readCnt);
245 REPORTER_ASSERT(reporter, 0 == writeCnt);
246
Robert Phillips8a02f652017-05-12 14:49:16 -0400247 testingOnly_getIORefCnts(proxy3.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500248 REPORTER_ASSERT(reporter, 1 == refCnt);
249 REPORTER_ASSERT(reporter, 0 == readCnt);
250 REPORTER_ASSERT(reporter, 0 == writeCnt);
251
Robert Phillips8a02f652017-05-12 14:49:16 -0400252 testingOnly_getIORefCnts(proxy4.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500253 REPORTER_ASSERT(reporter, 1 == refCnt);
254 REPORTER_ASSERT(reporter, 0 == readCnt);
255 REPORTER_ASSERT(reporter, 0 == writeCnt);
256 }
257 }
258 }
259}
Brian Salomon587e08f2017-01-27 10:59:27 -0500260
261// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
262#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
263
264static GrColor texel_color(int i, int j) {
265 SkASSERT((unsigned)i < 256 && (unsigned)j < 256);
266 GrColor color = GrColorPackRGBA(j, (uint8_t)(i + j), (uint8_t)(2 * j - i), i);
267 return GrPremulColor(color);
268}
269
270static GrColor4f texel_color4f(int i, int j) { return GrColor4f::FromGrColor(texel_color(i, j)); }
271
Robert Phillips296b1cc2017-03-15 10:42:12 -0400272void test_draw_op(GrRenderTargetContext* rtc, sk_sp<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500273 sk_sp<GrTextureProxy> inputDataProxy) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500274 GrPaint paint;
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400275 paint.addColorTextureProcessor(std::move(inputDataProxy), nullptr, SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500276 paint.addColorFragmentProcessor(std::move(fp));
277 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400278
Brian Salomonbaaf4392017-06-15 09:59:23 -0400279 auto op = GrRectOpFactory::MakeNonAAFill(std::move(paint), SkMatrix::I(),
280 SkRect::MakeWH(rtc->width(), rtc->height()),
281 GrAAType::kNone);
Brian Salomonac70f842017-05-08 10:43:33 -0400282 rtc->addDrawOp(GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500283}
284
Brian Osmanc35a2d42017-03-17 10:58:53 -0400285#include "SkCommandLineFlags.h"
286DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?");
287
Hal Canary6f6961e2017-01-31 13:50:44 -0500288#if GR_TEST_UTILS
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500289DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500290 GrContext* context = ctxInfo.grContext();
Brian Salomon1c053642017-07-24 10:16:19 -0400291 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400292
293 uint32_t seed = 0;
294 if (FLAGS_randomProcessorTest) {
295 std::random_device rd;
296 seed = rd();
297 }
298 // If a non-deterministic bot fails this test, check the output to see what seed it used, then
299 // hard-code that value here:
300 SkRandom random(seed);
301
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400302 sk_sp<GrRenderTargetContext> rtc = context->makeDeferredRenderTargetContext(
Brian Salomon587e08f2017-01-27 10:59:27 -0500303 SkBackingFit::kExact, 256, 256, kRGBA_8888_GrPixelConfig, nullptr);
304 GrSurfaceDesc desc;
Robert Phillipse44ef102017-07-21 15:37:19 -0400305 desc.fFlags = kRenderTarget_GrSurfaceFlag;
306 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
Brian Salomon587e08f2017-01-27 10:59:27 -0500307 desc.fWidth = 256;
308 desc.fHeight = 256;
Brian Salomon587e08f2017-01-27 10:59:27 -0500309 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500310
Robert Phillipse78b7252017-04-06 07:59:41 -0400311 sk_sp<GrTextureProxy> proxies[2];
312
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500313 // Put premul data into the RGBA texture that the test FPs can optionally use.
314 std::unique_ptr<GrColor[]> rgbaData(new GrColor[256 * 256]);
315 for (int y = 0; y < 256; ++y) {
316 for (int x = 0; x < 256; ++x) {
317 rgbaData.get()[256 * y + x] =
318 texel_color(random.nextULessThan(256), random.nextULessThan(256));
319 }
320 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400321 proxies[0] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
322 rgbaData.get(), 256 * sizeof(GrColor));
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500323
324 // Put random values into the alpha texture that the test FPs can optionally use.
Brian Salomon587e08f2017-01-27 10:59:27 -0500325 desc.fConfig = kAlpha_8_GrPixelConfig;
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500326 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[256 * 256]);
327 for (int y = 0; y < 256; ++y) {
328 for (int x = 0; x < 256; ++x) {
329 alphaData.get()[256 * y + x] = random.nextULessThan(256);
330 }
331 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400332 proxies[1] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
333 alphaData.get(), 256);
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400334
335 if (!proxies[0] || !proxies[1]) {
336 return;
337 }
338
Robert Phillipse78b7252017-04-06 07:59:41 -0400339 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500340
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500341 // Use a different array of premul colors for the output of the fragment processor that preceeds
342 // the fragment processor under test.
Brian Salomon587e08f2017-01-27 10:59:27 -0500343 for (int y = 0; y < 256; ++y) {
344 for (int x = 0; x < 256; ++x) {
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500345 rgbaData.get()[256 * y + x] = texel_color(x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500346 }
347 }
348 desc.fConfig = kRGBA_8888_GrPixelConfig;
Robert Phillips30f9bc62017-02-22 15:28:38 -0500349
Robert Phillips26c90e02017-03-14 14:39:29 -0400350 sk_sp<GrTextureProxy> dataProxy = GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
Robert Phillips30f9bc62017-02-22 15:28:38 -0500351 desc, SkBudgeted::kYes,
352 rgbaData.get(),
353 256 * sizeof(GrColor));
Brian Salomon587e08f2017-01-27 10:59:27 -0500354
355 // Because processors factories configure themselves in random ways, this is not exhaustive.
356 for (int i = 0; i < FPFactory::Count(); ++i) {
357 int timesToInvokeFactory = 5;
358 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
359 // on child optimizations being present.
360 sk_sp<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
361 for (int j = 0; j < fp->numChildProcessors(); ++j) {
362 // This value made a reasonable trade off between time and coverage when this test was
363 // written.
364 timesToInvokeFactory *= FPFactory::Count() / 2;
365 }
366 for (int j = 0; j < timesToInvokeFactory; ++j) {
367 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillips9bee2e52017-05-29 12:37:20 -0400368 if (!fp->instantiate(context->resourceProvider())) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400369 continue;
370 }
371
Brian Salomon587e08f2017-01-27 10:59:27 -0500372 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500373 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500374 continue;
375 }
Robert Phillips296b1cc2017-03-15 10:42:12 -0400376 test_draw_op(rtc.get(), fp, dataProxy);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500377 memset(rgbaData.get(), 0x0, sizeof(GrColor) * 256 * 256);
Brian Salomon587e08f2017-01-27 10:59:27 -0500378 rtc->readPixels(
379 SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500380 rgbaData.get(), 0, 0, 0);
Brian Salomon587e08f2017-01-27 10:59:27 -0500381 bool passing = true;
382 if (0) { // Useful to see what FPs are being tested.
383 SkString children;
384 for (int c = 0; c < fp->numChildProcessors(); ++c) {
385 if (!c) {
386 children.append("(");
387 }
388 children.append(fp->childProcessor(c).name());
389 children.append(c == fp->numChildProcessors() - 1 ? ")" : ", ");
390 }
391 SkDebugf("%s %s\n", fp->name(), children.c_str());
392 }
393 for (int y = 0; y < 256 && passing; ++y) {
394 for (int x = 0; x < 256 && passing; ++x) {
395 GrColor input = texel_color(x, y);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500396 GrColor output = rgbaData.get()[y * 256 + x];
Brian Salomonf3b995b2017-02-15 10:22:23 -0500397 if (fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500398 // A modulating processor is allowed to modulate either the input color or
399 // just the input alpha.
400 bool legalColorModulation =
401 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
402 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
403 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
404 GrColorUnpackB(output) <= GrColorUnpackB(input);
405 bool legalAlphaModulation =
406 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
407 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
408 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
409 GrColorUnpackB(output) <= GrColorUnpackA(input);
410 if (!legalColorModulation && !legalAlphaModulation) {
411 ERRORF(reporter,
412 "\"Modulating\" processor %s made color/alpha value larger. "
Brian Osman2f3865b2017-03-15 13:35:51 -0400413 "Input: 0x%08x, Output: 0x%08x.",
Brian Salomon587e08f2017-01-27 10:59:27 -0500414 fp->name(), input, output);
415 passing = false;
416 }
417 }
418 GrColor4f input4f = texel_color4f(x, y);
419 GrColor4f output4f = GrColor4f::FromGrColor(output);
420 GrColor4f expected4f;
421 if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) {
422 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
423 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
424 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
425 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500426 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500427 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
428 ERRORF(reporter,
429 "Processor %s claimed output for const input doesn't match "
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500430 "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, "
431 "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)",
432 fp->name(), SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))),
433 kTol, input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2],
434 input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1],
435 output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0],
436 expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]);
Brian Salomon587e08f2017-01-27 10:59:27 -0500437 passing = false;
438 }
439 }
440 if (GrColorIsOpaque(input) && fp->preservesOpaqueInput() &&
441 !GrColorIsOpaque(output)) {
442 ERRORF(reporter,
443 "Processor %s claimed opaqueness is preserved but it is not. Input: "
Brian Osman2f3865b2017-03-15 13:35:51 -0400444 "0x%08x, Output: 0x%08x.",
Brian Salomon587e08f2017-01-27 10:59:27 -0500445 fp->name(), input, output);
446 passing = false;
447 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400448 if (!passing) {
Brian Osmanc35a2d42017-03-17 10:58:53 -0400449 ERRORF(reporter, "Seed: 0x%08x, Processor details: %s",
450 seed, fp->dumpInfo().c_str());
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400451 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500452 }
453 }
454 }
455 }
456}
Robert Phillips18166ee2017-06-01 12:55:44 -0400457
Hal Canary6f6961e2017-01-31 13:50:44 -0500458#endif // GR_TEST_UTILS
459#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
460#endif // SK_SUPPORT_GPU