blob: 6107b500989725b09e2c59963eb6f312f858d1f2 [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
Brian Salomonc65aec92017-03-09 09:03:58 -050011#include "GrClip.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050012#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050013#include "GrContextPriv.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050014#include "GrGpuResource.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040015#include "GrMemoryPool.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050016#include "GrProxyProvider.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 Salomon82ddc942017-07-14 12:00:13 -040022#include "ops/GrMeshDrawOp.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040023#include "ops/GrRectOpFactory.h"
Michael Ludwige8e10752018-10-01 12:42:53 -040024#include "TestUtils.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -050025
Hal Canary8a001442018-09-19 11:31:27 -040026#include <random>
27
Brian Salomonbc6b99d2017-01-11 10:32:34 -050028namespace {
Brian Salomon82ddc942017-07-14 12:00:13 -040029class TestOp : public GrMeshDrawOp {
Brian Salomonbc6b99d2017-01-11 10:32:34 -050030public:
31 DEFINE_OP_CLASS_ID
Robert Phillips7c525e62018-06-12 10:11:12 -040032 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
33 std::unique_ptr<GrFragmentProcessor> fp) {
Robert Phillipsc994a932018-06-19 13:09:54 -040034 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
35
36 return pool->allocate<TestOp>(std::move(fp));
Brian Salomon82ddc942017-07-14 12:00:13 -040037 }
38
Robert Phillips5f567c72017-09-14 08:27:37 -040039 const char* name() const override { return "TestOp"; }
40
Robert Phillipsf1748f52017-09-14 14:11:24 -040041 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips5f567c72017-09-14 08:27:37 -040042 fProcessors.visitProxies(func);
43 }
44
Brian Salomon82ddc942017-07-14 12:00:13 -040045 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
46
Brian Osman532b3f92018-07-11 10:02:07 -040047 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
Brian Salomon82ddc942017-07-14 12:00:13 -040048 static constexpr GrProcessorAnalysisColor kUnknownColor;
49 GrColor overrideColor;
50 fProcessors.finalize(kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip, false, caps,
Brian Osman532b3f92018-07-11 10:02:07 -040051 &overrideColor);
Brian Salomon82ddc942017-07-14 12:00:13 -040052 return RequiresDstTexture::kNo;
Brian Salomon649a3412017-03-09 13:50:43 -050053 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050054
55private:
Robert Phillips7c525e62018-06-12 10:11:12 -040056 friend class ::GrOpMemoryPool; // for ctor
57
Brian Salomonaff329b2017-08-11 09:40:37 -040058 TestOp(std::unique_ptr<GrFragmentProcessor> fp)
59 : INHERITED(ClassID()), fProcessors(std::move(fp)) {
Brian Salomon82ddc942017-07-14 12:00:13 -040060 this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsZeroArea::kNo);
61 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050062
Brian Salomon91326c32017-08-09 16:02:19 -040063 void onPrepareDraws(Target* target) override { return; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050064
Brian Salomon82ddc942017-07-14 12:00:13 -040065 GrProcessorSet fProcessors;
66
67 typedef GrMeshDrawOp INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050068};
69
70/**
71 * FP used to test ref/IO counts on owned GrGpuResources. Can also be a parent FP to test counts
72 * of resources owned by child FPs.
73 */
74class TestFP : public GrFragmentProcessor {
75public:
Brian Salomonaff329b2017-08-11 09:40:37 -040076 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) {
77 return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child)));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050078 }
Brian Salomonaff329b2017-08-11 09:40:37 -040079 static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<sk_sp<GrTextureProxy>>& proxies,
Brian Salomon559f5562017-11-15 14:28:33 -050080 const SkTArray<sk_sp<GrBuffer>>& buffers) {
81 return std::unique_ptr<GrFragmentProcessor>(new TestFP(proxies, buffers));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050082 }
83
84 const char* name() const override { return "test"; }
85
86 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
87 // We don't really care about reusing these.
88 static int32_t gKey = 0;
89 b->add32(sk_atomic_inc(&gKey));
90 }
91
Brian Salomonaff329b2017-08-11 09:40:37 -040092 std::unique_ptr<GrFragmentProcessor> clone() const override {
93 return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this));
Brian Salomonb17e6392017-07-28 13:41:51 -040094 }
95
Brian Salomonbc6b99d2017-01-11 10:32:34 -050096private:
Brian Salomon559f5562017-11-15 14:28:33 -050097 TestFP(const SkTArray<sk_sp<GrTextureProxy>>& proxies, const SkTArray<sk_sp<GrBuffer>>& buffers)
Brian Salomon662ea4b2018-07-12 14:53:49 -040098 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4) {
Robert Phillips30f9bc62017-02-22 15:28:38 -050099 for (const auto& proxy : proxies) {
Brian Salomonf7dcd762018-07-30 14:48:15 -0400100 fSamplers.emplace_back(proxy);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500101 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400102 this->setTextureSamplerCnt(fSamplers.count());
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500103 }
104
Brian Salomonaff329b2017-08-11 09:40:37 -0400105 TestFP(std::unique_ptr<GrFragmentProcessor> child)
Brian Salomon662ea4b2018-07-12 14:53:49 -0400106 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags), fSamplers(4) {
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500107 this->registerChildProcessor(std::move(child));
108 }
109
Brian Salomon96271cd2017-07-31 16:27:23 -0400110 explicit TestFP(const TestFP& that)
Brian Salomon662ea4b2018-07-12 14:53:49 -0400111 : INHERITED(kTestFP_ClassID, that.optimizationFlags()), fSamplers(4) {
Brian Salomonb17e6392017-07-28 13:41:51 -0400112 for (int i = 0; i < that.fSamplers.count(); ++i) {
113 fSamplers.emplace_back(that.fSamplers[i]);
Brian Salomonb17e6392017-07-28 13:41:51 -0400114 }
Brian Salomon96271cd2017-07-31 16:27:23 -0400115 for (int i = 0; i < that.numChildProcessors(); ++i) {
116 this->registerChildProcessor(that.childProcessor(i).clone());
Brian Salomonb17e6392017-07-28 13:41:51 -0400117 }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400118 this->setTextureSamplerCnt(fSamplers.count());
Brian Salomonb17e6392017-07-28 13:41:51 -0400119 }
120
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500121 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
122 class TestGLSLFP : public GrGLSLFragmentProcessor {
123 public:
124 TestGLSLFP() {}
125 void emitCode(EmitArgs& args) override {
126 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
127 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
128 }
129
130 private:
131 };
132 return new TestGLSLFP();
133 }
134
135 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
Brian Salomonf7dcd762018-07-30 14:48:15 -0400136 const TextureSampler& onTextureSampler(int i) const override { return fSamplers[i]; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500137
138 GrTAllocator<TextureSampler> fSamplers;
Brian Salomon587e08f2017-01-27 10:59:27 -0500139 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500140};
141}
142
Brian Salomonf046e152017-01-11 15:24:47 -0500143template <typename T>
144inline void testingOnly_getIORefCnts(const T* resource, int* refCnt, int* readCnt, int* writeCnt) {
145 *refCnt = resource->fRefCnt;
146 *readCnt = resource->fPendingReads;
147 *writeCnt = resource->fPendingWrites;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500148}
149
Robert Phillips2f493142017-03-02 18:18:38 -0500150void testingOnly_getIORefCnts(GrTextureProxy* proxy, int* refCnt, int* readCnt, int* writeCnt) {
Robert Phillips30f9bc62017-02-22 15:28:38 -0500151 *refCnt = proxy->getBackingRefCnt_TestOnly();
152 *readCnt = proxy->getPendingReadCnt_TestOnly();
153 *writeCnt = proxy->getPendingWriteCnt_TestOnly();
154}
155
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500156DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
157 GrContext* context = ctxInfo.grContext();
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500158 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500159
Robert Phillips16d8ec62017-07-27 16:16:25 -0400160 GrSurfaceDesc desc;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500161 desc.fWidth = 10;
162 desc.fHeight = 10;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400163 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500164
Brian Salomonb17e6392017-07-28 13:41:51 -0400165 for (bool makeClone : {false, true}) {
166 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
167 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500168 context->contextPriv().makeDeferredRenderTargetContext(
169 SkBackingFit::kApprox, 1, 1,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500170 kRGBA_8888_GrPixelConfig, nullptr));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500171 {
Brian Salomon2a4f9832018-03-03 22:43:43 -0500172 sk_sp<GrTextureProxy> proxy1 = proxyProvider->createProxy(
173 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
174 sk_sp<GrTextureProxy> proxy2 = proxyProvider->createProxy(
175 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
176 sk_sp<GrTextureProxy> proxy3 = proxyProvider->createProxy(
177 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
178 sk_sp<GrTextureProxy> proxy4 = proxyProvider->createProxy(
179 desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kExact, SkBudgeted::kYes);
Brian Salomonb17e6392017-07-28 13:41:51 -0400180 {
181 SkTArray<sk_sp<GrTextureProxy>> proxies;
182 SkTArray<sk_sp<GrBuffer>> buffers;
Brian Salomonb17e6392017-07-28 13:41:51 -0400183 proxies.push_back(proxy1);
Brian Salomon559f5562017-11-15 14:28:33 -0500184 auto fp = TestFP::Make(std::move(proxies), std::move(buffers));
Brian Salomonb17e6392017-07-28 13:41:51 -0400185 for (int i = 0; i < parentCnt; ++i) {
186 fp = TestFP::Make(std::move(fp));
187 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400188 std::unique_ptr<GrFragmentProcessor> clone;
Brian Salomonb17e6392017-07-28 13:41:51 -0400189 if (makeClone) {
190 clone = fp->clone();
191 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400192 std::unique_ptr<GrDrawOp> op(TestOp::Make(context, std::move(fp)));
Brian Salomonb17e6392017-07-28 13:41:51 -0400193 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
194 if (clone) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400195 op = TestOp::Make(context, std::move(clone));
Brian Salomonb17e6392017-07-28 13:41:51 -0400196 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
197 }
198 }
199 int refCnt, readCnt, writeCnt;
200
201 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
202 // IO counts should be double if there is a clone of the FP.
203 int ioRefMul = makeClone ? 2 : 1;
Robert Phillips715d08c2018-07-18 13:56:48 -0400204 REPORTER_ASSERT(reporter, -1 == refCnt);
Brian Salomonb17e6392017-07-28 13:41:51 -0400205 REPORTER_ASSERT(reporter, ioRefMul * 1 == readCnt);
206 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
207
Brian Salomonb17e6392017-07-28 13:41:51 -0400208 context->flush();
209
210 testingOnly_getIORefCnts(proxy1.get(), &refCnt, &readCnt, &writeCnt);
211 REPORTER_ASSERT(reporter, 1 == refCnt);
212 REPORTER_ASSERT(reporter, ioRefMul * 0 == readCnt);
213 REPORTER_ASSERT(reporter, ioRefMul * 0 == writeCnt);
214
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500215 }
216 }
217 }
218}
Brian Salomon587e08f2017-01-27 10:59:27 -0500219
220// This test uses the random GrFragmentProcessor test factory, which relies on static initializers.
221#if SK_ALLOW_STATIC_GLOBAL_INITIALIZERS
222
Brian Salomon0e05a822017-07-25 09:43:22 -0400223#include "SkCommandLineFlags.h"
224DEFINE_bool(randomProcessorTest, false, "Use non-deterministic seed for random processor tests?");
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400225DEFINE_uint32(processorSeed, 0, "Use specific seed for processor tests. Overridden by " \
226 "--randomProcessorTest.");
Brian Salomon0e05a822017-07-25 09:43:22 -0400227
228#if GR_TEST_UTILS
229
230static GrColor input_texel_color(int i, int j) {
231 GrColor color = GrColorPackRGBA((uint8_t)j, (uint8_t)(i + j), (uint8_t)(2 * j - i), (uint8_t)i);
Brian Salomon587e08f2017-01-27 10:59:27 -0500232 return GrPremulColor(color);
233}
234
Brian Osman1d5b5982018-10-01 13:41:39 -0400235static SkPMColor4f input_texel_color4f(int i, int j) {
236 return GrColor4f::FromGrColor(input_texel_color(i, j)).asRGBA4f<kPremul_SkAlphaType>();
Brian Salomon0e05a822017-07-25 09:43:22 -0400237}
Brian Salomon587e08f2017-01-27 10:59:27 -0500238
Robert Phillips7c525e62018-06-12 10:11:12 -0400239void test_draw_op(GrContext* context,
240 GrRenderTargetContext* rtc,
241 std::unique_ptr<GrFragmentProcessor> fp,
Robert Phillips30f9bc62017-02-22 15:28:38 -0500242 sk_sp<GrTextureProxy> inputDataProxy) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500243 GrPaint paint;
Brian Osman2240be92017-10-18 13:15:13 -0400244 paint.addColorTextureProcessor(std::move(inputDataProxy), SkMatrix::I());
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500245 paint.addColorFragmentProcessor(std::move(fp));
246 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400247
Robert Phillips7c525e62018-06-12 10:11:12 -0400248 auto op = GrRectOpFactory::MakeNonAAFill(context, std::move(paint), SkMatrix::I(),
Brian Salomonbaaf4392017-06-15 09:59:23 -0400249 SkRect::MakeWH(rtc->width(), rtc->height()),
250 GrAAType::kNone);
Brian Salomonac70f842017-05-08 10:43:33 -0400251 rtc->addDrawOp(GrNoClip(), std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500252}
253
Brian Salomon0e05a822017-07-25 09:43:22 -0400254/** Initializes the two test texture proxies that are available to the FP test factories. */
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500255bool init_test_textures(GrProxyProvider* proxyProvider, SkRandom* random,
256 sk_sp<GrTextureProxy> proxies[2]) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400257 static const int kTestTextureSize = 256;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400258
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500259 {
260 // Put premul data into the RGBA texture that the test FPs can optionally use.
261 std::unique_ptr<GrColor[]> rgbaData(new GrColor[kTestTextureSize * kTestTextureSize]);
262 for (int y = 0; y < kTestTextureSize; ++y) {
263 for (int x = 0; x < kTestTextureSize; ++x) {
264 rgbaData[kTestTextureSize * y + x] =
265 input_texel_color(random->nextULessThan(256), random->nextULessThan(256));
266 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400267 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400268
Brian Osman2700abc2018-09-12 10:19:41 -0400269 SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize,
270 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
271 SkPixmap pixmap(ii, rgbaData.get(), ii.minRowBytes());
272 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
273 proxies[0] = proxyProvider->createTextureProxy(img, kNone_GrSurfaceFlags, 1,
274 SkBudgeted::kYes, SkBackingFit::kExact);
Brian Salomon0e05a822017-07-25 09:43:22 -0400275 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500276
277 {
278 // Put random values into the alpha texture that the test FPs can optionally use.
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500279 std::unique_ptr<uint8_t[]> alphaData(new uint8_t[kTestTextureSize * kTestTextureSize]);
280 for (int y = 0; y < kTestTextureSize; ++y) {
281 for (int x = 0; x < kTestTextureSize; ++x) {
282 alphaData[kTestTextureSize * y + x] = random->nextULessThan(256);
283 }
284 }
285
Brian Osman2700abc2018-09-12 10:19:41 -0400286 SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize,
287 kAlpha_8_SkColorType, kPremul_SkAlphaType);
288 SkPixmap pixmap(ii, alphaData.get(), ii.minRowBytes());
289 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
290 proxies[1] = proxyProvider->createTextureProxy(img, kNone_GrSurfaceFlags, 1,
291 SkBudgeted::kYes, SkBackingFit::kExact);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500292 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400293
294 return proxies[0] && proxies[1];
295}
296
297// Creates a texture of premul colors used as the output of the fragment processor that precedes
298// the fragment processor under test. Color values are those provided by input_texel_color().
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500299sk_sp<GrTextureProxy> make_input_texture(GrProxyProvider* proxyProvider, int width, int height) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400300 std::unique_ptr<GrColor[]> data(new GrColor[width * height]);
301 for (int y = 0; y < width; ++y) {
302 for (int x = 0; x < height; ++x) {
303 data.get()[width * y + x] = input_texel_color(x, y);
304 }
305 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500306
Brian Osman2700abc2018-09-12 10:19:41 -0400307 SkImageInfo ii = SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
308 SkPixmap pixmap(ii, data.get(), ii.minRowBytes());
309 sk_sp<SkImage> img = SkImage::MakeRasterCopy(pixmap);
310 return proxyProvider->createTextureProxy(img, kNone_GrSurfaceFlags, 1,
311 SkBudgeted::kYes, SkBackingFit::kExact);
Brian Salomon0e05a822017-07-25 09:43:22 -0400312}
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500313
Michael Ludwige8e10752018-10-01 12:42:53 -0400314bool log_surface_context(sk_sp<GrSurfaceContext> src, SkString* dst) {
315 SkImageInfo ii = SkImageInfo::Make(src->width(), src->height(), kRGBA_8888_SkColorType,
316 kPremul_SkAlphaType);
317 SkBitmap bm;
318 SkAssertResult(bm.tryAllocPixels(ii));
319 SkAssertResult(src->readPixels(ii, bm.getPixels(), bm.rowBytes(), 0, 0));
320
321 return bitmap_to_base64_data_uri(bm, dst);
322}
323
324bool log_surface_proxy(GrContext* context, sk_sp<GrSurfaceProxy> src, SkString* dst) {
325 sk_sp<GrSurfaceContext> sContext(context->contextPriv().makeWrappedSurfaceContext(src));
326 return log_surface_context(sContext, dst);
327}
328
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500329DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500330 GrContext* context = ctxInfo.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500331 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500332 auto resourceProvider = context->contextPriv().resourceProvider();
Brian Salomon1c053642017-07-24 10:16:19 -0400333 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400334
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400335 uint32_t seed = FLAGS_processorSeed;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400336 if (FLAGS_randomProcessorTest) {
337 std::random_device rd;
338 seed = rd();
339 }
340 // If a non-deterministic bot fails this test, check the output to see what seed it used, then
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400341 // use --processorSeed <seed> (without --randomProcessorTest) to reproduce.
Brian Osmanc35a2d42017-03-17 10:58:53 -0400342 SkRandom random(seed);
343
Brian Salomon0e05a822017-07-25 09:43:22 -0400344 // Make the destination context for the test.
345 static constexpr int kRenderSize = 256;
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500346 sk_sp<GrRenderTargetContext> rtc = context->contextPriv().makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400347 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500348
Robert Phillipse78b7252017-04-06 07:59:41 -0400349 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500350 if (!init_test_textures(proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400351 ERRORF(reporter, "Could not create test textures");
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400352 return;
353 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400354 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
Brian Salomon587e08f2017-01-27 10:59:27 -0500355
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500356 auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500357
Ben Wagner8c791942017-07-25 11:47:48 -0400358 std::unique_ptr<GrColor[]> readData(new GrColor[kRenderSize * kRenderSize]);
Michael Ludwige8e10752018-10-01 12:42:53 -0400359 // Encoded images are very verbose and this tests many potential images, so only export the
360 // first failure (subsequent failures have a reasonable chance of being related).
361 bool loggedFirstFailure = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400362 bool loggedFirstWarning = false;
Brian Salomon0e05a822017-07-25 09:43:22 -0400363 // Because processor factories configure themselves in random ways, this is not exhaustive.
Brian Salomon587e08f2017-01-27 10:59:27 -0500364 for (int i = 0; i < FPFactory::Count(); ++i) {
365 int timesToInvokeFactory = 5;
366 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
367 // on child optimizations being present.
Brian Salomonaff329b2017-08-11 09:40:37 -0400368 std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
Brian Salomon587e08f2017-01-27 10:59:27 -0500369 for (int j = 0; j < fp->numChildProcessors(); ++j) {
370 // This value made a reasonable trade off between time and coverage when this test was
371 // written.
372 timesToInvokeFactory *= FPFactory::Count() / 2;
373 }
374 for (int j = 0; j < timesToInvokeFactory; ++j) {
375 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillips6be756b2018-01-16 15:07:54 -0500376 if (!fp->instantiate(resourceProvider)) {
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400377 continue;
378 }
379
Brian Salomon587e08f2017-01-27 10:59:27 -0500380 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500381 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500382 continue;
383 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400384
385 // Since we transfer away ownership of the original FP, we make a clone.
386 auto clone = fp->clone();
387
Robert Phillips7c525e62018-06-12 10:11:12 -0400388 test_draw_op(context, rtc.get(), std::move(fp), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400389 memset(readData.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
390 rtc->readPixels(SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
391 kPremul_SkAlphaType),
392 readData.get(), 0, 0, 0);
Brian Salomon587e08f2017-01-27 10:59:27 -0500393 if (0) { // Useful to see what FPs are being tested.
394 SkString children;
Brian Salomonaff329b2017-08-11 09:40:37 -0400395 for (int c = 0; c < clone->numChildProcessors(); ++c) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500396 if (!c) {
397 children.append("(");
398 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400399 children.append(clone->name());
400 children.append(c == clone->numChildProcessors() - 1 ? ")" : ", ");
Brian Salomon587e08f2017-01-27 10:59:27 -0500401 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400402 SkDebugf("%s %s\n", clone->name(), children.c_str());
Brian Salomon587e08f2017-01-27 10:59:27 -0500403 }
Michael Ludwig314d3772018-10-03 16:04:38 -0400404
405 // This test has a history of being flaky on a number of devices. If an FP is logically
406 // violating the optimizations, it's reasonable to expect it to violate requirements on
407 // a large number of pixels in the image. Sporadic pixel violations are more indicative
408 // of device errors and represents a separate problem.
409#if defined(SK_SKQP_GLOBAL_ERROR_TOLERANCE)
410 static constexpr int kMaxAcceptableFailedPixels = 0; // Strict when running as SKQP
411#else
412 static constexpr int kMaxAcceptableFailedPixels = 2 * kRenderSize; // ~0.7% of the image
413#endif
414
415 int failedPixelCount = 0;
416 // Collect first optimization failure message, to be output later as a warning or an
417 // error depending on whether the rendering "passed" or failed.
418 SkString coverageMessage;
419 SkString opaqueMessage;
420 SkString constMessage;
421 for (int y = 0; y < kRenderSize; ++y) {
422 for (int x = 0; x < kRenderSize; ++x) {
423 bool passing = true;
Brian Salomon0e05a822017-07-25 09:43:22 -0400424 GrColor input = input_texel_color(x, y);
425 GrColor output = readData.get()[y * kRenderSize + x];
Brian Salomonaff329b2017-08-11 09:40:37 -0400426 if (clone->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500427 // A modulating processor is allowed to modulate either the input color or
428 // just the input alpha.
429 bool legalColorModulation =
430 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
431 GrColorUnpackR(output) <= GrColorUnpackR(input) &&
432 GrColorUnpackG(output) <= GrColorUnpackG(input) &&
433 GrColorUnpackB(output) <= GrColorUnpackB(input);
434 bool legalAlphaModulation =
435 GrColorUnpackA(output) <= GrColorUnpackA(input) &&
436 GrColorUnpackR(output) <= GrColorUnpackA(input) &&
437 GrColorUnpackG(output) <= GrColorUnpackA(input) &&
438 GrColorUnpackB(output) <= GrColorUnpackA(input);
439 if (!legalColorModulation && !legalAlphaModulation) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500440 passing = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400441
442 if (coverageMessage.isEmpty()) {
443 coverageMessage.printf("\"Modulating\" processor %s made color/"
444 "alpha value larger. Input: 0x%08x, Output: 0x%08x, pixel "
445 "(%d, %d).", clone->name(), input, output, x, y);
446 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500447 }
448 }
Brian Osman1d5b5982018-10-01 13:41:39 -0400449 SkPMColor4f input4f = input_texel_color4f(x, y);
Brian Salomon587e08f2017-01-27 10:59:27 -0500450 GrColor4f output4f = GrColor4f::FromGrColor(output);
Brian Osman1d5b5982018-10-01 13:41:39 -0400451 SkPMColor4f expected4f;
Brian Salomonaff329b2017-08-11 09:40:37 -0400452 if (clone->hasConstantOutputForConstantInput(input4f, &expected4f)) {
Brian Osman1d5b5982018-10-01 13:41:39 -0400453 float rDiff = fabsf(output4f.fRGBA[0] - expected4f.fR);
454 float gDiff = fabsf(output4f.fRGBA[1] - expected4f.fG);
455 float bDiff = fabsf(output4f.fRGBA[2] - expected4f.fB);
456 float aDiff = fabsf(output4f.fRGBA[3] - expected4f.fA);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500457 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500458 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
Michael Ludwig314d3772018-10-03 16:04:38 -0400459 if (constMessage.isEmpty()) {
460 passing = false;
461
462 constMessage.printf("Processor %s claimed output for const input "
463 "doesn't match actual output. Error: %f, Tolerance: %f, "
464 "input: (%f, %f, %f, %f), actual: (%f, %f, %f, %f), "
465 "expected(%f, %f, %f, %f)", clone->name(),
466 SkTMax(rDiff, SkTMax(gDiff, SkTMax(bDiff, aDiff))), kTol,
467 input4f.fR, input4f.fG, input4f.fB, input4f.fA,
468 output4f.fRGBA[0], output4f.fRGBA[1], output4f.fRGBA[2],
469 output4f.fRGBA[3], expected4f.fR, expected4f.fG,
470 expected4f.fB, expected4f.fA);
471 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500472 }
473 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400474 if (GrColorIsOpaque(input) && clone->preservesOpaqueInput() &&
Brian Salomon587e08f2017-01-27 10:59:27 -0500475 !GrColorIsOpaque(output)) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500476 passing = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400477
478 if (opaqueMessage.isEmpty()) {
479 opaqueMessage.printf("Processor %s claimed opaqueness is preserved but "
480 "it is not. Input: 0x%08x, Output: 0x%08x.",
481 clone->name(), input, output);
Michael Ludwige8e10752018-10-01 12:42:53 -0400482 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400483 }
Michael Ludwig314d3772018-10-03 16:04:38 -0400484
485 if (!passing) {
486 // Regardless of how many optimizations the pixel violates, count it as a
487 // single bad pixel.
488 failedPixelCount++;
489 }
490 }
491 }
492
493 // Finished analyzing the entire image, see if the number of pixel failures meets the
494 // threshold for an FP violating the optimization requirements.
495 if (failedPixelCount > kMaxAcceptableFailedPixels) {
496 ERRORF(reporter, "Processor violated %d of %d pixels, seed: 0x%08x, processor: %s",
497 ", first failing pixel details are below:",
498 failedPixelCount, kRenderSize * kRenderSize, seed,
499 clone->dumpInfo().c_str());
500
501 // Print first failing pixel's details.
502 if (!coverageMessage.isEmpty()) {
503 ERRORF(reporter, coverageMessage.c_str());
504 }
505 if (!constMessage.isEmpty()) {
506 ERRORF(reporter, constMessage.c_str());
507 }
508 if (!opaqueMessage.isEmpty()) {
509 ERRORF(reporter, opaqueMessage.c_str());
510 }
511
512 if (!loggedFirstFailure) {
513 // Print with ERRORF to make sure the encoded image is output
514 SkString input;
515 log_surface_proxy(context, inputTexture, &input);
516 SkString output;
517 log_surface_context(rtc, &output);
518 ERRORF(reporter, "Input image: %s\n\n"
519 "===========================================================\n\n"
520 "Output image: %s\n", input.c_str(), output.c_str());
521 loggedFirstFailure = true;
522 }
523 } else {
524 // Don't trigger an error, but don't just hide the failures either.
525 INFOF(reporter, "Processor violated %d of %d pixels (below error threshold), seed: "
526 "0x%08x, processor: %s", failedPixelCount, kRenderSize * kRenderSize,
527 seed, clone->dumpInfo().c_str());
528 if (!coverageMessage.isEmpty()) {
529 INFOF(reporter, coverageMessage.c_str());
530 }
531 if (!constMessage.isEmpty()) {
532 INFOF(reporter, constMessage.c_str());
533 }
534 if (!opaqueMessage.isEmpty()) {
535 INFOF(reporter, opaqueMessage.c_str());
536 }
537 if (!loggedFirstWarning) {
538 SkString input;
539 log_surface_proxy(context, inputTexture, &input);
540 SkString output;
541 log_surface_context(rtc, &output);
542 INFOF(reporter, "Input image: %s\n\n"
543 "===========================================================\n\n"
544 "Output image: %s\n", input.c_str(), output.c_str());
545 loggedFirstWarning = true;
Brian Salomon587e08f2017-01-27 10:59:27 -0500546 }
547 }
548 }
549 }
550}
Robert Phillips18166ee2017-06-01 12:55:44 -0400551
Brian Salomon0e05a822017-07-25 09:43:22 -0400552// Tests that fragment processors returned by GrFragmentProcessor::clone() are equivalent to their
553// progenitors.
554DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
555 GrContext* context = ctxInfo.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500556 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500557 auto resourceProvider = context->contextPriv().resourceProvider();
Brian Salomon0e05a822017-07-25 09:43:22 -0400558
559 SkRandom random;
560
561 // Make the destination context for the test.
562 static constexpr int kRenderSize = 1024;
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500563 sk_sp<GrRenderTargetContext> rtc = context->contextPriv().makeDeferredRenderTargetContext(
Brian Salomon0e05a822017-07-25 09:43:22 -0400564 SkBackingFit::kExact, kRenderSize, kRenderSize, kRGBA_8888_GrPixelConfig, nullptr);
565
566 sk_sp<GrTextureProxy> proxies[2];
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500567 if (!init_test_textures(proxyProvider, &random, proxies)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400568 ERRORF(reporter, "Could not create test textures");
569 return;
570 }
571 GrProcessorTestData testData(&random, context, rtc.get(), proxies);
572
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500573 auto inputTexture = make_input_texture(proxyProvider, kRenderSize, kRenderSize);
Brian Salomon0e05a822017-07-25 09:43:22 -0400574 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
575 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
576 auto readInfo = SkImageInfo::Make(kRenderSize, kRenderSize, kRGBA_8888_SkColorType,
577 kPremul_SkAlphaType);
578
579 // Because processor factories configure themselves in random ways, this is not exhaustive.
580 for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
581 static constexpr int kTimesToInvokeFactory = 10;
582 for (int j = 0; j < kTimesToInvokeFactory; ++j) {
583 auto fp = GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
584 auto clone = fp->clone();
Brian Salomon0e05a822017-07-25 09:43:22 -0400585 if (!clone) {
Brian Salomon96271cd2017-07-31 16:27:23 -0400586 ERRORF(reporter, "Clone of processor %s failed.", fp->name());
Brian Salomon0e05a822017-07-25 09:43:22 -0400587 continue;
588 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400589 const char* name = fp->name();
Robert Phillips6be756b2018-01-16 15:07:54 -0500590 if (!fp->instantiate(resourceProvider) || !clone->instantiate(resourceProvider)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400591 continue;
592 }
Brian Salomonce06e262017-08-01 16:23:40 -0400593 REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name()));
594 REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() ==
595 clone->compatibleWithCoverageAsAlpha());
596 REPORTER_ASSERT(reporter, fp->isEqual(*clone));
597 REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput());
598 REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() ==
599 clone->hasConstantOutputForConstantInput());
600 REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors());
601 REPORTER_ASSERT(reporter, fp->usesLocalCoords() == clone->usesLocalCoords());
Brian Salomon0e05a822017-07-25 09:43:22 -0400602 // Draw with original and read back the results.
Robert Phillips7c525e62018-06-12 10:11:12 -0400603 test_draw_op(context, rtc.get(), std::move(fp), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400604 memset(readData1.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
605 rtc->readPixels(readInfo, readData1.get(), 0, 0, 0);
606
607 // Draw with clone and read back the results.
Robert Phillips7c525e62018-06-12 10:11:12 -0400608 test_draw_op(context, rtc.get(), std::move(clone), inputTexture);
Brian Salomon0e05a822017-07-25 09:43:22 -0400609 memset(readData2.get(), 0x0, sizeof(GrColor) * kRenderSize * kRenderSize);
610 rtc->readPixels(readInfo, readData2.get(), 0, 0, 0);
611
612 // Check that the results are the same.
613 bool passing = true;
614 for (int y = 0; y < kRenderSize && passing; ++y) {
615 for (int x = 0; x < kRenderSize && passing; ++x) {
616 int idx = y * kRenderSize + x;
617 if (readData1[idx] != readData2[idx]) {
618 ERRORF(reporter,
619 "Processor %s made clone produced different output. "
620 "Input color: 0x%08x, Original Output Color: 0x%08x, "
621 "Clone Output Color: 0x%08x..",
Brian Salomonaff329b2017-08-11 09:40:37 -0400622 name, input_texel_color(x, y), readData1[idx], readData2[idx]);
Brian Salomon0e05a822017-07-25 09:43:22 -0400623 passing = false;
624 }
625 }
626 }
627 }
628 }
629}
630
Hal Canary6f6961e2017-01-31 13:50:44 -0500631#endif // GR_TEST_UTILS
632#endif // SK_ALLOW_STATIC_GLOBAL_INITIALIZERS