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/SkStrikeCache.h" 14 #include "src/core/SkUtils.h" 15 #include "src/gpu/GrRecordingContextPriv.h" 16 #include "src/gpu/SkGr.h" 17 #include "src/gpu/text/GrStrikeCache.h" 18 #include "src/gpu/text/GrTextBlob.h" 19 #include "src/utils/SkUTF.h" 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 { isSuitableFor(Backend backend)26 bool isSuitableFor(Backend backend) override { 27 return backend == kGPU_Backend; 28 } 29 onGetName()30 const char* onGetName() override { 31 return "DirectMaskGlyphVertexFillBenchmark"; 32 } 33 onPerCanvasPreDraw(SkCanvas * canvas)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 auto glyphRunList = builder.textToGlyphRunList(font, paint, gText, len, {100, 100}); 43 SkASSERT(!glyphRunList.empty()); 44 SkSurfaceProps props; 45 if (canvas) { canvas->getProps(&props); } 46 47 auto colorSpace = SkColorSpace::MakeSRGB(); 48 SkGlyphRunListPainter painter{props, kUnknown_SkColorType, 49 colorSpace.get(), SkStrikeCache::GlobalStrikeCache()}; 50 SkMatrix drawMatrix = view; 51 const SkPoint drawOrigin = glyphRunList.origin(); 52 drawMatrix.preTranslate(drawOrigin.x(), drawOrigin.y()); 53 GrSDFTControl control{false, props.isUseDeviceIndependentFonts(), 256, 256}; 54 fBlob = GrTextBlob::Make(glyphRunList, paint, drawMatrix, false, control, &painter); 55 56 const GrAtlasSubRun* subRun = fBlob->testingOnlyFirstSubRun(); 57 SkASSERT(subRun); 58 subRun->testingOnly_packedGlyphIDToGrGlyph(&fCache); 59 fVertices.reset(new char[subRun->vertexStride(drawMatrix) * subRun->glyphCount() * 4]); 60 } 61 onDraw(int loops,SkCanvas * canvas)62 void onDraw(int loops, SkCanvas* canvas) override { 63 const GrAtlasSubRun* subRun = fBlob->testingOnlyFirstSubRun(); 64 SkASSERT(subRun); 65 66 SkIRect clip = SkIRect::MakeEmpty(); 67 SkPaint paint; 68 GrColor grColor = SkColorToPremulGrColor(paint.getColor()); 69 SkMatrix positionMatrix = SkMatrix::Translate(100, 100); 70 71 for (int loop = 0; loop < loops; loop++) { 72 subRun->fillVertexData(fVertices.get(), 0, subRun->glyphCount(), 73 grColor, positionMatrix, {0, 0}, clip); 74 } 75 } 76 77 private: 78 sk_sp<GrTextBlob> fBlob; 79 GrStrikeCache fCache; 80 std::unique_ptr<char[]> fVertices; 81 }; 82 83 DEF_BENCH(return new DirectMaskGlyphVertexFillBenchmark{}); 84