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/SkFilterQuality.h" 12 #include "include/core/SkImageEncoder.h" 13 #include "include/core/SkImageInfo.h" 14 #include "include/core/SkRefCnt.h" 15 #include "include/core/SkScalar.h" 16 #include "include/core/SkShader.h" 17 #include "include/core/SkTileMode.h" 18 #include "include/gpu/GrTypes.h" 19 #include <functional> // std::function 20 21 #if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26 22 #include <android/hardware_buffer.h> 23 #endif 24 25 class SkData; 26 class SkCanvas; 27 class SkImageFilter; 28 class SkImageGenerator; 29 class SkPaint; 30 class SkPicture; 31 class SkSurface; 32 class GrBackendTexture; 33 class GrContext; 34 class GrContextThreadSafeProxy; 35 class GrTexture; 36 37 struct SkYUVAIndex; 38 39 /** \class SkImage 40 SkImage describes a two dimensional array of pixels to draw. The pixels may be 41 decoded in a raster bitmap, encoded in a SkPicture or compressed data stream, 42 or located in GPU memory as a GPU texture. 43 44 SkImage cannot be modified after it is created. SkImage may allocate additional 45 storage as needed; for instance, an encoded SkImage may decode when drawn. 46 47 SkImage width and height are greater than zero. Creating an SkImage with zero width 48 or height returns SkImage equal to nullptr. 49 50 SkImage may be created from SkBitmap, SkPixmap, SkSurface, SkPicture, encoded streams, 51 GPU texture, YUV_ColorSpace data, or hardware buffer. Encoded streams supported 52 include BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP. Supported encoding details 53 vary with platform. 54 */ 55 class SK_API SkImage : public SkRefCnt { 56 public: 57 58 /** Caller data passed to RasterReleaseProc; may be nullptr. 59 */ 60 typedef void* ReleaseContext; 61 62 /** Creates SkImage from SkPixmap and copy of pixels. Since pixels are copied, SkPixmap 63 pixels may be modified or deleted without affecting SkImage. 64 65 SkImage is returned if SkPixmap is valid. Valid SkPixmap parameters include: 66 dimensions are greater than zero; 67 each dimension fits in 29 bits; 68 SkColorType and SkAlphaType are valid, and SkColorType is not kUnknown_SkColorType; 69 row bytes are large enough to hold one row of pixels; 70 pixel address is not nullptr. 71 72 @param pixmap SkImageInfo, pixel address, and row bytes 73 @return copy of SkPixmap pixels, or nullptr 74 75 example: https://fiddle.skia.org/c/@Image_MakeRasterCopy 76 */ 77 static sk_sp<SkImage> MakeRasterCopy(const SkPixmap& pixmap); 78 79 /** Creates SkImage from SkImageInfo, sharing pixels. 80 81 SkImage is returned if SkImageInfo is valid. Valid SkImageInfo parameters include: 82 dimensions are greater than zero; 83 each dimension fits in 29 bits; 84 SkColorType and SkAlphaType are valid, and SkColorType is not kUnknown_SkColorType; 85 rowBytes are large enough to hold one row of pixels; 86 pixels is not nullptr, and contains enough data for SkImage. 87 88 @param info contains width, height, SkAlphaType, SkColorType, SkColorSpace 89 @param pixels address or pixel storage 90 @param rowBytes size of pixel row or larger 91 @return SkImage sharing pixels, or nullptr 92 */ 93 static sk_sp<SkImage> MakeRasterData(const SkImageInfo& info, sk_sp<SkData> pixels, 94 size_t rowBytes); 95 96 /** Function called when SkImage no longer shares pixels. ReleaseContext is 97 provided by caller when SkImage is created, and may be nullptr. 98 */ 99 typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext); 100 101 /** Creates SkImage from pixmap, sharing SkPixmap pixels. Pixels must remain valid and 102 unchanged until rasterReleaseProc is called. rasterReleaseProc is passed 103 releaseContext when SkImage is deleted or no longer refers to pixmap pixels. 104 105 Pass nullptr for rasterReleaseProc to share SkPixmap without requiring a callback 106 when SkImage is released. Pass nullptr for releaseContext if rasterReleaseProc 107 does not require state. 108 109 SkImage is returned if pixmap is valid. Valid SkPixmap parameters include: 110 dimensions are greater than zero; 111 each dimension fits in 29 bits; 112 SkColorType and SkAlphaType are valid, and SkColorType is not kUnknown_SkColorType; 113 row bytes are large enough to hold one row of pixels; 114 pixel address is not nullptr. 115 116 @param pixmap SkImageInfo, pixel address, and row bytes 117 @param rasterReleaseProc function called when pixels can be released; or nullptr 118 @param releaseContext state passed to rasterReleaseProc; or nullptr 119 @return SkImage sharing pixmap 120 */ 121 static sk_sp<SkImage> MakeFromRaster(const SkPixmap& pixmap, 122 RasterReleaseProc rasterReleaseProc, 123 ReleaseContext releaseContext); 124 125 /** Creates SkImage from bitmap, sharing or copying bitmap pixels. If the bitmap 126 is marked immutable, and its pixel memory is shareable, it may be shared 127 instead of copied. 128 129 SkImage is returned if bitmap is valid. Valid SkBitmap parameters include: 130 dimensions are greater than zero; 131 each dimension fits in 29 bits; 132 SkColorType and SkAlphaType are valid, and SkColorType is not kUnknown_SkColorType; 133 row bytes are large enough to hold one row of pixels; 134 pixel address is not nullptr. 135 136 @param bitmap SkImageInfo, row bytes, and pixels 137 @return created SkImage, or nullptr 138 139 example: https://fiddle.skia.org/c/@Image_MakeFromBitmap 140 */ 141 static sk_sp<SkImage> MakeFromBitmap(const SkBitmap& bitmap); 142 143 /** Creates SkImage from data returned by imageGenerator. Generated data is owned by SkImage and 144 may not be shared or accessed. 145 146 subset allows selecting a portion of the full image. Pass nullptr to select the entire 147 image; otherwise, subset must be contained by image bounds. 148 149 SkImage is returned if generator data is valid. Valid data parameters vary by type of data 150 and platform. 151 152 imageGenerator may wrap SkPicture data, codec data, or custom data. 153 154 @param imageGenerator stock or custom routines to retrieve SkImage 155 @param subset bounds of returned SkImage; may be nullptr 156 @return created SkImage, or nullptr 157 */ 158 static sk_sp<SkImage> MakeFromGenerator(std::unique_ptr<SkImageGenerator> imageGenerator, 159 const SkIRect* subset = nullptr); 160 161 /** 162 * Return an image backed by the encoded data, but attempt to defer decoding until the image 163 * is actually used/drawn. This deferral allows the system to cache the result, either on the 164 * CPU or on the GPU, depending on where the image is drawn. If memory is low, the cache may 165 * be purged, causing the next draw of the image to have to re-decode. 166 * 167 * The subset parameter specifies a area within the decoded image to create the image from. 168 * If subset is null, then the entire image is returned. 169 * 170 * This is similar to DecodeTo[Raster,Texture], but this method will attempt to defer the 171 * actual decode, while the DecodeTo... method explicitly decode and allocate the backend 172 * when the call is made. 173 * 174 * If the encoded format is not supported, or subset is outside of the bounds of the decoded 175 * image, nullptr is returned. 176 * 177 * @param encoded the encoded data 178 * @param length the number of bytes of encoded data 179 * @param subset the bounds of the pixels within the decoded image to return. may be null. 180 * @return created SkImage, or nullptr 181 182 example: https://fiddle.skia.org/c/@Image_MakeFromEncoded 183 */ 184 static sk_sp<SkImage> MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset = nullptr); 185 186 /** 187 * Decode the data in encoded/length into a raster image. 188 * 189 * The subset parameter specifies a area within the decoded image to create the image from. 190 * If subset is null, then the entire image is returned. 191 * 192 * This is similar to MakeFromEncoded, but this method will always decode immediately, and 193 * allocate the memory for the pixels for the lifetime of the returned image. 194 * 195 * If the encoded format is not supported, or subset is outside of the bounds of the decoded 196 * image, nullptr is returned. 197 * 198 * @param encoded the encoded data 199 * @param length the number of bytes of encoded data 200 * @param subset the bounds of the pixels within the decoded image to return. may be null. 201 * @return created SkImage, or nullptr 202 */ 203 static sk_sp<SkImage> DecodeToRaster(const void* encoded, size_t length, 204 const SkIRect* subset = nullptr); 205 static sk_sp<SkImage> DecodeToRaster(const sk_sp<SkData>& data, 206 const SkIRect* subset = nullptr) { 207 return DecodeToRaster(data->data(), data->size(), subset); 208 } 209 210 /** 211 * Decode the data in encoded/length into a texture-backed image. 212 * 213 * The subset parameter specifies a area within the decoded image to create the image from. 214 * If subset is null, then the entire image is returned. 215 * 216 * This is similar to MakeFromEncoded, but this method will always decode immediately, and 217 * allocate the texture for the pixels for the lifetime of the returned image. 218 * 219 * If the encoded format is not supported, or subset is outside of the bounds of the decoded 220 * image, nullptr is returned. 221 * 222 * @param encoded the encoded data 223 * @param length the number of bytes of encoded data 224 * @param subset the bounds of the pixels within the decoded image to return. may be null. 225 * @return created SkImage, or nullptr 226 */ 227 static sk_sp<SkImage> DecodeToTexture(GrContext* ctx, const void* encoded, size_t length, 228 const SkIRect* subset = nullptr); 229 static sk_sp<SkImage> DecodeToTexture(GrContext* ctx, const sk_sp<SkData>& data, 230 const SkIRect* subset = nullptr) { 231 return DecodeToTexture(ctx, data->data(), data->size(), subset); 232 } 233 234 /* 235 * Experimental: 236 * Skia | GL_COMPRESSED_* | MTLPixelFormat* | VK_FORMAT_*_BLOCK 237 * -------------------------------------------------------------------------------------- 238 * kETC2_RGB8_UNORM | ETC1_RGB8 | ETC2_RGB8 (iOS-only) | ETC2_R8G8B8_UNORM 239 * | RGB8_ETC2 | | 240 * -------------------------------------------------------------------------------------- 241 * kBC1_RGB8_UNORM | RGB_S3TC_DXT1_EXT | N/A | BC1_RGB_UNORM 242 * -------------------------------------------------------------------------------------- 243 * kBC1_RGBA8_UNORM | RGBA_S3TC_DXT1_EXT | BC1_RGBA (macOS-only)| BC1_RGBA_UNORM 244 */ 245 enum class CompressionType { 246 kNone, 247 kETC2_RGB8_UNORM, // the same as ETC1 248 249 kBC1_RGB8_UNORM, 250 kBC1_RGBA8_UNORM, 251 kLast = kBC1_RGBA8_UNORM, 252 }; 253 254 static constexpr int kCompressionTypeCount = static_cast<int>(CompressionType::kLast) + 1; 255 256 static const CompressionType kETC1_CompressionType = CompressionType::kETC2_RGB8_UNORM; 257 258 /** Creates a GPU-backed SkImage from compressed data. 259 260 This method will return an SkImage representing the compressed data. 261 If the GPU doesn't support the specified compression method, the data 262 will be decompressed and then wrapped in a GPU-backed image. 263 264 Note: one can query the supported compression formats via 265 GrContext::compressedBackendFormat. 266 267 @param context GPU context 268 @param data compressed data to store in SkImage 269 @param width width of full SkImage 270 @param height height of full SkImage 271 @param type type of compression used 272 @param mipMapped does 'data' contain data for all the mipmap levels? 273 @param isProtected do the contents of 'data' require DRM protection (on Vulkan)? 274 @return created SkImage, or nullptr 275 */ 276 static sk_sp<SkImage> MakeTextureFromCompressed(GrContext* context, 277 sk_sp<SkData> data, 278 int width, int height, 279 CompressionType type, 280 GrMipMapped mipMapped = GrMipMapped::kNo, 281 GrProtected isProtected = GrProtected::kNo); 282 /** To be deprecated. Use MakeTextureFromCompressed. 283 */ 284 static sk_sp<SkImage> MakeFromCompressed(GrContext* context, 285 sk_sp<SkData> data, 286 int width, int height, 287 CompressionType type, 288 GrMipMapped mipMapped = GrMipMapped::kNo, 289 GrProtected isProtected = GrProtected::kNo) { 290 return MakeTextureFromCompressed(context, data, width, height, type, 291 mipMapped, isProtected); 292 293 } 294 295 /** Creates a CPU-backed SkImage from compressed data. 296 297 This method will decompress the compressed data and create an image wrapping 298 it. Any mipmap levels present in the compressed data are discarded. 299 300 @param data compressed data to store in SkImage 301 @param width width of full SkImage 302 @param height height of full SkImage 303 @param type type of compression used 304 @return created SkImage, or nullptr 305 */ 306 static sk_sp<SkImage> MakeRasterFromCompressed(sk_sp<SkData> data, 307 int width, int height, 308 CompressionType type); 309 310 /** User function called when supplied texture may be deleted. 311 */ 312 typedef void (*TextureReleaseProc)(ReleaseContext releaseContext); 313 314 /** Creates SkImage from GPU texture associated with context. Caller is responsible for 315 managing the lifetime of GPU texture. 316 317 SkImage is returned if format of backendTexture is recognized and supported. 318 Recognized formats vary by GPU back-end. 319 320 @param context GPU context 321 @param backendTexture texture residing on GPU 322 @param colorSpace range of colors; may be nullptr 323 @return created SkImage, or nullptr 324 */ MakeFromTexture(GrContext * context,const GrBackendTexture & backendTexture,GrSurfaceOrigin origin,SkColorType colorType,SkAlphaType alphaType,sk_sp<SkColorSpace> colorSpace)325 static sk_sp<SkImage> MakeFromTexture(GrContext* context, 326 const GrBackendTexture& backendTexture, 327 GrSurfaceOrigin origin, 328 SkColorType colorType, 329 SkAlphaType alphaType, 330 sk_sp<SkColorSpace> colorSpace) { 331 return MakeFromTexture(context, backendTexture, origin, colorType, alphaType, colorSpace, 332 nullptr, nullptr); 333 } 334 335 /** Creates SkImage from GPU texture associated with context. GPU texture must stay 336 valid and unchanged until textureReleaseProc is called. textureReleaseProc is 337 passed releaseContext when SkImage is deleted or no longer refers to texture. 338 339 SkImage is returned if format of backendTexture is recognized and supported. 340 Recognized formats vary by GPU back-end. 341 342 @param context GPU context 343 @param backendTexture texture residing on GPU 344 @param colorSpace This describes the color space of this image's contents, as 345 seen after sampling. In general, if the format of the backend 346 texture is SRGB, some linear colorSpace should be supplied 347 (e.g., SkColorSpace::MakeSRGBLinear()). If the format of the 348 backend texture is linear, then the colorSpace should include 349 a description of the transfer function as 350 well (e.g., SkColorSpace::MakeSRGB()). 351 @param textureReleaseProc function called when texture can be released 352 @param releaseContext state passed to textureReleaseProc 353 @return created SkImage, or nullptr 354 */ 355 static sk_sp<SkImage> MakeFromTexture(GrContext* context, 356 const GrBackendTexture& backendTexture, 357 GrSurfaceOrigin origin, 358 SkColorType colorType, 359 SkAlphaType alphaType, 360 sk_sp<SkColorSpace> colorSpace, 361 TextureReleaseProc textureReleaseProc, 362 ReleaseContext releaseContext); 363 364 /** Creates an SkImage from a GPU backend texture. The backend texture must stay 365 valid and unchanged until textureReleaseProc is called. The textureReleaseProc is 366 called when the SkImage is deleted or no longer refers to the texture and will be 367 passed the releaseContext. 368 369 An SkImage is returned if the format of backendTexture is recognized and supported. 370 Recognized formats vary by GPU back-end. 371 372 @param context the GPU context 373 @param backendTexture a texture already allocated by the GPU 374 @param alphaType This characterizes the nature of the alpha values in the 375 backend texture. For opaque compressed formats (e.g., ETC1) 376 this should usually be set to kOpaque_SkAlphaType. 377 @param colorSpace This describes the color space of this image's contents, as 378 seen after sampling. In general, if the format of the backend 379 texture is SRGB, some linear colorSpace should be supplied 380 (e.g., SkColorSpace::MakeSRGBLinear()). If the format of the 381 backend texture is linear, then the colorSpace should include 382 a description of the transfer function as 383 well (e.g., SkColorSpace::MakeSRGB()). 384 @param textureReleaseProc function called when the backend texture can be released 385 @param releaseContext state passed to textureReleaseProc 386 @return created SkImage, or nullptr 387 */ 388 static sk_sp<SkImage> MakeFromCompressedTexture(GrContext* context, 389 const GrBackendTexture& backendTexture, 390 GrSurfaceOrigin origin, 391 SkAlphaType alphaType, 392 sk_sp<SkColorSpace> colorSpace, 393 TextureReleaseProc textureReleaseProc = nullptr, 394 ReleaseContext releaseContext = nullptr); 395 396 /** Creates SkImage from pixmap. SkImage is uploaded to GPU back-end using context. 397 398 Created SkImage is available to other GPU contexts, and is available across thread 399 boundaries. All contexts must be in the same GPU share group, or otherwise 400 share resources. 401 402 When SkImage is no longer referenced, context releases texture memory 403 asynchronously. 404 405 GrBackendTexture created from pixmap is uploaded to match SkSurface created with 406 dstColorSpace. SkColorSpace of SkImage is determined by pixmap.colorSpace(). 407 408 SkImage is returned referring to GPU back-end if context is not nullptr, 409 format of data is recognized and supported, and if context supports moving 410 resources between contexts. Otherwise, pixmap pixel data is copied and SkImage 411 as returned in raster format if possible; nullptr may be returned. 412 Recognized GPU formats vary by platform and GPU back-end. 413 414 @param context GPU context 415 @param pixmap SkImageInfo, pixel address, and row bytes 416 @param buildMips create SkImage as mip map if true 417 @param dstColorSpace range of colors of matching SkSurface on GPU 418 @param limitToMaxTextureSize downscale image to GPU maximum texture size, if necessary 419 @return created SkImage, or nullptr 420 */ 421 static sk_sp<SkImage> MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap, 422 bool buildMips, 423 bool limitToMaxTextureSize = false); 424 425 /** Creates SkImage from backendTexture associated with context. backendTexture and 426 returned SkImage are managed internally, and are released when no longer needed. 427 428 SkImage is returned if format of backendTexture is recognized and supported. 429 Recognized formats vary by GPU back-end. 430 431 @param context GPU context 432 @param backendTexture texture residing on GPU 433 @param colorSpace range of colors; may be nullptr 434 @return created SkImage, or nullptr 435 */ 436 static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context, 437 const GrBackendTexture& backendTexture, 438 GrSurfaceOrigin surfaceOrigin, 439 SkColorType colorType, 440 SkAlphaType alphaType = kPremul_SkAlphaType, 441 sk_sp<SkColorSpace> colorSpace = nullptr); 442 443 /** Creates an SkImage by flattening the specified YUVA planes into a single, interleaved RGBA 444 image. 445 446 @param context GPU context 447 @param yuvColorSpace How the YUV values are converted to RGB 448 @param yuvaTextures array of (up to four) YUVA textures on GPU which contain the, 449 possibly interleaved, YUVA planes 450 @param yuvaIndices array indicating which texture in yuvaTextures, and channel 451 in that texture, maps to each component of YUVA. 452 @param imageSize size of the resulting image 453 @param imageOrigin origin of the resulting image. 454 @param imageColorSpace range of colors of the resulting image; may be nullptr 455 @return created SkImage, or nullptr 456 */ 457 static sk_sp<SkImage> MakeFromYUVATexturesCopy(GrContext* context, 458 SkYUVColorSpace yuvColorSpace, 459 const GrBackendTexture yuvaTextures[], 460 const SkYUVAIndex yuvaIndices[4], 461 SkISize imageSize, 462 GrSurfaceOrigin imageOrigin, 463 sk_sp<SkColorSpace> imageColorSpace = nullptr); 464 465 /** Creates an SkImage by flattening the specified YUVA planes into a single, interleaved RGBA 466 image. 'backendTexture' is used to store the result of the flattening. 467 468 @param context GPU context 469 @param yuvColorSpace How the YUV values are converted to RGB 470 @param yuvaTextures array of (up to four) YUVA textures on GPU which contain the, 471 possibly interleaved, YUVA planes 472 @param yuvaIndices array indicating which texture in yuvaTextures, and channel 473 in that texture, maps to each component of YUVA. 474 @param imageSize size of the resulting image 475 @param imageOrigin origin of the resulting image. 476 @param backendTexture the resource that stores the final pixels 477 @param imageColorSpace range of colors of the resulting image; may be nullptr 478 @param textureReleaseProc function called when backendTexture can be released 479 @param releaseContext state passed to textureReleaseProc 480 @return created SkImage, or nullptr 481 */ 482 static sk_sp<SkImage> MakeFromYUVATexturesCopyWithExternalBackend( 483 GrContext* context, 484 SkYUVColorSpace yuvColorSpace, 485 const GrBackendTexture yuvaTextures[], 486 const SkYUVAIndex yuvaIndices[4], 487 SkISize imageSize, 488 GrSurfaceOrigin imageOrigin, 489 const GrBackendTexture& backendTexture, 490 sk_sp<SkColorSpace> imageColorSpace = nullptr, 491 TextureReleaseProc textureReleaseProc = nullptr, 492 ReleaseContext releaseContext = nullptr); 493 494 /** Creates an SkImage by storing the specified YUVA planes into an image, to be rendered 495 via multitexturing. 496 497 @param context GPU context 498 @param yuvColorSpace How the YUV values are converted to RGB 499 @param yuvaTextures array of (up to four) YUVA textures on GPU which contain the, 500 possibly interleaved, YUVA planes 501 @param yuvaIndices array indicating which texture in yuvaTextures, and channel 502 in that texture, maps to each component of YUVA. 503 @param imageSize size of the resulting image 504 @param imageOrigin origin of the resulting image. 505 @param imageColorSpace range of colors of the resulting image; may be nullptr 506 @return created SkImage, or nullptr 507 */ 508 static sk_sp<SkImage> MakeFromYUVATextures(GrContext* context, 509 SkYUVColorSpace yuvColorSpace, 510 const GrBackendTexture yuvaTextures[], 511 const SkYUVAIndex yuvaIndices[4], 512 SkISize imageSize, 513 GrSurfaceOrigin imageOrigin, 514 sk_sp<SkColorSpace> imageColorSpace = nullptr); 515 516 /** Creates SkImage from pixmap array representing YUVA data. 517 SkImage is uploaded to GPU back-end using context. 518 519 Each GrBackendTexture created from yuvaPixmaps array is uploaded to match SkSurface 520 using SkColorSpace of SkPixmap. SkColorSpace of SkImage is determined by imageColorSpace. 521 522 SkImage is returned referring to GPU back-end if context is not nullptr and 523 format of data is recognized and supported. Otherwise, nullptr is returned. 524 Recognized GPU formats vary by platform and GPU back-end. 525 526 @param context GPU context 527 @param yuvColorSpace How the YUV values are converted to RGB 528 @param yuvaPixmaps array of (up to four) SkPixmap which contain the, 529 possibly interleaved, YUVA planes 530 @param yuvaIndices array indicating which pixmap in yuvaPixmaps, and channel 531 in that pixmap, maps to each component of YUVA. 532 @param imageSize size of the resulting image 533 @param imageOrigin origin of the resulting image. 534 @param buildMips create internal YUVA textures as mip map if true 535 @param limitToMaxTextureSize downscale image to GPU maximum texture size, if necessary 536 @param imageColorSpace range of colors of the resulting image; may be nullptr 537 @return created SkImage, or nullptr 538 */ 539 static sk_sp<SkImage> MakeFromYUVAPixmaps( 540 GrContext* context, SkYUVColorSpace yuvColorSpace, const SkPixmap yuvaPixmaps[], 541 const SkYUVAIndex yuvaIndices[4], SkISize imageSize, GrSurfaceOrigin imageOrigin, 542 bool buildMips, bool limitToMaxTextureSize = false, 543 sk_sp<SkColorSpace> imageColorSpace = nullptr); 544 545 /** To be deprecated. 546 */ 547 static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace, 548 const GrBackendTexture yuvTextures[3], 549 GrSurfaceOrigin imageOrigin, 550 sk_sp<SkColorSpace> imageColorSpace = nullptr); 551 552 /** To be deprecated. 553 */ 554 static sk_sp<SkImage> MakeFromYUVTexturesCopyWithExternalBackend( 555 GrContext* context, 556 SkYUVColorSpace yuvColorSpace, 557 const GrBackendTexture yuvTextures[3], 558 GrSurfaceOrigin imageOrigin, 559 const GrBackendTexture& backendTexture, 560 sk_sp<SkColorSpace> imageColorSpace = nullptr); 561 562 /** Creates SkImage from copy of nv12Textures, an array of textures on GPU. 563 nv12Textures[0] contains pixels for YUV component y plane. 564 nv12Textures[1] contains pixels for YUV component u plane, 565 followed by pixels for YUV component v plane. 566 Returned SkImage has the dimensions nv12Textures[2]. 567 yuvColorSpace describes how YUV colors convert to RGB colors. 568 569 @param context GPU context 570 @param yuvColorSpace How the YUV values are converted to RGB 571 @param nv12Textures array of YUV textures on GPU 572 @param imageColorSpace range of colors; may be nullptr 573 @return created SkImage, or nullptr 574 */ 575 static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context, 576 SkYUVColorSpace yuvColorSpace, 577 const GrBackendTexture nv12Textures[2], 578 GrSurfaceOrigin imageOrigin, 579 sk_sp<SkColorSpace> imageColorSpace = nullptr); 580 581 /** Creates SkImage from copy of nv12Textures, an array of textures on GPU. 582 nv12Textures[0] contains pixels for YUV component y plane. 583 nv12Textures[1] contains pixels for YUV component u plane, 584 followed by pixels for YUV component v plane. 585 Returned SkImage has the dimensions nv12Textures[2] and stores pixels in backendTexture. 586 yuvColorSpace describes how YUV colors convert to RGB colors. 587 588 @param context GPU context 589 @param yuvColorSpace How the YUV values are converted to RGB 590 @param nv12Textures array of YUV textures on GPU 591 @param backendTexture the resource that stores the final pixels 592 @param imageColorSpace range of colors; may be nullptr 593 @param textureReleaseProc function called when backendTexture can be released 594 @param releaseContext state passed to textureReleaseProc 595 @return created SkImage, or nullptr 596 */ 597 static sk_sp<SkImage> MakeFromNV12TexturesCopyWithExternalBackend( 598 GrContext* context, 599 SkYUVColorSpace yuvColorSpace, 600 const GrBackendTexture nv12Textures[2], 601 GrSurfaceOrigin imageOrigin, 602 const GrBackendTexture& backendTexture, 603 sk_sp<SkColorSpace> imageColorSpace = nullptr, 604 TextureReleaseProc textureReleaseProc = nullptr, 605 ReleaseContext releaseContext = nullptr); 606 607 enum class BitDepth { 608 kU8, //!< uses 8-bit unsigned int per color component 609 kF16, //!< uses 16-bit float per color component 610 }; 611 612 /** Creates SkImage from picture. Returned SkImage width and height are set by dimensions. 613 SkImage draws picture with matrix and paint, set to bitDepth and colorSpace. 614 615 If matrix is nullptr, draws with identity SkMatrix. If paint is nullptr, draws 616 with default SkPaint. colorSpace may be nullptr. 617 618 @param picture stream of drawing commands 619 @param dimensions width and height 620 @param matrix SkMatrix to rotate, scale, translate, and so on; may be nullptr 621 @param paint SkPaint to apply transparency, filtering, and so on; may be nullptr 622 @param bitDepth 8-bit integer or 16-bit float: per component 623 @param colorSpace range of colors; may be nullptr 624 @return created SkImage, or nullptr 625 */ 626 static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions, 627 const SkMatrix* matrix, const SkPaint* paint, 628 BitDepth bitDepth, 629 sk_sp<SkColorSpace> colorSpace); 630 631 #if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26 632 /** (See Skia bug 7447) 633 Creates SkImage from Android hardware buffer. 634 Returned SkImage takes a reference on the buffer. 635 636 Only available on Android, when __ANDROID_API__ is defined to be 26 or greater. 637 638 @param hardwareBuffer AHardwareBuffer Android hardware buffer 639 @param colorSpace range of colors; may be nullptr 640 @return created SkImage, or nullptr 641 */ 642 static sk_sp<SkImage> MakeFromAHardwareBuffer( 643 AHardwareBuffer* hardwareBuffer, 644 SkAlphaType alphaType = kPremul_SkAlphaType, 645 sk_sp<SkColorSpace> colorSpace = nullptr, 646 GrSurfaceOrigin surfaceOrigin = kTopLeft_GrSurfaceOrigin); 647 648 /** Creates SkImage from Android hardware buffer and uploads the data from the SkPixmap to it. 649 Returned SkImage takes a reference on the buffer. 650 651 Only available on Android, when __ANDROID_API__ is defined to be 26 or greater. 652 653 @param pixmap SkPixmap that contains data to be uploaded to the AHardwareBuffer 654 @param hardwareBuffer AHardwareBuffer Android hardware buffer 655 @return created SkImage, or nullptr 656 */ 657 static sk_sp<SkImage> MakeFromAHardwareBufferWithData( 658 GrContext* context, 659 const SkPixmap& pixmap, 660 AHardwareBuffer* hardwareBuffer, 661 GrSurfaceOrigin surfaceOrigin = kTopLeft_GrSurfaceOrigin); 662 #endif 663 664 /** Returns a SkImageInfo describing the width, height, color type, alpha type, and color space 665 of the SkImage. 666 667 @return image info of SkImage. 668 */ imageInfo()669 const SkImageInfo& imageInfo() const { return fInfo; } 670 671 /** Returns pixel count in each row. 672 673 @return pixel width in SkImage 674 */ width()675 int width() const { return fInfo.width(); } 676 677 /** Returns pixel row count. 678 679 @return pixel height in SkImage 680 */ height()681 int height() const { return fInfo.height(); } 682 683 /** Returns SkISize { width(), height() }. 684 685 @return integral size of width() and height() 686 */ dimensions()687 SkISize dimensions() const { return SkISize::Make(fInfo.width(), fInfo.height()); } 688 689 /** Returns SkIRect { 0, 0, width(), height() }. 690 691 @return integral rectangle from origin to width() and height() 692 */ bounds()693 SkIRect bounds() const { return SkIRect::MakeWH(fInfo.width(), fInfo.height()); } 694 695 /** Returns value unique to image. SkImage contents cannot change after SkImage is 696 created. Any operation to create a new SkImage will receive generate a new 697 unique number. 698 699 @return unique identifier 700 */ uniqueID()701 uint32_t uniqueID() const { return fUniqueID; } 702 703 /** Returns SkAlphaType. 704 705 SkAlphaType returned was a parameter to an SkImage constructor, 706 or was parsed from encoded data. 707 708 @return SkAlphaType in SkImage 709 710 example: https://fiddle.skia.org/c/@Image_alphaType 711 */ 712 SkAlphaType alphaType() const; 713 714 /** Returns SkColorType if known; otherwise, returns kUnknown_SkColorType. 715 716 @return SkColorType of SkImage 717 718 example: https://fiddle.skia.org/c/@Image_colorType 719 */ 720 SkColorType colorType() const; 721 722 /** Returns SkColorSpace, the range of colors, associated with SkImage. The 723 reference count of SkColorSpace is unchanged. The returned SkColorSpace is 724 immutable. 725 726 SkColorSpace returned was passed to an SkImage constructor, 727 or was parsed from encoded data. SkColorSpace returned may be ignored when SkImage 728 is drawn, depending on the capabilities of the SkSurface receiving the drawing. 729 730 @return SkColorSpace in SkImage, or nullptr 731 732 example: https://fiddle.skia.org/c/@Image_colorSpace 733 */ 734 SkColorSpace* colorSpace() const; 735 736 /** Returns a smart pointer to SkColorSpace, the range of colors, associated with 737 SkImage. The smart pointer tracks the number of objects sharing this 738 SkColorSpace reference so the memory is released when the owners destruct. 739 740 The returned SkColorSpace is immutable. 741 742 SkColorSpace returned was passed to an SkImage constructor, 743 or was parsed from encoded data. SkColorSpace returned may be ignored when SkImage 744 is drawn, depending on the capabilities of the SkSurface receiving the drawing. 745 746 @return SkColorSpace in SkImage, or nullptr, wrapped in a smart pointer 747 748 example: https://fiddle.skia.org/c/@Image_refColorSpace 749 */ 750 sk_sp<SkColorSpace> refColorSpace() const; 751 752 /** Returns true if SkImage pixels represent transparency only. If true, each pixel 753 is packed in 8 bits as defined by kAlpha_8_SkColorType. 754 755 @return true if pixels represent a transparency mask 756 757 example: https://fiddle.skia.org/c/@Image_isAlphaOnly 758 */ 759 bool isAlphaOnly() const; 760 761 /** Returns true if pixels ignore their alpha value and are treated as fully opaque. 762 763 @return true if SkAlphaType is kOpaque_SkAlphaType 764 */ isOpaque()765 bool isOpaque() const { return SkAlphaTypeIsOpaque(this->alphaType()); } 766 767 /** Creates SkShader from SkImage. SkShader dimensions are taken from SkImage. SkShader uses 768 SkTileMode rules to fill drawn area outside SkImage. localMatrix permits 769 transforming SkImage before SkCanvas matrix is applied. 770 771 @param tmx tiling in the x direction 772 @param tmy tiling in the y direction 773 @param localMatrix SkImage transformation, or nullptr 774 @return SkShader containing SkImage 775 */ 776 sk_sp<SkShader> makeShader(SkTileMode tmx, SkTileMode tmy, 777 const SkMatrix* localMatrix = nullptr) const; makeShader(SkTileMode tmx,SkTileMode tmy,const SkMatrix & localMatrix)778 sk_sp<SkShader> makeShader(SkTileMode tmx, SkTileMode tmy, const SkMatrix& localMatrix) const { 779 return this->makeShader(tmx, tmy, &localMatrix); 780 } 781 782 /** Creates SkShader from SkImage. SkShader dimensions are taken from SkImage. SkShader uses 783 SkShader::kClamp_TileMode to fill drawn area outside SkImage. localMatrix permits 784 transforming SkImage before SkCanvas matrix is applied. 785 786 @param localMatrix SkImage transformation, or nullptr 787 @return SkShader containing SkImage 788 */ 789 sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const { 790 return this->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, localMatrix); 791 } makeShader(const SkMatrix & localMatrix)792 sk_sp<SkShader> makeShader(const SkMatrix& localMatrix) const { 793 return this->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, &localMatrix); 794 } 795 796 797 /** Copies SkImage pixel address, row bytes, and SkImageInfo to pixmap, if address 798 is available, and returns true. If pixel address is not available, return 799 false and leave pixmap unchanged. 800 801 @param pixmap storage for pixel state if pixels are readable; otherwise, ignored 802 @return true if SkImage has direct access to pixels 803 804 example: https://fiddle.skia.org/c/@Image_peekPixels 805 */ 806 bool peekPixels(SkPixmap* pixmap) const; 807 808 /** Deprecated. 809 */ 810 GrTexture* getTexture() const; 811 812 /** Returns true the contents of SkImage was created on or uploaded to GPU memory, 813 and is available as a GPU texture. 814 815 @return true if SkImage is a GPU texture 816 817 example: https://fiddle.skia.org/c/@Image_isTextureBacked 818 */ 819 bool isTextureBacked() const; 820 821 /** Returns true if SkImage can be drawn on either raster surface or GPU surface. 822 If context is nullptr, tests if SkImage draws on raster surface; 823 otherwise, tests if SkImage draws on GPU surface associated with context. 824 825 SkImage backed by GPU texture may become invalid if associated GrContext is 826 invalid. lazy image may be invalid and may not draw to raster surface or 827 GPU surface or both. 828 829 @param context GPU context 830 @return true if SkImage can be drawn 831 832 example: https://fiddle.skia.org/c/@Image_isValid 833 */ 834 bool isValid(GrContext* context) const; 835 836 /** Flushes any pending uses of texture-backed images in the GPU backend. If the image is not 837 texture-backed (including promise texture images) or if the the GrContext does not 838 have the same context ID as the context backing the image then this is a no-op. 839 840 If the image was not used in any non-culled draws recorded on the passed GrContext then 841 this is a no-op unless the GrFlushInfo contains semaphores, a finish proc, or uses 842 kSyncCpu_GrFlushFlag. Those are respected even when the image has not been used. 843 844 @param context the context on which to flush pending usages of the image. 845 @param info flush options 846 */ 847 GrSemaphoresSubmitted flush(GrContext* context, const GrFlushInfo& flushInfo); 848 849 /** Version of flush() that uses a default GrFlushInfo. */ 850 void flush(GrContext*); 851 852 /** Retrieves the back-end texture. If SkImage has no back-end texture, an invalid 853 object is returned. Call GrBackendTexture::isValid to determine if the result 854 is valid. 855 856 If flushPendingGrContextIO is true, completes deferred I/O operations. 857 858 If origin in not nullptr, copies location of content drawn into SkImage. 859 860 @param flushPendingGrContextIO flag to flush outstanding requests 861 @return back-end API texture handle; invalid on failure 862 */ 863 GrBackendTexture getBackendTexture(bool flushPendingGrContextIO, 864 GrSurfaceOrigin* origin = nullptr) const; 865 866 /** \enum SkImage::CachingHint 867 CachingHint selects whether Skia may internally cache SkBitmap generated by 868 decoding SkImage, or by copying SkImage from GPU to CPU. The default behavior 869 allows caching SkBitmap. 870 871 Choose kDisallow_CachingHint if SkImage pixels are to be used only once, or 872 if SkImage pixels reside in a cache outside of Skia, or to reduce memory pressure. 873 874 Choosing kAllow_CachingHint does not ensure that pixels will be cached. 875 SkImage pixels may not be cached if memory requirements are too large or 876 pixels are not accessible. 877 */ 878 enum CachingHint { 879 kAllow_CachingHint, //!< allows internally caching decoded and copied pixels 880 kDisallow_CachingHint, //!< disallows internally caching decoded and copied pixels 881 }; 882 883 /** Copies SkRect of pixels from SkImage to dstPixels. Copy starts at offset (srcX, srcY), 884 and does not exceed SkImage (width(), height()). 885 886 dstInfo specifies width, height, SkColorType, SkAlphaType, and SkColorSpace of 887 destination. dstRowBytes specifies the gap from one destination row to the next. 888 Returns true if pixels are copied. Returns false if: 889 - dstInfo.addr() equals nullptr 890 - dstRowBytes is less than dstInfo.minRowBytes() 891 - SkPixelRef is nullptr 892 893 Pixels are copied only if pixel conversion is possible. If SkImage SkColorType is 894 kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match. 895 If SkImage SkColorType is kGray_8_SkColorType, dstInfo.colorSpace() must match. 896 If SkImage SkAlphaType is kOpaque_SkAlphaType, dstInfo.alphaType() must 897 match. If SkImage SkColorSpace is nullptr, dstInfo.colorSpace() must match. Returns 898 false if pixel conversion is not possible. 899 900 srcX and srcY may be negative to copy only top or left of source. Returns 901 false if width() or height() is zero or negative. 902 Returns false if abs(srcX) >= Image width(), or if abs(srcY) >= Image height(). 903 904 If cachingHint is kAllow_CachingHint, pixels may be retained locally. 905 If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache. 906 907 @param dstInfo destination width, height, SkColorType, SkAlphaType, SkColorSpace 908 @param dstPixels destination pixel storage 909 @param dstRowBytes destination row length 910 @param srcX column index whose absolute value is less than width() 911 @param srcY row index whose absolute value is less than height() 912 @return true if pixels are copied to dstPixels 913 */ 914 bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, 915 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const; 916 917 /** Copies a SkRect of pixels from SkImage to dst. Copy starts at (srcX, srcY), and 918 does not exceed SkImage (width(), height()). 919 920 dst specifies width, height, SkColorType, SkAlphaType, SkColorSpace, pixel storage, 921 and row bytes of destination. dst.rowBytes() specifics the gap from one destination 922 row to the next. Returns true if pixels are copied. Returns false if: 923 - dst pixel storage equals nullptr 924 - dst.rowBytes is less than SkImageInfo::minRowBytes 925 - SkPixelRef is nullptr 926 927 Pixels are copied only if pixel conversion is possible. If SkImage SkColorType is 928 kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType() must match. 929 If SkImage SkColorType is kGray_8_SkColorType, dst.colorSpace() must match. 930 If SkImage SkAlphaType is kOpaque_SkAlphaType, dst.alphaType() must 931 match. If SkImage SkColorSpace is nullptr, dst.colorSpace() must match. Returns 932 false if pixel conversion is not possible. 933 934 srcX and srcY may be negative to copy only top or left of source. Returns 935 false if width() or height() is zero or negative. 936 Returns false if abs(srcX) >= Image width(), or if abs(srcY) >= Image height(). 937 938 If cachingHint is kAllow_CachingHint, pixels may be retained locally. 939 If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache. 940 941 @param dst destination SkPixmap: SkImageInfo, pixels, row bytes 942 @param srcX column index whose absolute value is less than width() 943 @param srcY row index whose absolute value is less than height() 944 @return true if pixels are copied to dst 945 */ 946 bool readPixels(const SkPixmap& dst, int srcX, int srcY, 947 CachingHint cachingHint = kAllow_CachingHint) const; 948 949 /** Copies SkImage to dst, scaling pixels to fit dst.width() and dst.height(), and 950 converting pixels to match dst.colorType() and dst.alphaType(). Returns true if 951 pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes() is 952 less than dst SkImageInfo::minRowBytes. 953 954 Pixels are copied only if pixel conversion is possible. If SkImage SkColorType is 955 kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType() must match. 956 If SkImage SkColorType is kGray_8_SkColorType, dst.colorSpace() must match. 957 If SkImage SkAlphaType is kOpaque_SkAlphaType, dst.alphaType() must 958 match. If SkImage SkColorSpace is nullptr, dst.colorSpace() must match. Returns 959 false if pixel conversion is not possible. 960 961 Scales the image, with filterQuality, to match dst.width() and dst.height(). 962 filterQuality kNone_SkFilterQuality is fastest, typically implemented with 963 nearest neighbor filter. kLow_SkFilterQuality is typically implemented with 964 bilerp filter. kMedium_SkFilterQuality is typically implemented with 965 bilerp filter, and mip-map filter when size is reduced. 966 kHigh_SkFilterQuality is slowest, typically implemented with bicubic filter. 967 968 If cachingHint is kAllow_CachingHint, pixels may be retained locally. 969 If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache. 970 971 @param dst destination SkPixmap: SkImageInfo, pixels, row bytes 972 @return true if pixels are scaled to fit dst 973 */ 974 bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality, 975 CachingHint cachingHint = kAllow_CachingHint) const; 976 977 /** Encodes SkImage pixels, returning result as SkData. 978 979 Returns nullptr if encoding fails, or if encodedImageFormat is not supported. 980 981 SkImage encoding in a format requires both building with one or more of: 982 SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support 983 for the encoded format. 984 985 If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can 986 additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP, 987 SkEncodedImageFormat::kGIF. 988 989 quality is a platform and format specific metric trading off size and encoding 990 error. When used, quality equaling 100 encodes with the least error. quality may 991 be ignored by the encoder. 992 993 @param encodedImageFormat one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG, 994 SkEncodedImageFormat::kWEBP 995 @param quality encoder specific metric with 100 equaling best 996 @return encoded SkImage, or nullptr 997 998 example: https://fiddle.skia.org/c/@Image_encodeToData 999 */ 1000 sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const; 1001 1002 /** Encodes SkImage pixels, returning result as SkData. Returns existing encoded data 1003 if present; otherwise, SkImage is encoded with SkEncodedImageFormat::kPNG. Skia 1004 must be built with SK_HAS_PNG_LIBRARY to encode SkImage. 1005 1006 Returns nullptr if existing encoded data is missing or invalid, and 1007 encoding fails. 1008 1009 @return encoded SkImage, or nullptr 1010 1011 example: https://fiddle.skia.org/c/@Image_encodeToData_2 1012 */ 1013 sk_sp<SkData> encodeToData() const; 1014 1015 /** Returns encoded SkImage pixels as SkData, if SkImage was created from supported 1016 encoded stream format. Platform support for formats vary and may require building 1017 with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY. 1018 1019 Returns nullptr if SkImage contents are not encoded. 1020 1021 @return encoded SkImage, or nullptr 1022 1023 example: https://fiddle.skia.org/c/@Image_refEncodedData 1024 */ 1025 sk_sp<SkData> refEncodedData() const; 1026 1027 /** Returns subset of SkImage. subset must be fully contained by SkImage dimensions(). 1028 The implementation may share pixels, or may copy them. 1029 1030 Returns nullptr if subset is empty, or subset is not contained by bounds, or 1031 pixels in SkImage could not be read or copied. 1032 1033 @param subset bounds of returned SkImage 1034 @return partial or full SkImage, or nullptr 1035 1036 example: https://fiddle.skia.org/c/@Image_makeSubset 1037 */ 1038 sk_sp<SkImage> makeSubset(const SkIRect& subset) const; 1039 1040 /** Returns SkImage backed by GPU texture associated with context. Returned SkImage is 1041 compatible with SkSurface created with dstColorSpace. The returned SkImage respects 1042 mipMapped setting; if mipMapped equals GrMipMapped::kYes, the backing texture 1043 allocates mip map levels. Returns original SkImage if context 1044 and dstColorSpace match and mipMapped is compatible with backing GPU texture. 1045 1046 Returns nullptr if context is nullptr, or if SkImage was created with another 1047 GrContext. 1048 1049 @param context GPU context 1050 @param dstColorSpace range of colors of matching SkSurface on GPU 1051 @param mipMapped whether created SkImage texture must allocate mip map levels 1052 @return created SkImage, or nullptr 1053 */ 1054 sk_sp<SkImage> makeTextureImage(GrContext* context, GrMipMapped = GrMipMapped::kNo) const; 1055 1056 /** Returns raster image or lazy image. Copies SkImage backed by GPU texture into 1057 CPU memory if needed. Returns original SkImage if decoded in raster bitmap, 1058 or if encoded in a stream. 1059 1060 Returns nullptr if backed by GPU texture and copy fails. 1061 1062 @return raster image, lazy image, or nullptr 1063 1064 example: https://fiddle.skia.org/c/@Image_makeNonTextureImage 1065 */ 1066 sk_sp<SkImage> makeNonTextureImage() const; 1067 1068 /** Returns raster image. Copies SkImage backed by GPU texture into CPU memory, 1069 or decodes SkImage from lazy image. Returns original SkImage if decoded in 1070 raster bitmap. 1071 1072 Returns nullptr if copy, decode, or pixel read fails. 1073 1074 If cachingHint is kAllow_CachingHint, pixels may be retained locally. 1075 If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache. 1076 1077 @return raster image, or nullptr 1078 1079 example: https://fiddle.skia.org/c/@Image_makeRasterImage 1080 */ 1081 sk_sp<SkImage> makeRasterImage(CachingHint cachingHint = kDisallow_CachingHint) const; 1082 1083 /** Creates filtered SkImage. filter processes original SkImage, potentially changing 1084 color, position, and size. subset is the bounds of original SkImage processed 1085 by filter. clipBounds is the expected bounds of the filtered SkImage. outSubset 1086 is required storage for the actual bounds of the filtered SkImage. offset is 1087 required storage for translation of returned SkImage. 1088 1089 Returns nullptr if SkImage could not be created. If nullptr is returned, outSubset 1090 and offset are undefined. 1091 1092 Useful for animation of SkImageFilter that varies size from frame to frame. 1093 Returned SkImage is created larger than required by filter so that GPU texture 1094 can be reused with different sized effects. outSubset describes the valid bounds 1095 of GPU texture returned. offset translates the returned SkImage to keep subsequent 1096 animation frames aligned with respect to each other. 1097 1098 @param context the GrContext in play - if it exists 1099 @param filter how SkImage is sampled when transformed 1100 @param subset bounds of SkImage processed by filter 1101 @param clipBounds expected bounds of filtered SkImage 1102 @param outSubset storage for returned SkImage bounds 1103 @param offset storage for returned SkImage translation 1104 @return filtered SkImage, or nullptr 1105 */ 1106 sk_sp<SkImage> makeWithFilter(GrContext* context, 1107 const SkImageFilter* filter, const SkIRect& subset, 1108 const SkIRect& clipBounds, SkIRect* outSubset, 1109 SkIPoint* offset) const; 1110 1111 /** To be deprecated. 1112 */ 1113 sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset, 1114 const SkIRect& clipBounds, SkIRect* outSubset, 1115 SkIPoint* offset) const; 1116 1117 /** Defines a callback function, taking one parameter of type GrBackendTexture with 1118 no return value. Function is called when back-end texture is to be released. 1119 */ 1120 typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc; 1121 1122 /** Creates a GrBackendTexture from the provided SkImage. Returns true and 1123 stores result in backendTexture and backendTextureReleaseProc if 1124 texture is created; otherwise, returns false and leaves 1125 backendTexture and backendTextureReleaseProc unmodified. 1126 1127 Call backendTextureReleaseProc after deleting backendTexture. 1128 backendTextureReleaseProc cleans up auxiliary data related to returned 1129 backendTexture. The caller must delete returned backendTexture after use. 1130 1131 If SkImage is both texture backed and singly referenced, image is returned in 1132 backendTexture without conversion or making a copy. SkImage is singly referenced 1133 if its was transferred solely using std::move(). 1134 1135 If SkImage is not texture backed, returns texture with SkImage contents. 1136 1137 @param context GPU context 1138 @param image SkImage used for texture 1139 @param backendTexture storage for back-end texture 1140 @param backendTextureReleaseProc storage for clean up function 1141 @return true if back-end texture was created 1142 */ 1143 static bool MakeBackendTextureFromSkImage(GrContext* context, 1144 sk_sp<SkImage> image, 1145 GrBackendTexture* backendTexture, 1146 BackendTextureReleaseProc* backendTextureReleaseProc); 1147 1148 /** Deprecated. 1149 */ 1150 enum LegacyBitmapMode { 1151 kRO_LegacyBitmapMode, //!< returned bitmap is read-only and immutable 1152 }; 1153 1154 /** Deprecated. 1155 Creates raster SkBitmap with same pixels as SkImage. If legacyBitmapMode is 1156 kRO_LegacyBitmapMode, returned bitmap is read-only and immutable. 1157 Returns true if SkBitmap is stored in bitmap. Returns false and resets bitmap if 1158 SkBitmap write did not succeed. 1159 1160 @param bitmap storage for legacy SkBitmap 1161 @param legacyBitmapMode bitmap is read-only and immutable 1162 @return true if SkBitmap was created 1163 */ 1164 bool asLegacyBitmap(SkBitmap* bitmap, 1165 LegacyBitmapMode legacyBitmapMode = kRO_LegacyBitmapMode) const; 1166 1167 /** Returns true if SkImage is backed by an image-generator or other service that creates 1168 and caches its pixels or texture on-demand. 1169 1170 @return true if SkImage is created as needed 1171 1172 example: https://fiddle.skia.org/c/@Image_isLazyGenerated_a 1173 example: https://fiddle.skia.org/c/@Image_isLazyGenerated_b 1174 */ 1175 bool isLazyGenerated() const; 1176 1177 /** Creates SkImage in target SkColorSpace. 1178 Returns nullptr if SkImage could not be created. 1179 1180 Returns original SkImage if it is in target SkColorSpace. 1181 Otherwise, converts pixels from SkImage SkColorSpace to target SkColorSpace. 1182 If SkImage colorSpace() returns nullptr, SkImage SkColorSpace is assumed to be sRGB. 1183 1184 @param target SkColorSpace describing color range of returned SkImage 1185 @return created SkImage in target SkColorSpace 1186 1187 example: https://fiddle.skia.org/c/@Image_makeColorSpace 1188 */ 1189 sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target) const; 1190 1191 /** Experimental. 1192 Creates SkImage in target SkColorType and SkColorSpace. 1193 Returns nullptr if SkImage could not be created. 1194 1195 Returns original SkImage if it is in target SkColorType and SkColorSpace. 1196 1197 @param targetColorType SkColorType of returned SkImage 1198 @param targetColorSpace SkColorSpace of returned SkImage 1199 @return created SkImage in target SkColorType and SkColorSpace 1200 */ 1201 sk_sp<SkImage> makeColorTypeAndColorSpace(SkColorType targetColorType, 1202 sk_sp<SkColorSpace> targetColorSpace) const; 1203 1204 /** Creates a new SkImage identical to this one, but with a different SkColorSpace. 1205 This does not convert the underlying pixel data, so the resulting image will draw 1206 differently. 1207 */ 1208 sk_sp<SkImage> reinterpretColorSpace(sk_sp<SkColorSpace> newColorSpace) const; 1209 1210 private: 1211 SkImage(const SkImageInfo& info, uint32_t uniqueID); 1212 friend class SkImage_Base; 1213 1214 SkImageInfo fInfo; 1215 const uint32_t fUniqueID; 1216 1217 typedef SkRefCnt INHERITED; 1218 }; 1219 1220 #endif 1221