| 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 "include/core/SkCanvas.h" |
| 9 |
#include "include/core/SkFontMetrics.h" |
| 10 |
#include "include/core/SkFontMgr.h" |
| 11 |
#include "include/core/SkTextBlob.h" |
| 12 |
#include "include/core/SkTypeface.h" |
| 13 |
#include "src/base/SkRandom.h" |
| 14 |
#include "src/core/SkTHash.h" |
| 15 |
#include "tools/Resources.h" |
| 16 |
#include "tools/ToolUtils.h" |
| 17 |
#include "tools/fonts/FontToolUtils.h" |
| 18 |
#include "tools/timer/TimeUtils.h" |
| 19 |
#include "tools/viewer/Slide.h" |
| 20 |
|
| 21 |
#if defined(SK_GANESH) |
| 22 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 23 |
#include "src/gpu/AtlasTypes.h" |
| 24 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 25 |
|
| 26 |
using MaskFormat = skgpu::MaskFormat; |
| 27 |
#endif |
| 28 |
|
| 29 |
static sk_sp<SkTypeface> chinese_typeface() { |
| 30 |
#ifdef SK_BUILD_FOR_ANDROID |
| 31 |
return ToolUtils::CreateTypefaceFromResource("fonts/NotoSansCJK-Regular.ttc"); |
| 32 |
#elif defined(SK_BUILD_FOR_WIN) |
| 33 |
return ToolUtils::CreateTestTypeface("SimSun", SkFontStyle()); |
| 34 |
#elif defined(SK_BUILD_FOR_MAC) |
| 35 |
return ToolUtils::CreateTestTypeface("Hiragino Sans GB W3", SkFontStyle()); |
| 36 |
#elif defined(SK_BUILD_FOR_IOS) |
| 37 |
return ToolUtils::CreateTestTypeface("Hiragino Sans GB W3", SkFontStyle()); |
| 38 |
#elif defined(SK_BUILD_FOR_UNIX) |
| 39 |
return ToolUtils::CreateTestTypeface("Noto Sans CJK SC", SkFontStyle()); |
| 40 |
#else |
| 41 |
return nullptr; |
| 42 |
#endif |
| 43 |
} |
| 44 |
|
| 45 |
class ChineseFlingSlide : public Slide { |
| 46 |
inline static constexpr int kNumBlobs = 200; |
| 47 |
inline static constexpr int kWordLength = 16; |
| 48 |
|
| 49 |
sk_sp<SkTypeface> fTypeface; |
| 50 |
SkFontMetrics fMetrics; |
| 51 |
sk_sp<SkTextBlob> fBlobs[kNumBlobs]; |
| 52 |
SkRandom fRand; |
| 53 |
int fIndex = 0; |
| 54 |
|
| 55 |
public: |
| 56 |
ChineseFlingSlide() { fName = "chinese-fling"; } |
| 57 |
|
| 58 |
void draw(SkCanvas* canvas) override { |
| 59 |
canvas->clear(0xFFDDDDDD); |
| 60 |
|
| 61 |
SkPaint paint; |
| 62 |
paint.setColor(0xDE000000); |
| 63 |
|
| 64 |
// draw a consistent run of the 'words' - one word per line |
| 65 |
int index = fIndex; |
| 66 |
for (SkScalar y = 0.0f; y < 1024.0f; ) { |
| 67 |
|
| 68 |
y += -fMetrics.fAscent; |
| 69 |
canvas->drawTextBlob(fBlobs[index], 0, y, paint); |
| 70 |
|
| 71 |
y += fMetrics.fDescent + fMetrics.fLeading; |
| 72 |
++index; |
| 73 |
index %= kNumBlobs; |
| 74 |
} |
| 75 |
// now "fling" a random amount |
| 76 |
fIndex += fRand.nextRangeU(5, 20); |
| 77 |
fIndex %= kNumBlobs; |
| 78 |
} |
| 79 |
|
| 80 |
void load(SkScalar w, SkScalar h) override { |
| 81 |
fTypeface = chinese_typeface(); |
| 82 |
|
| 83 |
SkFont font(fTypeface, 56); |
| 84 |
font.getMetrics(&fMetrics); |
| 85 |
|
| 86 |
SkUnichar glyphs[kWordLength]; |
| 87 |
for (int32_t i = 0; i < kNumBlobs; ++i) { |
| 88 |
this->createRandomWord(glyphs); |
| 89 |
|
| 90 |
SkTextBlobBuilder builder; |
| 91 |
ToolUtils::add_to_text_blob_w_len(&builder, |
| 92 |
(const char*)glyphs, |
| 93 |
kWordLength * 4, |
| 94 |
SkTextEncoding::kUTF32, |
| 95 |
font, |
| 96 |
0, |
| 97 |
0); |
| 98 |
|
| 99 |
fBlobs[i] = builder.make(); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// Construct a random kWordLength character 'word' drawing from the full Chinese set |
| 104 |
void createRandomWord(SkUnichar glyphs[kWordLength]) { |
| 105 |
for (int i = 0; i < kWordLength; ++i) { |
| 106 |
glyphs[i] = fRand.nextRangeU(0x4F00, 0x9FA0); |
| 107 |
} |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
class ChineseZoomSlide : public Slide { |
| 112 |
inline static constexpr int kNumBlobs = 8; |
| 113 |
inline static constexpr int kParagraphLength = 175; |
| 114 |
|
| 115 |
bool fAfterFirstFrame = false; |
| 116 |
sk_sp<SkTypeface> fTypeface; |
| 117 |
SkFontMetrics fMetrics; |
| 118 |
sk_sp<SkTextBlob> fBlobs[kNumBlobs]; |
| 119 |
SkRandom fRand; |
| 120 |
SkScalar fScale = 15; |
| 121 |
SkScalar fTranslate = 0; |
| 122 |
|
| 123 |
public: |
| 124 |
ChineseZoomSlide() { fName = "chinese-zoom"; } |
| 125 |
|
| 126 |
bool onChar(SkUnichar uni) override { |
| 127 |
if ('>' == uni) { |
| 128 |
fScale += 0.125f; |
| 129 |
return true; |
| 130 |
} |
| 131 |
if ('<' == uni) { |
| 132 |
fScale -= 0.125f; |
| 133 |
return true; |
| 134 |
} |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
void draw(SkCanvas* canvas) override { |
| 139 |
canvas->clear(0xFFDDDDDD); |
| 140 |
|
| 141 |
SkPaint paint; |
| 142 |
paint.setAntiAlias(true); |
| 143 |
paint.setColor(0xDE000000); |
| 144 |
|
| 145 |
if (fAfterFirstFrame) { |
| 146 |
#if defined(SK_GANESH) |
| 147 |
auto direct = GrAsDirectContext(canvas->recordingContext()); |
| 148 |
if (direct) { |
| 149 |
sk_sp<SkImage> image = direct->priv().testingOnly_getFontAtlasImage(MaskFormat::kA8, |
| 150 |
0); |
| 151 |
canvas->drawImageRect(image, |
| 152 |
SkRect::MakeXYWH(10.0f, 10.0f, 512.0f, 512.0), |
| 153 |
SkSamplingOptions(), &paint); |
| 154 |
image = direct->priv().testingOnly_getFontAtlasImage(MaskFormat::kA8, 1); |
| 155 |
canvas->drawImageRect(image, |
| 156 |
SkRect::MakeXYWH(522.0f, 10.0f, 512.f, 512.0f), |
| 157 |
SkSamplingOptions(), &paint); |
| 158 |
image = direct->priv().testingOnly_getFontAtlasImage(MaskFormat::kA8, 2); |
| 159 |
canvas->drawImageRect(image, |
| 160 |
SkRect::MakeXYWH(10.0f, 522.0f, 512.0f, 512.0f), |
| 161 |
SkSamplingOptions(), &paint); |
| 162 |
image = direct->priv().testingOnly_getFontAtlasImage(MaskFormat::kA8, 3); |
| 163 |
canvas->drawImageRect(image, |
| 164 |
SkRect::MakeXYWH(522.0f, 522.0f, 512.0f, 512.0f), |
| 165 |
SkSamplingOptions(), &paint); |
| 166 |
} |
| 167 |
#endif |
| 168 |
} |
| 169 |
|
| 170 |
canvas->scale(fScale, fScale); |
| 171 |
canvas->translate(0, fTranslate); |
| 172 |
fTranslate -= 0.5f; |
| 173 |
|
| 174 |
// draw a consistent run of the 'words' - one word per line |
| 175 |
SkScalar y = 0; |
| 176 |
for (int index = 0; index < kNumBlobs; ++index) { |
| 177 |
y += -fMetrics.fAscent; |
| 178 |
canvas->drawTextBlob(fBlobs[index], 0, y, paint); |
| 179 |
|
| 180 |
y += 3*(fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading); |
| 181 |
} |
| 182 |
if (!fAfterFirstFrame) { |
| 183 |
fAfterFirstFrame = true; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
void load(SkScalar w, SkScalar h) override { |
| 188 |
fTypeface = chinese_typeface(); |
| 189 |
|
| 190 |
SkFont font(fTypeface, 11); |
| 191 |
font.getMetrics(&fMetrics); |
| 192 |
|
| 193 |
SkPaint paint; |
| 194 |
paint.setColor(0xDE000000); |
| 195 |
|
| 196 |
SkUnichar glyphs[45]; |
| 197 |
for (int32_t i = 0; i < kNumBlobs; ++i) { |
| 198 |
SkTextBlobBuilder builder; |
| 199 |
auto paragraphLength = kParagraphLength; |
| 200 |
SkScalar y = 0; |
| 201 |
while (paragraphLength - 45 > 0) { |
| 202 |
auto currentLineLength = std::min(45, paragraphLength - 45); |
| 203 |
this->createRandomLine(glyphs, currentLineLength); |
| 204 |
|
| 205 |
ToolUtils::add_to_text_blob_w_len(&builder, |
| 206 |
(const char*)glyphs, |
| 207 |
currentLineLength * 4, |
| 208 |
SkTextEncoding::kUTF32, |
| 209 |
font, |
| 210 |
0, |
| 211 |
y); |
| 212 |
y += fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading; |
| 213 |
paragraphLength -= 45; |
| 214 |
} |
| 215 |
fBlobs[i] = builder.make(); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
// Construct a random kWordLength character 'word' drawing from the full Chinese set |
| 220 |
void createRandomLine(SkUnichar glyphs[45], int lineLength) { |
| 221 |
for (auto i = 0; i < lineLength; ++i) { |
| 222 |
glyphs[i] = fRand.nextRangeU(0x4F00, 0x9FA0); |
| 223 |
} |
| 224 |
} |
| 225 |
}; |
| 226 |
|
| 227 |
/** |
| 228 |
* Scrolls a large number of unique glyphs so the set of glyphs that need to be active in the cache |
| 229 |
* evolves over time and glyphs age out of being needed in current frame. |
| 230 |
*/ |
| 231 |
class ChineseScrollSlide : public Slide { |
| 232 |
static constexpr int kNumBlobs = 50; |
| 233 |
static constexpr int kLinesPerBlob = 6; |
| 234 |
static constexpr int kLineLength = 45; |
| 235 |
static constexpr float kFontSize = 25; |
| 236 |
|
| 237 |
static constexpr int kDefaultScrollUnitsPerSecondLog2 = 7; |
| 238 |
|
| 239 |
sk_sp<SkTypeface> fTypeface; |
| 240 |
sk_sp<SkTextBlob> fBlobs[kNumBlobs]; |
| 241 |
float fVerticalPeriod; |
| 242 |
SkRandom fRand; |
| 243 |
float fTranslate = 0; |
| 244 |
double fPrevNs = std::numeric_limits<double>::infinity(); |
| 245 |
// The scroll speed is sign(value)*2^abs(value) where sign is tri-state (-1, 0, or 1) |
| 246 |
int fScrollUnitsPerSecondLog2 = kDefaultScrollUnitsPerSecondLog2; |
| 247 |
|
| 248 |
public: |
| 249 |
ChineseScrollSlide() { fName = "chinese-scroll"; } |
| 250 |
|
| 251 |
bool onChar(SkUnichar uni) override { |
| 252 |
if (',' == uni) { |
| 253 |
--fScrollUnitsPerSecondLog2; |
| 254 |
return true; |
| 255 |
} |
| 256 |
if ('.' == uni) { |
| 257 |
++fScrollUnitsPerSecondLog2; |
| 258 |
return true; |
| 259 |
} |
| 260 |
return false; |
| 261 |
} |
| 262 |
|
| 263 |
bool animate(double ns) override { |
| 264 |
if (!fScrollUnitsPerSecondLog2) { |
| 265 |
fPrevNs = std::numeric_limits<double>::infinity(); |
| 266 |
return false; |
| 267 |
} |
| 268 |
if (!std::isfinite(fPrevNs)) { |
| 269 |
fPrevNs = ns; |
| 270 |
return true; |
| 271 |
} |
| 272 |
float scrollUnitsPerSecond = std::pow(2, std::abs(fScrollUnitsPerSecondLog2)); |
| 273 |
if (fScrollUnitsPerSecondLog2 < 0) { |
| 274 |
scrollUnitsPerSecond = -scrollUnitsPerSecond; |
| 275 |
} |
| 276 |
float dt = TimeUtils::NanosToSeconds(ns - fPrevNs) * scrollUnitsPerSecond; |
| 277 |
fTranslate -= dt; |
| 278 |
fPrevNs = ns; |
| 279 |
return true; |
| 280 |
} |
| 281 |
|
| 282 |
void draw(SkCanvas* canvas) override { |
| 283 |
canvas->clear(0xFFDDDDDD); |
| 284 |
|
| 285 |
SkPaint paint; |
| 286 |
paint.setAntiAlias(true); |
| 287 |
paint.setColor(0xDE000000); |
| 288 |
|
| 289 |
canvas->translate(0, fTranslate); |
| 290 |
|
| 291 |
// We want the text to repeat in y. If we assume the total height of the text |
| 292 |
// is larger than the window height then we need at most 2 repetitions to |
| 293 |
// cover the window. |
| 294 |
float n = std::floor(-fTranslate / fVerticalPeriod); |
| 295 |
float y = n * fVerticalPeriod; |
| 296 |
for (int i = 0; i < 2; ++i) { |
| 297 |
for (int b = 0; b < kNumBlobs; ++b) { |
| 298 |
canvas->drawTextBlob(fBlobs[b], 0, y, paint); |
| 299 |
} |
| 300 |
y += fVerticalPeriod; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
void load(SkScalar w, SkScalar h) override { |
| 305 |
fTypeface = chinese_typeface(); |
| 306 |
|
| 307 |
SkFont font(fTypeface, kFontSize); |
| 308 |
SkFontMetrics metrics; |
| 309 |
font.getMetrics(&metrics); |
| 310 |
|
| 311 |
SkPaint paint; |
| 312 |
paint.setColor(0xDE000000); |
| 313 |
|
| 314 |
skia_private::THashSet<SkUnichar> usedGlyphs; |
| 315 |
float y = 0; |
| 316 |
for (int i = 0; i < kNumBlobs; ++i) { |
| 317 |
SkTextBlobBuilder builder; |
| 318 |
for (int j = 0; j < kLinesPerBlob; ++j) { |
| 319 |
SkUnichar glyphs[kLineLength]; |
| 320 |
this->createRandomLine(glyphs, kLineLength, usedGlyphs); |
| 321 |
|
| 322 |
ToolUtils::add_to_text_blob_w_len(&builder, |
| 323 |
(const char*)glyphs, |
| 324 |
kLineLength * 4, |
| 325 |
SkTextEncoding::kUTF32, |
| 326 |
font, |
| 327 |
0, |
| 328 |
y); |
| 329 |
y += metrics.fDescent - metrics.fAscent + metrics.fLeading; |
| 330 |
} |
| 331 |
fBlobs[i] = builder.make(); |
| 332 |
} |
| 333 |
fVerticalPeriod = std::ceil(y); |
| 334 |
} |
| 335 |
|
| 336 |
// Construct a random lineLength character 'word' drawing from the full Chinese set |
| 337 |
// but not using glyphs in usedGlyphs. |
| 338 |
void createRandomLine(SkUnichar glyphs[], int lineLength, auto& usedGlyphs) { |
| 339 |
for (auto i = 0; i < lineLength; ++i) { |
| 340 |
do { |
| 341 |
glyphs[i] = fRand.nextRangeU(0x4F00, 0x9FA0); |
| 342 |
} while (usedGlyphs.contains(glyphs[i])); |
| 343 |
usedGlyphs.add(glyphs[i]); |
| 344 |
} |
| 345 |
} |
| 346 |
}; |
| 347 |
|
| 348 |
////////////////////////////////////////////////////////////////////////////// |
| 349 |
|
| 350 |
DEF_SLIDE( return new ChineseFlingSlide(); ) |
| 351 |
DEF_SLIDE( return new ChineseZoomSlide(); ) |
| 352 |
DEF_SLIDE( return new ChineseScrollSlide(); ) |