1 // Copyright 2019 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3
4 #include "tools/HashAndEncode.h"
5
6 #include "include/core/SkColorSpace.h"
7 #include "include/core/SkICC.h"
8 #include "include/core/SkString.h"
9 #include "modules/skcms/skcms.h"
10
11 #include <png.h>
12
rec2020()13 static sk_sp<SkColorSpace> rec2020() {
14 return SkColorSpace::MakeRGB(SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020);
15 }
16
HashAndEncode(const SkBitmap & bitmap)17 HashAndEncode::HashAndEncode(const SkBitmap& bitmap) : fSize(bitmap.info().dimensions()) {
18 skcms_AlphaFormat srcAlpha;
19 switch (bitmap.alphaType()) {
20 case kUnknown_SkAlphaType: return;
21
22 case kOpaque_SkAlphaType:
23 case kUnpremul_SkAlphaType: srcAlpha = skcms_AlphaFormat_Unpremul; break;
24 case kPremul_SkAlphaType: srcAlpha = skcms_AlphaFormat_PremulAsEncoded; break;
25 }
26
27 skcms_PixelFormat srcFmt;
28 switch (bitmap.colorType()) {
29 case kUnknown_SkColorType: return;
30
31 case kAlpha_8_SkColorType: srcFmt = skcms_PixelFormat_A_8; break;
32 case kRGB_565_SkColorType: srcFmt = skcms_PixelFormat_BGR_565; break;
33 case kARGB_4444_SkColorType: srcFmt = skcms_PixelFormat_ABGR_4444; break;
34 case kRGBA_8888_SkColorType: srcFmt = skcms_PixelFormat_RGBA_8888; break;
35 case kBGRA_8888_SkColorType: srcFmt = skcms_PixelFormat_BGRA_8888; break;
36 case kSRGBA_8888_SkColorType: srcFmt = skcms_PixelFormat_RGBA_8888_sRGB; break;
37 case kRGBA_1010102_SkColorType: srcFmt = skcms_PixelFormat_RGBA_1010102; break;
38 case kBGRA_1010102_SkColorType: srcFmt = skcms_PixelFormat_BGRA_1010102; break;
39 case kBGR_101010x_XR_SkColorType: srcFmt = skcms_PixelFormat_BGR_101010x_XR; break;
40 case kGray_8_SkColorType: srcFmt = skcms_PixelFormat_G_8; break;
41 // skcms doesn't have R_8. Pretend it's G_8, but see below for color space trickery:
42 case kR8_unorm_SkColorType: srcFmt = skcms_PixelFormat_G_8; break;
43 case kRGBA_F16Norm_SkColorType: srcFmt = skcms_PixelFormat_RGBA_hhhh; break;
44 case kRGBA_F16_SkColorType: srcFmt = skcms_PixelFormat_RGBA_hhhh; break;
45 case kRGBA_F32_SkColorType: srcFmt = skcms_PixelFormat_RGBA_ffff; break;
46 case kR16G16B16A16_unorm_SkColorType: srcFmt = skcms_PixelFormat_RGBA_16161616LE; break;
47
48 case kRGB_888x_SkColorType: srcFmt = skcms_PixelFormat_RGBA_8888;
49 srcAlpha = skcms_AlphaFormat_Opaque; break;
50 case kRGB_101010x_SkColorType: srcFmt = skcms_PixelFormat_RGBA_1010102;
51 srcAlpha = skcms_AlphaFormat_Opaque; break;
52 case kBGR_101010x_SkColorType: srcFmt = skcms_PixelFormat_BGRA_1010102;
53 srcAlpha = skcms_AlphaFormat_Opaque; break;
54
55 case kR8G8_unorm_SkColorType: return;
56 case kR16G16_unorm_SkColorType: return;
57 case kR16G16_float_SkColorType: return;
58 case kA16_unorm_SkColorType: return;
59 case kA16_float_SkColorType: return;
60 }
61
62 skcms_ICCProfile srcProfile = *skcms_sRGB_profile();
63 if (auto cs = bitmap.colorSpace()) {
64 cs->toProfile(&srcProfile);
65 }
66
67 // NOTE: If the color type is R8, we told skcms it's actually G8 above. To get red PNGs,
68 // we tweak the source color space to throw away any green and blue:
69 if (bitmap.colorType() == kR8_unorm_SkColorType) {
70 srcProfile.toXYZD50.vals[0][1] = srcProfile.toXYZD50.vals[0][2] = 0;
71 srcProfile.toXYZD50.vals[1][1] = srcProfile.toXYZD50.vals[1][2] = 0;
72 srcProfile.toXYZD50.vals[2][1] = srcProfile.toXYZD50.vals[2][2] = 0;
73 }
74
75 // Our common format that can represent anything we draw and encode as a PNG:
76 // - 16-bit big-endian RGBA
77 // - unpremul
78 // - Rec. 2020 gamut and transfer function
79 skcms_PixelFormat dstFmt = skcms_PixelFormat_RGBA_16161616BE;
80 skcms_AlphaFormat dstAlpha = skcms_AlphaFormat_Unpremul;
81 skcms_ICCProfile dstProfile;
82 rec2020()->toProfile(&dstProfile);
83
84 int N = fSize.width() * fSize.height();
85 fPixels.reset(new uint64_t[N]);
86
87 const void* src = bitmap.getPixels();
88 void* dst = fPixels.get();
89 while (N > 0) {
90 int todo = std::min(N, 1<<27); // Keep todo*8 <= 1B; skcms requires N*bpp < MAX_INT.
91 if (!skcms_Transform(src, srcFmt, srcAlpha, &srcProfile,
92 dst, dstFmt, dstAlpha, &dstProfile, todo)) {
93 SkASSERT(false);
94 fPixels.reset(nullptr);
95 break;
96 }
97 src = (char*)src + todo*SkColorTypeBytesPerPixel(bitmap.colorType());
98 dst = (char*)dst + todo*sizeof(uint64_t);
99 N -= todo;
100 }
101 }
102
feedHash(SkWStream * st) const103 void HashAndEncode::feedHash(SkWStream* st) const {
104 st->write(&fSize, sizeof(fSize));
105 if (const uint64_t* px = fPixels.get()) {
106 st->write(px, sizeof(*px) * fSize.width() * fSize.height());
107 }
108
109 // N.B. changing salt will change the hash of all images produced by DM,
110 // and will cause tens of thousands of new images to be uploaded to Gold.
111 int salt = 1;
112 st->write(&salt, sizeof(salt));
113 }
114
115 // NOTE: HashAndEncode uses libpng directly rather than through an abstraction
116 // like SkPngEncoder to make sure we get stable, portable results independent
117 // of any changes to Skia production encoder.
118
encodePNG(SkWStream * st,const char * md5,CommandLineFlags::StringArray key,CommandLineFlags::StringArray properties) const119 bool HashAndEncode::encodePNG(SkWStream* st,
120 const char* md5,
121 CommandLineFlags::StringArray key,
122 CommandLineFlags::StringArray properties) const {
123 if (!fPixels) {
124 return false;
125 }
126
127 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
128 if (!png) {
129 return false;
130 }
131
132 png_infop info = png_create_info_struct(png);
133 if (!info) {
134 png_destroy_write_struct(&png, &info);
135 return false;
136 }
137 auto write_to_stream = +[](png_structp png, png_bytep ptr, png_size_t len) {
138 auto st = (SkWStream*)png_get_io_ptr(png);
139 if (!st->write(ptr, len)) {
140 png_error(png, "HashAndEncode::encodePNG() failed writing stream");
141 }
142 };
143 png_set_write_fn(png, st, write_to_stream, nullptr);
144
145 SkString description;
146 description.append("Key: ");
147 for (int i = 0; i < key.size(); i++) {
148 description.appendf("%s ", key[i]);
149 }
150 description.append("Properties: ");
151 for (int i = 0; i < properties.size(); i++) {
152 description.appendf("%s ", properties[i]);
153 }
154 description.appendf("MD5: %s", md5);
155
156 png_text text[2];
157 text[0].key = (png_charp)"Author";
158 text[0].text = (png_charp)"DM unified Rec.2020";
159 text[0].compression = PNG_TEXT_COMPRESSION_NONE;
160 text[1].key = (png_charp)"Description";
161 text[1].text = (png_charp)description.c_str();
162 text[1].compression = PNG_TEXT_COMPRESSION_NONE;
163 png_set_text(png, info, text, std::size(text));
164
165 png_set_IHDR(png, info, (png_uint_32)fSize.width()
166 , (png_uint_32)fSize.height()
167 , 16/*bits per channel*/
168 , PNG_COLOR_TYPE_RGB_ALPHA
169 , PNG_INTERLACE_NONE
170 , PNG_COMPRESSION_TYPE_DEFAULT
171 , PNG_FILTER_TYPE_DEFAULT);
172
173 // Fastest encoding and decoding, at slight file size cost is no filtering, compression 1.
174 png_set_filter(png, PNG_FILTER_TYPE_BASE, PNG_FILTER_NONE);
175 png_set_compression_level(png, 1);
176
177 static const sk_sp<SkData> profile =
178 SkWriteICCProfile(SkNamedTransferFn::kRec2020, SkNamedGamut::kRec2020);
179 png_set_iCCP(png, info,
180 "Rec.2020",
181 0/*compression type... no idea what options are available here*/,
182 (png_const_bytep)profile->data(),
183 (png_uint_32) profile->size());
184
185 png_write_info(png, info);
186 for (int y = 0; y < fSize.height(); y++) {
187 png_write_row(png, (png_bytep)(fPixels.get() + y*fSize.width()));
188 }
189 png_write_end(png, info);
190
191 png_destroy_write_struct(&png, &info);
192 return true;
193 }
194
195