OLD | NEW |
(Empty) | |
| 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" |
| 9 |
| 10 #include "Resources.h" |
| 11 #include "SkGradientShader.h" |
| 12 |
| 13 DEF_SIMPLE_GM(gamma, canvas, 500, 200) { |
| 14 SkPaint p; |
| 15 const SkScalar sz = 50.0f; |
| 16 const int szInt = SkScalarTruncToInt(sz); |
| 17 const SkScalar tx = sz + 5.0f; |
| 18 const SkRect r = SkRect::MakeXYWH(0, 0, sz, sz); |
| 19 SkShader::TileMode rpt = SkShader::kRepeat_TileMode; |
| 20 |
| 21 SkBitmap ditherBmp; |
| 22 ditherBmp.allocN32Pixels(2, 2); |
| 23 SkPMColor* pixels = reinterpret_cast<SkPMColor*>(ditherBmp.getPixels()); |
| 24 pixels[0] = pixels[3] = SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF); |
| 25 pixels[1] = pixels[2] = SkPackARGB32(0xFF, 0, 0, 0); |
| 26 |
| 27 SkBitmap linearGreyBmp; |
| 28 SkImageInfo linearGreyInfo = SkImageInfo::MakeN32(szInt, szInt, kOpaque_SkAl
phaType, |
| 29 kLinear_SkColorProfileType
); |
| 30 linearGreyBmp.allocPixels(linearGreyInfo); |
| 31 linearGreyBmp.eraseARGB(0xFF, 0x7F, 0x7F, 0x7F); |
| 32 |
| 33 SkBitmap srgbGreyBmp; |
| 34 SkImageInfo srgbGreyInfo = SkImageInfo::MakeN32(szInt, szInt, kOpaque_SkAlph
aType, |
| 35 kSRGB_SkColorProfileType); |
| 36 srgbGreyBmp.allocPixels(srgbGreyInfo); |
| 37 srgbGreyBmp.eraseARGB(0xFF, 0xBC, 0xBC, 0xBC); |
| 38 |
| 39 SkPaint textPaint; |
| 40 textPaint.setColor(SK_ColorWHITE); |
| 41 |
| 42 // Helpers: |
| 43 auto advance = [&]() { |
| 44 canvas->translate(tx, 0); |
| 45 p.reset(); |
| 46 }; |
| 47 |
| 48 auto nextRect = [&](const char* label, const char* label2) { |
| 49 canvas->drawRect(r, p); |
| 50 canvas->drawText(label, strlen(label), 0, sz + textPaint.getFontSpacing(
), textPaint); |
| 51 if (label2) { |
| 52 canvas->drawText(label2, strlen(label2), 0, sz + 2 * textPaint.getFo
ntSpacing(), |
| 53 textPaint); |
| 54 } |
| 55 advance(); |
| 56 }; |
| 57 |
| 58 auto nextBitmap = [&](const SkBitmap& bmp, const char* label) { |
| 59 canvas->drawBitmap(bmp, 0, 0); |
| 60 canvas->drawText(label, strlen(label), 0, sz + textPaint.getFontSpacing(
), textPaint); |
| 61 advance(); |
| 62 }; |
| 63 |
| 64 auto nextXferRect = [&](SkColor srcColor, SkXfermode::Mode mode, SkColor dst
Color) { |
| 65 p.setColor(dstColor); |
| 66 canvas->drawRect(r, p); |
| 67 p.setColor(srcColor); |
| 68 p.setXfermodeMode(mode); |
| 69 canvas->drawRect(r, p); |
| 70 |
| 71 SkString srcText = SkStringPrintf("%08X", srcColor); |
| 72 SkString dstText = SkStringPrintf("%08X", dstColor); |
| 73 canvas->drawText(srcText.c_str(), srcText.size(), 0, sz + textPaint.getF
ontSpacing(), |
| 74 textPaint); |
| 75 const char* modeName = SkXfermode::ModeName(mode); |
| 76 canvas->drawText(modeName, strlen(modeName), 0, sz + 2 * textPaint.getFo
ntSpacing(), |
| 77 textPaint); |
| 78 canvas->drawText(dstText.c_str(), dstText.size(), 0, sz + 3 * textPaint.
getFontSpacing(), |
| 79 textPaint); |
| 80 advance(); |
| 81 }; |
| 82 |
| 83 // Necessary for certain Xfermode tests to work (ie some of them output whit
e @ 50% alpha): |
| 84 canvas->clear(SK_ColorBLACK); |
| 85 |
| 86 // *Everything* should be perceptually 50% grey. Only the first rectangle |
| 87 // is guaranteed to draw that way, though. |
| 88 canvas->save(); |
| 89 |
| 90 // Black/white dither, pixel perfect. This is ground truth. |
| 91 p.setShader(SkShader::CreateBitmapShader( |
| 92 ditherBmp, rpt, rpt))->unref(); |
| 93 p.setFilterQuality(SkFilterQuality::kNone_SkFilterQuality); |
| 94 nextRect("Dither", "Reference"); |
| 95 |
| 96 // Black/white dither, sampled at half-texel offset. Tests bilerp. |
| 97 // NOTE: We need to apply a non-identity scale and/or rotation to trick |
| 98 // the raster pipeline into *not* snapping to nearest. |
| 99 SkMatrix offsetMatrix = SkMatrix::Concat( |
| 100 SkMatrix::MakeScale(-1.0f), SkMatrix::MakeTrans(0.5f, 0.0f)); |
| 101 p.setShader(SkShader::CreateBitmapShader( |
| 102 ditherBmp, rpt, rpt, &offsetMatrix))->unref(); |
| 103 p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality); |
| 104 nextRect("Dither", "Bilerp"); |
| 105 |
| 106 // Black/white dither, scaled down by 2x. Tests minification. |
| 107 SkMatrix scaleMatrix = SkMatrix::MakeScale(0.5f); |
| 108 p.setShader(SkShader::CreateBitmapShader( |
| 109 ditherBmp, rpt, rpt, &scaleMatrix))->unref(); |
| 110 p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality); |
| 111 nextRect("Dither", "Scale"); |
| 112 |
| 113 // 50% grey via paint color. |
| 114 p.setColor(0xff7f7f7f); |
| 115 nextRect("Color", 0); |
| 116 |
| 117 // Black -> White gradient, scaled to sample just the middle. |
| 118 // Tests gradient interpolation. |
| 119 SkPoint points[2] = { |
| 120 SkPoint::Make(0 - (sz * 10), 0), |
| 121 SkPoint::Make(sz + (sz * 10), 0) |
| 122 }; |
| 123 SkColor colors[2] = { SK_ColorBLACK, SK_ColorWHITE }; |
| 124 p.setShader(SkGradientShader::CreateLinear( |
| 125 points, colors, nullptr, 2, SkShader::kClamp_TileMode))->unref(); |
| 126 nextRect("Gradient", 0); |
| 127 |
| 128 // 50% grey from linear bitmap, with drawBitmap |
| 129 nextBitmap(linearGreyBmp, "Lnr BMP"); |
| 130 |
| 131 // 50% grey from sRGB bitmap, with drawBitmap |
| 132 nextBitmap(srgbGreyBmp, "sRGB BMP"); |
| 133 |
| 134 // Bitmap wrapped in a shader (linear): |
| 135 p.setShader(SkShader::CreateBitmapShader(linearGreyBmp, rpt, rpt))->unref(); |
| 136 p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality); |
| 137 nextRect("Lnr BMP", "Shader"); |
| 138 |
| 139 // Bitmap wrapped in a shader (sRGB): |
| 140 p.setShader(SkShader::CreateBitmapShader(srgbGreyBmp, rpt, rpt))->unref(); |
| 141 p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality); |
| 142 nextRect("sRGB BMP", "Shader"); |
| 143 |
| 144 // Carriage return. |
| 145 canvas->restore(); |
| 146 canvas->translate(0, 2 * sz); |
| 147 |
| 148 const U8CPU sqrtHalf = 0xB4; |
| 149 const SkColor sqrtHalfAlpha = SkColorSetARGB(sqrtHalf, 0, 0, 0); |
| 150 const SkColor sqrtHalfWhite = SkColorSetARGB(0xFF, sqrtHalf, sqrtHalf, sqrtH
alf); |
| 151 |
| 152 // Xfermode tests, all done off-screen so certain modes work... |
| 153 |
| 154 canvas->saveLayer(nullptr, nullptr); |
| 155 |
| 156 nextXferRect(0x7fffffff, SkXfermode::kSrcOver_Mode, SK_ColorBLACK); |
| 157 nextXferRect(0x7f000000, SkXfermode::kSrcOver_Mode, SK_ColorWHITE); |
| 158 |
| 159 nextXferRect(SK_ColorBLACK, SkXfermode::kDstOver_Mode, 0x7fffffff); |
| 160 nextXferRect(SK_ColorWHITE, SkXfermode::kSrcIn_Mode, 0x7fff00ff); |
| 161 nextXferRect(0x7fff00ff, SkXfermode::kDstIn_Mode, SK_ColorWHITE); |
| 162 nextXferRect(sqrtHalfWhite, SkXfermode::kSrcIn_Mode, sqrtHalfAlpha); |
| 163 nextXferRect(sqrtHalfAlpha, SkXfermode::kDstIn_Mode, sqrtHalfWhite); |
| 164 |
| 165 nextXferRect(0xff3f3f3f, SkXfermode::kPlus_Mode, 0xff3f3f3f); |
| 166 nextXferRect(sqrtHalfWhite, SkXfermode::kModulate_Mode, sqrtHalfWhite); |
| 167 |
| 168 canvas->restore(); |
| 169 |
| 170 canvas->restore(); |
| 171 } |
OLD | NEW |