1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/include/core/SkData.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,148 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2011 Google Inc. 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 + 1.13 + 1.14 +#ifndef SkData_DEFINED 1.15 +#define SkData_DEFINED 1.16 + 1.17 +#include "SkRefCnt.h" 1.18 + 1.19 +struct SkFILE; 1.20 + 1.21 +/** 1.22 + * SkData holds an immutable data buffer. Not only is the data immutable, 1.23 + * but the actual ptr that is returned (by data() or bytes()) is guaranteed 1.24 + * to always be the same for the life of this instance. 1.25 + */ 1.26 +class SK_API SkData : public SkRefCnt { 1.27 +public: 1.28 + SK_DECLARE_INST_COUNT(SkData) 1.29 + 1.30 + /** 1.31 + * Returns the number of bytes stored. 1.32 + */ 1.33 + size_t size() const { return fSize; } 1.34 + 1.35 + bool isEmpty() const { return 0 == fSize; } 1.36 + 1.37 + /** 1.38 + * Returns the ptr to the data. 1.39 + */ 1.40 + const void* data() const { return fPtr; } 1.41 + 1.42 + /** 1.43 + * Like data(), returns a read-only ptr into the data, but in this case 1.44 + * it is cast to uint8_t*, to make it easy to add an offset to it. 1.45 + */ 1.46 + const uint8_t* bytes() const { 1.47 + return reinterpret_cast<const uint8_t*>(fPtr); 1.48 + } 1.49 + 1.50 + /** 1.51 + * Helper to copy a range of the data into a caller-provided buffer. 1.52 + * Returns the actual number of bytes copied, after clamping offset and 1.53 + * length to the size of the data. If buffer is NULL, it is ignored, and 1.54 + * only the computed number of bytes is returned. 1.55 + */ 1.56 + size_t copyRange(size_t offset, size_t length, void* buffer) const; 1.57 + 1.58 + /** 1.59 + * Returns true if these two objects have the same length and contents, 1.60 + * effectively returning 0 == memcmp(...) 1.61 + */ 1.62 + bool equals(const SkData* other) const; 1.63 + 1.64 + /** 1.65 + * Function that, if provided, will be called when the SkData goes out 1.66 + * of scope, allowing for custom allocation/freeing of the data. 1.67 + */ 1.68 + typedef void (*ReleaseProc)(const void* ptr, size_t length, void* context); 1.69 + 1.70 + /** 1.71 + * Create a new dataref by copying the specified data 1.72 + */ 1.73 + static SkData* NewWithCopy(const void* data, size_t length); 1.74 + 1.75 + /** 1.76 + * Create a new dataref by copying the specified c-string 1.77 + * (a null-terminated array of bytes). The returned SkData will have size() 1.78 + * equal to strlen(cstr) + 1. If cstr is NULL, it will be treated the same 1.79 + * as "". 1.80 + */ 1.81 + static SkData* NewWithCString(const char cstr[]); 1.82 + 1.83 + /** 1.84 + * Create a new dataref, taking the data ptr as is, and using the 1.85 + * releaseproc to free it. The proc may be NULL. 1.86 + */ 1.87 + static SkData* NewWithProc(const void* data, size_t length, 1.88 + ReleaseProc proc, void* context); 1.89 + 1.90 + /** 1.91 + * Create a new dataref from a pointer allocated by malloc. The Data object 1.92 + * takes ownership of that allocation, and will handling calling sk_free. 1.93 + */ 1.94 + static SkData* NewFromMalloc(const void* data, size_t length); 1.95 + 1.96 + /** 1.97 + * Create a new dataref the file with the specified path. 1.98 + * If the file cannot be opened, this returns NULL. 1.99 + */ 1.100 + static SkData* NewFromFileName(const char path[]); 1.101 + 1.102 + /** 1.103 + * Create a new dataref from a SkFILE. 1.104 + * This does not take ownership of the SkFILE, nor close it. 1.105 + * The caller is free to close the SkFILE at its convenience. 1.106 + * The SkFILE must be open for reading only. 1.107 + * Returns NULL on failure. 1.108 + */ 1.109 + static SkData* NewFromFILE(SkFILE* f); 1.110 + 1.111 + /** 1.112 + * Create a new dataref from a file descriptor. 1.113 + * This does not take ownership of the file descriptor, nor close it. 1.114 + * The caller is free to close the file descriptor at its convenience. 1.115 + * The file descriptor must be open for reading only. 1.116 + * Returns NULL on failure. 1.117 + */ 1.118 + static SkData* NewFromFD(int fd); 1.119 + 1.120 + /** 1.121 + * Create a new dataref using a subset of the data in the specified 1.122 + * src dataref. 1.123 + */ 1.124 + static SkData* NewSubset(const SkData* src, size_t offset, size_t length); 1.125 + 1.126 + /** 1.127 + * Returns a new empty dataref (or a reference to a shared empty dataref). 1.128 + * New or shared, the caller must see that unref() is eventually called. 1.129 + */ 1.130 + static SkData* NewEmpty(); 1.131 + 1.132 +private: 1.133 + ReleaseProc fReleaseProc; 1.134 + void* fReleaseProcContext; 1.135 + 1.136 + const void* fPtr; 1.137 + size_t fSize; 1.138 + 1.139 + SkData(const void* ptr, size_t size, ReleaseProc, void* context); 1.140 + virtual ~SkData(); 1.141 + 1.142 + // Called the first time someone calls NewEmpty to initialize the singleton. 1.143 + static void NewEmptyImpl(int/*unused*/); 1.144 + 1.145 + typedef SkRefCnt INHERITED; 1.146 +}; 1.147 + 1.148 +/** Typedef of SkAutoTUnref<SkData> for automatically unref-ing a SkData. */ 1.149 +typedef SkAutoTUnref<SkData> SkAutoDataUnref; 1.150 + 1.151 +#endif