blob: 65b1da140536ff6d6600da6014880f3ac3979288 [file] [log] [blame]
bsalomon@google.coma3108262011-10-10 14:08:47 +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 */
7
8#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
bsalomon@google.coma3108262011-10-10 14:08:47 +000011#include "SkColorPriv.h"
12#include "SkShader.h"
13
bsalomon@google.coma3108262011-10-10 14:08:47 +000014static const struct {
15 SkXfermode::Mode fMode;
16 const char* fLabel;
17} gModes[] = {
18 { SkXfermode::kClear_Mode, "Clear" },
19 { SkXfermode::kSrc_Mode, "Src" },
20 { SkXfermode::kDst_Mode, "Dst" },
21 { SkXfermode::kSrcOver_Mode, "SrcOver" },
22 { SkXfermode::kDstOver_Mode, "DstOver" },
23 { SkXfermode::kSrcIn_Mode, "SrcIn" },
24 { SkXfermode::kDstIn_Mode, "DstIn" },
25 { SkXfermode::kSrcOut_Mode, "SrcOut" },
26 { SkXfermode::kDstOut_Mode, "DstOut" },
27 { SkXfermode::kSrcATop_Mode, "SrcATop" },
28 { SkXfermode::kDstATop_Mode, "DstATop" },
29 { SkXfermode::kXor_Mode, "Xor" },
30};
31
32const int gWidth = 64;
33const int gHeight = 64;
34const SkScalar W = SkIntToScalar(gWidth);
35const SkScalar H = SkIntToScalar(gHeight);
36
reedcfb6bdf2016-03-29 11:32:50 -070037static SkScalar drawCell(SkCanvas* canvas, const sk_sp<SkXfermode>& mode, SkAlpha a0, SkAlpha a1) {
bsalomon@google.coma3108262011-10-10 14:08:47 +000038 SkPaint paint;
39 paint.setAntiAlias(true);
40
41 SkRect r = SkRect::MakeWH(W, H);
42 r.inset(W/10, H/10);
43
44 paint.setColor(SK_ColorBLUE);
45 paint.setAlpha(a0);
46 canvas->drawOval(r, paint);
47
48 paint.setColor(SK_ColorRED);
49 paint.setAlpha(a1);
50 paint.setXfermode(mode);
51
52 SkScalar offset = SK_Scalar1 / 3;
53 SkRect rect = SkRect::MakeXYWH(W / 4 + offset,
54 H / 4 + offset,
55 W / 2, H / 2);
56 canvas->drawRect(rect, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000057
bsalomon@google.coma3108262011-10-10 14:08:47 +000058 return H;
59}
60
reed8a21c9f2016-03-08 18:50:00 -080061static sk_sp<SkShader> make_bg_shader() {
bsalomon@google.coma3108262011-10-10 14:08:47 +000062 SkBitmap bm;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000063 bm.allocN32Pixels(2, 2);
bsalomon@google.coma3108262011-10-10 14:08:47 +000064 *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF;
65 *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCC,
66 0xCC, 0xCC);
67
bsalomon@google.coma3108262011-10-10 14:08:47 +000068 SkMatrix m;
69 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000070
reed8a21c9f2016-03-08 18:50:00 -080071 return SkShader::MakeBitmapShader(bm,
72 SkShader::kRepeat_TileMode,
73 SkShader::kRepeat_TileMode,
74 &m);
bsalomon@google.coma3108262011-10-10 14:08:47 +000075}
76
77class AARectsModesView : public SampleView {
78 SkPaint fBGPaint;
79public:
80 AARectsModesView () {
reed8a21c9f2016-03-08 18:50:00 -080081 fBGPaint.setShader(make_bg_shader());
bsalomon@google.coma3108262011-10-10 14:08:47 +000082 }
83
84protected:
85 // overrides from SkEventSink
86 virtual bool onQuery(SkEvent* evt) {
87 if (SampleCode::TitleQ(*evt)) {
88 SampleCode::TitleR(evt, "AARectsModes");
89 return true;
90 }
91 return this->INHERITED::onQuery(evt);
92 }
93
94 virtual void onDrawContent(SkCanvas* canvas) {
95 const SkRect bounds = SkRect::MakeWH(W, H);
96 static const SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 };
97
98 canvas->translate(SkIntToScalar(4), SkIntToScalar(4));
99
100 for (int alpha = 0; alpha < 4; ++alpha) {
101 canvas->save();
102 canvas->save();
103 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) {
104 if (6 == i) {
105 canvas->restore();
106 canvas->translate(W * 5, 0);
107 canvas->save();
108 }
reedcfb6bdf2016-03-29 11:32:50 -0700109 sk_sp<SkXfermode> mode = SkXfermode::Make(gModes[i].fMode);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000110
bsalomon@google.coma3108262011-10-10 14:08:47 +0000111 canvas->drawRect(bounds, fBGPaint);
halcanary96fcdcc2015-08-27 07:41:13 -0700112 canvas->saveLayer(&bounds, nullptr);
reedcfb6bdf2016-03-29 11:32:50 -0700113 SkScalar dy = drawCell(canvas, mode, gAlphaValue[alpha & 1],
bsalomon@google.coma3108262011-10-10 14:08:47 +0000114 gAlphaValue[alpha & 2]);
115 canvas->restore();
116
117 canvas->translate(0, dy * 5 / 4);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000118 }
119 canvas->restore();
120 canvas->restore();
121 canvas->translate(W * 5 / 4, 0);
122 }
123 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
bsalomon@google.coma3108262011-10-10 14:08:47 +0000125private:
126 typedef SampleView INHERITED;
127};
128
129///////////////////////////////////////////////////////////////////////////////
130
131static SkView* MyFactory() { return new AARectsModesView; }
132static SkViewRegister reg(MyFactory);