blob: 2533c14a1faa98a01f62abc9e6620e6778e9d697 [file] [log] [blame]
reed@google.com5dd85a42012-11-15 13:46:47 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBlendMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkColor.h"
12#include "include/core/SkFont.h"
13#include "include/core/SkImageInfo.h"
14#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040016#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 Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include "include/core/SkTileMode.h"
25#include "include/core/SkTypeface.h"
26#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "include/effects/SkGradientShader.h"
28#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040029#include "tools/fonts/FontToolUtils.h"
reed@google.com5dd85a42012-11-15 13:46:47 +000030
reed@google.coma4f81372012-11-15 15:56:38 +000031#define W SkIntToScalar(80)
reed@google.com5dd85a42012-11-15 13:46:47 +000032#define H SkIntToScalar(60)
33
reed@google.coma4f81372012-11-15 15:56:38 +000034typedef void (*PaintProc)(SkPaint*);
35
mike@reedtribe.org92aa7562012-11-16 02:23:10 +000036static void identity_paintproc(SkPaint* paint) {
halcanary96fcdcc2015-08-27 07:41:13 -070037 paint->setShader(nullptr);
mike@reedtribe.org92aa7562012-11-16 02:23:10 +000038}
39
reed@google.coma4f81372012-11-15 15:56:38 +000040static void gradient_paintproc(SkPaint* paint) {
41 const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE };
42 const SkPoint pts[] = { { 0, 0 }, { W, H } };
Herb Derbyc37b3862022-06-21 09:49:17 -040043 paint->setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040044 SkTileMode::kClamp));
reed@google.coma4f81372012-11-15 15:56:38 +000045}
46
Hal Canarydf2d27e2019-01-08 09:38:02 -050047typedef void (*Proc)(SkCanvas*, const SkPaint&, const SkFont&);
reed@google.com5dd85a42012-11-15 13:46:47 +000048
Hal Canarydf2d27e2019-01-08 09:38:02 -050049static void draw_hair(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
reed@google.com5dd85a42012-11-15 13:46:47 +000050 SkPaint p(paint);
51 p.setStrokeWidth(0);
52 canvas->drawLine(0, 0, W, H, p);
53}
54
Hal Canarydf2d27e2019-01-08 09:38:02 -050055static void draw_thick(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
reed@google.com5dd85a42012-11-15 13:46:47 +000056 SkPaint p(paint);
57 p.setStrokeWidth(H/5);
58 canvas->drawLine(0, 0, W, H, p);
59}
60
Hal Canarydf2d27e2019-01-08 09:38:02 -050061static void draw_rect(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
reed@google.com5dd85a42012-11-15 13:46:47 +000062 canvas->drawRect(SkRect::MakeWH(W, H), paint);
63}
64
Hal Canarydf2d27e2019-01-08 09:38:02 -050065static void draw_oval(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
reed@google.com5dd85a42012-11-15 13:46:47 +000066 canvas->drawOval(SkRect::MakeWH(W, H), paint);
67}
68
Hal Canarydf2d27e2019-01-08 09:38:02 -050069static void draw_text(SkCanvas* canvas, const SkPaint& paint, const SkFont& font) {
70 canvas->drawString("Hamburge", 0, H*2/3, font, paint);
reed@google.com5dd85a42012-11-15 13:46:47 +000071}
72
73class SrcModeGM : public skiagm::GM {
74 SkPath fPath;
reed@google.com5dd85a42012-11-15 13:46:47 +000075
Hal Canarybd865e22019-07-18 11:51:19 -040076 void onOnceBeforeDraw() override { this->setBGColor(SK_ColorBLACK); }
reed@google.com5dd85a42012-11-15 13:46:47 +000077
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000078 SkString getName() const override { return SkString("srcmode"); }
Hal Canarybd865e22019-07-18 11:51:19 -040079
Leandro Lovisolo8f023882023-08-15 21:13:52 +000080 SkISize getISize() override { return {640, 760}; }
reed@google.com5dd85a42012-11-15 13:46:47 +000081
reed@google.com86eca472012-11-15 17:24:23 +000082 void drawContent(SkCanvas* canvas) {
reed@google.com5dd85a42012-11-15 13:46:47 +000083 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
84
85 SkPaint paint;
Kevin Lubicke836c3a2023-10-20 06:55:35 -040086 SkFont font(ToolUtils::DefaultPortableTypeface(), H / 4);
caryclark97a26d02015-07-17 13:20:48 -070087 paint.setColor(0x80F60000);
reed@google.com5dd85a42012-11-15 13:46:47 +000088
89 const Proc procs[] = {
90 draw_hair, draw_thick, draw_rect, draw_oval, draw_text
91 };
92
reed374772b2016-10-05 17:33:02 -070093 const SkBlendMode modes[] = {
94 SkBlendMode::kSrcOver, SkBlendMode::kSrc, SkBlendMode::kClear
reed@google.com5dd85a42012-11-15 13:46:47 +000095 };
96
reed@google.coma4f81372012-11-15 15:56:38 +000097 const PaintProc paintProcs[] = {
98 identity_paintproc, gradient_paintproc
99 };
100
101 for (int aa = 0; aa <= 1; ++aa) {
102 paint.setAntiAlias(SkToBool(aa));
Hal Canarydf2d27e2019-01-08 09:38:02 -0500103 font.setEdging(SkToBool(aa) ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
reed@google.com5dd85a42012-11-15 13:46:47 +0000104 canvas->save();
Herb Derbyc37b3862022-06-21 09:49:17 -0400105 for (size_t i = 0; i < std::size(paintProcs); ++i) {
reed@google.coma4f81372012-11-15 15:56:38 +0000106 paintProcs[i](&paint);
Herb Derbyc37b3862022-06-21 09:49:17 -0400107 for (size_t x = 0; x < std::size(modes); ++x) {
reed374772b2016-10-05 17:33:02 -0700108 paint.setBlendMode(modes[x]);
reed@google.coma4f81372012-11-15 15:56:38 +0000109 canvas->save();
Herb Derbyc37b3862022-06-21 09:49:17 -0400110 for (size_t y = 0; y < std::size(procs); ++y) {
Hal Canarydf2d27e2019-01-08 09:38:02 -0500111 procs[y](canvas, paint, font);
reed@google.coma4f81372012-11-15 15:56:38 +0000112 canvas->translate(0, H * 5 / 4);
113 }
114 canvas->restore();
115 canvas->translate(W * 5 / 4, 0);
116 }
reed@google.com5dd85a42012-11-15 13:46:47 +0000117 }
118 canvas->restore();
Herb Derbyc37b3862022-06-21 09:49:17 -0400119 canvas->translate(0, (H * 5 / 4) * std::size(procs));
reed@google.com5dd85a42012-11-15 13:46:47 +0000120 }
121 }
122
Brian Salomona8f43ce2019-09-10 11:43:25 -0400123 static sk_sp<SkSurface> compat_surface(SkCanvas* canvas, const SkISize& size) {
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000124 SkImageInfo info = SkImageInfo::MakeN32Premul(size);
Brian Salomona8f43ce2019-09-10 11:43:25 -0400125 sk_sp<SkSurface> surface = canvas->makeSurface(info);
halcanary96fcdcc2015-08-27 07:41:13 -0700126 if (nullptr == surface) {
Cary Clarka24712e2018-09-05 18:41:40 +0000127 // picture canvas will return null, so fall-back to raster
Kevin Lubick5c93acf2023-05-09 12:11:43 -0400128 surface = SkSurfaces::Raster(info);
reed52d9ac62014-06-30 09:05:34 -0700129 }
130 return surface;
reed@google.com86eca472012-11-15 17:24:23 +0000131 }
132
Hal Canarybd865e22019-07-18 11:51:19 -0400133 void onDraw(SkCanvas* canvas) override {
Brian Salomona8f43ce2019-09-10 11:43:25 -0400134 auto surf(compat_surface(canvas, this->getISize()));
reed@google.com86eca472012-11-15 17:24:23 +0000135 surf->getCanvas()->drawColor(SK_ColorWHITE);
136 this->drawContent(surf->getCanvas());
Mike Reedb746b1f2021-01-06 08:43:51 -0500137 surf->draw(canvas, 0, 0);
reed@google.com86eca472012-11-15 17:24:23 +0000138 }
reed@google.com5dd85a42012-11-15 13:46:47 +0000139};
140
141///////////////////////////////////////////////////////////////////////////////
142
143DEF_GM(return new SrcModeGM;)