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 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 8 | #include "Sample.h" |
Mike Reed | 75ae421 | 2018-01-23 11:24:08 -0500 | [diff] [blame] | 9 | #include "SkBitmap.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" |
Mike Reed | b579f07 | 2019-01-03 15:45:53 -0500 | [diff] [blame] | 13 | #include "SkFont.h" |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 14 | #include "SkGradientShader.h" |
| 15 | #include "SkGraphics.h" |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 16 | #include "SkPath.h" |
| 17 | #include "SkRandom.h" |
| 18 | #include "SkRegion.h" |
| 19 | #include "SkShader.h" |
Hal Canary | ea60b95 | 2018-08-21 11:45:46 -0400 | [diff] [blame] | 20 | #include "SkUTF.h" |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 21 | #include "SkColorPriv.h" |
| 22 | #include "SkColorFilter.h" |
Mike Reed | a697df9 | 2018-10-26 07:28:30 -0400 | [diff] [blame] | 23 | #include "SkTextUtils.h" |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 24 | #include "SkTime.h" |
| 25 | #include "SkTypeface.h" |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 26 | |
| 27 | #include "SkStream.h" |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 28 | #include "SkColorPriv.h" |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 29 | #include "SkBlurMaskFilter.h" |
| 30 | |
Mike Reed | b579f07 | 2019-01-03 15:45:53 -0500 | [diff] [blame] | 31 | static void setNamedTypeface(SkFont* font, const char name[]) { |
| 32 | font->setTypeface(SkTypeface::MakeFromName(name, SkFontStyle())); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | static uint16_t gBG[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF }; |
| 36 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 37 | class XfermodesBlurView : public Sample { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 38 | SkBitmap fBG; |
| 39 | SkBitmap fSrcB, fDstB; |
| 40 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 41 | 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] | 42 | SkPaint p; |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 43 | p.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, |
| 44 | SkBlurMask::ConvertRadiusToSigma(5))); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 45 | |
| 46 | SkScalar ww = SkIntToScalar(W); |
| 47 | SkScalar hh = SkIntToScalar(H); |
| 48 | |
| 49 | // draw a circle covering the upper |
| 50 | // left three quarters of the canvas |
| 51 | p.setColor(0xFFCC44FF); |
| 52 | SkRect r; |
| 53 | r.set(0, 0, ww*3/4, hh*3/4); |
| 54 | r.offset(x, y); |
| 55 | canvas->drawOval(r, p); |
| 56 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 57 | p.setBlendMode(mode); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 58 | |
| 59 | // draw a square overlapping the circle |
| 60 | // in the lower right of the canvas |
| 61 | p.setColor(0x00AA6633 | alpha << 24); |
| 62 | r.set(ww/3, hh/3, ww*19/20, hh*19/20); |
| 63 | r.offset(x, y); |
| 64 | canvas->drawRect(r, p); |
| 65 | } |
| 66 | |
| 67 | public: |
| 68 | const static int W = 64; |
| 69 | const static int H = 64; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 70 | XfermodesBlurView() { |
commit-bot@chromium.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 71 | fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType, kPremul_SkAlphaType), |
| 72 | gBG, 4); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 76 | virtual bool onQuery(Sample::Event* evt) { |
| 77 | if (Sample::TitleQ(*evt)) { |
| 78 | Sample::TitleR(evt, "XfermodesBlur"); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 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 | |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 87 | const SkBlendMode gModes[] = { |
| 88 | SkBlendMode::kClear, |
| 89 | SkBlendMode::kSrc, |
| 90 | SkBlendMode::kDst, |
| 91 | SkBlendMode::kSrcOver, |
| 92 | SkBlendMode::kDstOver, |
| 93 | SkBlendMode::kSrcIn, |
| 94 | SkBlendMode::kDstIn, |
| 95 | SkBlendMode::kSrcOut, |
| 96 | SkBlendMode::kDstOut, |
| 97 | SkBlendMode::kSrcATop, |
| 98 | SkBlendMode::kDstATop, |
| 99 | SkBlendMode::kXor, |
| 100 | SkBlendMode::kPlus, |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | const SkScalar w = SkIntToScalar(W); |
| 104 | const SkScalar h = SkIntToScalar(H); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 105 | SkMatrix m; |
| 106 | m.setScale(SkIntToScalar(6), SkIntToScalar(6)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 107 | auto s = SkShader::MakeBitmapShader(fBG, SkShader::kRepeat_TileMode, |
| 108 | SkShader::kRepeat_TileMode, &m); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 109 | |
Mike Reed | b579f07 | 2019-01-03 15:45:53 -0500 | [diff] [blame] | 110 | SkFont font; |
| 111 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 112 | setNamedTypeface(&font, "Menlo Regular"); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 113 | |
| 114 | const int W = 5; |
| 115 | |
| 116 | SkScalar x0 = 0; |
| 117 | for (int twice = 0; twice < 2; twice++) { |
| 118 | SkScalar x = x0, y = 0; |
| 119 | for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 120 | SkRect r; |
| 121 | r.set(x, y, x+w, y+h); |
| 122 | |
| 123 | SkPaint p; |
| 124 | p.setStyle(SkPaint::kFill_Style); |
| 125 | p.setShader(s); |
| 126 | canvas->drawRect(r, p); |
| 127 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 128 | canvas->saveLayer(&r, nullptr); |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 129 | 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] | 130 | canvas->restore(); |
| 131 | |
| 132 | r.inset(-SK_ScalarHalf, -SK_ScalarHalf); |
| 133 | p.setStyle(SkPaint::kStroke_Style); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 134 | p.setShader(nullptr); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 135 | canvas->drawRect(r, p); |
| 136 | |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 137 | const char* label = SkBlendMode_Name(gModes[i]); |
Mike Reed | b579f07 | 2019-01-03 15:45:53 -0500 | [diff] [blame] | 138 | SkTextUtils::DrawString(canvas, label, x + w/2, y - font.getSize()/2, font, SkPaint(), |
Mike Reed | 3a42ec0 | 2018-10-30 12:53:21 -0400 | [diff] [blame] | 139 | SkTextUtils::kCenter_Align); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 140 | x += w + SkIntToScalar(10); |
| 141 | if ((i % W) == W - 1) { |
| 142 | x = x0; |
| 143 | y += h + SkIntToScalar(30); |
| 144 | } |
| 145 | } |
| 146 | x0 += SkIntToScalar(400); |
| 147 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 151 | typedef Sample INHERITED; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | ////////////////////////////////////////////////////////////////////////////// |
| 155 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 156 | DEF_SAMPLE( return new XfermodesBlurView(); ) |