blob: f64b1d15aacb2dfd2392c5eeb1a0da3596285cd3 [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
8#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
reed@google.com5dd85a42012-11-15 13:46:47 +000010#include "SkCanvas.h"
reed@google.coma4f81372012-11-15 15:56:38 +000011#include "SkGradientShader.h"
bungemand3ebb482015-08-05 13:57:49 -070012#include "SkPath.h"
reed@google.com86eca472012-11-15 17:24:23 +000013#include "SkSurface.h"
reed@google.com5dd85a42012-11-15 13:46:47 +000014
reed@google.coma4f81372012-11-15 15:56:38 +000015#define W SkIntToScalar(80)
reed@google.com5dd85a42012-11-15 13:46:47 +000016#define H SkIntToScalar(60)
17
reed@google.coma4f81372012-11-15 15:56:38 +000018typedef void (*PaintProc)(SkPaint*);
19
mike@reedtribe.org92aa7562012-11-16 02:23:10 +000020static void identity_paintproc(SkPaint* paint) {
halcanary96fcdcc2015-08-27 07:41:13 -070021 paint->setShader(nullptr);
mike@reedtribe.org92aa7562012-11-16 02:23:10 +000022}
23
reed@google.coma4f81372012-11-15 15:56:38 +000024static void gradient_paintproc(SkPaint* paint) {
25 const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE };
26 const SkPoint pts[] = { { 0, 0 }, { W, H } };
reed1a9b9642016-03-13 14:13:58 -070027 paint->setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
28 SkShader::kClamp_TileMode));
reed@google.coma4f81372012-11-15 15:56:38 +000029}
30
Hal Canarydf2d27e2019-01-08 09:38:02 -050031typedef void (*Proc)(SkCanvas*, const SkPaint&, const SkFont&);
reed@google.com5dd85a42012-11-15 13:46:47 +000032
Hal Canarydf2d27e2019-01-08 09:38:02 -050033static void draw_hair(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
reed@google.com5dd85a42012-11-15 13:46:47 +000034 SkPaint p(paint);
35 p.setStrokeWidth(0);
36 canvas->drawLine(0, 0, W, H, p);
37}
38
Hal Canarydf2d27e2019-01-08 09:38:02 -050039static void draw_thick(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
reed@google.com5dd85a42012-11-15 13:46:47 +000040 SkPaint p(paint);
41 p.setStrokeWidth(H/5);
42 canvas->drawLine(0, 0, W, H, p);
43}
44
Hal Canarydf2d27e2019-01-08 09:38:02 -050045static void draw_rect(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
reed@google.com5dd85a42012-11-15 13:46:47 +000046 canvas->drawRect(SkRect::MakeWH(W, H), paint);
47}
48
Hal Canarydf2d27e2019-01-08 09:38:02 -050049static void draw_oval(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
reed@google.com5dd85a42012-11-15 13:46:47 +000050 canvas->drawOval(SkRect::MakeWH(W, H), paint);
51}
52
Hal Canarydf2d27e2019-01-08 09:38:02 -050053static void draw_text(SkCanvas* canvas, const SkPaint& paint, const SkFont& font) {
54 canvas->drawString("Hamburge", 0, H*2/3, font, paint);
reed@google.com5dd85a42012-11-15 13:46:47 +000055}
56
57class SrcModeGM : public skiagm::GM {
58 SkPath fPath;
59public:
60 SrcModeGM() {
reed@google.com86eca472012-11-15 17:24:23 +000061 this->setBGColor(SK_ColorBLACK);
reed@google.com5dd85a42012-11-15 13:46:47 +000062 }
63
64protected:
65 virtual SkString onShortName() {
66 return SkString("srcmode");
67 }
68
69 virtual SkISize onISize() {
reed@google.coma4f81372012-11-15 15:56:38 +000070 return SkISize::Make(640, 760);
reed@google.com5dd85a42012-11-15 13:46:47 +000071 }
72
reed@google.com86eca472012-11-15 17:24:23 +000073 void drawContent(SkCanvas* canvas) {
reed@google.com5dd85a42012-11-15 13:46:47 +000074 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
75
76 SkPaint paint;
Hal Canarydf2d27e2019-01-08 09:38:02 -050077 SkFont font(sk_tool_utils::create_portable_typeface(), H/4);
caryclark97a26d02015-07-17 13:20:48 -070078 paint.setColor(0x80F60000);
reed@google.com5dd85a42012-11-15 13:46:47 +000079
80 const Proc procs[] = {
81 draw_hair, draw_thick, draw_rect, draw_oval, draw_text
82 };
83
reed374772b2016-10-05 17:33:02 -070084 const SkBlendMode modes[] = {
85 SkBlendMode::kSrcOver, SkBlendMode::kSrc, SkBlendMode::kClear
reed@google.com5dd85a42012-11-15 13:46:47 +000086 };
87
reed@google.coma4f81372012-11-15 15:56:38 +000088 const PaintProc paintProcs[] = {
89 identity_paintproc, gradient_paintproc
90 };
91
92 for (int aa = 0; aa <= 1; ++aa) {
93 paint.setAntiAlias(SkToBool(aa));
Hal Canarydf2d27e2019-01-08 09:38:02 -050094 font.setEdging(SkToBool(aa) ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
reed@google.com5dd85a42012-11-15 13:46:47 +000095 canvas->save();
reed@google.coma4f81372012-11-15 15:56:38 +000096 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) {
reed374772b2016-10-05 17:33:02 -070099 paint.setBlendMode(modes[x]);
reed@google.coma4f81372012-11-15 15:56:38 +0000100 canvas->save();
101 for (size_t y = 0; y < SK_ARRAY_COUNT(procs); ++y) {
Hal Canarydf2d27e2019-01-08 09:38:02 -0500102 procs[y](canvas, paint, font);
reed@google.coma4f81372012-11-15 15:56:38 +0000103 canvas->translate(0, H * 5 / 4);
104 }
105 canvas->restore();
106 canvas->translate(W * 5 / 4, 0);
107 }
reed@google.com5dd85a42012-11-15 13:46:47 +0000108 }
109 canvas->restore();
reed@google.coma4f81372012-11-15 15:56:38 +0000110 canvas->translate(0, (H * 5 / 4) * SK_ARRAY_COUNT(procs));
reed@google.com5dd85a42012-11-15 13:46:47 +0000111 }
112 }
113
reede8f30622016-03-23 18:59:25 -0700114 static sk_sp<SkSurface> compat_surface(SkCanvas* canvas, const SkISize& size, bool skipGPU) {
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000115 SkImageInfo info = SkImageInfo::MakeN32Premul(size);
reed52d9ac62014-06-30 09:05:34 -0700116
117 bool callNewSurface = true;
reed52d9ac62014-06-30 09:05:34 -0700118 if (canvas->getGrContext() && skipGPU) {
119 callNewSurface = false;
mike@reedtribe.org92aa7562012-11-16 02:23:10 +0000120 }
reede8f30622016-03-23 18:59:25 -0700121 sk_sp<SkSurface> surface = callNewSurface ? canvas->makeSurface(info) : nullptr;
halcanary96fcdcc2015-08-27 07:41:13 -0700122 if (nullptr == surface) {
Cary Clarka24712e2018-09-05 18:41:40 +0000123 // picture canvas will return null, so fall-back to raster
reede8f30622016-03-23 18:59:25 -0700124 surface = SkSurface::MakeRaster(info);
reed52d9ac62014-06-30 09:05:34 -0700125 }
126 return surface;
reed@google.com86eca472012-11-15 17:24:23 +0000127 }
128
129 virtual void onDraw(SkCanvas* canvas) {
reede8f30622016-03-23 18:59:25 -0700130 auto surf(compat_surface(canvas, this->getISize(), this->isCanvasDeferred()));
reed@google.com86eca472012-11-15 17:24:23 +0000131 surf->getCanvas()->drawColor(SK_ColorWHITE);
132 this->drawContent(surf->getCanvas());
halcanary96fcdcc2015-08-27 07:41:13 -0700133 surf->draw(canvas, 0, 0, nullptr);
reed@google.com86eca472012-11-15 17:24:23 +0000134 }
135
reed@google.com5dd85a42012-11-15 13:46:47 +0000136private:
137 typedef skiagm::GM INHERITED;
138};
139
140///////////////////////////////////////////////////////////////////////////////
141
142DEF_GM(return new SrcModeGM;)