blob: e5e16af1f19525a4046b38f15cde55216c4357a8 [file] [log] [blame]
Cary Clarka560c472017-11-27 10:44:06 -05001#Topic Image
2#Alias Image_Reference
3
Cary Clark08895c42018-02-01 09:37:32 -05004#Subtopic Overview
Cary Clark4855f782018-02-06 09:41:53 -05005 #Subtopic Subtopic
Cary Clark08895c42018-02-01 09:37:32 -05006 #Populate
7 ##
8##
9
Cary Clarka560c472017-11-27 10:44:06 -050010#Class SkImage
11
Cary Clark61ca7c52018-01-02 11:34:14 -050012Image describes a two dimensional array of pixels to draw. The pixels may be
Cary Clark4855f782018-02-06 09:41:53 -050013decoded in a Raster_Bitmap, encoded in a Picture or compressed data stream,
Cary Clark61ca7c52018-01-02 11:34:14 -050014or located in GPU memory as a GPU_Texture.
15
16Image cannot be modified after it is created. Image may allocate additional
17storage as needed; for instance, an encoded Image may decode when drawn.
18
19Image width and height are greater than zero. Creating an Image with zero width
20or height returns Image equal to nullptr.
21
22Image may be created from Bitmap, Pixmap, Surface, Picture, encoded streams,
23GPU_Texture, YUV_ColorSpace data, or hardware buffer. Encoded streams supported
Cary Clark4855f782018-02-06 09:41:53 -050024include BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP. Supported encoding details
Cary Clark61ca7c52018-01-02 11:34:14 -050025vary with platform.
26
Cary Clark08895c42018-02-01 09:37:32 -050027#Subtopic Raster_Image
Cary Clark61ca7c52018-01-02 11:34:14 -050028#Alias Raster_Image
Cary Clark4855f782018-02-06 09:41:53 -050029#Line # pixels decoded in Raster_Bitmap ##
30Raster_Image pixels are decoded in a Raster_Bitmap. These pixels may be read
Cary Clark61ca7c52018-01-02 11:34:14 -050031directly and in most cases written to, although edited pixels may not be drawn
32if Image has been copied internally.
33##
34
Cary Clark08895c42018-02-01 09:37:32 -050035#Subtopic Texture_Image
36#Line # pixels located on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -050037Texture_Image are located on GPU and pixels are not accessible. Texture_Image
38are allocated optimally for best performance. Raster_Image may
39be drawn to GPU_Surface, but pixels are uploaded from CPU to GPU downgrading
40performance.
41##
42
Cary Clark08895c42018-02-01 09:37:32 -050043#Subtopic Lazy_Image
44#Line # deferred pixel buffer ##
Cary Clark61ca7c52018-01-02 11:34:14 -050045Lazy_Image defer allocating buffer for Image pixels and decoding stream until
46Image is drawn. Lazy_Image caches result if possible to speed up repeated
47drawing.
48##
Cary Clarka560c472017-11-27 10:44:06 -050049
Cary Clark4855f782018-02-06 09:41:53 -050050#Subtopic Related_Function
Cary Clark08895c42018-02-01 09:37:32 -050051#Populate
52##
Cary Clarka560c472017-11-27 10:44:06 -050053
Cary Clark4855f782018-02-06 09:41:53 -050054#Subtopic Class_or_Struct
Cary Clark08895c42018-02-01 09:37:32 -050055#Populate
Cary Clarka560c472017-11-27 10:44:06 -050056##
57
Cary Clark4855f782018-02-06 09:41:53 -050058#Subtopic Constructor
Cary Clark08895c42018-02-01 09:37:32 -050059#Populate
60##
Cary Clark5081eed2018-01-22 07:55:48 -050061
Cary Clark4855f782018-02-06 09:41:53 -050062#Subtopic Member_Function
Cary Clark08895c42018-02-01 09:37:32 -050063#Populate
64##
Cary Clarka560c472017-11-27 10:44:06 -050065
Cary Clarka560c472017-11-27 10:44:06 -050066#Typedef SkImageInfo Info
67
68##
69
Cary Clarka560c472017-11-27 10:44:06 -050070# ------------------------------------------------------------------------------
71
72#Method static sk_sp<SkImage> MakeRasterCopy(const SkPixmap& pixmap)
Cary Clark4855f782018-02-06 09:41:53 -050073#In Constructor
74#Line # creates Image from Pixmap and copied pixels ##
Cary Clark2f466242017-12-11 16:03:17 -050075Creates Image from Pixmap and copy of pixels. Since pixels are copied, Pixmap
76pixels may be modified or deleted without affecting Image.
Cary Clarka560c472017-11-27 10:44:06 -050077
Cary Clark3cd22cc2017-12-01 11:49:58 -050078Image is returned if Pixmap is valid. Valid Pixmap parameters include:
79dimensions are greater than zero;
80each dimension fits in 29 bits;
81Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
82row bytes are large enough to hold one row of pixels;
83pixel address is not nullptr.
84
85#Param pixmap Image_Info, pixel address, and row bytes ##
86
87#Return copy of Pixmap pixels, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -050088
89#Example
Cary Clark2f466242017-12-11 16:03:17 -050090#Height 50
91#Description
92Draw a five by five bitmap, and draw a copy in an Image. Editing the pixmap
93alters the bitmap draw, but does not alter the Image draw since the Image
94contains a copy of the pixels.
95##
96 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
97 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
98 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
99 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
100 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
101 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
102 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
103 SkBitmap bitmap;
104 bitmap.installPixels(pixmap);
105 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
106 *pixmap.writable_addr8(2, 2) = 0x00;
107 canvas->scale(10, 10);
108 canvas->drawBitmap(bitmap, 0, 0);
109 canvas->drawImage(image, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500110##
111
Cary Clark3cd22cc2017-12-01 11:49:58 -0500112#SeeAlso MakeRasterData MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500113
114#Method ##
115
116# ------------------------------------------------------------------------------
117
118#Method static sk_sp<SkImage> MakeRasterData(const Info& info, sk_sp<SkData> pixels, size_t rowBytes)
Cary Clark4855f782018-02-06 09:41:53 -0500119#In Constructor
120#Line # creates Image from Image_Info and shared pixels ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500121Creates Image from Image_Info, sharing pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500122
Cary Clark3cd22cc2017-12-01 11:49:58 -0500123Image is returned if Image_Info is valid. Valid Image_Info parameters include:
124dimensions are greater than zero;
125each dimension fits in 29 bits;
126Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
127rowBytes are large enough to hold one row of pixels;
128pixels is not nullptr, and contains enough data for Image.
129
130#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
131#Param pixels address or pixel storage ##
132#Param rowBytes size of pixel row or larger ##
133
134#Return Image sharing pixels, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500135
136#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500137#Image 3
138 size_t rowBytes = image->width() * SkColorTypeBytesPerPixel(kRGBA_8888_SkColorType);
139 sk_sp<SkData> data = SkData::MakeUninitialized(rowBytes * image->height());
140 SkImageInfo dstInfo = SkImageInfo::MakeN32(image->width(), image->height(),
141 kPremul_SkAlphaType);
142 image->readPixels(dstInfo, data->writable_data(), rowBytes, 0, 0, SkImage::kAllow_CachingHint);
143 sk_sp<SkImage> raw = SkImage::MakeRasterData(dstInfo.makeColorType(kRGBA_8888_SkColorType),
144 data, rowBytes);
145 canvas->drawImage(image, 0, 0);
146 canvas->drawImage(raw.get(), 128, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500147##
148
Cary Clark3cd22cc2017-12-01 11:49:58 -0500149#SeeAlso MakeRasterCopy MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500150
151#Method ##
152
153# ------------------------------------------------------------------------------
154
Cary Clark3cd22cc2017-12-01 11:49:58 -0500155#Typedef void* ReleaseContext
156
157Caller data passed to RasterReleaseProc; may be nullptr.
158
159#SeeAlso MakeFromRaster RasterReleaseProc
160
161##
162
Cary Clarka560c472017-11-27 10:44:06 -0500163#Typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext)
164
Cary Clark3cd22cc2017-12-01 11:49:58 -0500165Function called when Image no longer shares pixels. ReleaseContext is
166provided by caller when Image is created, and may be nullptr.
167
168#SeeAlso ReleaseContext MakeFromRaster
169
Cary Clarka560c472017-11-27 10:44:06 -0500170##
171
172#Method static sk_sp<SkImage> MakeFromRaster(const SkPixmap& pixmap,
173 RasterReleaseProc rasterReleaseProc,
174 ReleaseContext releaseContext)
Cary Clark4855f782018-02-06 09:41:53 -0500175#In Constructor
176#Line # creates Image from Pixmap, with release ##
Cary Clarka560c472017-11-27 10:44:06 -0500177
Cary Clark0c5f5462017-12-15 11:21:51 -0500178Creates Image from pixmap, sharing Pixmap pixels. Pixels must remain valid and
Cary Clark3cd22cc2017-12-01 11:49:58 -0500179unchanged until rasterReleaseProc is called. rasterReleaseProc is passed
180releaseContext when Image is deleted or no longer refers to pixmap pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500181
Cary Clark0c5f5462017-12-15 11:21:51 -0500182Pass nullptr for rasterReleaseProc to share Pixmap without requiring a callback
183when Image is released. Pass nullptr for releaseContext if rasterReleaseProc
184does not require state.
185
Cary Clark3cd22cc2017-12-01 11:49:58 -0500186Image is returned if pixmap is valid. Valid Pixmap parameters include:
187dimensions are greater than zero;
188each dimension fits in 29 bits;
189Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
190row bytes are large enough to hold one row of pixels;
191pixel address is not nullptr.
192
193#Param pixmap Image_Info, pixel address, and row bytes ##
Cary Clark0c5f5462017-12-15 11:21:51 -0500194#Param rasterReleaseProc function called when pixels can be released; or nullptr ##
195#Param releaseContext state passed to rasterReleaseProc; or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500196
Cary Clark0c5f5462017-12-15 11:21:51 -0500197#Return Image sharing pixmap ##
Cary Clarka560c472017-11-27 10:44:06 -0500198
199#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500200#Function
201static void releaseProc(const void* pixels, SkImage::ReleaseContext context) {
202 int* countPtr = static_cast<int*>(context);
203 *countPtr += 1;
204}
205##
206
207void draw(SkCanvas* canvas) {
208 SkColor color = 0;
209 SkPixmap pixmap(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), &color, 4);
210 int releaseCount = 0;
211 sk_sp<SkImage> image(SkImage::MakeFromRaster(pixmap, releaseProc, &releaseCount));
212 SkDebugf("before reset: %d\n", releaseCount);
213 image.reset();
214 SkDebugf("after reset: %d\n", releaseCount);
215}
216#StdOut
217before reset: 0
218after reset: 1
219##
Cary Clarka560c472017-11-27 10:44:06 -0500220##
221
Cary Clark3cd22cc2017-12-01 11:49:58 -0500222#SeeAlso MakeRasterCopy MakeRasterData MakeFromGenerator RasterReleaseProc ReleaseContext
Cary Clarka560c472017-11-27 10:44:06 -0500223
224#Method ##
225
226# ------------------------------------------------------------------------------
227
228#Method static sk_sp<SkImage> MakeFromBitmap(const SkBitmap& bitmap)
Cary Clark4855f782018-02-06 09:41:53 -0500229#In Constructor
230#Line # creates Image from Bitmap, sharing or copying pixels ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500231Creates Image from bitmap, sharing or copying bitmap pixels. If the bitmap
232is marked immutable, and its pixel memory is shareable, it may be shared
233instead of copied.
Cary Clarka560c472017-11-27 10:44:06 -0500234
Cary Clark3cd22cc2017-12-01 11:49:58 -0500235Image is returned if bitmap is valid. Valid Bitmap parameters include:
236dimensions are greater than zero;
237each dimension fits in 29 bits;
238Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
239row bytes are large enough to hold one row of pixels;
240pixel address is not nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500241
Cary Clark3cd22cc2017-12-01 11:49:58 -0500242#Param bitmap Image_Info, row bytes, and pixels ##
243
244#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500245
246#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500247#Description
248The first Bitmap is shared; writing to the pixel memory changes the first
249Image.
250The second Bitmap is marked immutable, and is copied; writing to the pixel
251memory does not alter the second Image.
252##
253#Height 50
254 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
255 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
256 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
257 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
258 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
259 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
260 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
261 SkBitmap bitmap;
262 bitmap.installPixels(pixmap);
263 sk_sp<SkImage> image1 = SkImage::MakeFromBitmap(bitmap);
264 bitmap.setImmutable();
265 sk_sp<SkImage> image2 = SkImage::MakeFromBitmap(bitmap);
266 *pixmap.writable_addr8(2, 2) = 0x00;
267 canvas->scale(10, 10);
268 canvas->drawImage(image1, 0, 0);
269 canvas->drawImage(image2, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500270##
271
Cary Clark3cd22cc2017-12-01 11:49:58 -0500272#SeeAlso MakeFromRaster MakeRasterCopy MakeFromGenerator MakeRasterData
Cary Clarka560c472017-11-27 10:44:06 -0500273
274#Method ##
275
276# ------------------------------------------------------------------------------
277
278#Method static sk_sp<SkImage> MakeFromGenerator(std::unique_ptr<SkImageGenerator> imageGenerator,
279 const SkIRect* subset = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500280#In Constructor
281#Line # creates Image from a stream of data ##
Cary Clarka560c472017-11-27 10:44:06 -0500282
Cary Clark0c5f5462017-12-15 11:21:51 -0500283Creates Image from data returned by imageGenerator. Generated data is owned by Image and may not
284be shared or accessed.
Cary Clarka560c472017-11-27 10:44:06 -0500285
Cary Clark0c5f5462017-12-15 11:21:51 -0500286subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
287otherwise, subset must be contained by image bounds.
288
289Image is returned if generator data is valid. Valid data parameters vary by type of data
290and platform.
Cary Clarka560c472017-11-27 10:44:06 -0500291
Cary Clark3cd22cc2017-12-01 11:49:58 -0500292imageGenerator may wrap Picture data, codec data, or custom data.
293
294#Param imageGenerator stock or custom routines to retrieve Image ##
295#Param subset bounds of returned Image; may be nullptr ##
296
297#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500298
299#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500300#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500301#Description
302The generator returning Picture cannot be shared; std::move transfers ownership to generated Image.
303##
304 SkPictureRecorder recorder;
305 recorder.beginRecording(100, 100)->drawColor(SK_ColorRED);
306 auto picture = recorder.finishRecordingAsPicture();
307 auto gen = SkImageGenerator::MakeFromPicture({100, 100}, picture, nullptr, nullptr,
308 SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB());
309 sk_sp<SkImage> image = SkImage::MakeFromGenerator(std::move(gen));
310 canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500311##
312
Cary Clark3cd22cc2017-12-01 11:49:58 -0500313#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500314
315#Method ##
316
317# ------------------------------------------------------------------------------
318
319#Method static sk_sp<SkImage> MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500320#In Constructor
321#Line # creates Image from encoded data ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500322Creates Image from encoded data.
Cary Clark0c5f5462017-12-15 11:21:51 -0500323subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
324otherwise, subset must be contained by image bounds.
Cary Clarka560c472017-11-27 10:44:06 -0500325
Cary Clark3cd22cc2017-12-01 11:49:58 -0500326Image is returned if format of the encoded data is recognized and supported.
Cary Clark4855f782018-02-06 09:41:53 -0500327Recognized formats vary by platform.
Cary Clarka560c472017-11-27 10:44:06 -0500328
Cary Clark3cd22cc2017-12-01 11:49:58 -0500329#Param encoded data of Image to decode ##
330#Param subset bounds of returned Image; may be nullptr ##
331
332#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500333
Cary Clark61ca7c52018-01-02 11:34:14 -0500334#Example
335#Image 3
336int x = 0;
337for (int quality : { 100, 50, 10, 1} ) {
338 sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, quality);
339 sk_sp<SkImage> image = SkImage::MakeFromEncoded(encodedData);
340 canvas->drawImage(image, x, 0);
341 x += 64;
342}
Cary Clarka560c472017-11-27 10:44:06 -0500343##
344
Cary Clark3cd22cc2017-12-01 11:49:58 -0500345#SeeAlso MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500346
347#Method ##
348
349# ------------------------------------------------------------------------------
350
351#Typedef void (*TextureReleaseProc)(ReleaseContext releaseContext)
352
353##
354
355#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
356 const GrBackendTexture& backendTexture,
357 GrSurfaceOrigin origin,
358 SkAlphaType alphaType,
359 sk_sp<SkColorSpace> colorSpace)
Cary Clarkac47b882018-01-11 10:35:44 -0500360#Deprecated
Cary Clark61ca7c52018-01-02 11:34:14 -0500361#Method ##
362
363# ------------------------------------------------------------------------------
364
365#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
366 const GrBackendTexture& backendTexture,
367 GrSurfaceOrigin origin,
368 SkAlphaType alphaType,
369 sk_sp<SkColorSpace> colorSpace,
370 TextureReleaseProc textureReleaseProc,
371 ReleaseContext releaseContext)
372
Cary Clarkac47b882018-01-11 10:35:44 -0500373#Deprecated
Cary Clark61ca7c52018-01-02 11:34:14 -0500374#Method ##
375
376# ------------------------------------------------------------------------------
377
378#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
379 const GrBackendTexture& backendTexture,
380 GrSurfaceOrigin origin,
381 SkColorType colorType,
382 SkAlphaType alphaType,
383 sk_sp<SkColorSpace> colorSpace)
Cary Clarkf895a422018-02-27 09:54:21 -0500384#In Constructor
385#Line # creates Image from GPU_Texture ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500386Creates Image from GPU_Texture associated with context. Caller is responsible for
387managing the lifetime of GPU_Texture.
388
389Image is returned if format of backendTexture is recognized and supported.
390Recognized formats vary by GPU back-end.
391
392#Param context GPU_Context ##
393#Param backendTexture texture residing on GPU ##
394#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500395#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500396##
Cary Clark681287e2018-03-16 11:34:15 -0400397#Param alphaType one of: #list_of_alpha_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500398##
399#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500400
401#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500402
403#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500404#Image 3
405#Platform gpu
406#Height 128
407#Description
408A back-end texture has been created and uploaded to the GPU outside of this example.
409##
410GrContext* context = canvas->getGrContext();
411if (!context) {
412 return;
413}
414canvas->scale(.25f, .25f);
415int x = 0;
416for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500417 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark56356312018-02-08 14:45:18 -0500418 origin, kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
Cary Clark0c5f5462017-12-15 11:21:51 -0500419 canvas->drawImage(image, x, 0);
420x += 512;
421}
Cary Clarka560c472017-11-27 10:44:06 -0500422##
423
Cary Clark3cd22cc2017-12-01 11:49:58 -0500424#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500425
426#Method ##
427
428# ------------------------------------------------------------------------------
429
430#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
431 const GrBackendTexture& backendTexture,
432 GrSurfaceOrigin origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500433 SkColorType colorType,
Cary Clarka560c472017-11-27 10:44:06 -0500434 SkAlphaType alphaType,
435 sk_sp<SkColorSpace> colorSpace,
436 TextureReleaseProc textureReleaseProc,
437 ReleaseContext releaseContext)
438
Cary Clark61ca7c52018-01-02 11:34:14 -0500439Creates Image from GPU_Texture associated with context. GPU_Texture must stay
Cary Clark3cd22cc2017-12-01 11:49:58 -0500440valid and unchanged until textureReleaseProc is called. textureReleaseProc is
441passed releaseContext when Image is deleted or no longer refers to texture.
Cary Clarka560c472017-11-27 10:44:06 -0500442
Cary Clark3cd22cc2017-12-01 11:49:58 -0500443Image is returned if format of backendTexture is recognized and supported.
444Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500445
Cary Clark3cd22cc2017-12-01 11:49:58 -0500446#Param context GPU_Context ##
447#Param backendTexture texture residing on GPU ##
448#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500449#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500450##
Cary Clark681287e2018-03-16 11:34:15 -0400451#Param alphaType one of: #list_of_alpha_types#
Cary Clark3cd22cc2017-12-01 11:49:58 -0500452##
Cary Clark61ca7c52018-01-02 11:34:14 -0500453#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500454#Param textureReleaseProc function called when texture can be released ##
455#Param releaseContext state passed to textureReleaseProc ##
456
457#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500458
Cary Clark0c5f5462017-12-15 11:21:51 -0500459#ToDo
460This doesn't do anything clever with TextureReleaseProc because it may not get called
Cary Clark61ca7c52018-01-02 11:34:14 -0500461fwithin the lifetime of the example
Cary Clark0c5f5462017-12-15 11:21:51 -0500462##
463
Cary Clarka560c472017-11-27 10:44:06 -0500464#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500465#Platform gpu
466#Image 4
Cary Clark0c5f5462017-12-15 11:21:51 -0500467GrContext* context = canvas->getGrContext();
468if (!context) {
469 return;
470}
Cary Clarkac47b882018-01-11 10:35:44 -0500471auto debugster = [](SkImage::ReleaseContext releaseContext) -> void {
472 *((int *) releaseContext) += 128;
Cary Clark0c5f5462017-12-15 11:21:51 -0500473};
474int x = 0;
475for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500476 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark61ca7c52018-01-02 11:34:14 -0500477 origin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr, debugster, &x);
Cary Clark0c5f5462017-12-15 11:21:51 -0500478 canvas->drawImage(image, x, 0);
479 x += 128;
480}
Cary Clarka560c472017-11-27 10:44:06 -0500481##
482
Cary Clark3cd22cc2017-12-01 11:49:58 -0500483#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500484
485#Method ##
486
487# ------------------------------------------------------------------------------
488
489#Method static sk_sp<SkImage> MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> data,
490 bool buildMips,
491 SkColorSpace* dstColorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500492#In Constructor
493#Line # creates Image from encoded data, and uploads to GPU ##
Cary Clarka560c472017-11-27 10:44:06 -0500494
Cary Clark3cd22cc2017-12-01 11:49:58 -0500495Creates Image from encoded data. Image is uploaded to GPU back-end using context.
496
497Created Image is available to other GPU contexts, and is available across thread
498boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
499share resources.
500
501When Image is no longer referenced, context releases texture memory
Cary Clarka560c472017-11-27 10:44:06 -0500502asynchronously.
Cary Clarka560c472017-11-27 10:44:06 -0500503
Cary Clark3cd22cc2017-12-01 11:49:58 -0500504Texture decoded from data is uploaded to match Surface created with
505dstColorSpace. Color_Space of Image is determined by encoded data.
Cary Clarka560c472017-11-27 10:44:06 -0500506
Cary Clark3cd22cc2017-12-01 11:49:58 -0500507Image is returned if format of data is recognized and supported, and if context
508supports moving resources. Recognized formats vary by platform and GPU back-end.
509
Cary Clark61ca7c52018-01-02 11:34:14 -0500510Image is returned using MakeFromEncoded if context is nullptr or does not support
511moving resources between contexts.
512
Cary Clark3cd22cc2017-12-01 11:49:58 -0500513#Param context GPU_Context ##
514#Param data Image to decode ##
515#Param buildMips create Image as Mip_Map if true ##
516#Param dstColorSpace range of colors of matching Surface on GPU ##
517
518#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500519
520#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500521#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500522#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500523GrContext* context = canvas->getGrContext();
524sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, 100);
525sk_sp<SkImage> image = SkImage::MakeCrossContextFromEncoded(context,
526 encodedData, false, nullptr);
527canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500528##
529
Cary Clark3cd22cc2017-12-01 11:49:58 -0500530#SeeAlso MakeCrossContextFromPixmap
531
532#Method ##
533
534# ------------------------------------------------------------------------------
535
536#Method static sk_sp<SkImage> MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap,
537 bool buildMips,
538 SkColorSpace* dstColorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500539#In Constructor
540#Line # creates Image from Pixmap, and uploads to GPU ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500541
542Creates Image from pixmap. Image is uploaded to GPU back-end using context.
543
544Created Image is available to other GPU contexts, and is available across thread
545boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
546share resources.
547
548When Image is no longer referenced, context releases texture memory
549asynchronously.
550
551Texture created from pixmap is uploaded to match Surface created with
552dstColorSpace. Color_Space of Image is determined by pixmap.colorSpace().
553
Cary Clark61ca7c52018-01-02 11:34:14 -0500554Image is returned referring to GPU back-end if context is not nullptr,
555format of data is recognized and supported, and if context supports moving
556resources between contexts. Otherwise, pixmap pixel data is copied and Image
557as returned in raster format if possible; nullptr may be returned.
558Recognized GPU formats vary by platform and GPU back-end.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500559
560#Param context GPU_Context ##
561#Param pixmap Image_Info, pixel address, and row bytes ##
562#Param buildMips create Image as Mip_Map if true ##
563#Param dstColorSpace range of colors of matching Surface on GPU ##
564
565#Return created Image, or nullptr ##
566
567#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500568#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500569#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500570GrContext* context = canvas->getGrContext();
571SkPixmap pixmap;
572if (source.peekPixels(&pixmap)) {
573 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(context, pixmap,
574 false, nullptr);
575 canvas->drawImage(image, 0, 0);
576}
Cary Clark3cd22cc2017-12-01 11:49:58 -0500577##
578
579#SeeAlso MakeCrossContextFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500580
581#Method ##
582
583# ------------------------------------------------------------------------------
584
585#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
586 const GrBackendTexture& backendTexture,
587 GrSurfaceOrigin surfaceOrigin,
588 SkAlphaType alphaType = kPremul_SkAlphaType,
589 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500590#Deprecated
Cary Clark61ca7c52018-01-02 11:34:14 -0500591#Method ##
592
593# ------------------------------------------------------------------------------
594
595#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
596 const GrBackendTexture& backendTexture,
597 GrSurfaceOrigin surfaceOrigin,
598 SkColorType colorType,
599 SkAlphaType alphaType = kPremul_SkAlphaType,
600 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500601#In Constructor
602#Line # creates Image from GPU_Texture, managed internally ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500603Creates Image from backendTexture associated with context. backendTexture and
604returned Image are managed internally, and are released when no longer needed.
Cary Clarka560c472017-11-27 10:44:06 -0500605
Cary Clark3cd22cc2017-12-01 11:49:58 -0500606Image is returned if format of backendTexture is recognized and supported.
607Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500608
Cary Clark3cd22cc2017-12-01 11:49:58 -0500609#Param context GPU_Context ##
610#Param backendTexture texture residing on GPU ##
611#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500612#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500613##
Cary Clark681287e2018-03-16 11:34:15 -0400614#Param alphaType one of: #list_of_alpha_types#
Cary Clark3cd22cc2017-12-01 11:49:58 -0500615##
Cary Clark61ca7c52018-01-02 11:34:14 -0500616#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500617
618#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500619
620#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500621#Image 5
622#Platform gpu
Cary Clark61ca7c52018-01-02 11:34:14 -0500623 if (!canvas->getGrContext()) {
624 return;
625 }
626 canvas->scale(.5f, .5f);
627 canvas->clear(0x7f3f5f7f);
628 int x = 0, y = 0;
629 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
630 for (auto alpha : { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType } ) {
631 sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(canvas->getGrContext(),
632 backEndTexture, origin,
633 kRGBA_8888_SkColorType, alpha);
634 canvas->drawImage(image, x, y);
635 x += 160;
636 }
637 x -= 160 * 3;
638 y += 256;
639 }
Cary Clarka560c472017-11-27 10:44:06 -0500640##
641
Cary Clark61ca7c52018-01-02 11:34:14 -0500642#SeeAlso MakeFromTexture MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500643
644#Method ##
645
646# ------------------------------------------------------------------------------
647
648#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400649 const GrBackendTexture yuvTextures[3],
Cary Clarka560c472017-11-27 10:44:06 -0500650 GrSurfaceOrigin surfaceOrigin,
651 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500652#In Constructor
653#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500654
Brian Salomon6a426c12018-03-15 12:16:02 -0400655Creates Image from copy of yuvTextures, an array of textures on GPU.
656yuvTextures contain pixels for YUV planes of Image. Returned Image has the dimensions
657yuvTextures[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500658
Cary Clark61ca7c52018-01-02 11:34:14 -0500659#Param context GPU_Context ##
660#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
661 kRec709_SkYUVColorSpace
662##
Brian Salomon6a426c12018-03-15 12:16:02 -0400663#Param yuvTextures array of YUV textures on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500664#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
665#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500666
Cary Clark61ca7c52018-01-02 11:34:14 -0500667#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500668
Cary Clark61ca7c52018-01-02 11:34:14 -0500669# seems too complicated to create an example for this
670#ToDo
671should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500672##
673
Cary Clark61ca7c52018-01-02 11:34:14 -0500674#NoExample
675##
676
677#SeeAlso MakeFromNV12TexturesCopy
678
679#Method ##
680
681# ------------------------------------------------------------------------------
682
683#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400684 const GrBackendTexture yuvTextures[3],
Cary Clark61ca7c52018-01-02 11:34:14 -0500685 const SkISize yuvSizes[3],
686 GrSurfaceOrigin surfaceOrigin,
687 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark681287e2018-03-16 11:34:15 -0400688#Deprecated
Cary Clarka560c472017-11-27 10:44:06 -0500689#Method ##
690
691# ------------------------------------------------------------------------------
692
693#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
694 SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400695 const GrBackendTexture nv12Textures[2],
Cary Clarka560c472017-11-27 10:44:06 -0500696 GrSurfaceOrigin surfaceOrigin,
697 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500698#In Constructor
Brian Salomon6a426c12018-03-15 12:16:02 -0400699#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500700
Cary Clark681287e2018-03-16 11:34:15 -0400701Creates Image from copy of nv12Textures, an array of textures on GPU.
Brian Salomon6a426c12018-03-15 12:16:02 -0400702nv12Textures[0] contains pixels for YUV_Component_Y plane.
703nv12Textures[1] contains pixels for YUV_Component_U plane,
Cary Clark61ca7c52018-01-02 11:34:14 -0500704followed by pixels for YUV_Component_V plane.
Cary Clark681287e2018-03-16 11:34:15 -0400705Returned Image has the dimensions nv12Textures[2].
706yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500707
Cary Clark61ca7c52018-01-02 11:34:14 -0500708#Param context GPU_Context ##
709#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
710 kRec709_SkYUVColorSpace
711##
Brian Salomon6a426c12018-03-15 12:16:02 -0400712#Param nv12Textures array of YUV textures on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500713#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
714#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500715
Cary Clark61ca7c52018-01-02 11:34:14 -0500716#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500717
Cary Clark61ca7c52018-01-02 11:34:14 -0500718# seems too complicated to create an example for this
719#ToDo
720should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500721##
722
Cary Clark61ca7c52018-01-02 11:34:14 -0500723#NoExample
724##
725
726#SeeAlso MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500727
728#Method ##
729
730# ------------------------------------------------------------------------------
731
Cary Clark61ca7c52018-01-02 11:34:14 -0500732#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
733 SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400734 const GrBackendTexture nv12Textures[2],
Cary Clark61ca7c52018-01-02 11:34:14 -0500735 const SkISize nv12Sizes[2],
736 GrSurfaceOrigin surfaceOrigin,
737 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark681287e2018-03-16 11:34:15 -0400738#Deprecated
Cary Clark61ca7c52018-01-02 11:34:14 -0500739#Method ##
740
741# ------------------------------------------------------------------------------
742
Cary Clark4855f782018-02-06 09:41:53 -0500743# currently uncalled by any test or client ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500744#Bug 7424
Cary Clark61ca7c52018-01-02 11:34:14 -0500745
Cary Clark56356312018-02-08 14:45:18 -0500746#EnumClass BitDepth
Cary Clarka560c472017-11-27 10:44:06 -0500747
748#Code
Cary Clark61ca7c52018-01-02 11:34:14 -0500749 enum class BitDepth {
Cary Clarka560c472017-11-27 10:44:06 -0500750 kU8,
751 kF16,
752 };
753##
754
755#Const kU8 0
Cary Clark61ca7c52018-01-02 11:34:14 -0500756Use 8 bits per Color_ARGB component using unsigned integer format.
Cary Clarka560c472017-11-27 10:44:06 -0500757##
758#Const kF16 1
Cary Clark61ca7c52018-01-02 11:34:14 -0500759Use 16 bits per Color_ARGB component using half-precision floating point format.
Cary Clarka560c472017-11-27 10:44:06 -0500760##
761
Cary Clark61ca7c52018-01-02 11:34:14 -0500762#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500763##
764
Cary Clark61ca7c52018-01-02 11:34:14 -0500765#SeeAlso MakeFromPicture
Cary Clarka560c472017-11-27 10:44:06 -0500766
Cary Clark56356312018-02-08 14:45:18 -0500767#EnumClass ##
Cary Clarka560c472017-11-27 10:44:06 -0500768
769# ------------------------------------------------------------------------------
770
771#Method static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
772 const SkMatrix* matrix, const SkPaint* paint,
773 BitDepth bitDepth,
774 sk_sp<SkColorSpace> colorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500775#In Constructor
776#Line # creates Image from Picture ##
Cary Clarka560c472017-11-27 10:44:06 -0500777
Cary Clark61ca7c52018-01-02 11:34:14 -0500778Creates Image from picture. Returned Image width and height are set by dimensions.
779Image draws picture with matrix and paint, set to bitDepth and colorSpace.
Cary Clarka560c472017-11-27 10:44:06 -0500780
Cary Clark61ca7c52018-01-02 11:34:14 -0500781If matrix is nullptr, draws with identity Matrix. If paint is nullptr, draws
782with default Paint. colorSpace may be nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500783
Cary Clark61ca7c52018-01-02 11:34:14 -0500784#Param picture stream of drawing commands ##
785#Param dimensions width and height ##
786#Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ##
787#Param paint Paint to apply transparency, filtering, and so on; may be nullptr ##
788#Param bitDepth 8 bit integer or 16 bit float: per component ##
789#Param colorSpace range of colors; may be nullptr ##
790
791#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500792
793#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500794 SkPaint paint;
795 SkPictureRecorder recorder;
796 SkCanvas* recordingCanvas = recorder.beginRecording(50, 50);
797 for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) {
798 paint.setColor(color);
799 recordingCanvas->drawRect({10, 10, 30, 40}, paint);
800 recordingCanvas->translate(10, 10);
801 recordingCanvas->scale(1.2f, 1.4f);
802 }
803 sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
804 int x = 0, y = 0;
805 for (auto alpha : { 70, 140, 210 } ) {
806 paint.setAlpha(alpha);
807 auto srgbColorSpace = SkColorSpace::MakeSRGB();
808 sk_sp<SkImage> image = SkImage::MakeFromPicture(playback, {50, 50}, nullptr, &paint,
809 SkImage::BitDepth::kU8, srgbColorSpace);
810 canvas->drawImage(image, x, y);
811 x += 70; y += 70;
812 }
Cary Clarka560c472017-11-27 10:44:06 -0500813##
814
Cary Clark61ca7c52018-01-02 11:34:14 -0500815#SeeAlso SkCanvas::drawPicture
Cary Clarka560c472017-11-27 10:44:06 -0500816
817#Method ##
818
819# ------------------------------------------------------------------------------
820
821#Method static sk_sp<SkImage> MakeFromAHardwareBuffer(AHardwareBuffer* hardwareBuffer,
822 SkAlphaType alphaType = kPremul_SkAlphaType,
823 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500824#In Constructor
825#Line # creates Image from Android hardware buffer ##
Cary Clarka560c472017-11-27 10:44:06 -0500826
Cary Clark4855f782018-02-06 09:41:53 -0500827#Bug 7447
Cary Clarka560c472017-11-27 10:44:06 -0500828
Cary Clark61ca7c52018-01-02 11:34:14 -0500829Creates Image from Android hardware buffer.
830Returned Image takes a reference on the buffer.
Cary Clarka560c472017-11-27 10:44:06 -0500831
Cary Clark61ca7c52018-01-02 11:34:14 -0500832Only available on Android, when __ANDROID_API__ is defined to be 26 or greater.
Cary Clarka560c472017-11-27 10:44:06 -0500833
Cary Clark61ca7c52018-01-02 11:34:14 -0500834#Param hardwareBuffer AHardwareBuffer Android hardware buffer ##
Cary Clark681287e2018-03-16 11:34:15 -0400835#Param alphaType one of: #list_of_alpha_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500836##
837#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500838
Cary Clark61ca7c52018-01-02 11:34:14 -0500839#Return created Image, or nullptr ##
840
841#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500842##
843
Cary Clark61ca7c52018-01-02 11:34:14 -0500844#SeeAlso MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -0500845
846#Method ##
847
848# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -0500849#Subtopic Property
850#Populate
851#Line # values and attributes ##
852##
Cary Clarka560c472017-11-27 10:44:06 -0500853
854#Method int width() const
Cary Clark4855f782018-02-06 09:41:53 -0500855#In Property
856#Line # returns pixel column count ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500857Returns pixel count in each row.
858
859#Return pixel width in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500860
861#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500862#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500863#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500864 canvas->translate(10, 10);
865 canvas->drawImage(image, 0, 0);
866 canvas->translate(0, image->height());
867 SkPaint paint;
868 paint.setTextAlign(SkPaint::kCenter_Align);
869 canvas->drawLine(0, 10, image->width(), 10, paint);
870 canvas->drawString("width", image->width() / 2, 25, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500871##
872
Cary Clark61ca7c52018-01-02 11:34:14 -0500873#SeeAlso dimensions() height()
Cary Clarka560c472017-11-27 10:44:06 -0500874
875#Method ##
876
877# ------------------------------------------------------------------------------
878
879#Method int height() const
Cary Clark4855f782018-02-06 09:41:53 -0500880#In Property
881#Line # returns pixel row count ##
Cary Clark2f466242017-12-11 16:03:17 -0500882Returns pixel row count.
883
Cary Clark61ca7c52018-01-02 11:34:14 -0500884#Return pixel height in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500885
886#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500887#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500888#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500889 canvas->translate(10, 10);
890 canvas->drawImage(image, 0, 0);
891 canvas->translate(image->width(), 0);
892 SkPaint paint;
893 paint.setTextAlign(SkPaint::kCenter_Align);
894 paint.setVerticalText(true);
895 canvas->drawLine(10, 0, 10, image->height(), paint);
Cary Clarkac47b882018-01-11 10:35:44 -0500896 canvas->drawString("height", 25, image->height() / 2, paint);
897##
Cary Clarka560c472017-11-27 10:44:06 -0500898
Cary Clark61ca7c52018-01-02 11:34:14 -0500899#SeeAlso dimensions() width()
Cary Clarka560c472017-11-27 10:44:06 -0500900
901#Method ##
902
903# ------------------------------------------------------------------------------
904
905#Method SkISize dimensions() const
Cary Clark4855f782018-02-06 09:41:53 -0500906#In Property
907#Line # returns width() and height() ##
Cary Clark681287e2018-03-16 11:34:15 -0400908
Cary Clark2f466242017-12-11 16:03:17 -0500909Returns ISize { width(), height() }.
910
911#Return integral size of width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -0500912
913#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500914#Image 4
915 SkISize dimensions = image->dimensions();
916 SkIRect bounds = image->bounds();
917 SkIRect dimensionsAsBounds = SkIRect::MakeSize(dimensions);
918 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clark681287e2018-03-16 11:34:15 -0400919#StdOut
920dimensionsAsBounds == bounds
921##
Cary Clarka560c472017-11-27 10:44:06 -0500922##
923
Cary Clark61ca7c52018-01-02 11:34:14 -0500924#SeeAlso height() width() bounds()
Cary Clarka560c472017-11-27 10:44:06 -0500925
926#Method ##
927
928# ------------------------------------------------------------------------------
929
930#Method SkIRect bounds() const
Cary Clark4855f782018-02-06 09:41:53 -0500931#In Property
932#Line # returns width() and height() as Rectangle ##
Cary Clark2f466242017-12-11 16:03:17 -0500933Returns IRect { 0, 0, width(), height() }.
934
935#Return integral rectangle from origin to width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -0500936
937#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500938#Height 128
939#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -0500940 SkIRect bounds = image->bounds();
Cary Clarkac47b882018-01-11 10:35:44 -0500941 for (int x : { 0, bounds.width() } ) {
942 for (int y : { 0, bounds.height() } ) {
Cary Clark61ca7c52018-01-02 11:34:14 -0500943 canvas->drawImage(image, x, y);
944 }
945 }
Cary Clarka560c472017-11-27 10:44:06 -0500946##
947
Cary Clark61ca7c52018-01-02 11:34:14 -0500948#SeeAlso dimensions()
Cary Clarka560c472017-11-27 10:44:06 -0500949
950#Method ##
951
952# ------------------------------------------------------------------------------
953
954#Method uint32_t uniqueID() const
Cary Clark4855f782018-02-06 09:41:53 -0500955#In Property
956#Line # identifier for Image ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500957Returns value unique to image. Image contents cannot change after Image is
958created. Any operation to create a new Image will receive generate a new
959unique number.
960
961#Return unique identifier ##
Cary Clarka560c472017-11-27 10:44:06 -0500962
963#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500964#Image 5
965#Height 156
966 sk_sp<SkImage> subset = image->makeSubset({10, 20, 90, 100});
967 canvas->drawImage(image, 0, 0);
968 canvas->drawImage(subset, 128, 0);
969 SkPaint paint;
970 SkString s;
971 s.printf("original id: %d", image->uniqueID());
972 canvas->drawString(s, 20, image->height() + 20, paint);
973 s.printf("subset id: %d", subset->uniqueID());
974 canvas->drawString(s, 148, subset->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500975##
976
Cary Clark61ca7c52018-01-02 11:34:14 -0500977#SeeAlso isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -0500978
979#Method ##
980
981# ------------------------------------------------------------------------------
982
983#Method SkAlphaType alphaType() const
Cary Clark4855f782018-02-06 09:41:53 -0500984#In Property
985#Line # returns Alpha_Type ##
Cary Clark681287e2018-03-16 11:34:15 -0400986Returns Alpha_Type, one of: #list_of_alpha_types#.
Cary Clark61ca7c52018-01-02 11:34:14 -0500987
988Alpha_Type returned was a parameter to an Image constructor,
989or was parsed from encoded data.
990
991#Return Alpha_Type in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500992
993#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500994#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500995#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500996 const char* alphaTypeStr[] = { "Unknown", "Opaque", "Premul", "Unpremul" };
997 SkAlphaType alphaType = image->alphaType();
Cary Clarkac47b882018-01-11 10:35:44 -0500998 canvas->drawImage(image, 16, 0);
Cary Clark61ca7c52018-01-02 11:34:14 -0500999 SkPaint paint;
1000 canvas->drawString(alphaTypeStr[(int) alphaType], 20, image->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001001##
1002
Cary Clark61ca7c52018-01-02 11:34:14 -05001003#SeeAlso SkImageInfo::alphaType
Cary Clarka560c472017-11-27 10:44:06 -05001004
1005#Method ##
1006
1007# ------------------------------------------------------------------------------
1008
Greg Daniel56008aa2018-03-14 15:33:42 -04001009#Method SkColorType colorType() const
1010#In Property
1011#Line # returns Color_Type ##
1012
1013Returns Color_Type if known; otherwise, returns kUnknown_SkColorType.
1014
1015#Return Color_Type of Image ##
1016
1017#Example
1018// incomplete
1019##
1020
1021#SeeAlso SkImageInfo::colorType
1022
1023#Method ##
1024
1025# ------------------------------------------------------------------------------
1026
Cary Clarka560c472017-11-27 10:44:06 -05001027#Method SkColorSpace* colorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001028#In Property
1029#Line # returns Color_Space ##
Cary Clark2f466242017-12-11 16:03:17 -05001030Returns Color_Space, the range of colors, associated with Image. The
1031reference count of Color_Space is unchanged. The returned Color_Space is
1032immutable.
Cary Clarka560c472017-11-27 10:44:06 -05001033
Cary Clark61dfc3a2018-01-03 08:37:53 -05001034Color_Space returned was passed to an Image constructor,
1035or was parsed from encoded data. Color_Space returned may be ignored when Image
1036is drawn, depending on the capabilities of the Surface receiving the drawing.
Cary Clark2f466242017-12-11 16:03:17 -05001037
1038#Return Color_Space in Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001039
1040#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001041#Image 3
1042#Set sRGB
1043 SkPixmap pixmap;
1044 source.peekPixels(&pixmap);
1045 canvas->scale(.25f, .25f);
1046 int y = 0;
1047 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1048 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1049 int x = 0;
1050 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1051 for (int index = 0; index < 2; ++index) {
1052 pixmap.setColorSpace(colorSpace);
1053 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1054 canvas->drawImage(image, x, y);
1055 colorSpace = image->colorSpace()->makeColorSpin();
1056 x += 512;
1057 }
1058 y += 512;
1059 }
Cary Clarka560c472017-11-27 10:44:06 -05001060##
1061
Cary Clark61dfc3a2018-01-03 08:37:53 -05001062#SeeAlso refColorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001063
1064#Method ##
1065
1066# ------------------------------------------------------------------------------
1067
1068#Method sk_sp<SkColorSpace> refColorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001069#In Property
1070#Line # returns Image_Info Color_Space ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001071Returns a smart pointer to Color_Space, the range of colors, associated with
1072Image. The smart pointer tracks the number of objects sharing this
1073SkColorSpace reference so the memory is released when the owners destruct.
1074
1075The returned SkColorSpace is immutable.
1076
1077Color_Space returned was passed to an Image constructor,
1078or was parsed from encoded data. Color_Space returned may be ignored when Image
1079is drawn, depending on the capabilities of the Surface receiving the drawing.
1080
1081#Return Color_Space in Image, or nullptr, wrapped in a smart pointer ##
Cary Clarka560c472017-11-27 10:44:06 -05001082
1083#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001084#Image 3
1085#Set sRGB
1086 SkPixmap pixmap;
1087 source.peekPixels(&pixmap);
1088 canvas->scale(.25f, .25f);
1089 int y = 0;
1090 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1091 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1092 int x = 0;
1093 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1094 for (int index = 0; index < 2; ++index) {
1095 pixmap.setColorSpace(colorSpace);
1096 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1097 canvas->drawImage(image, x, y);
1098 colorSpace = image->refColorSpace()->makeColorSpin();
1099 x += 512;
1100 }
1101 y += 512;
1102 }
Cary Clarka560c472017-11-27 10:44:06 -05001103##
1104
Cary Clark61dfc3a2018-01-03 08:37:53 -05001105#SeeAlso colorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001106
1107#Method ##
1108
1109# ------------------------------------------------------------------------------
1110
1111#Method bool isAlphaOnly() const
Cary Clark4855f782018-02-06 09:41:53 -05001112#In Property
1113#Line # returns if pixels represent a transparency mask ##
Cary Clark2f466242017-12-11 16:03:17 -05001114Returns true if Image pixels represent transparency only. If true, each pixel
1115is packed in 8 bits as defined by kAlpha_8_SkColorType.
Cary Clarka560c472017-11-27 10:44:06 -05001116
Cary Clark2f466242017-12-11 16:03:17 -05001117#Return true if pixels represent a transparency mask ##
Cary Clarka560c472017-11-27 10:44:06 -05001118
1119#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001120 uint8_t pmColors = 0;
1121 sk_sp<SkImage> image = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1), &pmColors, 1});
1122 SkDebugf("alphaOnly = %s\n", image->isAlphaOnly() ? "true" : "false");
1123#StdOut
1124alphaOnly = true
1125##
Cary Clarka560c472017-11-27 10:44:06 -05001126##
1127
Cary Clark61dfc3a2018-01-03 08:37:53 -05001128#SeeAlso alphaType isOpaque
Cary Clarka560c472017-11-27 10:44:06 -05001129
1130#Method ##
1131
1132# ------------------------------------------------------------------------------
1133
1134#Method bool isOpaque() const
Cary Clark4855f782018-02-06 09:41:53 -05001135#In Property
1136#Line # returns if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001137Returns true if pixels ignore their Alpha value and are treated as fully opaque.
Cary Clark2f466242017-12-11 16:03:17 -05001138
1139#Return true if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clarka560c472017-11-27 10:44:06 -05001140
1141#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001142 auto check_isopaque = [](const SkImageInfo& imageInfo) -> void {
1143 auto surface(SkSurface::MakeRaster(imageInfo));
1144 auto image(surface->makeImageSnapshot());
1145 SkDebugf("isOpaque = %s\n", image->isOpaque() ? "true" : "false");
1146 };
1147
1148 check_isopaque(SkImageInfo::MakeN32Premul(5, 5));
1149 check_isopaque(SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType));
1150#StdOut
1151isOpaque = false
1152isOpaque = true
1153##
Cary Clarka560c472017-11-27 10:44:06 -05001154##
1155
Cary Clark61dfc3a2018-01-03 08:37:53 -05001156#SeeAlso alphaType isAlphaOnly
Cary Clarka560c472017-11-27 10:44:06 -05001157
1158#Method ##
1159
1160# ------------------------------------------------------------------------------
1161
1162#Method sk_sp<SkShader> makeShader(SkShader::TileMode tileMode1, SkShader::TileMode tileMode2,
1163 const SkMatrix* localMatrix = nullptr) const
Cary Clark4855f782018-02-06 09:41:53 -05001164#In Constructor
1165#Line # creates Shader, Paint element that can tile Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001166
Cary Clark61dfc3a2018-01-03 08:37:53 -05001167Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1168SkShader::TileMode rules to fill drawn area outside Image. localMatrix permits
1169transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001170
Cary Clark61dfc3a2018-01-03 08:37:53 -05001171#Param tileMode1 tiling in x, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1172 SkShader::kMirror_TileMode
1173##
1174#Param tileMode2 tiling in y, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1175 SkShader::kMirror_TileMode
1176##
1177#Param localMatrix Image transformation, or nullptr ##
1178
1179#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001180
1181#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001182#Image 4
1183SkMatrix matrix;
1184matrix.setRotate(45);
1185SkPaint paint;
1186paint.setShader(image->makeShader(SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode,
1187 &matrix));
1188canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001189##
1190
Cary Clark61dfc3a2018-01-03 08:37:53 -05001191#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001192
1193#Method ##
1194
1195# ------------------------------------------------------------------------------
1196
1197#Method sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const
1198
Cary Clark61dfc3a2018-01-03 08:37:53 -05001199Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1200SkShader::kClamp_TileMode to fill drawn area outside Image. localMatrix permits
1201transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001202
Cary Clark61dfc3a2018-01-03 08:37:53 -05001203#Param localMatrix Image transformation, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001204
Cary Clark61dfc3a2018-01-03 08:37:53 -05001205#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001206
1207#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001208#Image 5
1209SkMatrix matrix;
1210matrix.setRotate(45);
1211matrix.postTranslate(125, 30);
1212SkPaint paint;
1213paint.setShader(image->makeShader(&matrix));
1214canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001215##
1216
Cary Clarkf5404bb2018-01-05 12:10:09 -05001217#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001218
1219#Method ##
1220
1221# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001222#Subtopic Pixels
1223#Populate
1224#Line # read and write pixel values ##
1225##
Cary Clarka560c472017-11-27 10:44:06 -05001226
1227#Method bool peekPixels(SkPixmap* pixmap) const
Cary Clark78de7512018-02-07 07:27:09 -05001228#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001229#Line # returns Pixmap if possible ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001230Copies Image pixel address, row bytes, and Image_Info to pixmap, if address
1231is available, and returns true. If pixel address is not available, return
1232false and leave pixmap unchanged.
Cary Clarka560c472017-11-27 10:44:06 -05001233
Cary Clarkf5404bb2018-01-05 12:10:09 -05001234#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
Cary Clarka560c472017-11-27 10:44:06 -05001235
Cary Clarkf5404bb2018-01-05 12:10:09 -05001236#Return true if Image has direct access to pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001237
1238#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001239 SkBitmap bitmap;
1240 bitmap.allocPixels(SkImageInfo::MakeN32Premul(12, 11));
1241 SkCanvas offscreen(bitmap);
1242 offscreen.clear(SK_ColorWHITE);
1243 SkPaint paint;
1244 offscreen.drawString("%", 1, 10, paint);
1245 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
1246 SkPixmap pixmap;
1247 if (image->peekPixels(&pixmap)) {
1248 const SkPMColor* pixels = pixmap.addr32();
1249 SkPMColor pmWhite = pixels[0];
1250 for (int y = 0; y < image->height(); ++y) {
1251 for (int x = 0; x < image->width(); ++x) {
1252 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
1253 }
1254 SkDebugf("\n");
1255 }
1256 }
1257#StdOut
1258------------
1259--xx----x---
1260-x--x--x----
1261-x--x--x----
1262-x--x-x-----
1263--xx-xx-xx--
1264-----x-x--x-
1265----x--x--x-
1266----x--x--x-
1267---x----xx--
1268------------
1269##
Cary Clarka560c472017-11-27 10:44:06 -05001270##
1271
Cary Clarkf5404bb2018-01-05 12:10:09 -05001272#SeeAlso readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001273
1274#Method ##
1275
1276# ------------------------------------------------------------------------------
1277
1278#Method GrTexture* getTexture() const
Cary Clark2f466242017-12-11 16:03:17 -05001279#Deprecated
Cary Clarka560c472017-11-27 10:44:06 -05001280#Method ##
1281
1282# ------------------------------------------------------------------------------
1283
1284#Method bool isTextureBacked() const
Cary Clark78de7512018-02-07 07:27:09 -05001285#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001286#Line # returns if Image was created from GPU_Texture ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001287Returns true the contents of Image was created on or uploaded to GPU memory,
1288and is available as a GPU_Texture.
Cary Clarka560c472017-11-27 10:44:06 -05001289
Cary Clarkf5404bb2018-01-05 12:10:09 -05001290#Return true if Image is a GPU_Texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001291
1292#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001293#Image 5
1294#Platform gpu
1295auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1296 if (nullptr == image) {
1297 return;
1298 }
1299 SkPaint paint;
1300 paint.setAntiAlias(true);
1301 paint.setTextAlign(SkPaint::kCenter_Align);
1302 canvas->drawImage(image, 0, 0);
1303 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1304 canvas->drawString(image->isTextureBacked() ? "is GPU texture" : "not GPU texture",
1305 image->width() / 2, image->height() * 3 / 4, paint);
1306};
1307sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1308sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1309 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1310drawImage(image, "image");
1311canvas->translate(image->width(), 0);
1312drawImage(bitmapImage, "source");
1313canvas->translate(-image->width(), image->height());
1314drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001315##
1316
Cary Clarkf5404bb2018-01-05 12:10:09 -05001317#SeeAlso MakeFromTexture isValid
Cary Clarka560c472017-11-27 10:44:06 -05001318
1319#Method ##
1320
1321# ------------------------------------------------------------------------------
1322
1323#Method bool isValid(GrContext* context) const
Cary Clark4855f782018-02-06 09:41:53 -05001324#In Property
1325#Line # returns if Image can draw to Raster_Surface or GPU_Context ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001326Returns true if Image can be drawn on either Raster_Surface or GPU_Surface.
1327If context is nullptr, tests if Image draws on Raster_Surface;
1328otherwise, tests if Image draws on GPU_Surface associated with context.
Cary Clarka560c472017-11-27 10:44:06 -05001329
Cary Clarkf5404bb2018-01-05 12:10:09 -05001330Image backed by GPU_Texture may become invalid if associated GrContext is
1331invalid. Lazy_Image may be invalid and may not draw to Raster_Surface or
1332GPU_Surface or both.
Cary Clarka560c472017-11-27 10:44:06 -05001333
Cary Clark61ca7c52018-01-02 11:34:14 -05001334#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05001335
Cary Clarkf5404bb2018-01-05 12:10:09 -05001336#Return true if Image can be drawn ##
Cary Clarka560c472017-11-27 10:44:06 -05001337
1338#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001339#Image 5
1340#Platform gpu
1341auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1342 if (nullptr == image) {
1343 return;
1344 }
1345 SkPaint paint;
1346 paint.setAntiAlias(true);
1347 paint.setTextAlign(SkPaint::kCenter_Align);
1348 canvas->drawImage(image, 0, 0);
1349 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1350 if (canvas->getGrContext()) {
1351 canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" :
1352 "not valid on GPU", image->width() / 2, image->height() * 5 / 8, paint);
1353 }
1354 canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" :
1355 "not valid on CPU", image->width() / 2, image->height() * 7 / 8, paint);
1356};
1357sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1358sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1359 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1360drawImage(image, "image");
1361canvas->translate(image->width(), 0);
1362drawImage(bitmapImage, "source");
1363canvas->translate(-image->width(), image->height());
1364drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001365##
1366
Cary Clarkf5404bb2018-01-05 12:10:09 -05001367#SeeAlso isTextureBacked isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001368
1369#Method ##
1370
1371# ------------------------------------------------------------------------------
1372
1373#Method GrBackendObject getTextureHandle(bool flushPendingGrContextIO,
1374 GrSurfaceOrigin* origin = nullptr) const
Cary Clark78de7512018-02-07 07:27:09 -05001375#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001376#Line # returns GPU reference to Image as texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001377
Cary Clark2f466242017-12-11 16:03:17 -05001378Retrieves the back-end API handle of texture. If flushPendingGrContextIO is true,
1379complete deferred I/O operations.
Cary Clarka560c472017-11-27 10:44:06 -05001380
Cary Clark2f466242017-12-11 16:03:17 -05001381If origin in not nullptr, copies location of content drawn into Image.
Cary Clarka560c472017-11-27 10:44:06 -05001382
Cary Clark2f466242017-12-11 16:03:17 -05001383#Param flushPendingGrContextIO flag to flush outstanding requests ##
1384#Param origin storage for one of: kTopLeft_GrSurfaceOrigin,
1385 kBottomLeft_GrSurfaceOrigin; or nullptr
1386##
1387
Cary Clarkac47b882018-01-11 10:35:44 -05001388#Return back-end API texture handle, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001389
1390#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001391#Image 4
Cary Clark2f466242017-12-11 16:03:17 -05001392#Platform gpu
1393GrContext* context = canvas->getGrContext();
1394if (!context) {
1395 return;
1396}
1397SkPaint paint;
1398paint.setAntiAlias(true);
1399SkString str;
Cary Clarkac47b882018-01-11 10:35:44 -05001400int y = -10;
Cary Clark2f466242017-12-11 16:03:17 -05001401for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin } ) {
1402 sk_sp<SkImage> srcImage(SkImage::MakeFromTexture(context,
1403 backEndTexture, origin, kPremul_SkAlphaType, nullptr));
1404 GrSurfaceOrigin readBackOrigin;
1405 GrBackendObject readBackHandle = srcImage->getTextureHandle(false, &readBackOrigin);
Cary Clark681287e2018-03-16 11:34:15 -04001406 str.printf("readBackHandle: 0x%lx", readBackHandle);
Cary Clarkac47b882018-01-11 10:35:44 -05001407 canvas->drawString(str, 5, y += 30, paint);
1408 canvas->drawImage(srcImage, 80, y += 10);
Cary Clark2f466242017-12-11 16:03:17 -05001409 str.printf("origin: k%s_GrSurfaceOrigin", readBackOrigin ? "BottomLeft" : "TopLeft");
Cary Clarkac47b882018-01-11 10:35:44 -05001410 canvas->drawString(str, 5, y += srcImage->height() + 10, paint);
Cary Clark2f466242017-12-11 16:03:17 -05001411}
Cary Clarka560c472017-11-27 10:44:06 -05001412##
1413
Cary Clarkac47b882018-01-11 10:35:44 -05001414#Example
1415#Image 5
1416#Platform gpu
1417 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1418 if (nullptr == image) {
1419 return;
1420 }
1421 SkPaint paint;
1422 paint.setAntiAlias(true);
1423 paint.setTextAlign(SkPaint::kCenter_Align);
1424 canvas->drawImage(image, 0, image->height() / 4);
1425 canvas->drawString(label, image->width() / 2, image->height() / 8, paint);
1426 GrSurfaceOrigin readBackOrigin;
1427 GrBackendObject readBackHandle = image->getTextureHandle(false, &readBackOrigin);
1428 canvas->drawString(readBackHandle ? "has readBackHandle" : "no readBackHandle",
1429 image->width() / 2, image->height() * 11 / 8, paint);
1430 };
1431 drawImage(image, "image");
1432 canvas->translate(image->width(), 0);
1433 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1434 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1435 drawImage(textureImage, "backEndTexture");
1436##
1437
1438#SeeAlso MakeFromTexture isTextureBacked
Cary Clarka560c472017-11-27 10:44:06 -05001439
1440#Method ##
1441
1442# ------------------------------------------------------------------------------
1443
1444#Enum CachingHint
1445
1446#Code
1447 enum CachingHint {
1448 kAllow_CachingHint,
1449 kDisallow_CachingHint,
1450 };
1451##
1452
Cary Clarkac47b882018-01-11 10:35:44 -05001453CachingHint selects whether Skia may internally cache Bitmaps generated by
1454decoding Image, or by copying Image from GPU to CPU. The default behavior
1455allows caching Bitmaps.
1456
1457Choose kDisallow_CachingHint if Image pixels are to be used only once, or
1458if Image pixels reside in a cache outside of Skia, or to reduce memory pressure.
1459
1460Choosing kAllow_CachingHint does not ensure that pixels will be cached.
1461Image pixels may not be cached if memory requirements are too large or
1462pixels are not accessible.
Cary Clarka560c472017-11-27 10:44:06 -05001463
1464#Const kAllow_CachingHint 0
Cary Clarkac47b882018-01-11 10:35:44 -05001465Allows Skia to internally cache decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001466##
1467#Const kDisallow_CachingHint 1
Cary Clarkac47b882018-01-11 10:35:44 -05001468Disallows Skia from internally caching decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001469##
1470
Cary Clarkac47b882018-01-11 10:35:44 -05001471#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001472##
1473
Cary Clarkac47b882018-01-11 10:35:44 -05001474#SeeAlso readPixels scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001475
1476#Enum ##
1477
1478# ------------------------------------------------------------------------------
1479
1480#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1481 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001482#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001483#Line # copies and converts pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001484
Cary Clarkac47b882018-01-11 10:35:44 -05001485Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY),
1486and does not exceed Image (width(), height()).
1487
1488dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
1489destination. dstRowBytes specifics the gap from one destination row to the next.
1490Returns true if pixels are copied. Returns false if:
1491#List
1492# dstInfo.addr() equals nullptr ##
1493# dstRowBytes is less than dstInfo.minRowBytes ##
1494# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001495##
1496
Cary Clarkac47b882018-01-11 10:35:44 -05001497Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1498kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1499If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace must match.
1500If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType must
1501match. If Image Color_Space is nullptr, dstInfo.colorSpace must match. Returns
1502false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001503
Cary Clarkac47b882018-01-11 10:35:44 -05001504srcX and srcY may be negative to copy only top or left of source. Returns
1505false if width() or height() is zero or negative.
1506Returns false if
1507#Formula
1508abs(srcX) >= Image width()
1509##
1510, or if
1511#Formula
1512abs(srcY) >= Image height()
1513##
1514.
Cary Clarka560c472017-11-27 10:44:06 -05001515
Cary Clarkac47b882018-01-11 10:35:44 -05001516If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1517If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1518
1519#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1520#Param dstPixels destination pixel storage ##
1521#Param dstRowBytes destination row length ##
1522#Param srcX column index whose absolute value is less than width() ##
1523#Param srcY row index whose absolute value is less than height() ##
1524#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1525
1526#Return true if pixels are copied to dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001527
1528#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001529#Image 3
1530 canvas->scale(.5f, .5f);
1531 const int width = 32;
1532 const int height = 32;
1533 std::vector<int32_t> dstPixels;
1534 dstPixels.resize(height * width * 4);
1535 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1536 for (int y = 0; y < 512; y += height ) {
1537 for (int x = 0; x < 512; x += width ) {
1538 if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) {
1539 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
1540 SkBitmap bitmap;
1541 bitmap.installPixels(dstPixmap);
1542 canvas->drawBitmap(bitmap, 0, 0);
1543 }
1544 canvas->translate(48, 0);
1545 }
1546 canvas->translate(-16 * 48, 48);
1547 }
Cary Clarka560c472017-11-27 10:44:06 -05001548##
1549
Cary Clarkac47b882018-01-11 10:35:44 -05001550#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001551
1552#Method ##
1553
1554# ------------------------------------------------------------------------------
1555
1556#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY,
1557 CachingHint cachingHint = kAllow_CachingHint) const
1558
Cary Clarkac47b882018-01-11 10:35:44 -05001559Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and
1560does not exceed Image (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001561
Cary Clarkac47b882018-01-11 10:35:44 -05001562dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
1563and row bytes of destination. dst.rowBytes specifics the gap from one destination
1564row to the next. Returns true if pixels are copied. Returns false if:
1565#List
1566# dst pixel storage equals nullptr ##
1567# dst.rowBytes is less than SkImageInfo::minRowBytes ##
1568# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001569##
1570
Cary Clarkac47b882018-01-11 10:35:44 -05001571Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1572kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1573If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1574If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1575match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1576false if pixel conversion is not possible.
1577
1578srcX and srcY may be negative to copy only top or left of source. Returns
1579false if width() or height() is zero or negative.
1580Returns false if
1581#Formula
1582abs(srcX) >= Image width()
1583##
1584, or if
1585#Formula
1586abs(srcY) >= Image height()
1587##
1588.
1589
1590If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1591If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1592
1593#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1594#Param srcX column index whose absolute value is less than width() ##
1595#Param srcY row index whose absolute value is less than height() ##
1596#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1597
1598#Return true if pixels are copied to dst ##
1599
1600#Example
1601#Image 3
1602 std::vector<int32_t> srcPixels;
1603 int rowBytes = image->width() * 4;
1604 int quarterWidth = image->width() / 4;
1605 int quarterHeight = image->height() / 4;
1606 srcPixels.resize(image->height() * rowBytes);
1607 for (int y = 0; y < 4; ++y) {
1608 for (int x = 0; x < 4; ++x) {
1609 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1610 &srcPixels.front() + x * image->height() * quarterWidth +
1611 y * quarterWidth, rowBytes);
1612 image->readPixels(pixmap, x * quarterWidth, y * quarterHeight);
1613 }
1614 }
1615 canvas->scale(.5f, .5f);
1616 SkBitmap bitmap;
1617 bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()),
1618 &srcPixels.front(), rowBytes);
1619 canvas->drawBitmap(bitmap, 0, 0);
1620##
1621
1622#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001623
1624#Method ##
1625
1626# ------------------------------------------------------------------------------
1627
1628#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality,
1629 CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001630#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001631#Line # scales and converts one Image to another ##
Cary Clarka560c472017-11-27 10:44:06 -05001632
Cary Clarkac47b882018-01-11 10:35:44 -05001633Copies Image to dst, scaling pixels to fit dst.width() and dst.height(), and
1634converting pixels to match dst.colorType and dst.alphaType. Returns true if
1635pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1636less than dst SkImageInfo::minRowBytes.
Cary Clarka560c472017-11-27 10:44:06 -05001637
Cary Clarkac47b882018-01-11 10:35:44 -05001638Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1639kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1640If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1641If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1642match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1643false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001644
Cary Clarkac47b882018-01-11 10:35:44 -05001645Scales the image, with filterQuality, to match dst.width() and dst.height().
1646filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1647Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1648Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1649Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1650kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1651
1652If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1653If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1654
1655#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1656#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1657 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1658##
1659#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1660
1661#Return true if pixels are scaled to fit dst ##
Cary Clarka560c472017-11-27 10:44:06 -05001662
1663#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001664#Image 3
1665#Height 128
1666 std::vector<int32_t> srcPixels;
1667 int quarterWidth = image->width() / 16;
1668 int rowBytes = quarterWidth * 4;
1669 int quarterHeight = image->height() / 16;
1670 srcPixels.resize(quarterHeight * rowBytes);
1671 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1672 &srcPixels.front(), rowBytes);
1673 canvas->scale(4, 4);
1674 SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality,
1675 kMedium_SkFilterQuality, kHigh_SkFilterQuality };
1676 for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) {
1677 image->scalePixels(pixmap, qualities[index]);
1678 sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
1679 canvas->drawImage(filtered, 16 * index, 0);
1680 }
Cary Clarka560c472017-11-27 10:44:06 -05001681##
1682
Cary Clarkac47b882018-01-11 10:35:44 -05001683#SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001684
1685#Method ##
1686
1687# ------------------------------------------------------------------------------
1688
1689#Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const
Cary Clark78de7512018-02-07 07:27:09 -05001690#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001691#Line # returns encoded Image as SkData ##
Cary Clarkac47b882018-01-11 10:35:44 -05001692Encodes Image pixels, returning result as SkData.
Cary Clark2f466242017-12-11 16:03:17 -05001693
Cary Clarkac47b882018-01-11 10:35:44 -05001694Returns nullptr if encoding fails, or if encodedImageFormat is not supported.
Cary Clarka560c472017-11-27 10:44:06 -05001695
Cary Clarkac47b882018-01-11 10:35:44 -05001696Image encoding in a format requires both building with one or more of:
1697SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support
1698for the encoded format.
1699
1700If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can
1701additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP,
1702SkEncodedImageFormat::kGIF.
1703
1704quality is a platform and format specific metric trading off size and encoding
1705error. When used, quality equaling 100 encodes with the least error. quality may
1706be ignored by the encoder.
1707
1708#Param encodedImageFormat one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
1709 SkEncodedImageFormat::kWEBP
1710 ##
1711#Param quality encoder specific metric with 100 equaling best ##
Cary Clarka560c472017-11-27 10:44:06 -05001712
Cary Clark2f466242017-12-11 16:03:17 -05001713#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001714
1715#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001716#Image 3
1717 canvas->scale(4, 4);
1718 SkIRect subset = {0, 0, 16, 64};
1719 int x = 0;
1720 for (int quality : { 0, 10, 50, 100 } ) {
1721 sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
1722 sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
1723 canvas->drawImage(filtered, x, 0);
1724 x += 16;
1725 }
Cary Clarka560c472017-11-27 10:44:06 -05001726##
1727
Cary Clarkac47b882018-01-11 10:35:44 -05001728#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001729
1730#Method ##
1731
1732# ------------------------------------------------------------------------------
1733
Cary Clark61ca7c52018-01-02 11:34:14 -05001734#Method sk_sp<SkData> encodeToData() const
Cary Clarka560c472017-11-27 10:44:06 -05001735
Cary Clarkac47b882018-01-11 10:35:44 -05001736Encodes Image pixels, returning result as SkData. Returns existing encoded data
1737if present; otherwise, Image is encoded with SkEncodedImageFormat::kPNG. Skia
1738must be built with SK_HAS_PNG_LIBRARY to encode Image.
Cary Clarka560c472017-11-27 10:44:06 -05001739
Cary Clarkac47b882018-01-11 10:35:44 -05001740Returns nullptr if existing encoded data is missing or invalid, and
Cary Clarka560c472017-11-27 10:44:06 -05001741encoding fails.
1742
Cary Clarkac47b882018-01-11 10:35:44 -05001743#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001744
1745#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001746#Image 3
1747 canvas->scale(4, 4);
1748 SkIRect subset = {136, 32, 200, 96};
1749 sk_sp<SkData> data(image->encodeToData());
1750 sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
1751 canvas->drawImage(eye, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001752##
1753
Cary Clarkac47b882018-01-11 10:35:44 -05001754#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001755
1756#Method ##
1757
1758# ------------------------------------------------------------------------------
1759
1760#Method sk_sp<SkData> refEncodedData() const
Cary Clark78de7512018-02-07 07:27:09 -05001761#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001762#Line # returns Image encoded in SkData if present ##
Cary Clarkac47b882018-01-11 10:35:44 -05001763Returns encoded Image pixels as SkData, if Image was created from supported
1764encoded stream format. Platform support for formats vary and may require building
1765with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY.
Cary Clarka560c472017-11-27 10:44:06 -05001766
Cary Clarkac47b882018-01-11 10:35:44 -05001767Returns nullptr if Image contents are not encoded.
Cary Clarka560c472017-11-27 10:44:06 -05001768
Cary Clarkac47b882018-01-11 10:35:44 -05001769#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001770
1771#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001772#Image 3
1773#Platform gpu
1774 struct {
1775 const char* name;
1776 sk_sp<SkImage> image;
1777 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1778 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1779 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr) } };
1780 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,
1820 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr) } };
1821 SkString string;
1822 SkPaint paint;
1823 for (const auto& test : tests ) {
1824 string.printf("%s: ", test.name);
1825 test.image ? (void) test.image->toString(&string) : string.append("no image");
1826 canvas->drawString(string, 10, 20, paint);
1827 canvas->translate(0, 20);
1828 }
Cary Clarka560c472017-11-27 10:44:06 -05001829##
1830
Cary Clarkac47b882018-01-11 10:35:44 -05001831#SeeAlso SkPaint::toString
Cary Clarka560c472017-11-27 10:44:06 -05001832
1833#Method ##
1834
1835# ------------------------------------------------------------------------------
1836
1837#Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const
Cary Clark4855f782018-02-06 09:41:53 -05001838#In Constructor
1839#Line # creates Image containing part of original ##
Cary Clarkac47b882018-01-11 10:35:44 -05001840Returns subset of Image. subset must be fully contained by Image dimensions().
1841The implementation may share pixels, or may copy them.
Cary Clarka560c472017-11-27 10:44:06 -05001842
Cary Clarkac47b882018-01-11 10:35:44 -05001843Returns nullptr if subset is empty, or subset is not contained by bounds, or
1844pixels in Image could not be read or copied.
Cary Clarka560c472017-11-27 10:44:06 -05001845
Cary Clarkac47b882018-01-11 10:35:44 -05001846#Param subset bounds of returned Image ##
1847
1848#Return partial or full Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001849
1850#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001851#Image 3
1852 canvas->scale(.5f, .5f);
1853 const int width = 32;
1854 const int height = 32;
1855 for (int y = 0; y < 512; y += height ) {
1856 for (int x = 0; x < 512; x += width ) {
1857 sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height}));
1858 canvas->drawImage(subset, x * 3 / 2, y * 3 / 2);
1859 }
1860 }
Cary Clarka560c472017-11-27 10:44:06 -05001861##
1862
Cary Clarkac47b882018-01-11 10:35:44 -05001863#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001864
1865#Method ##
1866
1867# ------------------------------------------------------------------------------
1868
1869#Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const
Cary Clark4855f782018-02-06 09:41:53 -05001870#In Constructor
1871#Line # creates Image matching Color_Space if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001872Returns Image backed by GPU_Texture associated with context. Returned Image is
1873compatible with Surface created with dstColorSpace. Returns original
1874Image if context and dstColorSpace match.
1875
1876Returns nullptr if context is nullptr, or if Image was created with another
1877GrContext.
Cary Clarka560c472017-11-27 10:44:06 -05001878
Cary Clark61ca7c52018-01-02 11:34:14 -05001879#Param context GPU_Context ##
Cary Clarkac47b882018-01-11 10:35:44 -05001880#Param dstColorSpace range of colors of matching Surface on GPU ##
Cary Clarka560c472017-11-27 10:44:06 -05001881
Cary Clarkac47b882018-01-11 10:35:44 -05001882#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001883
1884#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001885#Platform gpu
1886#Image 5
1887 auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void {
1888 if (nullptr == image || nullptr == context) {
1889 return;
1890 }
1891 SkPaint paint;
1892 paint.setAntiAlias(true);
1893 paint.setTextAlign(SkPaint::kCenter_Align);
1894 sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr));
1895 canvas->drawImage(texture, 0, 0);
1896 canvas->drawString(label, texture->width() / 2, texture->height() / 4, paint);
1897 };
1898 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1899 GrContext* context = canvas->getGrContext();
1900 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture,
1901 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1902 drawImage(image, context, "image");
1903 canvas->translate(image->width(), 0);
1904 drawImage(bitmapImage, context, "source");
1905 canvas->translate(-image->width(), image->height());
1906 drawImage(textureImage, context, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001907##
1908
Cary Clarkac47b882018-01-11 10:35:44 -05001909#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05001910
1911#Method ##
1912
1913# ------------------------------------------------------------------------------
1914
1915#Method sk_sp<SkImage> makeNonTextureImage() const
Cary Clark4855f782018-02-06 09:41:53 -05001916#In Constructor
1917#Line # creates Image without dependency on GPU_Texture ##
Cary Clarkac47b882018-01-11 10:35:44 -05001918Returns Raster_Image or Lazy_Image. Copies Image backed by GPU_Texture into
Cary Clark4855f782018-02-06 09:41:53 -05001919CPU memory if needed. Returns original Image if decoded in Raster_Bitmap,
Cary Clarkac47b882018-01-11 10:35:44 -05001920or if encoded in a stream.
Cary Clark61ca7c52018-01-02 11:34:14 -05001921
Cary Clarkac47b882018-01-11 10:35:44 -05001922Returns nullptr if backed by GPU_Texture and copy fails.
1923
1924#Return Raster_Image, Lazy_Image, or nullptr ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001925
1926#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001927#Image 5
1928#Platform gpu
1929 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1930 if (nullptr == image) {
1931 return;
1932 }
1933 SkPaint paint;
1934 paint.setAntiAlias(true);
1935 paint.setTextAlign(SkPaint::kCenter_Align);
1936 sk_sp<SkImage> nonTexture(image->makeNonTextureImage());
1937 canvas->drawImage(nonTexture, 0, 0);
1938 canvas->drawString(label, nonTexture->width() / 2, nonTexture->height() / 4, paint);
1939 };
1940 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1941 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1942 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1943 drawImage(image, "image");
1944 canvas->translate(image->width(), 0);
1945 drawImage(bitmapImage, "source");
1946 canvas->translate(-image->width(), image->height());
1947 drawImage(textureImage, "backEndTexture");
Cary Clark61ca7c52018-01-02 11:34:14 -05001948##
1949
Cary Clark56356312018-02-08 14:45:18 -05001950#SeeAlso makeTextureImage makeRasterImage MakeBackendTextureFromSkImage
Cary Clark61ca7c52018-01-02 11:34:14 -05001951
1952#Method ##
1953
1954# ------------------------------------------------------------------------------
1955
1956#Method sk_sp<SkImage> makeRasterImage() const
Cary Clark4855f782018-02-06 09:41:53 -05001957#In Constructor
1958#Line # creates Image compatible with Raster_Surface if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001959Returns Raster_Image. Copies Image backed by GPU_Texture into CPU memory,
Cary Clark4855f782018-02-06 09:41:53 -05001960or decodes Image from Lazy_Image. Returns original Image if decoded in
Cary Clarkac47b882018-01-11 10:35:44 -05001961Raster_Bitmap.
Cary Clarka560c472017-11-27 10:44:06 -05001962
Cary Clarkac47b882018-01-11 10:35:44 -05001963Returns nullptr if copy, decode, or pixel read fails.
Cary Clarka560c472017-11-27 10:44:06 -05001964
Cary Clarkac47b882018-01-11 10:35:44 -05001965#Return Raster_Image, or nullptr ##
1966
Cary Clark4855f782018-02-06 09:41:53 -05001967#Bug 7479
Cary Clarka560c472017-11-27 10:44:06 -05001968#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001969#Image 5
1970#Platform gpu
1971 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1972 if (nullptr == image) {
1973 return;
1974 }
1975 SkPaint paint;
1976 paint.setAntiAlias(true);
1977 paint.setTextAlign(SkPaint::kCenter_Align);
1978 sk_sp<SkImage> raster(image->makeRasterImage());
1979 canvas->drawImage(raster, 0, 0);
1980 canvas->drawString(label, raster->width() / 2, raster->height() / 4, paint);
1981 };
1982 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1983 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1984 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1985 drawImage(image, "image");
1986 canvas->translate(image->width(), 0);
1987 drawImage(bitmapImage, "source");
1988 canvas->translate(-image->width(), image->height());
1989 drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001990##
1991
Cary Clarkac47b882018-01-11 10:35:44 -05001992#SeeAlso isTextureBacked isLazyGenerated MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -05001993
1994#Method ##
1995
1996# ------------------------------------------------------------------------------
1997
1998#Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
1999 const SkIRect& clipBounds, SkIRect* outSubset,
2000 SkIPoint* offset) const
Cary Clark4855f782018-02-06 09:41:53 -05002001#In Constructor
2002#Line # creates filtered, clipped Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002003
Cary Clarkac47b882018-01-11 10:35:44 -05002004Creates filtered Image. filter processes original Image, potentially changing
2005color, position, and size. subset is the bounds of original Image processed
2006by filter. clipBounds is the expected bounds of the filtered Image. outSubset
2007is required storage for the actual bounds of the filtered Image. offset is
2008required storage for translation of returned Image.
Cary Clarka560c472017-11-27 10:44:06 -05002009
Cary Clarkac47b882018-01-11 10:35:44 -05002010Returns nullptr if Image could not be created. If nullptr is returned, outSubset
2011and offset are undefined.
2012
Cary Clark56356312018-02-08 14:45:18 -05002013Useful for animation of SkImageFilter that varies size from frame to frame.
2014Returned Image is created larger than required by filter so that GPU_Texture
2015can be reused with different sized effects. outSubset describes the valid bounds
2016of GPU_Texture returned. offset translates the returned Image to keep subsequent
2017animation frames aligned with respect to each other.
Cary Clarkac47b882018-01-11 10:35:44 -05002018
2019#Param filter how Image is sampled when transformed ##
Cary Clark56356312018-02-08 14:45:18 -05002020#Param subset bounds of Image processed by filter ##
2021#Param clipBounds expected bounds of filtered Image ##
2022#Param outSubset storage for returned Image bounds ##
2023#Param offset storage for returned Image translation ##
Cary Clarka560c472017-11-27 10:44:06 -05002024
Cary Clarkac47b882018-01-11 10:35:44 -05002025#Return filtered Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05002026
2027#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002028#Description
2029In each frame of the animation, filtered Image is drawn in a different location.
2030By translating canvas by returned offset, Image appears stationary.
2031##
2032#Image 5
2033#Platform gpu
2034#Duration 5
2035 sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make(
2036 -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE,
2037 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
2038 nullptr);
2039 sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr);
2040 SkIRect subset = image->bounds();
2041 SkIRect clipBounds = image->bounds();
2042 clipBounds.outset(60, 60);
2043 SkIRect outSubset;
2044 SkIPoint offset;
2045 sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds,
2046 &outSubset, &offset));
2047 SkPaint paint;
2048 paint.setAntiAlias(true);
2049 paint.setStyle(SkPaint::kStroke_Style);
2050 canvas->drawLine(0, 0, offset.fX, offset.fY, paint);
2051 canvas->translate(offset.fX, offset.fY);
2052 canvas->drawImage(filtered, 0, 0);
Cary Clark681287e2018-03-16 11:34:15 -04002053 canvas->drawRect(SkRect::Make(outSubset), paint);
Cary Clarka560c472017-11-27 10:44:06 -05002054##
2055
Cary Clark56356312018-02-08 14:45:18 -05002056#SeeAlso makeShader SkPaint::setImageFilter
Cary Clarka560c472017-11-27 10:44:06 -05002057
2058#Method ##
2059
2060# ------------------------------------------------------------------------------
2061
Cary Clarka560c472017-11-27 10:44:06 -05002062#Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc
2063
2064##
2065
2066# ------------------------------------------------------------------------------
2067
2068#Method static bool MakeBackendTextureFromSkImage(GrContext* context,
2069 sk_sp<SkImage> image,
2070 GrBackendTexture* backendTexture,
2071 BackendTextureReleaseProc* backendTextureReleaseProc)
Cary Clark4855f782018-02-06 09:41:53 -05002072#In Constructor
2073#Line # creates GPU_Texture from Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002074
Cary Clark56356312018-02-08 14:45:18 -05002075Creates a GrBackendTexture from the provided SkImage. Returns true and
2076stores result in backendTexture and backendTextureReleaseProc if
2077texture is created; otherwise, returns false and leaves
2078backendTexture and backendTextureReleaseProc unmodified.
Cary Clarka560c472017-11-27 10:44:06 -05002079
Cary Clark56356312018-02-08 14:45:18 -05002080Call backendTextureReleaseProc after deleting backendTexture.
2081backendTextureReleaseProc cleans up auxiliary data related to returned
2082backendTexture. The caller must delete returned backendTexture after use.
Cary Clarka560c472017-11-27 10:44:06 -05002083
Cary Clark56356312018-02-08 14:45:18 -05002084If Image is both texture backed and singly referenced, image is returned in
2085backendTexture without conversion or making a copy. Image is singly referenced
2086if its was transferred solely using std::move().
2087
2088If Image is not texture backed, returns texture with Image contents.
Cary Clarka560c472017-11-27 10:44:06 -05002089
Cary Clark61ca7c52018-01-02 11:34:14 -05002090#Param context GPU_Context ##
Cary Clark56356312018-02-08 14:45:18 -05002091#Param image Image used for texture ##
2092#Param backendTexture storage for backend texture ##
2093#Param backendTextureReleaseProc storage for clean up function ##
Cary Clarka560c472017-11-27 10:44:06 -05002094
Cary Clark56356312018-02-08 14:45:18 -05002095#Return true if backend texture was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002096
2097#Example
Cary Clark56356312018-02-08 14:45:18 -05002098#Platform gpu
2099#Height 64
2100#Function
Brian Salomon67f85842018-02-09 08:50:22 -05002101static sk_sp<SkImage> create_gpu_image(GrContext* grContext) {
2102 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
2103 auto surface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info));
2104 SkCanvas* canvas = surface->getCanvas();
2105 canvas->clear(SK_ColorWHITE);
2106 SkPaint paint;
2107 paint.setColor(SK_ColorBLACK);
2108 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
2109 return surface->makeImageSnapshot();
2110}
2111##
2112
2113void draw(SkCanvas* canvas) {
2114 GrContext* grContext = canvas->getGrContext();
2115 if (!grContext) {
2116 return;
2117 }
2118 sk_sp<SkImage> backEndImage = create_gpu_image(grContext);
2119 canvas->drawImage(backEndImage, 0, 0);
2120 GrBackendTexture texture;
2121 SkImage::BackendTextureReleaseProc proc;
2122 if (!SkImage::MakeBackendTextureFromSkImage(grContext, std::move(backEndImage),
2123 &texture, &proc)) {
2124 return;
2125 }
2126 sk_sp<SkImage> i2 = SkImage::MakeFromTexture(grContext, texture, kTopLeft_GrSurfaceOrigin,
2127 kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
2128 canvas->drawImage(i2, 30, 30);
Cary Clark56356312018-02-08 14:45:18 -05002129}
Cary Clarka560c472017-11-27 10:44:06 -05002130##
2131
Cary Clark56356312018-02-08 14:45:18 -05002132#SeeAlso MakeFromTexture makeTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002133
2134#Method ##
2135
2136# ------------------------------------------------------------------------------
2137
2138#Enum LegacyBitmapMode
Cary Clark56356312018-02-08 14:45:18 -05002139#Deprecated soon
Cary Clarka560c472017-11-27 10:44:06 -05002140#Code
2141 enum LegacyBitmapMode {
2142 kRO_LegacyBitmapMode,
Cary Clarka560c472017-11-27 10:44:06 -05002143 };
2144##
2145
Cary Clarka560c472017-11-27 10:44:06 -05002146#Const kRO_LegacyBitmapMode 0
Cary Clark56356312018-02-08 14:45:18 -05002147Returned bitmap is read-only and immutable.
Cary Clarka560c472017-11-27 10:44:06 -05002148##
Cary Clarka560c472017-11-27 10:44:06 -05002149
2150#Enum ##
2151
2152# ------------------------------------------------------------------------------
2153
Cary Clark56356312018-02-08 14:45:18 -05002154#Method bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode = kRO_LegacyBitmapMode) const
Cary Clark4855f782018-02-06 09:41:53 -05002155#In Constructor
2156#Line # returns as Raster_Bitmap ##
Cary Clarkac47b882018-01-11 10:35:44 -05002157Creates raster Bitmap with same pixels as Image. If legacyBitmapMode is
2158kRO_LegacyBitmapMode, returned bitmap is read-only and immutable.
2159Returns true if Bitmap is stored in bitmap. Returns false and resets bitmap if
2160Bitmap write did not succeed.
Cary Clarka560c472017-11-27 10:44:06 -05002161
Cary Clark3cd22cc2017-12-01 11:49:58 -05002162#Param bitmap storage for legacy Bitmap ##
Cary Clark56356312018-02-08 14:45:18 -05002163#Param legacyBitmapMode to be deprecated ##
Cary Clarka560c472017-11-27 10:44:06 -05002164
Cary Clark3cd22cc2017-12-01 11:49:58 -05002165#Return true if Bitmap was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002166
2167#Example
Cary Clark56356312018-02-08 14:45:18 -05002168#Image 4
2169#Platform gpu
Brian Salomon67f85842018-02-09 08:50:22 -05002170 SkBitmap bitImage;
2171 if (image->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2172 canvas->drawBitmap(bitImage, 0, 0);
2173 }
2174 GrContext* grContext = canvas->getGrContext();
2175 if (!grContext) {
2176 return;
2177 }
2178 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(grContext, backEndTexture,
2179 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2180 canvas->drawImage(textureImage, 45, 45);
2181 if (textureImage->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2182 canvas->drawBitmap(bitImage, 90, 90);
2183 }
Cary Clarka560c472017-11-27 10:44:06 -05002184##
2185
Cary Clark56356312018-02-08 14:45:18 -05002186#SeeAlso MakeRasterData makeRasterImage makeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002187
2188#Method ##
2189
2190# ------------------------------------------------------------------------------
2191
2192#Method bool isLazyGenerated() const
Cary Clark4855f782018-02-06 09:41:53 -05002193#In Property
2194#Line # returns if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002195Returns true if Image is backed by an image-generator or other service that creates
2196and caches its pixels or texture on-demand.
2197
Cary Clark2f466242017-12-11 16:03:17 -05002198#Return true if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002199
2200#Example
Cary Clark2f466242017-12-11 16:03:17 -05002201#Height 80
2202#Function
2203class TestImageGenerator : public SkImageGenerator {
2204public:
2205 TestImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(10, 10)) {}
2206 ~TestImageGenerator() override {}
2207protected:
2208 bool onGetPixels(const SkImageInfo& info, void* pixelPtr, size_t rowBytes,
2209 const Options& options) override {
2210 SkPMColor* pixels = static_cast<SkPMColor*>(pixelPtr);
2211 for (int y = 0; y < info.height(); ++y) {
2212 for (int x = 0; x < info.width(); ++x) {
2213 pixels[y * info.width() + x] = 0xff223344 + y * 0x000C0811;
2214 }
2215 }
2216 return true;
2217 }
2218};
2219##
2220void draw(SkCanvas* canvas) {
2221 auto gen = std::unique_ptr<TestImageGenerator>(new TestImageGenerator());
2222 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
2223 SkString lazy(image->isLazyGenerated() ? "is lazy" : "not lazy");
2224 canvas->scale(8, 8);
2225 canvas->drawImage(image, 0, 0, nullptr);
2226 SkPaint paint;
2227 paint.setTextSize(4);
2228 canvas->drawString(lazy, 2, 5, paint);
2229}
Cary Clarka560c472017-11-27 10:44:06 -05002230##
2231
Cary Clarkf5404bb2018-01-05 12:10:09 -05002232#Example
2233#Image 5
2234#Platform gpu
2235void draw(SkCanvas* canvas) {
2236 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2237 if (nullptr == image) {
2238 return;
2239 }
2240 SkPaint paint;
2241 paint.setAntiAlias(true);
2242 paint.setTextAlign(SkPaint::kCenter_Align);
2243 canvas->drawImage(image, 0, 0);
2244 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
2245 canvas->drawString(
2246 image->isLazyGenerated() ? "is lazily generated" : "not lazily generated",
2247 image->width() / 2, image->height() * 3 / 4, paint);
2248 };
2249 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2250 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2251 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2252 drawImage(image, "image");
2253 canvas->translate(image->width(), 0);
2254 drawImage(bitmapImage, "source");
2255 canvas->translate(-image->width(), image->height());
2256 drawImage(textureImage, "backEndTexture");
2257}
2258##
2259
Cary Clarkac47b882018-01-11 10:35:44 -05002260#SeeAlso isTextureBacked MakeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002261
2262#Method ##
2263
2264# ------------------------------------------------------------------------------
2265
2266#Method sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target,
2267 SkTransferFunctionBehavior premulBehavior) const
Cary Clark4855f782018-02-06 09:41:53 -05002268#In Constructor
2269#Line # creates Image matching Color_Space if possible ##
Cary Clarka560c472017-11-27 10:44:06 -05002270
Cary Clarkac47b882018-01-11 10:35:44 -05002271Creates Image in target Color_Space.
2272Returns nullptr if Image could not be created.
Cary Clarka560c472017-11-27 10:44:06 -05002273
Cary Clarkac47b882018-01-11 10:35:44 -05002274Returns original Image if it is in target Color_Space.
2275Otherwise, converts pixels from Image Color_Space to target Color_Space.
2276If Image colorSpace returns nullptr, Image Color_Space is assumed to be sRGB.
2277
2278SkTransferFunctionBehavior is to be deprecated.
2279
2280Set premulBehavior to SkTransferFunctionBehavior::kRespect to convert Image
2281pixels to a linear space, before converting to destination Color_Type
Cary Clarka560c472017-11-27 10:44:06 -05002282and Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -05002283
Cary Clarkac47b882018-01-11 10:35:44 -05002284Set premulBehavior to SkTransferFunctionBehavior::kIgnore to treat Image
2285pixels as linear, when converting to destination Color_Type
2286and Color_Space, ignoring pixel encoding.
Cary Clarka560c472017-11-27 10:44:06 -05002287
Cary Clarkac47b882018-01-11 10:35:44 -05002288#Param target Color_Space describing color range of returned Image ##
2289#Param premulBehavior one of: SkTransferFunctionBehavior::kRespect,
2290 SkTransferFunctionBehavior::kIgnore
Cary Clarka560c472017-11-27 10:44:06 -05002291##
2292
Cary Clarkac47b882018-01-11 10:35:44 -05002293#Return created Image in target Color_Space ##
2294
2295#Example
2296#Image 5
2297#Set sRGB
2298 sk_sp<SkColorSpace> normalColorSpace = SkColorSpace::MakeRGB(
2299 SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kSRGB_Gamut);
2300 sk_sp<SkColorSpace> wackyColorSpace = normalColorSpace->makeColorSpin();
2301 for (auto colorSpace : { normalColorSpace, wackyColorSpace } ) {
2302 for (auto transfer : { SkTransferFunctionBehavior::kRespect,
2303 SkTransferFunctionBehavior::kIgnore } ) {
2304 sk_sp<SkImage> colorSpaced = image->makeColorSpace(colorSpace, transfer);
2305 canvas->drawImage(colorSpaced, 0, 0);
2306 canvas->translate(128, 0);
2307 }
2308 canvas->translate(-256, 128);
2309 }
2310##
2311
2312#SeeAlso MakeFromPixture MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05002313
2314#Method ##
2315
2316#Class SkImage ##
2317
2318#Topic Image ##