Flutter Engine
The Flutter Engine
SkData.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
9
14#include "src/core/SkOSFile.h"
16
17#include <cstring>
18#include <new>
19
20SkData::SkData(const void* ptr, size_t size, ReleaseProc proc, void* context)
21 : fReleaseProc(proc)
22 , fReleaseProcContext(context)
23 , fPtr(ptr)
24 , fSize(size)
25{}
26
27/** This constructor means we are inline with our fPtr's contents.
28 * Thus we set fPtr to point right after this.
29 */
30SkData::SkData(size_t size)
31 : fReleaseProc(nullptr)
32 , fReleaseProcContext(nullptr)
33 , fPtr((const char*)(this + 1))
34 , fSize(size)
35{}
36
37SkData::~SkData() {
38 if (fReleaseProc) {
39 fReleaseProc(fPtr, fReleaseProcContext);
40 }
41}
42
43bool SkData::equals(const SkData* other) const {
44 if (this == other) {
45 return true;
46 }
47 if (nullptr == other) {
48 return false;
49 }
50 return fSize == other->fSize && !sk_careful_memcmp(fPtr, other->fPtr, fSize);
51}
52
53size_t SkData::copyRange(size_t offset, size_t length, void* buffer) const {
54 size_t available = fSize;
55 if (offset >= available || 0 == length) {
56 return 0;
57 }
58 available -= offset;
59 if (length > available) {
60 length = available;
61 }
62 SkASSERT(length > 0);
63
64 if (buffer) {
65 memcpy(buffer, this->bytes() + offset, length);
66 }
67 return length;
68}
69
70void SkData::operator delete(void* p) {
71 ::operator delete(p);
72}
73
74sk_sp<SkData> SkData::PrivateNewWithCopy(const void* srcOrNull, size_t length) {
75 if (0 == length) {
76 return SkData::MakeEmpty();
77 }
78
79 const size_t actualLength = length + sizeof(SkData);
80 SkASSERT_RELEASE(length < actualLength); // Check for overflow.
81
82 void* storage = ::operator new (actualLength);
83 sk_sp<SkData> data(new (storage) SkData(length));
84 if (srcOrNull) {
85 memcpy(data->writable_data(), srcOrNull, length);
86 }
87 return data;
88}
89
90void SkData::NoopReleaseProc(const void*, void*) {}
91
92///////////////////////////////////////////////////////////////////////////////
93
95 static SkOnce once;
96 static SkData* empty;
97
98 once([]{ empty = new SkData(nullptr, 0, nullptr, nullptr); });
99 return sk_ref_sp(empty);
100}
101
102// assumes fPtr was allocated via sk_malloc
103static void sk_free_releaseproc(const void* ptr, void*) {
104 sk_free(const_cast<void*>(ptr));
105}
106
108 return sk_sp<SkData>(new SkData(data, length, sk_free_releaseproc, nullptr));
109}
110
112 SkASSERT(src);
113 return PrivateNewWithCopy(src, length);
114}
115
117 return PrivateNewWithCopy(nullptr, length);
118}
119
122 if (length != 0) {
123 memset(data->writable_data(), 0, data->size());
124 }
125 return data;
126}
127
128sk_sp<SkData> SkData::MakeWithProc(const void* ptr, size_t length, ReleaseProc proc, void* ctx) {
129 return sk_sp<SkData>(new SkData(ptr, length, proc, ctx));
130}
131
132// assumes fPtr was allocated with sk_fmmap
133static void sk_mmap_releaseproc(const void* addr, void* ctx) {
134 size_t length = reinterpret_cast<size_t>(ctx);
136}
137
139 size_t size;
140 void* addr = sk_fmmap(f, &size);
141 if (nullptr == addr) {
142 return nullptr;
143 }
144
145 return SkData::MakeWithProc(addr, size, sk_mmap_releaseproc, reinterpret_cast<void*>(size));
146}
147
149 FILE* f = path ? sk_fopen(path, kRead_SkFILE_Flag) : nullptr;
150 if (nullptr == f) {
151 return nullptr;
152 }
153 auto data = MakeFromFILE(f);
154 sk_fclose(f);
155 return data;
156}
157
159 size_t size;
160 void* addr = sk_fdmmap(fd, &size);
161 if (nullptr == addr) {
162 return nullptr;
163 }
164 return SkData::MakeWithProc(addr, size, sk_mmap_releaseproc, reinterpret_cast<void*>(size));
165}
166
167// assumes context is a SkData
168static void sk_dataref_releaseproc(const void*, void* context) {
169 SkData* src = reinterpret_cast<SkData*>(context);
170 src->unref();
171}
172
174 /*
175 We could, if we wanted/need to, just make a deep copy of src's data,
176 rather than referencing it. This would duplicate the storage (of the
177 subset amount) but would possibly allow src to go out of scope sooner.
178 */
179
180 size_t available = src->size();
181 if (offset >= available || 0 == length) {
182 return SkData::MakeEmpty();
183 }
184 available -= offset;
185 if (length > available) {
186 length = available;
187 }
188 SkASSERT(length > 0);
189
190 src->ref(); // this will be balanced in sk_dataref_releaseproc
192 const_cast<SkData*>(src)));
193}
194
196 size_t size;
197 if (nullptr == cstr) {
198 cstr = "";
199 size = 1;
200 } else {
201 size = strlen(cstr) + 1;
202 }
203 return MakeWithCopy(cstr, size);
204}
205
206///////////////////////////////////////////////////////////////////////////////
207
209 // reduce the chance of OOM by checking that the stream has enough bytes to read from before
210 // allocating that potentially large buffer.
212 return nullptr;
213 }
215 if (stream->read(data->writable_data(), size) != size) {
216 return nullptr;
217 }
218 return data;
219}
#define SkASSERT_RELEASE(cond)
Definition: SkAssert.h:100
#define SkASSERT(cond)
Definition: SkAssert.h:116
static void sk_free_releaseproc(const void *ptr, void *)
Definition: SkData.cpp:103
static void sk_mmap_releaseproc(const void *addr, void *ctx)
Definition: SkData.cpp:133
static void sk_dataref_releaseproc(const void *, void *context)
Definition: SkData.cpp:168
SK_API void sk_free(void *)
static int sk_careful_memcmp(const void *a, const void *b, size_t len)
Definition: SkMalloc.h:143
FILE * sk_fopen(const char path[], SkFILE_Flags)
void sk_fclose(FILE *)
void sk_fmunmap(const void *addr, size_t length)
@ kRead_SkFILE_Flag
Definition: SkOSFile.h:20
void * sk_fdmmap(int fd, size_t *length)
void * sk_fmmap(FILE *f, size_t *length)
sk_sp< T > sk_ref_sp(T *obj)
Definition: SkRefCnt.h:381
bool StreamRemainingLengthIsBelow(SkStream *stream, size_t len)
Definition: SkStream.cpp:976
Definition: SkData.h:25
const uint8_t * bytes() const
Definition: SkData.h:43
size_t copyRange(size_t offset, size_t length, void *buffer) const
Definition: SkData.cpp:53
static sk_sp< SkData > MakeFromMalloc(const void *data, size_t length)
Definition: SkData.cpp:107
static sk_sp< SkData > MakeUninitialized(size_t length)
Definition: SkData.cpp:116
bool equals(const SkData *other) const
Definition: SkData.cpp:43
const void * data() const
Definition: SkData.h:37
static sk_sp< SkData > MakeWithCString(const char cstr[])
Definition: SkData.cpp:195
static sk_sp< SkData > MakeFromFD(int fd)
Definition: SkData.cpp:158
static sk_sp< SkData > MakeWithProc(const void *ptr, size_t length, ReleaseProc proc, void *ctx)
Definition: SkData.cpp:128
static sk_sp< SkData > MakeFromFILE(FILE *f)
Definition: SkData.cpp:138
static sk_sp< SkData > MakeWithCopy(const void *data, size_t length)
Definition: SkData.cpp:111
static sk_sp< SkData > MakeSubset(const SkData *src, size_t offset, size_t length)
Definition: SkData.cpp:173
static sk_sp< SkData > MakeFromStream(SkStream *, size_t size)
Definition: SkData.cpp:208
static sk_sp< SkData > MakeEmpty()
Definition: SkData.cpp:94
static sk_sp< SkData > MakeZeroInitialized(size_t length)
Definition: SkData.cpp:120
static sk_sp< SkData > MakeFromFileName(const char path[])
Definition: SkData.cpp:148
size_t size() const
Definition: SkData.h:30
Definition: SkOnce.h:22
EMSCRIPTEN_KEEPALIVE void empty()
size_t length
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
Definition: switches.h:57
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
SeparatedVector2 offset
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63