blob: ef25114cf01d2cbabd39932fad2c88e86293060e [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"
24#include "SkXfermode.h"
25
26#include "SkStream.h"
bsalomon@google.com271cffc2011-05-20 14:13:56 +000027#include "SkColorPriv.h"
bsalomon@google.com271cffc2011-05-20 14:13:56 +000028#include "SkBlurMaskFilter.h"
29
30static void setNamedTypeface(SkPaint* paint, const char name[]) {
mbocee6a9912016-05-31 11:42:36 -070031 paint->setTypeface(SkTypeface::MakeFromName(name, SkFontStyle()));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000032}
33
34static uint16_t gBG[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
35
36class XfermodesBlurView : public SampleView {
37 SkBitmap fBG;
38 SkBitmap fSrcB, fDstB;
39
reedcfb6bdf2016-03-29 11:32:50 -070040 void draw_mode(SkCanvas* canvas, sk_sp<SkXfermode> mode, int alpha,
bsalomon@google.com271cffc2011-05-20 14:13:56 +000041 SkScalar x, SkScalar y) {
42 SkPaint p;
reedefdfd512016-04-04 10:02:58 -070043 p.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
44 SkBlurMask::ConvertRadiusToSigma(5),
45 SkBlurMaskFilter::kNone_BlurFlag));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000046
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
reedcfb6bdf2016-03-29 11:32:50 -070058 p.setXfermode(std::move(mode));
bsalomon@google.com271cffc2011-05-20 14:13:56 +000059
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
68public:
69 const static int W = 64;
70 const static int H = 64;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000071 XfermodesBlurView() {
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000072 fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType, kPremul_SkAlphaType),
73 gBG, 4);
bsalomon@google.com271cffc2011-05-20 14:13:56 +000074 }
75
76protected:
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.com271cffc2011-05-20 14:13:56 +000087 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
88
reed@google.com87fac4a2011-08-04 13:50:17 +000089 if (false) {
90 SkPaint paint;
91 paint.setAntiAlias(true);
92 paint.setTextSize(50);
mbocee6a9912016-05-31 11:42:36 -070093 paint.setTypeface(SkTypeface::MakeFromName("Arial Unicode MS", SkFontStyle()));
reed@google.com87fac4a2011-08-04 13:50:17 +000094 char buffer[10];
95 size_t len = SkUTF8_FromUnichar(0x8500, buffer);
96 canvas->drawText(buffer, len, 40, 40, paint);
97 return;
98 }
reed@google.com0e0c94c2011-08-04 18:11:01 +000099 if (false) {
reed@google.com87fac4a2011-08-04 13:50:17 +0000100 SkPaint paint;
101 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000102
reed@google.com87fac4a2011-08-04 13:50:17 +0000103 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.com271cffc2011-05-20 14:13:56 +0000112 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.com8d3cd7a2013-01-30 21:36:11 +0000130 /*{ SkXfermode::kModulate_Mode, "Modulate" },
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000131 { 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.com271cffc2011-05-20 14:13:56 +0000145 SkMatrix m;
146 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
reed8a21c9f2016-03-08 18:50:00 -0800147 auto s = SkShader::MakeBitmapShader(fBG, SkShader::kRepeat_TileMode,
148 SkShader::kRepeat_TileMode, &m);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000149
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.com271cffc2011-05-20 14:13:56 +0000162 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
halcanary96fcdcc2015-08-27 07:41:13 -0700170 canvas->saveLayer(&r, nullptr);
reedcfb6bdf2016-03-29 11:32:50 -0700171 draw_mode(canvas, SkXfermode::Make(gModes[i].fMode),
172 twice ? 0x88 : 0xFF, r.fLeft, r.fTop);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000173 canvas->restore();
174
175 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
176 p.setStyle(SkPaint::kStroke_Style);
halcanary96fcdcc2015-08-27 07:41:13 -0700177 p.setShader(nullptr);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000178 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.com271cffc2011-05-20 14:13:56 +0000190 }
191
192private:
193 typedef SampleView INHERITED;
194};
195
196//////////////////////////////////////////////////////////////////////////////
197
198static SkView* MyFactory() { return new XfermodesBlurView; }
199static SkViewRegister reg(MyFactory);