draw offscreen so we can see the alpha-channel we are writing
[platform/upstream/libSkiaSharp.git] / gm / srcmode.cpp
1 /*
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"
10 #include "SkGradientShader.h"
11 #include "SkSurface.h"
12
13 #define W   SkIntToScalar(80)
14 #define H   SkIntToScalar(60)
15
16 typedef void (*PaintProc)(SkPaint*);
17
18 static void identity_paintproc(SkPaint* paint) {}
19 static void gradient_paintproc(SkPaint* paint) {
20     const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE };
21     const SkPoint pts[] = { { 0, 0 }, { W, H } };
22     SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL,
23                                                  SK_ARRAY_COUNT(colors),
24                                                  SkShader::kClamp_TileMode);
25     paint->setShader(s)->unref();
26 }
27
28 typedef void (*Proc)(SkCanvas*, const SkPaint&);
29
30 static void draw_hair(SkCanvas* canvas, const SkPaint& paint) {
31     SkPaint p(paint);
32     p.setStrokeWidth(0);
33     canvas->drawLine(0, 0, W, H, p);
34 }
35
36 static void draw_thick(SkCanvas* canvas, const SkPaint& paint) {
37     SkPaint p(paint);
38     p.setStrokeWidth(H/5);
39     canvas->drawLine(0, 0, W, H, p);
40 }
41
42 static void draw_rect(SkCanvas* canvas, const SkPaint& paint) {
43     canvas->drawRect(SkRect::MakeWH(W, H), paint);
44 }
45
46 static void draw_oval(SkCanvas* canvas, const SkPaint& paint) {
47     canvas->drawOval(SkRect::MakeWH(W, H), paint);
48 }
49
50 static void draw_text(SkCanvas* canvas, const SkPaint& paint) {
51     SkPaint p(paint);
52     p.setTextSize(H/4);
53     canvas->drawText("Hamburge", 8, 0, H*2/3, p);
54 }
55
56 class SrcModeGM : public skiagm::GM {
57     SkPath fPath;
58 public:
59     SrcModeGM() {
60         this->setBGColor(SK_ColorBLACK);
61     }
62
63 protected:
64     virtual SkString onShortName() {
65         return SkString("srcmode");
66     }
67
68     virtual SkISize onISize() {
69         return SkISize::Make(640, 760);
70     }
71
72     void drawContent(SkCanvas* canvas) {
73         canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
74
75         SkPaint paint;
76         paint.setColor(0x80FF0000);
77
78         const Proc procs[] = {
79             draw_hair, draw_thick, draw_rect, draw_oval, draw_text
80         };
81
82         const SkXfermode::Mode modes[] = {
83             SkXfermode::kSrcOver_Mode, SkXfermode::kSrc_Mode, SkXfermode::kClear_Mode
84         };
85
86         const PaintProc paintProcs[] = {
87             identity_paintproc, gradient_paintproc
88         };
89
90         for (int aa = 0; aa <= 1; ++aa) {
91             paint.setAntiAlias(SkToBool(aa));
92             canvas->save();
93             for (size_t i = 0; i < SK_ARRAY_COUNT(paintProcs); ++i) {
94                 paintProcs[i](&paint);
95                 for (size_t x = 0; x < SK_ARRAY_COUNT(modes); ++x) {
96                     paint.setXfermodeMode(modes[x]);
97                     canvas->save();
98                     for (size_t y = 0; y < SK_ARRAY_COUNT(procs); ++y) {
99                         procs[y](canvas, paint);
100                         canvas->translate(0, H * 5 / 4);
101                     }
102                     canvas->restore();
103                     canvas->translate(W * 5 / 4, 0);
104                 }
105             }
106             canvas->restore();
107             canvas->translate(0, (H * 5 / 4) * SK_ARRAY_COUNT(procs));
108         }
109     }
110
111     static SkSurface* compat_surface(SkCanvas* canvas, const SkISize& size) {
112         SkImage::Info info = {
113             size.width(),
114             size.height(),
115             SkImage::kPMColor_ColorType,
116             SkImage::kPremul_AlphaType
117         };
118         return SkSurface::NewRaster(info);
119     }
120
121     virtual void onDraw(SkCanvas* canvas) {
122         SkAutoTUnref<SkSurface> surf(compat_surface(canvas, this->getISize()));
123         surf->getCanvas()->drawColor(SK_ColorWHITE);
124         this->drawContent(surf->getCanvas());
125         surf->draw(canvas, 0, 0, NULL);
126         
127     }
128
129 private:
130     typedef skiagm::GM INHERITED;
131 };
132
133 ///////////////////////////////////////////////////////////////////////////////
134
135 DEF_GM(return new SrcModeGM;)
136