blob: 7aad9ae89393c7a072b03d190741dd2d9563a786 [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,
Cary Clark61ca7c52018-01-02 11:34:14 -0500358 SkColorType colorType,
359 SkAlphaType alphaType,
360 sk_sp<SkColorSpace> colorSpace)
Cary Clarkf895a422018-02-27 09:54:21 -0500361#In Constructor
362#Line # creates Image from GPU_Texture ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500363Creates Image from GPU_Texture associated with context. Caller is responsible for
364managing the lifetime of GPU_Texture.
365
366Image is returned if format of backendTexture is recognized and supported.
367Recognized formats vary by GPU back-end.
368
369#Param context GPU_Context ##
370#Param backendTexture texture residing on GPU ##
371#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500372#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500373##
Cary Clark681287e2018-03-16 11:34:15 -0400374#Param alphaType one of: #list_of_alpha_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500375##
376#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500377
378#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500379
380#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500381#Image 3
382#Platform gpu
383#Height 128
384#Description
385A back-end texture has been created and uploaded to the GPU outside of this example.
386##
387GrContext* context = canvas->getGrContext();
388if (!context) {
389 return;
390}
391canvas->scale(.25f, .25f);
392int x = 0;
393for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500394 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark56356312018-02-08 14:45:18 -0500395 origin, kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
Cary Clark0c5f5462017-12-15 11:21:51 -0500396 canvas->drawImage(image, x, 0);
397x += 512;
398}
Cary Clarka560c472017-11-27 10:44:06 -0500399##
400
Cary Clark3cd22cc2017-12-01 11:49:58 -0500401#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500402
403#Method ##
404
405# ------------------------------------------------------------------------------
406
407#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
408 const GrBackendTexture& backendTexture,
409 GrSurfaceOrigin origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500410 SkColorType colorType,
Cary Clarka560c472017-11-27 10:44:06 -0500411 SkAlphaType alphaType,
412 sk_sp<SkColorSpace> colorSpace,
413 TextureReleaseProc textureReleaseProc,
414 ReleaseContext releaseContext)
415
Cary Clark61ca7c52018-01-02 11:34:14 -0500416Creates Image from GPU_Texture associated with context. GPU_Texture must stay
Cary Clark3cd22cc2017-12-01 11:49:58 -0500417valid and unchanged until textureReleaseProc is called. textureReleaseProc is
418passed releaseContext when Image is deleted or no longer refers to texture.
Cary Clarka560c472017-11-27 10:44:06 -0500419
Cary Clark3cd22cc2017-12-01 11:49:58 -0500420Image is returned if format of backendTexture is recognized and supported.
421Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500422
Cary Clark3cd22cc2017-12-01 11:49:58 -0500423#Param context GPU_Context ##
424#Param backendTexture texture residing on GPU ##
425#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500426#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500427##
Cary Clark681287e2018-03-16 11:34:15 -0400428#Param alphaType one of: #list_of_alpha_types#
Cary Clark3cd22cc2017-12-01 11:49:58 -0500429##
Cary Clark61ca7c52018-01-02 11:34:14 -0500430#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500431#Param textureReleaseProc function called when texture can be released ##
432#Param releaseContext state passed to textureReleaseProc ##
433
434#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500435
Cary Clark0c5f5462017-12-15 11:21:51 -0500436#ToDo
437This doesn't do anything clever with TextureReleaseProc because it may not get called
Cary Clark61ca7c52018-01-02 11:34:14 -0500438fwithin the lifetime of the example
Cary Clark0c5f5462017-12-15 11:21:51 -0500439##
440
Cary Clarka560c472017-11-27 10:44:06 -0500441#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500442#Platform gpu
443#Image 4
Cary Clark0c5f5462017-12-15 11:21:51 -0500444GrContext* context = canvas->getGrContext();
445if (!context) {
446 return;
447}
Cary Clarkac47b882018-01-11 10:35:44 -0500448auto debugster = [](SkImage::ReleaseContext releaseContext) -> void {
449 *((int *) releaseContext) += 128;
Cary Clark0c5f5462017-12-15 11:21:51 -0500450};
451int x = 0;
452for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500453 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark61ca7c52018-01-02 11:34:14 -0500454 origin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr, debugster, &x);
Cary Clark0c5f5462017-12-15 11:21:51 -0500455 canvas->drawImage(image, x, 0);
456 x += 128;
457}
Cary Clarka560c472017-11-27 10:44:06 -0500458##
459
Cary Clark3cd22cc2017-12-01 11:49:58 -0500460#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500461
462#Method ##
463
464# ------------------------------------------------------------------------------
465
466#Method static sk_sp<SkImage> MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> data,
467 bool buildMips,
468 SkColorSpace* dstColorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500469#In Constructor
470#Line # creates Image from encoded data, and uploads to GPU ##
Cary Clarka560c472017-11-27 10:44:06 -0500471
Cary Clark3cd22cc2017-12-01 11:49:58 -0500472Creates Image from encoded data. Image is uploaded to GPU back-end using context.
473
474Created Image is available to other GPU contexts, and is available across thread
475boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
476share resources.
477
478When Image is no longer referenced, context releases texture memory
Cary Clarka560c472017-11-27 10:44:06 -0500479asynchronously.
Cary Clarka560c472017-11-27 10:44:06 -0500480
Cary Clark3cd22cc2017-12-01 11:49:58 -0500481Texture decoded from data is uploaded to match Surface created with
482dstColorSpace. Color_Space of Image is determined by encoded data.
Cary Clarka560c472017-11-27 10:44:06 -0500483
Cary Clark3cd22cc2017-12-01 11:49:58 -0500484Image is returned if format of data is recognized and supported, and if context
485supports moving resources. Recognized formats vary by platform and GPU back-end.
486
Cary Clark61ca7c52018-01-02 11:34:14 -0500487Image is returned using MakeFromEncoded if context is nullptr or does not support
488moving resources between contexts.
489
Cary Clark3cd22cc2017-12-01 11:49:58 -0500490#Param context GPU_Context ##
491#Param data Image to decode ##
492#Param buildMips create Image as Mip_Map if true ##
493#Param dstColorSpace range of colors of matching Surface on GPU ##
494
495#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500496
497#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500498#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500499#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500500GrContext* context = canvas->getGrContext();
501sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, 100);
502sk_sp<SkImage> image = SkImage::MakeCrossContextFromEncoded(context,
503 encodedData, false, nullptr);
504canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500505##
506
Cary Clark3cd22cc2017-12-01 11:49:58 -0500507#SeeAlso MakeCrossContextFromPixmap
508
509#Method ##
510
511# ------------------------------------------------------------------------------
512
513#Method static sk_sp<SkImage> MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap,
514 bool buildMips,
515 SkColorSpace* dstColorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500516#In Constructor
517#Line # creates Image from Pixmap, and uploads to GPU ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500518
519Creates Image from pixmap. Image is uploaded to GPU back-end using context.
520
521Created Image is available to other GPU contexts, and is available across thread
522boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
523share resources.
524
525When Image is no longer referenced, context releases texture memory
526asynchronously.
527
528Texture created from pixmap is uploaded to match Surface created with
529dstColorSpace. Color_Space of Image is determined by pixmap.colorSpace().
530
Cary Clark61ca7c52018-01-02 11:34:14 -0500531Image is returned referring to GPU back-end if context is not nullptr,
532format of data is recognized and supported, and if context supports moving
533resources between contexts. Otherwise, pixmap pixel data is copied and Image
534as returned in raster format if possible; nullptr may be returned.
535Recognized GPU formats vary by platform and GPU back-end.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500536
537#Param context GPU_Context ##
538#Param pixmap Image_Info, pixel address, and row bytes ##
539#Param buildMips create Image as Mip_Map if true ##
540#Param dstColorSpace range of colors of matching Surface on GPU ##
541
542#Return created Image, or nullptr ##
543
544#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500545#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500546#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500547GrContext* context = canvas->getGrContext();
548SkPixmap pixmap;
549if (source.peekPixels(&pixmap)) {
550 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(context, pixmap,
551 false, nullptr);
552 canvas->drawImage(image, 0, 0);
553}
Cary Clark3cd22cc2017-12-01 11:49:58 -0500554##
555
556#SeeAlso MakeCrossContextFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500557
558#Method ##
559
560# ------------------------------------------------------------------------------
561
562#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
563 const GrBackendTexture& backendTexture,
564 GrSurfaceOrigin surfaceOrigin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500565 SkColorType colorType,
566 SkAlphaType alphaType = kPremul_SkAlphaType,
567 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500568#In Constructor
569#Line # creates Image from GPU_Texture, managed internally ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500570Creates Image from backendTexture associated with context. backendTexture and
571returned Image are managed internally, and are released when no longer needed.
Cary Clarka560c472017-11-27 10:44:06 -0500572
Cary Clark3cd22cc2017-12-01 11:49:58 -0500573Image is returned if format of backendTexture is recognized and supported.
574Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500575
Cary Clark3cd22cc2017-12-01 11:49:58 -0500576#Param context GPU_Context ##
577#Param backendTexture texture residing on GPU ##
578#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500579#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500580##
Cary Clark681287e2018-03-16 11:34:15 -0400581#Param alphaType one of: #list_of_alpha_types#
Cary Clark3cd22cc2017-12-01 11:49:58 -0500582##
Cary Clark61ca7c52018-01-02 11:34:14 -0500583#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500584
585#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500586
587#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500588#Image 5
589#Platform gpu
Cary Clark61ca7c52018-01-02 11:34:14 -0500590 if (!canvas->getGrContext()) {
591 return;
592 }
593 canvas->scale(.5f, .5f);
594 canvas->clear(0x7f3f5f7f);
595 int x = 0, y = 0;
596 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
597 for (auto alpha : { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType } ) {
598 sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(canvas->getGrContext(),
599 backEndTexture, origin,
600 kRGBA_8888_SkColorType, alpha);
601 canvas->drawImage(image, x, y);
602 x += 160;
603 }
604 x -= 160 * 3;
605 y += 256;
606 }
Cary Clarka560c472017-11-27 10:44:06 -0500607##
608
Cary Clark61ca7c52018-01-02 11:34:14 -0500609#SeeAlso MakeFromTexture MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500610
611#Method ##
612
613# ------------------------------------------------------------------------------
614
615#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400616 const GrBackendTexture yuvTextures[3],
Cary Clarka560c472017-11-27 10:44:06 -0500617 GrSurfaceOrigin surfaceOrigin,
618 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500619#In Constructor
620#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500621
Brian Salomon6a426c12018-03-15 12:16:02 -0400622Creates Image from copy of yuvTextures, an array of textures on GPU.
623yuvTextures contain pixels for YUV planes of Image. Returned Image has the dimensions
624yuvTextures[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500625
Cary Clark61ca7c52018-01-02 11:34:14 -0500626#Param context GPU_Context ##
627#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
628 kRec709_SkYUVColorSpace
629##
Brian Salomon6a426c12018-03-15 12:16:02 -0400630#Param yuvTextures array of YUV textures on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500631#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
632#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500633
Cary Clark61ca7c52018-01-02 11:34:14 -0500634#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500635
Cary Clark61ca7c52018-01-02 11:34:14 -0500636# seems too complicated to create an example for this
637#ToDo
638should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500639##
640
Cary Clark61ca7c52018-01-02 11:34:14 -0500641#NoExample
642##
643
644#SeeAlso MakeFromNV12TexturesCopy
645
646#Method ##
647
648# ------------------------------------------------------------------------------
649
Cary Clarka560c472017-11-27 10:44:06 -0500650#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
651 SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400652 const GrBackendTexture nv12Textures[2],
Cary Clarka560c472017-11-27 10:44:06 -0500653 GrSurfaceOrigin surfaceOrigin,
654 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500655#In Constructor
Brian Salomon6a426c12018-03-15 12:16:02 -0400656#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500657
Cary Clark681287e2018-03-16 11:34:15 -0400658Creates Image from copy of nv12Textures, an array of textures on GPU.
Brian Salomon6a426c12018-03-15 12:16:02 -0400659nv12Textures[0] contains pixels for YUV_Component_Y plane.
660nv12Textures[1] contains pixels for YUV_Component_U plane,
Cary Clark61ca7c52018-01-02 11:34:14 -0500661followed by pixels for YUV_Component_V plane.
Cary Clark681287e2018-03-16 11:34:15 -0400662Returned Image has the dimensions nv12Textures[2].
663yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500664
Cary Clark61ca7c52018-01-02 11:34:14 -0500665#Param context GPU_Context ##
666#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
667 kRec709_SkYUVColorSpace
668##
Brian Salomon6a426c12018-03-15 12:16:02 -0400669#Param nv12Textures array of YUV textures on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500670#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
671#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500672
Cary Clark61ca7c52018-01-02 11:34:14 -0500673#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500674
Cary Clark61ca7c52018-01-02 11:34:14 -0500675# seems too complicated to create an example for this
676#ToDo
677should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500678##
679
Cary Clark61ca7c52018-01-02 11:34:14 -0500680#NoExample
681##
682
683#SeeAlso MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500684
685#Method ##
686
687# ------------------------------------------------------------------------------
688
Cary Clark4855f782018-02-06 09:41:53 -0500689# currently uncalled by any test or client ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500690#Bug 7424
Cary Clark61ca7c52018-01-02 11:34:14 -0500691
Cary Clark56356312018-02-08 14:45:18 -0500692#EnumClass BitDepth
Cary Clarka560c472017-11-27 10:44:06 -0500693
694#Code
Cary Clark61ca7c52018-01-02 11:34:14 -0500695 enum class BitDepth {
Cary Clarka560c472017-11-27 10:44:06 -0500696 kU8,
697 kF16,
698 };
699##
700
701#Const kU8 0
Cary Clark61ca7c52018-01-02 11:34:14 -0500702Use 8 bits per Color_ARGB component using unsigned integer format.
Cary Clarka560c472017-11-27 10:44:06 -0500703##
704#Const kF16 1
Cary Clark61ca7c52018-01-02 11:34:14 -0500705Use 16 bits per Color_ARGB component using half-precision floating point format.
Cary Clarka560c472017-11-27 10:44:06 -0500706##
707
Cary Clark61ca7c52018-01-02 11:34:14 -0500708#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500709##
710
Cary Clark61ca7c52018-01-02 11:34:14 -0500711#SeeAlso MakeFromPicture
Cary Clarka560c472017-11-27 10:44:06 -0500712
Cary Clark56356312018-02-08 14:45:18 -0500713#EnumClass ##
Cary Clarka560c472017-11-27 10:44:06 -0500714
715# ------------------------------------------------------------------------------
716
717#Method static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
718 const SkMatrix* matrix, const SkPaint* paint,
719 BitDepth bitDepth,
720 sk_sp<SkColorSpace> colorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500721#In Constructor
722#Line # creates Image from Picture ##
Cary Clarka560c472017-11-27 10:44:06 -0500723
Cary Clark61ca7c52018-01-02 11:34:14 -0500724Creates Image from picture. Returned Image width and height are set by dimensions.
725Image draws picture with matrix and paint, set to bitDepth and colorSpace.
Cary Clarka560c472017-11-27 10:44:06 -0500726
Cary Clark61ca7c52018-01-02 11:34:14 -0500727If matrix is nullptr, draws with identity Matrix. If paint is nullptr, draws
728with default Paint. colorSpace may be nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500729
Cary Clark61ca7c52018-01-02 11:34:14 -0500730#Param picture stream of drawing commands ##
731#Param dimensions width and height ##
732#Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ##
733#Param paint Paint to apply transparency, filtering, and so on; may be nullptr ##
734#Param bitDepth 8 bit integer or 16 bit float: per component ##
735#Param colorSpace range of colors; may be nullptr ##
736
737#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500738
739#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500740 SkPaint paint;
741 SkPictureRecorder recorder;
742 SkCanvas* recordingCanvas = recorder.beginRecording(50, 50);
743 for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) {
744 paint.setColor(color);
745 recordingCanvas->drawRect({10, 10, 30, 40}, paint);
746 recordingCanvas->translate(10, 10);
747 recordingCanvas->scale(1.2f, 1.4f);
748 }
749 sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
750 int x = 0, y = 0;
751 for (auto alpha : { 70, 140, 210 } ) {
752 paint.setAlpha(alpha);
753 auto srgbColorSpace = SkColorSpace::MakeSRGB();
754 sk_sp<SkImage> image = SkImage::MakeFromPicture(playback, {50, 50}, nullptr, &paint,
755 SkImage::BitDepth::kU8, srgbColorSpace);
756 canvas->drawImage(image, x, y);
757 x += 70; y += 70;
758 }
Cary Clarka560c472017-11-27 10:44:06 -0500759##
760
Cary Clark61ca7c52018-01-02 11:34:14 -0500761#SeeAlso SkCanvas::drawPicture
Cary Clarka560c472017-11-27 10:44:06 -0500762
763#Method ##
764
765# ------------------------------------------------------------------------------
766
767#Method static sk_sp<SkImage> MakeFromAHardwareBuffer(AHardwareBuffer* hardwareBuffer,
768 SkAlphaType alphaType = kPremul_SkAlphaType,
769 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500770#In Constructor
771#Line # creates Image from Android hardware buffer ##
Cary Clarka560c472017-11-27 10:44:06 -0500772
Cary Clark4855f782018-02-06 09:41:53 -0500773#Bug 7447
Cary Clarka560c472017-11-27 10:44:06 -0500774
Cary Clark61ca7c52018-01-02 11:34:14 -0500775Creates Image from Android hardware buffer.
776Returned Image takes a reference on the buffer.
Cary Clarka560c472017-11-27 10:44:06 -0500777
Cary Clark61ca7c52018-01-02 11:34:14 -0500778Only available on Android, when __ANDROID_API__ is defined to be 26 or greater.
Cary Clarka560c472017-11-27 10:44:06 -0500779
Cary Clark61ca7c52018-01-02 11:34:14 -0500780#Param hardwareBuffer AHardwareBuffer Android hardware buffer ##
Cary Clark681287e2018-03-16 11:34:15 -0400781#Param alphaType one of: #list_of_alpha_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500782##
783#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500784
Cary Clark61ca7c52018-01-02 11:34:14 -0500785#Return created Image, or nullptr ##
786
787#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500788##
789
Cary Clark61ca7c52018-01-02 11:34:14 -0500790#SeeAlso MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -0500791
792#Method ##
793
794# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -0500795#Subtopic Property
796#Populate
797#Line # values and attributes ##
798##
Cary Clarka560c472017-11-27 10:44:06 -0500799
800#Method int width() const
Cary Clark4855f782018-02-06 09:41:53 -0500801#In Property
802#Line # returns pixel column count ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500803Returns pixel count in each row.
804
805#Return pixel width in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500806
807#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500808#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500809#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500810 canvas->translate(10, 10);
811 canvas->drawImage(image, 0, 0);
812 canvas->translate(0, image->height());
813 SkPaint paint;
814 paint.setTextAlign(SkPaint::kCenter_Align);
815 canvas->drawLine(0, 10, image->width(), 10, paint);
816 canvas->drawString("width", image->width() / 2, 25, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500817##
818
Cary Clark61ca7c52018-01-02 11:34:14 -0500819#SeeAlso dimensions() height()
Cary Clarka560c472017-11-27 10:44:06 -0500820
821#Method ##
822
823# ------------------------------------------------------------------------------
824
825#Method int height() const
Cary Clark4855f782018-02-06 09:41:53 -0500826#In Property
827#Line # returns pixel row count ##
Cary Clark2f466242017-12-11 16:03:17 -0500828Returns pixel row count.
829
Cary Clark61ca7c52018-01-02 11:34:14 -0500830#Return pixel height in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500831
832#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500833#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500834#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500835 canvas->translate(10, 10);
836 canvas->drawImage(image, 0, 0);
837 canvas->translate(image->width(), 0);
838 SkPaint paint;
839 paint.setTextAlign(SkPaint::kCenter_Align);
840 paint.setVerticalText(true);
841 canvas->drawLine(10, 0, 10, image->height(), paint);
Cary Clarkac47b882018-01-11 10:35:44 -0500842 canvas->drawString("height", 25, image->height() / 2, paint);
843##
Cary Clarka560c472017-11-27 10:44:06 -0500844
Cary Clark61ca7c52018-01-02 11:34:14 -0500845#SeeAlso dimensions() width()
Cary Clarka560c472017-11-27 10:44:06 -0500846
847#Method ##
848
849# ------------------------------------------------------------------------------
850
851#Method SkISize dimensions() const
Cary Clark4855f782018-02-06 09:41:53 -0500852#In Property
853#Line # returns width() and height() ##
Cary Clark681287e2018-03-16 11:34:15 -0400854
Cary Clark2f466242017-12-11 16:03:17 -0500855Returns ISize { width(), height() }.
856
857#Return integral size of width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -0500858
859#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500860#Image 4
861 SkISize dimensions = image->dimensions();
862 SkIRect bounds = image->bounds();
863 SkIRect dimensionsAsBounds = SkIRect::MakeSize(dimensions);
864 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clark681287e2018-03-16 11:34:15 -0400865#StdOut
866dimensionsAsBounds == bounds
867##
Cary Clarka560c472017-11-27 10:44:06 -0500868##
869
Cary Clark61ca7c52018-01-02 11:34:14 -0500870#SeeAlso height() width() bounds()
Cary Clarka560c472017-11-27 10:44:06 -0500871
872#Method ##
873
874# ------------------------------------------------------------------------------
875
876#Method SkIRect bounds() const
Cary Clark4855f782018-02-06 09:41:53 -0500877#In Property
878#Line # returns width() and height() as Rectangle ##
Cary Clark2f466242017-12-11 16:03:17 -0500879Returns IRect { 0, 0, width(), height() }.
880
881#Return integral rectangle from origin to width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -0500882
883#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500884#Height 128
885#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -0500886 SkIRect bounds = image->bounds();
Cary Clarkac47b882018-01-11 10:35:44 -0500887 for (int x : { 0, bounds.width() } ) {
888 for (int y : { 0, bounds.height() } ) {
Cary Clark61ca7c52018-01-02 11:34:14 -0500889 canvas->drawImage(image, x, y);
890 }
891 }
Cary Clarka560c472017-11-27 10:44:06 -0500892##
893
Cary Clark61ca7c52018-01-02 11:34:14 -0500894#SeeAlso dimensions()
Cary Clarka560c472017-11-27 10:44:06 -0500895
896#Method ##
897
898# ------------------------------------------------------------------------------
899
900#Method uint32_t uniqueID() const
Cary Clark4855f782018-02-06 09:41:53 -0500901#In Property
902#Line # identifier for Image ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500903Returns value unique to image. Image contents cannot change after Image is
904created. Any operation to create a new Image will receive generate a new
905unique number.
906
907#Return unique identifier ##
Cary Clarka560c472017-11-27 10:44:06 -0500908
909#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500910#Image 5
911#Height 156
912 sk_sp<SkImage> subset = image->makeSubset({10, 20, 90, 100});
913 canvas->drawImage(image, 0, 0);
914 canvas->drawImage(subset, 128, 0);
915 SkPaint paint;
916 SkString s;
917 s.printf("original id: %d", image->uniqueID());
918 canvas->drawString(s, 20, image->height() + 20, paint);
919 s.printf("subset id: %d", subset->uniqueID());
920 canvas->drawString(s, 148, subset->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500921##
922
Cary Clark61ca7c52018-01-02 11:34:14 -0500923#SeeAlso isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -0500924
925#Method ##
926
927# ------------------------------------------------------------------------------
928
929#Method SkAlphaType alphaType() const
Cary Clark4855f782018-02-06 09:41:53 -0500930#In Property
931#Line # returns Alpha_Type ##
Cary Clark681287e2018-03-16 11:34:15 -0400932Returns Alpha_Type, one of: #list_of_alpha_types#.
Cary Clark61ca7c52018-01-02 11:34:14 -0500933
934Alpha_Type returned was a parameter to an Image constructor,
935or was parsed from encoded data.
936
937#Return Alpha_Type in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500938
939#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500940#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500941#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500942 const char* alphaTypeStr[] = { "Unknown", "Opaque", "Premul", "Unpremul" };
943 SkAlphaType alphaType = image->alphaType();
Cary Clarkac47b882018-01-11 10:35:44 -0500944 canvas->drawImage(image, 16, 0);
Cary Clark61ca7c52018-01-02 11:34:14 -0500945 SkPaint paint;
946 canvas->drawString(alphaTypeStr[(int) alphaType], 20, image->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500947##
948
Cary Clark61ca7c52018-01-02 11:34:14 -0500949#SeeAlso SkImageInfo::alphaType
Cary Clarka560c472017-11-27 10:44:06 -0500950
951#Method ##
952
953# ------------------------------------------------------------------------------
954
Greg Daniel56008aa2018-03-14 15:33:42 -0400955#Method SkColorType colorType() const
956#In Property
957#Line # returns Color_Type ##
958
959Returns Color_Type if known; otherwise, returns kUnknown_SkColorType.
960
961#Return Color_Type of Image ##
962
963#Example
964// incomplete
965##
966
967#SeeAlso SkImageInfo::colorType
968
969#Method ##
970
971# ------------------------------------------------------------------------------
972
Cary Clarka560c472017-11-27 10:44:06 -0500973#Method SkColorSpace* colorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -0500974#In Property
975#Line # returns Color_Space ##
Cary Clark2f466242017-12-11 16:03:17 -0500976Returns Color_Space, the range of colors, associated with Image. The
977reference count of Color_Space is unchanged. The returned Color_Space is
978immutable.
Cary Clarka560c472017-11-27 10:44:06 -0500979
Cary Clark61dfc3a2018-01-03 08:37:53 -0500980Color_Space returned was passed to an Image constructor,
981or was parsed from encoded data. Color_Space returned may be ignored when Image
982is drawn, depending on the capabilities of the Surface receiving the drawing.
Cary Clark2f466242017-12-11 16:03:17 -0500983
984#Return Color_Space in Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500985
986#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -0500987#Image 3
988#Set sRGB
989 SkPixmap pixmap;
990 source.peekPixels(&pixmap);
991 canvas->scale(.25f, .25f);
992 int y = 0;
993 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
994 SkColorSpace::kSRGB_RenderTargetGamma } ) {
995 int x = 0;
996 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
997 for (int index = 0; index < 2; ++index) {
998 pixmap.setColorSpace(colorSpace);
999 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1000 canvas->drawImage(image, x, y);
1001 colorSpace = image->colorSpace()->makeColorSpin();
1002 x += 512;
1003 }
1004 y += 512;
1005 }
Cary Clarka560c472017-11-27 10:44:06 -05001006##
1007
Cary Clark61dfc3a2018-01-03 08:37:53 -05001008#SeeAlso refColorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001009
1010#Method ##
1011
1012# ------------------------------------------------------------------------------
1013
1014#Method sk_sp<SkColorSpace> refColorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001015#In Property
1016#Line # returns Image_Info Color_Space ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001017Returns a smart pointer to Color_Space, the range of colors, associated with
1018Image. The smart pointer tracks the number of objects sharing this
1019SkColorSpace reference so the memory is released when the owners destruct.
1020
1021The returned SkColorSpace is immutable.
1022
1023Color_Space returned was passed to an Image constructor,
1024or was parsed from encoded data. Color_Space returned may be ignored when Image
1025is drawn, depending on the capabilities of the Surface receiving the drawing.
1026
1027#Return Color_Space in Image, or nullptr, wrapped in a smart pointer ##
Cary Clarka560c472017-11-27 10:44:06 -05001028
1029#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001030#Image 3
1031#Set sRGB
1032 SkPixmap pixmap;
1033 source.peekPixels(&pixmap);
1034 canvas->scale(.25f, .25f);
1035 int y = 0;
1036 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1037 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1038 int x = 0;
1039 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1040 for (int index = 0; index < 2; ++index) {
1041 pixmap.setColorSpace(colorSpace);
1042 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1043 canvas->drawImage(image, x, y);
1044 colorSpace = image->refColorSpace()->makeColorSpin();
1045 x += 512;
1046 }
1047 y += 512;
1048 }
Cary Clarka560c472017-11-27 10:44:06 -05001049##
1050
Cary Clark61dfc3a2018-01-03 08:37:53 -05001051#SeeAlso colorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001052
1053#Method ##
1054
1055# ------------------------------------------------------------------------------
1056
1057#Method bool isAlphaOnly() const
Cary Clark4855f782018-02-06 09:41:53 -05001058#In Property
1059#Line # returns if pixels represent a transparency mask ##
Cary Clark2f466242017-12-11 16:03:17 -05001060Returns true if Image pixels represent transparency only. If true, each pixel
1061is packed in 8 bits as defined by kAlpha_8_SkColorType.
Cary Clarka560c472017-11-27 10:44:06 -05001062
Cary Clark2f466242017-12-11 16:03:17 -05001063#Return true if pixels represent a transparency mask ##
Cary Clarka560c472017-11-27 10:44:06 -05001064
1065#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001066 uint8_t pmColors = 0;
1067 sk_sp<SkImage> image = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1), &pmColors, 1});
1068 SkDebugf("alphaOnly = %s\n", image->isAlphaOnly() ? "true" : "false");
1069#StdOut
1070alphaOnly = true
1071##
Cary Clarka560c472017-11-27 10:44:06 -05001072##
1073
Cary Clark61dfc3a2018-01-03 08:37:53 -05001074#SeeAlso alphaType isOpaque
Cary Clarka560c472017-11-27 10:44:06 -05001075
1076#Method ##
1077
1078# ------------------------------------------------------------------------------
1079
1080#Method bool isOpaque() const
Cary Clark4855f782018-02-06 09:41:53 -05001081#In Property
1082#Line # returns if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001083Returns true if pixels ignore their Alpha value and are treated as fully opaque.
Cary Clark2f466242017-12-11 16:03:17 -05001084
1085#Return true if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clarka560c472017-11-27 10:44:06 -05001086
1087#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001088 auto check_isopaque = [](const SkImageInfo& imageInfo) -> void {
1089 auto surface(SkSurface::MakeRaster(imageInfo));
1090 auto image(surface->makeImageSnapshot());
1091 SkDebugf("isOpaque = %s\n", image->isOpaque() ? "true" : "false");
1092 };
1093
1094 check_isopaque(SkImageInfo::MakeN32Premul(5, 5));
1095 check_isopaque(SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType));
1096#StdOut
1097isOpaque = false
1098isOpaque = true
1099##
Cary Clarka560c472017-11-27 10:44:06 -05001100##
1101
Cary Clark61dfc3a2018-01-03 08:37:53 -05001102#SeeAlso alphaType isAlphaOnly
Cary Clarka560c472017-11-27 10:44:06 -05001103
1104#Method ##
1105
1106# ------------------------------------------------------------------------------
1107
1108#Method sk_sp<SkShader> makeShader(SkShader::TileMode tileMode1, SkShader::TileMode tileMode2,
1109 const SkMatrix* localMatrix = nullptr) const
Cary Clark4855f782018-02-06 09:41:53 -05001110#In Constructor
1111#Line # creates Shader, Paint element that can tile Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001112
Cary Clark61dfc3a2018-01-03 08:37:53 -05001113Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1114SkShader::TileMode rules to fill drawn area outside Image. localMatrix permits
1115transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001116
Cary Clark61dfc3a2018-01-03 08:37:53 -05001117#Param tileMode1 tiling in x, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1118 SkShader::kMirror_TileMode
1119##
1120#Param tileMode2 tiling in y, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1121 SkShader::kMirror_TileMode
1122##
1123#Param localMatrix Image transformation, or nullptr ##
1124
1125#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001126
1127#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001128#Image 4
1129SkMatrix matrix;
1130matrix.setRotate(45);
1131SkPaint paint;
1132paint.setShader(image->makeShader(SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode,
1133 &matrix));
1134canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001135##
1136
Cary Clark61dfc3a2018-01-03 08:37:53 -05001137#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001138
1139#Method ##
1140
1141# ------------------------------------------------------------------------------
1142
1143#Method sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const
1144
Cary Clark61dfc3a2018-01-03 08:37:53 -05001145Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1146SkShader::kClamp_TileMode to fill drawn area outside Image. localMatrix permits
1147transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001148
Cary Clark61dfc3a2018-01-03 08:37:53 -05001149#Param localMatrix Image transformation, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001150
Cary Clark61dfc3a2018-01-03 08:37:53 -05001151#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001152
1153#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001154#Image 5
1155SkMatrix matrix;
1156matrix.setRotate(45);
1157matrix.postTranslate(125, 30);
1158SkPaint paint;
1159paint.setShader(image->makeShader(&matrix));
1160canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001161##
1162
Cary Clarkf5404bb2018-01-05 12:10:09 -05001163#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001164
1165#Method ##
1166
1167# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001168#Subtopic Pixels
1169#Populate
1170#Line # read and write pixel values ##
1171##
Cary Clarka560c472017-11-27 10:44:06 -05001172
1173#Method bool peekPixels(SkPixmap* pixmap) const
Cary Clark78de7512018-02-07 07:27:09 -05001174#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001175#Line # returns Pixmap if possible ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001176Copies Image pixel address, row bytes, and Image_Info to pixmap, if address
1177is available, and returns true. If pixel address is not available, return
1178false and leave pixmap unchanged.
Cary Clarka560c472017-11-27 10:44:06 -05001179
Cary Clarkf5404bb2018-01-05 12:10:09 -05001180#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
Cary Clarka560c472017-11-27 10:44:06 -05001181
Cary Clarkf5404bb2018-01-05 12:10:09 -05001182#Return true if Image has direct access to pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001183
1184#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001185 SkBitmap bitmap;
1186 bitmap.allocPixels(SkImageInfo::MakeN32Premul(12, 11));
1187 SkCanvas offscreen(bitmap);
1188 offscreen.clear(SK_ColorWHITE);
1189 SkPaint paint;
1190 offscreen.drawString("%", 1, 10, paint);
1191 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
1192 SkPixmap pixmap;
1193 if (image->peekPixels(&pixmap)) {
1194 const SkPMColor* pixels = pixmap.addr32();
1195 SkPMColor pmWhite = pixels[0];
1196 for (int y = 0; y < image->height(); ++y) {
1197 for (int x = 0; x < image->width(); ++x) {
1198 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
1199 }
1200 SkDebugf("\n");
1201 }
1202 }
1203#StdOut
1204------------
1205--xx----x---
1206-x--x--x----
1207-x--x--x----
1208-x--x-x-----
1209--xx-xx-xx--
1210-----x-x--x-
1211----x--x--x-
1212----x--x--x-
1213---x----xx--
1214------------
1215##
Cary Clarka560c472017-11-27 10:44:06 -05001216##
1217
Cary Clarkf5404bb2018-01-05 12:10:09 -05001218#SeeAlso readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001219
1220#Method ##
1221
1222# ------------------------------------------------------------------------------
1223
1224#Method GrTexture* getTexture() const
Cary Clark2f466242017-12-11 16:03:17 -05001225#Deprecated
Cary Clarka560c472017-11-27 10:44:06 -05001226#Method ##
1227
1228# ------------------------------------------------------------------------------
1229
1230#Method bool isTextureBacked() const
Cary Clark78de7512018-02-07 07:27:09 -05001231#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001232#Line # returns if Image was created from GPU_Texture ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001233Returns true the contents of Image was created on or uploaded to GPU memory,
1234and is available as a GPU_Texture.
Cary Clarka560c472017-11-27 10:44:06 -05001235
Cary Clarkf5404bb2018-01-05 12:10:09 -05001236#Return true if Image is a GPU_Texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001237
1238#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001239#Image 5
1240#Platform gpu
1241auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1242 if (nullptr == image) {
1243 return;
1244 }
1245 SkPaint paint;
1246 paint.setAntiAlias(true);
1247 paint.setTextAlign(SkPaint::kCenter_Align);
1248 canvas->drawImage(image, 0, 0);
1249 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1250 canvas->drawString(image->isTextureBacked() ? "is GPU texture" : "not GPU texture",
1251 image->width() / 2, image->height() * 3 / 4, paint);
1252};
1253sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1254sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001255 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1256 nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05001257drawImage(image, "image");
1258canvas->translate(image->width(), 0);
1259drawImage(bitmapImage, "source");
1260canvas->translate(-image->width(), image->height());
1261drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001262##
1263
Cary Clarkf5404bb2018-01-05 12:10:09 -05001264#SeeAlso MakeFromTexture isValid
Cary Clarka560c472017-11-27 10:44:06 -05001265
1266#Method ##
1267
1268# ------------------------------------------------------------------------------
1269
1270#Method bool isValid(GrContext* context) const
Cary Clark4855f782018-02-06 09:41:53 -05001271#In Property
1272#Line # returns if Image can draw to Raster_Surface or GPU_Context ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001273Returns true if Image can be drawn on either Raster_Surface or GPU_Surface.
1274If context is nullptr, tests if Image draws on Raster_Surface;
1275otherwise, tests if Image draws on GPU_Surface associated with context.
Cary Clarka560c472017-11-27 10:44:06 -05001276
Cary Clarkf5404bb2018-01-05 12:10:09 -05001277Image backed by GPU_Texture may become invalid if associated GrContext is
1278invalid. Lazy_Image may be invalid and may not draw to Raster_Surface or
1279GPU_Surface or both.
Cary Clarka560c472017-11-27 10:44:06 -05001280
Cary Clark61ca7c52018-01-02 11:34:14 -05001281#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05001282
Cary Clarkf5404bb2018-01-05 12:10:09 -05001283#Return true if Image can be drawn ##
Cary Clarka560c472017-11-27 10:44:06 -05001284
1285#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001286#Image 5
1287#Platform gpu
1288auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1289 if (nullptr == image) {
1290 return;
1291 }
1292 SkPaint paint;
1293 paint.setAntiAlias(true);
1294 paint.setTextAlign(SkPaint::kCenter_Align);
1295 canvas->drawImage(image, 0, 0);
1296 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1297 if (canvas->getGrContext()) {
1298 canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" :
1299 "not valid on GPU", image->width() / 2, image->height() * 5 / 8, paint);
1300 }
1301 canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" :
1302 "not valid on CPU", image->width() / 2, image->height() * 7 / 8, paint);
1303};
1304sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1305sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001306 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1307 nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05001308drawImage(image, "image");
1309canvas->translate(image->width(), 0);
1310drawImage(bitmapImage, "source");
1311canvas->translate(-image->width(), image->height());
1312drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001313##
1314
Cary Clarkf5404bb2018-01-05 12:10:09 -05001315#SeeAlso isTextureBacked isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001316
1317#Method ##
1318
1319# ------------------------------------------------------------------------------
1320
1321#Method GrBackendObject getTextureHandle(bool flushPendingGrContextIO,
1322 GrSurfaceOrigin* origin = nullptr) const
Cary Clark78de7512018-02-07 07:27:09 -05001323#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001324#Line # returns GPU reference to Image as texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001325
Cary Clark2f466242017-12-11 16:03:17 -05001326Retrieves the back-end API handle of texture. If flushPendingGrContextIO is true,
1327complete deferred I/O operations.
Cary Clarka560c472017-11-27 10:44:06 -05001328
Robert Phillipsc5509952018-04-04 15:54:55 -04001329If origin is not nullptr, copies location of content drawn into Image.
Cary Clarka560c472017-11-27 10:44:06 -05001330
Cary Clark2f466242017-12-11 16:03:17 -05001331#Param flushPendingGrContextIO flag to flush outstanding requests ##
1332#Param origin storage for one of: kTopLeft_GrSurfaceOrigin,
1333 kBottomLeft_GrSurfaceOrigin; or nullptr
1334##
1335
Cary Clarkac47b882018-01-11 10:35:44 -05001336#Return back-end API texture handle, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001337
1338#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001339#Image 4
Cary Clark2f466242017-12-11 16:03:17 -05001340#Platform gpu
1341GrContext* context = canvas->getGrContext();
1342if (!context) {
1343 return;
1344}
1345SkPaint paint;
1346paint.setAntiAlias(true);
1347SkString str;
Cary Clarkac47b882018-01-11 10:35:44 -05001348int y = -10;
Cary Clark2f466242017-12-11 16:03:17 -05001349for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin } ) {
1350 sk_sp<SkImage> srcImage(SkImage::MakeFromTexture(context,
Cary Clark471b6fe2018-03-21 08:52:41 -04001351 backEndTexture, origin, kN32_SkColorType, kPremul_SkAlphaType, nullptr));
Cary Clark2f466242017-12-11 16:03:17 -05001352 GrSurfaceOrigin readBackOrigin;
1353 GrBackendObject readBackHandle = srcImage->getTextureHandle(false, &readBackOrigin);
Cary Clark681287e2018-03-16 11:34:15 -04001354 str.printf("readBackHandle: 0x%lx", readBackHandle);
Cary Clarkac47b882018-01-11 10:35:44 -05001355 canvas->drawString(str, 5, y += 30, paint);
1356 canvas->drawImage(srcImage, 80, y += 10);
Cary Clark2f466242017-12-11 16:03:17 -05001357 str.printf("origin: k%s_GrSurfaceOrigin", readBackOrigin ? "BottomLeft" : "TopLeft");
Cary Clarkac47b882018-01-11 10:35:44 -05001358 canvas->drawString(str, 5, y += srcImage->height() + 10, paint);
Cary Clark2f466242017-12-11 16:03:17 -05001359}
Cary Clarka560c472017-11-27 10:44:06 -05001360##
1361
Cary Clarkac47b882018-01-11 10:35:44 -05001362#Example
1363#Image 5
1364#Platform gpu
1365 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1366 if (nullptr == image) {
1367 return;
1368 }
1369 SkPaint paint;
1370 paint.setAntiAlias(true);
1371 paint.setTextAlign(SkPaint::kCenter_Align);
1372 canvas->drawImage(image, 0, image->height() / 4);
1373 canvas->drawString(label, image->width() / 2, image->height() / 8, paint);
1374 GrSurfaceOrigin readBackOrigin;
1375 GrBackendObject readBackHandle = image->getTextureHandle(false, &readBackOrigin);
1376 canvas->drawString(readBackHandle ? "has readBackHandle" : "no readBackHandle",
1377 image->width() / 2, image->height() * 11 / 8, paint);
1378 };
1379 drawImage(image, "image");
1380 canvas->translate(image->width(), 0);
1381 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001382 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1383 nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001384 drawImage(textureImage, "backEndTexture");
1385##
1386
1387#SeeAlso MakeFromTexture isTextureBacked
Cary Clarka560c472017-11-27 10:44:06 -05001388
1389#Method ##
1390
1391# ------------------------------------------------------------------------------
1392
Robert Phillipsc5509952018-04-04 15:54:55 -04001393#Method GrBackendTexture getBackendTexture(bool flushPendingGrContextIO,
1394 GrSurfaceOrigin* origin = nullptr) const
1395#In Property
1396#Line # returns GPU reference to Image as texture ##
1397
1398Retrieves the backend texture. If there is none an invalid object will be returned.
1399If flushPendingGrContextIO is true, complete deferred I/O operations.
1400
1401If origin in not nullptr, copies location of content drawn into Image.
1402
1403#Param flushPendingGrContextIO flag to flush outstanding requests ##
1404#Param origin storage for one of: kTopLeft_GrSurfaceOrigin,
1405 kBottomLeft_GrSurfaceOrigin; or nullptr
1406##
1407
1408#Return back-end API texture handle. Invalid on failure. ##
1409
1410#NoExample
1411##
1412
1413#SeeAlso MakeFromTexture isTextureBacked
1414
1415#Method ##
1416
1417# ------------------------------------------------------------------------------
1418
Cary Clarka560c472017-11-27 10:44:06 -05001419#Enum CachingHint
1420
1421#Code
1422 enum CachingHint {
1423 kAllow_CachingHint,
1424 kDisallow_CachingHint,
1425 };
1426##
1427
Cary Clarkac47b882018-01-11 10:35:44 -05001428CachingHint selects whether Skia may internally cache Bitmaps generated by
1429decoding Image, or by copying Image from GPU to CPU. The default behavior
1430allows caching Bitmaps.
1431
1432Choose kDisallow_CachingHint if Image pixels are to be used only once, or
1433if Image pixels reside in a cache outside of Skia, or to reduce memory pressure.
1434
1435Choosing kAllow_CachingHint does not ensure that pixels will be cached.
1436Image pixels may not be cached if memory requirements are too large or
1437pixels are not accessible.
Cary Clarka560c472017-11-27 10:44:06 -05001438
1439#Const kAllow_CachingHint 0
Cary Clarkac47b882018-01-11 10:35:44 -05001440Allows Skia to internally cache decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001441##
1442#Const kDisallow_CachingHint 1
Cary Clarkac47b882018-01-11 10:35:44 -05001443Disallows Skia from internally caching decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001444##
1445
Cary Clarkac47b882018-01-11 10:35:44 -05001446#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001447##
1448
Cary Clarkac47b882018-01-11 10:35:44 -05001449#SeeAlso readPixels scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001450
1451#Enum ##
1452
1453# ------------------------------------------------------------------------------
1454
1455#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1456 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001457#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001458#Line # copies and converts pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001459
Cary Clarkac47b882018-01-11 10:35:44 -05001460Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY),
1461and does not exceed Image (width(), height()).
1462
1463dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
1464destination. dstRowBytes specifics the gap from one destination row to the next.
1465Returns true if pixels are copied. Returns false if:
1466#List
1467# dstInfo.addr() equals nullptr ##
1468# dstRowBytes is less than dstInfo.minRowBytes ##
1469# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001470##
1471
Cary Clarkac47b882018-01-11 10:35:44 -05001472Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1473kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1474If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace must match.
1475If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType must
1476match. If Image Color_Space is nullptr, dstInfo.colorSpace must match. Returns
1477false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001478
Cary Clarkac47b882018-01-11 10:35:44 -05001479srcX and srcY may be negative to copy only top or left of source. Returns
1480false if width() or height() is zero or negative.
1481Returns false if
1482#Formula
1483abs(srcX) >= Image width()
1484##
1485, or if
1486#Formula
1487abs(srcY) >= Image height()
1488##
1489.
Cary Clarka560c472017-11-27 10:44:06 -05001490
Cary Clarkac47b882018-01-11 10:35:44 -05001491If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1492If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1493
1494#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1495#Param dstPixels destination pixel storage ##
1496#Param dstRowBytes destination row length ##
1497#Param srcX column index whose absolute value is less than width() ##
1498#Param srcY row index whose absolute value is less than height() ##
1499#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1500
1501#Return true if pixels are copied to dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001502
1503#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001504#Image 3
1505 canvas->scale(.5f, .5f);
1506 const int width = 32;
1507 const int height = 32;
1508 std::vector<int32_t> dstPixels;
1509 dstPixels.resize(height * width * 4);
1510 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1511 for (int y = 0; y < 512; y += height ) {
1512 for (int x = 0; x < 512; x += width ) {
1513 if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) {
1514 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
1515 SkBitmap bitmap;
1516 bitmap.installPixels(dstPixmap);
1517 canvas->drawBitmap(bitmap, 0, 0);
1518 }
1519 canvas->translate(48, 0);
1520 }
1521 canvas->translate(-16 * 48, 48);
1522 }
Cary Clarka560c472017-11-27 10:44:06 -05001523##
1524
Cary Clarkac47b882018-01-11 10:35:44 -05001525#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001526
1527#Method ##
1528
1529# ------------------------------------------------------------------------------
1530
1531#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY,
1532 CachingHint cachingHint = kAllow_CachingHint) const
1533
Cary Clarkac47b882018-01-11 10:35:44 -05001534Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and
1535does not exceed Image (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001536
Cary Clarkac47b882018-01-11 10:35:44 -05001537dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
1538and row bytes of destination. dst.rowBytes specifics the gap from one destination
1539row to the next. Returns true if pixels are copied. Returns false if:
1540#List
1541# dst pixel storage equals nullptr ##
1542# dst.rowBytes is less than SkImageInfo::minRowBytes ##
1543# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001544##
1545
Cary Clarkac47b882018-01-11 10:35:44 -05001546Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1547kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1548If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1549If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1550match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1551false if pixel conversion is not possible.
1552
1553srcX and srcY may be negative to copy only top or left of source. Returns
1554false if width() or height() is zero or negative.
1555Returns false if
1556#Formula
1557abs(srcX) >= Image width()
1558##
1559, or if
1560#Formula
1561abs(srcY) >= Image height()
1562##
1563.
1564
1565If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1566If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1567
1568#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1569#Param srcX column index whose absolute value is less than width() ##
1570#Param srcY row index whose absolute value is less than height() ##
1571#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1572
1573#Return true if pixels are copied to dst ##
1574
1575#Example
1576#Image 3
1577 std::vector<int32_t> srcPixels;
1578 int rowBytes = image->width() * 4;
1579 int quarterWidth = image->width() / 4;
1580 int quarterHeight = image->height() / 4;
1581 srcPixels.resize(image->height() * rowBytes);
1582 for (int y = 0; y < 4; ++y) {
1583 for (int x = 0; x < 4; ++x) {
1584 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1585 &srcPixels.front() + x * image->height() * quarterWidth +
1586 y * quarterWidth, rowBytes);
1587 image->readPixels(pixmap, x * quarterWidth, y * quarterHeight);
1588 }
1589 }
1590 canvas->scale(.5f, .5f);
1591 SkBitmap bitmap;
1592 bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()),
1593 &srcPixels.front(), rowBytes);
1594 canvas->drawBitmap(bitmap, 0, 0);
1595##
1596
1597#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001598
1599#Method ##
1600
1601# ------------------------------------------------------------------------------
1602
1603#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality,
1604 CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001605#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001606#Line # scales and converts one Image to another ##
Cary Clarka560c472017-11-27 10:44:06 -05001607
Cary Clarkac47b882018-01-11 10:35:44 -05001608Copies Image to dst, scaling pixels to fit dst.width() and dst.height(), and
1609converting pixels to match dst.colorType and dst.alphaType. Returns true if
1610pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1611less than dst SkImageInfo::minRowBytes.
Cary Clarka560c472017-11-27 10:44:06 -05001612
Cary Clarkac47b882018-01-11 10:35:44 -05001613Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1614kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1615If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1616If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1617match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1618false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001619
Cary Clarkac47b882018-01-11 10:35:44 -05001620Scales the image, with filterQuality, to match dst.width() and dst.height().
1621filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1622Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1623Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1624Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1625kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1626
1627If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1628If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1629
1630#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1631#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1632 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1633##
1634#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1635
1636#Return true if pixels are scaled to fit dst ##
Cary Clarka560c472017-11-27 10:44:06 -05001637
1638#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001639#Image 3
1640#Height 128
1641 std::vector<int32_t> srcPixels;
1642 int quarterWidth = image->width() / 16;
1643 int rowBytes = quarterWidth * 4;
1644 int quarterHeight = image->height() / 16;
1645 srcPixels.resize(quarterHeight * rowBytes);
1646 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1647 &srcPixels.front(), rowBytes);
1648 canvas->scale(4, 4);
1649 SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality,
1650 kMedium_SkFilterQuality, kHigh_SkFilterQuality };
1651 for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) {
1652 image->scalePixels(pixmap, qualities[index]);
1653 sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
1654 canvas->drawImage(filtered, 16 * index, 0);
1655 }
Cary Clarka560c472017-11-27 10:44:06 -05001656##
1657
Cary Clarkac47b882018-01-11 10:35:44 -05001658#SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001659
1660#Method ##
1661
1662# ------------------------------------------------------------------------------
1663
1664#Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const
Cary Clark78de7512018-02-07 07:27:09 -05001665#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001666#Line # returns encoded Image as SkData ##
Cary Clarkac47b882018-01-11 10:35:44 -05001667Encodes Image pixels, returning result as SkData.
Cary Clark2f466242017-12-11 16:03:17 -05001668
Cary Clarkac47b882018-01-11 10:35:44 -05001669Returns nullptr if encoding fails, or if encodedImageFormat is not supported.
Cary Clarka560c472017-11-27 10:44:06 -05001670
Cary Clarkac47b882018-01-11 10:35:44 -05001671Image encoding in a format requires both building with one or more of:
1672SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support
1673for the encoded format.
1674
1675If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can
1676additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP,
1677SkEncodedImageFormat::kGIF.
1678
1679quality is a platform and format specific metric trading off size and encoding
1680error. When used, quality equaling 100 encodes with the least error. quality may
1681be ignored by the encoder.
1682
1683#Param encodedImageFormat one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
1684 SkEncodedImageFormat::kWEBP
1685 ##
1686#Param quality encoder specific metric with 100 equaling best ##
Cary Clarka560c472017-11-27 10:44:06 -05001687
Cary Clark2f466242017-12-11 16:03:17 -05001688#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001689
1690#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001691#Image 3
1692 canvas->scale(4, 4);
1693 SkIRect subset = {0, 0, 16, 64};
1694 int x = 0;
1695 for (int quality : { 0, 10, 50, 100 } ) {
1696 sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
1697 sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
1698 canvas->drawImage(filtered, x, 0);
1699 x += 16;
1700 }
Cary Clarka560c472017-11-27 10:44:06 -05001701##
1702
Cary Clarkac47b882018-01-11 10:35:44 -05001703#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001704
1705#Method ##
1706
1707# ------------------------------------------------------------------------------
1708
Cary Clark61ca7c52018-01-02 11:34:14 -05001709#Method sk_sp<SkData> encodeToData() const
Cary Clarka560c472017-11-27 10:44:06 -05001710
Cary Clarkac47b882018-01-11 10:35:44 -05001711Encodes Image pixels, returning result as SkData. Returns existing encoded data
1712if present; otherwise, Image is encoded with SkEncodedImageFormat::kPNG. Skia
1713must be built with SK_HAS_PNG_LIBRARY to encode Image.
Cary Clarka560c472017-11-27 10:44:06 -05001714
Cary Clarkac47b882018-01-11 10:35:44 -05001715Returns nullptr if existing encoded data is missing or invalid, and
Cary Clarka560c472017-11-27 10:44:06 -05001716encoding fails.
1717
Cary Clarkac47b882018-01-11 10:35:44 -05001718#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001719
1720#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001721#Image 3
1722 canvas->scale(4, 4);
1723 SkIRect subset = {136, 32, 200, 96};
1724 sk_sp<SkData> data(image->encodeToData());
1725 sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
1726 canvas->drawImage(eye, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001727##
1728
Cary Clarkac47b882018-01-11 10:35:44 -05001729#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001730
1731#Method ##
1732
1733# ------------------------------------------------------------------------------
1734
1735#Method sk_sp<SkData> refEncodedData() const
Cary Clark78de7512018-02-07 07:27:09 -05001736#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001737#Line # returns Image encoded in SkData if present ##
Cary Clarkac47b882018-01-11 10:35:44 -05001738Returns encoded Image pixels as SkData, if Image was created from supported
1739encoded stream format. Platform support for formats vary and may require building
1740with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY.
Cary Clarka560c472017-11-27 10:44:06 -05001741
Cary Clarkac47b882018-01-11 10:35:44 -05001742Returns nullptr if Image contents are not encoded.
Cary Clarka560c472017-11-27 10:44:06 -05001743
Cary Clarkac47b882018-01-11 10:35:44 -05001744#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001745
1746#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001747#Image 3
1748#Platform gpu
1749 struct {
1750 const char* name;
1751 sk_sp<SkImage> image;
1752 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1753 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001754 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1755 nullptr) } };
Cary Clarkac47b882018-01-11 10:35:44 -05001756 SkString string;
1757 SkPaint paint;
1758 for (const auto& test : tests ) {
1759 if (!test.image) {
1760 string.printf("no %s", test.name);
1761 } else {
1762 string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name);
1763 }
1764 canvas->drawString(string, 10, 20, paint);
1765 canvas->translate(0, 20);
1766 }
Cary Clarka560c472017-11-27 10:44:06 -05001767##
1768
Cary Clarkac47b882018-01-11 10:35:44 -05001769#SeeAlso encodeToData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001770
1771#Method ##
1772
1773# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001774#Subtopic Utility
1775#Populate
1776#Line # rarely called management functions ##
1777##
Cary Clarka560c472017-11-27 10:44:06 -05001778
1779#Method const char* toString(SkString* string) const
Cary Clark4855f782018-02-06 09:41:53 -05001780#In Utility
1781#Line # converts Image to machine readable form ##
Cary Clarkac47b882018-01-11 10:35:44 -05001782Appends Image description to string, including unique ID, width, height, and
1783whether the image is opaque.
Cary Clarka560c472017-11-27 10:44:06 -05001784
Cary Clarkac47b882018-01-11 10:35:44 -05001785#Param string storage for description; existing content is preserved ##
1786
1787#Return string appended with Image description ##
Cary Clarka560c472017-11-27 10:44:06 -05001788
1789#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001790#Image 4
1791 struct {
1792 const char* name;
1793 sk_sp<SkImage> image;
1794 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1795 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001796 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1797 nullptr) } };
Cary Clarkac47b882018-01-11 10:35:44 -05001798 SkString string;
1799 SkPaint paint;
1800 for (const auto& test : tests ) {
1801 string.printf("%s: ", test.name);
1802 test.image ? (void) test.image->toString(&string) : string.append("no image");
1803 canvas->drawString(string, 10, 20, paint);
1804 canvas->translate(0, 20);
1805 }
Cary Clarka560c472017-11-27 10:44:06 -05001806##
1807
Cary Clarkac47b882018-01-11 10:35:44 -05001808#SeeAlso SkPaint::toString
Cary Clarka560c472017-11-27 10:44:06 -05001809
1810#Method ##
1811
1812# ------------------------------------------------------------------------------
1813
1814#Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const
Cary Clark4855f782018-02-06 09:41:53 -05001815#In Constructor
1816#Line # creates Image containing part of original ##
Cary Clarkac47b882018-01-11 10:35:44 -05001817Returns subset of Image. subset must be fully contained by Image dimensions().
1818The implementation may share pixels, or may copy them.
Cary Clarka560c472017-11-27 10:44:06 -05001819
Cary Clarkac47b882018-01-11 10:35:44 -05001820Returns nullptr if subset is empty, or subset is not contained by bounds, or
1821pixels in Image could not be read or copied.
Cary Clarka560c472017-11-27 10:44:06 -05001822
Cary Clarkac47b882018-01-11 10:35:44 -05001823#Param subset bounds of returned Image ##
1824
1825#Return partial or full Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001826
1827#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001828#Image 3
1829 canvas->scale(.5f, .5f);
1830 const int width = 32;
1831 const int height = 32;
1832 for (int y = 0; y < 512; y += height ) {
1833 for (int x = 0; x < 512; x += width ) {
1834 sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height}));
1835 canvas->drawImage(subset, x * 3 / 2, y * 3 / 2);
1836 }
1837 }
Cary Clarka560c472017-11-27 10:44:06 -05001838##
1839
Cary Clarkac47b882018-01-11 10:35:44 -05001840#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001841
1842#Method ##
1843
1844# ------------------------------------------------------------------------------
1845
1846#Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const
Cary Clark4855f782018-02-06 09:41:53 -05001847#In Constructor
1848#Line # creates Image matching Color_Space if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001849Returns Image backed by GPU_Texture associated with context. Returned Image is
1850compatible with Surface created with dstColorSpace. Returns original
1851Image if context and dstColorSpace match.
1852
1853Returns nullptr if context is nullptr, or if Image was created with another
1854GrContext.
Cary Clarka560c472017-11-27 10:44:06 -05001855
Cary Clark61ca7c52018-01-02 11:34:14 -05001856#Param context GPU_Context ##
Cary Clarkac47b882018-01-11 10:35:44 -05001857#Param dstColorSpace range of colors of matching Surface on GPU ##
Cary Clarka560c472017-11-27 10:44:06 -05001858
Cary Clarkac47b882018-01-11 10:35:44 -05001859#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001860
1861#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001862#Platform gpu
1863#Image 5
1864 auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void {
1865 if (nullptr == image || nullptr == context) {
1866 return;
1867 }
1868 SkPaint paint;
1869 paint.setAntiAlias(true);
1870 paint.setTextAlign(SkPaint::kCenter_Align);
1871 sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr));
1872 canvas->drawImage(texture, 0, 0);
1873 canvas->drawString(label, texture->width() / 2, texture->height() / 4, paint);
1874 };
1875 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1876 GrContext* context = canvas->getGrContext();
1877 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001878 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1879 nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001880 drawImage(image, context, "image");
1881 canvas->translate(image->width(), 0);
1882 drawImage(bitmapImage, context, "source");
1883 canvas->translate(-image->width(), image->height());
1884 drawImage(textureImage, context, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001885##
1886
Cary Clarkac47b882018-01-11 10:35:44 -05001887#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05001888
1889#Method ##
1890
1891# ------------------------------------------------------------------------------
1892
1893#Method sk_sp<SkImage> makeNonTextureImage() const
Cary Clark4855f782018-02-06 09:41:53 -05001894#In Constructor
1895#Line # creates Image without dependency on GPU_Texture ##
Cary Clarkac47b882018-01-11 10:35:44 -05001896Returns Raster_Image or Lazy_Image. Copies Image backed by GPU_Texture into
Cary Clark4855f782018-02-06 09:41:53 -05001897CPU memory if needed. Returns original Image if decoded in Raster_Bitmap,
Cary Clarkac47b882018-01-11 10:35:44 -05001898or if encoded in a stream.
Cary Clark61ca7c52018-01-02 11:34:14 -05001899
Cary Clarkac47b882018-01-11 10:35:44 -05001900Returns nullptr if backed by GPU_Texture and copy fails.
1901
1902#Return Raster_Image, Lazy_Image, or nullptr ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001903
1904#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001905#Image 5
1906#Platform gpu
1907 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1908 if (nullptr == image) {
1909 return;
1910 }
1911 SkPaint paint;
1912 paint.setAntiAlias(true);
1913 paint.setTextAlign(SkPaint::kCenter_Align);
1914 sk_sp<SkImage> nonTexture(image->makeNonTextureImage());
1915 canvas->drawImage(nonTexture, 0, 0);
1916 canvas->drawString(label, nonTexture->width() / 2, nonTexture->height() / 4, paint);
1917 };
1918 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1919 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001920 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1921 nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001922 drawImage(image, "image");
1923 canvas->translate(image->width(), 0);
1924 drawImage(bitmapImage, "source");
1925 canvas->translate(-image->width(), image->height());
1926 drawImage(textureImage, "backEndTexture");
Cary Clark61ca7c52018-01-02 11:34:14 -05001927##
1928
Cary Clark56356312018-02-08 14:45:18 -05001929#SeeAlso makeTextureImage makeRasterImage MakeBackendTextureFromSkImage
Cary Clark61ca7c52018-01-02 11:34:14 -05001930
1931#Method ##
1932
1933# ------------------------------------------------------------------------------
1934
1935#Method sk_sp<SkImage> makeRasterImage() const
Cary Clark4855f782018-02-06 09:41:53 -05001936#In Constructor
1937#Line # creates Image compatible with Raster_Surface if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001938Returns Raster_Image. Copies Image backed by GPU_Texture into CPU memory,
Cary Clark4855f782018-02-06 09:41:53 -05001939or decodes Image from Lazy_Image. Returns original Image if decoded in
Cary Clarkac47b882018-01-11 10:35:44 -05001940Raster_Bitmap.
Cary Clarka560c472017-11-27 10:44:06 -05001941
Cary Clarkac47b882018-01-11 10:35:44 -05001942Returns nullptr if copy, decode, or pixel read fails.
Cary Clarka560c472017-11-27 10:44:06 -05001943
Cary Clarkac47b882018-01-11 10:35:44 -05001944#Return Raster_Image, or nullptr ##
1945
Cary Clark4855f782018-02-06 09:41:53 -05001946#Bug 7479
Cary Clarka560c472017-11-27 10:44:06 -05001947#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001948#Image 5
1949#Platform gpu
1950 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1951 if (nullptr == image) {
1952 return;
1953 }
1954 SkPaint paint;
1955 paint.setAntiAlias(true);
1956 paint.setTextAlign(SkPaint::kCenter_Align);
1957 sk_sp<SkImage> raster(image->makeRasterImage());
1958 canvas->drawImage(raster, 0, 0);
1959 canvas->drawString(label, raster->width() / 2, raster->height() / 4, paint);
1960 };
1961 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1962 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001963 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1964 nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001965 drawImage(image, "image");
1966 canvas->translate(image->width(), 0);
1967 drawImage(bitmapImage, "source");
1968 canvas->translate(-image->width(), image->height());
1969 drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001970##
1971
Cary Clarkac47b882018-01-11 10:35:44 -05001972#SeeAlso isTextureBacked isLazyGenerated MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -05001973
1974#Method ##
1975
1976# ------------------------------------------------------------------------------
1977
1978#Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
1979 const SkIRect& clipBounds, SkIRect* outSubset,
1980 SkIPoint* offset) const
Cary Clark4855f782018-02-06 09:41:53 -05001981#In Constructor
1982#Line # creates filtered, clipped Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001983
Cary Clarkac47b882018-01-11 10:35:44 -05001984Creates filtered Image. filter processes original Image, potentially changing
1985color, position, and size. subset is the bounds of original Image processed
1986by filter. clipBounds is the expected bounds of the filtered Image. outSubset
1987is required storage for the actual bounds of the filtered Image. offset is
1988required storage for translation of returned Image.
Cary Clarka560c472017-11-27 10:44:06 -05001989
Cary Clarkac47b882018-01-11 10:35:44 -05001990Returns nullptr if Image could not be created. If nullptr is returned, outSubset
1991and offset are undefined.
1992
Cary Clark56356312018-02-08 14:45:18 -05001993Useful for animation of SkImageFilter that varies size from frame to frame.
1994Returned Image is created larger than required by filter so that GPU_Texture
1995can be reused with different sized effects. outSubset describes the valid bounds
1996of GPU_Texture returned. offset translates the returned Image to keep subsequent
1997animation frames aligned with respect to each other.
Cary Clarkac47b882018-01-11 10:35:44 -05001998
1999#Param filter how Image is sampled when transformed ##
Cary Clark56356312018-02-08 14:45:18 -05002000#Param subset bounds of Image processed by filter ##
2001#Param clipBounds expected bounds of filtered Image ##
2002#Param outSubset storage for returned Image bounds ##
2003#Param offset storage for returned Image translation ##
Cary Clarka560c472017-11-27 10:44:06 -05002004
Cary Clarkac47b882018-01-11 10:35:44 -05002005#Return filtered Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05002006
2007#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002008#Description
2009In each frame of the animation, filtered Image is drawn in a different location.
2010By translating canvas by returned offset, Image appears stationary.
2011##
2012#Image 5
2013#Platform gpu
2014#Duration 5
2015 sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make(
2016 -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE,
2017 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
2018 nullptr);
2019 sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr);
2020 SkIRect subset = image->bounds();
2021 SkIRect clipBounds = image->bounds();
2022 clipBounds.outset(60, 60);
2023 SkIRect outSubset;
2024 SkIPoint offset;
2025 sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds,
2026 &outSubset, &offset));
2027 SkPaint paint;
2028 paint.setAntiAlias(true);
2029 paint.setStyle(SkPaint::kStroke_Style);
2030 canvas->drawLine(0, 0, offset.fX, offset.fY, paint);
2031 canvas->translate(offset.fX, offset.fY);
2032 canvas->drawImage(filtered, 0, 0);
Cary Clark681287e2018-03-16 11:34:15 -04002033 canvas->drawRect(SkRect::Make(outSubset), paint);
Cary Clarka560c472017-11-27 10:44:06 -05002034##
2035
Cary Clark56356312018-02-08 14:45:18 -05002036#SeeAlso makeShader SkPaint::setImageFilter
Cary Clarka560c472017-11-27 10:44:06 -05002037
2038#Method ##
2039
2040# ------------------------------------------------------------------------------
2041
Cary Clarka560c472017-11-27 10:44:06 -05002042#Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc
2043
2044##
2045
2046# ------------------------------------------------------------------------------
2047
2048#Method static bool MakeBackendTextureFromSkImage(GrContext* context,
2049 sk_sp<SkImage> image,
2050 GrBackendTexture* backendTexture,
2051 BackendTextureReleaseProc* backendTextureReleaseProc)
Cary Clark4855f782018-02-06 09:41:53 -05002052#In Constructor
2053#Line # creates GPU_Texture from Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002054
Cary Clark56356312018-02-08 14:45:18 -05002055Creates a GrBackendTexture from the provided SkImage. Returns true and
2056stores result in backendTexture and backendTextureReleaseProc if
2057texture is created; otherwise, returns false and leaves
2058backendTexture and backendTextureReleaseProc unmodified.
Cary Clarka560c472017-11-27 10:44:06 -05002059
Cary Clark56356312018-02-08 14:45:18 -05002060Call backendTextureReleaseProc after deleting backendTexture.
2061backendTextureReleaseProc cleans up auxiliary data related to returned
2062backendTexture. The caller must delete returned backendTexture after use.
Cary Clarka560c472017-11-27 10:44:06 -05002063
Cary Clark56356312018-02-08 14:45:18 -05002064If Image is both texture backed and singly referenced, image is returned in
2065backendTexture without conversion or making a copy. Image is singly referenced
2066if its was transferred solely using std::move().
2067
2068If Image is not texture backed, returns texture with Image contents.
Cary Clarka560c472017-11-27 10:44:06 -05002069
Cary Clark61ca7c52018-01-02 11:34:14 -05002070#Param context GPU_Context ##
Cary Clark56356312018-02-08 14:45:18 -05002071#Param image Image used for texture ##
2072#Param backendTexture storage for backend texture ##
2073#Param backendTextureReleaseProc storage for clean up function ##
Cary Clarka560c472017-11-27 10:44:06 -05002074
Cary Clark56356312018-02-08 14:45:18 -05002075#Return true if backend texture was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002076
2077#Example
Cary Clark56356312018-02-08 14:45:18 -05002078#Platform gpu
2079#Height 64
2080#Function
Brian Salomon67f85842018-02-09 08:50:22 -05002081static sk_sp<SkImage> create_gpu_image(GrContext* grContext) {
2082 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
2083 auto surface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info));
2084 SkCanvas* canvas = surface->getCanvas();
2085 canvas->clear(SK_ColorWHITE);
2086 SkPaint paint;
2087 paint.setColor(SK_ColorBLACK);
2088 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
2089 return surface->makeImageSnapshot();
2090}
2091##
2092
2093void draw(SkCanvas* canvas) {
2094 GrContext* grContext = canvas->getGrContext();
2095 if (!grContext) {
2096 return;
2097 }
2098 sk_sp<SkImage> backEndImage = create_gpu_image(grContext);
2099 canvas->drawImage(backEndImage, 0, 0);
2100 GrBackendTexture texture;
2101 SkImage::BackendTextureReleaseProc proc;
2102 if (!SkImage::MakeBackendTextureFromSkImage(grContext, std::move(backEndImage),
2103 &texture, &proc)) {
2104 return;
2105 }
2106 sk_sp<SkImage> i2 = SkImage::MakeFromTexture(grContext, texture, kTopLeft_GrSurfaceOrigin,
2107 kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
2108 canvas->drawImage(i2, 30, 30);
Cary Clark56356312018-02-08 14:45:18 -05002109}
Cary Clarka560c472017-11-27 10:44:06 -05002110##
2111
Cary Clark56356312018-02-08 14:45:18 -05002112#SeeAlso MakeFromTexture makeTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002113
2114#Method ##
2115
2116# ------------------------------------------------------------------------------
2117
2118#Enum LegacyBitmapMode
Cary Clark56356312018-02-08 14:45:18 -05002119#Deprecated soon
Cary Clarka560c472017-11-27 10:44:06 -05002120#Code
2121 enum LegacyBitmapMode {
2122 kRO_LegacyBitmapMode,
Cary Clarka560c472017-11-27 10:44:06 -05002123 };
2124##
2125
Cary Clarka560c472017-11-27 10:44:06 -05002126#Const kRO_LegacyBitmapMode 0
Cary Clark56356312018-02-08 14:45:18 -05002127Returned bitmap is read-only and immutable.
Cary Clarka560c472017-11-27 10:44:06 -05002128##
Cary Clarka560c472017-11-27 10:44:06 -05002129
2130#Enum ##
2131
2132# ------------------------------------------------------------------------------
2133
Cary Clark56356312018-02-08 14:45:18 -05002134#Method bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode = kRO_LegacyBitmapMode) const
Cary Clark4855f782018-02-06 09:41:53 -05002135#In Constructor
2136#Line # returns as Raster_Bitmap ##
Cary Clarkac47b882018-01-11 10:35:44 -05002137Creates raster Bitmap with same pixels as Image. If legacyBitmapMode is
2138kRO_LegacyBitmapMode, returned bitmap is read-only and immutable.
2139Returns true if Bitmap is stored in bitmap. Returns false and resets bitmap if
2140Bitmap write did not succeed.
Cary Clarka560c472017-11-27 10:44:06 -05002141
Cary Clark3cd22cc2017-12-01 11:49:58 -05002142#Param bitmap storage for legacy Bitmap ##
Cary Clark56356312018-02-08 14:45:18 -05002143#Param legacyBitmapMode to be deprecated ##
Cary Clarka560c472017-11-27 10:44:06 -05002144
Cary Clark3cd22cc2017-12-01 11:49:58 -05002145#Return true if Bitmap was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002146
2147#Example
Cary Clark56356312018-02-08 14:45:18 -05002148#Image 4
2149#Platform gpu
Brian Salomon67f85842018-02-09 08:50:22 -05002150 SkBitmap bitImage;
2151 if (image->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2152 canvas->drawBitmap(bitImage, 0, 0);
2153 }
2154 GrContext* grContext = canvas->getGrContext();
2155 if (!grContext) {
2156 return;
2157 }
2158 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(grContext, backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04002159 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
2160 nullptr));
Brian Salomon67f85842018-02-09 08:50:22 -05002161 canvas->drawImage(textureImage, 45, 45);
2162 if (textureImage->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2163 canvas->drawBitmap(bitImage, 90, 90);
2164 }
Cary Clarka560c472017-11-27 10:44:06 -05002165##
2166
Cary Clark56356312018-02-08 14:45:18 -05002167#SeeAlso MakeRasterData makeRasterImage makeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002168
2169#Method ##
2170
2171# ------------------------------------------------------------------------------
2172
2173#Method bool isLazyGenerated() const
Cary Clark4855f782018-02-06 09:41:53 -05002174#In Property
2175#Line # returns if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002176Returns true if Image is backed by an image-generator or other service that creates
2177and caches its pixels or texture on-demand.
2178
Cary Clark2f466242017-12-11 16:03:17 -05002179#Return true if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002180
2181#Example
Cary Clark2f466242017-12-11 16:03:17 -05002182#Height 80
2183#Function
2184class TestImageGenerator : public SkImageGenerator {
2185public:
2186 TestImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(10, 10)) {}
2187 ~TestImageGenerator() override {}
2188protected:
2189 bool onGetPixels(const SkImageInfo& info, void* pixelPtr, size_t rowBytes,
2190 const Options& options) override {
2191 SkPMColor* pixels = static_cast<SkPMColor*>(pixelPtr);
2192 for (int y = 0; y < info.height(); ++y) {
2193 for (int x = 0; x < info.width(); ++x) {
2194 pixels[y * info.width() + x] = 0xff223344 + y * 0x000C0811;
2195 }
2196 }
2197 return true;
2198 }
2199};
2200##
2201void draw(SkCanvas* canvas) {
2202 auto gen = std::unique_ptr<TestImageGenerator>(new TestImageGenerator());
2203 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
2204 SkString lazy(image->isLazyGenerated() ? "is lazy" : "not lazy");
2205 canvas->scale(8, 8);
2206 canvas->drawImage(image, 0, 0, nullptr);
2207 SkPaint paint;
2208 paint.setTextSize(4);
2209 canvas->drawString(lazy, 2, 5, paint);
2210}
Cary Clarka560c472017-11-27 10:44:06 -05002211##
2212
Cary Clarkf5404bb2018-01-05 12:10:09 -05002213#Example
2214#Image 5
2215#Platform gpu
2216void draw(SkCanvas* canvas) {
2217 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2218 if (nullptr == image) {
2219 return;
2220 }
2221 SkPaint paint;
2222 paint.setAntiAlias(true);
2223 paint.setTextAlign(SkPaint::kCenter_Align);
2224 canvas->drawImage(image, 0, 0);
2225 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
2226 canvas->drawString(
2227 image->isLazyGenerated() ? "is lazily generated" : "not lazily generated",
2228 image->width() / 2, image->height() * 3 / 4, paint);
2229 };
2230 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2231 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04002232 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
2233 nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05002234 drawImage(image, "image");
2235 canvas->translate(image->width(), 0);
2236 drawImage(bitmapImage, "source");
2237 canvas->translate(-image->width(), image->height());
2238 drawImage(textureImage, "backEndTexture");
2239}
2240##
2241
Cary Clarkac47b882018-01-11 10:35:44 -05002242#SeeAlso isTextureBacked MakeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002243
2244#Method ##
2245
2246# ------------------------------------------------------------------------------
2247
2248#Method sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target,
2249 SkTransferFunctionBehavior premulBehavior) const
Cary Clark4855f782018-02-06 09:41:53 -05002250#In Constructor
2251#Line # creates Image matching Color_Space if possible ##
Cary Clarka560c472017-11-27 10:44:06 -05002252
Cary Clarkac47b882018-01-11 10:35:44 -05002253Creates Image in target Color_Space.
2254Returns nullptr if Image could not be created.
Cary Clarka560c472017-11-27 10:44:06 -05002255
Cary Clarkac47b882018-01-11 10:35:44 -05002256Returns original Image if it is in target Color_Space.
2257Otherwise, converts pixels from Image Color_Space to target Color_Space.
2258If Image colorSpace returns nullptr, Image Color_Space is assumed to be sRGB.
2259
2260SkTransferFunctionBehavior is to be deprecated.
2261
2262Set premulBehavior to SkTransferFunctionBehavior::kRespect to convert Image
2263pixels to a linear space, before converting to destination Color_Type
Cary Clarka560c472017-11-27 10:44:06 -05002264and Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -05002265
Cary Clarkac47b882018-01-11 10:35:44 -05002266Set premulBehavior to SkTransferFunctionBehavior::kIgnore to treat Image
2267pixels as linear, when converting to destination Color_Type
2268and Color_Space, ignoring pixel encoding.
Cary Clarka560c472017-11-27 10:44:06 -05002269
Cary Clarkac47b882018-01-11 10:35:44 -05002270#Param target Color_Space describing color range of returned Image ##
2271#Param premulBehavior one of: SkTransferFunctionBehavior::kRespect,
2272 SkTransferFunctionBehavior::kIgnore
Cary Clarka560c472017-11-27 10:44:06 -05002273##
2274
Cary Clarkac47b882018-01-11 10:35:44 -05002275#Return created Image in target Color_Space ##
2276
2277#Example
2278#Image 5
2279#Set sRGB
2280 sk_sp<SkColorSpace> normalColorSpace = SkColorSpace::MakeRGB(
2281 SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kSRGB_Gamut);
2282 sk_sp<SkColorSpace> wackyColorSpace = normalColorSpace->makeColorSpin();
2283 for (auto colorSpace : { normalColorSpace, wackyColorSpace } ) {
2284 for (auto transfer : { SkTransferFunctionBehavior::kRespect,
2285 SkTransferFunctionBehavior::kIgnore } ) {
2286 sk_sp<SkImage> colorSpaced = image->makeColorSpace(colorSpace, transfer);
2287 canvas->drawImage(colorSpaced, 0, 0);
2288 canvas->translate(128, 0);
2289 }
2290 canvas->translate(-256, 128);
2291 }
2292##
2293
2294#SeeAlso MakeFromPixture MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05002295
2296#Method ##
2297
2298#Class SkImage ##
2299
2300#Topic Image ##