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 8 #ifndef SkData_DEFINED 9 #define SkData_DEFINED 10 11 #include <stdio.h> 12 13 #include "include/core/SkRefCnt.h" 14 15 class SkStream; 16 17 extern "C" { 18 typedef struct OH_NativeBuffer OH_NativeBuffer; 19 } 20 21 /** 22 * SkData holds an immutable data buffer. Not only is the data immutable, 23 * but the actual ptr that is returned (by data() or bytes()) is guaranteed 24 * to always be the same for the life of this instance. 25 */ 26 class SK_API SkData final : public SkNVRefCnt<SkData> { 27 public: 28 /** 29 * Returns the number of bytes stored. 30 */ size() const31 size_t size() const { return fSize; } 32 isEmpty() const33 bool isEmpty() const { return 0 == fSize; } 34 35 /** 36 * Returns the ptr to the data. 37 * USE WITH CAUTION. 38 * If SkData object is created from SkData::MakeFromOHNativeBuffer, 39 * this function always returns nullptr, and gets OH_NativeBuffer via getNativeBuffer(). 40 */ data() const41 const void* data() const { return fPtr; } 42 43 /** 44 * Returns the ptr to OH_NativeBuffer. 45 */ getNativeBuffer()46 OH_NativeBuffer* getNativeBuffer() { return fNativeBuffer; } 47 48 /** 49 * Like data(), returns a read-only ptr into the data, but in this case 50 * it is cast to uint8_t*, to make it easy to add an offset to it. 51 */ bytes() const52 const uint8_t* bytes() const { 53 return reinterpret_cast<const uint8_t*>(fPtr); 54 } 55 56 /** 57 * USE WITH CAUTION. 58 * This call will assert that the refcnt is 1, as a precaution against modifying the 59 * contents when another client/thread has access to the data. 60 */ writable_data()61 void* writable_data() { 62 if (fSize) { 63 // only assert we're unique if we're not empty 64 SkASSERT(this->unique()); 65 } 66 return const_cast<void*>(fPtr); 67 } 68 69 /** 70 * Helper to copy a range of the data into a caller-provided buffer. 71 * Returns the actual number of bytes copied, after clamping offset and 72 * length to the size of the data. If buffer is NULL, it is ignored, and 73 * only the computed number of bytes is returned. 74 */ 75 size_t copyRange(size_t offset, size_t length, void* buffer) const; 76 77 /** 78 * Returns true if these two objects have the same length and contents, 79 * effectively returning 0 == memcmp(...) 80 */ 81 bool equals(const SkData* other) const; 82 83 /** 84 * Function that, if provided, will be called when the SkData goes out 85 * of scope, allowing for custom allocation/freeing of the data's contents. 86 */ 87 typedef void (*ReleaseProc)(const void* ptr, void* context); 88 89 /** 90 * Create a new dataref by copying the specified data 91 */ 92 static sk_sp<SkData> MakeWithCopy(const void* data, size_t length); 93 94 95 /** 96 * Create a new data with uninitialized contents. The caller should call writable_data() 97 * to write into the buffer, but this must be done before another ref() is made. 98 */ 99 static sk_sp<SkData> MakeUninitialized(size_t length); 100 101 /** 102 * Create a new dataref by copying the specified c-string 103 * (a null-terminated array of bytes). The returned SkData will have size() 104 * equal to strlen(cstr) + 1. If cstr is NULL, it will be treated the same 105 * as "". 106 */ 107 static sk_sp<SkData> MakeWithCString(const char cstr[]); 108 109 /** 110 * Create a new dataref, taking the ptr as is, and using the 111 * releaseproc to free it. The proc may be NULL. 112 */ 113 static sk_sp<SkData> MakeWithProc(const void* ptr, size_t length, ReleaseProc proc, void* ctx); 114 115 /** 116 * Call this when the data parameter is already const and will outlive the lifetime of the 117 * SkData. Suitable for with const globals. 118 */ MakeWithoutCopy(const void* data, size_t length)119 static sk_sp<SkData> MakeWithoutCopy(const void* data, size_t length) { 120 return MakeWithProc(data, length, NoopReleaseProc, nullptr); 121 } 122 123 /** 124 * Create a new dataref from a pointer allocated by malloc. The Data object 125 * takes ownership of that allocation, and will handling calling sk_free. 126 */ 127 static sk_sp<SkData> MakeFromMalloc(const void* data, size_t length); 128 129 /** 130 * Create a new dataref the file with the specified path. 131 * If the file cannot be opened, this returns NULL. 132 */ 133 static sk_sp<SkData> MakeFromFileName(const char path[]); 134 135 /** 136 * Create a new dataref from a stdio FILE. 137 * This does not take ownership of the FILE, nor close it. 138 * The caller is free to close the FILE at its convenience. 139 * The FILE must be open for reading only. 140 * Returns NULL on failure. 141 */ 142 static sk_sp<SkData> MakeFromFILE(FILE* f); 143 144 /** 145 * Create a new dataref from a file descriptor. 146 * This does not take ownership of the file descriptor, nor close it. 147 * The caller is free to close the file descriptor at its convenience. 148 * The file descriptor must be open for reading only. 149 * Returns NULL on failure. 150 */ 151 static sk_sp<SkData> MakeFromFD(int fd); 152 153 /** 154 * Create a new dataref from an OH_NativeBuffer. 155 * Returns NULL on failure. 156 */ 157 static sk_sp<SkData> MakeFromOHNativeBuffer(OH_NativeBuffer* nativeBuffer, size_t size); 158 159 /** 160 * Attempt to read size bytes into a SkData. If the read succeeds, return the data, 161 * else return NULL. Either way the stream's cursor may have been changed as a result 162 * of calling read(). 163 */ 164 static sk_sp<SkData> MakeFromStream(SkStream*, size_t size); 165 166 /** 167 * Create a new dataref using a subset of the data in the specified 168 * src dataref. 169 */ 170 static sk_sp<SkData> MakeSubset(const SkData* src, size_t offset, size_t length); 171 172 /** 173 * Returns a new empty dataref (or a reference to a shared empty dataref). 174 * New or shared, the caller must see that unref() is eventually called. 175 */ 176 static sk_sp<SkData> MakeEmpty(); 177 178 private: 179 friend class SkNVRefCnt<SkData>; 180 ReleaseProc fReleaseProc; 181 void* fReleaseProcContext; 182 const void* fPtr; 183 size_t fSize; 184 OH_NativeBuffer* fNativeBuffer; 185 186 SkData(const void* ptr, size_t size, ReleaseProc, void* context); 187 SkData(const void* ptr, size_t size, OH_NativeBuffer* nativeBuffer, ReleaseProc, void* context); 188 explicit SkData(size_t size); // inplace new/delete 189 ~SkData(); 190 191 // Ensure the unsized delete is called. 192 void operator delete(void* p); 193 194 // shared internal factory 195 static sk_sp<SkData> PrivateNewWithCopy(const void* srcOrNull, size_t length); 196 197 static void NoopReleaseProc(const void*, void*); // {} 198 199 using INHERITED = SkRefCnt; 200 }; 201 202 #endif 203