blob: 41bb183884afc702440157f98fabd1dcbc2cc237 [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"
Brian Salomon5d4cd9e2017-02-09 11:16:46 -050016#include "GrPipelineBuilder.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050017#include "GrRenderTargetContext.h"
18#include "GrRenderTargetContextPriv.h"
19#include "GrResourceProvider.h"
20#include "glsl/GrGLSLFragmentProcessor.h"
21#include "glsl/GrGLSLFragmentShaderBuilder.h"
Brian Salomon5d4cd9e2017-02-09 11:16:46 -050022#include "ops/GrNonAAFillRectOp.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050023#include "ops/GrTestMeshDrawOp.h"
24
25namespace {
26class TestOp : public GrTestMeshDrawOp {
27public:
28 DEFINE_OP_CLASS_ID
29 const char* name() const override { return "TestOp"; }
30
Brian Salomond3ccb0a2017-04-03 10:38:00 -040031 static std::unique_ptr<GrLegacyMeshDrawOp> Make() {
32 return std::unique_ptr<GrLegacyMeshDrawOp>(new TestOp);
Brian Salomon649a3412017-03-09 13:50:43 -050033 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050034
35private:
36 TestOp() : INHERITED(ClassID(), SkRect::MakeWH(100, 100), 0xFFFFFFFF) {}
37
38 void onPrepareDraws(Target* target) const override { return; }
39
40 typedef GrTestMeshDrawOp INHERITED;
41};
42
43/**
44 * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts
45 * of resources owned by child FPs.
46 */
47class TestFP : public GrFragmentProcessor {
48public:
49 struct Image {
Robert Phillips8a02f652017-05-12 14:49:16 -040050 Image(sk_sp<GrTextureProxy> proxy, GrIOType ioType) : fProxy(proxy), fIOType(ioType) {}
51 sk_sp<GrTextureProxy> fProxy;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050052 GrIOType fIOType;
53 };
54 static sk_sp<GrFragmentProcessor> Make(sk_sp<GrFragmentProcessor> child) {
55 return sk_sp<GrFragmentProcessor>(new TestFP(std::move(child)));
56 }
Robert Phillips30f9bc62017-02-22 15:28:38 -050057 static sk_sp<GrFragmentProcessor> Make(GrContext* context,
58 const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Brian Salomonbc6b99d2017-01-11 10:32:34 -050059 const SkTArray<sk_sp<GrBuffer>>& buffers,
60 const SkTArray<Image>& images) {
Robert Phillips30f9bc62017-02-22 15:28:38 -050061 return sk_sp<GrFragmentProcessor>(new TestFP(context, proxies, buffers, images));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050062 }
63
64 const char* name() const override { return "test"; }
65
66 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
67 // We don't really care about reusing these.
68 static int32_t gKey = 0;
69 b->add32(sk_atomic_inc(&gKey));
70 }
71
Brian Salomonbc6b99d2017-01-11 10:32:34 -050072private:
Robert Phillips30f9bc62017-02-22 15:28:38 -050073 TestFP(GrContext* context,
74 const SkTArray<sk_sp<GrTextureProxy>>& proxies,
75 const SkTArray<sk_sp<GrBuffer>>& buffers,
Brian Salomonbc6b99d2017-01-11 10:32:34 -050076 const SkTArray<Image>& images)
Brian Salomon587e08f2017-01-27 10:59:27 -050077 : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) {
Robert Phillips30f9bc62017-02-22 15:28:38 -050078 for (const auto& proxy : proxies) {
Brian Osman32342f02017-03-04 08:12:46 -050079 this->addTextureSampler(&fSamplers.emplace_back(context->resourceProvider(), proxy));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050080 }
81 for (const auto& buffer : buffers) {
82 this->addBufferAccess(&fBuffers.emplace_back(kRGBA_8888_GrPixelConfig, buffer.get()));
83 }
84 for (const Image& image : images) {
Robert Phillips8a02f652017-05-12 14:49:16 -040085 fImages.emplace_back(image.fProxy, image.fIOType,
86 GrSLMemoryModel::kNone, GrSLRestrict::kNo);
87 this->addImageStorageAccess(context->resourceProvider(), &fImages.back());
Brian Salomonbc6b99d2017-01-11 10:32:34 -050088 }
89 }
90
Brian Salomon587e08f2017-01-27 10:59:27 -050091 TestFP(sk_sp<GrFragmentProcessor> child)
92 : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) {
Brian Salomonbc6b99d2017-01-11 10:32:34 -050093 this->registerChildProcessor(std::move(child));
94 }
95
96 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
97 class TestGLSLFP : public GrGLSLFragmentProcessor {
98 public:
99 TestGLSLFP() {}
100 void emitCode(EmitArgs& args) override {
101 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
102 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
103 }
104
105 private:
106 };
107 return new TestGLSLFP();
108 }
109
110 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
111
112 GrTAllocator<TextureSampler> fSamplers;
113 GrTAllocator<BufferAccess> fBuffers;
114 GrTAllocator<ImageStorageAccess> fImages;
Brian Salomon587e08f2017-01-27 10:59:27 -0500115 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500116};
117}
118
Brian Salomonf046e152017-01-11 15:24:47 -0500119template <typename T>
120inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) {
121 *refCnt = resource->fRefCnt;
122 *readCnt = resource->fPendingReads;
123 *writeCnt = resource->fPendingWrites;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500124}
125
Robert Phillips2f493142017-03-02 18:18:38 -0500126void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500127 *refCnt = proxy->getBackingRefCnt_TestOnly();
128 *readCnt = proxy->getPendingReadCnt_TestOnly();
129 *writeCnt = proxy->getPendingWriteCnt_TestOnly();
130}
131
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500132DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
133 GrContext* context = ctxInfo.grContext();
134
135 GrTextureDesc desc;
136 desc.fConfig = kRGBA_8888_GrPixelConfig;
137 desc.fWidth = 10;
138 desc.fHeight = 10;
139
140 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400141 sk_sp<GrRenderTargetContext> renderTargetContext(context->makeDeferredRenderTargetContext(
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500142 SkBackingFit::kApprox, 1, 1, kRGBA_8888_GrPixelConfig, nullptr));
143 {
144 bool texelBufferSupport = context->caps()->shaderCaps()->texelBufferSupport();
145 bool imageLoadStoreSupport = context->caps()->shaderCaps()->imageLoadStoreSupport();
Brian Osman32342f02017-03-04 08:12:46 -0500146 sk_sp<GrTextureProxy> proxy1(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
Robert Phillips8a02f652017-05-12 14:49:16 -0400147 desc, SkBackingFit::kExact,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500148 SkBudgeted::kYes));
Robert Phillips8a02f652017-05-12 14:49:16 -0400149 sk_sp<GrTextureProxy> proxy2(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
150 desc, SkBackingFit::kExact,
151 SkBudgeted::kYes));
152 sk_sp<GrTextureProxy> proxy3(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
153 desc, SkBackingFit::kExact,
154 SkBudgeted::kYes));
155 sk_sp<GrTextureProxy> proxy4(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
156 desc, SkBackingFit::kExact,
157 SkBudgeted::kYes));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500158 sk_sp<GrBuffer> buffer(texelBufferSupport
159 ? context->resourceProvider()->createBuffer(
160 1024, GrBufferType::kTexel_GrBufferType,
161 GrAccessPattern::kStatic_GrAccessPattern, 0)
162 : nullptr);
163 {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500164 SkTArray<sk_sp<GrTextureProxy>> proxies;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500165 SkTArray<sk_sp<GrBuffer>> buffers;
166 SkTArray<TestFP::Image> images;
Robert Phillips2f493142017-03-02 18:18:38 -0500167 proxies.push_back(proxy1);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500168 if (texelBufferSupport) {
169 buffers.push_back(buffer);
170 }
171 if (imageLoadStoreSupport) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400172 images.emplace_back(proxy2, GrIOType::kRead_GrIOType);
173 images.emplace_back(proxy3, GrIOType::kWrite_GrIOType);
174 images.emplace_back(proxy4, GrIOType::kRW_GrIOType);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500175 }
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400176 std::unique_ptr<GrLegacyMeshDrawOp> op(TestOp::Make());
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500177 GrPaint paint;
Robert Phillips30f9bc62017-02-22 15:28:38 -0500178 auto fp = TestFP::Make(context,
179 std::move(proxies), std::move(buffers), std::move(images));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500180 for (int i = 0; i < parentCnt; ++i) {
181 fp = TestFP::Make(std::move(fp));
182 }
183 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400184 renderTargetContext->priv().testingOnly_addLegacyMeshDrawOp(
Brian Salomon649a3412017-03-09 13:50:43 -0500185 std::move(paint), GrAAType::kNone, std::move(op));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500186 }
187 int refCnt, readCnt, writeCnt;
188
Robert Phillips30f9bc62017-02-22 15:28:38 -0500189 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500190 REPORTER_ASSERT(reporter, 1 == refCnt);
191 REPORTER_ASSERT(reporter, 1 == readCnt);
192 REPORTER_ASSERT(reporter, 0 == writeCnt);
193
194 if (texelBufferSupport) {
Brian Salomonf046e152017-01-11 15:24:47 -0500195 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500196 REPORTER_ASSERT(reporter, 1 == refCnt);
197 REPORTER_ASSERT(reporter, 1 == readCnt);
198 REPORTER_ASSERT(reporter, 0 == writeCnt);
199 }
200
201 if (imageLoadStoreSupport) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400202 testingOnly_getIORefCnts(proxy2.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500203 REPORTER_ASSERT(reporter, 1 == refCnt);
204 REPORTER_ASSERT(reporter, 1 == readCnt);
205 REPORTER_ASSERT(reporter, 0 == writeCnt);
206
Robert Phillips8a02f652017-05-12 14:49:16 -0400207 testingOnly_getIORefCnts(proxy3.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500208 REPORTER_ASSERT(reporter, 1 == refCnt);
209 REPORTER_ASSERT(reporter, 0 == readCnt);
210 REPORTER_ASSERT(reporter, 1 == writeCnt);
211
Robert Phillips8a02f652017-05-12 14:49:16 -0400212 testingOnly_getIORefCnts(proxy4.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, 1 == writeCnt);
216 }
217
218 context->flush();
219
Robert Phillips30f9bc62017-02-22 15:28:38 -0500220 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500221 REPORTER_ASSERT(reporter, 1 == refCnt);
222 REPORTER_ASSERT(reporter, 0 == readCnt);
223 REPORTER_ASSERT(reporter, 0 == writeCnt);
224
225 if (texelBufferSupport) {
Brian Salomonf046e152017-01-11 15:24:47 -0500226 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500227 REPORTER_ASSERT(reporter, 1 == refCnt);
228 REPORTER_ASSERT(reporter, 0 == readCnt);
229 REPORTER_ASSERT(reporter, 0 == writeCnt);
230 }
231
232 if (texelBufferSupport) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400233 testingOnly_getIORefCnts(proxy2.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500234 REPORTER_ASSERT(reporter, 1 == refCnt);
235 REPORTER_ASSERT(reporter, 0 == readCnt);
236 REPORTER_ASSERT(reporter, 0 == writeCnt);
237
Robert Phillips8a02f652017-05-12 14:49:16 -0400238 testingOnly_getIORefCnts(proxy3.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500239 REPORTER_ASSERT(reporter, 1 == refCnt);
240 REPORTER_ASSERT(reporter, 0 == readCnt);
241 REPORTER_ASSERT(reporter, 0 == writeCnt);
242
Robert Phillips8a02f652017-05-12 14:49:16 -0400243 testingOnly_getIORefCnts(proxy4.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 }
248 }
249 }
250}
Brian Salomon587e08f2017-01-27 10:59:27 -0500251
252// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
253#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
254
255static GrColor texel_color(int i, int j) {
256 SkASSERT((unsigned)i < 256 && (unsigned)j < 256);
257 GrColor color = GrColorPackRGBA(j, (uint8_t)(i + j), (uint8_t)(2 * j - i), i);
258 return GrPremulColor(color);
259}
260
261static GrColor4f texel_color4f(int i, int j) { return GrColor4f::FromGrColor(texel_color(i, j)); }
262
Robert Phillips296b1cc2017-03-15 10:42:12 -0400263void test_draw_op(GrRenderTargetContext* rtc, sk_sp<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500264 sk_sp<GrTextureProxy> inputDataProxy) {
Robert Phillips296b1cc2017-03-15 10:42:12 -0400265 GrResourceProvider* resourceProvider = rtc->resourceProvider();
266
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500267 GrPaint paint;
Robert Phillips296b1cc2017-03-15 10:42:12 -0400268 paint.addColorTextureProcessor(resourceProvider, std::move(inputDataProxy),
269 nullptr, SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500270 paint.addColorFragmentProcessor(std::move(fp));
271 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400272
273 auto op = GrNonAAFillRectOp::Make(std::move(paint), SkMatrix::I(),
274 SkRect::MakeWH(rtc->width(), rtc->height()), nullptr, nullptr,
275 GrAAType::kNone);
276 rtc->addDrawOp(GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500277}
278
Brian Osmanc35a2d42017-03-17 10:58:53 -0400279#include "SkCommandLineFlags.h"
280DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?");
281
Hal Canary6f6961e2017-01-31 13:50:44 -0500282#if GR_TEST_UTILS
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500283DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500284 GrContext* context = ctxInfo.grContext();
285 using FPFactory = GrProcessorTestFactory<GrFragmentProcessor>;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400286
287 uint32_t seed = 0;
288 if (FLAGS_randomProcessorTest) {
289 std::random_device rd;
290 seed = rd();
291 }
292 // If a non-deterministic bot fails this test, check the output to see what seed it used, then
293 // hard-code that value here:
294 SkRandom random(seed);
295
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400296 sk_sp<GrRenderTargetContext> rtc = context->makeDeferredRenderTargetContext(
Brian Salomon587e08f2017-01-27 10:59:27 -0500297 SkBackingFit::kExact, 256, 256, kRGBA_8888_GrPixelConfig, nullptr);
298 GrSurfaceDesc desc;
299 desc.fWidth = 256;
300 desc.fHeight = 256;
301 desc.fFlags = kRenderTarget_GrSurfaceFlag;
302 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500303
Robert Phillipse78b7252017-04-06 07:59:41 -0400304 sk_sp<GrTextureProxy> proxies[2];
305
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500306 // Put premul data into the RGBA texture that the test FPs can optionally use.
307 std::unique_ptr<GrColor[]> rgbaData(new GrColor[256 * 256]);
308 for (int y = 0; y < 256; ++y) {
309 for (int x = 0; x < 256; ++x) {
310 rgbaData.get()[256 * y + x] =
311 texel_color(random.nextULessThan(256), random.nextULessThan(256));
312 }
313 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400314 proxies[0] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
315 rgbaData.get(), 256 * sizeof(GrColor));
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500316
317 // Put random values into the alpha texture that the test FPs can optionally use.
Brian Salomon587e08f2017-01-27 10:59:27 -0500318 desc.fConfig = kAlpha_8_GrPixelConfig;
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500319 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[256 * 256]);
320 for (int y = 0; y < 256; ++y) {
321 for (int x = 0; x < 256; ++x) {
322 alphaData.get()[256 * y + x] = random.nextULessThan(256);
323 }
324 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400325 proxies[1] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
326 alphaData.get(), 256);
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400327
328 if (!proxies[0] || !proxies[1]) {
329 return;
330 }
331
Robert Phillipse78b7252017-04-06 07:59:41 -0400332 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500333
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500334 // Use a different array of premul colors for the output of the fragment processor that preceeds
335 // the fragment processor under test.
Brian Salomon587e08f2017-01-27 10:59:27 -0500336 for (int y = 0; y < 256; ++y) {
337 for (int x = 0; x < 256; ++x) {
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500338 rgbaData.get()[256 * y + x] = texel_color(x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500339 }
340 }
341 desc.fConfig = kRGBA_8888_GrPixelConfig;
Robert Phillips30f9bc62017-02-22 15:28:38 -0500342
Robert Phillips26c90e02017-03-14 14:39:29 -0400343 sk_sp<GrTextureProxy> dataProxy = GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
Robert Phillips30f9bc62017-02-22 15:28:38 -0500344 desc, SkBudgeted::kYes,
345 rgbaData.get(),
346 256 * sizeof(GrColor));
Brian Salomon587e08f2017-01-27 10:59:27 -0500347
348 // Because processors factories configure themselves in random ways, this is not exhaustive.
349 for (int i = 0; i < FPFactory::Count(); ++i) {
350 int timesToInvokeFactory = 5;
351 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
352 // on child optimizations being present.
353 sk_sp<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
354 for (int j = 0; j < fp->numChildProcessors(); ++j) {
355 // This value made a reasonable trade off between time and coverage when this test was
356 // written.
357 timesToInvokeFactory *= FPFactory::Count() / 2;
358 }
359 for (int j = 0; j < timesToInvokeFactory; ++j) {
360 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillips9bee2e52017-05-29 12:37:20 -0400361 if (!fp->instantiate(context->resourceProvider())) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400362 continue;
363 }
364
Brian Salomon587e08f2017-01-27 10:59:27 -0500365 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500366 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500367 continue;
368 }
Robert Phillips296b1cc2017-03-15 10:42:12 -0400369 test_draw_op(rtc.get(), fp, dataProxy);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500370 memset(rgbaData.get(), 0x0, sizeof(GrColor) * 256 * 256);
Brian Salomon587e08f2017-01-27 10:59:27 -0500371 rtc->readPixels(
372 SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500373 rgbaData.get(), 0, 0, 0);
Brian Salomon587e08f2017-01-27 10:59:27 -0500374 bool passing = true;
375 if (0) { // Useful to see what FPs are being tested.
376 SkString children;
377 for (int c = 0; c < fp->numChildProcessors(); ++c) {
378 if (!c) {
379 children.append("(");
380 }
381 children.append(fp->childProcessor(c).name());
382 children.append(c == fp->numChildProcessors() - 1 ? ")" : ", ");
383 }
384 SkDebugf("%s %s\n", fp->name(), children.c_str());
385 }
386 for (int y = 0; y < 256 && passing; ++y) {
387 for (int x = 0; x < 256 && passing; ++x) {
388 GrColor input = texel_color(x, y);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500389 GrColor output = rgbaData.get()[y * 256 + x];
Brian Salomonf3b995b2017-02-15 10:22:23 -0500390 if (fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500391 // A modulating processor is allowed to modulate either the input color or
392 // just the input alpha.
393 bool legalColorModulation =
394 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
395 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
396 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
397 GrColorUnpackB(output) <= GrColorUnpackB(input);
398 bool legalAlphaModulation =
399 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
400 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
401 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
402 GrColorUnpackB(output) <= GrColorUnpackA(input);
403 if (!legalColorModulation && !legalAlphaModulation) {
404 ERRORF(reporter,
405 "\"Modulating\" processor %s made color/alpha value larger. "
Brian Osman2f3865b2017-03-15 13:35:51 -0400406 "Input: 0x%08x, Output: 0x%08x.",
Brian Salomon587e08f2017-01-27 10:59:27 -0500407 fp->name(), input, output);
408 passing = false;
409 }
410 }
411 GrColor4f input4f = texel_color4f(x, y);
412 GrColor4f output4f = GrColor4f::FromGrColor(output);
413 GrColor4f expected4f;
414 if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) {
415 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
416 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
417 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
418 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500419 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500420 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
421 ERRORF(reporter,
422 "Processor %s claimed output for const input doesn't match "
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500423 "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, "
424 "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)",
425 fp->name(), SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))),
426 kTol, input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2],
427 input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1],
428 output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0],
429 expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]);
Brian Salomon587e08f2017-01-27 10:59:27 -0500430 passing = false;
431 }
432 }
433 if (GrColorIsOpaque(input) && fp->preservesOpaqueInput() &&
434 !GrColorIsOpaque(output)) {
435 ERRORF(reporter,
436 "Processor %s claimed opaqueness is preserved but it is not. Input: "
Brian Osman2f3865b2017-03-15 13:35:51 -0400437 "0x%08x, Output: 0x%08x.",
Brian Salomon587e08f2017-01-27 10:59:27 -0500438 fp->name(), input, output);
439 passing = false;
440 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400441 if (!passing) {
Brian Osmanc35a2d42017-03-17 10:58:53 -0400442 ERRORF(reporter, "Seed: 0x%08x, Processor details: %s",
443 seed, fp->dumpInfo().c_str());
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400444 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500445 }
446 }
447 }
448 }
449}
Hal Canary6f6961e2017-01-31 13:50:44 -0500450#endif // GR_TEST_UTILS
451#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
452#endif // SK_SUPPORT_GPU