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" |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 24 | |
| 25 | #include "SkStream.h" |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 26 | #include "SkColorPriv.h" |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 27 | #include "SkBlurMaskFilter.h" |
| 28 | |
| 29 | static void setNamedTypeface(SkPaint* paint, const char name[]) { |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 30 | paint->setTypeface(SkTypeface::MakeFromName(name, SkFontStyle())); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static uint16_t gBG[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF }; |
| 34 | |
| 35 | class XfermodesBlurView : public SampleView { |
| 36 | SkBitmap fBG; |
| 37 | SkBitmap fSrcB, fDstB; |
| 38 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 39 | void draw_mode(SkCanvas* canvas, SkBlendMode mode, int alpha, SkScalar x, SkScalar y) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 40 | SkPaint p; |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 41 | p.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, |
| 42 | SkBlurMask::ConvertRadiusToSigma(5), |
| 43 | SkBlurMaskFilter::kNone_BlurFlag)); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 44 | |
| 45 | SkScalar ww = SkIntToScalar(W); |
| 46 | SkScalar hh = SkIntToScalar(H); |
| 47 | |
| 48 | // draw a circle covering the upper |
| 49 | // left three quarters of the canvas |
| 50 | p.setColor(0xFFCC44FF); |
| 51 | SkRect r; |
| 52 | r.set(0, 0, ww*3/4, hh*3/4); |
| 53 | r.offset(x, y); |
| 54 | canvas->drawOval(r, p); |
| 55 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 56 | p.setBlendMode(mode); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 57 | |
| 58 | // draw a square overlapping the circle |
| 59 | // in the lower right of the canvas |
| 60 | p.setColor(0x00AA6633 | alpha << 24); |
| 61 | r.set(ww/3, hh/3, ww*19/20, hh*19/20); |
| 62 | r.offset(x, y); |
| 63 | canvas->drawRect(r, p); |
| 64 | } |
| 65 | |
| 66 | public: |
| 67 | const static int W = 64; |
| 68 | const static int H = 64; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 69 | XfermodesBlurView() { |
commit-bot@chromium.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 70 | fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType, kPremul_SkAlphaType), |
| 71 | gBG, 4); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | protected: |
| 75 | // overrides from SkEventSink |
| 76 | virtual bool onQuery(SkEvent* evt) { |
| 77 | if (SampleCode::TitleQ(*evt)) { |
| 78 | SampleCode::TitleR(evt, "XfermodesBlur"); |
| 79 | return true; |
| 80 | } |
| 81 | return this->INHERITED::onQuery(evt); |
| 82 | } |
| 83 | |
| 84 | virtual void onDrawContent(SkCanvas* canvas) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 85 | canvas->translate(SkIntToScalar(10), SkIntToScalar(20)); |
| 86 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 87 | if (false) { |
| 88 | SkPaint paint; |
| 89 | paint.setAntiAlias(true); |
| 90 | paint.setTextSize(50); |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 91 | paint.setTypeface(SkTypeface::MakeFromName("Arial Unicode MS", SkFontStyle())); |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 92 | char buffer[10]; |
| 93 | size_t len = SkUTF8_FromUnichar(0x8500, buffer); |
| 94 | canvas->drawText(buffer, len, 40, 40, paint); |
| 95 | return; |
| 96 | } |
reed@google.com | 0e0c94c | 2011-08-04 18:11:01 +0000 | [diff] [blame] | 97 | if (false) { |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 98 | SkPaint paint; |
| 99 | paint.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 100 | |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 101 | SkRect r0 = { 0, 0, 10.5f, 20 }; |
| 102 | SkRect r1 = { 10.5f, 10, 20, 30 }; |
| 103 | paint.setColor(SK_ColorRED); |
| 104 | canvas->drawRect(r0, paint); |
| 105 | paint.setColor(SK_ColorBLUE); |
| 106 | canvas->drawRect(r1, paint); |
| 107 | return; |
| 108 | } |
| 109 | |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 110 | const SkBlendMode gModes[] = { |
| 111 | SkBlendMode::kClear, |
| 112 | SkBlendMode::kSrc, |
| 113 | SkBlendMode::kDst, |
| 114 | SkBlendMode::kSrcOver, |
| 115 | SkBlendMode::kDstOver, |
| 116 | SkBlendMode::kSrcIn, |
| 117 | SkBlendMode::kDstIn, |
| 118 | SkBlendMode::kSrcOut, |
| 119 | SkBlendMode::kDstOut, |
| 120 | SkBlendMode::kSrcATop, |
| 121 | SkBlendMode::kDstATop, |
| 122 | SkBlendMode::kXor, |
| 123 | SkBlendMode::kPlus, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | const SkScalar w = SkIntToScalar(W); |
| 127 | const SkScalar h = SkIntToScalar(H); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 128 | SkMatrix m; |
| 129 | m.setScale(SkIntToScalar(6), SkIntToScalar(6)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 130 | auto s = SkShader::MakeBitmapShader(fBG, SkShader::kRepeat_TileMode, |
| 131 | SkShader::kRepeat_TileMode, &m); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 132 | |
| 133 | SkPaint labelP; |
| 134 | labelP.setAntiAlias(true); |
| 135 | labelP.setLCDRenderText(true); |
| 136 | labelP.setTextAlign(SkPaint::kCenter_Align); |
| 137 | setNamedTypeface(&labelP, "Menlo Regular"); |
| 138 | |
| 139 | const int W = 5; |
| 140 | |
| 141 | SkScalar x0 = 0; |
| 142 | for (int twice = 0; twice < 2; twice++) { |
| 143 | SkScalar x = x0, y = 0; |
| 144 | for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 145 | SkRect r; |
| 146 | r.set(x, y, x+w, y+h); |
| 147 | |
| 148 | SkPaint p; |
| 149 | p.setStyle(SkPaint::kFill_Style); |
| 150 | p.setShader(s); |
| 151 | canvas->drawRect(r, p); |
| 152 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 153 | canvas->saveLayer(&r, nullptr); |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 154 | draw_mode(canvas, gModes[i], twice ? 0x88 : 0xFF, r.fLeft, r.fTop); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 155 | canvas->restore(); |
| 156 | |
| 157 | r.inset(-SK_ScalarHalf, -SK_ScalarHalf); |
| 158 | p.setStyle(SkPaint::kStroke_Style); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 159 | p.setShader(nullptr); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 160 | canvas->drawRect(r, p); |
| 161 | |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 162 | const char* label = SkBlendMode_Name(gModes[i]); |
| 163 | canvas->drawText(label, strlen(label), |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 164 | x + w/2, y - labelP.getTextSize()/2, labelP); |
| 165 | x += w + SkIntToScalar(10); |
| 166 | if ((i % W) == W - 1) { |
| 167 | x = x0; |
| 168 | y += h + SkIntToScalar(30); |
| 169 | } |
| 170 | } |
| 171 | x0 += SkIntToScalar(400); |
| 172 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | private: |
| 176 | typedef SampleView INHERITED; |
| 177 | }; |
| 178 | |
| 179 | ////////////////////////////////////////////////////////////////////////////// |
| 180 | |
| 181 | static SkView* MyFactory() { return new XfermodesBlurView; } |
| 182 | static SkViewRegister reg(MyFactory); |