bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1 | /* |
| 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" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 11 | #include "SkPath.h" |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 12 | #include "SkShader.h" |
| 13 | |
reed@google.com | 7616615 | 2011-11-14 13:14:58 +0000 | [diff] [blame] | 14 | static 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 20 | |
reed@google.com | 7616615 | 2011-11-14 13:14:58 +0000 | [diff] [blame] | 21 | {610, 160}, {610, 160}, |
| 22 | {610, 199}, {610, 199}, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 23 | |
reed@google.com | 7616615 | 2011-11-14 13:14:58 +0000 | [diff] [blame] | 24 | {10, 198}, {610, 198}, |
| 25 | {610, 199}, {10, 199}, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 26 | |
reed@google.com | 7616615 | 2011-11-14 13:14:58 +0000 | [diff] [blame] | 27 | {10, 160}, {10, 160}, |
| 28 | {10, 199}, {10, 199} |
| 29 | }; |
| 30 | char verbs[] = { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 31 | 0, 1, 1, 1, 4, |
reed@google.com | 7616615 | 2011-11-14 13:14:58 +0000 | [diff] [blame] | 32 | 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.com | 31648eb | 2011-11-23 15:01:08 +0000 | [diff] [blame] | 38 | for (size_t i = 0; i < sizeof(verbs); ++i) { |
reed@google.com | 7616615 | 2011-11-14 13:14:58 +0000 | [diff] [blame] | 39 | 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.com | 31648eb | 2011-11-23 15:01:08 +0000 | [diff] [blame] | 51 | default: |
| 52 | SkASSERT(false); |
| 53 | break; |
reed@google.com | 7616615 | 2011-11-14 13:14:58 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | SkRect clip = {0, 130, 772, 531}; |
| 57 | canvas->clipRect(clip); |
| 58 | canvas->drawPath(path, paint); |
| 59 | } |
| 60 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 61 | static const struct { |
| 62 | SkXfermode::Mode fMode; |
| 63 | const char* fLabel; |
| 64 | } gModes[] = { |
| 65 | { SkXfermode::kClear_Mode, "Clear" }, |
| 66 | { SkXfermode::kSrc_Mode, "Src" }, |
| 67 | { SkXfermode::kDst_Mode, "Dst" }, |
| 68 | { SkXfermode::kSrcOver_Mode, "SrcOver" }, |
| 69 | { SkXfermode::kDstOver_Mode, "DstOver" }, |
| 70 | { SkXfermode::kSrcIn_Mode, "SrcIn" }, |
| 71 | { SkXfermode::kDstIn_Mode, "DstIn" }, |
| 72 | { SkXfermode::kSrcOut_Mode, "SrcOut" }, |
| 73 | { SkXfermode::kDstOut_Mode, "DstOut" }, |
| 74 | { SkXfermode::kSrcATop_Mode, "SrcATop" }, |
| 75 | { SkXfermode::kDstATop_Mode, "DstATop" }, |
| 76 | { SkXfermode::kXor_Mode, "Xor" }, |
| 77 | }; |
| 78 | |
| 79 | const int gWidth = 64; |
| 80 | const int gHeight = 64; |
| 81 | const SkScalar W = SkIntToScalar(gWidth); |
| 82 | const SkScalar H = SkIntToScalar(gHeight); |
| 83 | |
| 84 | static SkScalar drawCell(SkCanvas* canvas, SkXfermode* mode, |
| 85 | SkAlpha a0, SkAlpha a1) { |
| 86 | |
| 87 | SkPaint paint; |
| 88 | paint.setAntiAlias(true); |
| 89 | |
| 90 | SkRect r = SkRect::MakeWH(W, H); |
| 91 | r.inset(W/10, H/10); |
| 92 | |
| 93 | paint.setColor(SK_ColorBLUE); |
| 94 | paint.setAlpha(a0); |
| 95 | canvas->drawOval(r, paint); |
| 96 | |
| 97 | paint.setColor(SK_ColorRED); |
| 98 | paint.setAlpha(a1); |
| 99 | paint.setXfermode(mode); |
| 100 | |
| 101 | SkScalar offset = SK_Scalar1 / 3; |
| 102 | SkRect rect = SkRect::MakeXYWH(W / 4 + offset, |
| 103 | H / 4 + offset, |
| 104 | W / 2, H / 2); |
| 105 | canvas->drawRect(rect, paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 106 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 107 | return H; |
| 108 | } |
| 109 | |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 110 | static sk_sp<SkShader> make_bg_shader() { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 111 | SkBitmap bm; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 112 | bm.allocN32Pixels(2, 2); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 113 | *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF; |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 114 | *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCE, |
| 115 | 0xCF, 0xCE); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 116 | |
robertphillips | 1d24b8d | 2015-03-26 19:57:08 -0700 | [diff] [blame] | 117 | const SkMatrix m = SkMatrix::MakeScale(SkIntToScalar(6), SkIntToScalar(6)); |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 118 | return SkShader::MakeBitmapShader(bm, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, |
| 119 | &m); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | namespace skiagm { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 123 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 124 | class AARectModesGM : public GM { |
| 125 | SkPaint fBGPaint; |
| 126 | public: |
| 127 | AARectModesGM () { |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 128 | fBGPaint.setShader(make_bg_shader()); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | protected: |
| 132 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 133 | SkString onShortName() override { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 134 | return SkString("aarectmodes"); |
| 135 | } |
| 136 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 137 | SkISize onISize() override { return SkISize::Make(640, 480); } |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 138 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 139 | void onDraw(SkCanvas* canvas) override { |
caryclark@google.com | 1313086 | 2012-06-06 12:10:45 +0000 | [diff] [blame] | 140 | if (false) { // avoid bit rot, suppress warning |
| 141 | test4(canvas); |
| 142 | } |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 143 | const SkRect bounds = SkRect::MakeWH(W, H); |
| 144 | static const SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 }; |
| 145 | |
| 146 | canvas->translate(SkIntToScalar(4), SkIntToScalar(4)); |
| 147 | |
| 148 | for (int alpha = 0; alpha < 4; ++alpha) { |
| 149 | canvas->save(); |
| 150 | canvas->save(); |
| 151 | for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) { |
| 152 | if (6 == i) { |
| 153 | canvas->restore(); |
| 154 | canvas->translate(W * 5, 0); |
| 155 | canvas->save(); |
| 156 | } |
| 157 | SkXfermode* mode = SkXfermode::Create(gModes[i].fMode); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 158 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 159 | canvas->drawRect(bounds, fBGPaint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 160 | canvas->saveLayer(&bounds, nullptr); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 161 | SkScalar dy = drawCell(canvas, mode, |
| 162 | gAlphaValue[alpha & 1], |
| 163 | gAlphaValue[alpha & 2]); |
| 164 | canvas->restore(); |
| 165 | |
| 166 | canvas->translate(0, dy * 5 / 4); |
| 167 | SkSafeUnref(mode); |
| 168 | } |
| 169 | canvas->restore(); |
| 170 | canvas->restore(); |
| 171 | canvas->translate(W * 5 / 4, 0); |
| 172 | } |
| 173 | } |
| 174 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 175 | private: |
| 176 | typedef GM INHERITED; |
| 177 | }; |
| 178 | |
| 179 | ////////////////////////////////////////////////////////////////////////////// |
| 180 | |
| 181 | static GM* MyFactory(void*) { return new AARectModesGM; } |
| 182 | static GMRegistry reg(MyFactory); |
| 183 | |
| 184 | } |