Flutter Engine
The Flutter Engine
Public Member Functions | List of all members
HashAndEncode Class Reference

#include <HashAndEncode.h>

Public Member Functions

 HashAndEncode (const SkBitmap &)
 
void feedHash (SkWStream *) const
 
bool encodePNG (SkWStream *, const char *md5, CommandLineFlags::StringArray key, CommandLineFlags::StringArray properties) const
 

Detailed Description

Definition at line 14 of file HashAndEncode.h.

Constructor & Destructor Documentation

◆ HashAndEncode()

HashAndEncode::HashAndEncode ( const SkBitmap bitmap)
explicit

Definition at line 18 of file HashAndEncode.cpp.

18 : fSize(bitmap.info().dimensions()) {
19 skcms_AlphaFormat srcAlpha;
20 switch (bitmap.alphaType()) {
21 case kUnknown_SkAlphaType: return;
22
26 }
27
28 skcms_PixelFormat srcFmt;
29 switch (bitmap.colorType()) {
30 case kUnknown_SkColorType: return;
31
32 case kAlpha_8_SkColorType: srcFmt = skcms_PixelFormat_A_8; break;
41 case kGray_8_SkColorType: srcFmt = skcms_PixelFormat_G_8; break;
42 // skcms doesn't have R_8. Pretend it's G_8, but see below for color space trickery:
43 case kR8_unorm_SkColorType: srcFmt = skcms_PixelFormat_G_8; break;
48
50 srcAlpha = skcms_AlphaFormat_Opaque; break;
52 srcAlpha = skcms_AlphaFormat_Opaque; break;
54 srcAlpha = skcms_AlphaFormat_Opaque; break;
55
56 case kR8G8_unorm_SkColorType: return;
57 case kR16G16_unorm_SkColorType: return;
58 case kR16G16_float_SkColorType: return;
59 case kA16_unorm_SkColorType: return;
60 case kA16_float_SkColorType: return;
61 case kRGBA_10x6_SkColorType: return;
63 }
64
66 if (auto cs = bitmap.colorSpace()) {
67 cs->toProfile(&srcProfile);
68 }
69
70 // NOTE: If the color type is R8, we told skcms it's actually G8 above. To get red PNGs,
71 // we tweak the source color space to throw away any green and blue:
72 if (bitmap.colorType() == kR8_unorm_SkColorType) {
73 srcProfile.toXYZD50.vals[0][1] = srcProfile.toXYZD50.vals[0][2] = 0;
74 srcProfile.toXYZD50.vals[1][1] = srcProfile.toXYZD50.vals[1][2] = 0;
75 srcProfile.toXYZD50.vals[2][1] = srcProfile.toXYZD50.vals[2][2] = 0;
76 }
77
78 // Our common format that can represent anything we draw and encode as a PNG:
79 // - 16-bit big-endian RGBA
80 // - unpremul
81 // - Rec. 2020 gamut and transfer function
84 skcms_ICCProfile dstProfile;
85 rec2020()->toProfile(&dstProfile);
86
87 int N = fSize.width() * fSize.height();
88 fPixels.reset(new uint64_t[N]);
89
90 const void* src = bitmap.getPixels();
91 void* dst = fPixels.get();
92 while (N > 0) {
93 int todo = std::min(N, 1<<27); // Keep todo*8 <= 1B; skcms requires N*bpp < MAX_INT.
94 if (!skcms_Transform(src, srcFmt, srcAlpha, &srcProfile,
95 dst, dstFmt, dstAlpha, &dstProfile, todo)) {
96 SkASSERT(false);
97 fPixels.reset(nullptr);
98 break;
99 }
100 src = (const char*)src + todo*SkColorTypeBytesPerPixel(bitmap.colorType());
101 dst = ( char*)dst + todo*sizeof(uint64_t);
102 N -= todo;
103 }
104}
static sk_sp< SkColorSpace > rec2020()
kUnpremul_SkAlphaType
@ kUnknown_SkAlphaType
uninitialized
Definition: SkAlphaType.h:27
@ kOpaque_SkAlphaType
pixel is opaque
Definition: SkAlphaType.h:28
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
#define SkASSERT(cond)
Definition: SkAssert.h:116
@ 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
SK_API int SkColorTypeBytesPerPixel(SkColorType ct)
Definition: SkImageInfo.cpp:16
#define N
Definition: beziers.cpp:19
void toProfile(skcms_ICCProfile *) const
static float min(float r, float g, float b)
Definition: hsl.cpp:48
Definition: bitmap.py:1
dst
Definition: cp.py:12
bool skcms_Transform(const void *src, skcms_PixelFormat srcFmt, skcms_AlphaFormat srcAlpha, const skcms_ICCProfile *srcProfile, void *dst, skcms_PixelFormat dstFmt, skcms_AlphaFormat dstAlpha, const skcms_ICCProfile *dstProfile, size_t nz)
Definition: skcms.cc:2495
const skcms_ICCProfile * skcms_sRGB_profile()
Definition: skcms.cc:1393
skcms_PixelFormat
Definition: skcms_public.h:273
@ skcms_PixelFormat_RGBA_8888_sRGB
Definition: skcms_public.h:289
@ skcms_PixelFormat_RGBA_16161616BE
Definition: skcms_public.h:302
@ skcms_PixelFormat_BGR_101010x_XR
Definition: skcms_public.h:321
@ skcms_PixelFormat_RGBA_ffff
Definition: skcms_public.h:317
@ skcms_PixelFormat_RGBA_1010102
Definition: skcms_public.h:292
@ skcms_PixelFormat_BGRA_8888
Definition: skcms_public.h:288
@ skcms_PixelFormat_RGBA_hhhh
Definition: skcms_public.h:312
@ skcms_PixelFormat_RGBA_8888
Definition: skcms_public.h:287
@ skcms_PixelFormat_G_8
Definition: skcms_public.h:276
@ skcms_PixelFormat_BGR_565
Definition: skcms_public.h:280
@ skcms_PixelFormat_ABGR_4444
Definition: skcms_public.h:282
@ skcms_PixelFormat_A_8
Definition: skcms_public.h:274
@ skcms_PixelFormat_BGRA_1010102
Definition: skcms_public.h:293
@ skcms_PixelFormat_RGBA_16161616LE
Definition: skcms_public.h:297
skcms_AlphaFormat
Definition: skcms_public.h:336
@ skcms_AlphaFormat_Unpremul
Definition: skcms_public.h:339
@ skcms_AlphaFormat_PremulAsEncoded
Definition: skcms_public.h:341
@ skcms_AlphaFormat_Opaque
Definition: skcms_public.h:337
constexpr int32_t width() const
Definition: SkSize.h:36
constexpr int32_t height() const
Definition: SkSize.h:37
skcms_Matrix3x3 toXYZD50
Definition: skcms_public.h:191
float vals[3][3]
Definition: skcms_public.h:27

Member Function Documentation

◆ encodePNG()

bool HashAndEncode::encodePNG ( SkWStream st,
const char *  md5,
CommandLineFlags::StringArray  key,
CommandLineFlags::StringArray  properties 
) const

Definition at line 122 of file HashAndEncode.cpp.

125 {
126 if (!fPixels) {
127 return false;
128 }
129
130 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
131 if (!png) {
132 return false;
133 }
134
135 png_infop info = png_create_info_struct(png);
136 if (!info) {
137 png_destroy_write_struct(&png, &info);
138 return false;
139 }
140 auto write_to_stream = +[](png_structp png, png_bytep ptr, png_size_t len) {
141 auto st = (SkWStream*)png_get_io_ptr(png);
142 if (!st->write(ptr, len)) {
143 png_error(png, "HashAndEncode::encodePNG() failed writing stream");
144 }
145 };
146 png_set_write_fn(png, st, write_to_stream, nullptr);
147
148 SkString description;
149 description.append("Key: ");
150 for (int i = 0; i < key.size(); i++) {
151 description.appendf("%s ", key[i]);
152 }
153 description.append("Properties: ");
154 for (int i = 0; i < properties.size(); i++) {
155 description.appendf("%s ", properties[i]);
156 }
157 description.appendf("MD5: %s", md5);
158
159 png_text text[2];
160 text[0].key = const_cast<png_charp>("Author");
161 text[0].text = const_cast<png_charp>("DM unified Rec.2020");
162 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
163 text[1].key = const_cast<png_charp>("Description");
164 text[1].text = const_cast<png_charp>(description.c_str());
165 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
166 png_set_text(png, info, text, std::size(text));
167
168 png_set_IHDR(png, info, (png_uint_32)fSize.width()
169 , (png_uint_32)fSize.height()
170 , 16/*bits per channel*/
171 , PNG_COLOR_TYPE_RGB_ALPHA
172 , PNG_INTERLACE_NONE
173 , PNG_COMPRESSION_TYPE_DEFAULT
174 , PNG_FILTER_TYPE_DEFAULT);
175
176 // Fastest encoding and decoding, at slight file size cost is no filtering, compression 1.
177 png_set_filter(png, PNG_FILTER_TYPE_BASE, PNG_FILTER_NONE);
178 png_set_compression_level(png, 1);
179
180 static const sk_sp<SkData> profile =
182 png_set_iCCP(png, info,
183 "Rec.2020",
184 0/*compression type... no idea what options are available here*/,
185 (png_const_bytep)profile->data(),
186 (png_uint_32) profile->size());
187
188 png_write_info(png, info);
189 for (int y = 0; y < fSize.height(); y++) {
190 png_write_row(png, (png_bytep)(fPixels.get() + y*fSize.width()));
191 }
192 png_write_end(png, info);
193
194 png_destroy_write_struct(&png, &info);
195 return true;
196}
static SkMD5::Digest md5(const SkBitmap &bm)
Definition: CodecTest.cpp:77
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
SK_API sk_sp< SkData > SkWriteICCProfile(const skcms_TransferFunction &, const skcms_Matrix3x3 &toXYZD50)
Definition: SkICC.cpp:682
void append(const char text[])
Definition: SkString.h:203
const char * c_str() const
Definition: SkString.h:133
void void void appendf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:550
virtual bool write(const void *buffer, size_t size)=0
std::u16string text
double y
static constexpr skcms_Matrix3x3 kRec2020
Definition: SkColorSpace.h:93
static constexpr skcms_TransferFunction kRec2020
Definition: SkColorSpace.h:54
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259

◆ feedHash()

void HashAndEncode::feedHash ( SkWStream st) const

Definition at line 106 of file HashAndEncode.cpp.

106 {
107 st->write(&fSize, sizeof(fSize));
108 if (const uint64_t* px = fPixels.get()) {
109 st->write(px, sizeof(*px) * fSize.width() * fSize.height());
110 }
111
112 // N.B. changing salt will change the hash of all images produced by DM,
113 // and will cause tens of thousands of new images to be uploaded to Gold.
114 int salt = 1;
115 st->write(&salt, sizeof(salt));
116}

The documentation for this class was generated from the following files: