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