Coverage Report

Created: 2025-06-24 08:20

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