blob: a53679c7d6e536d1898a0b017ba06f5501bdb8f5 [file] [log] [blame]
Herb Derbyc27d5352020-08-12 13:58:34 -04001/*
2 * Copyright 2020 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 "bench/Benchmark.h"
9#include "include/core/SkFont.h"
10#include "include/core/SkTypeface.h"
11#include "include/gpu/GrDirectContext.h"
12#include "include/gpu/GrRecordingContext.h"
13#include "src/core/SkUtils.h"
14#include "src/gpu/GrRecordingContextPriv.h"
Herb Derbyc27d5352020-08-12 13:58:34 -040015#include "src/gpu/SkGr.h"
Herb Derbyd5a67642021-04-16 09:07:24 -040016#include "src/gpu/text/GrStrikeCache.h"
Herb Derbyc27d5352020-08-12 13:58:34 -040017#include "src/gpu/text/GrTextBlob.h"
18#include "src/utils/SkUTF.h"
19
20
21// From Project Guttenberg. This is UTF-8 text.
22static const char* gText =
23 "Call me Ishmael. Some years ago--never mind how long precisely";
24
25class DirectMaskGlyphVertexFillBenchmark : public Benchmark {
26 bool isSuitableFor(Backend backend) override {
27 return backend == kGPU_Backend;
28 }
29
30 const char* onGetName() override {
31 return "DirectMaskGlyphVertexFillBenchmark";
32 }
33
34 void onPerCanvasPreDraw(SkCanvas* canvas) override {
35 auto typeface = SkTypeface::MakeFromName("monospace", SkFontStyle());
36 SkFont font(typeface);
37
38 SkMatrix view = SkMatrix::I();
39 size_t len = strlen(gText);
40 SkGlyphRunBuilder builder;
41 SkPaint paint;
Herb Derby3c1ed9c2021-04-13 16:13:54 -040042 auto glyphRunList = builder.textToGlyphRunList(font, paint, gText, len, {100, 100});
Herb Derbyc27d5352020-08-12 13:58:34 -040043 SkASSERT(!glyphRunList.empty());
Ben Wagnerae4bb982020-09-24 14:49:00 -040044 SkSurfaceProps props;
45 if (canvas) { canvas->getProps(&props); }
Herb Derby63fe8e52021-03-08 13:22:56 -050046
Herb Derbyc27d5352020-08-12 13:58:34 -040047 auto colorSpace = SkColorSpace::MakeSRGB();
48 SkGlyphRunListPainter painter{props, kUnknown_SkColorType,
49 colorSpace.get(), SkStrikeCache::GlobalStrikeCache()};
Herb Derby861ef8f2021-02-26 16:49:06 -050050 SkMatrix drawMatrix = view;
Herb Derby63fe8e52021-03-08 13:22:56 -050051 const SkPoint drawOrigin = glyphRunList.origin();
Herb Derby861ef8f2021-02-26 16:49:06 -050052 drawMatrix.preTranslate(drawOrigin.x(), drawOrigin.y());
Herb Derby63fe8e52021-03-08 13:22:56 -050053 GrSDFTControl control{false, props.isUseDeviceIndependentFonts(), 256, 256};
Herb Derby0da2c142021-03-22 15:28:23 -040054 fBlob = GrTextBlob::Make(glyphRunList, paint, drawMatrix, control, &painter);
Herb Derbyc27d5352020-08-12 13:58:34 -040055
Herb Derby55f795e2021-02-05 13:45:05 -050056 SkASSERT(!fBlob->subRunList().isEmpty());
57 GrAtlasSubRun* subRun = fBlob->subRunList().front().testingOnly_atlasSubRun();
Herb Derbyd90024d2020-11-20 10:21:32 -050058 SkASSERT(subRun);
Herb Derbyc27d5352020-08-12 13:58:34 -040059 subRun->testingOnly_packedGlyphIDToGrGlyph(&fCache);
Herb Derby861ef8f2021-02-26 16:49:06 -050060 fVertices.reset(new char[subRun->vertexStride(drawMatrix) * subRun->glyphCount() * 4]);
Herb Derbyc27d5352020-08-12 13:58:34 -040061 }
62
63 void onDraw(int loops, SkCanvas* canvas) override {
Herb Derby55f795e2021-02-05 13:45:05 -050064 GrAtlasSubRun* subRun = fBlob->subRunList().front().testingOnly_atlasSubRun();
Herb Derbyd90024d2020-11-20 10:21:32 -050065 SkASSERT(subRun);
Herb Derbyc27d5352020-08-12 13:58:34 -040066
67 SkIRect clip = SkIRect::MakeEmpty();
68 SkPaint paint;
69 GrColor grColor = SkColorToPremulGrColor(paint.getColor());
Herb Derby40894182020-12-02 11:39:48 -050070 SkMatrix positionMatrix = SkMatrix::Translate(100, 100);
Herb Derbyc27d5352020-08-12 13:58:34 -040071
72 for (int loop = 0; loop < loops; loop++) {
73 subRun->fillVertexData(fVertices.get(), 0, subRun->glyphCount(),
Herb Derby40894182020-12-02 11:39:48 -050074 grColor, positionMatrix, clip);
Herb Derbyc27d5352020-08-12 13:58:34 -040075 }
76 }
77
78private:
79 sk_sp<GrTextBlob> fBlob;
80 GrStrikeCache fCache;
81 std::unique_ptr<char[]> fVertices;
82};
83
84DEF_BENCH(return new DirectMaskGlyphVertexFillBenchmark{});