blob: 9012981e810db3897a88c11bbeb6f675233275c7 [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"
reed@google.com5dd85a42012-11-15 13:46:47 +000029
reed@google.coma4f81372012-11-15 15:56:38 +000030#define W SkIntToScalar(80)
reed@google.com5dd85a42012-11-15 13:46:47 +000031#define H SkIntToScalar(60)
32
reed@google.coma4f81372012-11-15 15:56:38 +000033typedef void (*PaintProc)(SkPaint*);
34
mike@reedtribe.org92aa7562012-11-16 02:23:10 +000035static void identity_paintproc(SkPaint* paint) {
halcanary96fcdcc2015-08-27 07:41:13 -070036 paint->setShader(nullptr);
mike@reedtribe.org92aa7562012-11-16 02:23:10 +000037}
38
reed@google.coma4f81372012-11-15 15:56:38 +000039static void gradient_paintproc(SkPaint* paint) {
40 const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE };
41 const SkPoint pts[] = { { 0, 0 }, { W, H } };
reed1a9b9642016-03-13 14:13:58 -070042 paint->setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040043 SkTileMode::kClamp));
reed@google.coma4f81372012-11-15 15:56:38 +000044}
45
Hal Canarydf2d27e2019-01-08 09:38:02 -050046typedef void (*Proc)(SkCanvas*, const SkPaint&, const SkFont&);
reed@google.com5dd85a42012-11-15 13:46:47 +000047
Hal Canarydf2d27e2019-01-08 09:38:02 -050048static void draw_hair(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
reed@google.com5dd85a42012-11-15 13:46:47 +000049 SkPaint p(paint);
50 p.setStrokeWidth(0);
51 canvas->drawLine(0, 0, W, H, p);
52}
53
Hal Canarydf2d27e2019-01-08 09:38:02 -050054static void draw_thick(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
reed@google.com5dd85a42012-11-15 13:46:47 +000055 SkPaint p(paint);
56 p.setStrokeWidth(H/5);
57 canvas->drawLine(0, 0, W, H, p);
58}
59
Hal Canarydf2d27e2019-01-08 09:38:02 -050060static void draw_rect(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
reed@google.com5dd85a42012-11-15 13:46:47 +000061 canvas->drawRect(SkRect::MakeWH(W, H), paint);
62}
63
Hal Canarydf2d27e2019-01-08 09:38:02 -050064static void draw_oval(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
reed@google.com5dd85a42012-11-15 13:46:47 +000065 canvas->drawOval(SkRect::MakeWH(W, H), paint);
66}
67
Hal Canarydf2d27e2019-01-08 09:38:02 -050068static void draw_text(SkCanvas* canvas, const SkPaint& paint, const SkFont& font) {
69 canvas->drawString("Hamburge", 0, H*2/3, font, paint);
reed@google.com5dd85a42012-11-15 13:46:47 +000070}
71
72class SrcModeGM : public skiagm::GM {
73 SkPath fPath;
74public:
75 SrcModeGM() {
reed@google.com86eca472012-11-15 17:24:23 +000076 this->setBGColor(SK_ColorBLACK);
reed@google.com5dd85a42012-11-15 13:46:47 +000077 }
78
79protected:
80 virtual SkString onShortName() {
81 return SkString("srcmode");
82 }
83
84 virtual SkISize onISize() {
reed@google.coma4f81372012-11-15 15:56:38 +000085 return SkISize::Make(640, 760);
reed@google.com5dd85a42012-11-15 13:46:47 +000086 }
87
reed@google.com86eca472012-11-15 17:24:23 +000088 void drawContent(SkCanvas* canvas) {
reed@google.com5dd85a42012-11-15 13:46:47 +000089 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
90
91 SkPaint paint;
Mike Kleinea3f0142019-03-20 11:12:10 -050092 SkFont font(ToolUtils::create_portable_typeface(), H / 4);
caryclark97a26d02015-07-17 13:20:48 -070093 paint.setColor(0x80F60000);
reed@google.com5dd85a42012-11-15 13:46:47 +000094
95 const Proc procs[] = {
96 draw_hair, draw_thick, draw_rect, draw_oval, draw_text
97 };
98
reed374772b2016-10-05 17:33:02 -070099 const SkBlendMode modes[] = {
100 SkBlendMode::kSrcOver, SkBlendMode::kSrc, SkBlendMode::kClear
reed@google.com5dd85a42012-11-15 13:46:47 +0000101 };
102
reed@google.coma4f81372012-11-15 15:56:38 +0000103 const PaintProc paintProcs[] = {
104 identity_paintproc, gradient_paintproc
105 };
106
107 for (int aa = 0; aa <= 1; ++aa) {
108 paint.setAntiAlias(SkToBool(aa));
Hal Canarydf2d27e2019-01-08 09:38:02 -0500109 font.setEdging(SkToBool(aa) ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
reed@google.com5dd85a42012-11-15 13:46:47 +0000110 canvas->save();
reed@google.coma4f81372012-11-15 15:56:38 +0000111 for (size_t i = 0; i < SK_ARRAY_COUNT(paintProcs); ++i) {
112 paintProcs[i](&paint);
113 for (size_t x = 0; x < SK_ARRAY_COUNT(modes); ++x) {
reed374772b2016-10-05 17:33:02 -0700114 paint.setBlendMode(modes[x]);
reed@google.coma4f81372012-11-15 15:56:38 +0000115 canvas->save();
116 for (size_t y = 0; y < SK_ARRAY_COUNT(procs); ++y) {
Hal Canarydf2d27e2019-01-08 09:38:02 -0500117 procs[y](canvas, paint, font);
reed@google.coma4f81372012-11-15 15:56:38 +0000118 canvas->translate(0, H * 5 / 4);
119 }
120 canvas->restore();
121 canvas->translate(W * 5 / 4, 0);
122 }
reed@google.com5dd85a42012-11-15 13:46:47 +0000123 }
124 canvas->restore();
reed@google.coma4f81372012-11-15 15:56:38 +0000125 canvas->translate(0, (H * 5 / 4) * SK_ARRAY_COUNT(procs));
reed@google.com5dd85a42012-11-15 13:46:47 +0000126 }
127 }
128
reede8f30622016-03-23 18:59:25 -0700129 static sk_sp<SkSurface> compat_surface(SkCanvas* canvas, const SkISize& size, bool skipGPU) {
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000130 SkImageInfo info = SkImageInfo::MakeN32Premul(size);
reed52d9ac62014-06-30 09:05:34 -0700131
132 bool callNewSurface = true;
reed52d9ac62014-06-30 09:05:34 -0700133 if (canvas->getGrContext() && skipGPU) {
134 callNewSurface = false;
mike@reedtribe.org92aa7562012-11-16 02:23:10 +0000135 }
reede8f30622016-03-23 18:59:25 -0700136 sk_sp<SkSurface> surface = callNewSurface ? canvas->makeSurface(info) : nullptr;
halcanary96fcdcc2015-08-27 07:41:13 -0700137 if (nullptr == surface) {
Cary Clarka24712e2018-09-05 18:41:40 +0000138 // picture canvas will return null, so fall-back to raster
reede8f30622016-03-23 18:59:25 -0700139 surface = SkSurface::MakeRaster(info);
reed52d9ac62014-06-30 09:05:34 -0700140 }
141 return surface;
reed@google.com86eca472012-11-15 17:24:23 +0000142 }
143
144 virtual void onDraw(SkCanvas* canvas) {
reede8f30622016-03-23 18:59:25 -0700145 auto surf(compat_surface(canvas, this->getISize(), this->isCanvasDeferred()));
reed@google.com86eca472012-11-15 17:24:23 +0000146 surf->getCanvas()->drawColor(SK_ColorWHITE);
147 this->drawContent(surf->getCanvas());
halcanary96fcdcc2015-08-27 07:41:13 -0700148 surf->draw(canvas, 0, 0, nullptr);
reed@google.com86eca472012-11-15 17:24:23 +0000149 }
150
reed@google.com5dd85a42012-11-15 13:46:47 +0000151private:
152 typedef skiagm::GM INHERITED;
153};
154
155///////////////////////////////////////////////////////////////////////////////
156
157DEF_GM(return new SrcModeGM;)