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