blob: 63543647c2f1f5f4355b14cc61355bd5fb0a112a [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 Salomonbaaf4392017-06-15 09:59:23 -040022#include "ops/GrRectOpFactory.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 Phillipsfbcef6e2017-06-15 12:07:18 -040057 static sk_sp<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Brian Salomonbc6b99d2017-01-11 10:32:34 -050058 const SkTArray<sk_sp<GrBuffer>>& buffers,
59 const SkTArray<Image>& images) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040060 return sk_sp<GrFragmentProcessor>(new TestFP(proxies, buffers, images));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050061 }
62
63 const char* name() const override { return "test"; }
64
65 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
66 // We don't really care about reusing these.
67 static int32_t gKey = 0;
68 b->add32(sk_atomic_inc(&gKey));
69 }
70
Brian Salomonbc6b99d2017-01-11 10:32:34 -050071private:
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040072 TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Robert Phillips30f9bc62017-02-22 15:28:38 -050073 const SkTArray<sk_sp<GrBuffer>>& buffers,
Brian Salomonbc6b99d2017-01-11 10:32:34 -050074 const SkTArray<Image>& images)
Brian Salomon587e08f2017-01-27 10:59:27 -050075 : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) {
Robert Phillips30f9bc62017-02-22 15:28:38 -050076 for (const auto& proxy : proxies) {
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040077 this->addTextureSampler(&fSamplers.emplace_back(proxy));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050078 }
79 for (const auto& buffer : buffers) {
80 this->addBufferAccess(&fBuffers.emplace_back(kRGBA_8888_GrPixelConfig, buffer.get()));
81 }
82 for (const Image& image : images) {
Robert Phillips8a02f652017-05-12 14:49:16 -040083 fImages.emplace_back(image.fProxy, image.fIOType,
84 GrSLMemoryModel::kNone, GrSLRestrict::kNo);
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040085 this->addImageStorageAccess(&fImages.back());
Brian Salomonbc6b99d2017-01-11 10:32:34 -050086 }
87 }
88
Brian Salomon587e08f2017-01-27 10:59:27 -050089 TestFP(sk_sp<GrFragmentProcessor> child)
90 : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) {
Brian Salomonbc6b99d2017-01-11 10:32:34 -050091 this->registerChildProcessor(std::move(child));
92 }
93
94 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
95 class TestGLSLFP : public GrGLSLFragmentProcessor {
96 public:
97 TestGLSLFP() {}
98 void emitCode(EmitArgs& args) override {
99 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
100 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
101 }
102
103 private:
104 };
105 return new TestGLSLFP();
106 }
107
108 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
109
110 GrTAllocator<TextureSampler> fSamplers;
111 GrTAllocator<BufferAccess> fBuffers;
112 GrTAllocator<ImageStorageAccess> fImages;
Brian Salomon587e08f2017-01-27 10:59:27 -0500113 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500114};
115}
116
Brian Salomonf046e152017-01-11 15:24:47 -0500117template <typename T>
118inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) {
119 *refCnt = resource->fRefCnt;
120 *readCnt = resource->fPendingReads;
121 *writeCnt = resource->fPendingWrites;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500122}
123
Robert Phillips2f493142017-03-02 18:18:38 -0500124void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500125 *refCnt = proxy->getBackingRefCnt_TestOnly();
126 *readCnt = proxy->getPendingReadCnt_TestOnly();
127 *writeCnt = proxy->getPendingWriteCnt_TestOnly();
128}
129
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500130DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
131 GrContext* context = ctxInfo.grContext();
132
133 GrTextureDesc desc;
134 desc.fConfig = kRGBA_8888_GrPixelConfig;
135 desc.fWidth = 10;
136 desc.fHeight = 10;
137
138 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400139 sk_sp<GrRenderTargetContext> renderTargetContext(context->makeDeferredRenderTargetContext(
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500140 SkBackingFit::kApprox, 1, 1, kRGBA_8888_GrPixelConfig, nullptr));
141 {
142 bool texelBufferSupport = context->caps()->shaderCaps()->texelBufferSupport();
143 bool imageLoadStoreSupport = context->caps()->shaderCaps()->imageLoadStoreSupport();
Brian Osman32342f02017-03-04 08:12:46 -0500144 sk_sp<GrTextureProxy> proxy1(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
Robert Phillips8a02f652017-05-12 14:49:16 -0400145 desc, SkBackingFit::kExact,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500146 SkBudgeted::kYes));
Robert Phillips8a02f652017-05-12 14:49:16 -0400147 sk_sp<GrTextureProxy> proxy2(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
148 desc, SkBackingFit::kExact,
149 SkBudgeted::kYes));
150 sk_sp<GrTextureProxy> proxy3(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
151 desc, SkBackingFit::kExact,
152 SkBudgeted::kYes));
153 sk_sp<GrTextureProxy> proxy4(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
154 desc, SkBackingFit::kExact,
155 SkBudgeted::kYes));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500156 sk_sp<GrBuffer> buffer(texelBufferSupport
157 ? context->resourceProvider()->createBuffer(
158 1024, GrBufferType::kTexel_GrBufferType,
159 GrAccessPattern::kStatic_GrAccessPattern, 0)
160 : nullptr);
161 {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500162 SkTArray<sk_sp<GrTextureProxy>> proxies;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500163 SkTArray<sk_sp<GrBuffer>> buffers;
164 SkTArray<TestFP::Image> images;
Robert Phillips2f493142017-03-02 18:18:38 -0500165 proxies.push_back(proxy1);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500166 if (texelBufferSupport) {
167 buffers.push_back(buffer);
168 }
169 if (imageLoadStoreSupport) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400170 images.emplace_back(proxy2, GrIOType::kRead_GrIOType);
171 images.emplace_back(proxy3, GrIOType::kWrite_GrIOType);
172 images.emplace_back(proxy4, GrIOType::kRW_GrIOType);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500173 }
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400174 std::unique_ptr<GrLegacyMeshDrawOp> op(TestOp::Make());
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500175 GrPaint paint;
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400176 auto fp = TestFP::Make(std::move(proxies), std::move(buffers), std::move(images));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500177 for (int i = 0; i < parentCnt; ++i) {
178 fp = TestFP::Make(std::move(fp));
179 }
180 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400181 renderTargetContext->priv().testingOnly_addLegacyMeshDrawOp(
Brian Salomon649a3412017-03-09 13:50:43 -0500182 std::move(paint), GrAAType::kNone, std::move(op));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500183 }
184 int refCnt, readCnt, writeCnt;
185
Robert Phillips30f9bc62017-02-22 15:28:38 -0500186 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500187 REPORTER_ASSERT(reporter, 1 == refCnt);
188 REPORTER_ASSERT(reporter, 1 == readCnt);
189 REPORTER_ASSERT(reporter, 0 == writeCnt);
190
191 if (texelBufferSupport) {
Brian Salomonf046e152017-01-11 15:24:47 -0500192 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500193 REPORTER_ASSERT(reporter, 1 == refCnt);
194 REPORTER_ASSERT(reporter, 1 == readCnt);
195 REPORTER_ASSERT(reporter, 0 == writeCnt);
196 }
197
198 if (imageLoadStoreSupport) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400199 testingOnly_getIORefCnts(proxy2.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
Robert Phillips8a02f652017-05-12 14:49:16 -0400204 testingOnly_getIORefCnts(proxy3.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500205 REPORTER_ASSERT(reporter, 1 == refCnt);
206 REPORTER_ASSERT(reporter, 0 == readCnt);
207 REPORTER_ASSERT(reporter, 1 == writeCnt);
208
Robert Phillips8a02f652017-05-12 14:49:16 -0400209 testingOnly_getIORefCnts(proxy4.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500210 REPORTER_ASSERT(reporter, 1 == refCnt);
211 REPORTER_ASSERT(reporter, 1 == readCnt);
212 REPORTER_ASSERT(reporter, 1 == writeCnt);
213 }
214
215 context->flush();
216
Robert Phillips30f9bc62017-02-22 15:28:38 -0500217 testingOnly_getIORefCnts(proxy1.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, 0 == writeCnt);
221
222 if (texelBufferSupport) {
Brian Salomonf046e152017-01-11 15:24:47 -0500223 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500224 REPORTER_ASSERT(reporter, 1 == refCnt);
225 REPORTER_ASSERT(reporter, 0 == readCnt);
226 REPORTER_ASSERT(reporter, 0 == writeCnt);
227 }
228
229 if (texelBufferSupport) {
Robert Phillips8a02f652017-05-12 14:49:16 -0400230 testingOnly_getIORefCnts(proxy2.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
Robert Phillips8a02f652017-05-12 14:49:16 -0400235 testingOnly_getIORefCnts(proxy3.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
Robert Phillips8a02f652017-05-12 14:49:16 -0400240 testingOnly_getIORefCnts(proxy4.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500241 REPORTER_ASSERT(reporter, 1 == refCnt);
242 REPORTER_ASSERT(reporter, 0 == readCnt);
243 REPORTER_ASSERT(reporter, 0 == writeCnt);
244 }
245 }
246 }
247}
Brian Salomon587e08f2017-01-27 10:59:27 -0500248
249// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
250#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
251
252static GrColor texel_color(int i, int j) {
253 SkASSERT((unsigned)i < 256 && (unsigned)j < 256);
254 GrColor color = GrColorPackRGBA(j, (uint8_t)(i + j), (uint8_t)(2 * j - i), i);
255 return GrPremulColor(color);
256}
257
258static GrColor4f texel_color4f(int i, int j) { return GrColor4f::FromGrColor(texel_color(i, j)); }
259
Robert Phillips296b1cc2017-03-15 10:42:12 -0400260void test_draw_op(GrRenderTargetContext* rtc, sk_sp<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500261 sk_sp<GrTextureProxy> inputDataProxy) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500262 GrPaint paint;
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400263 paint.addColorTextureProcessor(std::move(inputDataProxy), nullptr, SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500264 paint.addColorFragmentProcessor(std::move(fp));
265 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400266
Brian Salomonbaaf4392017-06-15 09:59:23 -0400267 auto op = GrRectOpFactory::MakeNonAAFill(std::move(paint), SkMatrix::I(),
268 SkRect::MakeWH(rtc->width(), rtc->height()),
269 GrAAType::kNone);
Brian Salomonac70f842017-05-08 10:43:33 -0400270 rtc->addDrawOp(GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500271}
272
Brian Osmanc35a2d42017-03-17 10:58:53 -0400273#include "SkCommandLineFlags.h"
274DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?");
275
Hal Canary6f6961e2017-01-31 13:50:44 -0500276#if GR_TEST_UTILS
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500277DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500278 GrContext* context = ctxInfo.grContext();
279 using FPFactory = GrProcessorTestFactory<GrFragmentProcessor>;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400280
281 uint32_t seed = 0;
282 if (FLAGS_randomProcessorTest) {
283 std::random_device rd;
284 seed = rd();
285 }
286 // If a non-deterministic bot fails this test, check the output to see what seed it used, then
287 // hard-code that value here:
288 SkRandom random(seed);
289
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400290 sk_sp<GrRenderTargetContext> rtc = context->makeDeferredRenderTargetContext(
Brian Salomon587e08f2017-01-27 10:59:27 -0500291 SkBackingFit::kExact, 256, 256, kRGBA_8888_GrPixelConfig, nullptr);
292 GrSurfaceDesc desc;
293 desc.fWidth = 256;
294 desc.fHeight = 256;
295 desc.fFlags = kRenderTarget_GrSurfaceFlag;
296 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500297
Robert Phillipse78b7252017-04-06 07:59:41 -0400298 sk_sp<GrTextureProxy> proxies[2];
299
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500300 // Put premul data into the RGBA texture that the test FPs can optionally use.
301 std::unique_ptr<GrColor[]> rgbaData(new GrColor[256 * 256]);
302 for (int y = 0; y < 256; ++y) {
303 for (int x = 0; x < 256; ++x) {
304 rgbaData.get()[256 * y + x] =
305 texel_color(random.nextULessThan(256), random.nextULessThan(256));
306 }
307 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400308 proxies[0] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
309 rgbaData.get(), 256 * sizeof(GrColor));
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500310
311 // Put random values into the alpha texture that the test FPs can optionally use.
Brian Salomon587e08f2017-01-27 10:59:27 -0500312 desc.fConfig = kAlpha_8_GrPixelConfig;
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500313 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[256 * 256]);
314 for (int y = 0; y < 256; ++y) {
315 for (int x = 0; x < 256; ++x) {
316 alphaData.get()[256 * y + x] = random.nextULessThan(256);
317 }
318 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400319 proxies[1] = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, SkBudgeted::kYes,
320 alphaData.get(), 256);
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400321
322 if (!proxies[0] || !proxies[1]) {
323 return;
324 }
325
Robert Phillipse78b7252017-04-06 07:59:41 -0400326 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500327
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500328 // Use a different array of premul colors for the output of the fragment processor that preceeds
329 // the fragment processor under test.
Brian Salomon587e08f2017-01-27 10:59:27 -0500330 for (int y = 0; y < 256; ++y) {
331 for (int x = 0; x < 256; ++x) {
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500332 rgbaData.get()[256 * y + x] = texel_color(x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500333 }
334 }
335 desc.fConfig = kRGBA_8888_GrPixelConfig;
Robert Phillips30f9bc62017-02-22 15:28:38 -0500336
Robert Phillips26c90e02017-03-14 14:39:29 -0400337 sk_sp<GrTextureProxy> dataProxy = GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
Robert Phillips30f9bc62017-02-22 15:28:38 -0500338 desc, SkBudgeted::kYes,
339 rgbaData.get(),
340 256 * sizeof(GrColor));
Brian Salomon587e08f2017-01-27 10:59:27 -0500341
342 // Because processors factories configure themselves in random ways, this is not exhaustive.
343 for (int i = 0; i < FPFactory::Count(); ++i) {
344 int timesToInvokeFactory = 5;
345 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
346 // on child optimizations being present.
347 sk_sp<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
348 for (int j = 0; j < fp->numChildProcessors(); ++j) {
349 // This value made a reasonable trade off between time and coverage when this test was
350 // written.
351 timesToInvokeFactory *= FPFactory::Count() / 2;
352 }
353 for (int j = 0; j < timesToInvokeFactory; ++j) {
354 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillips9bee2e52017-05-29 12:37:20 -0400355 if (!fp->instantiate(context->resourceProvider())) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400356 continue;
357 }
358
Brian Salomon587e08f2017-01-27 10:59:27 -0500359 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500360 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500361 continue;
362 }
Robert Phillips296b1cc2017-03-15 10:42:12 -0400363 test_draw_op(rtc.get(), fp, dataProxy);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500364 memset(rgbaData.get(), 0x0, sizeof(GrColor) * 256 * 256);
Brian Salomon587e08f2017-01-27 10:59:27 -0500365 rtc->readPixels(
366 SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500367 rgbaData.get(), 0, 0, 0);
Brian Salomon587e08f2017-01-27 10:59:27 -0500368 bool passing = true;
369 if (0) { // Useful to see what FPs are being tested.
370 SkString children;
371 for (int c = 0; c < fp->numChildProcessors(); ++c) {
372 if (!c) {
373 children.append("(");
374 }
375 children.append(fp->childProcessor(c).name());
376 children.append(c == fp->numChildProcessors() - 1 ? ")" : ", ");
377 }
378 SkDebugf("%s %s\n", fp->name(), children.c_str());
379 }
380 for (int y = 0; y < 256 && passing; ++y) {
381 for (int x = 0; x < 256 && passing; ++x) {
382 GrColor input = texel_color(x, y);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500383 GrColor output = rgbaData.get()[y * 256 + x];
Brian Salomonf3b995b2017-02-15 10:22:23 -0500384 if (fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500385 // A modulating processor is allowed to modulate either the input color or
386 // just the input alpha.
387 bool legalColorModulation =
388 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
389 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
390 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
391 GrColorUnpackB(output) <= GrColorUnpackB(input);
392 bool legalAlphaModulation =
393 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
394 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
395 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
396 GrColorUnpackB(output) <= GrColorUnpackA(input);
397 if (!legalColorModulation && !legalAlphaModulation) {
398 ERRORF(reporter,
399 "\"Modulating\" processor %s made color/alpha value larger. "
Brian Osman2f3865b2017-03-15 13:35:51 -0400400 "Input: 0x%08x, Output: 0x%08x.",
Brian Salomon587e08f2017-01-27 10:59:27 -0500401 fp->name(), input, output);
402 passing = false;
403 }
404 }
405 GrColor4f input4f = texel_color4f(x, y);
406 GrColor4f output4f = GrColor4f::FromGrColor(output);
407 GrColor4f expected4f;
408 if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) {
409 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
410 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
411 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
412 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500413 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500414 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
415 ERRORF(reporter,
416 "Processor %s claimed output for const input doesn't match "
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500417 "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, "
418 "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)",
419 fp->name(), SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))),
420 kTol, input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2],
421 input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1],
422 output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0],
423 expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]);
Brian Salomon587e08f2017-01-27 10:59:27 -0500424 passing = false;
425 }
426 }
427 if (GrColorIsOpaque(input) && fp->preservesOpaqueInput() &&
428 !GrColorIsOpaque(output)) {
429 ERRORF(reporter,
430 "Processor %s claimed opaqueness is preserved but it is not. Input: "
Brian Osman2f3865b2017-03-15 13:35:51 -0400431 "0x%08x, Output: 0x%08x.",
Brian Salomon587e08f2017-01-27 10:59:27 -0500432 fp->name(), input, output);
433 passing = false;
434 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400435 if (!passing) {
Brian Osmanc35a2d42017-03-17 10:58:53 -0400436 ERRORF(reporter, "Seed: 0x%08x, Processor details: %s",
437 seed, fp->dumpInfo().c_str());
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400438 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500439 }
440 }
441 }
442 }
443}
Robert Phillips18166ee2017-06-01 12:55:44 -0400444
Hal Canary6f6961e2017-01-31 13:50:44 -0500445#endif // GR_TEST_UTILS
446#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
447#endif // SK_SUPPORT_GPU