blob: d0f2faeb19d55fdd2b6d799e02a7e650491e7ba5 [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 Salomonc65aec92017-03-09 09:03:58 -050012#include "GrClip.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050013#include "GrContext.h"
14#include "GrGpuResource.h"
Brian Salomon5d4cd9e2017-02-09 11:16:46 -050015#include "GrPipelineBuilder.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050016#include "GrRenderTargetContext.h"
17#include "GrRenderTargetContextPriv.h"
18#include "GrResourceProvider.h"
19#include "glsl/GrGLSLFragmentProcessor.h"
20#include "glsl/GrGLSLFragmentShaderBuilder.h"
Brian Salomon5d4cd9e2017-02-09 11:16:46 -050021#include "ops/GrNonAAFillRectOp.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050022#include "ops/GrTestMeshDrawOp.h"
23
24namespace {
25class TestOp : public GrTestMeshDrawOp {
26public:
27 DEFINE_OP_CLASS_ID
28 const char* name() const override { return "TestOp"; }
29
Brian Salomon649a3412017-03-09 13:50:43 -050030 static std::unique_ptr<GrMeshDrawOp> Make() {
31 return std::unique_ptr<GrMeshDrawOp>(new TestOp);
32 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050033
34private:
35 TestOp() : INHERITED(ClassID(), SkRect::MakeWH(100, 100), 0xFFFFFFFF) {}
36
37 void onPrepareDraws(Target* target) const override { return; }
38
39 typedef GrTestMeshDrawOp INHERITED;
40};
41
42/**
43 * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts
44 * of resources owned by child FPs.
45 */
46class TestFP : public GrFragmentProcessor {
47public:
48 struct Image {
49 Image(sk_sp<GrTexture> texture, GrIOType ioType) : fTexture(texture), fIOType(ioType) {}
50 sk_sp<GrTexture> fTexture;
51 GrIOType fIOType;
52 };
53 static sk_sp<GrFragmentProcessor> Make(sk_sp<GrFragmentProcessor> child) {
54 return sk_sp<GrFragmentProcessor>(new TestFP(std::move(child)));
55 }
Robert Phillips30f9bc62017-02-22 15:28:38 -050056 static sk_sp<GrFragmentProcessor> Make(GrContext* context,
57 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 Phillips30f9bc62017-02-22 15:28:38 -050060 return sk_sp<GrFragmentProcessor>(new TestFP(context, 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 Phillips30f9bc62017-02-22 15:28:38 -050072 TestFP(GrContext* context,
73 const SkTArray<sk_sp<GrTextureProxy>>& proxies,
74 const SkTArray<sk_sp<GrBuffer>>& buffers,
Brian Salomonbc6b99d2017-01-11 10:32:34 -050075 const SkTArray<Image>& images)
Brian Salomon587e08f2017-01-27 10:59:27 -050076 : INHERITED(kNone_OptimizationFlags), fSamplers(4), fBuffers(4), fImages(4) {
Robert Phillips30f9bc62017-02-22 15:28:38 -050077 for (const auto& proxy : proxies) {
Brian Osman32342f02017-03-04 08:12:46 -050078 this->addTextureSampler(&fSamplers.emplace_back(context->resourceProvider(), proxy));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050079 }
80 for (const auto& buffer : buffers) {
81 this->addBufferAccess(&fBuffers.emplace_back(kRGBA_8888_GrPixelConfig, buffer.get()));
82 }
83 for (const Image& image : images) {
84 this->addImageStorageAccess(&fImages.emplace_back(
85 image.fTexture, image.fIOType, GrSLMemoryModel::kNone, GrSLRestrict::kNo));
86 }
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++) {
139 sk_sp<GrRenderTargetContext> renderTargetContext(context->makeRenderTargetContext(
140 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 Phillips26c90e02017-03-14 14:39:29 -0400145 desc,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500146 SkBackingFit::kExact,
147 SkBudgeted::kYes));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500148 sk_sp<GrTexture> texture2(
149 context->resourceProvider()->createTexture(desc, SkBudgeted::kYes));
150 sk_sp<GrTexture> texture3(
151 context->resourceProvider()->createTexture(desc, SkBudgeted::kYes));
152 sk_sp<GrTexture> texture4(
153 context->resourceProvider()->createTexture(desc, SkBudgeted::kYes));
154 sk_sp<GrBuffer> buffer(texelBufferSupport
155 ? context->resourceProvider()->createBuffer(
156 1024, GrBufferType::kTexel_GrBufferType,
157 GrAccessPattern::kStatic_GrAccessPattern, 0)
158 : nullptr);
159 {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500160 SkTArray<sk_sp<GrTextureProxy>> proxies;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500161 SkTArray<sk_sp<GrBuffer>> buffers;
162 SkTArray<TestFP::Image> images;
Robert Phillips2f493142017-03-02 18:18:38 -0500163 proxies.push_back(proxy1);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500164 if (texelBufferSupport) {
165 buffers.push_back(buffer);
166 }
167 if (imageLoadStoreSupport) {
168 images.emplace_back(texture2, GrIOType::kRead_GrIOType);
169 images.emplace_back(texture3, GrIOType::kWrite_GrIOType);
170 images.emplace_back(texture4, GrIOType::kRW_GrIOType);
171 }
Brian Salomon649a3412017-03-09 13:50:43 -0500172 std::unique_ptr<GrMeshDrawOp> op(TestOp::Make());
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500173 GrPaint paint;
Robert Phillips30f9bc62017-02-22 15:28:38 -0500174 auto fp = TestFP::Make(context,
175 std::move(proxies), std::move(buffers), std::move(images));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500176 for (int i = 0; i < parentCnt; ++i) {
177 fp = TestFP::Make(std::move(fp));
178 }
179 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomon649a3412017-03-09 13:50:43 -0500180 renderTargetContext->priv().testingOnly_addMeshDrawOp(
181 std::move(paint), GrAAType::kNone, std::move(op));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500182 }
183 int refCnt, readCnt, writeCnt;
184
Robert Phillips30f9bc62017-02-22 15:28:38 -0500185 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500186 REPORTER_ASSERT(reporter, 1 == refCnt);
187 REPORTER_ASSERT(reporter, 1 == readCnt);
188 REPORTER_ASSERT(reporter, 0 == writeCnt);
189
190 if (texelBufferSupport) {
Brian Salomonf046e152017-01-11 15:24:47 -0500191 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500192 REPORTER_ASSERT(reporter, 1 == refCnt);
193 REPORTER_ASSERT(reporter, 1 == readCnt);
194 REPORTER_ASSERT(reporter, 0 == writeCnt);
195 }
196
197 if (imageLoadStoreSupport) {
Brian Salomonf046e152017-01-11 15:24:47 -0500198 testingOnly_getIORefCnts(texture2.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
Brian Salomonf046e152017-01-11 15:24:47 -0500203 testingOnly_getIORefCnts(texture3.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500204 REPORTER_ASSERT(reporter, 1 == refCnt);
205 REPORTER_ASSERT(reporter, 0 == readCnt);
206 REPORTER_ASSERT(reporter, 1 == writeCnt);
207
Brian Salomonf046e152017-01-11 15:24:47 -0500208 testingOnly_getIORefCnts(texture4.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500209 REPORTER_ASSERT(reporter, 1 == refCnt);
210 REPORTER_ASSERT(reporter, 1 == readCnt);
211 REPORTER_ASSERT(reporter, 1 == writeCnt);
212 }
213
214 context->flush();
215
Robert Phillips30f9bc62017-02-22 15:28:38 -0500216 testingOnly_getIORefCnts(proxy1.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, 0 == writeCnt);
220
221 if (texelBufferSupport) {
Brian Salomonf046e152017-01-11 15:24:47 -0500222 testingOnly_getIORefCnts(buffer.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500223 REPORTER_ASSERT(reporter, 1 == refCnt);
224 REPORTER_ASSERT(reporter, 0 == readCnt);
225 REPORTER_ASSERT(reporter, 0 == writeCnt);
226 }
227
228 if (texelBufferSupport) {
Brian Salomonf046e152017-01-11 15:24:47 -0500229 testingOnly_getIORefCnts(texture2.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
Brian Salomonf046e152017-01-11 15:24:47 -0500234 testingOnly_getIORefCnts(texture3.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500235 REPORTER_ASSERT(reporter, 1 == refCnt);
236 REPORTER_ASSERT(reporter, 0 == readCnt);
237 REPORTER_ASSERT(reporter, 0 == writeCnt);
238
Brian Salomonf046e152017-01-11 15:24:47 -0500239 testingOnly_getIORefCnts(texture4.get(), &refCnt, &readCnt, &writeCnt);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500240 REPORTER_ASSERT(reporter, 1 == refCnt);
241 REPORTER_ASSERT(reporter, 0 == readCnt);
242 REPORTER_ASSERT(reporter, 0 == writeCnt);
243 }
244 }
245 }
246}
Brian Salomon587e08f2017-01-27 10:59:27 -0500247
248// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
249#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
250
251static GrColor texel_color(int i, int j) {
252 SkASSERT((unsigned)i < 256 && (unsigned)j < 256);
253 GrColor color = GrColorPackRGBA(j, (uint8_t)(i + j), (uint8_t)(2 * j - i), i);
254 return GrPremulColor(color);
255}
256
257static GrColor4f texel_color4f(int i, int j) { return GrColor4f::FromGrColor(texel_color(i, j)); }
258
Robert Phillips296b1cc2017-03-15 10:42:12 -0400259void test_draw_op(GrRenderTargetContext* rtc, sk_sp<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500260 sk_sp<GrTextureProxy> inputDataProxy) {
Robert Phillips296b1cc2017-03-15 10:42:12 -0400261 GrResourceProvider* resourceProvider = rtc->resourceProvider();
262
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500263 GrPaint paint;
Robert Phillips296b1cc2017-03-15 10:42:12 -0400264 paint.addColorTextureProcessor(resourceProvider, std::move(inputDataProxy),
265 nullptr, SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500266 paint.addColorFragmentProcessor(std::move(fp));
267 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
268 GrPipelineBuilder pb(std::move(paint), GrAAType::kNone);
269 auto op =
270 GrNonAAFillRectOp::Make(GrColor_WHITE, SkMatrix::I(),
271 SkRect::MakeWH(rtc->width(), rtc->height()), nullptr, nullptr);
Brian Salomon649a3412017-03-09 13:50:43 -0500272 rtc->addMeshDrawOp(pb, GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500273}
274
Hal Canary6f6961e2017-01-31 13:50:44 -0500275#if GR_TEST_UTILS
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500276DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500277 GrContext* context = ctxInfo.grContext();
278 using FPFactory = GrProcessorTestFactory<GrFragmentProcessor>;
279 SkRandom random;
280 sk_sp<GrRenderTargetContext> rtc = context->makeRenderTargetContext(
281 SkBackingFit::kExact, 256, 256, kRGBA_8888_GrPixelConfig, nullptr);
282 GrSurfaceDesc desc;
283 desc.fWidth = 256;
284 desc.fHeight = 256;
285 desc.fFlags = kRenderTarget_GrSurfaceFlag;
286 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500287
288 // Put premul data into the RGBA texture that the test FPs can optionally use.
289 std::unique_ptr<GrColor[]> rgbaData(new GrColor[256 * 256]);
290 for (int y = 0; y < 256; ++y) {
291 for (int x = 0; x < 256; ++x) {
292 rgbaData.get()[256 * y + x] =
293 texel_color(random.nextULessThan(256), random.nextULessThan(256));
294 }
295 }
Brian Osman32342f02017-03-04 08:12:46 -0500296 sk_sp<GrTexture> tex0(context->resourceProvider()->createTexture(
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500297 desc, SkBudgeted::kYes, rgbaData.get(), 256 * sizeof(GrColor)));
298
299 // Put random values into the alpha texture that the test FPs can optionally use.
Brian Salomon587e08f2017-01-27 10:59:27 -0500300 desc.fConfig = kAlpha_8_GrPixelConfig;
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500301 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[256 * 256]);
302 for (int y = 0; y < 256; ++y) {
303 for (int x = 0; x < 256; ++x) {
304 alphaData.get()[256 * y + x] = random.nextULessThan(256);
305 }
306 }
Brian Osman32342f02017-03-04 08:12:46 -0500307 sk_sp<GrTexture> tex1(context->resourceProvider()->createTexture(desc, SkBudgeted::kYes,
308 alphaData.get(), 256));
Brian Salomon587e08f2017-01-27 10:59:27 -0500309 GrTexture* textures[] = {tex0.get(), tex1.get()};
310 GrProcessorTestData testData(&random, context, rtc.get(), textures);
311
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500312 // Use a different array of premul colors for the output of the fragment processor that preceeds
313 // the fragment processor under test.
Brian Salomon587e08f2017-01-27 10:59:27 -0500314 for (int y = 0; y < 256; ++y) {
315 for (int x = 0; x < 256; ++x) {
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500316 rgbaData.get()[256 * y + x] = texel_color(x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500317 }
318 }
319 desc.fConfig = kRGBA_8888_GrPixelConfig;
Robert Phillips30f9bc62017-02-22 15:28:38 -0500320
Robert Phillips26c90e02017-03-14 14:39:29 -0400321 sk_sp<GrTextureProxy> dataProxy = GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
Robert Phillips30f9bc62017-02-22 15:28:38 -0500322 desc, SkBudgeted::kYes,
323 rgbaData.get(),
324 256 * sizeof(GrColor));
Brian Salomon587e08f2017-01-27 10:59:27 -0500325
326 // Because processors factories configure themselves in random ways, this is not exhaustive.
327 for (int i = 0; i < FPFactory::Count(); ++i) {
328 int timesToInvokeFactory = 5;
329 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
330 // on child optimizations being present.
331 sk_sp<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
332 for (int j = 0; j < fp->numChildProcessors(); ++j) {
333 // This value made a reasonable trade off between time and coverage when this test was
334 // written.
335 timesToInvokeFactory *= FPFactory::Count() / 2;
336 }
337 for (int j = 0; j < timesToInvokeFactory; ++j) {
338 fp = FPFactory::MakeIdx(i, &testData);
339 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500340 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500341 continue;
342 }
Robert Phillips296b1cc2017-03-15 10:42:12 -0400343 test_draw_op(rtc.get(), fp, dataProxy);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500344 memset(rgbaData.get(), 0x0, sizeof(GrColor) * 256 * 256);
Brian Salomon587e08f2017-01-27 10:59:27 -0500345 rtc->readPixels(
346 SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500347 rgbaData.get(), 0, 0, 0);
Brian Salomon587e08f2017-01-27 10:59:27 -0500348 bool passing = true;
349 if (0) { // Useful to see what FPs are being tested.
350 SkString children;
351 for (int c = 0; c < fp->numChildProcessors(); ++c) {
352 if (!c) {
353 children.append("(");
354 }
355 children.append(fp->childProcessor(c).name());
356 children.append(c == fp->numChildProcessors() - 1 ? ")" : ", ");
357 }
358 SkDebugf("%s %s\n", fp->name(), children.c_str());
359 }
360 for (int y = 0; y < 256 && passing; ++y) {
361 for (int x = 0; x < 256 && passing; ++x) {
362 GrColor input = texel_color(x, y);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500363 GrColor output = rgbaData.get()[y * 256 + x];
Brian Salomonf3b995b2017-02-15 10:22:23 -0500364 if (fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500365 // A modulating processor is allowed to modulate either the input color or
366 // just the input alpha.
367 bool legalColorModulation =
368 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
369 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
370 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
371 GrColorUnpackB(output) <= GrColorUnpackB(input);
372 bool legalAlphaModulation =
373 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
374 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
375 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
376 GrColorUnpackB(output) <= GrColorUnpackA(input);
377 if (!legalColorModulation && !legalAlphaModulation) {
378 ERRORF(reporter,
379 "\"Modulating\" processor %s made color/alpha value larger. "
Brian Osman2f3865b2017-03-15 13:35:51 -0400380 "Input: 0x%08x, Output: 0x%08x.",
Brian Salomon587e08f2017-01-27 10:59:27 -0500381 fp->name(), input, output);
382 passing = false;
383 }
384 }
385 GrColor4f input4f = texel_color4f(x, y);
386 GrColor4f output4f = GrColor4f::FromGrColor(output);
387 GrColor4f expected4f;
388 if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) {
389 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fRGBA[0]);
390 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fRGBA[1]);
391 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fRGBA[2]);
392 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fRGBA[3]);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500393 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500394 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
395 ERRORF(reporter,
396 "Processor %s claimed output for const input doesn't match "
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500397 "actual output. Error: %f, Tolerance: %f, input: (%f, %f, %f, "
398 "%f), actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f)",
399 fp->name(), SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))),
400 kTol, input4f.fRGBA[0], input4f.fRGBA[1], input4f.fRGBA[2],
401 input4f.fRGBA[3], output4f.fRGBA[0], output4f.fRGBA[1],
402 output4f.fRGBA[2], output4f.fRGBA[3], expected4f.fRGBA[0],
403 expected4f.fRGBA[1], expected4f.fRGBA[2], expected4f.fRGBA[3]);
Brian Salomon587e08f2017-01-27 10:59:27 -0500404 passing = false;
405 }
406 }
407 if (GrColorIsOpaque(input) && fp->preservesOpaqueInput() &&
408 !GrColorIsOpaque(output)) {
409 ERRORF(reporter,
410 "Processor %s claimed opaqueness is preserved but it is not. Input: "
Brian Osman2f3865b2017-03-15 13:35:51 -0400411 "0x%08x, Output: 0x%08x.",
Brian Salomon587e08f2017-01-27 10:59:27 -0500412 fp->name(), input, output);
413 passing = false;
414 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400415 if (!passing) {
416 ERRORF(reporter, "Processor details: %s", fp->dumpInfo().c_str());
417 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500418 }
419 }
420 }
421 }
422}
Hal Canary6f6961e2017-01-31 13:50:44 -0500423#endif // GR_TEST_UTILS
424#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
425#endif // SK_SUPPORT_GPU