blob: dc4d4efa4db20ea5cc6f1545e4b08a8d8db8fcab [file] [log] [blame]
Cary Clarka560c472017-11-27 10:44:06 -05001#Topic Image
2#Alias Image_Reference
3
Cary Clark08895c42018-02-01 09:37:32 -05004#Subtopic Overview
Cary Clark4855f782018-02-06 09:41:53 -05005 #Subtopic Subtopic
Cary Clark08895c42018-02-01 09:37:32 -05006 #Populate
7 ##
8##
9
Cary Clarka560c472017-11-27 10:44:06 -050010#Class SkImage
11
Cary Clark61ca7c52018-01-02 11:34:14 -050012Image describes a two dimensional array of pixels to draw. The pixels may be
Cary Clark4855f782018-02-06 09:41:53 -050013decoded in a Raster_Bitmap, encoded in a Picture or compressed data stream,
Cary Clark61ca7c52018-01-02 11:34:14 -050014or 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
Cary Clark4855f782018-02-06 09:41:53 -050024include BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP. Supported encoding details
Cary Clark61ca7c52018-01-02 11:34:14 -050025vary with platform.
26
Cary Clark08895c42018-02-01 09:37:32 -050027#Subtopic Raster_Image
Cary Clark61ca7c52018-01-02 11:34:14 -050028#Alias Raster_Image
Cary Clark4855f782018-02-06 09:41:53 -050029#Line # pixels decoded in Raster_Bitmap ##
30Raster_Image pixels are decoded in a Raster_Bitmap. These pixels may be read
Cary Clark61ca7c52018-01-02 11:34:14 -050031directly and in most cases written to, although edited pixels may not be drawn
32if Image has been copied internally.
33##
34
Cary Clark08895c42018-02-01 09:37:32 -050035#Subtopic Texture_Image
36#Line # pixels located on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -050037Texture_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
Cary Clark08895c42018-02-01 09:37:32 -050043#Subtopic Lazy_Image
44#Line # deferred pixel buffer ##
Cary Clark61ca7c52018-01-02 11:34:14 -050045Lazy_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##
Cary Clarka560c472017-11-27 10:44:06 -050049
Cary Clark4855f782018-02-06 09:41:53 -050050#Subtopic Related_Function
Cary Clark08895c42018-02-01 09:37:32 -050051#Populate
52##
Cary Clarka560c472017-11-27 10:44:06 -050053
Cary Clark4855f782018-02-06 09:41:53 -050054#Subtopic Class_or_Struct
Cary Clark08895c42018-02-01 09:37:32 -050055#Populate
Cary Clarka560c472017-11-27 10:44:06 -050056##
57
Cary Clark4855f782018-02-06 09:41:53 -050058#Subtopic Constructor
Cary Clark08895c42018-02-01 09:37:32 -050059#Populate
60##
Cary Clark5081eed2018-01-22 07:55:48 -050061
Cary Clark4855f782018-02-06 09:41:53 -050062#Subtopic Member_Function
Cary Clark08895c42018-02-01 09:37:32 -050063#Populate
64##
Cary Clarka560c472017-11-27 10:44:06 -050065
Cary Clarka560c472017-11-27 10:44:06 -050066#Typedef SkImageInfo Info
67
68##
69
Cary Clarka560c472017-11-27 10:44:06 -050070# ------------------------------------------------------------------------------
71
72#Method static sk_sp<SkImage> MakeRasterCopy(const SkPixmap& pixmap)
Cary Clark4855f782018-02-06 09:41:53 -050073#In Constructor
74#Line # creates Image from Pixmap and copied pixels ##
Cary Clark2f466242017-12-11 16:03:17 -050075Creates Image from Pixmap and copy of pixels. Since pixels are copied, Pixmap
76pixels may be modified or deleted without affecting Image.
Cary Clarka560c472017-11-27 10:44:06 -050077
Cary Clark3cd22cc2017-12-01 11:49:58 -050078Image 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 ##
Cary Clarka560c472017-11-27 10:44:06 -050088
89#Example
Cary Clark2f466242017-12-11 16:03:17 -050090#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);
Cary Clarka560c472017-11-27 10:44:06 -0500110##
111
Cary Clark3cd22cc2017-12-01 11:49:58 -0500112#SeeAlso MakeRasterData MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500113
114#Method ##
115
116# ------------------------------------------------------------------------------
117
118#Method static sk_sp<SkImage> MakeRasterData(const Info& info, sk_sp<SkData> pixels, size_t rowBytes)
Cary Clark4855f782018-02-06 09:41:53 -0500119#In Constructor
120#Line # creates Image from Image_Info and shared pixels ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500121Creates Image from Image_Info, sharing pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500122
Cary Clark3cd22cc2017-12-01 11:49:58 -0500123Image 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 ##
Cary Clarka560c472017-11-27 10:44:06 -0500135
136#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500137#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);
Cary Clarka560c472017-11-27 10:44:06 -0500147##
148
Cary Clark3cd22cc2017-12-01 11:49:58 -0500149#SeeAlso MakeRasterCopy MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500150
151#Method ##
152
153# ------------------------------------------------------------------------------
154
Cary Clark3cd22cc2017-12-01 11:49:58 -0500155#Typedef void* ReleaseContext
156
157Caller data passed to RasterReleaseProc; may be nullptr.
158
159#SeeAlso MakeFromRaster RasterReleaseProc
160
161##
162
Cary Clarka560c472017-11-27 10:44:06 -0500163#Typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext)
164
Cary Clark3cd22cc2017-12-01 11:49:58 -0500165Function 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
Cary Clarka560c472017-11-27 10:44:06 -0500170##
171
172#Method static sk_sp<SkImage> MakeFromRaster(const SkPixmap& pixmap,
173 RasterReleaseProc rasterReleaseProc,
174 ReleaseContext releaseContext)
Cary Clark4855f782018-02-06 09:41:53 -0500175#In Constructor
176#Line # creates Image from Pixmap, with release ##
Cary Clarka560c472017-11-27 10:44:06 -0500177
Cary Clark0c5f5462017-12-15 11:21:51 -0500178Creates Image from pixmap, sharing Pixmap pixels. Pixels must remain valid and
Cary Clark3cd22cc2017-12-01 11:49:58 -0500179unchanged until rasterReleaseProc is called. rasterReleaseProc is passed
180releaseContext when Image is deleted or no longer refers to pixmap pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500181
Cary Clark0c5f5462017-12-15 11:21:51 -0500182Pass 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
Cary Clark3cd22cc2017-12-01 11:49:58 -0500186Image 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 ##
Cary Clark0c5f5462017-12-15 11:21:51 -0500194#Param rasterReleaseProc function called when pixels can be released; or nullptr ##
195#Param releaseContext state passed to rasterReleaseProc; or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500196
Cary Clark0c5f5462017-12-15 11:21:51 -0500197#Return Image sharing pixmap ##
Cary Clarka560c472017-11-27 10:44:06 -0500198
199#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500200#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##
Cary Clarka560c472017-11-27 10:44:06 -0500220##
221
Cary Clark3cd22cc2017-12-01 11:49:58 -0500222#SeeAlso MakeRasterCopy MakeRasterData MakeFromGenerator RasterReleaseProc ReleaseContext
Cary Clarka560c472017-11-27 10:44:06 -0500223
224#Method ##
225
226# ------------------------------------------------------------------------------
227
228#Method static sk_sp<SkImage> MakeFromBitmap(const SkBitmap& bitmap)
Cary Clark4855f782018-02-06 09:41:53 -0500229#In Constructor
230#Line # creates Image from Bitmap, sharing or copying pixels ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500231Creates 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.
Cary Clarka560c472017-11-27 10:44:06 -0500234
Cary Clark3cd22cc2017-12-01 11:49:58 -0500235Image 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.
Cary Clarka560c472017-11-27 10:44:06 -0500241
Cary Clark3cd22cc2017-12-01 11:49:58 -0500242#Param bitmap Image_Info, row bytes, and pixels ##
243
244#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500245
246#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500247#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);
Cary Clarka560c472017-11-27 10:44:06 -0500270##
271
Cary Clark3cd22cc2017-12-01 11:49:58 -0500272#SeeAlso MakeFromRaster MakeRasterCopy MakeFromGenerator MakeRasterData
Cary Clarka560c472017-11-27 10:44:06 -0500273
274#Method ##
275
276# ------------------------------------------------------------------------------
277
278#Method static sk_sp<SkImage> MakeFromGenerator(std::unique_ptr<SkImageGenerator> imageGenerator,
279 const SkIRect* subset = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500280#In Constructor
281#Line # creates Image from a stream of data ##
Cary Clarka560c472017-11-27 10:44:06 -0500282
Cary Clark0c5f5462017-12-15 11:21:51 -0500283Creates Image from data returned by imageGenerator. Generated data is owned by Image and may not
284be shared or accessed.
Cary Clarka560c472017-11-27 10:44:06 -0500285
Cary Clark0c5f5462017-12-15 11:21:51 -0500286subset 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.
Cary Clarka560c472017-11-27 10:44:06 -0500291
Cary Clark3cd22cc2017-12-01 11:49:58 -0500292imageGenerator 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 ##
Cary Clarka560c472017-11-27 10:44:06 -0500298
299#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500300#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500301#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);
Cary Clarka560c472017-11-27 10:44:06 -0500311##
312
Cary Clark3cd22cc2017-12-01 11:49:58 -0500313#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500314
315#Method ##
316
317# ------------------------------------------------------------------------------
318
319#Method static sk_sp<SkImage> MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500320#In Constructor
321#Line # creates Image from encoded data ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500322Creates Image from encoded data.
Cary Clark0c5f5462017-12-15 11:21:51 -0500323subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
324otherwise, subset must be contained by image bounds.
Cary Clarka560c472017-11-27 10:44:06 -0500325
Cary Clark3cd22cc2017-12-01 11:49:58 -0500326Image is returned if format of the encoded data is recognized and supported.
Cary Clark4855f782018-02-06 09:41:53 -0500327Recognized formats vary by platform.
Cary Clarka560c472017-11-27 10:44:06 -0500328
Cary Clark3cd22cc2017-12-01 11:49:58 -0500329#Param encoded data of Image to decode ##
330#Param subset bounds of returned Image; may be nullptr ##
331
332#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500333
Cary Clark61ca7c52018-01-02 11:34:14 -0500334#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}
Cary Clarka560c472017-11-27 10:44:06 -0500343##
344
Cary Clark3cd22cc2017-12-01 11:49:58 -0500345#SeeAlso MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500346
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)
Cary Clarkac47b882018-01-11 10:35:44 -0500360#Deprecated
Cary Clark61ca7c52018-01-02 11:34:14 -0500361#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
Cary Clarkac47b882018-01-11 10:35:44 -0500373#Deprecated
Cary Clark61ca7c52018-01-02 11:34:14 -0500374#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
385Creates Image from GPU_Texture associated with context. Caller is responsible for
386managing the lifetime of GPU_Texture.
387
388Image is returned if format of backendTexture is recognized and supported.
389Recognized formats vary by GPU back-end.
390
391#Param context GPU_Context ##
392#Param backendTexture texture residing on GPU ##
393#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
394#Param colorType one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
395 kRGB_565_SkColorType, kARGB_4444_SkColorType,
396 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
397 kGray_8_SkColorType, kRGBA_F16_SkColorType
398##
399#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
400 kPremul_SkAlphaType, kUnpremul_SkAlphaType
401##
402#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500403
404#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500405
406#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500407#Image 3
408#Platform gpu
409#Height 128
410#Description
411A back-end texture has been created and uploaded to the GPU outside of this example.
412##
413GrContext* context = canvas->getGrContext();
414if (!context) {
415 return;
416}
417canvas->scale(.25f, .25f);
418int x = 0;
419for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500420 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark56356312018-02-08 14:45:18 -0500421 origin, kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
Cary Clark0c5f5462017-12-15 11:21:51 -0500422 canvas->drawImage(image, x, 0);
423x += 512;
424}
Cary Clarka560c472017-11-27 10:44:06 -0500425##
426
Cary Clark3cd22cc2017-12-01 11:49:58 -0500427#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500428
429#Method ##
430
431# ------------------------------------------------------------------------------
432
433#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
434 const GrBackendTexture& backendTexture,
435 GrSurfaceOrigin origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500436 SkColorType colorType,
Cary Clarka560c472017-11-27 10:44:06 -0500437 SkAlphaType alphaType,
438 sk_sp<SkColorSpace> colorSpace,
439 TextureReleaseProc textureReleaseProc,
440 ReleaseContext releaseContext)
441
Cary Clark61ca7c52018-01-02 11:34:14 -0500442Creates Image from GPU_Texture associated with context. GPU_Texture must stay
Cary Clark3cd22cc2017-12-01 11:49:58 -0500443valid and unchanged until textureReleaseProc is called. textureReleaseProc is
444passed releaseContext when Image is deleted or no longer refers to texture.
Cary Clarka560c472017-11-27 10:44:06 -0500445
Cary Clark3cd22cc2017-12-01 11:49:58 -0500446Image is returned if format of backendTexture is recognized and supported.
447Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500448
Cary Clark3cd22cc2017-12-01 11:49:58 -0500449#Param context GPU_Context ##
450#Param backendTexture texture residing on GPU ##
451#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500452#Param colorType one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
453 kRGB_565_SkColorType, kARGB_4444_SkColorType,
454 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
455 kGray_8_SkColorType, kRGBA_F16_SkColorType
456##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500457#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
458 kPremul_SkAlphaType, kUnpremul_SkAlphaType
459##
Cary Clark61ca7c52018-01-02 11:34:14 -0500460#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500461#Param textureReleaseProc function called when texture can be released ##
462#Param releaseContext state passed to textureReleaseProc ##
463
464#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500465
Cary Clark0c5f5462017-12-15 11:21:51 -0500466#ToDo
467This doesn't do anything clever with TextureReleaseProc because it may not get called
Cary Clark61ca7c52018-01-02 11:34:14 -0500468fwithin the lifetime of the example
Cary Clark0c5f5462017-12-15 11:21:51 -0500469##
470
Cary Clarka560c472017-11-27 10:44:06 -0500471#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500472#Platform gpu
473#Image 4
Cary Clark0c5f5462017-12-15 11:21:51 -0500474GrContext* context = canvas->getGrContext();
475if (!context) {
476 return;
477}
Cary Clarkac47b882018-01-11 10:35:44 -0500478auto debugster = [](SkImage::ReleaseContext releaseContext) -> void {
479 *((int *) releaseContext) += 128;
Cary Clark0c5f5462017-12-15 11:21:51 -0500480};
481int x = 0;
482for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500483 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark61ca7c52018-01-02 11:34:14 -0500484 origin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr, debugster, &x);
Cary Clark0c5f5462017-12-15 11:21:51 -0500485 canvas->drawImage(image, x, 0);
486 x += 128;
487}
Cary Clarka560c472017-11-27 10:44:06 -0500488##
489
Cary Clark3cd22cc2017-12-01 11:49:58 -0500490#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500491
492#Method ##
493
494# ------------------------------------------------------------------------------
495
496#Method static sk_sp<SkImage> MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> data,
497 bool buildMips,
498 SkColorSpace* dstColorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500499#In Constructor
500#Line # creates Image from encoded data, and uploads to GPU ##
Cary Clarka560c472017-11-27 10:44:06 -0500501
Cary Clark3cd22cc2017-12-01 11:49:58 -0500502Creates Image from encoded data. Image is uploaded to GPU back-end using context.
503
504Created Image is available to other GPU contexts, and is available across thread
505boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
506share resources.
507
508When Image is no longer referenced, context releases texture memory
Cary Clarka560c472017-11-27 10:44:06 -0500509asynchronously.
Cary Clarka560c472017-11-27 10:44:06 -0500510
Cary Clark3cd22cc2017-12-01 11:49:58 -0500511Texture decoded from data is uploaded to match Surface created with
512dstColorSpace. Color_Space of Image is determined by encoded data.
Cary Clarka560c472017-11-27 10:44:06 -0500513
Cary Clark3cd22cc2017-12-01 11:49:58 -0500514Image is returned if format of data is recognized and supported, and if context
515supports moving resources. Recognized formats vary by platform and GPU back-end.
516
Cary Clark61ca7c52018-01-02 11:34:14 -0500517Image is returned using MakeFromEncoded if context is nullptr or does not support
518moving resources between contexts.
519
Cary Clark3cd22cc2017-12-01 11:49:58 -0500520#Param context GPU_Context ##
521#Param data Image to decode ##
522#Param buildMips create Image as Mip_Map if true ##
523#Param dstColorSpace range of colors of matching Surface on GPU ##
524
525#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500526
527#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500528#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500529#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500530GrContext* context = canvas->getGrContext();
531sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, 100);
532sk_sp<SkImage> image = SkImage::MakeCrossContextFromEncoded(context,
533 encodedData, false, nullptr);
534canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500535##
536
Cary Clark3cd22cc2017-12-01 11:49:58 -0500537#SeeAlso MakeCrossContextFromPixmap
538
539#Method ##
540
541# ------------------------------------------------------------------------------
542
543#Method static sk_sp<SkImage> MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap,
544 bool buildMips,
545 SkColorSpace* dstColorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500546#In Constructor
547#Line # creates Image from Pixmap, and uploads to GPU ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500548
549Creates Image from pixmap. Image is uploaded to GPU back-end using context.
550
551Created Image is available to other GPU contexts, and is available across thread
552boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
553share resources.
554
555When Image is no longer referenced, context releases texture memory
556asynchronously.
557
558Texture created from pixmap is uploaded to match Surface created with
559dstColorSpace. Color_Space of Image is determined by pixmap.colorSpace().
560
Cary Clark61ca7c52018-01-02 11:34:14 -0500561Image is returned referring to GPU back-end if context is not nullptr,
562format of data is recognized and supported, and if context supports moving
563resources between contexts. Otherwise, pixmap pixel data is copied and Image
564as returned in raster format if possible; nullptr may be returned.
565Recognized GPU formats vary by platform and GPU back-end.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500566
567#Param context GPU_Context ##
568#Param pixmap Image_Info, pixel address, and row bytes ##
569#Param buildMips create Image as Mip_Map if true ##
570#Param dstColorSpace range of colors of matching Surface on GPU ##
571
572#Return created Image, or nullptr ##
573
574#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500575#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500576#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500577GrContext* context = canvas->getGrContext();
578SkPixmap pixmap;
579if (source.peekPixels(&pixmap)) {
580 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(context, pixmap,
581 false, nullptr);
582 canvas->drawImage(image, 0, 0);
583}
Cary Clark3cd22cc2017-12-01 11:49:58 -0500584##
585
586#SeeAlso MakeCrossContextFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500587
588#Method ##
589
590# ------------------------------------------------------------------------------
591
592#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
593 const GrBackendTexture& backendTexture,
594 GrSurfaceOrigin surfaceOrigin,
595 SkAlphaType alphaType = kPremul_SkAlphaType,
596 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500597#Deprecated
Cary Clark61ca7c52018-01-02 11:34:14 -0500598#Method ##
599
600# ------------------------------------------------------------------------------
601
602#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
603 const GrBackendTexture& backendTexture,
604 GrSurfaceOrigin surfaceOrigin,
605 SkColorType colorType,
606 SkAlphaType alphaType = kPremul_SkAlphaType,
607 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500608#In Constructor
609#Line # creates Image from GPU_Texture, managed internally ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500610Creates Image from backendTexture associated with context. backendTexture and
611returned Image are managed internally, and are released when no longer needed.
Cary Clarka560c472017-11-27 10:44:06 -0500612
Cary Clark3cd22cc2017-12-01 11:49:58 -0500613Image is returned if format of backendTexture is recognized and supported.
614Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500615
Cary Clark3cd22cc2017-12-01 11:49:58 -0500616#Param context GPU_Context ##
617#Param backendTexture texture residing on GPU ##
618#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500619#Param colorType one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
620 kRGB_565_SkColorType, kARGB_4444_SkColorType,
621 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
622 kGray_8_SkColorType, kRGBA_F16_SkColorType
623##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500624#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
625 kPremul_SkAlphaType, kUnpremul_SkAlphaType
626##
Cary Clark61ca7c52018-01-02 11:34:14 -0500627#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500628
629#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500630
631#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500632#Image 5
633#Platform gpu
Cary Clark61ca7c52018-01-02 11:34:14 -0500634 if (!canvas->getGrContext()) {
635 return;
636 }
637 canvas->scale(.5f, .5f);
638 canvas->clear(0x7f3f5f7f);
639 int x = 0, y = 0;
640 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
641 for (auto alpha : { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType } ) {
642 sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(canvas->getGrContext(),
643 backEndTexture, origin,
644 kRGBA_8888_SkColorType, alpha);
645 canvas->drawImage(image, x, y);
646 x += 160;
647 }
648 x -= 160 * 3;
649 y += 256;
650 }
Cary Clarka560c472017-11-27 10:44:06 -0500651##
652
Cary Clark61ca7c52018-01-02 11:34:14 -0500653#SeeAlso MakeFromTexture MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500654
655#Method ##
656
657# ------------------------------------------------------------------------------
658
659#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
660 const GrBackendObject yuvTextureHandles[3],
661 const SkISize yuvSizes[3],
662 GrSurfaceOrigin surfaceOrigin,
663 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500664#In Constructor
665#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500666
Cary Clark61ca7c52018-01-02 11:34:14 -0500667Creates Image from copy of yuvTextureHandles, an array of textures on GPU.
668yuvTextureHandles contain pixels for YUV planes of Image.
Cary Clark4855f782018-02-06 09:41:53 -0500669yuvSizes contain dimensions for each pixel plane. Dimensions must be greater than
Cary Clark61ca7c52018-01-02 11:34:14 -0500670zero but may differ from plane to plane. Returned Image has the dimensions
671yuvSizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500672
Cary Clark61ca7c52018-01-02 11:34:14 -0500673#Param context GPU_Context ##
674#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
675 kRec709_SkYUVColorSpace
676##
677#Param yuvTextureHandles array of YUV textures on GPU ##
678#Param yuvSizes dimensions of YUV textures ##
679#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
680#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500681
Cary Clark61ca7c52018-01-02 11:34:14 -0500682#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500683
Cary Clark61ca7c52018-01-02 11:34:14 -0500684# seems too complicated to create an example for this
685#ToDo
686should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500687##
688
Cary Clark61ca7c52018-01-02 11:34:14 -0500689#NoExample
690##
691
692#SeeAlso MakeFromNV12TexturesCopy
693
694#Method ##
695
696# ------------------------------------------------------------------------------
697
698#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
699 const GrBackendTexture yuvTextureHandles[3],
700 const SkISize yuvSizes[3],
701 GrSurfaceOrigin surfaceOrigin,
702 sk_sp<SkColorSpace> colorSpace = nullptr)
703
704Creates Image from copy of yuvTextureHandles, an array of textures on GPU.
705yuvTextureHandles contain pixels for YUV planes of Image.
Cary Clark4855f782018-02-06 09:41:53 -0500706yuvSizes contain dimensions for each pixel plane. Dimensions must be greater than
Cary Clark61ca7c52018-01-02 11:34:14 -0500707zero but may differ from plane to plane. Returned Image has the dimensions
708yuvSizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
709
710#Param context GPU_Context ##
711#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
712 kRec709_SkYUVColorSpace
713##
714#Param yuvTextureHandles array of YUV textures on GPU ##
715#Param yuvSizes dimensions of YUV textures ##
716#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
717#Param colorSpace range of colors; may be nullptr ##
718
719#Return created Image, or nullptr ##
720
721# seems too complicated to create an example for this
722#ToDo
723should this be moved to chrome only?
724##
725
726#NoExample
727##
728
729#SeeAlso MakeFromNV12TexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500730
731#Method ##
732
733# ------------------------------------------------------------------------------
734
735#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
736 SkYUVColorSpace yuvColorSpace,
737 const GrBackendObject nv12TextureHandles[2],
738 const SkISize nv12Sizes[2],
739 GrSurfaceOrigin surfaceOrigin,
740 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500741#In Constructor
742#Line # creates Image from YUV_ColorSpace data in two planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500743
Cary Clark61ca7c52018-01-02 11:34:14 -0500744Creates Image from copy of nv12TextureHandles, an array of textures on GPU.
745nv12TextureHandles[0] contains pixels for YUV_Component_Y plane.
746nv12TextureHandles[1] contains pixels for YUV_Component_U plane,
747followed by pixels for YUV_Component_V plane.
Cary Clark4855f782018-02-06 09:41:53 -0500748nv12Sizes contain dimensions for each pixel plane. Dimensions must be greater than
Cary Clark61ca7c52018-01-02 11:34:14 -0500749zero but may differ from plane to plane. Returned Image has the dimensions
750nv12Sizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500751
Cary Clark61ca7c52018-01-02 11:34:14 -0500752#Param context GPU_Context ##
753#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
754 kRec709_SkYUVColorSpace
755##
756#Param nv12TextureHandles array of YUV textures on GPU ##
757#Param nv12Sizes dimensions of YUV textures ##
758#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
759#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500760
Cary Clark61ca7c52018-01-02 11:34:14 -0500761#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500762
Cary Clark61ca7c52018-01-02 11:34:14 -0500763# seems too complicated to create an example for this
764#ToDo
765should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500766##
767
Cary Clark61ca7c52018-01-02 11:34:14 -0500768#NoExample
769##
770
771#SeeAlso MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500772
773#Method ##
774
775# ------------------------------------------------------------------------------
776
Cary Clark61ca7c52018-01-02 11:34:14 -0500777#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
778 SkYUVColorSpace yuvColorSpace,
779 const GrBackendTexture nv12TextureHandles[2],
780 const SkISize nv12Sizes[2],
781 GrSurfaceOrigin surfaceOrigin,
782 sk_sp<SkColorSpace> colorSpace = nullptr)
783
784Creates Image from copy of nv12TextureHandles, an array of textures on GPU.
785nv12TextureHandles[0] contains pixels for YUV_Component_Y plane.
786nv12TextureHandles[1] contains pixels for YUV_Component_U plane,
787followed by pixels for YUV_Component_V plane.
Cary Clark4855f782018-02-06 09:41:53 -0500788nv12Sizes contain dimensions for each pixel plane. Dimensions must be greater than
Cary Clark61ca7c52018-01-02 11:34:14 -0500789zero but may differ from plane to plane. Returned Image has the dimensions
790nv12Sizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
791
792#Param context GPU_Context ##
793#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
794 kRec709_SkYUVColorSpace
795##
796#Param nv12TextureHandles array of YUV textures on GPU ##
797#Param nv12Sizes dimensions of YUV textures ##
798#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
799#Param colorSpace range of colors; may be nullptr ##
800
801#Return created Image, or nullptr ##
802
803# seems too complicated to create an example for this
804#ToDo
805should this be moved to chrome only?
806##
807
808#NoExample
809##
810
811#SeeAlso MakeFromYUVTexturesCopy
812
813#Method ##
814
815# ------------------------------------------------------------------------------
816
Cary Clark4855f782018-02-06 09:41:53 -0500817# currently uncalled by any test or client ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500818#Bug 7424
Cary Clark61ca7c52018-01-02 11:34:14 -0500819
Cary Clark56356312018-02-08 14:45:18 -0500820#EnumClass BitDepth
Cary Clarka560c472017-11-27 10:44:06 -0500821
822#Code
Cary Clark61ca7c52018-01-02 11:34:14 -0500823 enum class BitDepth {
Cary Clarka560c472017-11-27 10:44:06 -0500824 kU8,
825 kF16,
826 };
827##
828
829#Const kU8 0
Cary Clark61ca7c52018-01-02 11:34:14 -0500830Use 8 bits per Color_ARGB component using unsigned integer format.
Cary Clarka560c472017-11-27 10:44:06 -0500831##
832#Const kF16 1
Cary Clark61ca7c52018-01-02 11:34:14 -0500833Use 16 bits per Color_ARGB component using half-precision floating point format.
Cary Clarka560c472017-11-27 10:44:06 -0500834##
835
Cary Clark61ca7c52018-01-02 11:34:14 -0500836#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500837##
838
Cary Clark61ca7c52018-01-02 11:34:14 -0500839#SeeAlso MakeFromPicture
Cary Clarka560c472017-11-27 10:44:06 -0500840
Cary Clark56356312018-02-08 14:45:18 -0500841#EnumClass ##
Cary Clarka560c472017-11-27 10:44:06 -0500842
843# ------------------------------------------------------------------------------
844
845#Method static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
846 const SkMatrix* matrix, const SkPaint* paint,
847 BitDepth bitDepth,
848 sk_sp<SkColorSpace> colorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500849#In Constructor
850#Line # creates Image from Picture ##
Cary Clarka560c472017-11-27 10:44:06 -0500851
Cary Clark61ca7c52018-01-02 11:34:14 -0500852Creates Image from picture. Returned Image width and height are set by dimensions.
853Image draws picture with matrix and paint, set to bitDepth and colorSpace.
Cary Clarka560c472017-11-27 10:44:06 -0500854
Cary Clark61ca7c52018-01-02 11:34:14 -0500855If matrix is nullptr, draws with identity Matrix. If paint is nullptr, draws
856with default Paint. colorSpace may be nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500857
Cary Clark61ca7c52018-01-02 11:34:14 -0500858#Param picture stream of drawing commands ##
859#Param dimensions width and height ##
860#Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ##
861#Param paint Paint to apply transparency, filtering, and so on; may be nullptr ##
862#Param bitDepth 8 bit integer or 16 bit float: per component ##
863#Param colorSpace range of colors; may be nullptr ##
864
865#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500866
867#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500868 SkPaint paint;
869 SkPictureRecorder recorder;
870 SkCanvas* recordingCanvas = recorder.beginRecording(50, 50);
871 for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) {
872 paint.setColor(color);
873 recordingCanvas->drawRect({10, 10, 30, 40}, paint);
874 recordingCanvas->translate(10, 10);
875 recordingCanvas->scale(1.2f, 1.4f);
876 }
877 sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
878 int x = 0, y = 0;
879 for (auto alpha : { 70, 140, 210 } ) {
880 paint.setAlpha(alpha);
881 auto srgbColorSpace = SkColorSpace::MakeSRGB();
882 sk_sp<SkImage> image = SkImage::MakeFromPicture(playback, {50, 50}, nullptr, &paint,
883 SkImage::BitDepth::kU8, srgbColorSpace);
884 canvas->drawImage(image, x, y);
885 x += 70; y += 70;
886 }
Cary Clarka560c472017-11-27 10:44:06 -0500887##
888
Cary Clark61ca7c52018-01-02 11:34:14 -0500889#SeeAlso SkCanvas::drawPicture
Cary Clarka560c472017-11-27 10:44:06 -0500890
891#Method ##
892
893# ------------------------------------------------------------------------------
894
895#Method static sk_sp<SkImage> MakeFromAHardwareBuffer(AHardwareBuffer* hardwareBuffer,
896 SkAlphaType alphaType = kPremul_SkAlphaType,
897 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500898#In Constructor
899#Line # creates Image from Android hardware buffer ##
Cary Clarka560c472017-11-27 10:44:06 -0500900
Cary Clark4855f782018-02-06 09:41:53 -0500901#Bug 7447
Cary Clarka560c472017-11-27 10:44:06 -0500902
Cary Clark61ca7c52018-01-02 11:34:14 -0500903Creates Image from Android hardware buffer.
904Returned Image takes a reference on the buffer.
Cary Clarka560c472017-11-27 10:44:06 -0500905
Cary Clark61ca7c52018-01-02 11:34:14 -0500906Only available on Android, when __ANDROID_API__ is defined to be 26 or greater.
Cary Clarka560c472017-11-27 10:44:06 -0500907
Cary Clark61ca7c52018-01-02 11:34:14 -0500908#Param hardwareBuffer AHardwareBuffer Android hardware buffer ##
909#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
910 kPremul_SkAlphaType, kUnpremul_SkAlphaType
911##
912#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500913
Cary Clark61ca7c52018-01-02 11:34:14 -0500914#Return created Image, or nullptr ##
915
916#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500917##
918
Cary Clark61ca7c52018-01-02 11:34:14 -0500919#SeeAlso MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -0500920
921#Method ##
922
923# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -0500924#Subtopic Property
925#Populate
926#Line # values and attributes ##
927##
Cary Clarka560c472017-11-27 10:44:06 -0500928
929#Method int width() const
Cary Clark4855f782018-02-06 09:41:53 -0500930#In Property
931#Line # returns pixel column count ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500932Returns pixel count in each row.
933
934#Return pixel width in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500935
936#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500937#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500938#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500939 canvas->translate(10, 10);
940 canvas->drawImage(image, 0, 0);
941 canvas->translate(0, image->height());
942 SkPaint paint;
943 paint.setTextAlign(SkPaint::kCenter_Align);
944 canvas->drawLine(0, 10, image->width(), 10, paint);
945 canvas->drawString("width", image->width() / 2, 25, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500946##
947
Cary Clark61ca7c52018-01-02 11:34:14 -0500948#SeeAlso dimensions() height()
Cary Clarka560c472017-11-27 10:44:06 -0500949
950#Method ##
951
952# ------------------------------------------------------------------------------
953
954#Method int height() const
Cary Clark4855f782018-02-06 09:41:53 -0500955#In Property
956#Line # returns pixel row count ##
Cary Clark2f466242017-12-11 16:03:17 -0500957Returns pixel row count.
958
Cary Clark61ca7c52018-01-02 11:34:14 -0500959#Return pixel height in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500960
961#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500962#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500963#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500964 canvas->translate(10, 10);
965 canvas->drawImage(image, 0, 0);
966 canvas->translate(image->width(), 0);
967 SkPaint paint;
968 paint.setTextAlign(SkPaint::kCenter_Align);
969 paint.setVerticalText(true);
970 canvas->drawLine(10, 0, 10, image->height(), paint);
Cary Clarkac47b882018-01-11 10:35:44 -0500971 canvas->drawString("height", 25, image->height() / 2, paint);
972##
Cary Clarka560c472017-11-27 10:44:06 -0500973
Cary Clark61ca7c52018-01-02 11:34:14 -0500974#SeeAlso dimensions() width()
Cary Clarka560c472017-11-27 10:44:06 -0500975
976#Method ##
977
978# ------------------------------------------------------------------------------
979
980#Method SkISize dimensions() const
Cary Clark4855f782018-02-06 09:41:53 -0500981#In Property
982#Line # returns width() and height() ##
Cary Clark2f466242017-12-11 16:03:17 -0500983Returns ISize { width(), height() }.
984
985#Return integral size of width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -0500986
987#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500988#Image 4
989 SkISize dimensions = image->dimensions();
990 SkIRect bounds = image->bounds();
991 SkIRect dimensionsAsBounds = SkIRect::MakeSize(dimensions);
992 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clarka560c472017-11-27 10:44:06 -0500993##
994
Cary Clark61ca7c52018-01-02 11:34:14 -0500995#SeeAlso height() width() bounds()
Cary Clarka560c472017-11-27 10:44:06 -0500996
997#Method ##
998
999# ------------------------------------------------------------------------------
1000
1001#Method SkIRect bounds() const
Cary Clark4855f782018-02-06 09:41:53 -05001002#In Property
1003#Line # returns width() and height() as Rectangle ##
Cary Clark2f466242017-12-11 16:03:17 -05001004Returns IRect { 0, 0, width(), height() }.
1005
1006#Return integral rectangle from origin to width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -05001007
1008#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001009#Height 128
1010#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -05001011 SkIRect bounds = image->bounds();
Cary Clarkac47b882018-01-11 10:35:44 -05001012 for (int x : { 0, bounds.width() } ) {
1013 for (int y : { 0, bounds.height() } ) {
Cary Clark61ca7c52018-01-02 11:34:14 -05001014 canvas->drawImage(image, x, y);
1015 }
1016 }
Cary Clarka560c472017-11-27 10:44:06 -05001017##
1018
Cary Clark61ca7c52018-01-02 11:34:14 -05001019#SeeAlso dimensions()
Cary Clarka560c472017-11-27 10:44:06 -05001020
1021#Method ##
1022
1023# ------------------------------------------------------------------------------
1024
1025#Method uint32_t uniqueID() const
Cary Clark4855f782018-02-06 09:41:53 -05001026#In Property
1027#Line # identifier for Image ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001028Returns value unique to image. Image contents cannot change after Image is
1029created. Any operation to create a new Image will receive generate a new
1030unique number.
1031
1032#Return unique identifier ##
Cary Clarka560c472017-11-27 10:44:06 -05001033
1034#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001035#Image 5
1036#Height 156
1037 sk_sp<SkImage> subset = image->makeSubset({10, 20, 90, 100});
1038 canvas->drawImage(image, 0, 0);
1039 canvas->drawImage(subset, 128, 0);
1040 SkPaint paint;
1041 SkString s;
1042 s.printf("original id: %d", image->uniqueID());
1043 canvas->drawString(s, 20, image->height() + 20, paint);
1044 s.printf("subset id: %d", subset->uniqueID());
1045 canvas->drawString(s, 148, subset->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001046##
1047
Cary Clark61ca7c52018-01-02 11:34:14 -05001048#SeeAlso isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001049
1050#Method ##
1051
1052# ------------------------------------------------------------------------------
1053
1054#Method SkAlphaType alphaType() const
Cary Clark4855f782018-02-06 09:41:53 -05001055#In Property
1056#Line # returns Alpha_Type ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001057Returns Alpha_Type, one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
1058kPremul_SkAlphaType, kUnpremul_SkAlphaType.
1059
1060Alpha_Type returned was a parameter to an Image constructor,
1061or was parsed from encoded data.
1062
1063#Return Alpha_Type in Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001064
1065#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001066#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -05001067#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -05001068 const char* alphaTypeStr[] = { "Unknown", "Opaque", "Premul", "Unpremul" };
1069 SkAlphaType alphaType = image->alphaType();
Cary Clarkac47b882018-01-11 10:35:44 -05001070 canvas->drawImage(image, 16, 0);
Cary Clark61ca7c52018-01-02 11:34:14 -05001071 SkPaint paint;
1072 canvas->drawString(alphaTypeStr[(int) alphaType], 20, image->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001073##
1074
Cary Clark61ca7c52018-01-02 11:34:14 -05001075#SeeAlso SkImageInfo::alphaType
Cary Clarka560c472017-11-27 10:44:06 -05001076
1077#Method ##
1078
1079# ------------------------------------------------------------------------------
1080
1081#Method SkColorSpace* colorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001082#In Property
1083#Line # returns Color_Space ##
Cary Clark2f466242017-12-11 16:03:17 -05001084Returns Color_Space, the range of colors, associated with Image. The
1085reference count of Color_Space is unchanged. The returned Color_Space is
1086immutable.
Cary Clarka560c472017-11-27 10:44:06 -05001087
Cary Clark61dfc3a2018-01-03 08:37:53 -05001088Color_Space returned was passed to an Image constructor,
1089or was parsed from encoded data. Color_Space returned may be ignored when Image
1090is drawn, depending on the capabilities of the Surface receiving the drawing.
Cary Clark2f466242017-12-11 16:03:17 -05001091
1092#Return Color_Space in Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001093
1094#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001095#Image 3
1096#Set sRGB
1097 SkPixmap pixmap;
1098 source.peekPixels(&pixmap);
1099 canvas->scale(.25f, .25f);
1100 int y = 0;
1101 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1102 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1103 int x = 0;
1104 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1105 for (int index = 0; index < 2; ++index) {
1106 pixmap.setColorSpace(colorSpace);
1107 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1108 canvas->drawImage(image, x, y);
1109 colorSpace = image->colorSpace()->makeColorSpin();
1110 x += 512;
1111 }
1112 y += 512;
1113 }
Cary Clarka560c472017-11-27 10:44:06 -05001114##
1115
Cary Clark61dfc3a2018-01-03 08:37:53 -05001116#SeeAlso refColorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001117
1118#Method ##
1119
1120# ------------------------------------------------------------------------------
1121
1122#Method sk_sp<SkColorSpace> refColorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001123#In Property
1124#Line # returns Image_Info Color_Space ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001125Returns a smart pointer to Color_Space, the range of colors, associated with
1126Image. The smart pointer tracks the number of objects sharing this
1127SkColorSpace reference so the memory is released when the owners destruct.
1128
1129The returned SkColorSpace is immutable.
1130
1131Color_Space returned was passed to an Image constructor,
1132or was parsed from encoded data. Color_Space returned may be ignored when Image
1133is drawn, depending on the capabilities of the Surface receiving the drawing.
1134
1135#Return Color_Space in Image, or nullptr, wrapped in a smart pointer ##
Cary Clarka560c472017-11-27 10:44:06 -05001136
1137#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001138#Image 3
1139#Set sRGB
1140 SkPixmap pixmap;
1141 source.peekPixels(&pixmap);
1142 canvas->scale(.25f, .25f);
1143 int y = 0;
1144 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1145 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1146 int x = 0;
1147 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1148 for (int index = 0; index < 2; ++index) {
1149 pixmap.setColorSpace(colorSpace);
1150 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1151 canvas->drawImage(image, x, y);
1152 colorSpace = image->refColorSpace()->makeColorSpin();
1153 x += 512;
1154 }
1155 y += 512;
1156 }
Cary Clarka560c472017-11-27 10:44:06 -05001157##
1158
Cary Clark61dfc3a2018-01-03 08:37:53 -05001159#SeeAlso colorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001160
1161#Method ##
1162
1163# ------------------------------------------------------------------------------
1164
1165#Method bool isAlphaOnly() const
Cary Clark4855f782018-02-06 09:41:53 -05001166#In Property
1167#Line # returns if pixels represent a transparency mask ##
Cary Clark2f466242017-12-11 16:03:17 -05001168Returns true if Image pixels represent transparency only. If true, each pixel
1169is packed in 8 bits as defined by kAlpha_8_SkColorType.
Cary Clarka560c472017-11-27 10:44:06 -05001170
Cary Clark2f466242017-12-11 16:03:17 -05001171#Return true if pixels represent a transparency mask ##
Cary Clarka560c472017-11-27 10:44:06 -05001172
1173#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001174 uint8_t pmColors = 0;
1175 sk_sp<SkImage> image = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1), &pmColors, 1});
1176 SkDebugf("alphaOnly = %s\n", image->isAlphaOnly() ? "true" : "false");
1177#StdOut
1178alphaOnly = true
1179##
Cary Clarka560c472017-11-27 10:44:06 -05001180##
1181
Cary Clark61dfc3a2018-01-03 08:37:53 -05001182#SeeAlso alphaType isOpaque
Cary Clarka560c472017-11-27 10:44:06 -05001183
1184#Method ##
1185
1186# ------------------------------------------------------------------------------
1187
1188#Method bool isOpaque() const
Cary Clark4855f782018-02-06 09:41:53 -05001189#In Property
1190#Line # returns if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001191Returns true if pixels ignore their Alpha value and are treated as fully opaque.
Cary Clark2f466242017-12-11 16:03:17 -05001192
1193#Return true if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clarka560c472017-11-27 10:44:06 -05001194
1195#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001196 auto check_isopaque = [](const SkImageInfo& imageInfo) -> void {
1197 auto surface(SkSurface::MakeRaster(imageInfo));
1198 auto image(surface->makeImageSnapshot());
1199 SkDebugf("isOpaque = %s\n", image->isOpaque() ? "true" : "false");
1200 };
1201
1202 check_isopaque(SkImageInfo::MakeN32Premul(5, 5));
1203 check_isopaque(SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType));
1204#StdOut
1205isOpaque = false
1206isOpaque = true
1207##
Cary Clarka560c472017-11-27 10:44:06 -05001208##
1209
Cary Clark61dfc3a2018-01-03 08:37:53 -05001210#SeeAlso alphaType isAlphaOnly
Cary Clarka560c472017-11-27 10:44:06 -05001211
1212#Method ##
1213
1214# ------------------------------------------------------------------------------
1215
1216#Method sk_sp<SkShader> makeShader(SkShader::TileMode tileMode1, SkShader::TileMode tileMode2,
1217 const SkMatrix* localMatrix = nullptr) const
Cary Clark4855f782018-02-06 09:41:53 -05001218#In Constructor
1219#Line # creates Shader, Paint element that can tile Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001220
Cary Clark61dfc3a2018-01-03 08:37:53 -05001221Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1222SkShader::TileMode rules to fill drawn area outside Image. localMatrix permits
1223transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001224
Cary Clark61dfc3a2018-01-03 08:37:53 -05001225#Param tileMode1 tiling in x, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1226 SkShader::kMirror_TileMode
1227##
1228#Param tileMode2 tiling in y, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1229 SkShader::kMirror_TileMode
1230##
1231#Param localMatrix Image transformation, or nullptr ##
1232
1233#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001234
1235#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001236#Image 4
1237SkMatrix matrix;
1238matrix.setRotate(45);
1239SkPaint paint;
1240paint.setShader(image->makeShader(SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode,
1241 &matrix));
1242canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001243##
1244
Cary Clark61dfc3a2018-01-03 08:37:53 -05001245#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001246
1247#Method ##
1248
1249# ------------------------------------------------------------------------------
1250
1251#Method sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const
1252
Cary Clark61dfc3a2018-01-03 08:37:53 -05001253Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1254SkShader::kClamp_TileMode to fill drawn area outside Image. localMatrix permits
1255transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001256
Cary Clark61dfc3a2018-01-03 08:37:53 -05001257#Param localMatrix Image transformation, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001258
Cary Clark61dfc3a2018-01-03 08:37:53 -05001259#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001260
1261#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001262#Image 5
1263SkMatrix matrix;
1264matrix.setRotate(45);
1265matrix.postTranslate(125, 30);
1266SkPaint paint;
1267paint.setShader(image->makeShader(&matrix));
1268canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001269##
1270
Cary Clarkf5404bb2018-01-05 12:10:09 -05001271#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001272
1273#Method ##
1274
1275# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001276#Subtopic Pixels
1277#Populate
1278#Line # read and write pixel values ##
1279##
Cary Clarka560c472017-11-27 10:44:06 -05001280
1281#Method bool peekPixels(SkPixmap* pixmap) const
Cary Clark78de7512018-02-07 07:27:09 -05001282#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001283#Line # returns Pixmap if possible ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001284Copies Image pixel address, row bytes, and Image_Info to pixmap, if address
1285is available, and returns true. If pixel address is not available, return
1286false and leave pixmap unchanged.
Cary Clarka560c472017-11-27 10:44:06 -05001287
Cary Clarkf5404bb2018-01-05 12:10:09 -05001288#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
Cary Clarka560c472017-11-27 10:44:06 -05001289
Cary Clarkf5404bb2018-01-05 12:10:09 -05001290#Return true if Image has direct access to pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001291
1292#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001293 SkBitmap bitmap;
1294 bitmap.allocPixels(SkImageInfo::MakeN32Premul(12, 11));
1295 SkCanvas offscreen(bitmap);
1296 offscreen.clear(SK_ColorWHITE);
1297 SkPaint paint;
1298 offscreen.drawString("%", 1, 10, paint);
1299 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
1300 SkPixmap pixmap;
1301 if (image->peekPixels(&pixmap)) {
1302 const SkPMColor* pixels = pixmap.addr32();
1303 SkPMColor pmWhite = pixels[0];
1304 for (int y = 0; y < image->height(); ++y) {
1305 for (int x = 0; x < image->width(); ++x) {
1306 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
1307 }
1308 SkDebugf("\n");
1309 }
1310 }
1311#StdOut
1312------------
1313--xx----x---
1314-x--x--x----
1315-x--x--x----
1316-x--x-x-----
1317--xx-xx-xx--
1318-----x-x--x-
1319----x--x--x-
1320----x--x--x-
1321---x----xx--
1322------------
1323##
Cary Clarka560c472017-11-27 10:44:06 -05001324##
1325
Cary Clarkf5404bb2018-01-05 12:10:09 -05001326#SeeAlso readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001327
1328#Method ##
1329
1330# ------------------------------------------------------------------------------
1331
1332#Method GrTexture* getTexture() const
Cary Clark2f466242017-12-11 16:03:17 -05001333#Deprecated
Cary Clarka560c472017-11-27 10:44:06 -05001334#Method ##
1335
1336# ------------------------------------------------------------------------------
1337
1338#Method bool isTextureBacked() const
Cary Clark78de7512018-02-07 07:27:09 -05001339#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001340#Line # returns if Image was created from GPU_Texture ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001341Returns true the contents of Image was created on or uploaded to GPU memory,
1342and is available as a GPU_Texture.
Cary Clarka560c472017-11-27 10:44:06 -05001343
Cary Clarkf5404bb2018-01-05 12:10:09 -05001344#Return true if Image is a GPU_Texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001345
1346#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001347#Image 5
1348#Platform gpu
1349auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1350 if (nullptr == image) {
1351 return;
1352 }
1353 SkPaint paint;
1354 paint.setAntiAlias(true);
1355 paint.setTextAlign(SkPaint::kCenter_Align);
1356 canvas->drawImage(image, 0, 0);
1357 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1358 canvas->drawString(image->isTextureBacked() ? "is GPU texture" : "not GPU texture",
1359 image->width() / 2, image->height() * 3 / 4, paint);
1360};
1361sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1362sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1363 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1364drawImage(image, "image");
1365canvas->translate(image->width(), 0);
1366drawImage(bitmapImage, "source");
1367canvas->translate(-image->width(), image->height());
1368drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001369##
1370
Cary Clarkf5404bb2018-01-05 12:10:09 -05001371#SeeAlso MakeFromTexture isValid
Cary Clarka560c472017-11-27 10:44:06 -05001372
1373#Method ##
1374
1375# ------------------------------------------------------------------------------
1376
1377#Method bool isValid(GrContext* context) const
Cary Clark4855f782018-02-06 09:41:53 -05001378#In Property
1379#Line # returns if Image can draw to Raster_Surface or GPU_Context ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001380Returns true if Image can be drawn on either Raster_Surface or GPU_Surface.
1381If context is nullptr, tests if Image draws on Raster_Surface;
1382otherwise, tests if Image draws on GPU_Surface associated with context.
Cary Clarka560c472017-11-27 10:44:06 -05001383
Cary Clarkf5404bb2018-01-05 12:10:09 -05001384Image backed by GPU_Texture may become invalid if associated GrContext is
1385invalid. Lazy_Image may be invalid and may not draw to Raster_Surface or
1386GPU_Surface or both.
Cary Clarka560c472017-11-27 10:44:06 -05001387
Cary Clark61ca7c52018-01-02 11:34:14 -05001388#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05001389
Cary Clarkf5404bb2018-01-05 12:10:09 -05001390#Return true if Image can be drawn ##
Cary Clarka560c472017-11-27 10:44:06 -05001391
1392#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001393#Image 5
1394#Platform gpu
1395auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1396 if (nullptr == image) {
1397 return;
1398 }
1399 SkPaint paint;
1400 paint.setAntiAlias(true);
1401 paint.setTextAlign(SkPaint::kCenter_Align);
1402 canvas->drawImage(image, 0, 0);
1403 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1404 if (canvas->getGrContext()) {
1405 canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" :
1406 "not valid on GPU", image->width() / 2, image->height() * 5 / 8, paint);
1407 }
1408 canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" :
1409 "not valid on CPU", image->width() / 2, image->height() * 7 / 8, paint);
1410};
1411sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1412sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1413 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1414drawImage(image, "image");
1415canvas->translate(image->width(), 0);
1416drawImage(bitmapImage, "source");
1417canvas->translate(-image->width(), image->height());
1418drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001419##
1420
Cary Clarkf5404bb2018-01-05 12:10:09 -05001421#SeeAlso isTextureBacked isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001422
1423#Method ##
1424
1425# ------------------------------------------------------------------------------
1426
1427#Method GrBackendObject getTextureHandle(bool flushPendingGrContextIO,
1428 GrSurfaceOrigin* origin = nullptr) const
Cary Clark78de7512018-02-07 07:27:09 -05001429#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001430#Line # returns GPU reference to Image as texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001431
Cary Clark2f466242017-12-11 16:03:17 -05001432Retrieves the back-end API handle of texture. If flushPendingGrContextIO is true,
1433complete deferred I/O operations.
Cary Clarka560c472017-11-27 10:44:06 -05001434
Cary Clark2f466242017-12-11 16:03:17 -05001435If origin in not nullptr, copies location of content drawn into Image.
Cary Clarka560c472017-11-27 10:44:06 -05001436
Cary Clark2f466242017-12-11 16:03:17 -05001437#Param flushPendingGrContextIO flag to flush outstanding requests ##
1438#Param origin storage for one of: kTopLeft_GrSurfaceOrigin,
1439 kBottomLeft_GrSurfaceOrigin; or nullptr
1440##
1441
Cary Clarkac47b882018-01-11 10:35:44 -05001442#Return back-end API texture handle, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001443
1444#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001445#Image 4
Cary Clark2f466242017-12-11 16:03:17 -05001446#Platform gpu
1447GrContext* context = canvas->getGrContext();
1448if (!context) {
1449 return;
1450}
1451SkPaint paint;
1452paint.setAntiAlias(true);
1453SkString str;
Cary Clarkac47b882018-01-11 10:35:44 -05001454int y = -10;
Cary Clark2f466242017-12-11 16:03:17 -05001455for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin } ) {
1456 sk_sp<SkImage> srcImage(SkImage::MakeFromTexture(context,
1457 backEndTexture, origin, kPremul_SkAlphaType, nullptr));
1458 GrSurfaceOrigin readBackOrigin;
1459 GrBackendObject readBackHandle = srcImage->getTextureHandle(false, &readBackOrigin);
Cary Clarkac47b882018-01-11 10:35:44 -05001460 str.printf("readBackHandle: 0x%x", readBackHandle);
1461 canvas->drawString(str, 5, y += 30, paint);
1462 canvas->drawImage(srcImage, 80, y += 10);
Cary Clark2f466242017-12-11 16:03:17 -05001463 str.printf("origin: k%s_GrSurfaceOrigin", readBackOrigin ? "BottomLeft" : "TopLeft");
Cary Clarkac47b882018-01-11 10:35:44 -05001464 canvas->drawString(str, 5, y += srcImage->height() + 10, paint);
Cary Clark2f466242017-12-11 16:03:17 -05001465}
Cary Clarka560c472017-11-27 10:44:06 -05001466##
1467
Cary Clarkac47b882018-01-11 10:35:44 -05001468#Example
1469#Image 5
1470#Platform gpu
1471 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1472 if (nullptr == image) {
1473 return;
1474 }
1475 SkPaint paint;
1476 paint.setAntiAlias(true);
1477 paint.setTextAlign(SkPaint::kCenter_Align);
1478 canvas->drawImage(image, 0, image->height() / 4);
1479 canvas->drawString(label, image->width() / 2, image->height() / 8, paint);
1480 GrSurfaceOrigin readBackOrigin;
1481 GrBackendObject readBackHandle = image->getTextureHandle(false, &readBackOrigin);
1482 canvas->drawString(readBackHandle ? "has readBackHandle" : "no readBackHandle",
1483 image->width() / 2, image->height() * 11 / 8, paint);
1484 };
1485 drawImage(image, "image");
1486 canvas->translate(image->width(), 0);
1487 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1488 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1489 drawImage(textureImage, "backEndTexture");
1490##
1491
1492#SeeAlso MakeFromTexture isTextureBacked
Cary Clarka560c472017-11-27 10:44:06 -05001493
1494#Method ##
1495
1496# ------------------------------------------------------------------------------
1497
1498#Enum CachingHint
1499
1500#Code
1501 enum CachingHint {
1502 kAllow_CachingHint,
1503 kDisallow_CachingHint,
1504 };
1505##
1506
Cary Clarkac47b882018-01-11 10:35:44 -05001507CachingHint selects whether Skia may internally cache Bitmaps generated by
1508decoding Image, or by copying Image from GPU to CPU. The default behavior
1509allows caching Bitmaps.
1510
1511Choose kDisallow_CachingHint if Image pixels are to be used only once, or
1512if Image pixels reside in a cache outside of Skia, or to reduce memory pressure.
1513
1514Choosing kAllow_CachingHint does not ensure that pixels will be cached.
1515Image pixels may not be cached if memory requirements are too large or
1516pixels are not accessible.
Cary Clarka560c472017-11-27 10:44:06 -05001517
1518#Const kAllow_CachingHint 0
Cary Clarkac47b882018-01-11 10:35:44 -05001519Allows Skia to internally cache decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001520##
1521#Const kDisallow_CachingHint 1
Cary Clarkac47b882018-01-11 10:35:44 -05001522Disallows Skia from internally caching decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001523##
1524
Cary Clarkac47b882018-01-11 10:35:44 -05001525#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001526##
1527
Cary Clarkac47b882018-01-11 10:35:44 -05001528#SeeAlso readPixels scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001529
1530#Enum ##
1531
1532# ------------------------------------------------------------------------------
1533
1534#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1535 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001536#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001537#Line # copies and converts pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001538
Cary Clarkac47b882018-01-11 10:35:44 -05001539Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY),
1540and does not exceed Image (width(), height()).
1541
1542dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
1543destination. dstRowBytes specifics the gap from one destination row to the next.
1544Returns true if pixels are copied. Returns false if:
1545#List
1546# dstInfo.addr() equals nullptr ##
1547# dstRowBytes is less than dstInfo.minRowBytes ##
1548# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001549##
1550
Cary Clarkac47b882018-01-11 10:35:44 -05001551Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1552kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1553If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace must match.
1554If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType must
1555match. If Image Color_Space is nullptr, dstInfo.colorSpace must match. Returns
1556false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001557
Cary Clarkac47b882018-01-11 10:35:44 -05001558srcX and srcY may be negative to copy only top or left of source. Returns
1559false if width() or height() is zero or negative.
1560Returns false if
1561#Formula
1562abs(srcX) >= Image width()
1563##
1564, or if
1565#Formula
1566abs(srcY) >= Image height()
1567##
1568.
Cary Clarka560c472017-11-27 10:44:06 -05001569
Cary Clarkac47b882018-01-11 10:35:44 -05001570If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1571If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1572
1573#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1574#Param dstPixels destination pixel storage ##
1575#Param dstRowBytes destination row length ##
1576#Param srcX column index whose absolute value is less than width() ##
1577#Param srcY row index whose absolute value is less than height() ##
1578#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1579
1580#Return true if pixels are copied to dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001581
1582#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001583#Image 3
1584 canvas->scale(.5f, .5f);
1585 const int width = 32;
1586 const int height = 32;
1587 std::vector<int32_t> dstPixels;
1588 dstPixels.resize(height * width * 4);
1589 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1590 for (int y = 0; y < 512; y += height ) {
1591 for (int x = 0; x < 512; x += width ) {
1592 if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) {
1593 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
1594 SkBitmap bitmap;
1595 bitmap.installPixels(dstPixmap);
1596 canvas->drawBitmap(bitmap, 0, 0);
1597 }
1598 canvas->translate(48, 0);
1599 }
1600 canvas->translate(-16 * 48, 48);
1601 }
Cary Clarka560c472017-11-27 10:44:06 -05001602##
1603
Cary Clarkac47b882018-01-11 10:35:44 -05001604#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001605
1606#Method ##
1607
1608# ------------------------------------------------------------------------------
1609
1610#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY,
1611 CachingHint cachingHint = kAllow_CachingHint) const
1612
Cary Clarkac47b882018-01-11 10:35:44 -05001613Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and
1614does not exceed Image (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001615
Cary Clarkac47b882018-01-11 10:35:44 -05001616dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
1617and row bytes of destination. dst.rowBytes specifics the gap from one destination
1618row to the next. Returns true if pixels are copied. Returns false if:
1619#List
1620# dst pixel storage equals nullptr ##
1621# dst.rowBytes is less than SkImageInfo::minRowBytes ##
1622# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001623##
1624
Cary Clarkac47b882018-01-11 10:35:44 -05001625Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1626kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1627If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1628If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1629match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1630false if pixel conversion is not possible.
1631
1632srcX and srcY may be negative to copy only top or left of source. Returns
1633false if width() or height() is zero or negative.
1634Returns false if
1635#Formula
1636abs(srcX) >= Image width()
1637##
1638, or if
1639#Formula
1640abs(srcY) >= Image height()
1641##
1642.
1643
1644If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1645If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1646
1647#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1648#Param srcX column index whose absolute value is less than width() ##
1649#Param srcY row index whose absolute value is less than height() ##
1650#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1651
1652#Return true if pixels are copied to dst ##
1653
1654#Example
1655#Image 3
1656 std::vector<int32_t> srcPixels;
1657 int rowBytes = image->width() * 4;
1658 int quarterWidth = image->width() / 4;
1659 int quarterHeight = image->height() / 4;
1660 srcPixels.resize(image->height() * rowBytes);
1661 for (int y = 0; y < 4; ++y) {
1662 for (int x = 0; x < 4; ++x) {
1663 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1664 &srcPixels.front() + x * image->height() * quarterWidth +
1665 y * quarterWidth, rowBytes);
1666 image->readPixels(pixmap, x * quarterWidth, y * quarterHeight);
1667 }
1668 }
1669 canvas->scale(.5f, .5f);
1670 SkBitmap bitmap;
1671 bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()),
1672 &srcPixels.front(), rowBytes);
1673 canvas->drawBitmap(bitmap, 0, 0);
1674##
1675
1676#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001677
1678#Method ##
1679
1680# ------------------------------------------------------------------------------
1681
1682#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality,
1683 CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001684#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001685#Line # scales and converts one Image to another ##
Cary Clarka560c472017-11-27 10:44:06 -05001686
Cary Clarkac47b882018-01-11 10:35:44 -05001687Copies Image to dst, scaling pixels to fit dst.width() and dst.height(), and
1688converting pixels to match dst.colorType and dst.alphaType. Returns true if
1689pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1690less than dst SkImageInfo::minRowBytes.
Cary Clarka560c472017-11-27 10:44:06 -05001691
Cary Clarkac47b882018-01-11 10:35:44 -05001692Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1693kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1694If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1695If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1696match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1697false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001698
Cary Clarkac47b882018-01-11 10:35:44 -05001699Scales the image, with filterQuality, to match dst.width() and dst.height().
1700filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1701Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1702Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1703Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1704kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1705
1706If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1707If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1708
1709#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1710#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1711 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1712##
1713#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1714
1715#Return true if pixels are scaled to fit dst ##
Cary Clarka560c472017-11-27 10:44:06 -05001716
1717#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001718#Image 3
1719#Height 128
1720 std::vector<int32_t> srcPixels;
1721 int quarterWidth = image->width() / 16;
1722 int rowBytes = quarterWidth * 4;
1723 int quarterHeight = image->height() / 16;
1724 srcPixels.resize(quarterHeight * rowBytes);
1725 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1726 &srcPixels.front(), rowBytes);
1727 canvas->scale(4, 4);
1728 SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality,
1729 kMedium_SkFilterQuality, kHigh_SkFilterQuality };
1730 for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) {
1731 image->scalePixels(pixmap, qualities[index]);
1732 sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
1733 canvas->drawImage(filtered, 16 * index, 0);
1734 }
Cary Clarka560c472017-11-27 10:44:06 -05001735##
1736
Cary Clarkac47b882018-01-11 10:35:44 -05001737#SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001738
1739#Method ##
1740
1741# ------------------------------------------------------------------------------
1742
1743#Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const
Cary Clark78de7512018-02-07 07:27:09 -05001744#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001745#Line # returns encoded Image as SkData ##
Cary Clarkac47b882018-01-11 10:35:44 -05001746Encodes Image pixels, returning result as SkData.
Cary Clark2f466242017-12-11 16:03:17 -05001747
Cary Clarkac47b882018-01-11 10:35:44 -05001748Returns nullptr if encoding fails, or if encodedImageFormat is not supported.
Cary Clarka560c472017-11-27 10:44:06 -05001749
Cary Clarkac47b882018-01-11 10:35:44 -05001750Image encoding in a format requires both building with one or more of:
1751SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support
1752for the encoded format.
1753
1754If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can
1755additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP,
1756SkEncodedImageFormat::kGIF.
1757
1758quality is a platform and format specific metric trading off size and encoding
1759error. When used, quality equaling 100 encodes with the least error. quality may
1760be ignored by the encoder.
1761
1762#Param encodedImageFormat one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
1763 SkEncodedImageFormat::kWEBP
1764 ##
1765#Param quality encoder specific metric with 100 equaling best ##
Cary Clarka560c472017-11-27 10:44:06 -05001766
Cary Clark2f466242017-12-11 16:03:17 -05001767#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001768
1769#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001770#Image 3
1771 canvas->scale(4, 4);
1772 SkIRect subset = {0, 0, 16, 64};
1773 int x = 0;
1774 for (int quality : { 0, 10, 50, 100 } ) {
1775 sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
1776 sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
1777 canvas->drawImage(filtered, x, 0);
1778 x += 16;
1779 }
Cary Clarka560c472017-11-27 10:44:06 -05001780##
1781
Cary Clarkac47b882018-01-11 10:35:44 -05001782#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001783
1784#Method ##
1785
1786# ------------------------------------------------------------------------------
1787
Cary Clark61ca7c52018-01-02 11:34:14 -05001788#Method sk_sp<SkData> encodeToData() const
Cary Clarka560c472017-11-27 10:44:06 -05001789
Cary Clarkac47b882018-01-11 10:35:44 -05001790Encodes Image pixels, returning result as SkData. Returns existing encoded data
1791if present; otherwise, Image is encoded with SkEncodedImageFormat::kPNG. Skia
1792must be built with SK_HAS_PNG_LIBRARY to encode Image.
Cary Clarka560c472017-11-27 10:44:06 -05001793
Cary Clarkac47b882018-01-11 10:35:44 -05001794Returns nullptr if existing encoded data is missing or invalid, and
Cary Clarka560c472017-11-27 10:44:06 -05001795encoding fails.
1796
Cary Clarkac47b882018-01-11 10:35:44 -05001797#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001798
1799#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001800#Image 3
1801 canvas->scale(4, 4);
1802 SkIRect subset = {136, 32, 200, 96};
1803 sk_sp<SkData> data(image->encodeToData());
1804 sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
1805 canvas->drawImage(eye, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001806##
1807
Cary Clarkac47b882018-01-11 10:35:44 -05001808#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001809
1810#Method ##
1811
1812# ------------------------------------------------------------------------------
1813
1814#Method sk_sp<SkData> refEncodedData() const
Cary Clark78de7512018-02-07 07:27:09 -05001815#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001816#Line # returns Image encoded in SkData if present ##
Cary Clarkac47b882018-01-11 10:35:44 -05001817Returns encoded Image pixels as SkData, if Image was created from supported
1818encoded stream format. Platform support for formats vary and may require building
1819with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY.
Cary Clarka560c472017-11-27 10:44:06 -05001820
Cary Clarkac47b882018-01-11 10:35:44 -05001821Returns nullptr if Image contents are not encoded.
Cary Clarka560c472017-11-27 10:44:06 -05001822
Cary Clarkac47b882018-01-11 10:35:44 -05001823#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001824
1825#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001826#Image 3
1827#Platform gpu
1828 struct {
1829 const char* name;
1830 sk_sp<SkImage> image;
1831 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1832 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1833 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr) } };
1834 SkString string;
1835 SkPaint paint;
1836 for (const auto& test : tests ) {
1837 if (!test.image) {
1838 string.printf("no %s", test.name);
1839 } else {
1840 string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name);
1841 }
1842 canvas->drawString(string, 10, 20, paint);
1843 canvas->translate(0, 20);
1844 }
Cary Clarka560c472017-11-27 10:44:06 -05001845##
1846
Cary Clarkac47b882018-01-11 10:35:44 -05001847#SeeAlso encodeToData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001848
1849#Method ##
1850
1851# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001852#Subtopic Utility
1853#Populate
1854#Line # rarely called management functions ##
1855##
Cary Clarka560c472017-11-27 10:44:06 -05001856
1857#Method const char* toString(SkString* string) const
Cary Clark4855f782018-02-06 09:41:53 -05001858#In Utility
1859#Line # converts Image to machine readable form ##
Cary Clarkac47b882018-01-11 10:35:44 -05001860Appends Image description to string, including unique ID, width, height, and
1861whether the image is opaque.
Cary Clarka560c472017-11-27 10:44:06 -05001862
Cary Clarkac47b882018-01-11 10:35:44 -05001863#Param string storage for description; existing content is preserved ##
1864
1865#Return string appended with Image description ##
Cary Clarka560c472017-11-27 10:44:06 -05001866
1867#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001868#Image 4
1869 struct {
1870 const char* name;
1871 sk_sp<SkImage> image;
1872 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1873 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1874 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr) } };
1875 SkString string;
1876 SkPaint paint;
1877 for (const auto& test : tests ) {
1878 string.printf("%s: ", test.name);
1879 test.image ? (void) test.image->toString(&string) : string.append("no image");
1880 canvas->drawString(string, 10, 20, paint);
1881 canvas->translate(0, 20);
1882 }
Cary Clarka560c472017-11-27 10:44:06 -05001883##
1884
Cary Clarkac47b882018-01-11 10:35:44 -05001885#SeeAlso SkPaint::toString
Cary Clarka560c472017-11-27 10:44:06 -05001886
1887#Method ##
1888
1889# ------------------------------------------------------------------------------
1890
1891#Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const
Cary Clark4855f782018-02-06 09:41:53 -05001892#In Constructor
1893#Line # creates Image containing part of original ##
Cary Clarkac47b882018-01-11 10:35:44 -05001894Returns subset of Image. subset must be fully contained by Image dimensions().
1895The implementation may share pixels, or may copy them.
Cary Clarka560c472017-11-27 10:44:06 -05001896
Cary Clarkac47b882018-01-11 10:35:44 -05001897Returns nullptr if subset is empty, or subset is not contained by bounds, or
1898pixels in Image could not be read or copied.
Cary Clarka560c472017-11-27 10:44:06 -05001899
Cary Clarkac47b882018-01-11 10:35:44 -05001900#Param subset bounds of returned Image ##
1901
1902#Return partial or full Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001903
1904#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001905#Image 3
1906 canvas->scale(.5f, .5f);
1907 const int width = 32;
1908 const int height = 32;
1909 for (int y = 0; y < 512; y += height ) {
1910 for (int x = 0; x < 512; x += width ) {
1911 sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height}));
1912 canvas->drawImage(subset, x * 3 / 2, y * 3 / 2);
1913 }
1914 }
Cary Clarka560c472017-11-27 10:44:06 -05001915##
1916
Cary Clarkac47b882018-01-11 10:35:44 -05001917#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001918
1919#Method ##
1920
1921# ------------------------------------------------------------------------------
1922
1923#Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const
Cary Clark4855f782018-02-06 09:41:53 -05001924#In Constructor
1925#Line # creates Image matching Color_Space if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001926Returns Image backed by GPU_Texture associated with context. Returned Image is
1927compatible with Surface created with dstColorSpace. Returns original
1928Image if context and dstColorSpace match.
1929
1930Returns nullptr if context is nullptr, or if Image was created with another
1931GrContext.
Cary Clarka560c472017-11-27 10:44:06 -05001932
Cary Clark61ca7c52018-01-02 11:34:14 -05001933#Param context GPU_Context ##
Cary Clarkac47b882018-01-11 10:35:44 -05001934#Param dstColorSpace range of colors of matching Surface on GPU ##
Cary Clarka560c472017-11-27 10:44:06 -05001935
Cary Clarkac47b882018-01-11 10:35:44 -05001936#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001937
1938#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001939#Platform gpu
1940#Image 5
1941 auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void {
1942 if (nullptr == image || nullptr == context) {
1943 return;
1944 }
1945 SkPaint paint;
1946 paint.setAntiAlias(true);
1947 paint.setTextAlign(SkPaint::kCenter_Align);
1948 sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr));
1949 canvas->drawImage(texture, 0, 0);
1950 canvas->drawString(label, texture->width() / 2, texture->height() / 4, paint);
1951 };
1952 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1953 GrContext* context = canvas->getGrContext();
1954 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture,
1955 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1956 drawImage(image, context, "image");
1957 canvas->translate(image->width(), 0);
1958 drawImage(bitmapImage, context, "source");
1959 canvas->translate(-image->width(), image->height());
1960 drawImage(textureImage, context, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001961##
1962
Cary Clarkac47b882018-01-11 10:35:44 -05001963#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05001964
1965#Method ##
1966
1967# ------------------------------------------------------------------------------
1968
1969#Method sk_sp<SkImage> makeNonTextureImage() const
Cary Clark4855f782018-02-06 09:41:53 -05001970#In Constructor
1971#Line # creates Image without dependency on GPU_Texture ##
Cary Clarkac47b882018-01-11 10:35:44 -05001972Returns Raster_Image or Lazy_Image. Copies Image backed by GPU_Texture into
Cary Clark4855f782018-02-06 09:41:53 -05001973CPU memory if needed. Returns original Image if decoded in Raster_Bitmap,
Cary Clarkac47b882018-01-11 10:35:44 -05001974or if encoded in a stream.
Cary Clark61ca7c52018-01-02 11:34:14 -05001975
Cary Clarkac47b882018-01-11 10:35:44 -05001976Returns nullptr if backed by GPU_Texture and copy fails.
1977
1978#Return Raster_Image, Lazy_Image, or nullptr ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001979
1980#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001981#Image 5
1982#Platform gpu
1983 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1984 if (nullptr == image) {
1985 return;
1986 }
1987 SkPaint paint;
1988 paint.setAntiAlias(true);
1989 paint.setTextAlign(SkPaint::kCenter_Align);
1990 sk_sp<SkImage> nonTexture(image->makeNonTextureImage());
1991 canvas->drawImage(nonTexture, 0, 0);
1992 canvas->drawString(label, nonTexture->width() / 2, nonTexture->height() / 4, paint);
1993 };
1994 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1995 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1996 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1997 drawImage(image, "image");
1998 canvas->translate(image->width(), 0);
1999 drawImage(bitmapImage, "source");
2000 canvas->translate(-image->width(), image->height());
2001 drawImage(textureImage, "backEndTexture");
Cary Clark61ca7c52018-01-02 11:34:14 -05002002##
2003
Cary Clark56356312018-02-08 14:45:18 -05002004#SeeAlso makeTextureImage makeRasterImage MakeBackendTextureFromSkImage
Cary Clark61ca7c52018-01-02 11:34:14 -05002005
2006#Method ##
2007
2008# ------------------------------------------------------------------------------
2009
2010#Method sk_sp<SkImage> makeRasterImage() const
Cary Clark4855f782018-02-06 09:41:53 -05002011#In Constructor
2012#Line # creates Image compatible with Raster_Surface if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05002013Returns Raster_Image. Copies Image backed by GPU_Texture into CPU memory,
Cary Clark4855f782018-02-06 09:41:53 -05002014or decodes Image from Lazy_Image. Returns original Image if decoded in
Cary Clarkac47b882018-01-11 10:35:44 -05002015Raster_Bitmap.
Cary Clarka560c472017-11-27 10:44:06 -05002016
Cary Clarkac47b882018-01-11 10:35:44 -05002017Returns nullptr if copy, decode, or pixel read fails.
Cary Clarka560c472017-11-27 10:44:06 -05002018
Cary Clarkac47b882018-01-11 10:35:44 -05002019#Return Raster_Image, or nullptr ##
2020
Cary Clark4855f782018-02-06 09:41:53 -05002021#Bug 7479
Cary Clarka560c472017-11-27 10:44:06 -05002022#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002023#Image 5
2024#Platform gpu
2025 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2026 if (nullptr == image) {
2027 return;
2028 }
2029 SkPaint paint;
2030 paint.setAntiAlias(true);
2031 paint.setTextAlign(SkPaint::kCenter_Align);
2032 sk_sp<SkImage> raster(image->makeRasterImage());
2033 canvas->drawImage(raster, 0, 0);
2034 canvas->drawString(label, raster->width() / 2, raster->height() / 4, paint);
2035 };
2036 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2037 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2038 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2039 drawImage(image, "image");
2040 canvas->translate(image->width(), 0);
2041 drawImage(bitmapImage, "source");
2042 canvas->translate(-image->width(), image->height());
2043 drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05002044##
2045
Cary Clarkac47b882018-01-11 10:35:44 -05002046#SeeAlso isTextureBacked isLazyGenerated MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -05002047
2048#Method ##
2049
2050# ------------------------------------------------------------------------------
2051
2052#Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
2053 const SkIRect& clipBounds, SkIRect* outSubset,
2054 SkIPoint* offset) const
Cary Clark4855f782018-02-06 09:41:53 -05002055#In Constructor
2056#Line # creates filtered, clipped Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002057
Cary Clarkac47b882018-01-11 10:35:44 -05002058Creates filtered Image. filter processes original Image, potentially changing
2059color, position, and size. subset is the bounds of original Image processed
2060by filter. clipBounds is the expected bounds of the filtered Image. outSubset
2061is required storage for the actual bounds of the filtered Image. offset is
2062required storage for translation of returned Image.
Cary Clarka560c472017-11-27 10:44:06 -05002063
Cary Clarkac47b882018-01-11 10:35:44 -05002064Returns nullptr if Image could not be created. If nullptr is returned, outSubset
2065and offset are undefined.
2066
Cary Clark56356312018-02-08 14:45:18 -05002067Useful for animation of SkImageFilter that varies size from frame to frame.
2068Returned Image is created larger than required by filter so that GPU_Texture
2069can be reused with different sized effects. outSubset describes the valid bounds
2070of GPU_Texture returned. offset translates the returned Image to keep subsequent
2071animation frames aligned with respect to each other.
Cary Clarkac47b882018-01-11 10:35:44 -05002072
2073#Param filter how Image is sampled when transformed ##
Cary Clark56356312018-02-08 14:45:18 -05002074#Param subset bounds of Image processed by filter ##
2075#Param clipBounds expected bounds of filtered Image ##
2076#Param outSubset storage for returned Image bounds ##
2077#Param offset storage for returned Image translation ##
Cary Clarka560c472017-11-27 10:44:06 -05002078
Cary Clarkac47b882018-01-11 10:35:44 -05002079#Return filtered Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05002080
2081#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002082#Description
2083In each frame of the animation, filtered Image is drawn in a different location.
2084By translating canvas by returned offset, Image appears stationary.
2085##
2086#Image 5
2087#Platform gpu
2088#Duration 5
2089 sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make(
2090 -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE,
2091 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
2092 nullptr);
2093 sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr);
2094 SkIRect subset = image->bounds();
2095 SkIRect clipBounds = image->bounds();
2096 clipBounds.outset(60, 60);
2097 SkIRect outSubset;
2098 SkIPoint offset;
2099 sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds,
2100 &outSubset, &offset));
2101 SkPaint paint;
2102 paint.setAntiAlias(true);
2103 paint.setStyle(SkPaint::kStroke_Style);
2104 canvas->drawLine(0, 0, offset.fX, offset.fY, paint);
2105 canvas->translate(offset.fX, offset.fY);
2106 canvas->drawImage(filtered, 0, 0);
2107 canvas->drawRect(SkRect::MakeFromIRect(outSubset), paint);
Cary Clarka560c472017-11-27 10:44:06 -05002108##
2109
Cary Clark56356312018-02-08 14:45:18 -05002110#SeeAlso makeShader SkPaint::setImageFilter
Cary Clarka560c472017-11-27 10:44:06 -05002111
2112#Method ##
2113
2114# ------------------------------------------------------------------------------
2115
Cary Clark61ca7c52018-01-02 11:34:14 -05002116#Struct DeferredTextureImageUsageParams
Cary Clark4855f782018-02-06 09:41:53 -05002117#Deprecated soon
Cary Clark61ca7c52018-01-02 11:34:14 -05002118
Cary Clark4855f782018-02-06 09:41:53 -05002119Used only by Chrome.
Cary Clark61ca7c52018-01-02 11:34:14 -05002120
2121##
2122
2123#Method size_t getDeferredTextureImageData(const GrContextThreadSafeProxy& contextThreadSafeProxy,
2124 const DeferredTextureImageUsageParams deferredTextureImageUsageParams[],
2125 int paramCnt,
2126 void* buffer,
2127 SkColorSpace* dstColorSpace = nullptr,
2128 SkColorType dstColorType = kN32_SkColorType) const
Cary Clark4855f782018-02-06 09:41:53 -05002129#Deprecated soon
Cary Clark61ca7c52018-01-02 11:34:14 -05002130
Cary Clark4855f782018-02-06 09:41:53 -05002131Used only by Chrome.
Cary Clark61ca7c52018-01-02 11:34:14 -05002132##
2133
2134#Method static sk_sp<SkImage> MakeFromDeferredTextureImageData(GrContext* context, const void* data,
2135 SkBudgeted budgeted)
Cary Clark4855f782018-02-06 09:41:53 -05002136#Deprecated soon
Cary Clark61ca7c52018-01-02 11:34:14 -05002137
Cary Clark4855f782018-02-06 09:41:53 -05002138Used only by Chrome.
Cary Clark61ca7c52018-01-02 11:34:14 -05002139##
2140
2141# ------------------------------------------------------------------------------
2142
Cary Clarka560c472017-11-27 10:44:06 -05002143#Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc
2144
2145##
2146
2147# ------------------------------------------------------------------------------
2148
2149#Method static bool MakeBackendTextureFromSkImage(GrContext* context,
2150 sk_sp<SkImage> image,
2151 GrBackendTexture* backendTexture,
2152 BackendTextureReleaseProc* backendTextureReleaseProc)
Cary Clark4855f782018-02-06 09:41:53 -05002153#In Constructor
2154#Line # creates GPU_Texture from Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002155
Cary Clark56356312018-02-08 14:45:18 -05002156Creates a GrBackendTexture from the provided SkImage. Returns true and
2157stores result in backendTexture and backendTextureReleaseProc if
2158texture is created; otherwise, returns false and leaves
2159backendTexture and backendTextureReleaseProc unmodified.
Cary Clarka560c472017-11-27 10:44:06 -05002160
Cary Clark56356312018-02-08 14:45:18 -05002161Call backendTextureReleaseProc after deleting backendTexture.
2162backendTextureReleaseProc cleans up auxiliary data related to returned
2163backendTexture. The caller must delete returned backendTexture after use.
Cary Clarka560c472017-11-27 10:44:06 -05002164
Cary Clark56356312018-02-08 14:45:18 -05002165If Image is both texture backed and singly referenced, image is returned in
2166backendTexture without conversion or making a copy. Image is singly referenced
2167if its was transferred solely using std::move().
2168
2169If Image is not texture backed, returns texture with Image contents.
Cary Clarka560c472017-11-27 10:44:06 -05002170
Cary Clark61ca7c52018-01-02 11:34:14 -05002171#Param context GPU_Context ##
Cary Clark56356312018-02-08 14:45:18 -05002172#Param image Image used for texture ##
2173#Param backendTexture storage for backend texture ##
2174#Param backendTextureReleaseProc storage for clean up function ##
Cary Clarka560c472017-11-27 10:44:06 -05002175
Cary Clark56356312018-02-08 14:45:18 -05002176#Return true if backend texture was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002177
2178#Example
Cary Clark56356312018-02-08 14:45:18 -05002179#Platform gpu
2180#Height 64
2181#Function
2182static sk_sp<SkImage> create_gpu_image(GrContext* grContext) {
2183 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
2184 auto surface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info));
2185 SkCanvas* canvas = surface->getCanvas();
2186 canvas->clear(SK_ColorWHITE);
2187 SkPaint paint;
2188 paint.setColor(SK_ColorBLACK);
2189 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
2190 return surface->makeImageSnapshot();
2191}
2192##
2193
2194void draw(SkCanvas* canvas) {
2195 GrContext* grContext = canvas->getGrContext();
2196 if (!grContext) {
2197 return;
2198 }
2199 sk_sp<SkImage> backEndImage = create_gpu_image(grContext);
2200 canvas->drawImage(backEndImage, 0, 0);
2201 GrBackendTexture texture;
2202 SkImage::BackendTextureReleaseProc proc;
2203 if (!SkImage::MakeBackendTextureFromSkImage(grContext, std::move(backEndImage),
2204 &texture, &proc)) {
2205 return;
2206 }
2207 sk_sp<SkImage> i2 = SkImage::MakeFromTexture(grContext, texture, kTopLeft_GrSurfaceOrigin,
2208 kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
2209 canvas->drawImage(i2, 30, 30);
2210}
Cary Clarka560c472017-11-27 10:44:06 -05002211##
2212
Cary Clark56356312018-02-08 14:45:18 -05002213#SeeAlso MakeFromTexture makeTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002214
2215#Method ##
2216
2217# ------------------------------------------------------------------------------
2218
2219#Enum LegacyBitmapMode
Cary Clark56356312018-02-08 14:45:18 -05002220#Deprecated soon
Cary Clarka560c472017-11-27 10:44:06 -05002221#Code
2222 enum LegacyBitmapMode {
2223 kRO_LegacyBitmapMode,
Cary Clarka560c472017-11-27 10:44:06 -05002224 };
2225##
2226
Cary Clarka560c472017-11-27 10:44:06 -05002227#Const kRO_LegacyBitmapMode 0
Cary Clark56356312018-02-08 14:45:18 -05002228Returned bitmap is read-only and immutable.
Cary Clarka560c472017-11-27 10:44:06 -05002229##
Cary Clarka560c472017-11-27 10:44:06 -05002230
2231#Enum ##
2232
2233# ------------------------------------------------------------------------------
2234
Cary Clark56356312018-02-08 14:45:18 -05002235#Method bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode = kRO_LegacyBitmapMode) const
Cary Clark4855f782018-02-06 09:41:53 -05002236#In Constructor
2237#Line # returns as Raster_Bitmap ##
Cary Clarkac47b882018-01-11 10:35:44 -05002238Creates raster Bitmap with same pixels as Image. If legacyBitmapMode is
2239kRO_LegacyBitmapMode, returned bitmap is read-only and immutable.
2240Returns true if Bitmap is stored in bitmap. Returns false and resets bitmap if
2241Bitmap write did not succeed.
Cary Clarka560c472017-11-27 10:44:06 -05002242
Cary Clark3cd22cc2017-12-01 11:49:58 -05002243#Param bitmap storage for legacy Bitmap ##
Cary Clark56356312018-02-08 14:45:18 -05002244#Param legacyBitmapMode to be deprecated ##
Cary Clarka560c472017-11-27 10:44:06 -05002245
Cary Clark3cd22cc2017-12-01 11:49:58 -05002246#Return true if Bitmap was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002247
2248#Example
Cary Clark56356312018-02-08 14:45:18 -05002249#Image 4
2250#Platform gpu
2251 SkBitmap bitImage;
2252 if (image->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2253 canvas->drawBitmap(bitImage, 0, 0);
2254 }
2255 GrContext* grContext = canvas->getGrContext();
2256 if (!grContext) {
2257 return;
2258 }
2259 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(grContext, backEndTexture,
2260 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2261 canvas->drawImage(textureImage, 45, 45);
2262 if (textureImage->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2263 canvas->drawBitmap(bitImage, 90, 90);
2264 }
Cary Clarka560c472017-11-27 10:44:06 -05002265##
2266
Cary Clark56356312018-02-08 14:45:18 -05002267#SeeAlso MakeRasterData makeRasterImage makeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002268
2269#Method ##
2270
2271# ------------------------------------------------------------------------------
2272
2273#Method bool isLazyGenerated() const
Cary Clark4855f782018-02-06 09:41:53 -05002274#In Property
2275#Line # returns if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002276Returns true if Image is backed by an image-generator or other service that creates
2277and caches its pixels or texture on-demand.
2278
Cary Clark2f466242017-12-11 16:03:17 -05002279#Return true if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002280
2281#Example
Cary Clark2f466242017-12-11 16:03:17 -05002282#Height 80
2283#Function
2284class TestImageGenerator : public SkImageGenerator {
2285public:
2286 TestImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(10, 10)) {}
2287 ~TestImageGenerator() override {}
2288protected:
2289 bool onGetPixels(const SkImageInfo& info, void* pixelPtr, size_t rowBytes,
2290 const Options& options) override {
2291 SkPMColor* pixels = static_cast<SkPMColor*>(pixelPtr);
2292 for (int y = 0; y < info.height(); ++y) {
2293 for (int x = 0; x < info.width(); ++x) {
2294 pixels[y * info.width() + x] = 0xff223344 + y * 0x000C0811;
2295 }
2296 }
2297 return true;
2298 }
2299};
2300##
2301void draw(SkCanvas* canvas) {
2302 auto gen = std::unique_ptr<TestImageGenerator>(new TestImageGenerator());
2303 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
2304 SkString lazy(image->isLazyGenerated() ? "is lazy" : "not lazy");
2305 canvas->scale(8, 8);
2306 canvas->drawImage(image, 0, 0, nullptr);
2307 SkPaint paint;
2308 paint.setTextSize(4);
2309 canvas->drawString(lazy, 2, 5, paint);
2310}
Cary Clarka560c472017-11-27 10:44:06 -05002311##
2312
Cary Clarkf5404bb2018-01-05 12:10:09 -05002313#Example
2314#Image 5
2315#Platform gpu
2316void draw(SkCanvas* canvas) {
2317 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2318 if (nullptr == image) {
2319 return;
2320 }
2321 SkPaint paint;
2322 paint.setAntiAlias(true);
2323 paint.setTextAlign(SkPaint::kCenter_Align);
2324 canvas->drawImage(image, 0, 0);
2325 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
2326 canvas->drawString(
2327 image->isLazyGenerated() ? "is lazily generated" : "not lazily generated",
2328 image->width() / 2, image->height() * 3 / 4, paint);
2329 };
2330 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2331 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2332 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2333 drawImage(image, "image");
2334 canvas->translate(image->width(), 0);
2335 drawImage(bitmapImage, "source");
2336 canvas->translate(-image->width(), image->height());
2337 drawImage(textureImage, "backEndTexture");
2338}
2339##
2340
Cary Clarkac47b882018-01-11 10:35:44 -05002341#SeeAlso isTextureBacked MakeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002342
2343#Method ##
2344
2345# ------------------------------------------------------------------------------
2346
2347#Method sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target,
2348 SkTransferFunctionBehavior premulBehavior) const
Cary Clark4855f782018-02-06 09:41:53 -05002349#In Constructor
2350#Line # creates Image matching Color_Space if possible ##
Cary Clarka560c472017-11-27 10:44:06 -05002351
Cary Clarkac47b882018-01-11 10:35:44 -05002352Creates Image in target Color_Space.
2353Returns nullptr if Image could not be created.
Cary Clarka560c472017-11-27 10:44:06 -05002354
Cary Clarkac47b882018-01-11 10:35:44 -05002355Returns original Image if it is in target Color_Space.
2356Otherwise, converts pixels from Image Color_Space to target Color_Space.
2357If Image colorSpace returns nullptr, Image Color_Space is assumed to be sRGB.
2358
2359SkTransferFunctionBehavior is to be deprecated.
2360
2361Set premulBehavior to SkTransferFunctionBehavior::kRespect to convert Image
2362pixels to a linear space, before converting to destination Color_Type
Cary Clarka560c472017-11-27 10:44:06 -05002363and Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -05002364
Cary Clarkac47b882018-01-11 10:35:44 -05002365Set premulBehavior to SkTransferFunctionBehavior::kIgnore to treat Image
2366pixels as linear, when converting to destination Color_Type
2367and Color_Space, ignoring pixel encoding.
Cary Clarka560c472017-11-27 10:44:06 -05002368
Cary Clarkac47b882018-01-11 10:35:44 -05002369#Param target Color_Space describing color range of returned Image ##
2370#Param premulBehavior one of: SkTransferFunctionBehavior::kRespect,
2371 SkTransferFunctionBehavior::kIgnore
Cary Clarka560c472017-11-27 10:44:06 -05002372##
2373
Cary Clarkac47b882018-01-11 10:35:44 -05002374#Return created Image in target Color_Space ##
2375
2376#Example
2377#Image 5
2378#Set sRGB
2379 sk_sp<SkColorSpace> normalColorSpace = SkColorSpace::MakeRGB(
2380 SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kSRGB_Gamut);
2381 sk_sp<SkColorSpace> wackyColorSpace = normalColorSpace->makeColorSpin();
2382 for (auto colorSpace : { normalColorSpace, wackyColorSpace } ) {
2383 for (auto transfer : { SkTransferFunctionBehavior::kRespect,
2384 SkTransferFunctionBehavior::kIgnore } ) {
2385 sk_sp<SkImage> colorSpaced = image->makeColorSpace(colorSpace, transfer);
2386 canvas->drawImage(colorSpaced, 0, 0);
2387 canvas->translate(128, 0);
2388 }
2389 canvas->translate(-256, 128);
2390 }
2391##
2392
2393#SeeAlso MakeFromPixture MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05002394
2395#Method ##
2396
2397#Class SkImage ##
2398
2399#Topic Image ##