Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkImage_RasterFactories.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2023 Google LLC
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
11#include "include/core/SkData.h"
23
24#include <cstddef>
25#include <cstdint>
26#include <utility>
27
28class SkImageFilter;
29struct SkIPoint;
30struct SkIRect;
32
33static bool valid_args(const SkImageInfo& info, size_t rowBytes, size_t* minSize) {
34 const int maxDimension = SK_MaxS32 >> 2;
35
36 // TODO(mtklein): eliminate anything here that setInfo() has already checked.
37 SkBitmap b;
38 if (!b.setInfo(info, rowBytes)) {
39 return false;
40 }
41
42 if (info.width() <= 0 || info.height() <= 0) {
43 return false;
44 }
45 if (info.width() > maxDimension || info.height() > maxDimension) {
46 return false;
47 }
48 if ((unsigned)info.colorType() > (unsigned)kLastEnum_SkColorType) {
49 return false;
50 }
51 if ((unsigned)info.alphaType() > (unsigned)kLastEnum_SkAlphaType) {
52 return false;
53 }
54
55 if (kUnknown_SkColorType == info.colorType()) {
56 return false;
57 }
58 if (!info.validRowBytes(rowBytes)) {
59 return false;
60 }
61
62 size_t size = info.computeByteSize(rowBytes);
64 return false;
65 }
66
67 if (minSize) {
68 *minSize = size;
69 }
70 return true;
71}
72
73namespace SkImages {
75 if (!bm.pixelRef()) {
76 return nullptr;
77 }
78
80}
81
85
87 size_t size;
88 if (!valid_args(info, rowBytes, &size) || !data) {
89 return nullptr;
90 }
91
92 // did they give us enough data?
93 if (data->size() < size) {
94 return nullptr;
95 }
96
97 return sk_make_sp<SkImage_Raster>(info, std::move(data), rowBytes);
98}
99
101 const SkImageFilter* filter,
102 const SkIRect& subset,
103 const SkIRect& clipBounds,
104 SkIRect* outSubset,
105 SkIPoint* offset) {
106 if (!src || !filter) {
107 return nullptr;
108 }
109
111 return as_IFB(filter)->makeImageWithFilter(std::move(backend),
112 std::move(src),
113 subset,
114 clipBounds,
115 outSubset,
116 offset);
117}
118
119// TODO: this could be improved to decode and make use of the mipmap
120// levels potentially present in the compressed data. For now, any
121// mipmap levels are discarded.
123 int width,
124 int height,
126 size_t expectedSize = SkCompressedFormatDataSize(type, {width, height}, false);
127 if (!data || data->size() < expectedSize) {
128 return nullptr;
129 }
130
131 SkAlphaType at =
133
135
136 if (!valid_args(ii, ii.minRowBytes(), nullptr)) {
137 return nullptr;
138 }
139
141 if (!bitmap.tryAllocPixels(ii)) {
142 return nullptr;
143 }
144
145 if (!SkDecompress(std::move(data), {width, height}, type, &bitmap)) {
146 return nullptr;
147 }
148
149 bitmap.setImmutable();
150 return RasterFromBitmap(bitmap);
151}
152
154 size_t size;
155 if (!valid_args(pmap.info(), pmap.rowBytes(), &size) || !pmap.addr()) {
156 return nullptr;
157 }
158
159 sk_sp<SkData> data(SkData::MakeWithProc(pmap.addr(), size, proc, ctx));
160 return sk_make_sp<SkImage_Raster>(pmap.info(), std::move(data), pmap.rowBytes());
161}
162
163} // namespace SkImages
164
165sk_sp<SkImage> MakeRasterCopyPriv(const SkPixmap& pmap, uint32_t id) {
166 size_t size;
167 if (!valid_args(pmap.info(), pmap.rowBytes(), &size) || !pmap.addr()) {
168 return nullptr;
169 }
170
171 // Here we actually make a copy of the caller's pixel data
172 sk_sp<SkData> data(SkData::MakeWithCopy(pmap.addr(), size));
173 return sk_make_sp<SkImage_Raster>(pmap.info(), std::move(data), pmap.rowBytes(), id);
174}
const char * backend
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
SkAlphaType
Definition SkAlphaType.h:26
@ kOpaque_SkAlphaType
pixel is opaque
Definition SkAlphaType.h:28
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
@ kLastEnum_SkAlphaType
last valid value
Definition SkAlphaType.h:31
@ kLastEnum_SkColorType
last valid value
Definition SkColorType.h:56
@ kUnknown_SkColorType
uninitialized
Definition SkColorType.h:20
bool SkDecompress(sk_sp< SkData > data, SkISize dimensions, SkTextureCompressionType compressionType, SkBitmap *dst)
size_t SkCompressedFormatDataSize(SkTextureCompressionType compressionType, SkISize dimensions, bool mipmapped)
static constexpr bool SkTextureCompressionTypeIsOpaque(SkTextureCompressionType compression)
static SkImageFilter_Base * as_IFB(SkImageFilter *filter)
SK_SPI sk_sp< SkImage > SkMakeImageFromRasterBitmap(const SkBitmap &, SkCopyPixelsMode)
@ kIfMutable_SkCopyPixelsMode
only copy src pixels if they are marked mutable
Definition SkImagePriv.h:18
@ kNeedNewImageUniqueID
sk_sp< SkImage > MakeRasterCopyPriv(const SkPixmap &pmap, uint32_t id)
static bool valid_args(const SkImageInfo &info, size_t rowBytes, size_t *minSize)
sk_sp< SkImage > MakeRasterCopyPriv(const SkPixmap &pmap, uint32_t id)
static constexpr int32_t SK_MaxS32
Definition SkMath.h:21
SkPixelRef * pixelRef() const
Definition SkBitmap.h:720
static sk_sp< SkData > MakeWithProc(const void *ptr, size_t length, ReleaseProc proc, void *ctx)
Definition SkData.cpp:128
static sk_sp< SkData > MakeWithCopy(const void *data, size_t length)
Definition SkData.cpp:111
sk_sp< SkImage > makeImageWithFilter(sk_sp< skif::Backend > backend, sk_sp< SkImage > src, const SkIRect &subset, const SkIRect &clipBounds, SkIRect *outSubset, SkIPoint *offset) const
size_t rowBytes() const
Definition SkPixmap.h:145
const SkImageInfo & info() const
Definition SkPixmap.h:135
const void * addr() const
Definition SkPixmap.h:153
static bool b
SK_API sk_sp< SkImage > RasterFromData(const SkImageInfo &info, sk_sp< SkData > pixels, size_t rowBytes)
SK_API sk_sp< SkImage > RasterFromPixmap(const SkPixmap &pixmap, RasterReleaseProc rasterReleaseProc, ReleaseContext releaseContext)
SK_API sk_sp< SkImage > RasterFromBitmap(const SkBitmap &bitmap)
void * ReleaseContext
Definition SkImage.h:50
SK_API sk_sp< SkImage > RasterFromPixmapCopy(const SkPixmap &pixmap)
SK_API sk_sp< SkImage > MakeWithFilter(sk_sp< SkImage > src, const SkImageFilter *filter, const SkIRect &subset, const SkIRect &clipBounds, SkIRect *outSubset, SkIPoint *offset)
SK_API sk_sp< SkImage > RasterFromCompressedTextureData(sk_sp< SkData > data, int width, int height, SkTextureCompressionType type)
void(const void *pixels, ReleaseContext) RasterReleaseProc
Definition SkImage.h:54
sk_sp< Backend > MakeRasterBackend(const SkSurfaceProps &surfaceProps, SkColorType colorType)
int32_t height
int32_t width
Point offset
constexpr SkISize size() const
Definition SkRect.h:172
static bool ByteSizeOverflowed(size_t byteSize)
size_t minRowBytes() const
static SkImageInfo MakeN32(int width, int height, SkAlphaType at)