blob: 75721f1de41f2fd0317970d20d20731cefb8b48e [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
reed@google.com5dd85a42012-11-15 13:46:47 +000031typedef void (*Proc)(SkCanvas*, const SkPaint&);
32
33static void draw_hair(SkCanvas* canvas, const SkPaint& paint) {
34 SkPaint p(paint);
35 p.setStrokeWidth(0);
36 canvas->drawLine(0, 0, W, H, p);
37}
38
39static void draw_thick(SkCanvas* canvas, const SkPaint& paint) {
40 SkPaint p(paint);
41 p.setStrokeWidth(H/5);
42 canvas->drawLine(0, 0, W, H, p);
43}
44
45static void draw_rect(SkCanvas* canvas, const SkPaint& paint) {
46 canvas->drawRect(SkRect::MakeWH(W, H), paint);
47}
48
49static void draw_oval(SkCanvas* canvas, const SkPaint& paint) {
50 canvas->drawOval(SkRect::MakeWH(W, H), paint);
51}
52
53static void draw_text(SkCanvas* canvas, const SkPaint& paint) {
54 SkPaint p(paint);
55 p.setTextSize(H/4);
reed@google.coma4f81372012-11-15 15:56:38 +000056 canvas->drawText("Hamburge", 8, 0, H*2/3, p);
reed@google.com5dd85a42012-11-15 13:46:47 +000057}
58
59class SrcModeGM : public skiagm::GM {
60 SkPath fPath;
61public:
62 SrcModeGM() {
reed@google.com86eca472012-11-15 17:24:23 +000063 this->setBGColor(SK_ColorBLACK);
reed@google.com5dd85a42012-11-15 13:46:47 +000064 }
65
66protected:
67 virtual SkString onShortName() {
68 return SkString("srcmode");
69 }
70
71 virtual SkISize onISize() {
reed@google.coma4f81372012-11-15 15:56:38 +000072 return SkISize::Make(640, 760);
reed@google.com5dd85a42012-11-15 13:46:47 +000073 }
74
reed@google.com86eca472012-11-15 17:24:23 +000075 void drawContent(SkCanvas* canvas) {
reed@google.com5dd85a42012-11-15 13:46:47 +000076 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
77
78 SkPaint paint;
caryclark1818acb2015-07-24 12:09:25 -070079 sk_tool_utils::set_portable_typeface(&paint);
caryclark97a26d02015-07-17 13:20:48 -070080 paint.setColor(0x80F60000);
reed@google.com5dd85a42012-11-15 13:46:47 +000081
82 const Proc procs[] = {
83 draw_hair, draw_thick, draw_rect, draw_oval, draw_text
84 };
85
reed374772b2016-10-05 17:33:02 -070086 const SkBlendMode modes[] = {
87 SkBlendMode::kSrcOver, SkBlendMode::kSrc, SkBlendMode::kClear
reed@google.com5dd85a42012-11-15 13:46:47 +000088 };
89
reed@google.coma4f81372012-11-15 15:56:38 +000090 const PaintProc paintProcs[] = {
91 identity_paintproc, gradient_paintproc
92 };
93
94 for (int aa = 0; aa <= 1; ++aa) {
95 paint.setAntiAlias(SkToBool(aa));
reed@google.com5dd85a42012-11-15 13:46:47 +000096 canvas->save();
reed@google.coma4f81372012-11-15 15:56:38 +000097 for (size_t i = 0; i < SK_ARRAY_COUNT(paintProcs); ++i) {
98 paintProcs[i](&paint);
99 for (size_t x = 0; x < SK_ARRAY_COUNT(modes); ++x) {
reed374772b2016-10-05 17:33:02 -0700100 paint.setBlendMode(modes[x]);
reed@google.coma4f81372012-11-15 15:56:38 +0000101 canvas->save();
102 for (size_t y = 0; y < SK_ARRAY_COUNT(procs); ++y) {
103 procs[y](canvas, paint);
104 canvas->translate(0, H * 5 / 4);
105 }
106 canvas->restore();
107 canvas->translate(W * 5 / 4, 0);
108 }
reed@google.com5dd85a42012-11-15 13:46:47 +0000109 }
110 canvas->restore();
reed@google.coma4f81372012-11-15 15:56:38 +0000111 canvas->translate(0, (H * 5 / 4) * SK_ARRAY_COUNT(procs));
reed@google.com5dd85a42012-11-15 13:46:47 +0000112 }
113 }
114
reede8f30622016-03-23 18:59:25 -0700115 static sk_sp<SkSurface> compat_surface(SkCanvas* canvas, const SkISize& size, bool skipGPU) {
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000116 SkImageInfo info = SkImageInfo::MakeN32Premul(size);
reed52d9ac62014-06-30 09:05:34 -0700117
118 bool callNewSurface = true;
mike@reedtribe.org15889742012-11-16 02:31:03 +0000119#if SK_SUPPORT_GPU
reed52d9ac62014-06-30 09:05:34 -0700120 if (canvas->getGrContext() && skipGPU) {
121 callNewSurface = false;
mike@reedtribe.org92aa7562012-11-16 02:23:10 +0000122 }
mike@reedtribe.org15889742012-11-16 02:31:03 +0000123#endif
reede8f30622016-03-23 18:59:25 -0700124 sk_sp<SkSurface> surface = callNewSurface ? canvas->makeSurface(info) : nullptr;
halcanary96fcdcc2015-08-27 07:41:13 -0700125 if (nullptr == surface) {
reed52d9ac62014-06-30 09:05:34 -0700126 // picture canvas will return null, so fall-back to raster
reede8f30622016-03-23 18:59:25 -0700127 surface = SkSurface::MakeRaster(info);
reed52d9ac62014-06-30 09:05:34 -0700128 }
129 return surface;
reed@google.com86eca472012-11-15 17:24:23 +0000130 }
131
132 virtual void onDraw(SkCanvas* canvas) {
reede8f30622016-03-23 18:59:25 -0700133 auto surf(compat_surface(canvas, this->getISize(), this->isCanvasDeferred()));
reed@google.com86eca472012-11-15 17:24:23 +0000134 surf->getCanvas()->drawColor(SK_ColorWHITE);
135 this->drawContent(surf->getCanvas());
halcanary96fcdcc2015-08-27 07:41:13 -0700136 surf->draw(canvas, 0, 0, nullptr);
reed@google.com86eca472012-11-15 17:24:23 +0000137 }
138
reed@google.com5dd85a42012-11-15 13:46:47 +0000139private:
140 typedef skiagm::GM INHERITED;
141};
142
143///////////////////////////////////////////////////////////////////////////////
144
145DEF_GM(return new SrcModeGM;)