blob: 7a7e89e0525684609751576533ee13854ec76c3e [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) {
24 paint->setShader(NULL);
25}
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 } };
30 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL,
31 SK_ARRAY_COUNT(colors),
32 SkShader::kClamp_TileMode);
33 paint->setShader(s)->unref();
34}
35
reed@google.com5dd85a42012-11-15 13:46:47 +000036typedef void (*Proc)(SkCanvas*, const SkPaint&);
37
38static void draw_hair(SkCanvas* canvas, const SkPaint& paint) {
39 SkPaint p(paint);
40 p.setStrokeWidth(0);
41 canvas->drawLine(0, 0, W, H, p);
42}
43
44static void draw_thick(SkCanvas* canvas, const SkPaint& paint) {
45 SkPaint p(paint);
46 p.setStrokeWidth(H/5);
47 canvas->drawLine(0, 0, W, H, p);
48}
49
50static void draw_rect(SkCanvas* canvas, const SkPaint& paint) {
51 canvas->drawRect(SkRect::MakeWH(W, H), paint);
52}
53
54static void draw_oval(SkCanvas* canvas, const SkPaint& paint) {
55 canvas->drawOval(SkRect::MakeWH(W, H), paint);
56}
57
58static void draw_text(SkCanvas* canvas, const SkPaint& paint) {
59 SkPaint p(paint);
60 p.setTextSize(H/4);
reed@google.coma4f81372012-11-15 15:56:38 +000061 canvas->drawText("Hamburge", 8, 0, H*2/3, p);
reed@google.com5dd85a42012-11-15 13:46:47 +000062}
63
64class SrcModeGM : public skiagm::GM {
65 SkPath fPath;
66public:
67 SrcModeGM() {
reed@google.com86eca472012-11-15 17:24:23 +000068 this->setBGColor(SK_ColorBLACK);
reed@google.com5dd85a42012-11-15 13:46:47 +000069 }
70
71protected:
72 virtual SkString onShortName() {
73 return SkString("srcmode");
74 }
75
76 virtual SkISize onISize() {
reed@google.coma4f81372012-11-15 15:56:38 +000077 return SkISize::Make(640, 760);
reed@google.com5dd85a42012-11-15 13:46:47 +000078 }
79
reed@google.com86eca472012-11-15 17:24:23 +000080 void drawContent(SkCanvas* canvas) {
reed@google.com5dd85a42012-11-15 13:46:47 +000081 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
82
83 SkPaint paint;
caryclark1818acb2015-07-24 12:09:25 -070084 sk_tool_utils::set_portable_typeface(&paint);
caryclark97a26d02015-07-17 13:20:48 -070085 paint.setColor(0x80F60000);
reed@google.com5dd85a42012-11-15 13:46:47 +000086
87 const Proc procs[] = {
88 draw_hair, draw_thick, draw_rect, draw_oval, draw_text
89 };
90
91 const SkXfermode::Mode modes[] = {
92 SkXfermode::kSrcOver_Mode, SkXfermode::kSrc_Mode, SkXfermode::kClear_Mode
93 };
94
reed@google.coma4f81372012-11-15 15:56:38 +000095 const PaintProc paintProcs[] = {
96 identity_paintproc, gradient_paintproc
97 };
98
99 for (int aa = 0; aa <= 1; ++aa) {
100 paint.setAntiAlias(SkToBool(aa));
reed@google.com5dd85a42012-11-15 13:46:47 +0000101 canvas->save();
reed@google.coma4f81372012-11-15 15:56:38 +0000102 for (size_t i = 0; i < SK_ARRAY_COUNT(paintProcs); ++i) {
103 paintProcs[i](&paint);
104 for (size_t x = 0; x < SK_ARRAY_COUNT(modes); ++x) {
105 paint.setXfermodeMode(modes[x]);
106 canvas->save();
107 for (size_t y = 0; y < SK_ARRAY_COUNT(procs); ++y) {
108 procs[y](canvas, paint);
109 canvas->translate(0, H * 5 / 4);
110 }
111 canvas->restore();
112 canvas->translate(W * 5 / 4, 0);
113 }
reed@google.com5dd85a42012-11-15 13:46:47 +0000114 }
115 canvas->restore();
reed@google.coma4f81372012-11-15 15:56:38 +0000116 canvas->translate(0, (H * 5 / 4) * SK_ARRAY_COUNT(procs));
reed@google.com5dd85a42012-11-15 13:46:47 +0000117 }
118 }
119
reed52d9ac62014-06-30 09:05:34 -0700120 static SkSurface* compat_surface(SkCanvas* canvas, const SkISize& size, bool skipGPU) {
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000121 SkImageInfo info = SkImageInfo::MakeN32Premul(size);
reed52d9ac62014-06-30 09:05:34 -0700122
123 bool callNewSurface = true;
mike@reedtribe.org15889742012-11-16 02:31:03 +0000124#if SK_SUPPORT_GPU
reed52d9ac62014-06-30 09:05:34 -0700125 if (canvas->getGrContext() && skipGPU) {
126 callNewSurface = false;
mike@reedtribe.org92aa7562012-11-16 02:23:10 +0000127 }
mike@reedtribe.org15889742012-11-16 02:31:03 +0000128#endif
reed52d9ac62014-06-30 09:05:34 -0700129 SkSurface* surface = callNewSurface ? canvas->newSurface(info) : NULL;
130 if (NULL == surface) {
131 // picture canvas will return null, so fall-back to raster
132 surface = SkSurface::NewRaster(info);
133 }
134 return surface;
reed@google.com86eca472012-11-15 17:24:23 +0000135 }
136
137 virtual void onDraw(SkCanvas* canvas) {
reed@google.comaef73612012-11-16 13:41:45 +0000138 SkAutoTUnref<SkSurface> surf(compat_surface(canvas, this->getISize(),
139 this->isCanvasDeferred()));
reed@google.com86eca472012-11-15 17:24:23 +0000140 surf->getCanvas()->drawColor(SK_ColorWHITE);
141 this->drawContent(surf->getCanvas());
142 surf->draw(canvas, 0, 0, NULL);
reed@google.com86eca472012-11-15 17:24:23 +0000143 }
144
reed@google.com5dd85a42012-11-15 13:46:47 +0000145private:
146 typedef skiagm::GM INHERITED;
147};
148
149///////////////////////////////////////////////////////////////////////////////
150
151DEF_GM(return new SrcModeGM;)