Debian Codegraph @

include/core/SkImage.h

raw
1 /*
2 * Copyright 2012 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 #ifndef SkImage_DEFINED
9 #define SkImage_DEFINED
10
11 #include "include/core/SkAlphaType.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkSize.h"
16 #include "include/private/base/SkAPI.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <memory>
21 #include <optional>
22
23 class GrDirectContext;
24 class SkBitmap;
25 class SkColorSpace;
26 class SkData;
27 class SkImage;
28 class SkImageFilter;
29 class SkImageGenerator;
30 class SkMatrix;
31 class SkMipmap;
32 class SkPaint;
33 class SkPicture;
34 class SkPixmap;
35 class SkRecorder;
36 class SkShader;
37 class SkSurfaceProps;
38 enum SkColorType : int;
39 enum class SkTextureCompressionType;
40 enum class SkTileMode;
41
42 struct SkIPoint;
43 struct SkSamplingOptions;
44
45 namespace SkImages {
46
47 /** Caller data passed to RasterReleaseProc; may be nullptr. */
48 using ReleaseContext = void*;
49 /** Function called when SkImage no longer shares pixels. ReleaseContext is
50 provided by caller when SkImage is created, and may be nullptr.
51 */
52 using RasterReleaseProc = void(const void* pixels, ReleaseContext);
53
54 /** Creates a CPU-backed SkImage from bitmap, sharing or copying bitmap pixels. If the bitmap
55 is marked immutable, and its pixel memory is shareable, it may be shared
56 instead of copied.
57
58 SkImage is returned if bitmap is valid. Valid SkBitmap parameters include:
59 dimensions are greater than zero;
60 each dimension fits in 29 bits;
61 SkColorType and SkAlphaType are valid, and SkColorType is not kUnknown_SkColorType;
62 row bytes are large enough to hold one row of pixels;
63 pixel address is not nullptr.
64
65 @param bitmap SkImageInfo, row bytes, and pixels
66 @return created SkImage, or nullptr
67 */
68 SK_API sk_sp<SkImage> RasterFromBitmap(const SkBitmap& bitmap);
69
70 /** Creates a CPU-backed SkImage from compressed data.
71
72 This method will decompress the compressed data and create an image wrapping
73 it. Any mipmap levels present in the compressed data are discarded.
74
75 @param data compressed data to store in SkImage
76 @param width width of full SkImage
77 @param height height of full SkImage
78 @param type type of compression used
79 @return created SkImage, or nullptr
80 */
81 SK_API sk_sp<SkImage> RasterFromCompressedTextureData(sk_sp<SkData> data,
82 int width,
83 int height,
84 SkTextureCompressionType type);
85
86 /**
87 * Return a SkImage using the encoded data, but attempts to defer decoding until the
88 * image is actually used/drawn. This deferral allows the system to cache the result, either on the
89 * CPU or on the GPU, depending on where the image is drawn. If memory is low, the cache may
90 * be purged, causing the next draw of the image to have to re-decode.
91 *
92 * If alphaType is nullopt, the image's alpha type will be chosen automatically based on the
93 * image format. Transparent images will default to kPremul_SkAlphaType. If alphaType contains
94 * kPremul_SkAlphaType or kUnpremul_SkAlphaType, that alpha type will be used. Forcing opaque
95 * (passing kOpaque_SkAlphaType) is not allowed, and will return nullptr.
96 *
97 * If the encoded format is not supported, nullptr is returned.
98 *
99 * If possible, clients should use SkCodecs::DeferredImage instead.
100 *
101 * @param encoded the encoded data
102 * @return created SkImage, or nullptr
103
104 example: https://fiddle.skia.org/c/@Image_DeferredFromEncodedData
105 */
106 SK_API sk_sp<SkImage> DeferredFromEncodedData(sk_sp<const SkData> encoded,
107 std::optional<SkAlphaType> alphaType = std::nullopt);
108
109 /** Creates SkImage from data returned by imageGenerator. The image data will not be created
110 (on either the CPU or GPU) until the image is actually drawn.
111 Generated data is owned by SkImage and may not be shared or accessed.
112
113 SkImage is returned if generator data is valid. Valid data parameters vary by type of data
114 and platform.
115
116 imageGenerator may wrap SkPicture data, codec data, or custom data.
117
118 @param imageGenerator stock or custom routines to retrieve SkImage
119 @return created SkImage, or nullptr
120 */
121 SK_API sk_sp<SkImage> DeferredFromGenerator(std::unique_ptr<SkImageGenerator> imageGenerator);
122
123 enum class BitDepth {
124 kU8, //!< uses 8-bit unsigned int per color component
125 kF16, //!< uses 16-bit float per color component
126 };
127
128 /** Creates SkImage from picture. Returned SkImage width and height are set by dimensions.
129 SkImage draws picture with matrix and paint, set to bitDepth and colorSpace.
130
131 The Picture data is not turned into an image (CPU or GPU) until it is drawn.
132
133 If matrix is nullptr, draws with identity SkMatrix. If paint is nullptr, draws
134 with default SkPaint. colorSpace may be nullptr.
135
136 @param picture stream of drawing commands
137 @param dimensions width and height
138 @param matrix SkMatrix to rotate, scale, translate, and so on; may be nullptr
139 @param paint SkPaint to apply transparency, filtering, and so on; may be nullptr
140 @param bitDepth 8-bit integer or 16-bit float: per component
141 @param colorSpace range of colors; may be nullptr
142 @param props props to use when rasterizing the picture
143 @return created SkImage, or nullptr
144 */
145 SK_API sk_sp<SkImage> DeferredFromPicture(sk_sp<SkPicture> picture,
146 const SkISize& dimensions,
147 const SkMatrix* matrix,
148 const SkPaint* paint,
149 BitDepth bitDepth,
150 sk_sp<SkColorSpace> colorSpace,
151 SkSurfaceProps props);
152 SK_API sk_sp<SkImage> DeferredFromPicture(sk_sp<SkPicture> picture,
153 const SkISize& dimensions,
154 const SkMatrix* matrix,
155 const SkPaint* paint,
156 BitDepth bitDepth,
157 sk_sp<SkColorSpace> colorSpace);
158
159 /** Creates a CPU-backed SkImage from pixmap, copying the pixel data.
160 As a result, pixmap pixels may be modified or deleted without affecting SkImage.
161
162 SkImage is returned if SkPixmap is valid. Valid SkPixmap parameters include:
163 dimensions are greater than zero;
164 each dimension fits in 29 bits;
165 SkColorType and SkAlphaType are valid, and SkColorType is not kUnknown_SkColorType;
166 row bytes are large enough to hold one row of pixels;
167 pixel address is not nullptr.
168
169 @param pixmap SkImageInfo, pixel address, and row bytes
170 @return copy of SkPixmap pixels, or nullptr
171
172 example: https://fiddle.skia.org/c/@Image_RasterFromPixmapCopy
173 */
174 SK_API sk_sp<SkImage> RasterFromPixmapCopy(const SkPixmap& pixmap);
175
176 /** Creates CPU-backed SkImage from pixmap, sharing SkPixmap pixels. Pixels must remain valid and
177 unchanged until rasterReleaseProc is called. rasterReleaseProc is passed
178 releaseContext when SkImage is deleted or no longer refers to pixmap pixels.
179
180 Pass nullptr for rasterReleaseProc to share SkPixmap without requiring a callback
181 when SkImage is released. Pass nullptr for releaseContext if rasterReleaseProc
182 does not require state.
183
184 SkImage is returned if pixmap is valid. Valid SkPixmap parameters include:
185 dimensions are greater than zero;
186 each dimension fits in 29 bits;
187 SkColorType and SkAlphaType are valid, and SkColorType is not kUnknown_SkColorType;
188 row bytes are large enough to hold one row of pixels;
189 pixel address is not nullptr.
190
191 @param pixmap SkImageInfo, pixel address, and row bytes
192 @param rasterReleaseProc function called when pixels can be released; or nullptr
193 @param releaseContext state passed to rasterReleaseProc; or nullptr
194 @return SkImage sharing pixmap
195 */
196 SK_API sk_sp<SkImage> RasterFromPixmap(const SkPixmap& pixmap,
197 RasterReleaseProc rasterReleaseProc,
198 ReleaseContext releaseContext);
199
200 /** Creates CPU-backed SkImage from pixel data described by info.
201 The pixels data will *not* be copied.
202
203 SkImage is returned if SkImageInfo is valid. Valid SkImageInfo parameters include:
204 dimensions are greater than zero;
205 each dimension fits in 29 bits;
206 SkColorType and SkAlphaType are valid, and SkColorType is not kUnknown_SkColorType;
207 rowBytes are large enough to hold one row of pixels;
208 pixels is not nullptr, and contains enough data for SkImage.
209
210 @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace
211 @param pixels address or pixel storage
212 @param rowBytes size of pixel row or larger
213 @return SkImage sharing pixels, or nullptr
214 */
215 SK_API sk_sp<SkImage> RasterFromData(const SkImageInfo& info,
216 sk_sp<SkData> pixels,
217 size_t rowBytes);
218
219 /** Creates a filtered SkImage on the CPU. filter processes the src image, potentially changing
220 the color, position, and size. subset is the bounds of src that are processed
221 by filter. clipBounds is the expected bounds of the filtered SkImage. outSubset
222 is required storage for the actual bounds of the filtered SkImage. offset is
223 required storage for translation of returned SkImage.
224
225 Returns nullptr a filtered result could not be created. If nullptr is returned, outSubset
226 and offset are undefined.
227
228 Useful for animation of SkImageFilter that varies size from frame to frame.
229 outSubset describes the valid bounds of returned image. offset translates the returned SkImage
230 to keep subsequent animation frames aligned with respect to each other.
231
232 @param src the image to be filtered
233 @param filter the image filter to be applied
234 @param subset bounds of SkImage processed by filter
235 @param clipBounds expected bounds of filtered SkImage
236 @param outSubset storage for returned SkImage bounds
237 @param offset storage for returned SkImage translation
238 @return filtered SkImage, or nullptr
239 */
240 SK_API sk_sp<SkImage> MakeWithFilter(sk_sp<SkImage> src,
241 const SkImageFilter* filter,
242 const SkIRect& subset,
243 const SkIRect& clipBounds,
244 SkIRect* outSubset,
245 SkIPoint* offset);
246
247 } // namespace SkImages
248
249 /** \class SkImage
250 SkImage describes a two dimensional array of pixels to draw. The pixels may be
251 decoded in a raster bitmap, encoded in a SkPicture or compressed data stream,
252 or located in GPU memory as a GPU texture.
253
254 SkImage cannot be modified after it is created. SkImage may allocate additional
255 storage as needed; for instance, an encoded SkImage may decode when drawn.
256
257 SkImage width and height are greater than zero. Creating an SkImage with zero width
258 or height returns SkImage equal to nullptr.
259
260 SkImage may be created from SkBitmap, SkPixmap, SkSurface, SkPicture, encoded streams,
261 GPU texture, YUV_ColorSpace data, or hardware buffer. Encoded streams supported
262 include BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP. Supported encoding details
263 vary with platform.
264
265 See SkImages namespace for the static factory methods to make SkImages.
266
267 Clients should *not* subclass SkImage as there is a lot of internal machinery that is
268 not publicly accessible.
269 */
270 class SK_API SkImage : public SkRefCnt {
271 public:
272 /** Returns a SkImageInfo describing the width, height, color type, alpha type, and color space
273 of the SkImage.
274
275 @return image info of SkImage.
276 */
277 const SkImageInfo& imageInfo() const { return fInfo; }
278
279 /** Returns pixel count in each row.
280
281 @return pixel width in SkImage
282 */
283 int width() const { return fInfo.width(); }
284
285 /** Returns pixel row count.
286
287 @return pixel height in SkImage
288 */
289 int height() const { return fInfo.height(); }
290
291 /** Returns SkISize { width(), height() }.
292
293 @return integral size of width() and height()
294 */
295 SkISize dimensions() const { return SkISize::Make(fInfo.width(), fInfo.height()); }
296
297 /** Returns SkIRect { 0, 0, width(), height() }.
298
299 @return integral rectangle from origin to width() and height()
300 */
301 SkIRect bounds() const { return SkIRect::MakeWH(fInfo.width(), fInfo.height()); }
302
303 /** Returns value unique to image. SkImage contents cannot change after SkImage is
304 created. Any operation to create a new SkImage will receive generate a new
305 unique number.
306
307 @return unique identifier
308 */
309 uint32_t uniqueID() const { return fUniqueID; }
310
311 /** Returns SkAlphaType.
312
313 SkAlphaType returned was a parameter to an SkImage constructor,
314 or was parsed from encoded data.
315
316 @return SkAlphaType in SkImage
317
318 example: https://fiddle.skia.org/c/@Image_alphaType
319 */
320 SkAlphaType alphaType() const;
321
322 /** Returns SkColorType if known; otherwise, returns kUnknown_SkColorType.
323
324 @return SkColorType of SkImage
325
326 example: https://fiddle.skia.org/c/@Image_colorType
327 */
328 SkColorType colorType() const;
329
330 /** Returns SkColorSpace, the range of colors, associated with SkImage. The
331 reference count of SkColorSpace is unchanged. The returned SkColorSpace is
332 immutable.
333
334 SkColorSpace returned was passed to an SkImage constructor,
335 or was parsed from encoded data. SkColorSpace returned may be ignored when SkImage
336 is drawn, depending on the capabilities of the SkSurface receiving the drawing.
337
338 @return SkColorSpace in SkImage, or nullptr
339
340 example: https://fiddle.skia.org/c/@Image_colorSpace
341 */
342 SkColorSpace* colorSpace() const;
343
344 /** Returns a smart pointer to SkColorSpace, the range of colors, associated with
345 SkImage. The smart pointer tracks the number of objects sharing this
346 SkColorSpace reference so the memory is released when the owners destruct.
347
348 The returned SkColorSpace is immutable.
349
350 SkColorSpace returned was passed to an SkImage constructor,
351 or was parsed from encoded data. SkColorSpace returned may be ignored when SkImage
352 is drawn, depending on the capabilities of the SkSurface receiving the drawing.
353
354 @return SkColorSpace in SkImage, or nullptr, wrapped in a smart pointer
355
356 example: https://fiddle.skia.org/c/@Image_refColorSpace
357 */
358 sk_sp<SkColorSpace> refColorSpace() const;
359
360 /** Returns true if SkImage pixels represent transparency only. If true, each pixel
361 is packed in 8 bits as defined by kAlpha_8_SkColorType.
362
363 @return true if pixels represent a transparency mask
364
365 example: https://fiddle.skia.org/c/@Image_isAlphaOnly
366 */
367 bool isAlphaOnly() const;
368
369 /** Returns true if pixels ignore their alpha value and are treated as fully opaque.
370
371 @return true if SkAlphaType is kOpaque_SkAlphaType
372 */
373 bool isOpaque() const { return SkAlphaTypeIsOpaque(this->alphaType()); }
374
375 /**
376 * Make a shader with the specified tiling and mipmap sampling.
377 */
378 sk_sp<SkShader> makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions&,
379 const SkMatrix* localMatrix = nullptr) const;
380 sk_sp<SkShader> makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions& sampling,
381 const SkMatrix& lm) const;
382 /** Defaults to clamp in both X and Y. */
383 sk_sp<SkShader> makeShader(const SkSamplingOptions& sampling, const SkMatrix& lm) const;
384 sk_sp<SkShader> makeShader(const SkSamplingOptions& sampling,
385 const SkMatrix* lm = nullptr) const;
386
387 /**
388 * makeRawShader functions like makeShader, but for images that contain non-color data.
389 * This includes images encoding things like normals, material properties (eg, roughness),
390 * heightmaps, or any other purely mathematical data that happens to be stored in an image.
391 * These types of images are useful with some programmable shaders (see: SkRuntimeEffect).
392 *
393 * Raw image shaders work like regular image shaders (including filtering and tiling), with
394 * a few major differences:
395 * - No color space transformation is ever applied (the color space of the image is ignored).
396 * - Images with an alpha type of kUnpremul are *not* automatically premultiplied.
397 * - Bicubic filtering is not supported. If SkSamplingOptions::useCubic is true, these
398 * factories will return nullptr.
399 */
400 sk_sp<SkShader> makeRawShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions&,
401 const SkMatrix* localMatrix = nullptr) const;
402 sk_sp<SkShader> makeRawShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions& sampling,
403 const SkMatrix& lm) const;
404 /** Defaults to clamp in both X and Y. */
405 sk_sp<SkShader> makeRawShader(const SkSamplingOptions& sampling, const SkMatrix& lm) const;
406 sk_sp<SkShader> makeRawShader(const SkSamplingOptions& sampling,
407 const SkMatrix* lm = nullptr) const;
408
409 /** Copies SkImage pixel address, row bytes, and SkImageInfo to pixmap, if address
410 is available, and returns true. If pixel address is not available, return
411 false and leave pixmap unchanged.
412
413 @param pixmap storage for pixel state if pixels are readable; otherwise, ignored
414 @return true if SkImage has direct access to pixels
415
416 example: https://fiddle.skia.org/c/@Image_peekPixels
417 */
418 bool peekPixels(SkPixmap* pixmap) const;
419
420 /** Returns true if the contents of SkImage was created on or uploaded to GPU memory,
421 and is available as a GPU texture.
422
423 @return true if SkImage is a GPU texture
424
425 example: https://fiddle.skia.org/c/@Image_isTextureBacked
426 */
427 virtual bool isTextureBacked() const = 0;
428
429 /** Returns an approximation of the amount of texture memory used by the image. Returns
430 zero if the image is not texture backed or if the texture has an external format.
431 */
432 virtual size_t textureSize() const = 0;
433
434 /** Returns true if SkImage can be drawn on either raster surface or GPU surface.
435 If recorder is nullptr, tests if SkImage draws on raster surface;
436 otherwise, tests if SkImage draws on the associated GPU surface.
437
438 SkImage backed by GPU texture may become invalid if associated context is
439 invalid. lazy image may be invalid and may not draw to raster surface or
440 GPU surface or both.
441
442 @param context GPU context
443 @return true if SkImage can be drawn
444
445 example: https://fiddle.skia.org/c/@Image_isValid
446 */
447 virtual bool isValid(SkRecorder*) const = 0;
448
449 /** \enum SkImage::CachingHint
450 CachingHint selects whether Skia may internally cache SkBitmap generated by
451 decoding SkImage, or by copying SkImage from GPU to CPU. The default behavior
452 allows caching SkBitmap.
453
454 Choose kDisallow_CachingHint if SkImage pixels are to be used only once, or
455 if SkImage pixels reside in a cache outside of Skia, or to reduce memory pressure.
456
457 Choosing kAllow_CachingHint does not ensure that pixels will be cached.
458 SkImage pixels may not be cached if memory requirements are too large or
459 pixels are not accessible.
460 */
461 enum CachingHint {
462 kAllow_CachingHint, //!< allows internally caching decoded and copied pixels
463 kDisallow_CachingHint, //!< disallows internally caching decoded and copied pixels
464 };
465
466 /** Copies SkRect of pixels from SkImage to dstPixels. Copy starts at offset (srcX, srcY),
467 and does not exceed SkImage (width(), height()).
468
469 Graphite has deprecated this API in favor of the equivalent asynchronous API on
470 skgpu::graphite::Context (with an optional explicit synchonization).
471
472 dstInfo specifies width, height, SkColorType, SkAlphaType, and SkColorSpace of
473 destination. dstRowBytes specifies the gap from one destination row to the next.
474 Returns true if pixels are copied. Returns false if:
475 - dstInfo.addr() equals nullptr
476 - dstRowBytes is less than dstInfo.minRowBytes()
477 - SkPixelRef is nullptr
478
479 Pixels are copied only if pixel conversion is possible. If SkImage SkColorType is
480 kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match.
481 If SkImage SkColorType is kGray_8_SkColorType, dstInfo.colorSpace() must match.
482 If SkImage SkAlphaType is kOpaque_SkAlphaType, dstInfo.alphaType() must
483 match. If SkImage SkColorSpace is nullptr, dstInfo.colorSpace() must match. Returns
484 false if pixel conversion is not possible.
485
486 srcX and srcY may be negative to copy only top or left of source. Returns
487 false if width() or height() is zero or negative.
488 Returns false if abs(srcX) >= Image width(), or if abs(srcY) >= Image height().
489
490 If cachingHint is kAllow_CachingHint, pixels may be retained locally.
491 If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
492
493 @param context the GrDirectContext in play, if it exists
494 @param dstInfo destination width, height, SkColorType, SkAlphaType, SkColorSpace
495 @param dstPixels destination pixel storage
496 @param dstRowBytes destination row length
497 @param srcX column index whose absolute value is less than width()
498 @param srcY row index whose absolute value is less than height()
499 @param cachingHint whether the pixels should be cached locally
500 @return true if pixels are copied to dstPixels
501 */
502 bool readPixels(GrDirectContext* context,
503 const SkImageInfo& dstInfo,
504 void* dstPixels,
505 size_t dstRowBytes,
506 int srcX, int srcY,
507 CachingHint cachingHint = kAllow_CachingHint) const;
508
509 /** Copies a SkRect of pixels from SkImage to dst. Copy starts at (srcX, srcY), and
510 does not exceed SkImage (width(), height()).
511
512 Graphite has deprecated this API in favor of the equivalent asynchronous API on
513 skgpu::graphite::Context (with an optional explicit synchonization).
514
515 dst specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage,
516 and row bytes of destination. dst.rowBytes() specifics the gap from one destination
517 row to the next. Returns true if pixels are copied. Returns false if:
518 - dst pixel storage equals nullptr
519 - dst.rowBytes is less than SkImageInfo::minRowBytes
520 - SkPixelRef is nullptr
521
522 Pixels are copied only if pixel conversion is possible. If SkImage SkColorType is
523 kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType() must match.
524 If SkImage SkColorType is kGray_8_SkColorType, dst.colorSpace() must match.
525 If SkImage SkAlphaType is kOpaque_SkAlphaType, dst.alphaType() must
526 match. If SkImage SkColorSpace is nullptr, dst.colorSpace() must match. Returns
527 false if pixel conversion is not possible.
528
529 srcX and srcY may be negative to copy only top or left of source. Returns
530 false if width() or height() is zero or negative.
531 Returns false if abs(srcX) >= Image width(), or if abs(srcY) >= Image height().
532
533 If cachingHint is kAllow_CachingHint, pixels may be retained locally.
534 If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
535
536 @param context the GrDirectContext in play, if it exists
537 @param dst destination SkPixmap: SkImageInfo, pixels, row bytes
538 @param srcX column index whose absolute value is less than width()
539 @param srcY row index whose absolute value is less than height()
540 @param cachingHint whether the pixels should be cached locallyZ
541 @return true if pixels are copied to dst
542 */
543 bool readPixels(GrDirectContext* context,
544 const SkPixmap& dst,
545 int srcX,
546 int srcY,
547 CachingHint cachingHint = kAllow_CachingHint) const;
548
549 #ifndef SK_IMAGE_READ_PIXELS_DISABLE_LEGACY_API
550 /** Deprecated. Use the variants that accept a GrDirectContext. */
551 bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
552 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const;
553 bool readPixels(const SkPixmap& dst, int srcX, int srcY,
554 CachingHint cachingHint = kAllow_CachingHint) const;
555 #endif
556
557 /** The result from asyncRescaleAndReadPixels() or asyncRescaleAndReadPixelsYUV420(). */
558 class AsyncReadResult {
559 public:
560 AsyncReadResult(const AsyncReadResult&) = delete;
561 AsyncReadResult(AsyncReadResult&&) = delete;
562 AsyncReadResult& operator=(const AsyncReadResult&) = delete;
563 AsyncReadResult& operator=(AsyncReadResult&&) = delete;
564
565 virtual ~AsyncReadResult() = default;
566 /** Returns how many planes of data are in the result. e.g. 3 for YUV data. */
567 virtual int count() const = 0;
568 /** Returns the raw pixel data for a given plane.
569 *
570 * It will be organized as per the dst SkImageInfo passed in to the async read call.
571 *
572 * Clients may wish to create an SkPixmap with this data using the dst SkImageInfo
573 * and rowBytes(i).
574 */
575 virtual const void* data(int i) const = 0;
576 /** Returns how many bytes correspond to a single row of image data */
577 virtual size_t rowBytes(int i) const = 0;
578
579 protected:
580 AsyncReadResult() = default;
581 };
582
583 /** Client-provided context that is passed to client-provided ReadPixelsContext. */
584 using ReadPixelsContext = void*;
585
586 /** Client-provided callback to asyncRescaleAndReadPixels() or
587 asyncRescaleAndReadPixelsYUV420() that is called when read result is ready or on failure.
588 */
589 using ReadPixelsCallback = void(ReadPixelsContext, std::unique_ptr<const AsyncReadResult>);
590
591 enum class RescaleGamma : bool { kSrc, kLinear };
592
593 enum class RescaleMode {
594 kNearest,
595 kLinear,
596 kRepeatedLinear,
597 kRepeatedCubic,
598 };
599
600 /** Makes image pixel data available to caller, possibly asynchronously. It can also rescale
601 the image pixels.
602
603 Currently asynchronous reads are only supported in the Ganesh GPU backend and only when the
604 underlying 3D API supports transfer buffers and CPU/GPU synchronization primitives. In all
605 other cases this operates synchronously.
606
607 For the Graphite backend this API has been deprecated in favor of the equivalent API
608 on skgpu::graphite::Context.
609
610 Data is read from the source sub-rectangle, is optionally converted to a linear gamma, is
611 rescaled to the size indicated by 'info', is then converted to the color space, color type,
612 and alpha type of 'info'. A 'srcRect' that is not contained by the bounds of the image
613 causes failure.
614
615 When the pixel data is ready the caller's ReadPixelsCallback is called with a
616 AsyncReadResult containing pixel data in the requested color type, alpha type, and color
617 space. The AsyncReadResult will have count() == 1. Upon failure the callback is called with
618 nullptr for AsyncReadResult. For a GPU image this flushes work but a submit must occur to
619 guarantee a finite time before the callback is called.
620
621 The data is valid for the lifetime of AsyncReadResult with the exception that if the SkImage
622 is GPU-backed the data is immediately invalidated if the context is abandoned or
623 destroyed.
624
625 @param info info of the requested pixels
626 @param srcRect subrectangle of image to read
627 @param rescaleGamma controls whether rescaling is done in the image's gamma or whether
628 the source data is transformed to a linear gamma before rescaling.
629 @param rescaleMode controls the technique (and cost) of the rescaling
630 @param callback function to call with result of the read
631 @param context passed to callback
632 */
633 void asyncRescaleAndReadPixels(const SkImageInfo& info,
634 const SkIRect& srcRect,
635 RescaleGamma rescaleGamma,
636 RescaleMode rescaleMode,
637 ReadPixelsCallback callback,
638 ReadPixelsContext context) const;
639
640 /**
641 Similar to asyncRescaleAndReadPixels but performs an additional conversion to YUV. The
642 RGB->YUV conversion is controlled by 'yuvColorSpace'. The YUV data is returned as three
643 planes ordered y, u, v. The u and v planes are half the width and height of the resized
644 rectangle. The y, u, and v values are single bytes. Currently this fails if 'dstSize'
645 width and height are not even. A 'srcRect' that is not contained by the bounds of the
646 image causes failure.
647
648 When the pixel data is ready the caller's ReadPixelsCallback is called with a
649 AsyncReadResult containing the planar data. The AsyncReadResult will have count() == 3.
650 Upon failure the callback is called with nullptr for AsyncReadResult. For a GPU image this
651 flushes work but a submit must occur to guarantee a finite time before the callback is
652 called.
653
654 The data is valid for the lifetime of AsyncReadResult with the exception that if the SkImage
655 is GPU-backed the data is immediately invalidated if the context is abandoned or
656 destroyed.
657
658 @param yuvColorSpace The transformation from RGB to YUV. Applied to the resized image
659 after it is converted to dstColorSpace.
660 @param dstColorSpace The color space to convert the resized image to, after rescaling.
661 @param srcRect The portion of the image to rescale and convert to YUV planes.
662 @param dstSize The size to rescale srcRect to
663 @param rescaleGamma controls whether rescaling is done in the image's gamma or whether
664 the source data is transformed to a linear gamma before rescaling.
665 @param rescaleMode controls the technique (and cost) of the rescaling
666 @param callback function to call with the planar read result
667 @param context passed to callback
668 */
669 void asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
670 sk_sp<SkColorSpace> dstColorSpace,
671 const SkIRect& srcRect,
672 const SkISize& dstSize,
673 RescaleGamma rescaleGamma,
674 RescaleMode rescaleMode,
675 ReadPixelsCallback callback,
676 ReadPixelsContext context) const;
677
678 /**
679 * Identical to asyncRescaleAndReadPixelsYUV420 but a fourth plane is returned in the
680 * AsyncReadResult passed to 'callback'. The fourth plane contains the alpha chanel at the
681 * same full resolution as the Y plane.
682 */
683 void asyncRescaleAndReadPixelsYUVA420(SkYUVColorSpace yuvColorSpace,
684 sk_sp<SkColorSpace> dstColorSpace,
685 const SkIRect& srcRect,
686 const SkISize& dstSize,
687 RescaleGamma rescaleGamma,
688 RescaleMode rescaleMode,
689 ReadPixelsCallback callback,
690 ReadPixelsContext context) const;
691
692 /** Copies SkImage to dst, scaling pixels to fit dst.width() and dst.height(), and
693 converting pixels to match dst.colorType() and dst.alphaType(). Returns true if
694 pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes() is
695 less than dst SkImageInfo::minRowBytes.
696
697 Pixels are copied only if pixel conversion is possible. If SkImage SkColorType is
698 kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType() must match.
699 If SkImage SkColorType is kGray_8_SkColorType, dst.colorSpace() must match.
700 If SkImage SkAlphaType is kOpaque_SkAlphaType, dst.alphaType() must
701 match. If SkImage SkColorSpace is nullptr, dst.colorSpace() must match. Returns
702 false if pixel conversion is not possible.
703
704 If cachingHint is kAllow_CachingHint, pixels may be retained locally.
705 If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
706
707 @param dst destination SkPixmap: SkImageInfo, pixels, row bytes
708 @return true if pixels are scaled to fit dst
709 */
710 bool scalePixels(const SkPixmap& dst, const SkSamplingOptions&,
711 CachingHint cachingHint = kAllow_CachingHint) const;
712
713 /**
714 * Create a new image by copying this image and scaling to fit the ImageInfo's dimensions
715 * and converting the pixels into the ImageInfo's ColorInfo.
716 * This is done retaining the domain (backend) of the image (e.g. gpu, raster)
717 *
718 * The Recorder parameter is required if the original image was created on a graphite Recorder,
719 * but must be nullptr if it was create in some other way (e.g. GrContext, raster, deferred).
720 *
721 * return nullptr if the requested ColorInfo is not supported, its dimesions are out of range,
722 * or if the recorder is null on a graphite Image.
723 */
724 sk_sp<SkImage> makeScaled(SkRecorder*, const SkImageInfo&, const SkSamplingOptions&) const;
725 sk_sp<SkImage> makeScaled(SkRecorder*,
726 const SkImageInfo&,
727 const SkSamplingOptions&,
728 const SkSurfaceProps&) const;
729
730 sk_sp<SkImage> makeScaled(const SkImageInfo& info, const SkSamplingOptions& sampling) const;
731
732 /** Returns encoded SkImage pixels as SkData, if SkImage was created from supported
733 encoded stream format. Platform support for formats vary and may require building
734 with one or more of: SK_ENCODE_JPEG, SK_ENCODE_PNG, SK_ENCODE_WEBP.
735
736 Returns nullptr if SkImage contents are not encoded.
737
738 @return encoded SkImage, or nullptr
739
740 example: https://fiddle.skia.org/c/@Image_refEncodedData
741 */
742 sk_sp<const SkData> refEncodedData() const;
743
744 struct RequiredProperties {
745 bool fMipmapped = false;
746
747 bool operator==(const RequiredProperties& other) const {
748 return fMipmapped == other.fMipmapped;
749 }
750
751 bool operator!=(const RequiredProperties& other) const { return !(*this == other); }
752
753 bool operator<(const RequiredProperties& other) const {
754 return fMipmapped < other.fMipmapped;
755 }
756 };
757
758 /** Returns subset of this image.
759
760 Returns nullptr if any of the following are true:
761 - Subset is empty
762 - Subset is not contained inside the image's bounds
763 - Pixels in the image could not be read or copied
764 - This image is texture-backed and the provided context is null or does not match
765 the source image's context.
766
767 If the source image was texture-backed, the resulting image will be texture-backed also.
768 Otherwise, the returned image will be raster-backed.
769
770 @param recorder the recorder of the source image (nullptr is ok if the
771 source image was texture-backed).
772 @param subset bounds of returned SkImage
773 @param RequiredProperties properties the returned SkImage must possess (e.g. mipmaps)
774 @return the subsetted image, or nullptr
775 */
776 virtual sk_sp<SkImage> makeSubset(SkRecorder*,
777 const SkIRect& subset,
778 RequiredProperties) const = 0;
779
780 /**
781 * Returns true if the image has mipmap levels.
782 */
783 bool hasMipmaps() const;
784
785 /**
786 * Returns true if the image holds protected content.
787 */
788 bool isProtected() const;
789
790 /**
791 * Returns an image with the same "base" pixels as the this image, but with mipmap levels
792 * automatically generated and attached.
793 */
794 sk_sp<SkImage> withDefaultMipmaps() const;
795
796 /** Returns raster image or lazy image. Copies SkImage backed by GPU texture into
797 CPU memory if needed. Returns original SkImage if decoded in raster bitmap,
798 or if encoded in a stream.
799
800 Returns nullptr if backed by GPU texture and copy fails.
801
802 @return raster image, lazy image, or nullptr
803
804 example: https://fiddle.skia.org/c/@Image_makeNonTextureImage
805 */
806 sk_sp<SkImage> makeNonTextureImage(GrDirectContext* = nullptr) const;
807
808 /** Returns raster image. Copies SkImage backed by GPU texture into CPU memory,
809 or decodes SkImage from lazy image. Returns original SkImage if decoded in
810 raster bitmap.
811
812 Returns nullptr if copy, decode, or pixel read fails.
813
814 If cachingHint is kAllow_CachingHint, pixels may be retained locally.
815 If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
816
817 @return raster image, or nullptr
818
819 example: https://fiddle.skia.org/c/@Image_makeRasterImage
820 */
821 sk_sp<SkImage> makeRasterImage(GrDirectContext*,
822 CachingHint cachingHint = kDisallow_CachingHint) const;
823
824 #if !defined(SK_IMAGE_READ_PIXELS_DISABLE_LEGACY_API)
825 sk_sp<SkImage> makeRasterImage(CachingHint cachingHint = kDisallow_CachingHint) const {
826 return this->makeRasterImage(nullptr, cachingHint);
827 }
828 #endif
829
830 /** Deprecated.
831 */
832 enum LegacyBitmapMode {
833 kRO_LegacyBitmapMode, //!< returned bitmap is read-only and immutable
834 };
835
836 /** Deprecated.
837 Creates raster SkBitmap with same pixels as SkImage. If legacyBitmapMode is
838 kRO_LegacyBitmapMode, returned bitmap is read-only and immutable.
839 Returns true if SkBitmap is stored in bitmap. Returns false and resets bitmap if
840 SkBitmap write did not succeed.
841
842 @param bitmap storage for legacy SkBitmap
843 @param legacyBitmapMode bitmap is read-only and immutable
844 @return true if SkBitmap was created
845 */
846 bool asLegacyBitmap(SkBitmap* bitmap,
847 LegacyBitmapMode legacyBitmapMode = kRO_LegacyBitmapMode) const;
848
849 /** Returns true if SkImage is backed by an image-generator or other service that creates
850 and caches its pixels or texture on-demand.
851
852 @return true if SkImage is created as needed
853
854 example: https://fiddle.skia.org/c/@Image_isLazyGenerated_a
855 example: https://fiddle.skia.org/c/@Image_isLazyGenerated_b
856 */
857 virtual bool isLazyGenerated() const = 0;
858
859 /** Creates SkImage in target SkColorSpace.
860 Returns nullptr if SkImage could not be created.
861
862 Returns original SkImage if it is in target SkColorSpace.
863 Otherwise, converts pixels from SkImage SkColorSpace to target SkColorSpace.
864 If SkImage colorSpace() returns nullptr, SkImage SkColorSpace is assumed to be sRGB.
865
866 If this image is graphite-backed, the recorder parameter is required.
867
868 @param targetColorSpace SkColorSpace describing color range of returned SkImage
869 @param recorder The Recorder in which to create the new image
870 @param RequiredProperties properties the returned SkImage must possess (e.g. mipmaps)
871 @return created SkImage in target SkColorSpace
872 */
873 virtual sk_sp<SkImage> makeColorSpace(SkRecorder*,
874 sk_sp<SkColorSpace> targetColorSpace,
875 RequiredProperties) const = 0;
876
877 /** Experimental.
878 Creates SkImage in target SkColorType and SkColorSpace.
879 Returns nullptr if SkImage could not be created.
880
881 Returns original SkImage if it is in target SkColorType and SkColorSpace.
882
883 If this image is graphite-backed, the recorder parameter is required.
884
885 @param targetColorType SkColorType of returned SkImage
886 @param targetColorSpace SkColorSpace of returned SkImage
887 @param recorder The Recorder in which to create the new image
888 @param RequiredProperties properties the returned SkImage must possess (e.g. mipmaps)
889 @return created SkImage in target SkColorType and SkColorSpace
890 */
891 virtual sk_sp<SkImage> makeColorTypeAndColorSpace(SkRecorder*,
892 SkColorType targetColorType,
893 sk_sp<SkColorSpace> targetColorSpace,
894 RequiredProperties) const = 0;
895
896 /** Creates a new SkImage identical to this one, but with a different SkColorSpace.
897 This does not convert the underlying pixel data, so the resulting image will draw
898 differently.
899 */
900 sk_sp<SkImage> reinterpretColorSpace(sk_sp<SkColorSpace> newColorSpace) const;
901
902 private:
903 SkImage(const SkImageInfo& info, uint32_t uniqueID);
904
905 friend class SkBitmap;
906 friend class SkImage_Base; // for private ctor
907 friend class SkImage_Raster; // for withMipmaps
908 friend class SkMipmapBuilder;
909
910 SkImageInfo fInfo;
911 const uint32_t fUniqueID;
912
913 sk_sp<SkImage> withMipmaps(sk_sp<SkMipmap>) const;
914
915 using INHERITED = SkRefCnt;
916 };
917
918 #endif