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