blob: a73d9f2e2005e2861e15a004ab4c62f2cfd40e5c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
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"
12#include "Sk64.h"
13#include "SkCornerPathEffect.h"
14#include "SkGradientShader.h"
15#include "SkGraphics.h"
16#include "SkImageDecoder.h"
17#include "SkKernel33MaskFilter.h"
18#include "SkPath.h"
19#include "SkRandom.h"
20#include "SkRegion.h"
21#include "SkShader.h"
22#include "SkUtils.h"
23#include "SkColorPriv.h"
24#include "SkColorFilter.h"
25#include "SkTime.h"
26#include "SkTypeface.h"
27#include "SkXfermode.h"
28
29#include "SkStream.h"
30#include "SkXMLParser.h"
31#include "SkColorPriv.h"
32#include "SkImageDecoder.h"
33#include "SkBlurMaskFilter.h"
34
35static void setNamedTypeface(SkPaint* paint, const char name[]) {
36 SkTypeface* face = SkTypeface::CreateFromName(name, SkTypeface::kNormal);
37 paint->setTypeface(face);
38 SkSafeUnref(face);
39}
40
41static uint16_t gBG[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
42
43class XfermodesBlurView : public SampleView {
44 SkBitmap fBG;
45 SkBitmap fSrcB, fDstB;
46
47 void draw_mode(SkCanvas* canvas, SkXfermode* mode, int alpha,
48 SkScalar x, SkScalar y) {
49 SkPaint p;
robertphillips@google.comb7061172013-09-06 14:16:12 +000050 SkMaskFilter* mf = SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle,
51 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
52 SkBlurMaskFilter::kNone_BlurFlag);
bsalomon@google.com271cffc2011-05-20 14:13:56 +000053 p.setMaskFilter(mf)->unref();
54
55 SkScalar ww = SkIntToScalar(W);
56 SkScalar hh = SkIntToScalar(H);
57
58 // draw a circle covering the upper
59 // left three quarters of the canvas
60 p.setColor(0xFFCC44FF);
61 SkRect r;
62 r.set(0, 0, ww*3/4, hh*3/4);
63 r.offset(x, y);
64 canvas->drawOval(r, p);
65
66 p.setXfermode(mode);
67
68 // draw a square overlapping the circle
69 // in the lower right of the canvas
70 p.setColor(0x00AA6633 | alpha << 24);
71 r.set(ww/3, hh/3, ww*19/20, hh*19/20);
72 r.offset(x, y);
73 canvas->drawRect(r, p);
74 }
75
76public:
77 const static int W = 64;
78 const static int H = 64;
bsalomon@google.com22c5dea2011-07-07 14:38:03 +000079 XfermodesBlurView() {
reed@google.com383a6972013-10-21 14:00:07 +000080 fBG.setConfig(SkBitmap::kARGB_4444_Config, 2, 2, 4, kOpaque_SkAlphaType);
bsalomon@google.com271cffc2011-05-20 14:13:56 +000081 fBG.setPixels(gBG);
bsalomon@google.com271cffc2011-05-20 14:13:56 +000082 }
83
84protected:
85 // overrides from SkEventSink
86 virtual bool onQuery(SkEvent* evt) {
87 if (SampleCode::TitleQ(*evt)) {
88 SampleCode::TitleR(evt, "XfermodesBlur");
89 return true;
90 }
91 return this->INHERITED::onQuery(evt);
92 }
93
94 virtual void onDrawContent(SkCanvas* canvas) {
bsalomon@google.com271cffc2011-05-20 14:13:56 +000095 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
96
reed@google.com87fac4a2011-08-04 13:50:17 +000097 if (false) {
98 SkPaint paint;
99 paint.setAntiAlias(true);
100 paint.setTextSize(50);
101 paint.setTypeface(SkTypeface::CreateFromName("Arial Unicode MS", SkTypeface::kNormal));
102 SkSafeUnref(paint.getTypeface());
103 char buffer[10];
104 size_t len = SkUTF8_FromUnichar(0x8500, buffer);
105 canvas->drawText(buffer, len, 40, 40, paint);
106 return;
107 }
reed@google.com0e0c94c2011-08-04 18:11:01 +0000108 if (false) {
reed@google.com87fac4a2011-08-04 13:50:17 +0000109 SkPaint paint;
110 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000111
reed@google.com87fac4a2011-08-04 13:50:17 +0000112 SkRect r0 = { 0, 0, 10.5f, 20 };
113 SkRect r1 = { 10.5f, 10, 20, 30 };
114 paint.setColor(SK_ColorRED);
115 canvas->drawRect(r0, paint);
116 paint.setColor(SK_ColorBLUE);
117 canvas->drawRect(r1, paint);
118 return;
119 }
120
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000121 const struct {
122 SkXfermode::Mode fMode;
123 const char* fLabel;
124 } gModes[] = {
125 { SkXfermode::kClear_Mode, "Clear" },
126 { SkXfermode::kSrc_Mode, "Src" },
127 { SkXfermode::kDst_Mode, "Dst" },
128 { SkXfermode::kSrcOver_Mode, "SrcOver" },
129 { SkXfermode::kDstOver_Mode, "DstOver" },
130 { SkXfermode::kSrcIn_Mode, "SrcIn" },
131 { SkXfermode::kDstIn_Mode, "DstIn" },
132 { SkXfermode::kSrcOut_Mode, "SrcOut" },
133 { SkXfermode::kDstOut_Mode, "DstOut" },
134 { SkXfermode::kSrcATop_Mode, "SrcATop" },
135 { SkXfermode::kDstATop_Mode, "DstATop" },
136 { SkXfermode::kXor_Mode, "Xor" },
137
138 { SkXfermode::kPlus_Mode, "Plus" },
reed@google.com8d3cd7a2013-01-30 21:36:11 +0000139 /*{ SkXfermode::kModulate_Mode, "Modulate" },
bsalomon@google.com271cffc2011-05-20 14:13:56 +0000140 { SkXfermode::kScreen_Mode, "Screen" },
141 { SkXfermode::kOverlay_Mode, "Overlay" },
142 { SkXfermode::kDarken_Mode, "Darken" },
143 { SkXfermode::kLighten_Mode, "Lighten" },
144 { SkXfermode::kColorDodge_Mode, "ColorDodge" },
145 { SkXfermode::kColorBurn_Mode, "ColorBurn" },
146 { SkXfermode::kHardLight_Mode, "HardLight" },
147 { SkXfermode::kSoftLight_Mode, "SoftLight" },
148 { SkXfermode::kDifference_Mode, "Difference" },
149 { SkXfermode::kExclusion_Mode, "Exclusion" },*/
150 };
151
152 const SkScalar w = SkIntToScalar(W);
153 const SkScalar h = SkIntToScalar(H);
154 SkShader* s = SkShader::CreateBitmapShader(fBG,
155 SkShader::kRepeat_TileMode,
156 SkShader::kRepeat_TileMode);
157 SkMatrix m;
158 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
159 s->setLocalMatrix(m);
160
161 SkPaint labelP;
162 labelP.setAntiAlias(true);
163 labelP.setLCDRenderText(true);
164 labelP.setTextAlign(SkPaint::kCenter_Align);
165 setNamedTypeface(&labelP, "Menlo Regular");
166
167 const int W = 5;
168
169 SkScalar x0 = 0;
170 for (int twice = 0; twice < 2; twice++) {
171 SkScalar x = x0, y = 0;
172 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
173 SkXfermode* mode = SkXfermode::Create(gModes[i].fMode);
174 SkAutoUnref aur(mode);
175 SkRect r;
176 r.set(x, y, x+w, y+h);
177
178 SkPaint p;
179 p.setStyle(SkPaint::kFill_Style);
180 p.setShader(s);
181 canvas->drawRect(r, p);
182
183 canvas->saveLayer(&r, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag);
184 draw_mode(canvas, mode, twice ? 0x88 : 0xFF, r.fLeft, r.fTop);
185 canvas->restore();
186
187 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
188 p.setStyle(SkPaint::kStroke_Style);
189 p.setShader(NULL);
190 canvas->drawRect(r, p);
191
192 canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel),
193 x + w/2, y - labelP.getTextSize()/2, labelP);
194 x += w + SkIntToScalar(10);
195 if ((i % W) == W - 1) {
196 x = x0;
197 y += h + SkIntToScalar(30);
198 }
199 }
200 x0 += SkIntToScalar(400);
201 }
202 s->unref();
203 }
204
205private:
206 typedef SampleView INHERITED;
207};
208
209//////////////////////////////////////////////////////////////////////////////
210
211static SkView* MyFactory() { return new XfermodesBlurView; }
212static SkViewRegister reg(MyFactory);