blob: 2169ffde538e7b1228c1716a19f059bf0c18db50 [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
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000110 const struct {
reed374772b2016-10-05 17:33:02 -0700111 SkBlendMode fMode;
112 const char* fLabel;
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000113 } gModes[] = {
reed374772b2016-10-05 17:33:02 -0700114 { SkBlendMode::kClear, "Clear" },
115 { SkBlendMode::kSrc, "Src" },
116 { SkBlendMode::kDst, "Dst" },
117 { SkBlendMode::kSrcOver, "SrcOver" },
118 { SkBlendMode::kDstOver, "DstOver" },
119 { SkBlendMode::kSrcIn, "SrcIn" },
120 { SkBlendMode::kDstIn, "DstIn" },
121 { SkBlendMode::kSrcOut, "SrcOut" },
122 { SkBlendMode::kDstOut, "DstOut" },
123 { SkBlendMode::kSrcATop, "SrcATop" },
124 { SkBlendMode::kDstATop, "DstATop" },
125 { SkBlendMode::kXor, "Xor" },
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000126
reed374772b2016-10-05 17:33:02 -0700127 { SkBlendMode::kPlus, "Plus" },
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000128 };
129
130 const SkScalar w = SkIntToScalar(W);
131 const SkScalar h = SkIntToScalar(H);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000132 SkMatrix m;
133 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
reed8a21c9f2016-03-08 18:50:00 -0800134 auto s = SkShader::MakeBitmapShader(fBG, SkShader::kRepeat_TileMode,
135 SkShader::kRepeat_TileMode, &m);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000136
137 SkPaint labelP;
138 labelP.setAntiAlias(true);
139 labelP.setLCDRenderText(true);
140 labelP.setTextAlign(SkPaint::kCenter_Align);
141 setNamedTypeface(&labelP, "Menlo Regular");
142
143 const int W = 5;
144
145 SkScalar x0 = 0;
146 for (int twice = 0; twice < 2; twice++) {
147 SkScalar x = x0, y = 0;
148 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000149 SkRect r;
150 r.set(x, y, x+w, y+h);
151
152 SkPaint p;
153 p.setStyle(SkPaint::kFill_Style);
154 p.setShader(s);
155 canvas->drawRect(r, p);
156
halcanary96fcdcc2015-08-27 07:41:13 -0700157 canvas->saveLayer(&r, nullptr);
reed374772b2016-10-05 17:33:02 -0700158 draw_mode(canvas, gModes[i].fMode, twice ? 0x88 : 0xFF, r.fLeft, r.fTop);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000159 canvas->restore();
160
161 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
162 p.setStyle(SkPaint::kStroke_Style);
halcanary96fcdcc2015-08-27 07:41:13 -0700163 p.setShader(nullptr);
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000164 canvas->drawRect(r, p);
165
166 canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel),
167 x + w/2, y - labelP.getTextSize()/2, labelP);
168 x += w + SkIntToScalar(10);
169 if ((i % W) == W - 1) {
170 x = x0;
171 y += h + SkIntToScalar(30);
172 }
173 }
174 x0 += SkIntToScalar(400);
175 }
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000176 }
177
178private:
179 typedef SampleView INHERITED;
180};
181
182//////////////////////////////////////////////////////////////////////////////
183
184static SkView* MyFactory() { return new XfermodesBlurView; }
185static SkViewRegister reg(MyFactory);