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