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 "sk_tool_utils.h"
10 #include "SkCanvas.h"
11 #include "SkGradientShader.h"
12 #include "SkPath.h"
13 #include "SkSurface.h"
14 
15 #define W   SkIntToScalar(80)
16 #define H   SkIntToScalar(60)
17 
18 typedef void (*PaintProc)(SkPaint*);
19 
identity_paintproc(SkPaint * paint)20 static void identity_paintproc(SkPaint* paint) {
21     paint->setShader(nullptr);
22 }
23 
gradient_paintproc(SkPaint * paint)24 static void gradient_paintproc(SkPaint* paint) {
25     const SkColor colors[] = { SK_ColorGREEN, SK_ColorBLUE };
26     const SkPoint pts[] = { { 0, 0 }, { W, H } };
27     paint->setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
28                                                   SkShader::kClamp_TileMode));
29 }
30 
31 typedef void (*Proc)(SkCanvas*, const SkPaint&, const SkFont&);
32 
draw_hair(SkCanvas * canvas,const SkPaint & paint,const SkFont &)33 static void draw_hair(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
34     SkPaint p(paint);
35     p.setStrokeWidth(0);
36     canvas->drawLine(0, 0, W, H, p);
37 }
38 
draw_thick(SkCanvas * canvas,const SkPaint & paint,const SkFont &)39 static void draw_thick(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
40     SkPaint p(paint);
41     p.setStrokeWidth(H/5);
42     canvas->drawLine(0, 0, W, H, p);
43 }
44 
draw_rect(SkCanvas * canvas,const SkPaint & paint,const SkFont &)45 static void draw_rect(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
46     canvas->drawRect(SkRect::MakeWH(W, H), paint);
47 }
48 
draw_oval(SkCanvas * canvas,const SkPaint & paint,const SkFont &)49 static void draw_oval(SkCanvas* canvas, const SkPaint& paint, const SkFont&) {
50     canvas->drawOval(SkRect::MakeWH(W, H), paint);
51 }
52 
draw_text(SkCanvas * canvas,const SkPaint & paint,const SkFont & font)53 static void draw_text(SkCanvas* canvas, const SkPaint& paint, const SkFont& font) {
54     canvas->drawString("Hamburge", 0, H*2/3, font, paint);
55 }
56 
57 class SrcModeGM : public skiagm::GM {
58     SkPath fPath;
59 public:
SrcModeGM()60     SrcModeGM() {
61         this->setBGColor(SK_ColorBLACK);
62     }
63 
64 protected:
onShortName()65     virtual SkString onShortName() {
66         return SkString("srcmode");
67     }
68 
onISize()69     virtual SkISize onISize() {
70         return SkISize::Make(640, 760);
71     }
72 
drawContent(SkCanvas * canvas)73     void drawContent(SkCanvas* canvas) {
74         canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
75 
76         SkPaint paint;
77         SkFont font(sk_tool_utils::create_portable_typeface(), H/4);
78         paint.setColor(0x80F60000);
79 
80         const Proc procs[] = {
81             draw_hair, draw_thick, draw_rect, draw_oval, draw_text
82         };
83 
84         const SkBlendMode modes[] = {
85             SkBlendMode::kSrcOver, SkBlendMode::kSrc, SkBlendMode::kClear
86         };
87 
88         const PaintProc paintProcs[] = {
89             identity_paintproc, gradient_paintproc
90         };
91 
92         for (int aa = 0; aa <= 1; ++aa) {
93             paint.setAntiAlias(SkToBool(aa));
94             font.setEdging(SkToBool(aa) ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
95             canvas->save();
96             for (size_t i = 0; i < SK_ARRAY_COUNT(paintProcs); ++i) {
97                 paintProcs[i](&paint);
98                 for (size_t x = 0; x < SK_ARRAY_COUNT(modes); ++x) {
99                     paint.setBlendMode(modes[x]);
100                     canvas->save();
101                     for (size_t y = 0; y < SK_ARRAY_COUNT(procs); ++y) {
102                         procs[y](canvas, paint, font);
103                         canvas->translate(0, H * 5 / 4);
104                     }
105                     canvas->restore();
106                     canvas->translate(W * 5 / 4, 0);
107                 }
108             }
109             canvas->restore();
110             canvas->translate(0, (H * 5 / 4) * SK_ARRAY_COUNT(procs));
111         }
112     }
113 
compat_surface(SkCanvas * canvas,const SkISize & size,bool skipGPU)114     static sk_sp<SkSurface> compat_surface(SkCanvas* canvas, const SkISize& size, bool skipGPU) {
115         SkImageInfo info = SkImageInfo::MakeN32Premul(size);
116 
117         bool callNewSurface = true;
118         if (canvas->getGrContext() && skipGPU) {
119             callNewSurface = false;
120         }
121         sk_sp<SkSurface> surface = callNewSurface ? canvas->makeSurface(info) : nullptr;
122         if (nullptr == surface) {
123             // picture canvas will return null, so fall-back to raster
124             surface = SkSurface::MakeRaster(info);
125         }
126         return surface;
127     }
128 
onDraw(SkCanvas * canvas)129     virtual void onDraw(SkCanvas* canvas) {
130         auto surf(compat_surface(canvas, this->getISize(), this->isCanvasDeferred()));
131         surf->getCanvas()->drawColor(SK_ColorWHITE);
132         this->drawContent(surf->getCanvas());
133         surf->draw(canvas, 0, 0, nullptr);
134     }
135 
136 private:
137     typedef skiagm::GM INHERITED;
138 };
139 
140 ///////////////////////////////////////////////////////////////////////////////
141 
142 DEF_GM(return new SrcModeGM;)
143