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