blob: 626b46bdf241097b780c334977b43e734179295d [file] [log] [blame]
Brian Salomoncbcb0a12017-11-19 13:20:13 -05001/*
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 Wagnerd1701ba2019-04-30 13:44:26 -04008#include "include/core/SkTypes.h"
Brian Salomoncbcb0a12017-11-19 13:20:13 -05009
10#if SK_SUPPORT_ATLAS_TEXT
11
Ben Wagnerd1701ba2019-04-30 13:44:26 -040012#include "gm/gm.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/atlastext/SkAtlasTextContext.h"
14#include "include/atlastext/SkAtlasTextFont.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040015#include "include/atlastext/SkAtlasTextRenderer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/atlastext/SkAtlasTextTarget.h"
17#include "include/core/SkBitmap.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040018#include "include/core/SkBlendMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkCanvas.h"
20#include "include/core/SkFont.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040021#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 Kleinc0bd9f92019-04-23 12:05:21 -050030#include "include/core/SkTypeface.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040031#include "include/utils/SkRandom.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/utils/SkUTF.h"
33#include "tools/ToolUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "tools/gpu/atlastext/GLTestAtlasTextRenderer.h"
35#include "tools/gpu/atlastext/TestAtlasTextRenderer.h"
Brian Salomoncbcb0a12017-11-19 13:20:13 -050036
Ben Wagnerd1701ba2019-04-30 13:44:26 -040037#include <memory>
38#include <utility>
39
40class GrContext;
41class GrRenderTargetContext;
42
Brian Salomoncbcb0a12017-11-19 13:20:13 -050043// GM that draws text using the Atlas Text interface offscreen and then blits that to the canvas.
44
45static SkScalar draw_string(SkAtlasTextTarget* target, const SkString& text, SkScalar x, SkScalar y,
46 uint32_t color, sk_sp<SkTypeface> typeface, float size) {
Brian Salomona0ba7142017-11-20 13:17:43 -050047 if (!text.size()) {
48 return x;
49 }
Mike Reed94cca602018-12-02 16:04:27 -050050 auto atlas_font = SkAtlasTextFont::Make(typeface, size);
Hal Canaryf107a2f2018-07-25 16:52:48 -040051 int cnt = SkUTF::CountUTF8(text.c_str(), text.size());
Brian Salomon0c1c2b32017-11-20 13:13:01 -050052 std::unique_ptr<SkGlyphID[]> glyphs(new SkGlyphID[cnt]);
Brian Salomon0c1c2b32017-11-20 13:13:01 -050053
Brian Salomona0ba7142017-11-20 13:17:43 -050054 // Using a paint to get the positions for each glyph.
Mike Reed94cca602018-12-02 16:04:27 -050055 SkFont font;
56 font.setSize(size);
57 font.setTypeface(std::move(typeface));
Mike Reed64670cb2019-04-16 11:37:38 -070058
59 font.textToGlyphs(text.c_str(), text.size(), SkTextEncoding::kUTF8, glyphs.get(), cnt);
60
Brian Salomona0ba7142017-11-20 13:17:43 -050061 std::unique_ptr<SkScalar[]> widths(new SkScalar[cnt]);
Mike Reed94cca602018-12-02 16:04:27 -050062 font.getWidths(glyphs.get(), cnt, widths.get());
Brian Salomona0ba7142017-11-20 13:17:43 -050063
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 Reed94cca602018-12-02 16:04:27 -050070 target->drawText(glyphs.get(), positions.get(), cnt, color, *atlas_font);
Brian Salomona0ba7142017-11-20 13:17:43 -050071
Brian Salomonb5086962017-12-13 10:59:33 -050072 // Return the width of the of draw.
73 return positions[cnt - 1].fX + widths[cnt - 1] - positions[0].fX;
Brian Salomoncbcb0a12017-11-19 13:20:13 -050074}
75
Mike Klein16b1efb2019-04-02 10:01:11 -040076class AtlasTextGM : public skiagm::GpuGM {
Brian Salomoncbcb0a12017-11-19 13:20:13 -050077public:
78 AtlasTextGM() = default;
79
80protected:
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 Phillipsa3f70262018-02-08 10:59:38 -050092 if (!targetHandle) {
93 return;
94 }
Robert Phillipsc994a932018-06-19 13:09:54 -040095
Brian Salomoncbcb0a12017-11-19 13:20:13 -050096 fTarget = SkAtlasTextTarget::Make(fContext, kSize, kSize, targetHandle);
97
Mike Kleinea3f0142019-03-20 11:12:10 -050098 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 Salomoncbcb0a12017-11-19 13:20:13 -0500104 }
105
Mike Klein16b1efb2019-04-02 10:01:11 -0400106 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 Dalton50e24d72019-02-07 16:20:09 -0700116 return DrawResult::kFail;
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500117 }
Brian Salomon40dc8a72017-11-20 11:01:54 -0500118 fRenderer->clearTarget(fTarget->handle(), 0xFF808080);
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500119 auto bmp = this->drawText();
120 SkPaint paint;
121 paint.setBlendMode(SkBlendMode::kSrc);
122 canvas->drawBitmap(bmp, 0, 0);
Chris Dalton50e24d72019-02-07 16:20:09 -0700123 return DrawResult::kOk;
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500124 }
125
126private:
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 Salomonb5086962017-12-13 10:59:33 -0500143 // 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 Salomoncbcb0a12017-11-19 13:20:13 -0500163 x = SkScalarCeilToScalar(x);
Brian Salomonb5086962017-12-13 10:59:33 -0500164 fTarget->restore();
165 // Flush periodically to test continued drawing after a flush.
166 if ((random.nextU() % 8) == 0) {
Brian Salomoncf1aa532017-11-29 14:58:59 -0500167 fTarget->flush();
168 }
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500169 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
193constexpr int AtlasTextGM::kSize;
194
195//////////////////////////////////////////////////////////////////////////////
196
197DEF_GM(return new AtlasTextGM;)
198
199#endif