2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2011-06-24 13:11:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkData_DEFINED
|
|
|
|
#define SkData_DEFINED
|
|
|
|
|
2015-11-20 21:47:49 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkRefCnt.h"
|
2011-06-24 13:11:05 +00:00
|
|
|
|
2014-09-12 19:12:27 +00:00
|
|
|
class SkStream;
|
2013-04-24 20:03:00 +00:00
|
|
|
|
2011-06-24 13:11:05 +00:00
|
|
|
/**
|
|
|
|
* SkData holds an immutable data buffer. Not only is the data immutable,
|
|
|
|
* but the actual ptr that is returned (by data() or bytes()) is guaranteed
|
|
|
|
* to always be the same for the life of this instance.
|
|
|
|
*/
|
2018-10-30 15:23:00 +00:00
|
|
|
class SK_API SkData final : public SkNVRefCnt<SkData> {
|
2011-06-24 13:11:05 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Returns the number of bytes stored.
|
|
|
|
*/
|
|
|
|
size_t size() const { return fSize; }
|
|
|
|
|
2012-06-28 15:40:09 +00:00
|
|
|
bool isEmpty() const { return 0 == fSize; }
|
|
|
|
|
2011-06-24 13:11:05 +00:00
|
|
|
/**
|
|
|
|
* Returns the ptr to the data.
|
|
|
|
*/
|
|
|
|
const void* data() const { return fPtr; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like data(), returns a read-only ptr into the data, but in this case
|
|
|
|
* it is cast to uint8_t*, to make it easy to add an offset to it.
|
|
|
|
*/
|
|
|
|
const uint8_t* bytes() const {
|
|
|
|
return reinterpret_cast<const uint8_t*>(fPtr);
|
|
|
|
}
|
|
|
|
|
2014-09-11 15:42:36 +00:00
|
|
|
/**
|
|
|
|
* USE WITH CAUTION.
|
|
|
|
* This call will assert that the refcnt is 1, as a precaution against modifying the
|
|
|
|
* contents when another client/thread has access to the data.
|
|
|
|
*/
|
|
|
|
void* writable_data() {
|
|
|
|
if (fSize) {
|
|
|
|
// only assert we're unique if we're not empty
|
|
|
|
SkASSERT(this->unique());
|
|
|
|
}
|
2020-02-17 16:54:25 +00:00
|
|
|
return const_cast<void*>(fPtr);
|
2014-09-11 15:42:36 +00:00
|
|
|
}
|
|
|
|
|
2011-06-24 13:11:05 +00:00
|
|
|
/**
|
|
|
|
* Helper to copy a range of the data into a caller-provided buffer.
|
|
|
|
* Returns the actual number of bytes copied, after clamping offset and
|
|
|
|
* length to the size of the data. If buffer is NULL, it is ignored, and
|
|
|
|
* only the computed number of bytes is returned.
|
|
|
|
*/
|
|
|
|
size_t copyRange(size_t offset, size_t length, void* buffer) const;
|
|
|
|
|
2012-06-28 15:40:09 +00:00
|
|
|
/**
|
|
|
|
* Returns true if these two objects have the same length and contents,
|
|
|
|
* effectively returning 0 == memcmp(...)
|
|
|
|
*/
|
|
|
|
bool equals(const SkData* other) const;
|
|
|
|
|
2011-06-24 13:11:05 +00:00
|
|
|
/**
|
|
|
|
* Function that, if provided, will be called when the SkData goes out
|
2015-06-17 16:58:24 +00:00
|
|
|
* of scope, allowing for custom allocation/freeing of the data's contents.
|
2011-06-24 13:11:05 +00:00
|
|
|
*/
|
2015-06-17 16:58:24 +00:00
|
|
|
typedef void (*ReleaseProc)(const void* ptr, void* context);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-06-24 13:11:05 +00:00
|
|
|
/**
|
|
|
|
* Create a new dataref by copying the specified data
|
|
|
|
*/
|
2016-03-11 21:02:28 +00:00
|
|
|
static sk_sp<SkData> MakeWithCopy(const void* data, size_t length);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2016-08-09 19:20:04 +00:00
|
|
|
|
2014-09-11 15:42:36 +00:00
|
|
|
/**
|
|
|
|
* Create a new data with uninitialized contents. The caller should call writable_data()
|
|
|
|
* to write into the buffer, but this must be done before another ref() is made.
|
|
|
|
*/
|
2016-03-11 21:02:28 +00:00
|
|
|
static sk_sp<SkData> MakeUninitialized(size_t length);
|
2014-09-11 15:42:36 +00:00
|
|
|
|
2012-06-28 15:40:09 +00:00
|
|
|
/**
|
|
|
|
* Create a new dataref by copying the specified c-string
|
2012-07-02 20:28:31 +00:00
|
|
|
* (a null-terminated array of bytes). The returned SkData will have size()
|
|
|
|
* equal to strlen(cstr) + 1. If cstr is NULL, it will be treated the same
|
|
|
|
* as "".
|
2012-06-28 15:40:09 +00:00
|
|
|
*/
|
2016-03-11 21:02:28 +00:00
|
|
|
static sk_sp<SkData> MakeWithCString(const char cstr[]);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2011-06-24 13:11:05 +00:00
|
|
|
/**
|
2015-06-17 16:58:24 +00:00
|
|
|
* Create a new dataref, taking the ptr as is, and using the
|
2011-06-24 13:11:05 +00:00
|
|
|
* releaseproc to free it. The proc may be NULL.
|
|
|
|
*/
|
2016-03-11 21:02:28 +00:00
|
|
|
static sk_sp<SkData> MakeWithProc(const void* ptr, size_t length, ReleaseProc proc, void* ctx);
|
2015-06-17 16:58:24 +00:00
|
|
|
|
2014-09-11 15:42:36 +00:00
|
|
|
/**
|
|
|
|
* Call this when the data parameter is already const and will outlive the lifetime of the
|
|
|
|
* SkData. Suitable for with const globals.
|
|
|
|
*/
|
2016-03-11 21:02:28 +00:00
|
|
|
static sk_sp<SkData> MakeWithoutCopy(const void* data, size_t length) {
|
|
|
|
return MakeWithProc(data, length, DummyReleaseProc, nullptr);
|
2014-09-11 15:42:36 +00:00
|
|
|
}
|
2011-06-24 13:11:05 +00:00
|
|
|
|
2011-06-24 19:12:12 +00:00
|
|
|
/**
|
2012-05-21 20:00:39 +00:00
|
|
|
* Create a new dataref from a pointer allocated by malloc. The Data object
|
|
|
|
* takes ownership of that allocation, and will handling calling sk_free.
|
2011-06-24 19:12:12 +00:00
|
|
|
*/
|
2016-03-11 21:02:28 +00:00
|
|
|
static sk_sp<SkData> MakeFromMalloc(const void* data, size_t length);
|
2013-06-11 07:01:17 +00:00
|
|
|
|
2013-06-11 02:20:28 +00:00
|
|
|
/**
|
|
|
|
* Create a new dataref the file with the specified path.
|
|
|
|
* If the file cannot be opened, this returns NULL.
|
|
|
|
*/
|
2016-03-11 21:02:28 +00:00
|
|
|
static sk_sp<SkData> MakeFromFileName(const char path[]);
|
2013-06-11 07:01:17 +00:00
|
|
|
|
2013-03-18 21:08:46 +00:00
|
|
|
/**
|
2015-11-20 21:47:49 +00:00
|
|
|
* Create a new dataref from a stdio FILE.
|
|
|
|
* This does not take ownership of the FILE, nor close it.
|
|
|
|
* The caller is free to close the FILE at its convenience.
|
|
|
|
* The FILE must be open for reading only.
|
2013-04-24 20:03:00 +00:00
|
|
|
* Returns NULL on failure.
|
2013-03-18 21:08:46 +00:00
|
|
|
*/
|
2016-03-11 21:02:28 +00:00
|
|
|
static sk_sp<SkData> MakeFromFILE(FILE* f);
|
2013-03-19 07:15:10 +00:00
|
|
|
|
2013-06-03 17:10:35 +00:00
|
|
|
/**
|
|
|
|
* Create a new dataref from a file descriptor.
|
|
|
|
* This does not take ownership of the file descriptor, nor close it.
|
|
|
|
* The caller is free to close the file descriptor at its convenience.
|
|
|
|
* The file descriptor must be open for reading only.
|
|
|
|
* Returns NULL on failure.
|
|
|
|
*/
|
2016-03-11 21:02:28 +00:00
|
|
|
static sk_sp<SkData> MakeFromFD(int fd);
|
2013-06-04 07:00:53 +00:00
|
|
|
|
2014-09-12 19:12:27 +00:00
|
|
|
/**
|
|
|
|
* Attempt to read size bytes into a SkData. If the read succeeds, return the data,
|
|
|
|
* else return NULL. Either way the stream's cursor may have been changed as a result
|
|
|
|
* of calling read().
|
|
|
|
*/
|
2016-03-11 21:02:28 +00:00
|
|
|
static sk_sp<SkData> MakeFromStream(SkStream*, size_t size);
|
2014-09-12 19:12:27 +00:00
|
|
|
|
2011-06-24 13:11:05 +00:00
|
|
|
/**
|
|
|
|
* Create a new dataref using a subset of the data in the specified
|
|
|
|
* src dataref.
|
|
|
|
*/
|
2016-03-11 21:02:28 +00:00
|
|
|
static sk_sp<SkData> MakeSubset(const SkData* src, size_t offset, size_t length);
|
2011-06-24 13:11:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a new empty dataref (or a reference to a shared empty dataref).
|
|
|
|
* New or shared, the caller must see that unref() is eventually called.
|
|
|
|
*/
|
2016-03-11 21:02:28 +00:00
|
|
|
static sk_sp<SkData> MakeEmpty();
|
|
|
|
|
2011-06-24 13:11:05 +00:00
|
|
|
private:
|
2018-10-30 15:23:00 +00:00
|
|
|
friend class SkNVRefCnt<SkData>;
|
2011-06-24 13:11:05 +00:00
|
|
|
ReleaseProc fReleaseProc;
|
|
|
|
void* fReleaseProcContext;
|
2020-02-17 16:54:25 +00:00
|
|
|
const void* fPtr;
|
2011-06-24 13:11:05 +00:00
|
|
|
size_t fSize;
|
|
|
|
|
|
|
|
SkData(const void* ptr, size_t size, ReleaseProc, void* context);
|
2015-06-18 20:42:03 +00:00
|
|
|
explicit SkData(size_t size); // inplace new/delete
|
2016-08-09 19:20:04 +00:00
|
|
|
~SkData();
|
2012-07-10 17:30:58 +00:00
|
|
|
|
2016-10-19 21:15:44 +00:00
|
|
|
// Ensure the unsized delete is called.
|
2018-07-09 05:14:26 +00:00
|
|
|
void operator delete(void* p);
|
2016-10-19 21:15:44 +00:00
|
|
|
|
2014-09-11 15:42:36 +00:00
|
|
|
// shared internal factory
|
2016-03-11 21:02:28 +00:00
|
|
|
static sk_sp<SkData> PrivateNewWithCopy(const void* srcOrNull, size_t length);
|
2014-09-11 15:42:36 +00:00
|
|
|
|
2016-05-06 15:03:44 +00:00
|
|
|
static void DummyReleaseProc(const void*, void*); // {}
|
2015-06-17 16:58:24 +00:00
|
|
|
|
2013-10-14 14:33:11 +00:00
|
|
|
typedef SkRefCnt INHERITED;
|
2011-06-24 13:11:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|