xref: /aosp_15_r20/external/skia/src/core/SkMallocPixelRef.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2011 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/SkMallocPixelRef.h"
9 
10 #include "include/core/SkAlphaType.h"
11 #include "include/core/SkColorType.h"
12 #include "include/core/SkData.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkPixelRef.h"
15 #include "include/private/base/SkMalloc.h"
16 
17 #include <utility>
18 
is_valid(const SkImageInfo & info)19 static bool is_valid(const SkImageInfo& info) {
20     if (info.width() < 0 || info.height() < 0 ||
21         (unsigned)info.colorType() > (unsigned)kLastEnum_SkColorType ||
22         (unsigned)info.alphaType() > (unsigned)kLastEnum_SkAlphaType)
23     {
24         return false;
25     }
26     return true;
27 }
28 
MakeAllocate(const SkImageInfo & info,size_t rowBytes)29 sk_sp<SkPixelRef> SkMallocPixelRef::MakeAllocate(const SkImageInfo& info, size_t rowBytes) {
30     if (rowBytes == 0) {
31         rowBytes = info.minRowBytes();
32         // rowBytes can still be zero, if it overflowed (width * bytesPerPixel > size_t)
33         // or if colortype is unknown
34     }
35     if (!is_valid(info) || !info.validRowBytes(rowBytes)) {
36         return nullptr;
37     }
38     size_t size = info.computeByteSize(rowBytes);
39     if (SkImageInfo::ByteSizeOverflowed(size)) {
40         return nullptr;
41     }
42 #if defined(SK_BUILD_FOR_FUZZER)
43     if (size > 10000000) {
44         return nullptr;
45     }
46 #endif
47     void* addr = sk_calloc_canfail(size);
48     if (nullptr == addr) {
49         return nullptr;
50     }
51 
52     struct PixelRef final : public SkPixelRef {
53         PixelRef(int w, int h, void* s, size_t r) : SkPixelRef(w, h, s, r) {}
54         ~PixelRef() override { sk_free(this->pixels()); }
55     };
56     return sk_sp<SkPixelRef>(new PixelRef(info.width(), info.height(), addr, rowBytes));
57 }
58 
MakeWithData(const SkImageInfo & info,size_t rowBytes,sk_sp<SkData> data)59 sk_sp<SkPixelRef> SkMallocPixelRef::MakeWithData(const SkImageInfo& info,
60                                                  size_t rowBytes,
61                                                  sk_sp<SkData> data) {
62     SkASSERT(data != nullptr);
63     if (!is_valid(info)) {
64         return nullptr;
65     }
66     // TODO: what should we return if computeByteSize returns 0?
67     // - the info was empty?
68     // - we overflowed computing the size?
69     if ((rowBytes < info.minRowBytes()) || (data->size() < info.computeByteSize(rowBytes))) {
70         return nullptr;
71     }
72     struct PixelRef final : public SkPixelRef {
73         sk_sp<SkData> fData;
74         PixelRef(int w, int h, void* s, size_t r, sk_sp<SkData> d)
75             : SkPixelRef(w, h, s, r), fData(std::move(d)) {}
76     };
77     void* pixels = const_cast<void*>(data->data());
78     sk_sp<SkPixelRef> pr(new PixelRef(info.width(), info.height(), pixels, rowBytes,
79                                       std::move(data)));
80     pr->setImmutable(); // since we were created with (immutable) data
81     return pr;
82 }
83