blob: 85a3561de2fe46bd0c419748d1d1d7ee78f4cfde [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"
reed@google.com86eca472012-11-15 17:24:23 +000011#include "SkSurface.h"
reed@google.com5dd85a42012-11-15 13:46:47 +000012
mike@reedtribe.org15889742012-11-16 02:31:03 +000013#if SK_SUPPORT_GPU
14 #include "SkGpuDevice.h"
15#endif
mike@reedtribe.org92aa7562012-11-16 02:23:10 +000016
reed@google.coma4f81372012-11-15 15:56:38 +000017#define W SkIntToScalar(80)
reed@google.com5dd85a42012-11-15 13:46:47 +000018#define H SkIntToScalar(60)
19
reed@google.coma4f81372012-11-15 15:56:38 +000020typedef void (*PaintProc)(SkPaint*);
21
mike@reedtribe.org92aa7562012-11-16 02:23:10 +000022static void identity_paintproc(SkPaint* paint) {
23 paint->setShader(NULL);
24}
25
reed@google.coma4f81372012-11-15 15:56:38 +000026static void gradient_paintproc(SkPaint* paint) {
27 const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE };
28 const SkPoint pts[] = { { 0, 0 }, { W, H } };
29 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL,
30 SK_ARRAY_COUNT(colors),
31 SkShader::kClamp_TileMode);
32 paint->setShader(s)->unref();
33}
34
reed@google.com5dd85a42012-11-15 13:46:47 +000035typedef void (*Proc)(SkCanvas*, const SkPaint&);
36
37static void draw_hair(SkCanvas* canvas, const SkPaint& paint) {
38 SkPaint p(paint);
39 p.setStrokeWidth(0);
40 canvas->drawLine(0, 0, W, H, p);
41}
42
43static void draw_thick(SkCanvas* canvas, const SkPaint& paint) {
44 SkPaint p(paint);
45 p.setStrokeWidth(H/5);
46 canvas->drawLine(0, 0, W, H, p);
47}
48
49static void draw_rect(SkCanvas* canvas, const SkPaint& paint) {
50 canvas->drawRect(SkRect::MakeWH(W, H), paint);
51}
52
53static void draw_oval(SkCanvas* canvas, const SkPaint& paint) {
54 canvas->drawOval(SkRect::MakeWH(W, H), paint);
55}
56
57static void draw_text(SkCanvas* canvas, const SkPaint& paint) {
58 SkPaint p(paint);
59 p.setTextSize(H/4);
reed@google.coma4f81372012-11-15 15:56:38 +000060 canvas->drawText("Hamburge", 8, 0, H*2/3, p);
reed@google.com5dd85a42012-11-15 13:46:47 +000061}
62
63class SrcModeGM : public skiagm::GM {
64 SkPath fPath;
65public:
66 SrcModeGM() {
reed@google.com86eca472012-11-15 17:24:23 +000067 this->setBGColor(SK_ColorBLACK);
reed@google.com5dd85a42012-11-15 13:46:47 +000068 }
69
70protected:
71 virtual SkString onShortName() {
72 return SkString("srcmode");
73 }
74
75 virtual SkISize onISize() {
reed@google.coma4f81372012-11-15 15:56:38 +000076 return SkISize::Make(640, 760);
reed@google.com5dd85a42012-11-15 13:46:47 +000077 }
78
reed@google.com86eca472012-11-15 17:24:23 +000079 void drawContent(SkCanvas* canvas) {
reed@google.com5dd85a42012-11-15 13:46:47 +000080 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
81
82 SkPaint paint;
Cary Clark992c7b02014-07-31 08:58:44 -040083 sk_tool_utils::set_portable_typeface(&paint);
reed@google.com5dd85a42012-11-15 13:46:47 +000084 paint.setColor(0x80FF0000);
85
86 const Proc procs[] = {
87 draw_hair, draw_thick, draw_rect, draw_oval, draw_text
88 };
89
90 const SkXfermode::Mode modes[] = {
91 SkXfermode::kSrcOver_Mode, SkXfermode::kSrc_Mode, SkXfermode::kClear_Mode
92 };
93
reed@google.coma4f81372012-11-15 15:56:38 +000094 const PaintProc paintProcs[] = {
95 identity_paintproc, gradient_paintproc
96 };
97
98 for (int aa = 0; aa <= 1; ++aa) {
99 paint.setAntiAlias(SkToBool(aa));
reed@google.com5dd85a42012-11-15 13:46:47 +0000100 canvas->save();
reed@google.coma4f81372012-11-15 15:56:38 +0000101 for (size_t i = 0; i < SK_ARRAY_COUNT(paintProcs); ++i) {
102 paintProcs[i](&paint);
103 for (size_t x = 0; x < SK_ARRAY_COUNT(modes); ++x) {
104 paint.setXfermodeMode(modes[x]);
105 canvas->save();
106 for (size_t y = 0; y < SK_ARRAY_COUNT(procs); ++y) {
107 procs[y](canvas, paint);
108 canvas->translate(0, H * 5 / 4);
109 }
110 canvas->restore();
111 canvas->translate(W * 5 / 4, 0);
112 }
reed@google.com5dd85a42012-11-15 13:46:47 +0000113 }
114 canvas->restore();
reed@google.coma4f81372012-11-15 15:56:38 +0000115 canvas->translate(0, (H * 5 / 4) * SK_ARRAY_COUNT(procs));
reed@google.com5dd85a42012-11-15 13:46:47 +0000116 }
117 }
118
reed52d9ac62014-06-30 09:05:34 -0700119 static SkSurface* compat_surface(SkCanvas* canvas, const SkISize& size, bool skipGPU) {
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000120 SkImageInfo info = SkImageInfo::MakeN32Premul(size);
reed52d9ac62014-06-30 09:05:34 -0700121
122 bool callNewSurface = true;
mike@reedtribe.org15889742012-11-16 02:31:03 +0000123#if SK_SUPPORT_GPU
reed52d9ac62014-06-30 09:05:34 -0700124 if (canvas->getGrContext() && skipGPU) {
125 callNewSurface = false;
mike@reedtribe.org92aa7562012-11-16 02:23:10 +0000126 }
mike@reedtribe.org15889742012-11-16 02:31:03 +0000127#endif
reed52d9ac62014-06-30 09:05:34 -0700128 SkSurface* surface = callNewSurface ? canvas->newSurface(info) : NULL;
129 if (NULL == surface) {
130 // picture canvas will return null, so fall-back to raster
131 surface = SkSurface::NewRaster(info);
132 }
133 return surface;
reed@google.com86eca472012-11-15 17:24:23 +0000134 }
135
136 virtual void onDraw(SkCanvas* canvas) {
reed@google.comaef73612012-11-16 13:41:45 +0000137 SkAutoTUnref<SkSurface> surf(compat_surface(canvas, this->getISize(),
138 this->isCanvasDeferred()));
reed@google.com86eca472012-11-15 17:24:23 +0000139 surf->getCanvas()->drawColor(SK_ColorWHITE);
140 this->drawContent(surf->getCanvas());
141 surf->draw(canvas, 0, 0, NULL);
reed@google.com86eca472012-11-15 17:24:23 +0000142 }
143
reed@google.com5dd85a42012-11-15 13:46:47 +0000144private:
145 typedef skiagm::GM INHERITED;
146};
147
148///////////////////////////////////////////////////////////////////////////////
149
150DEF_GM(return new SrcModeGM;)