blob: 32ddbe33970d629957d18dd5f285d9800105a552 [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,
Brian Osman584b5012018-04-13 15:48:26 -0400468 SkColorSpace* dstColorSpace,
469 bool limitToMaxTextureSize = false)
Cary Clark4855f782018-02-06 09:41:53 -0500470#In Constructor
471#Line # creates Image from encoded data, and uploads to GPU ##
Cary Clarka560c472017-11-27 10:44:06 -0500472
Cary Clark3cd22cc2017-12-01 11:49:58 -0500473Creates Image from encoded data. Image is uploaded to GPU back-end using context.
474
475Created Image is available to other GPU contexts, and is available across thread
476boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
477share resources.
478
479When Image is no longer referenced, context releases texture memory
Cary Clarka560c472017-11-27 10:44:06 -0500480asynchronously.
Cary Clarka560c472017-11-27 10:44:06 -0500481
Cary Clark3cd22cc2017-12-01 11:49:58 -0500482Texture decoded from data is uploaded to match Surface created with
483dstColorSpace. Color_Space of Image is determined by encoded data.
Cary Clarka560c472017-11-27 10:44:06 -0500484
Cary Clark3cd22cc2017-12-01 11:49:58 -0500485Image is returned if format of data is recognized and supported, and if context
486supports moving resources. Recognized formats vary by platform and GPU back-end.
487
Cary Clark61ca7c52018-01-02 11:34:14 -0500488Image is returned using MakeFromEncoded if context is nullptr or does not support
489moving resources between contexts.
490
Cary Clark3cd22cc2017-12-01 11:49:58 -0500491#Param context GPU_Context ##
492#Param data Image to decode ##
493#Param buildMips create Image as Mip_Map if true ##
494#Param dstColorSpace range of colors of matching Surface on GPU ##
Brian Osman584b5012018-04-13 15:48:26 -0400495#Param limitToMaxTextureSize downscale image to GPU maximum texture size, if necessary ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500496
497#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500498
499#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500500#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500501#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500502GrContext* context = canvas->getGrContext();
503sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, 100);
504sk_sp<SkImage> image = SkImage::MakeCrossContextFromEncoded(context,
505 encodedData, false, nullptr);
506canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500507##
508
Cary Clark3cd22cc2017-12-01 11:49:58 -0500509#SeeAlso MakeCrossContextFromPixmap
510
511#Method ##
512
513# ------------------------------------------------------------------------------
514
515#Method static sk_sp<SkImage> MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap,
516 bool buildMips,
Brian Osman584b5012018-04-13 15:48:26 -0400517 SkColorSpace* dstColorSpace,
518 bool limitToMaxTextureSize = false)
Cary Clark4855f782018-02-06 09:41:53 -0500519#In Constructor
520#Line # creates Image from Pixmap, and uploads to GPU ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500521
522Creates Image from pixmap. Image is uploaded to GPU back-end using context.
523
524Created Image is available to other GPU contexts, and is available across thread
525boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
526share resources.
527
528When Image is no longer referenced, context releases texture memory
529asynchronously.
530
531Texture created from pixmap is uploaded to match Surface created with
532dstColorSpace. Color_Space of Image is determined by pixmap.colorSpace().
533
Cary Clark61ca7c52018-01-02 11:34:14 -0500534Image is returned referring to GPU back-end if context is not nullptr,
535format of data is recognized and supported, and if context supports moving
536resources between contexts. Otherwise, pixmap pixel data is copied and Image
537as returned in raster format if possible; nullptr may be returned.
538Recognized GPU formats vary by platform and GPU back-end.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500539
540#Param context GPU_Context ##
541#Param pixmap Image_Info, pixel address, and row bytes ##
542#Param buildMips create Image as Mip_Map if true ##
543#Param dstColorSpace range of colors of matching Surface on GPU ##
Brian Osman584b5012018-04-13 15:48:26 -0400544#Param limitToMaxTextureSize downscale image to GPU maximum texture size, if necessary ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500545
546#Return created Image, or nullptr ##
547
548#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500549#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500550#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500551GrContext* context = canvas->getGrContext();
552SkPixmap pixmap;
553if (source.peekPixels(&pixmap)) {
554 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(context, pixmap,
555 false, nullptr);
556 canvas->drawImage(image, 0, 0);
557}
Cary Clark3cd22cc2017-12-01 11:49:58 -0500558##
559
560#SeeAlso MakeCrossContextFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500561
562#Method ##
563
564# ------------------------------------------------------------------------------
565
566#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
567 const GrBackendTexture& backendTexture,
568 GrSurfaceOrigin surfaceOrigin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500569 SkColorType colorType,
570 SkAlphaType alphaType = kPremul_SkAlphaType,
571 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500572#In Constructor
573#Line # creates Image from GPU_Texture, managed internally ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500574Creates Image from backendTexture associated with context. backendTexture and
575returned Image are managed internally, and are released when no longer needed.
Cary Clarka560c472017-11-27 10:44:06 -0500576
Cary Clark3cd22cc2017-12-01 11:49:58 -0500577Image is returned if format of backendTexture is recognized and supported.
578Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500579
Cary Clark3cd22cc2017-12-01 11:49:58 -0500580#Param context GPU_Context ##
581#Param backendTexture texture residing on GPU ##
582#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500583#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500584##
Cary Clark681287e2018-03-16 11:34:15 -0400585#Param alphaType one of: #list_of_alpha_types#
Cary Clark3cd22cc2017-12-01 11:49:58 -0500586##
Cary Clark61ca7c52018-01-02 11:34:14 -0500587#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500588
589#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500590
591#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500592#Image 5
593#Platform gpu
Cary Clark61ca7c52018-01-02 11:34:14 -0500594 if (!canvas->getGrContext()) {
595 return;
596 }
597 canvas->scale(.5f, .5f);
598 canvas->clear(0x7f3f5f7f);
599 int x = 0, y = 0;
600 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
601 for (auto alpha : { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType } ) {
602 sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(canvas->getGrContext(),
603 backEndTexture, origin,
604 kRGBA_8888_SkColorType, alpha);
605 canvas->drawImage(image, x, y);
606 x += 160;
607 }
608 x -= 160 * 3;
609 y += 256;
610 }
Cary Clarka560c472017-11-27 10:44:06 -0500611##
612
Cary Clark61ca7c52018-01-02 11:34:14 -0500613#SeeAlso MakeFromTexture MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500614
615#Method ##
616
617# ------------------------------------------------------------------------------
618
619#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400620 const GrBackendTexture yuvTextures[3],
Cary Clarka560c472017-11-27 10:44:06 -0500621 GrSurfaceOrigin surfaceOrigin,
622 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500623#In Constructor
624#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500625
Brian Salomon6a426c12018-03-15 12:16:02 -0400626Creates Image from copy of yuvTextures, an array of textures on GPU.
627yuvTextures contain pixels for YUV planes of Image. Returned Image has the dimensions
628yuvTextures[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500629
Cary Clark61ca7c52018-01-02 11:34:14 -0500630#Param context GPU_Context ##
631#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
632 kRec709_SkYUVColorSpace
633##
Brian Salomon6a426c12018-03-15 12:16:02 -0400634#Param yuvTextures array of YUV textures on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500635#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
636#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500637
Cary Clark61ca7c52018-01-02 11:34:14 -0500638#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500639
Cary Clark61ca7c52018-01-02 11:34:14 -0500640# seems too complicated to create an example for this
641#ToDo
642should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500643##
644
Cary Clark61ca7c52018-01-02 11:34:14 -0500645#NoExample
646##
647
648#SeeAlso MakeFromNV12TexturesCopy
649
650#Method ##
651
652# ------------------------------------------------------------------------------
653
Cary Clarka560c472017-11-27 10:44:06 -0500654#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
655 SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400656 const GrBackendTexture nv12Textures[2],
Cary Clarka560c472017-11-27 10:44:06 -0500657 GrSurfaceOrigin surfaceOrigin,
658 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500659#In Constructor
Brian Salomon6a426c12018-03-15 12:16:02 -0400660#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500661
Cary Clark681287e2018-03-16 11:34:15 -0400662Creates Image from copy of nv12Textures, an array of textures on GPU.
Brian Salomon6a426c12018-03-15 12:16:02 -0400663nv12Textures[0] contains pixels for YUV_Component_Y plane.
664nv12Textures[1] contains pixels for YUV_Component_U plane,
Cary Clark61ca7c52018-01-02 11:34:14 -0500665followed by pixels for YUV_Component_V plane.
Cary Clark681287e2018-03-16 11:34:15 -0400666Returned Image has the dimensions nv12Textures[2].
667yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500668
Cary Clark61ca7c52018-01-02 11:34:14 -0500669#Param context GPU_Context ##
670#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
671 kRec709_SkYUVColorSpace
672##
Brian Salomon6a426c12018-03-15 12:16:02 -0400673#Param nv12Textures array of YUV textures on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500674#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
675#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500676
Cary Clark61ca7c52018-01-02 11:34:14 -0500677#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500678
Cary Clark61ca7c52018-01-02 11:34:14 -0500679# seems too complicated to create an example for this
680#ToDo
681should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500682##
683
Cary Clark61ca7c52018-01-02 11:34:14 -0500684#NoExample
685##
686
687#SeeAlso MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500688
689#Method ##
690
691# ------------------------------------------------------------------------------
692
Cary Clark4855f782018-02-06 09:41:53 -0500693# currently uncalled by any test or client ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500694#Bug 7424
Cary Clark61ca7c52018-01-02 11:34:14 -0500695
Cary Clark56356312018-02-08 14:45:18 -0500696#EnumClass BitDepth
Cary Clarka560c472017-11-27 10:44:06 -0500697
698#Code
Cary Clark61ca7c52018-01-02 11:34:14 -0500699 enum class BitDepth {
Cary Clarka560c472017-11-27 10:44:06 -0500700 kU8,
701 kF16,
702 };
703##
704
705#Const kU8 0
Cary Clark61ca7c52018-01-02 11:34:14 -0500706Use 8 bits per Color_ARGB component using unsigned integer format.
Cary Clarka560c472017-11-27 10:44:06 -0500707##
708#Const kF16 1
Cary Clark61ca7c52018-01-02 11:34:14 -0500709Use 16 bits per Color_ARGB component using half-precision floating point format.
Cary Clarka560c472017-11-27 10:44:06 -0500710##
711
Cary Clark61ca7c52018-01-02 11:34:14 -0500712#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500713##
714
Cary Clark61ca7c52018-01-02 11:34:14 -0500715#SeeAlso MakeFromPicture
Cary Clarka560c472017-11-27 10:44:06 -0500716
Cary Clark56356312018-02-08 14:45:18 -0500717#EnumClass ##
Cary Clarka560c472017-11-27 10:44:06 -0500718
719# ------------------------------------------------------------------------------
720
721#Method static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
722 const SkMatrix* matrix, const SkPaint* paint,
723 BitDepth bitDepth,
724 sk_sp<SkColorSpace> colorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500725#In Constructor
726#Line # creates Image from Picture ##
Cary Clarka560c472017-11-27 10:44:06 -0500727
Cary Clark61ca7c52018-01-02 11:34:14 -0500728Creates Image from picture. Returned Image width and height are set by dimensions.
729Image draws picture with matrix and paint, set to bitDepth and colorSpace.
Cary Clarka560c472017-11-27 10:44:06 -0500730
Cary Clark61ca7c52018-01-02 11:34:14 -0500731If matrix is nullptr, draws with identity Matrix. If paint is nullptr, draws
732with default Paint. colorSpace may be nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500733
Cary Clark61ca7c52018-01-02 11:34:14 -0500734#Param picture stream of drawing commands ##
735#Param dimensions width and height ##
736#Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ##
737#Param paint Paint to apply transparency, filtering, and so on; may be nullptr ##
738#Param bitDepth 8 bit integer or 16 bit float: per component ##
739#Param colorSpace range of colors; may be nullptr ##
740
741#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500742
743#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500744 SkPaint paint;
745 SkPictureRecorder recorder;
746 SkCanvas* recordingCanvas = recorder.beginRecording(50, 50);
747 for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) {
748 paint.setColor(color);
749 recordingCanvas->drawRect({10, 10, 30, 40}, paint);
750 recordingCanvas->translate(10, 10);
751 recordingCanvas->scale(1.2f, 1.4f);
752 }
753 sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
754 int x = 0, y = 0;
755 for (auto alpha : { 70, 140, 210 } ) {
756 paint.setAlpha(alpha);
757 auto srgbColorSpace = SkColorSpace::MakeSRGB();
758 sk_sp<SkImage> image = SkImage::MakeFromPicture(playback, {50, 50}, nullptr, &paint,
759 SkImage::BitDepth::kU8, srgbColorSpace);
760 canvas->drawImage(image, x, y);
761 x += 70; y += 70;
762 }
Cary Clarka560c472017-11-27 10:44:06 -0500763##
764
Cary Clark61ca7c52018-01-02 11:34:14 -0500765#SeeAlso SkCanvas::drawPicture
Cary Clarka560c472017-11-27 10:44:06 -0500766
767#Method ##
768
769# ------------------------------------------------------------------------------
770
771#Method static sk_sp<SkImage> MakeFromAHardwareBuffer(AHardwareBuffer* hardwareBuffer,
772 SkAlphaType alphaType = kPremul_SkAlphaType,
773 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500774#In Constructor
775#Line # creates Image from Android hardware buffer ##
Cary Clarka560c472017-11-27 10:44:06 -0500776
Cary Clark4855f782018-02-06 09:41:53 -0500777#Bug 7447
Cary Clarka560c472017-11-27 10:44:06 -0500778
Cary Clark61ca7c52018-01-02 11:34:14 -0500779Creates Image from Android hardware buffer.
780Returned Image takes a reference on the buffer.
Cary Clarka560c472017-11-27 10:44:06 -0500781
Cary Clark61ca7c52018-01-02 11:34:14 -0500782Only available on Android, when __ANDROID_API__ is defined to be 26 or greater.
Cary Clarka560c472017-11-27 10:44:06 -0500783
Cary Clark61ca7c52018-01-02 11:34:14 -0500784#Param hardwareBuffer AHardwareBuffer Android hardware buffer ##
Cary Clark681287e2018-03-16 11:34:15 -0400785#Param alphaType one of: #list_of_alpha_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500786##
787#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500788
Cary Clark61ca7c52018-01-02 11:34:14 -0500789#Return created Image, or nullptr ##
790
791#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500792##
793
Cary Clark61ca7c52018-01-02 11:34:14 -0500794#SeeAlso MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -0500795
796#Method ##
797
798# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -0500799#Subtopic Property
800#Populate
801#Line # values and attributes ##
802##
Cary Clarka560c472017-11-27 10:44:06 -0500803
804#Method int width() const
Cary Clark4855f782018-02-06 09:41:53 -0500805#In Property
806#Line # returns pixel column count ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500807Returns pixel count in each row.
808
809#Return pixel width in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500810
811#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500812#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500813#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500814 canvas->translate(10, 10);
815 canvas->drawImage(image, 0, 0);
816 canvas->translate(0, image->height());
817 SkPaint paint;
818 paint.setTextAlign(SkPaint::kCenter_Align);
819 canvas->drawLine(0, 10, image->width(), 10, paint);
820 canvas->drawString("width", image->width() / 2, 25, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500821##
822
Cary Clark61ca7c52018-01-02 11:34:14 -0500823#SeeAlso dimensions() height()
Cary Clarka560c472017-11-27 10:44:06 -0500824
825#Method ##
826
827# ------------------------------------------------------------------------------
828
829#Method int height() const
Cary Clark4855f782018-02-06 09:41:53 -0500830#In Property
831#Line # returns pixel row count ##
Cary Clark2f466242017-12-11 16:03:17 -0500832Returns pixel row count.
833
Cary Clark61ca7c52018-01-02 11:34:14 -0500834#Return pixel height in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500835
836#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500837#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500838#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500839 canvas->translate(10, 10);
840 canvas->drawImage(image, 0, 0);
841 canvas->translate(image->width(), 0);
842 SkPaint paint;
843 paint.setTextAlign(SkPaint::kCenter_Align);
844 paint.setVerticalText(true);
845 canvas->drawLine(10, 0, 10, image->height(), paint);
Cary Clarkac47b882018-01-11 10:35:44 -0500846 canvas->drawString("height", 25, image->height() / 2, paint);
847##
Cary Clarka560c472017-11-27 10:44:06 -0500848
Cary Clark61ca7c52018-01-02 11:34:14 -0500849#SeeAlso dimensions() width()
Cary Clarka560c472017-11-27 10:44:06 -0500850
851#Method ##
852
853# ------------------------------------------------------------------------------
854
855#Method SkISize dimensions() const
Cary Clark4855f782018-02-06 09:41:53 -0500856#In Property
857#Line # returns width() and height() ##
Cary Clark681287e2018-03-16 11:34:15 -0400858
Cary Clark2f466242017-12-11 16:03:17 -0500859Returns ISize { width(), height() }.
860
861#Return integral size of width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -0500862
863#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500864#Image 4
865 SkISize dimensions = image->dimensions();
866 SkIRect bounds = image->bounds();
867 SkIRect dimensionsAsBounds = SkIRect::MakeSize(dimensions);
868 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clark681287e2018-03-16 11:34:15 -0400869#StdOut
870dimensionsAsBounds == bounds
871##
Cary Clarka560c472017-11-27 10:44:06 -0500872##
873
Cary Clark61ca7c52018-01-02 11:34:14 -0500874#SeeAlso height() width() bounds()
Cary Clarka560c472017-11-27 10:44:06 -0500875
876#Method ##
877
878# ------------------------------------------------------------------------------
879
880#Method SkIRect bounds() const
Cary Clark4855f782018-02-06 09:41:53 -0500881#In Property
882#Line # returns width() and height() as Rectangle ##
Cary Clark2f466242017-12-11 16:03:17 -0500883Returns IRect { 0, 0, width(), height() }.
884
885#Return integral rectangle from origin to width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -0500886
887#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500888#Height 128
889#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -0500890 SkIRect bounds = image->bounds();
Cary Clarkac47b882018-01-11 10:35:44 -0500891 for (int x : { 0, bounds.width() } ) {
892 for (int y : { 0, bounds.height() } ) {
Cary Clark61ca7c52018-01-02 11:34:14 -0500893 canvas->drawImage(image, x, y);
894 }
895 }
Cary Clarka560c472017-11-27 10:44:06 -0500896##
897
Cary Clark61ca7c52018-01-02 11:34:14 -0500898#SeeAlso dimensions()
Cary Clarka560c472017-11-27 10:44:06 -0500899
900#Method ##
901
902# ------------------------------------------------------------------------------
903
904#Method uint32_t uniqueID() const
Cary Clark4855f782018-02-06 09:41:53 -0500905#In Property
906#Line # identifier for Image ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500907Returns value unique to image. Image contents cannot change after Image is
908created. Any operation to create a new Image will receive generate a new
909unique number.
910
911#Return unique identifier ##
Cary Clarka560c472017-11-27 10:44:06 -0500912
913#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500914#Image 5
915#Height 156
916 sk_sp<SkImage> subset = image->makeSubset({10, 20, 90, 100});
917 canvas->drawImage(image, 0, 0);
918 canvas->drawImage(subset, 128, 0);
919 SkPaint paint;
920 SkString s;
921 s.printf("original id: %d", image->uniqueID());
922 canvas->drawString(s, 20, image->height() + 20, paint);
923 s.printf("subset id: %d", subset->uniqueID());
924 canvas->drawString(s, 148, subset->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500925##
926
Cary Clark61ca7c52018-01-02 11:34:14 -0500927#SeeAlso isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -0500928
929#Method ##
930
931# ------------------------------------------------------------------------------
932
933#Method SkAlphaType alphaType() const
Cary Clark4855f782018-02-06 09:41:53 -0500934#In Property
935#Line # returns Alpha_Type ##
Cary Clark681287e2018-03-16 11:34:15 -0400936Returns Alpha_Type, one of: #list_of_alpha_types#.
Cary Clark61ca7c52018-01-02 11:34:14 -0500937
938Alpha_Type returned was a parameter to an Image constructor,
939or was parsed from encoded data.
940
941#Return Alpha_Type in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500942
943#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500944#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500945#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500946 const char* alphaTypeStr[] = { "Unknown", "Opaque", "Premul", "Unpremul" };
947 SkAlphaType alphaType = image->alphaType();
Cary Clarkac47b882018-01-11 10:35:44 -0500948 canvas->drawImage(image, 16, 0);
Cary Clark61ca7c52018-01-02 11:34:14 -0500949 SkPaint paint;
950 canvas->drawString(alphaTypeStr[(int) alphaType], 20, image->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500951##
952
Cary Clark61ca7c52018-01-02 11:34:14 -0500953#SeeAlso SkImageInfo::alphaType
Cary Clarka560c472017-11-27 10:44:06 -0500954
955#Method ##
956
957# ------------------------------------------------------------------------------
958
Greg Daniel56008aa2018-03-14 15:33:42 -0400959#Method SkColorType colorType() const
960#In Property
961#Line # returns Color_Type ##
962
963Returns Color_Type if known; otherwise, returns kUnknown_SkColorType.
964
965#Return Color_Type of Image ##
966
967#Example
968// incomplete
969##
970
971#SeeAlso SkImageInfo::colorType
972
973#Method ##
974
975# ------------------------------------------------------------------------------
976
Cary Clarka560c472017-11-27 10:44:06 -0500977#Method SkColorSpace* colorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -0500978#In Property
979#Line # returns Color_Space ##
Cary Clark2f466242017-12-11 16:03:17 -0500980Returns Color_Space, the range of colors, associated with Image. The
981reference count of Color_Space is unchanged. The returned Color_Space is
982immutable.
Cary Clarka560c472017-11-27 10:44:06 -0500983
Cary Clark61dfc3a2018-01-03 08:37:53 -0500984Color_Space returned was passed to an Image constructor,
985or was parsed from encoded data. Color_Space returned may be ignored when Image
986is drawn, depending on the capabilities of the Surface receiving the drawing.
Cary Clark2f466242017-12-11 16:03:17 -0500987
988#Return Color_Space in Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500989
990#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -0500991#Image 3
992#Set sRGB
993 SkPixmap pixmap;
994 source.peekPixels(&pixmap);
995 canvas->scale(.25f, .25f);
996 int y = 0;
997 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
998 SkColorSpace::kSRGB_RenderTargetGamma } ) {
999 int x = 0;
1000 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1001 for (int index = 0; index < 2; ++index) {
1002 pixmap.setColorSpace(colorSpace);
1003 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1004 canvas->drawImage(image, x, y);
1005 colorSpace = image->colorSpace()->makeColorSpin();
1006 x += 512;
1007 }
1008 y += 512;
1009 }
Cary Clarka560c472017-11-27 10:44:06 -05001010##
1011
Cary Clark61dfc3a2018-01-03 08:37:53 -05001012#SeeAlso refColorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001013
1014#Method ##
1015
1016# ------------------------------------------------------------------------------
1017
1018#Method sk_sp<SkColorSpace> refColorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001019#In Property
1020#Line # returns Image_Info Color_Space ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001021Returns a smart pointer to Color_Space, the range of colors, associated with
1022Image. The smart pointer tracks the number of objects sharing this
1023SkColorSpace reference so the memory is released when the owners destruct.
1024
1025The returned SkColorSpace is immutable.
1026
1027Color_Space returned was passed to an Image constructor,
1028or was parsed from encoded data. Color_Space returned may be ignored when Image
1029is drawn, depending on the capabilities of the Surface receiving the drawing.
1030
1031#Return Color_Space in Image, or nullptr, wrapped in a smart pointer ##
Cary Clarka560c472017-11-27 10:44:06 -05001032
1033#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001034#Image 3
1035#Set sRGB
1036 SkPixmap pixmap;
1037 source.peekPixels(&pixmap);
1038 canvas->scale(.25f, .25f);
1039 int y = 0;
1040 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1041 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1042 int x = 0;
1043 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1044 for (int index = 0; index < 2; ++index) {
1045 pixmap.setColorSpace(colorSpace);
1046 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1047 canvas->drawImage(image, x, y);
1048 colorSpace = image->refColorSpace()->makeColorSpin();
1049 x += 512;
1050 }
1051 y += 512;
1052 }
Cary Clarka560c472017-11-27 10:44:06 -05001053##
1054
Cary Clark61dfc3a2018-01-03 08:37:53 -05001055#SeeAlso colorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001056
1057#Method ##
1058
1059# ------------------------------------------------------------------------------
1060
1061#Method bool isAlphaOnly() const
Cary Clark4855f782018-02-06 09:41:53 -05001062#In Property
1063#Line # returns if pixels represent a transparency mask ##
Cary Clark2f466242017-12-11 16:03:17 -05001064Returns true if Image pixels represent transparency only. If true, each pixel
1065is packed in 8 bits as defined by kAlpha_8_SkColorType.
Cary Clarka560c472017-11-27 10:44:06 -05001066
Cary Clark2f466242017-12-11 16:03:17 -05001067#Return true if pixels represent a transparency mask ##
Cary Clarka560c472017-11-27 10:44:06 -05001068
1069#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001070 uint8_t pmColors = 0;
1071 sk_sp<SkImage> image = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1), &pmColors, 1});
1072 SkDebugf("alphaOnly = %s\n", image->isAlphaOnly() ? "true" : "false");
1073#StdOut
1074alphaOnly = true
1075##
Cary Clarka560c472017-11-27 10:44:06 -05001076##
1077
Cary Clark61dfc3a2018-01-03 08:37:53 -05001078#SeeAlso alphaType isOpaque
Cary Clarka560c472017-11-27 10:44:06 -05001079
1080#Method ##
1081
1082# ------------------------------------------------------------------------------
1083
1084#Method bool isOpaque() const
Cary Clark4855f782018-02-06 09:41:53 -05001085#In Property
1086#Line # returns if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001087Returns true if pixels ignore their Alpha value and are treated as fully opaque.
Cary Clark2f466242017-12-11 16:03:17 -05001088
1089#Return true if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clarka560c472017-11-27 10:44:06 -05001090
1091#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001092 auto check_isopaque = [](const SkImageInfo& imageInfo) -> void {
1093 auto surface(SkSurface::MakeRaster(imageInfo));
1094 auto image(surface->makeImageSnapshot());
1095 SkDebugf("isOpaque = %s\n", image->isOpaque() ? "true" : "false");
1096 };
1097
1098 check_isopaque(SkImageInfo::MakeN32Premul(5, 5));
1099 check_isopaque(SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType));
1100#StdOut
1101isOpaque = false
1102isOpaque = true
1103##
Cary Clarka560c472017-11-27 10:44:06 -05001104##
1105
Cary Clark61dfc3a2018-01-03 08:37:53 -05001106#SeeAlso alphaType isAlphaOnly
Cary Clarka560c472017-11-27 10:44:06 -05001107
1108#Method ##
1109
1110# ------------------------------------------------------------------------------
1111
1112#Method sk_sp<SkShader> makeShader(SkShader::TileMode tileMode1, SkShader::TileMode tileMode2,
1113 const SkMatrix* localMatrix = nullptr) const
Cary Clark4855f782018-02-06 09:41:53 -05001114#In Constructor
1115#Line # creates Shader, Paint element that can tile Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001116
Cary Clark61dfc3a2018-01-03 08:37:53 -05001117Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1118SkShader::TileMode rules to fill drawn area outside Image. localMatrix permits
1119transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001120
Cary Clark61dfc3a2018-01-03 08:37:53 -05001121#Param tileMode1 tiling in x, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1122 SkShader::kMirror_TileMode
1123##
1124#Param tileMode2 tiling in y, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1125 SkShader::kMirror_TileMode
1126##
1127#Param localMatrix Image transformation, or nullptr ##
1128
1129#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001130
1131#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001132#Image 4
1133SkMatrix matrix;
1134matrix.setRotate(45);
1135SkPaint paint;
1136paint.setShader(image->makeShader(SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode,
1137 &matrix));
1138canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001139##
1140
Cary Clark61dfc3a2018-01-03 08:37:53 -05001141#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001142
1143#Method ##
1144
1145# ------------------------------------------------------------------------------
1146
1147#Method sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const
1148
Cary Clark61dfc3a2018-01-03 08:37:53 -05001149Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1150SkShader::kClamp_TileMode to fill drawn area outside Image. localMatrix permits
1151transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001152
Cary Clark61dfc3a2018-01-03 08:37:53 -05001153#Param localMatrix Image transformation, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001154
Cary Clark61dfc3a2018-01-03 08:37:53 -05001155#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001156
1157#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001158#Image 5
1159SkMatrix matrix;
1160matrix.setRotate(45);
1161matrix.postTranslate(125, 30);
1162SkPaint paint;
1163paint.setShader(image->makeShader(&matrix));
1164canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001165##
1166
Cary Clarkf5404bb2018-01-05 12:10:09 -05001167#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001168
1169#Method ##
1170
1171# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001172#Subtopic Pixels
1173#Populate
1174#Line # read and write pixel values ##
1175##
Cary Clarka560c472017-11-27 10:44:06 -05001176
1177#Method bool peekPixels(SkPixmap* pixmap) const
Cary Clark78de7512018-02-07 07:27:09 -05001178#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001179#Line # returns Pixmap if possible ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001180Copies Image pixel address, row bytes, and Image_Info to pixmap, if address
1181is available, and returns true. If pixel address is not available, return
1182false and leave pixmap unchanged.
Cary Clarka560c472017-11-27 10:44:06 -05001183
Cary Clarkf5404bb2018-01-05 12:10:09 -05001184#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
Cary Clarka560c472017-11-27 10:44:06 -05001185
Cary Clarkf5404bb2018-01-05 12:10:09 -05001186#Return true if Image has direct access to pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001187
1188#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001189 SkBitmap bitmap;
1190 bitmap.allocPixels(SkImageInfo::MakeN32Premul(12, 11));
1191 SkCanvas offscreen(bitmap);
1192 offscreen.clear(SK_ColorWHITE);
1193 SkPaint paint;
1194 offscreen.drawString("%", 1, 10, paint);
1195 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
1196 SkPixmap pixmap;
1197 if (image->peekPixels(&pixmap)) {
1198 const SkPMColor* pixels = pixmap.addr32();
1199 SkPMColor pmWhite = pixels[0];
1200 for (int y = 0; y < image->height(); ++y) {
1201 for (int x = 0; x < image->width(); ++x) {
1202 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
1203 }
1204 SkDebugf("\n");
1205 }
1206 }
1207#StdOut
1208------------
1209--xx----x---
1210-x--x--x----
1211-x--x--x----
1212-x--x-x-----
1213--xx-xx-xx--
1214-----x-x--x-
1215----x--x--x-
1216----x--x--x-
1217---x----xx--
1218------------
1219##
Cary Clarka560c472017-11-27 10:44:06 -05001220##
1221
Cary Clarkf5404bb2018-01-05 12:10:09 -05001222#SeeAlso readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001223
1224#Method ##
1225
1226# ------------------------------------------------------------------------------
1227
1228#Method GrTexture* getTexture() const
Cary Clark2f466242017-12-11 16:03:17 -05001229#Deprecated
Cary Clarka560c472017-11-27 10:44:06 -05001230#Method ##
1231
1232# ------------------------------------------------------------------------------
1233
1234#Method bool isTextureBacked() const
Cary Clark78de7512018-02-07 07:27:09 -05001235#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001236#Line # returns if Image was created from GPU_Texture ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001237Returns true the contents of Image was created on or uploaded to GPU memory,
1238and is available as a GPU_Texture.
Cary Clarka560c472017-11-27 10:44:06 -05001239
Cary Clarkf5404bb2018-01-05 12:10:09 -05001240#Return true if Image is a GPU_Texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001241
1242#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001243#Image 5
1244#Platform gpu
1245auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1246 if (nullptr == image) {
1247 return;
1248 }
1249 SkPaint paint;
1250 paint.setAntiAlias(true);
1251 paint.setTextAlign(SkPaint::kCenter_Align);
1252 canvas->drawImage(image, 0, 0);
1253 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1254 canvas->drawString(image->isTextureBacked() ? "is GPU texture" : "not GPU texture",
1255 image->width() / 2, image->height() * 3 / 4, paint);
1256};
1257sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1258sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001259 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1260 nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05001261drawImage(image, "image");
1262canvas->translate(image->width(), 0);
1263drawImage(bitmapImage, "source");
1264canvas->translate(-image->width(), image->height());
1265drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001266##
1267
Cary Clarkf5404bb2018-01-05 12:10:09 -05001268#SeeAlso MakeFromTexture isValid
Cary Clarka560c472017-11-27 10:44:06 -05001269
1270#Method ##
1271
1272# ------------------------------------------------------------------------------
1273
1274#Method bool isValid(GrContext* context) const
Cary Clark4855f782018-02-06 09:41:53 -05001275#In Property
1276#Line # returns if Image can draw to Raster_Surface or GPU_Context ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001277Returns true if Image can be drawn on either Raster_Surface or GPU_Surface.
1278If context is nullptr, tests if Image draws on Raster_Surface;
1279otherwise, tests if Image draws on GPU_Surface associated with context.
Cary Clarka560c472017-11-27 10:44:06 -05001280
Cary Clarkf5404bb2018-01-05 12:10:09 -05001281Image backed by GPU_Texture may become invalid if associated GrContext is
1282invalid. Lazy_Image may be invalid and may not draw to Raster_Surface or
1283GPU_Surface or both.
Cary Clarka560c472017-11-27 10:44:06 -05001284
Cary Clark61ca7c52018-01-02 11:34:14 -05001285#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05001286
Cary Clarkf5404bb2018-01-05 12:10:09 -05001287#Return true if Image can be drawn ##
Cary Clarka560c472017-11-27 10:44:06 -05001288
1289#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001290#Image 5
1291#Platform gpu
1292auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1293 if (nullptr == image) {
1294 return;
1295 }
1296 SkPaint paint;
1297 paint.setAntiAlias(true);
1298 paint.setTextAlign(SkPaint::kCenter_Align);
1299 canvas->drawImage(image, 0, 0);
1300 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1301 if (canvas->getGrContext()) {
1302 canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" :
1303 "not valid on GPU", image->width() / 2, image->height() * 5 / 8, paint);
1304 }
1305 canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" :
1306 "not valid on CPU", image->width() / 2, image->height() * 7 / 8, paint);
1307};
1308sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1309sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001310 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1311 nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05001312drawImage(image, "image");
1313canvas->translate(image->width(), 0);
1314drawImage(bitmapImage, "source");
1315canvas->translate(-image->width(), image->height());
1316drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001317##
1318
Cary Clarkf5404bb2018-01-05 12:10:09 -05001319#SeeAlso isTextureBacked isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001320
1321#Method ##
1322
1323# ------------------------------------------------------------------------------
1324
1325#Method GrBackendObject getTextureHandle(bool flushPendingGrContextIO,
1326 GrSurfaceOrigin* origin = nullptr) const
Cary Clark78de7512018-02-07 07:27:09 -05001327#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001328#Line # returns GPU reference to Image as texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001329
Cary Clark2f466242017-12-11 16:03:17 -05001330Retrieves the back-end API handle of texture. If flushPendingGrContextIO is true,
1331complete deferred I/O operations.
Cary Clarka560c472017-11-27 10:44:06 -05001332
Robert Phillipsc5509952018-04-04 15:54:55 -04001333If origin is not nullptr, copies location of content drawn into Image.
Cary Clarka560c472017-11-27 10:44:06 -05001334
Cary Clark2f466242017-12-11 16:03:17 -05001335#Param flushPendingGrContextIO flag to flush outstanding requests ##
1336#Param origin storage for one of: kTopLeft_GrSurfaceOrigin,
1337 kBottomLeft_GrSurfaceOrigin; or nullptr
1338##
1339
Cary Clarkac47b882018-01-11 10:35:44 -05001340#Return back-end API texture handle, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001341
1342#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001343#Image 4
Cary Clark2f466242017-12-11 16:03:17 -05001344#Platform gpu
1345GrContext* context = canvas->getGrContext();
1346if (!context) {
1347 return;
1348}
1349SkPaint paint;
1350paint.setAntiAlias(true);
1351SkString str;
Cary Clarkac47b882018-01-11 10:35:44 -05001352int y = -10;
Cary Clark2f466242017-12-11 16:03:17 -05001353for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin } ) {
1354 sk_sp<SkImage> srcImage(SkImage::MakeFromTexture(context,
Cary Clark471b6fe2018-03-21 08:52:41 -04001355 backEndTexture, origin, kN32_SkColorType, kPremul_SkAlphaType, nullptr));
Cary Clark2f466242017-12-11 16:03:17 -05001356 GrSurfaceOrigin readBackOrigin;
1357 GrBackendObject readBackHandle = srcImage->getTextureHandle(false, &readBackOrigin);
Cary Clark681287e2018-03-16 11:34:15 -04001358 str.printf("readBackHandle: 0x%lx", readBackHandle);
Cary Clarkac47b882018-01-11 10:35:44 -05001359 canvas->drawString(str, 5, y += 30, paint);
1360 canvas->drawImage(srcImage, 80, y += 10);
Cary Clark2f466242017-12-11 16:03:17 -05001361 str.printf("origin: k%s_GrSurfaceOrigin", readBackOrigin ? "BottomLeft" : "TopLeft");
Cary Clarkac47b882018-01-11 10:35:44 -05001362 canvas->drawString(str, 5, y += srcImage->height() + 10, paint);
Cary Clark2f466242017-12-11 16:03:17 -05001363}
Cary Clarka560c472017-11-27 10:44:06 -05001364##
1365
Cary Clarkac47b882018-01-11 10:35:44 -05001366#Example
1367#Image 5
1368#Platform gpu
1369 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1370 if (nullptr == image) {
1371 return;
1372 }
1373 SkPaint paint;
1374 paint.setAntiAlias(true);
1375 paint.setTextAlign(SkPaint::kCenter_Align);
1376 canvas->drawImage(image, 0, image->height() / 4);
1377 canvas->drawString(label, image->width() / 2, image->height() / 8, paint);
1378 GrSurfaceOrigin readBackOrigin;
1379 GrBackendObject readBackHandle = image->getTextureHandle(false, &readBackOrigin);
1380 canvas->drawString(readBackHandle ? "has readBackHandle" : "no readBackHandle",
1381 image->width() / 2, image->height() * 11 / 8, paint);
1382 };
1383 drawImage(image, "image");
1384 canvas->translate(image->width(), 0);
1385 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001386 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1387 nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001388 drawImage(textureImage, "backEndTexture");
1389##
1390
1391#SeeAlso MakeFromTexture isTextureBacked
Cary Clarka560c472017-11-27 10:44:06 -05001392
1393#Method ##
1394
1395# ------------------------------------------------------------------------------
1396
Robert Phillipsc5509952018-04-04 15:54:55 -04001397#Method GrBackendTexture getBackendTexture(bool flushPendingGrContextIO,
1398 GrSurfaceOrigin* origin = nullptr) const
1399#In Property
1400#Line # returns GPU reference to Image as texture ##
1401
Cary Clarkba75aee2018-04-05 08:18:41 -04001402Retrieves the backend texture. If Image has no backend texture, an invalid
1403object is returned. Call GrBackendTexture::isValid to determine if the result
1404is valid.
1405
1406If flushPendingGrContextIO is true, completes deferred I/O operations.
Robert Phillipsc5509952018-04-04 15:54:55 -04001407
1408If origin in not nullptr, copies location of content drawn into Image.
1409
1410#Param flushPendingGrContextIO flag to flush outstanding requests ##
1411#Param origin storage for one of: kTopLeft_GrSurfaceOrigin,
1412 kBottomLeft_GrSurfaceOrigin; or nullptr
1413##
1414
Cary Clarkba75aee2018-04-05 08:18:41 -04001415#Return back-end API texture handle; invalid on failure ##
Robert Phillipsc5509952018-04-04 15:54:55 -04001416
Cary Clarkba75aee2018-04-05 08:18:41 -04001417#Example
1418#Image 3
1419#Platform gpu
Brian Osman584b5012018-04-13 15:48:26 -04001420 GrContext* grContext = canvas->getGrContext();
1421 if (!grContext) {
1422 canvas->drawString("GPU only!", 20, 40, SkPaint());
1423 return;
1424 }
1425 sk_sp<SkImage> imageFromBackend = SkImage::MakeFromAdoptedTexture(grContext, backEndTexture,
1426 kBottomLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1427 GrBackendTexture textureFromImage = imageFromBackend->getBackendTexture(false);
1428 if (!textureFromImage.isValid()) {
1429 return;
1430 }
1431 sk_sp<SkImage> imageFromTexture = SkImage::MakeFromAdoptedTexture(grContext, textureFromImage,
1432 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1433 canvas->drawImage(imageFromTexture, 0, 0);
Cary Clarkba75aee2018-04-05 08:18:41 -04001434 canvas->drawImage(imageFromBackend, 128, 128);
Robert Phillipsc5509952018-04-04 15:54:55 -04001435##
1436
1437#SeeAlso MakeFromTexture isTextureBacked
1438
1439#Method ##
1440
1441# ------------------------------------------------------------------------------
1442
Cary Clarka560c472017-11-27 10:44:06 -05001443#Enum CachingHint
1444
1445#Code
1446 enum CachingHint {
1447 kAllow_CachingHint,
1448 kDisallow_CachingHint,
1449 };
1450##
1451
Cary Clarkac47b882018-01-11 10:35:44 -05001452CachingHint selects whether Skia may internally cache Bitmaps generated by
1453decoding Image, or by copying Image from GPU to CPU. The default behavior
1454allows caching Bitmaps.
1455
1456Choose kDisallow_CachingHint if Image pixels are to be used only once, or
1457if Image pixels reside in a cache outside of Skia, or to reduce memory pressure.
1458
1459Choosing kAllow_CachingHint does not ensure that pixels will be cached.
1460Image pixels may not be cached if memory requirements are too large or
1461pixels are not accessible.
Cary Clarka560c472017-11-27 10:44:06 -05001462
1463#Const kAllow_CachingHint 0
Cary Clarkac47b882018-01-11 10:35:44 -05001464Allows Skia to internally cache decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001465##
1466#Const kDisallow_CachingHint 1
Cary Clarkac47b882018-01-11 10:35:44 -05001467Disallows Skia from internally caching decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001468##
1469
Cary Clarkac47b882018-01-11 10:35:44 -05001470#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001471##
1472
Cary Clarkac47b882018-01-11 10:35:44 -05001473#SeeAlso readPixels scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001474
1475#Enum ##
1476
1477# ------------------------------------------------------------------------------
1478
1479#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1480 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001481#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001482#Line # copies and converts pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001483
Cary Clarkac47b882018-01-11 10:35:44 -05001484Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY),
1485and does not exceed Image (width(), height()).
1486
1487dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
1488destination. dstRowBytes specifics the gap from one destination row to the next.
1489Returns true if pixels are copied. Returns false if:
1490#List
1491# dstInfo.addr() equals nullptr ##
1492# dstRowBytes is less than dstInfo.minRowBytes ##
1493# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001494##
1495
Cary Clarkac47b882018-01-11 10:35:44 -05001496Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1497kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1498If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace must match.
1499If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType must
1500match. If Image Color_Space is nullptr, dstInfo.colorSpace must match. Returns
1501false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001502
Cary Clarkac47b882018-01-11 10:35:44 -05001503srcX and srcY may be negative to copy only top or left of source. Returns
1504false if width() or height() is zero or negative.
1505Returns false if
1506#Formula
1507abs(srcX) >= Image width()
1508##
1509, or if
1510#Formula
1511abs(srcY) >= Image height()
1512##
1513.
Cary Clarka560c472017-11-27 10:44:06 -05001514
Cary Clarkac47b882018-01-11 10:35:44 -05001515If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1516If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1517
1518#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1519#Param dstPixels destination pixel storage ##
1520#Param dstRowBytes destination row length ##
1521#Param srcX column index whose absolute value is less than width() ##
1522#Param srcY row index whose absolute value is less than height() ##
1523#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1524
1525#Return true if pixels are copied to dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001526
1527#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001528#Image 3
1529 canvas->scale(.5f, .5f);
1530 const int width = 32;
1531 const int height = 32;
1532 std::vector<int32_t> dstPixels;
1533 dstPixels.resize(height * width * 4);
1534 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1535 for (int y = 0; y < 512; y += height ) {
1536 for (int x = 0; x < 512; x += width ) {
1537 if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) {
1538 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
1539 SkBitmap bitmap;
1540 bitmap.installPixels(dstPixmap);
1541 canvas->drawBitmap(bitmap, 0, 0);
1542 }
1543 canvas->translate(48, 0);
1544 }
1545 canvas->translate(-16 * 48, 48);
1546 }
Cary Clarka560c472017-11-27 10:44:06 -05001547##
1548
Cary Clarkac47b882018-01-11 10:35:44 -05001549#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001550
1551#Method ##
1552
1553# ------------------------------------------------------------------------------
1554
1555#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY,
1556 CachingHint cachingHint = kAllow_CachingHint) const
1557
Cary Clarkac47b882018-01-11 10:35:44 -05001558Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and
1559does not exceed Image (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001560
Cary Clarkac47b882018-01-11 10:35:44 -05001561dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
1562and row bytes of destination. dst.rowBytes specifics the gap from one destination
1563row to the next. Returns true if pixels are copied. Returns false if:
1564#List
1565# dst pixel storage equals nullptr ##
1566# dst.rowBytes is less than SkImageInfo::minRowBytes ##
1567# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001568##
1569
Cary Clarkac47b882018-01-11 10:35:44 -05001570Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1571kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1572If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1573If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1574match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1575false if pixel conversion is not possible.
1576
1577srcX and srcY may be negative to copy only top or left of source. Returns
1578false if width() or height() is zero or negative.
1579Returns false if
1580#Formula
1581abs(srcX) >= Image width()
1582##
1583, or if
1584#Formula
1585abs(srcY) >= Image height()
1586##
1587.
1588
1589If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1590If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1591
1592#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1593#Param srcX column index whose absolute value is less than width() ##
1594#Param srcY row index whose absolute value is less than height() ##
1595#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1596
1597#Return true if pixels are copied to dst ##
1598
1599#Example
1600#Image 3
1601 std::vector<int32_t> srcPixels;
1602 int rowBytes = image->width() * 4;
1603 int quarterWidth = image->width() / 4;
1604 int quarterHeight = image->height() / 4;
1605 srcPixels.resize(image->height() * rowBytes);
1606 for (int y = 0; y < 4; ++y) {
1607 for (int x = 0; x < 4; ++x) {
1608 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1609 &srcPixels.front() + x * image->height() * quarterWidth +
1610 y * quarterWidth, rowBytes);
1611 image->readPixels(pixmap, x * quarterWidth, y * quarterHeight);
1612 }
1613 }
1614 canvas->scale(.5f, .5f);
1615 SkBitmap bitmap;
1616 bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()),
1617 &srcPixels.front(), rowBytes);
1618 canvas->drawBitmap(bitmap, 0, 0);
1619##
1620
1621#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001622
1623#Method ##
1624
1625# ------------------------------------------------------------------------------
1626
1627#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality,
1628 CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001629#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001630#Line # scales and converts one Image to another ##
Cary Clarka560c472017-11-27 10:44:06 -05001631
Cary Clarkac47b882018-01-11 10:35:44 -05001632Copies Image to dst, scaling pixels to fit dst.width() and dst.height(), and
1633converting pixels to match dst.colorType and dst.alphaType. Returns true if
1634pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1635less than dst SkImageInfo::minRowBytes.
Cary Clarka560c472017-11-27 10:44:06 -05001636
Cary Clarkac47b882018-01-11 10:35:44 -05001637Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1638kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1639If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1640If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1641match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1642false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001643
Cary Clarkac47b882018-01-11 10:35:44 -05001644Scales the image, with filterQuality, to match dst.width() and dst.height().
1645filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1646Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1647Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1648Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1649kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1650
1651If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1652If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1653
1654#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1655#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1656 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1657##
1658#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1659
1660#Return true if pixels are scaled to fit dst ##
Cary Clarka560c472017-11-27 10:44:06 -05001661
1662#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001663#Image 3
1664#Height 128
1665 std::vector<int32_t> srcPixels;
1666 int quarterWidth = image->width() / 16;
1667 int rowBytes = quarterWidth * 4;
1668 int quarterHeight = image->height() / 16;
1669 srcPixels.resize(quarterHeight * rowBytes);
1670 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1671 &srcPixels.front(), rowBytes);
1672 canvas->scale(4, 4);
1673 SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality,
1674 kMedium_SkFilterQuality, kHigh_SkFilterQuality };
1675 for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) {
1676 image->scalePixels(pixmap, qualities[index]);
1677 sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
1678 canvas->drawImage(filtered, 16 * index, 0);
1679 }
Cary Clarka560c472017-11-27 10:44:06 -05001680##
1681
Cary Clarkac47b882018-01-11 10:35:44 -05001682#SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001683
1684#Method ##
1685
1686# ------------------------------------------------------------------------------
1687
1688#Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const
Cary Clark78de7512018-02-07 07:27:09 -05001689#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001690#Line # returns encoded Image as SkData ##
Cary Clarkac47b882018-01-11 10:35:44 -05001691Encodes Image pixels, returning result as SkData.
Cary Clark2f466242017-12-11 16:03:17 -05001692
Cary Clarkac47b882018-01-11 10:35:44 -05001693Returns nullptr if encoding fails, or if encodedImageFormat is not supported.
Cary Clarka560c472017-11-27 10:44:06 -05001694
Cary Clarkac47b882018-01-11 10:35:44 -05001695Image encoding in a format requires both building with one or more of:
1696SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support
1697for the encoded format.
1698
1699If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can
1700additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP,
1701SkEncodedImageFormat::kGIF.
1702
1703quality is a platform and format specific metric trading off size and encoding
1704error. When used, quality equaling 100 encodes with the least error. quality may
1705be ignored by the encoder.
1706
1707#Param encodedImageFormat one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
1708 SkEncodedImageFormat::kWEBP
1709 ##
1710#Param quality encoder specific metric with 100 equaling best ##
Cary Clarka560c472017-11-27 10:44:06 -05001711
Cary Clark2f466242017-12-11 16:03:17 -05001712#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001713
1714#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001715#Image 3
1716 canvas->scale(4, 4);
1717 SkIRect subset = {0, 0, 16, 64};
1718 int x = 0;
1719 for (int quality : { 0, 10, 50, 100 } ) {
1720 sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
1721 sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
1722 canvas->drawImage(filtered, x, 0);
1723 x += 16;
1724 }
Cary Clarka560c472017-11-27 10:44:06 -05001725##
1726
Cary Clarkac47b882018-01-11 10:35:44 -05001727#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001728
1729#Method ##
1730
1731# ------------------------------------------------------------------------------
1732
Cary Clark61ca7c52018-01-02 11:34:14 -05001733#Method sk_sp<SkData> encodeToData() const
Cary Clarka560c472017-11-27 10:44:06 -05001734
Cary Clarkac47b882018-01-11 10:35:44 -05001735Encodes Image pixels, returning result as SkData. Returns existing encoded data
1736if present; otherwise, Image is encoded with SkEncodedImageFormat::kPNG. Skia
1737must be built with SK_HAS_PNG_LIBRARY to encode Image.
Cary Clarka560c472017-11-27 10:44:06 -05001738
Cary Clarkac47b882018-01-11 10:35:44 -05001739Returns nullptr if existing encoded data is missing or invalid, and
Cary Clarka560c472017-11-27 10:44:06 -05001740encoding fails.
1741
Cary Clarkac47b882018-01-11 10:35:44 -05001742#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001743
1744#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001745#Image 3
1746 canvas->scale(4, 4);
1747 SkIRect subset = {136, 32, 200, 96};
1748 sk_sp<SkData> data(image->encodeToData());
1749 sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
1750 canvas->drawImage(eye, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001751##
1752
Cary Clarkac47b882018-01-11 10:35:44 -05001753#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001754
1755#Method ##
1756
1757# ------------------------------------------------------------------------------
1758
1759#Method sk_sp<SkData> refEncodedData() const
Cary Clark78de7512018-02-07 07:27:09 -05001760#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001761#Line # returns Image encoded in SkData if present ##
Cary Clarkac47b882018-01-11 10:35:44 -05001762Returns encoded Image pixels as SkData, if Image was created from supported
1763encoded stream format. Platform support for formats vary and may require building
1764with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY.
Cary Clarka560c472017-11-27 10:44:06 -05001765
Cary Clarkac47b882018-01-11 10:35:44 -05001766Returns nullptr if Image contents are not encoded.
Cary Clarka560c472017-11-27 10:44:06 -05001767
Cary Clarkac47b882018-01-11 10:35:44 -05001768#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001769
1770#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001771#Image 3
1772#Platform gpu
1773 struct {
1774 const char* name;
1775 sk_sp<SkImage> image;
1776 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1777 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001778 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1779 nullptr) } };
Cary Clarkac47b882018-01-11 10:35:44 -05001780 SkString string;
1781 SkPaint paint;
1782 for (const auto& test : tests ) {
1783 if (!test.image) {
1784 string.printf("no %s", test.name);
1785 } else {
1786 string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name);
1787 }
1788 canvas->drawString(string, 10, 20, paint);
1789 canvas->translate(0, 20);
1790 }
Cary Clarka560c472017-11-27 10:44:06 -05001791##
1792
Cary Clarkac47b882018-01-11 10:35:44 -05001793#SeeAlso encodeToData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001794
1795#Method ##
1796
1797# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001798#Subtopic Utility
1799#Populate
1800#Line # rarely called management functions ##
1801##
Cary Clarka560c472017-11-27 10:44:06 -05001802
1803#Method const char* toString(SkString* string) const
Cary Clark4855f782018-02-06 09:41:53 -05001804#In Utility
1805#Line # converts Image to machine readable form ##
Cary Clarkac47b882018-01-11 10:35:44 -05001806Appends Image description to string, including unique ID, width, height, and
1807whether the image is opaque.
Cary Clarka560c472017-11-27 10:44:06 -05001808
Cary Clarkac47b882018-01-11 10:35:44 -05001809#Param string storage for description; existing content is preserved ##
1810
1811#Return string appended with Image description ##
Cary Clarka560c472017-11-27 10:44:06 -05001812
1813#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001814#Image 4
1815 struct {
1816 const char* name;
1817 sk_sp<SkImage> image;
1818 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1819 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001820 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1821 nullptr) } };
Cary Clarkac47b882018-01-11 10:35:44 -05001822 SkString string;
1823 SkPaint paint;
1824 for (const auto& test : tests ) {
1825 string.printf("%s: ", test.name);
1826 test.image ? (void) test.image->toString(&string) : string.append("no image");
1827 canvas->drawString(string, 10, 20, paint);
1828 canvas->translate(0, 20);
1829 }
Cary Clarka560c472017-11-27 10:44:06 -05001830##
1831
Cary Clarkac47b882018-01-11 10:35:44 -05001832#SeeAlso SkPaint::toString
Cary Clarka560c472017-11-27 10:44:06 -05001833
1834#Method ##
1835
1836# ------------------------------------------------------------------------------
1837
1838#Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const
Cary Clark4855f782018-02-06 09:41:53 -05001839#In Constructor
1840#Line # creates Image containing part of original ##
Cary Clarkac47b882018-01-11 10:35:44 -05001841Returns subset of Image. subset must be fully contained by Image dimensions().
1842The implementation may share pixels, or may copy them.
Cary Clarka560c472017-11-27 10:44:06 -05001843
Cary Clarkac47b882018-01-11 10:35:44 -05001844Returns nullptr if subset is empty, or subset is not contained by bounds, or
1845pixels in Image could not be read or copied.
Cary Clarka560c472017-11-27 10:44:06 -05001846
Cary Clarkac47b882018-01-11 10:35:44 -05001847#Param subset bounds of returned Image ##
1848
1849#Return partial or full Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001850
1851#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001852#Image 3
1853 canvas->scale(.5f, .5f);
1854 const int width = 32;
1855 const int height = 32;
1856 for (int y = 0; y < 512; y += height ) {
1857 for (int x = 0; x < 512; x += width ) {
1858 sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height}));
1859 canvas->drawImage(subset, x * 3 / 2, y * 3 / 2);
1860 }
1861 }
Cary Clarka560c472017-11-27 10:44:06 -05001862##
1863
Cary Clarkac47b882018-01-11 10:35:44 -05001864#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001865
1866#Method ##
1867
1868# ------------------------------------------------------------------------------
1869
1870#Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const
Cary Clark4855f782018-02-06 09:41:53 -05001871#In Constructor
1872#Line # creates Image matching Color_Space if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001873Returns Image backed by GPU_Texture associated with context. Returned Image is
1874compatible with Surface created with dstColorSpace. Returns original
1875Image if context and dstColorSpace match.
1876
1877Returns nullptr if context is nullptr, or if Image was created with another
1878GrContext.
Cary Clarka560c472017-11-27 10:44:06 -05001879
Cary Clark61ca7c52018-01-02 11:34:14 -05001880#Param context GPU_Context ##
Cary Clarkac47b882018-01-11 10:35:44 -05001881#Param dstColorSpace range of colors of matching Surface on GPU ##
Cary Clarka560c472017-11-27 10:44:06 -05001882
Cary Clarkac47b882018-01-11 10:35:44 -05001883#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001884
1885#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001886#Platform gpu
1887#Image 5
1888 auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void {
1889 if (nullptr == image || nullptr == context) {
1890 return;
1891 }
1892 SkPaint paint;
1893 paint.setAntiAlias(true);
1894 paint.setTextAlign(SkPaint::kCenter_Align);
1895 sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr));
1896 canvas->drawImage(texture, 0, 0);
1897 canvas->drawString(label, texture->width() / 2, texture->height() / 4, paint);
1898 };
1899 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1900 GrContext* context = canvas->getGrContext();
1901 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001902 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1903 nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001904 drawImage(image, context, "image");
1905 canvas->translate(image->width(), 0);
1906 drawImage(bitmapImage, context, "source");
1907 canvas->translate(-image->width(), image->height());
1908 drawImage(textureImage, context, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001909##
1910
Cary Clarkac47b882018-01-11 10:35:44 -05001911#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05001912
1913#Method ##
1914
1915# ------------------------------------------------------------------------------
1916
1917#Method sk_sp<SkImage> makeNonTextureImage() const
Cary Clark4855f782018-02-06 09:41:53 -05001918#In Constructor
1919#Line # creates Image without dependency on GPU_Texture ##
Cary Clarkac47b882018-01-11 10:35:44 -05001920Returns Raster_Image or Lazy_Image. Copies Image backed by GPU_Texture into
Cary Clark4855f782018-02-06 09:41:53 -05001921CPU memory if needed. Returns original Image if decoded in Raster_Bitmap,
Cary Clarkac47b882018-01-11 10:35:44 -05001922or if encoded in a stream.
Cary Clark61ca7c52018-01-02 11:34:14 -05001923
Cary Clarkac47b882018-01-11 10:35:44 -05001924Returns nullptr if backed by GPU_Texture and copy fails.
1925
1926#Return Raster_Image, Lazy_Image, or nullptr ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001927
1928#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001929#Image 5
1930#Platform gpu
1931 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1932 if (nullptr == image) {
1933 return;
1934 }
1935 SkPaint paint;
1936 paint.setAntiAlias(true);
1937 paint.setTextAlign(SkPaint::kCenter_Align);
1938 sk_sp<SkImage> nonTexture(image->makeNonTextureImage());
1939 canvas->drawImage(nonTexture, 0, 0);
1940 canvas->drawString(label, nonTexture->width() / 2, nonTexture->height() / 4, paint);
1941 };
1942 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1943 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001944 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1945 nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001946 drawImage(image, "image");
1947 canvas->translate(image->width(), 0);
1948 drawImage(bitmapImage, "source");
1949 canvas->translate(-image->width(), image->height());
1950 drawImage(textureImage, "backEndTexture");
Cary Clark61ca7c52018-01-02 11:34:14 -05001951##
1952
Cary Clark56356312018-02-08 14:45:18 -05001953#SeeAlso makeTextureImage makeRasterImage MakeBackendTextureFromSkImage
Cary Clark61ca7c52018-01-02 11:34:14 -05001954
1955#Method ##
1956
1957# ------------------------------------------------------------------------------
1958
1959#Method sk_sp<SkImage> makeRasterImage() const
Cary Clark4855f782018-02-06 09:41:53 -05001960#In Constructor
1961#Line # creates Image compatible with Raster_Surface if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001962Returns Raster_Image. Copies Image backed by GPU_Texture into CPU memory,
Cary Clark4855f782018-02-06 09:41:53 -05001963or decodes Image from Lazy_Image. Returns original Image if decoded in
Cary Clarkac47b882018-01-11 10:35:44 -05001964Raster_Bitmap.
Cary Clarka560c472017-11-27 10:44:06 -05001965
Cary Clarkac47b882018-01-11 10:35:44 -05001966Returns nullptr if copy, decode, or pixel read fails.
Cary Clarka560c472017-11-27 10:44:06 -05001967
Cary Clarkac47b882018-01-11 10:35:44 -05001968#Return Raster_Image, or nullptr ##
1969
Cary Clark4855f782018-02-06 09:41:53 -05001970#Bug 7479
Cary Clarka560c472017-11-27 10:44:06 -05001971#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001972#Image 5
1973#Platform gpu
1974 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1975 if (nullptr == image) {
1976 return;
1977 }
1978 SkPaint paint;
1979 paint.setAntiAlias(true);
1980 paint.setTextAlign(SkPaint::kCenter_Align);
1981 sk_sp<SkImage> raster(image->makeRasterImage());
1982 canvas->drawImage(raster, 0, 0);
1983 canvas->drawString(label, raster->width() / 2, raster->height() / 4, paint);
1984 };
1985 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1986 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001987 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1988 nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001989 drawImage(image, "image");
1990 canvas->translate(image->width(), 0);
1991 drawImage(bitmapImage, "source");
1992 canvas->translate(-image->width(), image->height());
1993 drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001994##
1995
Cary Clarkac47b882018-01-11 10:35:44 -05001996#SeeAlso isTextureBacked isLazyGenerated MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -05001997
1998#Method ##
1999
2000# ------------------------------------------------------------------------------
2001
2002#Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
2003 const SkIRect& clipBounds, SkIRect* outSubset,
2004 SkIPoint* offset) const
Cary Clark4855f782018-02-06 09:41:53 -05002005#In Constructor
2006#Line # creates filtered, clipped Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002007
Cary Clarkac47b882018-01-11 10:35:44 -05002008Creates filtered Image. filter processes original Image, potentially changing
2009color, position, and size. subset is the bounds of original Image processed
2010by filter. clipBounds is the expected bounds of the filtered Image. outSubset
2011is required storage for the actual bounds of the filtered Image. offset is
2012required storage for translation of returned Image.
Cary Clarka560c472017-11-27 10:44:06 -05002013
Cary Clarkac47b882018-01-11 10:35:44 -05002014Returns nullptr if Image could not be created. If nullptr is returned, outSubset
2015and offset are undefined.
2016
Cary Clark56356312018-02-08 14:45:18 -05002017Useful for animation of SkImageFilter that varies size from frame to frame.
2018Returned Image is created larger than required by filter so that GPU_Texture
2019can be reused with different sized effects. outSubset describes the valid bounds
2020of GPU_Texture returned. offset translates the returned Image to keep subsequent
2021animation frames aligned with respect to each other.
Cary Clarkac47b882018-01-11 10:35:44 -05002022
2023#Param filter how Image is sampled when transformed ##
Cary Clark56356312018-02-08 14:45:18 -05002024#Param subset bounds of Image processed by filter ##
2025#Param clipBounds expected bounds of filtered Image ##
2026#Param outSubset storage for returned Image bounds ##
2027#Param offset storage for returned Image translation ##
Cary Clarka560c472017-11-27 10:44:06 -05002028
Cary Clarkac47b882018-01-11 10:35:44 -05002029#Return filtered Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05002030
2031#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002032#Description
2033In each frame of the animation, filtered Image is drawn in a different location.
2034By translating canvas by returned offset, Image appears stationary.
2035##
2036#Image 5
2037#Platform gpu
2038#Duration 5
2039 sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make(
2040 -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE,
2041 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
2042 nullptr);
2043 sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr);
2044 SkIRect subset = image->bounds();
2045 SkIRect clipBounds = image->bounds();
2046 clipBounds.outset(60, 60);
2047 SkIRect outSubset;
2048 SkIPoint offset;
2049 sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds,
2050 &outSubset, &offset));
2051 SkPaint paint;
2052 paint.setAntiAlias(true);
2053 paint.setStyle(SkPaint::kStroke_Style);
2054 canvas->drawLine(0, 0, offset.fX, offset.fY, paint);
2055 canvas->translate(offset.fX, offset.fY);
2056 canvas->drawImage(filtered, 0, 0);
Cary Clark681287e2018-03-16 11:34:15 -04002057 canvas->drawRect(SkRect::Make(outSubset), paint);
Cary Clarka560c472017-11-27 10:44:06 -05002058##
2059
Cary Clark56356312018-02-08 14:45:18 -05002060#SeeAlso makeShader SkPaint::setImageFilter
Cary Clarka560c472017-11-27 10:44:06 -05002061
2062#Method ##
2063
2064# ------------------------------------------------------------------------------
2065
Cary Clarka560c472017-11-27 10:44:06 -05002066#Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc
2067
2068##
2069
2070# ------------------------------------------------------------------------------
2071
2072#Method static bool MakeBackendTextureFromSkImage(GrContext* context,
2073 sk_sp<SkImage> image,
2074 GrBackendTexture* backendTexture,
2075 BackendTextureReleaseProc* backendTextureReleaseProc)
Cary Clark4855f782018-02-06 09:41:53 -05002076#In Constructor
2077#Line # creates GPU_Texture from Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002078
Cary Clark56356312018-02-08 14:45:18 -05002079Creates a GrBackendTexture from the provided SkImage. Returns true and
2080stores result in backendTexture and backendTextureReleaseProc if
2081texture is created; otherwise, returns false and leaves
2082backendTexture and backendTextureReleaseProc unmodified.
Cary Clarka560c472017-11-27 10:44:06 -05002083
Cary Clark56356312018-02-08 14:45:18 -05002084Call backendTextureReleaseProc after deleting backendTexture.
2085backendTextureReleaseProc cleans up auxiliary data related to returned
2086backendTexture. The caller must delete returned backendTexture after use.
Cary Clarka560c472017-11-27 10:44:06 -05002087
Cary Clark56356312018-02-08 14:45:18 -05002088If Image is both texture backed and singly referenced, image is returned in
2089backendTexture without conversion or making a copy. Image is singly referenced
2090if its was transferred solely using std::move().
2091
2092If Image is not texture backed, returns texture with Image contents.
Cary Clarka560c472017-11-27 10:44:06 -05002093
Cary Clark61ca7c52018-01-02 11:34:14 -05002094#Param context GPU_Context ##
Cary Clark56356312018-02-08 14:45:18 -05002095#Param image Image used for texture ##
2096#Param backendTexture storage for backend texture ##
2097#Param backendTextureReleaseProc storage for clean up function ##
Cary Clarka560c472017-11-27 10:44:06 -05002098
Cary Clark56356312018-02-08 14:45:18 -05002099#Return true if backend texture was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002100
2101#Example
Cary Clark56356312018-02-08 14:45:18 -05002102#Platform gpu
2103#Height 64
2104#Function
Brian Salomon67f85842018-02-09 08:50:22 -05002105static sk_sp<SkImage> create_gpu_image(GrContext* grContext) {
2106 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
2107 auto surface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info));
2108 SkCanvas* canvas = surface->getCanvas();
2109 canvas->clear(SK_ColorWHITE);
2110 SkPaint paint;
2111 paint.setColor(SK_ColorBLACK);
2112 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
2113 return surface->makeImageSnapshot();
2114}
2115##
2116
2117void draw(SkCanvas* canvas) {
2118 GrContext* grContext = canvas->getGrContext();
2119 if (!grContext) {
2120 return;
2121 }
2122 sk_sp<SkImage> backEndImage = create_gpu_image(grContext);
2123 canvas->drawImage(backEndImage, 0, 0);
2124 GrBackendTexture texture;
2125 SkImage::BackendTextureReleaseProc proc;
2126 if (!SkImage::MakeBackendTextureFromSkImage(grContext, std::move(backEndImage),
2127 &texture, &proc)) {
2128 return;
2129 }
2130 sk_sp<SkImage> i2 = SkImage::MakeFromTexture(grContext, texture, kTopLeft_GrSurfaceOrigin,
2131 kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
2132 canvas->drawImage(i2, 30, 30);
Cary Clark56356312018-02-08 14:45:18 -05002133}
Cary Clarka560c472017-11-27 10:44:06 -05002134##
2135
Cary Clark56356312018-02-08 14:45:18 -05002136#SeeAlso MakeFromTexture makeTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002137
2138#Method ##
2139
2140# ------------------------------------------------------------------------------
2141
2142#Enum LegacyBitmapMode
Cary Clark56356312018-02-08 14:45:18 -05002143#Deprecated soon
Cary Clarka560c472017-11-27 10:44:06 -05002144#Code
2145 enum LegacyBitmapMode {
2146 kRO_LegacyBitmapMode,
Cary Clarka560c472017-11-27 10:44:06 -05002147 };
2148##
2149
Cary Clarka560c472017-11-27 10:44:06 -05002150#Const kRO_LegacyBitmapMode 0
Cary Clark56356312018-02-08 14:45:18 -05002151Returned bitmap is read-only and immutable.
Cary Clarka560c472017-11-27 10:44:06 -05002152##
Cary Clarka560c472017-11-27 10:44:06 -05002153
2154#Enum ##
2155
2156# ------------------------------------------------------------------------------
2157
Cary Clark56356312018-02-08 14:45:18 -05002158#Method bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode = kRO_LegacyBitmapMode) const
Cary Clark4855f782018-02-06 09:41:53 -05002159#In Constructor
2160#Line # returns as Raster_Bitmap ##
Cary Clarkac47b882018-01-11 10:35:44 -05002161Creates raster Bitmap with same pixels as Image. If legacyBitmapMode is
2162kRO_LegacyBitmapMode, returned bitmap is read-only and immutable.
2163Returns true if Bitmap is stored in bitmap. Returns false and resets bitmap if
2164Bitmap write did not succeed.
Cary Clarka560c472017-11-27 10:44:06 -05002165
Cary Clark3cd22cc2017-12-01 11:49:58 -05002166#Param bitmap storage for legacy Bitmap ##
Cary Clark56356312018-02-08 14:45:18 -05002167#Param legacyBitmapMode to be deprecated ##
Cary Clarka560c472017-11-27 10:44:06 -05002168
Cary Clark3cd22cc2017-12-01 11:49:58 -05002169#Return true if Bitmap was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002170
2171#Example
Cary Clark56356312018-02-08 14:45:18 -05002172#Image 4
2173#Platform gpu
Brian Salomon67f85842018-02-09 08:50:22 -05002174 SkBitmap bitImage;
2175 if (image->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2176 canvas->drawBitmap(bitImage, 0, 0);
2177 }
2178 GrContext* grContext = canvas->getGrContext();
2179 if (!grContext) {
2180 return;
2181 }
2182 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(grContext, backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04002183 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
2184 nullptr));
Brian Salomon67f85842018-02-09 08:50:22 -05002185 canvas->drawImage(textureImage, 45, 45);
2186 if (textureImage->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2187 canvas->drawBitmap(bitImage, 90, 90);
2188 }
Cary Clarka560c472017-11-27 10:44:06 -05002189##
2190
Cary Clark56356312018-02-08 14:45:18 -05002191#SeeAlso MakeRasterData makeRasterImage makeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002192
2193#Method ##
2194
2195# ------------------------------------------------------------------------------
2196
2197#Method bool isLazyGenerated() const
Cary Clark4855f782018-02-06 09:41:53 -05002198#In Property
2199#Line # returns if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002200Returns true if Image is backed by an image-generator or other service that creates
2201and caches its pixels or texture on-demand.
2202
Cary Clark2f466242017-12-11 16:03:17 -05002203#Return true if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002204
2205#Example
Cary Clark2f466242017-12-11 16:03:17 -05002206#Height 80
2207#Function
2208class TestImageGenerator : public SkImageGenerator {
2209public:
2210 TestImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(10, 10)) {}
2211 ~TestImageGenerator() override {}
2212protected:
2213 bool onGetPixels(const SkImageInfo& info, void* pixelPtr, size_t rowBytes,
2214 const Options& options) override {
2215 SkPMColor* pixels = static_cast<SkPMColor*>(pixelPtr);
2216 for (int y = 0; y < info.height(); ++y) {
2217 for (int x = 0; x < info.width(); ++x) {
2218 pixels[y * info.width() + x] = 0xff223344 + y * 0x000C0811;
2219 }
2220 }
2221 return true;
2222 }
2223};
2224##
2225void draw(SkCanvas* canvas) {
2226 auto gen = std::unique_ptr<TestImageGenerator>(new TestImageGenerator());
2227 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
2228 SkString lazy(image->isLazyGenerated() ? "is lazy" : "not lazy");
2229 canvas->scale(8, 8);
2230 canvas->drawImage(image, 0, 0, nullptr);
2231 SkPaint paint;
2232 paint.setTextSize(4);
2233 canvas->drawString(lazy, 2, 5, paint);
2234}
Cary Clarka560c472017-11-27 10:44:06 -05002235##
2236
Cary Clarkf5404bb2018-01-05 12:10:09 -05002237#Example
2238#Image 5
2239#Platform gpu
2240void draw(SkCanvas* canvas) {
2241 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2242 if (nullptr == image) {
2243 return;
2244 }
2245 SkPaint paint;
2246 paint.setAntiAlias(true);
2247 paint.setTextAlign(SkPaint::kCenter_Align);
2248 canvas->drawImage(image, 0, 0);
2249 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
2250 canvas->drawString(
2251 image->isLazyGenerated() ? "is lazily generated" : "not lazily generated",
2252 image->width() / 2, image->height() * 3 / 4, paint);
2253 };
2254 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2255 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04002256 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
2257 nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05002258 drawImage(image, "image");
2259 canvas->translate(image->width(), 0);
2260 drawImage(bitmapImage, "source");
2261 canvas->translate(-image->width(), image->height());
2262 drawImage(textureImage, "backEndTexture");
2263}
2264##
2265
Cary Clarkac47b882018-01-11 10:35:44 -05002266#SeeAlso isTextureBacked MakeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002267
2268#Method ##
2269
2270# ------------------------------------------------------------------------------
2271
2272#Method sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target,
2273 SkTransferFunctionBehavior premulBehavior) const
Cary Clark4855f782018-02-06 09:41:53 -05002274#In Constructor
2275#Line # creates Image matching Color_Space if possible ##
Cary Clarka560c472017-11-27 10:44:06 -05002276
Cary Clarkac47b882018-01-11 10:35:44 -05002277Creates Image in target Color_Space.
2278Returns nullptr if Image could not be created.
Cary Clarka560c472017-11-27 10:44:06 -05002279
Cary Clarkac47b882018-01-11 10:35:44 -05002280Returns original Image if it is in target Color_Space.
2281Otherwise, converts pixels from Image Color_Space to target Color_Space.
2282If Image colorSpace returns nullptr, Image Color_Space is assumed to be sRGB.
2283
2284SkTransferFunctionBehavior is to be deprecated.
2285
2286Set premulBehavior to SkTransferFunctionBehavior::kRespect to convert Image
2287pixels to a linear space, before converting to destination Color_Type
Cary Clarka560c472017-11-27 10:44:06 -05002288and Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -05002289
Cary Clarkac47b882018-01-11 10:35:44 -05002290Set premulBehavior to SkTransferFunctionBehavior::kIgnore to treat Image
2291pixels as linear, when converting to destination Color_Type
2292and Color_Space, ignoring pixel encoding.
Cary Clarka560c472017-11-27 10:44:06 -05002293
Cary Clarkac47b882018-01-11 10:35:44 -05002294#Param target Color_Space describing color range of returned Image ##
2295#Param premulBehavior one of: SkTransferFunctionBehavior::kRespect,
2296 SkTransferFunctionBehavior::kIgnore
Cary Clarka560c472017-11-27 10:44:06 -05002297##
2298
Cary Clarkac47b882018-01-11 10:35:44 -05002299#Return created Image in target Color_Space ##
2300
2301#Example
2302#Image 5
2303#Set sRGB
2304 sk_sp<SkColorSpace> normalColorSpace = SkColorSpace::MakeRGB(
2305 SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kSRGB_Gamut);
2306 sk_sp<SkColorSpace> wackyColorSpace = normalColorSpace->makeColorSpin();
2307 for (auto colorSpace : { normalColorSpace, wackyColorSpace } ) {
2308 for (auto transfer : { SkTransferFunctionBehavior::kRespect,
2309 SkTransferFunctionBehavior::kIgnore } ) {
2310 sk_sp<SkImage> colorSpaced = image->makeColorSpace(colorSpace, transfer);
2311 canvas->drawImage(colorSpaced, 0, 0);
2312 canvas->translate(128, 0);
2313 }
2314 canvas->translate(-256, 128);
2315 }
2316##
2317
2318#SeeAlso MakeFromPixture MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05002319
2320#Method ##
2321
2322#Class SkImage ##
2323
2324#Topic Image ##