xref: /aosp_15_r20/external/skia/gm/imagedither.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2024 Google LLC
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 "gm/gm.h"
9 #include "include/core/SkAlphaType.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorType.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkSamplingOptions.h"
18 #include "include/core/SkSurface.h"
19 #include "include/effects/SkGradientShader.h"
20 #include "include/effects/SkRuntimeEffect.h"
21 
stretch_colors_blender()22 sk_sp<SkBlender> stretch_colors_blender() {
23     return SkRuntimeEffect::MakeForBlender(SkString(
24         "half4 main(half4 src, half4 dst) { return ((dst.rgb - 0.25) * 16).rgb1; }"))
25             .effect->makeBlender(nullptr);
26 }
27 
28 DEF_SIMPLE_GM_CAN_FAIL(image_dither, canvas, errorMsg, 425, 110) {
29     if (!canvas->getSurface()) {
30         // This GM relies on a high-precision (F16) image to determine if image draws are dithered.
31         // Serializing configs will tend to throw away that data (compressing to PNG), so they will
32         // produce different results before/after serialization (and thus fail).
33         *errorMsg = "Not supported in recording mode";
34         return skiagm::DrawResult::kSkip;
35     }
36 
37     // First, we make a non-dithered image with a shallow radial gradient. This will be our source:
38     const SkColor colors[] = { 0xFF555555, 0xFF444444 };
39     const SkPoint points[] = {{0, 0}, {100, 100}};
40     sk_sp<SkShader> gradient = SkGradientShader::MakeLinear(
41             points, colors, nullptr, std::size(colors), SkTileMode::kClamp);
42     SkPaint gradientPaint;
43     gradientPaint.setShader(gradient);
44 
45     sk_sp<SkSurface> surface = SkSurfaces::Raster(
46             SkImageInfo::Make(100, 100, kRGBA_F16_SkColorType, kPremul_SkAlphaType));
47     surface->getCanvas()->drawPaint(gradientPaint);
48     sk_sp<SkImage> image = surface->makeImageSnapshot();
49 
50     // Now, we draw it three times:
51     // 1) As-is (no dither), to ensure that our source image doesn't have any dithering included
52     // 2) Using an image shader, with dithering enabled on the paint
53     // 3) With drawImage, with dithering enabled on the paint
54     //
55     // We'd like #2 and #3 to both respect the dither flag, for consistency (b/320529640)
56     canvas->translate(5, 5);
57 
58     canvas->drawImage(image, 0, 0);
59     canvas->translate(105, 0);
60 
61     SkPaint imageShaderPaint;
62     imageShaderPaint.setShader(image->makeShader(SkSamplingOptions{}));
63     imageShaderPaint.setDither(true);
64     canvas->drawRect({0, 0, 100, 100}, imageShaderPaint);
65     canvas->translate(105, 0);
66 
67     SkPaint drawImagePaint;
68     drawImagePaint.setDither(true);
69     canvas->drawImage(image, 0, 0, SkSamplingOptions{}, &drawImagePaint);
70     canvas->translate(105, 0);
71 
72     // Also draw the actual gradient with the dither flag, to see how it should look:
73     gradientPaint.setDither(true);
74     canvas->drawRect({0, 0, 100, 100}, gradientPaint);
75 
76     SkPaint colorStretchPaint;
77     colorStretchPaint.setBlender(stretch_colors_blender());
78     canvas->drawPaint(colorStretchPaint);
79 
80     return skiagm::DrawResult::kOk;
81 }
82