blob: 781ad71c1d0d4f3577080e55443ba33949b51261 [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
Cary Clark2f466242017-12-11 16:03:17 -05001329If origin in 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
1393#Enum CachingHint
1394
1395#Code
1396 enum CachingHint {
1397 kAllow_CachingHint,
1398 kDisallow_CachingHint,
1399 };
1400##
1401
Cary Clarkac47b882018-01-11 10:35:44 -05001402CachingHint selects whether Skia may internally cache Bitmaps generated by
1403decoding Image, or by copying Image from GPU to CPU. The default behavior
1404allows caching Bitmaps.
1405
1406Choose kDisallow_CachingHint if Image pixels are to be used only once, or
1407if Image pixels reside in a cache outside of Skia, or to reduce memory pressure.
1408
1409Choosing kAllow_CachingHint does not ensure that pixels will be cached.
1410Image pixels may not be cached if memory requirements are too large or
1411pixels are not accessible.
Cary Clarka560c472017-11-27 10:44:06 -05001412
1413#Const kAllow_CachingHint 0
Cary Clarkac47b882018-01-11 10:35:44 -05001414Allows Skia to internally cache decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001415##
1416#Const kDisallow_CachingHint 1
Cary Clarkac47b882018-01-11 10:35:44 -05001417Disallows Skia from internally caching decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001418##
1419
Cary Clarkac47b882018-01-11 10:35:44 -05001420#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001421##
1422
Cary Clarkac47b882018-01-11 10:35:44 -05001423#SeeAlso readPixels scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001424
1425#Enum ##
1426
1427# ------------------------------------------------------------------------------
1428
1429#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1430 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001431#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001432#Line # copies and converts pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001433
Cary Clarkac47b882018-01-11 10:35:44 -05001434Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY),
1435and does not exceed Image (width(), height()).
1436
1437dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
1438destination. dstRowBytes specifics the gap from one destination row to the next.
1439Returns true if pixels are copied. Returns false if:
1440#List
1441# dstInfo.addr() equals nullptr ##
1442# dstRowBytes is less than dstInfo.minRowBytes ##
1443# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001444##
1445
Cary Clarkac47b882018-01-11 10:35:44 -05001446Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1447kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1448If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace must match.
1449If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType must
1450match. If Image Color_Space is nullptr, dstInfo.colorSpace must match. Returns
1451false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001452
Cary Clarkac47b882018-01-11 10:35:44 -05001453srcX and srcY may be negative to copy only top or left of source. Returns
1454false if width() or height() is zero or negative.
1455Returns false if
1456#Formula
1457abs(srcX) >= Image width()
1458##
1459, or if
1460#Formula
1461abs(srcY) >= Image height()
1462##
1463.
Cary Clarka560c472017-11-27 10:44:06 -05001464
Cary Clarkac47b882018-01-11 10:35:44 -05001465If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1466If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1467
1468#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1469#Param dstPixels destination pixel storage ##
1470#Param dstRowBytes destination row length ##
1471#Param srcX column index whose absolute value is less than width() ##
1472#Param srcY row index whose absolute value is less than height() ##
1473#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1474
1475#Return true if pixels are copied to dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001476
1477#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001478#Image 3
1479 canvas->scale(.5f, .5f);
1480 const int width = 32;
1481 const int height = 32;
1482 std::vector<int32_t> dstPixels;
1483 dstPixels.resize(height * width * 4);
1484 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1485 for (int y = 0; y < 512; y += height ) {
1486 for (int x = 0; x < 512; x += width ) {
1487 if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) {
1488 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
1489 SkBitmap bitmap;
1490 bitmap.installPixels(dstPixmap);
1491 canvas->drawBitmap(bitmap, 0, 0);
1492 }
1493 canvas->translate(48, 0);
1494 }
1495 canvas->translate(-16 * 48, 48);
1496 }
Cary Clarka560c472017-11-27 10:44:06 -05001497##
1498
Cary Clarkac47b882018-01-11 10:35:44 -05001499#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001500
1501#Method ##
1502
1503# ------------------------------------------------------------------------------
1504
1505#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY,
1506 CachingHint cachingHint = kAllow_CachingHint) const
1507
Cary Clarkac47b882018-01-11 10:35:44 -05001508Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and
1509does not exceed Image (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001510
Cary Clarkac47b882018-01-11 10:35:44 -05001511dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
1512and row bytes of destination. dst.rowBytes specifics the gap from one destination
1513row to the next. Returns true if pixels are copied. Returns false if:
1514#List
1515# dst pixel storage equals nullptr ##
1516# dst.rowBytes is less than SkImageInfo::minRowBytes ##
1517# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001518##
1519
Cary Clarkac47b882018-01-11 10:35:44 -05001520Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1521kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1522If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1523If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1524match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1525false if pixel conversion is not possible.
1526
1527srcX and srcY may be negative to copy only top or left of source. Returns
1528false if width() or height() is zero or negative.
1529Returns false if
1530#Formula
1531abs(srcX) >= Image width()
1532##
1533, or if
1534#Formula
1535abs(srcY) >= Image height()
1536##
1537.
1538
1539If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1540If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1541
1542#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1543#Param srcX column index whose absolute value is less than width() ##
1544#Param srcY row index whose absolute value is less than height() ##
1545#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1546
1547#Return true if pixels are copied to dst ##
1548
1549#Example
1550#Image 3
1551 std::vector<int32_t> srcPixels;
1552 int rowBytes = image->width() * 4;
1553 int quarterWidth = image->width() / 4;
1554 int quarterHeight = image->height() / 4;
1555 srcPixels.resize(image->height() * rowBytes);
1556 for (int y = 0; y < 4; ++y) {
1557 for (int x = 0; x < 4; ++x) {
1558 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1559 &srcPixels.front() + x * image->height() * quarterWidth +
1560 y * quarterWidth, rowBytes);
1561 image->readPixels(pixmap, x * quarterWidth, y * quarterHeight);
1562 }
1563 }
1564 canvas->scale(.5f, .5f);
1565 SkBitmap bitmap;
1566 bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()),
1567 &srcPixels.front(), rowBytes);
1568 canvas->drawBitmap(bitmap, 0, 0);
1569##
1570
1571#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001572
1573#Method ##
1574
1575# ------------------------------------------------------------------------------
1576
1577#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality,
1578 CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001579#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001580#Line # scales and converts one Image to another ##
Cary Clarka560c472017-11-27 10:44:06 -05001581
Cary Clarkac47b882018-01-11 10:35:44 -05001582Copies Image to dst, scaling pixels to fit dst.width() and dst.height(), and
1583converting pixels to match dst.colorType and dst.alphaType. Returns true if
1584pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1585less than dst SkImageInfo::minRowBytes.
Cary Clarka560c472017-11-27 10:44:06 -05001586
Cary Clarkac47b882018-01-11 10:35:44 -05001587Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1588kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1589If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1590If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1591match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1592false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001593
Cary Clarkac47b882018-01-11 10:35:44 -05001594Scales the image, with filterQuality, to match dst.width() and dst.height().
1595filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1596Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1597Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1598Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1599kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1600
1601If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1602If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1603
1604#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1605#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1606 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1607##
1608#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1609
1610#Return true if pixels are scaled to fit dst ##
Cary Clarka560c472017-11-27 10:44:06 -05001611
1612#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001613#Image 3
1614#Height 128
1615 std::vector<int32_t> srcPixels;
1616 int quarterWidth = image->width() / 16;
1617 int rowBytes = quarterWidth * 4;
1618 int quarterHeight = image->height() / 16;
1619 srcPixels.resize(quarterHeight * rowBytes);
1620 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1621 &srcPixels.front(), rowBytes);
1622 canvas->scale(4, 4);
1623 SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality,
1624 kMedium_SkFilterQuality, kHigh_SkFilterQuality };
1625 for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) {
1626 image->scalePixels(pixmap, qualities[index]);
1627 sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
1628 canvas->drawImage(filtered, 16 * index, 0);
1629 }
Cary Clarka560c472017-11-27 10:44:06 -05001630##
1631
Cary Clarkac47b882018-01-11 10:35:44 -05001632#SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001633
1634#Method ##
1635
1636# ------------------------------------------------------------------------------
1637
1638#Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const
Cary Clark78de7512018-02-07 07:27:09 -05001639#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001640#Line # returns encoded Image as SkData ##
Cary Clarkac47b882018-01-11 10:35:44 -05001641Encodes Image pixels, returning result as SkData.
Cary Clark2f466242017-12-11 16:03:17 -05001642
Cary Clarkac47b882018-01-11 10:35:44 -05001643Returns nullptr if encoding fails, or if encodedImageFormat is not supported.
Cary Clarka560c472017-11-27 10:44:06 -05001644
Cary Clarkac47b882018-01-11 10:35:44 -05001645Image encoding in a format requires both building with one or more of:
1646SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support
1647for the encoded format.
1648
1649If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can
1650additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP,
1651SkEncodedImageFormat::kGIF.
1652
1653quality is a platform and format specific metric trading off size and encoding
1654error. When used, quality equaling 100 encodes with the least error. quality may
1655be ignored by the encoder.
1656
1657#Param encodedImageFormat one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
1658 SkEncodedImageFormat::kWEBP
1659 ##
1660#Param quality encoder specific metric with 100 equaling best ##
Cary Clarka560c472017-11-27 10:44:06 -05001661
Cary Clark2f466242017-12-11 16:03:17 -05001662#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001663
1664#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001665#Image 3
1666 canvas->scale(4, 4);
1667 SkIRect subset = {0, 0, 16, 64};
1668 int x = 0;
1669 for (int quality : { 0, 10, 50, 100 } ) {
1670 sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
1671 sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
1672 canvas->drawImage(filtered, x, 0);
1673 x += 16;
1674 }
Cary Clarka560c472017-11-27 10:44:06 -05001675##
1676
Cary Clarkac47b882018-01-11 10:35:44 -05001677#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001678
1679#Method ##
1680
1681# ------------------------------------------------------------------------------
1682
Cary Clark61ca7c52018-01-02 11:34:14 -05001683#Method sk_sp<SkData> encodeToData() const
Cary Clarka560c472017-11-27 10:44:06 -05001684
Cary Clarkac47b882018-01-11 10:35:44 -05001685Encodes Image pixels, returning result as SkData. Returns existing encoded data
1686if present; otherwise, Image is encoded with SkEncodedImageFormat::kPNG. Skia
1687must be built with SK_HAS_PNG_LIBRARY to encode Image.
Cary Clarka560c472017-11-27 10:44:06 -05001688
Cary Clarkac47b882018-01-11 10:35:44 -05001689Returns nullptr if existing encoded data is missing or invalid, and
Cary Clarka560c472017-11-27 10:44:06 -05001690encoding fails.
1691
Cary Clarkac47b882018-01-11 10:35:44 -05001692#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001693
1694#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001695#Image 3
1696 canvas->scale(4, 4);
1697 SkIRect subset = {136, 32, 200, 96};
1698 sk_sp<SkData> data(image->encodeToData());
1699 sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
1700 canvas->drawImage(eye, 0, 0);
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
1709#Method sk_sp<SkData> refEncodedData() const
Cary Clark78de7512018-02-07 07:27:09 -05001710#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001711#Line # returns Image encoded in SkData if present ##
Cary Clarkac47b882018-01-11 10:35:44 -05001712Returns encoded Image pixels as SkData, if Image was created from supported
1713encoded stream format. Platform support for formats vary and may require building
1714with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY.
Cary Clarka560c472017-11-27 10:44:06 -05001715
Cary Clarkac47b882018-01-11 10:35:44 -05001716Returns nullptr if Image contents are not encoded.
Cary Clarka560c472017-11-27 10:44:06 -05001717
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#Platform gpu
1723 struct {
1724 const char* name;
1725 sk_sp<SkImage> image;
1726 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1727 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001728 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1729 nullptr) } };
Cary Clarkac47b882018-01-11 10:35:44 -05001730 SkString string;
1731 SkPaint paint;
1732 for (const auto& test : tests ) {
1733 if (!test.image) {
1734 string.printf("no %s", test.name);
1735 } else {
1736 string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name);
1737 }
1738 canvas->drawString(string, 10, 20, paint);
1739 canvas->translate(0, 20);
1740 }
Cary Clarka560c472017-11-27 10:44:06 -05001741##
1742
Cary Clarkac47b882018-01-11 10:35:44 -05001743#SeeAlso encodeToData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001744
1745#Method ##
1746
1747# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001748#Subtopic Utility
1749#Populate
1750#Line # rarely called management functions ##
1751##
Cary Clarka560c472017-11-27 10:44:06 -05001752
1753#Method const char* toString(SkString* string) const
Cary Clark4855f782018-02-06 09:41:53 -05001754#In Utility
1755#Line # converts Image to machine readable form ##
Cary Clarkac47b882018-01-11 10:35:44 -05001756Appends Image description to string, including unique ID, width, height, and
1757whether the image is opaque.
Cary Clarka560c472017-11-27 10:44:06 -05001758
Cary Clarkac47b882018-01-11 10:35:44 -05001759#Param string storage for description; existing content is preserved ##
1760
1761#Return string appended with Image description ##
Cary Clarka560c472017-11-27 10:44:06 -05001762
1763#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001764#Image 4
1765 struct {
1766 const char* name;
1767 sk_sp<SkImage> image;
1768 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1769 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001770 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1771 nullptr) } };
Cary Clarkac47b882018-01-11 10:35:44 -05001772 SkString string;
1773 SkPaint paint;
1774 for (const auto& test : tests ) {
1775 string.printf("%s: ", test.name);
1776 test.image ? (void) test.image->toString(&string) : string.append("no image");
1777 canvas->drawString(string, 10, 20, paint);
1778 canvas->translate(0, 20);
1779 }
Cary Clarka560c472017-11-27 10:44:06 -05001780##
1781
Cary Clarkac47b882018-01-11 10:35:44 -05001782#SeeAlso SkPaint::toString
Cary Clarka560c472017-11-27 10:44:06 -05001783
1784#Method ##
1785
1786# ------------------------------------------------------------------------------
1787
1788#Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const
Cary Clark4855f782018-02-06 09:41:53 -05001789#In Constructor
1790#Line # creates Image containing part of original ##
Cary Clarkac47b882018-01-11 10:35:44 -05001791Returns subset of Image. subset must be fully contained by Image dimensions().
1792The implementation may share pixels, or may copy them.
Cary Clarka560c472017-11-27 10:44:06 -05001793
Cary Clarkac47b882018-01-11 10:35:44 -05001794Returns nullptr if subset is empty, or subset is not contained by bounds, or
1795pixels in Image could not be read or copied.
Cary Clarka560c472017-11-27 10:44:06 -05001796
Cary Clarkac47b882018-01-11 10:35:44 -05001797#Param subset bounds of returned Image ##
1798
1799#Return partial or full Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001800
1801#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001802#Image 3
1803 canvas->scale(.5f, .5f);
1804 const int width = 32;
1805 const int height = 32;
1806 for (int y = 0; y < 512; y += height ) {
1807 for (int x = 0; x < 512; x += width ) {
1808 sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height}));
1809 canvas->drawImage(subset, x * 3 / 2, y * 3 / 2);
1810 }
1811 }
Cary Clarka560c472017-11-27 10:44:06 -05001812##
1813
Cary Clarkac47b882018-01-11 10:35:44 -05001814#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001815
1816#Method ##
1817
1818# ------------------------------------------------------------------------------
1819
1820#Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const
Cary Clark4855f782018-02-06 09:41:53 -05001821#In Constructor
1822#Line # creates Image matching Color_Space if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001823Returns Image backed by GPU_Texture associated with context. Returned Image is
1824compatible with Surface created with dstColorSpace. Returns original
1825Image if context and dstColorSpace match.
1826
1827Returns nullptr if context is nullptr, or if Image was created with another
1828GrContext.
Cary Clarka560c472017-11-27 10:44:06 -05001829
Cary Clark61ca7c52018-01-02 11:34:14 -05001830#Param context GPU_Context ##
Cary Clarkac47b882018-01-11 10:35:44 -05001831#Param dstColorSpace range of colors of matching Surface on GPU ##
Cary Clarka560c472017-11-27 10:44:06 -05001832
Cary Clarkac47b882018-01-11 10:35:44 -05001833#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001834
1835#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001836#Platform gpu
1837#Image 5
1838 auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void {
1839 if (nullptr == image || nullptr == context) {
1840 return;
1841 }
1842 SkPaint paint;
1843 paint.setAntiAlias(true);
1844 paint.setTextAlign(SkPaint::kCenter_Align);
1845 sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr));
1846 canvas->drawImage(texture, 0, 0);
1847 canvas->drawString(label, texture->width() / 2, texture->height() / 4, paint);
1848 };
1849 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1850 GrContext* context = canvas->getGrContext();
1851 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001852 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1853 nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001854 drawImage(image, context, "image");
1855 canvas->translate(image->width(), 0);
1856 drawImage(bitmapImage, context, "source");
1857 canvas->translate(-image->width(), image->height());
1858 drawImage(textureImage, context, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001859##
1860
Cary Clarkac47b882018-01-11 10:35:44 -05001861#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05001862
1863#Method ##
1864
1865# ------------------------------------------------------------------------------
1866
1867#Method sk_sp<SkImage> makeNonTextureImage() const
Cary Clark4855f782018-02-06 09:41:53 -05001868#In Constructor
1869#Line # creates Image without dependency on GPU_Texture ##
Cary Clarkac47b882018-01-11 10:35:44 -05001870Returns Raster_Image or Lazy_Image. Copies Image backed by GPU_Texture into
Cary Clark4855f782018-02-06 09:41:53 -05001871CPU memory if needed. Returns original Image if decoded in Raster_Bitmap,
Cary Clarkac47b882018-01-11 10:35:44 -05001872or if encoded in a stream.
Cary Clark61ca7c52018-01-02 11:34:14 -05001873
Cary Clarkac47b882018-01-11 10:35:44 -05001874Returns nullptr if backed by GPU_Texture and copy fails.
1875
1876#Return Raster_Image, Lazy_Image, or nullptr ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001877
1878#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001879#Image 5
1880#Platform gpu
1881 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1882 if (nullptr == image) {
1883 return;
1884 }
1885 SkPaint paint;
1886 paint.setAntiAlias(true);
1887 paint.setTextAlign(SkPaint::kCenter_Align);
1888 sk_sp<SkImage> nonTexture(image->makeNonTextureImage());
1889 canvas->drawImage(nonTexture, 0, 0);
1890 canvas->drawString(label, nonTexture->width() / 2, nonTexture->height() / 4, paint);
1891 };
1892 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1893 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001894 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1895 nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001896 drawImage(image, "image");
1897 canvas->translate(image->width(), 0);
1898 drawImage(bitmapImage, "source");
1899 canvas->translate(-image->width(), image->height());
1900 drawImage(textureImage, "backEndTexture");
Cary Clark61ca7c52018-01-02 11:34:14 -05001901##
1902
Cary Clark56356312018-02-08 14:45:18 -05001903#SeeAlso makeTextureImage makeRasterImage MakeBackendTextureFromSkImage
Cary Clark61ca7c52018-01-02 11:34:14 -05001904
1905#Method ##
1906
1907# ------------------------------------------------------------------------------
1908
1909#Method sk_sp<SkImage> makeRasterImage() const
Cary Clark4855f782018-02-06 09:41:53 -05001910#In Constructor
1911#Line # creates Image compatible with Raster_Surface if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001912Returns Raster_Image. Copies Image backed by GPU_Texture into CPU memory,
Cary Clark4855f782018-02-06 09:41:53 -05001913or decodes Image from Lazy_Image. Returns original Image if decoded in
Cary Clarkac47b882018-01-11 10:35:44 -05001914Raster_Bitmap.
Cary Clarka560c472017-11-27 10:44:06 -05001915
Cary Clarkac47b882018-01-11 10:35:44 -05001916Returns nullptr if copy, decode, or pixel read fails.
Cary Clarka560c472017-11-27 10:44:06 -05001917
Cary Clarkac47b882018-01-11 10:35:44 -05001918#Return Raster_Image, or nullptr ##
1919
Cary Clark4855f782018-02-06 09:41:53 -05001920#Bug 7479
Cary Clarka560c472017-11-27 10:44:06 -05001921#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001922#Image 5
1923#Platform gpu
1924 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1925 if (nullptr == image) {
1926 return;
1927 }
1928 SkPaint paint;
1929 paint.setAntiAlias(true);
1930 paint.setTextAlign(SkPaint::kCenter_Align);
1931 sk_sp<SkImage> raster(image->makeRasterImage());
1932 canvas->drawImage(raster, 0, 0);
1933 canvas->drawString(label, raster->width() / 2, raster->height() / 4, paint);
1934 };
1935 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1936 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001937 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1938 nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001939 drawImage(image, "image");
1940 canvas->translate(image->width(), 0);
1941 drawImage(bitmapImage, "source");
1942 canvas->translate(-image->width(), image->height());
1943 drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001944##
1945
Cary Clarkac47b882018-01-11 10:35:44 -05001946#SeeAlso isTextureBacked isLazyGenerated MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -05001947
1948#Method ##
1949
1950# ------------------------------------------------------------------------------
1951
1952#Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
1953 const SkIRect& clipBounds, SkIRect* outSubset,
1954 SkIPoint* offset) const
Cary Clark4855f782018-02-06 09:41:53 -05001955#In Constructor
1956#Line # creates filtered, clipped Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001957
Cary Clarkac47b882018-01-11 10:35:44 -05001958Creates filtered Image. filter processes original Image, potentially changing
1959color, position, and size. subset is the bounds of original Image processed
1960by filter. clipBounds is the expected bounds of the filtered Image. outSubset
1961is required storage for the actual bounds of the filtered Image. offset is
1962required storage for translation of returned Image.
Cary Clarka560c472017-11-27 10:44:06 -05001963
Cary Clarkac47b882018-01-11 10:35:44 -05001964Returns nullptr if Image could not be created. If nullptr is returned, outSubset
1965and offset are undefined.
1966
Cary Clark56356312018-02-08 14:45:18 -05001967Useful for animation of SkImageFilter that varies size from frame to frame.
1968Returned Image is created larger than required by filter so that GPU_Texture
1969can be reused with different sized effects. outSubset describes the valid bounds
1970of GPU_Texture returned. offset translates the returned Image to keep subsequent
1971animation frames aligned with respect to each other.
Cary Clarkac47b882018-01-11 10:35:44 -05001972
1973#Param filter how Image is sampled when transformed ##
Cary Clark56356312018-02-08 14:45:18 -05001974#Param subset bounds of Image processed by filter ##
1975#Param clipBounds expected bounds of filtered Image ##
1976#Param outSubset storage for returned Image bounds ##
1977#Param offset storage for returned Image translation ##
Cary Clarka560c472017-11-27 10:44:06 -05001978
Cary Clarkac47b882018-01-11 10:35:44 -05001979#Return filtered Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001980
1981#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001982#Description
1983In each frame of the animation, filtered Image is drawn in a different location.
1984By translating canvas by returned offset, Image appears stationary.
1985##
1986#Image 5
1987#Platform gpu
1988#Duration 5
1989 sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make(
1990 -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE,
1991 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
1992 nullptr);
1993 sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr);
1994 SkIRect subset = image->bounds();
1995 SkIRect clipBounds = image->bounds();
1996 clipBounds.outset(60, 60);
1997 SkIRect outSubset;
1998 SkIPoint offset;
1999 sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds,
2000 &outSubset, &offset));
2001 SkPaint paint;
2002 paint.setAntiAlias(true);
2003 paint.setStyle(SkPaint::kStroke_Style);
2004 canvas->drawLine(0, 0, offset.fX, offset.fY, paint);
2005 canvas->translate(offset.fX, offset.fY);
2006 canvas->drawImage(filtered, 0, 0);
Cary Clark681287e2018-03-16 11:34:15 -04002007 canvas->drawRect(SkRect::Make(outSubset), paint);
Cary Clarka560c472017-11-27 10:44:06 -05002008##
2009
Cary Clark56356312018-02-08 14:45:18 -05002010#SeeAlso makeShader SkPaint::setImageFilter
Cary Clarka560c472017-11-27 10:44:06 -05002011
2012#Method ##
2013
2014# ------------------------------------------------------------------------------
2015
Cary Clarka560c472017-11-27 10:44:06 -05002016#Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc
2017
2018##
2019
2020# ------------------------------------------------------------------------------
2021
2022#Method static bool MakeBackendTextureFromSkImage(GrContext* context,
2023 sk_sp<SkImage> image,
2024 GrBackendTexture* backendTexture,
2025 BackendTextureReleaseProc* backendTextureReleaseProc)
Cary Clark4855f782018-02-06 09:41:53 -05002026#In Constructor
2027#Line # creates GPU_Texture from Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002028
Cary Clark56356312018-02-08 14:45:18 -05002029Creates a GrBackendTexture from the provided SkImage. Returns true and
2030stores result in backendTexture and backendTextureReleaseProc if
2031texture is created; otherwise, returns false and leaves
2032backendTexture and backendTextureReleaseProc unmodified.
Cary Clarka560c472017-11-27 10:44:06 -05002033
Cary Clark56356312018-02-08 14:45:18 -05002034Call backendTextureReleaseProc after deleting backendTexture.
2035backendTextureReleaseProc cleans up auxiliary data related to returned
2036backendTexture. The caller must delete returned backendTexture after use.
Cary Clarka560c472017-11-27 10:44:06 -05002037
Cary Clark56356312018-02-08 14:45:18 -05002038If Image is both texture backed and singly referenced, image is returned in
2039backendTexture without conversion or making a copy. Image is singly referenced
2040if its was transferred solely using std::move().
2041
2042If Image is not texture backed, returns texture with Image contents.
Cary Clarka560c472017-11-27 10:44:06 -05002043
Cary Clark61ca7c52018-01-02 11:34:14 -05002044#Param context GPU_Context ##
Cary Clark56356312018-02-08 14:45:18 -05002045#Param image Image used for texture ##
2046#Param backendTexture storage for backend texture ##
2047#Param backendTextureReleaseProc storage for clean up function ##
Cary Clarka560c472017-11-27 10:44:06 -05002048
Cary Clark56356312018-02-08 14:45:18 -05002049#Return true if backend texture was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002050
2051#Example
Cary Clark56356312018-02-08 14:45:18 -05002052#Platform gpu
2053#Height 64
2054#Function
Brian Salomon67f85842018-02-09 08:50:22 -05002055static sk_sp<SkImage> create_gpu_image(GrContext* grContext) {
2056 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
2057 auto surface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info));
2058 SkCanvas* canvas = surface->getCanvas();
2059 canvas->clear(SK_ColorWHITE);
2060 SkPaint paint;
2061 paint.setColor(SK_ColorBLACK);
2062 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
2063 return surface->makeImageSnapshot();
2064}
2065##
2066
2067void draw(SkCanvas* canvas) {
2068 GrContext* grContext = canvas->getGrContext();
2069 if (!grContext) {
2070 return;
2071 }
2072 sk_sp<SkImage> backEndImage = create_gpu_image(grContext);
2073 canvas->drawImage(backEndImage, 0, 0);
2074 GrBackendTexture texture;
2075 SkImage::BackendTextureReleaseProc proc;
2076 if (!SkImage::MakeBackendTextureFromSkImage(grContext, std::move(backEndImage),
2077 &texture, &proc)) {
2078 return;
2079 }
2080 sk_sp<SkImage> i2 = SkImage::MakeFromTexture(grContext, texture, kTopLeft_GrSurfaceOrigin,
2081 kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
2082 canvas->drawImage(i2, 30, 30);
Cary Clark56356312018-02-08 14:45:18 -05002083}
Cary Clarka560c472017-11-27 10:44:06 -05002084##
2085
Cary Clark56356312018-02-08 14:45:18 -05002086#SeeAlso MakeFromTexture makeTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002087
2088#Method ##
2089
2090# ------------------------------------------------------------------------------
2091
2092#Enum LegacyBitmapMode
Cary Clark56356312018-02-08 14:45:18 -05002093#Deprecated soon
Cary Clarka560c472017-11-27 10:44:06 -05002094#Code
2095 enum LegacyBitmapMode {
2096 kRO_LegacyBitmapMode,
Cary Clarka560c472017-11-27 10:44:06 -05002097 };
2098##
2099
Cary Clarka560c472017-11-27 10:44:06 -05002100#Const kRO_LegacyBitmapMode 0
Cary Clark56356312018-02-08 14:45:18 -05002101Returned bitmap is read-only and immutable.
Cary Clarka560c472017-11-27 10:44:06 -05002102##
Cary Clarka560c472017-11-27 10:44:06 -05002103
2104#Enum ##
2105
2106# ------------------------------------------------------------------------------
2107
Cary Clark56356312018-02-08 14:45:18 -05002108#Method bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode = kRO_LegacyBitmapMode) const
Cary Clark4855f782018-02-06 09:41:53 -05002109#In Constructor
2110#Line # returns as Raster_Bitmap ##
Cary Clarkac47b882018-01-11 10:35:44 -05002111Creates raster Bitmap with same pixels as Image. If legacyBitmapMode is
2112kRO_LegacyBitmapMode, returned bitmap is read-only and immutable.
2113Returns true if Bitmap is stored in bitmap. Returns false and resets bitmap if
2114Bitmap write did not succeed.
Cary Clarka560c472017-11-27 10:44:06 -05002115
Cary Clark3cd22cc2017-12-01 11:49:58 -05002116#Param bitmap storage for legacy Bitmap ##
Cary Clark56356312018-02-08 14:45:18 -05002117#Param legacyBitmapMode to be deprecated ##
Cary Clarka560c472017-11-27 10:44:06 -05002118
Cary Clark3cd22cc2017-12-01 11:49:58 -05002119#Return true if Bitmap was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002120
2121#Example
Cary Clark56356312018-02-08 14:45:18 -05002122#Image 4
2123#Platform gpu
Brian Salomon67f85842018-02-09 08:50:22 -05002124 SkBitmap bitImage;
2125 if (image->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2126 canvas->drawBitmap(bitImage, 0, 0);
2127 }
2128 GrContext* grContext = canvas->getGrContext();
2129 if (!grContext) {
2130 return;
2131 }
2132 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(grContext, backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04002133 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
2134 nullptr));
Brian Salomon67f85842018-02-09 08:50:22 -05002135 canvas->drawImage(textureImage, 45, 45);
2136 if (textureImage->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2137 canvas->drawBitmap(bitImage, 90, 90);
2138 }
Cary Clarka560c472017-11-27 10:44:06 -05002139##
2140
Cary Clark56356312018-02-08 14:45:18 -05002141#SeeAlso MakeRasterData makeRasterImage makeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002142
2143#Method ##
2144
2145# ------------------------------------------------------------------------------
2146
2147#Method bool isLazyGenerated() const
Cary Clark4855f782018-02-06 09:41:53 -05002148#In Property
2149#Line # returns if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002150Returns true if Image is backed by an image-generator or other service that creates
2151and caches its pixels or texture on-demand.
2152
Cary Clark2f466242017-12-11 16:03:17 -05002153#Return true if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002154
2155#Example
Cary Clark2f466242017-12-11 16:03:17 -05002156#Height 80
2157#Function
2158class TestImageGenerator : public SkImageGenerator {
2159public:
2160 TestImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(10, 10)) {}
2161 ~TestImageGenerator() override {}
2162protected:
2163 bool onGetPixels(const SkImageInfo& info, void* pixelPtr, size_t rowBytes,
2164 const Options& options) override {
2165 SkPMColor* pixels = static_cast<SkPMColor*>(pixelPtr);
2166 for (int y = 0; y < info.height(); ++y) {
2167 for (int x = 0; x < info.width(); ++x) {
2168 pixels[y * info.width() + x] = 0xff223344 + y * 0x000C0811;
2169 }
2170 }
2171 return true;
2172 }
2173};
2174##
2175void draw(SkCanvas* canvas) {
2176 auto gen = std::unique_ptr<TestImageGenerator>(new TestImageGenerator());
2177 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
2178 SkString lazy(image->isLazyGenerated() ? "is lazy" : "not lazy");
2179 canvas->scale(8, 8);
2180 canvas->drawImage(image, 0, 0, nullptr);
2181 SkPaint paint;
2182 paint.setTextSize(4);
2183 canvas->drawString(lazy, 2, 5, paint);
2184}
Cary Clarka560c472017-11-27 10:44:06 -05002185##
2186
Cary Clarkf5404bb2018-01-05 12:10:09 -05002187#Example
2188#Image 5
2189#Platform gpu
2190void draw(SkCanvas* canvas) {
2191 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2192 if (nullptr == image) {
2193 return;
2194 }
2195 SkPaint paint;
2196 paint.setAntiAlias(true);
2197 paint.setTextAlign(SkPaint::kCenter_Align);
2198 canvas->drawImage(image, 0, 0);
2199 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
2200 canvas->drawString(
2201 image->isLazyGenerated() ? "is lazily generated" : "not lazily generated",
2202 image->width() / 2, image->height() * 3 / 4, paint);
2203 };
2204 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2205 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04002206 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
2207 nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05002208 drawImage(image, "image");
2209 canvas->translate(image->width(), 0);
2210 drawImage(bitmapImage, "source");
2211 canvas->translate(-image->width(), image->height());
2212 drawImage(textureImage, "backEndTexture");
2213}
2214##
2215
Cary Clarkac47b882018-01-11 10:35:44 -05002216#SeeAlso isTextureBacked MakeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002217
2218#Method ##
2219
2220# ------------------------------------------------------------------------------
2221
2222#Method sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target,
2223 SkTransferFunctionBehavior premulBehavior) const
Cary Clark4855f782018-02-06 09:41:53 -05002224#In Constructor
2225#Line # creates Image matching Color_Space if possible ##
Cary Clarka560c472017-11-27 10:44:06 -05002226
Cary Clarkac47b882018-01-11 10:35:44 -05002227Creates Image in target Color_Space.
2228Returns nullptr if Image could not be created.
Cary Clarka560c472017-11-27 10:44:06 -05002229
Cary Clarkac47b882018-01-11 10:35:44 -05002230Returns original Image if it is in target Color_Space.
2231Otherwise, converts pixels from Image Color_Space to target Color_Space.
2232If Image colorSpace returns nullptr, Image Color_Space is assumed to be sRGB.
2233
2234SkTransferFunctionBehavior is to be deprecated.
2235
2236Set premulBehavior to SkTransferFunctionBehavior::kRespect to convert Image
2237pixels to a linear space, before converting to destination Color_Type
Cary Clarka560c472017-11-27 10:44:06 -05002238and Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -05002239
Cary Clarkac47b882018-01-11 10:35:44 -05002240Set premulBehavior to SkTransferFunctionBehavior::kIgnore to treat Image
2241pixels as linear, when converting to destination Color_Type
2242and Color_Space, ignoring pixel encoding.
Cary Clarka560c472017-11-27 10:44:06 -05002243
Cary Clarkac47b882018-01-11 10:35:44 -05002244#Param target Color_Space describing color range of returned Image ##
2245#Param premulBehavior one of: SkTransferFunctionBehavior::kRespect,
2246 SkTransferFunctionBehavior::kIgnore
Cary Clarka560c472017-11-27 10:44:06 -05002247##
2248
Cary Clarkac47b882018-01-11 10:35:44 -05002249#Return created Image in target Color_Space ##
2250
2251#Example
2252#Image 5
2253#Set sRGB
2254 sk_sp<SkColorSpace> normalColorSpace = SkColorSpace::MakeRGB(
2255 SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kSRGB_Gamut);
2256 sk_sp<SkColorSpace> wackyColorSpace = normalColorSpace->makeColorSpin();
2257 for (auto colorSpace : { normalColorSpace, wackyColorSpace } ) {
2258 for (auto transfer : { SkTransferFunctionBehavior::kRespect,
2259 SkTransferFunctionBehavior::kIgnore } ) {
2260 sk_sp<SkImage> colorSpaced = image->makeColorSpace(colorSpace, transfer);
2261 canvas->drawImage(colorSpaced, 0, 0);
2262 canvas->translate(128, 0);
2263 }
2264 canvas->translate(-256, 128);
2265 }
2266##
2267
2268#SeeAlso MakeFromPixture MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05002269
2270#Method ##
2271
2272#Class SkImage ##
2273
2274#Topic Image ##