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" |
bungeman | 7403d87 | 2015-08-04 14:56:53 -0700 | [diff] [blame^] | 11 | #include "SkPath.h" |
reed@google.com | 86eca47 | 2012-11-15 17:24:23 +0000 | [diff] [blame] | 12 | #include "SkSurface.h" |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 13 | |
mike@reedtribe.org | 1588974 | 2012-11-16 02:31:03 +0000 | [diff] [blame] | 14 | #if SK_SUPPORT_GPU |
| 15 | #include "SkGpuDevice.h" |
| 16 | #endif |
mike@reedtribe.org | 92aa756 | 2012-11-16 02:23:10 +0000 | [diff] [blame] | 17 | |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 18 | #define W SkIntToScalar(80) |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 19 | #define H SkIntToScalar(60) |
| 20 | |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 21 | typedef void (*PaintProc)(SkPaint*); |
| 22 | |
mike@reedtribe.org | 92aa756 | 2012-11-16 02:23:10 +0000 | [diff] [blame] | 23 | static void identity_paintproc(SkPaint* paint) { |
| 24 | paint->setShader(NULL); |
| 25 | } |
| 26 | |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 27 | static void gradient_paintproc(SkPaint* paint) { |
| 28 | const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE }; |
| 29 | const SkPoint pts[] = { { 0, 0 }, { W, H } }; |
| 30 | SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, |
| 31 | SK_ARRAY_COUNT(colors), |
| 32 | SkShader::kClamp_TileMode); |
| 33 | paint->setShader(s)->unref(); |
| 34 | } |
| 35 | |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 36 | typedef void (*Proc)(SkCanvas*, const SkPaint&); |
| 37 | |
| 38 | static void draw_hair(SkCanvas* canvas, const SkPaint& paint) { |
| 39 | SkPaint p(paint); |
| 40 | p.setStrokeWidth(0); |
| 41 | canvas->drawLine(0, 0, W, H, p); |
| 42 | } |
| 43 | |
| 44 | static void draw_thick(SkCanvas* canvas, const SkPaint& paint) { |
| 45 | SkPaint p(paint); |
| 46 | p.setStrokeWidth(H/5); |
| 47 | canvas->drawLine(0, 0, W, H, p); |
| 48 | } |
| 49 | |
| 50 | static void draw_rect(SkCanvas* canvas, const SkPaint& paint) { |
| 51 | canvas->drawRect(SkRect::MakeWH(W, H), paint); |
| 52 | } |
| 53 | |
| 54 | static void draw_oval(SkCanvas* canvas, const SkPaint& paint) { |
| 55 | canvas->drawOval(SkRect::MakeWH(W, H), paint); |
| 56 | } |
| 57 | |
| 58 | static void draw_text(SkCanvas* canvas, const SkPaint& paint) { |
| 59 | SkPaint p(paint); |
| 60 | p.setTextSize(H/4); |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 61 | canvas->drawText("Hamburge", 8, 0, H*2/3, p); |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | class SrcModeGM : public skiagm::GM { |
| 65 | SkPath fPath; |
| 66 | public: |
| 67 | SrcModeGM() { |
reed@google.com | 86eca47 | 2012-11-15 17:24:23 +0000 | [diff] [blame] | 68 | this->setBGColor(SK_ColorBLACK); |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | protected: |
| 72 | virtual SkString onShortName() { |
| 73 | return SkString("srcmode"); |
| 74 | } |
| 75 | |
| 76 | virtual SkISize onISize() { |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 77 | return SkISize::Make(640, 760); |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 78 | } |
| 79 | |
reed@google.com | 86eca47 | 2012-11-15 17:24:23 +0000 | [diff] [blame] | 80 | void drawContent(SkCanvas* canvas) { |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 81 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 82 | |
| 83 | SkPaint paint; |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 84 | sk_tool_utils::set_portable_typeface(&paint); |
caryclark | 97a26d0 | 2015-07-17 13:20:48 -0700 | [diff] [blame] | 85 | paint.setColor(0x80F60000); |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 86 | |
| 87 | const Proc procs[] = { |
| 88 | draw_hair, draw_thick, draw_rect, draw_oval, draw_text |
| 89 | }; |
| 90 | |
| 91 | const SkXfermode::Mode modes[] = { |
| 92 | SkXfermode::kSrcOver_Mode, SkXfermode::kSrc_Mode, SkXfermode::kClear_Mode |
| 93 | }; |
| 94 | |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 95 | const PaintProc paintProcs[] = { |
| 96 | identity_paintproc, gradient_paintproc |
| 97 | }; |
| 98 | |
| 99 | for (int aa = 0; aa <= 1; ++aa) { |
| 100 | paint.setAntiAlias(SkToBool(aa)); |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 101 | canvas->save(); |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 102 | for (size_t i = 0; i < SK_ARRAY_COUNT(paintProcs); ++i) { |
| 103 | paintProcs[i](&paint); |
| 104 | for (size_t x = 0; x < SK_ARRAY_COUNT(modes); ++x) { |
| 105 | paint.setXfermodeMode(modes[x]); |
| 106 | canvas->save(); |
| 107 | for (size_t y = 0; y < SK_ARRAY_COUNT(procs); ++y) { |
| 108 | procs[y](canvas, paint); |
| 109 | canvas->translate(0, H * 5 / 4); |
| 110 | } |
| 111 | canvas->restore(); |
| 112 | canvas->translate(W * 5 / 4, 0); |
| 113 | } |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 114 | } |
| 115 | canvas->restore(); |
reed@google.com | a4f8137 | 2012-11-15 15:56:38 +0000 | [diff] [blame] | 116 | canvas->translate(0, (H * 5 / 4) * SK_ARRAY_COUNT(procs)); |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 120 | static SkSurface* compat_surface(SkCanvas* canvas, const SkISize& size, bool skipGPU) { |
commit-bot@chromium.org | 32678d9 | 2014-01-15 02:38:22 +0000 | [diff] [blame] | 121 | SkImageInfo info = SkImageInfo::MakeN32Premul(size); |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 122 | |
| 123 | bool callNewSurface = true; |
mike@reedtribe.org | 1588974 | 2012-11-16 02:31:03 +0000 | [diff] [blame] | 124 | #if SK_SUPPORT_GPU |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 125 | if (canvas->getGrContext() && skipGPU) { |
| 126 | callNewSurface = false; |
mike@reedtribe.org | 92aa756 | 2012-11-16 02:23:10 +0000 | [diff] [blame] | 127 | } |
mike@reedtribe.org | 1588974 | 2012-11-16 02:31:03 +0000 | [diff] [blame] | 128 | #endif |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 129 | SkSurface* surface = callNewSurface ? canvas->newSurface(info) : NULL; |
| 130 | if (NULL == surface) { |
| 131 | // picture canvas will return null, so fall-back to raster |
| 132 | surface = SkSurface::NewRaster(info); |
| 133 | } |
| 134 | return surface; |
reed@google.com | 86eca47 | 2012-11-15 17:24:23 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | virtual void onDraw(SkCanvas* canvas) { |
reed@google.com | aef7361 | 2012-11-16 13:41:45 +0000 | [diff] [blame] | 138 | SkAutoTUnref<SkSurface> surf(compat_surface(canvas, this->getISize(), |
| 139 | this->isCanvasDeferred())); |
reed@google.com | 86eca47 | 2012-11-15 17:24:23 +0000 | [diff] [blame] | 140 | surf->getCanvas()->drawColor(SK_ColorWHITE); |
| 141 | this->drawContent(surf->getCanvas()); |
| 142 | surf->draw(canvas, 0, 0, NULL); |
reed@google.com | 86eca47 | 2012-11-15 17:24:23 +0000 | [diff] [blame] | 143 | } |
| 144 | |
reed@google.com | 5dd85a4 | 2012-11-15 13:46:47 +0000 | [diff] [blame] | 145 | private: |
| 146 | typedef skiagm::GM INHERITED; |
| 147 | }; |
| 148 | |
| 149 | /////////////////////////////////////////////////////////////////////////////// |
| 150 | |
| 151 | DEF_GM(return new SrcModeGM;) |