xref: /aosp_15_r20/external/skia/tests/GrGlyphVectorTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2 * Copyright 2022 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 "include/core/SkData.h"
9 #include "include/core/SkFont.h"
10 #include "include/core/SkRefCnt.h"
11 #include "include/core/SkSpan.h"
12 #include "include/core/SkTypes.h"
13 #include "src/base/SkZip.h"
14 #include "src/core/SkDescriptor.h"
15 #include "src/core/SkGlyph.h"
16 #include "src/core/SkReadBuffer.h"
17 #include "src/core/SkStrike.h"
18 #include "src/core/SkStrikeSpec.h"
19 #include "src/core/SkWriteBuffer.h"
20 #include "src/text/StrikeForGPU.h"
21 #include "src/text/gpu/GlyphVector.h"
22 #include "src/text/gpu/SubRunAllocator.h"
23 #include "tests/Test.h"
24 #include "tools/fonts/FontToolUtils.h"
25 
26 #include <initializer_list>
27 #include <limits.h>
28 #include <optional>
29 #include <utility>
30 
31 using GlyphVector = sktext::gpu::GlyphVector;
32 using SubRunAllocator = sktext::gpu::SubRunAllocator;
33 
34 namespace sktext::gpu {
35 class GlyphVectorTestingPeer {
36 public:
GetDescriptor(const GlyphVector & v)37     static const SkDescriptor& GetDescriptor(const GlyphVector& v) {
38         return v.fStrikePromise.descriptor();
39     }
GetGlyphs(const GlyphVector & v)40     static SkSpan<GlyphVector::Variant> GetGlyphs(const GlyphVector& v) {
41         return v.fGlyphs;
42     }
43 };
44 
DEF_TEST(GlyphVector_Serialization,r)45 DEF_TEST(GlyphVector_Serialization, r) {
46     SkFont font = ToolUtils::DefaultFont();
47     auto [strikeSpec, _] = SkStrikeSpec::MakeCanonicalized(font);
48 
49     SubRunAllocator alloc;
50 
51     const int N = 10;
52     SkPackedGlyphID* glyphs = alloc.makePODArray<SkPackedGlyphID>(N);
53     for (int i = 0; i < N; i++) {
54         glyphs[i] = SkPackedGlyphID(SkGlyphID(i));
55     }
56 
57     SkStrikePromise promise{strikeSpec.findOrCreateStrike()};
58 
59     GlyphVector src = GlyphVector::Make(std::move(promise), SkSpan(glyphs, N), &alloc);
60 
61     SkBinaryWriteBuffer wBuffer({});
62     src.flatten(wBuffer);
63 
64     auto data = wBuffer.snapshotAsData();
65     SkReadBuffer rBuffer{data->data(), data->size()};
66     auto dst = GlyphVector::MakeFromBuffer(rBuffer, nullptr, &alloc);
67     REPORTER_ASSERT(r, dst.has_value());
68     REPORTER_ASSERT(r,
69                     GlyphVectorTestingPeer::GetDescriptor(src) ==
70                             GlyphVectorTestingPeer::GetDescriptor(*dst));
71 
72     auto srcGlyphs = GlyphVectorTestingPeer::GetGlyphs(src);
73     auto dstGlyphs = GlyphVectorTestingPeer::GetGlyphs(*dst);
74     for (auto [srcGlyphID, dstGlyphID] : SkMakeZip(srcGlyphs, dstGlyphs)) {
75         REPORTER_ASSERT(r, srcGlyphID.packedGlyphID == dstGlyphID.packedGlyphID);
76     }
77 }
78 
DEF_TEST(GlyphVector_BadLengths,r)79 DEF_TEST(GlyphVector_BadLengths, r) {
80     auto [strikeSpec, _] = SkStrikeSpec::MakeCanonicalized(ToolUtils::DefaultFont());
81 
82     // Strike to keep in the strike cache.
83     auto strike = strikeSpec.findOrCreateStrike();
84 
85     // Be sure to keep the strike alive. The promise to serialize as the first part of the
86     // GlyphVector.
87     SkStrikePromise promise{sk_sp<SkStrike>(strike)};
88     {
89         // Make broken stream by hand - zero length
90         SkBinaryWriteBuffer wBuffer({});
91         promise.flatten(wBuffer);
92         wBuffer.write32(0);  // length
93         auto data = wBuffer.snapshotAsData();
94         SkReadBuffer rBuffer{data->data(), data->size()};
95         SubRunAllocator alloc;
96         auto dst = GlyphVector::MakeFromBuffer(rBuffer, nullptr, &alloc);
97         REPORTER_ASSERT(r, !dst.has_value());
98     }
99 
100     {
101         // Make broken stream by hand - zero length
102         SkBinaryWriteBuffer wBuffer({});
103         promise.flatten(wBuffer);
104         // Make broken stream by hand - stream is too short
105         wBuffer.write32(5);  // length
106         wBuffer.writeUInt(12);  // random data
107         wBuffer.writeUInt(12);  // random data
108         wBuffer.writeUInt(12);  // random data
109         auto data = wBuffer.snapshotAsData();
110         SkReadBuffer rBuffer{data->data(), data->size()};
111         SubRunAllocator alloc;
112         auto dst = GlyphVector::MakeFromBuffer(rBuffer, nullptr, &alloc);
113         REPORTER_ASSERT(r, !dst.has_value());
114     }
115 
116     {
117         // Make broken stream by hand - length out of range of safe calculations
118         SkBinaryWriteBuffer wBuffer({});
119         promise.flatten(wBuffer);
120         wBuffer.write32(INT_MAX - 10);  // length
121         wBuffer.writeUInt(12);  // random data
122         wBuffer.writeUInt(12);  // random data
123         wBuffer.writeUInt(12);  // random data
124         auto data = wBuffer.snapshotAsData();
125         SkReadBuffer rBuffer{data->data(), data->size()};
126         SubRunAllocator alloc;
127         auto dst = GlyphVector::MakeFromBuffer(rBuffer, nullptr, &alloc);
128         REPORTER_ASSERT(r, !dst.has_value());
129     }
130 }
131 
132 }  // namespace sktext::gpu
133