Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkMallocPixelRef.cpp
Go to the documentation of this file.
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
9
12#include "include/core/SkData.h"
16
17#include <utility>
18
19static 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
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);
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
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}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
@ kLastEnum_SkAlphaType
last valid value
Definition SkAlphaType.h:31
#define SkASSERT(cond)
Definition SkAssert.h:116
@ kLastEnum_SkColorType
last valid value
Definition SkColorType.h:56
static bool is_valid(const SkImageInfo &info)
static void * sk_calloc_canfail(size_t size)
Definition SkMalloc.h:75
SK_API void sk_free(void *)
void * pixels() const
Definition SkPixelRef.h:36
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition main.cc:19
struct MyStruct s
SK_API sk_sp< SkPixelRef > MakeWithData(const SkImageInfo &, size_t rowBytes, sk_sp< SkData > data)
SK_API sk_sp< SkPixelRef > MakeAllocate(const SkImageInfo &, size_t rowBytes)
SkScalar w
SkScalar h
static bool ByteSizeOverflowed(size_t byteSize)