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