Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | // GM to stress TextBlob regeneration and the GPU font cache |
| 9 | // It's not necessary to run this with CPU configs |
| 10 | // |
| 11 | // The point here is to draw a set of text that will fit in one Plot, and then some large |
| 12 | // text. After a flush we draw the first set of text again with a slightly different color, |
| 13 | // and then enough new large text to spill the entire atlas. What *should* happen is that |
| 14 | // the Plot with the first set of text will not get overwritten by the new large text. |
| 15 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "gm/gm.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 18 | #include "include/core/SkColor.h" |
| 19 | #include "include/core/SkFont.h" |
| 20 | #include "include/core/SkFontStyle.h" |
| 21 | #include "include/core/SkFontTypes.h" |
| 22 | #include "include/core/SkPaint.h" |
| 23 | #include "include/core/SkRefCnt.h" |
| 24 | #include "include/core/SkScalar.h" |
| 25 | #include "include/core/SkSize.h" |
| 26 | #include "include/core/SkString.h" |
| 27 | #include "include/core/SkTextBlob.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "include/core/SkTypeface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 29 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 30 | #include "include/gpu/GrContext.h" |
| 31 | #include "include/gpu/GrContextOptions.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 32 | #include "include/private/GrTypesPriv.h" |
| 33 | #include "include/private/SkTemplates.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 34 | #include "src/gpu/GrContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 35 | #include "tools/ToolUtils.h" |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 36 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 37 | class GrRenderTargetContext; |
| 38 | |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 39 | static sk_sp<SkTextBlob> make_blob(const SkString& text, const SkFont& font) { |
| 40 | size_t len = text.size(); |
| 41 | SkAutoTArray<SkScalar> pos(len); |
| 42 | SkAutoTArray<SkGlyphID> glyphs(len); |
| 43 | |
| 44 | font.textToGlyphs(text.c_str(), len, SkTextEncoding::kUTF8, glyphs.get(), len); |
| 45 | font.getXPos(glyphs.get(), len, pos.get()); |
| 46 | return SkTextBlob::MakeFromPosTextH(text.c_str(), len, pos.get(), 0, font); |
| 47 | } |
| 48 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 49 | class FontRegenGM : public skiagm::GpuGM { |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 50 | public: |
| 51 | FontRegenGM() { |
| 52 | this->setBGColor(SK_ColorLTGRAY); |
| 53 | } |
| 54 | |
| 55 | void modifyGrContextOptions(GrContextOptions* options) override { |
| 56 | options->fGlyphCacheTextureMaximumBytes = 0; |
| 57 | options->fAllowMultipleGlyphCacheTextures = GrContextOptions::Enable::kNo; |
| 58 | } |
| 59 | |
| 60 | protected: |
| 61 | SkString onShortName() override { |
| 62 | SkString name("fontregen"); |
| 63 | return name; |
| 64 | } |
| 65 | |
| 66 | SkISize onISize() override { return SkISize::Make(kSize, kSize); } |
| 67 | |
| 68 | void onOnceBeforeDraw() override { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 69 | auto tf = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Normal()); |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 70 | |
| 71 | static const SkString kTexts[] = { |
| 72 | SkString("abcdefghijklmnopqrstuvwxyz"), |
| 73 | SkString("ABCDEFGHI"), |
| 74 | SkString("NOPQRSTUV") |
| 75 | }; |
| 76 | |
| 77 | SkFont font; |
| 78 | font.setEdging(SkFont::Edging::kAntiAlias); |
| 79 | font.setSubpixel(false); |
| 80 | font.setSize(80); |
| 81 | font.setTypeface(tf); |
| 82 | |
| 83 | fBlobs[0] = make_blob(kTexts[0], font); |
| 84 | font.setSize(162); |
| 85 | fBlobs[1] = make_blob(kTexts[1], font); |
| 86 | fBlobs[2] = make_blob(kTexts[2], font); |
| 87 | } |
| 88 | |
Robert Phillips | 9882dae | 2019-03-04 11:00:10 -0500 | [diff] [blame] | 89 | void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override { |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 90 | SkPaint paint; |
| 91 | paint.setColor(SK_ColorBLACK); |
| 92 | canvas->drawTextBlob(fBlobs[0], 10, 80, paint); |
| 93 | canvas->drawTextBlob(fBlobs[1], 10, 225, paint); |
Robert Phillips | 9882dae | 2019-03-04 11:00:10 -0500 | [diff] [blame] | 94 | context->flush(); |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 95 | |
| 96 | paint.setColor(0xFF010101); |
| 97 | canvas->drawTextBlob(fBlobs[0], 10, 305, paint); |
| 98 | canvas->drawTextBlob(fBlobs[2], 10, 465, paint); |
| 99 | |
| 100 | // Debugging tool for GPU. |
| 101 | static const bool kShowAtlas = false; |
| 102 | if (kShowAtlas) { |
Robert Phillips | 19104af | 2019-03-05 12:51:52 -0500 | [diff] [blame] | 103 | auto img = context->priv().testingOnly_getFontAtlasImage(kA8_GrMaskFormat); |
| 104 | canvas->drawImage(img, 200, 0); |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | static constexpr SkScalar kSize = 512; |
| 110 | |
| 111 | sk_sp<SkTextBlob> fBlobs[3]; |
| 112 | typedef GM INHERITED; |
| 113 | }; |
| 114 | |
| 115 | constexpr SkScalar FontRegenGM::kSize; |
| 116 | |
| 117 | ////////////////////////////////////////////////////////////////////////////// |
| 118 | |
| 119 | DEF_GM(return new FontRegenGM()) |