blob: 4aa8e0208fea318fc9aa1e2ce4f3d07fee234a61 [file] [log] [blame]
reed@google.com1ec27a02011-09-08 13:36:30 +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"
11#include "SkDevice.h"
12#include "SkColorPriv.h"
13#include "SkShader.h"
14
reed@google.com1ec27a02011-09-08 13:36:30 +000015static const struct {
16 SkXfermode::Mode fMode;
17 const char* fLabel;
18} gModes[] = {
19 { SkXfermode::kClear_Mode, "Clear" },
20 { SkXfermode::kSrc_Mode, "Src" },
21 { SkXfermode::kDst_Mode, "Dst" },
22 { SkXfermode::kSrcOver_Mode, "SrcOver" },
23 { SkXfermode::kDstOver_Mode, "DstOver" },
24 { SkXfermode::kSrcIn_Mode, "SrcIn" },
25 { SkXfermode::kDstIn_Mode, "DstIn" },
26 { SkXfermode::kSrcOut_Mode, "SrcOut" },
27 { SkXfermode::kDstOut_Mode, "DstOut" },
28 { SkXfermode::kSrcATop_Mode, "SrcATop" },
29 { SkXfermode::kDstATop_Mode, "DstATop" },
30 { SkXfermode::kXor_Mode, "Xor" },
31};
32
33const int gWidth = 64;
34const int gHeight = 64;
35const SkScalar W = SkIntToScalar(gWidth);
36const SkScalar H = SkIntToScalar(gHeight);
37
38static SkScalar drawCell(SkCanvas* canvas, SkXfermode* mode, SkAlpha a0, SkAlpha a1) {
39
40 SkPaint paint;
41 paint.setAntiAlias(true);
42
43 SkRect r = SkRect::MakeWH(W, H);
44 r.inset(W/10, H/10);
45
46 paint.setColor(SK_ColorBLUE);
47 paint.setAlpha(a0);
48 canvas->drawOval(r, paint);
49
50 paint.setColor(SK_ColorRED);
51 paint.setAlpha(a1);
52 paint.setXfermode(mode);
53 for (int angle = 0; angle < 24; ++angle) {
54 SkScalar x = SkScalarCos(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gWidth;
55 SkScalar y = SkScalarSin(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gHeight;
56 paint.setStrokeWidth(SK_Scalar1 * angle * 2 / 24);
57 canvas->drawLine(W/2, H/2, W/2 + x, H/2 + y, paint);
58 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000059
reed@google.com1ec27a02011-09-08 13:36:30 +000060 return H;
61}
62
63static SkShader* make_bg_shader() {
64 SkBitmap bm;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000065 bm.allocN32Pixels(2, 2);
reed@google.com1ec27a02011-09-08 13:36:30 +000066 *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF;
67 *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCC, 0xCC, 0xCC);
68
69 SkShader* s = SkShader::CreateBitmapShader(bm,
70 SkShader::kRepeat_TileMode,
71 SkShader::kRepeat_TileMode);
rmistry@google.comae933ce2012-08-23 18:19:56 +000072
reed@google.com1ec27a02011-09-08 13:36:30 +000073 SkMatrix m;
74 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
75 s->setLocalMatrix(m);
76 return s;
77}
78
79class HairModesView : public SampleView {
80 SkPaint fBGPaint;
81public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000082 HairModesView() {
reed@google.com1ec27a02011-09-08 13:36:30 +000083 fBGPaint.setShader(make_bg_shader())->unref();
84 }
85
86protected:
87 // overrides from SkEventSink
88 virtual bool onQuery(SkEvent* evt) {
89 if (SampleCode::TitleQ(*evt)) {
90 SampleCode::TitleR(evt, "HairlineModes");
91 return true;
92 }
93 return this->INHERITED::onQuery(evt);
94 }
95
96 virtual void onDrawContent(SkCanvas* canvas) {
97 const SkRect bounds = SkRect::MakeWH(W, H);
98 static const SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 };
99
100 canvas->translate(SkIntToScalar(4), SkIntToScalar(4));
101
102 for (int alpha = 0; alpha < 4; ++alpha) {
103 canvas->save();
104 canvas->save();
105 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) {
106 if (6 == i) {
107 canvas->restore();
108 canvas->translate(W * 5, 0);
109 canvas->save();
110 }
111 SkXfermode* mode = SkXfermode::Create(gModes[i].fMode);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000112
reed@google.com1ec27a02011-09-08 13:36:30 +0000113 canvas->drawRect(bounds, fBGPaint);
114 canvas->saveLayer(&bounds, NULL);
115 SkScalar dy = drawCell(canvas, mode,
116 gAlphaValue[alpha & 1],
117 gAlphaValue[alpha & 2]);
118 canvas->restore();
119
120 canvas->translate(0, dy * 5 / 4);
121 SkSafeUnref(mode);
122 }
123 canvas->restore();
124 canvas->restore();
125 canvas->translate(W * 5 / 4, 0);
126 }
127 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000128
reed@google.com1ec27a02011-09-08 13:36:30 +0000129private:
130 typedef SampleView INHERITED;
131};
132
133///////////////////////////////////////////////////////////////////////////////
134
135static SkView* MyFactory() { return new HairModesView; }
136static SkViewRegister reg(MyFactory);