Debian Codegraph @

src/core/SkAutoPixmapStorage.cpp

raw
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 #include "src/core/SkAutoPixmapStorage.h"
8
9 #include "include/core/SkData.h"
10 #include "include/core/SkImageInfo.h"
11 #include "include/private/base/SkAssert.h"
12
13 #include <utility>
14
15 SkAutoPixmapStorage::SkAutoPixmapStorage() : fStorage(nullptr) {}
16
17 SkAutoPixmapStorage::~SkAutoPixmapStorage() {
18 this->freeStorage();
19 }
20
21 SkAutoPixmapStorage::SkAutoPixmapStorage(SkAutoPixmapStorage&& other) : fStorage(nullptr) {
22 *this = std::move(other);
23 }
24
25 SkAutoPixmapStorage& SkAutoPixmapStorage::operator=(SkAutoPixmapStorage&& other) {
26 this->fStorage = other.fStorage;
27 this->INHERITED::reset(other.info(), this->fStorage, other.rowBytes());
28
29 other.fStorage = nullptr;
30 other.INHERITED::reset();
31
32 return *this;
33 }
34
35 size_t SkAutoPixmapStorage::AllocSize(const SkImageInfo& info, size_t* rowBytes) {
36 size_t rb = info.minRowBytes();
37 if (rowBytes) {
38 *rowBytes = rb;
39 }
40 return info.computeByteSize(rb);
41 }
42
43 bool SkAutoPixmapStorage::tryAlloc(const SkImageInfo& info) {
44 this->freeStorage();
45
46 size_t rb;
47 size_t size = AllocSize(info, &rb);
48 if (SkImageInfo::ByteSizeOverflowed(size)) {
49 return false;
50 }
51 void* pixels = sk_malloc_canfail(size);
52 if (nullptr == pixels) {
53 return false;
54 }
55 this->reset(info, pixels, rb);
56 fStorage = pixels;
57 return true;
58 }
59
60 void SkAutoPixmapStorage::alloc(const SkImageInfo& info) {
61 SkASSERT_RELEASE(this->tryAlloc(info));
62 }
63
64 void* SkAutoPixmapStorage::detachPixels() {
65 if (!fStorage) {
66 return nullptr;
67 }
68
69 void* data = fStorage;
70 fStorage = nullptr;
71 this->INHERITED::reset();
72
73 return data;
74 }
75
76 sk_sp<SkData> SkAutoPixmapStorage::detachPixelsAsData() {
77 if (!fStorage) {
78 return nullptr;
79 }
80
81 sk_sp<SkData> data = SkData::MakeFromMalloc(fStorage, this->computeByteSize());
82 fStorage = nullptr;
83 this->INHERITED::reset();
84
85 return data;
86 }