blob: 0280b317fb09410aa63de75f6bf8df9b36a0e614 [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 Clark0c5f5462017-12-15 11:21:51 -0500421 origin, kOpaque_SkAlphaType, nullptr);
422 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 Clarka560c472017-11-27 10:44:06 -0500820#Enum BitDepth
821
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
841#Enum ##
842
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# ------------------------------------------------------------------------------
1276
1277#Method bool peekPixels(SkPixmap* pixmap) const
1278
Cary Clark4855f782018-02-06 09:41:53 -05001279#Line # returns Pixmap if possible ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001280Copies Image pixel address, row bytes, and Image_Info to pixmap, if address
1281is available, and returns true. If pixel address is not available, return
1282false and leave pixmap unchanged.
Cary Clarka560c472017-11-27 10:44:06 -05001283
Cary Clarkf5404bb2018-01-05 12:10:09 -05001284#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
Cary Clarka560c472017-11-27 10:44:06 -05001285
Cary Clarkf5404bb2018-01-05 12:10:09 -05001286#Return true if Image has direct access to pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001287
1288#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001289 SkBitmap bitmap;
1290 bitmap.allocPixels(SkImageInfo::MakeN32Premul(12, 11));
1291 SkCanvas offscreen(bitmap);
1292 offscreen.clear(SK_ColorWHITE);
1293 SkPaint paint;
1294 offscreen.drawString("%", 1, 10, paint);
1295 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
1296 SkPixmap pixmap;
1297 if (image->peekPixels(&pixmap)) {
1298 const SkPMColor* pixels = pixmap.addr32();
1299 SkPMColor pmWhite = pixels[0];
1300 for (int y = 0; y < image->height(); ++y) {
1301 for (int x = 0; x < image->width(); ++x) {
1302 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
1303 }
1304 SkDebugf("\n");
1305 }
1306 }
1307#StdOut
1308------------
1309--xx----x---
1310-x--x--x----
1311-x--x--x----
1312-x--x-x-----
1313--xx-xx-xx--
1314-----x-x--x-
1315----x--x--x-
1316----x--x--x-
1317---x----xx--
1318------------
1319##
Cary Clarka560c472017-11-27 10:44:06 -05001320##
1321
Cary Clarkf5404bb2018-01-05 12:10:09 -05001322#SeeAlso readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001323
1324#Method ##
1325
1326# ------------------------------------------------------------------------------
1327
1328#Method GrTexture* getTexture() const
Cary Clark2f466242017-12-11 16:03:17 -05001329#Deprecated
Cary Clarka560c472017-11-27 10:44:06 -05001330#Method ##
1331
1332# ------------------------------------------------------------------------------
1333
1334#Method bool isTextureBacked() const
Cary Clark4855f782018-02-06 09:41:53 -05001335#In Property
1336#Line # returns if Image was created from GPU_Texture ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001337Returns true the contents of Image was created on or uploaded to GPU memory,
1338and is available as a GPU_Texture.
Cary Clarka560c472017-11-27 10:44:06 -05001339
Cary Clarkf5404bb2018-01-05 12:10:09 -05001340#Return true if Image is a GPU_Texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001341
1342#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001343#Image 5
1344#Platform gpu
1345auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1346 if (nullptr == image) {
1347 return;
1348 }
1349 SkPaint paint;
1350 paint.setAntiAlias(true);
1351 paint.setTextAlign(SkPaint::kCenter_Align);
1352 canvas->drawImage(image, 0, 0);
1353 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1354 canvas->drawString(image->isTextureBacked() ? "is GPU texture" : "not GPU texture",
1355 image->width() / 2, image->height() * 3 / 4, paint);
1356};
1357sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1358sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1359 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1360drawImage(image, "image");
1361canvas->translate(image->width(), 0);
1362drawImage(bitmapImage, "source");
1363canvas->translate(-image->width(), image->height());
1364drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001365##
1366
Cary Clarkf5404bb2018-01-05 12:10:09 -05001367#SeeAlso MakeFromTexture isValid
Cary Clarka560c472017-11-27 10:44:06 -05001368
1369#Method ##
1370
1371# ------------------------------------------------------------------------------
1372
1373#Method bool isValid(GrContext* context) const
Cary Clark4855f782018-02-06 09:41:53 -05001374#In Property
1375#Line # returns if Image can draw to Raster_Surface or GPU_Context ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001376Returns true if Image can be drawn on either Raster_Surface or GPU_Surface.
1377If context is nullptr, tests if Image draws on Raster_Surface;
1378otherwise, tests if Image draws on GPU_Surface associated with context.
Cary Clarka560c472017-11-27 10:44:06 -05001379
Cary Clarkf5404bb2018-01-05 12:10:09 -05001380Image backed by GPU_Texture may become invalid if associated GrContext is
1381invalid. Lazy_Image may be invalid and may not draw to Raster_Surface or
1382GPU_Surface or both.
Cary Clarka560c472017-11-27 10:44:06 -05001383
Cary Clark61ca7c52018-01-02 11:34:14 -05001384#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05001385
Cary Clarkf5404bb2018-01-05 12:10:09 -05001386#Return true if Image can be drawn ##
Cary Clarka560c472017-11-27 10:44:06 -05001387
1388#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001389#Image 5
1390#Platform gpu
1391auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1392 if (nullptr == image) {
1393 return;
1394 }
1395 SkPaint paint;
1396 paint.setAntiAlias(true);
1397 paint.setTextAlign(SkPaint::kCenter_Align);
1398 canvas->drawImage(image, 0, 0);
1399 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1400 if (canvas->getGrContext()) {
1401 canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" :
1402 "not valid on GPU", image->width() / 2, image->height() * 5 / 8, paint);
1403 }
1404 canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" :
1405 "not valid on CPU", image->width() / 2, image->height() * 7 / 8, paint);
1406};
1407sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1408sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1409 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1410drawImage(image, "image");
1411canvas->translate(image->width(), 0);
1412drawImage(bitmapImage, "source");
1413canvas->translate(-image->width(), image->height());
1414drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001415##
1416
Cary Clarkf5404bb2018-01-05 12:10:09 -05001417#SeeAlso isTextureBacked isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001418
1419#Method ##
1420
1421# ------------------------------------------------------------------------------
1422
1423#Method GrBackendObject getTextureHandle(bool flushPendingGrContextIO,
1424 GrSurfaceOrigin* origin = nullptr) const
Cary Clark4855f782018-02-06 09:41:53 -05001425#Line # returns GPU reference to Image as texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001426
Cary Clark2f466242017-12-11 16:03:17 -05001427Retrieves the back-end API handle of texture. If flushPendingGrContextIO is true,
1428complete deferred I/O operations.
Cary Clarka560c472017-11-27 10:44:06 -05001429
Cary Clark2f466242017-12-11 16:03:17 -05001430If origin in not nullptr, copies location of content drawn into Image.
Cary Clarka560c472017-11-27 10:44:06 -05001431
Cary Clark2f466242017-12-11 16:03:17 -05001432#Param flushPendingGrContextIO flag to flush outstanding requests ##
1433#Param origin storage for one of: kTopLeft_GrSurfaceOrigin,
1434 kBottomLeft_GrSurfaceOrigin; or nullptr
1435##
1436
Cary Clarkac47b882018-01-11 10:35:44 -05001437#Return back-end API texture handle, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001438
1439#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001440#Image 4
Cary Clark2f466242017-12-11 16:03:17 -05001441#Platform gpu
1442GrContext* context = canvas->getGrContext();
1443if (!context) {
1444 return;
1445}
1446SkPaint paint;
1447paint.setAntiAlias(true);
1448SkString str;
Cary Clarkac47b882018-01-11 10:35:44 -05001449int y = -10;
Cary Clark2f466242017-12-11 16:03:17 -05001450for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin } ) {
1451 sk_sp<SkImage> srcImage(SkImage::MakeFromTexture(context,
1452 backEndTexture, origin, kPremul_SkAlphaType, nullptr));
1453 GrSurfaceOrigin readBackOrigin;
1454 GrBackendObject readBackHandle = srcImage->getTextureHandle(false, &readBackOrigin);
Cary Clarkac47b882018-01-11 10:35:44 -05001455 str.printf("readBackHandle: 0x%x", readBackHandle);
1456 canvas->drawString(str, 5, y += 30, paint);
1457 canvas->drawImage(srcImage, 80, y += 10);
Cary Clark2f466242017-12-11 16:03:17 -05001458 str.printf("origin: k%s_GrSurfaceOrigin", readBackOrigin ? "BottomLeft" : "TopLeft");
Cary Clarkac47b882018-01-11 10:35:44 -05001459 canvas->drawString(str, 5, y += srcImage->height() + 10, paint);
Cary Clark2f466242017-12-11 16:03:17 -05001460}
Cary Clarka560c472017-11-27 10:44:06 -05001461##
1462
Cary Clarkac47b882018-01-11 10:35:44 -05001463#Example
1464#Image 5
1465#Platform gpu
1466 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1467 if (nullptr == image) {
1468 return;
1469 }
1470 SkPaint paint;
1471 paint.setAntiAlias(true);
1472 paint.setTextAlign(SkPaint::kCenter_Align);
1473 canvas->drawImage(image, 0, image->height() / 4);
1474 canvas->drawString(label, image->width() / 2, image->height() / 8, paint);
1475 GrSurfaceOrigin readBackOrigin;
1476 GrBackendObject readBackHandle = image->getTextureHandle(false, &readBackOrigin);
1477 canvas->drawString(readBackHandle ? "has readBackHandle" : "no readBackHandle",
1478 image->width() / 2, image->height() * 11 / 8, paint);
1479 };
1480 drawImage(image, "image");
1481 canvas->translate(image->width(), 0);
1482 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1483 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1484 drawImage(textureImage, "backEndTexture");
1485##
1486
1487#SeeAlso MakeFromTexture isTextureBacked
Cary Clarka560c472017-11-27 10:44:06 -05001488
1489#Method ##
1490
1491# ------------------------------------------------------------------------------
1492
1493#Enum CachingHint
1494
1495#Code
1496 enum CachingHint {
1497 kAllow_CachingHint,
1498 kDisallow_CachingHint,
1499 };
1500##
1501
Cary Clarkac47b882018-01-11 10:35:44 -05001502CachingHint selects whether Skia may internally cache Bitmaps generated by
1503decoding Image, or by copying Image from GPU to CPU. The default behavior
1504allows caching Bitmaps.
1505
1506Choose kDisallow_CachingHint if Image pixels are to be used only once, or
1507if Image pixels reside in a cache outside of Skia, or to reduce memory pressure.
1508
1509Choosing kAllow_CachingHint does not ensure that pixels will be cached.
1510Image pixels may not be cached if memory requirements are too large or
1511pixels are not accessible.
Cary Clarka560c472017-11-27 10:44:06 -05001512
1513#Const kAllow_CachingHint 0
Cary Clarkac47b882018-01-11 10:35:44 -05001514Allows Skia to internally cache decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001515##
1516#Const kDisallow_CachingHint 1
Cary Clarkac47b882018-01-11 10:35:44 -05001517Disallows Skia from internally caching decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001518##
1519
Cary Clarkac47b882018-01-11 10:35:44 -05001520#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001521##
1522
Cary Clarkac47b882018-01-11 10:35:44 -05001523#SeeAlso readPixels scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001524
1525#Enum ##
1526
1527# ------------------------------------------------------------------------------
1528
1529#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1530 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark4855f782018-02-06 09:41:53 -05001531#Line # copies and converts pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001532
Cary Clarkac47b882018-01-11 10:35:44 -05001533Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY),
1534and does not exceed Image (width(), height()).
1535
1536dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
1537destination. dstRowBytes specifics the gap from one destination row to the next.
1538Returns true if pixels are copied. Returns false if:
1539#List
1540# dstInfo.addr() equals nullptr ##
1541# dstRowBytes is less than dstInfo.minRowBytes ##
1542# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001543##
1544
Cary Clarkac47b882018-01-11 10:35:44 -05001545Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1546kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1547If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace must match.
1548If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType must
1549match. If Image Color_Space is nullptr, dstInfo.colorSpace must match. Returns
1550false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001551
Cary Clarkac47b882018-01-11 10:35:44 -05001552srcX and srcY may be negative to copy only top or left of source. Returns
1553false if width() or height() is zero or negative.
1554Returns false if
1555#Formula
1556abs(srcX) >= Image width()
1557##
1558, or if
1559#Formula
1560abs(srcY) >= Image height()
1561##
1562.
Cary Clarka560c472017-11-27 10:44:06 -05001563
Cary Clarkac47b882018-01-11 10:35:44 -05001564If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1565If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1566
1567#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1568#Param dstPixels destination pixel storage ##
1569#Param dstRowBytes destination row length ##
1570#Param srcX column index whose absolute value is less than width() ##
1571#Param srcY row index whose absolute value is less than height() ##
1572#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1573
1574#Return true if pixels are copied to dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001575
1576#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001577#Image 3
1578 canvas->scale(.5f, .5f);
1579 const int width = 32;
1580 const int height = 32;
1581 std::vector<int32_t> dstPixels;
1582 dstPixels.resize(height * width * 4);
1583 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1584 for (int y = 0; y < 512; y += height ) {
1585 for (int x = 0; x < 512; x += width ) {
1586 if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) {
1587 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
1588 SkBitmap bitmap;
1589 bitmap.installPixels(dstPixmap);
1590 canvas->drawBitmap(bitmap, 0, 0);
1591 }
1592 canvas->translate(48, 0);
1593 }
1594 canvas->translate(-16 * 48, 48);
1595 }
Cary Clarka560c472017-11-27 10:44:06 -05001596##
1597
Cary Clarkac47b882018-01-11 10:35:44 -05001598#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001599
1600#Method ##
1601
1602# ------------------------------------------------------------------------------
1603
1604#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY,
1605 CachingHint cachingHint = kAllow_CachingHint) const
1606
Cary Clarkac47b882018-01-11 10:35:44 -05001607Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and
1608does not exceed Image (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001609
Cary Clarkac47b882018-01-11 10:35:44 -05001610dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
1611and row bytes of destination. dst.rowBytes specifics the gap from one destination
1612row to the next. Returns true if pixels are copied. Returns false if:
1613#List
1614# dst pixel storage equals nullptr ##
1615# dst.rowBytes is less than SkImageInfo::minRowBytes ##
1616# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001617##
1618
Cary Clarkac47b882018-01-11 10:35:44 -05001619Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1620kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1621If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1622If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1623match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1624false if pixel conversion is not possible.
1625
1626srcX and srcY may be negative to copy only top or left of source. Returns
1627false if width() or height() is zero or negative.
1628Returns false if
1629#Formula
1630abs(srcX) >= Image width()
1631##
1632, or if
1633#Formula
1634abs(srcY) >= Image height()
1635##
1636.
1637
1638If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1639If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1640
1641#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1642#Param srcX column index whose absolute value is less than width() ##
1643#Param srcY row index whose absolute value is less than height() ##
1644#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1645
1646#Return true if pixels are copied to dst ##
1647
1648#Example
1649#Image 3
1650 std::vector<int32_t> srcPixels;
1651 int rowBytes = image->width() * 4;
1652 int quarterWidth = image->width() / 4;
1653 int quarterHeight = image->height() / 4;
1654 srcPixels.resize(image->height() * rowBytes);
1655 for (int y = 0; y < 4; ++y) {
1656 for (int x = 0; x < 4; ++x) {
1657 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1658 &srcPixels.front() + x * image->height() * quarterWidth +
1659 y * quarterWidth, rowBytes);
1660 image->readPixels(pixmap, x * quarterWidth, y * quarterHeight);
1661 }
1662 }
1663 canvas->scale(.5f, .5f);
1664 SkBitmap bitmap;
1665 bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()),
1666 &srcPixels.front(), rowBytes);
1667 canvas->drawBitmap(bitmap, 0, 0);
1668##
1669
1670#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001671
1672#Method ##
1673
1674# ------------------------------------------------------------------------------
1675
1676#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality,
1677 CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark4855f782018-02-06 09:41:53 -05001678#Line # scales and converts one Image to another ##
Cary Clarka560c472017-11-27 10:44:06 -05001679
Cary Clarkac47b882018-01-11 10:35:44 -05001680Copies Image to dst, scaling pixels to fit dst.width() and dst.height(), and
1681converting pixels to match dst.colorType and dst.alphaType. Returns true if
1682pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1683less than dst SkImageInfo::minRowBytes.
Cary Clarka560c472017-11-27 10:44:06 -05001684
Cary Clarkac47b882018-01-11 10:35:44 -05001685Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1686kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1687If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1688If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1689match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1690false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001691
Cary Clarkac47b882018-01-11 10:35:44 -05001692Scales the image, with filterQuality, to match dst.width() and dst.height().
1693filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1694Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1695Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1696Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1697kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1698
1699If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1700If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1701
1702#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1703#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1704 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1705##
1706#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1707
1708#Return true if pixels are scaled to fit dst ##
Cary Clarka560c472017-11-27 10:44:06 -05001709
1710#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001711#Image 3
1712#Height 128
1713 std::vector<int32_t> srcPixels;
1714 int quarterWidth = image->width() / 16;
1715 int rowBytes = quarterWidth * 4;
1716 int quarterHeight = image->height() / 16;
1717 srcPixels.resize(quarterHeight * rowBytes);
1718 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1719 &srcPixels.front(), rowBytes);
1720 canvas->scale(4, 4);
1721 SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality,
1722 kMedium_SkFilterQuality, kHigh_SkFilterQuality };
1723 for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) {
1724 image->scalePixels(pixmap, qualities[index]);
1725 sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
1726 canvas->drawImage(filtered, 16 * index, 0);
1727 }
Cary Clarka560c472017-11-27 10:44:06 -05001728##
1729
Cary Clarkac47b882018-01-11 10:35:44 -05001730#SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001731
1732#Method ##
1733
1734# ------------------------------------------------------------------------------
1735
1736#Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const
1737
Cary Clark4855f782018-02-06 09:41:53 -05001738#Line # returns encoded Image as SkData ##
Cary Clarkac47b882018-01-11 10:35:44 -05001739Encodes Image pixels, returning result as SkData.
Cary Clark2f466242017-12-11 16:03:17 -05001740
Cary Clarkac47b882018-01-11 10:35:44 -05001741Returns nullptr if encoding fails, or if encodedImageFormat is not supported.
Cary Clarka560c472017-11-27 10:44:06 -05001742
Cary Clarkac47b882018-01-11 10:35:44 -05001743Image encoding in a format requires both building with one or more of:
1744SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support
1745for the encoded format.
1746
1747If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can
1748additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP,
1749SkEncodedImageFormat::kGIF.
1750
1751quality is a platform and format specific metric trading off size and encoding
1752error. When used, quality equaling 100 encodes with the least error. quality may
1753be ignored by the encoder.
1754
1755#Param encodedImageFormat one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
1756 SkEncodedImageFormat::kWEBP
1757 ##
1758#Param quality encoder specific metric with 100 equaling best ##
Cary Clarka560c472017-11-27 10:44:06 -05001759
Cary Clark2f466242017-12-11 16:03:17 -05001760#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001761
1762#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001763#Image 3
1764 canvas->scale(4, 4);
1765 SkIRect subset = {0, 0, 16, 64};
1766 int x = 0;
1767 for (int quality : { 0, 10, 50, 100 } ) {
1768 sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
1769 sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
1770 canvas->drawImage(filtered, x, 0);
1771 x += 16;
1772 }
Cary Clarka560c472017-11-27 10:44:06 -05001773##
1774
Cary Clarkac47b882018-01-11 10:35:44 -05001775#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001776
1777#Method ##
1778
1779# ------------------------------------------------------------------------------
1780
Cary Clark61ca7c52018-01-02 11:34:14 -05001781#Method sk_sp<SkData> encodeToData() const
Cary Clarka560c472017-11-27 10:44:06 -05001782
Cary Clarkac47b882018-01-11 10:35:44 -05001783Encodes Image pixels, returning result as SkData. Returns existing encoded data
1784if present; otherwise, Image is encoded with SkEncodedImageFormat::kPNG. Skia
1785must be built with SK_HAS_PNG_LIBRARY to encode Image.
Cary Clarka560c472017-11-27 10:44:06 -05001786
Cary Clarkac47b882018-01-11 10:35:44 -05001787Returns nullptr if existing encoded data is missing or invalid, and
Cary Clarka560c472017-11-27 10:44:06 -05001788encoding fails.
1789
Cary Clarkac47b882018-01-11 10:35:44 -05001790#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001791
1792#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001793#Image 3
1794 canvas->scale(4, 4);
1795 SkIRect subset = {136, 32, 200, 96};
1796 sk_sp<SkData> data(image->encodeToData());
1797 sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
1798 canvas->drawImage(eye, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001799##
1800
Cary Clarkac47b882018-01-11 10:35:44 -05001801#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001802
1803#Method ##
1804
1805# ------------------------------------------------------------------------------
1806
1807#Method sk_sp<SkData> refEncodedData() const
1808
Cary Clark4855f782018-02-06 09:41:53 -05001809#Line # returns Image encoded in SkData if present ##
Cary Clarkac47b882018-01-11 10:35:44 -05001810Returns encoded Image pixels as SkData, if Image was created from supported
1811encoded stream format. Platform support for formats vary and may require building
1812with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY.
Cary Clarka560c472017-11-27 10:44:06 -05001813
Cary Clarkac47b882018-01-11 10:35:44 -05001814Returns nullptr if Image contents are not encoded.
Cary Clarka560c472017-11-27 10:44:06 -05001815
Cary Clarkac47b882018-01-11 10:35:44 -05001816#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001817
1818#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001819#Image 3
1820#Platform gpu
1821 struct {
1822 const char* name;
1823 sk_sp<SkImage> image;
1824 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1825 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1826 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr) } };
1827 SkString string;
1828 SkPaint paint;
1829 for (const auto& test : tests ) {
1830 if (!test.image) {
1831 string.printf("no %s", test.name);
1832 } else {
1833 string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name);
1834 }
1835 canvas->drawString(string, 10, 20, paint);
1836 canvas->translate(0, 20);
1837 }
Cary Clarka560c472017-11-27 10:44:06 -05001838##
1839
Cary Clarkac47b882018-01-11 10:35:44 -05001840#SeeAlso encodeToData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001841
1842#Method ##
1843
1844# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001845#Subtopic Utility
1846#Populate
1847#Line # rarely called management functions ##
1848##
Cary Clarka560c472017-11-27 10:44:06 -05001849
1850#Method const char* toString(SkString* string) const
Cary Clark4855f782018-02-06 09:41:53 -05001851#In Utility
1852#Line # converts Image to machine readable form ##
Cary Clarkac47b882018-01-11 10:35:44 -05001853Appends Image description to string, including unique ID, width, height, and
1854whether the image is opaque.
Cary Clarka560c472017-11-27 10:44:06 -05001855
Cary Clarkac47b882018-01-11 10:35:44 -05001856#Param string storage for description; existing content is preserved ##
1857
1858#Return string appended with Image description ##
Cary Clarka560c472017-11-27 10:44:06 -05001859
1860#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001861#Image 4
1862 struct {
1863 const char* name;
1864 sk_sp<SkImage> image;
1865 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1866 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1867 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr) } };
1868 SkString string;
1869 SkPaint paint;
1870 for (const auto& test : tests ) {
1871 string.printf("%s: ", test.name);
1872 test.image ? (void) test.image->toString(&string) : string.append("no image");
1873 canvas->drawString(string, 10, 20, paint);
1874 canvas->translate(0, 20);
1875 }
Cary Clarka560c472017-11-27 10:44:06 -05001876##
1877
Cary Clarkac47b882018-01-11 10:35:44 -05001878#SeeAlso SkPaint::toString
Cary Clarka560c472017-11-27 10:44:06 -05001879
1880#Method ##
1881
1882# ------------------------------------------------------------------------------
1883
1884#Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const
Cary Clark4855f782018-02-06 09:41:53 -05001885#In Constructor
1886#Line # creates Image containing part of original ##
Cary Clarkac47b882018-01-11 10:35:44 -05001887Returns subset of Image. subset must be fully contained by Image dimensions().
1888The implementation may share pixels, or may copy them.
Cary Clarka560c472017-11-27 10:44:06 -05001889
Cary Clarkac47b882018-01-11 10:35:44 -05001890Returns nullptr if subset is empty, or subset is not contained by bounds, or
1891pixels in Image could not be read or copied.
Cary Clarka560c472017-11-27 10:44:06 -05001892
Cary Clarkac47b882018-01-11 10:35:44 -05001893#Param subset bounds of returned Image ##
1894
1895#Return partial or full Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001896
1897#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001898#Image 3
1899 canvas->scale(.5f, .5f);
1900 const int width = 32;
1901 const int height = 32;
1902 for (int y = 0; y < 512; y += height ) {
1903 for (int x = 0; x < 512; x += width ) {
1904 sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height}));
1905 canvas->drawImage(subset, x * 3 / 2, y * 3 / 2);
1906 }
1907 }
Cary Clarka560c472017-11-27 10:44:06 -05001908##
1909
Cary Clarkac47b882018-01-11 10:35:44 -05001910#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001911
1912#Method ##
1913
1914# ------------------------------------------------------------------------------
1915
1916#Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const
Cary Clark4855f782018-02-06 09:41:53 -05001917#In Constructor
1918#Line # creates Image matching Color_Space if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001919Returns Image backed by GPU_Texture associated with context. Returned Image is
1920compatible with Surface created with dstColorSpace. Returns original
1921Image if context and dstColorSpace match.
1922
1923Returns nullptr if context is nullptr, or if Image was created with another
1924GrContext.
Cary Clarka560c472017-11-27 10:44:06 -05001925
Cary Clark61ca7c52018-01-02 11:34:14 -05001926#Param context GPU_Context ##
Cary Clarkac47b882018-01-11 10:35:44 -05001927#Param dstColorSpace range of colors of matching Surface on GPU ##
Cary Clarka560c472017-11-27 10:44:06 -05001928
Cary Clarkac47b882018-01-11 10:35:44 -05001929#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001930
1931#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001932#Platform gpu
1933#Image 5
1934 auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void {
1935 if (nullptr == image || nullptr == context) {
1936 return;
1937 }
1938 SkPaint paint;
1939 paint.setAntiAlias(true);
1940 paint.setTextAlign(SkPaint::kCenter_Align);
1941 sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr));
1942 canvas->drawImage(texture, 0, 0);
1943 canvas->drawString(label, texture->width() / 2, texture->height() / 4, paint);
1944 };
1945 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1946 GrContext* context = canvas->getGrContext();
1947 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture,
1948 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1949 drawImage(image, context, "image");
1950 canvas->translate(image->width(), 0);
1951 drawImage(bitmapImage, context, "source");
1952 canvas->translate(-image->width(), image->height());
1953 drawImage(textureImage, context, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001954##
1955
Cary Clarkac47b882018-01-11 10:35:44 -05001956#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05001957
1958#Method ##
1959
1960# ------------------------------------------------------------------------------
1961
1962#Method sk_sp<SkImage> makeNonTextureImage() const
Cary Clark4855f782018-02-06 09:41:53 -05001963#In Constructor
1964#Line # creates Image without dependency on GPU_Texture ##
Cary Clarkac47b882018-01-11 10:35:44 -05001965Returns Raster_Image or Lazy_Image. Copies Image backed by GPU_Texture into
Cary Clark4855f782018-02-06 09:41:53 -05001966CPU memory if needed. Returns original Image if decoded in Raster_Bitmap,
Cary Clarkac47b882018-01-11 10:35:44 -05001967or if encoded in a stream.
Cary Clark61ca7c52018-01-02 11:34:14 -05001968
Cary Clarkac47b882018-01-11 10:35:44 -05001969Returns nullptr if backed by GPU_Texture and copy fails.
1970
1971#Return Raster_Image, Lazy_Image, or nullptr ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001972
1973#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001974#Image 5
1975#Platform gpu
1976 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1977 if (nullptr == image) {
1978 return;
1979 }
1980 SkPaint paint;
1981 paint.setAntiAlias(true);
1982 paint.setTextAlign(SkPaint::kCenter_Align);
1983 sk_sp<SkImage> nonTexture(image->makeNonTextureImage());
1984 canvas->drawImage(nonTexture, 0, 0);
1985 canvas->drawString(label, nonTexture->width() / 2, nonTexture->height() / 4, paint);
1986 };
1987 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1988 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1989 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1990 drawImage(image, "image");
1991 canvas->translate(image->width(), 0);
1992 drawImage(bitmapImage, "source");
1993 canvas->translate(-image->width(), image->height());
1994 drawImage(textureImage, "backEndTexture");
Cary Clark61ca7c52018-01-02 11:34:14 -05001995##
1996
1997#SeeAlso incomplete
1998
1999#Method ##
2000
2001# ------------------------------------------------------------------------------
2002
2003#Method sk_sp<SkImage> makeRasterImage() const
Cary Clark4855f782018-02-06 09:41:53 -05002004#In Constructor
2005#Line # creates Image compatible with Raster_Surface if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05002006Returns Raster_Image. Copies Image backed by GPU_Texture into CPU memory,
Cary Clark4855f782018-02-06 09:41:53 -05002007or decodes Image from Lazy_Image. Returns original Image if decoded in
Cary Clarkac47b882018-01-11 10:35:44 -05002008Raster_Bitmap.
Cary Clarka560c472017-11-27 10:44:06 -05002009
Cary Clarkac47b882018-01-11 10:35:44 -05002010Returns nullptr if copy, decode, or pixel read fails.
Cary Clarka560c472017-11-27 10:44:06 -05002011
Cary Clarkac47b882018-01-11 10:35:44 -05002012#Return Raster_Image, or nullptr ##
2013
Cary Clark4855f782018-02-06 09:41:53 -05002014#Bug 7479
Cary Clarka560c472017-11-27 10:44:06 -05002015#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002016#Image 5
2017#Platform gpu
2018 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2019 if (nullptr == image) {
2020 return;
2021 }
2022 SkPaint paint;
2023 paint.setAntiAlias(true);
2024 paint.setTextAlign(SkPaint::kCenter_Align);
2025 sk_sp<SkImage> raster(image->makeRasterImage());
2026 canvas->drawImage(raster, 0, 0);
2027 canvas->drawString(label, raster->width() / 2, raster->height() / 4, paint);
2028 };
2029 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2030 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2031 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2032 drawImage(image, "image");
2033 canvas->translate(image->width(), 0);
2034 drawImage(bitmapImage, "source");
2035 canvas->translate(-image->width(), image->height());
2036 drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05002037##
2038
Cary Clarkac47b882018-01-11 10:35:44 -05002039#SeeAlso isTextureBacked isLazyGenerated MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -05002040
2041#Method ##
2042
2043# ------------------------------------------------------------------------------
2044
2045#Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
2046 const SkIRect& clipBounds, SkIRect* outSubset,
2047 SkIPoint* offset) const
Cary Clark4855f782018-02-06 09:41:53 -05002048#In Constructor
2049#Line # creates filtered, clipped Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002050
Cary Clarkac47b882018-01-11 10:35:44 -05002051Creates filtered Image. filter processes original Image, potentially changing
2052color, position, and size. subset is the bounds of original Image processed
2053by filter. clipBounds is the expected bounds of the filtered Image. outSubset
2054is required storage for the actual bounds of the filtered Image. offset is
2055required storage for translation of returned Image.
Cary Clarka560c472017-11-27 10:44:06 -05002056
Cary Clarkac47b882018-01-11 10:35:44 -05002057Returns nullptr if Image could not be created. If nullptr is returned, outSubset
2058and offset are undefined.
2059
2060makeWithFilter is optimized to support Image backed by GPU_Texture drawn in an
2061animation with SkImageFilter that vary in size from one frame to the next. The
2062created Image is drawn at an increased size so that GPU_Texture can be reused
2063with different sized effects. outSubset describes the valid bounds of GPU_Texture
2064returned. The returned Image may be much larger than required for the filter.
2065offset translates the returned Image to keep subsequent animation frames
2066aligned with respect to each other.
2067
2068#Param filter how Image is sampled when transformed ##
Cary Clarka560c472017-11-27 10:44:06 -05002069#Param subset incomplete ##
2070#Param clipBounds incomplete ##
2071#Param outSubset incomplete ##
2072#Param offset incomplete ##
2073
Cary Clarkac47b882018-01-11 10:35:44 -05002074#Return filtered Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05002075
2076#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002077#Description
2078In each frame of the animation, filtered Image is drawn in a different location.
2079By translating canvas by returned offset, Image appears stationary.
2080##
2081#Image 5
2082#Platform gpu
2083#Duration 5
2084 sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make(
2085 -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE,
2086 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
2087 nullptr);
2088 sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr);
2089 SkIRect subset = image->bounds();
2090 SkIRect clipBounds = image->bounds();
2091 clipBounds.outset(60, 60);
2092 SkIRect outSubset;
2093 SkIPoint offset;
2094 sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds,
2095 &outSubset, &offset));
2096 SkPaint paint;
2097 paint.setAntiAlias(true);
2098 paint.setStyle(SkPaint::kStroke_Style);
2099 canvas->drawLine(0, 0, offset.fX, offset.fY, paint);
2100 canvas->translate(offset.fX, offset.fY);
2101 canvas->drawImage(filtered, 0, 0);
2102 canvas->drawRect(SkRect::MakeFromIRect(outSubset), paint);
Cary Clarka560c472017-11-27 10:44:06 -05002103##
2104
Cary Clarkac47b882018-01-11 10:35:44 -05002105#SeeAlso SkPaint::setImageFilter
Cary Clarka560c472017-11-27 10:44:06 -05002106
2107#Method ##
2108
2109# ------------------------------------------------------------------------------
2110
Cary Clark61ca7c52018-01-02 11:34:14 -05002111#Struct DeferredTextureImageUsageParams
Cary Clark4855f782018-02-06 09:41:53 -05002112#Deprecated soon
Cary Clark61ca7c52018-01-02 11:34:14 -05002113
Cary Clark4855f782018-02-06 09:41:53 -05002114Used only by Chrome.
Cary Clark61ca7c52018-01-02 11:34:14 -05002115
2116##
2117
2118#Method size_t getDeferredTextureImageData(const GrContextThreadSafeProxy& contextThreadSafeProxy,
2119 const DeferredTextureImageUsageParams deferredTextureImageUsageParams[],
2120 int paramCnt,
2121 void* buffer,
2122 SkColorSpace* dstColorSpace = nullptr,
2123 SkColorType dstColorType = kN32_SkColorType) const
Cary Clark4855f782018-02-06 09:41:53 -05002124#Deprecated soon
Cary Clark61ca7c52018-01-02 11:34:14 -05002125
Cary Clark4855f782018-02-06 09:41:53 -05002126Used only by Chrome.
Cary Clark61ca7c52018-01-02 11:34:14 -05002127##
2128
2129#Method static sk_sp<SkImage> MakeFromDeferredTextureImageData(GrContext* context, const void* data,
2130 SkBudgeted budgeted)
Cary Clark4855f782018-02-06 09:41:53 -05002131#Deprecated soon
Cary Clark61ca7c52018-01-02 11:34:14 -05002132
Cary Clark4855f782018-02-06 09:41:53 -05002133Used only by Chrome.
Cary Clark61ca7c52018-01-02 11:34:14 -05002134##
2135
2136# ------------------------------------------------------------------------------
2137
Cary Clarka560c472017-11-27 10:44:06 -05002138#Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc
2139
2140##
2141
2142# ------------------------------------------------------------------------------
2143
2144#Method static bool MakeBackendTextureFromSkImage(GrContext* context,
2145 sk_sp<SkImage> image,
2146 GrBackendTexture* backendTexture,
2147 BackendTextureReleaseProc* backendTextureReleaseProc)
Cary Clark4855f782018-02-06 09:41:53 -05002148#In Constructor
2149#Line # creates GPU_Texture from Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002150
2151Creates a GrBackendTexture from the provided SkImage. Returns true on success. The
2152GrBackendTexture and BackendTextureReleaseProc are populated on success. It is the callers
2153responsibility to call the BackendTextureReleaseProc once they have deleted the texture.
2154Note that the BackendTextureReleaseProc allows Skia to clean up auxiliary data related
2155to the GrBackendTexture, and is not a substitute for the client deleting the GrBackendTexture
2156themselves.
2157
2158If image is both texture backed and singly referenced; that is, its only
2159reference was transferred using std::move(): image is returned in backendTexture
2160without conversion or making a copy.
2161
Cary Clark4855f782018-02-06 09:41:53 -05002162If Image is not texture backed, this function returns texture with Image
2163contents.
Cary Clarka560c472017-11-27 10:44:06 -05002164
Cary Clark61ca7c52018-01-02 11:34:14 -05002165#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05002166#Param image incomplete ##
2167#Param backendTexture incomplete ##
2168#Param backendTextureReleaseProc incomplete ##
2169
2170#Return incomplete ##
2171
2172#Example
2173// incomplete
2174##
2175
2176#SeeAlso incomplete
2177
2178#Method ##
2179
2180# ------------------------------------------------------------------------------
2181
2182#Enum LegacyBitmapMode
2183
2184#Code
2185 enum LegacyBitmapMode {
2186 kRO_LegacyBitmapMode,
2187 kRW_LegacyBitmapMode,
2188 };
2189##
2190
2191Helper functions to convert to SkBitmap
2192
2193#Const kRO_LegacyBitmapMode 0
2194##
2195#Const kRW_LegacyBitmapMode 1
2196##
2197
2198#Example
2199// incomplete
2200##
2201
2202#SeeAlso incomplete
2203
2204#Enum ##
2205
2206# ------------------------------------------------------------------------------
2207
2208#Method bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode) const
Cary Clark4855f782018-02-06 09:41:53 -05002209#In Constructor
2210#Line # returns as Raster_Bitmap ##
Cary Clarkac47b882018-01-11 10:35:44 -05002211Creates raster Bitmap with same pixels as Image. If legacyBitmapMode is
2212kRO_LegacyBitmapMode, returned bitmap is read-only and immutable.
2213Returns true if Bitmap is stored in bitmap. Returns false and resets bitmap if
2214Bitmap write did not succeed.
Cary Clarka560c472017-11-27 10:44:06 -05002215
Cary Clark3cd22cc2017-12-01 11:49:58 -05002216#Param bitmap storage for legacy Bitmap ##
2217#Param legacyBitmapMode one of: kRO_LegacyBitmapMode, kRW_LegacyBitmapMode ##
Cary Clarka560c472017-11-27 10:44:06 -05002218
Cary Clark3cd22cc2017-12-01 11:49:58 -05002219#Return true if Bitmap was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002220
2221#Example
2222// incomplete
2223##
2224
2225#SeeAlso incomplete
2226
2227#Method ##
2228
2229# ------------------------------------------------------------------------------
2230
2231#Method bool isLazyGenerated() const
Cary Clark4855f782018-02-06 09:41:53 -05002232#In Property
2233#Line # returns if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002234Returns true if Image is backed by an image-generator or other service that creates
2235and caches its pixels or texture on-demand.
2236
Cary Clark2f466242017-12-11 16:03:17 -05002237#Return true if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002238
2239#Example
Cary Clark2f466242017-12-11 16:03:17 -05002240#Height 80
2241#Function
2242class TestImageGenerator : public SkImageGenerator {
2243public:
2244 TestImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(10, 10)) {}
2245 ~TestImageGenerator() override {}
2246protected:
2247 bool onGetPixels(const SkImageInfo& info, void* pixelPtr, size_t rowBytes,
2248 const Options& options) override {
2249 SkPMColor* pixels = static_cast<SkPMColor*>(pixelPtr);
2250 for (int y = 0; y < info.height(); ++y) {
2251 for (int x = 0; x < info.width(); ++x) {
2252 pixels[y * info.width() + x] = 0xff223344 + y * 0x000C0811;
2253 }
2254 }
2255 return true;
2256 }
2257};
2258##
2259void draw(SkCanvas* canvas) {
2260 auto gen = std::unique_ptr<TestImageGenerator>(new TestImageGenerator());
2261 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
2262 SkString lazy(image->isLazyGenerated() ? "is lazy" : "not lazy");
2263 canvas->scale(8, 8);
2264 canvas->drawImage(image, 0, 0, nullptr);
2265 SkPaint paint;
2266 paint.setTextSize(4);
2267 canvas->drawString(lazy, 2, 5, paint);
2268}
Cary Clarka560c472017-11-27 10:44:06 -05002269##
2270
Cary Clarkf5404bb2018-01-05 12:10:09 -05002271#Example
2272#Image 5
2273#Platform gpu
2274void draw(SkCanvas* canvas) {
2275 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2276 if (nullptr == image) {
2277 return;
2278 }
2279 SkPaint paint;
2280 paint.setAntiAlias(true);
2281 paint.setTextAlign(SkPaint::kCenter_Align);
2282 canvas->drawImage(image, 0, 0);
2283 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
2284 canvas->drawString(
2285 image->isLazyGenerated() ? "is lazily generated" : "not lazily generated",
2286 image->width() / 2, image->height() * 3 / 4, paint);
2287 };
2288 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2289 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2290 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2291 drawImage(image, "image");
2292 canvas->translate(image->width(), 0);
2293 drawImage(bitmapImage, "source");
2294 canvas->translate(-image->width(), image->height());
2295 drawImage(textureImage, "backEndTexture");
2296}
2297##
2298
Cary Clarkac47b882018-01-11 10:35:44 -05002299#SeeAlso isTextureBacked MakeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002300
2301#Method ##
2302
2303# ------------------------------------------------------------------------------
2304
2305#Method sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target,
2306 SkTransferFunctionBehavior premulBehavior) const
Cary Clark4855f782018-02-06 09:41:53 -05002307#In Constructor
2308#Line # creates Image matching Color_Space if possible ##
Cary Clarka560c472017-11-27 10:44:06 -05002309
Cary Clarkac47b882018-01-11 10:35:44 -05002310Creates Image in target Color_Space.
2311Returns nullptr if Image could not be created.
Cary Clarka560c472017-11-27 10:44:06 -05002312
Cary Clarkac47b882018-01-11 10:35:44 -05002313Returns original Image if it is in target Color_Space.
2314Otherwise, converts pixels from Image Color_Space to target Color_Space.
2315If Image colorSpace returns nullptr, Image Color_Space is assumed to be sRGB.
2316
2317SkTransferFunctionBehavior is to be deprecated.
2318
2319Set premulBehavior to SkTransferFunctionBehavior::kRespect to convert Image
2320pixels to a linear space, before converting to destination Color_Type
Cary Clarka560c472017-11-27 10:44:06 -05002321and Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -05002322
Cary Clarkac47b882018-01-11 10:35:44 -05002323Set premulBehavior to SkTransferFunctionBehavior::kIgnore to treat Image
2324pixels as linear, when converting to destination Color_Type
2325and Color_Space, ignoring pixel encoding.
Cary Clarka560c472017-11-27 10:44:06 -05002326
Cary Clarkac47b882018-01-11 10:35:44 -05002327#Param target Color_Space describing color range of returned Image ##
2328#Param premulBehavior one of: SkTransferFunctionBehavior::kRespect,
2329 SkTransferFunctionBehavior::kIgnore
Cary Clarka560c472017-11-27 10:44:06 -05002330##
2331
Cary Clarkac47b882018-01-11 10:35:44 -05002332#Return created Image in target Color_Space ##
2333
2334#Example
2335#Image 5
2336#Set sRGB
2337 sk_sp<SkColorSpace> normalColorSpace = SkColorSpace::MakeRGB(
2338 SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kSRGB_Gamut);
2339 sk_sp<SkColorSpace> wackyColorSpace = normalColorSpace->makeColorSpin();
2340 for (auto colorSpace : { normalColorSpace, wackyColorSpace } ) {
2341 for (auto transfer : { SkTransferFunctionBehavior::kRespect,
2342 SkTransferFunctionBehavior::kIgnore } ) {
2343 sk_sp<SkImage> colorSpaced = image->makeColorSpace(colorSpace, transfer);
2344 canvas->drawImage(colorSpaced, 0, 0);
2345 canvas->translate(128, 0);
2346 }
2347 canvas->translate(-256, 128);
2348 }
2349##
2350
2351#SeeAlso MakeFromPixture MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05002352
2353#Method ##
2354
2355#Class SkImage ##
2356
2357#Topic Image ##