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