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