blob: 1a251b91643ee44ade34a267f5962b56b7132520 [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"
Brian Salomoneebe7352020-12-09 16:37:04 -050015#include "src/gpu/GrSurfaceDrawContext.h"
Herb Derbyc27d5352020-08-12 13:58:34 -040016#include "src/gpu/SkGr.h"
17#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;
42 builder.drawTextUTF8(paint, font, gText, len, {100, 100});
43 auto glyphRunList = builder.useGlyphRunList();
44 SkASSERT(!glyphRunList.empty());
45 fBlob = GrTextBlob::Make(glyphRunList, view);
Ben Wagnerae4bb982020-09-24 14:49:00 -040046 SkSurfaceProps props;
47 if (canvas) { canvas->getProps(&props); }
Herb Derbyc27d5352020-08-12 13:58:34 -040048 auto colorSpace = SkColorSpace::MakeSRGB();
49 SkGlyphRunListPainter painter{props, kUnknown_SkColorType,
50 colorSpace.get(), SkStrikeCache::GlobalStrikeCache()};
51
52 GrSDFTOptions options{256, 256};
Herb Derby281583a2020-11-19 11:40:07 -050053 const SkPoint drawOrigin = glyphRunList.origin();
54 const SkPaint& drawPaint = glyphRunList.paint();
55 for (auto& glyphRun : glyphRunList) {
56 painter.processGlyphRun(
57 glyphRun, view, drawOrigin, drawPaint, props, false, options, fBlob.get());
58 }
Herb Derbyc27d5352020-08-12 13:58:34 -040059
60 SkASSERT(fBlob->subRunList().head() != nullptr);
Herb Derbyd90024d2020-11-20 10:21:32 -050061 GrAtlasSubRun* subRun = fBlob->subRunList().head()->testingOnly_atlasSubRun();
62 SkASSERT(subRun);
Herb Derbyc27d5352020-08-12 13:58:34 -040063 subRun->testingOnly_packedGlyphIDToGrGlyph(&fCache);
Herb Derby46d1e9f2020-12-01 14:12:04 -050064 fVertices.reset(new char[subRun->vertexStride(view) * subRun->glyphCount() * 4]);
Herb Derbyc27d5352020-08-12 13:58:34 -040065 }
66
67 void onDraw(int loops, SkCanvas* canvas) override {
Herb Derbyd90024d2020-11-20 10:21:32 -050068 GrAtlasSubRun* subRun = fBlob->subRunList().head()->testingOnly_atlasSubRun();
69 SkASSERT(subRun);
Herb Derbyc27d5352020-08-12 13:58:34 -040070
71 SkIRect clip = SkIRect::MakeEmpty();
72 SkPaint paint;
73 GrColor grColor = SkColorToPremulGrColor(paint.getColor());
Herb Derby40894182020-12-02 11:39:48 -050074 SkMatrix positionMatrix = SkMatrix::Translate(100, 100);
Herb Derbyc27d5352020-08-12 13:58:34 -040075
76 for (int loop = 0; loop < loops; loop++) {
77 subRun->fillVertexData(fVertices.get(), 0, subRun->glyphCount(),
Herb Derby40894182020-12-02 11:39:48 -050078 grColor, positionMatrix, clip);
Herb Derbyc27d5352020-08-12 13:58:34 -040079 }
80 }
81
82private:
83 sk_sp<GrTextBlob> fBlob;
84 GrStrikeCache fCache;
85 std::unique_ptr<char[]> fVertices;
86};
87
88DEF_BENCH(return new DirectMaskGlyphVertexFillBenchmark{});