blob: ef379a4ebed69d941dc5db0697285d0c7cc43083 [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 "gm.h"
9#include "SkCanvas.h"
10#include "SkColorPriv.h"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.h"
bsalomon@google.coma3108262011-10-10 14:08:47 +000012#include "SkShader.h"
13
reed@google.com76166152011-11-14 13:14:58 +000014static void test4(SkCanvas* canvas) {
15 SkPaint paint;
16 paint.setAntiAlias(true);
17 SkPoint pts[] = {
18 {10, 160}, {610, 160},
19 {610, 160}, {10, 160},
rmistry@google.comd6176b02012-08-23 18:14:13 +000020
reed@google.com76166152011-11-14 13:14:58 +000021 {610, 160}, {610, 160},
22 {610, 199}, {610, 199},
rmistry@google.comd6176b02012-08-23 18:14:13 +000023
reed@google.com76166152011-11-14 13:14:58 +000024 {10, 198}, {610, 198},
25 {610, 199}, {10, 199},
rmistry@google.comd6176b02012-08-23 18:14:13 +000026
reed@google.com76166152011-11-14 13:14:58 +000027 {10, 160}, {10, 160},
28 {10, 199}, {10, 199}
29 };
30 char verbs[] = {
rmistry@google.comd6176b02012-08-23 18:14:13 +000031 0, 1, 1, 1, 4,
reed@google.com76166152011-11-14 13:14:58 +000032 0, 1, 1, 1, 4,
33 0, 1, 1, 1, 4,
34 0, 1, 1, 1, 4
35 };
36 SkPath path;
37 SkPoint* ptPtr = pts;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000038 for (size_t i = 0; i < sizeof(verbs); ++i) {
reed@google.com76166152011-11-14 13:14:58 +000039 switch ((SkPath::Verb) verbs[i]) {
40 case SkPath::kMove_Verb:
41 path.moveTo(ptPtr->fX, ptPtr->fY);
42 ++ptPtr;
43 break;
44 case SkPath::kLine_Verb:
45 path.lineTo(ptPtr->fX, ptPtr->fY);
46 ++ptPtr;
47 break;
48 case SkPath::kClose_Verb:
49 path.close();
50 break;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000051 default:
52 SkASSERT(false);
53 break;
reed@google.com76166152011-11-14 13:14:58 +000054 }
55 }
56 SkRect clip = {0, 130, 772, 531};
57 canvas->clipRect(clip);
58 canvas->drawPath(path, paint);
59}
60
Brian Osmand1e67e72017-03-15 12:19:37 -040061constexpr SkBlendMode gModes[] = {
62 SkBlendMode::kClear,
63 SkBlendMode::kSrc,
64 SkBlendMode::kDst,
65 SkBlendMode::kSrcOver,
66 SkBlendMode::kDstOver,
67 SkBlendMode::kSrcIn,
68 SkBlendMode::kDstIn,
69 SkBlendMode::kSrcOut,
70 SkBlendMode::kDstOut,
71 SkBlendMode::kSrcATop,
72 SkBlendMode::kDstATop,
73 SkBlendMode::kXor,
bsalomon@google.coma3108262011-10-10 14:08:47 +000074};
75
76const int gWidth = 64;
77const int gHeight = 64;
78const SkScalar W = SkIntToScalar(gWidth);
79const SkScalar H = SkIntToScalar(gHeight);
80
reed374772b2016-10-05 17:33:02 -070081static SkScalar drawCell(SkCanvas* canvas, SkBlendMode mode, SkAlpha a0, SkAlpha a1) {
bsalomon@google.coma3108262011-10-10 14:08:47 +000082
83 SkPaint paint;
84 paint.setAntiAlias(true);
85
86 SkRect r = SkRect::MakeWH(W, H);
87 r.inset(W/10, H/10);
88
89 paint.setColor(SK_ColorBLUE);
90 paint.setAlpha(a0);
91 canvas->drawOval(r, paint);
92
93 paint.setColor(SK_ColorRED);
94 paint.setAlpha(a1);
reed374772b2016-10-05 17:33:02 -070095 paint.setBlendMode(mode);
bsalomon@google.coma3108262011-10-10 14:08:47 +000096
97 SkScalar offset = SK_Scalar1 / 3;
98 SkRect rect = SkRect::MakeXYWH(W / 4 + offset,
99 H / 4 + offset,
100 W / 2, H / 2);
101 canvas->drawRect(rect, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000102
bsalomon@google.coma3108262011-10-10 14:08:47 +0000103 return H;
104}
105
reed2ad1aa62016-03-09 09:50:50 -0800106static sk_sp<SkShader> make_bg_shader() {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000107 SkBitmap bm;
reed@google.comeb9a46c2014-01-25 16:46:20 +0000108 bm.allocN32Pixels(2, 2);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000109 *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF;
caryclark12596012015-07-29 05:27:47 -0700110 *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCE,
111 0xCF, 0xCE);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000112
robertphillips1d24b8d2015-03-26 19:57:08 -0700113 const SkMatrix m = SkMatrix::MakeScale(SkIntToScalar(6), SkIntToScalar(6));
reed2ad1aa62016-03-09 09:50:50 -0800114 return SkShader::MakeBitmapShader(bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode,
115 &m);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000116}
117
118namespace skiagm {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000119
bsalomon@google.coma3108262011-10-10 14:08:47 +0000120 class AARectModesGM : public GM {
121 SkPaint fBGPaint;
122 public:
123 AARectModesGM () {
reed2ad1aa62016-03-09 09:50:50 -0800124 fBGPaint.setShader(make_bg_shader());
bsalomon@google.coma3108262011-10-10 14:08:47 +0000125 }
126
127 protected:
128
mtklein36352bf2015-03-25 18:17:31 -0700129 SkString onShortName() override {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000130 return SkString("aarectmodes");
131 }
132
mtklein36352bf2015-03-25 18:17:31 -0700133 SkISize onISize() override { return SkISize::Make(640, 480); }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000134
mtklein36352bf2015-03-25 18:17:31 -0700135 void onDraw(SkCanvas* canvas) override {
caryclark@google.com13130862012-06-06 12:10:45 +0000136 if (false) { // avoid bit rot, suppress warning
137 test4(canvas);
138 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000139 const SkRect bounds = SkRect::MakeWH(W, H);
mtkleindbfd7ab2016-09-01 11:24:54 -0700140 constexpr SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 };
bsalomon@google.coma3108262011-10-10 14:08:47 +0000141
142 canvas->translate(SkIntToScalar(4), SkIntToScalar(4));
143
144 for (int alpha = 0; alpha < 4; ++alpha) {
145 canvas->save();
146 canvas->save();
147 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) {
148 if (6 == i) {
149 canvas->restore();
150 canvas->translate(W * 5, 0);
151 canvas->save();
152 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000153 canvas->drawRect(bounds, fBGPaint);
halcanary96fcdcc2015-08-27 07:41:13 -0700154 canvas->saveLayer(&bounds, nullptr);
Brian Osmand1e67e72017-03-15 12:19:37 -0400155 SkScalar dy = drawCell(canvas, gModes[i],
bsalomon@google.coma3108262011-10-10 14:08:47 +0000156 gAlphaValue[alpha & 1],
157 gAlphaValue[alpha & 2]);
158 canvas->restore();
159
160 canvas->translate(0, dy * 5 / 4);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000161 }
162 canvas->restore();
163 canvas->restore();
164 canvas->translate(W * 5 / 4, 0);
165 }
166 }
167
bsalomon@google.coma3108262011-10-10 14:08:47 +0000168 private:
169 typedef GM INHERITED;
170 };
171
172//////////////////////////////////////////////////////////////////////////////
173
174 static GM* MyFactory(void*) { return new AARectModesGM; }
175 static GMRegistry reg(MyFactory);
176
177}