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