blob: fdab82650b1be8111297682bafb63218877d329d [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"
Mike Reedcfb130c2020-08-03 11:02:20 -040017#include "include/core/SkPathBuilder.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040018#include "include/core/SkPoint.h"
19#include "include/core/SkRect.h"
20#include "include/core/SkRefCnt.h"
21#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/core/SkShader.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040023#include "include/core/SkTileMode.h"
24#include "include/core/SkTypes.h"
bsalomon@google.coma3108262011-10-10 14:08:47 +000025
reed@google.com76166152011-11-14 13:14:58 +000026static void test4(SkCanvas* canvas) {
27 SkPaint paint;
28 paint.setAntiAlias(true);
29 SkPoint pts[] = {
30 {10, 160}, {610, 160},
31 {610, 160}, {10, 160},
rmistry@google.comd6176b02012-08-23 18:14:13 +000032
reed@google.com76166152011-11-14 13:14:58 +000033 {610, 160}, {610, 160},
34 {610, 199}, {610, 199},
rmistry@google.comd6176b02012-08-23 18:14:13 +000035
reed@google.com76166152011-11-14 13:14:58 +000036 {10, 198}, {610, 198},
37 {610, 199}, {10, 199},
rmistry@google.comd6176b02012-08-23 18:14:13 +000038
reed@google.com76166152011-11-14 13:14:58 +000039 {10, 160}, {10, 160},
40 {10, 199}, {10, 199}
41 };
42 char verbs[] = {
rmistry@google.comd6176b02012-08-23 18:14:13 +000043 0, 1, 1, 1, 4,
reed@google.com76166152011-11-14 13:14:58 +000044 0, 1, 1, 1, 4,
45 0, 1, 1, 1, 4,
46 0, 1, 1, 1, 4
47 };
Mike Reedcfb130c2020-08-03 11:02:20 -040048 SkPathBuilder path;
reed@google.com76166152011-11-14 13:14:58 +000049 SkPoint* ptPtr = pts;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000050 for (size_t i = 0; i < sizeof(verbs); ++i) {
reed@google.com76166152011-11-14 13:14:58 +000051 switch ((SkPath::Verb) verbs[i]) {
52 case SkPath::kMove_Verb:
53 path.moveTo(ptPtr->fX, ptPtr->fY);
54 ++ptPtr;
55 break;
56 case SkPath::kLine_Verb:
57 path.lineTo(ptPtr->fX, ptPtr->fY);
58 ++ptPtr;
59 break;
60 case SkPath::kClose_Verb:
61 path.close();
62 break;
bsalomon@google.com31648eb2011-11-23 15:01:08 +000063 default:
64 SkASSERT(false);
65 break;
reed@google.com76166152011-11-14 13:14:58 +000066 }
67 }
68 SkRect clip = {0, 130, 772, 531};
69 canvas->clipRect(clip);
Mike Reedcfb130c2020-08-03 11:02:20 -040070 canvas->drawPath(path.detach(), paint);
reed@google.com76166152011-11-14 13:14:58 +000071}
72
Brian Osmand1e67e72017-03-15 12:19:37 -040073constexpr SkBlendMode gModes[] = {
74 SkBlendMode::kClear,
75 SkBlendMode::kSrc,
76 SkBlendMode::kDst,
77 SkBlendMode::kSrcOver,
78 SkBlendMode::kDstOver,
79 SkBlendMode::kSrcIn,
80 SkBlendMode::kDstIn,
81 SkBlendMode::kSrcOut,
82 SkBlendMode::kDstOut,
83 SkBlendMode::kSrcATop,
84 SkBlendMode::kDstATop,
85 SkBlendMode::kXor,
bsalomon@google.coma3108262011-10-10 14:08:47 +000086};
87
88const int gWidth = 64;
89const int gHeight = 64;
90const SkScalar W = SkIntToScalar(gWidth);
91const SkScalar H = SkIntToScalar(gHeight);
92
reed374772b2016-10-05 17:33:02 -070093static SkScalar drawCell(SkCanvas* canvas, SkBlendMode mode, SkAlpha a0, SkAlpha a1) {
bsalomon@google.coma3108262011-10-10 14:08:47 +000094
95 SkPaint paint;
96 paint.setAntiAlias(true);
97
98 SkRect r = SkRect::MakeWH(W, H);
99 r.inset(W/10, H/10);
100
101 paint.setColor(SK_ColorBLUE);
102 paint.setAlpha(a0);
103 canvas->drawOval(r, paint);
104
105 paint.setColor(SK_ColorRED);
106 paint.setAlpha(a1);
reed374772b2016-10-05 17:33:02 -0700107 paint.setBlendMode(mode);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000108
109 SkScalar offset = SK_Scalar1 / 3;
110 SkRect rect = SkRect::MakeXYWH(W / 4 + offset,
111 H / 4 + offset,
112 W / 2, H / 2);
113 canvas->drawRect(rect, paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000114
bsalomon@google.coma3108262011-10-10 14:08:47 +0000115 return H;
116}
117
reed2ad1aa62016-03-09 09:50:50 -0800118static sk_sp<SkShader> make_bg_shader() {
bsalomon@google.coma3108262011-10-10 14:08:47 +0000119 SkBitmap bm;
reed@google.comeb9a46c2014-01-25 16:46:20 +0000120 bm.allocN32Pixels(2, 2);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000121 *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF;
caryclark12596012015-07-29 05:27:47 -0700122 *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCE,
123 0xCF, 0xCE);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000124
Mike Reed82abece2020-12-12 09:51:11 -0500125 return bm.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions(),
126 SkMatrix::Scale(6, 6));
bsalomon@google.coma3108262011-10-10 14:08:47 +0000127}
128
Hal Canary607c44f2019-01-23 10:40:02 -0500129DEF_SIMPLE_GM(aarectmodes, canvas, 640, 480) {
130 SkPaint bgPaint;
131 bgPaint.setShader(make_bg_shader());
caryclark@google.com13130862012-06-06 12:10:45 +0000132 if (false) { // avoid bit rot, suppress warning
133 test4(canvas);
134 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000135 const SkRect bounds = SkRect::MakeWH(W, H);
mtkleindbfd7ab2016-09-01 11:24:54 -0700136 constexpr SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 };
bsalomon@google.coma3108262011-10-10 14:08:47 +0000137
138 canvas->translate(SkIntToScalar(4), SkIntToScalar(4));
139
140 for (int alpha = 0; alpha < 4; ++alpha) {
141 canvas->save();
142 canvas->save();
143 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) {
144 if (6 == i) {
145 canvas->restore();
146 canvas->translate(W * 5, 0);
147 canvas->save();
148 }
Hal Canary607c44f2019-01-23 10:40:02 -0500149 canvas->drawRect(bounds, bgPaint);
halcanary96fcdcc2015-08-27 07:41:13 -0700150 canvas->saveLayer(&bounds, nullptr);
Brian Osmand1e67e72017-03-15 12:19:37 -0400151 SkScalar dy = drawCell(canvas, gModes[i],
bsalomon@google.coma3108262011-10-10 14:08:47 +0000152 gAlphaValue[alpha & 1],
153 gAlphaValue[alpha & 2]);
154 canvas->restore();
155
156 canvas->translate(0, dy * 5 / 4);
bsalomon@google.coma3108262011-10-10 14:08:47 +0000157 }
158 canvas->restore();
159 canvas->restore();
160 canvas->translate(W * 5 / 4, 0);
161 }
bsalomon@google.coma3108262011-10-10 14:08:47 +0000162}