• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#Topic Image
2#Alias Image_Reference
3
4#Subtopic Overview
5    #Subtopic Subtopic
6    #Populate
7    ##
8##
9
10#Class SkImage
11
12Image describes a two dimensional array of pixels to draw. The pixels may be
13decoded in a Raster_Bitmap, encoded in a Picture or compressed data stream,
14or located in GPU memory as a GPU_Texture.
15
16Image cannot be modified after it is created. Image may allocate additional
17storage as needed; for instance, an encoded Image may decode when drawn.
18
19Image width and height are greater than zero. Creating an Image with zero width
20or height returns Image equal to nullptr.
21
22Image may be created from Bitmap, Pixmap, Surface, Picture, encoded streams,
23GPU_Texture, YUV_ColorSpace data, or hardware buffer. Encoded streams supported
24include BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP. Supported encoding details
25vary with platform.
26
27#Subtopic Raster_Image
28#Alias Raster_Image
29#Line # pixels decoded in Raster_Bitmap ##
30Raster_Image pixels are decoded in a Raster_Bitmap. These pixels may be read
31directly and in most cases written to, although edited pixels may not be drawn
32if Image has been copied internally.
33##
34
35#Subtopic Texture_Image
36#Line # pixels located on GPU ##
37Texture_Image are located on GPU and pixels are not accessible. Texture_Image
38are allocated optimally for best performance. Raster_Image may
39be drawn to GPU_Surface, but pixels are uploaded from CPU to GPU downgrading
40performance.
41##
42
43#Subtopic Lazy_Image
44#Line # deferred pixel buffer ##
45Lazy_Image defer allocating buffer for Image pixels and decoding stream until
46Image is drawn. Lazy_Image caches result if possible to speed up repeated
47drawing.
48##
49
50#Subtopic Related_Function
51#Populate
52##
53
54#Subtopic Class_or_Struct
55#Populate
56##
57
58#Subtopic Constructor
59#Populate
60##
61
62#Subtopic Member_Function
63#Populate
64##
65
66#Typedef SkImageInfo Info
67
68##
69
70# ------------------------------------------------------------------------------
71
72#Method static sk_sp<SkImage> MakeRasterCopy(const SkPixmap& pixmap)
73#In Constructor
74#Line # creates Image from Pixmap and copied pixels ##
75Creates Image from Pixmap and copy of pixels. Since pixels are copied, Pixmap
76pixels may be modified or deleted without affecting Image.
77
78Image is returned if Pixmap is valid. Valid Pixmap parameters include:
79dimensions are greater than zero;
80each dimension fits in 29 bits;
81Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
82row bytes are large enough to hold one row of pixels;
83pixel address is not nullptr.
84
85#Param pixmap  Image_Info, pixel address, and row bytes ##
86
87#Return copy of Pixmap pixels, or nullptr  ##
88
89#Example
90#Height 50
91#Description
92Draw a five by five bitmap, and draw a copy in an Image. Editing the pixmap
93alters the bitmap draw, but does not alter the Image draw since the Image
94contains a copy of the pixels.
95##
96    uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
97                            { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
98                            { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
99                            { 0x90, 0x81, 0xC5, 0x71, 0x33 },
100                            { 0x75, 0x55, 0x44, 0x40, 0x30 }};
101    SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
102    SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
103    SkBitmap bitmap;
104    bitmap.installPixels(pixmap);
105    sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
106    *pixmap.writable_addr8(2, 2) = 0x00;
107    canvas->scale(10, 10);
108    canvas->drawBitmap(bitmap, 0, 0);
109    canvas->drawImage(image, 10, 0);
110##
111
112#SeeAlso MakeRasterData MakeFromGenerator
113
114#Method ##
115
116# ------------------------------------------------------------------------------
117
118#Method static sk_sp<SkImage> MakeRasterData(const Info& info, sk_sp<SkData> pixels, size_t rowBytes)
119#In Constructor
120#Line # creates Image from Image_Info and shared pixels ##
121Creates Image from Image_Info, sharing pixels.
122
123Image is returned if Image_Info is valid. Valid Image_Info parameters include:
124dimensions are greater than zero;
125each dimension fits in 29 bits;
126Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
127rowBytes are large enough to hold one row of pixels;
128pixels is not nullptr, and contains enough data for Image.
129
130#Param info  contains width, height, Alpha_Type, Color_Type, Color_Space ##
131#Param pixels  address or pixel storage ##
132#Param rowBytes  size of pixel row or larger ##
133
134#Return Image sharing pixels, or nullptr ##
135
136#Example
137#Image 3
138    size_t rowBytes = image->width() * SkColorTypeBytesPerPixel(kRGBA_8888_SkColorType);
139    sk_sp<SkData> data = SkData::MakeUninitialized(rowBytes * image->height());
140    SkImageInfo dstInfo = SkImageInfo::MakeN32(image->width(), image->height(),
141                                               kPremul_SkAlphaType);
142    image->readPixels(dstInfo, data->writable_data(), rowBytes, 0, 0, SkImage::kAllow_CachingHint);
143    sk_sp<SkImage> raw = SkImage::MakeRasterData(dstInfo.makeColorType(kRGBA_8888_SkColorType),
144                                                 data, rowBytes);
145    canvas->drawImage(image, 0, 0);
146    canvas->drawImage(raw.get(), 128, 0);
147##
148
149#SeeAlso MakeRasterCopy MakeFromGenerator
150
151#Method ##
152
153# ------------------------------------------------------------------------------
154
155#Typedef void* ReleaseContext
156
157Caller data passed to RasterReleaseProc; may be nullptr.
158
159#SeeAlso MakeFromRaster RasterReleaseProc
160
161##
162
163#Typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext)
164
165Function called when Image no longer shares pixels. ReleaseContext is
166provided by caller when Image is created, and may be nullptr.
167
168#SeeAlso ReleaseContext MakeFromRaster
169
170##
171
172#Method static sk_sp<SkImage> MakeFromRaster(const SkPixmap& pixmap,
173                                         RasterReleaseProc rasterReleaseProc,
174                                         ReleaseContext releaseContext)
175#In Constructor
176#Line # creates Image from Pixmap, with release ##
177
178Creates Image from pixmap, sharing Pixmap pixels. Pixels must remain valid and
179unchanged until rasterReleaseProc is called. rasterReleaseProc is passed
180releaseContext when Image is deleted or no longer refers to pixmap pixels.
181
182Pass nullptr for rasterReleaseProc to share Pixmap without requiring a callback
183when Image is released. Pass nullptr for releaseContext if rasterReleaseProc
184does not require state.
185
186Image is returned if pixmap is valid. Valid Pixmap parameters include:
187dimensions are greater than zero;
188each dimension fits in 29 bits;
189Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
190row bytes are large enough to hold one row of pixels;
191pixel address is not nullptr.
192
193#Param pixmap  Image_Info, pixel address, and row bytes ##
194#Param rasterReleaseProc  function called when pixels can be released; or nullptr ##
195#Param releaseContext  state passed to rasterReleaseProc; or nullptr ##
196
197#Return Image sharing pixmap ##
198
199#Example
200#Function
201static void releaseProc(const void* pixels, SkImage::ReleaseContext context) {
202     int* countPtr = static_cast<int*>(context);
203     *countPtr += 1;
204}
205##
206
207void draw(SkCanvas* canvas) {
208    SkColor color = 0;
209    SkPixmap pixmap(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), &color, 4);
210    int releaseCount = 0;
211    sk_sp<SkImage> image(SkImage::MakeFromRaster(pixmap, releaseProc, &releaseCount));
212    SkDebugf("before reset: %d\n", releaseCount);
213    image.reset();
214    SkDebugf("after reset: %d\n", releaseCount);
215}
216#StdOut
217before reset: 0
218after reset: 1
219##
220##
221
222#SeeAlso MakeRasterCopy MakeRasterData MakeFromGenerator RasterReleaseProc ReleaseContext
223
224#Method ##
225
226# ------------------------------------------------------------------------------
227
228#Method static sk_sp<SkImage> MakeFromBitmap(const SkBitmap& bitmap)
229#In Constructor
230#Line # creates Image from Bitmap, sharing or copying pixels ##
231Creates Image from bitmap, sharing or copying bitmap pixels. If the bitmap
232is marked immutable, and its pixel memory is shareable, it may be shared
233instead of copied.
234
235Image is returned if bitmap is valid. Valid Bitmap parameters include:
236dimensions are greater than zero;
237each dimension fits in 29 bits;
238Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
239row bytes are large enough to hold one row of pixels;
240pixel address is not nullptr.
241
242#Param bitmap  Image_Info, row bytes, and pixels ##
243
244#Return created Image, or nullptr ##
245
246#Example
247#Description
248The first Bitmap is shared; writing to the pixel memory changes the first
249Image.
250The second Bitmap is marked immutable, and is copied; writing to the pixel
251memory does not alter the second Image.
252##
253#Height 50
254    uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
255                            { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
256                            { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
257                            { 0x90, 0x81, 0xC5, 0x71, 0x33 },
258                            { 0x75, 0x55, 0x44, 0x40, 0x30 }};
259    SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
260    SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
261    SkBitmap bitmap;
262    bitmap.installPixels(pixmap);
263    sk_sp<SkImage> image1 = SkImage::MakeFromBitmap(bitmap);
264    bitmap.setImmutable();
265    sk_sp<SkImage> image2 = SkImage::MakeFromBitmap(bitmap);
266    *pixmap.writable_addr8(2, 2) = 0x00;
267    canvas->scale(10, 10);
268    canvas->drawImage(image1, 0, 0);
269    canvas->drawImage(image2, 10, 0);
270##
271
272#SeeAlso MakeFromRaster MakeRasterCopy MakeFromGenerator MakeRasterData
273
274#Method ##
275
276# ------------------------------------------------------------------------------
277
278#Method static sk_sp<SkImage> MakeFromGenerator(std::unique_ptr<SkImageGenerator> imageGenerator,
279                                            const SkIRect* subset = nullptr)
280#In Constructor
281#Line # creates Image from a stream of data ##
282
283Creates Image from data returned by imageGenerator. Generated data is owned by Image and may not
284be shared or accessed.
285
286subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
287otherwise, subset must be contained by image bounds.
288
289Image is returned if generator data is valid. Valid data parameters vary by type of data
290and platform.
291
292imageGenerator may wrap Picture data, codec data, or custom data.
293
294#Param imageGenerator  stock or custom routines to retrieve Image ##
295#Param subset  bounds of returned Image; may be nullptr ##
296
297#Return created Image, or nullptr ##
298
299#Example
300#Height 128
301#Description
302The generator returning Picture cannot be shared; std::move transfers ownership to generated Image.
303##
304    SkPictureRecorder recorder;
305    recorder.beginRecording(100, 100)->drawColor(SK_ColorRED);
306    auto picture = recorder.finishRecordingAsPicture();
307    auto gen = SkImageGenerator::MakeFromPicture({100, 100}, picture, nullptr, nullptr,
308                                                 SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB());
309    sk_sp<SkImage> image = SkImage::MakeFromGenerator(std::move(gen));
310    canvas->drawImage(image, 0, 0);
311##
312
313#SeeAlso MakeFromEncoded
314
315#Method ##
316
317# ------------------------------------------------------------------------------
318
319#Method static sk_sp<SkImage> MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset = nullptr)
320#In Constructor
321#Line # creates Image from encoded data ##
322Creates Image from encoded data.
323subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
324otherwise, subset must be contained by image bounds.
325
326Image is returned if format of the encoded data is recognized and supported.
327Recognized formats vary by platform.
328
329#Param encoded  data of Image to decode ##
330#Param subset  bounds of returned Image; may be nullptr ##
331
332#Return created Image, or nullptr ##
333
334#Example
335#Image 3
336int x = 0;
337for (int quality : { 100, 50, 10, 1} ) {
338    sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, quality);
339    sk_sp<SkImage> image = SkImage::MakeFromEncoded(encodedData);
340    canvas->drawImage(image, x, 0);
341    x += 64;
342}
343##
344
345#SeeAlso MakeFromGenerator
346
347#Method ##
348
349# ------------------------------------------------------------------------------
350
351#Typedef void (*TextureReleaseProc)(ReleaseContext releaseContext)
352
353##
354
355#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
356                                          const GrBackendTexture& backendTexture,
357                                          GrSurfaceOrigin origin,
358                                          SkAlphaType alphaType,
359                                          sk_sp<SkColorSpace> colorSpace)
360#Deprecated
361#Method ##
362
363# ------------------------------------------------------------------------------
364
365#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
366                                          const GrBackendTexture& backendTexture,
367                                          GrSurfaceOrigin origin,
368                                          SkAlphaType alphaType,
369                                          sk_sp<SkColorSpace> colorSpace,
370                                          TextureReleaseProc textureReleaseProc,
371                                          ReleaseContext releaseContext)
372
373#Deprecated
374#Method ##
375
376# ------------------------------------------------------------------------------
377
378#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
379                                          const GrBackendTexture& backendTexture,
380                                          GrSurfaceOrigin origin,
381                                          SkColorType colorType,
382                                          SkAlphaType alphaType,
383                                          sk_sp<SkColorSpace> colorSpace)
384#In Constructor
385#Line # creates Image from GPU_Texture ##
386Creates Image from GPU_Texture associated with context. Caller is responsible for
387managing the lifetime of GPU_Texture.
388
389Image is returned if format of backendTexture is recognized and supported.
390Recognized formats vary by GPU back-end.
391
392#Param context  GPU_Context ##
393#Param backendTexture  texture residing on GPU ##
394#Param origin  one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
395#Param colorType  one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
396                          kRGB_565_SkColorType, kARGB_4444_SkColorType,
397                          kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
398                          kGray_8_SkColorType, kRGBA_F16_SkColorType
399##
400#Param alphaType  one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
401                          kPremul_SkAlphaType, kUnpremul_SkAlphaType
402##
403#Param colorSpace  range of colors; may be nullptr ##
404
405#Return created Image, or nullptr ##
406
407#Example
408#Image 3
409#Platform gpu
410#Height 128
411#Description
412A back-end texture has been created and uploaded to the GPU outside of this example.
413##
414GrContext* context = canvas->getGrContext();
415if (!context) {
416   return;
417}
418canvas->scale(.25f, .25f);
419int x = 0;
420for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
421    sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
422           origin, kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
423    canvas->drawImage(image, x, 0);
424x += 512;
425}
426##
427
428#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
429
430#Method ##
431
432# ------------------------------------------------------------------------------
433
434#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
435                                          const GrBackendTexture& backendTexture,
436                                          GrSurfaceOrigin origin,
437                                          SkColorType colorType,
438                                          SkAlphaType alphaType,
439                                          sk_sp<SkColorSpace> colorSpace,
440                                          TextureReleaseProc textureReleaseProc,
441                                          ReleaseContext releaseContext)
442
443Creates Image from GPU_Texture associated with context. GPU_Texture must stay
444valid and unchanged until textureReleaseProc is called. textureReleaseProc is
445passed releaseContext when Image is deleted or no longer refers to texture.
446
447Image is returned if format of backendTexture is recognized and supported.
448Recognized formats vary by GPU back-end.
449
450#Param context  GPU_Context ##
451#Param backendTexture  texture residing on GPU ##
452#Param origin  one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
453#Param colorType  one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
454                          kRGB_565_SkColorType, kARGB_4444_SkColorType,
455                          kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
456                          kGray_8_SkColorType, kRGBA_F16_SkColorType
457##
458#Param alphaType  one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
459                          kPremul_SkAlphaType, kUnpremul_SkAlphaType
460##
461#Param colorSpace  range of colors; may be nullptr ##
462#Param textureReleaseProc  function called when texture can be released ##
463#Param releaseContext  state passed to textureReleaseProc ##
464
465#Return created Image, or nullptr ##
466
467#ToDo
468This doesn't do anything clever with TextureReleaseProc because it may not get called
469fwithin the lifetime of the example
470##
471
472#Example
473#Platform gpu
474#Image 4
475GrContext* context = canvas->getGrContext();
476if (!context) {
477   return;
478}
479auto debugster = [](SkImage::ReleaseContext releaseContext) -> void {
480    *((int *) releaseContext) += 128;
481};
482int x = 0;
483for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
484    sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
485           origin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr, debugster, &x);
486    canvas->drawImage(image, x, 0);
487    x += 128;
488}
489##
490
491#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
492
493#Method ##
494
495# ------------------------------------------------------------------------------
496
497#Method static sk_sp<SkImage> MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> data,
498                                                      bool buildMips,
499                                                      SkColorSpace* dstColorSpace)
500#In Constructor
501#Line # creates Image from encoded data, and uploads to GPU ##
502
503Creates Image from encoded data. Image is uploaded to GPU back-end using context.
504
505Created Image is available to other GPU contexts, and is available across thread
506boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
507share resources.
508
509When Image is no longer referenced, context releases texture memory
510asynchronously.
511
512Texture decoded from data is uploaded to match Surface created with
513dstColorSpace. Color_Space of Image is determined by encoded data.
514
515Image is returned if format of data is recognized and supported, and if context
516supports moving resources. Recognized formats vary by platform and GPU back-end.
517
518Image is returned using MakeFromEncoded if context is nullptr or does not support
519moving resources between contexts.
520
521#Param context  GPU_Context ##
522#Param data  Image to decode ##
523#Param buildMips  create Image as Mip_Map if true ##
524#Param dstColorSpace  range of colors of matching Surface on GPU ##
525
526#Return created Image, or nullptr ##
527
528#Example
529#Image 4
530#Height 64
531GrContext* context = canvas->getGrContext();
532sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, 100);
533sk_sp<SkImage> image = SkImage::MakeCrossContextFromEncoded(context,
534                                                            encodedData, false, nullptr);
535canvas->drawImage(image, 0, 0);
536##
537
538#SeeAlso MakeCrossContextFromPixmap
539
540#Method ##
541
542# ------------------------------------------------------------------------------
543
544#Method static sk_sp<SkImage> MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap,
545                                                      bool buildMips,
546                                                      SkColorSpace* dstColorSpace)
547#In Constructor
548#Line # creates Image from Pixmap, and uploads to GPU ##
549
550Creates Image from pixmap. Image is uploaded to GPU back-end using context.
551
552Created Image is available to other GPU contexts, and is available across thread
553boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
554share resources.
555
556When Image is no longer referenced, context releases texture memory
557asynchronously.
558
559Texture created from pixmap is uploaded to match Surface created with
560dstColorSpace. Color_Space of Image is determined by pixmap.colorSpace().
561
562Image is returned referring to GPU back-end if context is not nullptr,
563format of data is recognized and supported, and if context supports moving
564resources between contexts. Otherwise, pixmap pixel data is copied and Image
565as returned in raster format if possible; nullptr may be returned.
566Recognized GPU formats vary by platform and GPU back-end.
567
568#Param context  GPU_Context ##
569#Param pixmap  Image_Info, pixel address, and row bytes ##
570#Param buildMips  create Image as Mip_Map if true ##
571#Param dstColorSpace  range of colors of matching Surface on GPU ##
572
573#Return created Image, or nullptr ##
574
575#Example
576#Image 4
577#Height 64
578GrContext* context = canvas->getGrContext();
579SkPixmap pixmap;
580if (source.peekPixels(&pixmap)) {
581    sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(context, pixmap,
582                                                               false, nullptr);
583    canvas->drawImage(image, 0, 0);
584}
585##
586
587#SeeAlso MakeCrossContextFromEncoded
588
589#Method ##
590
591# ------------------------------------------------------------------------------
592
593#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
594                                                 const GrBackendTexture& backendTexture,
595                                                 GrSurfaceOrigin surfaceOrigin,
596                                                 SkAlphaType alphaType = kPremul_SkAlphaType,
597                                                 sk_sp<SkColorSpace> colorSpace = nullptr)
598#Deprecated
599#Method ##
600
601# ------------------------------------------------------------------------------
602
603#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
604                                                 const GrBackendTexture& backendTexture,
605                                                 GrSurfaceOrigin surfaceOrigin,
606                                                 SkColorType colorType,
607                                                 SkAlphaType alphaType = kPremul_SkAlphaType,
608                                                 sk_sp<SkColorSpace> colorSpace = nullptr)
609#In Constructor
610#Line # creates Image from GPU_Texture, managed internally ##
611Creates Image from backendTexture associated with context. backendTexture and
612returned Image are managed internally, and are released when no longer needed.
613
614Image is returned if format of backendTexture is recognized and supported.
615Recognized formats vary by GPU back-end.
616
617#Param context  GPU_Context ##
618#Param backendTexture  texture residing on GPU ##
619#Param surfaceOrigin  one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
620#Param colorType  one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
621                          kRGB_565_SkColorType, kARGB_4444_SkColorType,
622                          kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
623                          kGray_8_SkColorType, kRGBA_F16_SkColorType
624##
625#Param alphaType  one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
626                          kPremul_SkAlphaType, kUnpremul_SkAlphaType
627##
628#Param colorSpace  range of colors; may be nullptr ##
629
630#Return created Image, or nullptr ##
631
632#Example
633#Image 5
634#Platform gpu
635   if (!canvas->getGrContext()) {
636       return;
637   }
638   canvas->scale(.5f, .5f);
639   canvas->clear(0x7f3f5f7f);
640   int x = 0, y = 0;
641   for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
642       for (auto alpha : { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType } ) {
643           sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(canvas->getGrContext(),
644                                                                  backEndTexture, origin,
645                                                                  kRGBA_8888_SkColorType, alpha);
646           canvas->drawImage(image, x, y);
647           x += 160;
648       }
649       x -= 160 * 3;
650       y += 256;
651   }
652##
653
654#SeeAlso MakeFromTexture MakeFromYUVTexturesCopy
655
656#Method ##
657
658# ------------------------------------------------------------------------------
659
660#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
661                                                  const GrBackendObject yuvTextureHandles[3],
662                                                  const SkISize yuvSizes[3],
663                                                  GrSurfaceOrigin surfaceOrigin,
664                                                  sk_sp<SkColorSpace> colorSpace = nullptr)
665#In Constructor
666#Line # creates Image from YUV_ColorSpace data in three planes ##
667
668Creates Image from copy of yuvTextureHandles, an array of textures on GPU.
669yuvTextureHandles contain pixels for YUV planes of Image.
670yuvSizes contain dimensions for each pixel plane. Dimensions must be greater than
671zero but may differ from plane to plane. Returned Image has the dimensions
672yuvSizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
673
674#Param context  GPU_Context ##
675#Param yuvColorSpace  one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
676                              kRec709_SkYUVColorSpace
677##
678#Param yuvTextureHandles  array of YUV textures on GPU ##
679#Param yuvSizes  dimensions of YUV textures ##
680#Param surfaceOrigin  one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
681#Param colorSpace  range of colors; may be nullptr ##
682
683#Return created Image, or nullptr ##
684
685# seems too complicated to create an example for this
686#ToDo
687should this be moved to chrome only?
688##
689
690#NoExample
691##
692
693#SeeAlso MakeFromNV12TexturesCopy
694
695#Method ##
696
697# ------------------------------------------------------------------------------
698
699#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
700                                                  const GrBackendTexture yuvTextureHandles[3],
701                                                  const SkISize yuvSizes[3],
702                                                  GrSurfaceOrigin surfaceOrigin,
703                                                  sk_sp<SkColorSpace> colorSpace = nullptr)
704
705Creates Image from copy of yuvTextureHandles, an array of textures on GPU.
706yuvTextureHandles contain pixels for YUV planes of Image.
707yuvSizes contain dimensions for each pixel plane. Dimensions must be greater than
708zero but may differ from plane to plane. Returned Image has the dimensions
709yuvSizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
710
711#Param context  GPU_Context ##
712#Param yuvColorSpace  one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
713                              kRec709_SkYUVColorSpace
714##
715#Param yuvTextureHandles  array of YUV textures on GPU ##
716#Param yuvSizes  dimensions of YUV textures ##
717#Param surfaceOrigin  one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
718#Param colorSpace  range of colors; may be nullptr ##
719
720#Return created Image, or nullptr ##
721
722# seems too complicated to create an example for this
723#ToDo
724should this be moved to chrome only?
725##
726
727#NoExample
728##
729
730#SeeAlso MakeFromNV12TexturesCopy
731
732#Method ##
733
734# ------------------------------------------------------------------------------
735
736#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
737                                                   SkYUVColorSpace yuvColorSpace,
738                                                   const GrBackendObject nv12TextureHandles[2],
739                                                   const SkISize nv12Sizes[2],
740                                                   GrSurfaceOrigin surfaceOrigin,
741                                                   sk_sp<SkColorSpace> colorSpace = nullptr)
742#In Constructor
743#Line # creates Image from YUV_ColorSpace data in two planes ##
744
745Creates Image from copy of nv12TextureHandles, an array of textures on GPU.
746nv12TextureHandles[0] contains pixels for YUV_Component_Y plane.
747nv12TextureHandles[1] contains pixels for YUV_Component_U plane,
748followed by pixels for YUV_Component_V plane.
749nv12Sizes contain dimensions for each pixel plane. Dimensions must be greater than
750zero but may differ from plane to plane. Returned Image has the dimensions
751nv12Sizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
752
753#Param context  GPU_Context ##
754#Param yuvColorSpace  one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
755                              kRec709_SkYUVColorSpace
756##
757#Param nv12TextureHandles  array of YUV textures on GPU ##
758#Param nv12Sizes  dimensions of YUV textures ##
759#Param surfaceOrigin  one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
760#Param colorSpace  range of colors; may be nullptr ##
761
762#Return created Image, or nullptr ##
763
764# seems too complicated to create an example for this
765#ToDo
766should this be moved to chrome only?
767##
768
769#NoExample
770##
771
772#SeeAlso MakeFromYUVTexturesCopy
773
774#Method ##
775
776# ------------------------------------------------------------------------------
777
778#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
779                                                   SkYUVColorSpace yuvColorSpace,
780                                                   const GrBackendTexture nv12TextureHandles[2],
781                                                   const SkISize nv12Sizes[2],
782                                                   GrSurfaceOrigin surfaceOrigin,
783                                                   sk_sp<SkColorSpace> colorSpace = nullptr)
784
785Creates Image from copy of nv12TextureHandles, an array of textures on GPU.
786nv12TextureHandles[0] contains pixels for YUV_Component_Y plane.
787nv12TextureHandles[1] contains pixels for YUV_Component_U plane,
788followed by pixels for YUV_Component_V plane.
789nv12Sizes contain dimensions for each pixel plane. Dimensions must be greater than
790zero but may differ from plane to plane. Returned Image has the dimensions
791nv12Sizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
792
793#Param context  GPU_Context ##
794#Param yuvColorSpace  one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
795                              kRec709_SkYUVColorSpace
796##
797#Param nv12TextureHandles  array of YUV textures on GPU ##
798#Param nv12Sizes  dimensions of YUV textures ##
799#Param surfaceOrigin  one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
800#Param colorSpace  range of colors; may be nullptr ##
801
802#Return created Image, or nullptr ##
803
804# seems too complicated to create an example for this
805#ToDo
806should this be moved to chrome only?
807##
808
809#NoExample
810##
811
812#SeeAlso MakeFromYUVTexturesCopy
813
814#Method ##
815
816# ------------------------------------------------------------------------------
817
818# currently uncalled by any test or client ##
819#Bug 7424
820
821#EnumClass BitDepth
822
823#Code
824    enum class BitDepth {
825        kU8,
826        kF16,
827    };
828##
829
830#Const kU8 0
831Use 8 bits per Color_ARGB component using unsigned integer format.
832##
833#Const kF16 1
834Use 16 bits per Color_ARGB component using half-precision floating point format.
835##
836
837#NoExample
838##
839
840#SeeAlso MakeFromPicture
841
842#EnumClass ##
843
844# ------------------------------------------------------------------------------
845
846#Method static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
847                                          const SkMatrix* matrix, const SkPaint* paint,
848                                          BitDepth bitDepth,
849                                          sk_sp<SkColorSpace> colorSpace)
850#In Constructor
851#Line # creates Image from Picture ##
852
853Creates Image from picture. Returned Image width and height are set by dimensions.
854Image draws picture with matrix and paint, set to bitDepth and colorSpace.
855
856If matrix is nullptr, draws with identity Matrix. If paint is nullptr, draws
857with default Paint. colorSpace may be nullptr.
858
859#Param picture  stream of drawing commands ##
860#Param dimensions  width and height ##
861#Param matrix  Matrix to rotate, scale, translate, and so on; may be nullptr ##
862#Param paint  Paint to apply transparency, filtering, and so on; may be nullptr ##
863#Param bitDepth  8 bit integer or 16 bit float: per component ##
864#Param colorSpace  range of colors; may be nullptr ##
865
866#Return created Image, or nullptr ##
867
868#Example
869    SkPaint paint;
870    SkPictureRecorder recorder;
871    SkCanvas* recordingCanvas = recorder.beginRecording(50, 50);
872    for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) {
873        paint.setColor(color);
874        recordingCanvas->drawRect({10, 10, 30, 40}, paint);
875        recordingCanvas->translate(10, 10);
876        recordingCanvas->scale(1.2f, 1.4f);
877    }
878    sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
879    int x = 0, y = 0;
880    for (auto alpha : { 70, 140, 210 } ) {
881        paint.setAlpha(alpha);
882        auto srgbColorSpace = SkColorSpace::MakeSRGB();
883        sk_sp<SkImage> image = SkImage::MakeFromPicture(playback, {50, 50}, nullptr, &paint,
884                                                        SkImage::BitDepth::kU8, srgbColorSpace);
885        canvas->drawImage(image, x, y);
886        x += 70; y += 70;
887    }
888##
889
890#SeeAlso SkCanvas::drawPicture
891
892#Method ##
893
894# ------------------------------------------------------------------------------
895
896#Method static sk_sp<SkImage> MakeFromAHardwareBuffer(AHardwareBuffer* hardwareBuffer,
897                                                 SkAlphaType alphaType = kPremul_SkAlphaType,
898                                                 sk_sp<SkColorSpace> colorSpace = nullptr)
899#In Constructor
900#Line # creates Image from Android hardware buffer ##
901
902#Bug 7447
903
904Creates Image from Android hardware buffer.
905Returned Image takes a reference on the buffer.
906
907Only available on Android, when __ANDROID_API__ is defined to be 26 or greater.
908
909#Param hardwareBuffer  AHardwareBuffer Android hardware buffer ##
910#Param alphaType  one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
911                          kPremul_SkAlphaType, kUnpremul_SkAlphaType
912##
913#Param colorSpace  range of colors; may be nullptr ##
914
915#Return created Image, or nullptr ##
916
917#NoExample
918##
919
920#SeeAlso MakeFromRaster
921
922#Method ##
923
924# ------------------------------------------------------------------------------
925#Subtopic Property
926#Populate
927#Line # values and attributes ##
928##
929
930#Method int width() const
931#In Property
932#Line # returns pixel column count ##
933Returns pixel count in each row.
934
935#Return pixel width in Image ##
936
937#Example
938#Image 4
939#Height 96
940   canvas->translate(10, 10);
941   canvas->drawImage(image, 0, 0);
942   canvas->translate(0, image->height());
943   SkPaint paint;
944   paint.setTextAlign(SkPaint::kCenter_Align);
945   canvas->drawLine(0, 10, image->width(), 10, paint);
946   canvas->drawString("width", image->width() / 2, 25, paint);
947##
948
949#SeeAlso dimensions() height()
950
951#Method ##
952
953# ------------------------------------------------------------------------------
954
955#Method int height() const
956#In Property
957#Line # returns pixel row count ##
958Returns pixel row count.
959
960#Return pixel height in Image ##
961
962#Example
963#Image 4
964#Height 96
965   canvas->translate(10, 10);
966   canvas->drawImage(image, 0, 0);
967   canvas->translate(image->width(), 0);
968   SkPaint paint;
969   paint.setTextAlign(SkPaint::kCenter_Align);
970   paint.setVerticalText(true);
971   canvas->drawLine(10, 0, 10, image->height(), paint);
972   canvas->drawString("height", 25, image->height() / 2, paint);
973##
974
975#SeeAlso dimensions() width()
976
977#Method ##
978
979# ------------------------------------------------------------------------------
980
981#Method SkISize dimensions() const
982#In Property
983#Line # returns width() and height() ##
984Returns ISize { width(), height() }.
985
986#Return integral size of width() and height() ##
987
988#Example
989#Image 4
990    SkISize dimensions = image->dimensions();
991    SkIRect bounds = image->bounds();
992    SkIRect dimensionsAsBounds = SkIRect::MakeSize(dimensions);
993    SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
994##
995
996#SeeAlso height() width() bounds()
997
998#Method ##
999
1000# ------------------------------------------------------------------------------
1001
1002#Method SkIRect bounds() const
1003#In Property
1004#Line # returns width() and height() as Rectangle ##
1005Returns IRect { 0, 0, width(), height() }.
1006
1007#Return integral rectangle from origin to width() and height() ##
1008
1009#Example
1010#Height 128
1011#Image 4
1012    SkIRect bounds = image->bounds();
1013    for (int x : { 0, bounds.width() } ) {
1014        for (int y : { 0, bounds.height() } ) {
1015            canvas->drawImage(image, x, y);
1016        }
1017    }
1018##
1019
1020#SeeAlso dimensions()
1021
1022#Method ##
1023
1024# ------------------------------------------------------------------------------
1025
1026#Method uint32_t uniqueID() const
1027#In Property
1028#Line # identifier for Image ##
1029Returns value unique to image. Image contents cannot change after Image is
1030created. Any operation to create a new Image will receive generate a new
1031unique number.
1032
1033#Return unique identifier ##
1034
1035#Example
1036#Image 5
1037#Height 156
1038 sk_sp<SkImage> subset = image->makeSubset({10, 20, 90, 100});
1039 canvas->drawImage(image, 0, 0);
1040 canvas->drawImage(subset, 128, 0);
1041 SkPaint paint;
1042 SkString s;
1043 s.printf("original id: %d", image->uniqueID());
1044 canvas->drawString(s, 20, image->height() + 20, paint);
1045 s.printf("subset id: %d", subset->uniqueID());
1046 canvas->drawString(s, 148, subset->height() + 20, paint);
1047##
1048
1049#SeeAlso isLazyGenerated
1050
1051#Method ##
1052
1053# ------------------------------------------------------------------------------
1054
1055#Method SkAlphaType alphaType() const
1056#In Property
1057#Line # returns Alpha_Type ##
1058Returns Alpha_Type, one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
1059kPremul_SkAlphaType, kUnpremul_SkAlphaType.
1060
1061Alpha_Type returned was a parameter to an Image constructor,
1062or was parsed from encoded data.
1063
1064#Return Alpha_Type in Image ##
1065
1066#Example
1067#Image 4
1068#Height 96
1069  const char* alphaTypeStr[] = { "Unknown", "Opaque", "Premul", "Unpremul" };
1070  SkAlphaType alphaType = image->alphaType();
1071  canvas->drawImage(image, 16, 0);
1072  SkPaint paint;
1073  canvas->drawString(alphaTypeStr[(int) alphaType], 20, image->height() + 20, paint);
1074##
1075
1076#SeeAlso SkImageInfo::alphaType
1077
1078#Method ##
1079
1080# ------------------------------------------------------------------------------
1081
1082#Method SkColorSpace* colorSpace() const
1083#In Property
1084#Line # returns Color_Space ##
1085Returns Color_Space, the range of colors, associated with Image.  The
1086reference count of Color_Space is unchanged. The returned Color_Space is
1087immutable.
1088
1089Color_Space returned was passed to an Image constructor,
1090or was parsed from encoded data. Color_Space returned may be ignored when Image
1091is drawn, depending on the capabilities of the Surface receiving the drawing.
1092
1093#Return Color_Space in Image, or nullptr ##
1094
1095#Example
1096#Image 3
1097#Set sRGB
1098    SkPixmap pixmap;
1099    source.peekPixels(&pixmap);
1100    canvas->scale(.25f, .25f);
1101    int y = 0;
1102    for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1103                        SkColorSpace::kSRGB_RenderTargetGamma } ) {
1104        int x = 0;
1105        sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1106        for (int index = 0; index < 2; ++index) {
1107            pixmap.setColorSpace(colorSpace);
1108            sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1109            canvas->drawImage(image, x, y);
1110            colorSpace = image->colorSpace()->makeColorSpin();
1111            x += 512;
1112        }
1113        y += 512;
1114    }
1115##
1116
1117#SeeAlso refColorSpace makeColorSpace
1118
1119#Method ##
1120
1121# ------------------------------------------------------------------------------
1122
1123#Method sk_sp<SkColorSpace> refColorSpace() const
1124#In Property
1125#Line # returns Image_Info Color_Space ##
1126Returns a smart pointer to Color_Space, the range of colors, associated with
1127Image.  The smart pointer tracks the number of objects sharing this
1128SkColorSpace reference so the memory is released when the owners destruct.
1129
1130The returned SkColorSpace is immutable.
1131
1132Color_Space returned was passed to an Image constructor,
1133or was parsed from encoded data. Color_Space returned may be ignored when Image
1134is drawn, depending on the capabilities of the Surface receiving the drawing.
1135
1136#Return Color_Space in Image, or nullptr, wrapped in a smart pointer ##
1137
1138#Example
1139#Image 3
1140#Set sRGB
1141    SkPixmap pixmap;
1142    source.peekPixels(&pixmap);
1143    canvas->scale(.25f, .25f);
1144    int y = 0;
1145    for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1146                        SkColorSpace::kSRGB_RenderTargetGamma } ) {
1147        int x = 0;
1148        sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1149        for (int index = 0; index < 2; ++index) {
1150            pixmap.setColorSpace(colorSpace);
1151            sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1152            canvas->drawImage(image, x, y);
1153            colorSpace = image->refColorSpace()->makeColorSpin();
1154            x += 512;
1155        }
1156        y += 512;
1157    }
1158##
1159
1160#SeeAlso colorSpace makeColorSpace
1161
1162#Method ##
1163
1164# ------------------------------------------------------------------------------
1165
1166#Method bool isAlphaOnly() const
1167#In Property
1168#Line # returns if pixels represent a transparency mask ##
1169Returns true if Image pixels represent transparency only. If true, each pixel
1170is packed in 8 bits as defined by kAlpha_8_SkColorType.
1171
1172#Return true if pixels represent a transparency mask ##
1173
1174#Example
1175    uint8_t pmColors = 0;
1176    sk_sp<SkImage> image = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1), &pmColors, 1});
1177    SkDebugf("alphaOnly = %s\n", image->isAlphaOnly() ? "true" : "false");
1178#StdOut
1179alphaOnly = true
1180##
1181##
1182
1183#SeeAlso alphaType isOpaque
1184
1185#Method ##
1186
1187# ------------------------------------------------------------------------------
1188
1189#Method bool isOpaque() const
1190#In Property
1191#Line # returns if Alpha_Type is kOpaque_SkAlphaType ##
1192Returns true if pixels ignore their Alpha value and are treated as fully opaque.
1193
1194#Return true if Alpha_Type is kOpaque_SkAlphaType ##
1195
1196#Example
1197    auto check_isopaque = [](const SkImageInfo& imageInfo) -> void {
1198        auto surface(SkSurface::MakeRaster(imageInfo));
1199        auto image(surface->makeImageSnapshot());
1200        SkDebugf("isOpaque = %s\n", image->isOpaque() ? "true" : "false");
1201    };
1202
1203    check_isopaque(SkImageInfo::MakeN32Premul(5, 5));
1204    check_isopaque(SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType));
1205#StdOut
1206isOpaque = false
1207isOpaque = true
1208##
1209##
1210
1211#SeeAlso alphaType isAlphaOnly
1212
1213#Method ##
1214
1215# ------------------------------------------------------------------------------
1216
1217#Method sk_sp<SkShader> makeShader(SkShader::TileMode tileMode1, SkShader::TileMode tileMode2,
1218                               const SkMatrix* localMatrix = nullptr) const
1219#In Constructor
1220#Line # creates Shader, Paint element that can tile Image ##
1221
1222Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1223SkShader::TileMode rules to fill drawn area outside Image. localMatrix permits
1224transforming Image before Canvas_Matrix is applied.
1225
1226#Param tileMode1  tiling in x, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1227                  SkShader::kMirror_TileMode
1228##
1229#Param tileMode2  tiling in y, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1230                  SkShader::kMirror_TileMode
1231##
1232#Param localMatrix  Image transformation, or nullptr ##
1233
1234#Return Shader containing Image ##
1235
1236#Example
1237#Image 4
1238SkMatrix matrix;
1239matrix.setRotate(45);
1240SkPaint paint;
1241paint.setShader(image->makeShader(SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode,
1242                                  &matrix));
1243canvas->drawPaint(paint);
1244##
1245
1246#SeeAlso scalePixels
1247
1248#Method ##
1249
1250# ------------------------------------------------------------------------------
1251
1252#Method sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const
1253
1254Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1255SkShader::kClamp_TileMode to fill drawn area outside Image. localMatrix permits
1256transforming Image before Canvas_Matrix is applied.
1257
1258#Param localMatrix  Image transformation, or nullptr ##
1259
1260#Return Shader containing Image ##
1261
1262#Example
1263#Image 5
1264SkMatrix matrix;
1265matrix.setRotate(45);
1266matrix.postTranslate(125, 30);
1267SkPaint paint;
1268paint.setShader(image->makeShader(&matrix));
1269canvas->drawPaint(paint);
1270##
1271
1272#SeeAlso scalePixels
1273
1274#Method ##
1275
1276# ------------------------------------------------------------------------------
1277#Subtopic Pixels
1278#Populate
1279#Line # read and write pixel values ##
1280##
1281
1282#Method bool peekPixels(SkPixmap* pixmap) const
1283#In Pixels
1284#Line # returns Pixmap if possible ##
1285Copies Image pixel address, row bytes, and Image_Info to pixmap, if address
1286is available, and returns true. If pixel address is not available, return
1287false and leave pixmap unchanged.
1288
1289#Param pixmap  storage for pixel state if pixels are readable; otherwise, ignored ##
1290
1291#Return true if Image has direct access to pixels ##
1292
1293#Example
1294    SkBitmap bitmap;
1295    bitmap.allocPixels(SkImageInfo::MakeN32Premul(12, 11));
1296    SkCanvas offscreen(bitmap);
1297    offscreen.clear(SK_ColorWHITE);
1298    SkPaint paint;
1299    offscreen.drawString("%", 1, 10, paint);
1300    sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
1301    SkPixmap pixmap;
1302    if (image->peekPixels(&pixmap)) {
1303        const SkPMColor* pixels = pixmap.addr32();
1304        SkPMColor pmWhite = pixels[0];
1305        for (int y = 0; y < image->height(); ++y) {
1306            for (int x = 0; x < image->width(); ++x) {
1307                SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
1308            }
1309            SkDebugf("\n");
1310        }
1311    }
1312#StdOut
1313------------
1314--xx----x---
1315-x--x--x----
1316-x--x--x----
1317-x--x-x-----
1318--xx-xx-xx--
1319-----x-x--x-
1320----x--x--x-
1321----x--x--x-
1322---x----xx--
1323------------
1324##
1325##
1326
1327#SeeAlso readPixels
1328
1329#Method ##
1330
1331# ------------------------------------------------------------------------------
1332
1333#Method GrTexture* getTexture() const
1334#Deprecated
1335#Method ##
1336
1337# ------------------------------------------------------------------------------
1338
1339#Method bool isTextureBacked() const
1340#In Property
1341#Line # returns if Image was created from GPU_Texture ##
1342Returns true the contents of Image was created on or uploaded to GPU memory,
1343and is available as a GPU_Texture.
1344
1345#Return true if Image is a GPU_Texture ##
1346
1347#Example
1348#Image 5
1349#Platform gpu
1350auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1351    if (nullptr == image) {
1352        return;
1353    }
1354    SkPaint paint;
1355    paint.setAntiAlias(true);
1356    paint.setTextAlign(SkPaint::kCenter_Align);
1357    canvas->drawImage(image, 0, 0);
1358    canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1359    canvas->drawString(image->isTextureBacked() ? "is GPU texture" : "not GPU texture",
1360                       image->width() / 2, image->height() * 3 / 4, paint);
1361};
1362sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1363sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1364                            kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1365drawImage(image, "image");
1366canvas->translate(image->width(), 0);
1367drawImage(bitmapImage, "source");
1368canvas->translate(-image->width(), image->height());
1369drawImage(textureImage, "backEndTexture");
1370##
1371
1372#SeeAlso MakeFromTexture isValid
1373
1374#Method ##
1375
1376# ------------------------------------------------------------------------------
1377
1378#Method bool isValid(GrContext* context) const
1379#In Property
1380#Line # returns if Image can draw to Raster_Surface or GPU_Context ##
1381Returns true if Image can be drawn on either Raster_Surface or GPU_Surface.
1382If context is nullptr, tests if Image draws on Raster_Surface;
1383otherwise, tests if Image draws on GPU_Surface associated with context.
1384
1385Image backed by GPU_Texture may become invalid if associated GrContext is
1386invalid. Lazy_Image may be invalid and may not draw to Raster_Surface or
1387GPU_Surface or both.
1388
1389#Param context  GPU_Context ##
1390
1391#Return true if Image can be drawn  ##
1392
1393#Example
1394#Image 5
1395#Platform gpu
1396auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1397    if (nullptr == image) {
1398        return;
1399    }
1400    SkPaint paint;
1401    paint.setAntiAlias(true);
1402    paint.setTextAlign(SkPaint::kCenter_Align);
1403    canvas->drawImage(image, 0, 0);
1404    canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1405    if (canvas->getGrContext()) {
1406        canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" :
1407                "not valid on GPU", image->width() / 2, image->height() * 5 / 8, paint);
1408    }
1409    canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" :
1410            "not valid on CPU", image->width() / 2, image->height() * 7 / 8, paint);
1411};
1412sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1413sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1414                            kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1415drawImage(image, "image");
1416canvas->translate(image->width(), 0);
1417drawImage(bitmapImage, "source");
1418canvas->translate(-image->width(), image->height());
1419drawImage(textureImage, "backEndTexture");
1420##
1421
1422#SeeAlso isTextureBacked isLazyGenerated
1423
1424#Method ##
1425
1426# ------------------------------------------------------------------------------
1427
1428#Method GrBackendObject getTextureHandle(bool flushPendingGrContextIO,
1429                                     GrSurfaceOrigin* origin = nullptr) const
1430#In Property
1431#Line # returns GPU reference to Image as texture ##
1432
1433Retrieves the back-end API handle of texture. If flushPendingGrContextIO is true,
1434complete deferred I/O operations.
1435
1436If origin in not nullptr, copies location of content drawn into Image.
1437
1438#Param flushPendingGrContextIO  flag to flush outstanding requests ##
1439#Param origin  storage for one of: kTopLeft_GrSurfaceOrigin,
1440               kBottomLeft_GrSurfaceOrigin; or nullptr
1441##
1442
1443#Return back-end API texture handle, or nullptr ##
1444
1445#Example
1446#Image 4
1447#Platform gpu
1448GrContext* context = canvas->getGrContext();
1449if (!context) {
1450   return;
1451}
1452SkPaint paint;
1453paint.setAntiAlias(true);
1454SkString str;
1455int y = -10;
1456for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin } ) {
1457    sk_sp<SkImage> srcImage(SkImage::MakeFromTexture(context,
1458            backEndTexture, origin, kPremul_SkAlphaType, nullptr));
1459    GrSurfaceOrigin readBackOrigin;
1460    GrBackendObject readBackHandle = srcImage->getTextureHandle(false, &readBackOrigin);
1461    str.printf("readBackHandle: 0x%x", readBackHandle);
1462    canvas->drawString(str, 5, y += 30, paint);
1463    canvas->drawImage(srcImage, 80, y += 10);
1464    str.printf("origin: k%s_GrSurfaceOrigin", readBackOrigin ? "BottomLeft" : "TopLeft");
1465    canvas->drawString(str, 5, y += srcImage->height() + 10, paint);
1466}
1467##
1468
1469#Example
1470#Image 5
1471#Platform gpu
1472    auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1473        if (nullptr == image) {
1474            return;
1475        }
1476        SkPaint paint;
1477        paint.setAntiAlias(true);
1478        paint.setTextAlign(SkPaint::kCenter_Align);
1479        canvas->drawImage(image, 0, image->height() / 4);
1480        canvas->drawString(label, image->width() / 2, image->height() / 8, paint);
1481        GrSurfaceOrigin readBackOrigin;
1482        GrBackendObject readBackHandle = image->getTextureHandle(false, &readBackOrigin);
1483        canvas->drawString(readBackHandle ? "has readBackHandle" : "no readBackHandle",
1484                           image->width() / 2, image->height() * 11 / 8, paint);
1485    };
1486    drawImage(image, "image");
1487    canvas->translate(image->width(), 0);
1488    sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1489                                kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1490    drawImage(textureImage, "backEndTexture");
1491##
1492
1493#SeeAlso MakeFromTexture isTextureBacked
1494
1495#Method ##
1496
1497# ------------------------------------------------------------------------------
1498
1499#Enum CachingHint
1500
1501#Code
1502    enum CachingHint {
1503        kAllow_CachingHint,
1504        kDisallow_CachingHint,
1505    };
1506##
1507
1508CachingHint selects whether Skia may internally cache Bitmaps generated by
1509decoding Image, or by copying Image from GPU to CPU. The default behavior
1510allows caching Bitmaps.
1511
1512Choose kDisallow_CachingHint if Image pixels are to be used only once, or
1513if Image pixels reside in a cache outside of Skia, or to reduce memory pressure.
1514
1515Choosing kAllow_CachingHint does not ensure that pixels will be cached.
1516Image pixels may not be cached if memory requirements are too large or
1517pixels are not accessible.
1518
1519#Const kAllow_CachingHint 0
1520Allows Skia to internally cache decoded and copied pixels.
1521##
1522#Const kDisallow_CachingHint 1
1523Disallows Skia from internally caching decoded and copied pixels.
1524##
1525
1526#NoExample
1527##
1528
1529#SeeAlso readPixels scalePixels
1530
1531#Enum ##
1532
1533# ------------------------------------------------------------------------------
1534
1535#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1536                    int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const
1537#In Pixels
1538#Line # copies and converts pixels ##
1539
1540Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY),
1541and does not exceed Image (width(), height()).
1542
1543dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
1544destination. dstRowBytes specifics the gap from one destination row to the next.
1545Returns true if pixels are copied. Returns false if:
1546#List
1547# dstInfo.addr() equals nullptr ##
1548# dstRowBytes is less than dstInfo.minRowBytes ##
1549# Pixel_Ref is nullptr ##
1550##
1551
1552Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1553kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1554If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace must match.
1555If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType must
1556match. If Image Color_Space is nullptr, dstInfo.colorSpace must match. Returns
1557false if pixel conversion is not possible.
1558
1559srcX and srcY may be negative to copy only top or left of source. Returns
1560false if width() or height() is zero or negative.
1561Returns false if
1562#Formula
1563abs(srcX) >= Image width()
1564##
1565, or if
1566#Formula
1567abs(srcY) >= Image height()
1568##
1569.
1570
1571If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1572If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1573
1574#Param dstInfo  destination width, height, Color_Type, Alpha_Type, Color_Space ##
1575#Param dstPixels  destination pixel storage ##
1576#Param dstRowBytes  destination row length ##
1577#Param srcX  column index whose absolute value is less than width() ##
1578#Param srcY  row index whose absolute value is less than height() ##
1579#Param cachingHint  one of: kAllow_CachingHint, kDisallow_CachingHint ##
1580
1581#Return true if pixels are copied to dstPixels ##
1582
1583#Example
1584#Image 3
1585    canvas->scale(.5f, .5f);
1586    const int width = 32;
1587    const int height = 32;
1588    std::vector<int32_t> dstPixels;
1589    dstPixels.resize(height * width * 4);
1590    SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1591    for (int y = 0; y < 512; y += height ) {
1592        for (int x = 0; x < 512; x += width ) {
1593            if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) {
1594                SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
1595                SkBitmap bitmap;
1596                bitmap.installPixels(dstPixmap);
1597                canvas->drawBitmap(bitmap, 0, 0);
1598            }
1599            canvas->translate(48, 0);
1600        }
1601        canvas->translate(-16 * 48, 48);
1602    }
1603##
1604
1605#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
1606
1607#Method ##
1608
1609# ------------------------------------------------------------------------------
1610
1611#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY,
1612                    CachingHint cachingHint = kAllow_CachingHint) const
1613
1614Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and
1615does not exceed Image (width(), height()).
1616
1617dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
1618and row bytes of destination. dst.rowBytes specifics the gap from one destination
1619row to the next. Returns true if pixels are copied. Returns false if:
1620#List
1621# dst pixel storage equals nullptr ##
1622# dst.rowBytes is less than SkImageInfo::minRowBytes ##
1623# Pixel_Ref is nullptr ##
1624##
1625
1626Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1627kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1628If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1629If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1630match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1631false if pixel conversion is not possible.
1632
1633srcX and srcY may be negative to copy only top or left of source. Returns
1634false if width() or height() is zero or negative.
1635Returns false if
1636#Formula
1637abs(srcX) >= Image width()
1638##
1639, or if
1640#Formula
1641abs(srcY) >= Image height()
1642##
1643.
1644
1645If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1646If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1647
1648#Param dst  destination Pixmap: Image_Info, pixels, row bytes ##
1649#Param srcX  column index whose absolute value is less than width() ##
1650#Param srcY  row index whose absolute value is less than height() ##
1651#Param cachingHint  one of: kAllow_CachingHint, kDisallow_CachingHint ##
1652
1653#Return true if pixels are copied to dst ##
1654
1655#Example
1656#Image 3
1657    std::vector<int32_t> srcPixels;
1658    int rowBytes = image->width() * 4;
1659    int quarterWidth = image->width() / 4;
1660    int quarterHeight = image->height() / 4;
1661    srcPixels.resize(image->height() * rowBytes);
1662    for (int y = 0; y < 4; ++y) {
1663        for (int x = 0; x < 4; ++x) {
1664            SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1665                    &srcPixels.front() + x * image->height() * quarterWidth +
1666                    y * quarterWidth, rowBytes);
1667            image->readPixels(pixmap, x * quarterWidth, y * quarterHeight);
1668        }
1669    }
1670    canvas->scale(.5f, .5f);
1671    SkBitmap bitmap;
1672    bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()),
1673                             &srcPixels.front(), rowBytes);
1674    canvas->drawBitmap(bitmap, 0, 0);
1675##
1676
1677#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
1678
1679#Method ##
1680
1681# ------------------------------------------------------------------------------
1682
1683#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality,
1684                     CachingHint cachingHint = kAllow_CachingHint) const
1685#In Pixels
1686#Line # scales and converts one Image to another ##
1687
1688Copies Image to dst, scaling pixels to fit dst.width() and dst.height(), and
1689converting pixels to match dst.colorType and dst.alphaType. Returns true if
1690pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1691less than dst SkImageInfo::minRowBytes.
1692
1693Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1694kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1695If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1696If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1697match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1698false if pixel conversion is not possible.
1699
1700Scales the image, with filterQuality, to match dst.width() and dst.height().
1701filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1702Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1703Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1704Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1705kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1706
1707If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1708If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1709
1710#Param dst  destination Pixmap: Image_Info, pixels, row bytes ##
1711#Param filterQuality  one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1712                      kMedium_SkFilterQuality, kHigh_SkFilterQuality
1713##
1714#Param cachingHint  one of: kAllow_CachingHint, kDisallow_CachingHint ##
1715
1716#Return true if pixels are scaled to fit dst ##
1717
1718#Example
1719#Image 3
1720#Height 128
1721    std::vector<int32_t> srcPixels;
1722    int quarterWidth = image->width() / 16;
1723    int rowBytes = quarterWidth * 4;
1724    int quarterHeight = image->height() / 16;
1725    srcPixels.resize(quarterHeight * rowBytes);
1726    SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1727                    &srcPixels.front(), rowBytes);
1728    canvas->scale(4, 4);
1729    SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality,
1730                     kMedium_SkFilterQuality, kHigh_SkFilterQuality };
1731    for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) {
1732        image->scalePixels(pixmap, qualities[index]);
1733        sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
1734        canvas->drawImage(filtered, 16 * index, 0);
1735    }
1736##
1737
1738#SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels
1739
1740#Method ##
1741
1742# ------------------------------------------------------------------------------
1743
1744#Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const
1745#In Utility
1746#Line # returns encoded Image as SkData ##
1747Encodes Image pixels, returning result as SkData.
1748
1749Returns nullptr if encoding fails, or if encodedImageFormat is not supported.
1750
1751Image encoding in a format requires both building with one or more of:
1752SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support
1753for the encoded format.
1754
1755If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can
1756additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP,
1757SkEncodedImageFormat::kGIF.
1758
1759quality is a platform and format specific metric trading off size and encoding
1760error. When used, quality equaling 100 encodes with the least error. quality may
1761be ignored by the encoder.
1762
1763#Param encodedImageFormat  one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
1764    SkEncodedImageFormat::kWEBP
1765 ##
1766#Param quality  encoder specific metric with 100 equaling best ##
1767
1768#Return encoded Image, or nullptr ##
1769
1770#Example
1771#Image 3
1772    canvas->scale(4, 4);
1773    SkIRect subset = {0, 0, 16, 64};
1774    int x = 0;
1775    for (int quality : { 0, 10, 50, 100 } ) {
1776        sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
1777        sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
1778        canvas->drawImage(filtered, x, 0);
1779        x += 16;
1780    }
1781##
1782
1783#SeeAlso refEncodedData MakeFromEncoded
1784
1785#Method ##
1786
1787# ------------------------------------------------------------------------------
1788
1789#Method sk_sp<SkData> encodeToData() const
1790
1791Encodes Image pixels, returning result as SkData. Returns existing encoded data
1792if present; otherwise, Image is encoded with SkEncodedImageFormat::kPNG. Skia
1793must be built with SK_HAS_PNG_LIBRARY to encode Image.
1794
1795Returns nullptr if existing encoded data is missing or invalid, and
1796encoding fails.
1797
1798#Return encoded Image, or nullptr ##
1799
1800#Example
1801#Image 3
1802    canvas->scale(4, 4);
1803    SkIRect subset = {136, 32, 200, 96};
1804    sk_sp<SkData> data(image->encodeToData());
1805    sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
1806    canvas->drawImage(eye, 0, 0);
1807##
1808
1809#SeeAlso refEncodedData MakeFromEncoded
1810
1811#Method ##
1812
1813# ------------------------------------------------------------------------------
1814
1815#Method sk_sp<SkData> refEncodedData() const
1816#In Utility
1817#Line # returns Image encoded in SkData if present ##
1818Returns encoded Image pixels as SkData, if Image was created from supported
1819encoded stream format. Platform support for formats vary and may require building
1820with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY.
1821
1822Returns nullptr if Image contents are not encoded.
1823
1824#Return encoded Image, or nullptr ##
1825
1826#Example
1827#Image 3
1828#Platform gpu
1829    struct {
1830        const char* name;
1831        sk_sp<SkImage> image;
1832    } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1833          { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1834                            kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr) } };
1835    SkString string;
1836    SkPaint paint;
1837    for (const auto& test : tests ) {
1838        if (!test.image) {
1839            string.printf("no %s", test.name);
1840        } else {
1841            string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name);
1842        }
1843        canvas->drawString(string, 10, 20, paint);
1844        canvas->translate(0, 20);
1845    }
1846##
1847
1848#SeeAlso encodeToData MakeFromEncoded
1849
1850#Method ##
1851
1852# ------------------------------------------------------------------------------
1853#Subtopic Utility
1854#Populate
1855#Line # rarely called management functions ##
1856##
1857
1858#Method const char* toString(SkString* string) const
1859#In Utility
1860#Line # converts Image to machine readable form ##
1861Appends Image description to string, including unique ID, width, height, and
1862whether the image is opaque.
1863
1864#Param string  storage for description; existing content is preserved ##
1865
1866#Return string appended with Image description ##
1867
1868#Example
1869#Image 4
1870    struct {
1871        const char* name;
1872        sk_sp<SkImage> image;
1873    } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1874          { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1875                            kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr) } };
1876    SkString string;
1877    SkPaint paint;
1878    for (const auto& test : tests ) {
1879        string.printf("%s: ", test.name);
1880        test.image ? (void) test.image->toString(&string) : string.append("no image");
1881        canvas->drawString(string, 10, 20, paint);
1882        canvas->translate(0, 20);
1883    }
1884##
1885
1886#SeeAlso SkPaint::toString
1887
1888#Method ##
1889
1890# ------------------------------------------------------------------------------
1891
1892#Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const
1893#In Constructor
1894#Line # creates Image containing part of original ##
1895Returns subset of Image. subset must be fully contained by Image dimensions().
1896The implementation may share pixels, or may copy them.
1897
1898Returns nullptr if subset is empty, or subset is not contained by bounds, or
1899pixels in Image could not be read or copied.
1900
1901#Param subset  bounds of returned Image ##
1902
1903#Return partial or full Image, or nullptr ##
1904
1905#Example
1906#Image 3
1907    canvas->scale(.5f, .5f);
1908    const int width = 32;
1909    const int height = 32;
1910    for (int y = 0; y < 512; y += height ) {
1911        for (int x = 0; x < 512; x += width ) {
1912            sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height}));
1913            canvas->drawImage(subset, x * 3 / 2, y * 3 / 2);
1914        }
1915    }
1916##
1917
1918#SeeAlso MakeFromEncoded
1919
1920#Method ##
1921
1922# ------------------------------------------------------------------------------
1923
1924#Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const
1925#In Constructor
1926#Line # creates Image matching Color_Space if possible ##
1927Returns Image backed by GPU_Texture associated with context. Returned Image is
1928compatible with Surface created with dstColorSpace. Returns original
1929Image if context and dstColorSpace match.
1930
1931Returns nullptr if context is nullptr, or if Image was created with another
1932GrContext.
1933
1934#Param context  GPU_Context ##
1935#Param dstColorSpace  range of colors of matching Surface on GPU ##
1936
1937#Return created Image, or nullptr ##
1938
1939#Example
1940#Platform gpu
1941#Image 5
1942    auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void {
1943        if (nullptr == image || nullptr == context) {
1944            return;
1945        }
1946        SkPaint paint;
1947        paint.setAntiAlias(true);
1948        paint.setTextAlign(SkPaint::kCenter_Align);
1949        sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr));
1950        canvas->drawImage(texture, 0, 0);
1951        canvas->drawString(label, texture->width() / 2, texture->height() / 4, paint);
1952    };
1953    sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1954    GrContext* context = canvas->getGrContext();
1955    sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture,
1956                                kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1957    drawImage(image, context, "image");
1958    canvas->translate(image->width(), 0);
1959    drawImage(bitmapImage, context, "source");
1960    canvas->translate(-image->width(), image->height());
1961    drawImage(textureImage, context, "backEndTexture");
1962##
1963
1964#SeeAlso MakeFromTexture
1965
1966#Method ##
1967
1968# ------------------------------------------------------------------------------
1969
1970#Method sk_sp<SkImage> makeNonTextureImage() const
1971#In Constructor
1972#Line # creates Image without dependency on GPU_Texture ##
1973Returns Raster_Image or Lazy_Image. Copies Image backed by GPU_Texture into
1974CPU memory if needed. Returns original Image if decoded in Raster_Bitmap,
1975or if encoded in a stream.
1976
1977Returns nullptr if backed by GPU_Texture and copy fails.
1978
1979#Return Raster_Image, Lazy_Image, or nullptr ##
1980
1981#Example
1982#Image 5
1983#Platform gpu
1984    auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1985        if (nullptr == image) {
1986            return;
1987        }
1988        SkPaint paint;
1989        paint.setAntiAlias(true);
1990        paint.setTextAlign(SkPaint::kCenter_Align);
1991        sk_sp<SkImage> nonTexture(image->makeNonTextureImage());
1992        canvas->drawImage(nonTexture, 0, 0);
1993        canvas->drawString(label, nonTexture->width() / 2, nonTexture->height() / 4, paint);
1994    };
1995    sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1996    sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1997                                kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1998    drawImage(image, "image");
1999    canvas->translate(image->width(), 0);
2000    drawImage(bitmapImage, "source");
2001    canvas->translate(-image->width(), image->height());
2002    drawImage(textureImage, "backEndTexture");
2003##
2004
2005#SeeAlso makeTextureImage makeRasterImage MakeBackendTextureFromSkImage
2006
2007#Method ##
2008
2009# ------------------------------------------------------------------------------
2010
2011#Method sk_sp<SkImage> makeRasterImage() const
2012#In Constructor
2013#Line # creates Image compatible with Raster_Surface if possible ##
2014Returns Raster_Image. Copies Image backed by GPU_Texture into CPU memory,
2015or decodes Image from Lazy_Image. Returns original Image if decoded in
2016Raster_Bitmap.
2017
2018Returns nullptr if copy, decode, or pixel read fails.
2019
2020#Return Raster_Image, or nullptr ##
2021
2022#Bug 7479
2023#Example
2024#Image 5
2025#Platform gpu
2026    auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2027        if (nullptr == image) {
2028            return;
2029        }
2030        SkPaint paint;
2031        paint.setAntiAlias(true);
2032        paint.setTextAlign(SkPaint::kCenter_Align);
2033        sk_sp<SkImage> raster(image->makeRasterImage());
2034        canvas->drawImage(raster, 0, 0);
2035        canvas->drawString(label, raster->width() / 2, raster->height() / 4, paint);
2036    };
2037    sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2038    sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2039                                kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2040    drawImage(image, "image");
2041    canvas->translate(image->width(), 0);
2042    drawImage(bitmapImage, "source");
2043    canvas->translate(-image->width(), image->height());
2044    drawImage(textureImage, "backEndTexture");
2045##
2046
2047#SeeAlso isTextureBacked isLazyGenerated MakeFromRaster
2048
2049#Method ##
2050
2051# ------------------------------------------------------------------------------
2052
2053#Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
2054                                  const SkIRect& clipBounds, SkIRect* outSubset,
2055                                  SkIPoint* offset) const
2056#In Constructor
2057#Line # creates filtered, clipped Image ##
2058
2059Creates filtered Image. filter processes original Image, potentially changing
2060color, position, and size. subset is the bounds of original Image processed
2061by filter. clipBounds is the expected bounds of the filtered Image. outSubset
2062is required storage for the actual bounds of the filtered Image. offset is
2063required storage for translation of returned Image.
2064
2065Returns nullptr if Image could not be created. If nullptr is returned, outSubset
2066and offset are undefined.
2067
2068Useful for animation of SkImageFilter that varies size from frame to frame.
2069Returned Image is created larger than required by filter so that GPU_Texture
2070can be reused with different sized effects. outSubset describes the valid bounds
2071of GPU_Texture returned. offset translates the returned Image to keep subsequent
2072animation frames aligned with respect to each other.
2073
2074#Param filter  how Image is sampled when transformed ##
2075#Param subset  bounds of Image processed by filter ##
2076#Param clipBounds  expected bounds of filtered Image ##
2077#Param outSubset  storage for returned Image bounds ##
2078#Param offset  storage for returned Image translation ##
2079
2080#Return filtered Image, or nullptr ##
2081
2082#Example
2083#Description
2084In each frame of the animation, filtered Image is drawn in a different location.
2085By translating canvas by returned offset, Image appears stationary.
2086##
2087#Image 5
2088#Platform gpu
2089#Duration 5
2090    sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make(
2091                -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE,
2092                SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
2093                nullptr);
2094    sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr);
2095    SkIRect subset = image->bounds();
2096    SkIRect clipBounds = image->bounds();
2097    clipBounds.outset(60, 60);
2098    SkIRect outSubset;
2099    SkIPoint offset;
2100    sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds,
2101                            &outSubset, &offset));
2102    SkPaint paint;
2103    paint.setAntiAlias(true);
2104    paint.setStyle(SkPaint::kStroke_Style);
2105    canvas->drawLine(0, 0, offset.fX, offset.fY, paint);
2106    canvas->translate(offset.fX, offset.fY);
2107    canvas->drawImage(filtered, 0, 0);
2108    canvas->drawRect(SkRect::MakeFromIRect(outSubset), paint);
2109##
2110
2111#SeeAlso makeShader SkPaint::setImageFilter
2112
2113#Method ##
2114
2115# ------------------------------------------------------------------------------
2116
2117#Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc
2118
2119##
2120
2121# ------------------------------------------------------------------------------
2122
2123#Method static bool MakeBackendTextureFromSkImage(GrContext* context,
2124                                              sk_sp<SkImage> image,
2125                                              GrBackendTexture* backendTexture,
2126                                              BackendTextureReleaseProc* backendTextureReleaseProc)
2127#In Constructor
2128#Line # creates GPU_Texture from Image ##
2129
2130Creates a GrBackendTexture from the provided SkImage. Returns true and
2131stores result in backendTexture and backendTextureReleaseProc if
2132texture is created; otherwise, returns false and leaves
2133backendTexture and backendTextureReleaseProc unmodified.
2134
2135Call backendTextureReleaseProc after deleting backendTexture.
2136backendTextureReleaseProc cleans up auxiliary data related to returned
2137backendTexture. The caller must delete returned backendTexture after use.
2138
2139If Image is both texture backed and singly referenced, image is returned in
2140backendTexture without conversion or making a copy. Image is singly referenced
2141if its was transferred solely using std::move().
2142
2143If Image is not texture backed, returns texture with Image contents.
2144
2145#Param context  GPU_Context ##
2146#Param image  Image used for texture ##
2147#Param backendTexture  storage for backend texture ##
2148#Param backendTextureReleaseProc  storage for clean up function ##
2149
2150#Return true if backend texture was created ##
2151
2152#Example
2153#Platform gpu
2154#Height 64
2155#Function
2156static sk_sp<SkImage> create_gpu_image(GrContext* grContext) {
2157    const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
2158    auto surface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info));
2159    SkCanvas* canvas = surface->getCanvas();
2160    canvas->clear(SK_ColorWHITE);
2161    SkPaint paint;
2162    paint.setColor(SK_ColorBLACK);
2163    canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
2164    return surface->makeImageSnapshot();
2165}
2166##
2167
2168void draw(SkCanvas* canvas) {
2169    GrContext* grContext = canvas->getGrContext();
2170    if (!grContext) {
2171        return;
2172    }
2173    sk_sp<SkImage> backEndImage = create_gpu_image(grContext);
2174    canvas->drawImage(backEndImage, 0, 0);
2175    GrBackendTexture texture;
2176    SkImage::BackendTextureReleaseProc proc;
2177    if (!SkImage::MakeBackendTextureFromSkImage(grContext, std::move(backEndImage),
2178            &texture, &proc)) {
2179        return;
2180    }
2181    sk_sp<SkImage> i2 = SkImage::MakeFromTexture(grContext, texture, kTopLeft_GrSurfaceOrigin,
2182            kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
2183    canvas->drawImage(i2, 30, 30);
2184}
2185##
2186
2187#SeeAlso MakeFromTexture makeTextureImage
2188
2189#Method ##
2190
2191# ------------------------------------------------------------------------------
2192
2193#Enum LegacyBitmapMode
2194#Deprecated soon
2195#Code
2196    enum LegacyBitmapMode {
2197        kRO_LegacyBitmapMode,
2198    };
2199##
2200
2201#Const kRO_LegacyBitmapMode 0
2202Returned bitmap is read-only and immutable.
2203##
2204
2205#Enum ##
2206
2207# ------------------------------------------------------------------------------
2208
2209#Method bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode = kRO_LegacyBitmapMode) const
2210#In Constructor
2211#Line # returns as Raster_Bitmap ##
2212Creates raster Bitmap with same pixels as Image. If legacyBitmapMode is
2213kRO_LegacyBitmapMode, returned bitmap is read-only and immutable.
2214Returns true if Bitmap is stored in bitmap. Returns false and resets bitmap if
2215Bitmap write did not succeed.
2216
2217#Param bitmap  storage for legacy Bitmap ##
2218#Param legacyBitmapMode  to be deprecated ##
2219
2220#Return true if Bitmap was created ##
2221
2222#Example
2223#Image 4
2224#Platform gpu
2225    SkBitmap bitImage;
2226    if (image->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2227        canvas->drawBitmap(bitImage, 0, 0);
2228    }
2229    GrContext* grContext = canvas->getGrContext();
2230    if (!grContext) {
2231        return;
2232    }
2233    sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(grContext, backEndTexture,
2234                                kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2235    canvas->drawImage(textureImage, 45, 45);
2236    if (textureImage->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2237        canvas->drawBitmap(bitImage, 90, 90);
2238    }
2239##
2240
2241#SeeAlso MakeRasterData makeRasterImage makeNonTextureImage
2242
2243#Method ##
2244
2245# ------------------------------------------------------------------------------
2246
2247#Method bool isLazyGenerated() const
2248#In Property
2249#Line # returns if Image is created as needed ##
2250Returns true if Image is backed by an image-generator or other service that creates
2251and caches its pixels or texture on-demand.
2252
2253#Return true if Image is created as needed ##
2254
2255#Example
2256#Height 80
2257#Function
2258class TestImageGenerator : public SkImageGenerator {
2259public:
2260    TestImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(10, 10)) {}
2261    ~TestImageGenerator() override {}
2262protected:
2263    bool onGetPixels(const SkImageInfo& info, void* pixelPtr, size_t rowBytes,
2264                     const Options& options) override {
2265        SkPMColor* pixels = static_cast<SkPMColor*>(pixelPtr);
2266        for (int y = 0; y < info.height(); ++y) {
2267            for (int x = 0; x < info.width(); ++x) {
2268                pixels[y * info.width() + x] = 0xff223344 + y * 0x000C0811;
2269            }
2270        }
2271        return true;
2272    }
2273};
2274##
2275void draw(SkCanvas* canvas) {
2276    auto gen = std::unique_ptr<TestImageGenerator>(new TestImageGenerator());
2277    sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
2278    SkString lazy(image->isLazyGenerated() ? "is lazy" : "not lazy");
2279    canvas->scale(8, 8);
2280    canvas->drawImage(image, 0, 0, nullptr);
2281    SkPaint paint;
2282    paint.setTextSize(4);
2283    canvas->drawString(lazy, 2, 5, paint);
2284}
2285##
2286
2287#Example
2288#Image 5
2289#Platform gpu
2290void draw(SkCanvas* canvas) {
2291    auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2292        if (nullptr == image) {
2293            return;
2294        }
2295        SkPaint paint;
2296        paint.setAntiAlias(true);
2297        paint.setTextAlign(SkPaint::kCenter_Align);
2298        canvas->drawImage(image, 0, 0);
2299        canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
2300        canvas->drawString(
2301                image->isLazyGenerated() ? "is lazily generated" : "not lazily generated",
2302                image->width() / 2, image->height() * 3 / 4, paint);
2303    };
2304    sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2305    sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2306                                kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2307    drawImage(image, "image");
2308    canvas->translate(image->width(), 0);
2309    drawImage(bitmapImage, "source");
2310    canvas->translate(-image->width(), image->height());
2311    drawImage(textureImage, "backEndTexture");
2312}
2313##
2314
2315#SeeAlso isTextureBacked MakeNonTextureImage
2316
2317#Method ##
2318
2319# ------------------------------------------------------------------------------
2320
2321#Method sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target,
2322                                  SkTransferFunctionBehavior premulBehavior) const
2323#In Constructor
2324#Line # creates Image matching Color_Space if possible ##
2325
2326Creates Image in target Color_Space.
2327Returns nullptr if Image could not be created.
2328
2329Returns original Image if it is in target Color_Space.
2330Otherwise, converts pixels from Image Color_Space to target Color_Space.
2331If Image colorSpace returns nullptr, Image Color_Space is assumed to be sRGB.
2332
2333SkTransferFunctionBehavior is to be deprecated.
2334
2335Set premulBehavior to SkTransferFunctionBehavior::kRespect to convert Image
2336pixels to a linear space, before converting to destination Color_Type
2337and Color_Space.
2338
2339Set premulBehavior to SkTransferFunctionBehavior::kIgnore to treat Image
2340pixels as linear, when converting to destination Color_Type
2341and Color_Space, ignoring pixel encoding.
2342
2343#Param target  Color_Space describing color range of returned Image ##
2344#Param premulBehavior  one of: SkTransferFunctionBehavior::kRespect,
2345                               SkTransferFunctionBehavior::kIgnore
2346##
2347
2348#Return created Image in target Color_Space ##
2349
2350#Example
2351#Image 5
2352#Set sRGB
2353    sk_sp<SkColorSpace> normalColorSpace = SkColorSpace::MakeRGB(
2354             SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kSRGB_Gamut);
2355    sk_sp<SkColorSpace> wackyColorSpace = normalColorSpace->makeColorSpin();
2356    for (auto colorSpace : { normalColorSpace, wackyColorSpace  } ) {
2357        for (auto transfer : { SkTransferFunctionBehavior::kRespect,
2358                               SkTransferFunctionBehavior::kIgnore } ) {
2359            sk_sp<SkImage> colorSpaced = image->makeColorSpace(colorSpace, transfer);
2360            canvas->drawImage(colorSpaced, 0, 0);
2361            canvas->translate(128, 0);
2362        }
2363        canvas->translate(-256, 128);
2364    }
2365##
2366
2367#SeeAlso MakeFromPixture MakeFromTexture
2368
2369#Method ##
2370
2371#Class SkImage ##
2372
2373#Topic Image ##
2374