epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | 3a31ac1 | 2011-06-24 13:11:05 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * 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.com | 3a31ac1 | 2011-06-24 13:11:05 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | 3a31ac1 | 2011-06-24 13:11:05 +0000 | [diff] [blame] | 11 | #ifndef SkData_DEFINED |
| 12 | #define SkData_DEFINED |
| 13 | |
djsollen@google.com | a1da103 | 2012-07-31 13:17:31 +0000 | [diff] [blame] | 14 | #include "SkFlattenable.h" |
reed@google.com | 3a31ac1 | 2011-06-24 13:11:05 +0000 | [diff] [blame] | 15 | |
commit-bot@chromium.org | 9711e44 | 2013-04-24 20:03:00 +0000 | [diff] [blame] | 16 | struct SkFILE; |
| 17 | |
reed@google.com | 3a31ac1 | 2011-06-24 13:11:05 +0000 | [diff] [blame] | 18 | /** |
| 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.com | a1da103 | 2012-07-31 13:17:31 +0000 | [diff] [blame] | 23 | class SK_API SkData : public SkFlattenable { |
reed@google.com | 3a31ac1 | 2011-06-24 13:11:05 +0000 | [diff] [blame] | 24 | public: |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 25 | SK_DECLARE_INST_COUNT(SkData) |
| 26 | |
reed@google.com | 3a31ac1 | 2011-06-24 13:11:05 +0000 | [diff] [blame] | 27 | /** |
| 28 | * Returns the number of bytes stored. |
| 29 | */ |
| 30 | size_t size() const { return fSize; } |
| 31 | |
reed@google.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 32 | bool isEmpty() const { return 0 == fSize; } |
| 33 | |
reed@google.com | 3a31ac1 | 2011-06-24 13:11:05 +0000 | [diff] [blame] | 34 | /** |
| 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.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 56 | * 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.com | 3a31ac1 | 2011-06-24 13:11:05 +0000 | [diff] [blame] | 62 | * 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 66 | |
reed@google.com | 3a31ac1 | 2011-06-24 13:11:05 +0000 | [diff] [blame] | 67 | /** |
| 68 | * Create a new dataref by copying the specified data |
| 69 | */ |
| 70 | static SkData* NewWithCopy(const void* data, size_t length); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 71 | |
reed@google.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 72 | /** |
| 73 | * Create a new dataref by copying the specified c-string |
reed@google.com | fd59d12 | 2012-07-02 20:28:31 +0000 | [diff] [blame] | 74 | * (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.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 77 | */ |
| 78 | static SkData* NewWithCString(const char cstr[]); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 79 | |
reed@google.com | 3a31ac1 | 2011-06-24 13:11:05 +0000 | [diff] [blame] | 80 | /** |
| 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.com | 1f1db4c | 2012-05-21 20:00:39 +0000 | [diff] [blame] | 88 | * 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.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 90 | */ |
| 91 | static SkData* NewFromMalloc(const void* data, size_t length); |
| 92 | |
| 93 | /** |
commit-bot@chromium.org | 9711e44 | 2013-04-24 20:03:00 +0000 | [diff] [blame] | 94 | * Create a new dataref from a SkFILE. |
| 95 | * This does not take ownership of the SkFILE, nor close it. |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 96 | * The caller is free to close the SkFILE at its convenience. |
commit-bot@chromium.org | 9711e44 | 2013-04-24 20:03:00 +0000 | [diff] [blame] | 97 | * The SkFILE must be open for reading only. |
| 98 | * Returns NULL on failure. |
reed@google.com | e1575aa | 2013-03-18 21:08:46 +0000 | [diff] [blame] | 99 | */ |
commit-bot@chromium.org | 9711e44 | 2013-04-24 20:03:00 +0000 | [diff] [blame] | 100 | static SkData* NewFromFILE(SkFILE* f); |
skia.committer@gmail.com | 8eaddb0 | 2013-03-19 07:15:10 +0000 | [diff] [blame] | 101 | |
reed@google.com | e1575aa | 2013-03-18 21:08:46 +0000 | [diff] [blame] | 102 | /** |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 103 | * 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.com | 11f2b44 | 2013-06-04 07:00:53 +0000 | [diff] [blame^] | 110 | |
bungeman@google.com | 11c9a55 | 2013-06-03 17:10:35 +0000 | [diff] [blame] | 111 | /** |
reed@google.com | 3a31ac1 | 2011-06-24 13:11:05 +0000 | [diff] [blame] | 112 | * 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.com | a1da103 | 2012-07-31 13:17:31 +0000 | [diff] [blame] | 123 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkData) |
| 124 | |
| 125 | protected: |
| 126 | SkData(SkFlattenableReadBuffer&); |
| 127 | virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; |
| 128 | |
reed@google.com | 3a31ac1 | 2011-06-24 13:11:05 +0000 | [diff] [blame] | 129 | private: |
| 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.com | 59f46b8 | 2012-07-10 17:30:58 +0000 | [diff] [blame] | 137 | virtual ~SkData(); |
| 138 | |
djsollen@google.com | a1da103 | 2012-07-31 13:17:31 +0000 | [diff] [blame] | 139 | typedef SkFlattenable INHERITED; |
reed@google.com | 3a31ac1 | 2011-06-24 13:11:05 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
bungeman@google.com | 6f4cf2a | 2013-04-16 15:24:31 +0000 | [diff] [blame] | 142 | /** Typedef of SkAutoTUnref<SkData> for automatically unref-ing a SkData. */ |
| 143 | typedef SkAutoTUnref<SkData> SkAutoDataUnref; |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 144 | |
reed@google.com | 3a31ac1 | 2011-06-24 13:11:05 +0000 | [diff] [blame] | 145 | #endif |