blob: 5ac102e63ecc4060da650fc3287932061245dad0 [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.com5dd85a42012-11-15 13:46:47 +000011
reed@google.coma4f81372012-11-15 15:56:38 +000012#define W SkIntToScalar(80)
reed@google.com5dd85a42012-11-15 13:46:47 +000013#define H SkIntToScalar(60)
14
reed@google.coma4f81372012-11-15 15:56:38 +000015typedef void (*PaintProc)(SkPaint*);
16
17static void identity_paintproc(SkPaint* paint) {}
18static void gradient_paintproc(SkPaint* paint) {
19 const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE };
20 const SkPoint pts[] = { { 0, 0 }, { W, H } };
21 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL,
22 SK_ARRAY_COUNT(colors),
23 SkShader::kClamp_TileMode);
24 paint->setShader(s)->unref();
25}
26
reed@google.com5dd85a42012-11-15 13:46:47 +000027typedef void (*Proc)(SkCanvas*, const SkPaint&);
28
29static void draw_hair(SkCanvas* canvas, const SkPaint& paint) {
30 SkPaint p(paint);
31 p.setStrokeWidth(0);
32 canvas->drawLine(0, 0, W, H, p);
33}
34
35static void draw_thick(SkCanvas* canvas, const SkPaint& paint) {
36 SkPaint p(paint);
37 p.setStrokeWidth(H/5);
38 canvas->drawLine(0, 0, W, H, p);
39}
40
41static void draw_rect(SkCanvas* canvas, const SkPaint& paint) {
42 canvas->drawRect(SkRect::MakeWH(W, H), paint);
43}
44
45static void draw_oval(SkCanvas* canvas, const SkPaint& paint) {
46 canvas->drawOval(SkRect::MakeWH(W, H), paint);
47}
48
49static void draw_text(SkCanvas* canvas, const SkPaint& paint) {
50 SkPaint p(paint);
51 p.setTextSize(H/4);
reed@google.coma4f81372012-11-15 15:56:38 +000052 canvas->drawText("Hamburge", 8, 0, H*2/3, p);
reed@google.com5dd85a42012-11-15 13:46:47 +000053}
54
55class SrcModeGM : public skiagm::GM {
56 SkPath fPath;
57public:
58 SrcModeGM() {
59 this->setBGColor(0xFFDDDDDD);
60 }
61
62protected:
63 virtual SkString onShortName() {
64 return SkString("srcmode");
65 }
66
67 virtual SkISize onISize() {
reed@google.coma4f81372012-11-15 15:56:38 +000068 return SkISize::Make(640, 760);
reed@google.com5dd85a42012-11-15 13:46:47 +000069 }
70
71 virtual void onDraw(SkCanvas* canvas) {
72 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
73
74 SkPaint paint;
reed@google.com5dd85a42012-11-15 13:46:47 +000075 paint.setColor(0x80FF0000);
76
77 const Proc procs[] = {
78 draw_hair, draw_thick, draw_rect, draw_oval, draw_text
79 };
80
81 const SkXfermode::Mode modes[] = {
82 SkXfermode::kSrcOver_Mode, SkXfermode::kSrc_Mode, SkXfermode::kClear_Mode
83 };
84
reed@google.coma4f81372012-11-15 15:56:38 +000085 const PaintProc paintProcs[] = {
86 identity_paintproc, gradient_paintproc
87 };
88
89 for (int aa = 0; aa <= 1; ++aa) {
90 paint.setAntiAlias(SkToBool(aa));
reed@google.com5dd85a42012-11-15 13:46:47 +000091 canvas->save();
reed@google.coma4f81372012-11-15 15:56:38 +000092 for (size_t i = 0; i < SK_ARRAY_COUNT(paintProcs); ++i) {
93 paintProcs[i](&paint);
94 for (size_t x = 0; x < SK_ARRAY_COUNT(modes); ++x) {
95 paint.setXfermodeMode(modes[x]);
96 canvas->save();
97 for (size_t y = 0; y < SK_ARRAY_COUNT(procs); ++y) {
98 procs[y](canvas, paint);
99 canvas->translate(0, H * 5 / 4);
100 }
101 canvas->restore();
102 canvas->translate(W * 5 / 4, 0);
103 }
reed@google.com5dd85a42012-11-15 13:46:47 +0000104 }
105 canvas->restore();
reed@google.coma4f81372012-11-15 15:56:38 +0000106 canvas->translate(0, (H * 5 / 4) * SK_ARRAY_COUNT(procs));
reed@google.com5dd85a42012-11-15 13:46:47 +0000107 }
108 }
109
110private:
111 typedef skiagm::GM INHERITED;
112};
113
114///////////////////////////////////////////////////////////////////////////////
115
116DEF_GM(return new SrcModeGM;)
117