blob: 320cb0cc0b830be633be1a8cda903aed7692d2ce [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"
Mike Reed75ae4212018-01-23 11:24:08 -050010#include "SkBitmap.h"
reed@google.com1ec27a02011-09-08 13:36:30 +000011#include "SkCanvas.h"
reed@google.com1ec27a02011-09-08 13:36:30 +000012#include "SkColorPriv.h"
13#include "SkShader.h"
14
Brian Osmand1e67e72017-03-15 12:19:37 -040015static const SkBlendMode gModes[] = {
16 SkBlendMode::kClear,
17 SkBlendMode::kSrc,
18 SkBlendMode::kDst,
19 SkBlendMode::kSrcOver,
20 SkBlendMode::kDstOver,
21 SkBlendMode::kSrcIn,
22 SkBlendMode::kDstIn,
23 SkBlendMode::kSrcOut,
24 SkBlendMode::kDstOut,
25 SkBlendMode::kSrcATop,
26 SkBlendMode::kDstATop,
27 SkBlendMode::kXor,
reed@google.com1ec27a02011-09-08 13:36:30 +000028};
29
30const int gWidth = 64;
31const int gHeight = 64;
32const SkScalar W = SkIntToScalar(gWidth);
33const SkScalar H = SkIntToScalar(gHeight);
34
reed374772b2016-10-05 17:33:02 -070035static SkScalar drawCell(SkCanvas* canvas, SkBlendMode mode, SkAlpha a0, SkAlpha a1) {
reed@google.com1ec27a02011-09-08 13:36:30 +000036 SkPaint paint;
37 paint.setAntiAlias(true);
38
39 SkRect r = SkRect::MakeWH(W, H);
40 r.inset(W/10, H/10);
41
42 paint.setColor(SK_ColorBLUE);
43 paint.setAlpha(a0);
44 canvas->drawOval(r, paint);
45
46 paint.setColor(SK_ColorRED);
47 paint.setAlpha(a1);
reed374772b2016-10-05 17:33:02 -070048 paint.setBlendMode(mode);
reed@google.com1ec27a02011-09-08 13:36:30 +000049 for (int angle = 0; angle < 24; ++angle) {
50 SkScalar x = SkScalarCos(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gWidth;
51 SkScalar y = SkScalarSin(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gHeight;
52 paint.setStrokeWidth(SK_Scalar1 * angle * 2 / 24);
53 canvas->drawLine(W/2, H/2, W/2 + x, H/2 + y, paint);
54 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000055
reed@google.com1ec27a02011-09-08 13:36:30 +000056 return H;
57}
58
reed8a21c9f2016-03-08 18:50:00 -080059static sk_sp<SkShader> make_bg_shader() {
reed@google.com1ec27a02011-09-08 13:36:30 +000060 SkBitmap bm;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000061 bm.allocN32Pixels(2, 2);
reed@google.com1ec27a02011-09-08 13:36:30 +000062 *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF;
63 *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCC, 0xCC, 0xCC);
64
reed@google.com1ec27a02011-09-08 13:36:30 +000065 SkMatrix m;
66 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000067
reed8a21c9f2016-03-08 18:50:00 -080068 return SkShader::MakeBitmapShader(bm, SkShader::kRepeat_TileMode,
69 SkShader::kRepeat_TileMode, &m);
reed@google.com1ec27a02011-09-08 13:36:30 +000070}
71
72class HairModesView : public SampleView {
73 SkPaint fBGPaint;
74public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000075 HairModesView() {
reed8a21c9f2016-03-08 18:50:00 -080076 fBGPaint.setShader(make_bg_shader());
reed@google.com1ec27a02011-09-08 13:36:30 +000077 }
78
79protected:
80 // overrides from SkEventSink
81 virtual bool onQuery(SkEvent* evt) {
82 if (SampleCode::TitleQ(*evt)) {
83 SampleCode::TitleR(evt, "HairlineModes");
84 return true;
85 }
86 return this->INHERITED::onQuery(evt);
87 }
88
89 virtual void onDrawContent(SkCanvas* canvas) {
90 const SkRect bounds = SkRect::MakeWH(W, H);
91 static const SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 };
92
93 canvas->translate(SkIntToScalar(4), SkIntToScalar(4));
94
95 for (int alpha = 0; alpha < 4; ++alpha) {
96 canvas->save();
97 canvas->save();
98 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) {
99 if (6 == i) {
100 canvas->restore();
101 canvas->translate(W * 5, 0);
102 canvas->save();
103 }
reed@google.com1ec27a02011-09-08 13:36:30 +0000104 canvas->drawRect(bounds, fBGPaint);
halcanary96fcdcc2015-08-27 07:41:13 -0700105 canvas->saveLayer(&bounds, nullptr);
Brian Osmand1e67e72017-03-15 12:19:37 -0400106 SkScalar dy = drawCell(canvas, gModes[i],
reed@google.com1ec27a02011-09-08 13:36:30 +0000107 gAlphaValue[alpha & 1],
108 gAlphaValue[alpha & 2]);
109 canvas->restore();
110
111 canvas->translate(0, dy * 5 / 4);
reed@google.com1ec27a02011-09-08 13:36:30 +0000112 }
113 canvas->restore();
114 canvas->restore();
115 canvas->translate(W * 5 / 4, 0);
116 }
117 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000118
reed@google.com1ec27a02011-09-08 13:36:30 +0000119private:
120 typedef SampleView INHERITED;
121};
122
123///////////////////////////////////////////////////////////////////////////////
124
125static SkView* MyFactory() { return new HairModesView; }
126static SkViewRegister reg(MyFactory);