Flutter Engine
The Flutter Engine
DitherUtils.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2023 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
9
10#ifndef SK_IGNORE_GPU_DITHER
11
15
16#include <cstdint>
17
18namespace skgpu {
19
20float DitherRangeForConfig(SkColorType dstColorType) {
21 SkASSERT(dstColorType != kUnknown_SkColorType);
22
23 // We use 1 / (2^bitdepth-1) as the range since each channel can hold 2^bitdepth values
24 switch (dstColorType) {
25 // 4 bit
27 return 1 / 15.f;
28
29 // 6 bit
31 return 1 / 63.f;
32
33 // 8 bit
42 return 1 / 255.f;
43
44 // 10 bit
52 return 1 / 1023.f;
53
54 // 16 bit
58 return 1 / 32767.f;
59
60 // Unknown
62 // Half
67 // Float
69 return 0.f; // no dithering
70 }
72}
73
75 static constexpr struct DitherTable {
76 constexpr DitherTable() : data() {
77 constexpr int kImgSize = 8; // if changed, also change value in sk_dither_shader
78
79 for (int x = 0; x < kImgSize; ++x) {
80 for (int y = 0; y < kImgSize; ++y) {
81 // The computation of 'm' and 'value' is lifted from CPU backend.
82 unsigned int m = (y & 1) << 5 | (x & 1) << 4 |
83 (y & 2) << 2 | (x & 2) << 1 |
84 (y & 4) >> 1 | (x & 4) >> 2;
85 float value = float(m) * 1.0 / 64.0 - 63.0 / 128.0;
86 // Bias by 0.5 to be in 0..1, mul by 255 and round to nearest int to make byte.
87 data[y * 8 + x] = (uint8_t)((value + 0.5) * 255.f + 0.5f);
88 }
89 }
90 }
91 uint8_t data[64];
92 } gTable;
93
94 SkBitmap bmp;
96 bmp.setPixels(const_cast<uint8_t*>(gTable.data));
97 bmp.setImmutable();
98 return bmp;
99}
100
101} // namespace skgpu
102
103#endif // SK_IGNORE_GPU_DITHER
#define SkUNREACHABLE
Definition: SkAssert.h:135
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkColorType
Definition: SkColorType.h:19
@ kR16G16B16A16_unorm_SkColorType
pixel with a little endian uint16_t for red, green, blue
Definition: SkColorType.h:50
@ kRGBA_10x6_SkColorType
pixel with 10 used bits (most significant) followed by 6 unused
Definition: SkColorType.h:33
@ kR8_unorm_SkColorType
Definition: SkColorType.h:54
@ kBGR_101010x_SkColorType
pixel with 10 bits each for blue, green, red; in 32-bit word
Definition: SkColorType.h:30
@ kARGB_4444_SkColorType
pixel with 4 bits for alpha, red, green, blue; in 16-bit word
Definition: SkColorType.h:23
@ kR8G8_unorm_SkColorType
pixel with a uint8_t for red and green
Definition: SkColorType.h:43
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition: SkColorType.h:26
@ kA16_unorm_SkColorType
pixel with a little endian uint16_t for alpha
Definition: SkColorType.h:48
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition: SkColorType.h:38
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition: SkColorType.h:21
@ kRGB_101010x_SkColorType
pixel with 10 bits each for red, green, blue; in 32-bit word
Definition: SkColorType.h:29
@ kSRGBA_8888_SkColorType
Definition: SkColorType.h:53
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition: SkColorType.h:35
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition: SkColorType.h:22
@ kBGRA_10101010_XR_SkColorType
pixel with 10 bits each for blue, green, red, alpha; in 64-bit word, extended range
Definition: SkColorType.h:32
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
@ kRGB_888x_SkColorType
pixel with 8 bits each for red, green, blue; in 32-bit word
Definition: SkColorType.h:25
@ kBGRA_1010102_SkColorType
10 bits for blue, green, red; 2 bits for alpha; in 32-bit word
Definition: SkColorType.h:28
@ kA16_float_SkColorType
pixel with a half float for alpha
Definition: SkColorType.h:45
@ kRGBA_F32_SkColorType
pixel using C float for red, green, blue, alpha; in 128-bit word
Definition: SkColorType.h:40
@ kRGBA_1010102_SkColorType
10 bits for red, green, blue; 2 bits for alpha; in 32-bit word
Definition: SkColorType.h:27
@ kBGR_101010x_XR_SkColorType
pixel with 10 bits each for blue, green, red; in 32-bit word, extended range
Definition: SkColorType.h:31
@ kR16G16_unorm_SkColorType
pixel with a little endian uint16_t for red and green
Definition: SkColorType.h:49
@ kRGBA_F16Norm_SkColorType
pixel with half floats in [0,1] for red, green, blue, alpha;
Definition: SkColorType.h:36
@ kUnknown_SkColorType
uninitialized
Definition: SkColorType.h:20
@ kR16G16_float_SkColorType
pixel with a half float for red and green
Definition: SkColorType.h:46
void setImmutable()
Definition: SkBitmap.cpp:400
void setPixels(void *pixels)
Definition: SkBitmap.cpp:207
bool setInfo(const SkImageInfo &imageInfo, size_t rowBytes=0)
Definition: SkBitmap.cpp:114
uint8_t value
double y
double x
Definition: GpuTools.h:21
float DitherRangeForConfig(SkColorType dstColorType)
Definition: DitherUtils.cpp:20
SkBitmap MakeDitherLUT()
Definition: DitherUtils.cpp:74
static SkImageInfo MakeA8(int width, int height)
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63