blob: 42359e3aef002834697c98966e313dcd1838ace8 [file] [log] [blame]
egdaniel723b0502015-09-15 09:31:40 -07001/*
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
halcanary9d524f22016-03-29 09:03:52 -07009/*
egdaniel723b0502015-09-15 09:31:40 -070010 * Tests text rendering with LCD and the various blend modes.
11 */
12
13#include "gm.h"
14#include "SkCanvas.h"
15#include "SkGradientShader.h"
egdaniel27b63352015-09-15 13:13:50 -070016#include "SkSurface.h"
egdaniel723b0502015-09-15 09:31:40 -070017
18namespace skiagm {
19
20static const int kColWidth = 180;
21static const int kNumCols = 4;
22static const int kWidth = kColWidth * kNumCols;
23static const int kHeight = 750;
24
reed1a9b9642016-03-13 14:13:58 -070025static sk_sp<SkShader> make_shader(const SkRect& bounds) {
egdaniel723b0502015-09-15 09:31:40 -070026 const SkPoint pts[] = {
27 { bounds.left(), bounds.top() },
28 { bounds.right(), bounds.bottom() },
29 };
30 const SkColor colors[] = {
31 SK_ColorRED, SK_ColorGREEN,
32 };
reed1a9b9642016-03-13 14:13:58 -070033 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
34 SkShader::kRepeat_TileMode);
egdaniel723b0502015-09-15 09:31:40 -070035}
36
37class LcdBlendGM : public skiagm::GM {
38public:
39 LcdBlendGM() {
40 const int kPointSize = 25;
41 fTextHeight = SkIntToScalar(kPointSize);
42 }
halcanary9d524f22016-03-29 09:03:52 -070043
egdaniel723b0502015-09-15 09:31:40 -070044protected:
45 SkString onShortName() override {
egdaniel27b63352015-09-15 13:13:50 -070046 return SkString("lcdblendmodes");
47 }
48
49 void onOnceBeforeDraw() override {
reed8a21c9f2016-03-08 18:50:00 -080050 fCheckerboard = sk_tool_utils::create_checkerboard_shader(SK_ColorBLACK, SK_ColorWHITE, 4);
egdaniel723b0502015-09-15 09:31:40 -070051 }
halcanary9d524f22016-03-29 09:03:52 -070052
egdaniel723b0502015-09-15 09:31:40 -070053 SkISize onISize() override { return SkISize::Make(kWidth, kHeight); }
halcanary9d524f22016-03-29 09:03:52 -070054
egdaniel723b0502015-09-15 09:31:40 -070055 void onDraw(SkCanvas* canvas) override {
egdaniel27b63352015-09-15 13:13:50 -070056 SkPaint p;
57 p.setAntiAlias(false);
58 p.setStyle(SkPaint::kFill_Style);
59 p.setShader(fCheckerboard);
60 SkRect r = SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
61 canvas->drawRect(r, p);
62
63 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
reede8f30622016-03-23 18:59:25 -070064 auto surface(canvas->makeSurface(info));
egdaniel27b63352015-09-15 13:13:50 -070065 if (nullptr == surface) {
reede8f30622016-03-23 18:59:25 -070066 surface = SkSurface::MakeRaster(info);
egdaniel27b63352015-09-15 13:13:50 -070067 }
68
69 SkCanvas* surfCanvas = surface->getCanvas();
70 this->drawColumn(surfCanvas, SK_ColorBLACK, SK_ColorWHITE, false);
71 surfCanvas->translate(SkIntToScalar(kColWidth), 0);
72 this->drawColumn(surfCanvas, SK_ColorWHITE, SK_ColorBLACK, false);
73 surfCanvas->translate(SkIntToScalar(kColWidth), 0);
74 this->drawColumn(surfCanvas, SK_ColorGREEN, SK_ColorMAGENTA, false);
75 surfCanvas->translate(SkIntToScalar(kColWidth), 0);
76 this->drawColumn(surfCanvas, SK_ColorCYAN, SK_ColorMAGENTA, true);
77
78 SkPaint surfPaint;
reedcfb6bdf2016-03-29 11:32:50 -070079 surfPaint.setXfermode(SkXfermode::Make(SkXfermode::kSrcOver_Mode));
egdaniel27b63352015-09-15 13:13:50 -070080 surface->draw(canvas, 0, 0, &surfPaint);
egdaniel723b0502015-09-15 09:31:40 -070081 }
82
83 void drawColumn(SkCanvas* canvas, SkColor backgroundColor, SkColor textColor, bool useGrad) {
84 const struct {
85 SkXfermode::Mode fMode;
86 const char* fLabel;
87 } gModes[] = {
88 { SkXfermode::kClear_Mode, "Clear" },
89 { SkXfermode::kSrc_Mode, "Src" },
90 { SkXfermode::kDst_Mode, "Dst" },
91 { SkXfermode::kSrcOver_Mode, "SrcOver" },
92 { SkXfermode::kDstOver_Mode, "DstOver" },
93 { SkXfermode::kSrcIn_Mode, "SrcIn" },
94 { SkXfermode::kDstIn_Mode, "DstIn" },
95 { SkXfermode::kSrcOut_Mode, "SrcOut" },
96 { SkXfermode::kDstOut_Mode, "DstOut" },
97 { SkXfermode::kSrcATop_Mode, "SrcATop" },
98 { SkXfermode::kDstATop_Mode, "DstATop" },
99 { SkXfermode::kXor_Mode, "Xor" },
100 { SkXfermode::kPlus_Mode, "Plus" },
101 { SkXfermode::kModulate_Mode, "Modulate" },
102 { SkXfermode::kScreen_Mode, "Screen" },
103 { SkXfermode::kOverlay_Mode, "Overlay" },
104 { SkXfermode::kDarken_Mode, "Darken" },
105 { SkXfermode::kLighten_Mode, "Lighten" },
106 { SkXfermode::kColorDodge_Mode, "ColorDodge" },
107 { SkXfermode::kColorBurn_Mode, "ColorBurn" },
108 { SkXfermode::kHardLight_Mode, "HardLight" },
109 { SkXfermode::kSoftLight_Mode, "SoftLight" },
110 { SkXfermode::kDifference_Mode, "Difference" },
111 { SkXfermode::kExclusion_Mode, "Exclusion" },
112 { SkXfermode::kMultiply_Mode, "Multiply" },
113 { SkXfermode::kHue_Mode, "Hue" },
114 { SkXfermode::kSaturation_Mode, "Saturation" },
115 { SkXfermode::kColor_Mode, "Color" },
116 { SkXfermode::kLuminosity_Mode, "Luminosity" },
117 };
118 // Draw background rect
119 SkPaint backgroundPaint;
120 backgroundPaint.setColor(backgroundColor);
121 canvas->drawRectCoords(0, 0, SkIntToScalar(kColWidth), SkIntToScalar(kHeight),
122 backgroundPaint);
123 SkScalar y = fTextHeight;
124 for (size_t m = 0; m < SK_ARRAY_COUNT(gModes); m++) {
egdaniel723b0502015-09-15 09:31:40 -0700125 SkPaint paint;
126 paint.setColor(textColor);
127 paint.setAntiAlias(true);
128 paint.setSubpixelText(true);
129 paint.setLCDRenderText(true);
130 paint.setTextSize(fTextHeight);
reedcfb6bdf2016-03-29 11:32:50 -0700131 paint.setXfermode(SkXfermode::Make(gModes[m].fMode));
egdaniel723b0502015-09-15 09:31:40 -0700132 sk_tool_utils::set_portable_typeface(&paint);
133 if (useGrad) {
134 SkRect r;
135 r.setXYWH(0, y - fTextHeight, SkIntToScalar(kColWidth), fTextHeight);
reed1a9b9642016-03-13 14:13:58 -0700136 paint.setShader(make_shader(r));
egdaniel723b0502015-09-15 09:31:40 -0700137 }
138 SkString string(gModes[m].fLabel);
139 canvas->drawText(gModes[m].fLabel, string.size(), 0, y, paint);
140 y+=fTextHeight;
141 }
142 }
halcanary9d524f22016-03-29 09:03:52 -0700143
egdaniel723b0502015-09-15 09:31:40 -0700144private:
145 SkScalar fTextHeight;
reed8a21c9f2016-03-08 18:50:00 -0800146 sk_sp<SkShader> fCheckerboard;
egdaniel723b0502015-09-15 09:31:40 -0700147 typedef skiagm::GM INHERITED;
148};
149
150//////////////////////////////////////////////////////////////////////////////
151
152DEF_GM( return new LcdBlendGM; )
153}