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