reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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" |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame^] | 10 | #include "SkGradientShader.h" |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 11 | |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame^] | 12 | #define W SkIntToScalar(80) |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 13 | #define H SkIntToScalar(60) |
| 14 | |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame^] | 15 | typedef void (*PaintProc)(SkPaint*); |
| 16 | |
| 17 | static void identity_paintproc(SkPaint* paint) {} |
| 18 | static void gradient_paintproc(SkPaint* paint) { |
| 19 | const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE }; |
| 20 | const SkPoint pts[] = { { 0, 0 }, { W, H } }; |
| 21 | SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, |
| 22 | SK_ARRAY_COUNT(colors), |
| 23 | SkShader::kClamp_TileMode); |
| 24 | paint->setShader(s)->unref(); |
| 25 | } |
| 26 | |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 27 | typedef void (*Proc)(SkCanvas*, const SkPaint&); |
| 28 | |
| 29 | static void draw_hair(SkCanvas* canvas, const SkPaint& paint) { |
| 30 | SkPaint p(paint); |
| 31 | p.setStrokeWidth(0); |
| 32 | canvas->drawLine(0, 0, W, H, p); |
| 33 | } |
| 34 | |
| 35 | static void draw_thick(SkCanvas* canvas, const SkPaint& paint) { |
| 36 | SkPaint p(paint); |
| 37 | p.setStrokeWidth(H/5); |
| 38 | canvas->drawLine(0, 0, W, H, p); |
| 39 | } |
| 40 | |
| 41 | static void draw_rect(SkCanvas* canvas, const SkPaint& paint) { |
| 42 | canvas->drawRect(SkRect::MakeWH(W, H), paint); |
| 43 | } |
| 44 | |
| 45 | static void draw_oval(SkCanvas* canvas, const SkPaint& paint) { |
| 46 | canvas->drawOval(SkRect::MakeWH(W, H), paint); |
| 47 | } |
| 48 | |
| 49 | static void draw_text(SkCanvas* canvas, const SkPaint& paint) { |
| 50 | SkPaint p(paint); |
| 51 | p.setTextSize(H/4); |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame^] | 52 | canvas->drawText("Hamburge", 8, 0, H*2/3, p); |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | class SrcModeGM : public skiagm::GM { |
| 56 | SkPath fPath; |
| 57 | public: |
| 58 | SrcModeGM() { |
| 59 | this->setBGColor(0xFFDDDDDD); |
| 60 | } |
| 61 | |
| 62 | protected: |
| 63 | virtual SkString onShortName() { |
| 64 | return SkString("srcmode"); |
| 65 | } |
| 66 | |
| 67 | virtual SkISize onISize() { |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame^] | 68 | return SkISize::Make(640, 760); |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | virtual void onDraw(SkCanvas* canvas) { |
| 72 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 73 | |
| 74 | SkPaint paint; |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 75 | paint.setColor(0x80FF0000); |
| 76 | |
| 77 | const Proc procs[] = { |
| 78 | draw_hair, draw_thick, draw_rect, draw_oval, draw_text |
| 79 | }; |
| 80 | |
| 81 | const SkXfermode::Mode modes[] = { |
| 82 | SkXfermode::kSrcOver_Mode, SkXfermode::kSrc_Mode, SkXfermode::kClear_Mode |
| 83 | }; |
| 84 | |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame^] | 85 | const PaintProc paintProcs[] = { |
| 86 | identity_paintproc, gradient_paintproc |
| 87 | }; |
| 88 | |
| 89 | for (int aa = 0; aa <= 1; ++aa) { |
| 90 | paint.setAntiAlias(SkToBool(aa)); |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 91 | canvas->save(); |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame^] | 92 | for (size_t i = 0; i < SK_ARRAY_COUNT(paintProcs); ++i) { |
| 93 | paintProcs[i](&paint); |
| 94 | for (size_t x = 0; x < SK_ARRAY_COUNT(modes); ++x) { |
| 95 | paint.setXfermodeMode(modes[x]); |
| 96 | canvas->save(); |
| 97 | for (size_t y = 0; y < SK_ARRAY_COUNT(procs); ++y) { |
| 98 | procs[y](canvas, paint); |
| 99 | canvas->translate(0, H * 5 / 4); |
| 100 | } |
| 101 | canvas->restore(); |
| 102 | canvas->translate(W * 5 / 4, 0); |
| 103 | } |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 104 | } |
| 105 | canvas->restore(); |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame^] | 106 | canvas->translate(0, (H * 5 / 4) * SK_ARRAY_COUNT(procs)); |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | typedef skiagm::GM INHERITED; |
| 112 | }; |
| 113 | |
| 114 | /////////////////////////////////////////////////////////////////////////////// |
| 115 | |
| 116 | DEF_GM(return new SrcModeGM;) |
| 117 | |