blob: 865b3a6029def8b69d460b6924ab5789d3af8448 [file] [log] [blame]
robertphillipsc5035e72016-03-17 06:58:39 -07001/*
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
11SkAutoPixmapStorage::SkAutoPixmapStorage() : fStorage(nullptr) {}
12
13SkAutoPixmapStorage::~SkAutoPixmapStorage() {
14 this->freeStorage();
15}
16
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
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
42void SkAutoPixmapStorage::alloc(const SkImageInfo& info) {
43 if (!this->tryAlloc(info)) {
44 sk_throw();
45 }
46}
47
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}