Herb Derby | c27d535 | 2020-08-12 13:58:34 -0400 | [diff] [blame] | 1 | /* |
| 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 Derby | c27d535 | 2020-08-12 13:58:34 -0400 | [diff] [blame] | 15 | #include "src/gpu/SkGr.h" |
Herb Derby | d5a6764 | 2021-04-16 09:07:24 -0400 | [diff] [blame] | 16 | #include "src/gpu/text/GrStrikeCache.h" |
Herb Derby | c27d535 | 2020-08-12 13:58:34 -0400 | [diff] [blame] | 17 | #include "src/gpu/text/GrTextBlob.h" |
| 18 | #include "src/utils/SkUTF.h" |
| 19 | |
| 20 | |
| 21 | // From Project Guttenberg. This is UTF-8 text. |
| 22 | static const char* gText = |
| 23 | "Call me Ishmael. Some years ago--never mind how long precisely"; |
| 24 | |
| 25 | class 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 Derby | 3c1ed9c | 2021-04-13 16:13:54 -0400 | [diff] [blame] | 42 | auto glyphRunList = builder.textToGlyphRunList(font, paint, gText, len, {100, 100}); |
Herb Derby | c27d535 | 2020-08-12 13:58:34 -0400 | [diff] [blame] | 43 | SkASSERT(!glyphRunList.empty()); |
Ben Wagner | ae4bb98 | 2020-09-24 14:49:00 -0400 | [diff] [blame] | 44 | SkSurfaceProps props; |
| 45 | if (canvas) { canvas->getProps(&props); } |
Herb Derby | 63fe8e5 | 2021-03-08 13:22:56 -0500 | [diff] [blame] | 46 | |
Herb Derby | c27d535 | 2020-08-12 13:58:34 -0400 | [diff] [blame] | 47 | auto colorSpace = SkColorSpace::MakeSRGB(); |
| 48 | SkGlyphRunListPainter painter{props, kUnknown_SkColorType, |
| 49 | colorSpace.get(), SkStrikeCache::GlobalStrikeCache()}; |
Herb Derby | 861ef8f | 2021-02-26 16:49:06 -0500 | [diff] [blame] | 50 | SkMatrix drawMatrix = view; |
Herb Derby | 63fe8e5 | 2021-03-08 13:22:56 -0500 | [diff] [blame] | 51 | const SkPoint drawOrigin = glyphRunList.origin(); |
Herb Derby | 861ef8f | 2021-02-26 16:49:06 -0500 | [diff] [blame] | 52 | drawMatrix.preTranslate(drawOrigin.x(), drawOrigin.y()); |
Herb Derby | 63fe8e5 | 2021-03-08 13:22:56 -0500 | [diff] [blame] | 53 | GrSDFTControl control{false, props.isUseDeviceIndependentFonts(), 256, 256}; |
Herb Derby | 0da2c14 | 2021-03-22 15:28:23 -0400 | [diff] [blame] | 54 | fBlob = GrTextBlob::Make(glyphRunList, paint, drawMatrix, control, &painter); |
Herb Derby | c27d535 | 2020-08-12 13:58:34 -0400 | [diff] [blame] | 55 | |
Herb Derby | 55f795e | 2021-02-05 13:45:05 -0500 | [diff] [blame] | 56 | SkASSERT(!fBlob->subRunList().isEmpty()); |
| 57 | GrAtlasSubRun* subRun = fBlob->subRunList().front().testingOnly_atlasSubRun(); |
Herb Derby | d90024d | 2020-11-20 10:21:32 -0500 | [diff] [blame] | 58 | SkASSERT(subRun); |
Herb Derby | c27d535 | 2020-08-12 13:58:34 -0400 | [diff] [blame] | 59 | subRun->testingOnly_packedGlyphIDToGrGlyph(&fCache); |
Herb Derby | 861ef8f | 2021-02-26 16:49:06 -0500 | [diff] [blame] | 60 | fVertices.reset(new char[subRun->vertexStride(drawMatrix) * subRun->glyphCount() * 4]); |
Herb Derby | c27d535 | 2020-08-12 13:58:34 -0400 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void onDraw(int loops, SkCanvas* canvas) override { |
Herb Derby | 55f795e | 2021-02-05 13:45:05 -0500 | [diff] [blame] | 64 | GrAtlasSubRun* subRun = fBlob->subRunList().front().testingOnly_atlasSubRun(); |
Herb Derby | d90024d | 2020-11-20 10:21:32 -0500 | [diff] [blame] | 65 | SkASSERT(subRun); |
Herb Derby | c27d535 | 2020-08-12 13:58:34 -0400 | [diff] [blame] | 66 | |
| 67 | SkIRect clip = SkIRect::MakeEmpty(); |
| 68 | SkPaint paint; |
| 69 | GrColor grColor = SkColorToPremulGrColor(paint.getColor()); |
Herb Derby | 4089418 | 2020-12-02 11:39:48 -0500 | [diff] [blame] | 70 | SkMatrix positionMatrix = SkMatrix::Translate(100, 100); |
Herb Derby | c27d535 | 2020-08-12 13:58:34 -0400 | [diff] [blame] | 71 | |
| 72 | for (int loop = 0; loop < loops; loop++) { |
| 73 | subRun->fillVertexData(fVertices.get(), 0, subRun->glyphCount(), |
Herb Derby | 4089418 | 2020-12-02 11:39:48 -0500 | [diff] [blame] | 74 | grColor, positionMatrix, clip); |
Herb Derby | c27d535 | 2020-08-12 13:58:34 -0400 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | sk_sp<GrTextBlob> fBlob; |
| 80 | GrStrikeCache fCache; |
| 81 | std::unique_ptr<char[]> fVertices; |
| 82 | }; |
| 83 | |
| 84 | DEF_BENCH(return new DirectMaskGlyphVertexFillBenchmark{}); |