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