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