Skia
2D Graphics Library
SkImage.h
Go to the documentation of this file.
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 
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 GrRecordingContext;
25 class SkBitmap;
26 class SkColorSpace;
27 class SkData;
28 class SkImage;
29 class SkImageFilter;
30 class SkImageGenerator;
31 class SkMatrix;
32 class SkMipmap;
33 class SkPaint;
34 class SkPicture;
35 class SkPixmap;
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 skgpu::graphite { class Recorder; }
46 
47 namespace SkImages {
48 
50 using ReleaseContext = void*;
54 using RasterReleaseProc = void(const void* pixels, ReleaseContext);
55 
71 
84  int width,
85  int height,
87 
107  std::optional<SkAlphaType> alphaType = std::nullopt);
108 
121 SK_API sk_sp<SkImage> DeferredFromGenerator(std::unique_ptr<SkImageGenerator> imageGenerator);
122 
123 enum class BitDepth {
124  kU8,
125  kF16,
126 };
127 
146  const SkISize& dimensions,
147  const SkMatrix* matrix,
148  const SkPaint* paint,
149  BitDepth bitDepth,
150  sk_sp<SkColorSpace> colorSpace,
151  SkSurfaceProps props);
153  const SkISize& dimensions,
154  const SkMatrix* matrix,
155  const SkPaint* paint,
156  BitDepth bitDepth,
157  sk_sp<SkColorSpace> colorSpace);
158 
175 
197  RasterReleaseProc rasterReleaseProc,
198  ReleaseContext releaseContext);
199 
216  sk_sp<SkData> pixels,
217  size_t rowBytes);
218 
241  const SkImageFilter* filter,
242  const SkIRect& subset,
243  const SkIRect& clipBounds,
244  SkIRect* outSubset,
245  SkIPoint* offset);
246 
247 } // namespace SkImages
248 
270 class SK_API SkImage : public SkRefCnt {
271 public:
277  const SkImageInfo& imageInfo() const { return fInfo; }
278 
283  int width() const { return fInfo.width(); }
284 
289  int height() const { return fInfo.height(); }
290 
295  SkISize dimensions() const { return SkISize::Make(fInfo.width(), fInfo.height()); }
296 
301  SkIRect bounds() const { return SkIRect::MakeWH(fInfo.width(), fInfo.height()); }
302 
309  uint32_t uniqueID() const { return fUniqueID; }
310 
321 
329 
343 
359 
367  bool isAlphaOnly() const;
368 
373  bool isOpaque() const { return SkAlphaTypeIsOpaque(this->alphaType()); }
374 
379  const SkMatrix* localMatrix = nullptr) const;
381  const SkMatrix& lm) const;
383  sk_sp<SkShader> makeShader(const SkSamplingOptions& sampling, const SkMatrix& lm) const;
385  const SkMatrix* lm = nullptr) const;
386 
401  const SkMatrix* localMatrix = nullptr) const;
403  const SkMatrix& lm) const;
405  sk_sp<SkShader> makeRawShader(const SkSamplingOptions& sampling, const SkMatrix& lm) const;
407  const SkMatrix* lm = nullptr) const;
408 
418  bool peekPixels(SkPixmap* pixmap) const;
419 
427  virtual bool isTextureBacked() const = 0;
428 
432  virtual size_t textureSize() const = 0;
433 
447  virtual bool isValid(GrRecordingContext* context) const = 0;
448 
461  enum CachingHint {
464  };
465 
500  const SkImageInfo& dstInfo,
501  void* dstPixels,
502  size_t dstRowBytes,
503  int srcX, int srcY,
504  CachingHint cachingHint = kAllow_CachingHint) const;
505 
538  const SkPixmap& dst,
539  int srcX,
540  int srcY,
541  CachingHint cachingHint = kAllow_CachingHint) const;
542 
543 #ifndef SK_IMAGE_READ_PIXELS_DISABLE_LEGACY_API
545  bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
546  int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const;
547  bool readPixels(const SkPixmap& dst, int srcX, int srcY,
548  CachingHint cachingHint = kAllow_CachingHint) const;
549 #endif
550 
553  public:
558 
559  virtual ~AsyncReadResult() = default;
560  virtual int count() const = 0;
561  virtual const void* data(int i) const = 0;
562  virtual size_t rowBytes(int i) const = 0;
563 
564  protected:
565  AsyncReadResult() = default;
566  };
567 
569  using ReadPixelsContext = void*;
570 
574  using ReadPixelsCallback = void(ReadPixelsContext, std::unique_ptr<const AsyncReadResult>);
575 
576  enum class RescaleGamma : bool { kSrc, kLinear };
577 
578  enum class RescaleMode {
579  kNearest,
580  kLinear,
581  kRepeatedLinear,
582  kRepeatedCubic,
583  };
584 
616  const SkIRect& srcRect,
617  RescaleGamma rescaleGamma,
618  RescaleMode rescaleMode,
619  ReadPixelsCallback callback,
620  ReadPixelsContext context) const;
621 
652  sk_sp<SkColorSpace> dstColorSpace,
653  const SkIRect& srcRect,
654  const SkISize& dstSize,
655  RescaleGamma rescaleGamma,
656  RescaleMode rescaleMode,
657  ReadPixelsCallback callback,
658  ReadPixelsContext context) const;
659 
666  sk_sp<SkColorSpace> dstColorSpace,
667  const SkIRect& srcRect,
668  const SkISize& dstSize,
669  RescaleGamma rescaleGamma,
670  RescaleMode rescaleMode,
671  ReadPixelsCallback callback,
672  ReadPixelsContext context) const;
673 
692  bool scalePixels(const SkPixmap& dst, const SkSamplingOptions&,
693  CachingHint cachingHint = kAllow_CachingHint) const;
694 
706 
726  virtual sk_sp<SkImage> makeSubset(GrDirectContext* direct, const SkIRect& subset) const = 0;
727 
730 
731  bool operator==(const RequiredProperties& other) const {
732  return fMipmapped == other.fMipmapped;
733  }
734 
735  bool operator!=(const RequiredProperties& other) const { return !(*this == other); }
736 
737  bool operator<(const RequiredProperties& other) const {
738  return fMipmapped < other.fMipmapped;
739  }
740  };
741 
760  virtual sk_sp<SkImage> makeSubset(skgpu::graphite::Recorder*,
761  const SkIRect& subset,
762  RequiredProperties) const = 0;
763 
767  bool hasMipmaps() const;
768 
772  bool isProtected() const;
773 
779 
791 
806  CachingHint cachingHint = kDisallow_CachingHint) const;
807 
808 #if !defined(SK_IMAGE_READ_PIXELS_DISABLE_LEGACY_API)
809  sk_sp<SkImage> makeRasterImage(CachingHint cachingHint = kDisallow_CachingHint) const {
810  return this->makeRasterImage(nullptr, cachingHint);
811  }
812 #endif
813 
818  };
819 
830  bool asLegacyBitmap(SkBitmap* bitmap,
831  LegacyBitmapMode legacyBitmapMode = kRO_LegacyBitmapMode) const;
832 
841  virtual bool isLazyGenerated() const = 0;
842 
860  sk_sp<SkColorSpace> target) const = 0;
861 
876  virtual sk_sp<SkImage> makeColorSpace(skgpu::graphite::Recorder*,
877  sk_sp<SkColorSpace> targetColorSpace,
878  RequiredProperties) const = 0;
879 
895  SkColorType targetColorType,
896  sk_sp<SkColorSpace> targetCS) const = 0;
897 
912  virtual sk_sp<SkImage> makeColorTypeAndColorSpace(skgpu::graphite::Recorder*,
913  SkColorType targetColorType,
914  sk_sp<SkColorSpace> targetColorSpace,
915  RequiredProperties) const = 0;
916 
922 
923 private:
924  SkImage(const SkImageInfo& info, uint32_t uniqueID);
925 
926  friend class SkBitmap;
927  friend class SkImage_Base; // for private ctor
928  friend class SkImage_Raster; // for withMipmaps
929  friend class SkMipmapBuilder;
930 
931  SkImageInfo fInfo;
932  const uint32_t fUniqueID;
933 
934  sk_sp<SkImage> withMipmaps(sk_sp<SkMipmap>) const;
935 
936  using INHERITED = SkRefCnt;
937 };
938 
939 #endif
static bool SkAlphaTypeIsOpaque(SkAlphaType at)
Returns true if SkAlphaType equals kOpaque_SkAlphaType.
Definition: SkAlphaType.h:41
SkAlphaType
Describes how to interpret the alpha component of a pixel.
Definition: SkAlphaType.h:26
SkColorType
Describes how pixel bits encode color.
Definition: SkColorType.h:19
SkYUVColorSpace
Definition: SkImageInfo.h:68
SkTextureCompressionType
Definition: SkTextureCompressionType.h:20
SkTileMode
Definition: SkTileMode.h:13
Definition: GrDirectContext.h:62
Definition: GrRecordingContext.h:42
SkBitmap describes a two-dimensional raster pixel array.
Definition: SkBitmap.h:59
Definition: SkColorSpace.h:107
SkData holds an immutable data buffer.
Definition: SkData.h:25
Base class for image filters.
Definition: SkImageFilter.h:35
Definition: SkImageGenerator.h:28
The result from asyncRescaleAndReadPixels() or asyncRescaleAndReadPixelsYUV420().
Definition: SkImage.h:552
AsyncReadResult(const AsyncReadResult &)=delete
AsyncReadResult & operator=(const AsyncReadResult &)=delete
virtual ~AsyncReadResult()=default
virtual size_t rowBytes(int i) const =0
virtual int count() const =0
AsyncReadResult(AsyncReadResult &&)=delete
virtual const void * data(int i) const =0
AsyncReadResult & operator=(AsyncReadResult &&)=delete
SkImage describes a two dimensional array of pixels to draw.
Definition: SkImage.h:270
bool readPixels(GrDirectContext *context, const SkPixmap &dst, int srcX, int srcY, CachingHint cachingHint=kAllow_CachingHint) const
Copies a SkRect of pixels from SkImage to dst.
bool isAlphaOnly() const
Returns true if SkImage pixels represent transparency only.
const SkImageInfo & imageInfo() const
Returns a SkImageInfo describing the width, height, color type, alpha type, and color space of the Sk...
Definition: SkImage.h:277
sk_sp< SkColorSpace > refColorSpace() const
Returns a smart pointer to SkColorSpace, the range of colors, associated with SkImage.
void asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace, sk_sp< SkColorSpace > dstColorSpace, const SkIRect &srcRect, const SkISize &dstSize, RescaleGamma rescaleGamma, RescaleMode rescaleMode, ReadPixelsCallback callback, ReadPixelsContext context) const
Similar to asyncRescaleAndReadPixels but performs an additional conversion to YUV.
void asyncRescaleAndReadPixelsYUVA420(SkYUVColorSpace yuvColorSpace, sk_sp< SkColorSpace > dstColorSpace, const SkIRect &srcRect, const SkISize &dstSize, RescaleGamma rescaleGamma, RescaleMode rescaleMode, ReadPixelsCallback callback, ReadPixelsContext context) const
Identical to asyncRescaleAndReadPixelsYUV420 but a fourth plane is returned in the AsyncReadResult pa...
sk_sp< SkImage > reinterpretColorSpace(sk_sp< SkColorSpace > newColorSpace) const
Creates a new SkImage identical to this one, but with a different SkColorSpace.
SkISize dimensions() const
Returns SkISize { width(), height() }.
Definition: SkImage.h:295
sk_sp< SkShader > makeShader(const SkSamplingOptions &sampling, const SkMatrix *lm=nullptr) const
virtual sk_sp< SkImage > makeColorSpace(GrDirectContext *direct, sk_sp< SkColorSpace > target) const =0
Creates SkImage in target SkColorSpace.
bool readPixels(GrDirectContext *context, const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY, CachingHint cachingHint=kAllow_CachingHint) const
Copies SkRect of pixels from SkImage to dstPixels.
uint32_t uniqueID() const
Returns value unique to image.
Definition: SkImage.h:309
void * ReadPixelsContext
Client-provided context that is passed to client-provided ReadPixelsContext.
Definition: SkImage.h:569
SkAlphaType alphaType() const
Returns SkAlphaType.
virtual bool isLazyGenerated() const =0
Returns true if SkImage is backed by an image-generator or other service that creates and caches its ...
bool readPixels(const SkPixmap &dst, int srcX, int srcY, CachingHint cachingHint=kAllow_CachingHint) const
RescaleMode
Definition: SkImage.h:578
virtual sk_sp< SkImage > makeSubset(GrDirectContext *direct, const SkIRect &subset) const =0
Returns subset of this image.
sk_sp< SkImage > makeNonTextureImage(GrDirectContext *=nullptr) const
Returns raster image or lazy image.
sk_sp< SkShader > makeRawShader(const SkSamplingOptions &sampling, const SkMatrix &lm) const
Defaults to clamp in both X and Y.
bool peekPixels(SkPixmap *pixmap) const
Copies SkImage pixel address, row bytes, and SkImageInfo to pixmap, if address is available,...
int width() const
Returns pixel count in each row.
Definition: SkImage.h:283
RescaleGamma
Definition: SkImage.h:576
virtual sk_sp< SkImage > makeColorTypeAndColorSpace(skgpu::graphite::Recorder *, SkColorType targetColorType, sk_sp< SkColorSpace > targetColorSpace, RequiredProperties) const =0
Experimental.
sk_sp< SkImage > withDefaultMipmaps() const
Returns an image with the same "base" pixels as the this image, but with mipmap levels automatically ...
sk_sp< SkShader > makeRawShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &sampling, const SkMatrix &lm) const
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Make a shader with the specified tiling and mipmap sampling.
sk_sp< SkShader > makeRawShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
makeRawShader functions like makeShader, but for images that contain non-color data.
sk_sp< SkImage > makeRasterImage(CachingHint cachingHint=kDisallow_CachingHint) const
Definition: SkImage.h:809
virtual sk_sp< SkImage > makeSubset(skgpu::graphite::Recorder *, const SkIRect &subset, RequiredProperties) const =0
Returns subset of this image.
virtual sk_sp< SkImage > makeColorSpace(skgpu::graphite::Recorder *, sk_sp< SkColorSpace > targetColorSpace, RequiredProperties) const =0
Creates SkImage in target SkColorSpace.
SkColorType colorType() const
Returns SkColorType if known; otherwise, returns kUnknown_SkColorType.
virtual bool isValid(GrRecordingContext *context) const =0
Returns true if SkImage can be drawn on either raster surface or GPU surface.
void asyncRescaleAndReadPixels(const SkImageInfo &info, const SkIRect &srcRect, RescaleGamma rescaleGamma, RescaleMode rescaleMode, ReadPixelsCallback callback, ReadPixelsContext context) const
Makes image pixel data available to caller, possibly asynchronously.
SkColorSpace * colorSpace() const
Returns SkColorSpace, the range of colors, associated with SkImage.
bool isOpaque() const
Returns true if pixels ignore their alpha value and are treated as fully opaque.
Definition: SkImage.h:373
virtual bool isTextureBacked() const =0
Returns true if the contents of SkImage was created on or uploaded to GPU memory, and is available as...
virtual size_t textureSize() const =0
Returns an approximation of the amount of texture memory used by the image.
int height() const
Returns pixel row count.
Definition: SkImage.h:289
SkIRect bounds() const
Returns SkIRect { 0, 0, width(), height() }.
Definition: SkImage.h:301
virtual sk_sp< SkImage > makeColorTypeAndColorSpace(GrDirectContext *direct, SkColorType targetColorType, sk_sp< SkColorSpace > targetCS) const =0
Experimental.
sk_sp< SkData > refEncodedData() const
Returns encoded SkImage pixels as SkData, if SkImage was created from supported encoded stream format...
CachingHint
Definition: SkImage.h:461
@ kDisallow_CachingHint
disallows internally caching decoded and copied pixels
Definition: SkImage.h:463
@ kAllow_CachingHint
allows internally caching decoded and copied pixels
Definition: SkImage.h:462
bool isProtected() const
Returns true if the image holds protected content.
sk_sp< SkImage > makeRasterImage(GrDirectContext *, CachingHint cachingHint=kDisallow_CachingHint) const
Returns raster image.
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &sampling, const SkMatrix &lm) const
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY, CachingHint cachingHint=kAllow_CachingHint) const
Deprecated.
bool hasMipmaps() const
Returns true if the image has mipmap levels.
sk_sp< SkShader > makeRawShader(const SkSamplingOptions &sampling, const SkMatrix *lm=nullptr) const
void(ReadPixelsContext, std::unique_ptr< const AsyncReadResult >) ReadPixelsCallback
Client-provided callback to asyncRescaleAndReadPixels() or asyncRescaleAndReadPixelsYUV420() that is ...
Definition: SkImage.h:574
bool scalePixels(const SkPixmap &dst, const SkSamplingOptions &, CachingHint cachingHint=kAllow_CachingHint) const
Copies SkImage to dst, scaling pixels to fit dst.width() and dst.height(), and converting pixels to m...
bool asLegacyBitmap(SkBitmap *bitmap, LegacyBitmapMode legacyBitmapMode=kRO_LegacyBitmapMode) const
Deprecated.
sk_sp< SkShader > makeShader(const SkSamplingOptions &sampling, const SkMatrix &lm) const
Defaults to clamp in both X and Y.
LegacyBitmapMode
Deprecated.
Definition: SkImage.h:816
@ kRO_LegacyBitmapMode
returned bitmap is read-only and immutable
Definition: SkImage.h:817
SkMatrix holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:53
SkPaint controls options applied when drawing.
Definition: SkPaint.h:44
SkPicture records drawing commands made to SkCanvas.
Definition: SkPicture.h:44
SkPixmap provides a utility to pair SkImageInfo with pixels and row bytes.
Definition: SkPixmap.h:40
Definition: SkRefCnt.h:119
Shaders specify the source color(s) for what is being drawn.
Definition: SkShader.h:35
Describes properties and constraints of a given SkSurface.
Definition: SkSurfaceProps.h:52
Definition: SkImage.h:47
SK_API sk_sp< SkImage > RasterFromData(const SkImageInfo &info, sk_sp< SkData > pixels, size_t rowBytes)
Creates CPU-backed SkImage from pixel data described by info.
SK_API sk_sp< SkImage > RasterFromCompressedTextureData(sk_sp< SkData > data, int width, int height, SkTextureCompressionType type)
Creates a CPU-backed SkImage from compressed data.
SK_API sk_sp< SkImage > DeferredFromPicture(sk_sp< SkPicture > picture, const SkISize &dimensions, const SkMatrix *matrix, const SkPaint *paint, BitDepth bitDepth, sk_sp< SkColorSpace > colorSpace, SkSurfaceProps props)
Creates SkImage from picture.
void * ReleaseContext
Caller data passed to RasterReleaseProc; may be nullptr.
Definition: SkImage.h:50
BitDepth
Definition: SkImage.h:123
@ kF16
uses 16-bit float per color component
@ kU8
uses 8-bit unsigned int per color component
SK_API sk_sp< SkImage > RasterFromPixmap(const SkPixmap &pixmap, RasterReleaseProc rasterReleaseProc, ReleaseContext releaseContext)
Creates CPU-backed SkImage from pixmap, sharing SkPixmap pixels.
SK_API sk_sp< SkImage > MakeWithFilter(sk_sp< SkImage > src, const SkImageFilter *filter, const SkIRect &subset, const SkIRect &clipBounds, SkIRect *outSubset, SkIPoint *offset)
Creates a filtered SkImage on the CPU.
void(const void *pixels, ReleaseContext) RasterReleaseProc
Function called when SkImage no longer shares pixels.
Definition: SkImage.h:54
SK_API sk_sp< SkImage > DeferredFromEncodedData(sk_sp< SkData > encoded, std::optional< SkAlphaType > alphaType=std::nullopt)
Return a SkImage using the encoded data, but attempts to defer decoding until the image is actually u...
SK_API sk_sp< SkImage > RasterFromPixmapCopy(const SkPixmap &pixmap)
Creates a CPU-backed SkImage from pixmap, copying the pixel data.
SK_API sk_sp< SkImage > RasterFromBitmap(const SkBitmap &bitmap)
Creates a CPU-backed SkImage from bitmap, sharing or copying bitmap pixels.
SK_API sk_sp< SkImage > DeferredFromGenerator(std::unique_ptr< SkImageGenerator > imageGenerator)
Creates SkImage from data returned by imageGenerator.
Definition: SkCanvas.h:73
SkIRect holds four 32-bit integer coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:32
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Returns constructed SkIRect set to (0, 0, w, h).
Definition: SkRect.h:56
Definition: SkSize.h:15
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:19
Describes pixel dimensions and encoding.
Definition: SkImageInfo.h:194
Definition: SkImage.h:728
bool operator!=(const RequiredProperties &other) const
Definition: SkImage.h:735
bool operator==(const RequiredProperties &other) const
Definition: SkImage.h:731
bool fMipmapped
Definition: SkImage.h:729
bool operator<(const RequiredProperties &other) const
Definition: SkImage.h:737
Definition: SkSamplingOptions.h:58