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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "tools/ToolUtils.h" |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 30 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 31 | #include <string.h> |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 32 | |
brianosman | 490452e | 2016-09-13 08:24:56 -0700 | [diff] [blame] | 33 | DEF_SIMPLE_GM(gamma, canvas, 850, 200) { |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 34 | SkPaint p; |
| 35 | const SkScalar sz = 50.0f; |
| 36 | const int szInt = SkScalarTruncToInt(sz); |
brianosman | 7a99a0a | 2016-08-30 07:08:07 -0700 | [diff] [blame] | 37 | const SkScalar tx = sz + 15.0f; |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 38 | const SkRect r = SkRect::MakeXYWH(0, 0, sz, sz); |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 39 | SkTileMode rpt = SkTileMode::kRepeat; |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 40 | auto srgbColorSpace = SkColorSpace::MakeSRGB(); |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 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; |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 49 | SkImageInfo linearGreyInfo = SkImageInfo::MakeN32(szInt, szInt, kOpaque_SkAlphaType, nullptr); |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 50 | linearGreyBmp.allocPixels(linearGreyInfo); |
| 51 | linearGreyBmp.eraseARGB(0xFF, 0x7F, 0x7F, 0x7F); |
| 52 | |
| 53 | SkBitmap srgbGreyBmp; |
| 54 | SkImageInfo srgbGreyInfo = SkImageInfo::MakeN32(szInt, szInt, kOpaque_SkAlphaType, |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 55 | srgbColorSpace); |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 56 | srgbGreyBmp.allocPixels(srgbGreyInfo); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 57 | // 0xBC = 255 * linear_to_srgb(0.5f) |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 58 | srgbGreyBmp.eraseARGB(0xFF, 0xBC, 0xBC, 0xBC); |
| 59 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 60 | SkBitmap mipmapBmp; |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 61 | SkImageInfo mipmapInfo = SkImageInfo::Make(2, 2, kN32_SkColorType, kOpaque_SkAlphaType, |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 62 | srgbColorSpace); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 63 | mipmapBmp.allocPixels(mipmapInfo); |
| 64 | SkPMColor* mipmapPixels = reinterpret_cast<SkPMColor*>(mipmapBmp.getPixels()); |
reed | 6644d93 | 2016-06-10 11:41:47 -0700 | [diff] [blame] | 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); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 69 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 70 | SkFont font(ToolUtils::create_portable_typeface()); |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 71 | |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 72 | SkPaint textPaint; |
brianosman | 7a99a0a | 2016-08-30 07:08:07 -0700 | [diff] [blame] | 73 | textPaint.setAntiAlias(true); |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 74 | textPaint.setColor(SK_ColorWHITE); |
| 75 | |
| 76 | // Helpers: |
| 77 | auto advance = [&]() { |
| 78 | canvas->translate(tx, 0); |
| 79 | p.reset(); |
| 80 | }; |
| 81 | |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 82 | auto drawString = [&](const char str[], SkScalar x, SkScalar y) { |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 83 | canvas->drawSimpleText(str, strlen(str), SkTextEncoding::kUTF8, x, y, font, textPaint); |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 84 | }; |
| 85 | |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 86 | auto nextRect = [&](const char* label, const char* label2) { |
| 87 | canvas->drawRect(r, p); |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 88 | drawString(label, 0, sz + font.getSpacing()); |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 89 | if (label2) { |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 90 | drawString(label2, 0, sz + 2 * font.getSpacing()); |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 91 | } |
| 92 | advance(); |
| 93 | }; |
| 94 | |
| 95 | auto nextBitmap = [&](const SkBitmap& bmp, const char* label) { |
| 96 | canvas->drawBitmap(bmp, 0, 0); |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 97 | drawString(label, 0, sz + font.getSpacing()); |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 98 | advance(); |
| 99 | }; |
| 100 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 101 | auto nextXferRect = [&](SkColor srcColor, SkBlendMode mode, SkColor dstColor) { |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 102 | p.setColor(dstColor); |
| 103 | canvas->drawRect(r, p); |
| 104 | p.setColor(srcColor); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 105 | p.setBlendMode(mode); |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 106 | canvas->drawRect(r, p); |
| 107 | |
| 108 | SkString srcText = SkStringPrintf("%08X", srcColor); |
| 109 | SkString dstText = SkStringPrintf("%08X", dstColor); |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 110 | drawString(srcText.c_str(), 0, sz + font.getSpacing()); |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 111 | const char* modeName = SkBlendMode_Name(mode); |
Mike Reed | 12a6d45 | 2018-12-21 22:22:31 -0500 | [diff] [blame] | 112 | drawString(modeName, 0, sz + 2 * font.getSpacing()); |
| 113 | drawString(dstText.c_str(), 0, sz + 3 * font.getSpacing()); |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 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. |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 125 | p.setShader(ditherBmp.makeShader(rpt, rpt)); |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 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)); |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 134 | p.setShader(ditherBmp.makeShader(rpt, rpt, &offsetMatrix)); |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 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); |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 140 | p.setShader(ditherBmp.makeShader(rpt, rpt, &scaleMatrix)); |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 141 | p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality); |
| 142 | nextRect("Dither", "Scale"); |
| 143 | |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 144 | // 25%/75% dither, scaled down by 2x. Tests ALL aspects of minification. Specifically, are |
| 145 | // sRGB sources decoded to linear before computing mipmaps? |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 146 | p.setShader(mipmapBmp.makeShader(rpt, rpt, &scaleMatrix)); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 147 | p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality); |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 148 | nextRect("MipMaps", nullptr); |
brianosman | 33f6b3f | 2016-06-02 05:49:21 -0700 | [diff] [blame] | 149 | |
brianosman | 7a99a0a | 2016-08-30 07:08:07 -0700 | [diff] [blame] | 150 | // 50% grey via paint color. Paint color (SkColor) is specified to be sRGB! |
| 151 | p.setColor(0xffbcbcbc); |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 152 | nextRect("Color", nullptr); |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 153 | |
brianosman | 490452e | 2016-09-13 08:24:56 -0700 | [diff] [blame] | 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 }; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 162 | p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 2, SkTileMode::kClamp)); |
brianosman | 490452e | 2016-09-13 08:24:56 -0700 | [diff] [blame] | 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 }; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 174 | p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 2, SkTileMode::kClamp)); |
brianosman | 490452e | 2016-09-13 08:24:56 -0700 | [diff] [blame] | 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 }; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 186 | p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 3, SkTileMode::kClamp)); |
brianosman | 490452e | 2016-09-13 08:24:56 -0700 | [diff] [blame] | 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 }; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 198 | p.setShader(SkGradientShader::MakeLinear(points, colors, nullptr, 5, SkTileMode::kClamp)); |
brianosman | 490452e | 2016-09-13 08:24:56 -0700 | [diff] [blame] | 199 | nextRect("Gradient", "Texture"); |
| 200 | } |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 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): |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 209 | p.setShader(linearGreyBmp.makeShader(rpt, rpt)); |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 210 | p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality); |
| 211 | nextRect("Lnr BMP", "Shader"); |
| 212 | |
| 213 | // Bitmap wrapped in a shader (sRGB): |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 214 | p.setShader(srgbGreyBmp.makeShader(rpt, rpt)); |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 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 | |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 222 | // Xfermode tests, all done off-screen so certain modes work... |
| 223 | |
| 224 | canvas->saveLayer(nullptr, nullptr); |
| 225 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 226 | nextXferRect(0x7fffffff, SkBlendMode::kSrcOver, SK_ColorBLACK); |
| 227 | nextXferRect(0x7f000000, SkBlendMode::kSrcOver, SK_ColorWHITE); |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 228 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 229 | nextXferRect(SK_ColorBLACK, SkBlendMode::kDstOver, 0x7fffffff); |
| 230 | nextXferRect(SK_ColorWHITE, SkBlendMode::kSrcIn, 0x7fff00ff); |
| 231 | nextXferRect(0x7fff00ff, SkBlendMode::kDstIn, SK_ColorWHITE); |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 232 | |
brianosman | 7a99a0a | 2016-08-30 07:08:07 -0700 | [diff] [blame] | 233 | // 0x89 = 255 * linear_to_srgb(0.25) |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 234 | nextXferRect(0xff898989, SkBlendMode::kPlus, 0xff898989); |
brianosman | 7a99a0a | 2016-08-30 07:08:07 -0700 | [diff] [blame] | 235 | |
| 236 | // 0xDB = 255 * linear_to_srgb(sqrt(0.5)) |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 237 | nextXferRect(0xffdbdbdb, SkBlendMode::kModulate, 0xffdbdbdb); |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 238 | |
| 239 | canvas->restore(); |
brianosman | e5824b9 | 2016-02-26 11:57:23 -0800 | [diff] [blame] | 240 | } |