epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
reed@google.com | bf0001d | 2014-01-13 14:53:55 +0000 | [diff] [blame] | 7 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 10 | #include "SkBlurMask.h" |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 12 | #include "SkCornerPathEffect.h" |
| 13 | #include "SkGradientShader.h" |
| 14 | #include "SkGraphics.h" |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 15 | #include "SkPath.h" |
| 16 | #include "SkRandom.h" |
| 17 | #include "SkRegion.h" |
| 18 | #include "SkShader.h" |
| 19 | #include "SkUtils.h" |
| 20 | #include "SkColorPriv.h" |
| 21 | #include "SkColorFilter.h" |
| 22 | #include "SkTime.h" |
| 23 | #include "SkTypeface.h" |
| 24 | #include "SkXfermode.h" |
| 25 | |
| 26 | #include "SkStream.h" |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 27 | #include "SkColorPriv.h" |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 28 | #include "SkBlurMaskFilter.h" |
| 29 | |
| 30 | static void setNamedTypeface(SkPaint* paint, const char name[]) { |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 31 | paint->setTypeface(SkTypeface::MakeFromName(name, SkFontStyle())); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static uint16_t gBG[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF }; |
| 35 | |
| 36 | class XfermodesBlurView : public SampleView { |
| 37 | SkBitmap fBG; |
| 38 | SkBitmap fSrcB, fDstB; |
| 39 | |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 40 | void draw_mode(SkCanvas* canvas, sk_sp<SkXfermode> mode, int alpha, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 41 | SkScalar x, SkScalar y) { |
| 42 | SkPaint p; |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 43 | p.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, |
| 44 | SkBlurMask::ConvertRadiusToSigma(5), |
| 45 | SkBlurMaskFilter::kNone_BlurFlag)); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 46 | |
| 47 | SkScalar ww = SkIntToScalar(W); |
| 48 | SkScalar hh = SkIntToScalar(H); |
| 49 | |
| 50 | // draw a circle covering the upper |
| 51 | // left three quarters of the canvas |
| 52 | p.setColor(0xFFCC44FF); |
| 53 | SkRect r; |
| 54 | r.set(0, 0, ww*3/4, hh*3/4); |
| 55 | r.offset(x, y); |
| 56 | canvas->drawOval(r, p); |
| 57 | |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 58 | p.setXfermode(std::move(mode)); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 59 | |
| 60 | // draw a square overlapping the circle |
| 61 | // in the lower right of the canvas |
| 62 | p.setColor(0x00AA6633 | alpha << 24); |
| 63 | r.set(ww/3, hh/3, ww*19/20, hh*19/20); |
| 64 | r.offset(x, y); |
| 65 | canvas->drawRect(r, p); |
| 66 | } |
| 67 | |
| 68 | public: |
| 69 | const static int W = 64; |
| 70 | const static int H = 64; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 71 | XfermodesBlurView() { |
commit-bot@chromium.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 72 | fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType, kPremul_SkAlphaType), |
| 73 | gBG, 4); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | protected: |
| 77 | // overrides from SkEventSink |
| 78 | virtual bool onQuery(SkEvent* evt) { |
| 79 | if (SampleCode::TitleQ(*evt)) { |
| 80 | SampleCode::TitleR(evt, "XfermodesBlur"); |
| 81 | return true; |
| 82 | } |
| 83 | return this->INHERITED::onQuery(evt); |
| 84 | } |
| 85 | |
| 86 | virtual void onDrawContent(SkCanvas* canvas) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 87 | canvas->translate(SkIntToScalar(10), SkIntToScalar(20)); |
| 88 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 89 | if (false) { |
| 90 | SkPaint paint; |
| 91 | paint.setAntiAlias(true); |
| 92 | paint.setTextSize(50); |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 93 | paint.setTypeface(SkTypeface::MakeFromName("Arial Unicode MS", SkFontStyle())); |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 94 | char buffer[10]; |
| 95 | size_t len = SkUTF8_FromUnichar(0x8500, buffer); |
| 96 | canvas->drawText(buffer, len, 40, 40, paint); |
| 97 | return; |
| 98 | } |
reed@google.com | 0e0c94c | 2011-08-04 18:11:01 +0000 | [diff] [blame] | 99 | if (false) { |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 100 | SkPaint paint; |
| 101 | paint.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 102 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 103 | SkRect r0 = { 0, 0, 10.5f, 20 }; |
| 104 | SkRect r1 = { 10.5f, 10, 20, 30 }; |
| 105 | paint.setColor(SK_ColorRED); |
| 106 | canvas->drawRect(r0, paint); |
| 107 | paint.setColor(SK_ColorBLUE); |
| 108 | canvas->drawRect(r1, paint); |
| 109 | return; |
| 110 | } |
| 111 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 112 | const struct { |
| 113 | SkXfermode::Mode fMode; |
| 114 | const char* fLabel; |
| 115 | } gModes[] = { |
| 116 | { SkXfermode::kClear_Mode, "Clear" }, |
| 117 | { SkXfermode::kSrc_Mode, "Src" }, |
| 118 | { SkXfermode::kDst_Mode, "Dst" }, |
| 119 | { SkXfermode::kSrcOver_Mode, "SrcOver" }, |
| 120 | { SkXfermode::kDstOver_Mode, "DstOver" }, |
| 121 | { SkXfermode::kSrcIn_Mode, "SrcIn" }, |
| 122 | { SkXfermode::kDstIn_Mode, "DstIn" }, |
| 123 | { SkXfermode::kSrcOut_Mode, "SrcOut" }, |
| 124 | { SkXfermode::kDstOut_Mode, "DstOut" }, |
| 125 | { SkXfermode::kSrcATop_Mode, "SrcATop" }, |
| 126 | { SkXfermode::kDstATop_Mode, "DstATop" }, |
| 127 | { SkXfermode::kXor_Mode, "Xor" }, |
| 128 | |
| 129 | { SkXfermode::kPlus_Mode, "Plus" }, |
reed@google.com | 8d3cd7a | 2013-01-30 21:36:11 +0000 | [diff] [blame] | 130 | /*{ SkXfermode::kModulate_Mode, "Modulate" }, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 131 | { SkXfermode::kScreen_Mode, "Screen" }, |
| 132 | { SkXfermode::kOverlay_Mode, "Overlay" }, |
| 133 | { SkXfermode::kDarken_Mode, "Darken" }, |
| 134 | { SkXfermode::kLighten_Mode, "Lighten" }, |
| 135 | { SkXfermode::kColorDodge_Mode, "ColorDodge" }, |
| 136 | { SkXfermode::kColorBurn_Mode, "ColorBurn" }, |
| 137 | { SkXfermode::kHardLight_Mode, "HardLight" }, |
| 138 | { SkXfermode::kSoftLight_Mode, "SoftLight" }, |
| 139 | { SkXfermode::kDifference_Mode, "Difference" }, |
| 140 | { SkXfermode::kExclusion_Mode, "Exclusion" },*/ |
| 141 | }; |
| 142 | |
| 143 | const SkScalar w = SkIntToScalar(W); |
| 144 | const SkScalar h = SkIntToScalar(H); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 145 | SkMatrix m; |
| 146 | m.setScale(SkIntToScalar(6), SkIntToScalar(6)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 147 | auto s = SkShader::MakeBitmapShader(fBG, SkShader::kRepeat_TileMode, |
| 148 | SkShader::kRepeat_TileMode, &m); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 149 | |
| 150 | SkPaint labelP; |
| 151 | labelP.setAntiAlias(true); |
| 152 | labelP.setLCDRenderText(true); |
| 153 | labelP.setTextAlign(SkPaint::kCenter_Align); |
| 154 | setNamedTypeface(&labelP, "Menlo Regular"); |
| 155 | |
| 156 | const int W = 5; |
| 157 | |
| 158 | SkScalar x0 = 0; |
| 159 | for (int twice = 0; twice < 2; twice++) { |
| 160 | SkScalar x = x0, y = 0; |
| 161 | for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 162 | SkRect r; |
| 163 | r.set(x, y, x+w, y+h); |
| 164 | |
| 165 | SkPaint p; |
| 166 | p.setStyle(SkPaint::kFill_Style); |
| 167 | p.setShader(s); |
| 168 | canvas->drawRect(r, p); |
| 169 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 170 | canvas->saveLayer(&r, nullptr); |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 171 | draw_mode(canvas, SkXfermode::Make(gModes[i].fMode), |
| 172 | twice ? 0x88 : 0xFF, r.fLeft, r.fTop); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 173 | canvas->restore(); |
| 174 | |
| 175 | r.inset(-SK_ScalarHalf, -SK_ScalarHalf); |
| 176 | p.setStyle(SkPaint::kStroke_Style); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 177 | p.setShader(nullptr); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 178 | canvas->drawRect(r, p); |
| 179 | |
| 180 | canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel), |
| 181 | x + w/2, y - labelP.getTextSize()/2, labelP); |
| 182 | x += w + SkIntToScalar(10); |
| 183 | if ((i % W) == W - 1) { |
| 184 | x = x0; |
| 185 | y += h + SkIntToScalar(30); |
| 186 | } |
| 187 | } |
| 188 | x0 += SkIntToScalar(400); |
| 189 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | private: |
| 193 | typedef SampleView INHERITED; |
| 194 | }; |
| 195 | |
| 196 | ////////////////////////////////////////////////////////////////////////////// |
| 197 | |
| 198 | static SkView* MyFactory() { return new XfermodesBlurView; } |
| 199 | static SkViewRegister reg(MyFactory); |