mirrored from https://skia.googlesource.com/skia
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathSkImage.cpp
More file actions
337 lines (288 loc) · 13.9 KB
/
SkImage.cpp
File metadata and controls
337 lines (288 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Copyright 2012 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/core/SkImage.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkBlendMode.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkData.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPixmap.h"
#include "include/core/SkSurface.h"
#include "include/core/SkSurfaceProps.h"
#include "include/core/SkTileMode.h"
#include "include/core/SkTypes.h"
#include "src/core/SkColorSpacePriv.h"
#include "src/core/SkImageInfoPriv.h"
#include "src/core/SkMipmap.h"
#include "src/core/SkNextID.h"
#include "src/image/SkImage_Base.h"
#include "src/shaders/SkImageShader.h"
#include <utility>
class SkShader;
SkImage::SkImage(const SkImageInfo& info, uint32_t uniqueID)
: fInfo(info)
, fUniqueID(kNeedNewImageUniqueID == uniqueID ? SkNextID::ImageID() : uniqueID) {
SkASSERT(info.width() > 0);
SkASSERT(info.height() > 0);
}
bool SkImage::peekPixels(SkPixmap* pm) const {
SkPixmap tmp;
if (!pm) {
pm = &tmp;
}
return as_IB(this)->onPeekPixels(pm);
}
bool SkImage::readPixels(GrDirectContext* dContext, const SkImageInfo& dstInfo, void* dstPixels,
size_t dstRowBytes, int srcX, int srcY, CachingHint chint) const {
return as_IB(this)->onReadPixels(dContext, dstInfo, dstPixels, dstRowBytes, srcX, srcY, chint);
}
sk_sp<SkImage> SkImage::makeScaled(const SkImageInfo& newInfo,
const SkSamplingOptions& sampling) const {
return makeScaled(nullptr, newInfo, sampling, SkSurfaceProps{});
}
sk_sp<SkImage> SkImage::makeScaled(SkRecorder* recorder,
const SkImageInfo& newInfo,
const SkSamplingOptions& sampling) const {
return makeScaled(recorder, newInfo, sampling, SkSurfaceProps{});
}
sk_sp<SkImage> SkImage::makeScaled(SkRecorder* recorder,
const SkImageInfo& newInfo,
const SkSamplingOptions& sampling,
const SkSurfaceProps& props) const {
if (!SkImageInfoIsValid(newInfo)) {
return nullptr;
}
if (newInfo == this->imageInfo()) {
return sk_ref_sp(this);
}
auto surf = as_IB(this)->onMakeSurface(recorder, newInfo);
if (!surf) {
return nullptr;
}
SkPaint paint;
paint.setBlendMode(SkBlendMode::kSrc);
surf->getCanvas()->drawImageRect(this,
SkRect::MakeIWH(newInfo.width(), newInfo.height()),
sampling,
&paint);
return surf->makeImageSnapshot();
}
#ifndef SK_IMAGE_READ_PIXELS_DISABLE_LEGACY_API
bool SkImage::readPixels(const SkImageInfo& dstInfo, void* dstPixels,
size_t dstRowBytes, int srcX, int srcY, CachingHint chint) const {
auto dContext = as_IB(this)->directContext();
return this->readPixels(dContext, dstInfo, dstPixels, dstRowBytes, srcX, srcY, chint);
}
#endif
void SkImage::asyncRescaleAndReadPixels(const SkImageInfo& info,
const SkIRect& srcRect,
RescaleGamma rescaleGamma,
RescaleMode rescaleMode,
ReadPixelsCallback callback,
ReadPixelsContext context) const {
if (!SkIRect::MakeWH(this->width(), this->height()).contains(srcRect) ||
!SkImageInfoIsValid(info)) {
callback(context, nullptr);
return;
}
as_IB(this)->onAsyncRescaleAndReadPixels(
info, srcRect, rescaleGamma, rescaleMode, callback, context);
}
void SkImage::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
sk_sp<SkColorSpace> dstColorSpace,
const SkIRect& srcRect,
const SkISize& dstSize,
RescaleGamma rescaleGamma,
RescaleMode rescaleMode,
ReadPixelsCallback callback,
ReadPixelsContext context) const {
if (!SkIRect::MakeWH(this->width(), this->height()).contains(srcRect) || dstSize.isZero() ||
(dstSize.width() & 0b1) || (dstSize.height() & 0b1)) {
callback(context, nullptr);
return;
}
as_IB(this)->onAsyncRescaleAndReadPixelsYUV420(yuvColorSpace,
/*readAlpha=*/false,
std::move(dstColorSpace),
srcRect,
dstSize,
rescaleGamma,
rescaleMode,
callback,
context);
}
void SkImage::asyncRescaleAndReadPixelsYUVA420(SkYUVColorSpace yuvColorSpace,
sk_sp<SkColorSpace> dstColorSpace,
const SkIRect& srcRect,
const SkISize& dstSize,
RescaleGamma rescaleGamma,
RescaleMode rescaleMode,
ReadPixelsCallback callback,
ReadPixelsContext context) const {
if (!SkIRect::MakeWH(this->width(), this->height()).contains(srcRect) || dstSize.isZero() ||
(dstSize.width() & 0b1) || (dstSize.height() & 0b1)) {
callback(context, nullptr);
return;
}
as_IB(this)->onAsyncRescaleAndReadPixelsYUV420(yuvColorSpace,
/*readAlpha=*/true,
std::move(dstColorSpace),
srcRect,
dstSize,
rescaleGamma,
rescaleMode,
callback,
context);
}
bool SkImage::scalePixels(const SkPixmap& dst, const SkSamplingOptions& sampling,
CachingHint chint) const {
// Context TODO: Elevate GrDirectContext requirement to public API.
auto dContext = as_IB(this)->directContext();
if (this->width() == dst.width() && this->height() == dst.height()) {
return this->readPixels(dContext, dst, 0, 0, chint);
}
// Idea: If/when SkImageGenerator supports a native-scaling API (where the generator itself
// can scale more efficiently) we should take advantage of it here.
//
SkBitmap bm;
if (as_IB(this)->getROPixels(dContext, &bm, chint)) {
SkPixmap pmap;
// Note: By calling the pixmap scaler, we never cache the final result, so the chint
// is (currently) only being applied to the getROPixels. If we get a request to
// also attempt to cache the final (scaled) result, we would add that logic here.
//
return bm.peekPixels(&pmap) && pmap.scalePixels(dst, sampling);
}
return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
SkColorType SkImage::colorType() const { return fInfo.colorType(); }
SkAlphaType SkImage::alphaType() const { return fInfo.alphaType(); }
SkColorSpace* SkImage::colorSpace() const { return fInfo.colorSpace(); }
sk_sp<SkColorSpace> SkImage::refColorSpace() const { return fInfo.refColorSpace(); }
sk_sp<SkShader> SkImage::makeShader(const SkSamplingOptions& sampling, const SkMatrix& lm) const {
return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)),
SkTileMode::kClamp, SkTileMode::kClamp,
sampling, &lm);
}
sk_sp<SkShader> SkImage::makeShader(const SkSamplingOptions& sampling, const SkMatrix* lm) const {
return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)),
SkTileMode::kClamp, SkTileMode::kClamp,
sampling, lm);
}
sk_sp<SkShader> SkImage::makeShader(SkTileMode tmx, SkTileMode tmy,
const SkSamplingOptions& sampling,
const SkMatrix& lm) const {
return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)), tmx, tmy,
sampling, &lm);
}
sk_sp<SkShader> SkImage::makeShader(SkTileMode tmx, SkTileMode tmy,
const SkSamplingOptions& sampling,
const SkMatrix* localMatrix) const {
return SkImageShader::Make(sk_ref_sp(const_cast<SkImage*>(this)), tmx, tmy,
sampling, localMatrix);
}
sk_sp<SkShader> SkImage::makeRawShader(SkTileMode tmx, SkTileMode tmy,
const SkSamplingOptions& sampling,
const SkMatrix& lm) const {
return SkImageShader::MakeRaw(sk_ref_sp(const_cast<SkImage*>(this)), tmx, tmy,
sampling, &lm);
}
sk_sp<SkShader> SkImage::makeRawShader(const SkSamplingOptions& sampling,
const SkMatrix& lm) const {
return SkImageShader::MakeRaw(sk_ref_sp(const_cast<SkImage*>(this)),
SkTileMode::kClamp, SkTileMode::kClamp,
sampling, &lm);
}
sk_sp<SkShader> SkImage::makeRawShader(const SkSamplingOptions& sampling,
const SkMatrix* localMatrix) const {
return SkImageShader::MakeRaw(sk_ref_sp(const_cast<SkImage*>(this)),
SkTileMode::kClamp, SkTileMode::kClamp,
sampling, localMatrix);
}
sk_sp<SkShader> SkImage::makeRawShader(SkTileMode tmx, SkTileMode tmy,
const SkSamplingOptions& sampling,
const SkMatrix* localMatrix) const {
return SkImageShader::MakeRaw(sk_ref_sp(const_cast<SkImage*>(this)), tmx, tmy,
sampling, localMatrix);
}
sk_sp<const SkData> SkImage::refEncodedData() const { return as_IB(this)->onRefEncoded(); }
///////////////////////////////////////////////////////////////////////////////////////////////////
bool SkImage::readPixels(GrDirectContext* dContext, const SkPixmap& pmap, int srcX, int srcY,
CachingHint chint) const {
return this->readPixels(dContext, pmap.info(), pmap.writable_addr(), pmap.rowBytes(), srcX,
srcY, chint);
}
#ifndef SK_IMAGE_READ_PIXELS_DISABLE_LEGACY_API
bool SkImage::readPixels(const SkPixmap& pmap, int srcX, int srcY, CachingHint chint) const {
auto dContext = as_IB(this)->directContext();
return this->readPixels(dContext, pmap, srcX, srcY, chint);
}
#endif
bool SkImage::asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode ) const {
// Context TODO: Elevate GrDirectContext requirement to public API.
auto dContext = as_IB(this)->directContext();
return as_IB(this)->onAsLegacyBitmap(dContext, bitmap);
}
bool SkImage::isAlphaOnly() const { return SkColorTypeIsAlphaOnly(fInfo.colorType()); }
sk_sp<SkImage> SkImage::reinterpretColorSpace(sk_sp<SkColorSpace> target) const {
if (!target) {
return nullptr;
}
// No need to create a new image if:
// (1) The color spaces are equal.
// (2) The color type is kAlpha8.
SkColorSpace* colorSpace = this->colorSpace();
if (!colorSpace) {
colorSpace = sk_srgb_singleton();
}
if (SkColorSpace::Equals(colorSpace, target.get()) || this->isAlphaOnly()) {
return sk_ref_sp(const_cast<SkImage*>(this));
}
return as_IB(this)->onReinterpretColorSpace(std::move(target));
}
sk_sp<SkImage> SkImage::makeNonTextureImage(GrDirectContext* dContext) const {
if (!this->isTextureBacked()) {
return sk_ref_sp(const_cast<SkImage*>(this));
}
return this->makeRasterImage(dContext, kDisallow_CachingHint);
}
sk_sp<SkImage> SkImage::makeRasterImage(GrDirectContext* dContext, CachingHint chint) const {
SkPixmap pm;
if (this->peekPixels(&pm)) {
return sk_ref_sp(const_cast<SkImage*>(this));
}
const size_t rowBytes = fInfo.minRowBytes();
size_t size = fInfo.computeByteSize(rowBytes);
if (SkImageInfo::ByteSizeOverflowed(size)) {
return nullptr;
}
if (!dContext) {
// Try to get the saved context if the client didn't pass it in (but they really should).
dContext = as_IB(this)->directContext();
}
sk_sp<SkData> data = SkData::MakeUninitialized(size);
pm = {fInfo.makeColorSpace(nullptr), data->writable_data(), fInfo.minRowBytes()};
if (!this->readPixels(dContext, pm, 0, 0, chint)) {
return nullptr;
}
return SkImages::RasterFromData(fInfo, std::move(data), rowBytes);
}
bool SkImage::hasMipmaps() const { return as_IB(this)->onHasMipmaps(); }
bool SkImage::isProtected() const { return as_IB(this)->onIsProtected(); }
sk_sp<SkImage> SkImage::withMipmaps(sk_sp<SkMipmap> mips) const {
if (mips == nullptr || mips->validForRootLevel(this->imageInfo())) {
if (auto result = as_IB(this)->onMakeWithMipmaps(std::move(mips))) {
return result;
}
}
return sk_ref_sp((const_cast<SkImage*>(this)));
}
sk_sp<SkImage> SkImage::withDefaultMipmaps() const {
return this->withMipmaps(nullptr);
}