blob: 35261dc303d2abb8165ffb6c29e744d9c4c26096 [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 {
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400456 // broken
Cary Clark08417bc2018-10-03 10:44:13 -0400457 // *((int *) releaseContext) += 128;
Cary Clark0c5f5462017-12-15 11:21:51 -0500458};
459int x = 0;
460for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500461 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark61ca7c52018-01-02 11:34:14 -0500462 origin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr, debugster, &x);
Cary Clark0c5f5462017-12-15 11:21:51 -0500463 canvas->drawImage(image, x, 0);
464 x += 128;
465}
Cary Clarka560c472017-11-27 10:44:06 -0500466##
467
Cary Clark3cd22cc2017-12-01 11:49:58 -0500468#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500469
470#Method ##
471
472# ------------------------------------------------------------------------------
473
474#Method static sk_sp<SkImage> MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> data,
475 bool buildMips,
Brian Osman584b5012018-04-13 15:48:26 -0400476 SkColorSpace* dstColorSpace,
477 bool limitToMaxTextureSize = false)
Cary Clark4855f782018-02-06 09:41:53 -0500478#In Constructor
479#Line # creates Image from encoded data, and uploads to GPU ##
Cary Clarka560c472017-11-27 10:44:06 -0500480
Cary Clark682c58d2018-05-16 07:07:07 -0400481Creates Image from encoded data. Image is uploaded to GPU back-end using context.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500482
483Created Image is available to other GPU contexts, and is available across thread
Cary Clark682c58d2018-05-16 07:07:07 -0400484boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
Cary Clark3cd22cc2017-12-01 11:49:58 -0500485share resources.
486
487When Image is no longer referenced, context releases texture memory
Cary Clarka560c472017-11-27 10:44:06 -0500488asynchronously.
Cary Clarka560c472017-11-27 10:44:06 -0500489
Cary Clark3cd22cc2017-12-01 11:49:58 -0500490Texture decoded from data is uploaded to match Surface created with
491dstColorSpace. Color_Space of Image is determined by encoded data.
Cary Clarka560c472017-11-27 10:44:06 -0500492
Cary Clark3cd22cc2017-12-01 11:49:58 -0500493Image is returned if format of data is recognized and supported, and if context
494supports moving resources. Recognized formats vary by platform and GPU back-end.
495
Cary Clark61ca7c52018-01-02 11:34:14 -0500496Image is returned using MakeFromEncoded if context is nullptr or does not support
497moving resources between contexts.
498
Cary Clark3cd22cc2017-12-01 11:49:58 -0500499#Param context GPU_Context ##
500#Param data Image to decode ##
501#Param buildMips create Image as Mip_Map if true ##
502#Param dstColorSpace range of colors of matching Surface on GPU ##
Brian Osman584b5012018-04-13 15:48:26 -0400503#Param limitToMaxTextureSize downscale image to GPU maximum texture size, if necessary ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500504
505#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500506
507#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500508#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500509#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500510GrContext* context = canvas->getGrContext();
511sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, 100);
512sk_sp<SkImage> image = SkImage::MakeCrossContextFromEncoded(context,
513 encodedData, false, nullptr);
514canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500515##
516
Cary Clark3cd22cc2017-12-01 11:49:58 -0500517#SeeAlso MakeCrossContextFromPixmap
518
519#Method ##
520
521# ------------------------------------------------------------------------------
522
523#Method static sk_sp<SkImage> MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap,
524 bool buildMips,
Brian Osman584b5012018-04-13 15:48:26 -0400525 SkColorSpace* dstColorSpace,
526 bool limitToMaxTextureSize = false)
Cary Clark4855f782018-02-06 09:41:53 -0500527#In Constructor
528#Line # creates Image from Pixmap, and uploads to GPU ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500529
Cary Clark682c58d2018-05-16 07:07:07 -0400530Creates Image from pixmap. Image is uploaded to GPU back-end using context.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500531
532Created Image is available to other GPU contexts, and is available across thread
Cary Clark682c58d2018-05-16 07:07:07 -0400533boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
Cary Clark3cd22cc2017-12-01 11:49:58 -0500534share resources.
535
536When Image is no longer referenced, context releases texture memory
537asynchronously.
538
539Texture created from pixmap is uploaded to match Surface created with
540dstColorSpace. Color_Space of Image is determined by pixmap.colorSpace().
541
Cary Clark682c58d2018-05-16 07:07:07 -0400542Image is returned referring to GPU back-end if context is not nullptr,
Cary Clark61ca7c52018-01-02 11:34:14 -0500543format of data is recognized and supported, and if context supports moving
544resources between contexts. Otherwise, pixmap pixel data is copied and Image
545as returned in raster format if possible; nullptr may be returned.
546Recognized GPU formats vary by platform and GPU back-end.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500547
548#Param context GPU_Context ##
549#Param pixmap Image_Info, pixel address, and row bytes ##
550#Param buildMips create Image as Mip_Map if true ##
551#Param dstColorSpace range of colors of matching Surface on GPU ##
Brian Osman584b5012018-04-13 15:48:26 -0400552#Param limitToMaxTextureSize downscale image to GPU maximum texture size, if necessary ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500553
554#Return created Image, or nullptr ##
555
556#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500557#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500558#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500559GrContext* context = canvas->getGrContext();
560SkPixmap pixmap;
561if (source.peekPixels(&pixmap)) {
562 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(context, pixmap,
563 false, nullptr);
564 canvas->drawImage(image, 0, 0);
565}
Cary Clark3cd22cc2017-12-01 11:49:58 -0500566##
567
568#SeeAlso MakeCrossContextFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500569
570#Method ##
571
572# ------------------------------------------------------------------------------
573
574#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
575 const GrBackendTexture& backendTexture,
576 GrSurfaceOrigin surfaceOrigin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500577 SkColorType colorType,
578 SkAlphaType alphaType = kPremul_SkAlphaType,
579 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500580#In Constructor
581#Line # creates Image from GPU_Texture, managed internally ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500582Creates Image from backendTexture associated with context. backendTexture and
583returned Image are managed internally, and are released when no longer needed.
Cary Clarka560c472017-11-27 10:44:06 -0500584
Cary Clark3cd22cc2017-12-01 11:49:58 -0500585Image is returned if format of backendTexture is recognized and supported.
586Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500587
Cary Clark3cd22cc2017-12-01 11:49:58 -0500588#Param context GPU_Context ##
589#Param backendTexture texture residing on GPU ##
590#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500591#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500592##
Cary Clark681287e2018-03-16 11:34:15 -0400593#Param alphaType one of: #list_of_alpha_types#
Cary Clark3cd22cc2017-12-01 11:49:58 -0500594##
Cary Clark61ca7c52018-01-02 11:34:14 -0500595#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500596
597#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500598
599#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500600#Image 5
601#Platform gpu
Cary Clark61ca7c52018-01-02 11:34:14 -0500602 if (!canvas->getGrContext()) {
603 return;
604 }
605 canvas->scale(.5f, .5f);
606 canvas->clear(0x7f3f5f7f);
607 int x = 0, y = 0;
608 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
609 for (auto alpha : { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType } ) {
610 sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(canvas->getGrContext(),
Cary Clark682c58d2018-05-16 07:07:07 -0400611 backEndTexture, origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500612 kRGBA_8888_SkColorType, alpha);
613 canvas->drawImage(image, x, y);
614 x += 160;
615 }
616 x -= 160 * 3;
617 y += 256;
618 }
Cary Clarka560c472017-11-27 10:44:06 -0500619##
620
Cary Clark61ca7c52018-01-02 11:34:14 -0500621#SeeAlso MakeFromTexture MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500622
623#Method ##
624
625# ------------------------------------------------------------------------------
626
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400627#Method static sk_sp<SkImage> MakeFromYUVATexturesCopy(GrContext* context,
628 SkYUVColorSpace yuvColorSpace,
629 const GrBackendTexture yuvaTextures[],
630 const SkYUVAIndex yuvaIndices[4],
631 SkISize imageSize,
632 GrSurfaceOrigin imageOrigin,
633 sk_sp<SkColorSpace> imageColorSpace = nullptr)
634#In Constructor
635#Line # creates Image from YUV_ColorSpace data ##
636Creates an SkImage by flattening the specified YUVA planes into a single, interleaved RGBA image.
637
638#Param context GPU context ##
639#Param yuvColorSpace How the YUV values are converted to RGB. One of:
640 kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
641 kRec709_SkYUVColorSpace
642##
643#Param yuvaTextures array of (up to four) YUVA textures on GPU which contain the,
644 possibly interleaved, YUVA planes
645##
646#Param yuvaIndices array indicating which texture (in 'yuvaTextures') and channel
647 (in the specified texture) maps to each of Y, U, V, and A.
648##
649#Param imageSize size of the resulting image ##
650#Param imageOrigin origin of the resulting image. One of: kBottomLeft_GrSurfaceOrigin,
651 kTopLeft_GrSurfaceOrigin
652##
653#Param imageColorSpace range of colors of the resulting image; may be nullptr ##
654
655#Return created SkImage, or nullptr ##
656
657# seems too complicated to create an example for this
658#ToDo
659should this be moved to chrome only?
660##
661
662#NoExample
663##
664
665#SeeAlso MakeFromYUVATexturesCopyWithExternalBackend
666
667#Method ##
668
669# ------------------------------------------------------------------------------
670
671#Method static sk_sp<SkImage> MakeFromYUVATexturesCopyWithExternalBackend(
672 GrContext* context,
673 SkYUVColorSpace yuvColorSpace,
674 const GrBackendTexture yuvaTextures[],
675 const SkYUVAIndex yuvaIndices[4],
676 SkISize imageSize,
677 GrSurfaceOrigin imageOrigin,
678 const GrBackendTexture& backendTexture,
679 sk_sp<SkColorSpace> imageColorSpace = nullptr)
680#In Constructor
681#Line # creates Image from planar YUV_ColorSpace, stored in texture ##
682
683Creates an SkImage by flattening the specified YUVA planes into a single, interleaved RGBA
684image. backendTexture is used to store the result of the flattening.
685
686#Param context GPU context ##
687#Param yuvColorSpace How the YUV values are converted to RGB. One of:
688 kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
689 kRec709_SkYUVColorSpace
690##
691#Param yuvaTextures array of (up to four) YUVA textures on GPU which contain the,
692 possibly interleaved, YUVA planes
693##
694#Param yuvaIndices array indicating which texture (in 'yuvaTextures') and channel
695 (in the specified texture) maps to each of Y, U, V, and A.
696##
697#Param imageSize size of the resulting image ##
698#Param imageOrigin origin of the resulting image. One of: kBottomLeft_GrSurfaceOrigin,
699 kTopLeft_GrSurfaceOrigin
700##
701#Param backendTexture the resource that stores the final pixels ##
702#Param imageColorSpace range of colors of the resulting image; may be nullptr ##
703
704#Return created SkImage, or nullptr ##
705
706# seems too complicated to create an example for this
707#ToDo
708should this be moved to chrome only?
709##
710
711#NoExample
712##
713
714#SeeAlso MakeFromYUVATexturesCopy
715
716#Method ##
717
Cary Clarka560c472017-11-27 10:44:06 -0500718#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400719 const GrBackendTexture yuvTextures[3],
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400720 GrSurfaceOrigin imageOrigin,
721 sk_sp<SkColorSpace> imageColorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500722#In Constructor
723#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500724
Brian Salomon6a426c12018-03-15 12:16:02 -0400725Creates Image from copy of yuvTextures, an array of textures on GPU.
726yuvTextures contain pixels for YUV planes of Image. Returned Image has the dimensions
727yuvTextures[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500728
Cary Clark61ca7c52018-01-02 11:34:14 -0500729#Param context GPU_Context ##
730#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
731 kRec709_SkYUVColorSpace
732##
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400733#Param yuvTextures array of YUV textures on GPU ##
734#Param imageOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
735#Param imageColorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500736
Cary Clark61ca7c52018-01-02 11:34:14 -0500737#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500738
Cary Clark61ca7c52018-01-02 11:34:14 -0500739# seems too complicated to create an example for this
740#ToDo
741should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500742##
743
Cary Clark61ca7c52018-01-02 11:34:14 -0500744#NoExample
745##
746
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400747#SeeAlso MakeFromYUVTexturesCopyWithExternalBackend MakeFromNV12TexturesCopy MakeFromYUVATexturesCopy
Cary Clarkcdc371a2018-09-18 07:31:37 -0400748
749#Method ##
750
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400751# ------------------------------------------------------------------------------
752
Cary Clarkcdc371a2018-09-18 07:31:37 -0400753#Method static sk_sp<SkImage> MakeFromYUVTexturesCopyWithExternalBackend(
754 GrContext* context, SkYUVColorSpace yuvColorSpace,
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400755 const GrBackendTexture yuvTextures[3], GrSurfaceOrigin imageOrigin,
756 const GrBackendTexture& backendTexture, sk_sp<SkColorSpace> imageColorSpace = nullptr);
Cary Clarkcdc371a2018-09-18 07:31:37 -0400757#In Constructor
758#Line # creates Image from planar YUV_ColorSpace, stored in texture ##
759
760Creates Image from copy of yuvTextures, an array of textures on GPU.
761yuvTextures contain pixels for YUV planes of Image. Returned Image has the dimensions
762yuvTextures[0] and stores pixels in backendTexture. yuvColorSpace describes how YUV colors
763convert to RGB colors.
764
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400765#Param context GPU_Context ##
766#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
767 kRec709_SkYUVColorSpace
Cary Clarkcdc371a2018-09-18 07:31:37 -0400768##
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400769#Param yuvTextures array of YUV textures on GPU ##
770#Param imageOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
771#Param backendTexture the resource that stores the final pixels ##
772#Param imageColorSpace range of colors; may be nullptr ##
Cary Clarkcdc371a2018-09-18 07:31:37 -0400773
774#Return created SkImage, or nullptr ##
775
776# seems too complicated to create an example for this
777#ToDo
778should this be moved to chrome only?
779##
780
781#NoExample
782##
783
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400784#SeeAlso MakeFromYUVTexturesCopy MakeFromNV12TexturesCopy MakeFromYUVATexturesCopyWithExternalBackend
Cary Clark61ca7c52018-01-02 11:34:14 -0500785
786#Method ##
787
788# ------------------------------------------------------------------------------
789
Cary Clarka560c472017-11-27 10:44:06 -0500790#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
791 SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400792 const GrBackendTexture nv12Textures[2],
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400793 GrSurfaceOrigin imageOrigin,
794 sk_sp<SkColorSpace> imageColorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500795#In Constructor
Brian Salomon6a426c12018-03-15 12:16:02 -0400796#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500797
Cary Clark681287e2018-03-16 11:34:15 -0400798Creates Image from copy of nv12Textures, an array of textures on GPU.
Brian Salomon6a426c12018-03-15 12:16:02 -0400799nv12Textures[0] contains pixels for YUV_Component_Y plane.
800nv12Textures[1] contains pixels for YUV_Component_U plane,
Cary Clark61ca7c52018-01-02 11:34:14 -0500801followed by pixels for YUV_Component_V plane.
Cary Clark681287e2018-03-16 11:34:15 -0400802Returned Image has the dimensions nv12Textures[2].
803yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500804
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400805#Param context GPU_Context ##
806#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
807 kRec709_SkYUVColorSpace
Cary Clark61ca7c52018-01-02 11:34:14 -0500808##
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400809#Param nv12Textures array of YUV textures on GPU ##
810#Param imageOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
811#Param imageColorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500812
Cary Clark61ca7c52018-01-02 11:34:14 -0500813#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500814
Cary Clark61ca7c52018-01-02 11:34:14 -0500815# seems too complicated to create an example for this
816#ToDo
817should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500818##
819
Cary Clark61ca7c52018-01-02 11:34:14 -0500820#NoExample
821##
822
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400823#SeeAlso MakeFromNV12TexturesCopyWithExternalBackend MakeFromYUVTexturesCopy MakeFromYUVATexturesCopy
Cary Clarkcdc371a2018-09-18 07:31:37 -0400824
825#Method ##
826
827#Method static sk_sp<SkImage> MakeFromNV12TexturesCopyWithExternalBackend(
828 GrContext* context,
829 SkYUVColorSpace yuvColorSpace,
830 const GrBackendTexture nv12Textures[2],
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400831 GrSurfaceOrigin imageOrigin,
832 const GrBackendTexture& backendTexture,
833 sk_sp<SkColorSpace> imageColorSpace = nullptr);
Cary Clarkcdc371a2018-09-18 07:31:37 -0400834#In Constructor
835#Line # creates Image from planar YUV_ColorSpace, stored in texture ##
836
837Creates Image from copy of nv12Textures, an array of textures on GPU.
838nv12Textures[0] contains pixels for YUV_Component_Y plane.
839nv12Textures[1] contains pixels for YUV_Component_U plane,
840followed by pixels for YUV_Component_V plane.
841Returned Image has the dimensions nv12Textures[2] and stores pixels in backendTexture.
842yuvColorSpace describes how YUV colors convert to RGB colors.
843
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400844#Param context GPU_Context ##
845#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
846 kRec709_SkYUVColorSpace
Cary Clarkcdc371a2018-09-18 07:31:37 -0400847##
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400848#Param nv12Textures array of YUV textures on GPU ##
849#Param imageOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
850#Param backendTexture the resource that stores the final pixels ##
851#Param imageColorSpace range of colors; may be nullptr ##
Cary Clarkcdc371a2018-09-18 07:31:37 -0400852
853#Return created Image, or nullptr ##
854
855# seems too complicated to create an example for this
856#ToDo
857should this be moved to chrome only?
858##
859
860#NoExample
861##
862
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400863#SeeAlso MakeFromNV12TexturesCopy MakeFromYUVTexturesCopy MakeFromYUVATexturesCopyWithExternalBackend
Cary Clarka560c472017-11-27 10:44:06 -0500864
865#Method ##
866
867# ------------------------------------------------------------------------------
868
Cary Clark4855f782018-02-06 09:41:53 -0500869# currently uncalled by any test or client ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500870#Bug 7424
Cary Clark61ca7c52018-01-02 11:34:14 -0500871
Cary Clark56356312018-02-08 14:45:18 -0500872#EnumClass BitDepth
Cary Clark682c58d2018-05-16 07:07:07 -0400873#Line # options for MakeFromPicture ##
Cary Clarka560c472017-11-27 10:44:06 -0500874#Code
Cary Clark61ca7c52018-01-02 11:34:14 -0500875 enum class BitDepth {
Cary Clarka560c472017-11-27 10:44:06 -0500876 kU8,
877 kF16,
878 };
879##
880
881#Const kU8 0
Cary Clark682c58d2018-05-16 07:07:07 -0400882#Line # uses 8-bit unsigned int per Color component ##
Cary Clarkffb3d682018-05-17 12:17:28 -0400883Use 8 bits per ARGB component using unsigned integer format.
Cary Clarka560c472017-11-27 10:44:06 -0500884##
885#Const kF16 1
Cary Clark682c58d2018-05-16 07:07:07 -0400886#Line # uses 16-bit float per Color component ##
Cary Clarkffb3d682018-05-17 12:17:28 -0400887Use 16 bits per ARGB component using half-precision floating point format.
Cary Clarka560c472017-11-27 10:44:06 -0500888##
889
Cary Clark61ca7c52018-01-02 11:34:14 -0500890#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500891##
892
Cary Clark61ca7c52018-01-02 11:34:14 -0500893#SeeAlso MakeFromPicture
Cary Clarka560c472017-11-27 10:44:06 -0500894
Cary Clark56356312018-02-08 14:45:18 -0500895#EnumClass ##
Cary Clarka560c472017-11-27 10:44:06 -0500896
897# ------------------------------------------------------------------------------
898
899#Method static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
900 const SkMatrix* matrix, const SkPaint* paint,
901 BitDepth bitDepth,
902 sk_sp<SkColorSpace> colorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500903#In Constructor
904#Line # creates Image from Picture ##
Cary Clarka560c472017-11-27 10:44:06 -0500905
Cary Clark61ca7c52018-01-02 11:34:14 -0500906Creates Image from picture. Returned Image width and height are set by dimensions.
907Image draws picture with matrix and paint, set to bitDepth and colorSpace.
Cary Clarka560c472017-11-27 10:44:06 -0500908
Cary Clark61ca7c52018-01-02 11:34:14 -0500909If matrix is nullptr, draws with identity Matrix. If paint is nullptr, draws
Cary Clark682c58d2018-05-16 07:07:07 -0400910with default Paint. colorSpace may be nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500911
Cary Clark61ca7c52018-01-02 11:34:14 -0500912#Param picture stream of drawing commands ##
913#Param dimensions width and height ##
914#Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ##
915#Param paint Paint to apply transparency, filtering, and so on; may be nullptr ##
Cary Clark682c58d2018-05-16 07:07:07 -0400916#Param bitDepth 8-bit integer or 16-bit float: per component ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500917#Param colorSpace range of colors; may be nullptr ##
918
919#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500920
921#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500922 SkPaint paint;
923 SkPictureRecorder recorder;
924 SkCanvas* recordingCanvas = recorder.beginRecording(50, 50);
925 for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) {
926 paint.setColor(color);
927 recordingCanvas->drawRect({10, 10, 30, 40}, paint);
928 recordingCanvas->translate(10, 10);
929 recordingCanvas->scale(1.2f, 1.4f);
930 }
931 sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
932 int x = 0, y = 0;
933 for (auto alpha : { 70, 140, 210 } ) {
934 paint.setAlpha(alpha);
935 auto srgbColorSpace = SkColorSpace::MakeSRGB();
936 sk_sp<SkImage> image = SkImage::MakeFromPicture(playback, {50, 50}, nullptr, &paint,
937 SkImage::BitDepth::kU8, srgbColorSpace);
938 canvas->drawImage(image, x, y);
939 x += 70; y += 70;
940 }
Cary Clarka560c472017-11-27 10:44:06 -0500941##
942
Cary Clark61ca7c52018-01-02 11:34:14 -0500943#SeeAlso SkCanvas::drawPicture
Cary Clarka560c472017-11-27 10:44:06 -0500944
945#Method ##
946
947# ------------------------------------------------------------------------------
948
Cary Clark9548ea92018-09-13 15:26:33 -0400949#Method static sk_sp<SkImage> MakeFromAHardwareBuffer(
950 AHardwareBuffer* hardwareBuffer,
951 SkAlphaType alphaType = kPremul_SkAlphaType,
952 sk_sp<SkColorSpace> colorSpace = nullptr,
953 GrSurfaceOrigin surfaceOrigin = kTopLeft_GrSurfaceOrigin)
Cary Clark4855f782018-02-06 09:41:53 -0500954#In Constructor
955#Line # creates Image from Android hardware buffer ##
Cary Clarka560c472017-11-27 10:44:06 -0500956
Cary Clark4855f782018-02-06 09:41:53 -0500957#Bug 7447
Cary Clarka560c472017-11-27 10:44:06 -0500958
Cary Clark61ca7c52018-01-02 11:34:14 -0500959Creates Image from Android hardware buffer.
960Returned Image takes a reference on the buffer.
Cary Clarka560c472017-11-27 10:44:06 -0500961
Cary Clark61ca7c52018-01-02 11:34:14 -0500962Only available on Android, when __ANDROID_API__ is defined to be 26 or greater.
Cary Clarka560c472017-11-27 10:44:06 -0500963
Cary Clark61ca7c52018-01-02 11:34:14 -0500964#Param hardwareBuffer AHardwareBuffer Android hardware buffer ##
Cary Clark681287e2018-03-16 11:34:15 -0400965#Param alphaType one of: #list_of_alpha_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500966##
967#Param colorSpace range of colors; may be nullptr ##
Cary Clark9548ea92018-09-13 15:26:33 -0400968#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clarka560c472017-11-27 10:44:06 -0500969
Cary Clark61ca7c52018-01-02 11:34:14 -0500970#Return created Image, or nullptr ##
971
972#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500973##
974
Cary Clark61ca7c52018-01-02 11:34:14 -0500975#SeeAlso MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -0500976
977#Method ##
978
979# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -0500980#Subtopic Property
981#Populate
982#Line # values and attributes ##
983##
Cary Clarka560c472017-11-27 10:44:06 -0500984
985#Method int width() const
Cary Clark4855f782018-02-06 09:41:53 -0500986#In Property
987#Line # returns pixel column count ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500988Returns pixel count in each row.
989
990#Return pixel width in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500991
992#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500993#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500994#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500995 canvas->translate(10, 10);
996 canvas->drawImage(image, 0, 0);
997 canvas->translate(0, image->height());
998 SkPaint paint;
999 paint.setTextAlign(SkPaint::kCenter_Align);
1000 canvas->drawLine(0, 10, image->width(), 10, paint);
1001 canvas->drawString("width", image->width() / 2, 25, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001002##
1003
Cary Clark61ca7c52018-01-02 11:34:14 -05001004#SeeAlso dimensions() height()
Cary Clarka560c472017-11-27 10:44:06 -05001005
1006#Method ##
1007
1008# ------------------------------------------------------------------------------
1009
1010#Method int height() const
Cary Clark4855f782018-02-06 09:41:53 -05001011#In Property
1012#Line # returns pixel row count ##
Cary Clark2f466242017-12-11 16:03:17 -05001013Returns pixel row count.
1014
Cary Clark61ca7c52018-01-02 11:34:14 -05001015#Return pixel height in Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001016
1017#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001018#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -05001019#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -05001020 canvas->translate(10, 10);
1021 canvas->drawImage(image, 0, 0);
1022 canvas->translate(image->width(), 0);
1023 SkPaint paint;
1024 paint.setTextAlign(SkPaint::kCenter_Align);
1025 paint.setVerticalText(true);
1026 canvas->drawLine(10, 0, 10, image->height(), paint);
Cary Clarkac47b882018-01-11 10:35:44 -05001027 canvas->drawString("height", 25, image->height() / 2, paint);
1028##
Cary Clarka560c472017-11-27 10:44:06 -05001029
Cary Clark61ca7c52018-01-02 11:34:14 -05001030#SeeAlso dimensions() width()
Cary Clarka560c472017-11-27 10:44:06 -05001031
1032#Method ##
1033
1034# ------------------------------------------------------------------------------
1035
1036#Method SkISize dimensions() const
Cary Clark4855f782018-02-06 09:41:53 -05001037#In Property
1038#Line # returns width() and height() ##
Cary Clark681287e2018-03-16 11:34:15 -04001039
Cary Clark2f466242017-12-11 16:03:17 -05001040Returns ISize { width(), height() }.
1041
1042#Return integral size of width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -05001043
1044#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001045#Image 4
1046 SkISize dimensions = image->dimensions();
1047 SkIRect bounds = image->bounds();
1048 SkIRect dimensionsAsBounds = SkIRect::MakeSize(dimensions);
1049 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clark681287e2018-03-16 11:34:15 -04001050#StdOut
1051dimensionsAsBounds == bounds
1052##
Cary Clarka560c472017-11-27 10:44:06 -05001053##
1054
Cary Clark61ca7c52018-01-02 11:34:14 -05001055#SeeAlso height() width() bounds()
Cary Clarka560c472017-11-27 10:44:06 -05001056
1057#Method ##
1058
1059# ------------------------------------------------------------------------------
1060
1061#Method SkIRect bounds() const
Cary Clark4855f782018-02-06 09:41:53 -05001062#In Property
1063#Line # returns width() and height() as Rectangle ##
Cary Clark2f466242017-12-11 16:03:17 -05001064Returns IRect { 0, 0, width(), height() }.
1065
1066#Return integral rectangle from origin to width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -05001067
1068#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001069#Height 128
1070#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -05001071 SkIRect bounds = image->bounds();
Cary Clarkac47b882018-01-11 10:35:44 -05001072 for (int x : { 0, bounds.width() } ) {
1073 for (int y : { 0, bounds.height() } ) {
Cary Clark61ca7c52018-01-02 11:34:14 -05001074 canvas->drawImage(image, x, y);
1075 }
1076 }
Cary Clarka560c472017-11-27 10:44:06 -05001077##
1078
Cary Clark682c58d2018-05-16 07:07:07 -04001079#SeeAlso dimensions()
Cary Clarka560c472017-11-27 10:44:06 -05001080
1081#Method ##
1082
1083# ------------------------------------------------------------------------------
1084
1085#Method uint32_t uniqueID() const
Cary Clark4855f782018-02-06 09:41:53 -05001086#In Property
Cary Clark682c58d2018-05-16 07:07:07 -04001087#Line # returns identifier for Image ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001088Returns value unique to image. Image contents cannot change after Image is
1089created. Any operation to create a new Image will receive generate a new
1090unique number.
1091
1092#Return unique identifier ##
Cary Clarka560c472017-11-27 10:44:06 -05001093
1094#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001095#Image 5
1096#Height 156
1097 sk_sp<SkImage> subset = image->makeSubset({10, 20, 90, 100});
1098 canvas->drawImage(image, 0, 0);
1099 canvas->drawImage(subset, 128, 0);
1100 SkPaint paint;
1101 SkString s;
1102 s.printf("original id: %d", image->uniqueID());
1103 canvas->drawString(s, 20, image->height() + 20, paint);
1104 s.printf("subset id: %d", subset->uniqueID());
1105 canvas->drawString(s, 148, subset->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001106##
1107
Cary Clark61ca7c52018-01-02 11:34:14 -05001108#SeeAlso isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001109
1110#Method ##
1111
1112# ------------------------------------------------------------------------------
1113
1114#Method SkAlphaType alphaType() const
Cary Clark4855f782018-02-06 09:41:53 -05001115#In Property
1116#Line # returns Alpha_Type ##
Cary Clark681287e2018-03-16 11:34:15 -04001117Returns Alpha_Type, one of: #list_of_alpha_types#.
Cary Clark61ca7c52018-01-02 11:34:14 -05001118
1119Alpha_Type returned was a parameter to an Image constructor,
1120or was parsed from encoded data.
1121
1122#Return Alpha_Type in Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001123
1124#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001125#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -05001126#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -05001127 const char* alphaTypeStr[] = { "Unknown", "Opaque", "Premul", "Unpremul" };
1128 SkAlphaType alphaType = image->alphaType();
Cary Clarkac47b882018-01-11 10:35:44 -05001129 canvas->drawImage(image, 16, 0);
Cary Clarkffb3d682018-05-17 12:17:28 -04001130 canvas->drawString(alphaTypeStr[(int) alphaType], 20, image->height() + 20, SkPaint());
Cary Clarka560c472017-11-27 10:44:06 -05001131##
1132
Cary Clark61ca7c52018-01-02 11:34:14 -05001133#SeeAlso SkImageInfo::alphaType
Cary Clarka560c472017-11-27 10:44:06 -05001134
1135#Method ##
1136
1137# ------------------------------------------------------------------------------
1138
Greg Daniel56008aa2018-03-14 15:33:42 -04001139#Method SkColorType colorType() const
1140#In Property
1141#Line # returns Color_Type ##
1142
1143Returns Color_Type if known; otherwise, returns kUnknown_SkColorType.
1144
1145#Return Color_Type of Image ##
1146
1147#Example
Cary Clarkffb3d682018-05-17 12:17:28 -04001148#Image 4
1149#Height 96
1150 const char* colors[] = { "Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
1151 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16" };
1152 SkColorType colorType = image->colorType();
1153 canvas->drawImage(image, 16, 0);
1154 canvas->drawString(colors[(int) colorType], 20, image->height() + 20, SkPaint());
Greg Daniel56008aa2018-03-14 15:33:42 -04001155##
1156
1157#SeeAlso SkImageInfo::colorType
1158
1159#Method ##
1160
1161# ------------------------------------------------------------------------------
1162
Cary Clarka560c472017-11-27 10:44:06 -05001163#Method SkColorSpace* colorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001164#In Property
1165#Line # returns Color_Space ##
Cary Clark2f466242017-12-11 16:03:17 -05001166Returns Color_Space, the range of colors, associated with Image. The
1167reference count of Color_Space is unchanged. The returned Color_Space is
1168immutable.
Cary Clarka560c472017-11-27 10:44:06 -05001169
Cary Clark61dfc3a2018-01-03 08:37:53 -05001170Color_Space returned was passed to an Image constructor,
1171or was parsed from encoded data. Color_Space returned may be ignored when Image
1172is drawn, depending on the capabilities of the Surface receiving the drawing.
Cary Clark2f466242017-12-11 16:03:17 -05001173
1174#Return Color_Space in Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001175
1176#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001177#Image 3
1178#Set sRGB
1179 SkPixmap pixmap;
1180 source.peekPixels(&pixmap);
1181 canvas->scale(.25f, .25f);
1182 int y = 0;
1183 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1184 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1185 int x = 0;
1186 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1187 for (int index = 0; index < 2; ++index) {
1188 pixmap.setColorSpace(colorSpace);
1189 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1190 canvas->drawImage(image, x, y);
1191 colorSpace = image->colorSpace()->makeColorSpin();
1192 x += 512;
1193 }
1194 y += 512;
1195 }
Cary Clarka560c472017-11-27 10:44:06 -05001196##
1197
Cary Clark61dfc3a2018-01-03 08:37:53 -05001198#SeeAlso refColorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001199
1200#Method ##
1201
1202# ------------------------------------------------------------------------------
1203
1204#Method sk_sp<SkColorSpace> refColorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001205#In Property
1206#Line # returns Image_Info Color_Space ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001207Returns a smart pointer to Color_Space, the range of colors, associated with
1208Image. The smart pointer tracks the number of objects sharing this
1209SkColorSpace reference so the memory is released when the owners destruct.
1210
1211The returned SkColorSpace is immutable.
1212
1213Color_Space returned was passed to an Image constructor,
1214or was parsed from encoded data. Color_Space returned may be ignored when Image
1215is drawn, depending on the capabilities of the Surface receiving the drawing.
1216
1217#Return Color_Space in Image, or nullptr, wrapped in a smart pointer ##
Cary Clarka560c472017-11-27 10:44:06 -05001218
1219#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001220#Image 3
1221#Set sRGB
1222 SkPixmap pixmap;
1223 source.peekPixels(&pixmap);
1224 canvas->scale(.25f, .25f);
1225 int y = 0;
1226 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1227 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1228 int x = 0;
1229 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1230 for (int index = 0; index < 2; ++index) {
1231 pixmap.setColorSpace(colorSpace);
1232 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1233 canvas->drawImage(image, x, y);
1234 colorSpace = image->refColorSpace()->makeColorSpin();
1235 x += 512;
1236 }
1237 y += 512;
1238 }
Cary Clarka560c472017-11-27 10:44:06 -05001239##
1240
Cary Clark61dfc3a2018-01-03 08:37:53 -05001241#SeeAlso colorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001242
1243#Method ##
1244
1245# ------------------------------------------------------------------------------
1246
1247#Method bool isAlphaOnly() const
Cary Clark4855f782018-02-06 09:41:53 -05001248#In Property
1249#Line # returns if pixels represent a transparency mask ##
Cary Clark2f466242017-12-11 16:03:17 -05001250Returns true if Image pixels represent transparency only. If true, each pixel
1251is packed in 8 bits as defined by kAlpha_8_SkColorType.
Cary Clarka560c472017-11-27 10:44:06 -05001252
Cary Clark2f466242017-12-11 16:03:17 -05001253#Return true if pixels represent a transparency mask ##
Cary Clarka560c472017-11-27 10:44:06 -05001254
1255#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001256 uint8_t pmColors = 0;
1257 sk_sp<SkImage> image = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1), &pmColors, 1});
1258 SkDebugf("alphaOnly = %s\n", image->isAlphaOnly() ? "true" : "false");
1259#StdOut
1260alphaOnly = true
1261##
Cary Clarka560c472017-11-27 10:44:06 -05001262##
1263
Cary Clark61dfc3a2018-01-03 08:37:53 -05001264#SeeAlso alphaType isOpaque
Cary Clarka560c472017-11-27 10:44:06 -05001265
1266#Method ##
1267
1268# ------------------------------------------------------------------------------
1269
1270#Method bool isOpaque() const
Cary Clark4855f782018-02-06 09:41:53 -05001271#In Property
1272#Line # returns if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001273Returns true if pixels ignore their Alpha value and are treated as fully opaque.
Cary Clark2f466242017-12-11 16:03:17 -05001274
1275#Return true if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clarka560c472017-11-27 10:44:06 -05001276
1277#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001278 auto check_isopaque = [](const SkImageInfo& imageInfo) -> void {
1279 auto surface(SkSurface::MakeRaster(imageInfo));
1280 auto image(surface->makeImageSnapshot());
1281 SkDebugf("isOpaque = %s\n", image->isOpaque() ? "true" : "false");
1282 };
1283
1284 check_isopaque(SkImageInfo::MakeN32Premul(5, 5));
1285 check_isopaque(SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType));
1286#StdOut
1287isOpaque = false
1288isOpaque = true
1289##
Cary Clarka560c472017-11-27 10:44:06 -05001290##
1291
Cary Clark61dfc3a2018-01-03 08:37:53 -05001292#SeeAlso alphaType isAlphaOnly
Cary Clarka560c472017-11-27 10:44:06 -05001293
1294#Method ##
1295
1296# ------------------------------------------------------------------------------
1297
1298#Method sk_sp<SkShader> makeShader(SkShader::TileMode tileMode1, SkShader::TileMode tileMode2,
1299 const SkMatrix* localMatrix = nullptr) const
Cary Clark4855f782018-02-06 09:41:53 -05001300#In Constructor
1301#Line # creates Shader, Paint element that can tile Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001302
Cary Clark61dfc3a2018-01-03 08:37:53 -05001303Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1304SkShader::TileMode rules to fill drawn area outside Image. localMatrix permits
1305transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001306
Cary Clark5538c132018-06-14 12:28:14 -04001307#Param tileMode1 tiling on x-axis, one of: SkShader::kClamp_TileMode,
1308 SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
Cary Clark61dfc3a2018-01-03 08:37:53 -05001309##
Cary Clark5538c132018-06-14 12:28:14 -04001310#Param tileMode2 tiling on y-axis, one of: SkShader::kClamp_TileMode,
1311 SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
Cary Clark61dfc3a2018-01-03 08:37:53 -05001312##
1313#Param localMatrix Image transformation, or nullptr ##
1314
1315#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001316
1317#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001318#Image 4
1319SkMatrix matrix;
1320matrix.setRotate(45);
1321SkPaint paint;
1322paint.setShader(image->makeShader(SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode,
1323 &matrix));
1324canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001325##
1326
Cary Clark61dfc3a2018-01-03 08:37:53 -05001327#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001328
1329#Method ##
1330
1331# ------------------------------------------------------------------------------
1332
1333#Method sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const
1334
Cary Clark61dfc3a2018-01-03 08:37:53 -05001335Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1336SkShader::kClamp_TileMode to fill drawn area outside Image. localMatrix permits
1337transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001338
Cary Clark61dfc3a2018-01-03 08:37:53 -05001339#Param localMatrix Image transformation, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001340
Cary Clark61dfc3a2018-01-03 08:37:53 -05001341#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001342
1343#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001344#Image 5
1345SkMatrix matrix;
1346matrix.setRotate(45);
1347matrix.postTranslate(125, 30);
1348SkPaint paint;
1349paint.setShader(image->makeShader(&matrix));
1350canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001351##
1352
Cary Clarkf5404bb2018-01-05 12:10:09 -05001353#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001354
1355#Method ##
1356
1357# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001358#Subtopic Pixels
1359#Populate
1360#Line # read and write pixel values ##
1361##
Cary Clarka560c472017-11-27 10:44:06 -05001362
1363#Method bool peekPixels(SkPixmap* pixmap) const
Cary Clark78de7512018-02-07 07:27:09 -05001364#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001365#Line # returns Pixmap if possible ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001366Copies Image pixel address, row bytes, and Image_Info to pixmap, if address
1367is available, and returns true. If pixel address is not available, return
1368false and leave pixmap unchanged.
Cary Clarka560c472017-11-27 10:44:06 -05001369
Cary Clarkf5404bb2018-01-05 12:10:09 -05001370#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
Cary Clarka560c472017-11-27 10:44:06 -05001371
Cary Clarkf5404bb2018-01-05 12:10:09 -05001372#Return true if Image has direct access to pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001373
1374#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001375 SkBitmap bitmap;
1376 bitmap.allocPixels(SkImageInfo::MakeN32Premul(12, 11));
1377 SkCanvas offscreen(bitmap);
1378 offscreen.clear(SK_ColorWHITE);
1379 SkPaint paint;
1380 offscreen.drawString("%", 1, 10, paint);
1381 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
1382 SkPixmap pixmap;
1383 if (image->peekPixels(&pixmap)) {
1384 const SkPMColor* pixels = pixmap.addr32();
1385 SkPMColor pmWhite = pixels[0];
1386 for (int y = 0; y < image->height(); ++y) {
1387 for (int x = 0; x < image->width(); ++x) {
1388 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
1389 }
1390 SkDebugf("\n");
1391 }
1392 }
1393#StdOut
1394------------
1395--xx----x---
1396-x--x--x----
1397-x--x--x----
1398-x--x-x-----
1399--xx-xx-xx--
1400-----x-x--x-
1401----x--x--x-
1402----x--x--x-
1403---x----xx--
1404------------
1405##
Cary Clarka560c472017-11-27 10:44:06 -05001406##
1407
Cary Clarkf5404bb2018-01-05 12:10:09 -05001408#SeeAlso readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001409
1410#Method ##
1411
1412# ------------------------------------------------------------------------------
1413
1414#Method GrTexture* getTexture() const
Cary Clark682c58d2018-05-16 07:07:07 -04001415#Deprecated
Cary Clarka560c472017-11-27 10:44:06 -05001416#Method ##
1417
1418# ------------------------------------------------------------------------------
1419
1420#Method bool isTextureBacked() const
Cary Clark78de7512018-02-07 07:27:09 -05001421#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001422#Line # returns if Image was created from GPU_Texture ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001423Returns true the contents of Image was created on or uploaded to GPU memory,
1424and is available as a GPU_Texture.
Cary Clarka560c472017-11-27 10:44:06 -05001425
Cary Clarkf5404bb2018-01-05 12:10:09 -05001426#Return true if Image is a GPU_Texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001427
1428#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001429#Image 5
1430#Platform gpu
1431auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1432 if (nullptr == image) {
1433 return;
1434 }
1435 SkPaint paint;
1436 paint.setAntiAlias(true);
1437 paint.setTextAlign(SkPaint::kCenter_Align);
1438 canvas->drawImage(image, 0, 0);
1439 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1440 canvas->drawString(image->isTextureBacked() ? "is GPU texture" : "not GPU texture",
1441 image->width() / 2, image->height() * 3 / 4, paint);
1442};
1443sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1444sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001445 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1446 kOpaque_SkAlphaType, nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05001447drawImage(image, "image");
1448canvas->translate(image->width(), 0);
1449drawImage(bitmapImage, "source");
1450canvas->translate(-image->width(), image->height());
1451drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001452##
1453
Cary Clarkf5404bb2018-01-05 12:10:09 -05001454#SeeAlso MakeFromTexture isValid
Cary Clarka560c472017-11-27 10:44:06 -05001455
1456#Method ##
1457
1458# ------------------------------------------------------------------------------
1459
1460#Method bool isValid(GrContext* context) const
Cary Clark4855f782018-02-06 09:41:53 -05001461#In Property
1462#Line # returns if Image can draw to Raster_Surface or GPU_Context ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001463Returns true if Image can be drawn on either Raster_Surface or GPU_Surface.
1464If context is nullptr, tests if Image draws on Raster_Surface;
1465otherwise, tests if Image draws on GPU_Surface associated with context.
Cary Clarka560c472017-11-27 10:44:06 -05001466
Cary Clarkf5404bb2018-01-05 12:10:09 -05001467Image backed by GPU_Texture may become invalid if associated GrContext is
1468invalid. Lazy_Image may be invalid and may not draw to Raster_Surface or
1469GPU_Surface or both.
Cary Clarka560c472017-11-27 10:44:06 -05001470
Cary Clark61ca7c52018-01-02 11:34:14 -05001471#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05001472
Cary Clarkf5404bb2018-01-05 12:10:09 -05001473#Return true if Image can be drawn ##
Cary Clarka560c472017-11-27 10:44:06 -05001474
1475#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001476#Image 5
1477#Platform gpu
1478auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1479 if (nullptr == image) {
1480 return;
1481 }
1482 SkPaint paint;
1483 paint.setAntiAlias(true);
1484 paint.setTextAlign(SkPaint::kCenter_Align);
1485 canvas->drawImage(image, 0, 0);
1486 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1487 if (canvas->getGrContext()) {
1488 canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" :
1489 "not valid on GPU", image->width() / 2, image->height() * 5 / 8, paint);
1490 }
1491 canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" :
1492 "not valid on CPU", image->width() / 2, image->height() * 7 / 8, paint);
1493};
1494sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1495sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001496 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1497 kOpaque_SkAlphaType, nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05001498drawImage(image, "image");
1499canvas->translate(image->width(), 0);
1500drawImage(bitmapImage, "source");
1501canvas->translate(-image->width(), image->height());
1502drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001503##
1504
Cary Clarkf5404bb2018-01-05 12:10:09 -05001505#SeeAlso isTextureBacked isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001506
1507#Method ##
1508
1509# ------------------------------------------------------------------------------
1510
Robert Phillipsc5509952018-04-04 15:54:55 -04001511#Method GrBackendTexture getBackendTexture(bool flushPendingGrContextIO,
1512 GrSurfaceOrigin* origin = nullptr) const
1513#In Property
1514#Line # returns GPU reference to Image as texture ##
1515
Cary Clark682c58d2018-05-16 07:07:07 -04001516Retrieves the back-end texture. If Image has no back-end texture, an invalid
Cary Clarkba75aee2018-04-05 08:18:41 -04001517object is returned. Call GrBackendTexture::isValid to determine if the result
1518is valid.
1519
1520If flushPendingGrContextIO is true, completes deferred I/O operations.
Robert Phillipsc5509952018-04-04 15:54:55 -04001521
1522If origin in not nullptr, copies location of content drawn into Image.
1523
1524#Param flushPendingGrContextIO flag to flush outstanding requests ##
1525#Param origin storage for one of: kTopLeft_GrSurfaceOrigin,
1526 kBottomLeft_GrSurfaceOrigin; or nullptr
1527##
1528
Cary Clarkba75aee2018-04-05 08:18:41 -04001529#Return back-end API texture handle; invalid on failure ##
Robert Phillipsc5509952018-04-04 15:54:55 -04001530
Cary Clarkba75aee2018-04-05 08:18:41 -04001531#Example
1532#Image 3
1533#Platform gpu
Brian Osman584b5012018-04-13 15:48:26 -04001534 GrContext* grContext = canvas->getGrContext();
1535 if (!grContext) {
1536 canvas->drawString("GPU only!", 20, 40, SkPaint());
1537 return;
1538 }
1539 sk_sp<SkImage> imageFromBackend = SkImage::MakeFromAdoptedTexture(grContext, backEndTexture,
1540 kBottomLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1541 GrBackendTexture textureFromImage = imageFromBackend->getBackendTexture(false);
1542 if (!textureFromImage.isValid()) {
1543 return;
1544 }
1545 sk_sp<SkImage> imageFromTexture = SkImage::MakeFromAdoptedTexture(grContext, textureFromImage,
1546 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1547 canvas->drawImage(imageFromTexture, 0, 0);
Cary Clarkba75aee2018-04-05 08:18:41 -04001548 canvas->drawImage(imageFromBackend, 128, 128);
Robert Phillipsc5509952018-04-04 15:54:55 -04001549##
1550
1551#SeeAlso MakeFromTexture isTextureBacked
1552
1553#Method ##
1554
1555# ------------------------------------------------------------------------------
1556
Cary Clarka560c472017-11-27 10:44:06 -05001557#Enum CachingHint
Cary Clark682c58d2018-05-16 07:07:07 -04001558#Line # options for readPixels and scalePixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001559#Code
1560 enum CachingHint {
1561 kAllow_CachingHint,
1562 kDisallow_CachingHint,
1563 };
1564##
1565
Cary Clarkac47b882018-01-11 10:35:44 -05001566CachingHint selects whether Skia may internally cache Bitmaps generated by
1567decoding Image, or by copying Image from GPU to CPU. The default behavior
Cary Clark682c58d2018-05-16 07:07:07 -04001568allows caching Bitmaps.
Cary Clarkac47b882018-01-11 10:35:44 -05001569
1570Choose kDisallow_CachingHint if Image pixels are to be used only once, or
1571if Image pixels reside in a cache outside of Skia, or to reduce memory pressure.
1572
1573Choosing kAllow_CachingHint does not ensure that pixels will be cached.
1574Image pixels may not be cached if memory requirements are too large or
1575pixels are not accessible.
Cary Clarka560c472017-11-27 10:44:06 -05001576
1577#Const kAllow_CachingHint 0
Cary Clark682c58d2018-05-16 07:07:07 -04001578#Line # allows internally caching decoded and copied pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001579##
1580#Const kDisallow_CachingHint 1
Cary Clark682c58d2018-05-16 07:07:07 -04001581#Line # disallows internally caching decoded and copied pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001582##
1583
Cary Clarkac47b882018-01-11 10:35:44 -05001584#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001585##
1586
Cary Clarkac47b882018-01-11 10:35:44 -05001587#SeeAlso readPixels scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001588
1589#Enum ##
1590
1591# ------------------------------------------------------------------------------
1592
1593#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1594 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001595#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001596#Line # copies and converts pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001597
Cary Clarkac47b882018-01-11 10:35:44 -05001598Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY),
Cary Clark682c58d2018-05-16 07:07:07 -04001599and does not exceed Image (width(), height()).
Cary Clarkac47b882018-01-11 10:35:44 -05001600
1601dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
1602destination. dstRowBytes specifics the gap from one destination row to the next.
1603Returns true if pixels are copied. Returns false if:
1604#List
1605# dstInfo.addr() equals nullptr ##
1606# dstRowBytes is less than dstInfo.minRowBytes ##
1607# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001608##
1609
Cary Clarkac47b882018-01-11 10:35:44 -05001610Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1611kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1612If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace must match.
1613If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType must
1614match. If Image Color_Space is nullptr, dstInfo.colorSpace must match. Returns
1615false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001616
Cary Clarkac47b882018-01-11 10:35:44 -05001617srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark682c58d2018-05-16 07:07:07 -04001618false if width() or height() is zero or negative.
Cary Clark2be81cf2018-09-13 12:04:30 -04001619Returns false if #Formula # abs(srcX) >= Image width() ##, or if #Formula # abs(srcY) >= Image height() ##.
Cary Clarka560c472017-11-27 10:44:06 -05001620
Cary Clarkac47b882018-01-11 10:35:44 -05001621If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1622If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1623
1624#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1625#Param dstPixels destination pixel storage ##
1626#Param dstRowBytes destination row length ##
1627#Param srcX column index whose absolute value is less than width() ##
1628#Param srcY row index whose absolute value is less than height() ##
1629#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1630
1631#Return true if pixels are copied to dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001632
1633#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001634#Image 3
1635 canvas->scale(.5f, .5f);
1636 const int width = 32;
1637 const int height = 32;
1638 std::vector<int32_t> dstPixels;
1639 dstPixels.resize(height * width * 4);
1640 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1641 for (int y = 0; y < 512; y += height ) {
1642 for (int x = 0; x < 512; x += width ) {
1643 if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) {
1644 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
1645 SkBitmap bitmap;
1646 bitmap.installPixels(dstPixmap);
1647 canvas->drawBitmap(bitmap, 0, 0);
1648 }
1649 canvas->translate(48, 0);
1650 }
1651 canvas->translate(-16 * 48, 48);
1652 }
Cary Clarka560c472017-11-27 10:44:06 -05001653##
1654
Cary Clarkac47b882018-01-11 10:35:44 -05001655#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001656
1657#Method ##
1658
1659# ------------------------------------------------------------------------------
1660
1661#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY,
1662 CachingHint cachingHint = kAllow_CachingHint) const
1663
Cary Clarkac47b882018-01-11 10:35:44 -05001664Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and
Cary Clark682c58d2018-05-16 07:07:07 -04001665does not exceed Image (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001666
Cary Clarkac47b882018-01-11 10:35:44 -05001667dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
1668and row bytes of destination. dst.rowBytes specifics the gap from one destination
1669row to the next. Returns true if pixels are copied. Returns false if:
1670#List
1671# dst pixel storage equals nullptr ##
1672# dst.rowBytes is less than SkImageInfo::minRowBytes ##
1673# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001674##
1675
Cary Clarkac47b882018-01-11 10:35:44 -05001676Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1677kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1678If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1679If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1680match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1681false if pixel conversion is not possible.
Cary Clark682c58d2018-05-16 07:07:07 -04001682
Cary Clarkac47b882018-01-11 10:35:44 -05001683srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark682c58d2018-05-16 07:07:07 -04001684false if width() or height() is zero or negative.
Cary Clark2be81cf2018-09-13 12:04:30 -04001685Returns false if #Formula # abs(srcX) >= Image width() ##, or if #Formula # abs(srcY) >= Image height() ##.
Cary Clarkac47b882018-01-11 10:35:44 -05001686
1687If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1688If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1689
1690#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1691#Param srcX column index whose absolute value is less than width() ##
1692#Param srcY row index whose absolute value is less than height() ##
1693#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1694
1695#Return true if pixels are copied to dst ##
1696
1697#Example
1698#Image 3
1699 std::vector<int32_t> srcPixels;
1700 int rowBytes = image->width() * 4;
1701 int quarterWidth = image->width() / 4;
1702 int quarterHeight = image->height() / 4;
1703 srcPixels.resize(image->height() * rowBytes);
1704 for (int y = 0; y < 4; ++y) {
1705 for (int x = 0; x < 4; ++x) {
1706 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1707 &srcPixels.front() + x * image->height() * quarterWidth +
1708 y * quarterWidth, rowBytes);
1709 image->readPixels(pixmap, x * quarterWidth, y * quarterHeight);
1710 }
1711 }
1712 canvas->scale(.5f, .5f);
1713 SkBitmap bitmap;
1714 bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()),
1715 &srcPixels.front(), rowBytes);
1716 canvas->drawBitmap(bitmap, 0, 0);
1717##
1718
1719#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001720
1721#Method ##
1722
1723# ------------------------------------------------------------------------------
1724
1725#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality,
1726 CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001727#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001728#Line # scales and converts one Image to another ##
Cary Clarka560c472017-11-27 10:44:06 -05001729
Cary Clarkac47b882018-01-11 10:35:44 -05001730Copies Image to dst, scaling pixels to fit dst.width() and dst.height(), and
1731converting pixels to match dst.colorType and dst.alphaType. Returns true if
1732pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1733less than dst SkImageInfo::minRowBytes.
Cary Clarka560c472017-11-27 10:44:06 -05001734
Cary Clarkac47b882018-01-11 10:35:44 -05001735Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1736kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1737If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1738If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1739match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1740false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001741
Cary Clarkac47b882018-01-11 10:35:44 -05001742Scales the image, with filterQuality, to match dst.width() and dst.height().
1743filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1744Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1745Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1746Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1747kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1748
1749If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1750If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1751
1752#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1753#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1754 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1755##
1756#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1757
1758#Return true if pixels are scaled to fit dst ##
Cary Clarka560c472017-11-27 10:44:06 -05001759
1760#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001761#Image 3
1762#Height 128
1763 std::vector<int32_t> srcPixels;
1764 int quarterWidth = image->width() / 16;
1765 int rowBytes = quarterWidth * 4;
1766 int quarterHeight = image->height() / 16;
1767 srcPixels.resize(quarterHeight * rowBytes);
1768 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1769 &srcPixels.front(), rowBytes);
1770 canvas->scale(4, 4);
1771 SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality,
1772 kMedium_SkFilterQuality, kHigh_SkFilterQuality };
1773 for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) {
1774 image->scalePixels(pixmap, qualities[index]);
1775 sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
1776 canvas->drawImage(filtered, 16 * index, 0);
1777 }
Cary Clarka560c472017-11-27 10:44:06 -05001778##
1779
Cary Clarkac47b882018-01-11 10:35:44 -05001780#SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001781
1782#Method ##
1783
1784# ------------------------------------------------------------------------------
1785
1786#Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const
Cary Clark78de7512018-02-07 07:27:09 -05001787#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001788#Line # returns encoded Image as SkData ##
Cary Clark682c58d2018-05-16 07:07:07 -04001789Encodes Image pixels, returning result as SkData.
Cary Clark2f466242017-12-11 16:03:17 -05001790
Cary Clarkac47b882018-01-11 10:35:44 -05001791Returns nullptr if encoding fails, or if encodedImageFormat is not supported.
Cary Clarka560c472017-11-27 10:44:06 -05001792
Cary Clarkac47b882018-01-11 10:35:44 -05001793Image encoding in a format requires both building with one or more of:
1794SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support
1795for the encoded format.
1796
1797If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can
1798additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP,
1799SkEncodedImageFormat::kGIF.
1800
1801quality is a platform and format specific metric trading off size and encoding
1802error. When used, quality equaling 100 encodes with the least error. quality may
1803be ignored by the encoder.
1804
1805#Param encodedImageFormat one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
1806 SkEncodedImageFormat::kWEBP
1807 ##
1808#Param quality encoder specific metric with 100 equaling best ##
Cary Clarka560c472017-11-27 10:44:06 -05001809
Cary Clark2f466242017-12-11 16:03:17 -05001810#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001811
1812#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001813#Image 3
1814 canvas->scale(4, 4);
1815 SkIRect subset = {0, 0, 16, 64};
1816 int x = 0;
1817 for (int quality : { 0, 10, 50, 100 } ) {
1818 sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
1819 sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
1820 canvas->drawImage(filtered, x, 0);
1821 x += 16;
1822 }
Cary Clarka560c472017-11-27 10:44:06 -05001823##
1824
Cary Clarkac47b882018-01-11 10:35:44 -05001825#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001826
1827#Method ##
1828
1829# ------------------------------------------------------------------------------
1830
Cary Clark61ca7c52018-01-02 11:34:14 -05001831#Method sk_sp<SkData> encodeToData() const
Cary Clarka560c472017-11-27 10:44:06 -05001832
Cary Clarkac47b882018-01-11 10:35:44 -05001833Encodes Image pixels, returning result as SkData. Returns existing encoded data
1834if present; otherwise, Image is encoded with SkEncodedImageFormat::kPNG. Skia
1835must be built with SK_HAS_PNG_LIBRARY to encode Image.
Cary Clarka560c472017-11-27 10:44:06 -05001836
Cary Clark682c58d2018-05-16 07:07:07 -04001837Returns nullptr if existing encoded data is missing or invalid, and
Cary Clarka560c472017-11-27 10:44:06 -05001838encoding fails.
1839
Cary Clarkac47b882018-01-11 10:35:44 -05001840#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001841
1842#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001843#Image 3
1844 canvas->scale(4, 4);
1845 SkIRect subset = {136, 32, 200, 96};
1846 sk_sp<SkData> data(image->encodeToData());
1847 sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
1848 canvas->drawImage(eye, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001849##
1850
Cary Clarkac47b882018-01-11 10:35:44 -05001851#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001852
1853#Method ##
1854
1855# ------------------------------------------------------------------------------
1856
1857#Method sk_sp<SkData> refEncodedData() const
Cary Clark78de7512018-02-07 07:27:09 -05001858#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001859#Line # returns Image encoded in SkData if present ##
Cary Clarkac47b882018-01-11 10:35:44 -05001860Returns encoded Image pixels as SkData, if Image was created from supported
1861encoded stream format. Platform support for formats vary and may require building
1862with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY.
Cary Clarka560c472017-11-27 10:44:06 -05001863
Cary Clarkac47b882018-01-11 10:35:44 -05001864Returns nullptr if Image contents are not encoded.
Cary Clarka560c472017-11-27 10:44:06 -05001865
Cary Clarkac47b882018-01-11 10:35:44 -05001866#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001867
1868#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001869#Image 3
1870#Platform gpu
1871 struct {
1872 const char* name;
1873 sk_sp<SkImage> image;
1874 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1875 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001876 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1877 kOpaque_SkAlphaType, nullptr) } };
Cary Clarkac47b882018-01-11 10:35:44 -05001878 SkString string;
1879 SkPaint paint;
1880 for (const auto& test : tests ) {
1881 if (!test.image) {
1882 string.printf("no %s", test.name);
1883 } else {
1884 string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name);
1885 }
1886 canvas->drawString(string, 10, 20, paint);
1887 canvas->translate(0, 20);
1888 }
Cary Clarka560c472017-11-27 10:44:06 -05001889##
1890
Cary Clarkac47b882018-01-11 10:35:44 -05001891#SeeAlso encodeToData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001892
1893#Method ##
1894
1895# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001896#Subtopic Utility
1897#Populate
1898#Line # rarely called management functions ##
1899##
Cary Clarka560c472017-11-27 10:44:06 -05001900
Cary Clarka560c472017-11-27 10:44:06 -05001901#Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const
Cary Clark4855f782018-02-06 09:41:53 -05001902#In Constructor
1903#Line # creates Image containing part of original ##
Cary Clarkac47b882018-01-11 10:35:44 -05001904Returns subset of Image. subset must be fully contained by Image dimensions().
1905The implementation may share pixels, or may copy them.
Cary Clarka560c472017-11-27 10:44:06 -05001906
Cary Clarkac47b882018-01-11 10:35:44 -05001907Returns nullptr if subset is empty, or subset is not contained by bounds, or
1908pixels in Image could not be read or copied.
Cary Clarka560c472017-11-27 10:44:06 -05001909
Cary Clarkac47b882018-01-11 10:35:44 -05001910#Param subset bounds of returned Image ##
1911
1912#Return partial or full Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001913
1914#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001915#Image 3
1916 canvas->scale(.5f, .5f);
Cary Clarkc30382f2018-07-24 08:09:27 -04001917 const int width = 64;
1918 const int height = 64;
Cary Clarkac47b882018-01-11 10:35:44 -05001919 for (int y = 0; y < 512; y += height ) {
1920 for (int x = 0; x < 512; x += width ) {
1921 sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height}));
1922 canvas->drawImage(subset, x * 3 / 2, y * 3 / 2);
1923 }
1924 }
Cary Clarka560c472017-11-27 10:44:06 -05001925##
1926
Cary Clarkac47b882018-01-11 10:35:44 -05001927#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001928
1929#Method ##
1930
1931# ------------------------------------------------------------------------------
1932
Greg Daniel5f4b09d2018-06-12 16:39:59 -04001933#Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace,
1934 GrMipMapped mipMapped = GrMipMapped::kNo) const
Cary Clark4855f782018-02-06 09:41:53 -05001935#In Constructor
1936#Line # creates Image matching Color_Space if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001937Returns Image backed by GPU_Texture associated with context. Returned Image is
Cary Clark5538c132018-06-14 12:28:14 -04001938compatible with Surface created with dstColorSpace. The returned Image respects
1939mipMapped setting; if mipMapped equals GrMipMapped::kYes, the backing texture
1940allocates Mip_Map levels. Returns original Image if context
1941and dstColorSpace match and mipMapped is compatible with backing GPU_Texture.
Cary Clarkac47b882018-01-11 10:35:44 -05001942
1943Returns nullptr if context is nullptr, or if Image was created with another
1944GrContext.
Cary Clarka560c472017-11-27 10:44:06 -05001945
Cary Clark61ca7c52018-01-02 11:34:14 -05001946#Param context GPU_Context ##
Cary Clarkac47b882018-01-11 10:35:44 -05001947#Param dstColorSpace range of colors of matching Surface on GPU ##
Cary Clark5538c132018-06-14 12:28:14 -04001948#Param mipMapped whether created Image texture must allocate Mip_Map levels ##
Cary Clarka560c472017-11-27 10:44:06 -05001949
Cary Clarkac47b882018-01-11 10:35:44 -05001950#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001951
1952#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001953#Platform gpu
1954#Image 5
1955 auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void {
1956 if (nullptr == image || nullptr == context) {
1957 return;
1958 }
1959 SkPaint paint;
1960 paint.setAntiAlias(true);
1961 paint.setTextAlign(SkPaint::kCenter_Align);
1962 sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr));
1963 canvas->drawImage(texture, 0, 0);
1964 canvas->drawString(label, texture->width() / 2, texture->height() / 4, paint);
1965 };
1966 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1967 GrContext* context = canvas->getGrContext();
1968 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001969 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1970 kOpaque_SkAlphaType, nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001971 drawImage(image, context, "image");
1972 canvas->translate(image->width(), 0);
1973 drawImage(bitmapImage, context, "source");
1974 canvas->translate(-image->width(), image->height());
1975 drawImage(textureImage, context, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001976##
1977
Cary Clarkac47b882018-01-11 10:35:44 -05001978#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05001979
1980#Method ##
1981
1982# ------------------------------------------------------------------------------
1983
1984#Method sk_sp<SkImage> makeNonTextureImage() const
Cary Clark4855f782018-02-06 09:41:53 -05001985#In Constructor
1986#Line # creates Image without dependency on GPU_Texture ##
Cary Clarkac47b882018-01-11 10:35:44 -05001987Returns Raster_Image or Lazy_Image. Copies Image backed by GPU_Texture into
Cary Clark4855f782018-02-06 09:41:53 -05001988CPU memory if needed. Returns original Image if decoded in Raster_Bitmap,
Cary Clarkac47b882018-01-11 10:35:44 -05001989or if encoded in a stream.
Cary Clark61ca7c52018-01-02 11:34:14 -05001990
Cary Clarkac47b882018-01-11 10:35:44 -05001991Returns nullptr if backed by GPU_Texture and copy fails.
1992
1993#Return Raster_Image, Lazy_Image, or nullptr ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001994
1995#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001996#Image 5
1997#Platform gpu
1998 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1999 if (nullptr == image) {
2000 return;
2001 }
2002 SkPaint paint;
2003 paint.setAntiAlias(true);
2004 paint.setTextAlign(SkPaint::kCenter_Align);
2005 sk_sp<SkImage> nonTexture(image->makeNonTextureImage());
2006 canvas->drawImage(nonTexture, 0, 0);
2007 canvas->drawString(label, nonTexture->width() / 2, nonTexture->height() / 4, paint);
2008 };
2009 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2010 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04002011 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
2012 kOpaque_SkAlphaType, nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05002013 drawImage(image, "image");
2014 canvas->translate(image->width(), 0);
2015 drawImage(bitmapImage, "source");
2016 canvas->translate(-image->width(), image->height());
2017 drawImage(textureImage, "backEndTexture");
Cary Clark61ca7c52018-01-02 11:34:14 -05002018##
2019
Cary Clark56356312018-02-08 14:45:18 -05002020#SeeAlso makeTextureImage makeRasterImage MakeBackendTextureFromSkImage
Cary Clark61ca7c52018-01-02 11:34:14 -05002021
2022#Method ##
2023
2024# ------------------------------------------------------------------------------
2025
2026#Method sk_sp<SkImage> makeRasterImage() const
Cary Clark4855f782018-02-06 09:41:53 -05002027#In Constructor
2028#Line # creates Image compatible with Raster_Surface if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05002029Returns Raster_Image. Copies Image backed by GPU_Texture into CPU memory,
Cary Clark4855f782018-02-06 09:41:53 -05002030or decodes Image from Lazy_Image. Returns original Image if decoded in
Cary Clarkac47b882018-01-11 10:35:44 -05002031Raster_Bitmap.
Cary Clarka560c472017-11-27 10:44:06 -05002032
Cary Clarkac47b882018-01-11 10:35:44 -05002033Returns nullptr if copy, decode, or pixel read fails.
Cary Clarka560c472017-11-27 10:44:06 -05002034
Cary Clarkac47b882018-01-11 10:35:44 -05002035#Return Raster_Image, or nullptr ##
2036
Cary Clark4855f782018-02-06 09:41:53 -05002037#Bug 7479
Cary Clarka560c472017-11-27 10:44:06 -05002038#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002039#Image 5
2040#Platform gpu
2041 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2042 if (nullptr == image) {
2043 return;
2044 }
2045 SkPaint paint;
2046 paint.setAntiAlias(true);
2047 paint.setTextAlign(SkPaint::kCenter_Align);
2048 sk_sp<SkImage> raster(image->makeRasterImage());
2049 canvas->drawImage(raster, 0, 0);
2050 canvas->drawString(label, raster->width() / 2, raster->height() / 4, paint);
2051 };
2052 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2053 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04002054 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
2055 kOpaque_SkAlphaType, nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05002056 drawImage(image, "image");
2057 canvas->translate(image->width(), 0);
2058 drawImage(bitmapImage, "source");
2059 canvas->translate(-image->width(), image->height());
2060 drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05002061##
2062
Cary Clarkac47b882018-01-11 10:35:44 -05002063#SeeAlso isTextureBacked isLazyGenerated MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -05002064
2065#Method ##
2066
2067# ------------------------------------------------------------------------------
2068
2069#Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
2070 const SkIRect& clipBounds, SkIRect* outSubset,
2071 SkIPoint* offset) const
Cary Clark4855f782018-02-06 09:41:53 -05002072#In Constructor
2073#Line # creates filtered, clipped Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002074
Cary Clarkac47b882018-01-11 10:35:44 -05002075Creates filtered Image. filter processes original Image, potentially changing
2076color, position, and size. subset is the bounds of original Image processed
2077by filter. clipBounds is the expected bounds of the filtered Image. outSubset
2078is required storage for the actual bounds of the filtered Image. offset is
2079required storage for translation of returned Image.
Cary Clarka560c472017-11-27 10:44:06 -05002080
Cary Clarkac47b882018-01-11 10:35:44 -05002081Returns nullptr if Image could not be created. If nullptr is returned, outSubset
2082and offset are undefined.
2083
Cary Clark56356312018-02-08 14:45:18 -05002084Useful for animation of SkImageFilter that varies size from frame to frame.
2085Returned Image is created larger than required by filter so that GPU_Texture
2086can be reused with different sized effects. outSubset describes the valid bounds
2087of GPU_Texture returned. offset translates the returned Image to keep subsequent
2088animation frames aligned with respect to each other.
Cary Clarkac47b882018-01-11 10:35:44 -05002089
2090#Param filter how Image is sampled when transformed ##
Cary Clark56356312018-02-08 14:45:18 -05002091#Param subset bounds of Image processed by filter ##
2092#Param clipBounds expected bounds of filtered Image ##
2093#Param outSubset storage for returned Image bounds ##
2094#Param offset storage for returned Image translation ##
Cary Clarka560c472017-11-27 10:44:06 -05002095
Cary Clarkac47b882018-01-11 10:35:44 -05002096#Return filtered Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05002097
Cary Clark5717e822018-09-21 11:36:41 -04002098# Duration 5 breaks fiddlecli
Cary Clarka560c472017-11-27 10:44:06 -05002099#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002100#Description
2101In each frame of the animation, filtered Image is drawn in a different location.
2102By translating canvas by returned offset, Image appears stationary.
2103##
2104#Image 5
2105#Platform gpu
Cary Clark5717e822018-09-21 11:36:41 -04002106#Duration 1
Cary Clarkac47b882018-01-11 10:35:44 -05002107 sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make(
2108 -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE,
2109 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
2110 nullptr);
2111 sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr);
2112 SkIRect subset = image->bounds();
2113 SkIRect clipBounds = image->bounds();
2114 clipBounds.outset(60, 60);
2115 SkIRect outSubset;
2116 SkIPoint offset;
2117 sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds,
2118 &outSubset, &offset));
2119 SkPaint paint;
2120 paint.setAntiAlias(true);
2121 paint.setStyle(SkPaint::kStroke_Style);
2122 canvas->drawLine(0, 0, offset.fX, offset.fY, paint);
2123 canvas->translate(offset.fX, offset.fY);
2124 canvas->drawImage(filtered, 0, 0);
Cary Clark681287e2018-03-16 11:34:15 -04002125 canvas->drawRect(SkRect::Make(outSubset), paint);
Cary Clarka560c472017-11-27 10:44:06 -05002126##
2127
Cary Clark56356312018-02-08 14:45:18 -05002128#SeeAlso makeShader SkPaint::setImageFilter
Cary Clarka560c472017-11-27 10:44:06 -05002129
2130#Method ##
2131
2132# ------------------------------------------------------------------------------
2133
Cary Clarka560c472017-11-27 10:44:06 -05002134#Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc
Cary Clark682c58d2018-05-16 07:07:07 -04002135#Line # parameter type for MakeBackendTextureFromSkImage ##
Cary Clarkffb3d682018-05-17 12:17:28 -04002136
2137#Code
2138typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc;
2139##
2140
Cary Clark0d225392018-06-07 09:59:07 -04002141Defines a callback function, taking one parameter of type GrBackendTexture with
2142no return value. Function is called when back-end texture is to be released.
Cary Clarka560c472017-11-27 10:44:06 -05002143##
2144
2145# ------------------------------------------------------------------------------
2146
2147#Method static bool MakeBackendTextureFromSkImage(GrContext* context,
2148 sk_sp<SkImage> image,
2149 GrBackendTexture* backendTexture,
2150 BackendTextureReleaseProc* backendTextureReleaseProc)
Cary Clark4855f782018-02-06 09:41:53 -05002151#In Constructor
2152#Line # creates GPU_Texture from Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002153
Cary Clark56356312018-02-08 14:45:18 -05002154Creates a GrBackendTexture from the provided SkImage. Returns true and
2155stores result in backendTexture and backendTextureReleaseProc if
2156texture is created; otherwise, returns false and leaves
2157backendTexture and backendTextureReleaseProc unmodified.
Cary Clarka560c472017-11-27 10:44:06 -05002158
Cary Clark56356312018-02-08 14:45:18 -05002159Call backendTextureReleaseProc after deleting backendTexture.
2160backendTextureReleaseProc cleans up auxiliary data related to returned
2161backendTexture. The caller must delete returned backendTexture after use.
Cary Clarka560c472017-11-27 10:44:06 -05002162
Cary Clark56356312018-02-08 14:45:18 -05002163If Image is both texture backed and singly referenced, image is returned in
2164backendTexture without conversion or making a copy. Image is singly referenced
2165if its was transferred solely using std::move().
2166
2167If Image is not texture backed, returns texture with Image contents.
Cary Clarka560c472017-11-27 10:44:06 -05002168
Cary Clark61ca7c52018-01-02 11:34:14 -05002169#Param context GPU_Context ##
Cary Clark56356312018-02-08 14:45:18 -05002170#Param image Image used for texture ##
Cary Clark682c58d2018-05-16 07:07:07 -04002171#Param backendTexture storage for back-end texture ##
Cary Clark56356312018-02-08 14:45:18 -05002172#Param backendTextureReleaseProc storage for clean up function ##
Cary Clarka560c472017-11-27 10:44:06 -05002173
Cary Clark682c58d2018-05-16 07:07:07 -04002174#Return true if back-end texture was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002175
2176#Example
Cary Clark56356312018-02-08 14:45:18 -05002177#Platform gpu
2178#Height 64
2179#Function
Brian Salomon67f85842018-02-09 08:50:22 -05002180static sk_sp<SkImage> create_gpu_image(GrContext* grContext) {
2181 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
2182 auto surface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info));
2183 SkCanvas* canvas = surface->getCanvas();
2184 canvas->clear(SK_ColorWHITE);
2185 SkPaint paint;
2186 paint.setColor(SK_ColorBLACK);
2187 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
2188 return surface->makeImageSnapshot();
2189}
2190##
2191
Cary Clark682c58d2018-05-16 07:07:07 -04002192void draw(SkCanvas* canvas) {
Brian Salomon67f85842018-02-09 08:50:22 -05002193 GrContext* grContext = canvas->getGrContext();
2194 if (!grContext) {
2195 return;
2196 }
2197 sk_sp<SkImage> backEndImage = create_gpu_image(grContext);
2198 canvas->drawImage(backEndImage, 0, 0);
2199 GrBackendTexture texture;
2200 SkImage::BackendTextureReleaseProc proc;
2201 if (!SkImage::MakeBackendTextureFromSkImage(grContext, std::move(backEndImage),
2202 &texture, &proc)) {
2203 return;
2204 }
2205 sk_sp<SkImage> i2 = SkImage::MakeFromTexture(grContext, texture, kTopLeft_GrSurfaceOrigin,
2206 kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
2207 canvas->drawImage(i2, 30, 30);
Cary Clark56356312018-02-08 14:45:18 -05002208}
Cary Clarka560c472017-11-27 10:44:06 -05002209##
2210
Cary Clark56356312018-02-08 14:45:18 -05002211#SeeAlso MakeFromTexture makeTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002212
2213#Method ##
2214
2215# ------------------------------------------------------------------------------
2216
2217#Enum LegacyBitmapMode
Cary Clark56356312018-02-08 14:45:18 -05002218#Deprecated soon
Cary Clarka560c472017-11-27 10:44:06 -05002219#Code
2220 enum LegacyBitmapMode {
2221 kRO_LegacyBitmapMode,
Cary Clarka560c472017-11-27 10:44:06 -05002222 };
2223##
2224
Cary Clarka560c472017-11-27 10:44:06 -05002225#Const kRO_LegacyBitmapMode 0
Cary Clark682c58d2018-05-16 07:07:07 -04002226#Line # returned bitmap is read-only and immutable ##
Cary Clarka560c472017-11-27 10:44:06 -05002227##
Cary Clarka560c472017-11-27 10:44:06 -05002228
2229#Enum ##
2230
2231# ------------------------------------------------------------------------------
2232
Cary Clark56356312018-02-08 14:45:18 -05002233#Method bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode = kRO_LegacyBitmapMode) const
Cary Clark4855f782018-02-06 09:41:53 -05002234#In Constructor
2235#Line # returns as Raster_Bitmap ##
Cary Clarkac47b882018-01-11 10:35:44 -05002236Creates raster Bitmap with same pixels as Image. If legacyBitmapMode is
2237kRO_LegacyBitmapMode, returned bitmap is read-only and immutable.
2238Returns true if Bitmap is stored in bitmap. Returns false and resets bitmap if
2239Bitmap write did not succeed.
Cary Clarka560c472017-11-27 10:44:06 -05002240
Cary Clark3cd22cc2017-12-01 11:49:58 -05002241#Param bitmap storage for legacy Bitmap ##
Cary Clark56356312018-02-08 14:45:18 -05002242#Param legacyBitmapMode to be deprecated ##
Cary Clarka560c472017-11-27 10:44:06 -05002243
Cary Clark3cd22cc2017-12-01 11:49:58 -05002244#Return true if Bitmap was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002245
Cary Clarkae957c42018-06-07 17:07:17 -04002246#Example
Cary Clark56356312018-02-08 14:45:18 -05002247#Image 4
2248#Platform gpu
Brian Salomon67f85842018-02-09 08:50:22 -05002249 SkBitmap bitImage;
2250 if (image->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2251 canvas->drawBitmap(bitImage, 0, 0);
2252 }
2253 GrContext* grContext = canvas->getGrContext();
2254 if (!grContext) {
2255 return;
2256 }
2257 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(grContext, backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04002258 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
2259 kOpaque_SkAlphaType, nullptr));
Brian Salomon67f85842018-02-09 08:50:22 -05002260 canvas->drawImage(textureImage, 45, 45);
2261 if (textureImage->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2262 canvas->drawBitmap(bitImage, 90, 90);
2263 }
Cary Clarka560c472017-11-27 10:44:06 -05002264##
2265
Cary Clark56356312018-02-08 14:45:18 -05002266#SeeAlso MakeRasterData makeRasterImage makeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002267
2268#Method ##
2269
2270# ------------------------------------------------------------------------------
2271
2272#Method bool isLazyGenerated() const
Cary Clark4855f782018-02-06 09:41:53 -05002273#In Property
2274#Line # returns if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002275Returns true if Image is backed by an image-generator or other service that creates
2276and caches its pixels or texture on-demand.
2277
Cary Clark2f466242017-12-11 16:03:17 -05002278#Return true if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002279
2280#Example
Cary Clark2f466242017-12-11 16:03:17 -05002281#Height 80
2282#Function
2283class TestImageGenerator : public SkImageGenerator {
2284public:
2285 TestImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(10, 10)) {}
2286 ~TestImageGenerator() override {}
2287protected:
2288 bool onGetPixels(const SkImageInfo& info, void* pixelPtr, size_t rowBytes,
2289 const Options& options) override {
2290 SkPMColor* pixels = static_cast<SkPMColor*>(pixelPtr);
2291 for (int y = 0; y < info.height(); ++y) {
2292 for (int x = 0; x < info.width(); ++x) {
2293 pixels[y * info.width() + x] = 0xff223344 + y * 0x000C0811;
2294 }
2295 }
2296 return true;
2297 }
2298};
2299##
2300void draw(SkCanvas* canvas) {
2301 auto gen = std::unique_ptr<TestImageGenerator>(new TestImageGenerator());
2302 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
2303 SkString lazy(image->isLazyGenerated() ? "is lazy" : "not lazy");
2304 canvas->scale(8, 8);
2305 canvas->drawImage(image, 0, 0, nullptr);
2306 SkPaint paint;
2307 paint.setTextSize(4);
2308 canvas->drawString(lazy, 2, 5, paint);
2309}
Cary Clarka560c472017-11-27 10:44:06 -05002310##
2311
Cary Clarkf5404bb2018-01-05 12:10:09 -05002312#Example
2313#Image 5
2314#Platform gpu
2315void draw(SkCanvas* canvas) {
2316 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2317 if (nullptr == image) {
2318 return;
2319 }
2320 SkPaint paint;
2321 paint.setAntiAlias(true);
2322 paint.setTextAlign(SkPaint::kCenter_Align);
2323 canvas->drawImage(image, 0, 0);
2324 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
2325 canvas->drawString(
2326 image->isLazyGenerated() ? "is lazily generated" : "not lazily generated",
2327 image->width() / 2, image->height() * 3 / 4, paint);
2328 };
2329 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2330 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04002331 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
2332 kOpaque_SkAlphaType, nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05002333 drawImage(image, "image");
2334 canvas->translate(image->width(), 0);
2335 drawImage(bitmapImage, "source");
2336 canvas->translate(-image->width(), image->height());
2337 drawImage(textureImage, "backEndTexture");
2338}
2339##
2340
Cary Clarkac47b882018-01-11 10:35:44 -05002341#SeeAlso isTextureBacked MakeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002342
2343#Method ##
2344
2345# ------------------------------------------------------------------------------
2346
Cary Clarke80cd442018-07-17 13:19:56 -04002347#Method sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target) const
Cary Clark4855f782018-02-06 09:41:53 -05002348#In Constructor
2349#Line # creates Image matching Color_Space if possible ##
Cary Clarka560c472017-11-27 10:44:06 -05002350
Cary Clarkac47b882018-01-11 10:35:44 -05002351Creates Image in target Color_Space.
2352Returns nullptr if Image could not be created.
Cary Clarka560c472017-11-27 10:44:06 -05002353
Cary Clarkac47b882018-01-11 10:35:44 -05002354Returns original Image if it is in target Color_Space.
2355Otherwise, converts pixels from Image Color_Space to target Color_Space.
2356If Image colorSpace returns nullptr, Image Color_Space is assumed to be sRGB.
2357
Cary Clarkac47b882018-01-11 10:35:44 -05002358#Param target Color_Space describing color range of returned Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002359
Cary Clarkac47b882018-01-11 10:35:44 -05002360#Return created Image in target Color_Space ##
2361
2362#Example
2363#Image 5
2364#Set sRGB
2365 sk_sp<SkColorSpace> normalColorSpace = SkColorSpace::MakeRGB(
2366 SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kSRGB_Gamut);
2367 sk_sp<SkColorSpace> wackyColorSpace = normalColorSpace->makeColorSpin();
2368 for (auto colorSpace : { normalColorSpace, wackyColorSpace } ) {
Cary Clarkc3c1c312018-07-18 09:25:15 -04002369 sk_sp<SkImage> colorSpaced = image->makeColorSpace(colorSpace);
2370 canvas->drawImage(colorSpaced, 0, 0);
2371 canvas->translate(128, 0);
Cary Clarkac47b882018-01-11 10:35:44 -05002372 }
2373##
2374
2375#SeeAlso MakeFromPixture MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05002376
2377#Method ##
2378
2379#Class SkImage ##
2380
2381#Topic Image ##