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