blob: ff198bcf8b2b0ba094e3e22b1f37443a83f66ae3 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.combf0001d2014-01-13 14:53:55 +00007
bsalomon@google.com271cffc2011-05-20 14:13:56 +00008#include "SampleCode.h"
9#include "SkView.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000010#include "SkBlurMask.h"
bsalomon@google.com271cffc2011-05-20 14:13:56 +000011#include "SkCanvas.h"
bsalomon@google.com271cffc2011-05-20 14:13:56 +000012#include "SkCornerPathEffect.h"
13#include "SkGradientShader.h"
14#include "SkGraphics.h"
bsalomon@google.com271cffc2011-05-20 14:13:56 +000015#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.com271cffc2011-05-20 14:13:56 +000024
25#include "SkStream.h"
bsalomon@google.com271cffc2011-05-20 14:13:56 +000026#include "SkColorPriv.h"
bsalomon@google.com271cffc2011-05-20 14:13:56 +000027#include "SkBlurMaskFilter.h"
28
29static void setNamedTypeface(SkPaint* paint, const char name[]) {
mbocee6a9912016-05-31 11:42:36 -070030 paint->setTypeface(SkTypeface::MakeFromName(name, SkFontStyle()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000031}
32
33static uint16_t gBG[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
34
35class XfermodesBlurView : public SampleView {
36 SkBitmap fBG;
37 SkBitmap fSrcB, fDstB;
38
reed374772b2016-10-05 17:33:02 -070039 void draw_mode(SkCanvas* canvas, SkBlendMode mode, int alpha, SkScalar x, SkScalar y) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +000040 SkPaint p;
reedefdfd512016-04-04 10:02:58 -070041 p.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
42 SkBlurMask::ConvertRadiusToSigma(5),
43 SkBlurMaskFilter::kNone_BlurFlag));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000044
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
reed374772b2016-10-05 17:33:02 -070056 p.setBlendMode(mode);
bsalomon@google.com271cffc2011-05-20 14:13:56 +000057
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
66public:
67 const static int W = 64;
68 const static int H = 64;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000069 XfermodesBlurView() {
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000070 fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType, kPremul_SkAlphaType),
71 gBG, 4);
bsalomon@google.com271cffc2011-05-20 14:13:56 +000072 }
73
74protected:
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.com271cffc2011-05-20 14:13:56 +000085 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
86
reed@google.com87fac4a2011-08-04 13:50:17 +000087 if (false) {
88 SkPaint paint;
89 paint.setAntiAlias(true);
90 paint.setTextSize(50);
mbocee6a9912016-05-31 11:42:36 -070091 paint.setTypeface(SkTypeface::MakeFromName("Arial Unicode MS", SkFontStyle()));
reed@google.com87fac4a2011-08-04 13:50:17 +000092 char buffer[10];
93 size_t len = SkUTF8_FromUnichar(0x8500, buffer);
94 canvas->drawText(buffer, len, 40, 40, paint);
95 return;
96 }
reed@google.com0e0c94c2011-08-04 18:11:01 +000097 if (false) {
reed@google.com87fac4a2011-08-04 13:50:17 +000098 SkPaint paint;
99 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000100
reed@google.com87fac4a2011-08-04 13:50:17 +0000101 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 Osmand1e67e72017-03-15 12:19:37 -0400110 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.com271cffc2011-05-20 14:13:56 +0000124 };
125
126 const SkScalar w = SkIntToScalar(W);
127 const SkScalar h = SkIntToScalar(H);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000128 SkMatrix m;
129 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
reed8a21c9f2016-03-08 18:50:00 -0800130 auto s = SkShader::MakeBitmapShader(fBG, SkShader::kRepeat_TileMode,
131 SkShader::kRepeat_TileMode, &m);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000132
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.com271cffc2011-05-20 14:13:56 +0000145 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
halcanary96fcdcc2015-08-27 07:41:13 -0700153 canvas->saveLayer(&r, nullptr);
Brian Osmand1e67e72017-03-15 12:19:37 -0400154 draw_mode(canvas, gModes[i], twice ? 0x88 : 0xFF, r.fLeft, r.fTop);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000155 canvas->restore();
156
157 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
158 p.setStyle(SkPaint::kStroke_Style);
halcanary96fcdcc2015-08-27 07:41:13 -0700159 p.setShader(nullptr);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000160 canvas->drawRect(r, p);
161
Brian Osmand1e67e72017-03-15 12:19:37 -0400162 const char* label = SkBlendMode_Name(gModes[i]);
Cary Clark2a475ea2017-04-28 15:35:12 -0400163 canvas->drawString(label,
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000164 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.com271cffc2011-05-20 14:13:56 +0000173 }
174
175private:
176 typedef SampleView INHERITED;
177};
178
179//////////////////////////////////////////////////////////////////////////////
180
181static SkView* MyFactory() { return new XfermodesBlurView; }
182static SkViewRegister reg(MyFactory);