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