Flutter Engine
The Flutter Engine
Writer32Test.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2011 Google Inc.
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
16#include "src/base/SkRandom.h"
18#include "src/core/SkWriter32.h"
19#include "tests/Test.h"
20
21#include <array>
22#include <cstdint>
23#include <cstring>
24
25using namespace skia_private;
26
28 const void* expected, size_t size) {
29 SkAutoSMalloc<256> storage(size);
31 writer.flatten(storage.get());
32 REPORTER_ASSERT(reporter, !memcmp(storage.get(), expected, size));
33}
34
35
37 // There used to be a bug where we'd assert your first reservation had to
38 // fit in external storage if you used it. This would crash in debug mode.
39 uint8_t storage[4];
40 SkWriter32 writer(storage, sizeof(storage));
41 writer.reserve(40);
42}
43
45 uint8_t storage[8];
46 SkWriter32 writer(storage, sizeof(storage));
47
48 // Can we write nullptr?
49 writer.writeString(nullptr);
50 const int32_t expected[] = { 0x0, 0x0 };
51 check_contents(reporter, writer, expected, sizeof(expected));
52}
53
55 SkSWriter32<32> swriter;
56 int32_t array[3] = { 1, 2, 4 };
57
58 REPORTER_ASSERT(reporter, 0 == swriter.bytesWritten());
59 for (size_t i = 0; i < std::size(array); ++i) {
60 swriter.writeInt(array[i]);
61 }
62 check_contents(reporter, swriter, array, sizeof(array));
63
64 swriter.rewindToOffset(2*sizeof(int32_t));
65 REPORTER_ASSERT(reporter, sizeof(array) - 4 == swriter.bytesWritten());
66 swriter.writeInt(3);
67 REPORTER_ASSERT(reporter, sizeof(array) == swriter.bytesWritten());
68 array[2] = 3;
69 check_contents(reporter, swriter, array, sizeof(array));
70
71 // test rewinding past allocated chunks. This used to crash because we
72 // didn't truncate our link-list after freeing trailing blocks
73 SkWriter32 writer;
74 for (int i = 0; i < 100; ++i) {
75 writer.writeInt(i);
76 }
77 REPORTER_ASSERT(reporter, 100*4 == writer.bytesWritten());
78 for (int j = 100*4; j >= 0; j -= 16) {
79 writer.rewindToOffset(j);
80 }
82}
83
85 const uint32_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
86 for (size_t i = 0; i < std::size(data); ++i) {
87 REPORTER_ASSERT(reporter, i*4 == writer->bytesWritten());
88 writer->write32(data[i]);
89 REPORTER_ASSERT(reporter, data[i] == writer->readTAt<uint32_t>(i * 4));
90 }
91
92 char buffer[sizeof(data)];
93 REPORTER_ASSERT(reporter, sizeof(buffer) == writer->bytesWritten());
94 writer->flatten(buffer);
95 REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(buffer)));
96}
97
99 // Create some random data to write.
100 const size_t dataSize = 10;
101
102 AutoTMalloc<uint32_t> originalData(dataSize);
103 {
104 SkRandom rand(0);
105 for (size_t i = 0; i < dataSize; i++) {
106 originalData[(int) i] = rand.nextU();
107 }
108
109 // Write the random data to the writer at different lengths for
110 // different alignments.
111 for (size_t len = 0; len < dataSize; len++) {
112 writer->writePad(originalData.get(), len);
113 }
114 }
115
116 size_t totalBytes = writer->bytesWritten();
117
118 SkAutoMalloc readStorage(totalBytes);
119 writer->flatten(readStorage.get());
120
121 SkReadBuffer reader(readStorage.get(), totalBytes);
122
123 for (size_t len = 0; len < dataSize; len++) {
124 const char* readPtr = static_cast<const char*>(reader.skip(len));
125 // Ensure that the data read is the same as what was written.
126 REPORTER_ASSERT(reporter, memcmp(readPtr, originalData.get(), len) == 0);
127 // Ensure that the rest is padded with zeroes.
128 const char* stop = readPtr + SkAlign4(len);
129 readPtr += len;
130 while (readPtr < stop) {
131 REPORTER_ASSERT(reporter, *readPtr++ == 0);
132 }
133 }
134}
135
137 const size_t padding = 64;
138
139 const uint32_t uint1 = 0x12345678;
140 const uint32_t uint2 = 0x98765432;
141 const SkScalar scalar1 = 1234.5678f;
142 const SkScalar scalar2 = 9876.5432f;
143 const SkRect rect1 = SkRect::MakeXYWH(1, 2, 3, 4);
144 const SkRect rect2 = SkRect::MakeXYWH(5, 6, 7, 8);
145
146 for (size_t i = 0; i < (padding / 4); ++i) {
147 writer->write32(0);
148 }
149
150 writer->write32(uint1);
151 writer->writeRect(rect1);
152 writer->writeScalar(scalar1);
153
154 for (size_t i = 0; i < (padding / 4); ++i) {
155 writer->write32(0);
156 }
157
158 REPORTER_ASSERT(reporter, writer->readTAt<uint32_t>(padding) == uint1);
159 REPORTER_ASSERT(reporter, writer->readTAt<SkRect>(padding + sizeof(uint32_t)) == rect1);
161 padding + sizeof(uint32_t) + sizeof(SkRect)) == scalar1);
162
163 writer->overwriteTAt(padding, uint2);
164 writer->overwriteTAt(padding + sizeof(uint32_t), rect2);
165 writer->overwriteTAt(padding + sizeof(uint32_t) + sizeof(SkRect), scalar2);
166
167 REPORTER_ASSERT(reporter, writer->readTAt<uint32_t>(padding) == uint2);
168 REPORTER_ASSERT(reporter, writer->readTAt<SkRect>(padding + sizeof(uint32_t)) == rect2);
170 padding + sizeof(uint32_t) + sizeof(SkRect)) == scalar2);
171}
172
173DEF_TEST(Writer32_dynamic, reporter) {
174 SkWriter32 writer;
175 test1(reporter, &writer);
176
177 writer.reset();
178 testWritePad(reporter, &writer);
179
180 writer.reset();
181 testOverwriteT(reporter, &writer);
182}
183
184DEF_TEST(Writer32_small, reporter) {
185 SkSWriter32<8 * sizeof(intptr_t)> writer;
186 test1(reporter, &writer);
187
188 writer.reset(); // should just rewind our storage
189 testWritePad(reporter, &writer);
190
191 writer.reset();
192 testOverwriteT(reporter, &writer);
193}
194
195DEF_TEST(Writer32_large, reporter) {
196 SkSWriter32<1024 * sizeof(intptr_t)> writer;
197 test1(reporter, &writer);
198
199 writer.reset(); // should just rewind our storage
200 testWritePad(reporter, &writer);
201
202 writer.reset();
203 testOverwriteT(reporter, &writer);
204}
205
206DEF_TEST(Writer32_misc, reporter) {
210}
211
212DEF_TEST(Writer32_data, reporter) {
213 const char* str = "0123456789";
216
217 const size_t sizes[] = {
221 };
222
223 SkSWriter32<1000> writer;
224 size_t sizeWritten = 0;
225
226 writer.writeData(nullptr);
227 sizeWritten += sizes[0];
228 REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten());
229
230 writer.writeData(data0.get());
231 sizeWritten += sizes[1];
232 REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten());
233
234 writer.writeData(data1.get());
235 sizeWritten += sizes[2];
236 REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten());
237
238 auto result(writer.snapshotAsData());
239
240 SkReadBuffer reader(result->data(), result->size());
241 auto d0(reader.readByteArrayAsData()),
242 d1(reader.readByteArrayAsData()),
243 d2(reader.readByteArrayAsData());
244
245 REPORTER_ASSERT(reporter, 0 == d0->size());
246 REPORTER_ASSERT(reporter, strlen(str)+1 == d1->size());
247 REPORTER_ASSERT(reporter, !memcmp(str, d1->data(), strlen(str)+1));
248 REPORTER_ASSERT(reporter, 0 == d2->size());
249
250 REPORTER_ASSERT(reporter, reader.offset() == sizeWritten);
251 REPORTER_ASSERT(reporter, reader.eof());
252}
reporter
Definition: FontMgrTest.cpp:39
static constexpr T SkAlign4(T x)
Definition: SkAlign.h:16
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
static void check_contents(skiatest::Reporter *reporter, const SkWriter32 &writer, const void *expected, size_t size)
static void test_rewind(skiatest::Reporter *reporter)
static void test_reserve(skiatest::Reporter *reporter)
static void test1(skiatest::Reporter *reporter, SkWriter32 *writer)
DEF_TEST(Writer32_dynamic, reporter)
static void testOverwriteT(skiatest::Reporter *reporter, SkWriter32 *writer)
static void test_string_null(skiatest::Reporter *reporter)
static void testWritePad(skiatest::Reporter *reporter, SkWriter32 *writer)
void * get()
Definition: SkAutoMalloc.h:64
void * get() const
Definition: SkAutoMalloc.h:126
static sk_sp< SkData > MakeWithCString(const char cstr[])
Definition: SkData.cpp:195
static sk_sp< SkData > MakeEmpty()
Definition: SkData.cpp:94
uint32_t nextU()
Definition: SkRandom.h:42
const void * skip(size_t size)
sk_sp< SkData > readByteArrayAsData()
size_t offset() const
Definition: SkReadBuffer.h:78
void overwriteTAt(size_t offset, const T &value)
Definition: SkWriter32.h:94
void writeInt(int32_t value)
Definition: SkWriter32.h:105
static size_t WriteDataSize(const SkData *data)
Definition: SkWriter32.h:220
void write32(int32_t value)
Definition: SkWriter32.h:117
void writeRect(const SkRect &rect)
Definition: SkWriter32.h:133
void writeScalar(SkScalar value)
Definition: SkWriter32.h:121
uint32_t * reserve(size_t size)
Definition: SkWriter32.h:67
void writePad(const void *src, size_t size)
Definition: SkWriter32.h:192
void writeData(const SkData *data)
Definition: SkWriter32.h:212
void reset(void *external=nullptr, size_t externalBytes=0)
Definition: SkWriter32.h:54
const T & readTAt(size_t offset) const
Definition: SkWriter32.h:83
void flatten(void *dst) const
Definition: SkWriter32.h:235
void rewindToOffset(size_t offset)
Definition: SkWriter32.h:228
sk_sp< SkData > snapshotAsData() const
Definition: SkWriter32.cpp:78
void writeString(const char *str, size_t len=(size_t) -1)
Definition: SkWriter32.cpp:38
size_t bytesWritten() const
Definition: SkWriter32.h:48
T * get() const
Definition: SkRefCnt.h:303
float SkScalar
Definition: extension.cpp:12
GAsyncResult * result
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
Vec< 2, uint32_t > uint2
Definition: SkVx.h:1166
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63