blob: f45862af5ffa36c731203a59627b5f6661c33212 [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,
652 const GrBackendObject yuvTextureHandles[3],
653 const SkISize yuvSizes[3],
654 GrSurfaceOrigin surfaceOrigin,
655 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500656#In Constructor
657#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500658
Cary Clark61ca7c52018-01-02 11:34:14 -0500659Creates Image from copy of yuvTextureHandles, an array of textures on GPU.
660yuvTextureHandles contain pixels for YUV planes of Image.
Cary Clark4855f782018-02-06 09:41:53 -0500661yuvSizes contain dimensions for each pixel plane. Dimensions must be greater than
Cary Clark61ca7c52018-01-02 11:34:14 -0500662zero but may differ from plane to plane. Returned Image has the dimensions
663yuvSizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500664
Cary Clark61ca7c52018-01-02 11:34:14 -0500665#Param context GPU_Context ##
666#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
667 kRec709_SkYUVColorSpace
668##
669#Param yuvTextureHandles array of YUV textures on GPU ##
670#Param yuvSizes dimensions of YUV textures ##
671#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
672#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500673
Cary Clark61ca7c52018-01-02 11:34:14 -0500674#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500675
Cary Clark61ca7c52018-01-02 11:34:14 -0500676# seems too complicated to create an example for this
677#ToDo
678should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500679##
680
Cary Clark61ca7c52018-01-02 11:34:14 -0500681#NoExample
682##
683
684#SeeAlso MakeFromNV12TexturesCopy
685
686#Method ##
687
688# ------------------------------------------------------------------------------
689
690#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
691 const GrBackendTexture yuvTextureHandles[3],
692 const SkISize yuvSizes[3],
693 GrSurfaceOrigin surfaceOrigin,
694 sk_sp<SkColorSpace> colorSpace = nullptr)
695
696Creates Image from copy of yuvTextureHandles, an array of textures on GPU.
697yuvTextureHandles contain pixels for YUV planes of Image.
Cary Clark4855f782018-02-06 09:41:53 -0500698yuvSizes contain dimensions for each pixel plane. Dimensions must be greater than
Cary Clark61ca7c52018-01-02 11:34:14 -0500699zero but may differ from plane to plane. Returned Image has the dimensions
700yuvSizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
701
702#Param context GPU_Context ##
703#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
704 kRec709_SkYUVColorSpace
705##
706#Param yuvTextureHandles array of YUV textures on GPU ##
707#Param yuvSizes dimensions of YUV textures ##
708#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
709#Param colorSpace range of colors; may be nullptr ##
710
711#Return created Image, or nullptr ##
712
713# seems too complicated to create an example for this
714#ToDo
715should this be moved to chrome only?
716##
717
718#NoExample
719##
720
721#SeeAlso MakeFromNV12TexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500722
723#Method ##
724
725# ------------------------------------------------------------------------------
726
727#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
728 SkYUVColorSpace yuvColorSpace,
729 const GrBackendObject nv12TextureHandles[2],
730 const SkISize nv12Sizes[2],
731 GrSurfaceOrigin surfaceOrigin,
732 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500733#In Constructor
734#Line # creates Image from YUV_ColorSpace data in two planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500735
Cary Clark61ca7c52018-01-02 11:34:14 -0500736Creates Image from copy of nv12TextureHandles, an array of textures on GPU.
737nv12TextureHandles[0] contains pixels for YUV_Component_Y plane.
738nv12TextureHandles[1] contains pixels for YUV_Component_U plane,
739followed by pixels for YUV_Component_V plane.
Cary Clark4855f782018-02-06 09:41:53 -0500740nv12Sizes contain dimensions for each pixel plane. Dimensions must be greater than
Cary Clark61ca7c52018-01-02 11:34:14 -0500741zero but may differ from plane to plane. Returned Image has the dimensions
742nv12Sizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500743
Cary Clark61ca7c52018-01-02 11:34:14 -0500744#Param context GPU_Context ##
745#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
746 kRec709_SkYUVColorSpace
747##
748#Param nv12TextureHandles array of YUV textures on GPU ##
749#Param nv12Sizes dimensions of YUV textures ##
750#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
751#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500752
Cary Clark61ca7c52018-01-02 11:34:14 -0500753#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500754
Cary Clark61ca7c52018-01-02 11:34:14 -0500755# seems too complicated to create an example for this
756#ToDo
757should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500758##
759
Cary Clark61ca7c52018-01-02 11:34:14 -0500760#NoExample
761##
762
763#SeeAlso MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500764
765#Method ##
766
767# ------------------------------------------------------------------------------
768
Cary Clark61ca7c52018-01-02 11:34:14 -0500769#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
770 SkYUVColorSpace yuvColorSpace,
771 const GrBackendTexture nv12TextureHandles[2],
772 const SkISize nv12Sizes[2],
773 GrSurfaceOrigin surfaceOrigin,
774 sk_sp<SkColorSpace> colorSpace = nullptr)
775
776Creates Image from copy of nv12TextureHandles, an array of textures on GPU.
777nv12TextureHandles[0] contains pixels for YUV_Component_Y plane.
778nv12TextureHandles[1] contains pixels for YUV_Component_U plane,
779followed by pixels for YUV_Component_V plane.
Cary Clark4855f782018-02-06 09:41:53 -0500780nv12Sizes contain dimensions for each pixel plane. Dimensions must be greater than
Cary Clark61ca7c52018-01-02 11:34:14 -0500781zero but may differ from plane to plane. Returned Image has the dimensions
782nv12Sizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
783
784#Param context GPU_Context ##
785#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
786 kRec709_SkYUVColorSpace
787##
788#Param nv12TextureHandles array of YUV textures on GPU ##
789#Param nv12Sizes dimensions of YUV textures ##
790#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
791#Param colorSpace range of colors; may be nullptr ##
792
793#Return created Image, or nullptr ##
794
795# seems too complicated to create an example for this
796#ToDo
797should this be moved to chrome only?
798##
799
800#NoExample
801##
802
803#SeeAlso MakeFromYUVTexturesCopy
804
805#Method ##
806
807# ------------------------------------------------------------------------------
808
Cary Clark4855f782018-02-06 09:41:53 -0500809# currently uncalled by any test or client ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500810#Bug 7424
Cary Clark61ca7c52018-01-02 11:34:14 -0500811
Cary Clark56356312018-02-08 14:45:18 -0500812#EnumClass BitDepth
Cary Clarka560c472017-11-27 10:44:06 -0500813
814#Code
Cary Clark61ca7c52018-01-02 11:34:14 -0500815 enum class BitDepth {
Cary Clarka560c472017-11-27 10:44:06 -0500816 kU8,
817 kF16,
818 };
819##
820
821#Const kU8 0
Cary Clark61ca7c52018-01-02 11:34:14 -0500822Use 8 bits per Color_ARGB component using unsigned integer format.
Cary Clarka560c472017-11-27 10:44:06 -0500823##
824#Const kF16 1
Cary Clark61ca7c52018-01-02 11:34:14 -0500825Use 16 bits per Color_ARGB component using half-precision floating point format.
Cary Clarka560c472017-11-27 10:44:06 -0500826##
827
Cary Clark61ca7c52018-01-02 11:34:14 -0500828#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500829##
830
Cary Clark61ca7c52018-01-02 11:34:14 -0500831#SeeAlso MakeFromPicture
Cary Clarka560c472017-11-27 10:44:06 -0500832
Cary Clark56356312018-02-08 14:45:18 -0500833#EnumClass ##
Cary Clarka560c472017-11-27 10:44:06 -0500834
835# ------------------------------------------------------------------------------
836
837#Method static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
838 const SkMatrix* matrix, const SkPaint* paint,
839 BitDepth bitDepth,
840 sk_sp<SkColorSpace> colorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500841#In Constructor
842#Line # creates Image from Picture ##
Cary Clarka560c472017-11-27 10:44:06 -0500843
Cary Clark61ca7c52018-01-02 11:34:14 -0500844Creates Image from picture. Returned Image width and height are set by dimensions.
845Image draws picture with matrix and paint, set to bitDepth and colorSpace.
Cary Clarka560c472017-11-27 10:44:06 -0500846
Cary Clark61ca7c52018-01-02 11:34:14 -0500847If matrix is nullptr, draws with identity Matrix. If paint is nullptr, draws
848with default Paint. colorSpace may be nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500849
Cary Clark61ca7c52018-01-02 11:34:14 -0500850#Param picture stream of drawing commands ##
851#Param dimensions width and height ##
852#Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ##
853#Param paint Paint to apply transparency, filtering, and so on; may be nullptr ##
854#Param bitDepth 8 bit integer or 16 bit float: per component ##
855#Param colorSpace range of colors; may be nullptr ##
856
857#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500858
859#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500860 SkPaint paint;
861 SkPictureRecorder recorder;
862 SkCanvas* recordingCanvas = recorder.beginRecording(50, 50);
863 for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) {
864 paint.setColor(color);
865 recordingCanvas->drawRect({10, 10, 30, 40}, paint);
866 recordingCanvas->translate(10, 10);
867 recordingCanvas->scale(1.2f, 1.4f);
868 }
869 sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
870 int x = 0, y = 0;
871 for (auto alpha : { 70, 140, 210 } ) {
872 paint.setAlpha(alpha);
873 auto srgbColorSpace = SkColorSpace::MakeSRGB();
874 sk_sp<SkImage> image = SkImage::MakeFromPicture(playback, {50, 50}, nullptr, &paint,
875 SkImage::BitDepth::kU8, srgbColorSpace);
876 canvas->drawImage(image, x, y);
877 x += 70; y += 70;
878 }
Cary Clarka560c472017-11-27 10:44:06 -0500879##
880
Cary Clark61ca7c52018-01-02 11:34:14 -0500881#SeeAlso SkCanvas::drawPicture
Cary Clarka560c472017-11-27 10:44:06 -0500882
883#Method ##
884
885# ------------------------------------------------------------------------------
886
887#Method static sk_sp<SkImage> MakeFromAHardwareBuffer(AHardwareBuffer* hardwareBuffer,
888 SkAlphaType alphaType = kPremul_SkAlphaType,
889 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500890#In Constructor
891#Line # creates Image from Android hardware buffer ##
Cary Clarka560c472017-11-27 10:44:06 -0500892
Cary Clark4855f782018-02-06 09:41:53 -0500893#Bug 7447
Cary Clarka560c472017-11-27 10:44:06 -0500894
Cary Clark61ca7c52018-01-02 11:34:14 -0500895Creates Image from Android hardware buffer.
896Returned Image takes a reference on the buffer.
Cary Clarka560c472017-11-27 10:44:06 -0500897
Cary Clark61ca7c52018-01-02 11:34:14 -0500898Only available on Android, when __ANDROID_API__ is defined to be 26 or greater.
Cary Clarka560c472017-11-27 10:44:06 -0500899
Cary Clark61ca7c52018-01-02 11:34:14 -0500900#Param hardwareBuffer AHardwareBuffer Android hardware buffer ##
901#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
902 kPremul_SkAlphaType, kUnpremul_SkAlphaType
903##
904#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500905
Cary Clark61ca7c52018-01-02 11:34:14 -0500906#Return created Image, or nullptr ##
907
908#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500909##
910
Cary Clark61ca7c52018-01-02 11:34:14 -0500911#SeeAlso MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -0500912
913#Method ##
914
915# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -0500916#Subtopic Property
917#Populate
918#Line # values and attributes ##
919##
Cary Clarka560c472017-11-27 10:44:06 -0500920
921#Method int width() const
Cary Clark4855f782018-02-06 09:41:53 -0500922#In Property
923#Line # returns pixel column count ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500924Returns pixel count in each row.
925
926#Return pixel width in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500927
928#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500929#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500930#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500931 canvas->translate(10, 10);
932 canvas->drawImage(image, 0, 0);
933 canvas->translate(0, image->height());
934 SkPaint paint;
935 paint.setTextAlign(SkPaint::kCenter_Align);
936 canvas->drawLine(0, 10, image->width(), 10, paint);
937 canvas->drawString("width", image->width() / 2, 25, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500938##
939
Cary Clark61ca7c52018-01-02 11:34:14 -0500940#SeeAlso dimensions() height()
Cary Clarka560c472017-11-27 10:44:06 -0500941
942#Method ##
943
944# ------------------------------------------------------------------------------
945
946#Method int height() const
Cary Clark4855f782018-02-06 09:41:53 -0500947#In Property
948#Line # returns pixel row count ##
Cary Clark2f466242017-12-11 16:03:17 -0500949Returns pixel row count.
950
Cary Clark61ca7c52018-01-02 11:34:14 -0500951#Return pixel height in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500952
953#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500954#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500955#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500956 canvas->translate(10, 10);
957 canvas->drawImage(image, 0, 0);
958 canvas->translate(image->width(), 0);
959 SkPaint paint;
960 paint.setTextAlign(SkPaint::kCenter_Align);
961 paint.setVerticalText(true);
962 canvas->drawLine(10, 0, 10, image->height(), paint);
Cary Clarkac47b882018-01-11 10:35:44 -0500963 canvas->drawString("height", 25, image->height() / 2, paint);
964##
Cary Clarka560c472017-11-27 10:44:06 -0500965
Cary Clark61ca7c52018-01-02 11:34:14 -0500966#SeeAlso dimensions() width()
Cary Clarka560c472017-11-27 10:44:06 -0500967
968#Method ##
969
970# ------------------------------------------------------------------------------
971
972#Method SkISize dimensions() const
Cary Clark4855f782018-02-06 09:41:53 -0500973#In Property
974#Line # returns width() and height() ##
Cary Clark2f466242017-12-11 16:03:17 -0500975Returns ISize { width(), height() }.
976
977#Return integral size of width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -0500978
979#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500980#Image 4
981 SkISize dimensions = image->dimensions();
982 SkIRect bounds = image->bounds();
983 SkIRect dimensionsAsBounds = SkIRect::MakeSize(dimensions);
984 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clarka560c472017-11-27 10:44:06 -0500985##
986
Cary Clark61ca7c52018-01-02 11:34:14 -0500987#SeeAlso height() width() bounds()
Cary Clarka560c472017-11-27 10:44:06 -0500988
989#Method ##
990
991# ------------------------------------------------------------------------------
992
993#Method SkIRect bounds() const
Cary Clark4855f782018-02-06 09:41:53 -0500994#In Property
995#Line # returns width() and height() as Rectangle ##
Cary Clark2f466242017-12-11 16:03:17 -0500996Returns IRect { 0, 0, width(), height() }.
997
998#Return integral rectangle from origin to width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -0500999
1000#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001001#Height 128
1002#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -05001003 SkIRect bounds = image->bounds();
Cary Clarkac47b882018-01-11 10:35:44 -05001004 for (int x : { 0, bounds.width() } ) {
1005 for (int y : { 0, bounds.height() } ) {
Cary Clark61ca7c52018-01-02 11:34:14 -05001006 canvas->drawImage(image, x, y);
1007 }
1008 }
Cary Clarka560c472017-11-27 10:44:06 -05001009##
1010
Cary Clark61ca7c52018-01-02 11:34:14 -05001011#SeeAlso dimensions()
Cary Clarka560c472017-11-27 10:44:06 -05001012
1013#Method ##
1014
1015# ------------------------------------------------------------------------------
1016
1017#Method uint32_t uniqueID() const
Cary Clark4855f782018-02-06 09:41:53 -05001018#In Property
1019#Line # identifier for Image ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001020Returns value unique to image. Image contents cannot change after Image is
1021created. Any operation to create a new Image will receive generate a new
1022unique number.
1023
1024#Return unique identifier ##
Cary Clarka560c472017-11-27 10:44:06 -05001025
1026#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001027#Image 5
1028#Height 156
1029 sk_sp<SkImage> subset = image->makeSubset({10, 20, 90, 100});
1030 canvas->drawImage(image, 0, 0);
1031 canvas->drawImage(subset, 128, 0);
1032 SkPaint paint;
1033 SkString s;
1034 s.printf("original id: %d", image->uniqueID());
1035 canvas->drawString(s, 20, image->height() + 20, paint);
1036 s.printf("subset id: %d", subset->uniqueID());
1037 canvas->drawString(s, 148, subset->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001038##
1039
Cary Clark61ca7c52018-01-02 11:34:14 -05001040#SeeAlso isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001041
1042#Method ##
1043
1044# ------------------------------------------------------------------------------
1045
1046#Method SkAlphaType alphaType() const
Cary Clark4855f782018-02-06 09:41:53 -05001047#In Property
1048#Line # returns Alpha_Type ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001049Returns Alpha_Type, one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
1050kPremul_SkAlphaType, kUnpremul_SkAlphaType.
1051
1052Alpha_Type returned was a parameter to an Image constructor,
1053or was parsed from encoded data.
1054
1055#Return Alpha_Type in Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001056
1057#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001058#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -05001059#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -05001060 const char* alphaTypeStr[] = { "Unknown", "Opaque", "Premul", "Unpremul" };
1061 SkAlphaType alphaType = image->alphaType();
Cary Clarkac47b882018-01-11 10:35:44 -05001062 canvas->drawImage(image, 16, 0);
Cary Clark61ca7c52018-01-02 11:34:14 -05001063 SkPaint paint;
1064 canvas->drawString(alphaTypeStr[(int) alphaType], 20, image->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001065##
1066
Cary Clark61ca7c52018-01-02 11:34:14 -05001067#SeeAlso SkImageInfo::alphaType
Cary Clarka560c472017-11-27 10:44:06 -05001068
1069#Method ##
1070
1071# ------------------------------------------------------------------------------
1072
Greg Daniel56008aa2018-03-14 15:33:42 -04001073#Method SkColorType colorType() const
1074#In Property
1075#Line # returns Color_Type ##
1076
1077Returns Color_Type if known; otherwise, returns kUnknown_SkColorType.
1078
1079#Return Color_Type of Image ##
1080
1081#Example
1082// incomplete
1083##
1084
1085#SeeAlso SkImageInfo::colorType
1086
1087#Method ##
1088
1089# ------------------------------------------------------------------------------
1090
Cary Clarka560c472017-11-27 10:44:06 -05001091#Method SkColorSpace* colorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001092#In Property
1093#Line # returns Color_Space ##
Cary Clark2f466242017-12-11 16:03:17 -05001094Returns Color_Space, the range of colors, associated with Image. The
1095reference count of Color_Space is unchanged. The returned Color_Space is
1096immutable.
Cary Clarka560c472017-11-27 10:44:06 -05001097
Cary Clark61dfc3a2018-01-03 08:37:53 -05001098Color_Space returned was passed to an Image constructor,
1099or was parsed from encoded data. Color_Space returned may be ignored when Image
1100is drawn, depending on the capabilities of the Surface receiving the drawing.
Cary Clark2f466242017-12-11 16:03:17 -05001101
1102#Return Color_Space in Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001103
1104#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001105#Image 3
1106#Set sRGB
1107 SkPixmap pixmap;
1108 source.peekPixels(&pixmap);
1109 canvas->scale(.25f, .25f);
1110 int y = 0;
1111 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1112 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1113 int x = 0;
1114 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1115 for (int index = 0; index < 2; ++index) {
1116 pixmap.setColorSpace(colorSpace);
1117 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1118 canvas->drawImage(image, x, y);
1119 colorSpace = image->colorSpace()->makeColorSpin();
1120 x += 512;
1121 }
1122 y += 512;
1123 }
Cary Clarka560c472017-11-27 10:44:06 -05001124##
1125
Cary Clark61dfc3a2018-01-03 08:37:53 -05001126#SeeAlso refColorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001127
1128#Method ##
1129
1130# ------------------------------------------------------------------------------
1131
1132#Method sk_sp<SkColorSpace> refColorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001133#In Property
1134#Line # returns Image_Info Color_Space ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001135Returns a smart pointer to Color_Space, the range of colors, associated with
1136Image. The smart pointer tracks the number of objects sharing this
1137SkColorSpace reference so the memory is released when the owners destruct.
1138
1139The returned SkColorSpace is immutable.
1140
1141Color_Space returned was passed to an Image constructor,
1142or was parsed from encoded data. Color_Space returned may be ignored when Image
1143is drawn, depending on the capabilities of the Surface receiving the drawing.
1144
1145#Return Color_Space in Image, or nullptr, wrapped in a smart pointer ##
Cary Clarka560c472017-11-27 10:44:06 -05001146
1147#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001148#Image 3
1149#Set sRGB
1150 SkPixmap pixmap;
1151 source.peekPixels(&pixmap);
1152 canvas->scale(.25f, .25f);
1153 int y = 0;
1154 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1155 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1156 int x = 0;
1157 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1158 for (int index = 0; index < 2; ++index) {
1159 pixmap.setColorSpace(colorSpace);
1160 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1161 canvas->drawImage(image, x, y);
1162 colorSpace = image->refColorSpace()->makeColorSpin();
1163 x += 512;
1164 }
1165 y += 512;
1166 }
Cary Clarka560c472017-11-27 10:44:06 -05001167##
1168
Cary Clark61dfc3a2018-01-03 08:37:53 -05001169#SeeAlso colorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001170
1171#Method ##
1172
1173# ------------------------------------------------------------------------------
1174
1175#Method bool isAlphaOnly() const
Cary Clark4855f782018-02-06 09:41:53 -05001176#In Property
1177#Line # returns if pixels represent a transparency mask ##
Cary Clark2f466242017-12-11 16:03:17 -05001178Returns true if Image pixels represent transparency only. If true, each pixel
1179is packed in 8 bits as defined by kAlpha_8_SkColorType.
Cary Clarka560c472017-11-27 10:44:06 -05001180
Cary Clark2f466242017-12-11 16:03:17 -05001181#Return true if pixels represent a transparency mask ##
Cary Clarka560c472017-11-27 10:44:06 -05001182
1183#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001184 uint8_t pmColors = 0;
1185 sk_sp<SkImage> image = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1), &pmColors, 1});
1186 SkDebugf("alphaOnly = %s\n", image->isAlphaOnly() ? "true" : "false");
1187#StdOut
1188alphaOnly = true
1189##
Cary Clarka560c472017-11-27 10:44:06 -05001190##
1191
Cary Clark61dfc3a2018-01-03 08:37:53 -05001192#SeeAlso alphaType isOpaque
Cary Clarka560c472017-11-27 10:44:06 -05001193
1194#Method ##
1195
1196# ------------------------------------------------------------------------------
1197
1198#Method bool isOpaque() const
Cary Clark4855f782018-02-06 09:41:53 -05001199#In Property
1200#Line # returns if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001201Returns true if pixels ignore their Alpha value and are treated as fully opaque.
Cary Clark2f466242017-12-11 16:03:17 -05001202
1203#Return true if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clarka560c472017-11-27 10:44:06 -05001204
1205#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001206 auto check_isopaque = [](const SkImageInfo& imageInfo) -> void {
1207 auto surface(SkSurface::MakeRaster(imageInfo));
1208 auto image(surface->makeImageSnapshot());
1209 SkDebugf("isOpaque = %s\n", image->isOpaque() ? "true" : "false");
1210 };
1211
1212 check_isopaque(SkImageInfo::MakeN32Premul(5, 5));
1213 check_isopaque(SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType));
1214#StdOut
1215isOpaque = false
1216isOpaque = true
1217##
Cary Clarka560c472017-11-27 10:44:06 -05001218##
1219
Cary Clark61dfc3a2018-01-03 08:37:53 -05001220#SeeAlso alphaType isAlphaOnly
Cary Clarka560c472017-11-27 10:44:06 -05001221
1222#Method ##
1223
1224# ------------------------------------------------------------------------------
1225
1226#Method sk_sp<SkShader> makeShader(SkShader::TileMode tileMode1, SkShader::TileMode tileMode2,
1227 const SkMatrix* localMatrix = nullptr) const
Cary Clark4855f782018-02-06 09:41:53 -05001228#In Constructor
1229#Line # creates Shader, Paint element that can tile Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001230
Cary Clark61dfc3a2018-01-03 08:37:53 -05001231Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1232SkShader::TileMode rules to fill drawn area outside Image. localMatrix permits
1233transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001234
Cary Clark61dfc3a2018-01-03 08:37:53 -05001235#Param tileMode1 tiling in x, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1236 SkShader::kMirror_TileMode
1237##
1238#Param tileMode2 tiling in y, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1239 SkShader::kMirror_TileMode
1240##
1241#Param localMatrix Image transformation, or nullptr ##
1242
1243#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001244
1245#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001246#Image 4
1247SkMatrix matrix;
1248matrix.setRotate(45);
1249SkPaint paint;
1250paint.setShader(image->makeShader(SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode,
1251 &matrix));
1252canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001253##
1254
Cary Clark61dfc3a2018-01-03 08:37:53 -05001255#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001256
1257#Method ##
1258
1259# ------------------------------------------------------------------------------
1260
1261#Method sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const
1262
Cary Clark61dfc3a2018-01-03 08:37:53 -05001263Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1264SkShader::kClamp_TileMode to fill drawn area outside Image. localMatrix permits
1265transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001266
Cary Clark61dfc3a2018-01-03 08:37:53 -05001267#Param localMatrix Image transformation, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001268
Cary Clark61dfc3a2018-01-03 08:37:53 -05001269#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001270
1271#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001272#Image 5
1273SkMatrix matrix;
1274matrix.setRotate(45);
1275matrix.postTranslate(125, 30);
1276SkPaint paint;
1277paint.setShader(image->makeShader(&matrix));
1278canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001279##
1280
Cary Clarkf5404bb2018-01-05 12:10:09 -05001281#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001282
1283#Method ##
1284
1285# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001286#Subtopic Pixels
1287#Populate
1288#Line # read and write pixel values ##
1289##
Cary Clarka560c472017-11-27 10:44:06 -05001290
1291#Method bool peekPixels(SkPixmap* pixmap) const
Cary Clark78de7512018-02-07 07:27:09 -05001292#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001293#Line # returns Pixmap if possible ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001294Copies Image pixel address, row bytes, and Image_Info to pixmap, if address
1295is available, and returns true. If pixel address is not available, return
1296false and leave pixmap unchanged.
Cary Clarka560c472017-11-27 10:44:06 -05001297
Cary Clarkf5404bb2018-01-05 12:10:09 -05001298#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
Cary Clarka560c472017-11-27 10:44:06 -05001299
Cary Clarkf5404bb2018-01-05 12:10:09 -05001300#Return true if Image has direct access to pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001301
1302#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001303 SkBitmap bitmap;
1304 bitmap.allocPixels(SkImageInfo::MakeN32Premul(12, 11));
1305 SkCanvas offscreen(bitmap);
1306 offscreen.clear(SK_ColorWHITE);
1307 SkPaint paint;
1308 offscreen.drawString("%", 1, 10, paint);
1309 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
1310 SkPixmap pixmap;
1311 if (image->peekPixels(&pixmap)) {
1312 const SkPMColor* pixels = pixmap.addr32();
1313 SkPMColor pmWhite = pixels[0];
1314 for (int y = 0; y < image->height(); ++y) {
1315 for (int x = 0; x < image->width(); ++x) {
1316 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
1317 }
1318 SkDebugf("\n");
1319 }
1320 }
1321#StdOut
1322------------
1323--xx----x---
1324-x--x--x----
1325-x--x--x----
1326-x--x-x-----
1327--xx-xx-xx--
1328-----x-x--x-
1329----x--x--x-
1330----x--x--x-
1331---x----xx--
1332------------
1333##
Cary Clarka560c472017-11-27 10:44:06 -05001334##
1335
Cary Clarkf5404bb2018-01-05 12:10:09 -05001336#SeeAlso readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001337
1338#Method ##
1339
1340# ------------------------------------------------------------------------------
1341
1342#Method GrTexture* getTexture() const
Cary Clark2f466242017-12-11 16:03:17 -05001343#Deprecated
Cary Clarka560c472017-11-27 10:44:06 -05001344#Method ##
1345
1346# ------------------------------------------------------------------------------
1347
1348#Method bool isTextureBacked() const
Cary Clark78de7512018-02-07 07:27:09 -05001349#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001350#Line # returns if Image was created from GPU_Texture ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001351Returns true the contents of Image was created on or uploaded to GPU memory,
1352and is available as a GPU_Texture.
Cary Clarka560c472017-11-27 10:44:06 -05001353
Cary Clarkf5404bb2018-01-05 12:10:09 -05001354#Return true if Image is a GPU_Texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001355
1356#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001357#Image 5
1358#Platform gpu
1359auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1360 if (nullptr == image) {
1361 return;
1362 }
1363 SkPaint paint;
1364 paint.setAntiAlias(true);
1365 paint.setTextAlign(SkPaint::kCenter_Align);
1366 canvas->drawImage(image, 0, 0);
1367 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1368 canvas->drawString(image->isTextureBacked() ? "is GPU texture" : "not GPU texture",
1369 image->width() / 2, image->height() * 3 / 4, paint);
1370};
1371sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1372sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1373 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1374drawImage(image, "image");
1375canvas->translate(image->width(), 0);
1376drawImage(bitmapImage, "source");
1377canvas->translate(-image->width(), image->height());
1378drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001379##
1380
Cary Clarkf5404bb2018-01-05 12:10:09 -05001381#SeeAlso MakeFromTexture isValid
Cary Clarka560c472017-11-27 10:44:06 -05001382
1383#Method ##
1384
1385# ------------------------------------------------------------------------------
1386
1387#Method bool isValid(GrContext* context) const
Cary Clark4855f782018-02-06 09:41:53 -05001388#In Property
1389#Line # returns if Image can draw to Raster_Surface or GPU_Context ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001390Returns true if Image can be drawn on either Raster_Surface or GPU_Surface.
1391If context is nullptr, tests if Image draws on Raster_Surface;
1392otherwise, tests if Image draws on GPU_Surface associated with context.
Cary Clarka560c472017-11-27 10:44:06 -05001393
Cary Clarkf5404bb2018-01-05 12:10:09 -05001394Image backed by GPU_Texture may become invalid if associated GrContext is
1395invalid. Lazy_Image may be invalid and may not draw to Raster_Surface or
1396GPU_Surface or both.
Cary Clarka560c472017-11-27 10:44:06 -05001397
Cary Clark61ca7c52018-01-02 11:34:14 -05001398#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05001399
Cary Clarkf5404bb2018-01-05 12:10:09 -05001400#Return true if Image can be drawn ##
Cary Clarka560c472017-11-27 10:44:06 -05001401
1402#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001403#Image 5
1404#Platform gpu
1405auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1406 if (nullptr == image) {
1407 return;
1408 }
1409 SkPaint paint;
1410 paint.setAntiAlias(true);
1411 paint.setTextAlign(SkPaint::kCenter_Align);
1412 canvas->drawImage(image, 0, 0);
1413 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1414 if (canvas->getGrContext()) {
1415 canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" :
1416 "not valid on GPU", image->width() / 2, image->height() * 5 / 8, paint);
1417 }
1418 canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" :
1419 "not valid on CPU", image->width() / 2, image->height() * 7 / 8, paint);
1420};
1421sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1422sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1423 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1424drawImage(image, "image");
1425canvas->translate(image->width(), 0);
1426drawImage(bitmapImage, "source");
1427canvas->translate(-image->width(), image->height());
1428drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001429##
1430
Cary Clarkf5404bb2018-01-05 12:10:09 -05001431#SeeAlso isTextureBacked isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001432
1433#Method ##
1434
1435# ------------------------------------------------------------------------------
1436
1437#Method GrBackendObject getTextureHandle(bool flushPendingGrContextIO,
1438 GrSurfaceOrigin* origin = nullptr) const
Cary Clark78de7512018-02-07 07:27:09 -05001439#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001440#Line # returns GPU reference to Image as texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001441
Cary Clark2f466242017-12-11 16:03:17 -05001442Retrieves the back-end API handle of texture. If flushPendingGrContextIO is true,
1443complete deferred I/O operations.
Cary Clarka560c472017-11-27 10:44:06 -05001444
Cary Clark2f466242017-12-11 16:03:17 -05001445If origin in not nullptr, copies location of content drawn into Image.
Cary Clarka560c472017-11-27 10:44:06 -05001446
Cary Clark2f466242017-12-11 16:03:17 -05001447#Param flushPendingGrContextIO flag to flush outstanding requests ##
1448#Param origin storage for one of: kTopLeft_GrSurfaceOrigin,
1449 kBottomLeft_GrSurfaceOrigin; or nullptr
1450##
1451
Cary Clarkac47b882018-01-11 10:35:44 -05001452#Return back-end API texture handle, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001453
1454#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001455#Image 4
Cary Clark2f466242017-12-11 16:03:17 -05001456#Platform gpu
1457GrContext* context = canvas->getGrContext();
1458if (!context) {
1459 return;
1460}
1461SkPaint paint;
1462paint.setAntiAlias(true);
1463SkString str;
Cary Clarkac47b882018-01-11 10:35:44 -05001464int y = -10;
Cary Clark2f466242017-12-11 16:03:17 -05001465for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin } ) {
1466 sk_sp<SkImage> srcImage(SkImage::MakeFromTexture(context,
1467 backEndTexture, origin, kPremul_SkAlphaType, nullptr));
1468 GrSurfaceOrigin readBackOrigin;
1469 GrBackendObject readBackHandle = srcImage->getTextureHandle(false, &readBackOrigin);
Cary Clarkac47b882018-01-11 10:35:44 -05001470 str.printf("readBackHandle: 0x%x", readBackHandle);
1471 canvas->drawString(str, 5, y += 30, paint);
1472 canvas->drawImage(srcImage, 80, y += 10);
Cary Clark2f466242017-12-11 16:03:17 -05001473 str.printf("origin: k%s_GrSurfaceOrigin", readBackOrigin ? "BottomLeft" : "TopLeft");
Cary Clarkac47b882018-01-11 10:35:44 -05001474 canvas->drawString(str, 5, y += srcImage->height() + 10, paint);
Cary Clark2f466242017-12-11 16:03:17 -05001475}
Cary Clarka560c472017-11-27 10:44:06 -05001476##
1477
Cary Clarkac47b882018-01-11 10:35:44 -05001478#Example
1479#Image 5
1480#Platform gpu
1481 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1482 if (nullptr == image) {
1483 return;
1484 }
1485 SkPaint paint;
1486 paint.setAntiAlias(true);
1487 paint.setTextAlign(SkPaint::kCenter_Align);
1488 canvas->drawImage(image, 0, image->height() / 4);
1489 canvas->drawString(label, image->width() / 2, image->height() / 8, paint);
1490 GrSurfaceOrigin readBackOrigin;
1491 GrBackendObject readBackHandle = image->getTextureHandle(false, &readBackOrigin);
1492 canvas->drawString(readBackHandle ? "has readBackHandle" : "no readBackHandle",
1493 image->width() / 2, image->height() * 11 / 8, paint);
1494 };
1495 drawImage(image, "image");
1496 canvas->translate(image->width(), 0);
1497 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1498 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1499 drawImage(textureImage, "backEndTexture");
1500##
1501
1502#SeeAlso MakeFromTexture isTextureBacked
Cary Clarka560c472017-11-27 10:44:06 -05001503
1504#Method ##
1505
1506# ------------------------------------------------------------------------------
1507
1508#Enum CachingHint
1509
1510#Code
1511 enum CachingHint {
1512 kAllow_CachingHint,
1513 kDisallow_CachingHint,
1514 };
1515##
1516
Cary Clarkac47b882018-01-11 10:35:44 -05001517CachingHint selects whether Skia may internally cache Bitmaps generated by
1518decoding Image, or by copying Image from GPU to CPU. The default behavior
1519allows caching Bitmaps.
1520
1521Choose kDisallow_CachingHint if Image pixels are to be used only once, or
1522if Image pixels reside in a cache outside of Skia, or to reduce memory pressure.
1523
1524Choosing kAllow_CachingHint does not ensure that pixels will be cached.
1525Image pixels may not be cached if memory requirements are too large or
1526pixels are not accessible.
Cary Clarka560c472017-11-27 10:44:06 -05001527
1528#Const kAllow_CachingHint 0
Cary Clarkac47b882018-01-11 10:35:44 -05001529Allows Skia to internally cache decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001530##
1531#Const kDisallow_CachingHint 1
Cary Clarkac47b882018-01-11 10:35:44 -05001532Disallows Skia from internally caching decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001533##
1534
Cary Clarkac47b882018-01-11 10:35:44 -05001535#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001536##
1537
Cary Clarkac47b882018-01-11 10:35:44 -05001538#SeeAlso readPixels scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001539
1540#Enum ##
1541
1542# ------------------------------------------------------------------------------
1543
1544#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1545 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001546#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001547#Line # copies and converts pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001548
Cary Clarkac47b882018-01-11 10:35:44 -05001549Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY),
1550and does not exceed Image (width(), height()).
1551
1552dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
1553destination. dstRowBytes specifics the gap from one destination row to the next.
1554Returns true if pixels are copied. Returns false if:
1555#List
1556# dstInfo.addr() equals nullptr ##
1557# dstRowBytes is less than dstInfo.minRowBytes ##
1558# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001559##
1560
Cary Clarkac47b882018-01-11 10:35:44 -05001561Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1562kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1563If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace must match.
1564If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType must
1565match. If Image Color_Space is nullptr, dstInfo.colorSpace must match. Returns
1566false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001567
Cary Clarkac47b882018-01-11 10:35:44 -05001568srcX and srcY may be negative to copy only top or left of source. Returns
1569false if width() or height() is zero or negative.
1570Returns false if
1571#Formula
1572abs(srcX) >= Image width()
1573##
1574, or if
1575#Formula
1576abs(srcY) >= Image height()
1577##
1578.
Cary Clarka560c472017-11-27 10:44:06 -05001579
Cary Clarkac47b882018-01-11 10:35:44 -05001580If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1581If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1582
1583#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1584#Param dstPixels destination pixel storage ##
1585#Param dstRowBytes destination row length ##
1586#Param srcX column index whose absolute value is less than width() ##
1587#Param srcY row index whose absolute value is less than height() ##
1588#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1589
1590#Return true if pixels are copied to dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001591
1592#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001593#Image 3
1594 canvas->scale(.5f, .5f);
1595 const int width = 32;
1596 const int height = 32;
1597 std::vector<int32_t> dstPixels;
1598 dstPixels.resize(height * width * 4);
1599 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1600 for (int y = 0; y < 512; y += height ) {
1601 for (int x = 0; x < 512; x += width ) {
1602 if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) {
1603 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
1604 SkBitmap bitmap;
1605 bitmap.installPixels(dstPixmap);
1606 canvas->drawBitmap(bitmap, 0, 0);
1607 }
1608 canvas->translate(48, 0);
1609 }
1610 canvas->translate(-16 * 48, 48);
1611 }
Cary Clarka560c472017-11-27 10:44:06 -05001612##
1613
Cary Clarkac47b882018-01-11 10:35:44 -05001614#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001615
1616#Method ##
1617
1618# ------------------------------------------------------------------------------
1619
1620#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY,
1621 CachingHint cachingHint = kAllow_CachingHint) const
1622
Cary Clarkac47b882018-01-11 10:35:44 -05001623Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and
1624does not exceed Image (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001625
Cary Clarkac47b882018-01-11 10:35:44 -05001626dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
1627and row bytes of destination. dst.rowBytes specifics the gap from one destination
1628row to the next. Returns true if pixels are copied. Returns false if:
1629#List
1630# dst pixel storage equals nullptr ##
1631# dst.rowBytes is less than SkImageInfo::minRowBytes ##
1632# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001633##
1634
Cary Clarkac47b882018-01-11 10:35:44 -05001635Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1636kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1637If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1638If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1639match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1640false if pixel conversion is not possible.
1641
1642srcX and srcY may be negative to copy only top or left of source. Returns
1643false if width() or height() is zero or negative.
1644Returns false if
1645#Formula
1646abs(srcX) >= Image width()
1647##
1648, or if
1649#Formula
1650abs(srcY) >= Image height()
1651##
1652.
1653
1654If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1655If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1656
1657#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1658#Param srcX column index whose absolute value is less than width() ##
1659#Param srcY row index whose absolute value is less than height() ##
1660#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1661
1662#Return true if pixels are copied to dst ##
1663
1664#Example
1665#Image 3
1666 std::vector<int32_t> srcPixels;
1667 int rowBytes = image->width() * 4;
1668 int quarterWidth = image->width() / 4;
1669 int quarterHeight = image->height() / 4;
1670 srcPixels.resize(image->height() * rowBytes);
1671 for (int y = 0; y < 4; ++y) {
1672 for (int x = 0; x < 4; ++x) {
1673 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1674 &srcPixels.front() + x * image->height() * quarterWidth +
1675 y * quarterWidth, rowBytes);
1676 image->readPixels(pixmap, x * quarterWidth, y * quarterHeight);
1677 }
1678 }
1679 canvas->scale(.5f, .5f);
1680 SkBitmap bitmap;
1681 bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()),
1682 &srcPixels.front(), rowBytes);
1683 canvas->drawBitmap(bitmap, 0, 0);
1684##
1685
1686#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001687
1688#Method ##
1689
1690# ------------------------------------------------------------------------------
1691
1692#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality,
1693 CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001694#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001695#Line # scales and converts one Image to another ##
Cary Clarka560c472017-11-27 10:44:06 -05001696
Cary Clarkac47b882018-01-11 10:35:44 -05001697Copies Image to dst, scaling pixels to fit dst.width() and dst.height(), and
1698converting pixels to match dst.colorType and dst.alphaType. Returns true if
1699pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1700less than dst SkImageInfo::minRowBytes.
Cary Clarka560c472017-11-27 10:44:06 -05001701
Cary Clarkac47b882018-01-11 10:35:44 -05001702Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1703kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1704If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1705If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1706match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1707false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001708
Cary Clarkac47b882018-01-11 10:35:44 -05001709Scales the image, with filterQuality, to match dst.width() and dst.height().
1710filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1711Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1712Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1713Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1714kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1715
1716If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1717If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1718
1719#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1720#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1721 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1722##
1723#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1724
1725#Return true if pixels are scaled to fit dst ##
Cary Clarka560c472017-11-27 10:44:06 -05001726
1727#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001728#Image 3
1729#Height 128
1730 std::vector<int32_t> srcPixels;
1731 int quarterWidth = image->width() / 16;
1732 int rowBytes = quarterWidth * 4;
1733 int quarterHeight = image->height() / 16;
1734 srcPixels.resize(quarterHeight * rowBytes);
1735 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1736 &srcPixels.front(), rowBytes);
1737 canvas->scale(4, 4);
1738 SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality,
1739 kMedium_SkFilterQuality, kHigh_SkFilterQuality };
1740 for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) {
1741 image->scalePixels(pixmap, qualities[index]);
1742 sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
1743 canvas->drawImage(filtered, 16 * index, 0);
1744 }
Cary Clarka560c472017-11-27 10:44:06 -05001745##
1746
Cary Clarkac47b882018-01-11 10:35:44 -05001747#SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001748
1749#Method ##
1750
1751# ------------------------------------------------------------------------------
1752
1753#Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const
Cary Clark78de7512018-02-07 07:27:09 -05001754#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001755#Line # returns encoded Image as SkData ##
Cary Clarkac47b882018-01-11 10:35:44 -05001756Encodes Image pixels, returning result as SkData.
Cary Clark2f466242017-12-11 16:03:17 -05001757
Cary Clarkac47b882018-01-11 10:35:44 -05001758Returns nullptr if encoding fails, or if encodedImageFormat is not supported.
Cary Clarka560c472017-11-27 10:44:06 -05001759
Cary Clarkac47b882018-01-11 10:35:44 -05001760Image encoding in a format requires both building with one or more of:
1761SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support
1762for the encoded format.
1763
1764If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can
1765additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP,
1766SkEncodedImageFormat::kGIF.
1767
1768quality is a platform and format specific metric trading off size and encoding
1769error. When used, quality equaling 100 encodes with the least error. quality may
1770be ignored by the encoder.
1771
1772#Param encodedImageFormat one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
1773 SkEncodedImageFormat::kWEBP
1774 ##
1775#Param quality encoder specific metric with 100 equaling best ##
Cary Clarka560c472017-11-27 10:44:06 -05001776
Cary Clark2f466242017-12-11 16:03:17 -05001777#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001778
1779#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001780#Image 3
1781 canvas->scale(4, 4);
1782 SkIRect subset = {0, 0, 16, 64};
1783 int x = 0;
1784 for (int quality : { 0, 10, 50, 100 } ) {
1785 sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
1786 sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
1787 canvas->drawImage(filtered, x, 0);
1788 x += 16;
1789 }
Cary Clarka560c472017-11-27 10:44:06 -05001790##
1791
Cary Clarkac47b882018-01-11 10:35:44 -05001792#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001793
1794#Method ##
1795
1796# ------------------------------------------------------------------------------
1797
Cary Clark61ca7c52018-01-02 11:34:14 -05001798#Method sk_sp<SkData> encodeToData() const
Cary Clarka560c472017-11-27 10:44:06 -05001799
Cary Clarkac47b882018-01-11 10:35:44 -05001800Encodes Image pixels, returning result as SkData. Returns existing encoded data
1801if present; otherwise, Image is encoded with SkEncodedImageFormat::kPNG. Skia
1802must be built with SK_HAS_PNG_LIBRARY to encode Image.
Cary Clarka560c472017-11-27 10:44:06 -05001803
Cary Clarkac47b882018-01-11 10:35:44 -05001804Returns nullptr if existing encoded data is missing or invalid, and
Cary Clarka560c472017-11-27 10:44:06 -05001805encoding fails.
1806
Cary Clarkac47b882018-01-11 10:35:44 -05001807#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001808
1809#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001810#Image 3
1811 canvas->scale(4, 4);
1812 SkIRect subset = {136, 32, 200, 96};
1813 sk_sp<SkData> data(image->encodeToData());
1814 sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
1815 canvas->drawImage(eye, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001816##
1817
Cary Clarkac47b882018-01-11 10:35:44 -05001818#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001819
1820#Method ##
1821
1822# ------------------------------------------------------------------------------
1823
1824#Method sk_sp<SkData> refEncodedData() const
Cary Clark78de7512018-02-07 07:27:09 -05001825#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001826#Line # returns Image encoded in SkData if present ##
Cary Clarkac47b882018-01-11 10:35:44 -05001827Returns encoded Image pixels as SkData, if Image was created from supported
1828encoded stream format. Platform support for formats vary and may require building
1829with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY.
Cary Clarka560c472017-11-27 10:44:06 -05001830
Cary Clarkac47b882018-01-11 10:35:44 -05001831Returns nullptr if Image contents are not encoded.
Cary Clarka560c472017-11-27 10:44:06 -05001832
Cary Clarkac47b882018-01-11 10:35:44 -05001833#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001834
1835#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001836#Image 3
1837#Platform gpu
1838 struct {
1839 const char* name;
1840 sk_sp<SkImage> image;
1841 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1842 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1843 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr) } };
1844 SkString string;
1845 SkPaint paint;
1846 for (const auto& test : tests ) {
1847 if (!test.image) {
1848 string.printf("no %s", test.name);
1849 } else {
1850 string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name);
1851 }
1852 canvas->drawString(string, 10, 20, paint);
1853 canvas->translate(0, 20);
1854 }
Cary Clarka560c472017-11-27 10:44:06 -05001855##
1856
Cary Clarkac47b882018-01-11 10:35:44 -05001857#SeeAlso encodeToData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001858
1859#Method ##
1860
1861# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001862#Subtopic Utility
1863#Populate
1864#Line # rarely called management functions ##
1865##
Cary Clarka560c472017-11-27 10:44:06 -05001866
1867#Method const char* toString(SkString* string) const
Cary Clark4855f782018-02-06 09:41:53 -05001868#In Utility
1869#Line # converts Image to machine readable form ##
Cary Clarkac47b882018-01-11 10:35:44 -05001870Appends Image description to string, including unique ID, width, height, and
1871whether the image is opaque.
Cary Clarka560c472017-11-27 10:44:06 -05001872
Cary Clarkac47b882018-01-11 10:35:44 -05001873#Param string storage for description; existing content is preserved ##
1874
1875#Return string appended with Image description ##
Cary Clarka560c472017-11-27 10:44:06 -05001876
1877#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001878#Image 4
1879 struct {
1880 const char* name;
1881 sk_sp<SkImage> image;
1882 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1883 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1884 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr) } };
1885 SkString string;
1886 SkPaint paint;
1887 for (const auto& test : tests ) {
1888 string.printf("%s: ", test.name);
1889 test.image ? (void) test.image->toString(&string) : string.append("no image");
1890 canvas->drawString(string, 10, 20, paint);
1891 canvas->translate(0, 20);
1892 }
Cary Clarka560c472017-11-27 10:44:06 -05001893##
1894
Cary Clarkac47b882018-01-11 10:35:44 -05001895#SeeAlso SkPaint::toString
Cary Clarka560c472017-11-27 10:44:06 -05001896
1897#Method ##
1898
1899# ------------------------------------------------------------------------------
1900
1901#Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const
Cary Clark4855f782018-02-06 09:41:53 -05001902#In Constructor
1903#Line # creates Image containing part of original ##
Cary Clarkac47b882018-01-11 10:35:44 -05001904Returns subset of Image. subset must be fully contained by Image dimensions().
1905The implementation may share pixels, or may copy them.
Cary Clarka560c472017-11-27 10:44:06 -05001906
Cary Clarkac47b882018-01-11 10:35:44 -05001907Returns nullptr if subset is empty, or subset is not contained by bounds, or
1908pixels in Image could not be read or copied.
Cary Clarka560c472017-11-27 10:44:06 -05001909
Cary Clarkac47b882018-01-11 10:35:44 -05001910#Param subset bounds of returned Image ##
1911
1912#Return partial or full Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001913
1914#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001915#Image 3
1916 canvas->scale(.5f, .5f);
1917 const int width = 32;
1918 const int height = 32;
1919 for (int y = 0; y < 512; y += height ) {
1920 for (int x = 0; x < 512; x += width ) {
1921 sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height}));
1922 canvas->drawImage(subset, x * 3 / 2, y * 3 / 2);
1923 }
1924 }
Cary Clarka560c472017-11-27 10:44:06 -05001925##
1926
Cary Clarkac47b882018-01-11 10:35:44 -05001927#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001928
1929#Method ##
1930
1931# ------------------------------------------------------------------------------
1932
1933#Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const
Cary Clark4855f782018-02-06 09:41:53 -05001934#In Constructor
1935#Line # creates Image matching Color_Space if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001936Returns Image backed by GPU_Texture associated with context. Returned Image is
1937compatible with Surface created with dstColorSpace. Returns original
1938Image if context and dstColorSpace match.
1939
1940Returns nullptr if context is nullptr, or if Image was created with another
1941GrContext.
Cary Clarka560c472017-11-27 10:44:06 -05001942
Cary Clark61ca7c52018-01-02 11:34:14 -05001943#Param context GPU_Context ##
Cary Clarkac47b882018-01-11 10:35:44 -05001944#Param dstColorSpace range of colors of matching Surface on GPU ##
Cary Clarka560c472017-11-27 10:44:06 -05001945
Cary Clarkac47b882018-01-11 10:35:44 -05001946#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001947
1948#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001949#Platform gpu
1950#Image 5
1951 auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void {
1952 if (nullptr == image || nullptr == context) {
1953 return;
1954 }
1955 SkPaint paint;
1956 paint.setAntiAlias(true);
1957 paint.setTextAlign(SkPaint::kCenter_Align);
1958 sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr));
1959 canvas->drawImage(texture, 0, 0);
1960 canvas->drawString(label, texture->width() / 2, texture->height() / 4, paint);
1961 };
1962 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1963 GrContext* context = canvas->getGrContext();
1964 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture,
1965 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1966 drawImage(image, context, "image");
1967 canvas->translate(image->width(), 0);
1968 drawImage(bitmapImage, context, "source");
1969 canvas->translate(-image->width(), image->height());
1970 drawImage(textureImage, context, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001971##
1972
Cary Clarkac47b882018-01-11 10:35:44 -05001973#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05001974
1975#Method ##
1976
1977# ------------------------------------------------------------------------------
1978
1979#Method sk_sp<SkImage> makeNonTextureImage() const
Cary Clark4855f782018-02-06 09:41:53 -05001980#In Constructor
1981#Line # creates Image without dependency on GPU_Texture ##
Cary Clarkac47b882018-01-11 10:35:44 -05001982Returns Raster_Image or Lazy_Image. Copies Image backed by GPU_Texture into
Cary Clark4855f782018-02-06 09:41:53 -05001983CPU memory if needed. Returns original Image if decoded in Raster_Bitmap,
Cary Clarkac47b882018-01-11 10:35:44 -05001984or if encoded in a stream.
Cary Clark61ca7c52018-01-02 11:34:14 -05001985
Cary Clarkac47b882018-01-11 10:35:44 -05001986Returns nullptr if backed by GPU_Texture and copy fails.
1987
1988#Return Raster_Image, Lazy_Image, or nullptr ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001989
1990#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001991#Image 5
1992#Platform gpu
1993 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1994 if (nullptr == image) {
1995 return;
1996 }
1997 SkPaint paint;
1998 paint.setAntiAlias(true);
1999 paint.setTextAlign(SkPaint::kCenter_Align);
2000 sk_sp<SkImage> nonTexture(image->makeNonTextureImage());
2001 canvas->drawImage(nonTexture, 0, 0);
2002 canvas->drawString(label, nonTexture->width() / 2, nonTexture->height() / 4, paint);
2003 };
2004 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2005 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2006 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2007 drawImage(image, "image");
2008 canvas->translate(image->width(), 0);
2009 drawImage(bitmapImage, "source");
2010 canvas->translate(-image->width(), image->height());
2011 drawImage(textureImage, "backEndTexture");
Cary Clark61ca7c52018-01-02 11:34:14 -05002012##
2013
Cary Clark56356312018-02-08 14:45:18 -05002014#SeeAlso makeTextureImage makeRasterImage MakeBackendTextureFromSkImage
Cary Clark61ca7c52018-01-02 11:34:14 -05002015
2016#Method ##
2017
2018# ------------------------------------------------------------------------------
2019
2020#Method sk_sp<SkImage> makeRasterImage() const
Cary Clark4855f782018-02-06 09:41:53 -05002021#In Constructor
2022#Line # creates Image compatible with Raster_Surface if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05002023Returns Raster_Image. Copies Image backed by GPU_Texture into CPU memory,
Cary Clark4855f782018-02-06 09:41:53 -05002024or decodes Image from Lazy_Image. Returns original Image if decoded in
Cary Clarkac47b882018-01-11 10:35:44 -05002025Raster_Bitmap.
Cary Clarka560c472017-11-27 10:44:06 -05002026
Cary Clarkac47b882018-01-11 10:35:44 -05002027Returns nullptr if copy, decode, or pixel read fails.
Cary Clarka560c472017-11-27 10:44:06 -05002028
Cary Clarkac47b882018-01-11 10:35:44 -05002029#Return Raster_Image, or nullptr ##
2030
Cary Clark4855f782018-02-06 09:41:53 -05002031#Bug 7479
Cary Clarka560c472017-11-27 10:44:06 -05002032#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002033#Image 5
2034#Platform gpu
2035 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2036 if (nullptr == image) {
2037 return;
2038 }
2039 SkPaint paint;
2040 paint.setAntiAlias(true);
2041 paint.setTextAlign(SkPaint::kCenter_Align);
2042 sk_sp<SkImage> raster(image->makeRasterImage());
2043 canvas->drawImage(raster, 0, 0);
2044 canvas->drawString(label, raster->width() / 2, raster->height() / 4, paint);
2045 };
2046 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2047 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2048 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2049 drawImage(image, "image");
2050 canvas->translate(image->width(), 0);
2051 drawImage(bitmapImage, "source");
2052 canvas->translate(-image->width(), image->height());
2053 drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05002054##
2055
Cary Clarkac47b882018-01-11 10:35:44 -05002056#SeeAlso isTextureBacked isLazyGenerated MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -05002057
2058#Method ##
2059
2060# ------------------------------------------------------------------------------
2061
2062#Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
2063 const SkIRect& clipBounds, SkIRect* outSubset,
2064 SkIPoint* offset) const
Cary Clark4855f782018-02-06 09:41:53 -05002065#In Constructor
2066#Line # creates filtered, clipped Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002067
Cary Clarkac47b882018-01-11 10:35:44 -05002068Creates filtered Image. filter processes original Image, potentially changing
2069color, position, and size. subset is the bounds of original Image processed
2070by filter. clipBounds is the expected bounds of the filtered Image. outSubset
2071is required storage for the actual bounds of the filtered Image. offset is
2072required storage for translation of returned Image.
Cary Clarka560c472017-11-27 10:44:06 -05002073
Cary Clarkac47b882018-01-11 10:35:44 -05002074Returns nullptr if Image could not be created. If nullptr is returned, outSubset
2075and offset are undefined.
2076
Cary Clark56356312018-02-08 14:45:18 -05002077Useful for animation of SkImageFilter that varies size from frame to frame.
2078Returned Image is created larger than required by filter so that GPU_Texture
2079can be reused with different sized effects. outSubset describes the valid bounds
2080of GPU_Texture returned. offset translates the returned Image to keep subsequent
2081animation frames aligned with respect to each other.
Cary Clarkac47b882018-01-11 10:35:44 -05002082
2083#Param filter how Image is sampled when transformed ##
Cary Clark56356312018-02-08 14:45:18 -05002084#Param subset bounds of Image processed by filter ##
2085#Param clipBounds expected bounds of filtered Image ##
2086#Param outSubset storage for returned Image bounds ##
2087#Param offset storage for returned Image translation ##
Cary Clarka560c472017-11-27 10:44:06 -05002088
Cary Clarkac47b882018-01-11 10:35:44 -05002089#Return filtered Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05002090
2091#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002092#Description
2093In each frame of the animation, filtered Image is drawn in a different location.
2094By translating canvas by returned offset, Image appears stationary.
2095##
2096#Image 5
2097#Platform gpu
2098#Duration 5
2099 sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make(
2100 -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE,
2101 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
2102 nullptr);
2103 sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr);
2104 SkIRect subset = image->bounds();
2105 SkIRect clipBounds = image->bounds();
2106 clipBounds.outset(60, 60);
2107 SkIRect outSubset;
2108 SkIPoint offset;
2109 sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds,
2110 &outSubset, &offset));
2111 SkPaint paint;
2112 paint.setAntiAlias(true);
2113 paint.setStyle(SkPaint::kStroke_Style);
2114 canvas->drawLine(0, 0, offset.fX, offset.fY, paint);
2115 canvas->translate(offset.fX, offset.fY);
2116 canvas->drawImage(filtered, 0, 0);
2117 canvas->drawRect(SkRect::MakeFromIRect(outSubset), paint);
Cary Clarka560c472017-11-27 10:44:06 -05002118##
2119
Cary Clark56356312018-02-08 14:45:18 -05002120#SeeAlso makeShader SkPaint::setImageFilter
Cary Clarka560c472017-11-27 10:44:06 -05002121
2122#Method ##
2123
2124# ------------------------------------------------------------------------------
2125
Cary Clarka560c472017-11-27 10:44:06 -05002126#Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc
2127
2128##
2129
2130# ------------------------------------------------------------------------------
2131
2132#Method static bool MakeBackendTextureFromSkImage(GrContext* context,
2133 sk_sp<SkImage> image,
2134 GrBackendTexture* backendTexture,
2135 BackendTextureReleaseProc* backendTextureReleaseProc)
Cary Clark4855f782018-02-06 09:41:53 -05002136#In Constructor
2137#Line # creates GPU_Texture from Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002138
Cary Clark56356312018-02-08 14:45:18 -05002139Creates a GrBackendTexture from the provided SkImage. Returns true and
2140stores result in backendTexture and backendTextureReleaseProc if
2141texture is created; otherwise, returns false and leaves
2142backendTexture and backendTextureReleaseProc unmodified.
Cary Clarka560c472017-11-27 10:44:06 -05002143
Cary Clark56356312018-02-08 14:45:18 -05002144Call backendTextureReleaseProc after deleting backendTexture.
2145backendTextureReleaseProc cleans up auxiliary data related to returned
2146backendTexture. The caller must delete returned backendTexture after use.
Cary Clarka560c472017-11-27 10:44:06 -05002147
Cary Clark56356312018-02-08 14:45:18 -05002148If Image is both texture backed and singly referenced, image is returned in
2149backendTexture without conversion or making a copy. Image is singly referenced
2150if its was transferred solely using std::move().
2151
2152If Image is not texture backed, returns texture with Image contents.
Cary Clarka560c472017-11-27 10:44:06 -05002153
Cary Clark61ca7c52018-01-02 11:34:14 -05002154#Param context GPU_Context ##
Cary Clark56356312018-02-08 14:45:18 -05002155#Param image Image used for texture ##
2156#Param backendTexture storage for backend texture ##
2157#Param backendTextureReleaseProc storage for clean up function ##
Cary Clarka560c472017-11-27 10:44:06 -05002158
Cary Clark56356312018-02-08 14:45:18 -05002159#Return true if backend texture was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002160
2161#Example
Cary Clark56356312018-02-08 14:45:18 -05002162#Platform gpu
2163#Height 64
2164#Function
Brian Salomon67f85842018-02-09 08:50:22 -05002165static sk_sp<SkImage> create_gpu_image(GrContext* grContext) {
2166 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
2167 auto surface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info));
2168 SkCanvas* canvas = surface->getCanvas();
2169 canvas->clear(SK_ColorWHITE);
2170 SkPaint paint;
2171 paint.setColor(SK_ColorBLACK);
2172 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
2173 return surface->makeImageSnapshot();
2174}
2175##
2176
2177void draw(SkCanvas* canvas) {
2178 GrContext* grContext = canvas->getGrContext();
2179 if (!grContext) {
2180 return;
2181 }
2182 sk_sp<SkImage> backEndImage = create_gpu_image(grContext);
2183 canvas->drawImage(backEndImage, 0, 0);
2184 GrBackendTexture texture;
2185 SkImage::BackendTextureReleaseProc proc;
2186 if (!SkImage::MakeBackendTextureFromSkImage(grContext, std::move(backEndImage),
2187 &texture, &proc)) {
2188 return;
2189 }
2190 sk_sp<SkImage> i2 = SkImage::MakeFromTexture(grContext, texture, kTopLeft_GrSurfaceOrigin,
2191 kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
2192 canvas->drawImage(i2, 30, 30);
Cary Clark56356312018-02-08 14:45:18 -05002193}
Cary Clarka560c472017-11-27 10:44:06 -05002194##
2195
Cary Clark56356312018-02-08 14:45:18 -05002196#SeeAlso MakeFromTexture makeTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002197
2198#Method ##
2199
2200# ------------------------------------------------------------------------------
2201
2202#Enum LegacyBitmapMode
Cary Clark56356312018-02-08 14:45:18 -05002203#Deprecated soon
Cary Clarka560c472017-11-27 10:44:06 -05002204#Code
2205 enum LegacyBitmapMode {
2206 kRO_LegacyBitmapMode,
Cary Clarka560c472017-11-27 10:44:06 -05002207 };
2208##
2209
Cary Clarka560c472017-11-27 10:44:06 -05002210#Const kRO_LegacyBitmapMode 0
Cary Clark56356312018-02-08 14:45:18 -05002211Returned bitmap is read-only and immutable.
Cary Clarka560c472017-11-27 10:44:06 -05002212##
Cary Clarka560c472017-11-27 10:44:06 -05002213
2214#Enum ##
2215
2216# ------------------------------------------------------------------------------
2217
Cary Clark56356312018-02-08 14:45:18 -05002218#Method bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode = kRO_LegacyBitmapMode) const
Cary Clark4855f782018-02-06 09:41:53 -05002219#In Constructor
2220#Line # returns as Raster_Bitmap ##
Cary Clarkac47b882018-01-11 10:35:44 -05002221Creates raster Bitmap with same pixels as Image. If legacyBitmapMode is
2222kRO_LegacyBitmapMode, returned bitmap is read-only and immutable.
2223Returns true if Bitmap is stored in bitmap. Returns false and resets bitmap if
2224Bitmap write did not succeed.
Cary Clarka560c472017-11-27 10:44:06 -05002225
Cary Clark3cd22cc2017-12-01 11:49:58 -05002226#Param bitmap storage for legacy Bitmap ##
Cary Clark56356312018-02-08 14:45:18 -05002227#Param legacyBitmapMode to be deprecated ##
Cary Clarka560c472017-11-27 10:44:06 -05002228
Cary Clark3cd22cc2017-12-01 11:49:58 -05002229#Return true if Bitmap was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002230
2231#Example
Cary Clark56356312018-02-08 14:45:18 -05002232#Image 4
2233#Platform gpu
Brian Salomon67f85842018-02-09 08:50:22 -05002234 SkBitmap bitImage;
2235 if (image->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2236 canvas->drawBitmap(bitImage, 0, 0);
2237 }
2238 GrContext* grContext = canvas->getGrContext();
2239 if (!grContext) {
2240 return;
2241 }
2242 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(grContext, backEndTexture,
2243 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2244 canvas->drawImage(textureImage, 45, 45);
2245 if (textureImage->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2246 canvas->drawBitmap(bitImage, 90, 90);
2247 }
Cary Clarka560c472017-11-27 10:44:06 -05002248##
2249
Cary Clark56356312018-02-08 14:45:18 -05002250#SeeAlso MakeRasterData makeRasterImage makeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002251
2252#Method ##
2253
2254# ------------------------------------------------------------------------------
2255
2256#Method bool isLazyGenerated() const
Cary Clark4855f782018-02-06 09:41:53 -05002257#In Property
2258#Line # returns if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002259Returns true if Image is backed by an image-generator or other service that creates
2260and caches its pixels or texture on-demand.
2261
Cary Clark2f466242017-12-11 16:03:17 -05002262#Return true if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002263
2264#Example
Cary Clark2f466242017-12-11 16:03:17 -05002265#Height 80
2266#Function
2267class TestImageGenerator : public SkImageGenerator {
2268public:
2269 TestImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(10, 10)) {}
2270 ~TestImageGenerator() override {}
2271protected:
2272 bool onGetPixels(const SkImageInfo& info, void* pixelPtr, size_t rowBytes,
2273 const Options& options) override {
2274 SkPMColor* pixels = static_cast<SkPMColor*>(pixelPtr);
2275 for (int y = 0; y < info.height(); ++y) {
2276 for (int x = 0; x < info.width(); ++x) {
2277 pixels[y * info.width() + x] = 0xff223344 + y * 0x000C0811;
2278 }
2279 }
2280 return true;
2281 }
2282};
2283##
2284void draw(SkCanvas* canvas) {
2285 auto gen = std::unique_ptr<TestImageGenerator>(new TestImageGenerator());
2286 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
2287 SkString lazy(image->isLazyGenerated() ? "is lazy" : "not lazy");
2288 canvas->scale(8, 8);
2289 canvas->drawImage(image, 0, 0, nullptr);
2290 SkPaint paint;
2291 paint.setTextSize(4);
2292 canvas->drawString(lazy, 2, 5, paint);
2293}
Cary Clarka560c472017-11-27 10:44:06 -05002294##
2295
Cary Clarkf5404bb2018-01-05 12:10:09 -05002296#Example
2297#Image 5
2298#Platform gpu
2299void draw(SkCanvas* canvas) {
2300 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2301 if (nullptr == image) {
2302 return;
2303 }
2304 SkPaint paint;
2305 paint.setAntiAlias(true);
2306 paint.setTextAlign(SkPaint::kCenter_Align);
2307 canvas->drawImage(image, 0, 0);
2308 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
2309 canvas->drawString(
2310 image->isLazyGenerated() ? "is lazily generated" : "not lazily generated",
2311 image->width() / 2, image->height() * 3 / 4, paint);
2312 };
2313 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2314 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2315 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2316 drawImage(image, "image");
2317 canvas->translate(image->width(), 0);
2318 drawImage(bitmapImage, "source");
2319 canvas->translate(-image->width(), image->height());
2320 drawImage(textureImage, "backEndTexture");
2321}
2322##
2323
Cary Clarkac47b882018-01-11 10:35:44 -05002324#SeeAlso isTextureBacked MakeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002325
2326#Method ##
2327
2328# ------------------------------------------------------------------------------
2329
2330#Method sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target,
2331 SkTransferFunctionBehavior premulBehavior) const
Cary Clark4855f782018-02-06 09:41:53 -05002332#In Constructor
2333#Line # creates Image matching Color_Space if possible ##
Cary Clarka560c472017-11-27 10:44:06 -05002334
Cary Clarkac47b882018-01-11 10:35:44 -05002335Creates Image in target Color_Space.
2336Returns nullptr if Image could not be created.
Cary Clarka560c472017-11-27 10:44:06 -05002337
Cary Clarkac47b882018-01-11 10:35:44 -05002338Returns original Image if it is in target Color_Space.
2339Otherwise, converts pixels from Image Color_Space to target Color_Space.
2340If Image colorSpace returns nullptr, Image Color_Space is assumed to be sRGB.
2341
2342SkTransferFunctionBehavior is to be deprecated.
2343
2344Set premulBehavior to SkTransferFunctionBehavior::kRespect to convert Image
2345pixels to a linear space, before converting to destination Color_Type
Cary Clarka560c472017-11-27 10:44:06 -05002346and Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -05002347
Cary Clarkac47b882018-01-11 10:35:44 -05002348Set premulBehavior to SkTransferFunctionBehavior::kIgnore to treat Image
2349pixels as linear, when converting to destination Color_Type
2350and Color_Space, ignoring pixel encoding.
Cary Clarka560c472017-11-27 10:44:06 -05002351
Cary Clarkac47b882018-01-11 10:35:44 -05002352#Param target Color_Space describing color range of returned Image ##
2353#Param premulBehavior one of: SkTransferFunctionBehavior::kRespect,
2354 SkTransferFunctionBehavior::kIgnore
Cary Clarka560c472017-11-27 10:44:06 -05002355##
2356
Cary Clarkac47b882018-01-11 10:35:44 -05002357#Return created Image in target Color_Space ##
2358
2359#Example
2360#Image 5
2361#Set sRGB
2362 sk_sp<SkColorSpace> normalColorSpace = SkColorSpace::MakeRGB(
2363 SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kSRGB_Gamut);
2364 sk_sp<SkColorSpace> wackyColorSpace = normalColorSpace->makeColorSpin();
2365 for (auto colorSpace : { normalColorSpace, wackyColorSpace } ) {
2366 for (auto transfer : { SkTransferFunctionBehavior::kRespect,
2367 SkTransferFunctionBehavior::kIgnore } ) {
2368 sk_sp<SkImage> colorSpaced = image->makeColorSpace(colorSpace, transfer);
2369 canvas->drawImage(colorSpaced, 0, 0);
2370 canvas->translate(128, 0);
2371 }
2372 canvas->translate(-256, 128);
2373 }
2374##
2375
2376#SeeAlso MakeFromPixture MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05002377
2378#Method ##
2379
2380#Class SkImage ##
2381
2382#Topic Image ##