blob: 7f5832f2410b1c88bc2814dc86ca01d806b343ea [file] [log] [blame]
Cary Clarka560c472017-11-27 10:44:06 -05001#Topic Image
2#Alias Image_Reference
3
Cary Clark08895c42018-02-01 09:37:32 -05004#Subtopic Overview
Cary Clark4855f782018-02-06 09:41:53 -05005 #Subtopic Subtopic
Cary Clark08895c42018-02-01 09:37:32 -05006 #Populate
7 ##
8##
9
Cary Clarka560c472017-11-27 10:44:06 -050010#Class SkImage
11
Cary Clark61ca7c52018-01-02 11:34:14 -050012Image describes a two dimensional array of pixels to draw. The pixels may be
Cary Clark4855f782018-02-06 09:41:53 -050013decoded in a Raster_Bitmap, encoded in a Picture or compressed data stream,
Cary Clark61ca7c52018-01-02 11:34:14 -050014or located in GPU memory as a GPU_Texture.
15
16Image cannot be modified after it is created. Image may allocate additional
17storage as needed; for instance, an encoded Image may decode when drawn.
18
19Image width and height are greater than zero. Creating an Image with zero width
20or height returns Image equal to nullptr.
21
22Image may be created from Bitmap, Pixmap, Surface, Picture, encoded streams,
23GPU_Texture, YUV_ColorSpace data, or hardware buffer. Encoded streams supported
Cary Clark4855f782018-02-06 09:41:53 -050024include BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP. Supported encoding details
Cary Clark61ca7c52018-01-02 11:34:14 -050025vary with platform.
26
Cary Clark08895c42018-02-01 09:37:32 -050027#Subtopic Raster_Image
Cary Clark61ca7c52018-01-02 11:34:14 -050028#Alias Raster_Image
Cary Clark4855f782018-02-06 09:41:53 -050029#Line # pixels decoded in Raster_Bitmap ##
30Raster_Image pixels are decoded in a Raster_Bitmap. These pixels may be read
Cary Clark61ca7c52018-01-02 11:34:14 -050031directly and in most cases written to, although edited pixels may not be drawn
32if Image has been copied internally.
33##
34
Cary Clark08895c42018-02-01 09:37:32 -050035#Subtopic Texture_Image
36#Line # pixels located on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -050037Texture_Image are located on GPU and pixels are not accessible. Texture_Image
38are allocated optimally for best performance. Raster_Image may
39be drawn to GPU_Surface, but pixels are uploaded from CPU to GPU downgrading
40performance.
41##
42
Cary Clark08895c42018-02-01 09:37:32 -050043#Subtopic Lazy_Image
44#Line # deferred pixel buffer ##
Cary Clark61ca7c52018-01-02 11:34:14 -050045Lazy_Image defer allocating buffer for Image pixels and decoding stream until
46Image is drawn. Lazy_Image caches result if possible to speed up repeated
47drawing.
48##
Cary Clarka560c472017-11-27 10:44:06 -050049
Cary Clark4855f782018-02-06 09:41:53 -050050#Subtopic Related_Function
Cary Clark08895c42018-02-01 09:37:32 -050051#Populate
52##
Cary Clarka560c472017-11-27 10:44:06 -050053
Cary Clark4855f782018-02-06 09:41:53 -050054#Subtopic Class_or_Struct
Cary Clark08895c42018-02-01 09:37:32 -050055#Populate
Cary Clarka560c472017-11-27 10:44:06 -050056##
57
Cary Clark4855f782018-02-06 09:41:53 -050058#Subtopic Constructor
Cary Clark08895c42018-02-01 09:37:32 -050059#Populate
60##
Cary Clark5081eed2018-01-22 07:55:48 -050061
Cary Clark4855f782018-02-06 09:41:53 -050062#Subtopic Member_Function
Cary Clark08895c42018-02-01 09:37:32 -050063#Populate
64##
Cary Clarka560c472017-11-27 10:44:06 -050065
Cary Clarka560c472017-11-27 10:44:06 -050066#Typedef SkImageInfo Info
67
68##
69
Cary Clarka560c472017-11-27 10:44:06 -050070# ------------------------------------------------------------------------------
71
72#Method static sk_sp<SkImage> MakeRasterCopy(const SkPixmap& pixmap)
Cary Clark4855f782018-02-06 09:41:53 -050073#In Constructor
74#Line # creates Image from Pixmap and copied pixels ##
Cary Clark2f466242017-12-11 16:03:17 -050075Creates Image from Pixmap and copy of pixels. Since pixels are copied, Pixmap
76pixels may be modified or deleted without affecting Image.
Cary Clarka560c472017-11-27 10:44:06 -050077
Cary Clark3cd22cc2017-12-01 11:49:58 -050078Image is returned if Pixmap is valid. Valid Pixmap parameters include:
79dimensions are greater than zero;
80each dimension fits in 29 bits;
81Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
82row bytes are large enough to hold one row of pixels;
83pixel address is not nullptr.
84
85#Param pixmap Image_Info, pixel address, and row bytes ##
86
87#Return copy of Pixmap pixels, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -050088
89#Example
Cary Clark2f466242017-12-11 16:03:17 -050090#Height 50
91#Description
92Draw a five by five bitmap, and draw a copy in an Image. Editing the pixmap
93alters the bitmap draw, but does not alter the Image draw since the Image
94contains a copy of the pixels.
95##
96 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
97 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
98 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
99 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
100 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
101 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
102 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
103 SkBitmap bitmap;
104 bitmap.installPixels(pixmap);
105 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
106 *pixmap.writable_addr8(2, 2) = 0x00;
107 canvas->scale(10, 10);
108 canvas->drawBitmap(bitmap, 0, 0);
109 canvas->drawImage(image, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500110##
111
Cary Clark3cd22cc2017-12-01 11:49:58 -0500112#SeeAlso MakeRasterData MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500113
114#Method ##
115
116# ------------------------------------------------------------------------------
117
118#Method static sk_sp<SkImage> MakeRasterData(const Info& info, sk_sp<SkData> pixels, size_t rowBytes)
Cary Clark4855f782018-02-06 09:41:53 -0500119#In Constructor
120#Line # creates Image from Image_Info and shared pixels ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500121Creates Image from Image_Info, sharing pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500122
Cary Clark3cd22cc2017-12-01 11:49:58 -0500123Image is returned if Image_Info is valid. Valid Image_Info parameters include:
124dimensions are greater than zero;
125each dimension fits in 29 bits;
126Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
127rowBytes are large enough to hold one row of pixels;
128pixels is not nullptr, and contains enough data for Image.
129
130#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
131#Param pixels address or pixel storage ##
132#Param rowBytes size of pixel row or larger ##
133
134#Return Image sharing pixels, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500135
136#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500137#Image 3
138 size_t rowBytes = image->width() * SkColorTypeBytesPerPixel(kRGBA_8888_SkColorType);
139 sk_sp<SkData> data = SkData::MakeUninitialized(rowBytes * image->height());
140 SkImageInfo dstInfo = SkImageInfo::MakeN32(image->width(), image->height(),
141 kPremul_SkAlphaType);
142 image->readPixels(dstInfo, data->writable_data(), rowBytes, 0, 0, SkImage::kAllow_CachingHint);
143 sk_sp<SkImage> raw = SkImage::MakeRasterData(dstInfo.makeColorType(kRGBA_8888_SkColorType),
144 data, rowBytes);
145 canvas->drawImage(image, 0, 0);
146 canvas->drawImage(raw.get(), 128, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500147##
148
Cary Clark3cd22cc2017-12-01 11:49:58 -0500149#SeeAlso MakeRasterCopy MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500150
151#Method ##
152
153# ------------------------------------------------------------------------------
154
Cary Clark3cd22cc2017-12-01 11:49:58 -0500155#Typedef void* ReleaseContext
156
157Caller data passed to RasterReleaseProc; may be nullptr.
158
159#SeeAlso MakeFromRaster RasterReleaseProc
160
161##
162
Cary Clarka560c472017-11-27 10:44:06 -0500163#Typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext)
164
Cary Clark3cd22cc2017-12-01 11:49:58 -0500165Function called when Image no longer shares pixels. ReleaseContext is
166provided by caller when Image is created, and may be nullptr.
167
168#SeeAlso ReleaseContext MakeFromRaster
169
Cary Clarka560c472017-11-27 10:44:06 -0500170##
171
172#Method static sk_sp<SkImage> MakeFromRaster(const SkPixmap& pixmap,
173 RasterReleaseProc rasterReleaseProc,
174 ReleaseContext releaseContext)
Cary Clark4855f782018-02-06 09:41:53 -0500175#In Constructor
176#Line # creates Image from Pixmap, with release ##
Cary Clarka560c472017-11-27 10:44:06 -0500177
Cary Clark0c5f5462017-12-15 11:21:51 -0500178Creates Image from pixmap, sharing Pixmap pixels. Pixels must remain valid and
Cary Clark3cd22cc2017-12-01 11:49:58 -0500179unchanged until rasterReleaseProc is called. rasterReleaseProc is passed
180releaseContext when Image is deleted or no longer refers to pixmap pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500181
Cary Clark0c5f5462017-12-15 11:21:51 -0500182Pass nullptr for rasterReleaseProc to share Pixmap without requiring a callback
183when Image is released. Pass nullptr for releaseContext if rasterReleaseProc
184does not require state.
185
Cary Clark3cd22cc2017-12-01 11:49:58 -0500186Image is returned if pixmap is valid. Valid Pixmap parameters include:
187dimensions are greater than zero;
188each dimension fits in 29 bits;
189Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
190row bytes are large enough to hold one row of pixels;
191pixel address is not nullptr.
192
193#Param pixmap Image_Info, pixel address, and row bytes ##
Cary Clark0c5f5462017-12-15 11:21:51 -0500194#Param rasterReleaseProc function called when pixels can be released; or nullptr ##
195#Param releaseContext state passed to rasterReleaseProc; or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500196
Cary Clark0c5f5462017-12-15 11:21:51 -0500197#Return Image sharing pixmap ##
Cary Clarka560c472017-11-27 10:44:06 -0500198
199#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500200#Function
201static void releaseProc(const void* pixels, SkImage::ReleaseContext context) {
202 int* countPtr = static_cast<int*>(context);
203 *countPtr += 1;
204}
205##
206
207void draw(SkCanvas* canvas) {
208 SkColor color = 0;
209 SkPixmap pixmap(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), &color, 4);
210 int releaseCount = 0;
211 sk_sp<SkImage> image(SkImage::MakeFromRaster(pixmap, releaseProc, &releaseCount));
212 SkDebugf("before reset: %d\n", releaseCount);
213 image.reset();
214 SkDebugf("after reset: %d\n", releaseCount);
215}
216#StdOut
217before reset: 0
218after reset: 1
219##
Cary Clarka560c472017-11-27 10:44:06 -0500220##
221
Cary Clark3cd22cc2017-12-01 11:49:58 -0500222#SeeAlso MakeRasterCopy MakeRasterData MakeFromGenerator RasterReleaseProc ReleaseContext
Cary Clarka560c472017-11-27 10:44:06 -0500223
224#Method ##
225
226# ------------------------------------------------------------------------------
227
228#Method static sk_sp<SkImage> MakeFromBitmap(const SkBitmap& bitmap)
Cary Clark4855f782018-02-06 09:41:53 -0500229#In Constructor
230#Line # creates Image from Bitmap, sharing or copying pixels ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500231Creates Image from bitmap, sharing or copying bitmap pixels. If the bitmap
232is marked immutable, and its pixel memory is shareable, it may be shared
233instead of copied.
Cary Clarka560c472017-11-27 10:44:06 -0500234
Cary Clark3cd22cc2017-12-01 11:49:58 -0500235Image is returned if bitmap is valid. Valid Bitmap parameters include:
236dimensions are greater than zero;
237each dimension fits in 29 bits;
238Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
239row bytes are large enough to hold one row of pixels;
240pixel address is not nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500241
Cary Clark3cd22cc2017-12-01 11:49:58 -0500242#Param bitmap Image_Info, row bytes, and pixels ##
243
244#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500245
246#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500247#Description
248The first Bitmap is shared; writing to the pixel memory changes the first
249Image.
250The second Bitmap is marked immutable, and is copied; writing to the pixel
251memory does not alter the second Image.
252##
253#Height 50
254 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
255 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
256 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
257 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
258 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
259 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
260 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
261 SkBitmap bitmap;
262 bitmap.installPixels(pixmap);
263 sk_sp<SkImage> image1 = SkImage::MakeFromBitmap(bitmap);
264 bitmap.setImmutable();
265 sk_sp<SkImage> image2 = SkImage::MakeFromBitmap(bitmap);
266 *pixmap.writable_addr8(2, 2) = 0x00;
267 canvas->scale(10, 10);
268 canvas->drawImage(image1, 0, 0);
269 canvas->drawImage(image2, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500270##
271
Cary Clark3cd22cc2017-12-01 11:49:58 -0500272#SeeAlso MakeFromRaster MakeRasterCopy MakeFromGenerator MakeRasterData
Cary Clarka560c472017-11-27 10:44:06 -0500273
274#Method ##
275
276# ------------------------------------------------------------------------------
277
278#Method static sk_sp<SkImage> MakeFromGenerator(std::unique_ptr<SkImageGenerator> imageGenerator,
279 const SkIRect* subset = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500280#In Constructor
281#Line # creates Image from a stream of data ##
Cary Clarka560c472017-11-27 10:44:06 -0500282
Cary Clark0c5f5462017-12-15 11:21:51 -0500283Creates Image from data returned by imageGenerator. Generated data is owned by Image and may not
284be shared or accessed.
Cary Clarka560c472017-11-27 10:44:06 -0500285
Cary Clark0c5f5462017-12-15 11:21:51 -0500286subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
287otherwise, subset must be contained by image bounds.
288
289Image is returned if generator data is valid. Valid data parameters vary by type of data
290and platform.
Cary Clarka560c472017-11-27 10:44:06 -0500291
Cary Clark3cd22cc2017-12-01 11:49:58 -0500292imageGenerator may wrap Picture data, codec data, or custom data.
293
294#Param imageGenerator stock or custom routines to retrieve Image ##
295#Param subset bounds of returned Image; may be nullptr ##
296
297#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500298
299#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500300#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500301#Description
302The generator returning Picture cannot be shared; std::move transfers ownership to generated Image.
303##
304 SkPictureRecorder recorder;
305 recorder.beginRecording(100, 100)->drawColor(SK_ColorRED);
306 auto picture = recorder.finishRecordingAsPicture();
307 auto gen = SkImageGenerator::MakeFromPicture({100, 100}, picture, nullptr, nullptr,
308 SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB());
309 sk_sp<SkImage> image = SkImage::MakeFromGenerator(std::move(gen));
310 canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500311##
312
Cary Clark3cd22cc2017-12-01 11:49:58 -0500313#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500314
315#Method ##
316
317# ------------------------------------------------------------------------------
318
319#Method static sk_sp<SkImage> MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500320#In Constructor
321#Line # creates Image from encoded data ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500322Creates Image from encoded data.
Cary Clark0c5f5462017-12-15 11:21:51 -0500323subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
324otherwise, subset must be contained by image bounds.
Cary Clarka560c472017-11-27 10:44:06 -0500325
Cary Clark3cd22cc2017-12-01 11:49:58 -0500326Image is returned if format of the encoded data is recognized and supported.
Cary Clark4855f782018-02-06 09:41:53 -0500327Recognized formats vary by platform.
Cary Clarka560c472017-11-27 10:44:06 -0500328
Cary Clark3cd22cc2017-12-01 11:49:58 -0500329#Param encoded data of Image to decode ##
330#Param subset bounds of returned Image; may be nullptr ##
331
332#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500333
Cary Clark61ca7c52018-01-02 11:34:14 -0500334#Example
335#Image 3
336int x = 0;
337for (int quality : { 100, 50, 10, 1} ) {
338 sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, quality);
339 sk_sp<SkImage> image = SkImage::MakeFromEncoded(encodedData);
340 canvas->drawImage(image, x, 0);
341 x += 64;
342}
Cary Clarka560c472017-11-27 10:44:06 -0500343##
344
Cary Clark3cd22cc2017-12-01 11:49:58 -0500345#SeeAlso MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500346
347#Method ##
348
349# ------------------------------------------------------------------------------
350
351#Typedef void (*TextureReleaseProc)(ReleaseContext releaseContext)
352
353##
354
355#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
356 const GrBackendTexture& backendTexture,
357 GrSurfaceOrigin origin,
358 SkAlphaType alphaType,
359 sk_sp<SkColorSpace> colorSpace)
Cary Clarkac47b882018-01-11 10:35:44 -0500360#Deprecated
Cary Clark61ca7c52018-01-02 11:34:14 -0500361#Method ##
362
363# ------------------------------------------------------------------------------
364
365#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
366 const GrBackendTexture& backendTexture,
367 GrSurfaceOrigin origin,
368 SkAlphaType alphaType,
369 sk_sp<SkColorSpace> colorSpace,
370 TextureReleaseProc textureReleaseProc,
371 ReleaseContext releaseContext)
372
Cary Clarkac47b882018-01-11 10:35:44 -0500373#Deprecated
Cary Clark61ca7c52018-01-02 11:34:14 -0500374#Method ##
375
376# ------------------------------------------------------------------------------
377
378#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
379 const GrBackendTexture& backendTexture,
380 GrSurfaceOrigin origin,
381 SkColorType colorType,
382 SkAlphaType alphaType,
383 sk_sp<SkColorSpace> colorSpace)
Cary Clarkf895a422018-02-27 09:54:21 -0500384#In Constructor
385#Line # creates Image from GPU_Texture ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500386Creates Image from GPU_Texture associated with context. Caller is responsible for
387managing the lifetime of GPU_Texture.
388
389Image is returned if format of backendTexture is recognized and supported.
390Recognized formats vary by GPU back-end.
391
392#Param context GPU_Context ##
393#Param backendTexture texture residing on GPU ##
394#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
395#Param colorType one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
396 kRGB_565_SkColorType, kARGB_4444_SkColorType,
397 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
398 kGray_8_SkColorType, kRGBA_F16_SkColorType
399##
400#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
401 kPremul_SkAlphaType, kUnpremul_SkAlphaType
402##
403#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500404
405#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500406
407#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500408#Image 3
409#Platform gpu
410#Height 128
411#Description
412A back-end texture has been created and uploaded to the GPU outside of this example.
413##
414GrContext* context = canvas->getGrContext();
415if (!context) {
416 return;
417}
418canvas->scale(.25f, .25f);
419int x = 0;
420for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500421 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark56356312018-02-08 14:45:18 -0500422 origin, kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
Cary Clark0c5f5462017-12-15 11:21:51 -0500423 canvas->drawImage(image, x, 0);
424x += 512;
425}
Cary Clarka560c472017-11-27 10:44:06 -0500426##
427
Cary Clark3cd22cc2017-12-01 11:49:58 -0500428#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500429
430#Method ##
431
432# ------------------------------------------------------------------------------
433
434#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
435 const GrBackendTexture& backendTexture,
436 GrSurfaceOrigin origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500437 SkColorType colorType,
Cary Clarka560c472017-11-27 10:44:06 -0500438 SkAlphaType alphaType,
439 sk_sp<SkColorSpace> colorSpace,
440 TextureReleaseProc textureReleaseProc,
441 ReleaseContext releaseContext)
442
Cary Clark61ca7c52018-01-02 11:34:14 -0500443Creates Image from GPU_Texture associated with context. GPU_Texture must stay
Cary Clark3cd22cc2017-12-01 11:49:58 -0500444valid and unchanged until textureReleaseProc is called. textureReleaseProc is
445passed releaseContext when Image is deleted or no longer refers to texture.
Cary Clarka560c472017-11-27 10:44:06 -0500446
Cary Clark3cd22cc2017-12-01 11:49:58 -0500447Image is returned if format of backendTexture is recognized and supported.
448Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500449
Cary Clark3cd22cc2017-12-01 11:49:58 -0500450#Param context GPU_Context ##
451#Param backendTexture texture residing on GPU ##
452#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500453#Param colorType one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
454 kRGB_565_SkColorType, kARGB_4444_SkColorType,
455 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
456 kGray_8_SkColorType, kRGBA_F16_SkColorType
457##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500458#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
459 kPremul_SkAlphaType, kUnpremul_SkAlphaType
460##
Cary Clark61ca7c52018-01-02 11:34:14 -0500461#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500462#Param textureReleaseProc function called when texture can be released ##
463#Param releaseContext state passed to textureReleaseProc ##
464
465#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500466
Cary Clark0c5f5462017-12-15 11:21:51 -0500467#ToDo
468This doesn't do anything clever with TextureReleaseProc because it may not get called
Cary Clark61ca7c52018-01-02 11:34:14 -0500469fwithin the lifetime of the example
Cary Clark0c5f5462017-12-15 11:21:51 -0500470##
471
Cary Clarka560c472017-11-27 10:44:06 -0500472#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500473#Platform gpu
474#Image 4
Cary Clark0c5f5462017-12-15 11:21:51 -0500475GrContext* context = canvas->getGrContext();
476if (!context) {
477 return;
478}
Cary Clarkac47b882018-01-11 10:35:44 -0500479auto debugster = [](SkImage::ReleaseContext releaseContext) -> void {
480 *((int *) releaseContext) += 128;
Cary Clark0c5f5462017-12-15 11:21:51 -0500481};
482int x = 0;
483for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500484 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark61ca7c52018-01-02 11:34:14 -0500485 origin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr, debugster, &x);
Cary Clark0c5f5462017-12-15 11:21:51 -0500486 canvas->drawImage(image, x, 0);
487 x += 128;
488}
Cary Clarka560c472017-11-27 10:44:06 -0500489##
490
Cary Clark3cd22cc2017-12-01 11:49:58 -0500491#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500492
493#Method ##
494
495# ------------------------------------------------------------------------------
496
497#Method static sk_sp<SkImage> MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> data,
498 bool buildMips,
499 SkColorSpace* dstColorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500500#In Constructor
501#Line # creates Image from encoded data, and uploads to GPU ##
Cary Clarka560c472017-11-27 10:44:06 -0500502
Cary Clark3cd22cc2017-12-01 11:49:58 -0500503Creates Image from encoded data. Image is uploaded to GPU back-end using context.
504
505Created Image is available to other GPU contexts, and is available across thread
506boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
507share resources.
508
509When Image is no longer referenced, context releases texture memory
Cary Clarka560c472017-11-27 10:44:06 -0500510asynchronously.
Cary Clarka560c472017-11-27 10:44:06 -0500511
Cary Clark3cd22cc2017-12-01 11:49:58 -0500512Texture decoded from data is uploaded to match Surface created with
513dstColorSpace. Color_Space of Image is determined by encoded data.
Cary Clarka560c472017-11-27 10:44:06 -0500514
Cary Clark3cd22cc2017-12-01 11:49:58 -0500515Image is returned if format of data is recognized and supported, and if context
516supports moving resources. Recognized formats vary by platform and GPU back-end.
517
Cary Clark61ca7c52018-01-02 11:34:14 -0500518Image is returned using MakeFromEncoded if context is nullptr or does not support
519moving resources between contexts.
520
Cary Clark3cd22cc2017-12-01 11:49:58 -0500521#Param context GPU_Context ##
522#Param data Image to decode ##
523#Param buildMips create Image as Mip_Map if true ##
524#Param dstColorSpace range of colors of matching Surface on GPU ##
525
526#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500527
528#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500529#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500530#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500531GrContext* context = canvas->getGrContext();
532sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, 100);
533sk_sp<SkImage> image = SkImage::MakeCrossContextFromEncoded(context,
534 encodedData, false, nullptr);
535canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500536##
537
Cary Clark3cd22cc2017-12-01 11:49:58 -0500538#SeeAlso MakeCrossContextFromPixmap
539
540#Method ##
541
542# ------------------------------------------------------------------------------
543
544#Method static sk_sp<SkImage> MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap,
545 bool buildMips,
546 SkColorSpace* dstColorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500547#In Constructor
548#Line # creates Image from Pixmap, and uploads to GPU ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500549
550Creates Image from pixmap. Image is uploaded to GPU back-end using context.
551
552Created Image is available to other GPU contexts, and is available across thread
553boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
554share resources.
555
556When Image is no longer referenced, context releases texture memory
557asynchronously.
558
559Texture created from pixmap is uploaded to match Surface created with
560dstColorSpace. Color_Space of Image is determined by pixmap.colorSpace().
561
Cary Clark61ca7c52018-01-02 11:34:14 -0500562Image is returned referring to GPU back-end if context is not nullptr,
563format of data is recognized and supported, and if context supports moving
564resources between contexts. Otherwise, pixmap pixel data is copied and Image
565as returned in raster format if possible; nullptr may be returned.
566Recognized GPU formats vary by platform and GPU back-end.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500567
568#Param context GPU_Context ##
569#Param pixmap Image_Info, pixel address, and row bytes ##
570#Param buildMips create Image as Mip_Map if true ##
571#Param dstColorSpace range of colors of matching Surface on GPU ##
572
573#Return created Image, or nullptr ##
574
575#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500576#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500577#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500578GrContext* context = canvas->getGrContext();
579SkPixmap pixmap;
580if (source.peekPixels(&pixmap)) {
581 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(context, pixmap,
582 false, nullptr);
583 canvas->drawImage(image, 0, 0);
584}
Cary Clark3cd22cc2017-12-01 11:49:58 -0500585##
586
587#SeeAlso MakeCrossContextFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500588
589#Method ##
590
591# ------------------------------------------------------------------------------
592
593#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
594 const GrBackendTexture& backendTexture,
595 GrSurfaceOrigin surfaceOrigin,
596 SkAlphaType alphaType = kPremul_SkAlphaType,
597 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500598#Deprecated
Cary Clark61ca7c52018-01-02 11:34:14 -0500599#Method ##
600
601# ------------------------------------------------------------------------------
602
603#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
604 const GrBackendTexture& backendTexture,
605 GrSurfaceOrigin surfaceOrigin,
606 SkColorType colorType,
607 SkAlphaType alphaType = kPremul_SkAlphaType,
608 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500609#In Constructor
610#Line # creates Image from GPU_Texture, managed internally ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500611Creates Image from backendTexture associated with context. backendTexture and
612returned Image are managed internally, and are released when no longer needed.
Cary Clarka560c472017-11-27 10:44:06 -0500613
Cary Clark3cd22cc2017-12-01 11:49:58 -0500614Image is returned if format of backendTexture is recognized and supported.
615Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500616
Cary Clark3cd22cc2017-12-01 11:49:58 -0500617#Param context GPU_Context ##
618#Param backendTexture texture residing on GPU ##
619#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500620#Param colorType one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
621 kRGB_565_SkColorType, kARGB_4444_SkColorType,
622 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
623 kGray_8_SkColorType, kRGBA_F16_SkColorType
624##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500625#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
626 kPremul_SkAlphaType, kUnpremul_SkAlphaType
627##
Cary Clark61ca7c52018-01-02 11:34:14 -0500628#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500629
630#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500631
632#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500633#Image 5
634#Platform gpu
Cary Clark61ca7c52018-01-02 11:34:14 -0500635 if (!canvas->getGrContext()) {
636 return;
637 }
638 canvas->scale(.5f, .5f);
639 canvas->clear(0x7f3f5f7f);
640 int x = 0, y = 0;
641 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
642 for (auto alpha : { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType } ) {
643 sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(canvas->getGrContext(),
644 backEndTexture, origin,
645 kRGBA_8888_SkColorType, alpha);
646 canvas->drawImage(image, x, y);
647 x += 160;
648 }
649 x -= 160 * 3;
650 y += 256;
651 }
Cary Clarka560c472017-11-27 10:44:06 -0500652##
653
Cary Clark61ca7c52018-01-02 11:34:14 -0500654#SeeAlso MakeFromTexture MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500655
656#Method ##
657
658# ------------------------------------------------------------------------------
659
660#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
661 const GrBackendObject yuvTextureHandles[3],
662 const SkISize yuvSizes[3],
663 GrSurfaceOrigin surfaceOrigin,
664 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500665#In Constructor
666#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500667
Cary Clark61ca7c52018-01-02 11:34:14 -0500668Creates Image from copy of yuvTextureHandles, an array of textures on GPU.
669yuvTextureHandles contain pixels for YUV planes of Image.
Cary Clark4855f782018-02-06 09:41:53 -0500670yuvSizes contain dimensions for each pixel plane. Dimensions must be greater than
Cary Clark61ca7c52018-01-02 11:34:14 -0500671zero but may differ from plane to plane. Returned Image has the dimensions
672yuvSizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500673
Cary Clark61ca7c52018-01-02 11:34:14 -0500674#Param context GPU_Context ##
675#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
676 kRec709_SkYUVColorSpace
677##
678#Param yuvTextureHandles array of YUV textures on GPU ##
679#Param yuvSizes dimensions of YUV textures ##
680#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
681#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500682
Cary Clark61ca7c52018-01-02 11:34:14 -0500683#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500684
Cary Clark61ca7c52018-01-02 11:34:14 -0500685# seems too complicated to create an example for this
686#ToDo
687should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500688##
689
Cary Clark61ca7c52018-01-02 11:34:14 -0500690#NoExample
691##
692
693#SeeAlso MakeFromNV12TexturesCopy
694
695#Method ##
696
697# ------------------------------------------------------------------------------
698
699#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
700 const GrBackendTexture yuvTextureHandles[3],
701 const SkISize yuvSizes[3],
702 GrSurfaceOrigin surfaceOrigin,
703 sk_sp<SkColorSpace> colorSpace = nullptr)
704
705Creates Image from copy of yuvTextureHandles, an array of textures on GPU.
706yuvTextureHandles contain pixels for YUV planes of Image.
Cary Clark4855f782018-02-06 09:41:53 -0500707yuvSizes contain dimensions for each pixel plane. Dimensions must be greater than
Cary Clark61ca7c52018-01-02 11:34:14 -0500708zero but may differ from plane to plane. Returned Image has the dimensions
709yuvSizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
710
711#Param context GPU_Context ##
712#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
713 kRec709_SkYUVColorSpace
714##
715#Param yuvTextureHandles array of YUV textures on GPU ##
716#Param yuvSizes dimensions of YUV textures ##
717#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
718#Param colorSpace range of colors; may be nullptr ##
719
720#Return created Image, or nullptr ##
721
722# seems too complicated to create an example for this
723#ToDo
724should this be moved to chrome only?
725##
726
727#NoExample
728##
729
730#SeeAlso MakeFromNV12TexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500731
732#Method ##
733
734# ------------------------------------------------------------------------------
735
736#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
737 SkYUVColorSpace yuvColorSpace,
738 const GrBackendObject nv12TextureHandles[2],
739 const SkISize nv12Sizes[2],
740 GrSurfaceOrigin surfaceOrigin,
741 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500742#In Constructor
743#Line # creates Image from YUV_ColorSpace data in two planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500744
Cary Clark61ca7c52018-01-02 11:34:14 -0500745Creates Image from copy of nv12TextureHandles, an array of textures on GPU.
746nv12TextureHandles[0] contains pixels for YUV_Component_Y plane.
747nv12TextureHandles[1] contains pixels for YUV_Component_U plane,
748followed by pixels for YUV_Component_V plane.
Cary Clark4855f782018-02-06 09:41:53 -0500749nv12Sizes contain dimensions for each pixel plane. Dimensions must be greater than
Cary Clark61ca7c52018-01-02 11:34:14 -0500750zero but may differ from plane to plane. Returned Image has the dimensions
751nv12Sizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500752
Cary Clark61ca7c52018-01-02 11:34:14 -0500753#Param context GPU_Context ##
754#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
755 kRec709_SkYUVColorSpace
756##
757#Param nv12TextureHandles array of YUV textures on GPU ##
758#Param nv12Sizes dimensions of YUV textures ##
759#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
760#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500761
Cary Clark61ca7c52018-01-02 11:34:14 -0500762#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500763
Cary Clark61ca7c52018-01-02 11:34:14 -0500764# seems too complicated to create an example for this
765#ToDo
766should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500767##
768
Cary Clark61ca7c52018-01-02 11:34:14 -0500769#NoExample
770##
771
772#SeeAlso MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500773
774#Method ##
775
776# ------------------------------------------------------------------------------
777
Cary Clark61ca7c52018-01-02 11:34:14 -0500778#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
779 SkYUVColorSpace yuvColorSpace,
780 const GrBackendTexture nv12TextureHandles[2],
781 const SkISize nv12Sizes[2],
782 GrSurfaceOrigin surfaceOrigin,
783 sk_sp<SkColorSpace> colorSpace = nullptr)
784
785Creates Image from copy of nv12TextureHandles, an array of textures on GPU.
786nv12TextureHandles[0] contains pixels for YUV_Component_Y plane.
787nv12TextureHandles[1] contains pixels for YUV_Component_U plane,
788followed by pixels for YUV_Component_V plane.
Cary Clark4855f782018-02-06 09:41:53 -0500789nv12Sizes contain dimensions for each pixel plane. Dimensions must be greater than
Cary Clark61ca7c52018-01-02 11:34:14 -0500790zero but may differ from plane to plane. Returned Image has the dimensions
791nv12Sizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
792
793#Param context GPU_Context ##
794#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
795 kRec709_SkYUVColorSpace
796##
797#Param nv12TextureHandles array of YUV textures on GPU ##
798#Param nv12Sizes dimensions of YUV textures ##
799#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
800#Param colorSpace range of colors; may be nullptr ##
801
802#Return created Image, or nullptr ##
803
804# seems too complicated to create an example for this
805#ToDo
806should this be moved to chrome only?
807##
808
809#NoExample
810##
811
812#SeeAlso MakeFromYUVTexturesCopy
813
814#Method ##
815
816# ------------------------------------------------------------------------------
817
Cary Clark4855f782018-02-06 09:41:53 -0500818# currently uncalled by any test or client ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500819#Bug 7424
Cary Clark61ca7c52018-01-02 11:34:14 -0500820
Cary Clark56356312018-02-08 14:45:18 -0500821#EnumClass BitDepth
Cary Clarka560c472017-11-27 10:44:06 -0500822
823#Code
Cary Clark61ca7c52018-01-02 11:34:14 -0500824 enum class BitDepth {
Cary Clarka560c472017-11-27 10:44:06 -0500825 kU8,
826 kF16,
827 };
828##
829
830#Const kU8 0
Cary Clark61ca7c52018-01-02 11:34:14 -0500831Use 8 bits per Color_ARGB component using unsigned integer format.
Cary Clarka560c472017-11-27 10:44:06 -0500832##
833#Const kF16 1
Cary Clark61ca7c52018-01-02 11:34:14 -0500834Use 16 bits per Color_ARGB component using half-precision floating point format.
Cary Clarka560c472017-11-27 10:44:06 -0500835##
836
Cary Clark61ca7c52018-01-02 11:34:14 -0500837#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500838##
839
Cary Clark61ca7c52018-01-02 11:34:14 -0500840#SeeAlso MakeFromPicture
Cary Clarka560c472017-11-27 10:44:06 -0500841
Cary Clark56356312018-02-08 14:45:18 -0500842#EnumClass ##
Cary Clarka560c472017-11-27 10:44:06 -0500843
844# ------------------------------------------------------------------------------
845
846#Method static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
847 const SkMatrix* matrix, const SkPaint* paint,
848 BitDepth bitDepth,
849 sk_sp<SkColorSpace> colorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500850#In Constructor
851#Line # creates Image from Picture ##
Cary Clarka560c472017-11-27 10:44:06 -0500852
Cary Clark61ca7c52018-01-02 11:34:14 -0500853Creates Image from picture. Returned Image width and height are set by dimensions.
854Image draws picture with matrix and paint, set to bitDepth and colorSpace.
Cary Clarka560c472017-11-27 10:44:06 -0500855
Cary Clark61ca7c52018-01-02 11:34:14 -0500856If matrix is nullptr, draws with identity Matrix. If paint is nullptr, draws
857with default Paint. colorSpace may be nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500858
Cary Clark61ca7c52018-01-02 11:34:14 -0500859#Param picture stream of drawing commands ##
860#Param dimensions width and height ##
861#Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ##
862#Param paint Paint to apply transparency, filtering, and so on; may be nullptr ##
863#Param bitDepth 8 bit integer or 16 bit float: per component ##
864#Param colorSpace range of colors; may be nullptr ##
865
866#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500867
868#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500869 SkPaint paint;
870 SkPictureRecorder recorder;
871 SkCanvas* recordingCanvas = recorder.beginRecording(50, 50);
872 for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) {
873 paint.setColor(color);
874 recordingCanvas->drawRect({10, 10, 30, 40}, paint);
875 recordingCanvas->translate(10, 10);
876 recordingCanvas->scale(1.2f, 1.4f);
877 }
878 sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
879 int x = 0, y = 0;
880 for (auto alpha : { 70, 140, 210 } ) {
881 paint.setAlpha(alpha);
882 auto srgbColorSpace = SkColorSpace::MakeSRGB();
883 sk_sp<SkImage> image = SkImage::MakeFromPicture(playback, {50, 50}, nullptr, &paint,
884 SkImage::BitDepth::kU8, srgbColorSpace);
885 canvas->drawImage(image, x, y);
886 x += 70; y += 70;
887 }
Cary Clarka560c472017-11-27 10:44:06 -0500888##
889
Cary Clark61ca7c52018-01-02 11:34:14 -0500890#SeeAlso SkCanvas::drawPicture
Cary Clarka560c472017-11-27 10:44:06 -0500891
892#Method ##
893
894# ------------------------------------------------------------------------------
895
896#Method static sk_sp<SkImage> MakeFromAHardwareBuffer(AHardwareBuffer* hardwareBuffer,
897 SkAlphaType alphaType = kPremul_SkAlphaType,
898 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500899#In Constructor
900#Line # creates Image from Android hardware buffer ##
Cary Clarka560c472017-11-27 10:44:06 -0500901
Cary Clark4855f782018-02-06 09:41:53 -0500902#Bug 7447
Cary Clarka560c472017-11-27 10:44:06 -0500903
Cary Clark61ca7c52018-01-02 11:34:14 -0500904Creates Image from Android hardware buffer.
905Returned Image takes a reference on the buffer.
Cary Clarka560c472017-11-27 10:44:06 -0500906
Cary Clark61ca7c52018-01-02 11:34:14 -0500907Only available on Android, when __ANDROID_API__ is defined to be 26 or greater.
Cary Clarka560c472017-11-27 10:44:06 -0500908
Cary Clark61ca7c52018-01-02 11:34:14 -0500909#Param hardwareBuffer AHardwareBuffer Android hardware buffer ##
910#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
911 kPremul_SkAlphaType, kUnpremul_SkAlphaType
912##
913#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500914
Cary Clark61ca7c52018-01-02 11:34:14 -0500915#Return created Image, or nullptr ##
916
917#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500918##
919
Cary Clark61ca7c52018-01-02 11:34:14 -0500920#SeeAlso MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -0500921
922#Method ##
923
924# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -0500925#Subtopic Property
926#Populate
927#Line # values and attributes ##
928##
Cary Clarka560c472017-11-27 10:44:06 -0500929
930#Method int width() const
Cary Clark4855f782018-02-06 09:41:53 -0500931#In Property
932#Line # returns pixel column count ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500933Returns pixel count in each row.
934
935#Return pixel width in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500936
937#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500938#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500939#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500940 canvas->translate(10, 10);
941 canvas->drawImage(image, 0, 0);
942 canvas->translate(0, image->height());
943 SkPaint paint;
944 paint.setTextAlign(SkPaint::kCenter_Align);
945 canvas->drawLine(0, 10, image->width(), 10, paint);
946 canvas->drawString("width", image->width() / 2, 25, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500947##
948
Cary Clark61ca7c52018-01-02 11:34:14 -0500949#SeeAlso dimensions() height()
Cary Clarka560c472017-11-27 10:44:06 -0500950
951#Method ##
952
953# ------------------------------------------------------------------------------
954
955#Method int height() const
Cary Clark4855f782018-02-06 09:41:53 -0500956#In Property
957#Line # returns pixel row count ##
Cary Clark2f466242017-12-11 16:03:17 -0500958Returns pixel row count.
959
Cary Clark61ca7c52018-01-02 11:34:14 -0500960#Return pixel height in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500961
962#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500963#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500964#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500965 canvas->translate(10, 10);
966 canvas->drawImage(image, 0, 0);
967 canvas->translate(image->width(), 0);
968 SkPaint paint;
969 paint.setTextAlign(SkPaint::kCenter_Align);
970 paint.setVerticalText(true);
971 canvas->drawLine(10, 0, 10, image->height(), paint);
Cary Clarkac47b882018-01-11 10:35:44 -0500972 canvas->drawString("height", 25, image->height() / 2, paint);
973##
Cary Clarka560c472017-11-27 10:44:06 -0500974
Cary Clark61ca7c52018-01-02 11:34:14 -0500975#SeeAlso dimensions() width()
Cary Clarka560c472017-11-27 10:44:06 -0500976
977#Method ##
978
979# ------------------------------------------------------------------------------
980
981#Method SkISize dimensions() const
Cary Clark4855f782018-02-06 09:41:53 -0500982#In Property
983#Line # returns width() and height() ##
Cary Clark2f466242017-12-11 16:03:17 -0500984Returns ISize { width(), height() }.
985
986#Return integral size of width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -0500987
988#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500989#Image 4
990 SkISize dimensions = image->dimensions();
991 SkIRect bounds = image->bounds();
992 SkIRect dimensionsAsBounds = SkIRect::MakeSize(dimensions);
993 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clarka560c472017-11-27 10:44:06 -0500994##
995
Cary Clark61ca7c52018-01-02 11:34:14 -0500996#SeeAlso height() width() bounds()
Cary Clarka560c472017-11-27 10:44:06 -0500997
998#Method ##
999
1000# ------------------------------------------------------------------------------
1001
1002#Method SkIRect bounds() const
Cary Clark4855f782018-02-06 09:41:53 -05001003#In Property
1004#Line # returns width() and height() as Rectangle ##
Cary Clark2f466242017-12-11 16:03:17 -05001005Returns IRect { 0, 0, width(), height() }.
1006
1007#Return integral rectangle from origin to width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -05001008
1009#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001010#Height 128
1011#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -05001012 SkIRect bounds = image->bounds();
Cary Clarkac47b882018-01-11 10:35:44 -05001013 for (int x : { 0, bounds.width() } ) {
1014 for (int y : { 0, bounds.height() } ) {
Cary Clark61ca7c52018-01-02 11:34:14 -05001015 canvas->drawImage(image, x, y);
1016 }
1017 }
Cary Clarka560c472017-11-27 10:44:06 -05001018##
1019
Cary Clark61ca7c52018-01-02 11:34:14 -05001020#SeeAlso dimensions()
Cary Clarka560c472017-11-27 10:44:06 -05001021
1022#Method ##
1023
1024# ------------------------------------------------------------------------------
1025
1026#Method uint32_t uniqueID() const
Cary Clark4855f782018-02-06 09:41:53 -05001027#In Property
1028#Line # identifier for Image ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001029Returns value unique to image. Image contents cannot change after Image is
1030created. Any operation to create a new Image will receive generate a new
1031unique number.
1032
1033#Return unique identifier ##
Cary Clarka560c472017-11-27 10:44:06 -05001034
1035#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001036#Image 5
1037#Height 156
1038 sk_sp<SkImage> subset = image->makeSubset({10, 20, 90, 100});
1039 canvas->drawImage(image, 0, 0);
1040 canvas->drawImage(subset, 128, 0);
1041 SkPaint paint;
1042 SkString s;
1043 s.printf("original id: %d", image->uniqueID());
1044 canvas->drawString(s, 20, image->height() + 20, paint);
1045 s.printf("subset id: %d", subset->uniqueID());
1046 canvas->drawString(s, 148, subset->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001047##
1048
Cary Clark61ca7c52018-01-02 11:34:14 -05001049#SeeAlso isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001050
1051#Method ##
1052
1053# ------------------------------------------------------------------------------
1054
1055#Method SkAlphaType alphaType() const
Cary Clark4855f782018-02-06 09:41:53 -05001056#In Property
1057#Line # returns Alpha_Type ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001058Returns Alpha_Type, one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
1059kPremul_SkAlphaType, kUnpremul_SkAlphaType.
1060
1061Alpha_Type returned was a parameter to an Image constructor,
1062or was parsed from encoded data.
1063
1064#Return Alpha_Type in Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001065
1066#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001067#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -05001068#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -05001069 const char* alphaTypeStr[] = { "Unknown", "Opaque", "Premul", "Unpremul" };
1070 SkAlphaType alphaType = image->alphaType();
Cary Clarkac47b882018-01-11 10:35:44 -05001071 canvas->drawImage(image, 16, 0);
Cary Clark61ca7c52018-01-02 11:34:14 -05001072 SkPaint paint;
1073 canvas->drawString(alphaTypeStr[(int) alphaType], 20, image->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001074##
1075
Cary Clark61ca7c52018-01-02 11:34:14 -05001076#SeeAlso SkImageInfo::alphaType
Cary Clarka560c472017-11-27 10:44:06 -05001077
1078#Method ##
1079
1080# ------------------------------------------------------------------------------
1081
1082#Method SkColorSpace* colorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001083#In Property
1084#Line # returns Color_Space ##
Cary Clark2f466242017-12-11 16:03:17 -05001085Returns Color_Space, the range of colors, associated with Image. The
1086reference count of Color_Space is unchanged. The returned Color_Space is
1087immutable.
Cary Clarka560c472017-11-27 10:44:06 -05001088
Cary Clark61dfc3a2018-01-03 08:37:53 -05001089Color_Space returned was passed to an Image constructor,
1090or was parsed from encoded data. Color_Space returned may be ignored when Image
1091is drawn, depending on the capabilities of the Surface receiving the drawing.
Cary Clark2f466242017-12-11 16:03:17 -05001092
1093#Return Color_Space in Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001094
1095#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001096#Image 3
1097#Set sRGB
1098 SkPixmap pixmap;
1099 source.peekPixels(&pixmap);
1100 canvas->scale(.25f, .25f);
1101 int y = 0;
1102 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1103 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1104 int x = 0;
1105 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1106 for (int index = 0; index < 2; ++index) {
1107 pixmap.setColorSpace(colorSpace);
1108 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1109 canvas->drawImage(image, x, y);
1110 colorSpace = image->colorSpace()->makeColorSpin();
1111 x += 512;
1112 }
1113 y += 512;
1114 }
Cary Clarka560c472017-11-27 10:44:06 -05001115##
1116
Cary Clark61dfc3a2018-01-03 08:37:53 -05001117#SeeAlso refColorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001118
1119#Method ##
1120
1121# ------------------------------------------------------------------------------
1122
1123#Method sk_sp<SkColorSpace> refColorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001124#In Property
1125#Line # returns Image_Info Color_Space ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001126Returns a smart pointer to Color_Space, the range of colors, associated with
1127Image. The smart pointer tracks the number of objects sharing this
1128SkColorSpace reference so the memory is released when the owners destruct.
1129
1130The returned SkColorSpace is immutable.
1131
1132Color_Space returned was passed to an Image constructor,
1133or was parsed from encoded data. Color_Space returned may be ignored when Image
1134is drawn, depending on the capabilities of the Surface receiving the drawing.
1135
1136#Return Color_Space in Image, or nullptr, wrapped in a smart pointer ##
Cary Clarka560c472017-11-27 10:44:06 -05001137
1138#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001139#Image 3
1140#Set sRGB
1141 SkPixmap pixmap;
1142 source.peekPixels(&pixmap);
1143 canvas->scale(.25f, .25f);
1144 int y = 0;
1145 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1146 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1147 int x = 0;
1148 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1149 for (int index = 0; index < 2; ++index) {
1150 pixmap.setColorSpace(colorSpace);
1151 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1152 canvas->drawImage(image, x, y);
1153 colorSpace = image->refColorSpace()->makeColorSpin();
1154 x += 512;
1155 }
1156 y += 512;
1157 }
Cary Clarka560c472017-11-27 10:44:06 -05001158##
1159
Cary Clark61dfc3a2018-01-03 08:37:53 -05001160#SeeAlso colorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001161
1162#Method ##
1163
1164# ------------------------------------------------------------------------------
1165
1166#Method bool isAlphaOnly() const
Cary Clark4855f782018-02-06 09:41:53 -05001167#In Property
1168#Line # returns if pixels represent a transparency mask ##
Cary Clark2f466242017-12-11 16:03:17 -05001169Returns true if Image pixels represent transparency only. If true, each pixel
1170is packed in 8 bits as defined by kAlpha_8_SkColorType.
Cary Clarka560c472017-11-27 10:44:06 -05001171
Cary Clark2f466242017-12-11 16:03:17 -05001172#Return true if pixels represent a transparency mask ##
Cary Clarka560c472017-11-27 10:44:06 -05001173
1174#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001175 uint8_t pmColors = 0;
1176 sk_sp<SkImage> image = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1), &pmColors, 1});
1177 SkDebugf("alphaOnly = %s\n", image->isAlphaOnly() ? "true" : "false");
1178#StdOut
1179alphaOnly = true
1180##
Cary Clarka560c472017-11-27 10:44:06 -05001181##
1182
Cary Clark61dfc3a2018-01-03 08:37:53 -05001183#SeeAlso alphaType isOpaque
Cary Clarka560c472017-11-27 10:44:06 -05001184
1185#Method ##
1186
1187# ------------------------------------------------------------------------------
1188
1189#Method bool isOpaque() const
Cary Clark4855f782018-02-06 09:41:53 -05001190#In Property
1191#Line # returns if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001192Returns true if pixels ignore their Alpha value and are treated as fully opaque.
Cary Clark2f466242017-12-11 16:03:17 -05001193
1194#Return true if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clarka560c472017-11-27 10:44:06 -05001195
1196#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001197 auto check_isopaque = [](const SkImageInfo& imageInfo) -> void {
1198 auto surface(SkSurface::MakeRaster(imageInfo));
1199 auto image(surface->makeImageSnapshot());
1200 SkDebugf("isOpaque = %s\n", image->isOpaque() ? "true" : "false");
1201 };
1202
1203 check_isopaque(SkImageInfo::MakeN32Premul(5, 5));
1204 check_isopaque(SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType));
1205#StdOut
1206isOpaque = false
1207isOpaque = true
1208##
Cary Clarka560c472017-11-27 10:44:06 -05001209##
1210
Cary Clark61dfc3a2018-01-03 08:37:53 -05001211#SeeAlso alphaType isAlphaOnly
Cary Clarka560c472017-11-27 10:44:06 -05001212
1213#Method ##
1214
1215# ------------------------------------------------------------------------------
1216
1217#Method sk_sp<SkShader> makeShader(SkShader::TileMode tileMode1, SkShader::TileMode tileMode2,
1218 const SkMatrix* localMatrix = nullptr) const
Cary Clark4855f782018-02-06 09:41:53 -05001219#In Constructor
1220#Line # creates Shader, Paint element that can tile Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001221
Cary Clark61dfc3a2018-01-03 08:37:53 -05001222Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1223SkShader::TileMode rules to fill drawn area outside Image. localMatrix permits
1224transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001225
Cary Clark61dfc3a2018-01-03 08:37:53 -05001226#Param tileMode1 tiling in x, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1227 SkShader::kMirror_TileMode
1228##
1229#Param tileMode2 tiling in y, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1230 SkShader::kMirror_TileMode
1231##
1232#Param localMatrix Image transformation, or nullptr ##
1233
1234#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001235
1236#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001237#Image 4
1238SkMatrix matrix;
1239matrix.setRotate(45);
1240SkPaint paint;
1241paint.setShader(image->makeShader(SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode,
1242 &matrix));
1243canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001244##
1245
Cary Clark61dfc3a2018-01-03 08:37:53 -05001246#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001247
1248#Method ##
1249
1250# ------------------------------------------------------------------------------
1251
1252#Method sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const
1253
Cary Clark61dfc3a2018-01-03 08:37:53 -05001254Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1255SkShader::kClamp_TileMode to fill drawn area outside Image. localMatrix permits
1256transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001257
Cary Clark61dfc3a2018-01-03 08:37:53 -05001258#Param localMatrix Image transformation, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001259
Cary Clark61dfc3a2018-01-03 08:37:53 -05001260#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001261
1262#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001263#Image 5
1264SkMatrix matrix;
1265matrix.setRotate(45);
1266matrix.postTranslate(125, 30);
1267SkPaint paint;
1268paint.setShader(image->makeShader(&matrix));
1269canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001270##
1271
Cary Clarkf5404bb2018-01-05 12:10:09 -05001272#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001273
1274#Method ##
1275
1276# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001277#Subtopic Pixels
1278#Populate
1279#Line # read and write pixel values ##
1280##
Cary Clarka560c472017-11-27 10:44:06 -05001281
1282#Method bool peekPixels(SkPixmap* pixmap) const
Cary Clark78de7512018-02-07 07:27:09 -05001283#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001284#Line # returns Pixmap if possible ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001285Copies Image pixel address, row bytes, and Image_Info to pixmap, if address
1286is available, and returns true. If pixel address is not available, return
1287false and leave pixmap unchanged.
Cary Clarka560c472017-11-27 10:44:06 -05001288
Cary Clarkf5404bb2018-01-05 12:10:09 -05001289#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
Cary Clarka560c472017-11-27 10:44:06 -05001290
Cary Clarkf5404bb2018-01-05 12:10:09 -05001291#Return true if Image has direct access to pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001292
1293#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001294 SkBitmap bitmap;
1295 bitmap.allocPixels(SkImageInfo::MakeN32Premul(12, 11));
1296 SkCanvas offscreen(bitmap);
1297 offscreen.clear(SK_ColorWHITE);
1298 SkPaint paint;
1299 offscreen.drawString("%", 1, 10, paint);
1300 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
1301 SkPixmap pixmap;
1302 if (image->peekPixels(&pixmap)) {
1303 const SkPMColor* pixels = pixmap.addr32();
1304 SkPMColor pmWhite = pixels[0];
1305 for (int y = 0; y < image->height(); ++y) {
1306 for (int x = 0; x < image->width(); ++x) {
1307 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
1308 }
1309 SkDebugf("\n");
1310 }
1311 }
1312#StdOut
1313------------
1314--xx----x---
1315-x--x--x----
1316-x--x--x----
1317-x--x-x-----
1318--xx-xx-xx--
1319-----x-x--x-
1320----x--x--x-
1321----x--x--x-
1322---x----xx--
1323------------
1324##
Cary Clarka560c472017-11-27 10:44:06 -05001325##
1326
Cary Clarkf5404bb2018-01-05 12:10:09 -05001327#SeeAlso readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001328
1329#Method ##
1330
1331# ------------------------------------------------------------------------------
1332
1333#Method GrTexture* getTexture() const
Cary Clark2f466242017-12-11 16:03:17 -05001334#Deprecated
Cary Clarka560c472017-11-27 10:44:06 -05001335#Method ##
1336
1337# ------------------------------------------------------------------------------
1338
1339#Method bool isTextureBacked() const
Cary Clark78de7512018-02-07 07:27:09 -05001340#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001341#Line # returns if Image was created from GPU_Texture ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001342Returns true the contents of Image was created on or uploaded to GPU memory,
1343and is available as a GPU_Texture.
Cary Clarka560c472017-11-27 10:44:06 -05001344
Cary Clarkf5404bb2018-01-05 12:10:09 -05001345#Return true if Image is a GPU_Texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001346
1347#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001348#Image 5
1349#Platform gpu
1350auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1351 if (nullptr == image) {
1352 return;
1353 }
1354 SkPaint paint;
1355 paint.setAntiAlias(true);
1356 paint.setTextAlign(SkPaint::kCenter_Align);
1357 canvas->drawImage(image, 0, 0);
1358 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1359 canvas->drawString(image->isTextureBacked() ? "is GPU texture" : "not GPU texture",
1360 image->width() / 2, image->height() * 3 / 4, paint);
1361};
1362sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1363sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1364 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1365drawImage(image, "image");
1366canvas->translate(image->width(), 0);
1367drawImage(bitmapImage, "source");
1368canvas->translate(-image->width(), image->height());
1369drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001370##
1371
Cary Clarkf5404bb2018-01-05 12:10:09 -05001372#SeeAlso MakeFromTexture isValid
Cary Clarka560c472017-11-27 10:44:06 -05001373
1374#Method ##
1375
1376# ------------------------------------------------------------------------------
1377
1378#Method bool isValid(GrContext* context) const
Cary Clark4855f782018-02-06 09:41:53 -05001379#In Property
1380#Line # returns if Image can draw to Raster_Surface or GPU_Context ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001381Returns true if Image can be drawn on either Raster_Surface or GPU_Surface.
1382If context is nullptr, tests if Image draws on Raster_Surface;
1383otherwise, tests if Image draws on GPU_Surface associated with context.
Cary Clarka560c472017-11-27 10:44:06 -05001384
Cary Clarkf5404bb2018-01-05 12:10:09 -05001385Image backed by GPU_Texture may become invalid if associated GrContext is
1386invalid. Lazy_Image may be invalid and may not draw to Raster_Surface or
1387GPU_Surface or both.
Cary Clarka560c472017-11-27 10:44:06 -05001388
Cary Clark61ca7c52018-01-02 11:34:14 -05001389#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05001390
Cary Clarkf5404bb2018-01-05 12:10:09 -05001391#Return true if Image can be drawn ##
Cary Clarka560c472017-11-27 10:44:06 -05001392
1393#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001394#Image 5
1395#Platform gpu
1396auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1397 if (nullptr == image) {
1398 return;
1399 }
1400 SkPaint paint;
1401 paint.setAntiAlias(true);
1402 paint.setTextAlign(SkPaint::kCenter_Align);
1403 canvas->drawImage(image, 0, 0);
1404 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1405 if (canvas->getGrContext()) {
1406 canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" :
1407 "not valid on GPU", image->width() / 2, image->height() * 5 / 8, paint);
1408 }
1409 canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" :
1410 "not valid on CPU", image->width() / 2, image->height() * 7 / 8, paint);
1411};
1412sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1413sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1414 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1415drawImage(image, "image");
1416canvas->translate(image->width(), 0);
1417drawImage(bitmapImage, "source");
1418canvas->translate(-image->width(), image->height());
1419drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001420##
1421
Cary Clarkf5404bb2018-01-05 12:10:09 -05001422#SeeAlso isTextureBacked isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001423
1424#Method ##
1425
1426# ------------------------------------------------------------------------------
1427
1428#Method GrBackendObject getTextureHandle(bool flushPendingGrContextIO,
1429 GrSurfaceOrigin* origin = nullptr) const
Cary Clark78de7512018-02-07 07:27:09 -05001430#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001431#Line # returns GPU reference to Image as texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001432
Cary Clark2f466242017-12-11 16:03:17 -05001433Retrieves the back-end API handle of texture. If flushPendingGrContextIO is true,
1434complete deferred I/O operations.
Cary Clarka560c472017-11-27 10:44:06 -05001435
Cary Clark2f466242017-12-11 16:03:17 -05001436If origin in not nullptr, copies location of content drawn into Image.
Cary Clarka560c472017-11-27 10:44:06 -05001437
Cary Clark2f466242017-12-11 16:03:17 -05001438#Param flushPendingGrContextIO flag to flush outstanding requests ##
1439#Param origin storage for one of: kTopLeft_GrSurfaceOrigin,
1440 kBottomLeft_GrSurfaceOrigin; or nullptr
1441##
1442
Cary Clarkac47b882018-01-11 10:35:44 -05001443#Return back-end API texture handle, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001444
1445#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001446#Image 4
Cary Clark2f466242017-12-11 16:03:17 -05001447#Platform gpu
1448GrContext* context = canvas->getGrContext();
1449if (!context) {
1450 return;
1451}
1452SkPaint paint;
1453paint.setAntiAlias(true);
1454SkString str;
Cary Clarkac47b882018-01-11 10:35:44 -05001455int y = -10;
Cary Clark2f466242017-12-11 16:03:17 -05001456for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin } ) {
1457 sk_sp<SkImage> srcImage(SkImage::MakeFromTexture(context,
1458 backEndTexture, origin, kPremul_SkAlphaType, nullptr));
1459 GrSurfaceOrigin readBackOrigin;
1460 GrBackendObject readBackHandle = srcImage->getTextureHandle(false, &readBackOrigin);
Cary Clarkac47b882018-01-11 10:35:44 -05001461 str.printf("readBackHandle: 0x%x", readBackHandle);
1462 canvas->drawString(str, 5, y += 30, paint);
1463 canvas->drawImage(srcImage, 80, y += 10);
Cary Clark2f466242017-12-11 16:03:17 -05001464 str.printf("origin: k%s_GrSurfaceOrigin", readBackOrigin ? "BottomLeft" : "TopLeft");
Cary Clarkac47b882018-01-11 10:35:44 -05001465 canvas->drawString(str, 5, y += srcImage->height() + 10, paint);
Cary Clark2f466242017-12-11 16:03:17 -05001466}
Cary Clarka560c472017-11-27 10:44:06 -05001467##
1468
Cary Clarkac47b882018-01-11 10:35:44 -05001469#Example
1470#Image 5
1471#Platform gpu
1472 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1473 if (nullptr == image) {
1474 return;
1475 }
1476 SkPaint paint;
1477 paint.setAntiAlias(true);
1478 paint.setTextAlign(SkPaint::kCenter_Align);
1479 canvas->drawImage(image, 0, image->height() / 4);
1480 canvas->drawString(label, image->width() / 2, image->height() / 8, paint);
1481 GrSurfaceOrigin readBackOrigin;
1482 GrBackendObject readBackHandle = image->getTextureHandle(false, &readBackOrigin);
1483 canvas->drawString(readBackHandle ? "has readBackHandle" : "no readBackHandle",
1484 image->width() / 2, image->height() * 11 / 8, paint);
1485 };
1486 drawImage(image, "image");
1487 canvas->translate(image->width(), 0);
1488 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1489 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1490 drawImage(textureImage, "backEndTexture");
1491##
1492
1493#SeeAlso MakeFromTexture isTextureBacked
Cary Clarka560c472017-11-27 10:44:06 -05001494
1495#Method ##
1496
1497# ------------------------------------------------------------------------------
1498
1499#Enum CachingHint
1500
1501#Code
1502 enum CachingHint {
1503 kAllow_CachingHint,
1504 kDisallow_CachingHint,
1505 };
1506##
1507
Cary Clarkac47b882018-01-11 10:35:44 -05001508CachingHint selects whether Skia may internally cache Bitmaps generated by
1509decoding Image, or by copying Image from GPU to CPU. The default behavior
1510allows caching Bitmaps.
1511
1512Choose kDisallow_CachingHint if Image pixels are to be used only once, or
1513if Image pixels reside in a cache outside of Skia, or to reduce memory pressure.
1514
1515Choosing kAllow_CachingHint does not ensure that pixels will be cached.
1516Image pixels may not be cached if memory requirements are too large or
1517pixels are not accessible.
Cary Clarka560c472017-11-27 10:44:06 -05001518
1519#Const kAllow_CachingHint 0
Cary Clarkac47b882018-01-11 10:35:44 -05001520Allows Skia to internally cache decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001521##
1522#Const kDisallow_CachingHint 1
Cary Clarkac47b882018-01-11 10:35:44 -05001523Disallows Skia from internally caching decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001524##
1525
Cary Clarkac47b882018-01-11 10:35:44 -05001526#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001527##
1528
Cary Clarkac47b882018-01-11 10:35:44 -05001529#SeeAlso readPixels scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001530
1531#Enum ##
1532
1533# ------------------------------------------------------------------------------
1534
1535#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1536 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001537#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001538#Line # copies and converts pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001539
Cary Clarkac47b882018-01-11 10:35:44 -05001540Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY),
1541and does not exceed Image (width(), height()).
1542
1543dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
1544destination. dstRowBytes specifics the gap from one destination row to the next.
1545Returns true if pixels are copied. Returns false if:
1546#List
1547# dstInfo.addr() equals nullptr ##
1548# dstRowBytes is less than dstInfo.minRowBytes ##
1549# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001550##
1551
Cary Clarkac47b882018-01-11 10:35:44 -05001552Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1553kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1554If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace must match.
1555If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType must
1556match. If Image Color_Space is nullptr, dstInfo.colorSpace must match. Returns
1557false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001558
Cary Clarkac47b882018-01-11 10:35:44 -05001559srcX and srcY may be negative to copy only top or left of source. Returns
1560false if width() or height() is zero or negative.
1561Returns false if
1562#Formula
1563abs(srcX) >= Image width()
1564##
1565, or if
1566#Formula
1567abs(srcY) >= Image height()
1568##
1569.
Cary Clarka560c472017-11-27 10:44:06 -05001570
Cary Clarkac47b882018-01-11 10:35:44 -05001571If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1572If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1573
1574#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1575#Param dstPixels destination pixel storage ##
1576#Param dstRowBytes destination row length ##
1577#Param srcX column index whose absolute value is less than width() ##
1578#Param srcY row index whose absolute value is less than height() ##
1579#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1580
1581#Return true if pixels are copied to dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001582
1583#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001584#Image 3
1585 canvas->scale(.5f, .5f);
1586 const int width = 32;
1587 const int height = 32;
1588 std::vector<int32_t> dstPixels;
1589 dstPixels.resize(height * width * 4);
1590 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1591 for (int y = 0; y < 512; y += height ) {
1592 for (int x = 0; x < 512; x += width ) {
1593 if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) {
1594 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
1595 SkBitmap bitmap;
1596 bitmap.installPixels(dstPixmap);
1597 canvas->drawBitmap(bitmap, 0, 0);
1598 }
1599 canvas->translate(48, 0);
1600 }
1601 canvas->translate(-16 * 48, 48);
1602 }
Cary Clarka560c472017-11-27 10:44:06 -05001603##
1604
Cary Clarkac47b882018-01-11 10:35:44 -05001605#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001606
1607#Method ##
1608
1609# ------------------------------------------------------------------------------
1610
1611#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY,
1612 CachingHint cachingHint = kAllow_CachingHint) const
1613
Cary Clarkac47b882018-01-11 10:35:44 -05001614Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and
1615does not exceed Image (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001616
Cary Clarkac47b882018-01-11 10:35:44 -05001617dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
1618and row bytes of destination. dst.rowBytes specifics the gap from one destination
1619row to the next. Returns true if pixels are copied. Returns false if:
1620#List
1621# dst pixel storage equals nullptr ##
1622# dst.rowBytes is less than SkImageInfo::minRowBytes ##
1623# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001624##
1625
Cary Clarkac47b882018-01-11 10:35:44 -05001626Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1627kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1628If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1629If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1630match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1631false if pixel conversion is not possible.
1632
1633srcX and srcY may be negative to copy only top or left of source. Returns
1634false if width() or height() is zero or negative.
1635Returns false if
1636#Formula
1637abs(srcX) >= Image width()
1638##
1639, or if
1640#Formula
1641abs(srcY) >= Image height()
1642##
1643.
1644
1645If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1646If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1647
1648#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1649#Param srcX column index whose absolute value is less than width() ##
1650#Param srcY row index whose absolute value is less than height() ##
1651#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1652
1653#Return true if pixels are copied to dst ##
1654
1655#Example
1656#Image 3
1657 std::vector<int32_t> srcPixels;
1658 int rowBytes = image->width() * 4;
1659 int quarterWidth = image->width() / 4;
1660 int quarterHeight = image->height() / 4;
1661 srcPixels.resize(image->height() * rowBytes);
1662 for (int y = 0; y < 4; ++y) {
1663 for (int x = 0; x < 4; ++x) {
1664 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1665 &srcPixels.front() + x * image->height() * quarterWidth +
1666 y * quarterWidth, rowBytes);
1667 image->readPixels(pixmap, x * quarterWidth, y * quarterHeight);
1668 }
1669 }
1670 canvas->scale(.5f, .5f);
1671 SkBitmap bitmap;
1672 bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()),
1673 &srcPixels.front(), rowBytes);
1674 canvas->drawBitmap(bitmap, 0, 0);
1675##
1676
1677#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001678
1679#Method ##
1680
1681# ------------------------------------------------------------------------------
1682
1683#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality,
1684 CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001685#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001686#Line # scales and converts one Image to another ##
Cary Clarka560c472017-11-27 10:44:06 -05001687
Cary Clarkac47b882018-01-11 10:35:44 -05001688Copies Image to dst, scaling pixels to fit dst.width() and dst.height(), and
1689converting pixels to match dst.colorType and dst.alphaType. Returns true if
1690pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1691less than dst SkImageInfo::minRowBytes.
Cary Clarka560c472017-11-27 10:44:06 -05001692
Cary Clarkac47b882018-01-11 10:35:44 -05001693Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1694kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1695If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1696If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1697match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1698false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001699
Cary Clarkac47b882018-01-11 10:35:44 -05001700Scales the image, with filterQuality, to match dst.width() and dst.height().
1701filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1702Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1703Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1704Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1705kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1706
1707If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1708If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1709
1710#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1711#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1712 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1713##
1714#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1715
1716#Return true if pixels are scaled to fit dst ##
Cary Clarka560c472017-11-27 10:44:06 -05001717
1718#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001719#Image 3
1720#Height 128
1721 std::vector<int32_t> srcPixels;
1722 int quarterWidth = image->width() / 16;
1723 int rowBytes = quarterWidth * 4;
1724 int quarterHeight = image->height() / 16;
1725 srcPixels.resize(quarterHeight * rowBytes);
1726 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1727 &srcPixels.front(), rowBytes);
1728 canvas->scale(4, 4);
1729 SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality,
1730 kMedium_SkFilterQuality, kHigh_SkFilterQuality };
1731 for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) {
1732 image->scalePixels(pixmap, qualities[index]);
1733 sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
1734 canvas->drawImage(filtered, 16 * index, 0);
1735 }
Cary Clarka560c472017-11-27 10:44:06 -05001736##
1737
Cary Clarkac47b882018-01-11 10:35:44 -05001738#SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001739
1740#Method ##
1741
1742# ------------------------------------------------------------------------------
1743
1744#Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const
Cary Clark78de7512018-02-07 07:27:09 -05001745#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001746#Line # returns encoded Image as SkData ##
Cary Clarkac47b882018-01-11 10:35:44 -05001747Encodes Image pixels, returning result as SkData.
Cary Clark2f466242017-12-11 16:03:17 -05001748
Cary Clarkac47b882018-01-11 10:35:44 -05001749Returns nullptr if encoding fails, or if encodedImageFormat is not supported.
Cary Clarka560c472017-11-27 10:44:06 -05001750
Cary Clarkac47b882018-01-11 10:35:44 -05001751Image encoding in a format requires both building with one or more of:
1752SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support
1753for the encoded format.
1754
1755If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can
1756additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP,
1757SkEncodedImageFormat::kGIF.
1758
1759quality is a platform and format specific metric trading off size and encoding
1760error. When used, quality equaling 100 encodes with the least error. quality may
1761be ignored by the encoder.
1762
1763#Param encodedImageFormat one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
1764 SkEncodedImageFormat::kWEBP
1765 ##
1766#Param quality encoder specific metric with 100 equaling best ##
Cary Clarka560c472017-11-27 10:44:06 -05001767
Cary Clark2f466242017-12-11 16:03:17 -05001768#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001769
1770#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001771#Image 3
1772 canvas->scale(4, 4);
1773 SkIRect subset = {0, 0, 16, 64};
1774 int x = 0;
1775 for (int quality : { 0, 10, 50, 100 } ) {
1776 sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
1777 sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
1778 canvas->drawImage(filtered, x, 0);
1779 x += 16;
1780 }
Cary Clarka560c472017-11-27 10:44:06 -05001781##
1782
Cary Clarkac47b882018-01-11 10:35:44 -05001783#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001784
1785#Method ##
1786
1787# ------------------------------------------------------------------------------
1788
Cary Clark61ca7c52018-01-02 11:34:14 -05001789#Method sk_sp<SkData> encodeToData() const
Cary Clarka560c472017-11-27 10:44:06 -05001790
Cary Clarkac47b882018-01-11 10:35:44 -05001791Encodes Image pixels, returning result as SkData. Returns existing encoded data
1792if present; otherwise, Image is encoded with SkEncodedImageFormat::kPNG. Skia
1793must be built with SK_HAS_PNG_LIBRARY to encode Image.
Cary Clarka560c472017-11-27 10:44:06 -05001794
Cary Clarkac47b882018-01-11 10:35:44 -05001795Returns nullptr if existing encoded data is missing or invalid, and
Cary Clarka560c472017-11-27 10:44:06 -05001796encoding fails.
1797
Cary Clarkac47b882018-01-11 10:35:44 -05001798#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001799
1800#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001801#Image 3
1802 canvas->scale(4, 4);
1803 SkIRect subset = {136, 32, 200, 96};
1804 sk_sp<SkData> data(image->encodeToData());
1805 sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
1806 canvas->drawImage(eye, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001807##
1808
Cary Clarkac47b882018-01-11 10:35:44 -05001809#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001810
1811#Method ##
1812
1813# ------------------------------------------------------------------------------
1814
1815#Method sk_sp<SkData> refEncodedData() const
Cary Clark78de7512018-02-07 07:27:09 -05001816#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001817#Line # returns Image encoded in SkData if present ##
Cary Clarkac47b882018-01-11 10:35:44 -05001818Returns encoded Image pixels as SkData, if Image was created from supported
1819encoded stream format. Platform support for formats vary and may require building
1820with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY.
Cary Clarka560c472017-11-27 10:44:06 -05001821
Cary Clarkac47b882018-01-11 10:35:44 -05001822Returns nullptr if Image contents are not encoded.
Cary Clarka560c472017-11-27 10:44:06 -05001823
Cary Clarkac47b882018-01-11 10:35:44 -05001824#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001825
1826#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001827#Image 3
1828#Platform gpu
1829 struct {
1830 const char* name;
1831 sk_sp<SkImage> image;
1832 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1833 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1834 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr) } };
1835 SkString string;
1836 SkPaint paint;
1837 for (const auto& test : tests ) {
1838 if (!test.image) {
1839 string.printf("no %s", test.name);
1840 } else {
1841 string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name);
1842 }
1843 canvas->drawString(string, 10, 20, paint);
1844 canvas->translate(0, 20);
1845 }
Cary Clarka560c472017-11-27 10:44:06 -05001846##
1847
Cary Clarkac47b882018-01-11 10:35:44 -05001848#SeeAlso encodeToData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001849
1850#Method ##
1851
1852# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001853#Subtopic Utility
1854#Populate
1855#Line # rarely called management functions ##
1856##
Cary Clarka560c472017-11-27 10:44:06 -05001857
1858#Method const char* toString(SkString* string) const
Cary Clark4855f782018-02-06 09:41:53 -05001859#In Utility
1860#Line # converts Image to machine readable form ##
Cary Clarkac47b882018-01-11 10:35:44 -05001861Appends Image description to string, including unique ID, width, height, and
1862whether the image is opaque.
Cary Clarka560c472017-11-27 10:44:06 -05001863
Cary Clarkac47b882018-01-11 10:35:44 -05001864#Param string storage for description; existing content is preserved ##
1865
1866#Return string appended with Image description ##
Cary Clarka560c472017-11-27 10:44:06 -05001867
1868#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001869#Image 4
1870 struct {
1871 const char* name;
1872 sk_sp<SkImage> image;
1873 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1874 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1875 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr) } };
1876 SkString string;
1877 SkPaint paint;
1878 for (const auto& test : tests ) {
1879 string.printf("%s: ", test.name);
1880 test.image ? (void) test.image->toString(&string) : string.append("no image");
1881 canvas->drawString(string, 10, 20, paint);
1882 canvas->translate(0, 20);
1883 }
Cary Clarka560c472017-11-27 10:44:06 -05001884##
1885
Cary Clarkac47b882018-01-11 10:35:44 -05001886#SeeAlso SkPaint::toString
Cary Clarka560c472017-11-27 10:44:06 -05001887
1888#Method ##
1889
1890# ------------------------------------------------------------------------------
1891
1892#Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const
Cary Clark4855f782018-02-06 09:41:53 -05001893#In Constructor
1894#Line # creates Image containing part of original ##
Cary Clarkac47b882018-01-11 10:35:44 -05001895Returns subset of Image. subset must be fully contained by Image dimensions().
1896The implementation may share pixels, or may copy them.
Cary Clarka560c472017-11-27 10:44:06 -05001897
Cary Clarkac47b882018-01-11 10:35:44 -05001898Returns nullptr if subset is empty, or subset is not contained by bounds, or
1899pixels in Image could not be read or copied.
Cary Clarka560c472017-11-27 10:44:06 -05001900
Cary Clarkac47b882018-01-11 10:35:44 -05001901#Param subset bounds of returned Image ##
1902
1903#Return partial or full Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001904
1905#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001906#Image 3
1907 canvas->scale(.5f, .5f);
1908 const int width = 32;
1909 const int height = 32;
1910 for (int y = 0; y < 512; y += height ) {
1911 for (int x = 0; x < 512; x += width ) {
1912 sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height}));
1913 canvas->drawImage(subset, x * 3 / 2, y * 3 / 2);
1914 }
1915 }
Cary Clarka560c472017-11-27 10:44:06 -05001916##
1917
Cary Clarkac47b882018-01-11 10:35:44 -05001918#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001919
1920#Method ##
1921
1922# ------------------------------------------------------------------------------
1923
1924#Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const
Cary Clark4855f782018-02-06 09:41:53 -05001925#In Constructor
1926#Line # creates Image matching Color_Space if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001927Returns Image backed by GPU_Texture associated with context. Returned Image is
1928compatible with Surface created with dstColorSpace. Returns original
1929Image if context and dstColorSpace match.
1930
1931Returns nullptr if context is nullptr, or if Image was created with another
1932GrContext.
Cary Clarka560c472017-11-27 10:44:06 -05001933
Cary Clark61ca7c52018-01-02 11:34:14 -05001934#Param context GPU_Context ##
Cary Clarkac47b882018-01-11 10:35:44 -05001935#Param dstColorSpace range of colors of matching Surface on GPU ##
Cary Clarka560c472017-11-27 10:44:06 -05001936
Cary Clarkac47b882018-01-11 10:35:44 -05001937#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001938
1939#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001940#Platform gpu
1941#Image 5
1942 auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void {
1943 if (nullptr == image || nullptr == context) {
1944 return;
1945 }
1946 SkPaint paint;
1947 paint.setAntiAlias(true);
1948 paint.setTextAlign(SkPaint::kCenter_Align);
1949 sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr));
1950 canvas->drawImage(texture, 0, 0);
1951 canvas->drawString(label, texture->width() / 2, texture->height() / 4, paint);
1952 };
1953 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1954 GrContext* context = canvas->getGrContext();
1955 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture,
1956 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1957 drawImage(image, context, "image");
1958 canvas->translate(image->width(), 0);
1959 drawImage(bitmapImage, context, "source");
1960 canvas->translate(-image->width(), image->height());
1961 drawImage(textureImage, context, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001962##
1963
Cary Clarkac47b882018-01-11 10:35:44 -05001964#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05001965
1966#Method ##
1967
1968# ------------------------------------------------------------------------------
1969
1970#Method sk_sp<SkImage> makeNonTextureImage() const
Cary Clark4855f782018-02-06 09:41:53 -05001971#In Constructor
1972#Line # creates Image without dependency on GPU_Texture ##
Cary Clarkac47b882018-01-11 10:35:44 -05001973Returns Raster_Image or Lazy_Image. Copies Image backed by GPU_Texture into
Cary Clark4855f782018-02-06 09:41:53 -05001974CPU memory if needed. Returns original Image if decoded in Raster_Bitmap,
Cary Clarkac47b882018-01-11 10:35:44 -05001975or if encoded in a stream.
Cary Clark61ca7c52018-01-02 11:34:14 -05001976
Cary Clarkac47b882018-01-11 10:35:44 -05001977Returns nullptr if backed by GPU_Texture and copy fails.
1978
1979#Return Raster_Image, Lazy_Image, or nullptr ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001980
1981#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001982#Image 5
1983#Platform gpu
1984 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1985 if (nullptr == image) {
1986 return;
1987 }
1988 SkPaint paint;
1989 paint.setAntiAlias(true);
1990 paint.setTextAlign(SkPaint::kCenter_Align);
1991 sk_sp<SkImage> nonTexture(image->makeNonTextureImage());
1992 canvas->drawImage(nonTexture, 0, 0);
1993 canvas->drawString(label, nonTexture->width() / 2, nonTexture->height() / 4, paint);
1994 };
1995 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1996 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1997 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1998 drawImage(image, "image");
1999 canvas->translate(image->width(), 0);
2000 drawImage(bitmapImage, "source");
2001 canvas->translate(-image->width(), image->height());
2002 drawImage(textureImage, "backEndTexture");
Cary Clark61ca7c52018-01-02 11:34:14 -05002003##
2004
Cary Clark56356312018-02-08 14:45:18 -05002005#SeeAlso makeTextureImage makeRasterImage MakeBackendTextureFromSkImage
Cary Clark61ca7c52018-01-02 11:34:14 -05002006
2007#Method ##
2008
2009# ------------------------------------------------------------------------------
2010
2011#Method sk_sp<SkImage> makeRasterImage() const
Cary Clark4855f782018-02-06 09:41:53 -05002012#In Constructor
2013#Line # creates Image compatible with Raster_Surface if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05002014Returns Raster_Image. Copies Image backed by GPU_Texture into CPU memory,
Cary Clark4855f782018-02-06 09:41:53 -05002015or decodes Image from Lazy_Image. Returns original Image if decoded in
Cary Clarkac47b882018-01-11 10:35:44 -05002016Raster_Bitmap.
Cary Clarka560c472017-11-27 10:44:06 -05002017
Cary Clarkac47b882018-01-11 10:35:44 -05002018Returns nullptr if copy, decode, or pixel read fails.
Cary Clarka560c472017-11-27 10:44:06 -05002019
Cary Clarkac47b882018-01-11 10:35:44 -05002020#Return Raster_Image, or nullptr ##
2021
Cary Clark4855f782018-02-06 09:41:53 -05002022#Bug 7479
Cary Clarka560c472017-11-27 10:44:06 -05002023#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002024#Image 5
2025#Platform gpu
2026 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2027 if (nullptr == image) {
2028 return;
2029 }
2030 SkPaint paint;
2031 paint.setAntiAlias(true);
2032 paint.setTextAlign(SkPaint::kCenter_Align);
2033 sk_sp<SkImage> raster(image->makeRasterImage());
2034 canvas->drawImage(raster, 0, 0);
2035 canvas->drawString(label, raster->width() / 2, raster->height() / 4, paint);
2036 };
2037 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2038 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2039 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2040 drawImage(image, "image");
2041 canvas->translate(image->width(), 0);
2042 drawImage(bitmapImage, "source");
2043 canvas->translate(-image->width(), image->height());
2044 drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05002045##
2046
Cary Clarkac47b882018-01-11 10:35:44 -05002047#SeeAlso isTextureBacked isLazyGenerated MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -05002048
2049#Method ##
2050
2051# ------------------------------------------------------------------------------
2052
2053#Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
2054 const SkIRect& clipBounds, SkIRect* outSubset,
2055 SkIPoint* offset) const
Cary Clark4855f782018-02-06 09:41:53 -05002056#In Constructor
2057#Line # creates filtered, clipped Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002058
Cary Clarkac47b882018-01-11 10:35:44 -05002059Creates filtered Image. filter processes original Image, potentially changing
2060color, position, and size. subset is the bounds of original Image processed
2061by filter. clipBounds is the expected bounds of the filtered Image. outSubset
2062is required storage for the actual bounds of the filtered Image. offset is
2063required storage for translation of returned Image.
Cary Clarka560c472017-11-27 10:44:06 -05002064
Cary Clarkac47b882018-01-11 10:35:44 -05002065Returns nullptr if Image could not be created. If nullptr is returned, outSubset
2066and offset are undefined.
2067
Cary Clark56356312018-02-08 14:45:18 -05002068Useful for animation of SkImageFilter that varies size from frame to frame.
2069Returned Image is created larger than required by filter so that GPU_Texture
2070can be reused with different sized effects. outSubset describes the valid bounds
2071of GPU_Texture returned. offset translates the returned Image to keep subsequent
2072animation frames aligned with respect to each other.
Cary Clarkac47b882018-01-11 10:35:44 -05002073
2074#Param filter how Image is sampled when transformed ##
Cary Clark56356312018-02-08 14:45:18 -05002075#Param subset bounds of Image processed by filter ##
2076#Param clipBounds expected bounds of filtered Image ##
2077#Param outSubset storage for returned Image bounds ##
2078#Param offset storage for returned Image translation ##
Cary Clarka560c472017-11-27 10:44:06 -05002079
Cary Clarkac47b882018-01-11 10:35:44 -05002080#Return filtered Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05002081
2082#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002083#Description
2084In each frame of the animation, filtered Image is drawn in a different location.
2085By translating canvas by returned offset, Image appears stationary.
2086##
2087#Image 5
2088#Platform gpu
2089#Duration 5
2090 sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make(
2091 -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE,
2092 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
2093 nullptr);
2094 sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr);
2095 SkIRect subset = image->bounds();
2096 SkIRect clipBounds = image->bounds();
2097 clipBounds.outset(60, 60);
2098 SkIRect outSubset;
2099 SkIPoint offset;
2100 sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds,
2101 &outSubset, &offset));
2102 SkPaint paint;
2103 paint.setAntiAlias(true);
2104 paint.setStyle(SkPaint::kStroke_Style);
2105 canvas->drawLine(0, 0, offset.fX, offset.fY, paint);
2106 canvas->translate(offset.fX, offset.fY);
2107 canvas->drawImage(filtered, 0, 0);
2108 canvas->drawRect(SkRect::MakeFromIRect(outSubset), paint);
Cary Clarka560c472017-11-27 10:44:06 -05002109##
2110
Cary Clark56356312018-02-08 14:45:18 -05002111#SeeAlso makeShader SkPaint::setImageFilter
Cary Clarka560c472017-11-27 10:44:06 -05002112
2113#Method ##
2114
2115# ------------------------------------------------------------------------------
2116
Cary Clarka560c472017-11-27 10:44:06 -05002117#Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc
2118
2119##
2120
2121# ------------------------------------------------------------------------------
2122
2123#Method static bool MakeBackendTextureFromSkImage(GrContext* context,
2124 sk_sp<SkImage> image,
2125 GrBackendTexture* backendTexture,
2126 BackendTextureReleaseProc* backendTextureReleaseProc)
Cary Clark4855f782018-02-06 09:41:53 -05002127#In Constructor
2128#Line # creates GPU_Texture from Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002129
Cary Clark56356312018-02-08 14:45:18 -05002130Creates a GrBackendTexture from the provided SkImage. Returns true and
2131stores result in backendTexture and backendTextureReleaseProc if
2132texture is created; otherwise, returns false and leaves
2133backendTexture and backendTextureReleaseProc unmodified.
Cary Clarka560c472017-11-27 10:44:06 -05002134
Cary Clark56356312018-02-08 14:45:18 -05002135Call backendTextureReleaseProc after deleting backendTexture.
2136backendTextureReleaseProc cleans up auxiliary data related to returned
2137backendTexture. The caller must delete returned backendTexture after use.
Cary Clarka560c472017-11-27 10:44:06 -05002138
Cary Clark56356312018-02-08 14:45:18 -05002139If Image is both texture backed and singly referenced, image is returned in
2140backendTexture without conversion or making a copy. Image is singly referenced
2141if its was transferred solely using std::move().
2142
2143If Image is not texture backed, returns texture with Image contents.
Cary Clarka560c472017-11-27 10:44:06 -05002144
Cary Clark61ca7c52018-01-02 11:34:14 -05002145#Param context GPU_Context ##
Cary Clark56356312018-02-08 14:45:18 -05002146#Param image Image used for texture ##
2147#Param backendTexture storage for backend texture ##
2148#Param backendTextureReleaseProc storage for clean up function ##
Cary Clarka560c472017-11-27 10:44:06 -05002149
Cary Clark56356312018-02-08 14:45:18 -05002150#Return true if backend texture was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002151
2152#Example
Cary Clark56356312018-02-08 14:45:18 -05002153#Platform gpu
2154#Height 64
2155#Function
Brian Salomon67f85842018-02-09 08:50:22 -05002156static sk_sp<SkImage> create_gpu_image(GrContext* grContext) {
2157 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
2158 auto surface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info));
2159 SkCanvas* canvas = surface->getCanvas();
2160 canvas->clear(SK_ColorWHITE);
2161 SkPaint paint;
2162 paint.setColor(SK_ColorBLACK);
2163 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
2164 return surface->makeImageSnapshot();
2165}
2166##
2167
2168void draw(SkCanvas* canvas) {
2169 GrContext* grContext = canvas->getGrContext();
2170 if (!grContext) {
2171 return;
2172 }
2173 sk_sp<SkImage> backEndImage = create_gpu_image(grContext);
2174 canvas->drawImage(backEndImage, 0, 0);
2175 GrBackendTexture texture;
2176 SkImage::BackendTextureReleaseProc proc;
2177 if (!SkImage::MakeBackendTextureFromSkImage(grContext, std::move(backEndImage),
2178 &texture, &proc)) {
2179 return;
2180 }
2181 sk_sp<SkImage> i2 = SkImage::MakeFromTexture(grContext, texture, kTopLeft_GrSurfaceOrigin,
2182 kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
2183 canvas->drawImage(i2, 30, 30);
Cary Clark56356312018-02-08 14:45:18 -05002184}
Cary Clarka560c472017-11-27 10:44:06 -05002185##
2186
Cary Clark56356312018-02-08 14:45:18 -05002187#SeeAlso MakeFromTexture makeTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002188
2189#Method ##
2190
2191# ------------------------------------------------------------------------------
2192
2193#Enum LegacyBitmapMode
Cary Clark56356312018-02-08 14:45:18 -05002194#Deprecated soon
Cary Clarka560c472017-11-27 10:44:06 -05002195#Code
2196 enum LegacyBitmapMode {
2197 kRO_LegacyBitmapMode,
Cary Clarka560c472017-11-27 10:44:06 -05002198 };
2199##
2200
Cary Clarka560c472017-11-27 10:44:06 -05002201#Const kRO_LegacyBitmapMode 0
Cary Clark56356312018-02-08 14:45:18 -05002202Returned bitmap is read-only and immutable.
Cary Clarka560c472017-11-27 10:44:06 -05002203##
Cary Clarka560c472017-11-27 10:44:06 -05002204
2205#Enum ##
2206
2207# ------------------------------------------------------------------------------
2208
Cary Clark56356312018-02-08 14:45:18 -05002209#Method bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode = kRO_LegacyBitmapMode) const
Cary Clark4855f782018-02-06 09:41:53 -05002210#In Constructor
2211#Line # returns as Raster_Bitmap ##
Cary Clarkac47b882018-01-11 10:35:44 -05002212Creates raster Bitmap with same pixels as Image. If legacyBitmapMode is
2213kRO_LegacyBitmapMode, returned bitmap is read-only and immutable.
2214Returns true if Bitmap is stored in bitmap. Returns false and resets bitmap if
2215Bitmap write did not succeed.
Cary Clarka560c472017-11-27 10:44:06 -05002216
Cary Clark3cd22cc2017-12-01 11:49:58 -05002217#Param bitmap storage for legacy Bitmap ##
Cary Clark56356312018-02-08 14:45:18 -05002218#Param legacyBitmapMode to be deprecated ##
Cary Clarka560c472017-11-27 10:44:06 -05002219
Cary Clark3cd22cc2017-12-01 11:49:58 -05002220#Return true if Bitmap was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002221
2222#Example
Cary Clark56356312018-02-08 14:45:18 -05002223#Image 4
2224#Platform gpu
Brian Salomon67f85842018-02-09 08:50:22 -05002225 SkBitmap bitImage;
2226 if (image->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2227 canvas->drawBitmap(bitImage, 0, 0);
2228 }
2229 GrContext* grContext = canvas->getGrContext();
2230 if (!grContext) {
2231 return;
2232 }
2233 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(grContext, backEndTexture,
2234 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2235 canvas->drawImage(textureImage, 45, 45);
2236 if (textureImage->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2237 canvas->drawBitmap(bitImage, 90, 90);
2238 }
Cary Clarka560c472017-11-27 10:44:06 -05002239##
2240
Cary Clark56356312018-02-08 14:45:18 -05002241#SeeAlso MakeRasterData makeRasterImage makeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002242
2243#Method ##
2244
2245# ------------------------------------------------------------------------------
2246
2247#Method bool isLazyGenerated() const
Cary Clark4855f782018-02-06 09:41:53 -05002248#In Property
2249#Line # returns if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002250Returns true if Image is backed by an image-generator or other service that creates
2251and caches its pixels or texture on-demand.
2252
Cary Clark2f466242017-12-11 16:03:17 -05002253#Return true if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002254
2255#Example
Cary Clark2f466242017-12-11 16:03:17 -05002256#Height 80
2257#Function
2258class TestImageGenerator : public SkImageGenerator {
2259public:
2260 TestImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(10, 10)) {}
2261 ~TestImageGenerator() override {}
2262protected:
2263 bool onGetPixels(const SkImageInfo& info, void* pixelPtr, size_t rowBytes,
2264 const Options& options) override {
2265 SkPMColor* pixels = static_cast<SkPMColor*>(pixelPtr);
2266 for (int y = 0; y < info.height(); ++y) {
2267 for (int x = 0; x < info.width(); ++x) {
2268 pixels[y * info.width() + x] = 0xff223344 + y * 0x000C0811;
2269 }
2270 }
2271 return true;
2272 }
2273};
2274##
2275void draw(SkCanvas* canvas) {
2276 auto gen = std::unique_ptr<TestImageGenerator>(new TestImageGenerator());
2277 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
2278 SkString lazy(image->isLazyGenerated() ? "is lazy" : "not lazy");
2279 canvas->scale(8, 8);
2280 canvas->drawImage(image, 0, 0, nullptr);
2281 SkPaint paint;
2282 paint.setTextSize(4);
2283 canvas->drawString(lazy, 2, 5, paint);
2284}
Cary Clarka560c472017-11-27 10:44:06 -05002285##
2286
Cary Clarkf5404bb2018-01-05 12:10:09 -05002287#Example
2288#Image 5
2289#Platform gpu
2290void draw(SkCanvas* canvas) {
2291 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2292 if (nullptr == image) {
2293 return;
2294 }
2295 SkPaint paint;
2296 paint.setAntiAlias(true);
2297 paint.setTextAlign(SkPaint::kCenter_Align);
2298 canvas->drawImage(image, 0, 0);
2299 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
2300 canvas->drawString(
2301 image->isLazyGenerated() ? "is lazily generated" : "not lazily generated",
2302 image->width() / 2, image->height() * 3 / 4, paint);
2303 };
2304 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2305 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2306 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2307 drawImage(image, "image");
2308 canvas->translate(image->width(), 0);
2309 drawImage(bitmapImage, "source");
2310 canvas->translate(-image->width(), image->height());
2311 drawImage(textureImage, "backEndTexture");
2312}
2313##
2314
Cary Clarkac47b882018-01-11 10:35:44 -05002315#SeeAlso isTextureBacked MakeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002316
2317#Method ##
2318
2319# ------------------------------------------------------------------------------
2320
2321#Method sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target,
2322 SkTransferFunctionBehavior premulBehavior) const
Cary Clark4855f782018-02-06 09:41:53 -05002323#In Constructor
2324#Line # creates Image matching Color_Space if possible ##
Cary Clarka560c472017-11-27 10:44:06 -05002325
Cary Clarkac47b882018-01-11 10:35:44 -05002326Creates Image in target Color_Space.
2327Returns nullptr if Image could not be created.
Cary Clarka560c472017-11-27 10:44:06 -05002328
Cary Clarkac47b882018-01-11 10:35:44 -05002329Returns original Image if it is in target Color_Space.
2330Otherwise, converts pixels from Image Color_Space to target Color_Space.
2331If Image colorSpace returns nullptr, Image Color_Space is assumed to be sRGB.
2332
2333SkTransferFunctionBehavior is to be deprecated.
2334
2335Set premulBehavior to SkTransferFunctionBehavior::kRespect to convert Image
2336pixels to a linear space, before converting to destination Color_Type
Cary Clarka560c472017-11-27 10:44:06 -05002337and Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -05002338
Cary Clarkac47b882018-01-11 10:35:44 -05002339Set premulBehavior to SkTransferFunctionBehavior::kIgnore to treat Image
2340pixels as linear, when converting to destination Color_Type
2341and Color_Space, ignoring pixel encoding.
Cary Clarka560c472017-11-27 10:44:06 -05002342
Cary Clarkac47b882018-01-11 10:35:44 -05002343#Param target Color_Space describing color range of returned Image ##
2344#Param premulBehavior one of: SkTransferFunctionBehavior::kRespect,
2345 SkTransferFunctionBehavior::kIgnore
Cary Clarka560c472017-11-27 10:44:06 -05002346##
2347
Cary Clarkac47b882018-01-11 10:35:44 -05002348#Return created Image in target Color_Space ##
2349
2350#Example
2351#Image 5
2352#Set sRGB
2353 sk_sp<SkColorSpace> normalColorSpace = SkColorSpace::MakeRGB(
2354 SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kSRGB_Gamut);
2355 sk_sp<SkColorSpace> wackyColorSpace = normalColorSpace->makeColorSpin();
2356 for (auto colorSpace : { normalColorSpace, wackyColorSpace } ) {
2357 for (auto transfer : { SkTransferFunctionBehavior::kRespect,
2358 SkTransferFunctionBehavior::kIgnore } ) {
2359 sk_sp<SkImage> colorSpaced = image->makeColorSpace(colorSpace, transfer);
2360 canvas->drawImage(colorSpaced, 0, 0);
2361 canvas->translate(128, 0);
2362 }
2363 canvas->translate(-256, 128);
2364 }
2365##
2366
2367#SeeAlso MakeFromPixture MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05002368
2369#Method ##
2370
2371#Class SkImage ##
2372
2373#Topic Image ##