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