blob: ab7da47743e45fd8bd34f0c5059558c6c4407302 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.com3a31ac12011-06-24 13:11:05 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.com3a31ac12011-06-24 13:11:05 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.com3a31ac12011-06-24 13:11:05 +000011#ifndef SkData_DEFINED
12#define SkData_DEFINED
13
djsollen@google.coma1da1032012-07-31 13:17:31 +000014#include "SkFlattenable.h"
reed@google.com3a31ac12011-06-24 13:11:05 +000015
commit-bot@chromium.org9711e442013-04-24 20:03:00 +000016struct SkFILE;
17
reed@google.com3a31ac12011-06-24 13:11:05 +000018/**
19 * SkData holds an immutable data buffer. Not only is the data immutable,
20 * but the actual ptr that is returned (by data() or bytes()) is guaranteed
21 * to always be the same for the life of this instance.
22 */
djsollen@google.coma1da1032012-07-31 13:17:31 +000023class SK_API SkData : public SkFlattenable {
reed@google.com3a31ac12011-06-24 13:11:05 +000024public:
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000025 SK_DECLARE_INST_COUNT(SkData)
26
reed@google.com3a31ac12011-06-24 13:11:05 +000027 /**
28 * Returns the number of bytes stored.
29 */
30 size_t size() const { return fSize; }
31
reed@google.comdbc936d2012-06-28 15:40:09 +000032 bool isEmpty() const { return 0 == fSize; }
33
reed@google.com3a31ac12011-06-24 13:11:05 +000034 /**
35 * Returns the ptr to the data.
36 */
37 const void* data() const { return fPtr; }
38
39 /**
40 * Like data(), returns a read-only ptr into the data, but in this case
41 * it is cast to uint8_t*, to make it easy to add an offset to it.
42 */
43 const uint8_t* bytes() const {
44 return reinterpret_cast<const uint8_t*>(fPtr);
45 }
46
47 /**
48 * Helper to copy a range of the data into a caller-provided buffer.
49 * Returns the actual number of bytes copied, after clamping offset and
50 * length to the size of the data. If buffer is NULL, it is ignored, and
51 * only the computed number of bytes is returned.
52 */
53 size_t copyRange(size_t offset, size_t length, void* buffer) const;
54
55 /**
reed@google.comdbc936d2012-06-28 15:40:09 +000056 * Returns true if these two objects have the same length and contents,
57 * effectively returning 0 == memcmp(...)
58 */
59 bool equals(const SkData* other) const;
60
61 /**
reed@google.com3a31ac12011-06-24 13:11:05 +000062 * Function that, if provided, will be called when the SkData goes out
63 * of scope, allowing for custom allocation/freeing of the data.
64 */
65 typedef void (*ReleaseProc)(const void* ptr, size_t length, void* context);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000066
reed@google.com3a31ac12011-06-24 13:11:05 +000067 /**
68 * Create a new dataref by copying the specified data
69 */
70 static SkData* NewWithCopy(const void* data, size_t length);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000071
reed@google.comdbc936d2012-06-28 15:40:09 +000072 /**
73 * Create a new dataref by copying the specified c-string
reed@google.comfd59d122012-07-02 20:28:31 +000074 * (a null-terminated array of bytes). The returned SkData will have size()
75 * equal to strlen(cstr) + 1. If cstr is NULL, it will be treated the same
76 * as "".
reed@google.comdbc936d2012-06-28 15:40:09 +000077 */
78 static SkData* NewWithCString(const char cstr[]);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000079
reed@google.com3a31ac12011-06-24 13:11:05 +000080 /**
81 * Create a new dataref, taking the data ptr as is, and using the
82 * releaseproc to free it. The proc may be NULL.
83 */
84 static SkData* NewWithProc(const void* data, size_t length,
85 ReleaseProc proc, void* context);
86
87 /**
reed@google.com1f1db4c2012-05-21 20:00:39 +000088 * Create a new dataref from a pointer allocated by malloc. The Data object
89 * takes ownership of that allocation, and will handling calling sk_free.
reed@google.com8a85d0c2011-06-24 19:12:12 +000090 */
91 static SkData* NewFromMalloc(const void* data, size_t length);
92
93 /**
commit-bot@chromium.org9711e442013-04-24 20:03:00 +000094 * Create a new dataref from a SkFILE.
95 * This does not take ownership of the SkFILE, nor close it.
bungeman@google.com11c9a552013-06-03 17:10:35 +000096 * The caller is free to close the SkFILE at its convenience.
commit-bot@chromium.org9711e442013-04-24 20:03:00 +000097 * The SkFILE must be open for reading only.
98 * Returns NULL on failure.
reed@google.come1575aa2013-03-18 21:08:46 +000099 */
commit-bot@chromium.org9711e442013-04-24 20:03:00 +0000100 static SkData* NewFromFILE(SkFILE* f);
skia.committer@gmail.com8eaddb02013-03-19 07:15:10 +0000101
reed@google.come1575aa2013-03-18 21:08:46 +0000102 /**
bungeman@google.com11c9a552013-06-03 17:10:35 +0000103 * Create a new dataref from a file descriptor.
104 * This does not take ownership of the file descriptor, nor close it.
105 * The caller is free to close the file descriptor at its convenience.
106 * The file descriptor must be open for reading only.
107 * Returns NULL on failure.
108 */
109 static SkData* NewFromFD(int fd);
skia.committer@gmail.com11f2b442013-06-04 07:00:53 +0000110
bungeman@google.com11c9a552013-06-03 17:10:35 +0000111 /**
reed@google.com3a31ac12011-06-24 13:11:05 +0000112 * Create a new dataref using a subset of the data in the specified
113 * src dataref.
114 */
115 static SkData* NewSubset(const SkData* src, size_t offset, size_t length);
116
117 /**
118 * Returns a new empty dataref (or a reference to a shared empty dataref).
119 * New or shared, the caller must see that unref() is eventually called.
120 */
121 static SkData* NewEmpty();
122
djsollen@google.coma1da1032012-07-31 13:17:31 +0000123 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkData)
124
125protected:
126 SkData(SkFlattenableReadBuffer&);
127 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
128
reed@google.com3a31ac12011-06-24 13:11:05 +0000129private:
130 ReleaseProc fReleaseProc;
131 void* fReleaseProcContext;
132
133 const void* fPtr;
134 size_t fSize;
135
136 SkData(const void* ptr, size_t size, ReleaseProc, void* context);
robertphillips@google.com59f46b82012-07-10 17:30:58 +0000137 virtual ~SkData();
138
djsollen@google.coma1da1032012-07-31 13:17:31 +0000139 typedef SkFlattenable INHERITED;
reed@google.com3a31ac12011-06-24 13:11:05 +0000140};
141
bungeman@google.com6f4cf2a2013-04-16 15:24:31 +0000142/** Typedef of SkAutoTUnref<SkData> for automatically unref-ing a SkData. */
143typedef SkAutoTUnref<SkData> SkAutoDataUnref;
reed@google.com8a85d0c2011-06-24 19:12:12 +0000144
reed@google.com3a31ac12011-06-24 13:11:05 +0000145#endif