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/SkColorPriv.h"
11 #include "include/core/SkShader.h"
12 #include "samplecode/Sample.h"
13
14 static const SkBlendMode gModes[] = {
15 SkBlendMode::kClear,
16 SkBlendMode::kSrc,
17 SkBlendMode::kDst,
18 SkBlendMode::kSrcOver,
19 SkBlendMode::kDstOver,
20 SkBlendMode::kSrcIn,
21 SkBlendMode::kDstIn,
22 SkBlendMode::kSrcOut,
23 SkBlendMode::kDstOut,
24 SkBlendMode::kSrcATop,
25 SkBlendMode::kDstATop,
26 SkBlendMode::kXor,
27 };
28
29 const int gWidth = 64;
30 const int gHeight = 64;
31 const SkScalar W = SkIntToScalar(gWidth);
32 const SkScalar H = SkIntToScalar(gHeight);
33
drawCell(SkCanvas * canvas,SkBlendMode mode,SkAlpha a0,SkAlpha a1)34 static SkScalar drawCell(SkCanvas* canvas, SkBlendMode mode, SkAlpha a0, SkAlpha a1) {
35 SkPaint paint;
36 paint.setAntiAlias(true);
37
38 SkRect r = SkRect::MakeWH(W, H);
39 r.inset(W/10, H/10);
40
41 paint.setColor(SK_ColorBLUE);
42 paint.setAlpha(a0);
43 canvas->drawOval(r, paint);
44
45 paint.setColor(SK_ColorRED);
46 paint.setAlpha(a1);
47 paint.setBlendMode(mode);
48
49 SkScalar offset = SK_Scalar1 / 3;
50 SkRect rect = SkRect::MakeXYWH(W / 4 + offset,
51 H / 4 + offset,
52 W / 2, H / 2);
53 canvas->drawRect(rect, paint);
54
55 return H;
56 }
57
make_bg_shader()58 static sk_sp<SkShader> make_bg_shader() {
59 SkBitmap bm;
60 bm.allocN32Pixels(2, 2);
61 *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF;
62 *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCC,
63 0xCC, 0xCC);
64
65 return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions(),
66 SkMatrix::Scale(6, 6));
67 }
68
69 class AARectsModesView : public Sample {
70 SkPaint fBGPaint;
71
onOnceBeforeDraw()72 void onOnceBeforeDraw() override {
73 fBGPaint.setShader(make_bg_shader());
74 }
75
name()76 SkString name() override { return SkString("AARectsModes"); }
77
onDrawContent(SkCanvas * canvas)78 void onDrawContent(SkCanvas* canvas) override {
79 const SkRect bounds = SkRect::MakeWH(W, H);
80 static const SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 };
81
82 canvas->translate(SkIntToScalar(4), SkIntToScalar(4));
83
84 for (int alpha = 0; alpha < 4; ++alpha) {
85 canvas->save();
86 canvas->save();
87 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) {
88 if (6 == i) {
89 canvas->restore();
90 canvas->translate(W * 5, 0);
91 canvas->save();
92 }
93
94 canvas->drawRect(bounds, fBGPaint);
95 canvas->saveLayer(&bounds, nullptr);
96 SkScalar dy = drawCell(canvas, gModes[i], gAlphaValue[alpha & 1],
97 gAlphaValue[alpha & 2]);
98 canvas->restore();
99
100 canvas->translate(0, dy * 5 / 4);
101 }
102 canvas->restore();
103 canvas->restore();
104 canvas->translate(W * 5 / 4, 0);
105 }
106 }
107 };
108 DEF_SAMPLE( return new AARectsModesView(); )
109