robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 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 | |
| 11 | SkAutoPixmapStorage::SkAutoPixmapStorage() : fStorage(nullptr) {} |
| 12 | |
| 13 | SkAutoPixmapStorage::~SkAutoPixmapStorage() { |
| 14 | this->freeStorage(); |
| 15 | } |
| 16 | |
Eric Karl | 7a8c84c | 2017-06-12 10:05:49 -0700 | [diff] [blame] | 17 | SkAutoPixmapStorage& SkAutoPixmapStorage::operator=(SkAutoPixmapStorage&& other) { |
| 18 | this->fStorage = other.fStorage; |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 19 | this->INHERITED::reset(other.info(), this->fStorage, other.rowBytes()); |
Eric Karl | 7a8c84c | 2017-06-12 10:05:49 -0700 | [diff] [blame] | 20 | |
| 21 | other.fStorage = nullptr; |
| 22 | other.INHERITED::reset(); |
| 23 | |
| 24 | return *this; |
| 25 | } |
| 26 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 27 | size_t SkAutoPixmapStorage::AllocSize(const SkImageInfo& info, size_t* rowBytes) { |
| 28 | size_t rb = info.minRowBytes(); |
| 29 | if (rowBytes) { |
| 30 | *rowBytes = rb; |
| 31 | } |
Mike Reed | cd284c5 | 2017-09-29 15:22:56 -0400 | [diff] [blame^] | 32 | return info.computeByteSize(rb); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | bool SkAutoPixmapStorage::tryAlloc(const SkImageInfo& info) { |
| 36 | this->freeStorage(); |
| 37 | |
| 38 | size_t rb; |
| 39 | size_t size = AllocSize(info, &rb); |
| 40 | if (0 == size) { |
| 41 | return false; |
| 42 | } |
| 43 | void* pixels = sk_malloc_flags(size, 0); |
| 44 | if (nullptr == pixels) { |
| 45 | return false; |
| 46 | } |
| 47 | this->reset(info, pixels, rb); |
| 48 | fStorage = pixels; |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | void SkAutoPixmapStorage::alloc(const SkImageInfo& info) { |
Ben Wagner | 7ca9a74 | 2017-08-17 14:05:04 -0400 | [diff] [blame] | 53 | SkASSERT_RELEASE(this->tryAlloc(info)); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | const SkData* SkAutoPixmapStorage::detachPixelsAsData() { |
| 57 | if (!fStorage) { |
| 58 | return nullptr; |
| 59 | } |
| 60 | |
Mike Reed | cd284c5 | 2017-09-29 15:22:56 -0400 | [diff] [blame^] | 61 | auto data = SkData::MakeFromMalloc(fStorage, this->computeByteSize()); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 62 | fStorage = nullptr; |
| 63 | this->INHERITED::reset(); |
| 64 | |
| 65 | return data.release(); |
| 66 | } |