• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2024 Google LLC
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 "tools/gpu/ganesh/AtlasTextOpTools.h"
9 
10 #include "include/core/SkFont.h"
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkPaint.h"
13 #include "src/core/SkScalerContext.h"
14 #include "src/core/SkStrikeCache.h"
15 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
16 #include "src/gpu/ganesh/SurfaceDrawContext.h"
17 #include "src/text/GlyphRun.h"
18 #include "src/text/gpu/TextBlob.h"
19 #include "tools/text/gpu/TextBlobTools.h"
20 
21 #if defined(GR_TEST_UTILS)
22 #include "src/base/SkRandom.h"
23 #include "src/gpu/ganesh/GrDrawOpTest.h"
24 #endif
25 
26 namespace skgpu::ganesh {
27 
CreateOp(skgpu::ganesh::SurfaceDrawContext * sdc,const SkPaint & skPaint,const SkFont & font,const SkMatrix & ctm,const char * text,int x,int y)28 GrOp::Owner AtlasTextOpTools::CreateOp(skgpu::ganesh::SurfaceDrawContext* sdc,
29                                        const SkPaint& skPaint,
30                                        const SkFont& font,
31                                        const SkMatrix& ctm,
32                                        const char* text,
33                                        int x,
34                                        int y) {
35     size_t textLen = (int)strlen(text);
36 
37     SkMatrix drawMatrix = ctm;
38     drawMatrix.preTranslate(x, y);
39     auto drawOrigin = SkPoint::Make(x, y);
40     sktext::GlyphRunBuilder builder;
41     auto glyphRunList = builder.textToGlyphRunList(font, skPaint, text, textLen, drawOrigin);
42     if (glyphRunList.empty()) {
43         return nullptr;
44     }
45 
46     auto rContext = sdc->recordingContext();
47     sktext::gpu::SDFTControl control =
48             rContext->priv().getSDFTControl(sdc->surfaceProps().isUseDeviceIndependentFonts());
49 
50     SkStrikeDeviceInfo strikeDeviceInfo{
51             sdc->surfaceProps(), SkScalerContextFlags::kBoostContrast, &control};
52 
53     sk_sp<sktext::gpu::TextBlob> blob =
54             sktext::gpu::TextBlob::Make(glyphRunList,
55                                         skPaint,
56                                         drawMatrix,
57                                         strikeDeviceInfo,
58                                         SkStrikeCache::GlobalStrikeCache());
59 
60     const sktext::gpu::AtlasSubRun* subRun = sktext::gpu::TextBlobTools::FirstSubRun(blob.get());
61     if (!subRun) {
62         return nullptr;
63     }
64 
65     GrOp::Owner op;
66     std::tie(std::ignore, op) =
67             subRun->makeAtlasTextOp(nullptr, ctm, glyphRunList.origin(), skPaint, blob, sdc);
68     return op;
69 }
70 
71 }  // namespace skgpu::ganesh
72 
73 #if defined(GR_TEST_UTILS)
GR_DRAW_OP_TEST_DEFINE(AtlasTextOp)74 GR_DRAW_OP_TEST_DEFINE(AtlasTextOp) {
75     SkMatrix ctm = GrTest::TestMatrixInvertible(random);
76 
77     SkPaint skPaint;
78     skPaint.setColor(random->nextU());
79 
80     SkFont font;
81     if (random->nextBool()) {
82         font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
83     } else {
84         font.setEdging(random->nextBool() ? SkFont::Edging::kAntiAlias : SkFont::Edging::kAlias);
85     }
86     font.setSubpixel(random->nextBool());
87 
88     const char* text = "The quick brown fox jumps over the lazy dog.";
89 
90     // create some random x/y offsets, including negative offsets
91     static const int kMaxTrans = 1024;
92     int xPos = (random->nextU() % 2) * 2 - 1;
93     int yPos = (random->nextU() % 2) * 2 - 1;
94     int xInt = (random->nextU() % kMaxTrans) * xPos;
95     int yInt = (random->nextU() % kMaxTrans) * yPos;
96 
97     return skgpu::ganesh::AtlasTextOpTools::CreateOp(sdc, skPaint, font, ctm, text, xInt, yInt);
98 }
99 #endif
100