reed@google.com | ab97397 | 2011-09-19 19:01:38 +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 | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 13 | static const struct { |
| 14 | SkXfermode::Mode fMode; |
| 15 | const char* fLabel; |
| 16 | } gModes[] = { |
| 17 | { SkXfermode::kClear_Mode, "Clear" }, |
| 18 | { SkXfermode::kSrc_Mode, "Src" }, |
| 19 | { SkXfermode::kDst_Mode, "Dst" }, |
| 20 | { SkXfermode::kSrcOver_Mode, "SrcOver" }, |
| 21 | { SkXfermode::kDstOver_Mode, "DstOver" }, |
| 22 | { SkXfermode::kSrcIn_Mode, "SrcIn" }, |
| 23 | { SkXfermode::kDstIn_Mode, "DstIn" }, |
| 24 | { SkXfermode::kSrcOut_Mode, "SrcOut" }, |
| 25 | { SkXfermode::kDstOut_Mode, "DstOut" }, |
| 26 | { SkXfermode::kSrcATop_Mode, "SrcATop" }, |
| 27 | { SkXfermode::kDstATop_Mode, "DstATop" }, |
| 28 | { SkXfermode::kXor_Mode, "Xor" }, |
| 29 | }; |
| 30 | |
| 31 | const int gWidth = 64; |
| 32 | const int gHeight = 64; |
| 33 | const SkScalar W = SkIntToScalar(gWidth); |
| 34 | const SkScalar H = SkIntToScalar(gHeight); |
| 35 | |
| 36 | static SkScalar drawCell(SkCanvas* canvas, SkXfermode* mode, SkAlpha a0, SkAlpha a1) { |
| 37 | |
| 38 | SkPaint paint; |
| 39 | paint.setAntiAlias(true); |
| 40 | |
| 41 | SkRect r = SkRect::MakeWH(W, H); |
| 42 | r.inset(W/10, H/10); |
| 43 | |
| 44 | paint.setColor(SK_ColorBLUE); |
| 45 | paint.setAlpha(a0); |
| 46 | canvas->drawOval(r, paint); |
| 47 | |
| 48 | paint.setColor(SK_ColorRED); |
| 49 | paint.setAlpha(a1); |
| 50 | paint.setXfermode(mode); |
| 51 | for (int angle = 0; angle < 24; ++angle) { |
| 52 | SkScalar x = SkScalarCos(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gWidth; |
| 53 | SkScalar y = SkScalarSin(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gHeight; |
| 54 | paint.setStrokeWidth(SK_Scalar1 * angle * 2 / 24); |
| 55 | canvas->drawLine(W/2, H/2, W/2 + x, H/2 + y, paint); |
| 56 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 57 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 58 | return H; |
| 59 | } |
| 60 | |
| 61 | static SkShader* make_bg_shader() { |
| 62 | SkBitmap bm; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 63 | bm.allocN32Pixels(2, 2); |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 64 | *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF; |
| 65 | *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCC, 0xCC, 0xCC); |
| 66 | |
| 67 | SkShader* s = SkShader::CreateBitmapShader(bm, |
| 68 | SkShader::kRepeat_TileMode, |
| 69 | SkShader::kRepeat_TileMode); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 70 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 71 | SkMatrix m; |
| 72 | m.setScale(SkIntToScalar(6), SkIntToScalar(6)); |
| 73 | s->setLocalMatrix(m); |
| 74 | return s; |
| 75 | } |
| 76 | |
| 77 | namespace skiagm { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 78 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 79 | class HairModesGM : public GM { |
| 80 | SkPaint fBGPaint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 81 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 82 | protected: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 83 | |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 84 | virtual SkString onShortName() SK_OVERRIDE { |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 85 | return SkString("hairmodes"); |
| 86 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 87 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 88 | virtual SkISize onISize() { return make_isize(640, 480); } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 89 | |
reed@google.com | 7775d66 | 2012-11-27 15:15:58 +0000 | [diff] [blame] | 90 | virtual void onOnceBeforeDraw() SK_OVERRIDE { |
| 91 | fBGPaint.setShader(make_bg_shader())->unref(); |
| 92 | } |
| 93 | |
| 94 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 95 | const SkRect bounds = SkRect::MakeWH(W, H); |
| 96 | static const SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 97 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 98 | canvas->translate(SkIntToScalar(4), SkIntToScalar(4)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 99 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 100 | for (int alpha = 0; alpha < 4; ++alpha) { |
| 101 | canvas->save(); |
| 102 | canvas->save(); |
| 103 | for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) { |
| 104 | if (6 == i) { |
| 105 | canvas->restore(); |
| 106 | canvas->translate(W * 5, 0); |
| 107 | canvas->save(); |
| 108 | } |
| 109 | SkXfermode* mode = SkXfermode::Create(gModes[i].fMode); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 110 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 111 | canvas->drawRect(bounds, fBGPaint); |
| 112 | canvas->saveLayer(&bounds, NULL); |
| 113 | SkScalar dy = drawCell(canvas, mode, |
| 114 | gAlphaValue[alpha & 1], |
| 115 | gAlphaValue[alpha & 2]); |
| 116 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 117 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 118 | canvas->translate(0, dy * 5 / 4); |
| 119 | SkSafeUnref(mode); |
| 120 | } |
| 121 | canvas->restore(); |
| 122 | canvas->restore(); |
| 123 | canvas->translate(W * 5 / 4, 0); |
| 124 | } |
| 125 | } |
reed@google.com | fbc2117 | 2011-09-19 19:08:33 +0000 | [diff] [blame] | 126 | |
| 127 | // disable pdf for now, since it crashes on mac |
| 128 | virtual uint32_t onGetFlags() const { return kSkipPDF_Flag; } |
| 129 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 130 | private: |
| 131 | typedef GM INHERITED; |
| 132 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 133 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 134 | ////////////////////////////////////////////////////////////////////////////// |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 135 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 136 | static GM* MyFactory(void*) { return new HairModesGM; } |
| 137 | static GMRegistry reg(MyFactory); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 138 | |
reed@google.com | ab97397 | 2011-09-19 19:01:38 +0000 | [diff] [blame] | 139 | } |