blob: 28bb04bd4eab70e8ab30a5c623ec448f3a87017c [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"
9#include "SkCanvas.h"
reed@google.coma4f81372012-11-15 15:56:38 +000010#include "SkGradientShader.h"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.h"
reed@google.com86eca472012-11-15 17:24:23 +000012#include "SkSurface.h"
reed@google.com5dd85a42012-11-15 13:46:47 +000013
mike@reedtribe.org15889742012-11-16 02:31:03 +000014#if SK_SUPPORT_GPU
15 #include "SkGpuDevice.h"
16#endif
mike@reedtribe.org92aa7562012-11-16 02:23:10 +000017
reed@google.coma4f81372012-11-15 15:56:38 +000018#define W SkIntToScalar(80)
reed@google.com5dd85a42012-11-15 13:46:47 +000019#define H SkIntToScalar(60)
20
reed@google.coma4f81372012-11-15 15:56:38 +000021typedef void (*PaintProc)(SkPaint*);
22
mike@reedtribe.org92aa7562012-11-16 02:23:10 +000023static void identity_paintproc(SkPaint* paint) {
halcanary96fcdcc2015-08-27 07:41:13 -070024 paint->setShader(nullptr);
mike@reedtribe.org92aa7562012-11-16 02:23:10 +000025}
26
reed@google.coma4f81372012-11-15 15:56:38 +000027static void gradient_paintproc(SkPaint* paint) {
28 const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE };
29 const SkPoint pts[] = { { 0, 0 }, { W, H } };
reed1a9b9642016-03-13 14:13:58 -070030 paint->setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
31 SkShader::kClamp_TileMode));
reed@google.coma4f81372012-11-15 15:56:38 +000032}
33
reed@google.com5dd85a42012-11-15 13:46:47 +000034typedef void (*Proc)(SkCanvas*, const SkPaint&);
35
36static void draw_hair(SkCanvas* canvas, const SkPaint& paint) {
37 SkPaint p(paint);
38 p.setStrokeWidth(0);
39 canvas->drawLine(0, 0, W, H, p);
40}
41
42static void draw_thick(SkCanvas* canvas, const SkPaint& paint) {
43 SkPaint p(paint);
44 p.setStrokeWidth(H/5);
45 canvas->drawLine(0, 0, W, H, p);
46}
47
48static void draw_rect(SkCanvas* canvas, const SkPaint& paint) {
49 canvas->drawRect(SkRect::MakeWH(W, H), paint);
50}
51
52static void draw_oval(SkCanvas* canvas, const SkPaint& paint) {
53 canvas->drawOval(SkRect::MakeWH(W, H), paint);
54}
55
56static void draw_text(SkCanvas* canvas, const SkPaint& paint) {
57 SkPaint p(paint);
58 p.setTextSize(H/4);
reed@google.coma4f81372012-11-15 15:56:38 +000059 canvas->drawText("Hamburge", 8, 0, H*2/3, p);
reed@google.com5dd85a42012-11-15 13:46:47 +000060}
61
62class SrcModeGM : public skiagm::GM {
63 SkPath fPath;
64public:
65 SrcModeGM() {
reed@google.com86eca472012-11-15 17:24:23 +000066 this->setBGColor(SK_ColorBLACK);
reed@google.com5dd85a42012-11-15 13:46:47 +000067 }
68
69protected:
70 virtual SkString onShortName() {
71 return SkString("srcmode");
72 }
73
74 virtual SkISize onISize() {
reed@google.coma4f81372012-11-15 15:56:38 +000075 return SkISize::Make(640, 760);
reed@google.com5dd85a42012-11-15 13:46:47 +000076 }
77
reed@google.com86eca472012-11-15 17:24:23 +000078 void drawContent(SkCanvas* canvas) {
reed@google.com5dd85a42012-11-15 13:46:47 +000079 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
80
81 SkPaint paint;
caryclark1818acb2015-07-24 12:09:25 -070082 sk_tool_utils::set_portable_typeface(&paint);
caryclark97a26d02015-07-17 13:20:48 -070083 paint.setColor(0x80F60000);
reed@google.com5dd85a42012-11-15 13:46:47 +000084
85 const Proc procs[] = {
86 draw_hair, draw_thick, draw_rect, draw_oval, draw_text
87 };
88
89 const SkXfermode::Mode modes[] = {
90 SkXfermode::kSrcOver_Mode, SkXfermode::kSrc_Mode, SkXfermode::kClear_Mode
91 };
92
reed@google.coma4f81372012-11-15 15:56:38 +000093 const PaintProc paintProcs[] = {
94 identity_paintproc, gradient_paintproc
95 };
96
97 for (int aa = 0; aa <= 1; ++aa) {
98 paint.setAntiAlias(SkToBool(aa));
reed@google.com5dd85a42012-11-15 13:46:47 +000099 canvas->save();
reed@google.coma4f81372012-11-15 15:56:38 +0000100 for (size_t i = 0; i < SK_ARRAY_COUNT(paintProcs); ++i) {
101 paintProcs[i](&paint);
102 for (size_t x = 0; x < SK_ARRAY_COUNT(modes); ++x) {
103 paint.setXfermodeMode(modes[x]);
104 canvas->save();
105 for (size_t y = 0; y < SK_ARRAY_COUNT(procs); ++y) {
106 procs[y](canvas, paint);
107 canvas->translate(0, H * 5 / 4);
108 }
109 canvas->restore();
110 canvas->translate(W * 5 / 4, 0);
111 }
reed@google.com5dd85a42012-11-15 13:46:47 +0000112 }
113 canvas->restore();
reed@google.coma4f81372012-11-15 15:56:38 +0000114 canvas->translate(0, (H * 5 / 4) * SK_ARRAY_COUNT(procs));
reed@google.com5dd85a42012-11-15 13:46:47 +0000115 }
116 }
117
reed52d9ac62014-06-30 09:05:34 -0700118 static SkSurface* compat_surface(SkCanvas* canvas, const SkISize& size, bool skipGPU) {
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000119 SkImageInfo info = SkImageInfo::MakeN32Premul(size);
reed52d9ac62014-06-30 09:05:34 -0700120
121 bool callNewSurface = true;
mike@reedtribe.org15889742012-11-16 02:31:03 +0000122#if SK_SUPPORT_GPU
reed52d9ac62014-06-30 09:05:34 -0700123 if (canvas->getGrContext() && skipGPU) {
124 callNewSurface = false;
mike@reedtribe.org92aa7562012-11-16 02:23:10 +0000125 }
mike@reedtribe.org15889742012-11-16 02:31:03 +0000126#endif
halcanary96fcdcc2015-08-27 07:41:13 -0700127 SkSurface* surface = callNewSurface ? canvas->newSurface(info) : nullptr;
128 if (nullptr == surface) {
reed52d9ac62014-06-30 09:05:34 -0700129 // picture canvas will return null, so fall-back to raster
130 surface = SkSurface::NewRaster(info);
131 }
132 return surface;
reed@google.com86eca472012-11-15 17:24:23 +0000133 }
134
135 virtual void onDraw(SkCanvas* canvas) {
reed@google.comaef73612012-11-16 13:41:45 +0000136 SkAutoTUnref<SkSurface> surf(compat_surface(canvas, this->getISize(),
137 this->isCanvasDeferred()));
reed@google.com86eca472012-11-15 17:24:23 +0000138 surf->getCanvas()->drawColor(SK_ColorWHITE);
139 this->drawContent(surf->getCanvas());
halcanary96fcdcc2015-08-27 07:41:13 -0700140 surf->draw(canvas, 0, 0, nullptr);
reed@google.com86eca472012-11-15 17:24:23 +0000141 }
142
reed@google.com5dd85a42012-11-15 13:46:47 +0000143private:
144 typedef skiagm::GM INHERITED;
145};
146
147///////////////////////////////////////////////////////////////////////////////
148
149DEF_GM(return new SrcModeGM;)