Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
third_party
skia
src
core
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
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
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
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
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
}
info
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition
DM.cpp:213
SkAlphaType.h
kLastEnum_SkAlphaType
@ kLastEnum_SkAlphaType
last valid value
Definition
SkAlphaType.h:31
SkASSERT
#define SkASSERT(cond)
Definition
SkAssert.h:116
SkColorType.h
kLastEnum_SkColorType
@ kLastEnum_SkColorType
last valid value
Definition
SkColorType.h:56
SkData.h
SkImageInfo.h
is_valid
static bool is_valid(const SkImageInfo &info)
Definition
SkMallocPixelRef.cpp:19
SkMallocPixelRef.h
SkMalloc.h
sk_calloc_canfail
static void * sk_calloc_canfail(size_t size)
Definition
SkMalloc.h:75
sk_free
SK_API void sk_free(void *)
Definition
SkMemory_malloc.cpp:83
SkPixelRef.h
SkPixelRef
Definition
SkPixelRef.h:28
SkPixelRef::pixels
void * pixels() const
Definition
SkPixelRef.h:36
sk_sp
Definition
SkRefCnt.h:220
d
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition
main.cc:19
s
struct MyStruct s
SkMallocPixelRef::MakeWithData
SK_API sk_sp< SkPixelRef > MakeWithData(const SkImageInfo &, size_t rowBytes, sk_sp< SkData > data)
Definition
SkMallocPixelRef.cpp:59
SkMallocPixelRef::MakeAllocate
SK_API sk_sp< SkPixelRef > MakeAllocate(const SkImageInfo &, size_t rowBytes)
Definition
SkMallocPixelRef.cpp:29
w
SkScalar w
Definition
pictureshadertile.cpp:30
h
SkScalar h
Definition
pictureshadertile.cpp:30
SkImageInfo
Definition
SkImageInfo.h:208
SkImageInfo::ByteSizeOverflowed
static bool ByteSizeOverflowed(size_t byteSize)
Definition
SkImageInfo.h:588
Generated on Fri Apr 26 2024 06:16:44 for Flutter Engine by
1.9.8