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 "SkFilterQuality.h" 12 #include "SkImageInfo.h" 13 #include "SkImageEncoder.h" 14 #include "SkRefCnt.h" 15 #include "SkScalar.h" 16 #include "SkShader.h" 17 18 #if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26 19 #include <android/hardware_buffer.h> 20 #endif 21 22 class SkData; 23 class SkCanvas; 24 class SkImageFilter; 25 class SkImageGenerator; 26 class SkPaint; 27 class SkPicture; 28 class SkString; 29 class SkSurface; 30 class GrBackendTexture; 31 class GrContext; 32 class GrContextThreadSafeProxy; 33 class GrTexture; 34 35 /** 36 * SkImage is an abstraction for drawing a rectagle of pixels, though the 37 * particular type of image could be actually storing its data on the GPU, or 38 * as drawing commands (picture or PDF or otherwise), ready to be played back 39 * into another canvas. 40 * 41 * The content of SkImage is always immutable, though the actual storage may 42 * change, if for example that image can be re-created via encoded data or 43 * other means. 44 * 45 * SkImage always has a non-zero dimensions. If there is a request to create a new image, either 46 * directly or via SkSurface, and either of the requested dimensions are zero, then NULL will be 47 * returned. 48 */ 49 class SK_API SkImage : public SkRefCnt { 50 public: 51 typedef SkImageInfo Info; 52 typedef void* ReleaseContext; 53 54 static sk_sp<SkImage> MakeRasterCopy(const SkPixmap& pixmap); 55 static sk_sp<SkImage> MakeRasterData(const Info& info, sk_sp<SkData> pixels, size_t rowBytes); 56 57 typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext); 58 59 /** 60 * Return a new Image referencing the specified pixels. These must remain valid and unchanged 61 * until the specified release-proc is called, indicating that Skia no longer has a reference 62 * to the pixels. 63 * 64 * Returns NULL if the requested pixmap info is unsupported. 65 */ 66 static sk_sp<SkImage> MakeFromRaster(const SkPixmap& pixmap, 67 RasterReleaseProc rasterReleaseProc, 68 ReleaseContext releaseContext); 69 70 /** 71 * Construct a new image from the specified bitmap. If the bitmap is marked immutable, and 72 * its pixel memory is shareable, it may be shared instead of copied. 73 */ 74 static sk_sp<SkImage> MakeFromBitmap(const SkBitmap& bitmap); 75 76 /** 77 * Construct a new SkImage based on the given ImageGenerator. Returns NULL on error. 78 * This function will always take ownership of the passed generator. 79 * 80 * If a subset is specified, it must be contained within the generator's bounds. 81 */ 82 static sk_sp<SkImage> MakeFromGenerator(std::unique_ptr<SkImageGenerator> imageGenerator, 83 const SkIRect* subset = nullptr); 84 85 /** 86 * Construct a new SkImage based on the specified encoded data. Returns NULL on failure, 87 * which can mean that the format of the encoded data was not recognized/supported. 88 * 89 * If a subset is specified, it must be contained within the encoded data's bounds. 90 */ 91 static sk_sp<SkImage> MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset = nullptr); 92 93 typedef void (*TextureReleaseProc)(ReleaseContext releaseContext); 94 95 /** 96 * Create a new image from the specified descriptor. Note - the caller is responsible for 97 * managing the lifetime of the underlying platform texture. 98 * 99 * Will return NULL if the specified backend texture is unsupported. 100 * 101 * DEPRECATED: This factory is deprecated and clients should use the factory below which takes 102 * an SkColorType. 103 */ 104 static sk_sp<SkImage> MakeFromTexture(GrContext* context, 105 const GrBackendTexture& backendTexture, 106 GrSurfaceOrigin origin, 107 SkAlphaType alphaType, 108 sk_sp<SkColorSpace> colorSpace) { 109 return MakeFromTexture(context, backendTexture, origin, alphaType, colorSpace, nullptr, 110 nullptr); 111 } 112 113 /** 114 * Create a new image from the GrBackendTexture. The underlying platform texture must stay 115 * valid and unaltered until the specified release-proc is invoked, indicating that Skia 116 * no longer is holding a reference to it. 117 * 118 * Will return NULL if the specified backend texture is unsupported. 119 * 120 * DEPRECATED: This factory is deprecated and clients should use the factory below which takes 121 * an SkColorType. 122 */ 123 static sk_sp<SkImage> MakeFromTexture(GrContext* context, 124 const GrBackendTexture& backendTexture, 125 GrSurfaceOrigin origin, 126 SkAlphaType alphaType, 127 sk_sp<SkColorSpace> colorSpace, 128 TextureReleaseProc textureReleaseProc, 129 ReleaseContext releaseContext); 130 131 /** 132 * Create a new image from the specified descriptor. Note - the caller is responsible for 133 * managing the lifetime of the underlying platform texture. 134 * 135 * The GrBackendTexture must have a valid backend format supplied (GrGLTextureInfo::fFormat, 136 * GrVkImageInfo::fFormat, etc.) in it. The passed in SkColorType informs skia how it should 137 * interpret the backend format supplied by the GrBackendTexture. If the format in the 138 * GrBackendTexture is not compitable with the SkColorType, SkAlphaType, and SkColorSpace we 139 * will return nullptr. 140 */ 141 static sk_sp<SkImage> MakeFromTexture(GrContext* context, 142 const GrBackendTexture& backendTexture, 143 GrSurfaceOrigin origin, 144 SkColorType colorType, 145 SkAlphaType alphaType, 146 sk_sp<SkColorSpace> colorSpace) { 147 return MakeFromTexture(context, backendTexture, origin, colorType, alphaType, colorSpace, 148 nullptr, nullptr); 149 } 150 151 /** 152 * Create a new image from the GrBackendTexture. The underlying platform texture must stay 153 * valid and unaltered until the specified release-proc is invoked, indicating that Skia 154 * no longer is holding a reference to it. 155 * 156 * The GrBackendTexture must have a valid backend format supplied (GrGLTextureInfo::fFormat, 157 * GrVkImageInfo::fFormat, etc.) in it. The passed in SkColorType informs skia how it should 158 * interpret the backend format supplied by the GrBackendTexture. If the format in the 159 * GrBackendTexture is not compitable with the SkColorType, SkAlphaType, and SkColorSpace we 160 * will return nullptr. 161 */ 162 static sk_sp<SkImage> MakeFromTexture(GrContext* context, 163 const GrBackendTexture& backendTexture, 164 GrSurfaceOrigin origin, 165 SkColorType colorType, 166 SkAlphaType alphaType, 167 sk_sp<SkColorSpace> colorSpace, 168 TextureReleaseProc textureReleaseProc, 169 ReleaseContext releaseContext); 170 171 /** 172 * Decodes and uploads the encoded data to a GPU backed image using the supplied GrContext. 173 * That image can be safely used by other GrContexts, across thread boundaries. The GrContext 174 * used here, and the ones used to draw this image later must be in the same GL share group, 175 * or use the same Vulkan VkDevice and VkQueue, or otherwise be able to share resources. 176 * 177 * When the image's ref count reaches zero, the original GrContext will destroy the texture, 178 * asynchronously. 179 * 180 * The texture will be decoded and uploaded to be suitable for use with surfaces that have the 181 * supplied destination color space. The color space of the image itself will be determined 182 * from the encoded data. 183 */ 184 static sk_sp<SkImage> MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> data, 185 bool buildMips, SkColorSpace* dstColorSpace); 186 187 /** 188 * Uploads the pixmap to a GPU backed image using the supplied GrContext. 189 * That image can be safely used by other GrContexts, across thread boundaries. The GrContext 190 * used here, and the ones used to draw this image later must be in the same GL share group, 191 * or use the same Vulkan VkDevice and VkQueue, or otherwise be able to share resources. 192 * 193 * When the image's ref count reaches zero, the original GrContext will destroy the texture, 194 * asynchronously. 195 * 196 * The texture will be processed to be suitable for use with surfaces that have the 197 * supplied destination color space. The color space of the image itself will be determined 198 * from the pixmap. 199 */ 200 static sk_sp<SkImage> MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap, 201 bool buildMips, SkColorSpace* dstColorSpace); 202 203 /** 204 * Create a new image from the specified descriptor. Note - Skia will delete or recycle the 205 * texture when the image is released. 206 * 207 * Will return NULL if the specified backend texture is unsupported. 208 * 209 * This is not supported if the GrContext was obtained from a deferred display list canvas 210 * since if the SkImage never gets used and flushed to a real GrContext, we have no way to be 211 * able to delete the underlying backend texture. 212 * 213 * DEPRECATED: This factory is deprecated and clients should use the factory below which takes 214 * an SkColorType. 215 */ 216 static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context, 217 const GrBackendTexture& backendTexture, 218 GrSurfaceOrigin surfaceOrigin, 219 SkAlphaType alphaType = kPremul_SkAlphaType, 220 sk_sp<SkColorSpace> colorSpace = nullptr); 221 222 /** 223 * Create a new image from the specified descriptor. Note - Skia will delete or recycle the 224 * texture when the image is released. 225 * 226 * The GrBackendTexture must have a valid backend format supplied (GrGLTextureInfo::fFormat, 227 * GrVkImageInfo::fFormat, etc.) in it. The passed in SkColorType informs skia how it should 228 * interpret the backend format supplied by the GrBackendTexture. If the format in the 229 * GrBackendTexture is not compitable with the SkColorType, SkAlphaType, and SkColorSpace we 230 * will return nullptr. 231 * 232 * This is not supported if the GrContext was obtained from a deferred display list canvas 233 * since if the SkImage never gets used and flushed to a real GrContext, we have no way to be 234 * able to delete the underlying backend texture. 235 */ 236 static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context, 237 const GrBackendTexture& backendTexture, 238 GrSurfaceOrigin surfaceOrigin, 239 SkColorType colorType, 240 SkAlphaType alphaType = kPremul_SkAlphaType, 241 sk_sp<SkColorSpace> colorSpace = nullptr); 242 243 /** 244 * Create a new image by copying the pixels from the specified y, u, v textures. The data 245 * from the textures is immediately ingested into the image and the textures can be modified or 246 * deleted after the function returns. The image will have the dimensions of the y texture. 247 */ 248 static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace, 249 const GrBackendObject yuvTextureHandles[3], 250 const SkISize yuvSizes[3], 251 GrSurfaceOrigin surfaceOrigin, 252 sk_sp<SkColorSpace> colorSpace = nullptr); 253 254 /** 255 * Create a new image by copying the pixels from the specified y and uv textures. The data 256 * from the textures is immediately ingested into the image and the textures can be modified or 257 * deleted after the function returns. The image will have the dimensions of the y texture. 258 */ 259 static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context, 260 SkYUVColorSpace yuvColorSpace, 261 const GrBackendObject nv12TextureHandles[2], 262 const SkISize nv12Sizes[2], 263 GrSurfaceOrigin surfaceOrigin, 264 sk_sp<SkColorSpace> colorSpace = nullptr); 265 266 /** 267 * Create a new image by copying the pixels from the specified y, u, v textures. The data 268 * from the textures is immediately ingested into the image and the textures can be modified or 269 * deleted after the function returns. The image will have the dimensions of the y texture. 270 */ 271 static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace, 272 const GrBackendTexture yuvTextureHandles[3], 273 const SkISize yuvSizes[3], 274 GrSurfaceOrigin surfaceOrigin, 275 sk_sp<SkColorSpace> colorSpace = nullptr); 276 277 /** 278 * Create a new image by copying the pixels from the specified y and uv textures. The data 279 * from the textures is immediately ingested into the image and the textures can be modified or 280 * deleted after the function returns. The image will have the dimensions of the y texture. 281 */ 282 static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context, 283 SkYUVColorSpace yuvColorSpace, 284 const GrBackendTexture nv12TextureHandles[2], 285 const SkISize nv12Sizes[2], 286 GrSurfaceOrigin surfaceOrigin, 287 sk_sp<SkColorSpace> colorSpace = nullptr); 288 289 enum class BitDepth { 290 kU8, 291 kF16, 292 }; 293 294 /** 295 * Create a new image from the specified picture. 296 * On creation of the SkImage, snap the SkPicture to a particular BitDepth and SkColorSpace. 297 */ 298 static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions, 299 const SkMatrix* matrix, const SkPaint* paint, 300 BitDepth bitDepth, 301 sk_sp<SkColorSpace> colorSpace); 302 303 #if defined(SK_BUILD_FOR_ANDROID) && __ANDROID_API__ >= 26 304 /** 305 * Create a new image from the an Android hardware buffer. 306 * The new image takes a reference on the buffer. 307 */ 308 static sk_sp<SkImage> MakeFromAHardwareBuffer(AHardwareBuffer* hardwareBuffer, 309 SkAlphaType alphaType = kPremul_SkAlphaType, 310 sk_sp<SkColorSpace> colorSpace = nullptr); 311 #endif 312 313 /////////////////////////////////////////////////////////////////////////////////////////////// 314 315 int width() const { return fWidth; } 316 int height() const { return fHeight; } 317 SkISize dimensions() const { return SkISize::Make(fWidth, fHeight); } 318 SkIRect bounds() const { return SkIRect::MakeWH(fWidth, fHeight); } 319 uint32_t uniqueID() const { return fUniqueID; } 320 SkAlphaType alphaType() const; 321 322 /** 323 * Returns the color space of the SkImage. 324 * 325 * This is the color space that was supplied on creation of the SkImage or a color 326 * space that was parsed from encoded data. This color space is not guaranteed to be 327 * renderable. Can return nullptr if the SkImage was created without a color space. 328 */ 329 SkColorSpace* colorSpace() const; 330 sk_sp<SkColorSpace> refColorSpace() const; 331 332 /** 333 * Returns true fi the image will be drawn as a mask, with no intrinsic color of its own. 334 */ 335 bool isAlphaOnly() const; 336 bool isOpaque() const { return SkAlphaTypeIsOpaque(this->alphaType()); } 337 338 sk_sp<SkShader> makeShader(SkShader::TileMode tileMode1, SkShader::TileMode tileMode2, 339 const SkMatrix* localMatrix = nullptr) const; 340 /** 341 * Helper version of makeShader() that specifies Clamp tilemode. 342 */ 343 sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const { 344 return this->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, localMatrix); 345 } 346 347 /** 348 * If the image has direct access to its pixels (i.e. they are in local RAM) 349 * return true, and if not null, return in the pixmap parameter the info about the 350 * images pixels. 351 * 352 * On failure, return false and ignore the pixmap parameter. 353 */ 354 bool peekPixels(SkPixmap* pixmap) const; 355 356 // DEPRECATED - currently used by Canvas2DLayerBridge in Chromium. 357 GrTexture* getTexture() const; 358 359 /** 360 * Returns true if the image is texture backed. 361 */ 362 bool isTextureBacked() const; 363 364 /** 365 * Returns true if the image is able to be drawn to a particular type of device. If context 366 * is nullptr, tests for drawability to CPU devices. Otherwise, tests for drawability to a GPU 367 * device backed by context. 368 * 369 * Texture-backed images may become invalid if their underlying GrContext is abandoned. Some 370 * generator-backed images may be invalid for CPU and/or GPU. 371 */ 372 bool isValid(GrContext* context) const; 373 374 /** 375 * Retrieves the backend API handle of the texture. If flushPendingGrContextIO then the 376 * GrContext will issue to the backend API any deferred IO operations on the texture before 377 * returning. 378 * If 'origin' is supplied it will be filled in with the origin of the content drawn 379 * into the image. 380 */ 381 GrBackendObject getTextureHandle(bool flushPendingGrContextIO, 382 GrSurfaceOrigin* origin = nullptr) const; 383 384 /** 385 * Hints to image calls where the system might cache computed intermediates (e.g. the results 386 * of decoding or a read-back from the GPU. Passing kAllow signals that the system's default 387 * behavior is fine. Passing kDisallow signals that caching should be avoided. 388 */ 389 enum CachingHint { 390 kAllow_CachingHint, 391 kDisallow_CachingHint, 392 }; 393 394 /** 395 * Copy the pixels from the image into the specified buffer (pixels + rowBytes), 396 * converting them into the requested format (dstInfo). The image pixels are read 397 * starting at the specified (srcX,srcY) location. 398 * 399 * The specified ImageInfo and (srcX,srcY) offset specifies a source rectangle 400 * 401 * srcR.setXYWH(srcX, srcY, dstInfo.width(), dstInfo.height()); 402 * 403 * srcR is intersected with the bounds of the image. If this intersection is not empty, 404 * then we have two sets of pixels (of equal size). Replace the dst pixels with the 405 * corresponding src pixels, performing any colortype/alphatype transformations needed 406 * (in the case where the src and dst have different colortypes or alphatypes). 407 * 408 * This call can fail, returning false, for several reasons: 409 * - If srcR does not intersect the image bounds. 410 * - If the requested colortype/alphatype cannot be converted from the image's types. 411 */ 412 bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, 413 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const; 414 415 bool readPixels(const SkPixmap& dst, int srcX, int srcY, 416 CachingHint cachingHint = kAllow_CachingHint) const; 417 418 /** 419 * Copy the pixels from this image into the dst pixmap, converting as needed into dst's 420 * colortype/alphatype. If the conversion cannot be performed, false is returned. 421 * 422 * If dst's dimensions differ from the src dimension, the image will be scaled, applying the 423 * specified filter-quality. 424 */ 425 bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality, 426 CachingHint cachingHint = kAllow_CachingHint) const; 427 428 /** 429 * Encode the image's pixels and return the result as SkData. This will ignore any possible 430 * existing encoded data (see refEncodedData()), and will always attempt to encode the 431 * image using the specified encoded image format. 432 * 433 * If the image type cannot be encoded, or the requested encoder format is 434 * not supported, this will return nullptr. 435 */ 436 sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const; 437 438 /** 439 * Encode the image and return the result as SkData. This will attempt to reuse existing 440 * encoded data (as returned by refEncodedData). If there is no eisting data, the image 441 * will be encoded using PNG. On an error, this returns nullptr. 442 */ 443 sk_sp<SkData> encodeToData() const; 444 445 /** 446 * If the image already has its contents in encoded form (e.g. PNG or JPEG), return that 447 * as SkData. If the image does not already has its contents in encoded form, return NULL. 448 * 449 * Note: to force the image to return its contents as encoded data, use encodeToData(...). 450 */ 451 sk_sp<SkData> refEncodedData() const; 452 453 const char* toString(SkString* string) const; 454 455 /** 456 * Return a new image that is a subset of this image. The underlying implementation may 457 * share the pixels, or it may make a copy. 458 * 459 * If subset does not intersect the bounds of this image, or the copy/share cannot be made, 460 * NULL will be returned. 461 */ 462 sk_sp<SkImage> makeSubset(const SkIRect& subset) const; 463 464 /** 465 * Ensures that an image is backed by a texture (when GrContext is non-null), suitable for use 466 * with surfaces that have the supplied destination color space. If no transformation is 467 * required, the returned image may be the same as this image. If this image is from a 468 * different GrContext, this will fail. 469 */ 470 sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const; 471 472 /** 473 * If the image is texture-backed this will make a raster copy of it (or nullptr if reading back 474 * the pixels fails). Otherwise, it returns the original image. 475 */ 476 sk_sp<SkImage> makeNonTextureImage() const; 477 478 /** 479 * If this image is already backed by raster (i.e. peekPixels would succeed), then this just 480 * returns itself. If not, this attempts to create a raster version of this image and returns 481 * that, or nullptr if that fails. 482 */ 483 sk_sp<SkImage> makeRasterImage() const; 484 485 /** 486 * Apply a given image filter to this image, and return the filtered result. 487 * 488 * The subset represents the active portion of this image. The return value is similarly an 489 * SkImage, with an active subset (outSubset). This is usually used with texture-backed 490 * images, where the texture may be approx-match and thus larger than the required size. 491 * 492 * clipBounds constrains the device-space extent of the image which may be produced to the 493 * given rect. 494 * 495 * offset is the amount to translate the resulting image relative to the src when it is drawn. 496 * This is an out-param. 497 * 498 * If the result image cannot be created, or the result would be transparent black, null 499 * is returned, in which case the offset and outSubset parameters should be ignored by the 500 * caller. 501 */ 502 sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset, 503 const SkIRect& clipBounds, SkIRect* outSubset, 504 SkIPoint* offset) const; 505 506 /** Drawing params for which a deferred texture image data should be optimized. */ 507 struct DeferredTextureImageUsageParams { 508 DeferredTextureImageUsageParams(const SkMatrix matrix, const SkFilterQuality quality, 509 int preScaleMipLevel) 510 : fMatrix(matrix), fQuality(quality), fPreScaleMipLevel(preScaleMipLevel) {} 511 SkMatrix fMatrix; 512 SkFilterQuality fQuality; 513 int fPreScaleMipLevel; 514 }; 515 516 /** 517 * This method allows clients to capture the data necessary to turn a SkImage into a texture- 518 * backed image. If the original image is codec-backed this will decode into a format optimized 519 * for the context represented by the proxy. This method is thread safe with respect to the 520 * GrContext whence the proxy came. Clients allocate and manage the storage of the deferred 521 * texture data and control its lifetime. No cleanup is required, thus it is safe to simply free 522 * the memory out from under the data. 523 * 524 * The same method is used both for getting the size necessary for pre-uploaded texture data 525 * and for retrieving the data. The params array represents the set of draws over which to 526 * optimize the pre-upload data. 527 * 528 * When called with a null buffer this returns the size that the client must allocate in order 529 * to create deferred texture data for this image (or zero if this is an inappropriate 530 * candidate). The buffer allocated by the client should be 8 byte aligned. 531 * 532 * When buffer is not null this fills in the deferred texture data for this image in the 533 * provided buffer (assuming this is an appropriate candidate image and the buffer is 534 * appropriately aligned). Upon success the size written is returned, otherwise 0. 535 * 536 * dstColorSpace is the color space of the surface where this texture will ultimately be used. 537 * If the method determines that mip-maps are needed, this helps determine the correct strategy 538 * for building them (gamma-correct or not). 539 * 540 * dstColorType is the color type of the surface where this texture will ultimately be used. 541 * This determines the format with which the image will be uploaded to the GPU. If dstColorType 542 * does not support color spaces (low bit depth types such as ARGB_4444), then dstColorSpace 543 * must be null. 544 */ 545 size_t getDeferredTextureImageData(const GrContextThreadSafeProxy& contextThreadSafeProxy, 546 const DeferredTextureImageUsageParams deferredTextureImageUsageParams[], 547 int paramCnt, 548 void* buffer, 549 SkColorSpace* dstColorSpace = nullptr, 550 SkColorType dstColorType = kN32_SkColorType) const; 551 552 /** 553 * Returns a texture-backed image from data produced in SkImage::getDeferredTextureImageData. 554 * The context must be the context that provided the proxy passed to 555 * getDeferredTextureImageData. 556 */ 557 static sk_sp<SkImage> MakeFromDeferredTextureImageData(GrContext* context, const void* data, 558 SkBudgeted budgeted); 559 560 typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc; 561 562 /** 563 * Creates a GrBackendTexture from the provided SkImage. Returns true on success. The 564 * GrBackendTexture and BackendTextureReleaseProc are populated on success. It is the callers 565 * responsibility to call the BackendTextureReleaseProc once they have deleted the texture. 566 * 567 * Note that the BackendTextureReleaseProc allows Skia to clean up auxiliary data related 568 * to the GrBackendTexture, and is not a substitute for the client deleting the GrBackendTexture 569 * themselves. 570 * 571 * Note that if the SkImage is both texture backed and unowned (the only reference is 572 * std::moved into this function), Skia can return the texture directly, avoiding any 573 * conversions or copies. 574 * 575 * If the SkImage is not texture backed, this function will generate a texture with the image's 576 * contents and return that. 577 */ 578 static bool MakeBackendTextureFromSkImage(GrContext* context, 579 sk_sp<SkImage> image, 580 GrBackendTexture* backendTexture, 581 BackendTextureReleaseProc* backendTextureReleaseProc); 582 583 584 // Helper functions to convert to SkBitmap 585 586 enum LegacyBitmapMode { 587 kRO_LegacyBitmapMode, 588 kRW_LegacyBitmapMode, 589 }; 590 591 /** 592 * Attempt to create a bitmap with the same pixels as the image. The result will always be 593 * a raster-backed bitmap (texture-backed bitmaps are DEPRECATED, and not supported here). 594 * 595 * If the mode is kRO (read-only), the resulting bitmap will be marked as immutable. 596 * 597 * On succcess, returns true. On failure, returns false and the bitmap parameter will be reset 598 * to empty. 599 */ 600 bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode) const; 601 602 /** 603 * Returns true if the image is backed by an image-generator or other src that creates 604 * (and caches) its pixels / texture on-demand. 605 */ 606 bool isLazyGenerated() const; 607 608 /** 609 * If |target| is supported, returns an SkImage in the |target| color space. 610 * Otherwise, returns nullptr. 611 * 612 * This will leave the image as is if it already in the |target| color space. 613 * Otherwise, it will convert the pixels from the src color space to the |target| 614 * color space. If this->colorSpace() is nullptr, the src color space will be 615 * treated as sRGB. 616 * 617 * If |premulBehavior| is kIgnore, any premultiplication or unpremultiplication will 618 * be performed in the gamma encoded space. If it is kRespect, premultiplication is 619 * assumed to be linear. 620 */ 621 sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target, 622 SkTransferFunctionBehavior premulBehavior) const; 623 624 private: 625 SkImage(int width, int height, uint32_t uniqueID); 626 friend class SkImage_Base; 627 628 static sk_sp<SkImage> MakeTextureFromMipMap(GrContext*, const SkImageInfo&, 629 const GrMipLevel texels[], int mipLevelCount, 630 SkBudgeted, SkDestinationSurfaceColorMode); 631 632 const int fWidth; 633 const int fHeight; 634 const uint32_t fUniqueID; 635 636 typedef SkRefCnt INHERITED; 637 }; 638 639 #endif 640