epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 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. |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 9 | /* Tests text rendering with LCD and subpixel rendering turned on and off. |
| 10 | */ |
| 11 | |
| 12 | #include "gm.h" |
| 13 | #include "SkCanvas.h" |
fmalita | 65d79ce | 2014-11-19 09:23:22 -0800 | [diff] [blame] | 14 | #include "SkPicture.h" |
| 15 | #include "SkPictureImageFilter.h" |
| 16 | #include "SkPictureRecorder.h" |
| 17 | #include "SkSurface.h" |
| 18 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 19 | |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 20 | class LcdTextGM : public skiagm::GM { |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 21 | public: |
| 22 | LcdTextGM() { |
| 23 | const int pointSize = 36; |
| 24 | textHeight = SkIntToScalar(pointSize); |
| 25 | } |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 26 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 27 | protected: |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 28 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 29 | SkString onShortName() { |
| 30 | return SkString("lcdtext"); |
| 31 | } |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 32 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 33 | SkISize onISize() { return SkISize::Make(640, 480); } |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 34 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 35 | virtual void onDraw(SkCanvas* canvas) { |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 36 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 37 | y = textHeight; |
| 38 | drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderTrue"), |
| 39 | true, true); |
| 40 | drawText(canvas, SkString("TEXT: SubpixelTrue LCDRenderFalse"), |
| 41 | true, false); |
| 42 | drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderTrue"), |
| 43 | false, true); |
| 44 | drawText(canvas, SkString("TEXT: SubpixelFalse LCDRenderFalse"), |
| 45 | false, false); |
| 46 | } |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 47 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 48 | void drawText(SkCanvas* canvas, const SkString& string, |
| 49 | bool subpixelTextEnabled, bool lcdRenderTextEnabled) { |
| 50 | SkPaint paint; |
| 51 | paint.setColor(SK_ColorBLACK); |
| 52 | paint.setDither(true); |
| 53 | paint.setAntiAlias(true); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 54 | sk_tool_utils::set_portable_typeface(&paint); |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 55 | paint.setSubpixelText(subpixelTextEnabled); |
| 56 | paint.setLCDRenderText(lcdRenderTextEnabled); |
| 57 | paint.setTextSize(textHeight); |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 58 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 59 | canvas->drawText(string.c_str(), string.size(), 0, y, paint); |
| 60 | y += textHeight; |
| 61 | } |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 62 | |
| 63 | private: |
| 64 | typedef skiagm::GM INHERITED; |
| 65 | SkScalar y, textHeight; |
| 66 | }; |
| 67 | |
| 68 | /* |
| 69 | * Skia will automatically disable LCD requests if the total size exceeds some limit |
| 70 | * (hard coded in this test for now, as it is now avaiable as an API) |
| 71 | * |
| 72 | * Test this both by changing "textsize" and by changing the computed size (textsize * CTM) |
| 73 | */ |
| 74 | class LcdTextSizeGM : public skiagm::GM { |
| 75 | enum { |
| 76 | kLCDTextSizeLimit = 48 |
| 77 | }; |
| 78 | |
| 79 | static void ScaleAbout(SkCanvas* canvas, SkScalar sx, SkScalar sy, SkScalar px, SkScalar py) { |
| 80 | SkMatrix m; |
| 81 | m.setScale(sx, sy, px, py); |
| 82 | canvas->concat(m); |
| 83 | } |
| 84 | |
| 85 | public: |
| 86 | LcdTextSizeGM() {} |
| 87 | |
| 88 | protected: |
| 89 | SkString onShortName() { |
| 90 | return SkString("lcdtextsize"); |
| 91 | } |
| 92 | |
| 93 | SkISize onISize() { return SkISize::Make(320, 120); } |
| 94 | |
| 95 | virtual void onDraw(SkCanvas* canvas) { |
| 96 | const char* lcd_text = "LCD"; |
| 97 | const char* gray_text = "GRAY"; |
| 98 | |
| 99 | SkPaint paint; |
| 100 | paint.setAntiAlias(true); |
| 101 | paint.setLCDRenderText(true); |
| 102 | |
| 103 | const struct { |
| 104 | SkPoint fLoc; |
| 105 | SkScalar fTextSize; |
| 106 | SkScalar fScale; |
| 107 | const char* fText; |
| 108 | } rec[] = { |
| 109 | { { 10, 50 }, kLCDTextSizeLimit - 1, 1, lcd_text }, |
| 110 | { { 160, 50 }, kLCDTextSizeLimit + 1, 1, gray_text }, |
| 111 | { { 10, 100 }, kLCDTextSizeLimit / 2, 1.99f, lcd_text }, |
| 112 | { { 160, 100 }, kLCDTextSizeLimit / 2, 2.01f, gray_text }, |
| 113 | }; |
| 114 | |
| 115 | for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) { |
| 116 | const SkPoint loc = rec[i].fLoc; |
| 117 | SkAutoCanvasRestore acr(canvas, true); |
| 118 | |
| 119 | paint.setTextSize(rec[i].fTextSize); |
| 120 | ScaleAbout(canvas, rec[i].fScale, rec[i].fScale, loc.x(), loc.y()); |
| 121 | canvas->drawText(rec[i].fText, strlen(rec[i].fText), loc.x(), loc.y(), paint); |
| 122 | } |
| 123 | } |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 124 | |
| 125 | private: |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 126 | typedef skiagm::GM INHERITED; |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
reed | a3aff06 | 2014-11-13 10:40:58 -0800 | [diff] [blame] | 129 | // ensure that we respect the SkPixelGeometry in SurfaceProps |
| 130 | class LcdTextProps : public skiagm::GM { |
| 131 | static void DrawText(SkCanvas* canvas) { |
| 132 | canvas->drawColor(SK_ColorWHITE); |
| 133 | SkPaint paint; |
| 134 | paint.setAntiAlias(true); |
| 135 | paint.setLCDRenderText(true); |
| 136 | paint.setTextSize(30); |
| 137 | canvas->drawText("Base", 4, 4, 30, paint); |
| 138 | canvas->saveLayer(NULL, NULL); |
| 139 | canvas->drawText("Layer", 5, 4, 70, paint); |
| 140 | canvas->restore(); |
| 141 | } |
| 142 | |
fmalita | 65d79ce | 2014-11-19 09:23:22 -0800 | [diff] [blame] | 143 | static SkSurface* MakeSurface(SkCanvas* canvas, const SkImageInfo& info, SkPixelGeometry geo) { |
| 144 | SkSurfaceProps props = SkSurfaceProps(0, geo); |
| 145 | SkSurface* surface = canvas->newSurface(info, &props); |
| 146 | |
| 147 | if (!surface) { |
| 148 | surface = SkSurface::NewRaster(info, &props); |
| 149 | } |
| 150 | |
| 151 | return surface; |
| 152 | } |
| 153 | |
fmalita | f4905cc | 2014-11-13 11:29:07 -0800 | [diff] [blame] | 154 | protected: |
reed | a3aff06 | 2014-11-13 10:40:58 -0800 | [diff] [blame] | 155 | SkString onShortName() SK_OVERRIDE { |
| 156 | return SkString("lcdtextprops"); |
| 157 | } |
| 158 | |
fmalita | 65d79ce | 2014-11-19 09:23:22 -0800 | [diff] [blame] | 159 | SkISize onISize() SK_OVERRIDE { return SkISize::Make(230, 230); } |
reed | a3aff06 | 2014-11-13 10:40:58 -0800 | [diff] [blame] | 160 | |
fmalita | f4905cc | 2014-11-13 11:29:07 -0800 | [diff] [blame] | 161 | uint32_t onGetFlags() const SK_OVERRIDE { |
| 162 | return kSkip565_Flag; |
| 163 | } |
| 164 | |
fmalita | 65d79ce | 2014-11-19 09:23:22 -0800 | [diff] [blame] | 165 | virtual void onOnceBeforeDraw() SK_OVERRIDE { |
| 166 | fInfo = SkImageInfo::MakeN32Premul(100, 100); |
| 167 | SkPictureRecorder recorder; |
| 168 | DrawText(recorder.beginRecording(SkIntToScalar(fInfo.width()), |
| 169 | SkIntToScalar(fInfo.height()))); |
| 170 | SkAutoTUnref<SkPicture> pic(recorder.endRecording()); |
| 171 | SkAutoTUnref<SkImageFilter> filter(SkPictureImageFilter::Create(pic.get())); |
| 172 | fFilterPaint.setImageFilter(filter.get()); |
| 173 | } |
| 174 | |
reed | a3aff06 | 2014-11-13 10:40:58 -0800 | [diff] [blame] | 175 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 176 | const SkPixelGeometry geos[] = { |
| 177 | kRGB_H_SkPixelGeometry, |
| 178 | kUnknown_SkPixelGeometry, |
| 179 | }; |
| 180 | |
reed | a3aff06 | 2014-11-13 10:40:58 -0800 | [diff] [blame] | 181 | for (size_t i = 0; i < SK_ARRAY_COUNT(geos); ++i) { |
fmalita | 65d79ce | 2014-11-19 09:23:22 -0800 | [diff] [blame] | 182 | SkAutoTUnref<SkSurface> surf(MakeSurface(canvas, fInfo, geos[i])); |
reed | a3aff06 | 2014-11-13 10:40:58 -0800 | [diff] [blame] | 183 | DrawText(surf->getCanvas()); |
fmalita | 65d79ce | 2014-11-19 09:23:22 -0800 | [diff] [blame] | 184 | surf->draw(canvas, SkIntToScalar(i * (fInfo.width() + 10)), 0, NULL); |
| 185 | } |
| 186 | |
| 187 | for (size_t i = 0; i < SK_ARRAY_COUNT(geos); ++i) { |
| 188 | SkAutoTUnref<SkSurface> surf(MakeSurface(canvas, fInfo, geos[i])); |
| 189 | surf->getCanvas()->saveLayer(NULL, &fFilterPaint); |
| 190 | surf->getCanvas()->restore(); |
| 191 | surf->draw(canvas, |
| 192 | SkIntToScalar(i * (fInfo.width() + 10)), |
| 193 | SkIntToScalar(fInfo.height() + 10), |
| 194 | NULL); |
reed | a3aff06 | 2014-11-13 10:40:58 -0800 | [diff] [blame] | 195 | } |
| 196 | } |
fmalita | 65d79ce | 2014-11-19 09:23:22 -0800 | [diff] [blame] | 197 | |
| 198 | private: |
| 199 | SkPaint fFilterPaint; |
| 200 | SkImageInfo fInfo; |
| 201 | |
| 202 | typedef skiagm::GM INHERITED; |
reed | a3aff06 | 2014-11-13 10:40:58 -0800 | [diff] [blame] | 203 | }; |
| 204 | |
epoger@google.com | 213c42b | 2011-06-28 16:20:27 +0000 | [diff] [blame] | 205 | /////////////////////////////////////////////////////////////////////////////// |
| 206 | |
reed | 4942e75 | 2014-10-01 13:59:33 -0700 | [diff] [blame] | 207 | DEF_GM( return new LcdTextGM; ) |
| 208 | DEF_GM( return new LcdTextSizeGM; ) |
Florin Malita | 66b4ce9 | 2014-11-13 15:00:03 -0500 | [diff] [blame] | 209 | // Temporarily disabled (dftext interference) |
| 210 | // DEF_GM( return new LcdTextProps; ) |