Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
imagedither.cpp
Go to the documentation of this file.
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"
21
24 "half4 main(half4 src, half4 dst) { return ((dst.rgb - 0.25) * 16).rgb1; }"))
25 .effect->makeBlender(nullptr);
26}
27
28DEF_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";
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}};
41 points, colors, nullptr, std::size(colors), SkTileMode::kClamp);
42 SkPaint gradientPaint;
43 gradientPaint.setShader(gradient);
44
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
81}
static const int points[]
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition SkColorType.h:38
uint32_t SkColor
Definition SkColor.h:37
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition SkImage.cpp:179
void setDither(bool dither)
Definition SkPaint.h:182
void setShader(sk_sp< SkShader > shader)
void setBlender(sk_sp< SkBlender > blender)
Definition SkPaint.cpp:155
sk_sp< SkBlender > makeBlender(sk_sp< const SkData > uniforms, SkSpan< const ChildPtr > children={}) const
static Result MakeForBlender(SkString sksl, const Options &)
VkSurfaceKHR surface
Definition main.cc:49
sk_sp< SkImage > image
Definition examples.cpp:29
#define DEF_SIMPLE_GM_CAN_FAIL(NAME, CANVAS, ERR_MSG, W, H)
Definition gm.h:62
sk_sp< SkBlender > stretch_colors_blender()
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
sk_sp< SkRuntimeEffect > effect