blob: 4142e562e60ac22a2a154a3c5fac59798f1f239e [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tests/Test.h"
Brian Salomonbc6b99d2017-01-11 10:32:34 -05009
Robert Phillips6d344c32020-07-06 10:56:46 -040010#include "include/gpu/GrDirectContext.h"
Greg Daniel6f5441a2020-01-28 17:02:49 -050011#include "src/gpu/GrBitmapTextureMaker.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrClip.h"
13#include "src/gpu/GrContextPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000014#include "src/gpu/GrGpuResource.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040015#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrMemoryPool.h"
17#include "src/gpu/GrProxyProvider.h"
18#include "src/gpu/GrRenderTargetContext.h"
19#include "src/gpu/GrRenderTargetContextPriv.h"
20#include "src/gpu/GrResourceProvider.h"
21#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
22#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
23#include "src/gpu/ops/GrFillRectOp.h"
24#include "src/gpu/ops/GrMeshDrawOp.h"
25#include "tests/TestUtils.h"
Mike Klein0ec1c572018-12-04 11:52:51 -050026#include <atomic>
Hal Canary8a001442018-09-19 11:31:27 -040027#include <random>
28
Brian Salomonbc6b99d2017-01-11 10:32:34 -050029namespace {
Brian Salomon82ddc942017-07-14 12:00:13 -040030class TestOp : public GrMeshDrawOp {
Brian Salomonbc6b99d2017-01-11 10:32:34 -050031public:
32 DEFINE_OP_CLASS_ID
Robert Phillips7c525e62018-06-12 10:11:12 -040033 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
34 std::unique_ptr<GrFragmentProcessor> fp) {
Robert Phillips9da87e02019-02-04 13:26:26 -050035 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -040036
37 return pool->allocate<TestOp>(std::move(fp));
Brian Salomon82ddc942017-07-14 12:00:13 -040038 }
39
Robert Phillips5f567c72017-09-14 08:27:37 -040040 const char* name() const override { return "TestOp"; }
41
Chris Dalton1706cbf2019-05-21 19:35:29 -060042 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips5f567c72017-09-14 08:27:37 -040043 fProcessors.visitProxies(func);
44 }
45
Brian Salomon82ddc942017-07-14 12:00:13 -040046 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
47
Chris Dalton6ce447a2019-06-23 18:07:38 -060048 GrProcessorSet::Analysis finalize(
49 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
50 GrClampType clampType) override {
Brian Salomon82ddc942017-07-14 12:00:13 -040051 static constexpr GrProcessorAnalysisColor kUnknownColor;
Brian Osmancf860852018-10-31 14:04:39 -040052 SkPMColor4f overrideColor;
Chris Daltonb8fff0d2019-03-05 10:11:58 -070053 return fProcessors.finalize(
54 kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip,
Chris Dalton6ce447a2019-06-23 18:07:38 -060055 &GrUserStencilSettings::kUnused, hasMixedSampledCoverage, caps, clampType,
56 &overrideColor);
Brian Salomon649a3412017-03-09 13:50:43 -050057 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050058
59private:
Robert Phillips7c525e62018-06-12 10:11:12 -040060 friend class ::GrOpMemoryPool; // for ctor
61
Brian Salomonaff329b2017-08-11 09:40:37 -040062 TestOp(std::unique_ptr<GrFragmentProcessor> fp)
63 : INHERITED(ClassID()), fProcessors(std::move(fp)) {
Greg Daniel5faf4742019-10-01 15:14:44 -040064 this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsHairline::kNo);
Brian Salomon82ddc942017-07-14 12:00:13 -040065 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050066
Robert Phillips2669a7b2020-03-12 12:07:19 -040067 GrProgramInfo* programInfo() override { return nullptr; }
Robert Phillips4133dc42020-03-11 15:55:55 -040068 void onCreateProgramInfo(const GrCaps*,
69 SkArenaAlloc*,
Brian Salomon8afde5f2020-04-01 16:22:00 -040070 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -040071 GrAppliedClip&&,
72 const GrXferProcessor::DstProxyView&) override { return; }
Robert Phillips2669a7b2020-03-12 12:07:19 -040073 void onPrePrepareDraws(GrRecordingContext*,
Brian Salomon8afde5f2020-04-01 16:22:00 -040074 const GrSurfaceProxyView* writeView,
Robert Phillips2669a7b2020-03-12 12:07:19 -040075 GrAppliedClip*,
76 const GrXferProcessor::DstProxyView&) override { return; }
Brian Salomon91326c32017-08-09 16:02:19 -040077 void onPrepareDraws(Target* target) override { return; }
Chris Dalton07cdcfc92019-02-26 11:13:22 -070078 void onExecute(GrOpFlushState*, const SkRect&) override { return; }
Brian Salomonbc6b99d2017-01-11 10:32:34 -050079
Brian Salomon82ddc942017-07-14 12:00:13 -040080 GrProcessorSet fProcessors;
81
82 typedef GrMeshDrawOp INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -050083};
84
85/**
Robert Phillips3d4cac52019-06-11 08:08:08 -040086 * FP used to test ref counts on owned GrGpuResources. Can also be a parent FP to test counts
Brian Salomonbc6b99d2017-01-11 10:32:34 -050087 * of resources owned by child FPs.
88 */
89class TestFP : public GrFragmentProcessor {
90public:
Brian Salomonaff329b2017-08-11 09:40:37 -040091 static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) {
92 return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child)));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050093 }
Brian Salomon92b9ccf2020-06-17 14:19:19 -040094 static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<GrSurfaceProxyView>& views) {
95 return std::unique_ptr<GrFragmentProcessor>(new TestFP(views));
Brian Salomonbc6b99d2017-01-11 10:32:34 -050096 }
97
98 const char* name() const override { return "test"; }
99
100 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
Mike Klein0ec1c572018-12-04 11:52:51 -0500101 static std::atomic<int32_t> nextKey{0};
102 b->add32(nextKey++);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500103 }
104
Brian Salomonaff329b2017-08-11 09:40:37 -0400105 std::unique_ptr<GrFragmentProcessor> clone() const override {
106 return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this));
Brian Salomonb17e6392017-07-28 13:41:51 -0400107 }
108
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500109private:
Brian Salomon92b9ccf2020-06-17 14:19:19 -0400110 TestFP(const SkTArray<GrSurfaceProxyView>& views)
111 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags) {
Greg Daniel026a60c2020-02-12 10:53:51 -0500112 for (const auto& view : views) {
Michael Ludwig9aba6252020-06-22 14:46:36 -0400113 this->registerChild(GrTextureEffect::Make(view, kUnknown_SkAlphaType));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500114 }
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500115 }
116
Brian Salomonaff329b2017-08-11 09:40:37 -0400117 TestFP(std::unique_ptr<GrFragmentProcessor> child)
Brian Salomon92b9ccf2020-06-17 14:19:19 -0400118 : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags) {
Michael Ludwig9aba6252020-06-22 14:46:36 -0400119 this->registerChild(std::move(child));
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500120 }
121
Brian Salomon92b9ccf2020-06-17 14:19:19 -0400122 explicit TestFP(const TestFP& that) : INHERITED(kTestFP_ClassID, that.optimizationFlags()) {
123 this->cloneAndRegisterAllChildProcessors(that);
Brian Salomonb17e6392017-07-28 13:41:51 -0400124 }
125
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500126 virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
127 class TestGLSLFP : public GrGLSLFragmentProcessor {
128 public:
129 TestGLSLFP() {}
130 void emitCode(EmitArgs& args) override {
131 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
132 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, args.fInputColor);
133 }
134
135 private:
136 };
137 return new TestGLSLFP();
138 }
139
140 bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
141
Brian Salomon587e08f2017-01-27 10:59:27 -0500142 typedef GrFragmentProcessor INHERITED;
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500143};
144}
145
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500146DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400147 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500148 GrProxyProvider* proxyProvider = context->priv().proxyProvider();
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500149
Brian Salomona56a7462020-02-07 14:17:25 -0500150 static constexpr SkISize kDims = {10, 10};
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500151
Greg Daniel4065d452018-11-16 15:43:41 -0500152 const GrBackendFormat format =
Robert Phillips0a15cc62019-07-30 12:49:10 -0400153 context->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
154 GrRenderable::kNo);
Greg Daniel47c20e82020-01-21 14:29:57 -0500155 GrSwizzle swizzle = context->priv().caps()->getReadSwizzle(format, GrColorType::kRGBA_8888);
Greg Daniel4065d452018-11-16 15:43:41 -0500156
Brian Salomonb17e6392017-07-28 13:41:51 -0400157 for (bool makeClone : {false, true}) {
158 for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
Greg Daniele20fcad2020-01-08 11:52:34 -0500159 auto renderTargetContext = GrRenderTargetContext::Make(
160 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1, 1});
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500161 {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400162 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
Brian Salomondf1bd6d2020-03-26 20:37:01 -0400163 format, kDims, GrRenderable::kNo, 1, GrMipMapped::kNo, SkBackingFit::kExact,
164 SkBudgeted::kYes, GrProtected::kNo);
Robert Phillips3d4cac52019-06-11 08:08:08 -0400165
Brian Salomonb17e6392017-07-28 13:41:51 -0400166 {
Greg Daniel026a60c2020-02-12 10:53:51 -0500167 SkTArray<GrSurfaceProxyView> views;
Greg Daniel026a60c2020-02-12 10:53:51 -0500168 views.push_back({proxy, kTopLeft_GrSurfaceOrigin, swizzle});
Brian Salomon92b9ccf2020-06-17 14:19:19 -0400169 auto fp = TestFP::Make(std::move(views));
Brian Salomonb17e6392017-07-28 13:41:51 -0400170 for (int i = 0; i < parentCnt; ++i) {
171 fp = TestFP::Make(std::move(fp));
172 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400173 std::unique_ptr<GrFragmentProcessor> clone;
Brian Salomonb17e6392017-07-28 13:41:51 -0400174 if (makeClone) {
175 clone = fp->clone();
176 }
Robert Phillips7c525e62018-06-12 10:11:12 -0400177 std::unique_ptr<GrDrawOp> op(TestOp::Make(context, std::move(fp)));
Brian Salomonb17e6392017-07-28 13:41:51 -0400178 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
179 if (clone) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400180 op = TestOp::Make(context, std::move(clone));
Brian Salomonb17e6392017-07-28 13:41:51 -0400181 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
182 }
183 }
Brian Salomonb17e6392017-07-28 13:41:51 -0400184
Robert Phillips3d4cac52019-06-11 08:08:08 -0400185 // If the fp is cloned the number of refs should increase by one (for the clone)
186 int expectedProxyRefs = makeClone ? 3 : 2;
187
Brian Salomon28a8f282019-10-24 20:07:39 -0400188 CheckSingleThreadedProxyRefs(reporter, proxy.get(), expectedProxyRefs, -1);
Brian Salomonb17e6392017-07-28 13:41:51 -0400189
Greg Daniel0a2464f2020-05-14 15:45:44 -0400190 context->flushAndSubmit();
Brian Salomonb17e6392017-07-28 13:41:51 -0400191
Brian Salomon557e8122019-10-24 10:37:08 -0400192 // just one from the 'proxy' sk_sp
Brian Salomon28a8f282019-10-24 20:07:39 -0400193 CheckSingleThreadedProxyRefs(reporter, proxy.get(), 1, 1);
Brian Salomonbc6b99d2017-01-11 10:32:34 -0500194 }
195 }
196 }
197}
Brian Salomon587e08f2017-01-27 10:59:27 -0500198
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500199#include "tools/flags/CommandLineFlags.h"
Mike Klein84836b72019-03-21 11:31:36 -0500200static DEFINE_bool(randomProcessorTest, false,
201 "Use non-deterministic seed for random processor tests?");
Mike Klein5b3f3432019-03-21 11:42:21 -0500202static DEFINE_int(processorSeed, 0,
203 "Use specific seed for processor tests. Overridden by --randomProcessorTest.");
Brian Salomon0e05a822017-07-25 09:43:22 -0400204
205#if GR_TEST_UTILS
206
Michael Ludwig7034f152018-10-08 16:43:58 -0400207static GrColor input_texel_color(int i, int j, SkScalar delta) {
208 // Delta must be less than 0.5 to prevent over/underflow issues with the input color
209 SkASSERT(delta <= 0.5);
Brian Salomon587e08f2017-01-27 10:59:27 -0500210
Brian Osman50ea3c02019-02-04 10:01:53 -0500211 SkColor color = SkColorSetARGB((uint8_t)(i & 0xFF),
212 (uint8_t)(j & 0xFF),
213 (uint8_t)((i + j) & 0xFF),
214 (uint8_t)((2 * j - i) & 0xFF));
Brian Osmancb3d0872018-10-16 15:19:28 -0400215 SkColor4f color4f = SkColor4f::FromColor(color);
Brian Salomonbc73eb42019-12-18 14:57:45 -0500216 // We only apply delta to the r,g, and b channels. This is because we're using this
217 // to test the canTweakAlphaForCoverage() optimization. A processor is allowed
218 // to use the input color's alpha in its calculation and report this optimization.
219 for (int i = 0; i < 3; i++) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400220 if (color4f[i] > 0.5) {
221 color4f[i] -= delta;
Michael Ludwig7034f152018-10-08 16:43:58 -0400222 } else {
Brian Osmancb3d0872018-10-16 15:19:28 -0400223 color4f[i] += delta;
Michael Ludwig7034f152018-10-08 16:43:58 -0400224 }
225 }
Brian Osmancb3d0872018-10-16 15:19:28 -0400226 return color4f.premul().toBytes_RGBA();
Brian Salomon0e05a822017-07-25 09:43:22 -0400227}
Brian Salomon587e08f2017-01-27 10:59:27 -0500228
Robert Phillips7c525e62018-06-12 10:11:12 -0400229void test_draw_op(GrContext* context,
230 GrRenderTargetContext* rtc,
231 std::unique_ptr<GrFragmentProcessor> fp,
Greg Daniel124486b2020-02-11 11:54:55 -0500232 GrSurfaceProxyView inputDataView,
Brian Salomonfc118442019-11-22 19:09:27 -0500233 SkAlphaType inputAlphaType) {
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500234 GrPaint paint;
Greg Daniel124486b2020-02-11 11:54:55 -0500235 paint.addColorFragmentProcessor(GrTextureEffect::Make(std::move(inputDataView),
236 inputAlphaType));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500237 paint.addColorFragmentProcessor(std::move(fp));
238 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonac70f842017-05-08 10:43:33 -0400239
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400240 auto op = GrFillRectOp::MakeNonAARect(context, std::move(paint), SkMatrix::I(),
241 SkRect::MakeWH(rtc->width(), rtc->height()));
Brian Salomonfc118442019-11-22 19:09:27 -0500242 rtc->priv().testingOnly_addDrawOp(std::move(op));
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500243}
244
Greg Daniel124486b2020-02-11 11:54:55 -0500245// This assumes that the output buffer will be the same size as inputDataView
Brian Salomon3f4de782020-06-18 14:16:00 -0400246void render_fp(GrContext* context,
247 GrRenderTargetContext* rtc,
248 std::unique_ptr<GrFragmentProcessor> fp,
249 GrSurfaceProxyView inputDataView,
250 SkAlphaType inputAlphaType,
251 GrColor* buffer) {
252 test_draw_op(context, rtc, std::move(fp), inputDataView, inputAlphaType);
Greg Daniel124486b2020-02-11 11:54:55 -0500253 memset(buffer, 0x0,
254 sizeof(GrColor) * inputDataView.proxy()->width() * inputDataView.proxy()->height());
255 rtc->readPixels(SkImageInfo::Make(inputDataView.proxy()->dimensions(), kRGBA_8888_SkColorType,
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400256 kPremul_SkAlphaType),
Brian Salomon1d435302019-07-01 13:05:28 -0400257 buffer, 0, {0, 0});
Michael Ludwig7034f152018-10-08 16:43:58 -0400258}
259
Brian Salomon0e05a822017-07-25 09:43:22 -0400260/** Initializes the two test texture proxies that are available to the FP test factories. */
Robert Phillips7eeb74f2019-03-29 07:26:46 -0400261bool init_test_textures(GrResourceProvider* resourceProvider,
Greg Daniel6f5441a2020-01-28 17:02:49 -0500262 GrRecordingContext* context,
Robert Phillips7eeb74f2019-03-29 07:26:46 -0400263 SkRandom* random,
Greg Daniel026a60c2020-02-12 10:53:51 -0500264 GrProcessorTestData::ViewInfo views[2]) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400265 static const int kTestTextureSize = 256;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400266
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500267 {
268 // Put premul data into the RGBA texture that the test FPs can optionally use.
Greg Daniel6f5441a2020-01-28 17:02:49 -0500269 GrColor* rgbaData = new GrColor[kTestTextureSize * kTestTextureSize];
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500270 for (int y = 0; y < kTestTextureSize; ++y) {
271 for (int x = 0; x < kTestTextureSize; ++x) {
Michael Ludwig7034f152018-10-08 16:43:58 -0400272 rgbaData[kTestTextureSize * y + x] = input_texel_color(
273 random->nextULessThan(256), random->nextULessThan(256), 0.0f);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500274 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400275 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400276
Brian Osman2700abc2018-09-12 10:19:41 -0400277 SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize,
278 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500279 SkBitmap bitmap;
280 bitmap.installPixels(ii, rgbaData, ii.minRowBytes(),
281 [](void* addr, void* context) { delete[] (GrColor*)addr; }, nullptr);
282 bitmap.setImmutable();
Brian Salomonbc074a62020-03-18 10:06:13 -0400283 GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted);
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500284 auto view = maker.view(GrMipMapped::kNo);
Greg Danielcc104db2020-02-03 14:17:08 -0500285 if (!view.proxy() || !view.proxy()->instantiate(resourceProvider)) {
Brian Salomon766098d2019-12-18 10:41:58 -0500286 return false;
287 }
Greg Daniel026a60c2020-02-12 10:53:51 -0500288 views[0] = {view, GrColorType::kRGBA_8888, kPremul_SkAlphaType};
Brian Salomon0e05a822017-07-25 09:43:22 -0400289 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500290
291 {
292 // Put random values into the alpha texture that the test FPs can optionally use.
Greg Daniel6f5441a2020-01-28 17:02:49 -0500293 uint8_t* alphaData = new uint8_t[kTestTextureSize * kTestTextureSize];
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500294 for (int y = 0; y < kTestTextureSize; ++y) {
295 for (int x = 0; x < kTestTextureSize; ++x) {
296 alphaData[kTestTextureSize * y + x] = random->nextULessThan(256);
297 }
298 }
299
Brian Osman2700abc2018-09-12 10:19:41 -0400300 SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize,
301 kAlpha_8_SkColorType, kPremul_SkAlphaType);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500302 SkBitmap bitmap;
303 bitmap.installPixels(ii, alphaData, ii.minRowBytes(),
304 [](void* addr, void* context) { delete[] (uint8_t*)addr; }, nullptr);
305 bitmap.setImmutable();
Brian Salomonbc074a62020-03-18 10:06:13 -0400306 GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted);
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500307 auto view = maker.view(GrMipMapped::kNo);
Greg Danielcc104db2020-02-03 14:17:08 -0500308 if (!view.proxy() || !view.proxy()->instantiate(resourceProvider)) {
Brian Salomon766098d2019-12-18 10:41:58 -0500309 return false;
310 }
Greg Daniel026a60c2020-02-12 10:53:51 -0500311 views[1] = {view, GrColorType::kAlpha_8, kPremul_SkAlphaType};
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500312 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400313
Brian Salomon766098d2019-12-18 10:41:58 -0500314 return true;
Brian Salomon0e05a822017-07-25 09:43:22 -0400315}
316
317// Creates a texture of premul colors used as the output of the fragment processor that precedes
318// the fragment processor under test. Color values are those provided by input_texel_color().
Greg Daniel124486b2020-02-11 11:54:55 -0500319GrSurfaceProxyView make_input_texture(GrRecordingContext* context, int width, int height,
320 SkScalar delta) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500321 GrColor* data = new GrColor[width * height];
Brian Salomon0e05a822017-07-25 09:43:22 -0400322 for (int y = 0; y < width; ++y) {
323 for (int x = 0; x < height; ++x) {
Greg Daniel6f5441a2020-01-28 17:02:49 -0500324 data[width * y + x] = input_texel_color(x, y, delta);
Brian Salomon0e05a822017-07-25 09:43:22 -0400325 }
326 }
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500327
Brian Osman2700abc2018-09-12 10:19:41 -0400328 SkImageInfo ii = SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Greg Daniel6f5441a2020-01-28 17:02:49 -0500329 SkBitmap bitmap;
330 bitmap.installPixels(ii, data, ii.minRowBytes(),
331 [](void* addr, void* context) { delete[] (GrColor*)addr; }, nullptr);
332 bitmap.setImmutable();
Brian Salomonbc074a62020-03-18 10:06:13 -0400333 GrBitmapTextureMaker maker(context, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted);
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500334 return maker.view(GrMipMapped::kNo);
Brian Salomon0e05a822017-07-25 09:43:22 -0400335}
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500336
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400337// We tag logged data as unpremul to avoid conversion when encoding as PNG. The input texture
338// actually contains unpremul data. Also, even though we made the result data by rendering into
339// a "unpremul" GrRenderTargetContext, our input texture is unpremul and outside of the random
340// effect configuration, we didn't do anything to ensure the output is actually premul. We just
341// don't currently allow kUnpremul GrRenderTargetContexts.
342static constexpr auto kLogAlphaType = kUnpremul_SkAlphaType;
Michael Ludwige8e10752018-10-01 12:42:53 -0400343
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400344bool log_pixels(GrColor* pixels, int widthHeight, SkString* dst) {
345 auto info = SkImageInfo::Make(widthHeight, widthHeight, kRGBA_8888_SkColorType, kLogAlphaType);
346 SkBitmap bmp;
347 bmp.installPixels(info, pixels, widthHeight * sizeof(GrColor));
Brian Salomon28a8f282019-10-24 20:07:39 -0400348 return BipmapToBase64DataURI(bmp, dst);
Michael Ludwige8e10752018-10-01 12:42:53 -0400349}
350
Greg Daniel124486b2020-02-11 11:54:55 -0500351bool log_texture_view(GrContext* context, GrSurfaceProxyView src, SkString* dst) {
352 SkImageInfo ii = SkImageInfo::Make(src.proxy()->dimensions(), kRGBA_8888_SkColorType,
353 kLogAlphaType);
Greg Daniel3912a4b2020-01-14 09:56:04 -0500354
Greg Daniel124486b2020-02-11 11:54:55 -0500355 auto sContext = GrSurfaceContext::Make(context, std::move(src), GrColorType::kRGBA_8888,
Greg Daniel3912a4b2020-01-14 09:56:04 -0500356 kLogAlphaType, nullptr);
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400357 SkBitmap bm;
358 SkAssertResult(bm.tryAllocPixels(ii));
359 SkAssertResult(sContext->readPixels(ii, bm.getPixels(), bm.rowBytes(), {0, 0}));
Brian Salomon28a8f282019-10-24 20:07:39 -0400360 return BipmapToBase64DataURI(bm, dst);
Michael Ludwige8e10752018-10-01 12:42:53 -0400361}
362
Michael Ludwig7034f152018-10-08 16:43:58 -0400363bool fuzzy_color_equals(const SkPMColor4f& c1, const SkPMColor4f& c2) {
364 // With the loss of precision of rendering into 32-bit color, then estimating the FP's output
Brian Salomon682ba432019-12-17 20:20:23 +0000365 // from that, it is not uncommon for a valid output to differ from estimate by up to 0.01
Michael Ludwig7034f152018-10-08 16:43:58 -0400366 // (really 1/128 ~ .0078, but frequently floating point issues make that tolerance a little
367 // too unforgiving).
Brian Salomon682ba432019-12-17 20:20:23 +0000368 static constexpr SkScalar kTolerance = 0.01f;
Michael Ludwig7034f152018-10-08 16:43:58 -0400369 for (int i = 0; i < 4; i++) {
370 if (!SkScalarNearlyEqual(c1[i], c2[i], kTolerance)) {
371 return false;
372 }
373 }
374 return true;
375}
376
Brian Salomonbc73eb42019-12-18 14:57:45 -0500377// Given three input colors (color preceding the FP being tested) provided to the FP at the same
378// local coord and the three corresponding FP outputs, this ensures that either:
379// out[0] = fp * in[0].a, out[1] = fp * in[1].a, and out[2] = fp * in[2].a
380// where fp is the pre-modulated color that should not be changing across frames (FP's state doesn't
381// change), OR:
382// out[0] = fp * in[0], out[1] = fp * in[1], and out[2] = fp * in[2]
383// (per-channel modulation instead of modulation by just the alpha channel)
384// It does this by estimating the pre-modulated fp color from one of the input/output pairs and
385// confirms the conditions hold for the other two pairs.
386// It is required that the three input colors have the same alpha as fp is allowed to be a function
387// of the input alpha (but not r, g, or b).
388bool legal_modulation(const GrColor in[3], const GrColor out[3]) {
Michael Ludwig7034f152018-10-08 16:43:58 -0400389 // Convert to floating point, which is the number space the FP operates in (more or less)
Brian Salomonbc73eb42019-12-18 14:57:45 -0500390 SkPMColor4f inf[3], outf[3];
391 for (int i = 0; i < 3; ++i) {
392 inf[i] = SkPMColor4f::FromBytes_RGBA(in[i]);
393 outf[i] = SkPMColor4f::FromBytes_RGBA(out[i]);
394 }
395 // This test is only valid if all the input alphas are the same.
396 SkASSERT(inf[0].fA == inf[1].fA && inf[1].fA == inf[2].fA);
Michael Ludwig7034f152018-10-08 16:43:58 -0400397
398 // Reconstruct the output of the FP before the shader modulated its color with the input value.
399 // When the original input is very small, it may cause the final output color to round
400 // to 0, in which case we estimate the pre-modulated color using one of the stepped frames that
401 // will then have a guaranteed larger channel value (since the offset will be added to it).
Brian Salomon0a7ca7a2019-12-27 12:18:19 -0500402 SkPMColor4f fpPreColorModulation = {0,0,0,0};
403 SkPMColor4f fpPreAlphaModulation = {0,0,0,0};
Michael Ludwig7034f152018-10-08 16:43:58 -0400404 for (int i = 0; i < 4; i++) {
Brian Salomonbc73eb42019-12-18 14:57:45 -0500405 // Use the most stepped up frame
406 int maxInIdx = inf[0][i] > inf[1][i] ? 0 : 1;
407 maxInIdx = inf[maxInIdx][i] > inf[2][i] ? maxInIdx : 2;
408 const auto& in = inf[maxInIdx];
409 const auto& out = outf[maxInIdx];
Brian Salomon0a7ca7a2019-12-27 12:18:19 -0500410 if (in[i] > 0) {
411 fpPreColorModulation[i] = out[i] / in[i];
412 }
413 if (in[3] > 0) {
414 fpPreAlphaModulation[i] = out[i] / in[3];
415 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400416 }
417
418 // With reconstructed pre-modulated FP output, derive the expected value of fp * input for each
419 // of the transformed input colors.
Brian Salomonbc73eb42019-12-18 14:57:45 -0500420 SkPMColor4f expectedForAlphaModulation[3];
421 SkPMColor4f expectedForColorModulation[3];
422 for (int i = 0; i < 3; ++i) {
423 expectedForAlphaModulation[i] = fpPreAlphaModulation * inf[i].fA;
424 expectedForColorModulation[i] = fpPreColorModulation * inf[i];
425 // If the input alpha is 0 then the other channels should also be zero
426 // since the color is assumed to be premul. Modulating zeros by anything
427 // should produce zeros.
428 if (inf[i].fA == 0) {
429 SkASSERT(inf[i].fR == 0 && inf[i].fG == 0 && inf[i].fB == 0);
430 expectedForColorModulation[i] = expectedForAlphaModulation[i] = {0, 0, 0, 0};
431 }
432 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400433
Brian Salomonbc73eb42019-12-18 14:57:45 -0500434 bool isLegalColorModulation = fuzzy_color_equals(outf[0], expectedForColorModulation[0]) &&
435 fuzzy_color_equals(outf[1], expectedForColorModulation[1]) &&
436 fuzzy_color_equals(outf[2], expectedForColorModulation[2]);
437
438 bool isLegalAlphaModulation = fuzzy_color_equals(outf[0], expectedForAlphaModulation[0]) &&
439 fuzzy_color_equals(outf[1], expectedForAlphaModulation[1]) &&
440 fuzzy_color_equals(outf[2], expectedForAlphaModulation[2]);
441
442 // This can be enabled to print the values that caused this check to fail.
443 if (0 && !isLegalColorModulation && !isLegalAlphaModulation) {
444 SkDebugf("Color modulation test\n\timplied mod color: (%.03f, %.03f, %.03f, %.03f)\n",
445 fpPreColorModulation[0],
446 fpPreColorModulation[1],
447 fpPreColorModulation[2],
448 fpPreColorModulation[3]);
449 for (int i = 0; i < 3; ++i) {
450 SkDebugf("\t(%.03f, %.03f, %.03f, %.03f) -> "
451 "(%.03f, %.03f, %.03f, %.03f) | "
452 "(%.03f, %.03f, %.03f, %.03f), ok: %d\n",
453 inf[i].fR, inf[i].fG, inf[i].fB, inf[i].fA,
454 outf[i].fR, outf[i].fG, outf[i].fB, outf[i].fA,
455 expectedForColorModulation[i].fR, expectedForColorModulation[i].fG,
456 expectedForColorModulation[i].fB, expectedForColorModulation[i].fA,
457 fuzzy_color_equals(outf[i], expectedForColorModulation[i]));
458 }
459 SkDebugf("Alpha modulation test\n\timplied mod color: (%.03f, %.03f, %.03f, %.03f)\n",
460 fpPreAlphaModulation[0],
461 fpPreAlphaModulation[1],
462 fpPreAlphaModulation[2],
463 fpPreAlphaModulation[3]);
464 for (int i = 0; i < 3; ++i) {
465 SkDebugf("\t(%.03f, %.03f, %.03f, %.03f) -> "
466 "(%.03f, %.03f, %.03f, %.03f) | "
467 "(%.03f, %.03f, %.03f, %.03f), ok: %d\n",
468 inf[i].fR, inf[i].fG, inf[i].fB, inf[i].fA,
469 outf[i].fR, outf[i].fG, outf[i].fB, outf[i].fA,
470 expectedForAlphaModulation[i].fR, expectedForAlphaModulation[i].fG,
471 expectedForAlphaModulation[i].fB, expectedForAlphaModulation[i].fA,
472 fuzzy_color_equals(outf[i], expectedForAlphaModulation[i]));
473 }
474 }
475 return isLegalColorModulation || isLegalAlphaModulation;
Michael Ludwig7034f152018-10-08 16:43:58 -0400476}
477
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500478DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400479 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500480 auto resourceProvider = context->priv().resourceProvider();
Brian Salomon1c053642017-07-24 10:16:19 -0400481 using FPFactory = GrFragmentProcessorTestFactory;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400482
Michael Ludwig3ad86d02018-09-28 16:00:38 -0400483 uint32_t seed = FLAGS_processorSeed;
Brian Osmanc35a2d42017-03-17 10:58:53 -0400484 if (FLAGS_randomProcessorTest) {
485 std::random_device rd;
486 seed = rd();
487 }
488 // 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 -0400489 // use --processorSeed <seed> (without --randomProcessorTest) to reproduce.
Brian Osmanc35a2d42017-03-17 10:58:53 -0400490 SkRandom random(seed);
491
Brian Salomon0e05a822017-07-25 09:43:22 -0400492 // Make the destination context for the test.
493 static constexpr int kRenderSize = 256;
Greg Daniele20fcad2020-01-08 11:52:34 -0500494 auto rtc = GrRenderTargetContext::Make(
495 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact,
496 {kRenderSize, kRenderSize});
Brian Salomond1a8bdf2017-02-10 12:39:26 -0500497
Greg Daniel026a60c2020-02-12 10:53:51 -0500498 GrProcessorTestData::ViewInfo views[2];
499 if (!init_test_textures(resourceProvider, context, &random, views)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400500 ERRORF(reporter, "Could not create test textures");
Robert Phillipsbb581ce2017-05-29 15:05:15 -0400501 return;
502 }
Greg Daniel026a60c2020-02-12 10:53:51 -0500503 GrProcessorTestData testData(&random, context, 2, views);
Brian Salomon587e08f2017-01-27 10:59:27 -0500504
Michael Ludwig7034f152018-10-08 16:43:58 -0400505 // Coverage optimization uses three frames with a linearly transformed input texture. The first
506 // frame has no offset, second frames add .2 and .4, which should then be present as a fixed
507 // difference between the frame outputs if the FP is properly following the modulation
508 // requirements of the coverage optimization.
509 static constexpr SkScalar kInputDelta = 0.2f;
Greg Daniel6f5441a2020-01-28 17:02:49 -0500510 auto inputTexture1 = make_input_texture(context, kRenderSize, kRenderSize, 0.0f);
511 auto inputTexture2 = make_input_texture(context, kRenderSize, kRenderSize, kInputDelta);
512 auto inputTexture3 = make_input_texture(context, kRenderSize, kRenderSize, 2*kInputDelta);
Robert Phillips30f9bc62017-02-22 15:28:38 -0500513
Michael Ludwige8e10752018-10-01 12:42:53 -0400514 // Encoded images are very verbose and this tests many potential images, so only export the
515 // first failure (subsequent failures have a reasonable chance of being related).
516 bool loggedFirstFailure = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400517 bool loggedFirstWarning = false;
Michael Ludwig7034f152018-10-08 16:43:58 -0400518
519 // Storage for the three frames required for coverage compatibility optimization. Each frame
520 // uses the correspondingly numbered inputTextureX.
521 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
522 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
523 std::unique_ptr<GrColor[]> readData3(new GrColor[kRenderSize * kRenderSize]);
524
Brian Salomon0e05a822017-07-25 09:43:22 -0400525 // Because processor factories configure themselves in random ways, this is not exhaustive.
Brian Salomon587e08f2017-01-27 10:59:27 -0500526 for (int i = 0; i < FPFactory::Count(); ++i) {
527 int timesToInvokeFactory = 5;
528 // Increase the number of attempts if the FP has child FPs since optimizations likely depend
529 // on child optimizations being present.
Brian Salomonaff329b2017-08-11 09:40:37 -0400530 std::unique_ptr<GrFragmentProcessor> fp = FPFactory::MakeIdx(i, &testData);
Brian Salomon587e08f2017-01-27 10:59:27 -0500531 for (int j = 0; j < fp->numChildProcessors(); ++j) {
532 // This value made a reasonable trade off between time and coverage when this test was
533 // written.
534 timesToInvokeFactory *= FPFactory::Count() / 2;
535 }
Brian Osman50ea3c02019-02-04 10:01:53 -0500536#if defined(__MSVC_RUNTIME_CHECKS)
537 // This test is infuriatingly slow with MSVC runtime checks enabled
538 timesToInvokeFactory = 1;
539#endif
Brian Salomon587e08f2017-01-27 10:59:27 -0500540 for (int j = 0; j < timesToInvokeFactory; ++j) {
541 fp = FPFactory::MakeIdx(i, &testData);
Robert Phillipsa91e0b72017-05-01 13:12:20 -0400542
Brian Salomon587e08f2017-01-27 10:59:27 -0500543 if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
Brian Salomonf3b995b2017-02-15 10:22:23 -0500544 !fp->compatibleWithCoverageAsAlpha()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500545 continue;
546 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400547
Brian Salomon3f4de782020-06-18 14:16:00 -0400548 // All draws use a clone so that we can continue to query fp. ProcessorCloneTest should
549 // validate that clones are equivalent to the original.
Michael Ludwig7034f152018-10-08 16:43:58 -0400550 if (fp->compatibleWithCoverageAsAlpha()) {
551 // 2nd and 3rd frames are only used when checking coverage optimization
Brian Salomon3f4de782020-06-18 14:16:00 -0400552 render_fp(context, rtc.get(), fp->clone(), inputTexture2, kPremul_SkAlphaType,
Greg Danielc594e622019-10-15 14:01:49 -0400553 readData2.get());
Brian Salomon3f4de782020-06-18 14:16:00 -0400554 render_fp(context, rtc.get(), fp->clone(), inputTexture3, kPremul_SkAlphaType,
Greg Danielc594e622019-10-15 14:01:49 -0400555 readData3.get());
Michael Ludwig7034f152018-10-08 16:43:58 -0400556 }
Brian Salomon3f4de782020-06-18 14:16:00 -0400557
Michael Ludwig7034f152018-10-08 16:43:58 -0400558 // Draw base frame last so that rtc holds the original FP behavior if we need to
559 // dump the image to the log.
Brian Salomon3f4de782020-06-18 14:16:00 -0400560 render_fp(context, rtc.get(), fp->clone(), inputTexture1, kPremul_SkAlphaType,
Greg Danielc594e622019-10-15 14:01:49 -0400561 readData1.get());
Brian Salomonaff329b2017-08-11 09:40:37 -0400562
Michael Ludwig314d3772018-10-03 16:04:38 -0400563 // This test has a history of being flaky on a number of devices. If an FP is logically
564 // violating the optimizations, it's reasonable to expect it to violate requirements on
565 // a large number of pixels in the image. Sporadic pixel violations are more indicative
566 // of device errors and represents a separate problem.
Michael Ludwig72ab3462018-12-10 12:43:36 -0500567#if defined(SK_BUILD_FOR_SKQP)
Michael Ludwig314d3772018-10-03 16:04:38 -0400568 static constexpr int kMaxAcceptableFailedPixels = 0; // Strict when running as SKQP
569#else
570 static constexpr int kMaxAcceptableFailedPixels = 2 * kRenderSize; // ~0.7% of the image
571#endif
572
573 int failedPixelCount = 0;
574 // Collect first optimization failure message, to be output later as a warning or an
575 // error depending on whether the rendering "passed" or failed.
576 SkString coverageMessage;
577 SkString opaqueMessage;
578 SkString constMessage;
579 for (int y = 0; y < kRenderSize; ++y) {
580 for (int x = 0; x < kRenderSize; ++x) {
581 bool passing = true;
Michael Ludwig7034f152018-10-08 16:43:58 -0400582 GrColor input = input_texel_color(x, y, 0.0f);
583 GrColor output = readData1.get()[y * kRenderSize + x];
584
585 if (fp->compatibleWithCoverageAsAlpha()) {
Brian Salomonbc73eb42019-12-18 14:57:45 -0500586 GrColor ins[3];
587 ins[0] = input;
588 ins[1] = input_texel_color(x, y, kInputDelta);
589 ins[2] = input_texel_color(x, y, 2 * kInputDelta);
Michael Ludwig7034f152018-10-08 16:43:58 -0400590
Brian Salomonbc73eb42019-12-18 14:57:45 -0500591 GrColor outs[3];
592 outs[0] = output;
593 outs[1] = readData2.get()[y * kRenderSize + x];
594 outs[2] = readData3.get()[y * kRenderSize + x];
Michael Ludwig7034f152018-10-08 16:43:58 -0400595
Brian Salomonbc73eb42019-12-18 14:57:45 -0500596 if (!legal_modulation(ins, outs)) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500597 passing = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400598 if (coverageMessage.isEmpty()) {
Brian Salomonbc73eb42019-12-18 14:57:45 -0500599 coverageMessage.printf(
600 "\"Modulating\" processor %s did not match "
Michael Ludwig7034f152018-10-08 16:43:58 -0400601 "alpha-modulation nor color-modulation rules. "
602 "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).",
603 fp->name(), input, output, x, y);
Michael Ludwig314d3772018-10-03 16:04:38 -0400604 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500605 }
606 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400607
Brian Osmancb3d0872018-10-16 15:19:28 -0400608 SkPMColor4f input4f = SkPMColor4f::FromBytes_RGBA(input);
609 SkPMColor4f output4f = SkPMColor4f::FromBytes_RGBA(output);
Brian Osman1d5b5982018-10-01 13:41:39 -0400610 SkPMColor4f expected4f;
Michael Ludwig7034f152018-10-08 16:43:58 -0400611 if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) {
Brian Osmancb3d0872018-10-16 15:19:28 -0400612 float rDiff = fabsf(output4f.fR - expected4f.fR);
613 float gDiff = fabsf(output4f.fG - expected4f.fG);
614 float bDiff = fabsf(output4f.fB - expected4f.fB);
615 float aDiff = fabsf(output4f.fA - expected4f.fA);
Brian Salomon5d4cd9e2017-02-09 11:16:46 -0500616 static constexpr float kTol = 4 / 255.f;
Brian Salomon587e08f2017-01-27 10:59:27 -0500617 if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
Michael Ludwig314d3772018-10-03 16:04:38 -0400618 if (constMessage.isEmpty()) {
619 passing = false;
620
621 constMessage.printf("Processor %s claimed output for const input "
622 "doesn't match actual output. Error: %f, Tolerance: %f, "
623 "input: (%f, %f, %f, %f), actual: (%f, %f, %f, %f), "
Michael Ludwig7034f152018-10-08 16:43:58 -0400624 "expected(%f, %f, %f, %f)", fp->name(),
Brian Osman788b9162020-02-07 10:36:46 -0500625 std::max(rDiff, std::max(gDiff, std::max(bDiff, aDiff))), kTol,
Michael Ludwig314d3772018-10-03 16:04:38 -0400626 input4f.fR, input4f.fG, input4f.fB, input4f.fA,
Brian Osmancb3d0872018-10-16 15:19:28 -0400627 output4f.fR, output4f.fG, output4f.fB, output4f.fA,
628 expected4f.fR, expected4f.fG, expected4f.fB, expected4f.fA);
Michael Ludwig314d3772018-10-03 16:04:38 -0400629 }
Brian Salomon587e08f2017-01-27 10:59:27 -0500630 }
631 }
Brian Osman00b29392018-11-05 15:42:43 -0500632 if (input4f.isOpaque() && fp->preservesOpaqueInput() && !output4f.isOpaque()) {
Brian Salomon587e08f2017-01-27 10:59:27 -0500633 passing = false;
Michael Ludwig314d3772018-10-03 16:04:38 -0400634
635 if (opaqueMessage.isEmpty()) {
636 opaqueMessage.printf("Processor %s claimed opaqueness is preserved but "
637 "it is not. Input: 0x%08x, Output: 0x%08x.",
Michael Ludwig7034f152018-10-08 16:43:58 -0400638 fp->name(), input, output);
Michael Ludwige8e10752018-10-01 12:42:53 -0400639 }
Brian Osmanbd1f76f2017-03-15 11:33:12 -0400640 }
Michael Ludwig314d3772018-10-03 16:04:38 -0400641
642 if (!passing) {
643 // Regardless of how many optimizations the pixel violates, count it as a
644 // single bad pixel.
645 failedPixelCount++;
646 }
647 }
648 }
649
650 // Finished analyzing the entire image, see if the number of pixel failures meets the
651 // threshold for an FP violating the optimization requirements.
652 if (failedPixelCount > kMaxAcceptableFailedPixels) {
Michael Ludwig4504a6522018-10-03 16:47:55 -0400653 ERRORF(reporter, "Processor violated %d of %d pixels, seed: 0x%08x, processor: %s"
Michael Ludwig314d3772018-10-03 16:04:38 -0400654 ", first failing pixel details are below:",
655 failedPixelCount, kRenderSize * kRenderSize, seed,
Michael Ludwig7034f152018-10-08 16:43:58 -0400656 fp->dumpInfo().c_str());
Michael Ludwig314d3772018-10-03 16:04:38 -0400657
658 // Print first failing pixel's details.
659 if (!coverageMessage.isEmpty()) {
660 ERRORF(reporter, coverageMessage.c_str());
661 }
662 if (!constMessage.isEmpty()) {
663 ERRORF(reporter, constMessage.c_str());
664 }
665 if (!opaqueMessage.isEmpty()) {
666 ERRORF(reporter, opaqueMessage.c_str());
667 }
668
669 if (!loggedFirstFailure) {
670 // Print with ERRORF to make sure the encoded image is output
671 SkString input;
Greg Daniel124486b2020-02-11 11:54:55 -0500672 log_texture_view(context, inputTexture1, &input);
Michael Ludwig314d3772018-10-03 16:04:38 -0400673 SkString output;
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400674 log_pixels(readData1.get(), kRenderSize, &output);
Michael Ludwig314d3772018-10-03 16:04:38 -0400675 ERRORF(reporter, "Input image: %s\n\n"
676 "===========================================================\n\n"
677 "Output image: %s\n", input.c_str(), output.c_str());
678 loggedFirstFailure = true;
679 }
Michael Ludwig7034f152018-10-08 16:43:58 -0400680 } else if(failedPixelCount > 0) {
Michael Ludwig314d3772018-10-03 16:04:38 -0400681 // Don't trigger an error, but don't just hide the failures either.
682 INFOF(reporter, "Processor violated %d of %d pixels (below error threshold), seed: "
683 "0x%08x, processor: %s", failedPixelCount, kRenderSize * kRenderSize,
Michael Ludwig7034f152018-10-08 16:43:58 -0400684 seed, fp->dumpInfo().c_str());
Michael Ludwig314d3772018-10-03 16:04:38 -0400685 if (!coverageMessage.isEmpty()) {
686 INFOF(reporter, coverageMessage.c_str());
687 }
688 if (!constMessage.isEmpty()) {
689 INFOF(reporter, constMessage.c_str());
690 }
691 if (!opaqueMessage.isEmpty()) {
692 INFOF(reporter, opaqueMessage.c_str());
693 }
694 if (!loggedFirstWarning) {
695 SkString input;
Greg Daniel124486b2020-02-11 11:54:55 -0500696 log_texture_view(context, inputTexture1, &input);
Michael Ludwig314d3772018-10-03 16:04:38 -0400697 SkString output;
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400698 log_pixels(readData1.get(), kRenderSize, &output);
Michael Ludwig314d3772018-10-03 16:04:38 -0400699 INFOF(reporter, "Input image: %s\n\n"
700 "===========================================================\n\n"
701 "Output image: %s\n", input.c_str(), output.c_str());
702 loggedFirstWarning = true;
Brian Salomon587e08f2017-01-27 10:59:27 -0500703 }
704 }
705 }
706 }
707}
Robert Phillips18166ee2017-06-01 12:55:44 -0400708
John Stiles8c71f3e2020-06-15 15:07:19 -0400709static void describe_fp_children(const GrFragmentProcessor& fp,
710 std::string indent,
711 SkString* text) {
712 for (int index = 0; index < fp.numChildProcessors(); ++index) {
Brian Osman12c5d292020-07-13 16:11:35 -0400713 const GrFragmentProcessor* childFP = fp.childProcessor(index);
714 text->appendf("\n%s(#%d) -> %s", indent.c_str(), index, childFP ? childFP->name() : "null");
715 if (childFP) {
716 describe_fp_children(*childFP, indent + "\t", text);
717 }
John Stiles8c71f3e2020-06-15 15:07:19 -0400718 }
719}
720
721static SkString describe_fp(const GrFragmentProcessor& fp) {
722 SkString text;
723 text.printf("\n%s", fp.name());
724 describe_fp_children(fp, "\t", &text);
725 return text;
726}
727
Brian Salomon3f4de782020-06-18 14:16:00 -0400728// Tests that a fragment processor returned by GrFragmentProcessor::clone() is equivalent to its
729// progenitor.
Brian Salomon0e05a822017-07-25 09:43:22 -0400730DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400731 auto context = ctxInfo.directContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500732 auto resourceProvider = context->priv().resourceProvider();
Brian Salomon0e05a822017-07-25 09:43:22 -0400733
734 SkRandom random;
735
736 // Make the destination context for the test.
737 static constexpr int kRenderSize = 1024;
Greg Daniele20fcad2020-01-08 11:52:34 -0500738 auto rtc = GrRenderTargetContext::Make(
739 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact,
740 {kRenderSize, kRenderSize});
Brian Salomon0e05a822017-07-25 09:43:22 -0400741
Greg Daniel026a60c2020-02-12 10:53:51 -0500742 GrProcessorTestData::ViewInfo views[2];
743 if (!init_test_textures(resourceProvider, context, &random, views)) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400744 ERRORF(reporter, "Could not create test textures");
745 return;
746 }
Greg Daniel026a60c2020-02-12 10:53:51 -0500747 GrProcessorTestData testData(&random, context, 2, views);
Brian Salomon0e05a822017-07-25 09:43:22 -0400748
Greg Daniel6f5441a2020-01-28 17:02:49 -0500749 auto inputTexture = make_input_texture(context, kRenderSize, kRenderSize, 0.0f);
Brian Salomon0e05a822017-07-25 09:43:22 -0400750 std::unique_ptr<GrColor[]> readData1(new GrColor[kRenderSize * kRenderSize]);
751 std::unique_ptr<GrColor[]> readData2(new GrColor[kRenderSize * kRenderSize]);
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400752 // On failure we write out images, but just write the first failing set as the print is very
753 // large.
754 bool loggedFirstFailure = false;
755
756 // This test has a history of being flaky on a number of devices. If an FP clone is logically
757 // wrong, it's reasonable to expect it produce a large number of pixel differences in the image
758 // Sporadic pixel violations are more indicative device errors and represents a separate
759 // problem.
760#if defined(SK_BUILD_FOR_SKQP)
761 static constexpr int kMaxAcceptableFailedPixels = 0; // Strict when running as SKQP
762#else
763 static constexpr int kMaxAcceptableFailedPixels = 2 * kRenderSize; // ~0.7% of the image
764#endif
Brian Salomon0e05a822017-07-25 09:43:22 -0400765
766 // Because processor factories configure themselves in random ways, this is not exhaustive.
767 for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
768 static constexpr int kTimesToInvokeFactory = 10;
769 for (int j = 0; j < kTimesToInvokeFactory; ++j) {
John Stiles8c71f3e2020-06-15 15:07:19 -0400770 std::unique_ptr<GrFragmentProcessor> fp =
771 GrFragmentProcessorTestFactory::MakeIdx(i, &testData);
772 std::unique_ptr<GrFragmentProcessor> clone = fp->clone();
Brian Salomon0e05a822017-07-25 09:43:22 -0400773 if (!clone) {
Brian Salomon96271cd2017-07-31 16:27:23 -0400774 ERRORF(reporter, "Clone of processor %s failed.", fp->name());
Brian Salomon0e05a822017-07-25 09:43:22 -0400775 continue;
776 }
Brian Salomonaff329b2017-08-11 09:40:37 -0400777 const char* name = fp->name();
John Stiles8c71f3e2020-06-15 15:07:19 -0400778 REPORTER_ASSERT(reporter, !strcmp(fp->name(), clone->name()),
779 "%s\n", describe_fp(*fp).c_str());
Brian Salomonce06e262017-08-01 16:23:40 -0400780 REPORTER_ASSERT(reporter, fp->compatibleWithCoverageAsAlpha() ==
John Stiles8c71f3e2020-06-15 15:07:19 -0400781 clone->compatibleWithCoverageAsAlpha(),
782 "%s\n", describe_fp(*fp).c_str());
783 REPORTER_ASSERT(reporter, fp->isEqual(*clone),
784 "%s\n", describe_fp(*fp).c_str());
785 REPORTER_ASSERT(reporter, fp->preservesOpaqueInput() == clone->preservesOpaqueInput(),
786 "%s\n", describe_fp(*fp).c_str());
Brian Salomonce06e262017-08-01 16:23:40 -0400787 REPORTER_ASSERT(reporter, fp->hasConstantOutputForConstantInput() ==
John Stiles8c71f3e2020-06-15 15:07:19 -0400788 clone->hasConstantOutputForConstantInput(),
789 "%s\n", describe_fp(*fp).c_str());
790 REPORTER_ASSERT(reporter, fp->numChildProcessors() == clone->numChildProcessors(),
791 "%s\n", describe_fp(*fp).c_str());
Brian Osman9cf98dc2020-07-01 17:21:27 -0400792 REPORTER_ASSERT(reporter, fp->usesVaryingCoords() == clone->usesVaryingCoords(),
Michael Ludwige88320b2020-06-24 09:04:56 -0400793 "%s\n", describe_fp(*fp).c_str());
794 REPORTER_ASSERT(reporter, fp->referencesSampleCoords() ==
795 clone->referencesSampleCoords(),
John Stiles8c71f3e2020-06-15 15:07:19 -0400796 "%s\n", describe_fp(*fp).c_str());
Brian Salomon0e05a822017-07-25 09:43:22 -0400797 // Draw with original and read back the results.
Brian Salomon3f4de782020-06-18 14:16:00 -0400798 render_fp(context, rtc.get(), std::move(fp), inputTexture, kPremul_SkAlphaType,
Greg Danielc594e622019-10-15 14:01:49 -0400799 readData1.get());
Brian Salomon0e05a822017-07-25 09:43:22 -0400800
801 // Draw with clone and read back the results.
Brian Salomon3f4de782020-06-18 14:16:00 -0400802 render_fp(context, rtc.get(), std::move(clone), inputTexture, kPremul_SkAlphaType,
Greg Danielc594e622019-10-15 14:01:49 -0400803 readData2.get());
Brian Salomon0e05a822017-07-25 09:43:22 -0400804
805 // Check that the results are the same.
806 bool passing = true;
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400807 int failedPixelCount = 0;
808 int firstWrongX = 0;
809 int firstWrongY = 0;
Brian Salomon0e05a822017-07-25 09:43:22 -0400810 for (int y = 0; y < kRenderSize && passing; ++y) {
811 for (int x = 0; x < kRenderSize && passing; ++x) {
812 int idx = y * kRenderSize + x;
813 if (readData1[idx] != readData2[idx]) {
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400814 if (!failedPixelCount) {
815 firstWrongX = x;
816 firstWrongY = y;
817 }
818 ++failedPixelCount;
819 }
820 if (failedPixelCount > kMaxAcceptableFailedPixels) {
Brian Salomon0e05a822017-07-25 09:43:22 -0400821 passing = false;
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400822 idx = firstWrongY * kRenderSize + firstWrongX;
823 ERRORF(reporter,
824 "Processor %s made clone produced different output at (%d, %d). "
825 "Input color: 0x%08x, Original Output Color: 0x%08x, "
826 "Clone Output Color: 0x%08x.",
827 name, firstWrongX, firstWrongY, input_texel_color(x, y, 0.0f),
828 readData1[idx], readData2[idx]);
829 if (!loggedFirstFailure) {
830 // Write the images out as data urls for inspection.
831 // We mark the data as unpremul to avoid conversion when encoding as
832 // PNG. Also, even though we made the data by rendering into
833 // a "unpremul" GrRenderTargetContext, our input texture is unpremul and
834 // outside of the random effect configuration, we didn't do anything to
835 // ensure the output is actually premul.
836 auto info = SkImageInfo::Make(kRenderSize, kRenderSize,
837 kRGBA_8888_SkColorType,
838 kUnpremul_SkAlphaType);
Brian Salomon3f4de782020-06-18 14:16:00 -0400839 SkString inputURL, origURL, cloneURL;
840 if (log_texture_view(context, inputTexture, &inputURL) &&
841 log_pixels(readData1.get(), kRenderSize, &origURL) &&
842 log_pixels(readData2.get(), kRenderSize, &cloneURL)) {
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400843 ERRORF(reporter,
844 "\nInput image:\n%s\n\n"
845 "==========================================================="
846 "\n\n"
847 "Orig output image:\n%s\n"
848 "==========================================================="
849 "\n\n"
850 "Clone output image:\n%s\n",
Brian Salomon3f4de782020-06-18 14:16:00 -0400851 inputURL.c_str(), origURL.c_str(), cloneURL.c_str());
Brian Salomoncd8b6d52019-08-13 12:40:04 -0400852 loggedFirstFailure = true;
853 }
854 }
Brian Salomon0e05a822017-07-25 09:43:22 -0400855 }
856 }
857 }
858 }
859 }
860}
861
Hal Canary6f6961e2017-01-31 13:50:44 -0500862#endif // GR_TEST_UTILS