blob: 6041bf77368ce80b5265f65f4c44338816be0c84 [file] [log] [blame]
Cary Clarka560c472017-11-27 10:44:06 -05001#Topic Image
Cary Clark137b8742018-05-30 09:21:49 -04002#Alias Image_Reference ##
Cary Clarka560c472017-11-27 10:44:06 -05003
4#Class SkImage
5
Cary Clark61ca7c52018-01-02 11:34:14 -05006Image describes a two dimensional array of pixels to draw. The pixels may be
Cary Clark4855f782018-02-06 09:41:53 -05007decoded in a Raster_Bitmap, encoded in a Picture or compressed data stream,
Cary Clark61ca7c52018-01-02 11:34:14 -05008or located in GPU memory as a GPU_Texture.
9
10Image cannot be modified after it is created. Image may allocate additional
11storage as needed; for instance, an encoded Image may decode when drawn.
12
13Image width and height are greater than zero. Creating an Image with zero width
14or height returns Image equal to nullptr.
15
16Image may be created from Bitmap, Pixmap, Surface, Picture, encoded streams,
17GPU_Texture, YUV_ColorSpace data, or hardware buffer. Encoded streams supported
Cary Clark4855f782018-02-06 09:41:53 -050018include BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP. Supported encoding details
Cary Clark61ca7c52018-01-02 11:34:14 -050019vary with platform.
20
Cary Clark08895c42018-02-01 09:37:32 -050021#Subtopic Raster_Image
Cary Clark137b8742018-05-30 09:21:49 -040022#Alias Raster_Image ##
Cary Clark4855f782018-02-06 09:41:53 -050023#Line # pixels decoded in Raster_Bitmap ##
24Raster_Image pixels are decoded in a Raster_Bitmap. These pixels may be read
Cary Clark61ca7c52018-01-02 11:34:14 -050025directly and in most cases written to, although edited pixels may not be drawn
26if Image has been copied internally.
27##
28
Cary Clark08895c42018-02-01 09:37:32 -050029#Subtopic Texture_Image
30#Line # pixels located on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -050031Texture_Image are located on GPU and pixels are not accessible. Texture_Image
32are allocated optimally for best performance. Raster_Image may
33be drawn to GPU_Surface, but pixels are uploaded from CPU to GPU downgrading
34performance.
35##
36
Cary Clark08895c42018-02-01 09:37:32 -050037#Subtopic Lazy_Image
38#Line # deferred pixel buffer ##
Cary Clark61ca7c52018-01-02 11:34:14 -050039Lazy_Image defer allocating buffer for Image pixels and decoding stream until
40Image is drawn. Lazy_Image caches result if possible to speed up repeated
41drawing.
42##
Cary Clarka560c472017-11-27 10:44:06 -050043
Cary Clark682c58d2018-05-16 07:07:07 -040044#Subtopic Overview
Cary Clark08895c42018-02-01 09:37:32 -050045#Populate
46##
Cary Clarka560c472017-11-27 10:44:06 -050047
Cary Clark682c58d2018-05-16 07:07:07 -040048#Subtopic Related_Function
Cary Clark08895c42018-02-01 09:37:32 -050049#Populate
Cary Clarka560c472017-11-27 10:44:06 -050050##
51
Cary Clark4855f782018-02-06 09:41:53 -050052#Subtopic Constructor
Cary Clark08895c42018-02-01 09:37:32 -050053#Populate
54##
Cary Clark5081eed2018-01-22 07:55:48 -050055
Cary Clark4855f782018-02-06 09:41:53 -050056#Subtopic Member_Function
Cary Clark08895c42018-02-01 09:37:32 -050057#Populate
58##
Cary Clarka560c472017-11-27 10:44:06 -050059
Cary Clarka560c472017-11-27 10:44:06 -050060# ------------------------------------------------------------------------------
61
62#Method static sk_sp<SkImage> MakeRasterCopy(const SkPixmap& pixmap)
Cary Clark4855f782018-02-06 09:41:53 -050063#In Constructor
64#Line # creates Image from Pixmap and copied pixels ##
Cary Clark2f466242017-12-11 16:03:17 -050065Creates Image from Pixmap and copy of pixels. Since pixels are copied, Pixmap
66pixels may be modified or deleted without affecting Image.
Cary Clarka560c472017-11-27 10:44:06 -050067
Cary Clark3cd22cc2017-12-01 11:49:58 -050068Image is returned if Pixmap is valid. Valid Pixmap parameters include:
69dimensions are greater than zero;
70each dimension fits in 29 bits;
71Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
72row bytes are large enough to hold one row of pixels;
73pixel address is not nullptr.
74
75#Param pixmap Image_Info, pixel address, and row bytes ##
76
77#Return copy of Pixmap pixels, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -050078
79#Example
Cary Clark2f466242017-12-11 16:03:17 -050080#Height 50
81#Description
82Draw a five by five bitmap, and draw a copy in an Image. Editing the pixmap
83alters the bitmap draw, but does not alter the Image draw since the Image
84contains a copy of the pixels.
85##
86 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
87 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
88 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
89 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
90 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
91 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
92 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
93 SkBitmap bitmap;
94 bitmap.installPixels(pixmap);
95 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
96 *pixmap.writable_addr8(2, 2) = 0x00;
97 canvas->scale(10, 10);
98 canvas->drawBitmap(bitmap, 0, 0);
99 canvas->drawImage(image, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500100##
101
Cary Clark3cd22cc2017-12-01 11:49:58 -0500102#SeeAlso MakeRasterData MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500103
104#Method ##
105
106# ------------------------------------------------------------------------------
107
Cary Clarkcc45cc72018-05-15 16:06:12 -0400108#Method static sk_sp<SkImage> MakeRasterData(const SkImageInfo& info, sk_sp<SkData> pixels, size_t rowBytes)
Cary Clark4855f782018-02-06 09:41:53 -0500109#In Constructor
110#Line # creates Image from Image_Info and shared pixels ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500111Creates Image from Image_Info, sharing pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500112
Cary Clark3cd22cc2017-12-01 11:49:58 -0500113Image is returned if Image_Info is valid. Valid Image_Info parameters include:
114dimensions are greater than zero;
115each dimension fits in 29 bits;
116Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
117rowBytes are large enough to hold one row of pixels;
118pixels is not nullptr, and contains enough data for Image.
119
120#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
121#Param pixels address or pixel storage ##
122#Param rowBytes size of pixel row or larger ##
123
124#Return Image sharing pixels, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500125
126#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500127#Image 3
128 size_t rowBytes = image->width() * SkColorTypeBytesPerPixel(kRGBA_8888_SkColorType);
129 sk_sp<SkData> data = SkData::MakeUninitialized(rowBytes * image->height());
Cary Clark682c58d2018-05-16 07:07:07 -0400130 SkImageInfo dstInfo = SkImageInfo::MakeN32(image->width(), image->height(),
Cary Clark0c5f5462017-12-15 11:21:51 -0500131 kPremul_SkAlphaType);
132 image->readPixels(dstInfo, data->writable_data(), rowBytes, 0, 0, SkImage::kAllow_CachingHint);
133 sk_sp<SkImage> raw = SkImage::MakeRasterData(dstInfo.makeColorType(kRGBA_8888_SkColorType),
134 data, rowBytes);
135 canvas->drawImage(image, 0, 0);
136 canvas->drawImage(raw.get(), 128, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500137##
138
Cary Clark3cd22cc2017-12-01 11:49:58 -0500139#SeeAlso MakeRasterCopy MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500140
141#Method ##
142
143# ------------------------------------------------------------------------------
144
Cary Clark3cd22cc2017-12-01 11:49:58 -0500145#Typedef void* ReleaseContext
Cary Clark682c58d2018-05-16 07:07:07 -0400146#Line # parameter type for MakeFromRaster ##
Cary Clarkffb3d682018-05-17 12:17:28 -0400147
148#Code
149typedef void* ReleaseContext;
150##
151
Cary Clark3cd22cc2017-12-01 11:49:58 -0500152Caller data passed to RasterReleaseProc; may be nullptr.
153
154#SeeAlso MakeFromRaster RasterReleaseProc
155
156##
157
Cary Clarka560c472017-11-27 10:44:06 -0500158#Typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext)
Cary Clark682c58d2018-05-16 07:07:07 -0400159#Line # parameter type for MakeFromRaster ##
Cary Clarkffb3d682018-05-17 12:17:28 -0400160
161#Code
162typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext);
163##
164
Cary Clark3cd22cc2017-12-01 11:49:58 -0500165Function called when Image no longer shares pixels. ReleaseContext is
Cary Clark682c58d2018-05-16 07:07:07 -0400166provided by caller when Image is created, and may be nullptr.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500167
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 Clark682c58d2018-05-16 07:07:07 -0400231Creates Image from bitmap, sharing or copying bitmap pixels. If the bitmap
Cary Clark3cd22cc2017-12-01 11:49:58 -0500232is 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 Clark682c58d2018-05-16 07:07:07 -0400322Creates 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)
Cary Clark682c58d2018-05-16 07:07:07 -0400352#Line # parameter type for MakeFromTexture ##
Cary Clarkffb3d682018-05-17 12:17:28 -0400353
354#Code
355typedef void (*TextureReleaseProc)(ReleaseContext releaseContext);
356##
357
Cary Clark682c58d2018-05-16 07:07:07 -0400358User function called when supplied texture may be deleted.
359#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -0500360##
361
362#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
363 const GrBackendTexture& backendTexture,
364 GrSurfaceOrigin origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500365 SkColorType colorType,
366 SkAlphaType alphaType,
367 sk_sp<SkColorSpace> colorSpace)
Cary Clarkf895a422018-02-27 09:54:21 -0500368#In Constructor
369#Line # creates Image from GPU_Texture ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500370Creates Image from GPU_Texture associated with context. Caller is responsible for
371managing the lifetime of GPU_Texture.
372
373Image is returned if format of backendTexture is recognized and supported.
374Recognized formats vary by GPU back-end.
375
376#Param context GPU_Context ##
377#Param backendTexture texture residing on GPU ##
378#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500379#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500380##
Cary Clark681287e2018-03-16 11:34:15 -0400381#Param alphaType one of: #list_of_alpha_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500382##
383#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500384
385#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500386
387#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500388#Image 3
389#Platform gpu
390#Height 128
391#Description
392A back-end texture has been created and uploaded to the GPU outside of this example.
393##
394GrContext* context = canvas->getGrContext();
395if (!context) {
396 return;
397}
398canvas->scale(.25f, .25f);
399int x = 0;
400for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500401 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -0400402 origin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr);
Cary Clark0c5f5462017-12-15 11:21:51 -0500403 canvas->drawImage(image, x, 0);
404x += 512;
405}
Cary Clarka560c472017-11-27 10:44:06 -0500406##
407
Cary Clark3cd22cc2017-12-01 11:49:58 -0500408#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500409
410#Method ##
411
412# ------------------------------------------------------------------------------
413
414#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
415 const GrBackendTexture& backendTexture,
416 GrSurfaceOrigin origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500417 SkColorType colorType,
Cary Clarka560c472017-11-27 10:44:06 -0500418 SkAlphaType alphaType,
419 sk_sp<SkColorSpace> colorSpace,
420 TextureReleaseProc textureReleaseProc,
421 ReleaseContext releaseContext)
422
Cary Clark61ca7c52018-01-02 11:34:14 -0500423Creates Image from GPU_Texture associated with context. GPU_Texture must stay
Cary Clark3cd22cc2017-12-01 11:49:58 -0500424valid and unchanged until textureReleaseProc is called. textureReleaseProc is
425passed releaseContext when Image is deleted or no longer refers to texture.
Cary Clarka560c472017-11-27 10:44:06 -0500426
Cary Clark3cd22cc2017-12-01 11:49:58 -0500427Image is returned if format of backendTexture is recognized and supported.
428Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500429
Cary Clark3cd22cc2017-12-01 11:49:58 -0500430#Param context GPU_Context ##
431#Param backendTexture texture residing on GPU ##
432#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500433#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500434##
Cary Clark681287e2018-03-16 11:34:15 -0400435#Param alphaType one of: #list_of_alpha_types#
Cary Clark3cd22cc2017-12-01 11:49:58 -0500436##
Cary Clark61ca7c52018-01-02 11:34:14 -0500437#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500438#Param textureReleaseProc function called when texture can be released ##
439#Param releaseContext state passed to textureReleaseProc ##
440
441#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500442
Cary Clark0c5f5462017-12-15 11:21:51 -0500443#ToDo
444This doesn't do anything clever with TextureReleaseProc because it may not get called
Cary Clark61ca7c52018-01-02 11:34:14 -0500445fwithin the lifetime of the example
Cary Clark0c5f5462017-12-15 11:21:51 -0500446##
447
Cary Clarka560c472017-11-27 10:44:06 -0500448#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500449#Platform gpu
450#Image 4
Cary Clark0c5f5462017-12-15 11:21:51 -0500451GrContext* context = canvas->getGrContext();
452if (!context) {
453 return;
454}
Cary Clarkac47b882018-01-11 10:35:44 -0500455auto debugster = [](SkImage::ReleaseContext releaseContext) -> void {
456 *((int *) releaseContext) += 128;
Cary Clark0c5f5462017-12-15 11:21:51 -0500457};
458int x = 0;
459for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500460 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark61ca7c52018-01-02 11:34:14 -0500461 origin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr, debugster, &x);
Cary Clark0c5f5462017-12-15 11:21:51 -0500462 canvas->drawImage(image, x, 0);
463 x += 128;
464}
Cary Clarka560c472017-11-27 10:44:06 -0500465##
466
Cary Clark3cd22cc2017-12-01 11:49:58 -0500467#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500468
469#Method ##
470
471# ------------------------------------------------------------------------------
472
473#Method static sk_sp<SkImage> MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> data,
474 bool buildMips,
Brian Osman584b5012018-04-13 15:48:26 -0400475 SkColorSpace* dstColorSpace,
476 bool limitToMaxTextureSize = false)
Cary Clark4855f782018-02-06 09:41:53 -0500477#In Constructor
478#Line # creates Image from encoded data, and uploads to GPU ##
Cary Clarka560c472017-11-27 10:44:06 -0500479
Cary Clark682c58d2018-05-16 07:07:07 -0400480Creates Image from encoded data. Image is uploaded to GPU back-end using context.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500481
482Created Image is available to other GPU contexts, and is available across thread
Cary Clark682c58d2018-05-16 07:07:07 -0400483boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
Cary Clark3cd22cc2017-12-01 11:49:58 -0500484share resources.
485
486When Image is no longer referenced, context releases texture memory
Cary Clarka560c472017-11-27 10:44:06 -0500487asynchronously.
Cary Clarka560c472017-11-27 10:44:06 -0500488
Cary Clark3cd22cc2017-12-01 11:49:58 -0500489Texture decoded from data is uploaded to match Surface created with
490dstColorSpace. Color_Space of Image is determined by encoded data.
Cary Clarka560c472017-11-27 10:44:06 -0500491
Cary Clark3cd22cc2017-12-01 11:49:58 -0500492Image is returned if format of data is recognized and supported, and if context
493supports moving resources. Recognized formats vary by platform and GPU back-end.
494
Cary Clark61ca7c52018-01-02 11:34:14 -0500495Image is returned using MakeFromEncoded if context is nullptr or does not support
496moving resources between contexts.
497
Cary Clark3cd22cc2017-12-01 11:49:58 -0500498#Param context GPU_Context ##
499#Param data Image to decode ##
500#Param buildMips create Image as Mip_Map if true ##
501#Param dstColorSpace range of colors of matching Surface on GPU ##
Brian Osman584b5012018-04-13 15:48:26 -0400502#Param limitToMaxTextureSize downscale image to GPU maximum texture size, if necessary ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500503
504#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500505
506#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500507#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500508#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500509GrContext* context = canvas->getGrContext();
510sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, 100);
511sk_sp<SkImage> image = SkImage::MakeCrossContextFromEncoded(context,
512 encodedData, false, nullptr);
513canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500514##
515
Cary Clark3cd22cc2017-12-01 11:49:58 -0500516#SeeAlso MakeCrossContextFromPixmap
517
518#Method ##
519
520# ------------------------------------------------------------------------------
521
522#Method static sk_sp<SkImage> MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap,
523 bool buildMips,
Brian Osman584b5012018-04-13 15:48:26 -0400524 SkColorSpace* dstColorSpace,
525 bool limitToMaxTextureSize = false)
Cary Clark4855f782018-02-06 09:41:53 -0500526#In Constructor
527#Line # creates Image from Pixmap, and uploads to GPU ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500528
Cary Clark682c58d2018-05-16 07:07:07 -0400529Creates Image from pixmap. Image is uploaded to GPU back-end using context.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500530
531Created Image is available to other GPU contexts, and is available across thread
Cary Clark682c58d2018-05-16 07:07:07 -0400532boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
Cary Clark3cd22cc2017-12-01 11:49:58 -0500533share resources.
534
535When Image is no longer referenced, context releases texture memory
536asynchronously.
537
538Texture created from pixmap is uploaded to match Surface created with
539dstColorSpace. Color_Space of Image is determined by pixmap.colorSpace().
540
Cary Clark682c58d2018-05-16 07:07:07 -0400541Image is returned referring to GPU back-end if context is not nullptr,
Cary Clark61ca7c52018-01-02 11:34:14 -0500542format of data is recognized and supported, and if context supports moving
543resources between contexts. Otherwise, pixmap pixel data is copied and Image
544as returned in raster format if possible; nullptr may be returned.
545Recognized GPU formats vary by platform and GPU back-end.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500546
547#Param context GPU_Context ##
548#Param pixmap Image_Info, pixel address, and row bytes ##
549#Param buildMips create Image as Mip_Map if true ##
550#Param dstColorSpace range of colors of matching Surface on GPU ##
Brian Osman584b5012018-04-13 15:48:26 -0400551#Param limitToMaxTextureSize downscale image to GPU maximum texture size, if necessary ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500552
553#Return created Image, or nullptr ##
554
555#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500556#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500557#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500558GrContext* context = canvas->getGrContext();
559SkPixmap pixmap;
560if (source.peekPixels(&pixmap)) {
561 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(context, pixmap,
562 false, nullptr);
563 canvas->drawImage(image, 0, 0);
564}
Cary Clark3cd22cc2017-12-01 11:49:58 -0500565##
566
567#SeeAlso MakeCrossContextFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500568
569#Method ##
570
571# ------------------------------------------------------------------------------
572
573#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
574 const GrBackendTexture& backendTexture,
575 GrSurfaceOrigin surfaceOrigin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500576 SkColorType colorType,
577 SkAlphaType alphaType = kPremul_SkAlphaType,
578 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500579#In Constructor
580#Line # creates Image from GPU_Texture, managed internally ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500581Creates Image from backendTexture associated with context. backendTexture and
582returned Image are managed internally, and are released when no longer needed.
Cary Clarka560c472017-11-27 10:44:06 -0500583
Cary Clark3cd22cc2017-12-01 11:49:58 -0500584Image is returned if format of backendTexture is recognized and supported.
585Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500586
Cary Clark3cd22cc2017-12-01 11:49:58 -0500587#Param context GPU_Context ##
588#Param backendTexture texture residing on GPU ##
589#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500590#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500591##
Cary Clark681287e2018-03-16 11:34:15 -0400592#Param alphaType one of: #list_of_alpha_types#
Cary Clark3cd22cc2017-12-01 11:49:58 -0500593##
Cary Clark61ca7c52018-01-02 11:34:14 -0500594#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500595
596#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500597
598#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500599#Image 5
600#Platform gpu
Cary Clark61ca7c52018-01-02 11:34:14 -0500601 if (!canvas->getGrContext()) {
602 return;
603 }
604 canvas->scale(.5f, .5f);
605 canvas->clear(0x7f3f5f7f);
606 int x = 0, y = 0;
607 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
608 for (auto alpha : { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType } ) {
609 sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(canvas->getGrContext(),
Cary Clark682c58d2018-05-16 07:07:07 -0400610 backEndTexture, origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500611 kRGBA_8888_SkColorType, alpha);
612 canvas->drawImage(image, x, y);
613 x += 160;
614 }
615 x -= 160 * 3;
616 y += 256;
617 }
Cary Clarka560c472017-11-27 10:44:06 -0500618##
619
Cary Clark61ca7c52018-01-02 11:34:14 -0500620#SeeAlso MakeFromTexture MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500621
622#Method ##
623
624# ------------------------------------------------------------------------------
625
626#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400627 const GrBackendTexture yuvTextures[3],
Cary Clarka560c472017-11-27 10:44:06 -0500628 GrSurfaceOrigin surfaceOrigin,
629 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500630#In Constructor
631#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500632
Brian Salomon6a426c12018-03-15 12:16:02 -0400633Creates Image from copy of yuvTextures, an array of textures on GPU.
634yuvTextures contain pixels for YUV planes of Image. Returned Image has the dimensions
635yuvTextures[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500636
Cary Clark61ca7c52018-01-02 11:34:14 -0500637#Param context GPU_Context ##
638#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
639 kRec709_SkYUVColorSpace
640##
Brian Salomon6a426c12018-03-15 12:16:02 -0400641#Param yuvTextures array of YUV textures on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500642#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
643#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500644
Cary Clark61ca7c52018-01-02 11:34:14 -0500645#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500646
Cary Clark61ca7c52018-01-02 11:34:14 -0500647# seems too complicated to create an example for this
648#ToDo
649should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500650##
651
Cary Clark61ca7c52018-01-02 11:34:14 -0500652#NoExample
653##
654
655#SeeAlso MakeFromNV12TexturesCopy
656
657#Method ##
658
659# ------------------------------------------------------------------------------
660
Cary Clarka560c472017-11-27 10:44:06 -0500661#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
662 SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400663 const GrBackendTexture nv12Textures[2],
Cary Clarka560c472017-11-27 10:44:06 -0500664 GrSurfaceOrigin surfaceOrigin,
665 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500666#In Constructor
Brian Salomon6a426c12018-03-15 12:16:02 -0400667#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500668
Cary Clark681287e2018-03-16 11:34:15 -0400669Creates Image from copy of nv12Textures, an array of textures on GPU.
Brian Salomon6a426c12018-03-15 12:16:02 -0400670nv12Textures[0] contains pixels for YUV_Component_Y plane.
671nv12Textures[1] contains pixels for YUV_Component_U plane,
Cary Clark61ca7c52018-01-02 11:34:14 -0500672followed by pixels for YUV_Component_V plane.
Cary Clark681287e2018-03-16 11:34:15 -0400673Returned Image has the dimensions nv12Textures[2].
674yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500675
Cary Clark61ca7c52018-01-02 11:34:14 -0500676#Param context GPU_Context ##
677#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
678 kRec709_SkYUVColorSpace
679##
Brian Salomon6a426c12018-03-15 12:16:02 -0400680#Param nv12Textures array of YUV textures on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500681#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
682#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500683
Cary Clark61ca7c52018-01-02 11:34:14 -0500684#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500685
Cary Clark61ca7c52018-01-02 11:34:14 -0500686# seems too complicated to create an example for this
687#ToDo
688should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500689##
690
Cary Clark61ca7c52018-01-02 11:34:14 -0500691#NoExample
692##
693
694#SeeAlso MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500695
696#Method ##
697
698# ------------------------------------------------------------------------------
699
Cary Clark4855f782018-02-06 09:41:53 -0500700# currently uncalled by any test or client ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500701#Bug 7424
Cary Clark61ca7c52018-01-02 11:34:14 -0500702
Cary Clark56356312018-02-08 14:45:18 -0500703#EnumClass BitDepth
Cary Clark682c58d2018-05-16 07:07:07 -0400704#Line # options for MakeFromPicture ##
Cary Clarka560c472017-11-27 10:44:06 -0500705#Code
Cary Clark61ca7c52018-01-02 11:34:14 -0500706 enum class BitDepth {
Cary Clarka560c472017-11-27 10:44:06 -0500707 kU8,
708 kF16,
709 };
710##
711
712#Const kU8 0
Cary Clark682c58d2018-05-16 07:07:07 -0400713#Line # uses 8-bit unsigned int per Color component ##
Cary Clarkffb3d682018-05-17 12:17:28 -0400714Use 8 bits per ARGB component using unsigned integer format.
Cary Clarka560c472017-11-27 10:44:06 -0500715##
716#Const kF16 1
Cary Clark682c58d2018-05-16 07:07:07 -0400717#Line # uses 16-bit float per Color component ##
Cary Clarkffb3d682018-05-17 12:17:28 -0400718Use 16 bits per ARGB component using half-precision floating point format.
Cary Clarka560c472017-11-27 10:44:06 -0500719##
720
Cary Clark61ca7c52018-01-02 11:34:14 -0500721#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500722##
723
Cary Clark61ca7c52018-01-02 11:34:14 -0500724#SeeAlso MakeFromPicture
Cary Clarka560c472017-11-27 10:44:06 -0500725
Cary Clark56356312018-02-08 14:45:18 -0500726#EnumClass ##
Cary Clarka560c472017-11-27 10:44:06 -0500727
728# ------------------------------------------------------------------------------
729
730#Method static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
731 const SkMatrix* matrix, const SkPaint* paint,
732 BitDepth bitDepth,
733 sk_sp<SkColorSpace> colorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500734#In Constructor
735#Line # creates Image from Picture ##
Cary Clarka560c472017-11-27 10:44:06 -0500736
Cary Clark61ca7c52018-01-02 11:34:14 -0500737Creates Image from picture. Returned Image width and height are set by dimensions.
738Image draws picture with matrix and paint, set to bitDepth and colorSpace.
Cary Clarka560c472017-11-27 10:44:06 -0500739
Cary Clark61ca7c52018-01-02 11:34:14 -0500740If matrix is nullptr, draws with identity Matrix. If paint is nullptr, draws
Cary Clark682c58d2018-05-16 07:07:07 -0400741with default Paint. colorSpace may be nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500742
Cary Clark61ca7c52018-01-02 11:34:14 -0500743#Param picture stream of drawing commands ##
744#Param dimensions width and height ##
745#Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ##
746#Param paint Paint to apply transparency, filtering, and so on; may be nullptr ##
Cary Clark682c58d2018-05-16 07:07:07 -0400747#Param bitDepth 8-bit integer or 16-bit float: per component ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500748#Param colorSpace range of colors; may be nullptr ##
749
750#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500751
752#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500753 SkPaint paint;
754 SkPictureRecorder recorder;
755 SkCanvas* recordingCanvas = recorder.beginRecording(50, 50);
756 for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) {
757 paint.setColor(color);
758 recordingCanvas->drawRect({10, 10, 30, 40}, paint);
759 recordingCanvas->translate(10, 10);
760 recordingCanvas->scale(1.2f, 1.4f);
761 }
762 sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
763 int x = 0, y = 0;
764 for (auto alpha : { 70, 140, 210 } ) {
765 paint.setAlpha(alpha);
766 auto srgbColorSpace = SkColorSpace::MakeSRGB();
767 sk_sp<SkImage> image = SkImage::MakeFromPicture(playback, {50, 50}, nullptr, &paint,
768 SkImage::BitDepth::kU8, srgbColorSpace);
769 canvas->drawImage(image, x, y);
770 x += 70; y += 70;
771 }
Cary Clarka560c472017-11-27 10:44:06 -0500772##
773
Cary Clark61ca7c52018-01-02 11:34:14 -0500774#SeeAlso SkCanvas::drawPicture
Cary Clarka560c472017-11-27 10:44:06 -0500775
776#Method ##
777
778# ------------------------------------------------------------------------------
779
Cary Clark9548ea92018-09-13 15:26:33 -0400780#Method static sk_sp<SkImage> MakeFromAHardwareBuffer(
781 AHardwareBuffer* hardwareBuffer,
782 SkAlphaType alphaType = kPremul_SkAlphaType,
783 sk_sp<SkColorSpace> colorSpace = nullptr,
784 GrSurfaceOrigin surfaceOrigin = kTopLeft_GrSurfaceOrigin)
Cary Clark4855f782018-02-06 09:41:53 -0500785#In Constructor
786#Line # creates Image from Android hardware buffer ##
Cary Clarka560c472017-11-27 10:44:06 -0500787
Cary Clark4855f782018-02-06 09:41:53 -0500788#Bug 7447
Cary Clarka560c472017-11-27 10:44:06 -0500789
Cary Clark61ca7c52018-01-02 11:34:14 -0500790Creates Image from Android hardware buffer.
791Returned Image takes a reference on the buffer.
Cary Clarka560c472017-11-27 10:44:06 -0500792
Cary Clark61ca7c52018-01-02 11:34:14 -0500793Only available on Android, when __ANDROID_API__ is defined to be 26 or greater.
Cary Clarka560c472017-11-27 10:44:06 -0500794
Cary Clark61ca7c52018-01-02 11:34:14 -0500795#Param hardwareBuffer AHardwareBuffer Android hardware buffer ##
Cary Clark681287e2018-03-16 11:34:15 -0400796#Param alphaType one of: #list_of_alpha_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500797##
798#Param colorSpace range of colors; may be nullptr ##
Cary Clark9548ea92018-09-13 15:26:33 -0400799#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clarka560c472017-11-27 10:44:06 -0500800
Cary Clark61ca7c52018-01-02 11:34:14 -0500801#Return created Image, or nullptr ##
802
803#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500804##
805
Cary Clark61ca7c52018-01-02 11:34:14 -0500806#SeeAlso MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -0500807
808#Method ##
809
810# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -0500811#Subtopic Property
812#Populate
813#Line # values and attributes ##
814##
Cary Clarka560c472017-11-27 10:44:06 -0500815
816#Method int width() const
Cary Clark4855f782018-02-06 09:41:53 -0500817#In Property
818#Line # returns pixel column count ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500819Returns pixel count in each row.
820
821#Return pixel width in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500822
823#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500824#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500825#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500826 canvas->translate(10, 10);
827 canvas->drawImage(image, 0, 0);
828 canvas->translate(0, image->height());
829 SkPaint paint;
830 paint.setTextAlign(SkPaint::kCenter_Align);
831 canvas->drawLine(0, 10, image->width(), 10, paint);
832 canvas->drawString("width", image->width() / 2, 25, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500833##
834
Cary Clark61ca7c52018-01-02 11:34:14 -0500835#SeeAlso dimensions() height()
Cary Clarka560c472017-11-27 10:44:06 -0500836
837#Method ##
838
839# ------------------------------------------------------------------------------
840
841#Method int height() const
Cary Clark4855f782018-02-06 09:41:53 -0500842#In Property
843#Line # returns pixel row count ##
Cary Clark2f466242017-12-11 16:03:17 -0500844Returns pixel row count.
845
Cary Clark61ca7c52018-01-02 11:34:14 -0500846#Return pixel height in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500847
848#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500849#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500850#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500851 canvas->translate(10, 10);
852 canvas->drawImage(image, 0, 0);
853 canvas->translate(image->width(), 0);
854 SkPaint paint;
855 paint.setTextAlign(SkPaint::kCenter_Align);
856 paint.setVerticalText(true);
857 canvas->drawLine(10, 0, 10, image->height(), paint);
Cary Clarkac47b882018-01-11 10:35:44 -0500858 canvas->drawString("height", 25, image->height() / 2, paint);
859##
Cary Clarka560c472017-11-27 10:44:06 -0500860
Cary Clark61ca7c52018-01-02 11:34:14 -0500861#SeeAlso dimensions() width()
Cary Clarka560c472017-11-27 10:44:06 -0500862
863#Method ##
864
865# ------------------------------------------------------------------------------
866
867#Method SkISize dimensions() const
Cary Clark4855f782018-02-06 09:41:53 -0500868#In Property
869#Line # returns width() and height() ##
Cary Clark681287e2018-03-16 11:34:15 -0400870
Cary Clark2f466242017-12-11 16:03:17 -0500871Returns ISize { width(), height() }.
872
873#Return integral size of width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -0500874
875#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500876#Image 4
877 SkISize dimensions = image->dimensions();
878 SkIRect bounds = image->bounds();
879 SkIRect dimensionsAsBounds = SkIRect::MakeSize(dimensions);
880 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clark681287e2018-03-16 11:34:15 -0400881#StdOut
882dimensionsAsBounds == bounds
883##
Cary Clarka560c472017-11-27 10:44:06 -0500884##
885
Cary Clark61ca7c52018-01-02 11:34:14 -0500886#SeeAlso height() width() bounds()
Cary Clarka560c472017-11-27 10:44:06 -0500887
888#Method ##
889
890# ------------------------------------------------------------------------------
891
892#Method SkIRect bounds() const
Cary Clark4855f782018-02-06 09:41:53 -0500893#In Property
894#Line # returns width() and height() as Rectangle ##
Cary Clark2f466242017-12-11 16:03:17 -0500895Returns IRect { 0, 0, width(), height() }.
896
897#Return integral rectangle from origin to width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -0500898
899#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500900#Height 128
901#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -0500902 SkIRect bounds = image->bounds();
Cary Clarkac47b882018-01-11 10:35:44 -0500903 for (int x : { 0, bounds.width() } ) {
904 for (int y : { 0, bounds.height() } ) {
Cary Clark61ca7c52018-01-02 11:34:14 -0500905 canvas->drawImage(image, x, y);
906 }
907 }
Cary Clarka560c472017-11-27 10:44:06 -0500908##
909
Cary Clark682c58d2018-05-16 07:07:07 -0400910#SeeAlso dimensions()
Cary Clarka560c472017-11-27 10:44:06 -0500911
912#Method ##
913
914# ------------------------------------------------------------------------------
915
916#Method uint32_t uniqueID() const
Cary Clark4855f782018-02-06 09:41:53 -0500917#In Property
Cary Clark682c58d2018-05-16 07:07:07 -0400918#Line # returns identifier for Image ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500919Returns value unique to image. Image contents cannot change after Image is
920created. Any operation to create a new Image will receive generate a new
921unique number.
922
923#Return unique identifier ##
Cary Clarka560c472017-11-27 10:44:06 -0500924
925#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500926#Image 5
927#Height 156
928 sk_sp<SkImage> subset = image->makeSubset({10, 20, 90, 100});
929 canvas->drawImage(image, 0, 0);
930 canvas->drawImage(subset, 128, 0);
931 SkPaint paint;
932 SkString s;
933 s.printf("original id: %d", image->uniqueID());
934 canvas->drawString(s, 20, image->height() + 20, paint);
935 s.printf("subset id: %d", subset->uniqueID());
936 canvas->drawString(s, 148, subset->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500937##
938
Cary Clark61ca7c52018-01-02 11:34:14 -0500939#SeeAlso isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -0500940
941#Method ##
942
943# ------------------------------------------------------------------------------
944
945#Method SkAlphaType alphaType() const
Cary Clark4855f782018-02-06 09:41:53 -0500946#In Property
947#Line # returns Alpha_Type ##
Cary Clark681287e2018-03-16 11:34:15 -0400948Returns Alpha_Type, one of: #list_of_alpha_types#.
Cary Clark61ca7c52018-01-02 11:34:14 -0500949
950Alpha_Type returned was a parameter to an Image constructor,
951or was parsed from encoded data.
952
953#Return Alpha_Type in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500954
955#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500956#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500957#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500958 const char* alphaTypeStr[] = { "Unknown", "Opaque", "Premul", "Unpremul" };
959 SkAlphaType alphaType = image->alphaType();
Cary Clarkac47b882018-01-11 10:35:44 -0500960 canvas->drawImage(image, 16, 0);
Cary Clarkffb3d682018-05-17 12:17:28 -0400961 canvas->drawString(alphaTypeStr[(int) alphaType], 20, image->height() + 20, SkPaint());
Cary Clarka560c472017-11-27 10:44:06 -0500962##
963
Cary Clark61ca7c52018-01-02 11:34:14 -0500964#SeeAlso SkImageInfo::alphaType
Cary Clarka560c472017-11-27 10:44:06 -0500965
966#Method ##
967
968# ------------------------------------------------------------------------------
969
Greg Daniel56008aa2018-03-14 15:33:42 -0400970#Method SkColorType colorType() const
971#In Property
972#Line # returns Color_Type ##
973
974Returns Color_Type if known; otherwise, returns kUnknown_SkColorType.
975
976#Return Color_Type of Image ##
977
978#Example
Cary Clarkffb3d682018-05-17 12:17:28 -0400979#Image 4
980#Height 96
981 const char* colors[] = { "Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
982 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16" };
983 SkColorType colorType = image->colorType();
984 canvas->drawImage(image, 16, 0);
985 canvas->drawString(colors[(int) colorType], 20, image->height() + 20, SkPaint());
Greg Daniel56008aa2018-03-14 15:33:42 -0400986##
987
988#SeeAlso SkImageInfo::colorType
989
990#Method ##
991
992# ------------------------------------------------------------------------------
993
Cary Clarka560c472017-11-27 10:44:06 -0500994#Method SkColorSpace* colorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -0500995#In Property
996#Line # returns Color_Space ##
Cary Clark2f466242017-12-11 16:03:17 -0500997Returns Color_Space, the range of colors, associated with Image. The
998reference count of Color_Space is unchanged. The returned Color_Space is
999immutable.
Cary Clarka560c472017-11-27 10:44:06 -05001000
Cary Clark61dfc3a2018-01-03 08:37:53 -05001001Color_Space returned was passed to an Image constructor,
1002or was parsed from encoded data. Color_Space returned may be ignored when Image
1003is drawn, depending on the capabilities of the Surface receiving the drawing.
Cary Clark2f466242017-12-11 16:03:17 -05001004
1005#Return Color_Space in Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001006
1007#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001008#Image 3
1009#Set sRGB
1010 SkPixmap pixmap;
1011 source.peekPixels(&pixmap);
1012 canvas->scale(.25f, .25f);
1013 int y = 0;
1014 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1015 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1016 int x = 0;
1017 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1018 for (int index = 0; index < 2; ++index) {
1019 pixmap.setColorSpace(colorSpace);
1020 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1021 canvas->drawImage(image, x, y);
1022 colorSpace = image->colorSpace()->makeColorSpin();
1023 x += 512;
1024 }
1025 y += 512;
1026 }
Cary Clarka560c472017-11-27 10:44:06 -05001027##
1028
Cary Clark61dfc3a2018-01-03 08:37:53 -05001029#SeeAlso refColorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001030
1031#Method ##
1032
1033# ------------------------------------------------------------------------------
1034
1035#Method sk_sp<SkColorSpace> refColorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001036#In Property
1037#Line # returns Image_Info Color_Space ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001038Returns a smart pointer to Color_Space, the range of colors, associated with
1039Image. The smart pointer tracks the number of objects sharing this
1040SkColorSpace reference so the memory is released when the owners destruct.
1041
1042The returned SkColorSpace is immutable.
1043
1044Color_Space returned was passed to an Image constructor,
1045or was parsed from encoded data. Color_Space returned may be ignored when Image
1046is drawn, depending on the capabilities of the Surface receiving the drawing.
1047
1048#Return Color_Space in Image, or nullptr, wrapped in a smart pointer ##
Cary Clarka560c472017-11-27 10:44:06 -05001049
1050#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001051#Image 3
1052#Set sRGB
1053 SkPixmap pixmap;
1054 source.peekPixels(&pixmap);
1055 canvas->scale(.25f, .25f);
1056 int y = 0;
1057 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1058 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1059 int x = 0;
1060 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1061 for (int index = 0; index < 2; ++index) {
1062 pixmap.setColorSpace(colorSpace);
1063 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1064 canvas->drawImage(image, x, y);
1065 colorSpace = image->refColorSpace()->makeColorSpin();
1066 x += 512;
1067 }
1068 y += 512;
1069 }
Cary Clarka560c472017-11-27 10:44:06 -05001070##
1071
Cary Clark61dfc3a2018-01-03 08:37:53 -05001072#SeeAlso colorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001073
1074#Method ##
1075
1076# ------------------------------------------------------------------------------
1077
1078#Method bool isAlphaOnly() const
Cary Clark4855f782018-02-06 09:41:53 -05001079#In Property
1080#Line # returns if pixels represent a transparency mask ##
Cary Clark2f466242017-12-11 16:03:17 -05001081Returns true if Image pixels represent transparency only. If true, each pixel
1082is packed in 8 bits as defined by kAlpha_8_SkColorType.
Cary Clarka560c472017-11-27 10:44:06 -05001083
Cary Clark2f466242017-12-11 16:03:17 -05001084#Return true if pixels represent a transparency mask ##
Cary Clarka560c472017-11-27 10:44:06 -05001085
1086#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001087 uint8_t pmColors = 0;
1088 sk_sp<SkImage> image = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1), &pmColors, 1});
1089 SkDebugf("alphaOnly = %s\n", image->isAlphaOnly() ? "true" : "false");
1090#StdOut
1091alphaOnly = true
1092##
Cary Clarka560c472017-11-27 10:44:06 -05001093##
1094
Cary Clark61dfc3a2018-01-03 08:37:53 -05001095#SeeAlso alphaType isOpaque
Cary Clarka560c472017-11-27 10:44:06 -05001096
1097#Method ##
1098
1099# ------------------------------------------------------------------------------
1100
1101#Method bool isOpaque() const
Cary Clark4855f782018-02-06 09:41:53 -05001102#In Property
1103#Line # returns if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001104Returns true if pixels ignore their Alpha value and are treated as fully opaque.
Cary Clark2f466242017-12-11 16:03:17 -05001105
1106#Return true if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clarka560c472017-11-27 10:44:06 -05001107
1108#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001109 auto check_isopaque = [](const SkImageInfo& imageInfo) -> void {
1110 auto surface(SkSurface::MakeRaster(imageInfo));
1111 auto image(surface->makeImageSnapshot());
1112 SkDebugf("isOpaque = %s\n", image->isOpaque() ? "true" : "false");
1113 };
1114
1115 check_isopaque(SkImageInfo::MakeN32Premul(5, 5));
1116 check_isopaque(SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType));
1117#StdOut
1118isOpaque = false
1119isOpaque = true
1120##
Cary Clarka560c472017-11-27 10:44:06 -05001121##
1122
Cary Clark61dfc3a2018-01-03 08:37:53 -05001123#SeeAlso alphaType isAlphaOnly
Cary Clarka560c472017-11-27 10:44:06 -05001124
1125#Method ##
1126
1127# ------------------------------------------------------------------------------
1128
1129#Method sk_sp<SkShader> makeShader(SkShader::TileMode tileMode1, SkShader::TileMode tileMode2,
1130 const SkMatrix* localMatrix = nullptr) const
Cary Clark4855f782018-02-06 09:41:53 -05001131#In Constructor
1132#Line # creates Shader, Paint element that can tile Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001133
Cary Clark61dfc3a2018-01-03 08:37:53 -05001134Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1135SkShader::TileMode rules to fill drawn area outside Image. localMatrix permits
1136transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001137
Cary Clark5538c132018-06-14 12:28:14 -04001138#Param tileMode1 tiling on x-axis, one of: SkShader::kClamp_TileMode,
1139 SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
Cary Clark61dfc3a2018-01-03 08:37:53 -05001140##
Cary Clark5538c132018-06-14 12:28:14 -04001141#Param tileMode2 tiling on y-axis, one of: SkShader::kClamp_TileMode,
1142 SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
Cary Clark61dfc3a2018-01-03 08:37:53 -05001143##
1144#Param localMatrix Image transformation, or nullptr ##
1145
1146#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001147
1148#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001149#Image 4
1150SkMatrix matrix;
1151matrix.setRotate(45);
1152SkPaint paint;
1153paint.setShader(image->makeShader(SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode,
1154 &matrix));
1155canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001156##
1157
Cary Clark61dfc3a2018-01-03 08:37:53 -05001158#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001159
1160#Method ##
1161
1162# ------------------------------------------------------------------------------
1163
1164#Method sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const
1165
Cary Clark61dfc3a2018-01-03 08:37:53 -05001166Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1167SkShader::kClamp_TileMode to fill drawn area outside Image. localMatrix permits
1168transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001169
Cary Clark61dfc3a2018-01-03 08:37:53 -05001170#Param localMatrix Image transformation, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001171
Cary Clark61dfc3a2018-01-03 08:37:53 -05001172#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001173
1174#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001175#Image 5
1176SkMatrix matrix;
1177matrix.setRotate(45);
1178matrix.postTranslate(125, 30);
1179SkPaint paint;
1180paint.setShader(image->makeShader(&matrix));
1181canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001182##
1183
Cary Clarkf5404bb2018-01-05 12:10:09 -05001184#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001185
1186#Method ##
1187
1188# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001189#Subtopic Pixels
1190#Populate
1191#Line # read and write pixel values ##
1192##
Cary Clarka560c472017-11-27 10:44:06 -05001193
1194#Method bool peekPixels(SkPixmap* pixmap) const
Cary Clark78de7512018-02-07 07:27:09 -05001195#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001196#Line # returns Pixmap if possible ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001197Copies Image pixel address, row bytes, and Image_Info to pixmap, if address
1198is available, and returns true. If pixel address is not available, return
1199false and leave pixmap unchanged.
Cary Clarka560c472017-11-27 10:44:06 -05001200
Cary Clarkf5404bb2018-01-05 12:10:09 -05001201#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
Cary Clarka560c472017-11-27 10:44:06 -05001202
Cary Clarkf5404bb2018-01-05 12:10:09 -05001203#Return true if Image has direct access to pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001204
1205#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001206 SkBitmap bitmap;
1207 bitmap.allocPixels(SkImageInfo::MakeN32Premul(12, 11));
1208 SkCanvas offscreen(bitmap);
1209 offscreen.clear(SK_ColorWHITE);
1210 SkPaint paint;
1211 offscreen.drawString("%", 1, 10, paint);
1212 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
1213 SkPixmap pixmap;
1214 if (image->peekPixels(&pixmap)) {
1215 const SkPMColor* pixels = pixmap.addr32();
1216 SkPMColor pmWhite = pixels[0];
1217 for (int y = 0; y < image->height(); ++y) {
1218 for (int x = 0; x < image->width(); ++x) {
1219 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
1220 }
1221 SkDebugf("\n");
1222 }
1223 }
1224#StdOut
1225------------
1226--xx----x---
1227-x--x--x----
1228-x--x--x----
1229-x--x-x-----
1230--xx-xx-xx--
1231-----x-x--x-
1232----x--x--x-
1233----x--x--x-
1234---x----xx--
1235------------
1236##
Cary Clarka560c472017-11-27 10:44:06 -05001237##
1238
Cary Clarkf5404bb2018-01-05 12:10:09 -05001239#SeeAlso readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001240
1241#Method ##
1242
1243# ------------------------------------------------------------------------------
1244
1245#Method GrTexture* getTexture() const
Cary Clark682c58d2018-05-16 07:07:07 -04001246#Deprecated
Cary Clarka560c472017-11-27 10:44:06 -05001247#Method ##
1248
1249# ------------------------------------------------------------------------------
1250
1251#Method bool isTextureBacked() const
Cary Clark78de7512018-02-07 07:27:09 -05001252#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001253#Line # returns if Image was created from GPU_Texture ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001254Returns true the contents of Image was created on or uploaded to GPU memory,
1255and is available as a GPU_Texture.
Cary Clarka560c472017-11-27 10:44:06 -05001256
Cary Clarkf5404bb2018-01-05 12:10:09 -05001257#Return true if Image is a GPU_Texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001258
1259#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001260#Image 5
1261#Platform gpu
1262auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1263 if (nullptr == image) {
1264 return;
1265 }
1266 SkPaint paint;
1267 paint.setAntiAlias(true);
1268 paint.setTextAlign(SkPaint::kCenter_Align);
1269 canvas->drawImage(image, 0, 0);
1270 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1271 canvas->drawString(image->isTextureBacked() ? "is GPU texture" : "not GPU texture",
1272 image->width() / 2, image->height() * 3 / 4, paint);
1273};
1274sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1275sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001276 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1277 kOpaque_SkAlphaType, nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05001278drawImage(image, "image");
1279canvas->translate(image->width(), 0);
1280drawImage(bitmapImage, "source");
1281canvas->translate(-image->width(), image->height());
1282drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001283##
1284
Cary Clarkf5404bb2018-01-05 12:10:09 -05001285#SeeAlso MakeFromTexture isValid
Cary Clarka560c472017-11-27 10:44:06 -05001286
1287#Method ##
1288
1289# ------------------------------------------------------------------------------
1290
1291#Method bool isValid(GrContext* context) const
Cary Clark4855f782018-02-06 09:41:53 -05001292#In Property
1293#Line # returns if Image can draw to Raster_Surface or GPU_Context ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001294Returns true if Image can be drawn on either Raster_Surface or GPU_Surface.
1295If context is nullptr, tests if Image draws on Raster_Surface;
1296otherwise, tests if Image draws on GPU_Surface associated with context.
Cary Clarka560c472017-11-27 10:44:06 -05001297
Cary Clarkf5404bb2018-01-05 12:10:09 -05001298Image backed by GPU_Texture may become invalid if associated GrContext is
1299invalid. Lazy_Image may be invalid and may not draw to Raster_Surface or
1300GPU_Surface or both.
Cary Clarka560c472017-11-27 10:44:06 -05001301
Cary Clark61ca7c52018-01-02 11:34:14 -05001302#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05001303
Cary Clarkf5404bb2018-01-05 12:10:09 -05001304#Return true if Image can be drawn ##
Cary Clarka560c472017-11-27 10:44:06 -05001305
1306#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001307#Image 5
1308#Platform gpu
1309auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1310 if (nullptr == image) {
1311 return;
1312 }
1313 SkPaint paint;
1314 paint.setAntiAlias(true);
1315 paint.setTextAlign(SkPaint::kCenter_Align);
1316 canvas->drawImage(image, 0, 0);
1317 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1318 if (canvas->getGrContext()) {
1319 canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" :
1320 "not valid on GPU", image->width() / 2, image->height() * 5 / 8, paint);
1321 }
1322 canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" :
1323 "not valid on CPU", image->width() / 2, image->height() * 7 / 8, paint);
1324};
1325sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1326sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001327 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1328 kOpaque_SkAlphaType, nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05001329drawImage(image, "image");
1330canvas->translate(image->width(), 0);
1331drawImage(bitmapImage, "source");
1332canvas->translate(-image->width(), image->height());
1333drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001334##
1335
Cary Clarkf5404bb2018-01-05 12:10:09 -05001336#SeeAlso isTextureBacked isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001337
1338#Method ##
1339
1340# ------------------------------------------------------------------------------
1341
Robert Phillipsc5509952018-04-04 15:54:55 -04001342#Method GrBackendTexture getBackendTexture(bool flushPendingGrContextIO,
1343 GrSurfaceOrigin* origin = nullptr) const
1344#In Property
1345#Line # returns GPU reference to Image as texture ##
1346
Cary Clark682c58d2018-05-16 07:07:07 -04001347Retrieves the back-end texture. If Image has no back-end texture, an invalid
Cary Clarkba75aee2018-04-05 08:18:41 -04001348object is returned. Call GrBackendTexture::isValid to determine if the result
1349is valid.
1350
1351If flushPendingGrContextIO is true, completes deferred I/O operations.
Robert Phillipsc5509952018-04-04 15:54:55 -04001352
1353If origin in not nullptr, copies location of content drawn into Image.
1354
1355#Param flushPendingGrContextIO flag to flush outstanding requests ##
1356#Param origin storage for one of: kTopLeft_GrSurfaceOrigin,
1357 kBottomLeft_GrSurfaceOrigin; or nullptr
1358##
1359
Cary Clarkba75aee2018-04-05 08:18:41 -04001360#Return back-end API texture handle; invalid on failure ##
Robert Phillipsc5509952018-04-04 15:54:55 -04001361
Cary Clarkba75aee2018-04-05 08:18:41 -04001362#Example
1363#Image 3
1364#Platform gpu
Brian Osman584b5012018-04-13 15:48:26 -04001365 GrContext* grContext = canvas->getGrContext();
1366 if (!grContext) {
1367 canvas->drawString("GPU only!", 20, 40, SkPaint());
1368 return;
1369 }
1370 sk_sp<SkImage> imageFromBackend = SkImage::MakeFromAdoptedTexture(grContext, backEndTexture,
1371 kBottomLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1372 GrBackendTexture textureFromImage = imageFromBackend->getBackendTexture(false);
1373 if (!textureFromImage.isValid()) {
1374 return;
1375 }
1376 sk_sp<SkImage> imageFromTexture = SkImage::MakeFromAdoptedTexture(grContext, textureFromImage,
1377 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1378 canvas->drawImage(imageFromTexture, 0, 0);
Cary Clarkba75aee2018-04-05 08:18:41 -04001379 canvas->drawImage(imageFromBackend, 128, 128);
Robert Phillipsc5509952018-04-04 15:54:55 -04001380##
1381
1382#SeeAlso MakeFromTexture isTextureBacked
1383
1384#Method ##
1385
1386# ------------------------------------------------------------------------------
1387
Cary Clarka560c472017-11-27 10:44:06 -05001388#Enum CachingHint
Cary Clark682c58d2018-05-16 07:07:07 -04001389#Line # options for readPixels and scalePixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001390#Code
1391 enum CachingHint {
1392 kAllow_CachingHint,
1393 kDisallow_CachingHint,
1394 };
1395##
1396
Cary Clarkac47b882018-01-11 10:35:44 -05001397CachingHint selects whether Skia may internally cache Bitmaps generated by
1398decoding Image, or by copying Image from GPU to CPU. The default behavior
Cary Clark682c58d2018-05-16 07:07:07 -04001399allows caching Bitmaps.
Cary Clarkac47b882018-01-11 10:35:44 -05001400
1401Choose kDisallow_CachingHint if Image pixels are to be used only once, or
1402if Image pixels reside in a cache outside of Skia, or to reduce memory pressure.
1403
1404Choosing kAllow_CachingHint does not ensure that pixels will be cached.
1405Image pixels may not be cached if memory requirements are too large or
1406pixels are not accessible.
Cary Clarka560c472017-11-27 10:44:06 -05001407
1408#Const kAllow_CachingHint 0
Cary Clark682c58d2018-05-16 07:07:07 -04001409#Line # allows internally caching decoded and copied pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001410##
1411#Const kDisallow_CachingHint 1
Cary Clark682c58d2018-05-16 07:07:07 -04001412#Line # disallows internally caching decoded and copied pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001413##
1414
Cary Clarkac47b882018-01-11 10:35:44 -05001415#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001416##
1417
Cary Clarkac47b882018-01-11 10:35:44 -05001418#SeeAlso readPixels scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001419
1420#Enum ##
1421
1422# ------------------------------------------------------------------------------
1423
1424#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1425 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001426#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001427#Line # copies and converts pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001428
Cary Clarkac47b882018-01-11 10:35:44 -05001429Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY),
Cary Clark682c58d2018-05-16 07:07:07 -04001430and does not exceed Image (width(), height()).
Cary Clarkac47b882018-01-11 10:35:44 -05001431
1432dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
1433destination. dstRowBytes specifics the gap from one destination row to the next.
1434Returns true if pixels are copied. Returns false if:
1435#List
1436# dstInfo.addr() equals nullptr ##
1437# dstRowBytes is less than dstInfo.minRowBytes ##
1438# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001439##
1440
Cary Clarkac47b882018-01-11 10:35:44 -05001441Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1442kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1443If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace must match.
1444If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType must
1445match. If Image Color_Space is nullptr, dstInfo.colorSpace must match. Returns
1446false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001447
Cary Clarkac47b882018-01-11 10:35:44 -05001448srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark682c58d2018-05-16 07:07:07 -04001449false if width() or height() is zero or negative.
Cary Clark2be81cf2018-09-13 12:04:30 -04001450Returns false if #Formula # abs(srcX) >= Image width() ##, or if #Formula # abs(srcY) >= Image height() ##.
Cary Clarka560c472017-11-27 10:44:06 -05001451
Cary Clarkac47b882018-01-11 10:35:44 -05001452If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1453If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1454
1455#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1456#Param dstPixels destination pixel storage ##
1457#Param dstRowBytes destination row length ##
1458#Param srcX column index whose absolute value is less than width() ##
1459#Param srcY row index whose absolute value is less than height() ##
1460#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1461
1462#Return true if pixels are copied to dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001463
1464#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001465#Image 3
1466 canvas->scale(.5f, .5f);
1467 const int width = 32;
1468 const int height = 32;
1469 std::vector<int32_t> dstPixels;
1470 dstPixels.resize(height * width * 4);
1471 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1472 for (int y = 0; y < 512; y += height ) {
1473 for (int x = 0; x < 512; x += width ) {
1474 if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) {
1475 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
1476 SkBitmap bitmap;
1477 bitmap.installPixels(dstPixmap);
1478 canvas->drawBitmap(bitmap, 0, 0);
1479 }
1480 canvas->translate(48, 0);
1481 }
1482 canvas->translate(-16 * 48, 48);
1483 }
Cary Clarka560c472017-11-27 10:44:06 -05001484##
1485
Cary Clarkac47b882018-01-11 10:35:44 -05001486#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001487
1488#Method ##
1489
1490# ------------------------------------------------------------------------------
1491
1492#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY,
1493 CachingHint cachingHint = kAllow_CachingHint) const
1494
Cary Clarkac47b882018-01-11 10:35:44 -05001495Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and
Cary Clark682c58d2018-05-16 07:07:07 -04001496does not exceed Image (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001497
Cary Clarkac47b882018-01-11 10:35:44 -05001498dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
1499and row bytes of destination. dst.rowBytes specifics the gap from one destination
1500row to the next. Returns true if pixels are copied. Returns false if:
1501#List
1502# dst pixel storage equals nullptr ##
1503# dst.rowBytes is less than SkImageInfo::minRowBytes ##
1504# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001505##
1506
Cary Clarkac47b882018-01-11 10:35:44 -05001507Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1508kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1509If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1510If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1511match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1512false if pixel conversion is not possible.
Cary Clark682c58d2018-05-16 07:07:07 -04001513
Cary Clarkac47b882018-01-11 10:35:44 -05001514srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark682c58d2018-05-16 07:07:07 -04001515false if width() or height() is zero or negative.
Cary Clark2be81cf2018-09-13 12:04:30 -04001516Returns false if #Formula # abs(srcX) >= Image width() ##, or if #Formula # abs(srcY) >= Image height() ##.
Cary Clarkac47b882018-01-11 10:35:44 -05001517
1518If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1519If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1520
1521#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
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 dst ##
1527
1528#Example
1529#Image 3
1530 std::vector<int32_t> srcPixels;
1531 int rowBytes = image->width() * 4;
1532 int quarterWidth = image->width() / 4;
1533 int quarterHeight = image->height() / 4;
1534 srcPixels.resize(image->height() * rowBytes);
1535 for (int y = 0; y < 4; ++y) {
1536 for (int x = 0; x < 4; ++x) {
1537 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1538 &srcPixels.front() + x * image->height() * quarterWidth +
1539 y * quarterWidth, rowBytes);
1540 image->readPixels(pixmap, x * quarterWidth, y * quarterHeight);
1541 }
1542 }
1543 canvas->scale(.5f, .5f);
1544 SkBitmap bitmap;
1545 bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()),
1546 &srcPixels.front(), rowBytes);
1547 canvas->drawBitmap(bitmap, 0, 0);
1548##
1549
1550#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 scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality,
1557 CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001558#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001559#Line # scales and converts one Image to another ##
Cary Clarka560c472017-11-27 10:44:06 -05001560
Cary Clarkac47b882018-01-11 10:35:44 -05001561Copies Image to dst, scaling pixels to fit dst.width() and dst.height(), and
1562converting pixels to match dst.colorType and dst.alphaType. Returns true if
1563pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1564less than dst SkImageInfo::minRowBytes.
Cary Clarka560c472017-11-27 10:44:06 -05001565
Cary Clarkac47b882018-01-11 10:35:44 -05001566Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1567kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1568If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1569If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1570match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1571false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001572
Cary Clarkac47b882018-01-11 10:35:44 -05001573Scales the image, with filterQuality, to match dst.width() and dst.height().
1574filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1575Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1576Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1577Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1578kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1579
1580If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1581If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1582
1583#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1584#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1585 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1586##
1587#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1588
1589#Return true if pixels are scaled to fit dst ##
Cary Clarka560c472017-11-27 10:44:06 -05001590
1591#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001592#Image 3
1593#Height 128
1594 std::vector<int32_t> srcPixels;
1595 int quarterWidth = image->width() / 16;
1596 int rowBytes = quarterWidth * 4;
1597 int quarterHeight = image->height() / 16;
1598 srcPixels.resize(quarterHeight * rowBytes);
1599 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1600 &srcPixels.front(), rowBytes);
1601 canvas->scale(4, 4);
1602 SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality,
1603 kMedium_SkFilterQuality, kHigh_SkFilterQuality };
1604 for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) {
1605 image->scalePixels(pixmap, qualities[index]);
1606 sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
1607 canvas->drawImage(filtered, 16 * index, 0);
1608 }
Cary Clarka560c472017-11-27 10:44:06 -05001609##
1610
Cary Clarkac47b882018-01-11 10:35:44 -05001611#SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001612
1613#Method ##
1614
1615# ------------------------------------------------------------------------------
1616
1617#Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const
Cary Clark78de7512018-02-07 07:27:09 -05001618#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001619#Line # returns encoded Image as SkData ##
Cary Clark682c58d2018-05-16 07:07:07 -04001620Encodes Image pixels, returning result as SkData.
Cary Clark2f466242017-12-11 16:03:17 -05001621
Cary Clarkac47b882018-01-11 10:35:44 -05001622Returns nullptr if encoding fails, or if encodedImageFormat is not supported.
Cary Clarka560c472017-11-27 10:44:06 -05001623
Cary Clarkac47b882018-01-11 10:35:44 -05001624Image encoding in a format requires both building with one or more of:
1625SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support
1626for the encoded format.
1627
1628If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can
1629additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP,
1630SkEncodedImageFormat::kGIF.
1631
1632quality is a platform and format specific metric trading off size and encoding
1633error. When used, quality equaling 100 encodes with the least error. quality may
1634be ignored by the encoder.
1635
1636#Param encodedImageFormat one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
1637 SkEncodedImageFormat::kWEBP
1638 ##
1639#Param quality encoder specific metric with 100 equaling best ##
Cary Clarka560c472017-11-27 10:44:06 -05001640
Cary Clark2f466242017-12-11 16:03:17 -05001641#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001642
1643#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001644#Image 3
1645 canvas->scale(4, 4);
1646 SkIRect subset = {0, 0, 16, 64};
1647 int x = 0;
1648 for (int quality : { 0, 10, 50, 100 } ) {
1649 sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
1650 sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
1651 canvas->drawImage(filtered, x, 0);
1652 x += 16;
1653 }
Cary Clarka560c472017-11-27 10:44:06 -05001654##
1655
Cary Clarkac47b882018-01-11 10:35:44 -05001656#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001657
1658#Method ##
1659
1660# ------------------------------------------------------------------------------
1661
Cary Clark61ca7c52018-01-02 11:34:14 -05001662#Method sk_sp<SkData> encodeToData() const
Cary Clarka560c472017-11-27 10:44:06 -05001663
Cary Clarkac47b882018-01-11 10:35:44 -05001664Encodes Image pixels, returning result as SkData. Returns existing encoded data
1665if present; otherwise, Image is encoded with SkEncodedImageFormat::kPNG. Skia
1666must be built with SK_HAS_PNG_LIBRARY to encode Image.
Cary Clarka560c472017-11-27 10:44:06 -05001667
Cary Clark682c58d2018-05-16 07:07:07 -04001668Returns nullptr if existing encoded data is missing or invalid, and
Cary Clarka560c472017-11-27 10:44:06 -05001669encoding fails.
1670
Cary Clarkac47b882018-01-11 10:35:44 -05001671#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001672
1673#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001674#Image 3
1675 canvas->scale(4, 4);
1676 SkIRect subset = {136, 32, 200, 96};
1677 sk_sp<SkData> data(image->encodeToData());
1678 sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
1679 canvas->drawImage(eye, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001680##
1681
Cary Clarkac47b882018-01-11 10:35:44 -05001682#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001683
1684#Method ##
1685
1686# ------------------------------------------------------------------------------
1687
1688#Method sk_sp<SkData> refEncodedData() const
Cary Clark78de7512018-02-07 07:27:09 -05001689#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001690#Line # returns Image encoded in SkData if present ##
Cary Clarkac47b882018-01-11 10:35:44 -05001691Returns encoded Image pixels as SkData, if Image was created from supported
1692encoded stream format. Platform support for formats vary and may require building
1693with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY.
Cary Clarka560c472017-11-27 10:44:06 -05001694
Cary Clarkac47b882018-01-11 10:35:44 -05001695Returns nullptr if Image contents are not encoded.
Cary Clarka560c472017-11-27 10:44:06 -05001696
Cary Clarkac47b882018-01-11 10:35:44 -05001697#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001698
1699#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001700#Image 3
1701#Platform gpu
1702 struct {
1703 const char* name;
1704 sk_sp<SkImage> image;
1705 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1706 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001707 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1708 kOpaque_SkAlphaType, nullptr) } };
Cary Clarkac47b882018-01-11 10:35:44 -05001709 SkString string;
1710 SkPaint paint;
1711 for (const auto& test : tests ) {
1712 if (!test.image) {
1713 string.printf("no %s", test.name);
1714 } else {
1715 string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name);
1716 }
1717 canvas->drawString(string, 10, 20, paint);
1718 canvas->translate(0, 20);
1719 }
Cary Clarka560c472017-11-27 10:44:06 -05001720##
1721
Cary Clarkac47b882018-01-11 10:35:44 -05001722#SeeAlso encodeToData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001723
1724#Method ##
1725
1726# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001727#Subtopic Utility
1728#Populate
1729#Line # rarely called management functions ##
1730##
Cary Clarka560c472017-11-27 10:44:06 -05001731
Cary Clarka560c472017-11-27 10:44:06 -05001732#Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const
Cary Clark4855f782018-02-06 09:41:53 -05001733#In Constructor
1734#Line # creates Image containing part of original ##
Cary Clarkac47b882018-01-11 10:35:44 -05001735Returns subset of Image. subset must be fully contained by Image dimensions().
1736The implementation may share pixels, or may copy them.
Cary Clarka560c472017-11-27 10:44:06 -05001737
Cary Clarkac47b882018-01-11 10:35:44 -05001738Returns nullptr if subset is empty, or subset is not contained by bounds, or
1739pixels in Image could not be read or copied.
Cary Clarka560c472017-11-27 10:44:06 -05001740
Cary Clarkac47b882018-01-11 10:35:44 -05001741#Param subset bounds of returned Image ##
1742
1743#Return partial or full 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(.5f, .5f);
Cary Clarkc30382f2018-07-24 08:09:27 -04001748 const int width = 64;
1749 const int height = 64;
Cary Clarkac47b882018-01-11 10:35:44 -05001750 for (int y = 0; y < 512; y += height ) {
1751 for (int x = 0; x < 512; x += width ) {
1752 sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height}));
1753 canvas->drawImage(subset, x * 3 / 2, y * 3 / 2);
1754 }
1755 }
Cary Clarka560c472017-11-27 10:44:06 -05001756##
1757
Cary Clarkac47b882018-01-11 10:35:44 -05001758#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001759
1760#Method ##
1761
1762# ------------------------------------------------------------------------------
1763
Greg Daniel5f4b09d2018-06-12 16:39:59 -04001764#Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace,
1765 GrMipMapped mipMapped = GrMipMapped::kNo) const
Cary Clark4855f782018-02-06 09:41:53 -05001766#In Constructor
1767#Line # creates Image matching Color_Space if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001768Returns Image backed by GPU_Texture associated with context. Returned Image is
Cary Clark5538c132018-06-14 12:28:14 -04001769compatible with Surface created with dstColorSpace. The returned Image respects
1770mipMapped setting; if mipMapped equals GrMipMapped::kYes, the backing texture
1771allocates Mip_Map levels. Returns original Image if context
1772and dstColorSpace match and mipMapped is compatible with backing GPU_Texture.
Cary Clarkac47b882018-01-11 10:35:44 -05001773
1774Returns nullptr if context is nullptr, or if Image was created with another
1775GrContext.
Cary Clarka560c472017-11-27 10:44:06 -05001776
Cary Clark61ca7c52018-01-02 11:34:14 -05001777#Param context GPU_Context ##
Cary Clarkac47b882018-01-11 10:35:44 -05001778#Param dstColorSpace range of colors of matching Surface on GPU ##
Cary Clark5538c132018-06-14 12:28:14 -04001779#Param mipMapped whether created Image texture must allocate Mip_Map levels ##
Cary Clarka560c472017-11-27 10:44:06 -05001780
Cary Clarkac47b882018-01-11 10:35:44 -05001781#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001782
1783#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001784#Platform gpu
1785#Image 5
1786 auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void {
1787 if (nullptr == image || nullptr == context) {
1788 return;
1789 }
1790 SkPaint paint;
1791 paint.setAntiAlias(true);
1792 paint.setTextAlign(SkPaint::kCenter_Align);
1793 sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr));
1794 canvas->drawImage(texture, 0, 0);
1795 canvas->drawString(label, texture->width() / 2, texture->height() / 4, paint);
1796 };
1797 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1798 GrContext* context = canvas->getGrContext();
1799 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001800 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1801 kOpaque_SkAlphaType, nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001802 drawImage(image, context, "image");
1803 canvas->translate(image->width(), 0);
1804 drawImage(bitmapImage, context, "source");
1805 canvas->translate(-image->width(), image->height());
1806 drawImage(textureImage, context, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001807##
1808
Cary Clarkac47b882018-01-11 10:35:44 -05001809#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05001810
1811#Method ##
1812
1813# ------------------------------------------------------------------------------
1814
1815#Method sk_sp<SkImage> makeNonTextureImage() const
Cary Clark4855f782018-02-06 09:41:53 -05001816#In Constructor
1817#Line # creates Image without dependency on GPU_Texture ##
Cary Clarkac47b882018-01-11 10:35:44 -05001818Returns Raster_Image or Lazy_Image. Copies Image backed by GPU_Texture into
Cary Clark4855f782018-02-06 09:41:53 -05001819CPU memory if needed. Returns original Image if decoded in Raster_Bitmap,
Cary Clarkac47b882018-01-11 10:35:44 -05001820or if encoded in a stream.
Cary Clark61ca7c52018-01-02 11:34:14 -05001821
Cary Clarkac47b882018-01-11 10:35:44 -05001822Returns nullptr if backed by GPU_Texture and copy fails.
1823
1824#Return Raster_Image, Lazy_Image, or nullptr ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001825
1826#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001827#Image 5
1828#Platform gpu
1829 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1830 if (nullptr == image) {
1831 return;
1832 }
1833 SkPaint paint;
1834 paint.setAntiAlias(true);
1835 paint.setTextAlign(SkPaint::kCenter_Align);
1836 sk_sp<SkImage> nonTexture(image->makeNonTextureImage());
1837 canvas->drawImage(nonTexture, 0, 0);
1838 canvas->drawString(label, nonTexture->width() / 2, nonTexture->height() / 4, paint);
1839 };
1840 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1841 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001842 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1843 kOpaque_SkAlphaType, nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001844 drawImage(image, "image");
1845 canvas->translate(image->width(), 0);
1846 drawImage(bitmapImage, "source");
1847 canvas->translate(-image->width(), image->height());
1848 drawImage(textureImage, "backEndTexture");
Cary Clark61ca7c52018-01-02 11:34:14 -05001849##
1850
Cary Clark56356312018-02-08 14:45:18 -05001851#SeeAlso makeTextureImage makeRasterImage MakeBackendTextureFromSkImage
Cary Clark61ca7c52018-01-02 11:34:14 -05001852
1853#Method ##
1854
1855# ------------------------------------------------------------------------------
1856
1857#Method sk_sp<SkImage> makeRasterImage() const
Cary Clark4855f782018-02-06 09:41:53 -05001858#In Constructor
1859#Line # creates Image compatible with Raster_Surface if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001860Returns Raster_Image. Copies Image backed by GPU_Texture into CPU memory,
Cary Clark4855f782018-02-06 09:41:53 -05001861or decodes Image from Lazy_Image. Returns original Image if decoded in
Cary Clarkac47b882018-01-11 10:35:44 -05001862Raster_Bitmap.
Cary Clarka560c472017-11-27 10:44:06 -05001863
Cary Clarkac47b882018-01-11 10:35:44 -05001864Returns nullptr if copy, decode, or pixel read fails.
Cary Clarka560c472017-11-27 10:44:06 -05001865
Cary Clarkac47b882018-01-11 10:35:44 -05001866#Return Raster_Image, or nullptr ##
1867
Cary Clark4855f782018-02-06 09:41:53 -05001868#Bug 7479
Cary Clarka560c472017-11-27 10:44:06 -05001869#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001870#Image 5
1871#Platform gpu
1872 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1873 if (nullptr == image) {
1874 return;
1875 }
1876 SkPaint paint;
1877 paint.setAntiAlias(true);
1878 paint.setTextAlign(SkPaint::kCenter_Align);
1879 sk_sp<SkImage> raster(image->makeRasterImage());
1880 canvas->drawImage(raster, 0, 0);
1881 canvas->drawString(label, raster->width() / 2, raster->height() / 4, paint);
1882 };
1883 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1884 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001885 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1886 kOpaque_SkAlphaType, nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001887 drawImage(image, "image");
1888 canvas->translate(image->width(), 0);
1889 drawImage(bitmapImage, "source");
1890 canvas->translate(-image->width(), image->height());
1891 drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001892##
1893
Cary Clarkac47b882018-01-11 10:35:44 -05001894#SeeAlso isTextureBacked isLazyGenerated MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -05001895
1896#Method ##
1897
1898# ------------------------------------------------------------------------------
1899
1900#Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
1901 const SkIRect& clipBounds, SkIRect* outSubset,
1902 SkIPoint* offset) const
Cary Clark4855f782018-02-06 09:41:53 -05001903#In Constructor
1904#Line # creates filtered, clipped Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001905
Cary Clarkac47b882018-01-11 10:35:44 -05001906Creates filtered Image. filter processes original Image, potentially changing
1907color, position, and size. subset is the bounds of original Image processed
1908by filter. clipBounds is the expected bounds of the filtered Image. outSubset
1909is required storage for the actual bounds of the filtered Image. offset is
1910required storage for translation of returned Image.
Cary Clarka560c472017-11-27 10:44:06 -05001911
Cary Clarkac47b882018-01-11 10:35:44 -05001912Returns nullptr if Image could not be created. If nullptr is returned, outSubset
1913and offset are undefined.
1914
Cary Clark56356312018-02-08 14:45:18 -05001915Useful for animation of SkImageFilter that varies size from frame to frame.
1916Returned Image is created larger than required by filter so that GPU_Texture
1917can be reused with different sized effects. outSubset describes the valid bounds
1918of GPU_Texture returned. offset translates the returned Image to keep subsequent
1919animation frames aligned with respect to each other.
Cary Clarkac47b882018-01-11 10:35:44 -05001920
1921#Param filter how Image is sampled when transformed ##
Cary Clark56356312018-02-08 14:45:18 -05001922#Param subset bounds of Image processed by filter ##
1923#Param clipBounds expected bounds of filtered Image ##
1924#Param outSubset storage for returned Image bounds ##
1925#Param offset storage for returned Image translation ##
Cary Clarka560c472017-11-27 10:44:06 -05001926
Cary Clarkac47b882018-01-11 10:35:44 -05001927#Return filtered Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001928
1929#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001930#Description
1931In each frame of the animation, filtered Image is drawn in a different location.
1932By translating canvas by returned offset, Image appears stationary.
1933##
1934#Image 5
1935#Platform gpu
1936#Duration 5
1937 sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make(
1938 -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE,
1939 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
1940 nullptr);
1941 sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr);
1942 SkIRect subset = image->bounds();
1943 SkIRect clipBounds = image->bounds();
1944 clipBounds.outset(60, 60);
1945 SkIRect outSubset;
1946 SkIPoint offset;
1947 sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds,
1948 &outSubset, &offset));
1949 SkPaint paint;
1950 paint.setAntiAlias(true);
1951 paint.setStyle(SkPaint::kStroke_Style);
1952 canvas->drawLine(0, 0, offset.fX, offset.fY, paint);
1953 canvas->translate(offset.fX, offset.fY);
1954 canvas->drawImage(filtered, 0, 0);
Cary Clark681287e2018-03-16 11:34:15 -04001955 canvas->drawRect(SkRect::Make(outSubset), paint);
Cary Clarka560c472017-11-27 10:44:06 -05001956##
1957
Cary Clark56356312018-02-08 14:45:18 -05001958#SeeAlso makeShader SkPaint::setImageFilter
Cary Clarka560c472017-11-27 10:44:06 -05001959
1960#Method ##
1961
1962# ------------------------------------------------------------------------------
1963
Cary Clarka560c472017-11-27 10:44:06 -05001964#Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc
Cary Clark682c58d2018-05-16 07:07:07 -04001965#Line # parameter type for MakeBackendTextureFromSkImage ##
Cary Clarkffb3d682018-05-17 12:17:28 -04001966
1967#Code
1968typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc;
1969##
1970
Cary Clark0d225392018-06-07 09:59:07 -04001971Defines a callback function, taking one parameter of type GrBackendTexture with
1972no return value. Function is called when back-end texture is to be released.
Cary Clarka560c472017-11-27 10:44:06 -05001973##
1974
1975# ------------------------------------------------------------------------------
1976
1977#Method static bool MakeBackendTextureFromSkImage(GrContext* context,
1978 sk_sp<SkImage> image,
1979 GrBackendTexture* backendTexture,
1980 BackendTextureReleaseProc* backendTextureReleaseProc)
Cary Clark4855f782018-02-06 09:41:53 -05001981#In Constructor
1982#Line # creates GPU_Texture from Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001983
Cary Clark56356312018-02-08 14:45:18 -05001984Creates a GrBackendTexture from the provided SkImage. Returns true and
1985stores result in backendTexture and backendTextureReleaseProc if
1986texture is created; otherwise, returns false and leaves
1987backendTexture and backendTextureReleaseProc unmodified.
Cary Clarka560c472017-11-27 10:44:06 -05001988
Cary Clark56356312018-02-08 14:45:18 -05001989Call backendTextureReleaseProc after deleting backendTexture.
1990backendTextureReleaseProc cleans up auxiliary data related to returned
1991backendTexture. The caller must delete returned backendTexture after use.
Cary Clarka560c472017-11-27 10:44:06 -05001992
Cary Clark56356312018-02-08 14:45:18 -05001993If Image is both texture backed and singly referenced, image is returned in
1994backendTexture without conversion or making a copy. Image is singly referenced
1995if its was transferred solely using std::move().
1996
1997If Image is not texture backed, returns texture with Image contents.
Cary Clarka560c472017-11-27 10:44:06 -05001998
Cary Clark61ca7c52018-01-02 11:34:14 -05001999#Param context GPU_Context ##
Cary Clark56356312018-02-08 14:45:18 -05002000#Param image Image used for texture ##
Cary Clark682c58d2018-05-16 07:07:07 -04002001#Param backendTexture storage for back-end texture ##
Cary Clark56356312018-02-08 14:45:18 -05002002#Param backendTextureReleaseProc storage for clean up function ##
Cary Clarka560c472017-11-27 10:44:06 -05002003
Cary Clark682c58d2018-05-16 07:07:07 -04002004#Return true if back-end texture was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002005
2006#Example
Cary Clark56356312018-02-08 14:45:18 -05002007#Platform gpu
2008#Height 64
2009#Function
Brian Salomon67f85842018-02-09 08:50:22 -05002010static sk_sp<SkImage> create_gpu_image(GrContext* grContext) {
2011 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
2012 auto surface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info));
2013 SkCanvas* canvas = surface->getCanvas();
2014 canvas->clear(SK_ColorWHITE);
2015 SkPaint paint;
2016 paint.setColor(SK_ColorBLACK);
2017 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
2018 return surface->makeImageSnapshot();
2019}
2020##
2021
Cary Clark682c58d2018-05-16 07:07:07 -04002022void draw(SkCanvas* canvas) {
Brian Salomon67f85842018-02-09 08:50:22 -05002023 GrContext* grContext = canvas->getGrContext();
2024 if (!grContext) {
2025 return;
2026 }
2027 sk_sp<SkImage> backEndImage = create_gpu_image(grContext);
2028 canvas->drawImage(backEndImage, 0, 0);
2029 GrBackendTexture texture;
2030 SkImage::BackendTextureReleaseProc proc;
2031 if (!SkImage::MakeBackendTextureFromSkImage(grContext, std::move(backEndImage),
2032 &texture, &proc)) {
2033 return;
2034 }
2035 sk_sp<SkImage> i2 = SkImage::MakeFromTexture(grContext, texture, kTopLeft_GrSurfaceOrigin,
2036 kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
2037 canvas->drawImage(i2, 30, 30);
Cary Clark56356312018-02-08 14:45:18 -05002038}
Cary Clarka560c472017-11-27 10:44:06 -05002039##
2040
Cary Clark56356312018-02-08 14:45:18 -05002041#SeeAlso MakeFromTexture makeTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002042
2043#Method ##
2044
2045# ------------------------------------------------------------------------------
2046
2047#Enum LegacyBitmapMode
Cary Clark56356312018-02-08 14:45:18 -05002048#Deprecated soon
Cary Clarka560c472017-11-27 10:44:06 -05002049#Code
2050 enum LegacyBitmapMode {
2051 kRO_LegacyBitmapMode,
Cary Clarka560c472017-11-27 10:44:06 -05002052 };
2053##
2054
Cary Clarka560c472017-11-27 10:44:06 -05002055#Const kRO_LegacyBitmapMode 0
Cary Clark682c58d2018-05-16 07:07:07 -04002056#Line # returned bitmap is read-only and immutable ##
Cary Clarka560c472017-11-27 10:44:06 -05002057##
Cary Clarka560c472017-11-27 10:44:06 -05002058
2059#Enum ##
2060
2061# ------------------------------------------------------------------------------
2062
Cary Clark56356312018-02-08 14:45:18 -05002063#Method bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode = kRO_LegacyBitmapMode) const
Cary Clark4855f782018-02-06 09:41:53 -05002064#In Constructor
2065#Line # returns as Raster_Bitmap ##
Cary Clarkac47b882018-01-11 10:35:44 -05002066Creates raster Bitmap with same pixels as Image. If legacyBitmapMode is
2067kRO_LegacyBitmapMode, returned bitmap is read-only and immutable.
2068Returns true if Bitmap is stored in bitmap. Returns false and resets bitmap if
2069Bitmap write did not succeed.
Cary Clarka560c472017-11-27 10:44:06 -05002070
Cary Clark3cd22cc2017-12-01 11:49:58 -05002071#Param bitmap storage for legacy Bitmap ##
Cary Clark56356312018-02-08 14:45:18 -05002072#Param legacyBitmapMode to be deprecated ##
Cary Clarka560c472017-11-27 10:44:06 -05002073
Cary Clark3cd22cc2017-12-01 11:49:58 -05002074#Return true if Bitmap was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002075
Cary Clarkae957c42018-06-07 17:07:17 -04002076#Example
Cary Clark56356312018-02-08 14:45:18 -05002077#Image 4
2078#Platform gpu
Brian Salomon67f85842018-02-09 08:50:22 -05002079 SkBitmap bitImage;
2080 if (image->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2081 canvas->drawBitmap(bitImage, 0, 0);
2082 }
2083 GrContext* grContext = canvas->getGrContext();
2084 if (!grContext) {
2085 return;
2086 }
2087 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(grContext, backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04002088 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
2089 kOpaque_SkAlphaType, nullptr));
Brian Salomon67f85842018-02-09 08:50:22 -05002090 canvas->drawImage(textureImage, 45, 45);
2091 if (textureImage->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2092 canvas->drawBitmap(bitImage, 90, 90);
2093 }
Cary Clarka560c472017-11-27 10:44:06 -05002094##
2095
Cary Clark56356312018-02-08 14:45:18 -05002096#SeeAlso MakeRasterData makeRasterImage makeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002097
2098#Method ##
2099
2100# ------------------------------------------------------------------------------
2101
2102#Method bool isLazyGenerated() const
Cary Clark4855f782018-02-06 09:41:53 -05002103#In Property
2104#Line # returns if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002105Returns true if Image is backed by an image-generator or other service that creates
2106and caches its pixels or texture on-demand.
2107
Cary Clark2f466242017-12-11 16:03:17 -05002108#Return true if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002109
2110#Example
Cary Clark2f466242017-12-11 16:03:17 -05002111#Height 80
2112#Function
2113class TestImageGenerator : public SkImageGenerator {
2114public:
2115 TestImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(10, 10)) {}
2116 ~TestImageGenerator() override {}
2117protected:
2118 bool onGetPixels(const SkImageInfo& info, void* pixelPtr, size_t rowBytes,
2119 const Options& options) override {
2120 SkPMColor* pixels = static_cast<SkPMColor*>(pixelPtr);
2121 for (int y = 0; y < info.height(); ++y) {
2122 for (int x = 0; x < info.width(); ++x) {
2123 pixels[y * info.width() + x] = 0xff223344 + y * 0x000C0811;
2124 }
2125 }
2126 return true;
2127 }
2128};
2129##
2130void draw(SkCanvas* canvas) {
2131 auto gen = std::unique_ptr<TestImageGenerator>(new TestImageGenerator());
2132 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
2133 SkString lazy(image->isLazyGenerated() ? "is lazy" : "not lazy");
2134 canvas->scale(8, 8);
2135 canvas->drawImage(image, 0, 0, nullptr);
2136 SkPaint paint;
2137 paint.setTextSize(4);
2138 canvas->drawString(lazy, 2, 5, paint);
2139}
Cary Clarka560c472017-11-27 10:44:06 -05002140##
2141
Cary Clarkf5404bb2018-01-05 12:10:09 -05002142#Example
2143#Image 5
2144#Platform gpu
2145void draw(SkCanvas* canvas) {
2146 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2147 if (nullptr == image) {
2148 return;
2149 }
2150 SkPaint paint;
2151 paint.setAntiAlias(true);
2152 paint.setTextAlign(SkPaint::kCenter_Align);
2153 canvas->drawImage(image, 0, 0);
2154 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
2155 canvas->drawString(
2156 image->isLazyGenerated() ? "is lazily generated" : "not lazily generated",
2157 image->width() / 2, image->height() * 3 / 4, paint);
2158 };
2159 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2160 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04002161 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
2162 kOpaque_SkAlphaType, nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05002163 drawImage(image, "image");
2164 canvas->translate(image->width(), 0);
2165 drawImage(bitmapImage, "source");
2166 canvas->translate(-image->width(), image->height());
2167 drawImage(textureImage, "backEndTexture");
2168}
2169##
2170
Cary Clarkac47b882018-01-11 10:35:44 -05002171#SeeAlso isTextureBacked MakeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002172
2173#Method ##
2174
2175# ------------------------------------------------------------------------------
2176
Cary Clarke80cd442018-07-17 13:19:56 -04002177#Method sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target) const
Cary Clark4855f782018-02-06 09:41:53 -05002178#In Constructor
2179#Line # creates Image matching Color_Space if possible ##
Cary Clarka560c472017-11-27 10:44:06 -05002180
Cary Clarkac47b882018-01-11 10:35:44 -05002181Creates Image in target Color_Space.
2182Returns nullptr if Image could not be created.
Cary Clarka560c472017-11-27 10:44:06 -05002183
Cary Clarkac47b882018-01-11 10:35:44 -05002184Returns original Image if it is in target Color_Space.
2185Otherwise, converts pixels from Image Color_Space to target Color_Space.
2186If Image colorSpace returns nullptr, Image Color_Space is assumed to be sRGB.
2187
Cary Clarkac47b882018-01-11 10:35:44 -05002188#Param target Color_Space describing color range of returned Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002189
Cary Clarkac47b882018-01-11 10:35:44 -05002190#Return created Image in target Color_Space ##
2191
2192#Example
2193#Image 5
2194#Set sRGB
2195 sk_sp<SkColorSpace> normalColorSpace = SkColorSpace::MakeRGB(
2196 SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kSRGB_Gamut);
2197 sk_sp<SkColorSpace> wackyColorSpace = normalColorSpace->makeColorSpin();
2198 for (auto colorSpace : { normalColorSpace, wackyColorSpace } ) {
Cary Clarkc3c1c312018-07-18 09:25:15 -04002199 sk_sp<SkImage> colorSpaced = image->makeColorSpace(colorSpace);
2200 canvas->drawImage(colorSpaced, 0, 0);
2201 canvas->translate(128, 0);
Cary Clarkac47b882018-01-11 10:35:44 -05002202 }
2203##
2204
2205#SeeAlso MakeFromPixture MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05002206
2207#Method ##
2208
2209#Class SkImage ##
2210
2211#Topic Image ##