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 */
7
8 #include "include/core/SkBitmap.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColorFilter.h"
11 #include "include/core/SkColorPriv.h"
12 #include "include/core/SkFont.h"
13 #include "include/core/SkGraphics.h"
14 #include "include/core/SkPath.h"
15 #include "include/core/SkRegion.h"
16 #include "include/core/SkShader.h"
17 #include "include/core/SkTime.h"
18 #include "include/core/SkTypeface.h"
19 #include "include/effects/SkCornerPathEffect.h"
20 #include "include/effects/SkGradientShader.h"
21 #include "include/utils/SkRandom.h"
22 #include "include/utils/SkTextUtils.h"
23 #include "samplecode/Sample.h"
24 #include "src/core/SkBlurMask.h"
25 #include "src/utils/SkUTF.h"
26
27 #include "include/core/SkColorPriv.h"
28 #include "include/core/SkMaskFilter.h"
29 #include "include/core/SkStream.h"
30
setNamedTypeface(SkFont * font,const char name[])31 static void setNamedTypeface(SkFont* font, const char name[]) {
32 font->setTypeface(SkTypeface::MakeFromName(name, SkFontStyle()));
33 }
34
35 static uint16_t gBG[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
36
37 class XfermodesBlurView : public Sample {
38 SkBitmap fBG;
39 SkBitmap fSrcB, fDstB;
40
draw_mode(SkCanvas * canvas,SkBlendMode mode,int alpha,SkScalar x,SkScalar y)41 void draw_mode(SkCanvas* canvas, SkBlendMode mode, int alpha, SkScalar x, SkScalar y) {
42 SkPaint p;
43 p.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
44 SkBlurMask::ConvertRadiusToSigma(5)));
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.setLTRB(0, 0, ww*3/4, hh*3/4);
54 r.offset(x, y);
55 canvas->drawOval(r, p);
56
57 p.setBlendMode(mode);
58
59 // draw a square overlapping the circle
60 // in the lower right of the canvas
61 p.setColor(0x00AA6633 | alpha << 24);
62 r.setLTRB(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;
XfermodesBlurView()70 XfermodesBlurView() {
71 fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType, kPremul_SkAlphaType),
72 gBG, 4);
73 }
74
75 protected:
name()76 SkString name() override { return SkString("XfermodesBlur"); }
77
onDrawContent(SkCanvas * canvas)78 void onDrawContent(SkCanvas* canvas) override {
79 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
80
81 const SkBlendMode gModes[] = {
82 SkBlendMode::kClear,
83 SkBlendMode::kSrc,
84 SkBlendMode::kDst,
85 SkBlendMode::kSrcOver,
86 SkBlendMode::kDstOver,
87 SkBlendMode::kSrcIn,
88 SkBlendMode::kDstIn,
89 SkBlendMode::kSrcOut,
90 SkBlendMode::kDstOut,
91 SkBlendMode::kSrcATop,
92 SkBlendMode::kDstATop,
93 SkBlendMode::kXor,
94 SkBlendMode::kPlus,
95 };
96
97 const SkScalar w = SkIntToScalar(W);
98 const SkScalar h = SkIntToScalar(H);
99 SkMatrix m;
100 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
101 auto s = fBG.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions(), m);
102
103 SkFont font;
104 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
105 setNamedTypeface(&font, "Menlo Regular");
106
107 const int kWrap = 5;
108
109 SkScalar x0 = 0;
110 for (int twice = 0; twice < 2; twice++) {
111 SkScalar x = x0, y = 0;
112 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
113 SkRect r;
114 r.setLTRB(x, y, x+w, y+h);
115
116 SkPaint p;
117 p.setStyle(SkPaint::kFill_Style);
118 p.setShader(s);
119 canvas->drawRect(r, p);
120
121 canvas->saveLayer(&r, nullptr);
122 draw_mode(canvas, gModes[i], twice ? 0x88 : 0xFF, r.fLeft, r.fTop);
123 canvas->restore();
124
125 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
126 p.setStyle(SkPaint::kStroke_Style);
127 p.setShader(nullptr);
128 canvas->drawRect(r, p);
129
130 const char* label = SkBlendMode_Name(gModes[i]);
131 SkTextUtils::DrawString(canvas, label, x + w/2, y - font.getSize()/2, font, SkPaint(),
132 SkTextUtils::kCenter_Align);
133 x += w + SkIntToScalar(10);
134 if ((i % kWrap) == kWrap - 1) {
135 x = x0;
136 y += h + SkIntToScalar(30);
137 }
138 }
139 x0 += SkIntToScalar(400);
140 }
141 }
142
143 private:
144 using INHERITED = Sample;
145 };
146
147 //////////////////////////////////////////////////////////////////////////////
148
149 DEF_SAMPLE( return new XfermodesBlurView(); )
150