Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / tests / ProcessorTest.cpp
1 /*
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 "tests/Test.h"
9
10 #include "include/core/SkColorSpace.h"
11 #include "include/gpu/GrDirectContext.h"
12 #include "src/gpu/KeyBuilder.h"
13 #include "src/gpu/ganesh/GrClip.h"
14 #include "src/gpu/ganesh/GrDirectContextPriv.h"
15 #include "src/gpu/ganesh/GrFragmentProcessor.h"
16 #include "src/gpu/ganesh/GrGpuResource.h"
17 #include "src/gpu/ganesh/GrImageInfo.h"
18 #include "src/gpu/ganesh/GrMemoryPool.h"
19 #include "src/gpu/ganesh/GrProxyProvider.h"
20 #include "src/gpu/ganesh/GrResourceProvider.h"
21 #include "src/gpu/ganesh/SkGr.h"
22 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
23 #include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
24 #include "src/gpu/ganesh/ops/GrMeshDrawOp.h"
25 #include "src/gpu/ganesh/v1/SurfaceDrawContext_v1.h"
26 #include "tests/TestHarness.h"
27 #include "tests/TestUtils.h"
28
29 #include <atomic>
30 #include <random>
31
32 namespace {
33 class TestOp : public GrMeshDrawOp {
34 public:
35     DEFINE_OP_CLASS_ID
36     static GrOp::Owner Make(GrRecordingContext* rContext,
37                             std::unique_ptr<GrFragmentProcessor> fp) {
38         return GrOp::Make<TestOp>(rContext, std::move(fp));
39     }
40
41     const char* name() const override { return "TestOp"; }
42
43     void visitProxies(const GrVisitProxyFunc& func) const override {
44         fProcessors.visitProxies(func);
45     }
46
47     FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
48
49     GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
50                                       GrClampType clampType) override {
51         static constexpr GrProcessorAnalysisColor kUnknownColor;
52         SkPMColor4f overrideColor;
53         return fProcessors.finalize(
54                 kUnknownColor, GrProcessorAnalysisCoverage::kNone, clip,
55                 &GrUserStencilSettings::kUnused, caps, clampType, &overrideColor);
56     }
57
58 private:
59     friend class ::GrOp; // for ctor
60
61     TestOp(std::unique_ptr<GrFragmentProcessor> fp)
62             : INHERITED(ClassID()), fProcessors(std::move(fp)) {
63         this->setBounds(SkRect::MakeWH(100, 100), HasAABloat::kNo, IsHairline::kNo);
64     }
65
66     GrProgramInfo* programInfo() override { return nullptr; }
67     void onCreateProgramInfo(const GrCaps*,
68                              SkArenaAlloc*,
69                              const GrSurfaceProxyView& writeView,
70                              bool usesMSAASurface,
71                              GrAppliedClip&&,
72                              const GrDstProxyView&,
73                              GrXferBarrierFlags renderPassXferBarriers,
74                              GrLoadOp colorLoadOp) override {}
75     void onPrePrepareDraws(GrRecordingContext*,
76                            const GrSurfaceProxyView& writeView,
77                            GrAppliedClip*,
78                            const GrDstProxyView&,
79                            GrXferBarrierFlags renderPassXferBarriers,
80                            GrLoadOp colorLoadOp) override {}
81     void onPrepareDraws(GrMeshDrawTarget*) override { return; }
82     void onExecute(GrOpFlushState*, const SkRect&) override { return; }
83
84     GrProcessorSet fProcessors;
85
86     using INHERITED = GrMeshDrawOp;
87 };
88
89 /**
90  * FP used to test ref counts on owned GrGpuResources. Can also be a parent FP to test counts
91  * of resources owned by child FPs.
92  */
93 class TestFP : public GrFragmentProcessor {
94 public:
95     static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child) {
96         return std::unique_ptr<GrFragmentProcessor>(new TestFP(std::move(child)));
97     }
98     static std::unique_ptr<GrFragmentProcessor> Make(const SkTArray<GrSurfaceProxyView>& views) {
99         return std::unique_ptr<GrFragmentProcessor>(new TestFP(views));
100     }
101
102     const char* name() const override { return "test"; }
103
104     void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder* b) const override {
105         static std::atomic<int32_t> nextKey{0};
106         b->add32(nextKey++);
107     }
108
109     std::unique_ptr<GrFragmentProcessor> clone() const override {
110         return std::unique_ptr<GrFragmentProcessor>(new TestFP(*this));
111     }
112
113 private:
114     TestFP(const SkTArray<GrSurfaceProxyView>& views)
115             : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags) {
116         for (const GrSurfaceProxyView& view : views) {
117             this->registerChild(GrTextureEffect::Make(view, kUnknown_SkAlphaType));
118         }
119     }
120
121     TestFP(std::unique_ptr<GrFragmentProcessor> child)
122             : INHERITED(kTestFP_ClassID, kNone_OptimizationFlags) {
123         this->registerChild(std::move(child));
124     }
125
126     explicit TestFP(const TestFP& that) : INHERITED(that) {}
127
128     std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override {
129         class Impl : public ProgramImpl {
130         public:
131             void emitCode(EmitArgs& args) override {
132                 args.fFragBuilder->codeAppendf("return half4(1);");
133             }
134
135         private:
136         };
137         return std::make_unique<Impl>();
138     }
139
140     bool onIsEqual(const GrFragmentProcessor&) const override { return false; }
141
142     using INHERITED = GrFragmentProcessor;
143 };
144 }  // namespace
145
146 DEF_GPUTEST_FOR_ALL_CONTEXTS(ProcessorRefTest, reporter, ctxInfo) {
147     auto dContext = ctxInfo.directContext();
148     GrProxyProvider* proxyProvider = dContext->priv().proxyProvider();
149
150     static constexpr SkISize kDims = {10, 10};
151
152     const GrBackendFormat format =
153         dContext->priv().caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888,
154                                                          GrRenderable::kNo);
155     skgpu::Swizzle swizzle = dContext->priv().caps()->getReadSwizzle(format,
156                                                                      GrColorType::kRGBA_8888);
157
158     for (bool makeClone : {false, true}) {
159         for (int parentCnt = 0; parentCnt < 2; parentCnt++) {
160             auto sdc = skgpu::v1::SurfaceDrawContext::Make(
161                     dContext, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, {1, 1},
162                     SkSurfaceProps());
163             {
164                 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(
165                         format, kDims, GrRenderable::kNo, 1, GrMipmapped::kNo, SkBackingFit::kExact,
166                         SkBudgeted::kYes, GrProtected::kNo);
167
168                 {
169                     SkTArray<GrSurfaceProxyView> views;
170                     views.push_back({proxy, kTopLeft_GrSurfaceOrigin, swizzle});
171                     auto fp = TestFP::Make(std::move(views));
172                     for (int i = 0; i < parentCnt; ++i) {
173                         fp = TestFP::Make(std::move(fp));
174                     }
175                     std::unique_ptr<GrFragmentProcessor> clone;
176                     if (makeClone) {
177                         clone = fp->clone();
178                     }
179                     GrOp::Owner op = TestOp::Make(dContext, std::move(fp));
180                     sdc->addDrawOp(std::move(op));
181                     if (clone) {
182                         op = TestOp::Make(dContext, std::move(clone));
183                         sdc->addDrawOp(std::move(op));
184                     }
185                 }
186
187                 // If the fp is cloned the number of refs should increase by one (for the clone)
188                 int expectedProxyRefs = makeClone ? 3 : 2;
189
190                 CheckSingleThreadedProxyRefs(reporter, proxy.get(), expectedProxyRefs, -1);
191
192                 dContext->flushAndSubmit();
193
194                 // just one from the 'proxy' sk_sp
195                 CheckSingleThreadedProxyRefs(reporter, proxy.get(), 1, 1);
196             }
197         }
198     }
199 }
200
201 #include "tools/flags/CommandLineFlags.h"
202 static DEFINE_bool(randomProcessorTest, false,
203                    "Use non-deterministic seed for random processor tests?");
204 static DEFINE_int(processorSeed, 0,
205                   "Use specific seed for processor tests. Overridden by --randomProcessorTest.");
206
207 #if GR_TEST_UTILS
208
209 static GrColor input_texel_color(int x, int y, SkScalar delta) {
210     // Delta must be less than 0.5 to prevent over/underflow issues with the input color
211     SkASSERT(delta <= 0.5);
212
213     SkColor color = SkColorSetARGB((uint8_t)(x & 0xFF),
214                                    (uint8_t)(y & 0xFF),
215                                    (uint8_t)((x + y) & 0xFF),
216                                    (uint8_t)((2 * y - x) & 0xFF));
217     SkColor4f color4f = SkColor4f::FromColor(color);
218     // We only apply delta to the r,g, and b channels. This is because we're using this
219     // to test the canTweakAlphaForCoverage() optimization. A processor is allowed
220     // to use the input color's alpha in its calculation and report this optimization.
221     for (int i = 0; i < 3; i++) {
222         if (color4f[i] > 0.5) {
223             color4f[i] -= delta;
224         } else {
225             color4f[i] += delta;
226         }
227     }
228     return color4f.premul().toBytes_RGBA();
229 }
230
231 // The output buffer must be the same size as the render-target context.
232 static void render_fp(GrDirectContext* dContext,
233                       skgpu::v1::SurfaceDrawContext* sdc,
234                       std::unique_ptr<GrFragmentProcessor> fp,
235                       GrColor* outBuffer) {
236     sdc->fillWithFP(std::move(fp));
237     std::fill_n(outBuffer, sdc->width() * sdc->height(), 0);
238     auto ii = SkImageInfo::Make(sdc->dimensions(), kRGBA_8888_SkColorType, kPremul_SkAlphaType);
239     GrPixmap resultPM(ii, outBuffer, sdc->width()*sizeof(uint32_t));
240     sdc->readPixels(dContext, resultPM, {0, 0});
241 }
242
243 // This class is responsible for reproducibly generating a random fragment processor.
244 // An identical randomly-designed FP can be generated as many times as needed.
245 class TestFPGenerator {
246     public:
247         TestFPGenerator() = delete;
248         TestFPGenerator(GrDirectContext* context, GrResourceProvider* resourceProvider)
249                 : fContext(context)
250                 , fResourceProvider(resourceProvider)
251                 , fInitialSeed(synthesizeInitialSeed())
252                 , fRandomSeed(fInitialSeed) {}
253
254         uint32_t initialSeed() { return fInitialSeed; }
255
256         bool init() {
257             // Initializes the two test texture proxies that are available to the FP test factories.
258             SkRandom random{fRandomSeed};
259             static constexpr int kTestTextureSize = 256;
260
261             {
262                 // Put premul data into the RGBA texture that the test FPs can optionally use.
263                 GrColor* rgbaData = new GrColor[kTestTextureSize * kTestTextureSize];
264                 for (int y = 0; y < kTestTextureSize; ++y) {
265                     for (int x = 0; x < kTestTextureSize; ++x) {
266                         rgbaData[kTestTextureSize * y + x] = input_texel_color(
267                                 random.nextULessThan(256), random.nextULessThan(256), 0.0f);
268                     }
269                 }
270
271                 SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize,
272                                                    kRGBA_8888_SkColorType, kPremul_SkAlphaType);
273                 SkBitmap bitmap;
274                 bitmap.installPixels(
275                         ii, rgbaData, ii.minRowBytes(),
276                         [](void* addr, void* context) { delete[](GrColor*) addr; }, nullptr);
277                 bitmap.setImmutable();
278                 auto view = std::get<0>(GrMakeUncachedBitmapProxyView(fContext, bitmap));
279                 if (!view || !view.proxy()->instantiate(fResourceProvider)) {
280                     SkDebugf("Unable to instantiate RGBA8888 test texture.");
281                     return false;
282                 }
283                 fTestViews[0] = GrProcessorTestData::ViewInfo{view, GrColorType::kRGBA_8888,
284                                                               kPremul_SkAlphaType};
285             }
286
287             {
288                 // Put random values into the alpha texture that the test FPs can optionally use.
289                 uint8_t* alphaData = new uint8_t[kTestTextureSize * kTestTextureSize];
290                 for (int y = 0; y < kTestTextureSize; ++y) {
291                     for (int x = 0; x < kTestTextureSize; ++x) {
292                         alphaData[kTestTextureSize * y + x] = random.nextULessThan(256);
293                     }
294                 }
295
296                 SkImageInfo ii = SkImageInfo::Make(kTestTextureSize, kTestTextureSize,
297                                                    kAlpha_8_SkColorType, kPremul_SkAlphaType);
298                 SkBitmap bitmap;
299                 bitmap.installPixels(
300                         ii, alphaData, ii.minRowBytes(),
301                         [](void* addr, void* context) { delete[](uint8_t*) addr; }, nullptr);
302                 bitmap.setImmutable();
303                 auto view = std::get<0>(GrMakeUncachedBitmapProxyView(fContext, bitmap));
304                 if (!view || !view.proxy()->instantiate(fResourceProvider)) {
305                     SkDebugf("Unable to instantiate A8 test texture.");
306                     return false;
307                 }
308                 fTestViews[1] = GrProcessorTestData::ViewInfo{view, GrColorType::kAlpha_8,
309                                                               kPremul_SkAlphaType};
310             }
311
312             return true;
313         }
314
315         void reroll() {
316             // Feed our current random seed into SkRandom to generate a new seed.
317             SkRandom random{fRandomSeed};
318             fRandomSeed = random.nextU();
319         }
320
321         std::unique_ptr<GrFragmentProcessor> make(int type, int randomTreeDepth,
322                                                   std::unique_ptr<GrFragmentProcessor> inputFP) {
323             // This will generate the exact same randomized FP (of each requested type) each time
324             // it's called. Call `reroll` to get a different FP.
325             SkRandom random{fRandomSeed};
326             GrProcessorTestData testData{&random, fContext, randomTreeDepth,
327                                          SK_ARRAY_COUNT(fTestViews), fTestViews,
328                                          std::move(inputFP)};
329             return GrFragmentProcessorTestFactory::MakeIdx(type, &testData);
330         }
331
332         std::unique_ptr<GrFragmentProcessor> make(int type, int randomTreeDepth,
333                                                   GrSurfaceProxyView view,
334                                                   SkAlphaType alpha = kPremul_SkAlphaType) {
335             return make(type, randomTreeDepth, GrTextureEffect::Make(view, alpha));
336         }
337
338     private:
339         static uint32_t synthesizeInitialSeed() {
340             if (FLAGS_randomProcessorTest) {
341                 std::random_device rd;
342                 return rd();
343             } else {
344                 return FLAGS_processorSeed;
345             }
346         }
347
348         GrDirectContext* fContext;              // owned by caller
349         GrResourceProvider* fResourceProvider;  // owned by caller
350         const uint32_t fInitialSeed;
351         uint32_t fRandomSeed;
352         GrProcessorTestData::ViewInfo fTestViews[2];
353 };
354
355 // Creates an array of color values from input_texel_color(), to be used as an input texture.
356 static std::vector<GrColor> make_input_pixels(int width, int height, SkScalar delta) {
357     std::vector<GrColor> pixel(width * height);
358     for (int y = 0; y < width; ++y) {
359         for (int x = 0; x < height; ++x) {
360             pixel[width * y + x] = input_texel_color(x, y, delta);
361         }
362     }
363
364     return pixel;
365 }
366
367 // Creates a texture of premul colors used as the output of the fragment processor that precedes
368 // the fragment processor under test. An array of W*H colors are passed in as the texture data.
369 static GrSurfaceProxyView make_input_texture(GrRecordingContext* context,
370                                       int width, int height, GrColor* pixel) {
371     SkImageInfo ii = SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
372     SkBitmap bitmap;
373     bitmap.installPixels(ii, pixel, ii.minRowBytes());
374     bitmap.setImmutable();
375     return std::get<0>(GrMakeUncachedBitmapProxyView(context, bitmap));
376 }
377
378 // We tag logged data as unpremul to avoid conversion when encoding as PNG. The input texture
379 // actually contains unpremul data. Also, even though we made the result data by rendering into
380 // a "unpremul" SurfaceDrawContext, our input texture is unpremul and outside of the random
381 // effect configuration, we didn't do anything to ensure the output is actually premul. We just
382 // don't currently allow kUnpremul GrSurfaceDrawContexts.
383 static constexpr auto kLogAlphaType = kUnpremul_SkAlphaType;
384
385 static bool log_pixels(GrColor* pixels, int widthHeight, SkString* dst) {
386     SkImageInfo info =
387             SkImageInfo::Make(widthHeight, widthHeight, kRGBA_8888_SkColorType, kLogAlphaType);
388     SkBitmap bmp;
389     bmp.installPixels(info, pixels, widthHeight * sizeof(GrColor));
390     return BipmapToBase64DataURI(bmp, dst);
391 }
392
393 static bool log_texture_view(GrDirectContext* dContext, GrSurfaceProxyView src, SkString* dst) {
394     SkImageInfo ii = SkImageInfo::Make(src.proxy()->dimensions(), kRGBA_8888_SkColorType,
395                                        kLogAlphaType);
396
397     auto sContext = dContext->priv().makeSC(std::move(src), ii.colorInfo());
398     SkBitmap bm;
399     SkAssertResult(bm.tryAllocPixels(ii));
400     SkAssertResult(sContext->readPixels(dContext, bm.pixmap(), {0, 0}));
401     return BipmapToBase64DataURI(bm, dst);
402 }
403
404 static bool fuzzy_color_equals(const SkPMColor4f& c1, const SkPMColor4f& c2) {
405     // With the loss of precision of rendering into 32-bit color, then estimating the FP's output
406     // from that, it is not uncommon for a valid output to differ from estimate by up to 0.01
407     // (really 1/128 ~ .0078, but frequently floating point issues make that tolerance a little
408     // too unforgiving).
409     static constexpr SkScalar kTolerance = 0.01f;
410     for (int i = 0; i < 4; i++) {
411         if (!SkScalarNearlyEqual(c1[i], c2[i], kTolerance)) {
412             return false;
413         }
414     }
415     return true;
416 }
417
418 // Given three input colors (color preceding the FP being tested) provided to the FP at the same
419 // local coord and the three corresponding FP outputs, this ensures that either:
420 //   out[0] = fp * in[0].a, out[1] = fp * in[1].a, and out[2] = fp * in[2].a
421 // where fp is the pre-modulated color that should not be changing across frames (FP's state doesn't
422 // change), OR:
423 //   out[0] = fp * in[0], out[1] = fp * in[1], and out[2] = fp * in[2]
424 // (per-channel modulation instead of modulation by just the alpha channel)
425 // It does this by estimating the pre-modulated fp color from one of the input/output pairs and
426 // confirms the conditions hold for the other two pairs.
427 // It is required that the three input colors have the same alpha as fp is allowed to be a function
428 // of the input alpha (but not r, g, or b).
429 static bool legal_modulation(const GrColor inGr[3], const GrColor outGr[3]) {
430     // Convert to floating point, which is the number space the FP operates in (more or less)
431     SkPMColor4f inf[3], outf[3];
432     for (int i = 0; i < 3; ++i) {
433         inf[i]  = SkPMColor4f::FromBytes_RGBA(inGr[i]);
434         outf[i] = SkPMColor4f::FromBytes_RGBA(outGr[i]);
435     }
436     // This test is only valid if all the input alphas are the same.
437     SkASSERT(inf[0].fA == inf[1].fA && inf[1].fA == inf[2].fA);
438
439     // Reconstruct the output of the FP before the shader modulated its color with the input value.
440     // When the original input is very small, it may cause the final output color to round
441     // to 0, in which case we estimate the pre-modulated color using one of the stepped frames that
442     // will then have a guaranteed larger channel value (since the offset will be added to it).
443     SkPMColor4f fpPreColorModulation = {0,0,0,0};
444     SkPMColor4f fpPreAlphaModulation = {0,0,0,0};
445     for (int i = 0; i < 4; i++) {
446         // Use the most stepped up frame
447         int maxInIdx = inf[0][i] > inf[1][i] ? 0 : 1;
448         maxInIdx = inf[maxInIdx][i] > inf[2][i] ? maxInIdx : 2;
449         const SkPMColor4f& in = inf[maxInIdx];
450         const SkPMColor4f& out = outf[maxInIdx];
451         if (in[i] > 0) {
452             fpPreColorModulation[i] = out[i] / in[i];
453         }
454         if (in[3] > 0) {
455             fpPreAlphaModulation[i] = out[i] / in[3];
456         }
457     }
458
459     // With reconstructed pre-modulated FP output, derive the expected value of fp * input for each
460     // of the transformed input colors.
461     SkPMColor4f expectedForAlphaModulation[3];
462     SkPMColor4f expectedForColorModulation[3];
463     for (int i = 0; i < 3; ++i) {
464         expectedForAlphaModulation[i] = fpPreAlphaModulation * inf[i].fA;
465         expectedForColorModulation[i] = fpPreColorModulation * inf[i];
466         // If the input alpha is 0 then the other channels should also be zero
467         // since the color is assumed to be premul. Modulating zeros by anything
468         // should produce zeros.
469         if (inf[i].fA == 0) {
470             SkASSERT(inf[i].fR == 0 && inf[i].fG == 0 && inf[i].fB == 0);
471             expectedForColorModulation[i] = expectedForAlphaModulation[i] = {0, 0, 0, 0};
472         }
473     }
474
475     bool isLegalColorModulation = fuzzy_color_equals(outf[0], expectedForColorModulation[0]) &&
476                                   fuzzy_color_equals(outf[1], expectedForColorModulation[1]) &&
477                                   fuzzy_color_equals(outf[2], expectedForColorModulation[2]);
478
479     bool isLegalAlphaModulation = fuzzy_color_equals(outf[0], expectedForAlphaModulation[0]) &&
480                                   fuzzy_color_equals(outf[1], expectedForAlphaModulation[1]) &&
481                                   fuzzy_color_equals(outf[2], expectedForAlphaModulation[2]);
482
483     // This can be enabled to print the values that caused this check to fail.
484     if ((false)) {
485         if (!isLegalColorModulation && !isLegalAlphaModulation) {
486             SkDebugf("Color modulation test\n\timplied mod color: (%.03f, %.03f, %.03f, %.03f)\n",
487                      fpPreColorModulation[0],
488                      fpPreColorModulation[1],
489                      fpPreColorModulation[2],
490                      fpPreColorModulation[3]);
491             for (int i = 0; i < 3; ++i) {
492                 SkDebugf("\t(%.03f, %.03f, %.03f, %.03f) -> "
493                          "(%.03f, %.03f, %.03f, %.03f) | "
494                          "(%.03f, %.03f, %.03f, %.03f), ok: %d\n",
495                          inf[i].fR, inf[i].fG, inf[i].fB, inf[i].fA,
496                          outf[i].fR, outf[i].fG, outf[i].fB, outf[i].fA,
497                          expectedForColorModulation[i].fR, expectedForColorModulation[i].fG,
498                          expectedForColorModulation[i].fB, expectedForColorModulation[i].fA,
499                          fuzzy_color_equals(outf[i], expectedForColorModulation[i]));
500             }
501             SkDebugf("Alpha modulation test\n\timplied mod color: (%.03f, %.03f, %.03f, %.03f)\n",
502                      fpPreAlphaModulation[0],
503                      fpPreAlphaModulation[1],
504                      fpPreAlphaModulation[2],
505                      fpPreAlphaModulation[3]);
506             for (int i = 0; i < 3; ++i) {
507                 SkDebugf("\t(%.03f, %.03f, %.03f, %.03f) -> "
508                          "(%.03f, %.03f, %.03f, %.03f) | "
509                          "(%.03f, %.03f, %.03f, %.03f), ok: %d\n",
510                          inf[i].fR, inf[i].fG, inf[i].fB, inf[i].fA,
511                          outf[i].fR, outf[i].fG, outf[i].fB, outf[i].fA,
512                          expectedForAlphaModulation[i].fR, expectedForAlphaModulation[i].fG,
513                          expectedForAlphaModulation[i].fB, expectedForAlphaModulation[i].fA,
514                          fuzzy_color_equals(outf[i], expectedForAlphaModulation[i]));
515             }
516         }
517     }
518     return isLegalColorModulation || isLegalAlphaModulation;
519 }
520
521 DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorOptimizationValidationTest, reporter, ctxInfo) {
522     GrDirectContext* context = ctxInfo.directContext();
523     GrResourceProvider* resourceProvider = context->priv().resourceProvider();
524     using FPFactory = GrFragmentProcessorTestFactory;
525
526     TestFPGenerator fpGenerator{context, resourceProvider};
527     if (!fpGenerator.init()) {
528         ERRORF(reporter, "Could not initialize TestFPGenerator");
529         return;
530     }
531
532     // Make the destination context for the test.
533     static constexpr int kRenderSize = 256;
534     auto sdc = skgpu::v1::SurfaceDrawContext::Make(
535             context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact,
536             {kRenderSize, kRenderSize}, SkSurfaceProps());
537
538     // Coverage optimization uses three frames with a linearly transformed input texture.  The first
539     // frame has no offset, second frames add .2 and .4, which should then be present as a fixed
540     // difference between the frame outputs if the FP is properly following the modulation
541     // requirements of the coverage optimization.
542     static constexpr SkScalar kInputDelta = 0.2f;
543     std::vector<GrColor> inputPixels1 = make_input_pixels(kRenderSize, kRenderSize, 0.0f);
544     std::vector<GrColor> inputPixels2 =
545             make_input_pixels(kRenderSize, kRenderSize, 1 * kInputDelta);
546     std::vector<GrColor> inputPixels3 =
547             make_input_pixels(kRenderSize, kRenderSize, 2 * kInputDelta);
548     GrSurfaceProxyView inputTexture1 =
549             make_input_texture(context, kRenderSize, kRenderSize, inputPixels1.data());
550     GrSurfaceProxyView inputTexture2 =
551             make_input_texture(context, kRenderSize, kRenderSize, inputPixels2.data());
552     GrSurfaceProxyView inputTexture3 =
553             make_input_texture(context, kRenderSize, kRenderSize, inputPixels3.data());
554
555     // Encoded images are very verbose and this tests many potential images, so only export the
556     // first failure (subsequent failures have a reasonable chance of being related).
557     bool loggedFirstFailure = false;
558     bool loggedFirstWarning = false;
559
560     // Storage for the three frames required for coverage compatibility optimization testing.
561     // Each frame uses the correspondingly numbered inputTextureX.
562     std::vector<GrColor> readData1(kRenderSize * kRenderSize);
563     std::vector<GrColor> readData2(kRenderSize * kRenderSize);
564     std::vector<GrColor> readData3(kRenderSize * kRenderSize);
565
566     // Because processor factories configure themselves in random ways, this is not exhaustive.
567     for (int i = 0; i < FPFactory::Count(); ++i) {
568         int optimizedForOpaqueInput = 0;
569         int optimizedForCoverageAsAlpha = 0;
570         int optimizedForConstantOutputForInput = 0;
571
572 #ifdef __MSVC_RUNTIME_CHECKS
573         // This test is infuriatingly slow with MSVC runtime checks enabled
574         static constexpr int kMinimumTrials = 1;
575         static constexpr int kMaximumTrials = 1;
576         static constexpr int kExpectedSuccesses = 1;
577 #else
578         // We start by testing each fragment-processor 100 times, watching the optimization bits
579         // that appear. If we see an optimization bit appear in those first 100 trials, we keep
580         // running tests until we see at least five successful trials that have this optimization
581         // bit enabled. If we never see a particular optimization bit after 100 trials, we assume
582         // that this FP doesn't support that optimization at all.
583         static constexpr int kMinimumTrials = 100;
584         static constexpr int kMaximumTrials = 2000;
585         static constexpr int kExpectedSuccesses = 5;
586 #endif
587
588         for (int trial = 0;; ++trial) {
589             // Create a randomly-configured FP.
590             fpGenerator.reroll();
591             std::unique_ptr<GrFragmentProcessor> fp =
592                     fpGenerator.make(i, /*randomTreeDepth=*/1, inputTexture1);
593
594             // If we have iterated enough times and seen a sufficient number of successes on each
595             // optimization bit that can be returned, stop running trials.
596             if (trial >= kMinimumTrials) {
597                 bool moreTrialsNeeded = (optimizedForOpaqueInput > 0 &&
598                                          optimizedForOpaqueInput < kExpectedSuccesses) ||
599                                         (optimizedForCoverageAsAlpha > 0 &&
600                                          optimizedForCoverageAsAlpha < kExpectedSuccesses) ||
601                                         (optimizedForConstantOutputForInput > 0 &&
602                                          optimizedForConstantOutputForInput < kExpectedSuccesses);
603                 if (!moreTrialsNeeded) break;
604
605                 if (trial >= kMaximumTrials) {
606                     SkDebugf("Abandoning ProcessorOptimizationValidationTest after %d trials. "
607                              "Seed: 0x%08x, processor:\n%s",
608                              kMaximumTrials, fpGenerator.initialSeed(), fp->dumpTreeInfo().c_str());
609                     break;
610                 }
611             }
612
613             // Skip further testing if this trial has no optimization bits enabled.
614             if (!fp->hasConstantOutputForConstantInput() && !fp->preservesOpaqueInput() &&
615                 !fp->compatibleWithCoverageAsAlpha()) {
616                 continue;
617             }
618
619             // We can make identical copies of the test FP in order to test coverage-as-alpha.
620             if (fp->compatibleWithCoverageAsAlpha()) {
621                 // Create and render two identical versions of this FP, but using different input
622                 // textures, to check coverage optimization. We don't need to do this step for
623                 // constant-output or preserving-opacity tests.
624                 render_fp(context, sdc.get(),
625                           fpGenerator.make(i, /*randomTreeDepth=*/1, inputTexture2),
626                           readData2.data());
627                 render_fp(context, sdc.get(),
628                           fpGenerator.make(i, /*randomTreeDepth=*/1, inputTexture3),
629                           readData3.data());
630                 ++optimizedForCoverageAsAlpha;
631             }
632
633             if (fp->hasConstantOutputForConstantInput()) {
634                 ++optimizedForConstantOutputForInput;
635             }
636
637             if (fp->preservesOpaqueInput()) {
638                 ++optimizedForOpaqueInput;
639             }
640
641             // Draw base frame last so that rtc holds the original FP behavior if we need to dump
642             // the image to the log.
643             render_fp(context, sdc.get(), fpGenerator.make(i, /*randomTreeDepth=*/1, inputTexture1),
644                       readData1.data());
645
646             // This test has a history of being flaky on a number of devices. If an FP is logically
647             // violating the optimizations, it's reasonable to expect it to violate requirements on
648             // a large number of pixels in the image. Sporadic pixel violations are more indicative
649             // of device errors and represents a separate problem.
650             static const int kMaxAcceptableFailedPixels =
651                     CurrentTestHarnessIsSkQP() ? 0 :  // Strict when running as SKQP
652                             2 * kRenderSize;          // ~0.7% of the image
653
654             // Collect first optimization failure message, to be output later as a warning or an
655             // error depending on whether the rendering "passed" or failed.
656             int failedPixelCount = 0;
657             SkString coverageMessage;
658             SkString opaqueMessage;
659             SkString constMessage;
660             for (int y = 0; y < kRenderSize; ++y) {
661                 for (int x = 0; x < kRenderSize; ++x) {
662                     bool passing = true;
663                     GrColor input = inputPixels1[y * kRenderSize + x];
664                     GrColor output = readData1[y * kRenderSize + x];
665
666                     if (fp->compatibleWithCoverageAsAlpha()) {
667                         GrColor ins[3];
668                         ins[0] = input;
669                         ins[1] = inputPixels2[y * kRenderSize + x];
670                         ins[2] = inputPixels3[y * kRenderSize + x];
671
672                         GrColor outs[3];
673                         outs[0] = output;
674                         outs[1] = readData2[y * kRenderSize + x];
675                         outs[2] = readData3[y * kRenderSize + x];
676
677                         if (!legal_modulation(ins, outs)) {
678                             passing = false;
679                             if (coverageMessage.isEmpty()) {
680                                 coverageMessage.printf(
681                                         "\"Modulating\" processor did not match alpha-modulation "
682                                         "nor color-modulation rules.\n"
683                                         "Input: 0x%08x, Output: 0x%08x, pixel (%d, %d).",
684                                         input, output, x, y);
685                             }
686                         }
687                     }
688
689                     SkPMColor4f input4f = SkPMColor4f::FromBytes_RGBA(input);
690                     SkPMColor4f output4f = SkPMColor4f::FromBytes_RGBA(output);
691                     SkPMColor4f expected4f;
692                     if (fp->hasConstantOutputForConstantInput(input4f, &expected4f)) {
693                         float rDiff = fabsf(output4f.fR - expected4f.fR);
694                         float gDiff = fabsf(output4f.fG - expected4f.fG);
695                         float bDiff = fabsf(output4f.fB - expected4f.fB);
696                         float aDiff = fabsf(output4f.fA - expected4f.fA);
697                         static constexpr float kTol = 4 / 255.f;
698                         if (rDiff > kTol || gDiff > kTol || bDiff > kTol || aDiff > kTol) {
699                             if (constMessage.isEmpty()) {
700                                 passing = false;
701
702                                 constMessage.printf(
703                                         "Processor claimed output for const input doesn't match "
704                                         "actual output.\n"
705                                         "Error: %f, Tolerance: %f, input: (%f, %f, %f, %f), "
706                                         "actual: (%f, %f, %f, %f), expected(%f, %f, %f, %f).",
707                                         std::max(rDiff, std::max(gDiff, std::max(bDiff, aDiff))),
708                                         kTol, input4f.fR, input4f.fG, input4f.fB, input4f.fA,
709                                         output4f.fR, output4f.fG, output4f.fB, output4f.fA,
710                                         expected4f.fR, expected4f.fG, expected4f.fB, expected4f.fA);
711                             }
712                         }
713                     }
714                     if (input4f.isOpaque() && fp->preservesOpaqueInput() && !output4f.isOpaque()) {
715                         passing = false;
716
717                         if (opaqueMessage.isEmpty()) {
718                             opaqueMessage.printf(
719                                     "Processor claimed opaqueness is preserved but "
720                                     "it is not. Input: 0x%08x, Output: 0x%08x.",
721                                     input, output);
722                         }
723                     }
724
725                     if (!passing) {
726                         // Regardless of how many optimizations the pixel violates, count it as a
727                         // single bad pixel.
728                         failedPixelCount++;
729                     }
730                 }
731             }
732
733             // Finished analyzing the entire image, see if the number of pixel failures meets the
734             // threshold for an FP violating the optimization requirements.
735             if (failedPixelCount > kMaxAcceptableFailedPixels) {
736                 ERRORF(reporter,
737                        "Processor violated %d of %d pixels, seed: 0x%08x.\n"
738                        "Processor:\n%s\nFirst failing pixel details are below:",
739                        failedPixelCount, kRenderSize * kRenderSize, fpGenerator.initialSeed(),
740                        fp->dumpTreeInfo().c_str());
741
742                 // Print first failing pixel's details.
743                 if (!coverageMessage.isEmpty()) {
744                     ERRORF(reporter, "%s", coverageMessage.c_str());
745                 }
746                 if (!constMessage.isEmpty()) {
747                     ERRORF(reporter, "%s", constMessage.c_str());
748                 }
749                 if (!opaqueMessage.isEmpty()) {
750                     ERRORF(reporter, "%s", opaqueMessage.c_str());
751                 }
752
753                 if (!loggedFirstFailure) {
754                     // Print with ERRORF to make sure the encoded image is output
755                     SkString input;
756                     log_texture_view(context, inputTexture1, &input);
757                     SkString output;
758                     log_pixels(readData1.data(), kRenderSize, &output);
759                     ERRORF(reporter, "Input image: %s\n\n"
760                            "===========================================================\n\n"
761                            "Output image: %s\n", input.c_str(), output.c_str());
762                     loggedFirstFailure = true;
763                 }
764             } else if (failedPixelCount > 0) {
765                 // Don't trigger an error, but don't just hide the failures either.
766                 INFOF(reporter, "Processor violated %d of %d pixels (below error threshold), seed: "
767                       "0x%08x, processor: %s", failedPixelCount, kRenderSize * kRenderSize,
768                       fpGenerator.initialSeed(), fp->dumpInfo().c_str());
769                 if (!coverageMessage.isEmpty()) {
770                     INFOF(reporter, "%s", coverageMessage.c_str());
771                 }
772                 if (!constMessage.isEmpty()) {
773                     INFOF(reporter, "%s", constMessage.c_str());
774                 }
775                 if (!opaqueMessage.isEmpty()) {
776                     INFOF(reporter, "%s", opaqueMessage.c_str());
777                 }
778                 if (!loggedFirstWarning) {
779                     SkString input;
780                     log_texture_view(context, inputTexture1, &input);
781                     SkString output;
782                     log_pixels(readData1.data(), kRenderSize, &output);
783                     INFOF(reporter, "Input image: %s\n\n"
784                           "===========================================================\n\n"
785                           "Output image: %s\n", input.c_str(), output.c_str());
786                     loggedFirstWarning = true;
787                 }
788             }
789         }
790     }
791 }
792
793 static void assert_processor_equality(skiatest::Reporter* reporter,
794                                       const GrFragmentProcessor& fp,
795                                       const GrFragmentProcessor& clone) {
796     REPORTER_ASSERT(reporter, !strcmp(fp.name(), clone.name()),
797                               "\n%s", fp.dumpTreeInfo().c_str());
798     REPORTER_ASSERT(reporter, fp.compatibleWithCoverageAsAlpha() ==
799                               clone.compatibleWithCoverageAsAlpha(),
800                               "\n%s", fp.dumpTreeInfo().c_str());
801     REPORTER_ASSERT(reporter, fp.isEqual(clone),
802                               "\n%s", fp.dumpTreeInfo().c_str());
803     REPORTER_ASSERT(reporter, fp.preservesOpaqueInput() == clone.preservesOpaqueInput(),
804                               "\n%s", fp.dumpTreeInfo().c_str());
805     REPORTER_ASSERT(reporter, fp.hasConstantOutputForConstantInput() ==
806                               clone.hasConstantOutputForConstantInput(),
807                               "\n%s", fp.dumpTreeInfo().c_str());
808     REPORTER_ASSERT(reporter, fp.numChildProcessors() == clone.numChildProcessors(),
809                               "\n%s", fp.dumpTreeInfo().c_str());
810     REPORTER_ASSERT(reporter, fp.sampleUsage() == clone.sampleUsage(),
811                               "\n%s", fp.dumpTreeInfo().c_str());
812     REPORTER_ASSERT(reporter, fp.usesSampleCoords() == clone.usesSampleCoords(),
813                               "\n%s", fp.dumpTreeInfo().c_str());
814 }
815
816 static bool verify_identical_render(skiatest::Reporter* reporter, int renderSize,
817                                     const char* processorType,
818                                     const GrColor readData1[], const GrColor readData2[]) {
819     // The ProcessorClone test has a history of being flaky on a number of devices. If an FP clone
820     // is logically wrong, it's reasonable to expect it produce a large number of pixel differences
821     // in the image. Sporadic pixel violations are more indicative device errors and represents a
822     // separate problem.
823     static const int maxAcceptableFailedPixels =
824             CurrentTestHarnessIsSkQP() ? 0 :  // Strict when running as SKQP
825                     2 * renderSize;           // ~0.002% of the pixels (size 1024*1024)
826
827     int failedPixelCount = 0;
828     int firstWrongX = 0;
829     int firstWrongY = 0;
830     int idx = 0;
831     for (int y = 0; y < renderSize; ++y) {
832         for (int x = 0; x < renderSize; ++x, ++idx) {
833             if (readData1[idx] != readData2[idx]) {
834                 if (!failedPixelCount) {
835                     firstWrongX = x;
836                     firstWrongY = y;
837                 }
838                 ++failedPixelCount;
839             }
840             if (failedPixelCount > maxAcceptableFailedPixels) {
841                 idx = firstWrongY * renderSize + firstWrongX;
842                 ERRORF(reporter,
843                        "%s produced different output at (%d, %d). "
844                        "Input color: 0x%08x, Original Output Color: 0x%08x, "
845                        "Clone Output Color: 0x%08x.",
846                        processorType, firstWrongX, firstWrongY, input_texel_color(x, y, 0.0f),
847                        readData1[idx], readData2[idx]);
848
849                 return false;
850             }
851         }
852     }
853
854     return true;
855 }
856
857 static void log_clone_failure(skiatest::Reporter* reporter, int renderSize,
858                               GrDirectContext* context, const GrSurfaceProxyView& inputTexture,
859                               GrColor pixelsFP[], GrColor pixelsClone[], GrColor pixelsRegen[]) {
860     // Write the images out as data URLs for inspection.
861     SkString inputURL, origURL, cloneURL, regenURL;
862     if (log_texture_view(context, inputTexture, &inputURL) &&
863         log_pixels(pixelsFP, renderSize, &origURL) &&
864         log_pixels(pixelsClone, renderSize, &cloneURL) &&
865         log_pixels(pixelsRegen, renderSize, &regenURL)) {
866         ERRORF(reporter,
867                "\nInput image:\n%s\n\n"
868                "==========================================================="
869                "\n\n"
870                "Orig output image:\n%s\n"
871                "==========================================================="
872                "\n\n"
873                "Clone output image:\n%s\n"
874                "==========================================================="
875                "\n\n"
876                "Regen output image:\n%s\n",
877                inputURL.c_str(), origURL.c_str(), cloneURL.c_str(), regenURL.c_str());
878     }
879 }
880
881 // Tests that a fragment processor returned by GrFragmentProcessor::clone() is equivalent to its
882 // progenitor.
883 DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ProcessorCloneTest, reporter, ctxInfo) {
884     GrDirectContext* context = ctxInfo.directContext();
885     GrResourceProvider* resourceProvider = context->priv().resourceProvider();
886
887     TestFPGenerator fpGenerator{context, resourceProvider};
888     if (!fpGenerator.init()) {
889         ERRORF(reporter, "Could not initialize TestFPGenerator");
890         return;
891     }
892
893     // Make the destination context for the test.
894     static constexpr int kRenderSize = 1024;
895     auto sdc = skgpu::v1::SurfaceDrawContext::Make(
896             context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact,
897             {kRenderSize, kRenderSize}, SkSurfaceProps());
898
899     std::vector<GrColor> inputPixels = make_input_pixels(kRenderSize, kRenderSize, 0.0f);
900     GrSurfaceProxyView inputTexture =
901             make_input_texture(context, kRenderSize, kRenderSize, inputPixels.data());
902
903     // On failure we write out images, but just write the first failing set as the print is very
904     // large.
905     bool loggedFirstFailure = false;
906
907     // Storage for the original frame's readback and the readback of its clone.
908     std::vector<GrColor> readDataFP(kRenderSize * kRenderSize);
909     std::vector<GrColor> readDataClone(kRenderSize * kRenderSize);
910     std::vector<GrColor> readDataRegen(kRenderSize * kRenderSize);
911
912     // Because processor factories configure themselves in random ways, this is not exhaustive.
913     for (int i = 0; i < GrFragmentProcessorTestFactory::Count(); ++i) {
914         static constexpr int kTimesToInvokeFactory = 10;
915         for (int j = 0; j < kTimesToInvokeFactory; ++j) {
916             fpGenerator.reroll();
917             std::unique_ptr<GrFragmentProcessor> fp =
918                     fpGenerator.make(i, /*randomTreeDepth=*/1, /*inputFP=*/nullptr);
919             std::unique_ptr<GrFragmentProcessor> regen =
920                     fpGenerator.make(i, /*randomTreeDepth=*/1, /*inputFP=*/nullptr);
921             std::unique_ptr<GrFragmentProcessor> clone = fp->clone();
922             if (!clone) {
923                 ERRORF(reporter, "Clone of processor %s failed.", fp->dumpTreeInfo().c_str());
924                 continue;
925             }
926             assert_processor_equality(reporter, *fp, *clone);
927
928             // Draw with original and read back the results.
929             render_fp(context, sdc.get(), std::move(fp), readDataFP.data());
930
931             // Draw with clone and read back the results.
932             render_fp(context, sdc.get(), std::move(clone), readDataClone.data());
933
934             // Check that the results are the same.
935             if (!verify_identical_render(reporter, kRenderSize, "Processor clone",
936                                          readDataFP.data(), readDataClone.data())) {
937                 // Dump a description from the regenerated processor (since the original FP has
938                 // already been consumed).
939                 ERRORF(reporter, "FP hierarchy:\n%s", regen->dumpTreeInfo().c_str());
940
941                 // Render and readback output from the regenerated FP. If this also mismatches, the
942                 // FP itself doesn't generate consistent output. This could happen if:
943                 // - the FP's TestCreate() does not always generate the same FP from a given seed
944                 // - the FP's Make() does not always generate the same FP when given the same inputs
945                 // - the FP itself generates inconsistent pixels (shader UB?)
946                 // - the driver has a bug
947                 render_fp(context, sdc.get(), std::move(regen), readDataRegen.data());
948
949                 if (!verify_identical_render(reporter, kRenderSize, "Regenerated processor",
950                                              readDataFP.data(), readDataRegen.data())) {
951                     ERRORF(reporter, "Output from regen did not match original!\n");
952                 } else {
953                     ERRORF(reporter, "Regenerated processor output matches original results.\n");
954                 }
955
956                 // If this is the first time we've encountered a cloning failure, log the generated
957                 // images to the reporter as data URLs.
958                 if (!loggedFirstFailure) {
959                     log_clone_failure(reporter, kRenderSize, context, inputTexture,
960                                       readDataFP.data(), readDataClone.data(),
961                                       readDataRegen.data());
962                     loggedFirstFailure = true;
963                 }
964             }
965         }
966     }
967 }
968
969 #endif  // GR_TEST_UTILS