egdaniel | ed3af66 | 2014-10-31 06:55:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | #include "gm.h" |
| 8 | #include "SkBitmap.h" |
| 9 | #include "SkGradientShader.h" |
| 10 | #include "SkShader.h" |
| 11 | #include "SkXfermode.h" |
| 12 | #include "../src/fonts/SkGScalerContext.h" |
| 13 | |
| 14 | namespace skiagm { |
| 15 | |
| 16 | static uint16_t gData[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF }; |
| 17 | |
| 18 | class ColorTypeXfermodeGM : public GM { |
egdaniel | ed3af66 | 2014-10-31 06:55:45 -0700 | [diff] [blame] | 19 | public: |
| 20 | const static int W = 64; |
| 21 | const static int H = 64; |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame^] | 22 | ColorTypeXfermodeGM() |
| 23 | : fColorType(NULL) { |
| 24 | } |
| 25 | |
| 26 | virtual ~ColorTypeXfermodeGM() { |
| 27 | SkSafeUnref(fColorType); |
| 28 | } |
| 29 | |
| 30 | protected: |
| 31 | void onOnceBeforeDraw() override { |
egdaniel | ed3af66 | 2014-10-31 06:55:45 -0700 | [diff] [blame] | 32 | const SkColor colors[] = { |
| 33 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, |
| 34 | SK_ColorMAGENTA, SK_ColorCYAN, SK_ColorYELLOW |
| 35 | }; |
| 36 | SkMatrix local; |
| 37 | local.setRotate(180); |
| 38 | SkShader* s = SkGradientShader::CreateSweep(0,0, colors, NULL, |
| 39 | SK_ARRAY_COUNT(colors), 0, &local); |
| 40 | |
| 41 | SkPaint paint; |
| 42 | paint.setAntiAlias(true); |
| 43 | paint.setShader(s)->unref(); |
| 44 | |
| 45 | SkTypeface* orig = sk_tool_utils::create_portable_typeface("Times", |
| 46 | SkTypeface::kBold); |
| 47 | if (NULL == orig) { |
| 48 | orig = SkTypeface::RefDefault(); |
| 49 | } |
| 50 | fColorType = SkNEW_ARGS(SkGTypeface, (orig, paint)); |
| 51 | orig->unref(); |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame^] | 52 | |
| 53 | fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType, |
| 54 | kOpaque_SkAlphaType), gData, 4); |
egdaniel | ed3af66 | 2014-10-31 06:55:45 -0700 | [diff] [blame] | 55 | } |
| 56 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 57 | virtual SkString onShortName() override { |
egdaniel | ed3af66 | 2014-10-31 06:55:45 -0700 | [diff] [blame] | 58 | return SkString("colortype_xfermodes"); |
| 59 | } |
| 60 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 61 | virtual SkISize onISize() override { |
egdaniel | ed3af66 | 2014-10-31 06:55:45 -0700 | [diff] [blame] | 62 | return SkISize::Make(400, 640); |
| 63 | } |
| 64 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 65 | virtual void onDraw(SkCanvas* canvas) override { |
egdaniel | ed3af66 | 2014-10-31 06:55:45 -0700 | [diff] [blame] | 66 | canvas->translate(SkIntToScalar(10), SkIntToScalar(20)); |
| 67 | |
| 68 | const struct { |
| 69 | SkXfermode::Mode fMode; |
| 70 | const char* fLabel; |
| 71 | } gModes[] = { |
| 72 | { SkXfermode::kClear_Mode, "Clear" }, |
| 73 | { SkXfermode::kSrc_Mode, "Src" }, |
| 74 | { SkXfermode::kDst_Mode, "Dst" }, |
| 75 | { SkXfermode::kSrcOver_Mode, "SrcOver" }, |
| 76 | { SkXfermode::kDstOver_Mode, "DstOver" }, |
| 77 | { SkXfermode::kSrcIn_Mode, "SrcIn" }, |
| 78 | { SkXfermode::kDstIn_Mode, "DstIn" }, |
| 79 | { SkXfermode::kSrcOut_Mode, "SrcOut" }, |
| 80 | { SkXfermode::kDstOut_Mode, "DstOut" }, |
| 81 | { SkXfermode::kSrcATop_Mode, "SrcATop" }, |
| 82 | { SkXfermode::kDstATop_Mode, "DstATop" }, |
| 83 | |
| 84 | { SkXfermode::kXor_Mode, "Xor" }, |
| 85 | { SkXfermode::kPlus_Mode, "Plus" }, |
| 86 | { SkXfermode::kModulate_Mode, "Modulate" }, |
| 87 | { SkXfermode::kScreen_Mode, "Screen" }, |
| 88 | { SkXfermode::kOverlay_Mode, "Overlay" }, |
| 89 | { SkXfermode::kDarken_Mode, "Darken" }, |
| 90 | { SkXfermode::kLighten_Mode, "Lighten" }, |
| 91 | { SkXfermode::kColorDodge_Mode, "ColorDodge" }, |
| 92 | { SkXfermode::kColorBurn_Mode, "ColorBurn" }, |
| 93 | { SkXfermode::kHardLight_Mode, "HardLight" }, |
| 94 | { SkXfermode::kSoftLight_Mode, "SoftLight" }, |
| 95 | { SkXfermode::kDifference_Mode, "Difference" }, |
| 96 | { SkXfermode::kExclusion_Mode, "Exclusion" }, |
| 97 | { SkXfermode::kMultiply_Mode, "Multiply" }, |
| 98 | { SkXfermode::kHue_Mode, "Hue" }, |
| 99 | { SkXfermode::kSaturation_Mode, "Saturation" }, |
| 100 | { SkXfermode::kColor_Mode, "Color" }, |
| 101 | { SkXfermode::kLuminosity_Mode, "Luminosity" }, |
| 102 | }; |
| 103 | |
| 104 | const SkScalar w = SkIntToScalar(W); |
| 105 | const SkScalar h = SkIntToScalar(H); |
| 106 | SkMatrix m; |
| 107 | m.setScale(SkIntToScalar(6), SkIntToScalar(6)); |
| 108 | SkShader* s = SkShader::CreateBitmapShader(fBG, |
| 109 | SkShader::kRepeat_TileMode, |
| 110 | SkShader::kRepeat_TileMode, |
| 111 | &m); |
| 112 | |
| 113 | SkPaint labelP; |
| 114 | labelP.setAntiAlias(true); |
| 115 | sk_tool_utils::set_portable_typeface(&labelP); |
| 116 | labelP.setTextAlign(SkPaint::kCenter_Align); |
| 117 | |
| 118 | SkPaint textP; |
| 119 | textP.setAntiAlias(true); |
| 120 | textP.setTypeface(fColorType); |
| 121 | textP.setTextSize(SkIntToScalar(70)); |
| 122 | |
| 123 | const int W = 5; |
| 124 | |
| 125 | SkScalar x0 = 0; |
| 126 | SkScalar y0 = 0; |
| 127 | SkScalar x = x0, y = y0; |
| 128 | for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) { |
| 129 | SkXfermode* mode = SkXfermode::Create(gModes[i].fMode); |
| 130 | SkAutoUnref aur(mode); |
| 131 | SkRect r; |
| 132 | r.set(x, y, x+w, y+h); |
| 133 | |
| 134 | SkPaint p; |
| 135 | p.setStyle(SkPaint::kFill_Style); |
| 136 | p.setShader(s); |
| 137 | canvas->drawRect(r, p); |
| 138 | |
| 139 | r.inset(-SK_ScalarHalf, -SK_ScalarHalf); |
| 140 | p.setStyle(SkPaint::kStroke_Style); |
| 141 | p.setShader(NULL); |
| 142 | canvas->drawRect(r, p); |
| 143 | |
| 144 | textP.setXfermode(mode); |
| 145 | canvas->drawText("H", 1, x+ w/10.f, y + 7.f*h/8.f, textP); |
| 146 | #if 1 |
| 147 | canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel), |
| 148 | x + w/2, y - labelP.getTextSize()/2, labelP); |
| 149 | #endif |
| 150 | x += w + SkIntToScalar(10); |
| 151 | if ((i % W) == W - 1) { |
| 152 | x = x0; |
| 153 | y += h + SkIntToScalar(30); |
| 154 | } |
| 155 | } |
| 156 | s->unref(); |
| 157 | } |
| 158 | |
egdaniel | ed3af66 | 2014-10-31 06:55:45 -0700 | [diff] [blame] | 159 | private: |
kkinnunen | b4a797f | 2015-05-21 06:15:28 -0700 | [diff] [blame^] | 160 | SkBitmap fBG; |
egdaniel | ed3af66 | 2014-10-31 06:55:45 -0700 | [diff] [blame] | 161 | SkTypeface* fColorType; |
| 162 | |
| 163 | typedef GM INHERITED; |
| 164 | }; |
| 165 | |
| 166 | ////////////////////////////////////////////////////////////////////////////// |
| 167 | |
| 168 | static GM* MyFactory(void*) { return new ColorTypeXfermodeGM; } |
| 169 | static GMRegistry reg(MyFactory); |
| 170 | |
| 171 | } |