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