OLD | NEW |
(Empty) | |
| 1 |
| 2 /* |
| 3 * Copyright 2016 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. |
| 7 */ |
| 8 |
| 9 #ifndef SkAutoPixmapStorage_DEFINED |
| 10 #define SkAutoPixmapStorage_DEFINED |
| 11 |
| 12 #include "SkPixmap.h" |
| 13 |
| 14 class SK_API SkAutoPixmapStorage : public SkPixmap { |
| 15 public: |
| 16 SkAutoPixmapStorage(); |
| 17 ~SkAutoPixmapStorage(); |
| 18 |
| 19 /** |
| 20 * Try to allocate memory for the pixels needed to match the specified Info.
On success |
| 21 * return true and fill out the pixmap to point to that memory. The storage
will be freed |
| 22 * when this object is destroyed, or if another call to tryAlloc() or alloc(
) is made. |
| 23 * |
| 24 * On failure, return false and reset() the pixmap to empty. |
| 25 */ |
| 26 bool tryAlloc(const SkImageInfo&); |
| 27 |
| 28 /** |
| 29 * Allocate memory for the pixels needed to match the specified Info and fil
l out the pixmap |
| 30 * to point to that memory. The storage will be freed when this object is de
stroyed, |
| 31 * or if another call to tryAlloc() or alloc() is made. |
| 32 * |
| 33 * If the memory cannot be allocated, calls sk_throw(). |
| 34 */ |
| 35 void alloc(const SkImageInfo&); |
| 36 |
| 37 /** |
| 38 * Gets the size and optionally the rowBytes that would be allocated by SkAut
oPixmapStorage if |
| 39 * alloc/tryAlloc was called. |
| 40 */ |
| 41 static size_t AllocSize(const SkImageInfo& info, size_t* rowBytes); |
| 42 |
| 43 /** |
| 44 * Returns an SkData object wrapping the allocated pixels memory, and resets
the pixmap. |
| 45 * If the storage hasn't been allocated, the result is NULL. |
| 46 */ |
| 47 const SkData* SK_WARN_UNUSED_RESULT detachPixelsAsData(); |
| 48 |
| 49 /** |
| 50 * Whereas 'reset' frees the backing memory and then clears the SkPixmap, |
| 51 * this entry point disowns the backing memory before clearing so the memory |
| 52 * isn't freed. It can be used when the Pixmap has been installed into |
| 53 * an SkBitmap and the SkBitmap should manage the memory's lifetime. |
| 54 */ |
| 55 void release(); |
| 56 |
| 57 // We wrap these so we can clear our internal storage |
| 58 |
| 59 void reset() { |
| 60 this->freeStorage(); |
| 61 this->INHERITED::reset(); |
| 62 } |
| 63 void reset(const SkImageInfo& info, const void* addr, size_t rb, SkColorTabl
e* ctable = NULL) { |
| 64 this->freeStorage(); |
| 65 this->INHERITED::reset(info, addr, rb, ctable); |
| 66 } |
| 67 void reset(const SkImageInfo& info) { |
| 68 this->freeStorage(); |
| 69 this->INHERITED::reset(info); |
| 70 } |
| 71 bool SK_WARN_UNUSED_RESULT reset(const SkMask& mask) { |
| 72 this->freeStorage(); |
| 73 return this->INHERITED::reset(mask); |
| 74 } |
| 75 |
| 76 private: |
| 77 void* fStorage; |
| 78 |
| 79 void freeStorage() { |
| 80 sk_free(fStorage); |
| 81 fStorage = nullptr; |
| 82 } |
| 83 |
| 84 typedef SkPixmap INHERITED; |
| 85 }; |
| 86 |
| 87 #endif |
OLD | NEW |