joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBlurTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkColorSpace.h" |
| 13 | #include "include/core/SkFont.h" |
| 14 | #include "include/core/SkFontTypes.h" |
| 15 | #include "include/core/SkImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/core/SkMaskFilter.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 17 | #include "include/core/SkPaint.h" |
| 18 | #include "include/core/SkRect.h" |
| 19 | #include "include/core/SkRefCnt.h" |
| 20 | #include "include/core/SkScalar.h" |
| 21 | #include "include/core/SkSize.h" |
| 22 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "include/core/SkSurface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 24 | #include "include/core/SkSurfaceProps.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "include/core/SkTextBlob.h" |
| 26 | #include "include/core/SkTypeface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 27 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "include/utils/SkRandom.h" |
| 29 | #include "src/core/SkBlurMask.h" |
| 30 | #include "tools/Resources.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 31 | #include "tools/ToolUtils.h" |
| 32 | |
| 33 | #include <string.h> |
| 34 | |
| 35 | class GrContext; |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 36 | |
| 37 | namespace skiagm { |
| 38 | class TextBlobMixedSizes : public GM { |
| 39 | public: |
| 40 | // This gm tests that textblobs of mixed sizes with a large glyph will render properly |
| 41 | TextBlobMixedSizes(bool useDFT) : fUseDFT(useDFT) {} |
| 42 | |
| 43 | protected: |
| 44 | void onOnceBeforeDraw() override { |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 45 | SkTextBlobBuilder builder; |
| 46 | |
| 47 | // make textblob. To stress distance fields, we choose sizes appropriately |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 48 | SkFont font(MakeResourceAsTypeface("fonts/HangingS.ttf"), 262); |
| 49 | font.setSubpixel(true); |
| 50 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 51 | |
joshualitt | 33e91f1 | 2015-09-18 11:28:59 -0700 | [diff] [blame] | 52 | const char* text = "Skia"; |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 53 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 54 | ToolUtils::add_to_text_blob(&builder, text, font, 0, 0); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 55 | |
| 56 | // large |
| 57 | SkRect bounds; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 58 | font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 59 | SkScalar yOffset = bounds.height(); |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 60 | font.setSize(162); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 61 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 62 | ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 63 | |
| 64 | // Medium |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 65 | font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 66 | yOffset += bounds.height(); |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 67 | font.setSize(72); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 68 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 69 | ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 70 | |
| 71 | // Small |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 72 | font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 73 | yOffset += bounds.height(); |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 74 | font.setSize(32); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 75 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 76 | ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 77 | |
| 78 | // micro (will fall out of distance field text even if distance field text is enabled) |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 79 | font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 80 | yOffset += bounds.height(); |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 81 | font.setSize(14); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 82 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 83 | ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 84 | |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 85 | // Zero size. |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 86 | font.measureText(text, strlen(text), SkTextEncoding::kUTF8, &bounds); |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 87 | yOffset += bounds.height(); |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 88 | font.setSize(0); |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 89 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 90 | ToolUtils::add_to_text_blob(&builder, text, font, 0, yOffset); |
kkinnunen | 68c63b3 | 2016-03-04 00:12:33 -0800 | [diff] [blame] | 91 | |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 92 | // build |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 93 | fBlob = builder.make(); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | SkString onShortName() override { |
Mike Klein | bea1f94 | 2019-03-08 11:11:55 -0600 | [diff] [blame] | 97 | return SkStringPrintf("textblobmixedsizes%s", |
Hal Canary | 58822d6 | 2017-11-14 12:08:34 -0500 | [diff] [blame] | 98 | fUseDFT ? "_df" : ""); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | SkISize onISize() override { |
| 102 | return SkISize::Make(kWidth, kHeight); |
| 103 | } |
| 104 | |
| 105 | void onDraw(SkCanvas* inputCanvas) override { |
| 106 | SkCanvas* canvas = inputCanvas; |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 107 | sk_sp<SkSurface> surface; |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 108 | if (fUseDFT) { |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 109 | // Create a new Canvas to enable DFT |
| 110 | GrContext* ctx = inputCanvas->getGrContext(); |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 111 | SkISize size = onISize(); |
Mike Reed | 693fdbd | 2017-01-12 10:13:40 -0500 | [diff] [blame] | 112 | sk_sp<SkColorSpace> colorSpace = inputCanvas->imageInfo().refColorSpace(); |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 113 | SkImageInfo info = SkImageInfo::MakeN32(size.width(), size.height(), |
| 114 | kPremul_SkAlphaType, colorSpace); |
brianosman | 3a0dbde | 2016-07-26 11:36:05 -0700 | [diff] [blame] | 115 | SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag, |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 116 | SkSurfaceProps::kLegacyFontHost_InitType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 117 | surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info, 0, &props); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 118 | canvas = surface.get() ? surface->getCanvas() : inputCanvas; |
| 119 | // init our new canvas with the old canvas's matrix |
| 120 | canvas->setMatrix(inputCanvas->getTotalMatrix()); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 121 | } |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 122 | canvas->drawColor(SK_ColorWHITE); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 123 | |
| 124 | SkRect bounds = fBlob->bounds(); |
| 125 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 126 | const int kPadX = SkScalarFloorToInt(bounds.width() / 3); |
| 127 | const int kPadY = SkScalarFloorToInt(bounds.height() / 3); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 128 | |
| 129 | int rowCount = 0; |
| 130 | canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY)); |
| 131 | canvas->save(); |
| 132 | SkRandom random; |
| 133 | |
| 134 | SkPaint paint; |
| 135 | if (!fUseDFT) { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 136 | paint.setColor(SK_ColorWHITE); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 137 | } |
| 138 | paint.setAntiAlias(false); |
| 139 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 140 | const SkScalar kSigma = SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(8)); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 141 | |
| 142 | // setup blur paint |
| 143 | SkPaint blurPaint(paint); |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 144 | blurPaint.setColor(SK_ColorBLACK); |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 145 | blurPaint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, kSigma)); |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 146 | |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 147 | for (int i = 0; i < 4; i++) { |
| 148 | canvas->save(); |
| 149 | switch (i % 2) { |
| 150 | case 0: |
| 151 | canvas->rotate(random.nextF() * 45.f); |
| 152 | break; |
| 153 | case 1: |
| 154 | canvas->rotate(-random.nextF() * 45.f); |
| 155 | break; |
| 156 | } |
| 157 | if (!fUseDFT) { |
| 158 | canvas->drawTextBlob(fBlob, 0, 0, blurPaint); |
| 159 | } |
| 160 | canvas->drawTextBlob(fBlob, 0, 0, paint); |
| 161 | canvas->restore(); |
| 162 | canvas->translate(bounds.width() + SK_Scalar1 * kPadX, 0); |
| 163 | ++rowCount; |
| 164 | if ((bounds.width() + 2 * kPadX) * rowCount > kWidth) { |
| 165 | canvas->restore(); |
| 166 | canvas->translate(0, bounds.height() + SK_Scalar1 * kPadY); |
| 167 | canvas->save(); |
| 168 | rowCount = 0; |
| 169 | } |
| 170 | } |
| 171 | canvas->restore(); |
| 172 | |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 173 | // render offscreen buffer |
| 174 | if (surface) { |
| 175 | SkAutoCanvasRestore acr(inputCanvas, true); |
| 176 | // since we prepended this matrix already, we blit using identity |
| 177 | inputCanvas->resetMatrix(); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 178 | inputCanvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, nullptr); |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 179 | } |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | private: |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 183 | sk_sp<SkTextBlob> fBlob; |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 184 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 185 | static constexpr int kWidth = 2100; |
| 186 | static constexpr int kHeight = 1900; |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 187 | |
| 188 | bool fUseDFT; |
| 189 | |
| 190 | typedef GM INHERITED; |
| 191 | }; |
| 192 | |
| 193 | ////////////////////////////////////////////////////////////////////////////// |
| 194 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 195 | DEF_GM( return new TextBlobMixedSizes(false); ) |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 196 | DEF_GM( return new TextBlobMixedSizes(true); ) |
joshualitt | e46cf96 | 2015-08-26 11:19:55 -0700 | [diff] [blame] | 197 | } |