blob: 9d593638e6692f19e305cb23cce3969098d8585f [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkBlendMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkCanvas.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040012#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkColorPriv.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040014#include "include/core/SkMatrix.h"
15#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkPath.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040017#include "include/core/SkPoint.h"
18#include "include/core/SkRect.h"
19#include "include/core/SkRefCnt.h"
20#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/core/SkShader.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040022#include "include/core/SkTileMode.h"
23#include "include/core/SkTypes.h"
bsalomon@google.coma3108262011-10-10 14:08:47 +000024
reed@google.com76166152011-11-14 13:14:58 +000025static void test4(SkCanvas* canvas) {
26 SkPaint paint;
27 paint.setAntiAlias(true);
28 SkPoint pts[] = {
29 {10, 160}, {610, 160},
30 {610, 160}, {10, 160},
rmistry@google.comd6176b02012-08-23 18:14:13 +000031
reed@google.com76166152011-11-14 13:14:58 +000032 {610, 160}, {610, 160},
33 {610, 199}, {610, 199},
rmistry@google.comd6176b02012-08-23 18:14:13 +000034
reed@google.com76166152011-11-14 13:14:58 +000035 {10, 198}, {610, 198},
36 {610, 199}, {10, 199},
rmistry@google.comd6176b02012-08-23 18:14:13 +000037
reed@google.com76166152011-11-14 13:14:58 +000038 {10, 160}, {10, 160},
39 {10, 199}, {10, 199}
40 };
41 char verbs[] = {
rmistry@google.comd6176b02012-08-23 18:14:13 +000042 0, 1, 1, 1, 4,
reed@google.com76166152011-11-14 13:14:58 +000043 0, 1, 1, 1, 4,
44 0, 1, 1, 1, 4,
45 0, 1, 1, 1, 4
46 };
47 SkPath path;
48 SkPoint* ptPtr = pts;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000049 for (size_t i = 0; i < sizeof(verbs); ++i) {
reed@google.com76166152011-11-14 13:14:58 +000050 switch ((SkPath::Verb) verbs[i]) {
51 case SkPath::kMove_Verb:
52 path.moveTo(ptPtr->fX, ptPtr->fY);
53 ++ptPtr;
54 break;
55 case SkPath::kLine_Verb:
56 path.lineTo(ptPtr->fX, ptPtr->fY);
57 ++ptPtr;
58 break;
59 case SkPath::kClose_Verb:
60 path.close();
61 break;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000062 default:
63 SkASSERT(false);
64 break;
reed@google.com76166152011-11-14 13:14:58 +000065 }
66 }
67 SkRect clip = {0, 130, 772, 531};
68 canvas->clipRect(clip);
69 canvas->drawPath(path, paint);
70}
71
Brian Osmand1e67e72017-03-15 12:19:37 -040072constexpr SkBlendMode gModes[] = {
73 SkBlendMode::kClear,
74 SkBlendMode::kSrc,
75 SkBlendMode::kDst,
76 SkBlendMode::kSrcOver,
77 SkBlendMode::kDstOver,
78 SkBlendMode::kSrcIn,
79 SkBlendMode::kDstIn,
80 SkBlendMode::kSrcOut,
81 SkBlendMode::kDstOut,
82 SkBlendMode::kSrcATop,
83 SkBlendMode::kDstATop,
84 SkBlendMode::kXor,
bsalomon@google.coma3108262011-10-10 14:08:47 +000085};
86
87const int gWidth = 64;
88const int gHeight = 64;
89const SkScalar W = SkIntToScalar(gWidth);
90const SkScalar H = SkIntToScalar(gHeight);
91
reed374772b2016-10-05 17:33:02 -070092static SkScalar drawCell(SkCanvas* canvas, SkBlendMode mode, SkAlpha a0, SkAlpha a1) {
bsalomon@google.coma3108262011-10-10 14:08:47 +000093
94 SkPaint paint;
95 paint.setAntiAlias(true);
96
97 SkRect r = SkRect::MakeWH(W, H);
98 r.inset(W/10, H/10);
99
100 paint.setColor(SK_ColorBLUE);
101 paint.setAlpha(a0);
102 canvas->drawOval(r, paint);
103
104 paint.setColor(SK_ColorRED);
105 paint.setAlpha(a1);
reed374772b2016-10-05 17:33:02 -0700106 paint.setBlendMode(mode);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000107
108 SkScalar offset = SK_Scalar1 / 3;
109 SkRect rect = SkRect::MakeXYWH(W / 4 + offset,
110 H / 4 + offset,
111 W / 2, H / 2);
112 canvas->drawRect(rect, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000113
bsalomon@google.coma3108262011-10-10 14:08:47 +0000114 return H;
115}
116
reed2ad1aa62016-03-09 09:50:50 -0800117static sk_sp<SkShader> make_bg_shader() {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000118 SkBitmap bm;
reed@google.comeb9a46c2014-01-25 16:46:20 +0000119 bm.allocN32Pixels(2, 2);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000120 *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF;
caryclark12596012015-07-29 05:27:47 -0700121 *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCE,
122 0xCF, 0xCE);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000123
robertphillips1d24b8d2015-03-26 19:57:08 -0700124 const SkMatrix m = SkMatrix::MakeScale(SkIntToScalar(6), SkIntToScalar(6));
Mike Reed50acf8f2019-04-08 13:20:23 -0400125 return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &m);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000126}
127
Hal Canary607c44f2019-01-23 10:40:02 -0500128DEF_SIMPLE_GM(aarectmodes, canvas, 640, 480) {
129 SkPaint bgPaint;
130 bgPaint.setShader(make_bg_shader());
caryclark@google.com13130862012-06-06 12:10:45 +0000131 if (false) { // avoid bit rot, suppress warning
132 test4(canvas);
133 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000134 const SkRect bounds = SkRect::MakeWH(W, H);
mtkleindbfd7ab2016-09-01 11:24:54 -0700135 constexpr SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 };
bsalomon@google.coma3108262011-10-10 14:08:47 +0000136
137 canvas->translate(SkIntToScalar(4), SkIntToScalar(4));
138
139 for (int alpha = 0; alpha < 4; ++alpha) {
140 canvas->save();
141 canvas->save();
142 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) {
143 if (6 == i) {
144 canvas->restore();
145 canvas->translate(W * 5, 0);
146 canvas->save();
147 }
Hal Canary607c44f2019-01-23 10:40:02 -0500148 canvas->drawRect(bounds, bgPaint);
halcanary96fcdcc2015-08-27 07:41:13 -0700149 canvas->saveLayer(&bounds, nullptr);
Brian Osmand1e67e72017-03-15 12:19:37 -0400150 SkScalar dy = drawCell(canvas, gModes[i],
bsalomon@google.coma3108262011-10-10 14:08:47 +0000151 gAlphaValue[alpha & 1],
152 gAlphaValue[alpha & 2]);
153 canvas->restore();
154
155 canvas->translate(0, dy * 5 / 4);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000156 }
157 canvas->restore();
158 canvas->restore();
159 canvas->translate(W * 5 / 4, 0);
160 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000161}