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