1 /* 2 * Copyright 2015 Google Inc. 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" 9 #include "include/core/SkBitmap.h" 10 #include "include/core/SkBlendMode.h" 11 #include "include/core/SkCanvas.h" 12 #include "include/core/SkColor.h" 13 #include "include/core/SkColorPriv.h" 14 #include "include/core/SkColorSpace.h" 15 #include "include/core/SkFilterQuality.h" 16 #include "include/core/SkFont.h" 17 #include "include/core/SkFontTypes.h" 18 #include "include/core/SkImageInfo.h" 19 #include "include/core/SkMatrix.h" 20 #include "include/core/SkPaint.h" 21 #include "include/core/SkPoint.h" 22 #include "include/core/SkRect.h" 23 #include "include/core/SkScalar.h" 24 #include "include/core/SkShader.h" 25 #include "include/core/SkString.h" 26 #include "include/core/SkTileMode.h" 27 #include "include/core/SkTypeface.h" 28 #include "include/effects/SkGradientShader.h" 29 #include "tools/ToolUtils.h" 30 31 #include <string.h> 32 33 DEF_SIMPLE_GM(gamma, canvas, 850, 200) { 34 SkPaint p; 35 const SkScalar sz = 50.0f; 36 const int szInt = SkScalarTruncToInt(sz); 37 const SkScalar tx = sz + 15.0f; 38 const SkRect r = SkRect::MakeXYWH(0, 0, sz, sz); 39 SkTileMode rpt = SkTileMode::kRepeat; 40 auto srgbColorSpace = SkColorSpace::MakeSRGB(); 41 42 SkBitmap ditherBmp; 43 ditherBmp.allocN32Pixels(2, 2); 44 SkPMColor* pixels = reinterpret_cast<SkPMColor*>(ditherBmp.getPixels()); 45 pixels[0] = pixels[3] = SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF); 46 pixels[1] = pixels[2] = SkPackARGB32(0xFF, 0, 0, 0); 47 48 SkBitmap linearGreyBmp; 49 SkImageInfo linearGreyInfo = SkImageInfo::MakeN32(szInt, szInt, kOpaque_SkAlphaType, nullptr); 50 linearGreyBmp.allocPixels(linearGreyInfo); 51 linearGreyBmp.eraseARGB(0xFF, 0x7F, 0x7F, 0x7F); 52 53 SkBitmap srgbGreyBmp; 54 SkImageInfo srgbGreyInfo = SkImageInfo::MakeN32(szInt, szInt, kOpaque_SkAlphaType, 55 srgbColorSpace); 56 srgbGreyBmp.allocPixels(srgbGreyInfo); 57 // 0xBC = 255 * linear_to_srgb(0.5f) 58 srgbGreyBmp.eraseARGB(0xFF, 0xBC, 0xBC, 0xBC); 59 60 SkBitmap mipmapBmp; 61 SkImageInfo mipmapInfo = SkImageInfo::Make(2, 2, kN32_SkColorType, kOpaque_SkAlphaType, 62 srgbColorSpace); 63 mipmapBmp.allocPixels(mipmapInfo); 64 SkPMColor* mipmapPixels = reinterpret_cast<SkPMColor*>(mipmapBmp.getPixels()); 65 unsigned s25 = 0x89; // 255 * linear_to_srgb(0.25f) 66 unsigned s75 = 0xE1; // 255 * linear_to_srgb(0.75f) 67 mipmapPixels[0] = mipmapPixels[3] = SkPackARGB32(0xFF, s25, s25, s25); 68 mipmapPixels[1] = mipmapPixels[2] = SkPackARGB32(0xFF, s75, s75, s75); 69 70 SkFont font(ToolUtils::create_portable_typeface()); 71 72 SkPaint textPaint; 73 textPaint.setAntiAlias(true); 74 textPaint.setColor(SK_ColorWHITE); 75 76 // Helpers: __anond90c64660102() 77 auto advance = [&]() { 78 canvas->translate(tx, 0); 79 p.reset(); 80 }; 81 __anond90c64660202(const char str[], SkScalar x, SkScalar y) 82 auto drawString = [&](const char str[], SkScalar x, SkScalar y) { 83 canvas->drawSimpleText(str, strlen(str), SkTextEncoding::kUTF8, x, y, font, textPaint); 84 }; 85 __anond90c64660302(const char* label, const char* label2) 86 auto nextRect = [&](const char* label, const char* label2) { 87 canvas->drawRect(r, p); 88 drawString(label, 0, sz + font.getSpacing()); 89 if (label2) { 90 drawString(label2, 0, sz + 2 * font.getSpacing()); 91 } 92 advance(); 93 }; 94 __anond90c64660402(const SkBitmap& bmp, const char* label) 95 auto nextBitmap = [&](const SkBitmap& bmp, const char* label) { 96 canvas->drawBitmap(bmp, 0, 0); 97 drawString(label, 0, sz + font.getSpacing()); 98 advance(); 99 }; 100 __anond90c64660502(SkColor srcColor, SkBlendMode mode, SkColor dstColor) 101 auto nextXferRect = [&](SkColor srcColor, SkBlendMode mode, SkColor dstColor) { 102 p.setColor(dstColor); 103 canvas->drawRect(r, p); 104 p.setColor(srcColor); 105 p.setBlendMode(mode); 106 canvas->drawRect(r, p); 107 108 SkString srcText = SkStringPrintf("%08X", srcColor); 109 SkString dstText = SkStringPrintf("%08X", dstColor); 110 drawString(srcText.c_str(), 0, sz + font.getSpacing()); 111 const char* modeName = SkBlendMode_Name(mode); 112 drawString(modeName, 0, sz + 2 * font.getSpacing()); 113 drawString(dstText.c_str(), 0, sz + 3 * font.getSpacing()); 114 advance(); 115 }; 116 117 // Necessary for certain Xfermode tests to work (ie some of them output white @ 50% alpha): 118 canvas->clear(SK_ColorBLACK); 119 120 // *Everything* should be perceptually 50% grey. Only the first rectangle 121 // is guaranteed to draw that way, though. 122 canvas->save(); 123 124 // Black/white dither, pixel perfect. This is ground truth. 125 p.setShader(ditherBmp.makeShader(rpt, rpt)); 126 p.setFilterQuality(SkFilterQuality::kNone_SkFilterQuality); 127 nextRect("Dither", "Reference"); 128 129 // Black/white dither, sampled at half-texel offset. Tests bilerp. 130 // NOTE: We need to apply a non-identity scale and/or rotation to trick 131 // the raster pipeline into *not* snapping to nearest. 132 SkMatrix offsetMatrix = SkMatrix::Concat( 133 SkMatrix::MakeScale(-1.0f), SkMatrix::MakeTrans(0.5f, 0.0f)); 134 p.setShader(ditherBmp.makeShader(rpt, rpt, &offsetMatrix)); 135 p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality); 136 nextRect("Dither", "Bilerp"); 137 138 // Black/white dither, scaled down by 2x. Tests minification. 139 SkMatrix scaleMatrix = SkMatrix::MakeScale(0.5f); 140 p.setShader(ditherBmp.makeShader(rpt, rpt, &scaleMatrix)); 141 p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality); 142 nextRect("Dither", "Scale"); 143 144 // 25%/75% dither, scaled down by 2x. Tests ALL aspects of minification. Specifically, are 145 // sRGB sources decoded to linear before computing mipmaps? 146 p.setShader(mipmapBmp.makeShader(rpt, rpt, &scaleMatrix)); 147 p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality); 148 nextRect("MipMaps", nullptr); 149 150 // 50% grey via paint color. Paint color (SkColor) is specified to be sRGB! 151 p.setColor(0xffbcbcbc); 152 nextRect("Color", nullptr); 153 154 { 155 // Black -> White gradient, scaled to sample just the middle. 156 // Tests gradient interpolation. 157 SkPoint points[2] = { 158 SkPoint::Make(0 - (sz * 10), 0), 159 SkPoint::Make(sz + (sz * 10), 0) 160 }; 161 SkColor colors[2] = { SK_ColorBLACK, SK_ColorWHITE }; 162 p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 2, SkTileMode::kClamp)); 163 nextRect("Gradient", "Interpolation"); 164 } 165 166 { 167 // Shallow gradient around 50% (perceptual) gray. Endpoints are SkColor, so sRGB. 168 // Tests gamma-correction of gradient stops before interpolation in two-stop case 169 SkPoint points[2] = { 170 SkPoint::Make(0, 0), 171 SkPoint::Make(sz, 0) 172 }; 173 SkColor colors[2] = { 0xffbbbbbb, 0xffbdbdbd }; 174 p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 2, SkTileMode::kClamp)); 175 nextRect("Gradient", "Endpoints"); 176 } 177 178 { 179 // Shallow 3-stop gradient around 50% (perceptual) gray. Endpoints are SkColor, so sRGB. 180 // Tests gamma-correction of gradient stops before interpolation in three-stop case 181 SkPoint points[2] = { 182 SkPoint::Make(0, 0), 183 SkPoint::Make(sz, 0) 184 }; 185 SkColor colors[3] = { 0xffbbbbbb, 0xffbdbdbd, 0xffbbbbbb }; 186 p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 3, SkTileMode::kClamp)); 187 nextRect("Gradient", "3-Stop"); 188 } 189 190 { 191 // Shallow N-stop gradient around 50% (perceptual) gray. Endpoints are SkColor, so sRGB. 192 // Tests gamma-correction of gradient stops before interpolation in texture implementation 193 SkPoint points[2] = { 194 SkPoint::Make(0, 0), 195 SkPoint::Make(sz, 0) 196 }; 197 SkColor colors[5] = { 0xffbbbbbb, 0xffbdbdbd, 0xffbbbbbb, 0xffbdbdbd, 0xffbbbbbb }; 198 p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 5, SkTileMode::kClamp)); 199 nextRect("Gradient", "Texture"); 200 } 201 202 // 50% grey from linear bitmap, with drawBitmap 203 nextBitmap(linearGreyBmp, "Lnr BMP"); 204 205 // 50% grey from sRGB bitmap, with drawBitmap 206 nextBitmap(srgbGreyBmp, "sRGB BMP"); 207 208 // Bitmap wrapped in a shader (linear): 209 p.setShader(linearGreyBmp.makeShader(rpt, rpt)); 210 p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality); 211 nextRect("Lnr BMP", "Shader"); 212 213 // Bitmap wrapped in a shader (sRGB): 214 p.setShader(srgbGreyBmp.makeShader(rpt, rpt)); 215 p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality); 216 nextRect("sRGB BMP", "Shader"); 217 218 // Carriage return. 219 canvas->restore(); 220 canvas->translate(0, 2 * sz); 221 222 // Xfermode tests, all done off-screen so certain modes work... 223 224 canvas->saveLayer(nullptr, nullptr); 225 226 nextXferRect(0x7fffffff, SkBlendMode::kSrcOver, SK_ColorBLACK); 227 nextXferRect(0x7f000000, SkBlendMode::kSrcOver, SK_ColorWHITE); 228 229 nextXferRect(SK_ColorBLACK, SkBlendMode::kDstOver, 0x7fffffff); 230 nextXferRect(SK_ColorWHITE, SkBlendMode::kSrcIn, 0x7fff00ff); 231 nextXferRect(0x7fff00ff, SkBlendMode::kDstIn, SK_ColorWHITE); 232 233 // 0x89 = 255 * linear_to_srgb(0.25) 234 nextXferRect(0xff898989, SkBlendMode::kPlus, 0xff898989); 235 236 // 0xDB = 255 * linear_to_srgb(sqrt(0.5)) 237 nextXferRect(0xffdbdbdb, SkBlendMode::kModulate, 0xffdbdbdb); 238 239 canvas->restore(); 240 } 241