Chromium Code Reviews| 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, kLinear_SkColorProfileType); | |
| 29 linearGreyBmp.allocPixels(linearGreyInfo); | |
| 30 linearGreyBmp.eraseRGB(0x7F, 0x7F, 0x7F); | |
| 31 | |
| 32 SkBitmap srgbGreyBmp; | |
| 33 SkImageInfo srgbGreyInfo = SkImageInfo::MakeN32(szInt, szInt, kOpaque_SkAlph aType, kSRGB_SkColorProfileType); | |
| 34 srgbGreyBmp.allocPixels(srgbGreyInfo); | |
| 35 srgbGreyBmp.eraseRGB(0xBC, 0xBC, 0xBC); | |
| 36 | |
| 37 SkPaint textPaint; | |
| 38 textPaint.setColor(SK_ColorWHITE); | |
| 39 | |
| 40 // Helpers: | |
| 41 auto nextRect = [&](const char* label) { | |
| 42 canvas->drawRect(r, p); | |
| 43 canvas->drawText(label, strlen(label), 0, sz + textPaint.getFontSpacing( ), textPaint); | |
| 44 canvas->translate(tx, 0); | |
| 45 p.reset(); | |
| 46 }; | |
| 47 | |
| 48 auto nextBitmap = [&](const SkBitmap& bmp, const char* label) { | |
| 49 canvas->drawBitmap(bmp, 0, 0); | |
| 50 canvas->drawText(label, strlen(label), 0, sz + textPaint.getFontSpacing( ), textPaint); | |
| 51 canvas->translate(tx, 0); | |
| 52 }; | |
| 53 | |
| 54 auto nextXferRect = [&](SkColor srcColor, SkXfermode::Mode mode, SkColor dst Color) { | |
| 55 p.setColor(dstColor); | |
| 56 canvas->drawRect(r, p); | |
| 57 p.setColor(srcColor); | |
| 58 p.setXfermodeMode(mode); | |
| 59 canvas->drawRect(r, p); | |
| 60 | |
| 61 char buf[32]; | |
| 62 sprintf(buf, "%08X", srcColor); | |
| 63 canvas->drawText(buf, strlen(buf), 0, sz + textPaint.getFontSpacing(), t extPaint); | |
| 64 const char* modeName = SkXfermode::ModeName(mode); | |
| 65 canvas->drawText(modeName, strlen(modeName), 0, sz + 2 * textPaint.getFo ntSpacing(), textPaint); | |
| 66 sprintf(buf, "%08X", dstColor); | |
| 67 canvas->drawText(buf, strlen(buf), 0, sz + 3 * textPaint.getFontSpacing( ), textPaint); | |
| 68 | |
| 69 canvas->translate(tx, 0); | |
| 70 p.reset(); | |
| 71 }; | |
| 72 | |
| 73 // Necessary for certain Xfermodes to work: | |
| 74 canvas->clear(SK_ColorTRANSPARENT); | |
| 75 | |
| 76 // *Everything* should be perceptually 50% grey. Only the first rectangle | |
| 77 // is guaranteed to draw that way, though. | |
| 78 canvas->save(); | |
| 79 | |
| 80 // Black/white dither, pixel perfect. This is ground truth. | |
| 81 p.setShader(SkShader::CreateBitmapShader( | |
| 82 ditherBmp, rpt, rpt))->unref(); | |
| 83 p.setFilterQuality(SkFilterQuality::kNone_SkFilterQuality); | |
| 84 nextRect("Dither"); | |
| 85 | |
| 86 // Black/white dither, sampled at half-texel offset. Tests bilerp. | |
| 87 // NOTE: We need to apply a non-identity scale and/or rotation to trick | |
| 88 // the raster pipeline into *not* snapping to nearest. | |
| 89 SkMatrix offsetMatrix = SkMatrix::Concat( | |
| 90 SkMatrix::MakeScale(-1.0f), SkMatrix::MakeTrans(0.5f, 0.0f)); | |
| 91 p.setShader(SkShader::CreateBitmapShader( | |
| 92 ditherBmp, rpt, rpt, &offsetMatrix))->unref(); | |
| 93 p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality); | |
| 94 nextRect("Bilerp"); | |
| 95 | |
| 96 // Black/white dither, scaled down by 2x. Tests minification. | |
| 97 SkMatrix scaleMatrix = SkMatrix::MakeScale(0.5f); | |
| 98 p.setShader(SkShader::CreateBitmapShader( | |
| 99 ditherBmp, rpt, rpt, &scaleMatrix))->unref(); | |
| 100 p.setFilterQuality(SkFilterQuality::kMedium_SkFilterQuality); | |
| 101 nextRect("Scale"); | |
| 102 | |
| 103 // 50% grey via paint color. | |
| 104 p.setColor(0xff7f7f7f); | |
| 105 nextRect("Color"); | |
| 106 | |
| 107 // Black -> White gradient, scaled to sample just the middle. | |
| 108 // Tests gradient interpolation. | |
| 109 SkPoint points[2] = { | |
| 110 SkPoint::Make(0 - (sz * 10), 0), | |
| 111 SkPoint::Make(sz + (sz * 10), 0) | |
| 112 }; | |
| 113 SkColor colors[2] = { SK_ColorBLACK, SK_ColorWHITE }; | |
| 114 p.setShader(SkGradientShader::CreateLinear( | |
| 115 points, colors, nullptr, 2, SkShader::kClamp_TileMode))->unref(); | |
| 116 nextRect("Gradient"); | |
| 117 | |
| 118 // 50% grey from linear bitmap, with drawBitmap | |
| 119 nextBitmap(linearGreyBmp, "Lnr BMP"); | |
| 120 /* p.setShader(SkShader::CreateBitmapShader( | |
|
Brian Osman
2016/02/25 22:39:00
This is another test I want to add in, but I switc
| |
| 121 linearGreyBmp, rpt, rpt))->unref(); | |
| 122 p.setFilterQuality(SkFilterQuality::kNone_SkFilterQuality); | |
| 123 nextRect("Lnr BMP");*/ | |
| 124 | |
| 125 // 50% grey from sRGB bitmap, with drawBitmap | |
| 126 nextBitmap(srgbGreyBmp, "sRGB BMP"); | |
| 127 | |
| 128 // Carriage return. | |
| 129 canvas->restore(); | |
| 130 canvas->translate(0, 2 * sz); | |
| 131 | |
| 132 const U8CPU sqrtHalf = 0xB4; | |
| 133 const SkColor sqrtHalfAlpha = SkColorSetARGB(sqrtHalf, 0, 0, 0); | |
| 134 const SkColor sqrtHalfWhite = SkColorSetARGB(0xFF, sqrtHalf, sqrtHalf, sqrtH alf); | |
| 135 | |
| 136 // Xfermode tests. | |
| 137 nextXferRect(0x7fffffff, SkXfermode::kSrcOver_Mode, SK_ColorBLACK); | |
| 138 nextXferRect(0x7f000000, SkXfermode::kSrcOver_Mode, SK_ColorWHITE); | |
| 139 | |
| 140 nextXferRect(SK_ColorBLACK, SkXfermode::kDstOver_Mode, 0x7fffffff); | |
| 141 nextXferRect(SK_ColorWHITE, SkXfermode::kSrcIn_Mode, 0x7fff00ff); | |
| 142 nextXferRect(0x7fff00ff, SkXfermode::kDstIn_Mode, SK_ColorWHITE); | |
| 143 nextXferRect(sqrtHalfWhite, SkXfermode::kSrcIn_Mode, sqrtHalfAlpha); | |
| 144 nextXferRect(sqrtHalfAlpha, SkXfermode::kDstIn_Mode, sqrtHalfWhite); | |
| 145 | |
| 146 nextXferRect(0xff3f3f3f, SkXfermode::kPlus_Mode, 0xff3f3f3f); | |
| 147 nextXferRect(sqrtHalfWhite, SkXfermode::kModulate_Mode, sqrtHalfWhite); | |
| 148 | |
| 149 canvas->restore(); | |
| 150 } | |
| OLD | NEW |