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