Flutter Engine
The Flutter Engine
Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
SkData Class Referencefinal

#include <SkData.h>

Inheritance diagram for SkData:
SkNVRefCnt< SkData >

Public Types

typedef void(* ReleaseProc) (const void *ptr, void *context)
 

Public Member Functions

size_t size () const
 
bool isEmpty () const
 
const void * data () const
 
const uint8_t * bytes () const
 
void * writable_data ()
 
size_t copyRange (size_t offset, size_t length, void *buffer) const
 
bool equals (const SkData *other) const
 
- Public Member Functions inherited from SkNVRefCnt< SkData >
 SkNVRefCnt ()
 
 ~SkNVRefCnt ()
 
bool unique () const
 
void ref () const
 
void unref () const
 
void deref () const
 
bool refCntGreaterThan (int32_t threadIsolatedTestCnt) const
 

Static Public Member Functions

static sk_sp< SkDataMakeWithCopy (const void *data, size_t length)
 
static sk_sp< SkDataMakeUninitialized (size_t length)
 
static sk_sp< SkDataMakeZeroInitialized (size_t length)
 
static sk_sp< SkDataMakeWithCString (const char cstr[])
 
static sk_sp< SkDataMakeWithProc (const void *ptr, size_t length, ReleaseProc proc, void *ctx)
 
static sk_sp< SkDataMakeWithoutCopy (const void *data, size_t length)
 
static sk_sp< SkDataMakeFromMalloc (const void *data, size_t length)
 
static sk_sp< SkDataMakeFromFileName (const char path[])
 
static sk_sp< SkDataMakeFromFILE (FILE *f)
 
static sk_sp< SkDataMakeFromFD (int fd)
 
static sk_sp< SkDataMakeFromStream (SkStream *, size_t size)
 
static sk_sp< SkDataMakeSubset (const SkData *src, size_t offset, size_t length)
 
static sk_sp< SkDataMakeEmpty ()
 

Friends

class SkNVRefCnt< SkData >
 

Detailed Description

SkData holds an immutable data buffer. Not only is the data immutable, but the actual ptr that is returned (by data() or bytes()) is guaranteed to always be the same for the life of this instance.

Definition at line 25 of file SkData.h.

Member Typedef Documentation

◆ ReleaseProc

typedef void(* SkData::ReleaseProc) (const void *ptr, void *context)

Function that, if provided, will be called when the SkData goes out of scope, allowing for custom allocation/freeing of the data's contents.

Definition at line 78 of file SkData.h.

Member Function Documentation

◆ bytes()

const uint8_t * SkData::bytes ( ) const
inline

Like data(), returns a read-only ptr into the data, but in this case it is cast to uint8_t*, to make it easy to add an offset to it.

Definition at line 43 of file SkData.h.

43 {
44 return reinterpret_cast<const uint8_t*>(fPtr);
45 }

◆ copyRange()

size_t SkData::copyRange ( size_t  offset,
size_t  length,
void *  buffer 
) const

Helper to copy a range of the data into a caller-provided buffer. Returns the actual number of bytes copied, after clamping offset and length to the size of the data. If buffer is NULL, it is ignored, and only the computed number of bytes is returned.

Definition at line 53 of file SkData.cpp.

53 {
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}
#define SkASSERT(cond)
Definition: SkAssert.h:116
const uint8_t * bytes() const
Definition: SkData.h:43
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 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
SeparatedVector2 offset

◆ data()

const void * SkData::data ( ) const
inline

Returns the ptr to the data.

Definition at line 37 of file SkData.h.

37{ return fPtr; }

◆ equals()

bool SkData::equals ( const SkData other) const

Returns true if these two objects have the same length and contents, effectively returning 0 == memcmp(...)

Definition at line 43 of file SkData.cpp.

43 {
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}
static int sk_careful_memcmp(const void *a, const void *b, size_t len)
Definition: SkMalloc.h:143

◆ isEmpty()

bool SkData::isEmpty ( ) const
inline

Definition at line 32 of file SkData.h.

32{ return 0 == fSize; }

◆ MakeEmpty()

sk_sp< SkData > SkData::MakeEmpty ( )
static

Returns a new empty dataref (or a reference to a shared empty dataref). New or shared, the caller must see that unref() is eventually called.

Definition at line 94 of file SkData.cpp.

94 {
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}
sk_sp< T > sk_ref_sp(T *obj)
Definition: SkRefCnt.h:381
Definition: SkData.h:25
Definition: SkOnce.h:22
EMSCRIPTEN_KEEPALIVE void empty()

◆ MakeFromFD()

sk_sp< SkData > SkData::MakeFromFD ( int  fd)
static

Create a new dataref from a file descriptor. This does not take ownership of the file descriptor, nor close it. The caller is free to close the file descriptor at its convenience. The file descriptor must be open for reading only. Returns NULL on failure.

Definition at line 158 of file SkData.cpp.

158 {
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}
static void sk_mmap_releaseproc(const void *addr, void *ctx)
Definition: SkData.cpp:133
void * sk_fdmmap(int fd, size_t *length)
static sk_sp< SkData > MakeWithProc(const void *ptr, size_t length, ReleaseProc proc, void *ctx)
Definition: SkData.cpp:128
size_t size() const
Definition: SkData.h:30

◆ MakeFromFILE()

sk_sp< SkData > SkData::MakeFromFILE ( FILE *  f)
static

Create a new dataref from a stdio FILE. This does not take ownership of the FILE, nor close it. The caller is free to close the FILE at its convenience. The FILE must be open for reading only. Returns NULL on failure.

Definition at line 138 of file SkData.cpp.

138 {
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}
void * sk_fmmap(FILE *f, size_t *length)

◆ MakeFromFileName()

sk_sp< SkData > SkData::MakeFromFileName ( const char  path[])
static

Create a new dataref the file with the specified path. If the file cannot be opened, this returns NULL.

Definition at line 148 of file SkData.cpp.

148 {
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}
FILE * sk_fopen(const char path[], SkFILE_Flags)
void sk_fclose(FILE *)
@ kRead_SkFILE_Flag
Definition: SkOSFile.h:20
const void * data() const
Definition: SkData.h:37
static sk_sp< SkData > MakeFromFILE(FILE *f)
Definition: SkData.cpp:138
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

◆ MakeFromMalloc()

sk_sp< SkData > SkData::MakeFromMalloc ( const void *  data,
size_t  length 
)
static

Create a new dataref from a pointer allocated by malloc. The Data object takes ownership of that allocation, and will handling calling sk_free.

Definition at line 107 of file SkData.cpp.

107 {
108 return sk_sp<SkData>(new SkData(data, length, sk_free_releaseproc, nullptr));
109}
static void sk_free_releaseproc(const void *ptr, void *)
Definition: SkData.cpp:103

◆ MakeFromStream()

sk_sp< SkData > SkData::MakeFromStream ( SkStream stream,
size_t  size 
)
static

Attempt to read size bytes into a SkData. If the read succeeds, return the data, else return NULL. Either way the stream's cursor may have been changed as a result of calling read().

Definition at line 208 of file SkData.cpp.

208 {
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}
bool StreamRemainingLengthIsBelow(SkStream *stream, size_t len)
Definition: SkStream.cpp:976
static sk_sp< SkData > MakeUninitialized(size_t length)
Definition: SkData.cpp:116

◆ MakeSubset()

sk_sp< SkData > SkData::MakeSubset ( const SkData src,
size_t  offset,
size_t  length 
)
static

Create a new dataref using a subset of the data in the specified src dataref.

Definition at line 173 of file SkData.cpp.

173 {
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}
static void sk_dataref_releaseproc(const void *, void *context)
Definition: SkData.cpp:168
static sk_sp< SkData > MakeEmpty()
Definition: SkData.cpp:94

◆ MakeUninitialized()

sk_sp< SkData > SkData::MakeUninitialized ( size_t  length)
static

Create a new data with uninitialized contents. The caller should call writable_data() to write into the buffer, but this must be done before another ref() is made.

Definition at line 116 of file SkData.cpp.

116 {
117 return PrivateNewWithCopy(nullptr, length);
118}

◆ MakeWithCopy()

sk_sp< SkData > SkData::MakeWithCopy ( const void *  data,
size_t  length 
)
static

Create a new dataref by copying the specified data

Definition at line 111 of file SkData.cpp.

111 {
112 SkASSERT(src);
113 return PrivateNewWithCopy(src, length);
114}

◆ MakeWithCString()

sk_sp< SkData > SkData::MakeWithCString ( const char  cstr[])
static

Create a new dataref by copying the specified c-string (a null-terminated array of bytes). The returned SkData will have size() equal to strlen(cstr) + 1. If cstr is NULL, it will be treated the same as "".

Definition at line 195 of file SkData.cpp.

195 {
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}
static sk_sp< SkData > MakeWithCopy(const void *data, size_t length)
Definition: SkData.cpp:111

◆ MakeWithoutCopy()

static sk_sp< SkData > SkData::MakeWithoutCopy ( const void *  data,
size_t  length 
)
inlinestatic

Call this when the data parameter is already const and will outlive the lifetime of the SkData. Suitable for with const globals.

Definition at line 116 of file SkData.h.

116 {
117 return MakeWithProc(data, length, NoopReleaseProc, nullptr);
118 }

◆ MakeWithProc()

sk_sp< SkData > SkData::MakeWithProc ( const void *  ptr,
size_t  length,
ReleaseProc  proc,
void *  ctx 
)
static

Create a new dataref, taking the ptr as is, and using the releaseproc to free it. The proc may be NULL.

Definition at line 128 of file SkData.cpp.

128 {
129 return sk_sp<SkData>(new SkData(ptr, length, proc, ctx));
130}

◆ MakeZeroInitialized()

sk_sp< SkData > SkData::MakeZeroInitialized ( size_t  length)
static

Create a new data with zero-initialized contents. The caller should call writable_data() to write into the buffer, but this must be done before another ref() is made.

Definition at line 120 of file SkData.cpp.

120 {
122 if (length != 0) {
123 memset(data->writable_data(), 0, data->size());
124 }
125 return data;
126}

◆ size()

size_t SkData::size ( ) const
inline

Returns the number of bytes stored.

Definition at line 30 of file SkData.h.

30{ return fSize; }

◆ writable_data()

void * SkData::writable_data ( )
inline

USE WITH CAUTION. This call will assert that the refcnt is 1, as a precaution against modifying the contents when another client/thread has access to the data.

Definition at line 52 of file SkData.h.

52 {
53 if (fSize) {
54 // only assert we're unique if we're not empty
55 SkASSERT(this->unique());
56 }
57 return const_cast<void*>(fPtr);
58 }
bool unique() const
Definition: SkRefCnt.h:175

Friends And Related Function Documentation

◆ SkNVRefCnt< SkData >

friend class SkNVRefCnt< SkData >
friend

Definition at line 167 of file SkData.h.


The documentation for this class was generated from the following files: