• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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/SkPoint.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkShader.h"
20 #include "include/core/SkSize.h"
21 #include "include/core/SkString.h"
22 #include "include/core/SkSurface.h"
23 #include "include/core/SkTileMode.h"
24 #include "include/core/SkTypeface.h"
25 #include "include/core/SkTypes.h"
26 #include "include/effects/SkGradientShader.h"
27 #include "tools/ToolUtils.h"
28 
29 namespace skiagm {
30 
31 constexpr int kColWidth = 180;
32 constexpr int kNumCols = 4;
33 constexpr int kWidth = kColWidth * kNumCols;
34 constexpr int kHeight = 750;
35 
make_shader(const SkRect & bounds)36 static sk_sp<SkShader> make_shader(const SkRect& bounds) {
37     const SkPoint pts[] = {
38         { bounds.left(), bounds.top() },
39         { bounds.right(), bounds.bottom() },
40     };
41     const SkColor colors[] = {
42         SK_ColorRED, SK_ColorGREEN,
43     };
44     return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
45                                         SkTileMode::kRepeat);
46 }
47 
48 class LcdBlendGM : public skiagm::GM {
49 public:
LcdBlendGM()50     LcdBlendGM() {
51         const int kPointSize = 25;
52         fTextHeight = SkIntToScalar(kPointSize);
53     }
54 
55 protected:
onShortName()56     SkString onShortName() override {
57         return SkString("lcdblendmodes");
58     }
59 
onOnceBeforeDraw()60     void onOnceBeforeDraw() override {
61         fCheckerboard = ToolUtils::create_checkerboard_shader(SK_ColorBLACK, SK_ColorWHITE, 4);
62     }
63 
onISize()64     SkISize onISize() override { return SkISize::Make(kWidth, kHeight); }
65 
onDraw(SkCanvas * canvas)66     void onDraw(SkCanvas* canvas) override {
67         SkPaint p;
68         p.setAntiAlias(false);
69         p.setStyle(SkPaint::kFill_Style);
70         p.setShader(fCheckerboard);
71         SkRect r = SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
72         canvas->drawRect(r, p);
73 
74         SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
75         auto        surface(ToolUtils::makeSurface(canvas, info));
76 
77         SkCanvas* surfCanvas = surface->getCanvas();
78         this->drawColumn(surfCanvas, SK_ColorBLACK, SK_ColorWHITE, false);
79         surfCanvas->translate(SkIntToScalar(kColWidth), 0);
80         this->drawColumn(surfCanvas, SK_ColorWHITE, SK_ColorBLACK, false);
81         surfCanvas->translate(SkIntToScalar(kColWidth), 0);
82         this->drawColumn(surfCanvas, SK_ColorGREEN, SK_ColorMAGENTA, false);
83         surfCanvas->translate(SkIntToScalar(kColWidth), 0);
84         this->drawColumn(surfCanvas, SK_ColorCYAN, SK_ColorMAGENTA, true);
85 
86         SkPaint surfPaint;
87         surfPaint.setBlendMode(SkBlendMode::kSrcOver);
88         surface->draw(canvas, 0, 0, &surfPaint);
89     }
90 
drawColumn(SkCanvas * canvas,SkColor backgroundColor,SkColor textColor,bool useGrad)91     void drawColumn(SkCanvas* canvas, SkColor backgroundColor, SkColor textColor, bool useGrad) {
92         const SkBlendMode gModes[] = {
93             SkBlendMode::kClear,
94             SkBlendMode::kSrc,
95             SkBlendMode::kDst,
96             SkBlendMode::kSrcOver,
97             SkBlendMode::kDstOver,
98             SkBlendMode::kSrcIn,
99             SkBlendMode::kDstIn,
100             SkBlendMode::kSrcOut,
101             SkBlendMode::kDstOut,
102             SkBlendMode::kSrcATop,
103             SkBlendMode::kDstATop,
104             SkBlendMode::kXor,
105             SkBlendMode::kPlus,
106             SkBlendMode::kModulate,
107             SkBlendMode::kScreen,
108             SkBlendMode::kOverlay,
109             SkBlendMode::kDarken,
110             SkBlendMode::kLighten,
111             SkBlendMode::kColorDodge,
112             SkBlendMode::kColorBurn,
113             SkBlendMode::kHardLight,
114             SkBlendMode::kSoftLight,
115             SkBlendMode::kDifference,
116             SkBlendMode::kExclusion,
117             SkBlendMode::kMultiply,
118             SkBlendMode::kHue,
119             SkBlendMode::kSaturation,
120             SkBlendMode::kColor,
121             SkBlendMode::kLuminosity,
122         };
123         // Draw background rect
124         SkPaint backgroundPaint;
125         backgroundPaint.setColor(backgroundColor);
126         canvas->drawRect(SkRect::MakeIWH(kColWidth, kHeight), backgroundPaint);
127         SkScalar y = fTextHeight;
128         for (size_t m = 0; m < SK_ARRAY_COUNT(gModes); m++) {
129             SkPaint paint;
130             paint.setColor(textColor);
131             paint.setBlendMode(gModes[m]);
132             SkFont font(ToolUtils::create_portable_typeface(), fTextHeight);
133             font.setSubpixel(true);
134             font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
135             if (useGrad) {
136                 SkRect r;
137                 r.setXYWH(0, y - fTextHeight, SkIntToScalar(kColWidth), fTextHeight);
138                 paint.setShader(make_shader(r));
139             }
140             SkString string(SkBlendMode_Name(gModes[m]));
141             canvas->drawString(string, 0, y, font, paint);
142             y+=fTextHeight;
143         }
144     }
145 
146 private:
147     SkScalar fTextHeight;
148     sk_sp<SkShader> fCheckerboard;
149     typedef skiagm::GM INHERITED;
150 };
151 
152 //////////////////////////////////////////////////////////////////////////////
153 
154 DEF_GM( return new LcdBlendGM; )
155 }
156