SkData.h 5.8 KB

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