blob: b94e4331b3c096d0a65ef3695288293248434b2a [file] [log] [blame]
Cary Clarka560c472017-11-27 10:44:06 -05001#Topic Image
2#Alias Image_Reference
3
Cary Clark08895c42018-02-01 09:37:32 -05004#Subtopic Overview
Cary Clark4855f782018-02-06 09:41:53 -05005 #Subtopic Subtopic
Cary Clark08895c42018-02-01 09:37:32 -05006 #Populate
7 ##
8##
9
Cary Clarka560c472017-11-27 10:44:06 -050010#Class SkImage
11
Cary Clark61ca7c52018-01-02 11:34:14 -050012Image describes a two dimensional array of pixels to draw. The pixels may be
Cary Clark4855f782018-02-06 09:41:53 -050013decoded in a Raster_Bitmap, encoded in a Picture or compressed data stream,
Cary Clark61ca7c52018-01-02 11:34:14 -050014or located in GPU memory as a GPU_Texture.
15
16Image cannot be modified after it is created. Image may allocate additional
17storage as needed; for instance, an encoded Image may decode when drawn.
18
19Image width and height are greater than zero. Creating an Image with zero width
20or height returns Image equal to nullptr.
21
22Image may be created from Bitmap, Pixmap, Surface, Picture, encoded streams,
23GPU_Texture, YUV_ColorSpace data, or hardware buffer. Encoded streams supported
Cary Clark4855f782018-02-06 09:41:53 -050024include BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP. Supported encoding details
Cary Clark61ca7c52018-01-02 11:34:14 -050025vary with platform.
26
Cary Clark08895c42018-02-01 09:37:32 -050027#Subtopic Raster_Image
Cary Clark61ca7c52018-01-02 11:34:14 -050028#Alias Raster_Image
Cary Clark4855f782018-02-06 09:41:53 -050029#Line # pixels decoded in Raster_Bitmap ##
30Raster_Image pixels are decoded in a Raster_Bitmap. These pixels may be read
Cary Clark61ca7c52018-01-02 11:34:14 -050031directly and in most cases written to, although edited pixels may not be drawn
32if Image has been copied internally.
33##
34
Cary Clark08895c42018-02-01 09:37:32 -050035#Subtopic Texture_Image
36#Line # pixels located on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -050037Texture_Image are located on GPU and pixels are not accessible. Texture_Image
38are allocated optimally for best performance. Raster_Image may
39be drawn to GPU_Surface, but pixels are uploaded from CPU to GPU downgrading
40performance.
41##
42
Cary Clark08895c42018-02-01 09:37:32 -050043#Subtopic Lazy_Image
44#Line # deferred pixel buffer ##
Cary Clark61ca7c52018-01-02 11:34:14 -050045Lazy_Image defer allocating buffer for Image pixels and decoding stream until
46Image is drawn. Lazy_Image caches result if possible to speed up repeated
47drawing.
48##
Cary Clarka560c472017-11-27 10:44:06 -050049
Cary Clark4855f782018-02-06 09:41:53 -050050#Subtopic Related_Function
Cary Clark08895c42018-02-01 09:37:32 -050051#Populate
52##
Cary Clarka560c472017-11-27 10:44:06 -050053
Cary Clark4855f782018-02-06 09:41:53 -050054#Subtopic Class_or_Struct
Cary Clark08895c42018-02-01 09:37:32 -050055#Populate
Cary Clarka560c472017-11-27 10:44:06 -050056##
57
Cary Clark4855f782018-02-06 09:41:53 -050058#Subtopic Constructor
Cary Clark08895c42018-02-01 09:37:32 -050059#Populate
60##
Cary Clark5081eed2018-01-22 07:55:48 -050061
Cary Clark4855f782018-02-06 09:41:53 -050062#Subtopic Member_Function
Cary Clark08895c42018-02-01 09:37:32 -050063#Populate
64##
Cary Clarka560c472017-11-27 10:44:06 -050065
Cary Clarka560c472017-11-27 10:44:06 -050066#Typedef SkImageInfo Info
67
68##
69
Cary Clarka560c472017-11-27 10:44:06 -050070# ------------------------------------------------------------------------------
71
72#Method static sk_sp<SkImage> MakeRasterCopy(const SkPixmap& pixmap)
Cary Clark4855f782018-02-06 09:41:53 -050073#In Constructor
74#Line # creates Image from Pixmap and copied pixels ##
Cary Clark2f466242017-12-11 16:03:17 -050075Creates Image from Pixmap and copy of pixels. Since pixels are copied, Pixmap
76pixels may be modified or deleted without affecting Image.
Cary Clarka560c472017-11-27 10:44:06 -050077
Cary Clark3cd22cc2017-12-01 11:49:58 -050078Image is returned if Pixmap is valid. Valid Pixmap parameters include:
79dimensions are greater than zero;
80each dimension fits in 29 bits;
81Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
82row bytes are large enough to hold one row of pixels;
83pixel address is not nullptr.
84
85#Param pixmap Image_Info, pixel address, and row bytes ##
86
87#Return copy of Pixmap pixels, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -050088
89#Example
Cary Clark2f466242017-12-11 16:03:17 -050090#Height 50
91#Description
92Draw a five by five bitmap, and draw a copy in an Image. Editing the pixmap
93alters the bitmap draw, but does not alter the Image draw since the Image
94contains a copy of the pixels.
95##
96 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
97 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
98 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
99 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
100 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
101 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
102 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
103 SkBitmap bitmap;
104 bitmap.installPixels(pixmap);
105 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
106 *pixmap.writable_addr8(2, 2) = 0x00;
107 canvas->scale(10, 10);
108 canvas->drawBitmap(bitmap, 0, 0);
109 canvas->drawImage(image, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500110##
111
Cary Clark3cd22cc2017-12-01 11:49:58 -0500112#SeeAlso MakeRasterData MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500113
114#Method ##
115
116# ------------------------------------------------------------------------------
117
118#Method static sk_sp<SkImage> MakeRasterData(const Info& info, sk_sp<SkData> pixels, size_t rowBytes)
Cary Clark4855f782018-02-06 09:41:53 -0500119#In Constructor
120#Line # creates Image from Image_Info and shared pixels ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500121Creates Image from Image_Info, sharing pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500122
Cary Clark3cd22cc2017-12-01 11:49:58 -0500123Image is returned if Image_Info is valid. Valid Image_Info parameters include:
124dimensions are greater than zero;
125each dimension fits in 29 bits;
126Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
127rowBytes are large enough to hold one row of pixels;
128pixels is not nullptr, and contains enough data for Image.
129
130#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
131#Param pixels address or pixel storage ##
132#Param rowBytes size of pixel row or larger ##
133
134#Return Image sharing pixels, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500135
136#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500137#Image 3
138 size_t rowBytes = image->width() * SkColorTypeBytesPerPixel(kRGBA_8888_SkColorType);
139 sk_sp<SkData> data = SkData::MakeUninitialized(rowBytes * image->height());
140 SkImageInfo dstInfo = SkImageInfo::MakeN32(image->width(), image->height(),
141 kPremul_SkAlphaType);
142 image->readPixels(dstInfo, data->writable_data(), rowBytes, 0, 0, SkImage::kAllow_CachingHint);
143 sk_sp<SkImage> raw = SkImage::MakeRasterData(dstInfo.makeColorType(kRGBA_8888_SkColorType),
144 data, rowBytes);
145 canvas->drawImage(image, 0, 0);
146 canvas->drawImage(raw.get(), 128, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500147##
148
Cary Clark3cd22cc2017-12-01 11:49:58 -0500149#SeeAlso MakeRasterCopy MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500150
151#Method ##
152
153# ------------------------------------------------------------------------------
154
Cary Clark3cd22cc2017-12-01 11:49:58 -0500155#Typedef void* ReleaseContext
156
157Caller data passed to RasterReleaseProc; may be nullptr.
158
159#SeeAlso MakeFromRaster RasterReleaseProc
160
161##
162
Cary Clarka560c472017-11-27 10:44:06 -0500163#Typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext)
164
Cary Clark3cd22cc2017-12-01 11:49:58 -0500165Function called when Image no longer shares pixels. ReleaseContext is
166provided by caller when Image is created, and may be nullptr.
167
168#SeeAlso ReleaseContext MakeFromRaster
169
Cary Clarka560c472017-11-27 10:44:06 -0500170##
171
172#Method static sk_sp<SkImage> MakeFromRaster(const SkPixmap& pixmap,
173 RasterReleaseProc rasterReleaseProc,
174 ReleaseContext releaseContext)
Cary Clark4855f782018-02-06 09:41:53 -0500175#In Constructor
176#Line # creates Image from Pixmap, with release ##
Cary Clarka560c472017-11-27 10:44:06 -0500177
Cary Clark0c5f5462017-12-15 11:21:51 -0500178Creates Image from pixmap, sharing Pixmap pixels. Pixels must remain valid and
Cary Clark3cd22cc2017-12-01 11:49:58 -0500179unchanged until rasterReleaseProc is called. rasterReleaseProc is passed
180releaseContext when Image is deleted or no longer refers to pixmap pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500181
Cary Clark0c5f5462017-12-15 11:21:51 -0500182Pass nullptr for rasterReleaseProc to share Pixmap without requiring a callback
183when Image is released. Pass nullptr for releaseContext if rasterReleaseProc
184does not require state.
185
Cary Clark3cd22cc2017-12-01 11:49:58 -0500186Image is returned if pixmap is valid. Valid Pixmap parameters include:
187dimensions are greater than zero;
188each dimension fits in 29 bits;
189Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
190row bytes are large enough to hold one row of pixels;
191pixel address is not nullptr.
192
193#Param pixmap Image_Info, pixel address, and row bytes ##
Cary Clark0c5f5462017-12-15 11:21:51 -0500194#Param rasterReleaseProc function called when pixels can be released; or nullptr ##
195#Param releaseContext state passed to rasterReleaseProc; or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500196
Cary Clark0c5f5462017-12-15 11:21:51 -0500197#Return Image sharing pixmap ##
Cary Clarka560c472017-11-27 10:44:06 -0500198
199#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500200#Function
201static void releaseProc(const void* pixels, SkImage::ReleaseContext context) {
202 int* countPtr = static_cast<int*>(context);
203 *countPtr += 1;
204}
205##
206
207void draw(SkCanvas* canvas) {
208 SkColor color = 0;
209 SkPixmap pixmap(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), &color, 4);
210 int releaseCount = 0;
211 sk_sp<SkImage> image(SkImage::MakeFromRaster(pixmap, releaseProc, &releaseCount));
212 SkDebugf("before reset: %d\n", releaseCount);
213 image.reset();
214 SkDebugf("after reset: %d\n", releaseCount);
215}
216#StdOut
217before reset: 0
218after reset: 1
219##
Cary Clarka560c472017-11-27 10:44:06 -0500220##
221
Cary Clark3cd22cc2017-12-01 11:49:58 -0500222#SeeAlso MakeRasterCopy MakeRasterData MakeFromGenerator RasterReleaseProc ReleaseContext
Cary Clarka560c472017-11-27 10:44:06 -0500223
224#Method ##
225
226# ------------------------------------------------------------------------------
227
228#Method static sk_sp<SkImage> MakeFromBitmap(const SkBitmap& bitmap)
Cary Clark4855f782018-02-06 09:41:53 -0500229#In Constructor
230#Line # creates Image from Bitmap, sharing or copying pixels ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500231Creates Image from bitmap, sharing or copying bitmap pixels. If the bitmap
232is marked immutable, and its pixel memory is shareable, it may be shared
233instead of copied.
Cary Clarka560c472017-11-27 10:44:06 -0500234
Cary Clark3cd22cc2017-12-01 11:49:58 -0500235Image is returned if bitmap is valid. Valid Bitmap parameters include:
236dimensions are greater than zero;
237each dimension fits in 29 bits;
238Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
239row bytes are large enough to hold one row of pixels;
240pixel address is not nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500241
Cary Clark3cd22cc2017-12-01 11:49:58 -0500242#Param bitmap Image_Info, row bytes, and pixels ##
243
244#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500245
246#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500247#Description
248The first Bitmap is shared; writing to the pixel memory changes the first
249Image.
250The second Bitmap is marked immutable, and is copied; writing to the pixel
251memory does not alter the second Image.
252##
253#Height 50
254 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
255 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
256 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
257 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
258 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
259 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
260 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
261 SkBitmap bitmap;
262 bitmap.installPixels(pixmap);
263 sk_sp<SkImage> image1 = SkImage::MakeFromBitmap(bitmap);
264 bitmap.setImmutable();
265 sk_sp<SkImage> image2 = SkImage::MakeFromBitmap(bitmap);
266 *pixmap.writable_addr8(2, 2) = 0x00;
267 canvas->scale(10, 10);
268 canvas->drawImage(image1, 0, 0);
269 canvas->drawImage(image2, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500270##
271
Cary Clark3cd22cc2017-12-01 11:49:58 -0500272#SeeAlso MakeFromRaster MakeRasterCopy MakeFromGenerator MakeRasterData
Cary Clarka560c472017-11-27 10:44:06 -0500273
274#Method ##
275
276# ------------------------------------------------------------------------------
277
278#Method static sk_sp<SkImage> MakeFromGenerator(std::unique_ptr<SkImageGenerator> imageGenerator,
279 const SkIRect* subset = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500280#In Constructor
281#Line # creates Image from a stream of data ##
Cary Clarka560c472017-11-27 10:44:06 -0500282
Cary Clark0c5f5462017-12-15 11:21:51 -0500283Creates Image from data returned by imageGenerator. Generated data is owned by Image and may not
284be shared or accessed.
Cary Clarka560c472017-11-27 10:44:06 -0500285
Cary Clark0c5f5462017-12-15 11:21:51 -0500286subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
287otherwise, subset must be contained by image bounds.
288
289Image is returned if generator data is valid. Valid data parameters vary by type of data
290and platform.
Cary Clarka560c472017-11-27 10:44:06 -0500291
Cary Clark3cd22cc2017-12-01 11:49:58 -0500292imageGenerator may wrap Picture data, codec data, or custom data.
293
294#Param imageGenerator stock or custom routines to retrieve Image ##
295#Param subset bounds of returned Image; may be nullptr ##
296
297#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500298
299#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500300#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500301#Description
302The generator returning Picture cannot be shared; std::move transfers ownership to generated Image.
303##
304 SkPictureRecorder recorder;
305 recorder.beginRecording(100, 100)->drawColor(SK_ColorRED);
306 auto picture = recorder.finishRecordingAsPicture();
307 auto gen = SkImageGenerator::MakeFromPicture({100, 100}, picture, nullptr, nullptr,
308 SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB());
309 sk_sp<SkImage> image = SkImage::MakeFromGenerator(std::move(gen));
310 canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500311##
312
Cary Clark3cd22cc2017-12-01 11:49:58 -0500313#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500314
315#Method ##
316
317# ------------------------------------------------------------------------------
318
319#Method static sk_sp<SkImage> MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500320#In Constructor
321#Line # creates Image from encoded data ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500322Creates Image from encoded data.
Cary Clark0c5f5462017-12-15 11:21:51 -0500323subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
324otherwise, subset must be contained by image bounds.
Cary Clarka560c472017-11-27 10:44:06 -0500325
Cary Clark3cd22cc2017-12-01 11:49:58 -0500326Image is returned if format of the encoded data is recognized and supported.
Cary Clark4855f782018-02-06 09:41:53 -0500327Recognized formats vary by platform.
Cary Clarka560c472017-11-27 10:44:06 -0500328
Cary Clark3cd22cc2017-12-01 11:49:58 -0500329#Param encoded data of Image to decode ##
330#Param subset bounds of returned Image; may be nullptr ##
331
332#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500333
Cary Clark61ca7c52018-01-02 11:34:14 -0500334#Example
335#Image 3
336int x = 0;
337for (int quality : { 100, 50, 10, 1} ) {
338 sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, quality);
339 sk_sp<SkImage> image = SkImage::MakeFromEncoded(encodedData);
340 canvas->drawImage(image, x, 0);
341 x += 64;
342}
Cary Clarka560c472017-11-27 10:44:06 -0500343##
344
Cary Clark3cd22cc2017-12-01 11:49:58 -0500345#SeeAlso MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500346
347#Method ##
348
349# ------------------------------------------------------------------------------
350
351#Typedef void (*TextureReleaseProc)(ReleaseContext releaseContext)
352
353##
354
355#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
356 const GrBackendTexture& backendTexture,
357 GrSurfaceOrigin origin,
358 SkAlphaType alphaType,
359 sk_sp<SkColorSpace> colorSpace)
Cary Clarkac47b882018-01-11 10:35:44 -0500360#Deprecated
Cary Clark61ca7c52018-01-02 11:34:14 -0500361#Method ##
362
363# ------------------------------------------------------------------------------
364
365#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
366 const GrBackendTexture& backendTexture,
367 GrSurfaceOrigin origin,
368 SkAlphaType alphaType,
369 sk_sp<SkColorSpace> colorSpace,
370 TextureReleaseProc textureReleaseProc,
371 ReleaseContext releaseContext)
372
Cary Clarkac47b882018-01-11 10:35:44 -0500373#Deprecated
Cary Clark61ca7c52018-01-02 11:34:14 -0500374#Method ##
375
376# ------------------------------------------------------------------------------
377
378#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
379 const GrBackendTexture& backendTexture,
380 GrSurfaceOrigin origin,
381 SkColorType colorType,
382 SkAlphaType alphaType,
383 sk_sp<SkColorSpace> colorSpace)
Cary Clarkf895a422018-02-27 09:54:21 -0500384#In Constructor
385#Line # creates Image from GPU_Texture ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500386Creates Image from GPU_Texture associated with context. Caller is responsible for
387managing the lifetime of GPU_Texture.
388
389Image is returned if format of backendTexture is recognized and supported.
390Recognized formats vary by GPU back-end.
391
392#Param context GPU_Context ##
393#Param backendTexture texture residing on GPU ##
394#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500395#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500396##
397#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
398 kPremul_SkAlphaType, kUnpremul_SkAlphaType
399##
400#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500401
402#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500403
404#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500405#Image 3
406#Platform gpu
407#Height 128
408#Description
409A back-end texture has been created and uploaded to the GPU outside of this example.
410##
411GrContext* context = canvas->getGrContext();
412if (!context) {
413 return;
414}
415canvas->scale(.25f, .25f);
416int x = 0;
417for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500418 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark56356312018-02-08 14:45:18 -0500419 origin, kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
Cary Clark0c5f5462017-12-15 11:21:51 -0500420 canvas->drawImage(image, x, 0);
421x += 512;
422}
Cary Clarka560c472017-11-27 10:44:06 -0500423##
424
Cary Clark3cd22cc2017-12-01 11:49:58 -0500425#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500426
427#Method ##
428
429# ------------------------------------------------------------------------------
430
431#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
432 const GrBackendTexture& backendTexture,
433 GrSurfaceOrigin origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500434 SkColorType colorType,
Cary Clarka560c472017-11-27 10:44:06 -0500435 SkAlphaType alphaType,
436 sk_sp<SkColorSpace> colorSpace,
437 TextureReleaseProc textureReleaseProc,
438 ReleaseContext releaseContext)
439
Cary Clark61ca7c52018-01-02 11:34:14 -0500440Creates Image from GPU_Texture associated with context. GPU_Texture must stay
Cary Clark3cd22cc2017-12-01 11:49:58 -0500441valid and unchanged until textureReleaseProc is called. textureReleaseProc is
442passed releaseContext when Image is deleted or no longer refers to texture.
Cary Clarka560c472017-11-27 10:44:06 -0500443
Cary Clark3cd22cc2017-12-01 11:49:58 -0500444Image is returned if format of backendTexture is recognized and supported.
445Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500446
Cary Clark3cd22cc2017-12-01 11:49:58 -0500447#Param context GPU_Context ##
448#Param backendTexture texture residing on GPU ##
449#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500450#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500451##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500452#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
453 kPremul_SkAlphaType, kUnpremul_SkAlphaType
454##
Cary Clark61ca7c52018-01-02 11:34:14 -0500455#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500456#Param textureReleaseProc function called when texture can be released ##
457#Param releaseContext state passed to textureReleaseProc ##
458
459#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500460
Cary Clark0c5f5462017-12-15 11:21:51 -0500461#ToDo
462This doesn't do anything clever with TextureReleaseProc because it may not get called
Cary Clark61ca7c52018-01-02 11:34:14 -0500463fwithin the lifetime of the example
Cary Clark0c5f5462017-12-15 11:21:51 -0500464##
465
Cary Clarka560c472017-11-27 10:44:06 -0500466#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500467#Platform gpu
468#Image 4
Cary Clark0c5f5462017-12-15 11:21:51 -0500469GrContext* context = canvas->getGrContext();
470if (!context) {
471 return;
472}
Cary Clarkac47b882018-01-11 10:35:44 -0500473auto debugster = [](SkImage::ReleaseContext releaseContext) -> void {
474 *((int *) releaseContext) += 128;
Cary Clark0c5f5462017-12-15 11:21:51 -0500475};
476int x = 0;
477for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500478 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark61ca7c52018-01-02 11:34:14 -0500479 origin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr, debugster, &x);
Cary Clark0c5f5462017-12-15 11:21:51 -0500480 canvas->drawImage(image, x, 0);
481 x += 128;
482}
Cary Clarka560c472017-11-27 10:44:06 -0500483##
484
Cary Clark3cd22cc2017-12-01 11:49:58 -0500485#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500486
487#Method ##
488
489# ------------------------------------------------------------------------------
490
491#Method static sk_sp<SkImage> MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> data,
492 bool buildMips,
493 SkColorSpace* dstColorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500494#In Constructor
495#Line # creates Image from encoded data, and uploads to GPU ##
Cary Clarka560c472017-11-27 10:44:06 -0500496
Cary Clark3cd22cc2017-12-01 11:49:58 -0500497Creates Image from encoded data. Image is uploaded to GPU back-end using context.
498
499Created Image is available to other GPU contexts, and is available across thread
500boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
501share resources.
502
503When Image is no longer referenced, context releases texture memory
Cary Clarka560c472017-11-27 10:44:06 -0500504asynchronously.
Cary Clarka560c472017-11-27 10:44:06 -0500505
Cary Clark3cd22cc2017-12-01 11:49:58 -0500506Texture decoded from data is uploaded to match Surface created with
507dstColorSpace. Color_Space of Image is determined by encoded data.
Cary Clarka560c472017-11-27 10:44:06 -0500508
Cary Clark3cd22cc2017-12-01 11:49:58 -0500509Image is returned if format of data is recognized and supported, and if context
510supports moving resources. Recognized formats vary by platform and GPU back-end.
511
Cary Clark61ca7c52018-01-02 11:34:14 -0500512Image is returned using MakeFromEncoded if context is nullptr or does not support
513moving resources between contexts.
514
Cary Clark3cd22cc2017-12-01 11:49:58 -0500515#Param context GPU_Context ##
516#Param data Image to decode ##
517#Param buildMips create Image as Mip_Map if true ##
518#Param dstColorSpace range of colors of matching Surface on GPU ##
519
520#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500521
522#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500523#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500524#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500525GrContext* context = canvas->getGrContext();
526sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, 100);
527sk_sp<SkImage> image = SkImage::MakeCrossContextFromEncoded(context,
528 encodedData, false, nullptr);
529canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500530##
531
Cary Clark3cd22cc2017-12-01 11:49:58 -0500532#SeeAlso MakeCrossContextFromPixmap
533
534#Method ##
535
536# ------------------------------------------------------------------------------
537
538#Method static sk_sp<SkImage> MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap,
539 bool buildMips,
540 SkColorSpace* dstColorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500541#In Constructor
542#Line # creates Image from Pixmap, and uploads to GPU ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500543
544Creates Image from pixmap. Image is uploaded to GPU back-end using context.
545
546Created Image is available to other GPU contexts, and is available across thread
547boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
548share resources.
549
550When Image is no longer referenced, context releases texture memory
551asynchronously.
552
553Texture created from pixmap is uploaded to match Surface created with
554dstColorSpace. Color_Space of Image is determined by pixmap.colorSpace().
555
Cary Clark61ca7c52018-01-02 11:34:14 -0500556Image is returned referring to GPU back-end if context is not nullptr,
557format of data is recognized and supported, and if context supports moving
558resources between contexts. Otherwise, pixmap pixel data is copied and Image
559as returned in raster format if possible; nullptr may be returned.
560Recognized GPU formats vary by platform and GPU back-end.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500561
562#Param context GPU_Context ##
563#Param pixmap Image_Info, pixel address, and row bytes ##
564#Param buildMips create Image as Mip_Map if true ##
565#Param dstColorSpace range of colors of matching Surface on GPU ##
566
567#Return created Image, or nullptr ##
568
569#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500570#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500571#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500572GrContext* context = canvas->getGrContext();
573SkPixmap pixmap;
574if (source.peekPixels(&pixmap)) {
575 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(context, pixmap,
576 false, nullptr);
577 canvas->drawImage(image, 0, 0);
578}
Cary Clark3cd22cc2017-12-01 11:49:58 -0500579##
580
581#SeeAlso MakeCrossContextFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500582
583#Method ##
584
585# ------------------------------------------------------------------------------
586
587#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
588 const GrBackendTexture& backendTexture,
589 GrSurfaceOrigin surfaceOrigin,
590 SkAlphaType alphaType = kPremul_SkAlphaType,
591 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500592#Deprecated
Cary Clark61ca7c52018-01-02 11:34:14 -0500593#Method ##
594
595# ------------------------------------------------------------------------------
596
597#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
598 const GrBackendTexture& backendTexture,
599 GrSurfaceOrigin surfaceOrigin,
600 SkColorType colorType,
601 SkAlphaType alphaType = kPremul_SkAlphaType,
602 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500603#In Constructor
604#Line # creates Image from GPU_Texture, managed internally ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500605Creates Image from backendTexture associated with context. backendTexture and
606returned Image are managed internally, and are released when no longer needed.
Cary Clarka560c472017-11-27 10:44:06 -0500607
Cary Clark3cd22cc2017-12-01 11:49:58 -0500608Image is returned if format of backendTexture is recognized and supported.
609Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500610
Cary Clark3cd22cc2017-12-01 11:49:58 -0500611#Param context GPU_Context ##
612#Param backendTexture texture residing on GPU ##
613#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500614#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500615##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500616#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
617 kPremul_SkAlphaType, kUnpremul_SkAlphaType
618##
Cary Clark61ca7c52018-01-02 11:34:14 -0500619#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500620
621#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500622
623#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500624#Image 5
625#Platform gpu
Cary Clark61ca7c52018-01-02 11:34:14 -0500626 if (!canvas->getGrContext()) {
627 return;
628 }
629 canvas->scale(.5f, .5f);
630 canvas->clear(0x7f3f5f7f);
631 int x = 0, y = 0;
632 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
633 for (auto alpha : { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType } ) {
634 sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(canvas->getGrContext(),
635 backEndTexture, origin,
636 kRGBA_8888_SkColorType, alpha);
637 canvas->drawImage(image, x, y);
638 x += 160;
639 }
640 x -= 160 * 3;
641 y += 256;
642 }
Cary Clarka560c472017-11-27 10:44:06 -0500643##
644
Cary Clark61ca7c52018-01-02 11:34:14 -0500645#SeeAlso MakeFromTexture MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500646
647#Method ##
648
649# ------------------------------------------------------------------------------
650
651#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400652 const GrBackendTexture yuvTextures[3],
Cary Clarka560c472017-11-27 10:44:06 -0500653 GrSurfaceOrigin surfaceOrigin,
654 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500655#In Constructor
656#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500657
Brian Salomon6a426c12018-03-15 12:16:02 -0400658Creates Image from copy of yuvTextures, an array of textures on GPU.
659yuvTextures contain pixels for YUV planes of Image. Returned Image has the dimensions
660yuvTextures[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500661
Cary Clark61ca7c52018-01-02 11:34:14 -0500662#Param context GPU_Context ##
663#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
664 kRec709_SkYUVColorSpace
665##
Brian Salomon6a426c12018-03-15 12:16:02 -0400666#Param yuvTextures array of YUV textures on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500667#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
668#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500669
Cary Clark61ca7c52018-01-02 11:34:14 -0500670#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500671
Cary Clark61ca7c52018-01-02 11:34:14 -0500672# seems too complicated to create an example for this
673#ToDo
674should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500675##
676
Cary Clark61ca7c52018-01-02 11:34:14 -0500677#NoExample
678##
679
680#SeeAlso MakeFromNV12TexturesCopy
681
682#Method ##
683
684# ------------------------------------------------------------------------------
685
686#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400687 const GrBackendTexture yuvTextures[3],
Cary Clark61ca7c52018-01-02 11:34:14 -0500688 const SkISize yuvSizes[3],
689 GrSurfaceOrigin surfaceOrigin,
690 sk_sp<SkColorSpace> colorSpace = nullptr)
Brian Salomon6a426c12018-03-15 12:16:02 -0400691This version of MakeFromYUVTexturesCopy is over specified and should not be used.
Cary Clark61ca7c52018-01-02 11:34:14 -0500692
693#Param context GPU_Context ##
694#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
695 kRec709_SkYUVColorSpace
696##
Brian Salomon6a426c12018-03-15 12:16:02 -0400697#Param yuvTextures array of YUV textures on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500698#Param yuvSizes dimensions of YUV textures ##
699#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
700#Param colorSpace range of colors; may be nullptr ##
701
702#Return created Image, or nullptr ##
703
704# seems too complicated to create an example for this
705#ToDo
706should this be moved to chrome only?
707##
708
709#NoExample
710##
711
712#SeeAlso MakeFromNV12TexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500713
714#Method ##
715
716# ------------------------------------------------------------------------------
717
718#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
719 SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400720 const GrBackendTexture nv12Textures[2],
Cary Clarka560c472017-11-27 10:44:06 -0500721 GrSurfaceOrigin surfaceOrigin,
722 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500723#In Constructor
Brian Salomon6a426c12018-03-15 12:16:02 -0400724#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500725
Cary Clark61ca7c52018-01-02 11:34:14 -0500726Creates Image from copy of nv12TextureHandles, an array of textures on GPU.
Brian Salomon6a426c12018-03-15 12:16:02 -0400727nv12Textures[0] contains pixels for YUV_Component_Y plane.
728nv12Textures[1] contains pixels for YUV_Component_U plane,
Cary Clark61ca7c52018-01-02 11:34:14 -0500729followed by pixels for YUV_Component_V plane.
Brian Salomon6a426c12018-03-15 12:16:02 -0400730Returned Image has the dimensions
731nv12Textures[2]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500732
Cary Clark61ca7c52018-01-02 11:34:14 -0500733#Param context GPU_Context ##
734#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
735 kRec709_SkYUVColorSpace
736##
Brian Salomon6a426c12018-03-15 12:16:02 -0400737#Param nv12Textures array of YUV textures on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500738#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
739#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500740
Cary Clark61ca7c52018-01-02 11:34:14 -0500741#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500742
Cary Clark61ca7c52018-01-02 11:34:14 -0500743# seems too complicated to create an example for this
744#ToDo
745should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500746##
747
Cary Clark61ca7c52018-01-02 11:34:14 -0500748#NoExample
749##
750
751#SeeAlso MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500752
753#Method ##
754
755# ------------------------------------------------------------------------------
756
Cary Clark61ca7c52018-01-02 11:34:14 -0500757#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
758 SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400759 const GrBackendTexture nv12Textures[2],
Cary Clark61ca7c52018-01-02 11:34:14 -0500760 const SkISize nv12Sizes[2],
761 GrSurfaceOrigin surfaceOrigin,
762 sk_sp<SkColorSpace> colorSpace = nullptr)
763
Brian Salomon6a426c12018-03-15 12:16:02 -0400764This version of MakeFromYUVTexturesCopy is over specified and should not be used.
Cary Clark61ca7c52018-01-02 11:34:14 -0500765
766#Param context GPU_Context ##
767#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
768 kRec709_SkYUVColorSpace
769##
Brian Salomon6a426c12018-03-15 12:16:02 -0400770#Param nv12Textures array of YUV textures on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500771#Param nv12Sizes dimensions of YUV textures ##
772#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
773#Param colorSpace range of colors; may be nullptr ##
774
775#Return created Image, or nullptr ##
776
777# seems too complicated to create an example for this
778#ToDo
779should this be moved to chrome only?
780##
781
782#NoExample
783##
784
785#SeeAlso MakeFromYUVTexturesCopy
786
787#Method ##
788
789# ------------------------------------------------------------------------------
790
Cary Clark4855f782018-02-06 09:41:53 -0500791# currently uncalled by any test or client ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500792#Bug 7424
Cary Clark61ca7c52018-01-02 11:34:14 -0500793
Cary Clark56356312018-02-08 14:45:18 -0500794#EnumClass BitDepth
Cary Clarka560c472017-11-27 10:44:06 -0500795
796#Code
Cary Clark61ca7c52018-01-02 11:34:14 -0500797 enum class BitDepth {
Cary Clarka560c472017-11-27 10:44:06 -0500798 kU8,
799 kF16,
800 };
801##
802
803#Const kU8 0
Cary Clark61ca7c52018-01-02 11:34:14 -0500804Use 8 bits per Color_ARGB component using unsigned integer format.
Cary Clarka560c472017-11-27 10:44:06 -0500805##
806#Const kF16 1
Cary Clark61ca7c52018-01-02 11:34:14 -0500807Use 16 bits per Color_ARGB component using half-precision floating point format.
Cary Clarka560c472017-11-27 10:44:06 -0500808##
809
Cary Clark61ca7c52018-01-02 11:34:14 -0500810#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500811##
812
Cary Clark61ca7c52018-01-02 11:34:14 -0500813#SeeAlso MakeFromPicture
Cary Clarka560c472017-11-27 10:44:06 -0500814
Cary Clark56356312018-02-08 14:45:18 -0500815#EnumClass ##
Cary Clarka560c472017-11-27 10:44:06 -0500816
817# ------------------------------------------------------------------------------
818
819#Method static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
820 const SkMatrix* matrix, const SkPaint* paint,
821 BitDepth bitDepth,
822 sk_sp<SkColorSpace> colorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500823#In Constructor
824#Line # creates Image from Picture ##
Cary Clarka560c472017-11-27 10:44:06 -0500825
Cary Clark61ca7c52018-01-02 11:34:14 -0500826Creates Image from picture. Returned Image width and height are set by dimensions.
827Image draws picture with matrix and paint, set to bitDepth and colorSpace.
Cary Clarka560c472017-11-27 10:44:06 -0500828
Cary Clark61ca7c52018-01-02 11:34:14 -0500829If matrix is nullptr, draws with identity Matrix. If paint is nullptr, draws
830with default Paint. colorSpace may be nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500831
Cary Clark61ca7c52018-01-02 11:34:14 -0500832#Param picture stream of drawing commands ##
833#Param dimensions width and height ##
834#Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ##
835#Param paint Paint to apply transparency, filtering, and so on; may be nullptr ##
836#Param bitDepth 8 bit integer or 16 bit float: per component ##
837#Param colorSpace range of colors; may be nullptr ##
838
839#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500840
841#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500842 SkPaint paint;
843 SkPictureRecorder recorder;
844 SkCanvas* recordingCanvas = recorder.beginRecording(50, 50);
845 for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) {
846 paint.setColor(color);
847 recordingCanvas->drawRect({10, 10, 30, 40}, paint);
848 recordingCanvas->translate(10, 10);
849 recordingCanvas->scale(1.2f, 1.4f);
850 }
851 sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
852 int x = 0, y = 0;
853 for (auto alpha : { 70, 140, 210 } ) {
854 paint.setAlpha(alpha);
855 auto srgbColorSpace = SkColorSpace::MakeSRGB();
856 sk_sp<SkImage> image = SkImage::MakeFromPicture(playback, {50, 50}, nullptr, &paint,
857 SkImage::BitDepth::kU8, srgbColorSpace);
858 canvas->drawImage(image, x, y);
859 x += 70; y += 70;
860 }
Cary Clarka560c472017-11-27 10:44:06 -0500861##
862
Cary Clark61ca7c52018-01-02 11:34:14 -0500863#SeeAlso SkCanvas::drawPicture
Cary Clarka560c472017-11-27 10:44:06 -0500864
865#Method ##
866
867# ------------------------------------------------------------------------------
868
869#Method static sk_sp<SkImage> MakeFromAHardwareBuffer(AHardwareBuffer* hardwareBuffer,
870 SkAlphaType alphaType = kPremul_SkAlphaType,
871 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500872#In Constructor
873#Line # creates Image from Android hardware buffer ##
Cary Clarka560c472017-11-27 10:44:06 -0500874
Cary Clark4855f782018-02-06 09:41:53 -0500875#Bug 7447
Cary Clarka560c472017-11-27 10:44:06 -0500876
Cary Clark61ca7c52018-01-02 11:34:14 -0500877Creates Image from Android hardware buffer.
878Returned Image takes a reference on the buffer.
Cary Clarka560c472017-11-27 10:44:06 -0500879
Cary Clark61ca7c52018-01-02 11:34:14 -0500880Only available on Android, when __ANDROID_API__ is defined to be 26 or greater.
Cary Clarka560c472017-11-27 10:44:06 -0500881
Cary Clark61ca7c52018-01-02 11:34:14 -0500882#Param hardwareBuffer AHardwareBuffer Android hardware buffer ##
883#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
884 kPremul_SkAlphaType, kUnpremul_SkAlphaType
885##
886#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500887
Cary Clark61ca7c52018-01-02 11:34:14 -0500888#Return created Image, or nullptr ##
889
890#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500891##
892
Cary Clark61ca7c52018-01-02 11:34:14 -0500893#SeeAlso MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -0500894
895#Method ##
896
897# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -0500898#Subtopic Property
899#Populate
900#Line # values and attributes ##
901##
Cary Clarka560c472017-11-27 10:44:06 -0500902
903#Method int width() const
Cary Clark4855f782018-02-06 09:41:53 -0500904#In Property
905#Line # returns pixel column count ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500906Returns pixel count in each row.
907
908#Return pixel width in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500909
910#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500911#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500912#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500913 canvas->translate(10, 10);
914 canvas->drawImage(image, 0, 0);
915 canvas->translate(0, image->height());
916 SkPaint paint;
917 paint.setTextAlign(SkPaint::kCenter_Align);
918 canvas->drawLine(0, 10, image->width(), 10, paint);
919 canvas->drawString("width", image->width() / 2, 25, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500920##
921
Cary Clark61ca7c52018-01-02 11:34:14 -0500922#SeeAlso dimensions() height()
Cary Clarka560c472017-11-27 10:44:06 -0500923
924#Method ##
925
926# ------------------------------------------------------------------------------
927
928#Method int height() const
Cary Clark4855f782018-02-06 09:41:53 -0500929#In Property
930#Line # returns pixel row count ##
Cary Clark2f466242017-12-11 16:03:17 -0500931Returns pixel row count.
932
Cary Clark61ca7c52018-01-02 11:34:14 -0500933#Return pixel height in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500934
935#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500936#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500937#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500938 canvas->translate(10, 10);
939 canvas->drawImage(image, 0, 0);
940 canvas->translate(image->width(), 0);
941 SkPaint paint;
942 paint.setTextAlign(SkPaint::kCenter_Align);
943 paint.setVerticalText(true);
944 canvas->drawLine(10, 0, 10, image->height(), paint);
Cary Clarkac47b882018-01-11 10:35:44 -0500945 canvas->drawString("height", 25, image->height() / 2, paint);
946##
Cary Clarka560c472017-11-27 10:44:06 -0500947
Cary Clark61ca7c52018-01-02 11:34:14 -0500948#SeeAlso dimensions() width()
Cary Clarka560c472017-11-27 10:44:06 -0500949
950#Method ##
951
952# ------------------------------------------------------------------------------
953
954#Method SkISize dimensions() const
Cary Clark4855f782018-02-06 09:41:53 -0500955#In Property
956#Line # returns width() and height() ##
Cary Clark2f466242017-12-11 16:03:17 -0500957Returns ISize { width(), height() }.
958
959#Return integral size of width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -0500960
961#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500962#Image 4
963 SkISize dimensions = image->dimensions();
964 SkIRect bounds = image->bounds();
965 SkIRect dimensionsAsBounds = SkIRect::MakeSize(dimensions);
966 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clarka560c472017-11-27 10:44:06 -0500967##
968
Cary Clark61ca7c52018-01-02 11:34:14 -0500969#SeeAlso height() width() bounds()
Cary Clarka560c472017-11-27 10:44:06 -0500970
971#Method ##
972
973# ------------------------------------------------------------------------------
974
975#Method SkIRect bounds() const
Cary Clark4855f782018-02-06 09:41:53 -0500976#In Property
977#Line # returns width() and height() as Rectangle ##
Cary Clark2f466242017-12-11 16:03:17 -0500978Returns IRect { 0, 0, width(), height() }.
979
980#Return integral rectangle from origin to width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -0500981
982#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500983#Height 128
984#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -0500985 SkIRect bounds = image->bounds();
Cary Clarkac47b882018-01-11 10:35:44 -0500986 for (int x : { 0, bounds.width() } ) {
987 for (int y : { 0, bounds.height() } ) {
Cary Clark61ca7c52018-01-02 11:34:14 -0500988 canvas->drawImage(image, x, y);
989 }
990 }
Cary Clarka560c472017-11-27 10:44:06 -0500991##
992
Cary Clark61ca7c52018-01-02 11:34:14 -0500993#SeeAlso dimensions()
Cary Clarka560c472017-11-27 10:44:06 -0500994
995#Method ##
996
997# ------------------------------------------------------------------------------
998
999#Method uint32_t uniqueID() const
Cary Clark4855f782018-02-06 09:41:53 -05001000#In Property
1001#Line # identifier for Image ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001002Returns value unique to image. Image contents cannot change after Image is
1003created. Any operation to create a new Image will receive generate a new
1004unique number.
1005
1006#Return unique identifier ##
Cary Clarka560c472017-11-27 10:44:06 -05001007
1008#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001009#Image 5
1010#Height 156
1011 sk_sp<SkImage> subset = image->makeSubset({10, 20, 90, 100});
1012 canvas->drawImage(image, 0, 0);
1013 canvas->drawImage(subset, 128, 0);
1014 SkPaint paint;
1015 SkString s;
1016 s.printf("original id: %d", image->uniqueID());
1017 canvas->drawString(s, 20, image->height() + 20, paint);
1018 s.printf("subset id: %d", subset->uniqueID());
1019 canvas->drawString(s, 148, subset->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001020##
1021
Cary Clark61ca7c52018-01-02 11:34:14 -05001022#SeeAlso isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001023
1024#Method ##
1025
1026# ------------------------------------------------------------------------------
1027
1028#Method SkAlphaType alphaType() const
Cary Clark4855f782018-02-06 09:41:53 -05001029#In Property
1030#Line # returns Alpha_Type ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001031Returns Alpha_Type, one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
1032kPremul_SkAlphaType, kUnpremul_SkAlphaType.
1033
1034Alpha_Type returned was a parameter to an Image constructor,
1035or was parsed from encoded data.
1036
1037#Return Alpha_Type in Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001038
1039#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001040#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -05001041#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -05001042 const char* alphaTypeStr[] = { "Unknown", "Opaque", "Premul", "Unpremul" };
1043 SkAlphaType alphaType = image->alphaType();
Cary Clarkac47b882018-01-11 10:35:44 -05001044 canvas->drawImage(image, 16, 0);
Cary Clark61ca7c52018-01-02 11:34:14 -05001045 SkPaint paint;
1046 canvas->drawString(alphaTypeStr[(int) alphaType], 20, image->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001047##
1048
Cary Clark61ca7c52018-01-02 11:34:14 -05001049#SeeAlso SkImageInfo::alphaType
Cary Clarka560c472017-11-27 10:44:06 -05001050
1051#Method ##
1052
1053# ------------------------------------------------------------------------------
1054
Greg Daniel56008aa2018-03-14 15:33:42 -04001055#Method SkColorType colorType() const
1056#In Property
1057#Line # returns Color_Type ##
1058
1059Returns Color_Type if known; otherwise, returns kUnknown_SkColorType.
1060
1061#Return Color_Type of Image ##
1062
1063#Example
1064// incomplete
1065##
1066
1067#SeeAlso SkImageInfo::colorType
1068
1069#Method ##
1070
1071# ------------------------------------------------------------------------------
1072
Cary Clarka560c472017-11-27 10:44:06 -05001073#Method SkColorSpace* colorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001074#In Property
1075#Line # returns Color_Space ##
Cary Clark2f466242017-12-11 16:03:17 -05001076Returns Color_Space, the range of colors, associated with Image. The
1077reference count of Color_Space is unchanged. The returned Color_Space is
1078immutable.
Cary Clarka560c472017-11-27 10:44:06 -05001079
Cary Clark61dfc3a2018-01-03 08:37:53 -05001080Color_Space returned was passed to an Image constructor,
1081or was parsed from encoded data. Color_Space returned may be ignored when Image
1082is drawn, depending on the capabilities of the Surface receiving the drawing.
Cary Clark2f466242017-12-11 16:03:17 -05001083
1084#Return Color_Space in Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001085
1086#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001087#Image 3
1088#Set sRGB
1089 SkPixmap pixmap;
1090 source.peekPixels(&pixmap);
1091 canvas->scale(.25f, .25f);
1092 int y = 0;
1093 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1094 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1095 int x = 0;
1096 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1097 for (int index = 0; index < 2; ++index) {
1098 pixmap.setColorSpace(colorSpace);
1099 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1100 canvas->drawImage(image, x, y);
1101 colorSpace = image->colorSpace()->makeColorSpin();
1102 x += 512;
1103 }
1104 y += 512;
1105 }
Cary Clarka560c472017-11-27 10:44:06 -05001106##
1107
Cary Clark61dfc3a2018-01-03 08:37:53 -05001108#SeeAlso refColorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001109
1110#Method ##
1111
1112# ------------------------------------------------------------------------------
1113
1114#Method sk_sp<SkColorSpace> refColorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001115#In Property
1116#Line # returns Image_Info Color_Space ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001117Returns a smart pointer to Color_Space, the range of colors, associated with
1118Image. The smart pointer tracks the number of objects sharing this
1119SkColorSpace reference so the memory is released when the owners destruct.
1120
1121The returned SkColorSpace is immutable.
1122
1123Color_Space returned was passed to an Image constructor,
1124or was parsed from encoded data. Color_Space returned may be ignored when Image
1125is drawn, depending on the capabilities of the Surface receiving the drawing.
1126
1127#Return Color_Space in Image, or nullptr, wrapped in a smart pointer ##
Cary Clarka560c472017-11-27 10:44:06 -05001128
1129#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001130#Image 3
1131#Set sRGB
1132 SkPixmap pixmap;
1133 source.peekPixels(&pixmap);
1134 canvas->scale(.25f, .25f);
1135 int y = 0;
1136 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1137 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1138 int x = 0;
1139 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1140 for (int index = 0; index < 2; ++index) {
1141 pixmap.setColorSpace(colorSpace);
1142 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1143 canvas->drawImage(image, x, y);
1144 colorSpace = image->refColorSpace()->makeColorSpin();
1145 x += 512;
1146 }
1147 y += 512;
1148 }
Cary Clarka560c472017-11-27 10:44:06 -05001149##
1150
Cary Clark61dfc3a2018-01-03 08:37:53 -05001151#SeeAlso colorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001152
1153#Method ##
1154
1155# ------------------------------------------------------------------------------
1156
1157#Method bool isAlphaOnly() const
Cary Clark4855f782018-02-06 09:41:53 -05001158#In Property
1159#Line # returns if pixels represent a transparency mask ##
Cary Clark2f466242017-12-11 16:03:17 -05001160Returns true if Image pixels represent transparency only. If true, each pixel
1161is packed in 8 bits as defined by kAlpha_8_SkColorType.
Cary Clarka560c472017-11-27 10:44:06 -05001162
Cary Clark2f466242017-12-11 16:03:17 -05001163#Return true if pixels represent a transparency mask ##
Cary Clarka560c472017-11-27 10:44:06 -05001164
1165#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001166 uint8_t pmColors = 0;
1167 sk_sp<SkImage> image = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1), &pmColors, 1});
1168 SkDebugf("alphaOnly = %s\n", image->isAlphaOnly() ? "true" : "false");
1169#StdOut
1170alphaOnly = true
1171##
Cary Clarka560c472017-11-27 10:44:06 -05001172##
1173
Cary Clark61dfc3a2018-01-03 08:37:53 -05001174#SeeAlso alphaType isOpaque
Cary Clarka560c472017-11-27 10:44:06 -05001175
1176#Method ##
1177
1178# ------------------------------------------------------------------------------
1179
1180#Method bool isOpaque() const
Cary Clark4855f782018-02-06 09:41:53 -05001181#In Property
1182#Line # returns if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001183Returns true if pixels ignore their Alpha value and are treated as fully opaque.
Cary Clark2f466242017-12-11 16:03:17 -05001184
1185#Return true if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clarka560c472017-11-27 10:44:06 -05001186
1187#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001188 auto check_isopaque = [](const SkImageInfo& imageInfo) -> void {
1189 auto surface(SkSurface::MakeRaster(imageInfo));
1190 auto image(surface->makeImageSnapshot());
1191 SkDebugf("isOpaque = %s\n", image->isOpaque() ? "true" : "false");
1192 };
1193
1194 check_isopaque(SkImageInfo::MakeN32Premul(5, 5));
1195 check_isopaque(SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType));
1196#StdOut
1197isOpaque = false
1198isOpaque = true
1199##
Cary Clarka560c472017-11-27 10:44:06 -05001200##
1201
Cary Clark61dfc3a2018-01-03 08:37:53 -05001202#SeeAlso alphaType isAlphaOnly
Cary Clarka560c472017-11-27 10:44:06 -05001203
1204#Method ##
1205
1206# ------------------------------------------------------------------------------
1207
1208#Method sk_sp<SkShader> makeShader(SkShader::TileMode tileMode1, SkShader::TileMode tileMode2,
1209 const SkMatrix* localMatrix = nullptr) const
Cary Clark4855f782018-02-06 09:41:53 -05001210#In Constructor
1211#Line # creates Shader, Paint element that can tile Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001212
Cary Clark61dfc3a2018-01-03 08:37:53 -05001213Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1214SkShader::TileMode rules to fill drawn area outside Image. localMatrix permits
1215transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001216
Cary Clark61dfc3a2018-01-03 08:37:53 -05001217#Param tileMode1 tiling in x, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1218 SkShader::kMirror_TileMode
1219##
1220#Param tileMode2 tiling in y, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1221 SkShader::kMirror_TileMode
1222##
1223#Param localMatrix Image transformation, or nullptr ##
1224
1225#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001226
1227#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001228#Image 4
1229SkMatrix matrix;
1230matrix.setRotate(45);
1231SkPaint paint;
1232paint.setShader(image->makeShader(SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode,
1233 &matrix));
1234canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001235##
1236
Cary Clark61dfc3a2018-01-03 08:37:53 -05001237#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001238
1239#Method ##
1240
1241# ------------------------------------------------------------------------------
1242
1243#Method sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const
1244
Cary Clark61dfc3a2018-01-03 08:37:53 -05001245Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1246SkShader::kClamp_TileMode to fill drawn area outside Image. localMatrix permits
1247transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001248
Cary Clark61dfc3a2018-01-03 08:37:53 -05001249#Param localMatrix Image transformation, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001250
Cary Clark61dfc3a2018-01-03 08:37:53 -05001251#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001252
1253#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001254#Image 5
1255SkMatrix matrix;
1256matrix.setRotate(45);
1257matrix.postTranslate(125, 30);
1258SkPaint paint;
1259paint.setShader(image->makeShader(&matrix));
1260canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001261##
1262
Cary Clarkf5404bb2018-01-05 12:10:09 -05001263#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001264
1265#Method ##
1266
1267# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001268#Subtopic Pixels
1269#Populate
1270#Line # read and write pixel values ##
1271##
Cary Clarka560c472017-11-27 10:44:06 -05001272
1273#Method bool peekPixels(SkPixmap* pixmap) const
Cary Clark78de7512018-02-07 07:27:09 -05001274#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001275#Line # returns Pixmap if possible ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001276Copies Image pixel address, row bytes, and Image_Info to pixmap, if address
1277is available, and returns true. If pixel address is not available, return
1278false and leave pixmap unchanged.
Cary Clarka560c472017-11-27 10:44:06 -05001279
Cary Clarkf5404bb2018-01-05 12:10:09 -05001280#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
Cary Clarka560c472017-11-27 10:44:06 -05001281
Cary Clarkf5404bb2018-01-05 12:10:09 -05001282#Return true if Image has direct access to pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001283
1284#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001285 SkBitmap bitmap;
1286 bitmap.allocPixels(SkImageInfo::MakeN32Premul(12, 11));
1287 SkCanvas offscreen(bitmap);
1288 offscreen.clear(SK_ColorWHITE);
1289 SkPaint paint;
1290 offscreen.drawString("%", 1, 10, paint);
1291 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
1292 SkPixmap pixmap;
1293 if (image->peekPixels(&pixmap)) {
1294 const SkPMColor* pixels = pixmap.addr32();
1295 SkPMColor pmWhite = pixels[0];
1296 for (int y = 0; y < image->height(); ++y) {
1297 for (int x = 0; x < image->width(); ++x) {
1298 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
1299 }
1300 SkDebugf("\n");
1301 }
1302 }
1303#StdOut
1304------------
1305--xx----x---
1306-x--x--x----
1307-x--x--x----
1308-x--x-x-----
1309--xx-xx-xx--
1310-----x-x--x-
1311----x--x--x-
1312----x--x--x-
1313---x----xx--
1314------------
1315##
Cary Clarka560c472017-11-27 10:44:06 -05001316##
1317
Cary Clarkf5404bb2018-01-05 12:10:09 -05001318#SeeAlso readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001319
1320#Method ##
1321
1322# ------------------------------------------------------------------------------
1323
1324#Method GrTexture* getTexture() const
Cary Clark2f466242017-12-11 16:03:17 -05001325#Deprecated
Cary Clarka560c472017-11-27 10:44:06 -05001326#Method ##
1327
1328# ------------------------------------------------------------------------------
1329
1330#Method bool isTextureBacked() const
Cary Clark78de7512018-02-07 07:27:09 -05001331#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001332#Line # returns if Image was created from GPU_Texture ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001333Returns true the contents of Image was created on or uploaded to GPU memory,
1334and is available as a GPU_Texture.
Cary Clarka560c472017-11-27 10:44:06 -05001335
Cary Clarkf5404bb2018-01-05 12:10:09 -05001336#Return true if Image is a GPU_Texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001337
1338#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001339#Image 5
1340#Platform gpu
1341auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1342 if (nullptr == image) {
1343 return;
1344 }
1345 SkPaint paint;
1346 paint.setAntiAlias(true);
1347 paint.setTextAlign(SkPaint::kCenter_Align);
1348 canvas->drawImage(image, 0, 0);
1349 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1350 canvas->drawString(image->isTextureBacked() ? "is GPU texture" : "not GPU texture",
1351 image->width() / 2, image->height() * 3 / 4, paint);
1352};
1353sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1354sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1355 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1356drawImage(image, "image");
1357canvas->translate(image->width(), 0);
1358drawImage(bitmapImage, "source");
1359canvas->translate(-image->width(), image->height());
1360drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001361##
1362
Cary Clarkf5404bb2018-01-05 12:10:09 -05001363#SeeAlso MakeFromTexture isValid
Cary Clarka560c472017-11-27 10:44:06 -05001364
1365#Method ##
1366
1367# ------------------------------------------------------------------------------
1368
1369#Method bool isValid(GrContext* context) const
Cary Clark4855f782018-02-06 09:41:53 -05001370#In Property
1371#Line # returns if Image can draw to Raster_Surface or GPU_Context ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001372Returns true if Image can be drawn on either Raster_Surface or GPU_Surface.
1373If context is nullptr, tests if Image draws on Raster_Surface;
1374otherwise, tests if Image draws on GPU_Surface associated with context.
Cary Clarka560c472017-11-27 10:44:06 -05001375
Cary Clarkf5404bb2018-01-05 12:10:09 -05001376Image backed by GPU_Texture may become invalid if associated GrContext is
1377invalid. Lazy_Image may be invalid and may not draw to Raster_Surface or
1378GPU_Surface or both.
Cary Clarka560c472017-11-27 10:44:06 -05001379
Cary Clark61ca7c52018-01-02 11:34:14 -05001380#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05001381
Cary Clarkf5404bb2018-01-05 12:10:09 -05001382#Return true if Image can be drawn ##
Cary Clarka560c472017-11-27 10:44:06 -05001383
1384#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001385#Image 5
1386#Platform gpu
1387auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1388 if (nullptr == image) {
1389 return;
1390 }
1391 SkPaint paint;
1392 paint.setAntiAlias(true);
1393 paint.setTextAlign(SkPaint::kCenter_Align);
1394 canvas->drawImage(image, 0, 0);
1395 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1396 if (canvas->getGrContext()) {
1397 canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" :
1398 "not valid on GPU", image->width() / 2, image->height() * 5 / 8, paint);
1399 }
1400 canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" :
1401 "not valid on CPU", image->width() / 2, image->height() * 7 / 8, paint);
1402};
1403sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1404sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1405 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1406drawImage(image, "image");
1407canvas->translate(image->width(), 0);
1408drawImage(bitmapImage, "source");
1409canvas->translate(-image->width(), image->height());
1410drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001411##
1412
Cary Clarkf5404bb2018-01-05 12:10:09 -05001413#SeeAlso isTextureBacked isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001414
1415#Method ##
1416
1417# ------------------------------------------------------------------------------
1418
1419#Method GrBackendObject getTextureHandle(bool flushPendingGrContextIO,
1420 GrSurfaceOrigin* origin = nullptr) const
Cary Clark78de7512018-02-07 07:27:09 -05001421#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001422#Line # returns GPU reference to Image as texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001423
Cary Clark2f466242017-12-11 16:03:17 -05001424Retrieves the back-end API handle of texture. If flushPendingGrContextIO is true,
1425complete deferred I/O operations.
Cary Clarka560c472017-11-27 10:44:06 -05001426
Cary Clark2f466242017-12-11 16:03:17 -05001427If origin in not nullptr, copies location of content drawn into Image.
Cary Clarka560c472017-11-27 10:44:06 -05001428
Cary Clark2f466242017-12-11 16:03:17 -05001429#Param flushPendingGrContextIO flag to flush outstanding requests ##
1430#Param origin storage for one of: kTopLeft_GrSurfaceOrigin,
1431 kBottomLeft_GrSurfaceOrigin; or nullptr
1432##
1433
Cary Clarkac47b882018-01-11 10:35:44 -05001434#Return back-end API texture handle, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001435
1436#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001437#Image 4
Cary Clark2f466242017-12-11 16:03:17 -05001438#Platform gpu
1439GrContext* context = canvas->getGrContext();
1440if (!context) {
1441 return;
1442}
1443SkPaint paint;
1444paint.setAntiAlias(true);
1445SkString str;
Cary Clarkac47b882018-01-11 10:35:44 -05001446int y = -10;
Cary Clark2f466242017-12-11 16:03:17 -05001447for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin } ) {
1448 sk_sp<SkImage> srcImage(SkImage::MakeFromTexture(context,
1449 backEndTexture, origin, kPremul_SkAlphaType, nullptr));
1450 GrSurfaceOrigin readBackOrigin;
1451 GrBackendObject readBackHandle = srcImage->getTextureHandle(false, &readBackOrigin);
Cary Clarkac47b882018-01-11 10:35:44 -05001452 str.printf("readBackHandle: 0x%x", readBackHandle);
1453 canvas->drawString(str, 5, y += 30, paint);
1454 canvas->drawImage(srcImage, 80, y += 10);
Cary Clark2f466242017-12-11 16:03:17 -05001455 str.printf("origin: k%s_GrSurfaceOrigin", readBackOrigin ? "BottomLeft" : "TopLeft");
Cary Clarkac47b882018-01-11 10:35:44 -05001456 canvas->drawString(str, 5, y += srcImage->height() + 10, paint);
Cary Clark2f466242017-12-11 16:03:17 -05001457}
Cary Clarka560c472017-11-27 10:44:06 -05001458##
1459
Cary Clarkac47b882018-01-11 10:35:44 -05001460#Example
1461#Image 5
1462#Platform gpu
1463 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1464 if (nullptr == image) {
1465 return;
1466 }
1467 SkPaint paint;
1468 paint.setAntiAlias(true);
1469 paint.setTextAlign(SkPaint::kCenter_Align);
1470 canvas->drawImage(image, 0, image->height() / 4);
1471 canvas->drawString(label, image->width() / 2, image->height() / 8, paint);
1472 GrSurfaceOrigin readBackOrigin;
1473 GrBackendObject readBackHandle = image->getTextureHandle(false, &readBackOrigin);
1474 canvas->drawString(readBackHandle ? "has readBackHandle" : "no readBackHandle",
1475 image->width() / 2, image->height() * 11 / 8, paint);
1476 };
1477 drawImage(image, "image");
1478 canvas->translate(image->width(), 0);
1479 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1480 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1481 drawImage(textureImage, "backEndTexture");
1482##
1483
1484#SeeAlso MakeFromTexture isTextureBacked
Cary Clarka560c472017-11-27 10:44:06 -05001485
1486#Method ##
1487
1488# ------------------------------------------------------------------------------
1489
1490#Enum CachingHint
1491
1492#Code
1493 enum CachingHint {
1494 kAllow_CachingHint,
1495 kDisallow_CachingHint,
1496 };
1497##
1498
Cary Clarkac47b882018-01-11 10:35:44 -05001499CachingHint selects whether Skia may internally cache Bitmaps generated by
1500decoding Image, or by copying Image from GPU to CPU. The default behavior
1501allows caching Bitmaps.
1502
1503Choose kDisallow_CachingHint if Image pixels are to be used only once, or
1504if Image pixels reside in a cache outside of Skia, or to reduce memory pressure.
1505
1506Choosing kAllow_CachingHint does not ensure that pixels will be cached.
1507Image pixels may not be cached if memory requirements are too large or
1508pixels are not accessible.
Cary Clarka560c472017-11-27 10:44:06 -05001509
1510#Const kAllow_CachingHint 0
Cary Clarkac47b882018-01-11 10:35:44 -05001511Allows Skia to internally cache decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001512##
1513#Const kDisallow_CachingHint 1
Cary Clarkac47b882018-01-11 10:35:44 -05001514Disallows Skia from internally caching decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001515##
1516
Cary Clarkac47b882018-01-11 10:35:44 -05001517#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001518##
1519
Cary Clarkac47b882018-01-11 10:35:44 -05001520#SeeAlso readPixels scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001521
1522#Enum ##
1523
1524# ------------------------------------------------------------------------------
1525
1526#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1527 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001528#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001529#Line # copies and converts pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001530
Cary Clarkac47b882018-01-11 10:35:44 -05001531Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY),
1532and does not exceed Image (width(), height()).
1533
1534dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
1535destination. dstRowBytes specifics the gap from one destination row to the next.
1536Returns true if pixels are copied. Returns false if:
1537#List
1538# dstInfo.addr() equals nullptr ##
1539# dstRowBytes is less than dstInfo.minRowBytes ##
1540# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001541##
1542
Cary Clarkac47b882018-01-11 10:35:44 -05001543Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1544kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1545If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace must match.
1546If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType must
1547match. If Image Color_Space is nullptr, dstInfo.colorSpace must match. Returns
1548false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001549
Cary Clarkac47b882018-01-11 10:35:44 -05001550srcX and srcY may be negative to copy only top or left of source. Returns
1551false if width() or height() is zero or negative.
1552Returns false if
1553#Formula
1554abs(srcX) >= Image width()
1555##
1556, or if
1557#Formula
1558abs(srcY) >= Image height()
1559##
1560.
Cary Clarka560c472017-11-27 10:44:06 -05001561
Cary Clarkac47b882018-01-11 10:35:44 -05001562If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1563If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1564
1565#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1566#Param dstPixels destination pixel storage ##
1567#Param dstRowBytes destination row length ##
1568#Param srcX column index whose absolute value is less than width() ##
1569#Param srcY row index whose absolute value is less than height() ##
1570#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1571
1572#Return true if pixels are copied to dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001573
1574#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001575#Image 3
1576 canvas->scale(.5f, .5f);
1577 const int width = 32;
1578 const int height = 32;
1579 std::vector<int32_t> dstPixels;
1580 dstPixels.resize(height * width * 4);
1581 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1582 for (int y = 0; y < 512; y += height ) {
1583 for (int x = 0; x < 512; x += width ) {
1584 if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) {
1585 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
1586 SkBitmap bitmap;
1587 bitmap.installPixels(dstPixmap);
1588 canvas->drawBitmap(bitmap, 0, 0);
1589 }
1590 canvas->translate(48, 0);
1591 }
1592 canvas->translate(-16 * 48, 48);
1593 }
Cary Clarka560c472017-11-27 10:44:06 -05001594##
1595
Cary Clarkac47b882018-01-11 10:35:44 -05001596#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001597
1598#Method ##
1599
1600# ------------------------------------------------------------------------------
1601
1602#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY,
1603 CachingHint cachingHint = kAllow_CachingHint) const
1604
Cary Clarkac47b882018-01-11 10:35:44 -05001605Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and
1606does not exceed Image (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001607
Cary Clarkac47b882018-01-11 10:35:44 -05001608dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
1609and row bytes of destination. dst.rowBytes specifics the gap from one destination
1610row to the next. Returns true if pixels are copied. Returns false if:
1611#List
1612# dst pixel storage equals nullptr ##
1613# dst.rowBytes is less than SkImageInfo::minRowBytes ##
1614# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001615##
1616
Cary Clarkac47b882018-01-11 10:35:44 -05001617Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1618kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1619If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1620If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1621match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1622false if pixel conversion is not possible.
1623
1624srcX and srcY may be negative to copy only top or left of source. Returns
1625false if width() or height() is zero or negative.
1626Returns false if
1627#Formula
1628abs(srcX) >= Image width()
1629##
1630, or if
1631#Formula
1632abs(srcY) >= Image height()
1633##
1634.
1635
1636If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1637If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1638
1639#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1640#Param srcX column index whose absolute value is less than width() ##
1641#Param srcY row index whose absolute value is less than height() ##
1642#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1643
1644#Return true if pixels are copied to dst ##
1645
1646#Example
1647#Image 3
1648 std::vector<int32_t> srcPixels;
1649 int rowBytes = image->width() * 4;
1650 int quarterWidth = image->width() / 4;
1651 int quarterHeight = image->height() / 4;
1652 srcPixels.resize(image->height() * rowBytes);
1653 for (int y = 0; y < 4; ++y) {
1654 for (int x = 0; x < 4; ++x) {
1655 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1656 &srcPixels.front() + x * image->height() * quarterWidth +
1657 y * quarterWidth, rowBytes);
1658 image->readPixels(pixmap, x * quarterWidth, y * quarterHeight);
1659 }
1660 }
1661 canvas->scale(.5f, .5f);
1662 SkBitmap bitmap;
1663 bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()),
1664 &srcPixels.front(), rowBytes);
1665 canvas->drawBitmap(bitmap, 0, 0);
1666##
1667
1668#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001669
1670#Method ##
1671
1672# ------------------------------------------------------------------------------
1673
1674#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality,
1675 CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001676#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001677#Line # scales and converts one Image to another ##
Cary Clarka560c472017-11-27 10:44:06 -05001678
Cary Clarkac47b882018-01-11 10:35:44 -05001679Copies Image to dst, scaling pixels to fit dst.width() and dst.height(), and
1680converting pixels to match dst.colorType and dst.alphaType. Returns true if
1681pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1682less than dst SkImageInfo::minRowBytes.
Cary Clarka560c472017-11-27 10:44:06 -05001683
Cary Clarkac47b882018-01-11 10:35:44 -05001684Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1685kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1686If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1687If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1688match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1689false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001690
Cary Clarkac47b882018-01-11 10:35:44 -05001691Scales the image, with filterQuality, to match dst.width() and dst.height().
1692filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1693Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1694Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1695Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1696kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1697
1698If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1699If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1700
1701#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1702#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1703 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1704##
1705#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1706
1707#Return true if pixels are scaled to fit dst ##
Cary Clarka560c472017-11-27 10:44:06 -05001708
1709#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001710#Image 3
1711#Height 128
1712 std::vector<int32_t> srcPixels;
1713 int quarterWidth = image->width() / 16;
1714 int rowBytes = quarterWidth * 4;
1715 int quarterHeight = image->height() / 16;
1716 srcPixels.resize(quarterHeight * rowBytes);
1717 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1718 &srcPixels.front(), rowBytes);
1719 canvas->scale(4, 4);
1720 SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality,
1721 kMedium_SkFilterQuality, kHigh_SkFilterQuality };
1722 for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) {
1723 image->scalePixels(pixmap, qualities[index]);
1724 sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
1725 canvas->drawImage(filtered, 16 * index, 0);
1726 }
Cary Clarka560c472017-11-27 10:44:06 -05001727##
1728
Cary Clarkac47b882018-01-11 10:35:44 -05001729#SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001730
1731#Method ##
1732
1733# ------------------------------------------------------------------------------
1734
1735#Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const
Cary Clark78de7512018-02-07 07:27:09 -05001736#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001737#Line # returns encoded Image as SkData ##
Cary Clarkac47b882018-01-11 10:35:44 -05001738Encodes Image pixels, returning result as SkData.
Cary Clark2f466242017-12-11 16:03:17 -05001739
Cary Clarkac47b882018-01-11 10:35:44 -05001740Returns nullptr if encoding fails, or if encodedImageFormat is not supported.
Cary Clarka560c472017-11-27 10:44:06 -05001741
Cary Clarkac47b882018-01-11 10:35:44 -05001742Image encoding in a format requires both building with one or more of:
1743SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support
1744for the encoded format.
1745
1746If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can
1747additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP,
1748SkEncodedImageFormat::kGIF.
1749
1750quality is a platform and format specific metric trading off size and encoding
1751error. When used, quality equaling 100 encodes with the least error. quality may
1752be ignored by the encoder.
1753
1754#Param encodedImageFormat one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
1755 SkEncodedImageFormat::kWEBP
1756 ##
1757#Param quality encoder specific metric with 100 equaling best ##
Cary Clarka560c472017-11-27 10:44:06 -05001758
Cary Clark2f466242017-12-11 16:03:17 -05001759#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001760
1761#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001762#Image 3
1763 canvas->scale(4, 4);
1764 SkIRect subset = {0, 0, 16, 64};
1765 int x = 0;
1766 for (int quality : { 0, 10, 50, 100 } ) {
1767 sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
1768 sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
1769 canvas->drawImage(filtered, x, 0);
1770 x += 16;
1771 }
Cary Clarka560c472017-11-27 10:44:06 -05001772##
1773
Cary Clarkac47b882018-01-11 10:35:44 -05001774#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001775
1776#Method ##
1777
1778# ------------------------------------------------------------------------------
1779
Cary Clark61ca7c52018-01-02 11:34:14 -05001780#Method sk_sp<SkData> encodeToData() const
Cary Clarka560c472017-11-27 10:44:06 -05001781
Cary Clarkac47b882018-01-11 10:35:44 -05001782Encodes Image pixels, returning result as SkData. Returns existing encoded data
1783if present; otherwise, Image is encoded with SkEncodedImageFormat::kPNG. Skia
1784must be built with SK_HAS_PNG_LIBRARY to encode Image.
Cary Clarka560c472017-11-27 10:44:06 -05001785
Cary Clarkac47b882018-01-11 10:35:44 -05001786Returns nullptr if existing encoded data is missing or invalid, and
Cary Clarka560c472017-11-27 10:44:06 -05001787encoding fails.
1788
Cary Clarkac47b882018-01-11 10:35:44 -05001789#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001790
1791#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001792#Image 3
1793 canvas->scale(4, 4);
1794 SkIRect subset = {136, 32, 200, 96};
1795 sk_sp<SkData> data(image->encodeToData());
1796 sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
1797 canvas->drawImage(eye, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001798##
1799
Cary Clarkac47b882018-01-11 10:35:44 -05001800#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001801
1802#Method ##
1803
1804# ------------------------------------------------------------------------------
1805
1806#Method sk_sp<SkData> refEncodedData() const
Cary Clark78de7512018-02-07 07:27:09 -05001807#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001808#Line # returns Image encoded in SkData if present ##
Cary Clarkac47b882018-01-11 10:35:44 -05001809Returns encoded Image pixels as SkData, if Image was created from supported
1810encoded stream format. Platform support for formats vary and may require building
1811with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY.
Cary Clarka560c472017-11-27 10:44:06 -05001812
Cary Clarkac47b882018-01-11 10:35:44 -05001813Returns nullptr if Image contents are not encoded.
Cary Clarka560c472017-11-27 10:44:06 -05001814
Cary Clarkac47b882018-01-11 10:35:44 -05001815#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001816
1817#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001818#Image 3
1819#Platform gpu
1820 struct {
1821 const char* name;
1822 sk_sp<SkImage> image;
1823 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1824 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1825 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr) } };
1826 SkString string;
1827 SkPaint paint;
1828 for (const auto& test : tests ) {
1829 if (!test.image) {
1830 string.printf("no %s", test.name);
1831 } else {
1832 string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name);
1833 }
1834 canvas->drawString(string, 10, 20, paint);
1835 canvas->translate(0, 20);
1836 }
Cary Clarka560c472017-11-27 10:44:06 -05001837##
1838
Cary Clarkac47b882018-01-11 10:35:44 -05001839#SeeAlso encodeToData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001840
1841#Method ##
1842
1843# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001844#Subtopic Utility
1845#Populate
1846#Line # rarely called management functions ##
1847##
Cary Clarka560c472017-11-27 10:44:06 -05001848
1849#Method const char* toString(SkString* string) const
Cary Clark4855f782018-02-06 09:41:53 -05001850#In Utility
1851#Line # converts Image to machine readable form ##
Cary Clarkac47b882018-01-11 10:35:44 -05001852Appends Image description to string, including unique ID, width, height, and
1853whether the image is opaque.
Cary Clarka560c472017-11-27 10:44:06 -05001854
Cary Clarkac47b882018-01-11 10:35:44 -05001855#Param string storage for description; existing content is preserved ##
1856
1857#Return string appended with Image description ##
Cary Clarka560c472017-11-27 10:44:06 -05001858
1859#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001860#Image 4
1861 struct {
1862 const char* name;
1863 sk_sp<SkImage> image;
1864 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1865 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1866 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr) } };
1867 SkString string;
1868 SkPaint paint;
1869 for (const auto& test : tests ) {
1870 string.printf("%s: ", test.name);
1871 test.image ? (void) test.image->toString(&string) : string.append("no image");
1872 canvas->drawString(string, 10, 20, paint);
1873 canvas->translate(0, 20);
1874 }
Cary Clarka560c472017-11-27 10:44:06 -05001875##
1876
Cary Clarkac47b882018-01-11 10:35:44 -05001877#SeeAlso SkPaint::toString
Cary Clarka560c472017-11-27 10:44:06 -05001878
1879#Method ##
1880
1881# ------------------------------------------------------------------------------
1882
1883#Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const
Cary Clark4855f782018-02-06 09:41:53 -05001884#In Constructor
1885#Line # creates Image containing part of original ##
Cary Clarkac47b882018-01-11 10:35:44 -05001886Returns subset of Image. subset must be fully contained by Image dimensions().
1887The implementation may share pixels, or may copy them.
Cary Clarka560c472017-11-27 10:44:06 -05001888
Cary Clarkac47b882018-01-11 10:35:44 -05001889Returns nullptr if subset is empty, or subset is not contained by bounds, or
1890pixels in Image could not be read or copied.
Cary Clarka560c472017-11-27 10:44:06 -05001891
Cary Clarkac47b882018-01-11 10:35:44 -05001892#Param subset bounds of returned Image ##
1893
1894#Return partial or full Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001895
1896#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001897#Image 3
1898 canvas->scale(.5f, .5f);
1899 const int width = 32;
1900 const int height = 32;
1901 for (int y = 0; y < 512; y += height ) {
1902 for (int x = 0; x < 512; x += width ) {
1903 sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height}));
1904 canvas->drawImage(subset, x * 3 / 2, y * 3 / 2);
1905 }
1906 }
Cary Clarka560c472017-11-27 10:44:06 -05001907##
1908
Cary Clarkac47b882018-01-11 10:35:44 -05001909#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001910
1911#Method ##
1912
1913# ------------------------------------------------------------------------------
1914
1915#Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const
Cary Clark4855f782018-02-06 09:41:53 -05001916#In Constructor
1917#Line # creates Image matching Color_Space if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001918Returns Image backed by GPU_Texture associated with context. Returned Image is
1919compatible with Surface created with dstColorSpace. Returns original
1920Image if context and dstColorSpace match.
1921
1922Returns nullptr if context is nullptr, or if Image was created with another
1923GrContext.
Cary Clarka560c472017-11-27 10:44:06 -05001924
Cary Clark61ca7c52018-01-02 11:34:14 -05001925#Param context GPU_Context ##
Cary Clarkac47b882018-01-11 10:35:44 -05001926#Param dstColorSpace range of colors of matching Surface on GPU ##
Cary Clarka560c472017-11-27 10:44:06 -05001927
Cary Clarkac47b882018-01-11 10:35:44 -05001928#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001929
1930#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001931#Platform gpu
1932#Image 5
1933 auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void {
1934 if (nullptr == image || nullptr == context) {
1935 return;
1936 }
1937 SkPaint paint;
1938 paint.setAntiAlias(true);
1939 paint.setTextAlign(SkPaint::kCenter_Align);
1940 sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr));
1941 canvas->drawImage(texture, 0, 0);
1942 canvas->drawString(label, texture->width() / 2, texture->height() / 4, paint);
1943 };
1944 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1945 GrContext* context = canvas->getGrContext();
1946 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture,
1947 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1948 drawImage(image, context, "image");
1949 canvas->translate(image->width(), 0);
1950 drawImage(bitmapImage, context, "source");
1951 canvas->translate(-image->width(), image->height());
1952 drawImage(textureImage, context, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001953##
1954
Cary Clarkac47b882018-01-11 10:35:44 -05001955#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05001956
1957#Method ##
1958
1959# ------------------------------------------------------------------------------
1960
1961#Method sk_sp<SkImage> makeNonTextureImage() const
Cary Clark4855f782018-02-06 09:41:53 -05001962#In Constructor
1963#Line # creates Image without dependency on GPU_Texture ##
Cary Clarkac47b882018-01-11 10:35:44 -05001964Returns Raster_Image or Lazy_Image. Copies Image backed by GPU_Texture into
Cary Clark4855f782018-02-06 09:41:53 -05001965CPU memory if needed. Returns original Image if decoded in Raster_Bitmap,
Cary Clarkac47b882018-01-11 10:35:44 -05001966or if encoded in a stream.
Cary Clark61ca7c52018-01-02 11:34:14 -05001967
Cary Clarkac47b882018-01-11 10:35:44 -05001968Returns nullptr if backed by GPU_Texture and copy fails.
1969
1970#Return Raster_Image, Lazy_Image, or nullptr ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001971
1972#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001973#Image 5
1974#Platform gpu
1975 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1976 if (nullptr == image) {
1977 return;
1978 }
1979 SkPaint paint;
1980 paint.setAntiAlias(true);
1981 paint.setTextAlign(SkPaint::kCenter_Align);
1982 sk_sp<SkImage> nonTexture(image->makeNonTextureImage());
1983 canvas->drawImage(nonTexture, 0, 0);
1984 canvas->drawString(label, nonTexture->width() / 2, nonTexture->height() / 4, paint);
1985 };
1986 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1987 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1988 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1989 drawImage(image, "image");
1990 canvas->translate(image->width(), 0);
1991 drawImage(bitmapImage, "source");
1992 canvas->translate(-image->width(), image->height());
1993 drawImage(textureImage, "backEndTexture");
Cary Clark61ca7c52018-01-02 11:34:14 -05001994##
1995
Cary Clark56356312018-02-08 14:45:18 -05001996#SeeAlso makeTextureImage makeRasterImage MakeBackendTextureFromSkImage
Cary Clark61ca7c52018-01-02 11:34:14 -05001997
1998#Method ##
1999
2000# ------------------------------------------------------------------------------
2001
2002#Method sk_sp<SkImage> makeRasterImage() const
Cary Clark4855f782018-02-06 09:41:53 -05002003#In Constructor
2004#Line # creates Image compatible with Raster_Surface if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05002005Returns Raster_Image. Copies Image backed by GPU_Texture into CPU memory,
Cary Clark4855f782018-02-06 09:41:53 -05002006or decodes Image from Lazy_Image. Returns original Image if decoded in
Cary Clarkac47b882018-01-11 10:35:44 -05002007Raster_Bitmap.
Cary Clarka560c472017-11-27 10:44:06 -05002008
Cary Clarkac47b882018-01-11 10:35:44 -05002009Returns nullptr if copy, decode, or pixel read fails.
Cary Clarka560c472017-11-27 10:44:06 -05002010
Cary Clarkac47b882018-01-11 10:35:44 -05002011#Return Raster_Image, or nullptr ##
2012
Cary Clark4855f782018-02-06 09:41:53 -05002013#Bug 7479
Cary Clarka560c472017-11-27 10:44:06 -05002014#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002015#Image 5
2016#Platform gpu
2017 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2018 if (nullptr == image) {
2019 return;
2020 }
2021 SkPaint paint;
2022 paint.setAntiAlias(true);
2023 paint.setTextAlign(SkPaint::kCenter_Align);
2024 sk_sp<SkImage> raster(image->makeRasterImage());
2025 canvas->drawImage(raster, 0, 0);
2026 canvas->drawString(label, raster->width() / 2, raster->height() / 4, paint);
2027 };
2028 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2029 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2030 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2031 drawImage(image, "image");
2032 canvas->translate(image->width(), 0);
2033 drawImage(bitmapImage, "source");
2034 canvas->translate(-image->width(), image->height());
2035 drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05002036##
2037
Cary Clarkac47b882018-01-11 10:35:44 -05002038#SeeAlso isTextureBacked isLazyGenerated MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -05002039
2040#Method ##
2041
2042# ------------------------------------------------------------------------------
2043
2044#Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
2045 const SkIRect& clipBounds, SkIRect* outSubset,
2046 SkIPoint* offset) const
Cary Clark4855f782018-02-06 09:41:53 -05002047#In Constructor
2048#Line # creates filtered, clipped Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002049
Cary Clarkac47b882018-01-11 10:35:44 -05002050Creates filtered Image. filter processes original Image, potentially changing
2051color, position, and size. subset is the bounds of original Image processed
2052by filter. clipBounds is the expected bounds of the filtered Image. outSubset
2053is required storage for the actual bounds of the filtered Image. offset is
2054required storage for translation of returned Image.
Cary Clarka560c472017-11-27 10:44:06 -05002055
Cary Clarkac47b882018-01-11 10:35:44 -05002056Returns nullptr if Image could not be created. If nullptr is returned, outSubset
2057and offset are undefined.
2058
Cary Clark56356312018-02-08 14:45:18 -05002059Useful for animation of SkImageFilter that varies size from frame to frame.
2060Returned Image is created larger than required by filter so that GPU_Texture
2061can be reused with different sized effects. outSubset describes the valid bounds
2062of GPU_Texture returned. offset translates the returned Image to keep subsequent
2063animation frames aligned with respect to each other.
Cary Clarkac47b882018-01-11 10:35:44 -05002064
2065#Param filter how Image is sampled when transformed ##
Cary Clark56356312018-02-08 14:45:18 -05002066#Param subset bounds of Image processed by filter ##
2067#Param clipBounds expected bounds of filtered Image ##
2068#Param outSubset storage for returned Image bounds ##
2069#Param offset storage for returned Image translation ##
Cary Clarka560c472017-11-27 10:44:06 -05002070
Cary Clarkac47b882018-01-11 10:35:44 -05002071#Return filtered Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05002072
2073#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002074#Description
2075In each frame of the animation, filtered Image is drawn in a different location.
2076By translating canvas by returned offset, Image appears stationary.
2077##
2078#Image 5
2079#Platform gpu
2080#Duration 5
2081 sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make(
2082 -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE,
2083 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
2084 nullptr);
2085 sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr);
2086 SkIRect subset = image->bounds();
2087 SkIRect clipBounds = image->bounds();
2088 clipBounds.outset(60, 60);
2089 SkIRect outSubset;
2090 SkIPoint offset;
2091 sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds,
2092 &outSubset, &offset));
2093 SkPaint paint;
2094 paint.setAntiAlias(true);
2095 paint.setStyle(SkPaint::kStroke_Style);
2096 canvas->drawLine(0, 0, offset.fX, offset.fY, paint);
2097 canvas->translate(offset.fX, offset.fY);
2098 canvas->drawImage(filtered, 0, 0);
2099 canvas->drawRect(SkRect::MakeFromIRect(outSubset), paint);
Cary Clarka560c472017-11-27 10:44:06 -05002100##
2101
Cary Clark56356312018-02-08 14:45:18 -05002102#SeeAlso makeShader SkPaint::setImageFilter
Cary Clarka560c472017-11-27 10:44:06 -05002103
2104#Method ##
2105
2106# ------------------------------------------------------------------------------
2107
Cary Clarka560c472017-11-27 10:44:06 -05002108#Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc
2109
2110##
2111
2112# ------------------------------------------------------------------------------
2113
2114#Method static bool MakeBackendTextureFromSkImage(GrContext* context,
2115 sk_sp<SkImage> image,
2116 GrBackendTexture* backendTexture,
2117 BackendTextureReleaseProc* backendTextureReleaseProc)
Cary Clark4855f782018-02-06 09:41:53 -05002118#In Constructor
2119#Line # creates GPU_Texture from Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002120
Cary Clark56356312018-02-08 14:45:18 -05002121Creates a GrBackendTexture from the provided SkImage. Returns true and
2122stores result in backendTexture and backendTextureReleaseProc if
2123texture is created; otherwise, returns false and leaves
2124backendTexture and backendTextureReleaseProc unmodified.
Cary Clarka560c472017-11-27 10:44:06 -05002125
Cary Clark56356312018-02-08 14:45:18 -05002126Call backendTextureReleaseProc after deleting backendTexture.
2127backendTextureReleaseProc cleans up auxiliary data related to returned
2128backendTexture. The caller must delete returned backendTexture after use.
Cary Clarka560c472017-11-27 10:44:06 -05002129
Cary Clark56356312018-02-08 14:45:18 -05002130If Image is both texture backed and singly referenced, image is returned in
2131backendTexture without conversion or making a copy. Image is singly referenced
2132if its was transferred solely using std::move().
2133
2134If Image is not texture backed, returns texture with Image contents.
Cary Clarka560c472017-11-27 10:44:06 -05002135
Cary Clark61ca7c52018-01-02 11:34:14 -05002136#Param context GPU_Context ##
Cary Clark56356312018-02-08 14:45:18 -05002137#Param image Image used for texture ##
2138#Param backendTexture storage for backend texture ##
2139#Param backendTextureReleaseProc storage for clean up function ##
Cary Clarka560c472017-11-27 10:44:06 -05002140
Cary Clark56356312018-02-08 14:45:18 -05002141#Return true if backend texture was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002142
2143#Example
Cary Clark56356312018-02-08 14:45:18 -05002144#Platform gpu
2145#Height 64
2146#Function
Brian Salomon67f85842018-02-09 08:50:22 -05002147static sk_sp<SkImage> create_gpu_image(GrContext* grContext) {
2148 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
2149 auto surface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info));
2150 SkCanvas* canvas = surface->getCanvas();
2151 canvas->clear(SK_ColorWHITE);
2152 SkPaint paint;
2153 paint.setColor(SK_ColorBLACK);
2154 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
2155 return surface->makeImageSnapshot();
2156}
2157##
2158
2159void draw(SkCanvas* canvas) {
2160 GrContext* grContext = canvas->getGrContext();
2161 if (!grContext) {
2162 return;
2163 }
2164 sk_sp<SkImage> backEndImage = create_gpu_image(grContext);
2165 canvas->drawImage(backEndImage, 0, 0);
2166 GrBackendTexture texture;
2167 SkImage::BackendTextureReleaseProc proc;
2168 if (!SkImage::MakeBackendTextureFromSkImage(grContext, std::move(backEndImage),
2169 &texture, &proc)) {
2170 return;
2171 }
2172 sk_sp<SkImage> i2 = SkImage::MakeFromTexture(grContext, texture, kTopLeft_GrSurfaceOrigin,
2173 kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
2174 canvas->drawImage(i2, 30, 30);
Cary Clark56356312018-02-08 14:45:18 -05002175}
Cary Clarka560c472017-11-27 10:44:06 -05002176##
2177
Cary Clark56356312018-02-08 14:45:18 -05002178#SeeAlso MakeFromTexture makeTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002179
2180#Method ##
2181
2182# ------------------------------------------------------------------------------
2183
2184#Enum LegacyBitmapMode
Cary Clark56356312018-02-08 14:45:18 -05002185#Deprecated soon
Cary Clarka560c472017-11-27 10:44:06 -05002186#Code
2187 enum LegacyBitmapMode {
2188 kRO_LegacyBitmapMode,
Cary Clarka560c472017-11-27 10:44:06 -05002189 };
2190##
2191
Cary Clarka560c472017-11-27 10:44:06 -05002192#Const kRO_LegacyBitmapMode 0
Cary Clark56356312018-02-08 14:45:18 -05002193Returned bitmap is read-only and immutable.
Cary Clarka560c472017-11-27 10:44:06 -05002194##
Cary Clarka560c472017-11-27 10:44:06 -05002195
2196#Enum ##
2197
2198# ------------------------------------------------------------------------------
2199
Cary Clark56356312018-02-08 14:45:18 -05002200#Method bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode = kRO_LegacyBitmapMode) const
Cary Clark4855f782018-02-06 09:41:53 -05002201#In Constructor
2202#Line # returns as Raster_Bitmap ##
Cary Clarkac47b882018-01-11 10:35:44 -05002203Creates raster Bitmap with same pixels as Image. If legacyBitmapMode is
2204kRO_LegacyBitmapMode, returned bitmap is read-only and immutable.
2205Returns true if Bitmap is stored in bitmap. Returns false and resets bitmap if
2206Bitmap write did not succeed.
Cary Clarka560c472017-11-27 10:44:06 -05002207
Cary Clark3cd22cc2017-12-01 11:49:58 -05002208#Param bitmap storage for legacy Bitmap ##
Cary Clark56356312018-02-08 14:45:18 -05002209#Param legacyBitmapMode to be deprecated ##
Cary Clarka560c472017-11-27 10:44:06 -05002210
Cary Clark3cd22cc2017-12-01 11:49:58 -05002211#Return true if Bitmap was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002212
2213#Example
Cary Clark56356312018-02-08 14:45:18 -05002214#Image 4
2215#Platform gpu
Brian Salomon67f85842018-02-09 08:50:22 -05002216 SkBitmap bitImage;
2217 if (image->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2218 canvas->drawBitmap(bitImage, 0, 0);
2219 }
2220 GrContext* grContext = canvas->getGrContext();
2221 if (!grContext) {
2222 return;
2223 }
2224 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(grContext, backEndTexture,
2225 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2226 canvas->drawImage(textureImage, 45, 45);
2227 if (textureImage->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2228 canvas->drawBitmap(bitImage, 90, 90);
2229 }
Cary Clarka560c472017-11-27 10:44:06 -05002230##
2231
Cary Clark56356312018-02-08 14:45:18 -05002232#SeeAlso MakeRasterData makeRasterImage makeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002233
2234#Method ##
2235
2236# ------------------------------------------------------------------------------
2237
2238#Method bool isLazyGenerated() const
Cary Clark4855f782018-02-06 09:41:53 -05002239#In Property
2240#Line # returns if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002241Returns true if Image is backed by an image-generator or other service that creates
2242and caches its pixels or texture on-demand.
2243
Cary Clark2f466242017-12-11 16:03:17 -05002244#Return true if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002245
2246#Example
Cary Clark2f466242017-12-11 16:03:17 -05002247#Height 80
2248#Function
2249class TestImageGenerator : public SkImageGenerator {
2250public:
2251 TestImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(10, 10)) {}
2252 ~TestImageGenerator() override {}
2253protected:
2254 bool onGetPixels(const SkImageInfo& info, void* pixelPtr, size_t rowBytes,
2255 const Options& options) override {
2256 SkPMColor* pixels = static_cast<SkPMColor*>(pixelPtr);
2257 for (int y = 0; y < info.height(); ++y) {
2258 for (int x = 0; x < info.width(); ++x) {
2259 pixels[y * info.width() + x] = 0xff223344 + y * 0x000C0811;
2260 }
2261 }
2262 return true;
2263 }
2264};
2265##
2266void draw(SkCanvas* canvas) {
2267 auto gen = std::unique_ptr<TestImageGenerator>(new TestImageGenerator());
2268 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
2269 SkString lazy(image->isLazyGenerated() ? "is lazy" : "not lazy");
2270 canvas->scale(8, 8);
2271 canvas->drawImage(image, 0, 0, nullptr);
2272 SkPaint paint;
2273 paint.setTextSize(4);
2274 canvas->drawString(lazy, 2, 5, paint);
2275}
Cary Clarka560c472017-11-27 10:44:06 -05002276##
2277
Cary Clarkf5404bb2018-01-05 12:10:09 -05002278#Example
2279#Image 5
2280#Platform gpu
2281void draw(SkCanvas* canvas) {
2282 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2283 if (nullptr == image) {
2284 return;
2285 }
2286 SkPaint paint;
2287 paint.setAntiAlias(true);
2288 paint.setTextAlign(SkPaint::kCenter_Align);
2289 canvas->drawImage(image, 0, 0);
2290 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
2291 canvas->drawString(
2292 image->isLazyGenerated() ? "is lazily generated" : "not lazily generated",
2293 image->width() / 2, image->height() * 3 / 4, paint);
2294 };
2295 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2296 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2297 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2298 drawImage(image, "image");
2299 canvas->translate(image->width(), 0);
2300 drawImage(bitmapImage, "source");
2301 canvas->translate(-image->width(), image->height());
2302 drawImage(textureImage, "backEndTexture");
2303}
2304##
2305
Cary Clarkac47b882018-01-11 10:35:44 -05002306#SeeAlso isTextureBacked MakeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002307
2308#Method ##
2309
2310# ------------------------------------------------------------------------------
2311
2312#Method sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target,
2313 SkTransferFunctionBehavior premulBehavior) const
Cary Clark4855f782018-02-06 09:41:53 -05002314#In Constructor
2315#Line # creates Image matching Color_Space if possible ##
Cary Clarka560c472017-11-27 10:44:06 -05002316
Cary Clarkac47b882018-01-11 10:35:44 -05002317Creates Image in target Color_Space.
2318Returns nullptr if Image could not be created.
Cary Clarka560c472017-11-27 10:44:06 -05002319
Cary Clarkac47b882018-01-11 10:35:44 -05002320Returns original Image if it is in target Color_Space.
2321Otherwise, converts pixels from Image Color_Space to target Color_Space.
2322If Image colorSpace returns nullptr, Image Color_Space is assumed to be sRGB.
2323
2324SkTransferFunctionBehavior is to be deprecated.
2325
2326Set premulBehavior to SkTransferFunctionBehavior::kRespect to convert Image
2327pixels to a linear space, before converting to destination Color_Type
Cary Clarka560c472017-11-27 10:44:06 -05002328and Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -05002329
Cary Clarkac47b882018-01-11 10:35:44 -05002330Set premulBehavior to SkTransferFunctionBehavior::kIgnore to treat Image
2331pixels as linear, when converting to destination Color_Type
2332and Color_Space, ignoring pixel encoding.
Cary Clarka560c472017-11-27 10:44:06 -05002333
Cary Clarkac47b882018-01-11 10:35:44 -05002334#Param target Color_Space describing color range of returned Image ##
2335#Param premulBehavior one of: SkTransferFunctionBehavior::kRespect,
2336 SkTransferFunctionBehavior::kIgnore
Cary Clarka560c472017-11-27 10:44:06 -05002337##
2338
Cary Clarkac47b882018-01-11 10:35:44 -05002339#Return created Image in target Color_Space ##
2340
2341#Example
2342#Image 5
2343#Set sRGB
2344 sk_sp<SkColorSpace> normalColorSpace = SkColorSpace::MakeRGB(
2345 SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kSRGB_Gamut);
2346 sk_sp<SkColorSpace> wackyColorSpace = normalColorSpace->makeColorSpin();
2347 for (auto colorSpace : { normalColorSpace, wackyColorSpace } ) {
2348 for (auto transfer : { SkTransferFunctionBehavior::kRespect,
2349 SkTransferFunctionBehavior::kIgnore } ) {
2350 sk_sp<SkImage> colorSpaced = image->makeColorSpace(colorSpace, transfer);
2351 canvas->drawImage(colorSpaced, 0, 0);
2352 canvas->translate(128, 0);
2353 }
2354 canvas->translate(-256, 128);
2355 }
2356##
2357
2358#SeeAlso MakeFromPixture MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05002359
2360#Method ##
2361
2362#Class SkImage ##
2363
2364#Topic Image ##