Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | #if SK_SUPPORT_ATLAS_TEXT |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 11 | #include "GrContext.h" |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 12 | |
| 13 | #include "SkAtlasTextContext.h" |
| 14 | #include "SkAtlasTextFont.h" |
| 15 | #include "SkAtlasTextTarget.h" |
| 16 | #include "SkBitmap.h" |
| 17 | #include "SkCanvas.h" |
Mike Reed | 94cca60 | 2018-12-02 16:04:27 -0500 | [diff] [blame] | 18 | #include "SkFont.h" |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 19 | #include "SkTypeface.h" |
Hal Canary | ea60b95 | 2018-08-21 11:45:46 -0400 | [diff] [blame] | 20 | #include "SkUTF.h" |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 21 | #include "gpu/TestContext.h" |
| 22 | #include "gpu/atlastext/GLTestAtlasTextRenderer.h" |
| 23 | #include "gpu/atlastext/TestAtlasTextRenderer.h" |
| 24 | #include "sk_tool_utils.h" |
| 25 | |
| 26 | // GM that draws text using the Atlas Text interface offscreen and then blits that to the canvas. |
| 27 | |
| 28 | static SkScalar draw_string(SkAtlasTextTarget* target, const SkString& text, SkScalar x, SkScalar y, |
| 29 | uint32_t color, sk_sp<SkTypeface> typeface, float size) { |
Brian Salomon | a0ba714 | 2017-11-20 13:17:43 -0500 | [diff] [blame] | 30 | if (!text.size()) { |
| 31 | return x; |
| 32 | } |
Mike Reed | 94cca60 | 2018-12-02 16:04:27 -0500 | [diff] [blame] | 33 | auto atlas_font = SkAtlasTextFont::Make(typeface, size); |
Hal Canary | f107a2f | 2018-07-25 16:52:48 -0400 | [diff] [blame] | 34 | int cnt = SkUTF::CountUTF8(text.c_str(), text.size()); |
Brian Salomon | 0c1c2b3 | 2017-11-20 13:13:01 -0500 | [diff] [blame] | 35 | std::unique_ptr<SkGlyphID[]> glyphs(new SkGlyphID[cnt]); |
| 36 | typeface->charsToGlyphs(text.c_str(), SkTypeface::Encoding::kUTF8_Encoding, glyphs.get(), cnt); |
| 37 | |
Brian Salomon | a0ba714 | 2017-11-20 13:17:43 -0500 | [diff] [blame] | 38 | // Using a paint to get the positions for each glyph. |
Mike Reed | 94cca60 | 2018-12-02 16:04:27 -0500 | [diff] [blame] | 39 | SkFont font; |
| 40 | font.setSize(size); |
| 41 | font.setTypeface(std::move(typeface)); |
Brian Salomon | a0ba714 | 2017-11-20 13:17:43 -0500 | [diff] [blame] | 42 | std::unique_ptr<SkScalar[]> widths(new SkScalar[cnt]); |
Mike Reed | 94cca60 | 2018-12-02 16:04:27 -0500 | [diff] [blame] | 43 | font.getWidths(glyphs.get(), cnt, widths.get()); |
Brian Salomon | a0ba714 | 2017-11-20 13:17:43 -0500 | [diff] [blame] | 44 | |
| 45 | std::unique_ptr<SkPoint[]> positions(new SkPoint[cnt]); |
| 46 | positions[0] = {x, y}; |
| 47 | for (int i = 1; i < cnt; ++i) { |
| 48 | positions[i] = {positions[i - 1].fX + widths[i - 1], y}; |
| 49 | } |
| 50 | |
Mike Reed | 94cca60 | 2018-12-02 16:04:27 -0500 | [diff] [blame] | 51 | target->drawText(glyphs.get(), positions.get(), cnt, color, *atlas_font); |
Brian Salomon | a0ba714 | 2017-11-20 13:17:43 -0500 | [diff] [blame] | 52 | |
Brian Salomon | b508696 | 2017-12-13 10:59:33 -0500 | [diff] [blame] | 53 | // Return the width of the of draw. |
| 54 | return positions[cnt - 1].fX + widths[cnt - 1] - positions[0].fX; |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | class AtlasTextGM : public skiagm::GM { |
| 58 | public: |
| 59 | AtlasTextGM() = default; |
| 60 | |
| 61 | protected: |
| 62 | SkString onShortName() override { return SkString("atlastext"); } |
| 63 | |
| 64 | SkISize onISize() override { return SkISize::Make(kSize, kSize); } |
| 65 | |
| 66 | void onOnceBeforeDraw() override { |
| 67 | fRenderer = sk_gpu_test::MakeGLTestAtlasTextRenderer(); |
| 68 | if (!fRenderer) { |
| 69 | return; |
| 70 | } |
| 71 | fContext = SkAtlasTextContext::Make(fRenderer); |
| 72 | auto targetHandle = fRenderer->makeTargetHandle(kSize, kSize); |
Robert Phillips | a3f7026 | 2018-02-08 10:59:38 -0500 | [diff] [blame] | 73 | if (!targetHandle) { |
| 74 | return; |
| 75 | } |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 76 | |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 77 | fTarget = SkAtlasTextTarget::Make(fContext, kSize, kSize, targetHandle); |
| 78 | |
| 79 | fTypefaces[0] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Italic()); |
| 80 | fTypefaces[1] = |
| 81 | sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Italic()); |
| 82 | fTypefaces[2] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Normal()); |
| 83 | fTypefaces[3] = |
| 84 | sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Normal()); |
| 85 | fTypefaces[4] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Bold()); |
| 86 | fTypefaces[5] = sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Bold()); |
| 87 | } |
| 88 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 89 | DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
Robert Phillips | a3f7026 | 2018-02-08 10:59:38 -0500 | [diff] [blame] | 90 | if (!fRenderer || !fTarget || !fTarget->handle()) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 91 | *errorMsg = "No renderer and/or target."; |
| 92 | return DrawResult::kFail; |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 93 | } |
Brian Salomon | 40dc8a7 | 2017-11-20 11:01:54 -0500 | [diff] [blame] | 94 | fRenderer->clearTarget(fTarget->handle(), 0xFF808080); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 95 | auto bmp = this->drawText(); |
| 96 | SkPaint paint; |
| 97 | paint.setBlendMode(SkBlendMode::kSrc); |
| 98 | canvas->drawBitmap(bmp, 0, 0); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 99 | return DrawResult::kOk; |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | private: |
| 103 | SkBitmap drawText() { |
| 104 | static const int kSizes[] = {8, 13, 18, 23, 30}; |
| 105 | |
| 106 | static const SkString kTexts[] = {SkString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), |
| 107 | SkString("abcdefghijklmnopqrstuvwxyz"), |
| 108 | SkString("0123456789"), |
| 109 | SkString("!@#$%^&*()<>[]{}")}; |
| 110 | SkScalar x = 0; |
| 111 | SkScalar y = 10; |
| 112 | |
| 113 | SkRandom random; |
| 114 | do { |
| 115 | for (auto s : kSizes) { |
| 116 | auto size = 2 * s; |
| 117 | for (const auto& typeface : fTypefaces) { |
| 118 | for (const auto& text : kTexts) { |
Brian Salomon | b508696 | 2017-12-13 10:59:33 -0500 | [diff] [blame] | 119 | // Choose a random color but don't let alpha be too small to see. |
| 120 | uint32_t color = random.nextU() | 0x40000000; |
| 121 | fTarget->save(); |
| 122 | // Randomly add a little bit of perspective |
| 123 | if (random.nextBool()) { |
| 124 | SkMatrix persp; |
| 125 | persp.reset(); |
| 126 | persp.setPerspY(0.0005f); |
| 127 | persp.preTranslate(-x, -y + s); |
| 128 | persp.postTranslate(x, y - s); |
| 129 | fTarget->concat(persp); |
| 130 | } |
| 131 | // Randomly switch between positioning with a matrix vs x, y passed to draw. |
| 132 | SkScalar drawX = x, drawY = y; |
| 133 | if (random.nextBool()) { |
| 134 | fTarget->translate(x, y); |
| 135 | drawX = drawY = 0; |
| 136 | } |
| 137 | x += size + |
| 138 | draw_string(fTarget.get(), text, drawX, drawY, color, typeface, size); |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 139 | x = SkScalarCeilToScalar(x); |
Brian Salomon | b508696 | 2017-12-13 10:59:33 -0500 | [diff] [blame] | 140 | fTarget->restore(); |
| 141 | // Flush periodically to test continued drawing after a flush. |
| 142 | if ((random.nextU() % 8) == 0) { |
Brian Salomon | cf1aa53 | 2017-11-29 14:58:59 -0500 | [diff] [blame] | 143 | fTarget->flush(); |
| 144 | } |
Brian Salomon | cbcb0a1 | 2017-11-19 13:20:13 -0500 | [diff] [blame] | 145 | if (x + 100 > kSize) { |
| 146 | x = 0; |
| 147 | y += SkScalarCeilToScalar(size + 3); |
| 148 | if (y > kSize) { |
| 149 | fTarget->flush(); |
| 150 | return fRenderer->readTargetHandle(fTarget->handle()); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } while (true); |
| 157 | } |
| 158 | |
| 159 | static constexpr int kSize = 1280; |
| 160 | |
| 161 | sk_sp<SkTypeface> fTypefaces[6]; |
| 162 | sk_sp<sk_gpu_test::TestAtlasTextRenderer> fRenderer; |
| 163 | std::unique_ptr<SkAtlasTextTarget> fTarget; |
| 164 | sk_sp<SkAtlasTextContext> fContext; |
| 165 | |
| 166 | typedef GM INHERITED; |
| 167 | }; |
| 168 | |
| 169 | constexpr int AtlasTextGM::kSize; |
| 170 | |
| 171 | ////////////////////////////////////////////////////////////////////////////// |
| 172 | |
| 173 | DEF_GM(return new AtlasTextGM;) |
| 174 | |
| 175 | #endif |