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