blob: 571dc49996583f44e3eca867374e84acc9990b8f [file] [log] [blame]
Cary Clarka560c472017-11-27 10:44:06 -05001#Topic Image
2#Alias Image_Reference
3
4#Class SkImage
5
Cary Clark61ca7c52018-01-02 11:34:14 -05006Image describes a two dimensional array of pixels to draw. The pixels may be
Cary Clark4855f782018-02-06 09:41:53 -05007decoded in a Raster_Bitmap, encoded in a Picture or compressed data stream,
Cary Clark61ca7c52018-01-02 11:34:14 -05008or located in GPU memory as a GPU_Texture.
9
10Image cannot be modified after it is created. Image may allocate additional
11storage as needed; for instance, an encoded Image may decode when drawn.
12
13Image width and height are greater than zero. Creating an Image with zero width
14or height returns Image equal to nullptr.
15
16Image may be created from Bitmap, Pixmap, Surface, Picture, encoded streams,
17GPU_Texture, YUV_ColorSpace data, or hardware buffer. Encoded streams supported
Cary Clark4855f782018-02-06 09:41:53 -050018include BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP. Supported encoding details
Cary Clark61ca7c52018-01-02 11:34:14 -050019vary with platform.
20
Cary Clark08895c42018-02-01 09:37:32 -050021#Subtopic Raster_Image
Cary Clark61ca7c52018-01-02 11:34:14 -050022#Alias Raster_Image
Cary Clark4855f782018-02-06 09:41:53 -050023#Line # pixels decoded in Raster_Bitmap ##
24Raster_Image pixels are decoded in a Raster_Bitmap. These pixels may be read
Cary Clark61ca7c52018-01-02 11:34:14 -050025directly and in most cases written to, although edited pixels may not be drawn
26if Image has been copied internally.
27##
28
Cary Clark08895c42018-02-01 09:37:32 -050029#Subtopic Texture_Image
30#Line # pixels located on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -050031Texture_Image are located on GPU and pixels are not accessible. Texture_Image
32are allocated optimally for best performance. Raster_Image may
33be drawn to GPU_Surface, but pixels are uploaded from CPU to GPU downgrading
34performance.
35##
36
Cary Clark08895c42018-02-01 09:37:32 -050037#Subtopic Lazy_Image
38#Line # deferred pixel buffer ##
Cary Clark61ca7c52018-01-02 11:34:14 -050039Lazy_Image defer allocating buffer for Image pixels and decoding stream until
40Image is drawn. Lazy_Image caches result if possible to speed up repeated
41drawing.
42##
Cary Clarka560c472017-11-27 10:44:06 -050043
Cary Clark682c58d2018-05-16 07:07:07 -040044#Subtopic Overview
Cary Clark08895c42018-02-01 09:37:32 -050045#Populate
46##
Cary Clarka560c472017-11-27 10:44:06 -050047
Cary Clark682c58d2018-05-16 07:07:07 -040048#Subtopic Related_Function
Cary Clark08895c42018-02-01 09:37:32 -050049#Populate
Cary Clarka560c472017-11-27 10:44:06 -050050##
51
Cary Clark4855f782018-02-06 09:41:53 -050052#Subtopic Constructor
Cary Clark08895c42018-02-01 09:37:32 -050053#Populate
54##
Cary Clark5081eed2018-01-22 07:55:48 -050055
Cary Clark4855f782018-02-06 09:41:53 -050056#Subtopic Member_Function
Cary Clark08895c42018-02-01 09:37:32 -050057#Populate
58##
Cary Clarka560c472017-11-27 10:44:06 -050059
Cary Clarka560c472017-11-27 10:44:06 -050060# ------------------------------------------------------------------------------
61
62#Method static sk_sp<SkImage> MakeRasterCopy(const SkPixmap& pixmap)
Cary Clark4855f782018-02-06 09:41:53 -050063#In Constructor
64#Line # creates Image from Pixmap and copied pixels ##
Cary Clark2f466242017-12-11 16:03:17 -050065Creates Image from Pixmap and copy of pixels. Since pixels are copied, Pixmap
66pixels may be modified or deleted without affecting Image.
Cary Clarka560c472017-11-27 10:44:06 -050067
Cary Clark3cd22cc2017-12-01 11:49:58 -050068Image is returned if Pixmap is valid. Valid Pixmap parameters include:
69dimensions are greater than zero;
70each dimension fits in 29 bits;
71Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
72row bytes are large enough to hold one row of pixels;
73pixel address is not nullptr.
74
75#Param pixmap Image_Info, pixel address, and row bytes ##
76
77#Return copy of Pixmap pixels, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -050078
79#Example
Cary Clark2f466242017-12-11 16:03:17 -050080#Height 50
81#Description
82Draw a five by five bitmap, and draw a copy in an Image. Editing the pixmap
83alters the bitmap draw, but does not alter the Image draw since the Image
84contains a copy of the pixels.
85##
86 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
87 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
88 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
89 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
90 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
91 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
92 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
93 SkBitmap bitmap;
94 bitmap.installPixels(pixmap);
95 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
96 *pixmap.writable_addr8(2, 2) = 0x00;
97 canvas->scale(10, 10);
98 canvas->drawBitmap(bitmap, 0, 0);
99 canvas->drawImage(image, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500100##
101
Cary Clark3cd22cc2017-12-01 11:49:58 -0500102#SeeAlso MakeRasterData MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500103
104#Method ##
105
106# ------------------------------------------------------------------------------
107
Cary Clarkcc45cc72018-05-15 16:06:12 -0400108#Method static sk_sp<SkImage> MakeRasterData(const SkImageInfo& info, sk_sp<SkData> pixels, size_t rowBytes)
Cary Clark4855f782018-02-06 09:41:53 -0500109#In Constructor
110#Line # creates Image from Image_Info and shared pixels ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500111Creates Image from Image_Info, sharing pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500112
Cary Clark3cd22cc2017-12-01 11:49:58 -0500113Image is returned if Image_Info is valid. Valid Image_Info parameters include:
114dimensions are greater than zero;
115each dimension fits in 29 bits;
116Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
117rowBytes are large enough to hold one row of pixels;
118pixels is not nullptr, and contains enough data for Image.
119
120#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
121#Param pixels address or pixel storage ##
122#Param rowBytes size of pixel row or larger ##
123
124#Return Image sharing pixels, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500125
126#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500127#Image 3
128 size_t rowBytes = image->width() * SkColorTypeBytesPerPixel(kRGBA_8888_SkColorType);
129 sk_sp<SkData> data = SkData::MakeUninitialized(rowBytes * image->height());
Cary Clark682c58d2018-05-16 07:07:07 -0400130 SkImageInfo dstInfo = SkImageInfo::MakeN32(image->width(), image->height(),
Cary Clark0c5f5462017-12-15 11:21:51 -0500131 kPremul_SkAlphaType);
132 image->readPixels(dstInfo, data->writable_data(), rowBytes, 0, 0, SkImage::kAllow_CachingHint);
133 sk_sp<SkImage> raw = SkImage::MakeRasterData(dstInfo.makeColorType(kRGBA_8888_SkColorType),
134 data, rowBytes);
135 canvas->drawImage(image, 0, 0);
136 canvas->drawImage(raw.get(), 128, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500137##
138
Cary Clark3cd22cc2017-12-01 11:49:58 -0500139#SeeAlso MakeRasterCopy MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500140
141#Method ##
142
143# ------------------------------------------------------------------------------
144
Cary Clark3cd22cc2017-12-01 11:49:58 -0500145#Typedef void* ReleaseContext
Cary Clark682c58d2018-05-16 07:07:07 -0400146#Line # parameter type for MakeFromRaster ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500147Caller data passed to RasterReleaseProc; may be nullptr.
148
149#SeeAlso MakeFromRaster RasterReleaseProc
150
151##
152
Cary Clarka560c472017-11-27 10:44:06 -0500153#Typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext)
Cary Clark682c58d2018-05-16 07:07:07 -0400154#Line # parameter type for MakeFromRaster ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500155Function called when Image no longer shares pixels. ReleaseContext is
Cary Clark682c58d2018-05-16 07:07:07 -0400156provided by caller when Image is created, and may be nullptr.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500157
158#SeeAlso ReleaseContext MakeFromRaster
159
Cary Clarka560c472017-11-27 10:44:06 -0500160##
161
162#Method static sk_sp<SkImage> MakeFromRaster(const SkPixmap& pixmap,
163 RasterReleaseProc rasterReleaseProc,
164 ReleaseContext releaseContext)
Cary Clark4855f782018-02-06 09:41:53 -0500165#In Constructor
166#Line # creates Image from Pixmap, with release ##
Cary Clarka560c472017-11-27 10:44:06 -0500167
Cary Clark0c5f5462017-12-15 11:21:51 -0500168Creates Image from pixmap, sharing Pixmap pixels. Pixels must remain valid and
Cary Clark3cd22cc2017-12-01 11:49:58 -0500169unchanged until rasterReleaseProc is called. rasterReleaseProc is passed
170releaseContext when Image is deleted or no longer refers to pixmap pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500171
Cary Clark0c5f5462017-12-15 11:21:51 -0500172Pass nullptr for rasterReleaseProc to share Pixmap without requiring a callback
173when Image is released. Pass nullptr for releaseContext if rasterReleaseProc
174does not require state.
175
Cary Clark3cd22cc2017-12-01 11:49:58 -0500176Image is returned if pixmap is valid. Valid Pixmap parameters include:
177dimensions are greater than zero;
178each dimension fits in 29 bits;
179Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
180row bytes are large enough to hold one row of pixels;
181pixel address is not nullptr.
182
183#Param pixmap Image_Info, pixel address, and row bytes ##
Cary Clark0c5f5462017-12-15 11:21:51 -0500184#Param rasterReleaseProc function called when pixels can be released; or nullptr ##
185#Param releaseContext state passed to rasterReleaseProc; or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500186
Cary Clark0c5f5462017-12-15 11:21:51 -0500187#Return Image sharing pixmap ##
Cary Clarka560c472017-11-27 10:44:06 -0500188
189#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500190#Function
191static void releaseProc(const void* pixels, SkImage::ReleaseContext context) {
192 int* countPtr = static_cast<int*>(context);
193 *countPtr += 1;
194}
195##
196
197void draw(SkCanvas* canvas) {
198 SkColor color = 0;
199 SkPixmap pixmap(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), &color, 4);
200 int releaseCount = 0;
201 sk_sp<SkImage> image(SkImage::MakeFromRaster(pixmap, releaseProc, &releaseCount));
202 SkDebugf("before reset: %d\n", releaseCount);
203 image.reset();
204 SkDebugf("after reset: %d\n", releaseCount);
205}
206#StdOut
207before reset: 0
208after reset: 1
209##
Cary Clarka560c472017-11-27 10:44:06 -0500210##
211
Cary Clark3cd22cc2017-12-01 11:49:58 -0500212#SeeAlso MakeRasterCopy MakeRasterData MakeFromGenerator RasterReleaseProc ReleaseContext
Cary Clarka560c472017-11-27 10:44:06 -0500213
214#Method ##
215
216# ------------------------------------------------------------------------------
217
218#Method static sk_sp<SkImage> MakeFromBitmap(const SkBitmap& bitmap)
Cary Clark4855f782018-02-06 09:41:53 -0500219#In Constructor
220#Line # creates Image from Bitmap, sharing or copying pixels ##
Cary Clark682c58d2018-05-16 07:07:07 -0400221Creates Image from bitmap, sharing or copying bitmap pixels. If the bitmap
Cary Clark3cd22cc2017-12-01 11:49:58 -0500222is marked immutable, and its pixel memory is shareable, it may be shared
223instead of copied.
Cary Clarka560c472017-11-27 10:44:06 -0500224
Cary Clark3cd22cc2017-12-01 11:49:58 -0500225Image is returned if bitmap is valid. Valid Bitmap parameters include:
226dimensions are greater than zero;
227each dimension fits in 29 bits;
228Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
229row bytes are large enough to hold one row of pixels;
230pixel address is not nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500231
Cary Clark3cd22cc2017-12-01 11:49:58 -0500232#Param bitmap Image_Info, row bytes, and pixels ##
233
234#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500235
236#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500237#Description
238The first Bitmap is shared; writing to the pixel memory changes the first
239Image.
240The second Bitmap is marked immutable, and is copied; writing to the pixel
241memory does not alter the second Image.
242##
243#Height 50
244 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
245 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
246 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
247 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
248 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
249 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
250 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
251 SkBitmap bitmap;
252 bitmap.installPixels(pixmap);
253 sk_sp<SkImage> image1 = SkImage::MakeFromBitmap(bitmap);
254 bitmap.setImmutable();
255 sk_sp<SkImage> image2 = SkImage::MakeFromBitmap(bitmap);
256 *pixmap.writable_addr8(2, 2) = 0x00;
257 canvas->scale(10, 10);
258 canvas->drawImage(image1, 0, 0);
259 canvas->drawImage(image2, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500260##
261
Cary Clark3cd22cc2017-12-01 11:49:58 -0500262#SeeAlso MakeFromRaster MakeRasterCopy MakeFromGenerator MakeRasterData
Cary Clarka560c472017-11-27 10:44:06 -0500263
264#Method ##
265
266# ------------------------------------------------------------------------------
267
268#Method static sk_sp<SkImage> MakeFromGenerator(std::unique_ptr<SkImageGenerator> imageGenerator,
269 const SkIRect* subset = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500270#In Constructor
271#Line # creates Image from a stream of data ##
Cary Clarka560c472017-11-27 10:44:06 -0500272
Cary Clark0c5f5462017-12-15 11:21:51 -0500273Creates Image from data returned by imageGenerator. Generated data is owned by Image and may not
274be shared or accessed.
Cary Clarka560c472017-11-27 10:44:06 -0500275
Cary Clark0c5f5462017-12-15 11:21:51 -0500276subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
277otherwise, subset must be contained by image bounds.
278
279Image is returned if generator data is valid. Valid data parameters vary by type of data
280and platform.
Cary Clarka560c472017-11-27 10:44:06 -0500281
Cary Clark3cd22cc2017-12-01 11:49:58 -0500282imageGenerator may wrap Picture data, codec data, or custom data.
283
284#Param imageGenerator stock or custom routines to retrieve Image ##
285#Param subset bounds of returned Image; may be nullptr ##
286
287#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500288
289#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500290#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500291#Description
292The generator returning Picture cannot be shared; std::move transfers ownership to generated Image.
293##
294 SkPictureRecorder recorder;
295 recorder.beginRecording(100, 100)->drawColor(SK_ColorRED);
296 auto picture = recorder.finishRecordingAsPicture();
297 auto gen = SkImageGenerator::MakeFromPicture({100, 100}, picture, nullptr, nullptr,
298 SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB());
299 sk_sp<SkImage> image = SkImage::MakeFromGenerator(std::move(gen));
300 canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500301##
302
Cary Clark3cd22cc2017-12-01 11:49:58 -0500303#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500304
305#Method ##
306
307# ------------------------------------------------------------------------------
308
309#Method static sk_sp<SkImage> MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500310#In Constructor
311#Line # creates Image from encoded data ##
Cary Clark682c58d2018-05-16 07:07:07 -0400312Creates Image from encoded data.
Cary Clark0c5f5462017-12-15 11:21:51 -0500313subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
314otherwise, subset must be contained by image bounds.
Cary Clarka560c472017-11-27 10:44:06 -0500315
Cary Clark3cd22cc2017-12-01 11:49:58 -0500316Image is returned if format of the encoded data is recognized and supported.
Cary Clark4855f782018-02-06 09:41:53 -0500317Recognized formats vary by platform.
Cary Clarka560c472017-11-27 10:44:06 -0500318
Cary Clark3cd22cc2017-12-01 11:49:58 -0500319#Param encoded data of Image to decode ##
320#Param subset bounds of returned Image; may be nullptr ##
321
322#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500323
Cary Clark61ca7c52018-01-02 11:34:14 -0500324#Example
325#Image 3
326int x = 0;
327for (int quality : { 100, 50, 10, 1} ) {
328 sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, quality);
329 sk_sp<SkImage> image = SkImage::MakeFromEncoded(encodedData);
330 canvas->drawImage(image, x, 0);
331 x += 64;
332}
Cary Clarka560c472017-11-27 10:44:06 -0500333##
334
Cary Clark3cd22cc2017-12-01 11:49:58 -0500335#SeeAlso MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500336
337#Method ##
338
339# ------------------------------------------------------------------------------
340
341#Typedef void (*TextureReleaseProc)(ReleaseContext releaseContext)
Cary Clark682c58d2018-05-16 07:07:07 -0400342#Line # parameter type for MakeFromTexture ##
343User function called when supplied texture may be deleted.
344#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -0500345##
346
347#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
348 const GrBackendTexture& backendTexture,
349 GrSurfaceOrigin origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500350 SkColorType colorType,
351 SkAlphaType alphaType,
352 sk_sp<SkColorSpace> colorSpace)
Cary Clarkf895a422018-02-27 09:54:21 -0500353#In Constructor
354#Line # creates Image from GPU_Texture ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500355Creates Image from GPU_Texture associated with context. Caller is responsible for
356managing the lifetime of GPU_Texture.
357
358Image is returned if format of backendTexture is recognized and supported.
359Recognized formats vary by GPU back-end.
360
361#Param context GPU_Context ##
362#Param backendTexture texture residing on GPU ##
363#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500364#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500365##
Cary Clark681287e2018-03-16 11:34:15 -0400366#Param alphaType one of: #list_of_alpha_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500367##
368#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500369
370#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500371
372#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500373#Image 3
374#Platform gpu
375#Height 128
376#Description
377A back-end texture has been created and uploaded to the GPU outside of this example.
378##
379GrContext* context = canvas->getGrContext();
380if (!context) {
381 return;
382}
383canvas->scale(.25f, .25f);
384int x = 0;
385for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500386 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark56356312018-02-08 14:45:18 -0500387 origin, kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
Cary Clark0c5f5462017-12-15 11:21:51 -0500388 canvas->drawImage(image, x, 0);
389x += 512;
390}
Cary Clarka560c472017-11-27 10:44:06 -0500391##
392
Cary Clark3cd22cc2017-12-01 11:49:58 -0500393#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500394
395#Method ##
396
397# ------------------------------------------------------------------------------
398
399#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
400 const GrBackendTexture& backendTexture,
401 GrSurfaceOrigin origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500402 SkColorType colorType,
Cary Clarka560c472017-11-27 10:44:06 -0500403 SkAlphaType alphaType,
404 sk_sp<SkColorSpace> colorSpace,
405 TextureReleaseProc textureReleaseProc,
406 ReleaseContext releaseContext)
407
Cary Clark61ca7c52018-01-02 11:34:14 -0500408Creates Image from GPU_Texture associated with context. GPU_Texture must stay
Cary Clark3cd22cc2017-12-01 11:49:58 -0500409valid and unchanged until textureReleaseProc is called. textureReleaseProc is
410passed releaseContext when Image is deleted or no longer refers to texture.
Cary Clarka560c472017-11-27 10:44:06 -0500411
Cary Clark3cd22cc2017-12-01 11:49:58 -0500412Image is returned if format of backendTexture is recognized and supported.
413Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500414
Cary Clark3cd22cc2017-12-01 11:49:58 -0500415#Param context GPU_Context ##
416#Param backendTexture texture residing on GPU ##
417#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500418#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500419##
Cary Clark681287e2018-03-16 11:34:15 -0400420#Param alphaType one of: #list_of_alpha_types#
Cary Clark3cd22cc2017-12-01 11:49:58 -0500421##
Cary Clark61ca7c52018-01-02 11:34:14 -0500422#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500423#Param textureReleaseProc function called when texture can be released ##
424#Param releaseContext state passed to textureReleaseProc ##
425
426#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500427
Cary Clark0c5f5462017-12-15 11:21:51 -0500428#ToDo
429This doesn't do anything clever with TextureReleaseProc because it may not get called
Cary Clark61ca7c52018-01-02 11:34:14 -0500430fwithin the lifetime of the example
Cary Clark0c5f5462017-12-15 11:21:51 -0500431##
432
Cary Clarka560c472017-11-27 10:44:06 -0500433#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500434#Platform gpu
435#Image 4
Cary Clark0c5f5462017-12-15 11:21:51 -0500436GrContext* context = canvas->getGrContext();
437if (!context) {
438 return;
439}
Cary Clarkac47b882018-01-11 10:35:44 -0500440auto debugster = [](SkImage::ReleaseContext releaseContext) -> void {
441 *((int *) releaseContext) += 128;
Cary Clark0c5f5462017-12-15 11:21:51 -0500442};
443int x = 0;
444for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500445 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark61ca7c52018-01-02 11:34:14 -0500446 origin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr, debugster, &x);
Cary Clark0c5f5462017-12-15 11:21:51 -0500447 canvas->drawImage(image, x, 0);
448 x += 128;
449}
Cary Clarka560c472017-11-27 10:44:06 -0500450##
451
Cary Clark3cd22cc2017-12-01 11:49:58 -0500452#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500453
454#Method ##
455
456# ------------------------------------------------------------------------------
457
458#Method static sk_sp<SkImage> MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> data,
459 bool buildMips,
Brian Osman584b5012018-04-13 15:48:26 -0400460 SkColorSpace* dstColorSpace,
461 bool limitToMaxTextureSize = false)
Cary Clark4855f782018-02-06 09:41:53 -0500462#In Constructor
463#Line # creates Image from encoded data, and uploads to GPU ##
Cary Clarka560c472017-11-27 10:44:06 -0500464
Cary Clark682c58d2018-05-16 07:07:07 -0400465Creates Image from encoded data. Image is uploaded to GPU back-end using context.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500466
467Created Image is available to other GPU contexts, and is available across thread
Cary Clark682c58d2018-05-16 07:07:07 -0400468boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
Cary Clark3cd22cc2017-12-01 11:49:58 -0500469share resources.
470
471When Image is no longer referenced, context releases texture memory
Cary Clarka560c472017-11-27 10:44:06 -0500472asynchronously.
Cary Clarka560c472017-11-27 10:44:06 -0500473
Cary Clark3cd22cc2017-12-01 11:49:58 -0500474Texture decoded from data is uploaded to match Surface created with
475dstColorSpace. Color_Space of Image is determined by encoded data.
Cary Clarka560c472017-11-27 10:44:06 -0500476
Cary Clark3cd22cc2017-12-01 11:49:58 -0500477Image is returned if format of data is recognized and supported, and if context
478supports moving resources. Recognized formats vary by platform and GPU back-end.
479
Cary Clark61ca7c52018-01-02 11:34:14 -0500480Image is returned using MakeFromEncoded if context is nullptr or does not support
481moving resources between contexts.
482
Cary Clark3cd22cc2017-12-01 11:49:58 -0500483#Param context GPU_Context ##
484#Param data Image to decode ##
485#Param buildMips create Image as Mip_Map if true ##
486#Param dstColorSpace range of colors of matching Surface on GPU ##
Brian Osman584b5012018-04-13 15:48:26 -0400487#Param limitToMaxTextureSize downscale image to GPU maximum texture size, if necessary ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500488
489#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500490
491#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500492#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500493#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500494GrContext* context = canvas->getGrContext();
495sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, 100);
496sk_sp<SkImage> image = SkImage::MakeCrossContextFromEncoded(context,
497 encodedData, false, nullptr);
498canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500499##
500
Cary Clark3cd22cc2017-12-01 11:49:58 -0500501#SeeAlso MakeCrossContextFromPixmap
502
503#Method ##
504
505# ------------------------------------------------------------------------------
506
507#Method static sk_sp<SkImage> MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap,
508 bool buildMips,
Brian Osman584b5012018-04-13 15:48:26 -0400509 SkColorSpace* dstColorSpace,
510 bool limitToMaxTextureSize = false)
Cary Clark4855f782018-02-06 09:41:53 -0500511#In Constructor
512#Line # creates Image from Pixmap, and uploads to GPU ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500513
Cary Clark682c58d2018-05-16 07:07:07 -0400514Creates Image from pixmap. Image is uploaded to GPU back-end using context.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500515
516Created Image is available to other GPU contexts, and is available across thread
Cary Clark682c58d2018-05-16 07:07:07 -0400517boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
Cary Clark3cd22cc2017-12-01 11:49:58 -0500518share resources.
519
520When Image is no longer referenced, context releases texture memory
521asynchronously.
522
523Texture created from pixmap is uploaded to match Surface created with
524dstColorSpace. Color_Space of Image is determined by pixmap.colorSpace().
525
Cary Clark682c58d2018-05-16 07:07:07 -0400526Image is returned referring to GPU back-end if context is not nullptr,
Cary Clark61ca7c52018-01-02 11:34:14 -0500527format of data is recognized and supported, and if context supports moving
528resources between contexts. Otherwise, pixmap pixel data is copied and Image
529as returned in raster format if possible; nullptr may be returned.
530Recognized GPU formats vary by platform and GPU back-end.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500531
532#Param context GPU_Context ##
533#Param pixmap Image_Info, pixel address, and row bytes ##
534#Param buildMips create Image as Mip_Map if true ##
535#Param dstColorSpace range of colors of matching Surface on GPU ##
Brian Osman584b5012018-04-13 15:48:26 -0400536#Param limitToMaxTextureSize downscale image to GPU maximum texture size, if necessary ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500537
538#Return created Image, or nullptr ##
539
540#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500541#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500542#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500543GrContext* context = canvas->getGrContext();
544SkPixmap pixmap;
545if (source.peekPixels(&pixmap)) {
546 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(context, pixmap,
547 false, nullptr);
548 canvas->drawImage(image, 0, 0);
549}
Cary Clark3cd22cc2017-12-01 11:49:58 -0500550##
551
552#SeeAlso MakeCrossContextFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500553
554#Method ##
555
556# ------------------------------------------------------------------------------
557
558#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
559 const GrBackendTexture& backendTexture,
560 GrSurfaceOrigin surfaceOrigin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500561 SkColorType colorType,
562 SkAlphaType alphaType = kPremul_SkAlphaType,
563 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500564#In Constructor
565#Line # creates Image from GPU_Texture, managed internally ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500566Creates Image from backendTexture associated with context. backendTexture and
567returned Image are managed internally, and are released when no longer needed.
Cary Clarka560c472017-11-27 10:44:06 -0500568
Cary Clark3cd22cc2017-12-01 11:49:58 -0500569Image is returned if format of backendTexture is recognized and supported.
570Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500571
Cary Clark3cd22cc2017-12-01 11:49:58 -0500572#Param context GPU_Context ##
573#Param backendTexture texture residing on GPU ##
574#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500575#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500576##
Cary Clark681287e2018-03-16 11:34:15 -0400577#Param alphaType one of: #list_of_alpha_types#
Cary Clark3cd22cc2017-12-01 11:49:58 -0500578##
Cary Clark61ca7c52018-01-02 11:34:14 -0500579#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500580
581#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500582
583#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500584#Image 5
585#Platform gpu
Cary Clark61ca7c52018-01-02 11:34:14 -0500586 if (!canvas->getGrContext()) {
587 return;
588 }
589 canvas->scale(.5f, .5f);
590 canvas->clear(0x7f3f5f7f);
591 int x = 0, y = 0;
592 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
593 for (auto alpha : { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType } ) {
594 sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(canvas->getGrContext(),
Cary Clark682c58d2018-05-16 07:07:07 -0400595 backEndTexture, origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500596 kRGBA_8888_SkColorType, alpha);
597 canvas->drawImage(image, x, y);
598 x += 160;
599 }
600 x -= 160 * 3;
601 y += 256;
602 }
Cary Clarka560c472017-11-27 10:44:06 -0500603##
604
Cary Clark61ca7c52018-01-02 11:34:14 -0500605#SeeAlso MakeFromTexture MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500606
607#Method ##
608
609# ------------------------------------------------------------------------------
610
611#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400612 const GrBackendTexture yuvTextures[3],
Cary Clarka560c472017-11-27 10:44:06 -0500613 GrSurfaceOrigin surfaceOrigin,
614 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500615#In Constructor
616#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500617
Brian Salomon6a426c12018-03-15 12:16:02 -0400618Creates Image from copy of yuvTextures, an array of textures on GPU.
619yuvTextures contain pixels for YUV planes of Image. Returned Image has the dimensions
620yuvTextures[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500621
Cary Clark61ca7c52018-01-02 11:34:14 -0500622#Param context GPU_Context ##
623#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
624 kRec709_SkYUVColorSpace
625##
Brian Salomon6a426c12018-03-15 12:16:02 -0400626#Param yuvTextures array of YUV textures on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500627#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
628#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500629
Cary Clark61ca7c52018-01-02 11:34:14 -0500630#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500631
Cary Clark61ca7c52018-01-02 11:34:14 -0500632# seems too complicated to create an example for this
633#ToDo
634should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500635##
636
Cary Clark61ca7c52018-01-02 11:34:14 -0500637#NoExample
638##
639
640#SeeAlso MakeFromNV12TexturesCopy
641
642#Method ##
643
644# ------------------------------------------------------------------------------
645
Cary Clarka560c472017-11-27 10:44:06 -0500646#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
647 SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400648 const GrBackendTexture nv12Textures[2],
Cary Clarka560c472017-11-27 10:44:06 -0500649 GrSurfaceOrigin surfaceOrigin,
650 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500651#In Constructor
Brian Salomon6a426c12018-03-15 12:16:02 -0400652#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500653
Cary Clark681287e2018-03-16 11:34:15 -0400654Creates Image from copy of nv12Textures, an array of textures on GPU.
Brian Salomon6a426c12018-03-15 12:16:02 -0400655nv12Textures[0] contains pixels for YUV_Component_Y plane.
656nv12Textures[1] contains pixels for YUV_Component_U plane,
Cary Clark61ca7c52018-01-02 11:34:14 -0500657followed by pixels for YUV_Component_V plane.
Cary Clark681287e2018-03-16 11:34:15 -0400658Returned Image has the dimensions nv12Textures[2].
659yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500660
Cary Clark61ca7c52018-01-02 11:34:14 -0500661#Param context GPU_Context ##
662#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
663 kRec709_SkYUVColorSpace
664##
Brian Salomon6a426c12018-03-15 12:16:02 -0400665#Param nv12Textures array of YUV textures on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500666#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
667#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500668
Cary Clark61ca7c52018-01-02 11:34:14 -0500669#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500670
Cary Clark61ca7c52018-01-02 11:34:14 -0500671# seems too complicated to create an example for this
672#ToDo
673should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500674##
675
Cary Clark61ca7c52018-01-02 11:34:14 -0500676#NoExample
677##
678
679#SeeAlso MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500680
681#Method ##
682
683# ------------------------------------------------------------------------------
684
Cary Clark4855f782018-02-06 09:41:53 -0500685# currently uncalled by any test or client ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500686#Bug 7424
Cary Clark61ca7c52018-01-02 11:34:14 -0500687
Cary Clark56356312018-02-08 14:45:18 -0500688#EnumClass BitDepth
Cary Clark682c58d2018-05-16 07:07:07 -0400689#Line # options for MakeFromPicture ##
Cary Clarka560c472017-11-27 10:44:06 -0500690#Code
Cary Clark61ca7c52018-01-02 11:34:14 -0500691 enum class BitDepth {
Cary Clarka560c472017-11-27 10:44:06 -0500692 kU8,
693 kF16,
694 };
695##
696
697#Const kU8 0
Cary Clark682c58d2018-05-16 07:07:07 -0400698#Line # uses 8-bit unsigned int per Color component ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500699Use 8 bits per Color_ARGB component using unsigned integer format.
Cary Clarka560c472017-11-27 10:44:06 -0500700##
701#Const kF16 1
Cary Clark682c58d2018-05-16 07:07:07 -0400702#Line # uses 16-bit float per Color component ##
703Use 16 bits per Color_ARGB component using half-precision floating point format.
Cary Clarka560c472017-11-27 10:44:06 -0500704##
705
Cary Clark61ca7c52018-01-02 11:34:14 -0500706#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500707##
708
Cary Clark61ca7c52018-01-02 11:34:14 -0500709#SeeAlso MakeFromPicture
Cary Clarka560c472017-11-27 10:44:06 -0500710
Cary Clark56356312018-02-08 14:45:18 -0500711#EnumClass ##
Cary Clarka560c472017-11-27 10:44:06 -0500712
713# ------------------------------------------------------------------------------
714
715#Method static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
716 const SkMatrix* matrix, const SkPaint* paint,
717 BitDepth bitDepth,
718 sk_sp<SkColorSpace> colorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500719#In Constructor
720#Line # creates Image from Picture ##
Cary Clarka560c472017-11-27 10:44:06 -0500721
Cary Clark61ca7c52018-01-02 11:34:14 -0500722Creates Image from picture. Returned Image width and height are set by dimensions.
723Image draws picture with matrix and paint, set to bitDepth and colorSpace.
Cary Clarka560c472017-11-27 10:44:06 -0500724
Cary Clark61ca7c52018-01-02 11:34:14 -0500725If matrix is nullptr, draws with identity Matrix. If paint is nullptr, draws
Cary Clark682c58d2018-05-16 07:07:07 -0400726with default Paint. colorSpace may be nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500727
Cary Clark61ca7c52018-01-02 11:34:14 -0500728#Param picture stream of drawing commands ##
729#Param dimensions width and height ##
730#Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ##
731#Param paint Paint to apply transparency, filtering, and so on; may be nullptr ##
Cary Clark682c58d2018-05-16 07:07:07 -0400732#Param bitDepth 8-bit integer or 16-bit float: per component ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500733#Param colorSpace range of colors; may be nullptr ##
734
735#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500736
737#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500738 SkPaint paint;
739 SkPictureRecorder recorder;
740 SkCanvas* recordingCanvas = recorder.beginRecording(50, 50);
741 for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) {
742 paint.setColor(color);
743 recordingCanvas->drawRect({10, 10, 30, 40}, paint);
744 recordingCanvas->translate(10, 10);
745 recordingCanvas->scale(1.2f, 1.4f);
746 }
747 sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
748 int x = 0, y = 0;
749 for (auto alpha : { 70, 140, 210 } ) {
750 paint.setAlpha(alpha);
751 auto srgbColorSpace = SkColorSpace::MakeSRGB();
752 sk_sp<SkImage> image = SkImage::MakeFromPicture(playback, {50, 50}, nullptr, &paint,
753 SkImage::BitDepth::kU8, srgbColorSpace);
754 canvas->drawImage(image, x, y);
755 x += 70; y += 70;
756 }
Cary Clarka560c472017-11-27 10:44:06 -0500757##
758
Cary Clark61ca7c52018-01-02 11:34:14 -0500759#SeeAlso SkCanvas::drawPicture
Cary Clarka560c472017-11-27 10:44:06 -0500760
761#Method ##
762
763# ------------------------------------------------------------------------------
764
765#Method static sk_sp<SkImage> MakeFromAHardwareBuffer(AHardwareBuffer* hardwareBuffer,
766 SkAlphaType alphaType = kPremul_SkAlphaType,
767 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500768#In Constructor
769#Line # creates Image from Android hardware buffer ##
Cary Clarka560c472017-11-27 10:44:06 -0500770
Cary Clark4855f782018-02-06 09:41:53 -0500771#Bug 7447
Cary Clarka560c472017-11-27 10:44:06 -0500772
Cary Clark61ca7c52018-01-02 11:34:14 -0500773Creates Image from Android hardware buffer.
774Returned Image takes a reference on the buffer.
Cary Clarka560c472017-11-27 10:44:06 -0500775
Cary Clark61ca7c52018-01-02 11:34:14 -0500776Only available on Android, when __ANDROID_API__ is defined to be 26 or greater.
Cary Clarka560c472017-11-27 10:44:06 -0500777
Cary Clark61ca7c52018-01-02 11:34:14 -0500778#Param hardwareBuffer AHardwareBuffer Android hardware buffer ##
Cary Clark681287e2018-03-16 11:34:15 -0400779#Param alphaType one of: #list_of_alpha_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500780##
781#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500782
Cary Clark61ca7c52018-01-02 11:34:14 -0500783#Return created Image, or nullptr ##
784
785#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500786##
787
Cary Clark61ca7c52018-01-02 11:34:14 -0500788#SeeAlso MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -0500789
790#Method ##
791
792# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -0500793#Subtopic Property
794#Populate
795#Line # values and attributes ##
796##
Cary Clarka560c472017-11-27 10:44:06 -0500797
798#Method int width() const
Cary Clark4855f782018-02-06 09:41:53 -0500799#In Property
800#Line # returns pixel column count ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500801Returns pixel count in each row.
802
803#Return pixel width in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500804
805#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500806#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500807#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500808 canvas->translate(10, 10);
809 canvas->drawImage(image, 0, 0);
810 canvas->translate(0, image->height());
811 SkPaint paint;
812 paint.setTextAlign(SkPaint::kCenter_Align);
813 canvas->drawLine(0, 10, image->width(), 10, paint);
814 canvas->drawString("width", image->width() / 2, 25, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500815##
816
Cary Clark61ca7c52018-01-02 11:34:14 -0500817#SeeAlso dimensions() height()
Cary Clarka560c472017-11-27 10:44:06 -0500818
819#Method ##
820
821# ------------------------------------------------------------------------------
822
823#Method int height() const
Cary Clark4855f782018-02-06 09:41:53 -0500824#In Property
825#Line # returns pixel row count ##
Cary Clark2f466242017-12-11 16:03:17 -0500826Returns pixel row count.
827
Cary Clark61ca7c52018-01-02 11:34:14 -0500828#Return pixel height in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500829
830#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500831#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500832#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500833 canvas->translate(10, 10);
834 canvas->drawImage(image, 0, 0);
835 canvas->translate(image->width(), 0);
836 SkPaint paint;
837 paint.setTextAlign(SkPaint::kCenter_Align);
838 paint.setVerticalText(true);
839 canvas->drawLine(10, 0, 10, image->height(), paint);
Cary Clarkac47b882018-01-11 10:35:44 -0500840 canvas->drawString("height", 25, image->height() / 2, paint);
841##
Cary Clarka560c472017-11-27 10:44:06 -0500842
Cary Clark61ca7c52018-01-02 11:34:14 -0500843#SeeAlso dimensions() width()
Cary Clarka560c472017-11-27 10:44:06 -0500844
845#Method ##
846
847# ------------------------------------------------------------------------------
848
849#Method SkISize dimensions() const
Cary Clark4855f782018-02-06 09:41:53 -0500850#In Property
851#Line # returns width() and height() ##
Cary Clark681287e2018-03-16 11:34:15 -0400852
Cary Clark2f466242017-12-11 16:03:17 -0500853Returns ISize { width(), height() }.
854
855#Return integral size of width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -0500856
857#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500858#Image 4
859 SkISize dimensions = image->dimensions();
860 SkIRect bounds = image->bounds();
861 SkIRect dimensionsAsBounds = SkIRect::MakeSize(dimensions);
862 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clark681287e2018-03-16 11:34:15 -0400863#StdOut
864dimensionsAsBounds == bounds
865##
Cary Clarka560c472017-11-27 10:44:06 -0500866##
867
Cary Clark61ca7c52018-01-02 11:34:14 -0500868#SeeAlso height() width() bounds()
Cary Clarka560c472017-11-27 10:44:06 -0500869
870#Method ##
871
872# ------------------------------------------------------------------------------
873
874#Method SkIRect bounds() const
Cary Clark4855f782018-02-06 09:41:53 -0500875#In Property
876#Line # returns width() and height() as Rectangle ##
Cary Clark2f466242017-12-11 16:03:17 -0500877Returns IRect { 0, 0, width(), height() }.
878
879#Return integral rectangle from origin to width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -0500880
881#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500882#Height 128
883#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -0500884 SkIRect bounds = image->bounds();
Cary Clarkac47b882018-01-11 10:35:44 -0500885 for (int x : { 0, bounds.width() } ) {
886 for (int y : { 0, bounds.height() } ) {
Cary Clark61ca7c52018-01-02 11:34:14 -0500887 canvas->drawImage(image, x, y);
888 }
889 }
Cary Clarka560c472017-11-27 10:44:06 -0500890##
891
Cary Clark682c58d2018-05-16 07:07:07 -0400892#SeeAlso dimensions()
Cary Clarka560c472017-11-27 10:44:06 -0500893
894#Method ##
895
896# ------------------------------------------------------------------------------
897
898#Method uint32_t uniqueID() const
Cary Clark4855f782018-02-06 09:41:53 -0500899#In Property
Cary Clark682c58d2018-05-16 07:07:07 -0400900#Line # returns identifier for Image ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500901Returns value unique to image. Image contents cannot change after Image is
902created. Any operation to create a new Image will receive generate a new
903unique number.
904
905#Return unique identifier ##
Cary Clarka560c472017-11-27 10:44:06 -0500906
907#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500908#Image 5
909#Height 156
910 sk_sp<SkImage> subset = image->makeSubset({10, 20, 90, 100});
911 canvas->drawImage(image, 0, 0);
912 canvas->drawImage(subset, 128, 0);
913 SkPaint paint;
914 SkString s;
915 s.printf("original id: %d", image->uniqueID());
916 canvas->drawString(s, 20, image->height() + 20, paint);
917 s.printf("subset id: %d", subset->uniqueID());
918 canvas->drawString(s, 148, subset->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500919##
920
Cary Clark61ca7c52018-01-02 11:34:14 -0500921#SeeAlso isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -0500922
923#Method ##
924
925# ------------------------------------------------------------------------------
926
927#Method SkAlphaType alphaType() const
Cary Clark4855f782018-02-06 09:41:53 -0500928#In Property
929#Line # returns Alpha_Type ##
Cary Clark681287e2018-03-16 11:34:15 -0400930Returns Alpha_Type, one of: #list_of_alpha_types#.
Cary Clark61ca7c52018-01-02 11:34:14 -0500931
932Alpha_Type returned was a parameter to an Image constructor,
933or was parsed from encoded data.
934
935#Return Alpha_Type in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500936
937#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500938#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500939#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500940 const char* alphaTypeStr[] = { "Unknown", "Opaque", "Premul", "Unpremul" };
941 SkAlphaType alphaType = image->alphaType();
Cary Clarkac47b882018-01-11 10:35:44 -0500942 canvas->drawImage(image, 16, 0);
Cary Clark61ca7c52018-01-02 11:34:14 -0500943 SkPaint paint;
944 canvas->drawString(alphaTypeStr[(int) alphaType], 20, image->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500945##
946
Cary Clark61ca7c52018-01-02 11:34:14 -0500947#SeeAlso SkImageInfo::alphaType
Cary Clarka560c472017-11-27 10:44:06 -0500948
949#Method ##
950
951# ------------------------------------------------------------------------------
952
Greg Daniel56008aa2018-03-14 15:33:42 -0400953#Method SkColorType colorType() const
954#In Property
955#Line # returns Color_Type ##
956
957Returns Color_Type if known; otherwise, returns kUnknown_SkColorType.
958
959#Return Color_Type of Image ##
960
961#Example
962// incomplete
963##
964
965#SeeAlso SkImageInfo::colorType
966
967#Method ##
968
969# ------------------------------------------------------------------------------
970
Cary Clarka560c472017-11-27 10:44:06 -0500971#Method SkColorSpace* colorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -0500972#In Property
973#Line # returns Color_Space ##
Cary Clark2f466242017-12-11 16:03:17 -0500974Returns Color_Space, the range of colors, associated with Image. The
975reference count of Color_Space is unchanged. The returned Color_Space is
976immutable.
Cary Clarka560c472017-11-27 10:44:06 -0500977
Cary Clark61dfc3a2018-01-03 08:37:53 -0500978Color_Space returned was passed to an Image constructor,
979or was parsed from encoded data. Color_Space returned may be ignored when Image
980is drawn, depending on the capabilities of the Surface receiving the drawing.
Cary Clark2f466242017-12-11 16:03:17 -0500981
982#Return Color_Space in Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500983
984#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -0500985#Image 3
986#Set sRGB
987 SkPixmap pixmap;
988 source.peekPixels(&pixmap);
989 canvas->scale(.25f, .25f);
990 int y = 0;
991 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
992 SkColorSpace::kSRGB_RenderTargetGamma } ) {
993 int x = 0;
994 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
995 for (int index = 0; index < 2; ++index) {
996 pixmap.setColorSpace(colorSpace);
997 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
998 canvas->drawImage(image, x, y);
999 colorSpace = image->colorSpace()->makeColorSpin();
1000 x += 512;
1001 }
1002 y += 512;
1003 }
Cary Clarka560c472017-11-27 10:44:06 -05001004##
1005
Cary Clark61dfc3a2018-01-03 08:37:53 -05001006#SeeAlso refColorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001007
1008#Method ##
1009
1010# ------------------------------------------------------------------------------
1011
1012#Method sk_sp<SkColorSpace> refColorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001013#In Property
1014#Line # returns Image_Info Color_Space ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001015Returns a smart pointer to Color_Space, the range of colors, associated with
1016Image. The smart pointer tracks the number of objects sharing this
1017SkColorSpace reference so the memory is released when the owners destruct.
1018
1019The returned SkColorSpace is immutable.
1020
1021Color_Space returned was passed to an Image constructor,
1022or was parsed from encoded data. Color_Space returned may be ignored when Image
1023is drawn, depending on the capabilities of the Surface receiving the drawing.
1024
1025#Return Color_Space in Image, or nullptr, wrapped in a smart pointer ##
Cary Clarka560c472017-11-27 10:44:06 -05001026
1027#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001028#Image 3
1029#Set sRGB
1030 SkPixmap pixmap;
1031 source.peekPixels(&pixmap);
1032 canvas->scale(.25f, .25f);
1033 int y = 0;
1034 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1035 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1036 int x = 0;
1037 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1038 for (int index = 0; index < 2; ++index) {
1039 pixmap.setColorSpace(colorSpace);
1040 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1041 canvas->drawImage(image, x, y);
1042 colorSpace = image->refColorSpace()->makeColorSpin();
1043 x += 512;
1044 }
1045 y += 512;
1046 }
Cary Clarka560c472017-11-27 10:44:06 -05001047##
1048
Cary Clark61dfc3a2018-01-03 08:37:53 -05001049#SeeAlso colorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001050
1051#Method ##
1052
1053# ------------------------------------------------------------------------------
1054
1055#Method bool isAlphaOnly() const
Cary Clark4855f782018-02-06 09:41:53 -05001056#In Property
1057#Line # returns if pixels represent a transparency mask ##
Cary Clark2f466242017-12-11 16:03:17 -05001058Returns true if Image pixels represent transparency only. If true, each pixel
1059is packed in 8 bits as defined by kAlpha_8_SkColorType.
Cary Clarka560c472017-11-27 10:44:06 -05001060
Cary Clark2f466242017-12-11 16:03:17 -05001061#Return true if pixels represent a transparency mask ##
Cary Clarka560c472017-11-27 10:44:06 -05001062
1063#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001064 uint8_t pmColors = 0;
1065 sk_sp<SkImage> image = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1), &pmColors, 1});
1066 SkDebugf("alphaOnly = %s\n", image->isAlphaOnly() ? "true" : "false");
1067#StdOut
1068alphaOnly = true
1069##
Cary Clarka560c472017-11-27 10:44:06 -05001070##
1071
Cary Clark61dfc3a2018-01-03 08:37:53 -05001072#SeeAlso alphaType isOpaque
Cary Clarka560c472017-11-27 10:44:06 -05001073
1074#Method ##
1075
1076# ------------------------------------------------------------------------------
1077
1078#Method bool isOpaque() const
Cary Clark4855f782018-02-06 09:41:53 -05001079#In Property
1080#Line # returns if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001081Returns true if pixels ignore their Alpha value and are treated as fully opaque.
Cary Clark2f466242017-12-11 16:03:17 -05001082
1083#Return true if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clarka560c472017-11-27 10:44:06 -05001084
1085#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001086 auto check_isopaque = [](const SkImageInfo& imageInfo) -> void {
1087 auto surface(SkSurface::MakeRaster(imageInfo));
1088 auto image(surface->makeImageSnapshot());
1089 SkDebugf("isOpaque = %s\n", image->isOpaque() ? "true" : "false");
1090 };
1091
1092 check_isopaque(SkImageInfo::MakeN32Premul(5, 5));
1093 check_isopaque(SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType));
1094#StdOut
1095isOpaque = false
1096isOpaque = true
1097##
Cary Clarka560c472017-11-27 10:44:06 -05001098##
1099
Cary Clark61dfc3a2018-01-03 08:37:53 -05001100#SeeAlso alphaType isAlphaOnly
Cary Clarka560c472017-11-27 10:44:06 -05001101
1102#Method ##
1103
1104# ------------------------------------------------------------------------------
1105
1106#Method sk_sp<SkShader> makeShader(SkShader::TileMode tileMode1, SkShader::TileMode tileMode2,
1107 const SkMatrix* localMatrix = nullptr) const
Cary Clark4855f782018-02-06 09:41:53 -05001108#In Constructor
1109#Line # creates Shader, Paint element that can tile Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001110
Cary Clark61dfc3a2018-01-03 08:37:53 -05001111Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1112SkShader::TileMode rules to fill drawn area outside Image. localMatrix permits
1113transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001114
Cary Clark61dfc3a2018-01-03 08:37:53 -05001115#Param tileMode1 tiling in x, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1116 SkShader::kMirror_TileMode
1117##
1118#Param tileMode2 tiling in y, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1119 SkShader::kMirror_TileMode
1120##
1121#Param localMatrix Image transformation, or nullptr ##
1122
1123#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001124
1125#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001126#Image 4
1127SkMatrix matrix;
1128matrix.setRotate(45);
1129SkPaint paint;
1130paint.setShader(image->makeShader(SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode,
1131 &matrix));
1132canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001133##
1134
Cary Clark61dfc3a2018-01-03 08:37:53 -05001135#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001136
1137#Method ##
1138
1139# ------------------------------------------------------------------------------
1140
1141#Method sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const
1142
Cary Clark61dfc3a2018-01-03 08:37:53 -05001143Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1144SkShader::kClamp_TileMode to fill drawn area outside Image. localMatrix permits
1145transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001146
Cary Clark61dfc3a2018-01-03 08:37:53 -05001147#Param localMatrix Image transformation, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001148
Cary Clark61dfc3a2018-01-03 08:37:53 -05001149#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001150
1151#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001152#Image 5
1153SkMatrix matrix;
1154matrix.setRotate(45);
1155matrix.postTranslate(125, 30);
1156SkPaint paint;
1157paint.setShader(image->makeShader(&matrix));
1158canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001159##
1160
Cary Clarkf5404bb2018-01-05 12:10:09 -05001161#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001162
1163#Method ##
1164
1165# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001166#Subtopic Pixels
1167#Populate
1168#Line # read and write pixel values ##
1169##
Cary Clarka560c472017-11-27 10:44:06 -05001170
1171#Method bool peekPixels(SkPixmap* pixmap) const
Cary Clark78de7512018-02-07 07:27:09 -05001172#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001173#Line # returns Pixmap if possible ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001174Copies Image pixel address, row bytes, and Image_Info to pixmap, if address
1175is available, and returns true. If pixel address is not available, return
1176false and leave pixmap unchanged.
Cary Clarka560c472017-11-27 10:44:06 -05001177
Cary Clarkf5404bb2018-01-05 12:10:09 -05001178#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
Cary Clarka560c472017-11-27 10:44:06 -05001179
Cary Clarkf5404bb2018-01-05 12:10:09 -05001180#Return true if Image has direct access to pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001181
1182#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001183 SkBitmap bitmap;
1184 bitmap.allocPixels(SkImageInfo::MakeN32Premul(12, 11));
1185 SkCanvas offscreen(bitmap);
1186 offscreen.clear(SK_ColorWHITE);
1187 SkPaint paint;
1188 offscreen.drawString("%", 1, 10, paint);
1189 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
1190 SkPixmap pixmap;
1191 if (image->peekPixels(&pixmap)) {
1192 const SkPMColor* pixels = pixmap.addr32();
1193 SkPMColor pmWhite = pixels[0];
1194 for (int y = 0; y < image->height(); ++y) {
1195 for (int x = 0; x < image->width(); ++x) {
1196 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
1197 }
1198 SkDebugf("\n");
1199 }
1200 }
1201#StdOut
1202------------
1203--xx----x---
1204-x--x--x----
1205-x--x--x----
1206-x--x-x-----
1207--xx-xx-xx--
1208-----x-x--x-
1209----x--x--x-
1210----x--x--x-
1211---x----xx--
1212------------
1213##
Cary Clarka560c472017-11-27 10:44:06 -05001214##
1215
Cary Clarkf5404bb2018-01-05 12:10:09 -05001216#SeeAlso readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001217
1218#Method ##
1219
1220# ------------------------------------------------------------------------------
1221
1222#Method GrTexture* getTexture() const
Cary Clark682c58d2018-05-16 07:07:07 -04001223#Deprecated
Cary Clarka560c472017-11-27 10:44:06 -05001224#Method ##
1225
1226# ------------------------------------------------------------------------------
1227
1228#Method bool isTextureBacked() const
Cary Clark78de7512018-02-07 07:27:09 -05001229#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001230#Line # returns if Image was created from GPU_Texture ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001231Returns true the contents of Image was created on or uploaded to GPU memory,
1232and is available as a GPU_Texture.
Cary Clarka560c472017-11-27 10:44:06 -05001233
Cary Clarkf5404bb2018-01-05 12:10:09 -05001234#Return true if Image is a GPU_Texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001235
1236#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001237#Image 5
1238#Platform gpu
1239auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1240 if (nullptr == image) {
1241 return;
1242 }
1243 SkPaint paint;
1244 paint.setAntiAlias(true);
1245 paint.setTextAlign(SkPaint::kCenter_Align);
1246 canvas->drawImage(image, 0, 0);
1247 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1248 canvas->drawString(image->isTextureBacked() ? "is GPU texture" : "not GPU texture",
1249 image->width() / 2, image->height() * 3 / 4, paint);
1250};
1251sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1252sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001253 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1254 nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05001255drawImage(image, "image");
1256canvas->translate(image->width(), 0);
1257drawImage(bitmapImage, "source");
1258canvas->translate(-image->width(), image->height());
1259drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001260##
1261
Cary Clarkf5404bb2018-01-05 12:10:09 -05001262#SeeAlso MakeFromTexture isValid
Cary Clarka560c472017-11-27 10:44:06 -05001263
1264#Method ##
1265
1266# ------------------------------------------------------------------------------
1267
1268#Method bool isValid(GrContext* context) const
Cary Clark4855f782018-02-06 09:41:53 -05001269#In Property
1270#Line # returns if Image can draw to Raster_Surface or GPU_Context ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001271Returns true if Image can be drawn on either Raster_Surface or GPU_Surface.
1272If context is nullptr, tests if Image draws on Raster_Surface;
1273otherwise, tests if Image draws on GPU_Surface associated with context.
Cary Clarka560c472017-11-27 10:44:06 -05001274
Cary Clarkf5404bb2018-01-05 12:10:09 -05001275Image backed by GPU_Texture may become invalid if associated GrContext is
1276invalid. Lazy_Image may be invalid and may not draw to Raster_Surface or
1277GPU_Surface or both.
Cary Clarka560c472017-11-27 10:44:06 -05001278
Cary Clark61ca7c52018-01-02 11:34:14 -05001279#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05001280
Cary Clarkf5404bb2018-01-05 12:10:09 -05001281#Return true if Image can be drawn ##
Cary Clarka560c472017-11-27 10:44:06 -05001282
1283#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001284#Image 5
1285#Platform gpu
1286auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1287 if (nullptr == image) {
1288 return;
1289 }
1290 SkPaint paint;
1291 paint.setAntiAlias(true);
1292 paint.setTextAlign(SkPaint::kCenter_Align);
1293 canvas->drawImage(image, 0, 0);
1294 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1295 if (canvas->getGrContext()) {
1296 canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" :
1297 "not valid on GPU", image->width() / 2, image->height() * 5 / 8, paint);
1298 }
1299 canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" :
1300 "not valid on CPU", image->width() / 2, image->height() * 7 / 8, paint);
1301};
1302sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1303sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001304 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1305 nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05001306drawImage(image, "image");
1307canvas->translate(image->width(), 0);
1308drawImage(bitmapImage, "source");
1309canvas->translate(-image->width(), image->height());
1310drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001311##
1312
Cary Clarkf5404bb2018-01-05 12:10:09 -05001313#SeeAlso isTextureBacked isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001314
1315#Method ##
1316
1317# ------------------------------------------------------------------------------
1318
Brian Osmana0ca9092018-05-10 22:04:08 +00001319#Method GrBackendObject getTextureHandle(bool flushPendingGrContextIO,
1320 GrSurfaceOrigin* origin = nullptr) const
1321#Deprecated
1322#Method ##
1323
1324# ------------------------------------------------------------------------------
1325
Robert Phillipsc5509952018-04-04 15:54:55 -04001326#Method GrBackendTexture getBackendTexture(bool flushPendingGrContextIO,
1327 GrSurfaceOrigin* origin = nullptr) const
1328#In Property
1329#Line # returns GPU reference to Image as texture ##
1330
Cary Clark682c58d2018-05-16 07:07:07 -04001331Retrieves the back-end texture. If Image has no back-end texture, an invalid
Cary Clarkba75aee2018-04-05 08:18:41 -04001332object is returned. Call GrBackendTexture::isValid to determine if the result
1333is valid.
1334
1335If flushPendingGrContextIO is true, completes deferred I/O operations.
Robert Phillipsc5509952018-04-04 15:54:55 -04001336
1337If origin in not nullptr, copies location of content drawn into Image.
1338
1339#Param flushPendingGrContextIO flag to flush outstanding requests ##
1340#Param origin storage for one of: kTopLeft_GrSurfaceOrigin,
1341 kBottomLeft_GrSurfaceOrigin; or nullptr
1342##
1343
Cary Clarkba75aee2018-04-05 08:18:41 -04001344#Return back-end API texture handle; invalid on failure ##
Robert Phillipsc5509952018-04-04 15:54:55 -04001345
Cary Clarkba75aee2018-04-05 08:18:41 -04001346#Example
1347#Image 3
1348#Platform gpu
Brian Osman584b5012018-04-13 15:48:26 -04001349 GrContext* grContext = canvas->getGrContext();
1350 if (!grContext) {
1351 canvas->drawString("GPU only!", 20, 40, SkPaint());
1352 return;
1353 }
1354 sk_sp<SkImage> imageFromBackend = SkImage::MakeFromAdoptedTexture(grContext, backEndTexture,
1355 kBottomLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1356 GrBackendTexture textureFromImage = imageFromBackend->getBackendTexture(false);
1357 if (!textureFromImage.isValid()) {
1358 return;
1359 }
1360 sk_sp<SkImage> imageFromTexture = SkImage::MakeFromAdoptedTexture(grContext, textureFromImage,
1361 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1362 canvas->drawImage(imageFromTexture, 0, 0);
Cary Clarkba75aee2018-04-05 08:18:41 -04001363 canvas->drawImage(imageFromBackend, 128, 128);
Robert Phillipsc5509952018-04-04 15:54:55 -04001364##
1365
1366#SeeAlso MakeFromTexture isTextureBacked
1367
1368#Method ##
1369
1370# ------------------------------------------------------------------------------
1371
Cary Clarka560c472017-11-27 10:44:06 -05001372#Enum CachingHint
Cary Clark682c58d2018-05-16 07:07:07 -04001373#Line # options for readPixels and scalePixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001374#Code
1375 enum CachingHint {
1376 kAllow_CachingHint,
1377 kDisallow_CachingHint,
1378 };
1379##
1380
Cary Clarkac47b882018-01-11 10:35:44 -05001381CachingHint selects whether Skia may internally cache Bitmaps generated by
1382decoding Image, or by copying Image from GPU to CPU. The default behavior
Cary Clark682c58d2018-05-16 07:07:07 -04001383allows caching Bitmaps.
Cary Clarkac47b882018-01-11 10:35:44 -05001384
1385Choose kDisallow_CachingHint if Image pixels are to be used only once, or
1386if Image pixels reside in a cache outside of Skia, or to reduce memory pressure.
1387
1388Choosing kAllow_CachingHint does not ensure that pixels will be cached.
1389Image pixels may not be cached if memory requirements are too large or
1390pixels are not accessible.
Cary Clarka560c472017-11-27 10:44:06 -05001391
1392#Const kAllow_CachingHint 0
Cary Clark682c58d2018-05-16 07:07:07 -04001393#Line # allows internally caching decoded and copied pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001394##
1395#Const kDisallow_CachingHint 1
Cary Clark682c58d2018-05-16 07:07:07 -04001396#Line # disallows internally caching decoded and copied pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001397##
1398
Cary Clarkac47b882018-01-11 10:35:44 -05001399#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001400##
1401
Cary Clarkac47b882018-01-11 10:35:44 -05001402#SeeAlso readPixels scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001403
1404#Enum ##
1405
1406# ------------------------------------------------------------------------------
1407
1408#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1409 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001410#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001411#Line # copies and converts pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001412
Cary Clarkac47b882018-01-11 10:35:44 -05001413Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY),
Cary Clark682c58d2018-05-16 07:07:07 -04001414and does not exceed Image (width(), height()).
Cary Clarkac47b882018-01-11 10:35:44 -05001415
1416dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
1417destination. dstRowBytes specifics the gap from one destination row to the next.
1418Returns true if pixels are copied. Returns false if:
1419#List
1420# dstInfo.addr() equals nullptr ##
1421# dstRowBytes is less than dstInfo.minRowBytes ##
1422# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001423##
1424
Cary Clarkac47b882018-01-11 10:35:44 -05001425Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1426kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1427If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace must match.
1428If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType must
1429match. If Image Color_Space is nullptr, dstInfo.colorSpace must match. Returns
1430false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001431
Cary Clarkac47b882018-01-11 10:35:44 -05001432srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark682c58d2018-05-16 07:07:07 -04001433false if width() or height() is zero or negative.
1434Returns false if
Cary Clarkac47b882018-01-11 10:35:44 -05001435#Formula
1436abs(srcX) >= Image width()
1437##
1438, or if
1439#Formula
1440abs(srcY) >= Image height()
1441##
1442.
Cary Clarka560c472017-11-27 10:44:06 -05001443
Cary Clarkac47b882018-01-11 10:35:44 -05001444If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1445If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1446
1447#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1448#Param dstPixels destination pixel storage ##
1449#Param dstRowBytes destination row length ##
1450#Param srcX column index whose absolute value is less than width() ##
1451#Param srcY row index whose absolute value is less than height() ##
1452#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1453
1454#Return true if pixels are copied to dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001455
1456#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001457#Image 3
1458 canvas->scale(.5f, .5f);
1459 const int width = 32;
1460 const int height = 32;
1461 std::vector<int32_t> dstPixels;
1462 dstPixels.resize(height * width * 4);
1463 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1464 for (int y = 0; y < 512; y += height ) {
1465 for (int x = 0; x < 512; x += width ) {
1466 if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) {
1467 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
1468 SkBitmap bitmap;
1469 bitmap.installPixels(dstPixmap);
1470 canvas->drawBitmap(bitmap, 0, 0);
1471 }
1472 canvas->translate(48, 0);
1473 }
1474 canvas->translate(-16 * 48, 48);
1475 }
Cary Clarka560c472017-11-27 10:44:06 -05001476##
1477
Cary Clarkac47b882018-01-11 10:35:44 -05001478#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001479
1480#Method ##
1481
1482# ------------------------------------------------------------------------------
1483
1484#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY,
1485 CachingHint cachingHint = kAllow_CachingHint) const
1486
Cary Clarkac47b882018-01-11 10:35:44 -05001487Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and
Cary Clark682c58d2018-05-16 07:07:07 -04001488does not exceed Image (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001489
Cary Clarkac47b882018-01-11 10:35:44 -05001490dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
1491and row bytes of destination. dst.rowBytes specifics the gap from one destination
1492row to the next. Returns true if pixels are copied. Returns false if:
1493#List
1494# dst pixel storage equals nullptr ##
1495# dst.rowBytes is less than SkImageInfo::minRowBytes ##
1496# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001497##
1498
Cary Clarkac47b882018-01-11 10:35:44 -05001499Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1500kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1501If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1502If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1503match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1504false if pixel conversion is not possible.
Cary Clark682c58d2018-05-16 07:07:07 -04001505
Cary Clarkac47b882018-01-11 10:35:44 -05001506srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark682c58d2018-05-16 07:07:07 -04001507false if width() or height() is zero or negative.
1508Returns false if
Cary Clarkac47b882018-01-11 10:35:44 -05001509#Formula
1510abs(srcX) >= Image width()
1511##
1512, or if
1513#Formula
1514abs(srcY) >= Image height()
1515##
1516.
1517
1518If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1519If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1520
1521#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1522#Param srcX column index whose absolute value is less than width() ##
1523#Param srcY row index whose absolute value is less than height() ##
1524#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1525
1526#Return true if pixels are copied to dst ##
1527
1528#Example
1529#Image 3
1530 std::vector<int32_t> srcPixels;
1531 int rowBytes = image->width() * 4;
1532 int quarterWidth = image->width() / 4;
1533 int quarterHeight = image->height() / 4;
1534 srcPixels.resize(image->height() * rowBytes);
1535 for (int y = 0; y < 4; ++y) {
1536 for (int x = 0; x < 4; ++x) {
1537 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1538 &srcPixels.front() + x * image->height() * quarterWidth +
1539 y * quarterWidth, rowBytes);
1540 image->readPixels(pixmap, x * quarterWidth, y * quarterHeight);
1541 }
1542 }
1543 canvas->scale(.5f, .5f);
1544 SkBitmap bitmap;
1545 bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()),
1546 &srcPixels.front(), rowBytes);
1547 canvas->drawBitmap(bitmap, 0, 0);
1548##
1549
1550#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001551
1552#Method ##
1553
1554# ------------------------------------------------------------------------------
1555
1556#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality,
1557 CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001558#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001559#Line # scales and converts one Image to another ##
Cary Clarka560c472017-11-27 10:44:06 -05001560
Cary Clarkac47b882018-01-11 10:35:44 -05001561Copies Image to dst, scaling pixels to fit dst.width() and dst.height(), and
1562converting pixels to match dst.colorType and dst.alphaType. Returns true if
1563pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1564less than dst SkImageInfo::minRowBytes.
Cary Clarka560c472017-11-27 10:44:06 -05001565
Cary Clarkac47b882018-01-11 10:35:44 -05001566Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1567kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1568If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1569If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1570match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1571false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001572
Cary Clarkac47b882018-01-11 10:35:44 -05001573Scales the image, with filterQuality, to match dst.width() and dst.height().
1574filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1575Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1576Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1577Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1578kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1579
1580If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1581If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1582
1583#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1584#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1585 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1586##
1587#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1588
1589#Return true if pixels are scaled to fit dst ##
Cary Clarka560c472017-11-27 10:44:06 -05001590
1591#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001592#Image 3
1593#Height 128
1594 std::vector<int32_t> srcPixels;
1595 int quarterWidth = image->width() / 16;
1596 int rowBytes = quarterWidth * 4;
1597 int quarterHeight = image->height() / 16;
1598 srcPixels.resize(quarterHeight * rowBytes);
1599 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1600 &srcPixels.front(), rowBytes);
1601 canvas->scale(4, 4);
1602 SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality,
1603 kMedium_SkFilterQuality, kHigh_SkFilterQuality };
1604 for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) {
1605 image->scalePixels(pixmap, qualities[index]);
1606 sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
1607 canvas->drawImage(filtered, 16 * index, 0);
1608 }
Cary Clarka560c472017-11-27 10:44:06 -05001609##
1610
Cary Clarkac47b882018-01-11 10:35:44 -05001611#SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001612
1613#Method ##
1614
1615# ------------------------------------------------------------------------------
1616
1617#Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const
Cary Clark78de7512018-02-07 07:27:09 -05001618#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001619#Line # returns encoded Image as SkData ##
Cary Clark682c58d2018-05-16 07:07:07 -04001620Encodes Image pixels, returning result as SkData.
Cary Clark2f466242017-12-11 16:03:17 -05001621
Cary Clarkac47b882018-01-11 10:35:44 -05001622Returns nullptr if encoding fails, or if encodedImageFormat is not supported.
Cary Clarka560c472017-11-27 10:44:06 -05001623
Cary Clarkac47b882018-01-11 10:35:44 -05001624Image encoding in a format requires both building with one or more of:
1625SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support
1626for the encoded format.
1627
1628If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can
1629additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP,
1630SkEncodedImageFormat::kGIF.
1631
1632quality is a platform and format specific metric trading off size and encoding
1633error. When used, quality equaling 100 encodes with the least error. quality may
1634be ignored by the encoder.
1635
1636#Param encodedImageFormat one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
1637 SkEncodedImageFormat::kWEBP
1638 ##
1639#Param quality encoder specific metric with 100 equaling best ##
Cary Clarka560c472017-11-27 10:44:06 -05001640
Cary Clark2f466242017-12-11 16:03:17 -05001641#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001642
1643#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001644#Image 3
1645 canvas->scale(4, 4);
1646 SkIRect subset = {0, 0, 16, 64};
1647 int x = 0;
1648 for (int quality : { 0, 10, 50, 100 } ) {
1649 sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
1650 sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
1651 canvas->drawImage(filtered, x, 0);
1652 x += 16;
1653 }
Cary Clarka560c472017-11-27 10:44:06 -05001654##
1655
Cary Clarkac47b882018-01-11 10:35:44 -05001656#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001657
1658#Method ##
1659
1660# ------------------------------------------------------------------------------
1661
Cary Clark61ca7c52018-01-02 11:34:14 -05001662#Method sk_sp<SkData> encodeToData() const
Cary Clarka560c472017-11-27 10:44:06 -05001663
Cary Clarkac47b882018-01-11 10:35:44 -05001664Encodes Image pixels, returning result as SkData. Returns existing encoded data
1665if present; otherwise, Image is encoded with SkEncodedImageFormat::kPNG. Skia
1666must be built with SK_HAS_PNG_LIBRARY to encode Image.
Cary Clarka560c472017-11-27 10:44:06 -05001667
Cary Clark682c58d2018-05-16 07:07:07 -04001668Returns nullptr if existing encoded data is missing or invalid, and
Cary Clarka560c472017-11-27 10:44:06 -05001669encoding fails.
1670
Cary Clarkac47b882018-01-11 10:35:44 -05001671#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001672
1673#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001674#Image 3
1675 canvas->scale(4, 4);
1676 SkIRect subset = {136, 32, 200, 96};
1677 sk_sp<SkData> data(image->encodeToData());
1678 sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
1679 canvas->drawImage(eye, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001680##
1681
Cary Clarkac47b882018-01-11 10:35:44 -05001682#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001683
1684#Method ##
1685
1686# ------------------------------------------------------------------------------
1687
1688#Method sk_sp<SkData> refEncodedData() const
Cary Clark78de7512018-02-07 07:27:09 -05001689#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001690#Line # returns Image encoded in SkData if present ##
Cary Clarkac47b882018-01-11 10:35:44 -05001691Returns encoded Image pixels as SkData, if Image was created from supported
1692encoded stream format. Platform support for formats vary and may require building
1693with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY.
Cary Clarka560c472017-11-27 10:44:06 -05001694
Cary Clarkac47b882018-01-11 10:35:44 -05001695Returns nullptr if Image contents are not encoded.
Cary Clarka560c472017-11-27 10:44:06 -05001696
Cary Clarkac47b882018-01-11 10:35:44 -05001697#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001698
1699#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001700#Image 3
1701#Platform gpu
1702 struct {
1703 const char* name;
1704 sk_sp<SkImage> image;
1705 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1706 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001707 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1708 nullptr) } };
Cary Clarkac47b882018-01-11 10:35:44 -05001709 SkString string;
1710 SkPaint paint;
1711 for (const auto& test : tests ) {
1712 if (!test.image) {
1713 string.printf("no %s", test.name);
1714 } else {
1715 string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name);
1716 }
1717 canvas->drawString(string, 10, 20, paint);
1718 canvas->translate(0, 20);
1719 }
Cary Clarka560c472017-11-27 10:44:06 -05001720##
1721
Cary Clarkac47b882018-01-11 10:35:44 -05001722#SeeAlso encodeToData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001723
1724#Method ##
1725
1726# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001727#Subtopic Utility
1728#Populate
1729#Line # rarely called management functions ##
1730##
Cary Clarka560c472017-11-27 10:44:06 -05001731
1732#Method const char* toString(SkString* string) const
Cary Clark4855f782018-02-06 09:41:53 -05001733#In Utility
1734#Line # converts Image to machine readable form ##
Cary Clarkac47b882018-01-11 10:35:44 -05001735Appends Image description to string, including unique ID, width, height, and
1736whether the image is opaque.
Cary Clarka560c472017-11-27 10:44:06 -05001737
Cary Clarkac47b882018-01-11 10:35:44 -05001738#Param string storage for description; existing content is preserved ##
1739
1740#Return string appended with Image description ##
Cary Clarka560c472017-11-27 10:44:06 -05001741
1742#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001743#Image 4
1744 struct {
1745 const char* name;
1746 sk_sp<SkImage> image;
1747 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1748 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001749 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1750 nullptr) } };
Cary Clarkac47b882018-01-11 10:35:44 -05001751 SkString string;
1752 SkPaint paint;
1753 for (const auto& test : tests ) {
1754 string.printf("%s: ", test.name);
1755 test.image ? (void) test.image->toString(&string) : string.append("no image");
1756 canvas->drawString(string, 10, 20, paint);
1757 canvas->translate(0, 20);
1758 }
Cary Clarka560c472017-11-27 10:44:06 -05001759##
1760
Cary Clarkac47b882018-01-11 10:35:44 -05001761#SeeAlso SkPaint::toString
Cary Clarka560c472017-11-27 10:44:06 -05001762
1763#Method ##
1764
1765# ------------------------------------------------------------------------------
1766
1767#Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const
Cary Clark4855f782018-02-06 09:41:53 -05001768#In Constructor
1769#Line # creates Image containing part of original ##
Cary Clarkac47b882018-01-11 10:35:44 -05001770Returns subset of Image. subset must be fully contained by Image dimensions().
1771The implementation may share pixels, or may copy them.
Cary Clarka560c472017-11-27 10:44:06 -05001772
Cary Clarkac47b882018-01-11 10:35:44 -05001773Returns nullptr if subset is empty, or subset is not contained by bounds, or
1774pixels in Image could not be read or copied.
Cary Clarka560c472017-11-27 10:44:06 -05001775
Cary Clarkac47b882018-01-11 10:35:44 -05001776#Param subset bounds of returned Image ##
1777
1778#Return partial or full Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001779
1780#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001781#Image 3
1782 canvas->scale(.5f, .5f);
1783 const int width = 32;
1784 const int height = 32;
1785 for (int y = 0; y < 512; y += height ) {
1786 for (int x = 0; x < 512; x += width ) {
1787 sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height}));
1788 canvas->drawImage(subset, x * 3 / 2, y * 3 / 2);
1789 }
1790 }
Cary Clarka560c472017-11-27 10:44:06 -05001791##
1792
Cary Clarkac47b882018-01-11 10:35:44 -05001793#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001794
1795#Method ##
1796
1797# ------------------------------------------------------------------------------
1798
1799#Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const
Cary Clark4855f782018-02-06 09:41:53 -05001800#In Constructor
1801#Line # creates Image matching Color_Space if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001802Returns Image backed by GPU_Texture associated with context. Returned Image is
1803compatible with Surface created with dstColorSpace. Returns original
1804Image if context and dstColorSpace match.
1805
1806Returns nullptr if context is nullptr, or if Image was created with another
1807GrContext.
Cary Clarka560c472017-11-27 10:44:06 -05001808
Cary Clark61ca7c52018-01-02 11:34:14 -05001809#Param context GPU_Context ##
Cary Clarkac47b882018-01-11 10:35:44 -05001810#Param dstColorSpace range of colors of matching Surface on GPU ##
Cary Clarka560c472017-11-27 10:44:06 -05001811
Cary Clarkac47b882018-01-11 10:35:44 -05001812#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001813
1814#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001815#Platform gpu
1816#Image 5
1817 auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void {
1818 if (nullptr == image || nullptr == context) {
1819 return;
1820 }
1821 SkPaint paint;
1822 paint.setAntiAlias(true);
1823 paint.setTextAlign(SkPaint::kCenter_Align);
1824 sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr));
1825 canvas->drawImage(texture, 0, 0);
1826 canvas->drawString(label, texture->width() / 2, texture->height() / 4, paint);
1827 };
1828 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1829 GrContext* context = canvas->getGrContext();
1830 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001831 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1832 nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001833 drawImage(image, context, "image");
1834 canvas->translate(image->width(), 0);
1835 drawImage(bitmapImage, context, "source");
1836 canvas->translate(-image->width(), image->height());
1837 drawImage(textureImage, context, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001838##
1839
Cary Clarkac47b882018-01-11 10:35:44 -05001840#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05001841
1842#Method ##
1843
1844# ------------------------------------------------------------------------------
1845
1846#Method sk_sp<SkImage> makeNonTextureImage() const
Cary Clark4855f782018-02-06 09:41:53 -05001847#In Constructor
1848#Line # creates Image without dependency on GPU_Texture ##
Cary Clarkac47b882018-01-11 10:35:44 -05001849Returns Raster_Image or Lazy_Image. Copies Image backed by GPU_Texture into
Cary Clark4855f782018-02-06 09:41:53 -05001850CPU memory if needed. Returns original Image if decoded in Raster_Bitmap,
Cary Clarkac47b882018-01-11 10:35:44 -05001851or if encoded in a stream.
Cary Clark61ca7c52018-01-02 11:34:14 -05001852
Cary Clarkac47b882018-01-11 10:35:44 -05001853Returns nullptr if backed by GPU_Texture and copy fails.
1854
1855#Return Raster_Image, Lazy_Image, or nullptr ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001856
1857#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001858#Image 5
1859#Platform gpu
1860 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1861 if (nullptr == image) {
1862 return;
1863 }
1864 SkPaint paint;
1865 paint.setAntiAlias(true);
1866 paint.setTextAlign(SkPaint::kCenter_Align);
1867 sk_sp<SkImage> nonTexture(image->makeNonTextureImage());
1868 canvas->drawImage(nonTexture, 0, 0);
1869 canvas->drawString(label, nonTexture->width() / 2, nonTexture->height() / 4, paint);
1870 };
1871 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1872 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001873 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1874 nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001875 drawImage(image, "image");
1876 canvas->translate(image->width(), 0);
1877 drawImage(bitmapImage, "source");
1878 canvas->translate(-image->width(), image->height());
1879 drawImage(textureImage, "backEndTexture");
Cary Clark61ca7c52018-01-02 11:34:14 -05001880##
1881
Cary Clark56356312018-02-08 14:45:18 -05001882#SeeAlso makeTextureImage makeRasterImage MakeBackendTextureFromSkImage
Cary Clark61ca7c52018-01-02 11:34:14 -05001883
1884#Method ##
1885
1886# ------------------------------------------------------------------------------
1887
1888#Method sk_sp<SkImage> makeRasterImage() const
Cary Clark4855f782018-02-06 09:41:53 -05001889#In Constructor
1890#Line # creates Image compatible with Raster_Surface if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001891Returns Raster_Image. Copies Image backed by GPU_Texture into CPU memory,
Cary Clark4855f782018-02-06 09:41:53 -05001892or decodes Image from Lazy_Image. Returns original Image if decoded in
Cary Clarkac47b882018-01-11 10:35:44 -05001893Raster_Bitmap.
Cary Clarka560c472017-11-27 10:44:06 -05001894
Cary Clarkac47b882018-01-11 10:35:44 -05001895Returns nullptr if copy, decode, or pixel read fails.
Cary Clarka560c472017-11-27 10:44:06 -05001896
Cary Clarkac47b882018-01-11 10:35:44 -05001897#Return Raster_Image, or nullptr ##
1898
Cary Clark4855f782018-02-06 09:41:53 -05001899#Bug 7479
Cary Clarka560c472017-11-27 10:44:06 -05001900#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001901#Image 5
1902#Platform gpu
1903 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1904 if (nullptr == image) {
1905 return;
1906 }
1907 SkPaint paint;
1908 paint.setAntiAlias(true);
1909 paint.setTextAlign(SkPaint::kCenter_Align);
1910 sk_sp<SkImage> raster(image->makeRasterImage());
1911 canvas->drawImage(raster, 0, 0);
1912 canvas->drawString(label, raster->width() / 2, raster->height() / 4, paint);
1913 };
1914 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1915 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001916 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1917 nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001918 drawImage(image, "image");
1919 canvas->translate(image->width(), 0);
1920 drawImage(bitmapImage, "source");
1921 canvas->translate(-image->width(), image->height());
1922 drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001923##
1924
Cary Clarkac47b882018-01-11 10:35:44 -05001925#SeeAlso isTextureBacked isLazyGenerated MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -05001926
1927#Method ##
1928
1929# ------------------------------------------------------------------------------
1930
1931#Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
1932 const SkIRect& clipBounds, SkIRect* outSubset,
1933 SkIPoint* offset) const
Cary Clark4855f782018-02-06 09:41:53 -05001934#In Constructor
1935#Line # creates filtered, clipped Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001936
Cary Clarkac47b882018-01-11 10:35:44 -05001937Creates filtered Image. filter processes original Image, potentially changing
1938color, position, and size. subset is the bounds of original Image processed
1939by filter. clipBounds is the expected bounds of the filtered Image. outSubset
1940is required storage for the actual bounds of the filtered Image. offset is
1941required storage for translation of returned Image.
Cary Clarka560c472017-11-27 10:44:06 -05001942
Cary Clarkac47b882018-01-11 10:35:44 -05001943Returns nullptr if Image could not be created. If nullptr is returned, outSubset
1944and offset are undefined.
1945
Cary Clark56356312018-02-08 14:45:18 -05001946Useful for animation of SkImageFilter that varies size from frame to frame.
1947Returned Image is created larger than required by filter so that GPU_Texture
1948can be reused with different sized effects. outSubset describes the valid bounds
1949of GPU_Texture returned. offset translates the returned Image to keep subsequent
1950animation frames aligned with respect to each other.
Cary Clarkac47b882018-01-11 10:35:44 -05001951
1952#Param filter how Image is sampled when transformed ##
Cary Clark56356312018-02-08 14:45:18 -05001953#Param subset bounds of Image processed by filter ##
1954#Param clipBounds expected bounds of filtered Image ##
1955#Param outSubset storage for returned Image bounds ##
1956#Param offset storage for returned Image translation ##
Cary Clarka560c472017-11-27 10:44:06 -05001957
Cary Clarkac47b882018-01-11 10:35:44 -05001958#Return filtered Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001959
1960#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001961#Description
1962In each frame of the animation, filtered Image is drawn in a different location.
1963By translating canvas by returned offset, Image appears stationary.
1964##
1965#Image 5
1966#Platform gpu
1967#Duration 5
1968 sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make(
1969 -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE,
1970 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
1971 nullptr);
1972 sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr);
1973 SkIRect subset = image->bounds();
1974 SkIRect clipBounds = image->bounds();
1975 clipBounds.outset(60, 60);
1976 SkIRect outSubset;
1977 SkIPoint offset;
1978 sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds,
1979 &outSubset, &offset));
1980 SkPaint paint;
1981 paint.setAntiAlias(true);
1982 paint.setStyle(SkPaint::kStroke_Style);
1983 canvas->drawLine(0, 0, offset.fX, offset.fY, paint);
1984 canvas->translate(offset.fX, offset.fY);
1985 canvas->drawImage(filtered, 0, 0);
Cary Clark681287e2018-03-16 11:34:15 -04001986 canvas->drawRect(SkRect::Make(outSubset), paint);
Cary Clarka560c472017-11-27 10:44:06 -05001987##
1988
Cary Clark56356312018-02-08 14:45:18 -05001989#SeeAlso makeShader SkPaint::setImageFilter
Cary Clarka560c472017-11-27 10:44:06 -05001990
1991#Method ##
1992
1993# ------------------------------------------------------------------------------
1994
Cary Clarka560c472017-11-27 10:44:06 -05001995#Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc
Cary Clark682c58d2018-05-16 07:07:07 -04001996#Line # parameter type for MakeBackendTextureFromSkImage ##
1997Defines a function take one parameter of type
1998GrBackendTexture with no return value.
Cary Clarka560c472017-11-27 10:44:06 -05001999##
2000
2001# ------------------------------------------------------------------------------
2002
2003#Method static bool MakeBackendTextureFromSkImage(GrContext* context,
2004 sk_sp<SkImage> image,
2005 GrBackendTexture* backendTexture,
2006 BackendTextureReleaseProc* backendTextureReleaseProc)
Cary Clark4855f782018-02-06 09:41:53 -05002007#In Constructor
2008#Line # creates GPU_Texture from Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002009
Cary Clark56356312018-02-08 14:45:18 -05002010Creates a GrBackendTexture from the provided SkImage. Returns true and
2011stores result in backendTexture and backendTextureReleaseProc if
2012texture is created; otherwise, returns false and leaves
2013backendTexture and backendTextureReleaseProc unmodified.
Cary Clarka560c472017-11-27 10:44:06 -05002014
Cary Clark56356312018-02-08 14:45:18 -05002015Call backendTextureReleaseProc after deleting backendTexture.
2016backendTextureReleaseProc cleans up auxiliary data related to returned
2017backendTexture. The caller must delete returned backendTexture after use.
Cary Clarka560c472017-11-27 10:44:06 -05002018
Cary Clark56356312018-02-08 14:45:18 -05002019If Image is both texture backed and singly referenced, image is returned in
2020backendTexture without conversion or making a copy. Image is singly referenced
2021if its was transferred solely using std::move().
2022
2023If Image is not texture backed, returns texture with Image contents.
Cary Clarka560c472017-11-27 10:44:06 -05002024
Cary Clark61ca7c52018-01-02 11:34:14 -05002025#Param context GPU_Context ##
Cary Clark56356312018-02-08 14:45:18 -05002026#Param image Image used for texture ##
Cary Clark682c58d2018-05-16 07:07:07 -04002027#Param backendTexture storage for back-end texture ##
Cary Clark56356312018-02-08 14:45:18 -05002028#Param backendTextureReleaseProc storage for clean up function ##
Cary Clarka560c472017-11-27 10:44:06 -05002029
Cary Clark682c58d2018-05-16 07:07:07 -04002030#Return true if back-end texture was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002031
2032#Example
Cary Clark56356312018-02-08 14:45:18 -05002033#Platform gpu
2034#Height 64
2035#Function
Brian Salomon67f85842018-02-09 08:50:22 -05002036static sk_sp<SkImage> create_gpu_image(GrContext* grContext) {
2037 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
2038 auto surface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info));
2039 SkCanvas* canvas = surface->getCanvas();
2040 canvas->clear(SK_ColorWHITE);
2041 SkPaint paint;
2042 paint.setColor(SK_ColorBLACK);
2043 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
2044 return surface->makeImageSnapshot();
2045}
2046##
2047
Cary Clark682c58d2018-05-16 07:07:07 -04002048void draw(SkCanvas* canvas) {
Brian Salomon67f85842018-02-09 08:50:22 -05002049 GrContext* grContext = canvas->getGrContext();
2050 if (!grContext) {
2051 return;
2052 }
2053 sk_sp<SkImage> backEndImage = create_gpu_image(grContext);
2054 canvas->drawImage(backEndImage, 0, 0);
2055 GrBackendTexture texture;
2056 SkImage::BackendTextureReleaseProc proc;
2057 if (!SkImage::MakeBackendTextureFromSkImage(grContext, std::move(backEndImage),
2058 &texture, &proc)) {
2059 return;
2060 }
2061 sk_sp<SkImage> i2 = SkImage::MakeFromTexture(grContext, texture, kTopLeft_GrSurfaceOrigin,
2062 kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
2063 canvas->drawImage(i2, 30, 30);
Cary Clark56356312018-02-08 14:45:18 -05002064}
Cary Clarka560c472017-11-27 10:44:06 -05002065##
2066
Cary Clark56356312018-02-08 14:45:18 -05002067#SeeAlso MakeFromTexture makeTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002068
2069#Method ##
2070
2071# ------------------------------------------------------------------------------
2072
2073#Enum LegacyBitmapMode
Cary Clark56356312018-02-08 14:45:18 -05002074#Deprecated soon
Cary Clarka560c472017-11-27 10:44:06 -05002075#Code
2076 enum LegacyBitmapMode {
2077 kRO_LegacyBitmapMode,
Cary Clarka560c472017-11-27 10:44:06 -05002078 };
2079##
2080
Cary Clarka560c472017-11-27 10:44:06 -05002081#Const kRO_LegacyBitmapMode 0
Cary Clark682c58d2018-05-16 07:07:07 -04002082#Line # returned bitmap is read-only and immutable ##
Cary Clarka560c472017-11-27 10:44:06 -05002083##
Cary Clarka560c472017-11-27 10:44:06 -05002084
2085#Enum ##
2086
2087# ------------------------------------------------------------------------------
2088
Cary Clark56356312018-02-08 14:45:18 -05002089#Method bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode = kRO_LegacyBitmapMode) const
Cary Clark4855f782018-02-06 09:41:53 -05002090#In Constructor
2091#Line # returns as Raster_Bitmap ##
Cary Clarkac47b882018-01-11 10:35:44 -05002092Creates raster Bitmap with same pixels as Image. If legacyBitmapMode is
2093kRO_LegacyBitmapMode, returned bitmap is read-only and immutable.
2094Returns true if Bitmap is stored in bitmap. Returns false and resets bitmap if
2095Bitmap write did not succeed.
Cary Clarka560c472017-11-27 10:44:06 -05002096
Cary Clark3cd22cc2017-12-01 11:49:58 -05002097#Param bitmap storage for legacy Bitmap ##
Cary Clark56356312018-02-08 14:45:18 -05002098#Param legacyBitmapMode to be deprecated ##
Cary Clarka560c472017-11-27 10:44:06 -05002099
Cary Clark3cd22cc2017-12-01 11:49:58 -05002100#Return true if Bitmap was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002101
2102#Example
Cary Clark56356312018-02-08 14:45:18 -05002103#Image 4
2104#Platform gpu
Brian Salomon67f85842018-02-09 08:50:22 -05002105 SkBitmap bitImage;
2106 if (image->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2107 canvas->drawBitmap(bitImage, 0, 0);
2108 }
2109 GrContext* grContext = canvas->getGrContext();
2110 if (!grContext) {
2111 return;
2112 }
2113 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(grContext, backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04002114 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
2115 nullptr));
Brian Salomon67f85842018-02-09 08:50:22 -05002116 canvas->drawImage(textureImage, 45, 45);
2117 if (textureImage->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2118 canvas->drawBitmap(bitImage, 90, 90);
2119 }
Cary Clarka560c472017-11-27 10:44:06 -05002120##
2121
Cary Clark56356312018-02-08 14:45:18 -05002122#SeeAlso MakeRasterData makeRasterImage makeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002123
2124#Method ##
2125
2126# ------------------------------------------------------------------------------
2127
2128#Method bool isLazyGenerated() const
Cary Clark4855f782018-02-06 09:41:53 -05002129#In Property
2130#Line # returns if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002131Returns true if Image is backed by an image-generator or other service that creates
2132and caches its pixels or texture on-demand.
2133
Cary Clark2f466242017-12-11 16:03:17 -05002134#Return true if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002135
2136#Example
Cary Clark2f466242017-12-11 16:03:17 -05002137#Height 80
2138#Function
2139class TestImageGenerator : public SkImageGenerator {
2140public:
2141 TestImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(10, 10)) {}
2142 ~TestImageGenerator() override {}
2143protected:
2144 bool onGetPixels(const SkImageInfo& info, void* pixelPtr, size_t rowBytes,
2145 const Options& options) override {
2146 SkPMColor* pixels = static_cast<SkPMColor*>(pixelPtr);
2147 for (int y = 0; y < info.height(); ++y) {
2148 for (int x = 0; x < info.width(); ++x) {
2149 pixels[y * info.width() + x] = 0xff223344 + y * 0x000C0811;
2150 }
2151 }
2152 return true;
2153 }
2154};
2155##
2156void draw(SkCanvas* canvas) {
2157 auto gen = std::unique_ptr<TestImageGenerator>(new TestImageGenerator());
2158 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
2159 SkString lazy(image->isLazyGenerated() ? "is lazy" : "not lazy");
2160 canvas->scale(8, 8);
2161 canvas->drawImage(image, 0, 0, nullptr);
2162 SkPaint paint;
2163 paint.setTextSize(4);
2164 canvas->drawString(lazy, 2, 5, paint);
2165}
Cary Clarka560c472017-11-27 10:44:06 -05002166##
2167
Cary Clarkf5404bb2018-01-05 12:10:09 -05002168#Example
2169#Image 5
2170#Platform gpu
2171void draw(SkCanvas* canvas) {
2172 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2173 if (nullptr == image) {
2174 return;
2175 }
2176 SkPaint paint;
2177 paint.setAntiAlias(true);
2178 paint.setTextAlign(SkPaint::kCenter_Align);
2179 canvas->drawImage(image, 0, 0);
2180 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
2181 canvas->drawString(
2182 image->isLazyGenerated() ? "is lazily generated" : "not lazily generated",
2183 image->width() / 2, image->height() * 3 / 4, paint);
2184 };
2185 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2186 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04002187 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
2188 nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05002189 drawImage(image, "image");
2190 canvas->translate(image->width(), 0);
2191 drawImage(bitmapImage, "source");
2192 canvas->translate(-image->width(), image->height());
2193 drawImage(textureImage, "backEndTexture");
2194}
2195##
2196
Cary Clarkac47b882018-01-11 10:35:44 -05002197#SeeAlso isTextureBacked MakeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002198
2199#Method ##
2200
2201# ------------------------------------------------------------------------------
2202
2203#Method sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target,
2204 SkTransferFunctionBehavior premulBehavior) const
Cary Clark4855f782018-02-06 09:41:53 -05002205#In Constructor
2206#Line # creates Image matching Color_Space if possible ##
Cary Clarka560c472017-11-27 10:44:06 -05002207
Cary Clarkac47b882018-01-11 10:35:44 -05002208Creates Image in target Color_Space.
2209Returns nullptr if Image could not be created.
Cary Clarka560c472017-11-27 10:44:06 -05002210
Cary Clarkac47b882018-01-11 10:35:44 -05002211Returns original Image if it is in target Color_Space.
2212Otherwise, converts pixels from Image Color_Space to target Color_Space.
2213If Image colorSpace returns nullptr, Image Color_Space is assumed to be sRGB.
2214
2215SkTransferFunctionBehavior is to be deprecated.
2216
2217Set premulBehavior to SkTransferFunctionBehavior::kRespect to convert Image
2218pixels to a linear space, before converting to destination Color_Type
Cary Clarka560c472017-11-27 10:44:06 -05002219and Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -05002220
Cary Clarkac47b882018-01-11 10:35:44 -05002221Set premulBehavior to SkTransferFunctionBehavior::kIgnore to treat Image
2222pixels as linear, when converting to destination Color_Type
2223and Color_Space, ignoring pixel encoding.
Cary Clarka560c472017-11-27 10:44:06 -05002224
Cary Clarkac47b882018-01-11 10:35:44 -05002225#Param target Color_Space describing color range of returned Image ##
2226#Param premulBehavior one of: SkTransferFunctionBehavior::kRespect,
2227 SkTransferFunctionBehavior::kIgnore
Cary Clarka560c472017-11-27 10:44:06 -05002228##
2229
Cary Clarkac47b882018-01-11 10:35:44 -05002230#Return created Image in target Color_Space ##
2231
2232#Example
2233#Image 5
2234#Set sRGB
2235 sk_sp<SkColorSpace> normalColorSpace = SkColorSpace::MakeRGB(
2236 SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kSRGB_Gamut);
2237 sk_sp<SkColorSpace> wackyColorSpace = normalColorSpace->makeColorSpin();
2238 for (auto colorSpace : { normalColorSpace, wackyColorSpace } ) {
2239 for (auto transfer : { SkTransferFunctionBehavior::kRespect,
2240 SkTransferFunctionBehavior::kIgnore } ) {
2241 sk_sp<SkImage> colorSpaced = image->makeColorSpace(colorSpace, transfer);
2242 canvas->drawImage(colorSpaced, 0, 0);
2243 canvas->translate(128, 0);
2244 }
2245 canvas->translate(-256, 128);
2246 }
2247##
2248
2249#SeeAlso MakeFromPixture MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05002250
2251#Method ##
2252
2253#Class SkImage ##
2254
2255#Topic Image ##