blob: 226b83fdd7ea970866adbfc85008cac7e50d36f4 [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
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 Salomon0c1c2b32017-11-20 13:13:01 -050018#include "SkUtils.h"
Brian Salomoncbcb0a12017-11-19 13:20:13 -050019#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
26static SkScalar draw_string(SkAtlasTextTarget* target, const SkString& text, SkScalar x, SkScalar y,
27 uint32_t color, sk_sp<SkTypeface> typeface, float size) {
Brian Salomona0ba7142017-11-20 13:17:43 -050028 if (!text.size()) {
29 return x;
30 }
Brian Salomon0c1c2b32017-11-20 13:13:01 -050031 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 Salomona0ba7142017-11-20 13:17:43 -050036 // Using a paint to get the positions for each glyph.
Brian Salomoncbcb0a12017-11-19 13:20:13 -050037 SkPaint paint;
38 paint.setTextSize(size);
Brian Salomon0c1c2b32017-11-20 13:13:01 -050039 paint.setTypeface(std::move(typeface));
Brian Salomona0ba7142017-11-20 13:17:43 -050040 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 Salomonb5086962017-12-13 10:59:33 -050052 // Return the width of the of draw.
53 return positions[cnt - 1].fX + widths[cnt - 1] - positions[0].fX;
Brian Salomoncbcb0a12017-11-19 13:20:13 -050054}
55
56class AtlasTextGM : public skiagm::GM {
57public:
58 AtlasTextGM() = default;
59
60protected:
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 Phillipsa3f70262018-02-08 10:59:38 -050072 if (!targetHandle) {
73 return;
74 }
Brian Salomoncbcb0a12017-11-19 13:20:13 -050075 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 Phillipsa3f70262018-02-08 10:59:38 -050088 if (!fRenderer || !fTarget || !fTarget->handle()) {
Brian Salomoncbcb0a12017-11-19 13:20:13 -050089 canvas->clear(SK_ColorRED);
90 return;
91 }
Brian Salomon40dc8a72017-11-20 11:01:54 -050092 fRenderer->clearTarget(fTarget->handle(), 0xFF808080);
Brian Salomoncbcb0a12017-11-19 13:20:13 -050093 auto bmp = this->drawText();
94 SkPaint paint;
95 paint.setBlendMode(SkBlendMode::kSrc);
96 canvas->drawBitmap(bmp, 0, 0);
97 }
98
99private:
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 Salomonb5086962017-12-13 10:59:33 -0500116 // 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 Salomoncbcb0a12017-11-19 13:20:13 -0500136 x = SkScalarCeilToScalar(x);
Brian Salomonb5086962017-12-13 10:59:33 -0500137 fTarget->restore();
138 // Flush periodically to test continued drawing after a flush.
139 if ((random.nextU() % 8) == 0) {
Brian Salomoncf1aa532017-11-29 14:58:59 -0500140 fTarget->flush();
141 }
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500142 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
166constexpr int AtlasTextGM::kSize;
167
168//////////////////////////////////////////////////////////////////////////////
169
170DEF_GM(return new AtlasTextGM;)
171
172#endif