Flutter Engine
The Flutter Engine
GrGlyphVectorTest.cpp
Go to the documentation of this file.
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
11#include "include/core/SkSpan.h"
13#include "src/base/SkZip.h"
15#include "src/core/SkGlyph.h"
17#include "src/core/SkStrike.h"
23#include "tests/Test.h"
25
26#include <initializer_list>
27#include <limits.h>
28#include <optional>
29#include <utility>
30
33
34namespace sktext::gpu {
36public:
37 static const SkDescriptor& GetDescriptor(const GlyphVector& v) {
38 return v.fStrikePromise.descriptor();
39 }
41 return v.fGlyphs;
42 }
43};
44
45DEF_TEST(GlyphVector_Serialization, r) {
47 auto [strikeSpec, _] = SkStrikeSpec::MakeCanonicalized(font);
48
49 SubRunAllocator alloc;
50
51 const int N = 10;
53 for (int i = 0; i < N; 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());
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
79DEF_TEST(GlyphVector_BadLengths, r) {
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
uint16_t glyphs[5]
Definition: FontMgrTest.cpp:46
sktext::gpu::SubRunAllocator SubRunAllocator
sktext::gpu::GlyphVector GlyphVector
SkSpan(Container &&) -> SkSpan< std::remove_pointer_t< decltype(std::data(std::declval< Container >()))> >
uint16_t SkGlyphID
Definition: SkTypes.h:179
constexpr auto SkMakeZip(Ts &&... ts)
Definition: SkZip.h:212
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
#define N
Definition: beziers.cpp:19
Definition: SkFont.h:35
static std::tuple< SkStrikeSpec, SkScalar > MakeCanonicalized(const SkFont &font, const SkPaint *paint=nullptr)
void write32(int32_t value)
Definition: SkWriteBuffer.h:63
const SkDescriptor & descriptor() const
static const SkDescriptor & GetDescriptor(const GlyphVector &v)
static SkSpan< GlyphVector::Variant > GetGlyphs(const GlyphVector &v)
static GlyphVector Make(SkStrikePromise &&promise, SkSpan< const SkPackedGlyphID > glyphs, SubRunAllocator *alloc)
Definition: GlyphVector.cpp:38
static std::optional< GlyphVector > MakeFromBuffer(SkReadBuffer &buffer, const SkStrikeClient *strikeClient, SubRunAllocator *alloc)
Definition: GlyphVector.cpp:50
SkFont DefaultFont()
font
Font Metadata and Metrics.
dst
Definition: cp.py:12
DEF_TEST(GlyphVector_Serialization, r)
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63