blob: 1345d237627475c3d0e62614421b54b837886605 [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"
Mike Klein33d20552017-03-22 13:47:51 -040014#include "sk_tool_utils.h"
egdaniel723b0502015-09-15 09:31:40 -070015#include "SkCanvas.h"
16#include "SkGradientShader.h"
egdaniel27b63352015-09-15 13:13:50 -070017#include "SkSurface.h"
egdaniel723b0502015-09-15 09:31:40 -070018
19namespace skiagm {
20
mtkleindbfd7ab2016-09-01 11:24:54 -070021constexpr int kColWidth = 180;
22constexpr int kNumCols = 4;
23constexpr int kWidth = kColWidth * kNumCols;
24constexpr int kHeight = 750;
egdaniel723b0502015-09-15 09:31:40 -070025
reed1a9b9642016-03-13 14:13:58 -070026static sk_sp<SkShader> make_shader(const SkRect& bounds) {
egdaniel723b0502015-09-15 09:31:40 -070027 const SkPoint pts[] = {
28 { bounds.left(), bounds.top() },
29 { bounds.right(), bounds.bottom() },
30 };
31 const SkColor colors[] = {
32 SK_ColorRED, SK_ColorGREEN,
33 };
reed1a9b9642016-03-13 14:13:58 -070034 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
35 SkShader::kRepeat_TileMode);
egdaniel723b0502015-09-15 09:31:40 -070036}
37
38class LcdBlendGM : public skiagm::GM {
39public:
40 LcdBlendGM() {
41 const int kPointSize = 25;
42 fTextHeight = SkIntToScalar(kPointSize);
43 }
halcanary9d524f22016-03-29 09:03:52 -070044
egdaniel723b0502015-09-15 09:31:40 -070045protected:
46 SkString onShortName() override {
egdaniel27b63352015-09-15 13:13:50 -070047 return SkString("lcdblendmodes");
48 }
49
50 void onOnceBeforeDraw() override {
reed8a21c9f2016-03-08 18:50:00 -080051 fCheckerboard = sk_tool_utils::create_checkerboard_shader(SK_ColorBLACK, SK_ColorWHITE, 4);
egdaniel723b0502015-09-15 09:31:40 -070052 }
halcanary9d524f22016-03-29 09:03:52 -070053
egdaniel723b0502015-09-15 09:31:40 -070054 SkISize onISize() override { return SkISize::Make(kWidth, kHeight); }
halcanary9d524f22016-03-29 09:03:52 -070055
egdaniel723b0502015-09-15 09:31:40 -070056 void onDraw(SkCanvas* canvas) override {
egdaniel27b63352015-09-15 13:13:50 -070057 SkPaint p;
58 p.setAntiAlias(false);
59 p.setStyle(SkPaint::kFill_Style);
60 p.setShader(fCheckerboard);
61 SkRect r = SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
62 canvas->drawRect(r, p);
63
64 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
reede8f30622016-03-23 18:59:25 -070065 auto surface(canvas->makeSurface(info));
egdaniel27b63352015-09-15 13:13:50 -070066 if (nullptr == surface) {
reede8f30622016-03-23 18:59:25 -070067 surface = SkSurface::MakeRaster(info);
egdaniel27b63352015-09-15 13:13:50 -070068 }
69
70 SkCanvas* surfCanvas = surface->getCanvas();
71 this->drawColumn(surfCanvas, SK_ColorBLACK, SK_ColorWHITE, false);
72 surfCanvas->translate(SkIntToScalar(kColWidth), 0);
73 this->drawColumn(surfCanvas, SK_ColorWHITE, SK_ColorBLACK, false);
74 surfCanvas->translate(SkIntToScalar(kColWidth), 0);
75 this->drawColumn(surfCanvas, SK_ColorGREEN, SK_ColorMAGENTA, false);
76 surfCanvas->translate(SkIntToScalar(kColWidth), 0);
77 this->drawColumn(surfCanvas, SK_ColorCYAN, SK_ColorMAGENTA, true);
78
79 SkPaint surfPaint;
reed374772b2016-10-05 17:33:02 -070080 surfPaint.setBlendMode(SkBlendMode::kSrcOver);
egdaniel27b63352015-09-15 13:13:50 -070081 surface->draw(canvas, 0, 0, &surfPaint);
egdaniel723b0502015-09-15 09:31:40 -070082 }
83
84 void drawColumn(SkCanvas* canvas, SkColor backgroundColor, SkColor textColor, bool useGrad) {
Brian Osmand1e67e72017-03-15 12:19:37 -040085 const SkBlendMode gModes[] = {
86 SkBlendMode::kClear,
87 SkBlendMode::kSrc,
88 SkBlendMode::kDst,
89 SkBlendMode::kSrcOver,
90 SkBlendMode::kDstOver,
91 SkBlendMode::kSrcIn,
92 SkBlendMode::kDstIn,
93 SkBlendMode::kSrcOut,
94 SkBlendMode::kDstOut,
95 SkBlendMode::kSrcATop,
96 SkBlendMode::kDstATop,
97 SkBlendMode::kXor,
98 SkBlendMode::kPlus,
99 SkBlendMode::kModulate,
100 SkBlendMode::kScreen,
101 SkBlendMode::kOverlay,
102 SkBlendMode::kDarken,
103 SkBlendMode::kLighten,
104 SkBlendMode::kColorDodge,
105 SkBlendMode::kColorBurn,
106 SkBlendMode::kHardLight,
107 SkBlendMode::kSoftLight,
108 SkBlendMode::kDifference,
109 SkBlendMode::kExclusion,
110 SkBlendMode::kMultiply,
111 SkBlendMode::kHue,
112 SkBlendMode::kSaturation,
113 SkBlendMode::kColor,
114 SkBlendMode::kLuminosity,
egdaniel723b0502015-09-15 09:31:40 -0700115 };
116 // Draw background rect
117 SkPaint backgroundPaint;
118 backgroundPaint.setColor(backgroundColor);
Mike Reed3661bc92017-02-22 13:21:42 -0500119 canvas->drawRect(SkRect::MakeIWH(kColWidth, kHeight), backgroundPaint);
egdaniel723b0502015-09-15 09:31:40 -0700120 SkScalar y = fTextHeight;
121 for (size_t m = 0; m < SK_ARRAY_COUNT(gModes); m++) {
egdaniel723b0502015-09-15 09:31:40 -0700122 SkPaint paint;
123 paint.setColor(textColor);
124 paint.setAntiAlias(true);
125 paint.setSubpixelText(true);
126 paint.setLCDRenderText(true);
127 paint.setTextSize(fTextHeight);
Brian Osmand1e67e72017-03-15 12:19:37 -0400128 paint.setBlendMode(gModes[m]);
egdaniel723b0502015-09-15 09:31:40 -0700129 sk_tool_utils::set_portable_typeface(&paint);
130 if (useGrad) {
131 SkRect r;
132 r.setXYWH(0, y - fTextHeight, SkIntToScalar(kColWidth), fTextHeight);
reed1a9b9642016-03-13 14:13:58 -0700133 paint.setShader(make_shader(r));
egdaniel723b0502015-09-15 09:31:40 -0700134 }
Brian Osmand1e67e72017-03-15 12:19:37 -0400135 SkString string(SkBlendMode_Name(gModes[m]));
Cary Clark2a475ea2017-04-28 15:35:12 -0400136 canvas->drawString(string, 0, y, paint);
egdaniel723b0502015-09-15 09:31:40 -0700137 y+=fTextHeight;
138 }
139 }
halcanary9d524f22016-03-29 09:03:52 -0700140
egdaniel723b0502015-09-15 09:31:40 -0700141private:
142 SkScalar fTextHeight;
reed8a21c9f2016-03-08 18:50:00 -0800143 sk_sp<SkShader> fCheckerboard;
egdaniel723b0502015-09-15 09:31:40 -0700144 typedef skiagm::GM INHERITED;
145};
146
147//////////////////////////////////////////////////////////////////////////////
148
149DEF_GM( return new LcdBlendGM; )
150}