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