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