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 #include "SkAutoPixmapStorage.h" |
| 10 #include "SkData.h" |
| 11 |
| 12 SkAutoPixmapStorage::SkAutoPixmapStorage() : fStorage(nullptr) {} |
| 13 |
| 14 SkAutoPixmapStorage::~SkAutoPixmapStorage() { |
| 15 this->freeStorage(); |
| 16 } |
| 17 |
| 18 size_t SkAutoPixmapStorage::AllocSize(const SkImageInfo& info, size_t* rowBytes)
{ |
| 19 size_t rb = info.minRowBytes(); |
| 20 if (rowBytes) { |
| 21 *rowBytes = rb; |
| 22 } |
| 23 return info.getSafeSize(rb); |
| 24 } |
| 25 |
| 26 bool SkAutoPixmapStorage::tryAlloc(const SkImageInfo& info) { |
| 27 this->freeStorage(); |
| 28 |
| 29 size_t rb; |
| 30 size_t size = AllocSize(info, &rb); |
| 31 if (0 == size) { |
| 32 return false; |
| 33 } |
| 34 void* pixels = sk_malloc_flags(size, 0); |
| 35 if (nullptr == pixels) { |
| 36 return false; |
| 37 } |
| 38 this->reset(info, pixels, rb); |
| 39 fStorage = pixels; |
| 40 return true; |
| 41 } |
| 42 |
| 43 void SkAutoPixmapStorage::alloc(const SkImageInfo& info) { |
| 44 if (!this->tryAlloc(info)) { |
| 45 sk_throw(); |
| 46 } |
| 47 } |
| 48 |
| 49 const SkData* SkAutoPixmapStorage::detachPixelsAsData() { |
| 50 if (!fStorage) { |
| 51 return nullptr; |
| 52 } |
| 53 |
| 54 auto data = SkData::MakeFromMalloc(fStorage, this->getSafeSize()); |
| 55 fStorage = nullptr; |
| 56 this->INHERITED::reset(); |
| 57 |
| 58 return data.release(); |
| 59 } |
| 60 |
| 61 void SkAutoPixmapStorage::release() { |
| 62 fStorage = nullptr; |
| 63 this->INHERITED::reset(); |
| 64 } |
OLD | NEW |