blob: cd338122637897ce6572455444708d498e1ebade [file] [log] [blame]
Cary Clarka560c472017-11-27 10:44:06 -05001#Topic Image
2#Alias Image_Reference
3
Cary Clark08895c42018-02-01 09:37:32 -05004#Subtopic Overview
Cary Clark4855f782018-02-06 09:41:53 -05005 #Subtopic Subtopic
Cary Clark08895c42018-02-01 09:37:32 -05006 #Populate
7 ##
8##
9
Cary Clarka560c472017-11-27 10:44:06 -050010#Class SkImage
11
Cary Clark61ca7c52018-01-02 11:34:14 -050012Image describes a two dimensional array of pixels to draw. The pixels may be
Cary Clark4855f782018-02-06 09:41:53 -050013decoded in a Raster_Bitmap, encoded in a Picture or compressed data stream,
Cary Clark61ca7c52018-01-02 11:34:14 -050014or located in GPU memory as a GPU_Texture.
15
16Image cannot be modified after it is created. Image may allocate additional
17storage as needed; for instance, an encoded Image may decode when drawn.
18
19Image width and height are greater than zero. Creating an Image with zero width
20or height returns Image equal to nullptr.
21
22Image may be created from Bitmap, Pixmap, Surface, Picture, encoded streams,
23GPU_Texture, YUV_ColorSpace data, or hardware buffer. Encoded streams supported
Cary Clark4855f782018-02-06 09:41:53 -050024include BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP. Supported encoding details
Cary Clark61ca7c52018-01-02 11:34:14 -050025vary with platform.
26
Cary Clark08895c42018-02-01 09:37:32 -050027#Subtopic Raster_Image
Cary Clark61ca7c52018-01-02 11:34:14 -050028#Alias Raster_Image
Cary Clark4855f782018-02-06 09:41:53 -050029#Line # pixels decoded in Raster_Bitmap ##
30Raster_Image pixels are decoded in a Raster_Bitmap. These pixels may be read
Cary Clark61ca7c52018-01-02 11:34:14 -050031directly and in most cases written to, although edited pixels may not be drawn
32if Image has been copied internally.
33##
34
Cary Clark08895c42018-02-01 09:37:32 -050035#Subtopic Texture_Image
36#Line # pixels located on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -050037Texture_Image are located on GPU and pixels are not accessible. Texture_Image
38are allocated optimally for best performance. Raster_Image may
39be drawn to GPU_Surface, but pixels are uploaded from CPU to GPU downgrading
40performance.
41##
42
Cary Clark08895c42018-02-01 09:37:32 -050043#Subtopic Lazy_Image
44#Line # deferred pixel buffer ##
Cary Clark61ca7c52018-01-02 11:34:14 -050045Lazy_Image defer allocating buffer for Image pixels and decoding stream until
46Image is drawn. Lazy_Image caches result if possible to speed up repeated
47drawing.
48##
Cary Clarka560c472017-11-27 10:44:06 -050049
Cary Clark4855f782018-02-06 09:41:53 -050050#Subtopic Related_Function
Cary Clark08895c42018-02-01 09:37:32 -050051#Populate
52##
Cary Clarka560c472017-11-27 10:44:06 -050053
Cary Clark4855f782018-02-06 09:41:53 -050054#Subtopic Class_or_Struct
Cary Clark08895c42018-02-01 09:37:32 -050055#Populate
Cary Clarka560c472017-11-27 10:44:06 -050056##
57
Cary Clark4855f782018-02-06 09:41:53 -050058#Subtopic Constructor
Cary Clark08895c42018-02-01 09:37:32 -050059#Populate
60##
Cary Clark5081eed2018-01-22 07:55:48 -050061
Cary Clark4855f782018-02-06 09:41:53 -050062#Subtopic Member_Function
Cary Clark08895c42018-02-01 09:37:32 -050063#Populate
64##
Cary Clarka560c472017-11-27 10:44:06 -050065
Cary Clarka560c472017-11-27 10:44:06 -050066# ------------------------------------------------------------------------------
67
68#Method static sk_sp<SkImage> MakeRasterCopy(const SkPixmap& pixmap)
Cary Clark4855f782018-02-06 09:41:53 -050069#In Constructor
70#Line # creates Image from Pixmap and copied pixels ##
Cary Clark2f466242017-12-11 16:03:17 -050071Creates Image from Pixmap and copy of pixels. Since pixels are copied, Pixmap
72pixels may be modified or deleted without affecting Image.
Cary Clarka560c472017-11-27 10:44:06 -050073
Cary Clark3cd22cc2017-12-01 11:49:58 -050074Image is returned if Pixmap is valid. Valid Pixmap parameters include:
75dimensions are greater than zero;
76each dimension fits in 29 bits;
77Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
78row bytes are large enough to hold one row of pixels;
79pixel address is not nullptr.
80
81#Param pixmap Image_Info, pixel address, and row bytes ##
82
83#Return copy of Pixmap pixels, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -050084
85#Example
Cary Clark2f466242017-12-11 16:03:17 -050086#Height 50
87#Description
88Draw a five by five bitmap, and draw a copy in an Image. Editing the pixmap
89alters the bitmap draw, but does not alter the Image draw since the Image
90contains a copy of the pixels.
91##
92 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
93 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
94 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
95 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
96 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
97 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
98 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
99 SkBitmap bitmap;
100 bitmap.installPixels(pixmap);
101 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
102 *pixmap.writable_addr8(2, 2) = 0x00;
103 canvas->scale(10, 10);
104 canvas->drawBitmap(bitmap, 0, 0);
105 canvas->drawImage(image, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500106##
107
Cary Clark3cd22cc2017-12-01 11:49:58 -0500108#SeeAlso MakeRasterData MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500109
110#Method ##
111
112# ------------------------------------------------------------------------------
113
Cary Clarkcc45cc72018-05-15 16:06:12 -0400114#Method static sk_sp<SkImage> MakeRasterData(const SkImageInfo& info, sk_sp<SkData> pixels, size_t rowBytes)
Cary Clark4855f782018-02-06 09:41:53 -0500115#In Constructor
116#Line # creates Image from Image_Info and shared pixels ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500117Creates Image from Image_Info, sharing pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500118
Cary Clark3cd22cc2017-12-01 11:49:58 -0500119Image is returned if Image_Info is valid. Valid Image_Info parameters include:
120dimensions are greater than zero;
121each dimension fits in 29 bits;
122Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
123rowBytes are large enough to hold one row of pixels;
124pixels is not nullptr, and contains enough data for Image.
125
126#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
127#Param pixels address or pixel storage ##
128#Param rowBytes size of pixel row or larger ##
129
130#Return Image sharing pixels, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500131
132#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500133#Image 3
134 size_t rowBytes = image->width() * SkColorTypeBytesPerPixel(kRGBA_8888_SkColorType);
135 sk_sp<SkData> data = SkData::MakeUninitialized(rowBytes * image->height());
136 SkImageInfo dstInfo = SkImageInfo::MakeN32(image->width(), image->height(),
137 kPremul_SkAlphaType);
138 image->readPixels(dstInfo, data->writable_data(), rowBytes, 0, 0, SkImage::kAllow_CachingHint);
139 sk_sp<SkImage> raw = SkImage::MakeRasterData(dstInfo.makeColorType(kRGBA_8888_SkColorType),
140 data, rowBytes);
141 canvas->drawImage(image, 0, 0);
142 canvas->drawImage(raw.get(), 128, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500143##
144
Cary Clark3cd22cc2017-12-01 11:49:58 -0500145#SeeAlso MakeRasterCopy MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500146
147#Method ##
148
149# ------------------------------------------------------------------------------
150
Cary Clark3cd22cc2017-12-01 11:49:58 -0500151#Typedef void* ReleaseContext
152
153Caller data passed to RasterReleaseProc; may be nullptr.
154
155#SeeAlso MakeFromRaster RasterReleaseProc
156
157##
158
Cary Clarka560c472017-11-27 10:44:06 -0500159#Typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext)
160
Cary Clark3cd22cc2017-12-01 11:49:58 -0500161Function called when Image no longer shares pixels. ReleaseContext is
162provided by caller when Image is created, and may be nullptr.
163
164#SeeAlso ReleaseContext MakeFromRaster
165
Cary Clarka560c472017-11-27 10:44:06 -0500166##
167
168#Method static sk_sp<SkImage> MakeFromRaster(const SkPixmap& pixmap,
169 RasterReleaseProc rasterReleaseProc,
170 ReleaseContext releaseContext)
Cary Clark4855f782018-02-06 09:41:53 -0500171#In Constructor
172#Line # creates Image from Pixmap, with release ##
Cary Clarka560c472017-11-27 10:44:06 -0500173
Cary Clark0c5f5462017-12-15 11:21:51 -0500174Creates Image from pixmap, sharing Pixmap pixels. Pixels must remain valid and
Cary Clark3cd22cc2017-12-01 11:49:58 -0500175unchanged until rasterReleaseProc is called. rasterReleaseProc is passed
176releaseContext when Image is deleted or no longer refers to pixmap pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500177
Cary Clark0c5f5462017-12-15 11:21:51 -0500178Pass nullptr for rasterReleaseProc to share Pixmap without requiring a callback
179when Image is released. Pass nullptr for releaseContext if rasterReleaseProc
180does not require state.
181
Cary Clark3cd22cc2017-12-01 11:49:58 -0500182Image is returned if pixmap is valid. Valid Pixmap parameters include:
183dimensions are greater than zero;
184each dimension fits in 29 bits;
185Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
186row bytes are large enough to hold one row of pixels;
187pixel address is not nullptr.
188
189#Param pixmap Image_Info, pixel address, and row bytes ##
Cary Clark0c5f5462017-12-15 11:21:51 -0500190#Param rasterReleaseProc function called when pixels can be released; or nullptr ##
191#Param releaseContext state passed to rasterReleaseProc; or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500192
Cary Clark0c5f5462017-12-15 11:21:51 -0500193#Return Image sharing pixmap ##
Cary Clarka560c472017-11-27 10:44:06 -0500194
195#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500196#Function
197static void releaseProc(const void* pixels, SkImage::ReleaseContext context) {
198 int* countPtr = static_cast<int*>(context);
199 *countPtr += 1;
200}
201##
202
203void draw(SkCanvas* canvas) {
204 SkColor color = 0;
205 SkPixmap pixmap(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), &color, 4);
206 int releaseCount = 0;
207 sk_sp<SkImage> image(SkImage::MakeFromRaster(pixmap, releaseProc, &releaseCount));
208 SkDebugf("before reset: %d\n", releaseCount);
209 image.reset();
210 SkDebugf("after reset: %d\n", releaseCount);
211}
212#StdOut
213before reset: 0
214after reset: 1
215##
Cary Clarka560c472017-11-27 10:44:06 -0500216##
217
Cary Clark3cd22cc2017-12-01 11:49:58 -0500218#SeeAlso MakeRasterCopy MakeRasterData MakeFromGenerator RasterReleaseProc ReleaseContext
Cary Clarka560c472017-11-27 10:44:06 -0500219
220#Method ##
221
222# ------------------------------------------------------------------------------
223
224#Method static sk_sp<SkImage> MakeFromBitmap(const SkBitmap& bitmap)
Cary Clark4855f782018-02-06 09:41:53 -0500225#In Constructor
226#Line # creates Image from Bitmap, sharing or copying pixels ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500227Creates Image from bitmap, sharing or copying bitmap pixels. If the bitmap
228is marked immutable, and its pixel memory is shareable, it may be shared
229instead of copied.
Cary Clarka560c472017-11-27 10:44:06 -0500230
Cary Clark3cd22cc2017-12-01 11:49:58 -0500231Image is returned if bitmap is valid. Valid Bitmap parameters include:
232dimensions are greater than zero;
233each dimension fits in 29 bits;
234Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
235row bytes are large enough to hold one row of pixels;
236pixel address is not nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500237
Cary Clark3cd22cc2017-12-01 11:49:58 -0500238#Param bitmap Image_Info, row bytes, and pixels ##
239
240#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500241
242#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500243#Description
244The first Bitmap is shared; writing to the pixel memory changes the first
245Image.
246The second Bitmap is marked immutable, and is copied; writing to the pixel
247memory does not alter the second Image.
248##
249#Height 50
250 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
251 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
252 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
253 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
254 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
255 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
256 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
257 SkBitmap bitmap;
258 bitmap.installPixels(pixmap);
259 sk_sp<SkImage> image1 = SkImage::MakeFromBitmap(bitmap);
260 bitmap.setImmutable();
261 sk_sp<SkImage> image2 = SkImage::MakeFromBitmap(bitmap);
262 *pixmap.writable_addr8(2, 2) = 0x00;
263 canvas->scale(10, 10);
264 canvas->drawImage(image1, 0, 0);
265 canvas->drawImage(image2, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500266##
267
Cary Clark3cd22cc2017-12-01 11:49:58 -0500268#SeeAlso MakeFromRaster MakeRasterCopy MakeFromGenerator MakeRasterData
Cary Clarka560c472017-11-27 10:44:06 -0500269
270#Method ##
271
272# ------------------------------------------------------------------------------
273
274#Method static sk_sp<SkImage> MakeFromGenerator(std::unique_ptr<SkImageGenerator> imageGenerator,
275 const SkIRect* subset = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500276#In Constructor
277#Line # creates Image from a stream of data ##
Cary Clarka560c472017-11-27 10:44:06 -0500278
Cary Clark0c5f5462017-12-15 11:21:51 -0500279Creates Image from data returned by imageGenerator. Generated data is owned by Image and may not
280be shared or accessed.
Cary Clarka560c472017-11-27 10:44:06 -0500281
Cary Clark0c5f5462017-12-15 11:21:51 -0500282subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
283otherwise, subset must be contained by image bounds.
284
285Image is returned if generator data is valid. Valid data parameters vary by type of data
286and platform.
Cary Clarka560c472017-11-27 10:44:06 -0500287
Cary Clark3cd22cc2017-12-01 11:49:58 -0500288imageGenerator may wrap Picture data, codec data, or custom data.
289
290#Param imageGenerator stock or custom routines to retrieve Image ##
291#Param subset bounds of returned Image; may be nullptr ##
292
293#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500294
295#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500296#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500297#Description
298The generator returning Picture cannot be shared; std::move transfers ownership to generated Image.
299##
300 SkPictureRecorder recorder;
301 recorder.beginRecording(100, 100)->drawColor(SK_ColorRED);
302 auto picture = recorder.finishRecordingAsPicture();
303 auto gen = SkImageGenerator::MakeFromPicture({100, 100}, picture, nullptr, nullptr,
304 SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB());
305 sk_sp<SkImage> image = SkImage::MakeFromGenerator(std::move(gen));
306 canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500307##
308
Cary Clark3cd22cc2017-12-01 11:49:58 -0500309#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500310
311#Method ##
312
313# ------------------------------------------------------------------------------
314
315#Method static sk_sp<SkImage> MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500316#In Constructor
317#Line # creates Image from encoded data ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500318Creates Image from encoded data.
Cary Clark0c5f5462017-12-15 11:21:51 -0500319subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
320otherwise, subset must be contained by image bounds.
Cary Clarka560c472017-11-27 10:44:06 -0500321
Cary Clark3cd22cc2017-12-01 11:49:58 -0500322Image is returned if format of the encoded data is recognized and supported.
Cary Clark4855f782018-02-06 09:41:53 -0500323Recognized formats vary by platform.
Cary Clarka560c472017-11-27 10:44:06 -0500324
Cary Clark3cd22cc2017-12-01 11:49:58 -0500325#Param encoded data of Image to decode ##
326#Param subset bounds of returned Image; may be nullptr ##
327
328#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500329
Cary Clark61ca7c52018-01-02 11:34:14 -0500330#Example
331#Image 3
332int x = 0;
333for (int quality : { 100, 50, 10, 1} ) {
334 sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, quality);
335 sk_sp<SkImage> image = SkImage::MakeFromEncoded(encodedData);
336 canvas->drawImage(image, x, 0);
337 x += 64;
338}
Cary Clarka560c472017-11-27 10:44:06 -0500339##
340
Cary Clark3cd22cc2017-12-01 11:49:58 -0500341#SeeAlso MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500342
343#Method ##
344
345# ------------------------------------------------------------------------------
346
347#Typedef void (*TextureReleaseProc)(ReleaseContext releaseContext)
348
349##
350
351#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
352 const GrBackendTexture& backendTexture,
353 GrSurfaceOrigin origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500354 SkColorType colorType,
355 SkAlphaType alphaType,
356 sk_sp<SkColorSpace> colorSpace)
Cary Clarkf895a422018-02-27 09:54:21 -0500357#In Constructor
358#Line # creates Image from GPU_Texture ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500359Creates Image from GPU_Texture associated with context. Caller is responsible for
360managing the lifetime of GPU_Texture.
361
362Image is returned if format of backendTexture is recognized and supported.
363Recognized formats vary by GPU back-end.
364
365#Param context GPU_Context ##
366#Param backendTexture texture residing on GPU ##
367#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500368#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500369##
Cary Clark681287e2018-03-16 11:34:15 -0400370#Param alphaType one of: #list_of_alpha_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500371##
372#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500373
374#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500375
376#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500377#Image 3
378#Platform gpu
379#Height 128
380#Description
381A back-end texture has been created and uploaded to the GPU outside of this example.
382##
383GrContext* context = canvas->getGrContext();
384if (!context) {
385 return;
386}
387canvas->scale(.25f, .25f);
388int x = 0;
389for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500390 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark56356312018-02-08 14:45:18 -0500391 origin, kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
Cary Clark0c5f5462017-12-15 11:21:51 -0500392 canvas->drawImage(image, x, 0);
393x += 512;
394}
Cary Clarka560c472017-11-27 10:44:06 -0500395##
396
Cary Clark3cd22cc2017-12-01 11:49:58 -0500397#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500398
399#Method ##
400
401# ------------------------------------------------------------------------------
402
403#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
404 const GrBackendTexture& backendTexture,
405 GrSurfaceOrigin origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500406 SkColorType colorType,
Cary Clarka560c472017-11-27 10:44:06 -0500407 SkAlphaType alphaType,
408 sk_sp<SkColorSpace> colorSpace,
409 TextureReleaseProc textureReleaseProc,
410 ReleaseContext releaseContext)
411
Cary Clark61ca7c52018-01-02 11:34:14 -0500412Creates Image from GPU_Texture associated with context. GPU_Texture must stay
Cary Clark3cd22cc2017-12-01 11:49:58 -0500413valid and unchanged until textureReleaseProc is called. textureReleaseProc is
414passed releaseContext when Image is deleted or no longer refers to texture.
Cary Clarka560c472017-11-27 10:44:06 -0500415
Cary Clark3cd22cc2017-12-01 11:49:58 -0500416Image is returned if format of backendTexture is recognized and supported.
417Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500418
Cary Clark3cd22cc2017-12-01 11:49:58 -0500419#Param context GPU_Context ##
420#Param backendTexture texture residing on GPU ##
421#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500422#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500423##
Cary Clark681287e2018-03-16 11:34:15 -0400424#Param alphaType one of: #list_of_alpha_types#
Cary Clark3cd22cc2017-12-01 11:49:58 -0500425##
Cary Clark61ca7c52018-01-02 11:34:14 -0500426#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500427#Param textureReleaseProc function called when texture can be released ##
428#Param releaseContext state passed to textureReleaseProc ##
429
430#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500431
Cary Clark0c5f5462017-12-15 11:21:51 -0500432#ToDo
433This doesn't do anything clever with TextureReleaseProc because it may not get called
Cary Clark61ca7c52018-01-02 11:34:14 -0500434fwithin the lifetime of the example
Cary Clark0c5f5462017-12-15 11:21:51 -0500435##
436
Cary Clarka560c472017-11-27 10:44:06 -0500437#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500438#Platform gpu
439#Image 4
Cary Clark0c5f5462017-12-15 11:21:51 -0500440GrContext* context = canvas->getGrContext();
441if (!context) {
442 return;
443}
Cary Clarkac47b882018-01-11 10:35:44 -0500444auto debugster = [](SkImage::ReleaseContext releaseContext) -> void {
445 *((int *) releaseContext) += 128;
Cary Clark0c5f5462017-12-15 11:21:51 -0500446};
447int x = 0;
448for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500449 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark61ca7c52018-01-02 11:34:14 -0500450 origin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr, debugster, &x);
Cary Clark0c5f5462017-12-15 11:21:51 -0500451 canvas->drawImage(image, x, 0);
452 x += 128;
453}
Cary Clarka560c472017-11-27 10:44:06 -0500454##
455
Cary Clark3cd22cc2017-12-01 11:49:58 -0500456#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500457
458#Method ##
459
460# ------------------------------------------------------------------------------
461
462#Method static sk_sp<SkImage> MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> data,
463 bool buildMips,
Brian Osman584b5012018-04-13 15:48:26 -0400464 SkColorSpace* dstColorSpace,
465 bool limitToMaxTextureSize = false)
Cary Clark4855f782018-02-06 09:41:53 -0500466#In Constructor
467#Line # creates Image from encoded data, and uploads to GPU ##
Cary Clarka560c472017-11-27 10:44:06 -0500468
Cary Clark3cd22cc2017-12-01 11:49:58 -0500469Creates Image from encoded data. Image is uploaded to GPU back-end using context.
470
471Created Image is available to other GPU contexts, and is available across thread
472boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
473share resources.
474
475When Image is no longer referenced, context releases texture memory
Cary Clarka560c472017-11-27 10:44:06 -0500476asynchronously.
Cary Clarka560c472017-11-27 10:44:06 -0500477
Cary Clark3cd22cc2017-12-01 11:49:58 -0500478Texture decoded from data is uploaded to match Surface created with
479dstColorSpace. Color_Space of Image is determined by encoded data.
Cary Clarka560c472017-11-27 10:44:06 -0500480
Cary Clark3cd22cc2017-12-01 11:49:58 -0500481Image is returned if format of data is recognized and supported, and if context
482supports moving resources. Recognized formats vary by platform and GPU back-end.
483
Cary Clark61ca7c52018-01-02 11:34:14 -0500484Image is returned using MakeFromEncoded if context is nullptr or does not support
485moving resources between contexts.
486
Cary Clark3cd22cc2017-12-01 11:49:58 -0500487#Param context GPU_Context ##
488#Param data Image to decode ##
489#Param buildMips create Image as Mip_Map if true ##
490#Param dstColorSpace range of colors of matching Surface on GPU ##
Brian Osman584b5012018-04-13 15:48:26 -0400491#Param limitToMaxTextureSize downscale image to GPU maximum texture size, if necessary ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500492
493#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500494
495#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500496#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500497#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500498GrContext* context = canvas->getGrContext();
499sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, 100);
500sk_sp<SkImage> image = SkImage::MakeCrossContextFromEncoded(context,
501 encodedData, false, nullptr);
502canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500503##
504
Cary Clark3cd22cc2017-12-01 11:49:58 -0500505#SeeAlso MakeCrossContextFromPixmap
506
507#Method ##
508
509# ------------------------------------------------------------------------------
510
511#Method static sk_sp<SkImage> MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap,
512 bool buildMips,
Brian Osman584b5012018-04-13 15:48:26 -0400513 SkColorSpace* dstColorSpace,
514 bool limitToMaxTextureSize = false)
Cary Clark4855f782018-02-06 09:41:53 -0500515#In Constructor
516#Line # creates Image from Pixmap, and uploads to GPU ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500517
518Creates Image from pixmap. Image is uploaded to GPU back-end using context.
519
520Created Image is available to other GPU contexts, and is available across thread
521boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
522share resources.
523
524When Image is no longer referenced, context releases texture memory
525asynchronously.
526
527Texture created from pixmap is uploaded to match Surface created with
528dstColorSpace. Color_Space of Image is determined by pixmap.colorSpace().
529
Cary Clark61ca7c52018-01-02 11:34:14 -0500530Image is returned referring to GPU back-end if context is not nullptr,
531format of data is recognized and supported, and if context supports moving
532resources between contexts. Otherwise, pixmap pixel data is copied and Image
533as returned in raster format if possible; nullptr may be returned.
534Recognized GPU formats vary by platform and GPU back-end.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500535
536#Param context GPU_Context ##
537#Param pixmap Image_Info, pixel address, and row bytes ##
538#Param buildMips create Image as Mip_Map if true ##
539#Param dstColorSpace range of colors of matching Surface on GPU ##
Brian Osman584b5012018-04-13 15:48:26 -0400540#Param limitToMaxTextureSize downscale image to GPU maximum texture size, if necessary ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500541
542#Return created Image, or nullptr ##
543
544#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500545#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500546#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500547GrContext* context = canvas->getGrContext();
548SkPixmap pixmap;
549if (source.peekPixels(&pixmap)) {
550 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(context, pixmap,
551 false, nullptr);
552 canvas->drawImage(image, 0, 0);
553}
Cary Clark3cd22cc2017-12-01 11:49:58 -0500554##
555
556#SeeAlso MakeCrossContextFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500557
558#Method ##
559
560# ------------------------------------------------------------------------------
561
562#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
563 const GrBackendTexture& backendTexture,
564 GrSurfaceOrigin surfaceOrigin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500565 SkColorType colorType,
566 SkAlphaType alphaType = kPremul_SkAlphaType,
567 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500568#In Constructor
569#Line # creates Image from GPU_Texture, managed internally ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500570Creates Image from backendTexture associated with context. backendTexture and
571returned Image are managed internally, and are released when no longer needed.
Cary Clarka560c472017-11-27 10:44:06 -0500572
Cary Clark3cd22cc2017-12-01 11:49:58 -0500573Image is returned if format of backendTexture is recognized and supported.
574Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500575
Cary Clark3cd22cc2017-12-01 11:49:58 -0500576#Param context GPU_Context ##
577#Param backendTexture texture residing on GPU ##
578#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500579#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500580##
Cary Clark681287e2018-03-16 11:34:15 -0400581#Param alphaType one of: #list_of_alpha_types#
Cary Clark3cd22cc2017-12-01 11:49:58 -0500582##
Cary Clark61ca7c52018-01-02 11:34:14 -0500583#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500584
585#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500586
587#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500588#Image 5
589#Platform gpu
Cary Clark61ca7c52018-01-02 11:34:14 -0500590 if (!canvas->getGrContext()) {
591 return;
592 }
593 canvas->scale(.5f, .5f);
594 canvas->clear(0x7f3f5f7f);
595 int x = 0, y = 0;
596 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
597 for (auto alpha : { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType } ) {
598 sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(canvas->getGrContext(),
599 backEndTexture, origin,
600 kRGBA_8888_SkColorType, alpha);
601 canvas->drawImage(image, x, y);
602 x += 160;
603 }
604 x -= 160 * 3;
605 y += 256;
606 }
Cary Clarka560c472017-11-27 10:44:06 -0500607##
608
Cary Clark61ca7c52018-01-02 11:34:14 -0500609#SeeAlso MakeFromTexture MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500610
611#Method ##
612
613# ------------------------------------------------------------------------------
614
615#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400616 const GrBackendTexture yuvTextures[3],
Cary Clarka560c472017-11-27 10:44:06 -0500617 GrSurfaceOrigin surfaceOrigin,
618 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500619#In Constructor
620#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500621
Brian Salomon6a426c12018-03-15 12:16:02 -0400622Creates Image from copy of yuvTextures, an array of textures on GPU.
623yuvTextures contain pixels for YUV planes of Image. Returned Image has the dimensions
624yuvTextures[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500625
Cary Clark61ca7c52018-01-02 11:34:14 -0500626#Param context GPU_Context ##
627#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
628 kRec709_SkYUVColorSpace
629##
Brian Salomon6a426c12018-03-15 12:16:02 -0400630#Param yuvTextures array of YUV textures on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500631#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
632#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500633
Cary Clark61ca7c52018-01-02 11:34:14 -0500634#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500635
Cary Clark61ca7c52018-01-02 11:34:14 -0500636# seems too complicated to create an example for this
637#ToDo
638should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500639##
640
Cary Clark61ca7c52018-01-02 11:34:14 -0500641#NoExample
642##
643
644#SeeAlso MakeFromNV12TexturesCopy
645
646#Method ##
647
648# ------------------------------------------------------------------------------
649
Cary Clarka560c472017-11-27 10:44:06 -0500650#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
651 SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400652 const GrBackendTexture nv12Textures[2],
Cary Clarka560c472017-11-27 10:44:06 -0500653 GrSurfaceOrigin surfaceOrigin,
654 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500655#In Constructor
Brian Salomon6a426c12018-03-15 12:16:02 -0400656#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500657
Cary Clark681287e2018-03-16 11:34:15 -0400658Creates Image from copy of nv12Textures, an array of textures on GPU.
Brian Salomon6a426c12018-03-15 12:16:02 -0400659nv12Textures[0] contains pixels for YUV_Component_Y plane.
660nv12Textures[1] contains pixels for YUV_Component_U plane,
Cary Clark61ca7c52018-01-02 11:34:14 -0500661followed by pixels for YUV_Component_V plane.
Cary Clark681287e2018-03-16 11:34:15 -0400662Returned Image has the dimensions nv12Textures[2].
663yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500664
Cary Clark61ca7c52018-01-02 11:34:14 -0500665#Param context GPU_Context ##
666#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
667 kRec709_SkYUVColorSpace
668##
Brian Salomon6a426c12018-03-15 12:16:02 -0400669#Param nv12Textures array of YUV textures on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500670#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
671#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500672
Cary Clark61ca7c52018-01-02 11:34:14 -0500673#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500674
Cary Clark61ca7c52018-01-02 11:34:14 -0500675# seems too complicated to create an example for this
676#ToDo
677should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500678##
679
Cary Clark61ca7c52018-01-02 11:34:14 -0500680#NoExample
681##
682
683#SeeAlso MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500684
685#Method ##
686
687# ------------------------------------------------------------------------------
688
Cary Clark4855f782018-02-06 09:41:53 -0500689# currently uncalled by any test or client ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500690#Bug 7424
Cary Clark61ca7c52018-01-02 11:34:14 -0500691
Cary Clark56356312018-02-08 14:45:18 -0500692#EnumClass BitDepth
Cary Clarka560c472017-11-27 10:44:06 -0500693
694#Code
Cary Clark61ca7c52018-01-02 11:34:14 -0500695 enum class BitDepth {
Cary Clarka560c472017-11-27 10:44:06 -0500696 kU8,
697 kF16,
698 };
699##
700
701#Const kU8 0
Cary Clark61ca7c52018-01-02 11:34:14 -0500702Use 8 bits per Color_ARGB component using unsigned integer format.
Cary Clarka560c472017-11-27 10:44:06 -0500703##
704#Const kF16 1
Cary Clark61ca7c52018-01-02 11:34:14 -0500705Use 16 bits per Color_ARGB component using half-precision floating point format.
Cary Clarka560c472017-11-27 10:44:06 -0500706##
707
Cary Clark61ca7c52018-01-02 11:34:14 -0500708#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500709##
710
Cary Clark61ca7c52018-01-02 11:34:14 -0500711#SeeAlso MakeFromPicture
Cary Clarka560c472017-11-27 10:44:06 -0500712
Cary Clark56356312018-02-08 14:45:18 -0500713#EnumClass ##
Cary Clarka560c472017-11-27 10:44:06 -0500714
715# ------------------------------------------------------------------------------
716
717#Method static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
718 const SkMatrix* matrix, const SkPaint* paint,
719 BitDepth bitDepth,
720 sk_sp<SkColorSpace> colorSpace)
Cary Clark4855f782018-02-06 09:41:53 -0500721#In Constructor
722#Line # creates Image from Picture ##
Cary Clarka560c472017-11-27 10:44:06 -0500723
Cary Clark61ca7c52018-01-02 11:34:14 -0500724Creates Image from picture. Returned Image width and height are set by dimensions.
725Image draws picture with matrix and paint, set to bitDepth and colorSpace.
Cary Clarka560c472017-11-27 10:44:06 -0500726
Cary Clark61ca7c52018-01-02 11:34:14 -0500727If matrix is nullptr, draws with identity Matrix. If paint is nullptr, draws
728with default Paint. colorSpace may be nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500729
Cary Clark61ca7c52018-01-02 11:34:14 -0500730#Param picture stream of drawing commands ##
731#Param dimensions width and height ##
732#Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ##
733#Param paint Paint to apply transparency, filtering, and so on; may be nullptr ##
734#Param bitDepth 8 bit integer or 16 bit float: per component ##
735#Param colorSpace range of colors; may be nullptr ##
736
737#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500738
739#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500740 SkPaint paint;
741 SkPictureRecorder recorder;
742 SkCanvas* recordingCanvas = recorder.beginRecording(50, 50);
743 for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) {
744 paint.setColor(color);
745 recordingCanvas->drawRect({10, 10, 30, 40}, paint);
746 recordingCanvas->translate(10, 10);
747 recordingCanvas->scale(1.2f, 1.4f);
748 }
749 sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
750 int x = 0, y = 0;
751 for (auto alpha : { 70, 140, 210 } ) {
752 paint.setAlpha(alpha);
753 auto srgbColorSpace = SkColorSpace::MakeSRGB();
754 sk_sp<SkImage> image = SkImage::MakeFromPicture(playback, {50, 50}, nullptr, &paint,
755 SkImage::BitDepth::kU8, srgbColorSpace);
756 canvas->drawImage(image, x, y);
757 x += 70; y += 70;
758 }
Cary Clarka560c472017-11-27 10:44:06 -0500759##
760
Cary Clark61ca7c52018-01-02 11:34:14 -0500761#SeeAlso SkCanvas::drawPicture
Cary Clarka560c472017-11-27 10:44:06 -0500762
763#Method ##
764
765# ------------------------------------------------------------------------------
766
767#Method static sk_sp<SkImage> MakeFromAHardwareBuffer(AHardwareBuffer* hardwareBuffer,
768 SkAlphaType alphaType = kPremul_SkAlphaType,
769 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500770#In Constructor
771#Line # creates Image from Android hardware buffer ##
Cary Clarka560c472017-11-27 10:44:06 -0500772
Cary Clark4855f782018-02-06 09:41:53 -0500773#Bug 7447
Cary Clarka560c472017-11-27 10:44:06 -0500774
Cary Clark61ca7c52018-01-02 11:34:14 -0500775Creates Image from Android hardware buffer.
776Returned Image takes a reference on the buffer.
Cary Clarka560c472017-11-27 10:44:06 -0500777
Cary Clark61ca7c52018-01-02 11:34:14 -0500778Only available on Android, when __ANDROID_API__ is defined to be 26 or greater.
Cary Clarka560c472017-11-27 10:44:06 -0500779
Cary Clark61ca7c52018-01-02 11:34:14 -0500780#Param hardwareBuffer AHardwareBuffer Android hardware buffer ##
Cary Clark681287e2018-03-16 11:34:15 -0400781#Param alphaType one of: #list_of_alpha_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500782##
783#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500784
Cary Clark61ca7c52018-01-02 11:34:14 -0500785#Return created Image, or nullptr ##
786
787#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500788##
789
Cary Clark61ca7c52018-01-02 11:34:14 -0500790#SeeAlso MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -0500791
792#Method ##
793
794# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -0500795#Subtopic Property
796#Populate
797#Line # values and attributes ##
798##
Cary Clarka560c472017-11-27 10:44:06 -0500799
800#Method int width() const
Cary Clark4855f782018-02-06 09:41:53 -0500801#In Property
802#Line # returns pixel column count ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500803Returns pixel count in each row.
804
805#Return pixel width in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500806
807#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500808#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500809#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500810 canvas->translate(10, 10);
811 canvas->drawImage(image, 0, 0);
812 canvas->translate(0, image->height());
813 SkPaint paint;
814 paint.setTextAlign(SkPaint::kCenter_Align);
815 canvas->drawLine(0, 10, image->width(), 10, paint);
816 canvas->drawString("width", image->width() / 2, 25, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500817##
818
Cary Clark61ca7c52018-01-02 11:34:14 -0500819#SeeAlso dimensions() height()
Cary Clarka560c472017-11-27 10:44:06 -0500820
821#Method ##
822
823# ------------------------------------------------------------------------------
824
825#Method int height() const
Cary Clark4855f782018-02-06 09:41:53 -0500826#In Property
827#Line # returns pixel row count ##
Cary Clark2f466242017-12-11 16:03:17 -0500828Returns pixel row count.
829
Cary Clark61ca7c52018-01-02 11:34:14 -0500830#Return pixel height in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500831
832#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500833#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500834#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500835 canvas->translate(10, 10);
836 canvas->drawImage(image, 0, 0);
837 canvas->translate(image->width(), 0);
838 SkPaint paint;
839 paint.setTextAlign(SkPaint::kCenter_Align);
840 paint.setVerticalText(true);
841 canvas->drawLine(10, 0, 10, image->height(), paint);
Cary Clarkac47b882018-01-11 10:35:44 -0500842 canvas->drawString("height", 25, image->height() / 2, paint);
843##
Cary Clarka560c472017-11-27 10:44:06 -0500844
Cary Clark61ca7c52018-01-02 11:34:14 -0500845#SeeAlso dimensions() width()
Cary Clarka560c472017-11-27 10:44:06 -0500846
847#Method ##
848
849# ------------------------------------------------------------------------------
850
851#Method SkISize dimensions() const
Cary Clark4855f782018-02-06 09:41:53 -0500852#In Property
853#Line # returns width() and height() ##
Cary Clark681287e2018-03-16 11:34:15 -0400854
Cary Clark2f466242017-12-11 16:03:17 -0500855Returns ISize { width(), height() }.
856
857#Return integral size of width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -0500858
859#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500860#Image 4
861 SkISize dimensions = image->dimensions();
862 SkIRect bounds = image->bounds();
863 SkIRect dimensionsAsBounds = SkIRect::MakeSize(dimensions);
864 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clark681287e2018-03-16 11:34:15 -0400865#StdOut
866dimensionsAsBounds == bounds
867##
Cary Clarka560c472017-11-27 10:44:06 -0500868##
869
Cary Clark61ca7c52018-01-02 11:34:14 -0500870#SeeAlso height() width() bounds()
Cary Clarka560c472017-11-27 10:44:06 -0500871
872#Method ##
873
874# ------------------------------------------------------------------------------
875
876#Method SkIRect bounds() const
Cary Clark4855f782018-02-06 09:41:53 -0500877#In Property
878#Line # returns width() and height() as Rectangle ##
Cary Clark2f466242017-12-11 16:03:17 -0500879Returns IRect { 0, 0, width(), height() }.
880
881#Return integral rectangle from origin to width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -0500882
883#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500884#Height 128
885#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -0500886 SkIRect bounds = image->bounds();
Cary Clarkac47b882018-01-11 10:35:44 -0500887 for (int x : { 0, bounds.width() } ) {
888 for (int y : { 0, bounds.height() } ) {
Cary Clark61ca7c52018-01-02 11:34:14 -0500889 canvas->drawImage(image, x, y);
890 }
891 }
Cary Clarka560c472017-11-27 10:44:06 -0500892##
893
Cary Clark61ca7c52018-01-02 11:34:14 -0500894#SeeAlso dimensions()
Cary Clarka560c472017-11-27 10:44:06 -0500895
896#Method ##
897
898# ------------------------------------------------------------------------------
899
900#Method uint32_t uniqueID() const
Cary Clark4855f782018-02-06 09:41:53 -0500901#In Property
902#Line # identifier for Image ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500903Returns value unique to image. Image contents cannot change after Image is
904created. Any operation to create a new Image will receive generate a new
905unique number.
906
907#Return unique identifier ##
Cary Clarka560c472017-11-27 10:44:06 -0500908
909#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500910#Image 5
911#Height 156
912 sk_sp<SkImage> subset = image->makeSubset({10, 20, 90, 100});
913 canvas->drawImage(image, 0, 0);
914 canvas->drawImage(subset, 128, 0);
915 SkPaint paint;
916 SkString s;
917 s.printf("original id: %d", image->uniqueID());
918 canvas->drawString(s, 20, image->height() + 20, paint);
919 s.printf("subset id: %d", subset->uniqueID());
920 canvas->drawString(s, 148, subset->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500921##
922
Cary Clark61ca7c52018-01-02 11:34:14 -0500923#SeeAlso isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -0500924
925#Method ##
926
927# ------------------------------------------------------------------------------
928
929#Method SkAlphaType alphaType() const
Cary Clark4855f782018-02-06 09:41:53 -0500930#In Property
931#Line # returns Alpha_Type ##
Cary Clark681287e2018-03-16 11:34:15 -0400932Returns Alpha_Type, one of: #list_of_alpha_types#.
Cary Clark61ca7c52018-01-02 11:34:14 -0500933
934Alpha_Type returned was a parameter to an Image constructor,
935or was parsed from encoded data.
936
937#Return Alpha_Type in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500938
939#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500940#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500941#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500942 const char* alphaTypeStr[] = { "Unknown", "Opaque", "Premul", "Unpremul" };
943 SkAlphaType alphaType = image->alphaType();
Cary Clarkac47b882018-01-11 10:35:44 -0500944 canvas->drawImage(image, 16, 0);
Cary Clark61ca7c52018-01-02 11:34:14 -0500945 SkPaint paint;
946 canvas->drawString(alphaTypeStr[(int) alphaType], 20, image->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500947##
948
Cary Clark61ca7c52018-01-02 11:34:14 -0500949#SeeAlso SkImageInfo::alphaType
Cary Clarka560c472017-11-27 10:44:06 -0500950
951#Method ##
952
953# ------------------------------------------------------------------------------
954
Greg Daniel56008aa2018-03-14 15:33:42 -0400955#Method SkColorType colorType() const
956#In Property
957#Line # returns Color_Type ##
958
959Returns Color_Type if known; otherwise, returns kUnknown_SkColorType.
960
961#Return Color_Type of Image ##
962
963#Example
964// incomplete
965##
966
967#SeeAlso SkImageInfo::colorType
968
969#Method ##
970
971# ------------------------------------------------------------------------------
972
Cary Clarka560c472017-11-27 10:44:06 -0500973#Method SkColorSpace* colorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -0500974#In Property
975#Line # returns Color_Space ##
Cary Clark2f466242017-12-11 16:03:17 -0500976Returns Color_Space, the range of colors, associated with Image. The
977reference count of Color_Space is unchanged. The returned Color_Space is
978immutable.
Cary Clarka560c472017-11-27 10:44:06 -0500979
Cary Clark61dfc3a2018-01-03 08:37:53 -0500980Color_Space returned was passed to an Image constructor,
981or was parsed from encoded data. Color_Space returned may be ignored when Image
982is drawn, depending on the capabilities of the Surface receiving the drawing.
Cary Clark2f466242017-12-11 16:03:17 -0500983
984#Return Color_Space in Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500985
986#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -0500987#Image 3
988#Set sRGB
989 SkPixmap pixmap;
990 source.peekPixels(&pixmap);
991 canvas->scale(.25f, .25f);
992 int y = 0;
993 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
994 SkColorSpace::kSRGB_RenderTargetGamma } ) {
995 int x = 0;
996 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
997 for (int index = 0; index < 2; ++index) {
998 pixmap.setColorSpace(colorSpace);
999 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1000 canvas->drawImage(image, x, y);
1001 colorSpace = image->colorSpace()->makeColorSpin();
1002 x += 512;
1003 }
1004 y += 512;
1005 }
Cary Clarka560c472017-11-27 10:44:06 -05001006##
1007
Cary Clark61dfc3a2018-01-03 08:37:53 -05001008#SeeAlso refColorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001009
1010#Method ##
1011
1012# ------------------------------------------------------------------------------
1013
1014#Method sk_sp<SkColorSpace> refColorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001015#In Property
1016#Line # returns Image_Info Color_Space ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001017Returns a smart pointer to Color_Space, the range of colors, associated with
1018Image. The smart pointer tracks the number of objects sharing this
1019SkColorSpace reference so the memory is released when the owners destruct.
1020
1021The returned SkColorSpace is immutable.
1022
1023Color_Space returned was passed to an Image constructor,
1024or was parsed from encoded data. Color_Space returned may be ignored when Image
1025is drawn, depending on the capabilities of the Surface receiving the drawing.
1026
1027#Return Color_Space in Image, or nullptr, wrapped in a smart pointer ##
Cary Clarka560c472017-11-27 10:44:06 -05001028
1029#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001030#Image 3
1031#Set sRGB
1032 SkPixmap pixmap;
1033 source.peekPixels(&pixmap);
1034 canvas->scale(.25f, .25f);
1035 int y = 0;
1036 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1037 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1038 int x = 0;
1039 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1040 for (int index = 0; index < 2; ++index) {
1041 pixmap.setColorSpace(colorSpace);
1042 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1043 canvas->drawImage(image, x, y);
1044 colorSpace = image->refColorSpace()->makeColorSpin();
1045 x += 512;
1046 }
1047 y += 512;
1048 }
Cary Clarka560c472017-11-27 10:44:06 -05001049##
1050
Cary Clark61dfc3a2018-01-03 08:37:53 -05001051#SeeAlso colorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001052
1053#Method ##
1054
1055# ------------------------------------------------------------------------------
1056
1057#Method bool isAlphaOnly() const
Cary Clark4855f782018-02-06 09:41:53 -05001058#In Property
1059#Line # returns if pixels represent a transparency mask ##
Cary Clark2f466242017-12-11 16:03:17 -05001060Returns true if Image pixels represent transparency only. If true, each pixel
1061is packed in 8 bits as defined by kAlpha_8_SkColorType.
Cary Clarka560c472017-11-27 10:44:06 -05001062
Cary Clark2f466242017-12-11 16:03:17 -05001063#Return true if pixels represent a transparency mask ##
Cary Clarka560c472017-11-27 10:44:06 -05001064
1065#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001066 uint8_t pmColors = 0;
1067 sk_sp<SkImage> image = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1), &pmColors, 1});
1068 SkDebugf("alphaOnly = %s\n", image->isAlphaOnly() ? "true" : "false");
1069#StdOut
1070alphaOnly = true
1071##
Cary Clarka560c472017-11-27 10:44:06 -05001072##
1073
Cary Clark61dfc3a2018-01-03 08:37:53 -05001074#SeeAlso alphaType isOpaque
Cary Clarka560c472017-11-27 10:44:06 -05001075
1076#Method ##
1077
1078# ------------------------------------------------------------------------------
1079
1080#Method bool isOpaque() const
Cary Clark4855f782018-02-06 09:41:53 -05001081#In Property
1082#Line # returns if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001083Returns true if pixels ignore their Alpha value and are treated as fully opaque.
Cary Clark2f466242017-12-11 16:03:17 -05001084
1085#Return true if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clarka560c472017-11-27 10:44:06 -05001086
1087#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001088 auto check_isopaque = [](const SkImageInfo& imageInfo) -> void {
1089 auto surface(SkSurface::MakeRaster(imageInfo));
1090 auto image(surface->makeImageSnapshot());
1091 SkDebugf("isOpaque = %s\n", image->isOpaque() ? "true" : "false");
1092 };
1093
1094 check_isopaque(SkImageInfo::MakeN32Premul(5, 5));
1095 check_isopaque(SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType));
1096#StdOut
1097isOpaque = false
1098isOpaque = true
1099##
Cary Clarka560c472017-11-27 10:44:06 -05001100##
1101
Cary Clark61dfc3a2018-01-03 08:37:53 -05001102#SeeAlso alphaType isAlphaOnly
Cary Clarka560c472017-11-27 10:44:06 -05001103
1104#Method ##
1105
1106# ------------------------------------------------------------------------------
1107
1108#Method sk_sp<SkShader> makeShader(SkShader::TileMode tileMode1, SkShader::TileMode tileMode2,
1109 const SkMatrix* localMatrix = nullptr) const
Cary Clark4855f782018-02-06 09:41:53 -05001110#In Constructor
1111#Line # creates Shader, Paint element that can tile Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001112
Cary Clark61dfc3a2018-01-03 08:37:53 -05001113Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1114SkShader::TileMode rules to fill drawn area outside Image. localMatrix permits
1115transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001116
Cary Clark61dfc3a2018-01-03 08:37:53 -05001117#Param tileMode1 tiling in x, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1118 SkShader::kMirror_TileMode
1119##
1120#Param tileMode2 tiling in y, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1121 SkShader::kMirror_TileMode
1122##
1123#Param localMatrix Image transformation, or nullptr ##
1124
1125#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001126
1127#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001128#Image 4
1129SkMatrix matrix;
1130matrix.setRotate(45);
1131SkPaint paint;
1132paint.setShader(image->makeShader(SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode,
1133 &matrix));
1134canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001135##
1136
Cary Clark61dfc3a2018-01-03 08:37:53 -05001137#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001138
1139#Method ##
1140
1141# ------------------------------------------------------------------------------
1142
1143#Method sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const
1144
Cary Clark61dfc3a2018-01-03 08:37:53 -05001145Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1146SkShader::kClamp_TileMode to fill drawn area outside Image. localMatrix permits
1147transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001148
Cary Clark61dfc3a2018-01-03 08:37:53 -05001149#Param localMatrix Image transformation, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001150
Cary Clark61dfc3a2018-01-03 08:37:53 -05001151#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001152
1153#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001154#Image 5
1155SkMatrix matrix;
1156matrix.setRotate(45);
1157matrix.postTranslate(125, 30);
1158SkPaint paint;
1159paint.setShader(image->makeShader(&matrix));
1160canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001161##
1162
Cary Clarkf5404bb2018-01-05 12:10:09 -05001163#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001164
1165#Method ##
1166
1167# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001168#Subtopic Pixels
1169#Populate
1170#Line # read and write pixel values ##
1171##
Cary Clarka560c472017-11-27 10:44:06 -05001172
1173#Method bool peekPixels(SkPixmap* pixmap) const
Cary Clark78de7512018-02-07 07:27:09 -05001174#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001175#Line # returns Pixmap if possible ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001176Copies Image pixel address, row bytes, and Image_Info to pixmap, if address
1177is available, and returns true. If pixel address is not available, return
1178false and leave pixmap unchanged.
Cary Clarka560c472017-11-27 10:44:06 -05001179
Cary Clarkf5404bb2018-01-05 12:10:09 -05001180#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
Cary Clarka560c472017-11-27 10:44:06 -05001181
Cary Clarkf5404bb2018-01-05 12:10:09 -05001182#Return true if Image has direct access to pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001183
1184#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001185 SkBitmap bitmap;
1186 bitmap.allocPixels(SkImageInfo::MakeN32Premul(12, 11));
1187 SkCanvas offscreen(bitmap);
1188 offscreen.clear(SK_ColorWHITE);
1189 SkPaint paint;
1190 offscreen.drawString("%", 1, 10, paint);
1191 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
1192 SkPixmap pixmap;
1193 if (image->peekPixels(&pixmap)) {
1194 const SkPMColor* pixels = pixmap.addr32();
1195 SkPMColor pmWhite = pixels[0];
1196 for (int y = 0; y < image->height(); ++y) {
1197 for (int x = 0; x < image->width(); ++x) {
1198 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
1199 }
1200 SkDebugf("\n");
1201 }
1202 }
1203#StdOut
1204------------
1205--xx----x---
1206-x--x--x----
1207-x--x--x----
1208-x--x-x-----
1209--xx-xx-xx--
1210-----x-x--x-
1211----x--x--x-
1212----x--x--x-
1213---x----xx--
1214------------
1215##
Cary Clarka560c472017-11-27 10:44:06 -05001216##
1217
Cary Clarkf5404bb2018-01-05 12:10:09 -05001218#SeeAlso readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001219
1220#Method ##
1221
1222# ------------------------------------------------------------------------------
1223
1224#Method GrTexture* getTexture() const
Cary Clark2f466242017-12-11 16:03:17 -05001225#Deprecated
Cary Clarka560c472017-11-27 10:44:06 -05001226#Method ##
1227
1228# ------------------------------------------------------------------------------
1229
1230#Method bool isTextureBacked() const
Cary Clark78de7512018-02-07 07:27:09 -05001231#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001232#Line # returns if Image was created from GPU_Texture ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001233Returns true the contents of Image was created on or uploaded to GPU memory,
1234and is available as a GPU_Texture.
Cary Clarka560c472017-11-27 10:44:06 -05001235
Cary Clarkf5404bb2018-01-05 12:10:09 -05001236#Return true if Image is a GPU_Texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001237
1238#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001239#Image 5
1240#Platform gpu
1241auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1242 if (nullptr == image) {
1243 return;
1244 }
1245 SkPaint paint;
1246 paint.setAntiAlias(true);
1247 paint.setTextAlign(SkPaint::kCenter_Align);
1248 canvas->drawImage(image, 0, 0);
1249 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1250 canvas->drawString(image->isTextureBacked() ? "is GPU texture" : "not GPU texture",
1251 image->width() / 2, image->height() * 3 / 4, paint);
1252};
1253sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1254sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001255 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1256 nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05001257drawImage(image, "image");
1258canvas->translate(image->width(), 0);
1259drawImage(bitmapImage, "source");
1260canvas->translate(-image->width(), image->height());
1261drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001262##
1263
Cary Clarkf5404bb2018-01-05 12:10:09 -05001264#SeeAlso MakeFromTexture isValid
Cary Clarka560c472017-11-27 10:44:06 -05001265
1266#Method ##
1267
1268# ------------------------------------------------------------------------------
1269
1270#Method bool isValid(GrContext* context) const
Cary Clark4855f782018-02-06 09:41:53 -05001271#In Property
1272#Line # returns if Image can draw to Raster_Surface or GPU_Context ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001273Returns true if Image can be drawn on either Raster_Surface or GPU_Surface.
1274If context is nullptr, tests if Image draws on Raster_Surface;
1275otherwise, tests if Image draws on GPU_Surface associated with context.
Cary Clarka560c472017-11-27 10:44:06 -05001276
Cary Clarkf5404bb2018-01-05 12:10:09 -05001277Image backed by GPU_Texture may become invalid if associated GrContext is
1278invalid. Lazy_Image may be invalid and may not draw to Raster_Surface or
1279GPU_Surface or both.
Cary Clarka560c472017-11-27 10:44:06 -05001280
Cary Clark61ca7c52018-01-02 11:34:14 -05001281#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05001282
Cary Clarkf5404bb2018-01-05 12:10:09 -05001283#Return true if Image can be drawn ##
Cary Clarka560c472017-11-27 10:44:06 -05001284
1285#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001286#Image 5
1287#Platform gpu
1288auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1289 if (nullptr == image) {
1290 return;
1291 }
1292 SkPaint paint;
1293 paint.setAntiAlias(true);
1294 paint.setTextAlign(SkPaint::kCenter_Align);
1295 canvas->drawImage(image, 0, 0);
1296 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1297 if (canvas->getGrContext()) {
1298 canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" :
1299 "not valid on GPU", image->width() / 2, image->height() * 5 / 8, paint);
1300 }
1301 canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" :
1302 "not valid on CPU", image->width() / 2, image->height() * 7 / 8, paint);
1303};
1304sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1305sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001306 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1307 nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05001308drawImage(image, "image");
1309canvas->translate(image->width(), 0);
1310drawImage(bitmapImage, "source");
1311canvas->translate(-image->width(), image->height());
1312drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001313##
1314
Cary Clarkf5404bb2018-01-05 12:10:09 -05001315#SeeAlso isTextureBacked isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001316
1317#Method ##
1318
1319# ------------------------------------------------------------------------------
1320
Brian Osmana0ca9092018-05-10 22:04:08 +00001321#Method GrBackendObject getTextureHandle(bool flushPendingGrContextIO,
1322 GrSurfaceOrigin* origin = nullptr) const
1323#Deprecated
1324#Method ##
1325
1326# ------------------------------------------------------------------------------
1327
Robert Phillipsc5509952018-04-04 15:54:55 -04001328#Method GrBackendTexture getBackendTexture(bool flushPendingGrContextIO,
1329 GrSurfaceOrigin* origin = nullptr) const
1330#In Property
1331#Line # returns GPU reference to Image as texture ##
1332
Cary Clarkba75aee2018-04-05 08:18:41 -04001333Retrieves the backend texture. If Image has no backend texture, an invalid
1334object is returned. Call GrBackendTexture::isValid to determine if the result
1335is valid.
1336
1337If flushPendingGrContextIO is true, completes deferred I/O operations.
Robert Phillipsc5509952018-04-04 15:54:55 -04001338
1339If origin in not nullptr, copies location of content drawn into Image.
1340
1341#Param flushPendingGrContextIO flag to flush outstanding requests ##
1342#Param origin storage for one of: kTopLeft_GrSurfaceOrigin,
1343 kBottomLeft_GrSurfaceOrigin; or nullptr
1344##
1345
Cary Clarkba75aee2018-04-05 08:18:41 -04001346#Return back-end API texture handle; invalid on failure ##
Robert Phillipsc5509952018-04-04 15:54:55 -04001347
Cary Clarkba75aee2018-04-05 08:18:41 -04001348#Example
1349#Image 3
1350#Platform gpu
Brian Osman584b5012018-04-13 15:48:26 -04001351 GrContext* grContext = canvas->getGrContext();
1352 if (!grContext) {
1353 canvas->drawString("GPU only!", 20, 40, SkPaint());
1354 return;
1355 }
1356 sk_sp<SkImage> imageFromBackend = SkImage::MakeFromAdoptedTexture(grContext, backEndTexture,
1357 kBottomLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1358 GrBackendTexture textureFromImage = imageFromBackend->getBackendTexture(false);
1359 if (!textureFromImage.isValid()) {
1360 return;
1361 }
1362 sk_sp<SkImage> imageFromTexture = SkImage::MakeFromAdoptedTexture(grContext, textureFromImage,
1363 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1364 canvas->drawImage(imageFromTexture, 0, 0);
Cary Clarkba75aee2018-04-05 08:18:41 -04001365 canvas->drawImage(imageFromBackend, 128, 128);
Robert Phillipsc5509952018-04-04 15:54:55 -04001366##
1367
1368#SeeAlso MakeFromTexture isTextureBacked
1369
1370#Method ##
1371
1372# ------------------------------------------------------------------------------
1373
Cary Clarka560c472017-11-27 10:44:06 -05001374#Enum CachingHint
1375
1376#Code
1377 enum CachingHint {
1378 kAllow_CachingHint,
1379 kDisallow_CachingHint,
1380 };
1381##
1382
Cary Clarkac47b882018-01-11 10:35:44 -05001383CachingHint selects whether Skia may internally cache Bitmaps generated by
1384decoding Image, or by copying Image from GPU to CPU. The default behavior
1385allows caching Bitmaps.
1386
1387Choose kDisallow_CachingHint if Image pixels are to be used only once, or
1388if Image pixels reside in a cache outside of Skia, or to reduce memory pressure.
1389
1390Choosing kAllow_CachingHint does not ensure that pixels will be cached.
1391Image pixels may not be cached if memory requirements are too large or
1392pixels are not accessible.
Cary Clarka560c472017-11-27 10:44:06 -05001393
1394#Const kAllow_CachingHint 0
Cary Clarkac47b882018-01-11 10:35:44 -05001395Allows Skia to internally cache decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001396##
1397#Const kDisallow_CachingHint 1
Cary Clarkac47b882018-01-11 10:35:44 -05001398Disallows Skia from internally caching decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001399##
1400
Cary Clarkac47b882018-01-11 10:35:44 -05001401#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001402##
1403
Cary Clarkac47b882018-01-11 10:35:44 -05001404#SeeAlso readPixels scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001405
1406#Enum ##
1407
1408# ------------------------------------------------------------------------------
1409
1410#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1411 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001412#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001413#Line # copies and converts pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001414
Cary Clarkac47b882018-01-11 10:35:44 -05001415Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY),
1416and does not exceed Image (width(), height()).
1417
1418dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
1419destination. dstRowBytes specifics the gap from one destination row to the next.
1420Returns true if pixels are copied. Returns false if:
1421#List
1422# dstInfo.addr() equals nullptr ##
1423# dstRowBytes is less than dstInfo.minRowBytes ##
1424# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001425##
1426
Cary Clarkac47b882018-01-11 10:35:44 -05001427Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1428kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1429If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace must match.
1430If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType must
1431match. If Image Color_Space is nullptr, dstInfo.colorSpace must match. Returns
1432false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001433
Cary Clarkac47b882018-01-11 10:35:44 -05001434srcX and srcY may be negative to copy only top or left of source. Returns
1435false if width() or height() is zero or negative.
1436Returns false if
1437#Formula
1438abs(srcX) >= Image width()
1439##
1440, or if
1441#Formula
1442abs(srcY) >= Image height()
1443##
1444.
Cary Clarka560c472017-11-27 10:44:06 -05001445
Cary Clarkac47b882018-01-11 10:35:44 -05001446If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1447If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1448
1449#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1450#Param dstPixels destination pixel storage ##
1451#Param dstRowBytes destination row length ##
1452#Param srcX column index whose absolute value is less than width() ##
1453#Param srcY row index whose absolute value is less than height() ##
1454#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1455
1456#Return true if pixels are copied to dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001457
1458#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001459#Image 3
1460 canvas->scale(.5f, .5f);
1461 const int width = 32;
1462 const int height = 32;
1463 std::vector<int32_t> dstPixels;
1464 dstPixels.resize(height * width * 4);
1465 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1466 for (int y = 0; y < 512; y += height ) {
1467 for (int x = 0; x < 512; x += width ) {
1468 if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) {
1469 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
1470 SkBitmap bitmap;
1471 bitmap.installPixels(dstPixmap);
1472 canvas->drawBitmap(bitmap, 0, 0);
1473 }
1474 canvas->translate(48, 0);
1475 }
1476 canvas->translate(-16 * 48, 48);
1477 }
Cary Clarka560c472017-11-27 10:44:06 -05001478##
1479
Cary Clarkac47b882018-01-11 10:35:44 -05001480#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001481
1482#Method ##
1483
1484# ------------------------------------------------------------------------------
1485
1486#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY,
1487 CachingHint cachingHint = kAllow_CachingHint) const
1488
Cary Clarkac47b882018-01-11 10:35:44 -05001489Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and
1490does not exceed Image (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001491
Cary Clarkac47b882018-01-11 10:35:44 -05001492dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
1493and row bytes of destination. dst.rowBytes specifics the gap from one destination
1494row to the next. Returns true if pixels are copied. Returns false if:
1495#List
1496# dst pixel storage equals nullptr ##
1497# dst.rowBytes is less than SkImageInfo::minRowBytes ##
1498# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001499##
1500
Cary Clarkac47b882018-01-11 10:35:44 -05001501Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1502kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1503If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1504If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1505match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1506false if pixel conversion is not possible.
1507
1508srcX and srcY may be negative to copy only top or left of source. Returns
1509false if width() or height() is zero or negative.
1510Returns false if
1511#Formula
1512abs(srcX) >= Image width()
1513##
1514, or if
1515#Formula
1516abs(srcY) >= Image height()
1517##
1518.
1519
1520If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1521If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1522
1523#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1524#Param srcX column index whose absolute value is less than width() ##
1525#Param srcY row index whose absolute value is less than height() ##
1526#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1527
1528#Return true if pixels are copied to dst ##
1529
1530#Example
1531#Image 3
1532 std::vector<int32_t> srcPixels;
1533 int rowBytes = image->width() * 4;
1534 int quarterWidth = image->width() / 4;
1535 int quarterHeight = image->height() / 4;
1536 srcPixels.resize(image->height() * rowBytes);
1537 for (int y = 0; y < 4; ++y) {
1538 for (int x = 0; x < 4; ++x) {
1539 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1540 &srcPixels.front() + x * image->height() * quarterWidth +
1541 y * quarterWidth, rowBytes);
1542 image->readPixels(pixmap, x * quarterWidth, y * quarterHeight);
1543 }
1544 }
1545 canvas->scale(.5f, .5f);
1546 SkBitmap bitmap;
1547 bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()),
1548 &srcPixels.front(), rowBytes);
1549 canvas->drawBitmap(bitmap, 0, 0);
1550##
1551
1552#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001553
1554#Method ##
1555
1556# ------------------------------------------------------------------------------
1557
1558#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality,
1559 CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001560#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001561#Line # scales and converts one Image to another ##
Cary Clarka560c472017-11-27 10:44:06 -05001562
Cary Clarkac47b882018-01-11 10:35:44 -05001563Copies Image to dst, scaling pixels to fit dst.width() and dst.height(), and
1564converting pixels to match dst.colorType and dst.alphaType. Returns true if
1565pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1566less than dst SkImageInfo::minRowBytes.
Cary Clarka560c472017-11-27 10:44:06 -05001567
Cary Clarkac47b882018-01-11 10:35:44 -05001568Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1569kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1570If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1571If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1572match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1573false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001574
Cary Clarkac47b882018-01-11 10:35:44 -05001575Scales the image, with filterQuality, to match dst.width() and dst.height().
1576filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1577Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1578Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1579Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1580kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1581
1582If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1583If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1584
1585#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1586#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1587 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1588##
1589#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1590
1591#Return true if pixels are scaled to fit dst ##
Cary Clarka560c472017-11-27 10:44:06 -05001592
1593#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001594#Image 3
1595#Height 128
1596 std::vector<int32_t> srcPixels;
1597 int quarterWidth = image->width() / 16;
1598 int rowBytes = quarterWidth * 4;
1599 int quarterHeight = image->height() / 16;
1600 srcPixels.resize(quarterHeight * rowBytes);
1601 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1602 &srcPixels.front(), rowBytes);
1603 canvas->scale(4, 4);
1604 SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality,
1605 kMedium_SkFilterQuality, kHigh_SkFilterQuality };
1606 for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) {
1607 image->scalePixels(pixmap, qualities[index]);
1608 sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
1609 canvas->drawImage(filtered, 16 * index, 0);
1610 }
Cary Clarka560c472017-11-27 10:44:06 -05001611##
1612
Cary Clarkac47b882018-01-11 10:35:44 -05001613#SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001614
1615#Method ##
1616
1617# ------------------------------------------------------------------------------
1618
1619#Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const
Cary Clark78de7512018-02-07 07:27:09 -05001620#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001621#Line # returns encoded Image as SkData ##
Cary Clarkac47b882018-01-11 10:35:44 -05001622Encodes Image pixels, returning result as SkData.
Cary Clark2f466242017-12-11 16:03:17 -05001623
Cary Clarkac47b882018-01-11 10:35:44 -05001624Returns nullptr if encoding fails, or if encodedImageFormat is not supported.
Cary Clarka560c472017-11-27 10:44:06 -05001625
Cary Clarkac47b882018-01-11 10:35:44 -05001626Image encoding in a format requires both building with one or more of:
1627SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support
1628for the encoded format.
1629
1630If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can
1631additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP,
1632SkEncodedImageFormat::kGIF.
1633
1634quality is a platform and format specific metric trading off size and encoding
1635error. When used, quality equaling 100 encodes with the least error. quality may
1636be ignored by the encoder.
1637
1638#Param encodedImageFormat one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
1639 SkEncodedImageFormat::kWEBP
1640 ##
1641#Param quality encoder specific metric with 100 equaling best ##
Cary Clarka560c472017-11-27 10:44:06 -05001642
Cary Clark2f466242017-12-11 16:03:17 -05001643#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001644
1645#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001646#Image 3
1647 canvas->scale(4, 4);
1648 SkIRect subset = {0, 0, 16, 64};
1649 int x = 0;
1650 for (int quality : { 0, 10, 50, 100 } ) {
1651 sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
1652 sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
1653 canvas->drawImage(filtered, x, 0);
1654 x += 16;
1655 }
Cary Clarka560c472017-11-27 10:44:06 -05001656##
1657
Cary Clarkac47b882018-01-11 10:35:44 -05001658#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001659
1660#Method ##
1661
1662# ------------------------------------------------------------------------------
1663
Cary Clark61ca7c52018-01-02 11:34:14 -05001664#Method sk_sp<SkData> encodeToData() const
Cary Clarka560c472017-11-27 10:44:06 -05001665
Cary Clarkac47b882018-01-11 10:35:44 -05001666Encodes Image pixels, returning result as SkData. Returns existing encoded data
1667if present; otherwise, Image is encoded with SkEncodedImageFormat::kPNG. Skia
1668must be built with SK_HAS_PNG_LIBRARY to encode Image.
Cary Clarka560c472017-11-27 10:44:06 -05001669
Cary Clarkac47b882018-01-11 10:35:44 -05001670Returns nullptr if existing encoded data is missing or invalid, and
Cary Clarka560c472017-11-27 10:44:06 -05001671encoding fails.
1672
Cary Clarkac47b882018-01-11 10:35:44 -05001673#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001674
1675#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001676#Image 3
1677 canvas->scale(4, 4);
1678 SkIRect subset = {136, 32, 200, 96};
1679 sk_sp<SkData> data(image->encodeToData());
1680 sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
1681 canvas->drawImage(eye, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001682##
1683
Cary Clarkac47b882018-01-11 10:35:44 -05001684#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001685
1686#Method ##
1687
1688# ------------------------------------------------------------------------------
1689
1690#Method sk_sp<SkData> refEncodedData() const
Cary Clark78de7512018-02-07 07:27:09 -05001691#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001692#Line # returns Image encoded in SkData if present ##
Cary Clarkac47b882018-01-11 10:35:44 -05001693Returns encoded Image pixels as SkData, if Image was created from supported
1694encoded stream format. Platform support for formats vary and may require building
1695with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY.
Cary Clarka560c472017-11-27 10:44:06 -05001696
Cary Clarkac47b882018-01-11 10:35:44 -05001697Returns nullptr if Image contents are not encoded.
Cary Clarka560c472017-11-27 10:44:06 -05001698
Cary Clarkac47b882018-01-11 10:35:44 -05001699#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001700
1701#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001702#Image 3
1703#Platform gpu
1704 struct {
1705 const char* name;
1706 sk_sp<SkImage> image;
1707 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1708 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001709 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1710 nullptr) } };
Cary Clarkac47b882018-01-11 10:35:44 -05001711 SkString string;
1712 SkPaint paint;
1713 for (const auto& test : tests ) {
1714 if (!test.image) {
1715 string.printf("no %s", test.name);
1716 } else {
1717 string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name);
1718 }
1719 canvas->drawString(string, 10, 20, paint);
1720 canvas->translate(0, 20);
1721 }
Cary Clarka560c472017-11-27 10:44:06 -05001722##
1723
Cary Clarkac47b882018-01-11 10:35:44 -05001724#SeeAlso encodeToData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001725
1726#Method ##
1727
1728# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001729#Subtopic Utility
1730#Populate
1731#Line # rarely called management functions ##
1732##
Cary Clarka560c472017-11-27 10:44:06 -05001733
1734#Method const char* toString(SkString* string) const
Cary Clark4855f782018-02-06 09:41:53 -05001735#In Utility
1736#Line # converts Image to machine readable form ##
Cary Clarkac47b882018-01-11 10:35:44 -05001737Appends Image description to string, including unique ID, width, height, and
1738whether the image is opaque.
Cary Clarka560c472017-11-27 10:44:06 -05001739
Cary Clarkac47b882018-01-11 10:35:44 -05001740#Param string storage for description; existing content is preserved ##
1741
1742#Return string appended with Image description ##
Cary Clarka560c472017-11-27 10:44:06 -05001743
1744#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001745#Image 4
1746 struct {
1747 const char* name;
1748 sk_sp<SkImage> image;
1749 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1750 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001751 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1752 nullptr) } };
Cary Clarkac47b882018-01-11 10:35:44 -05001753 SkString string;
1754 SkPaint paint;
1755 for (const auto& test : tests ) {
1756 string.printf("%s: ", test.name);
1757 test.image ? (void) test.image->toString(&string) : string.append("no image");
1758 canvas->drawString(string, 10, 20, paint);
1759 canvas->translate(0, 20);
1760 }
Cary Clarka560c472017-11-27 10:44:06 -05001761##
1762
Cary Clarkac47b882018-01-11 10:35:44 -05001763#SeeAlso SkPaint::toString
Cary Clarka560c472017-11-27 10:44:06 -05001764
1765#Method ##
1766
1767# ------------------------------------------------------------------------------
1768
1769#Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const
Cary Clark4855f782018-02-06 09:41:53 -05001770#In Constructor
1771#Line # creates Image containing part of original ##
Cary Clarkac47b882018-01-11 10:35:44 -05001772Returns subset of Image. subset must be fully contained by Image dimensions().
1773The implementation may share pixels, or may copy them.
Cary Clarka560c472017-11-27 10:44:06 -05001774
Cary Clarkac47b882018-01-11 10:35:44 -05001775Returns nullptr if subset is empty, or subset is not contained by bounds, or
1776pixels in Image could not be read or copied.
Cary Clarka560c472017-11-27 10:44:06 -05001777
Cary Clarkac47b882018-01-11 10:35:44 -05001778#Param subset bounds of returned Image ##
1779
1780#Return partial or full Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001781
1782#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001783#Image 3
1784 canvas->scale(.5f, .5f);
1785 const int width = 32;
1786 const int height = 32;
1787 for (int y = 0; y < 512; y += height ) {
1788 for (int x = 0; x < 512; x += width ) {
1789 sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height}));
1790 canvas->drawImage(subset, x * 3 / 2, y * 3 / 2);
1791 }
1792 }
Cary Clarka560c472017-11-27 10:44:06 -05001793##
1794
Cary Clarkac47b882018-01-11 10:35:44 -05001795#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001796
1797#Method ##
1798
1799# ------------------------------------------------------------------------------
1800
1801#Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const
Cary Clark4855f782018-02-06 09:41:53 -05001802#In Constructor
1803#Line # creates Image matching Color_Space if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001804Returns Image backed by GPU_Texture associated with context. Returned Image is
1805compatible with Surface created with dstColorSpace. Returns original
1806Image if context and dstColorSpace match.
1807
1808Returns nullptr if context is nullptr, or if Image was created with another
1809GrContext.
Cary Clarka560c472017-11-27 10:44:06 -05001810
Cary Clark61ca7c52018-01-02 11:34:14 -05001811#Param context GPU_Context ##
Cary Clarkac47b882018-01-11 10:35:44 -05001812#Param dstColorSpace range of colors of matching Surface on GPU ##
Cary Clarka560c472017-11-27 10:44:06 -05001813
Cary Clarkac47b882018-01-11 10:35:44 -05001814#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001815
1816#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001817#Platform gpu
1818#Image 5
1819 auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void {
1820 if (nullptr == image || nullptr == context) {
1821 return;
1822 }
1823 SkPaint paint;
1824 paint.setAntiAlias(true);
1825 paint.setTextAlign(SkPaint::kCenter_Align);
1826 sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr));
1827 canvas->drawImage(texture, 0, 0);
1828 canvas->drawString(label, texture->width() / 2, texture->height() / 4, paint);
1829 };
1830 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1831 GrContext* context = canvas->getGrContext();
1832 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001833 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1834 nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001835 drawImage(image, context, "image");
1836 canvas->translate(image->width(), 0);
1837 drawImage(bitmapImage, context, "source");
1838 canvas->translate(-image->width(), image->height());
1839 drawImage(textureImage, context, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001840##
1841
Cary Clarkac47b882018-01-11 10:35:44 -05001842#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05001843
1844#Method ##
1845
1846# ------------------------------------------------------------------------------
1847
1848#Method sk_sp<SkImage> makeNonTextureImage() const
Cary Clark4855f782018-02-06 09:41:53 -05001849#In Constructor
1850#Line # creates Image without dependency on GPU_Texture ##
Cary Clarkac47b882018-01-11 10:35:44 -05001851Returns Raster_Image or Lazy_Image. Copies Image backed by GPU_Texture into
Cary Clark4855f782018-02-06 09:41:53 -05001852CPU memory if needed. Returns original Image if decoded in Raster_Bitmap,
Cary Clarkac47b882018-01-11 10:35:44 -05001853or if encoded in a stream.
Cary Clark61ca7c52018-01-02 11:34:14 -05001854
Cary Clarkac47b882018-01-11 10:35:44 -05001855Returns nullptr if backed by GPU_Texture and copy fails.
1856
1857#Return Raster_Image, Lazy_Image, or nullptr ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001858
1859#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001860#Image 5
1861#Platform gpu
1862 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1863 if (nullptr == image) {
1864 return;
1865 }
1866 SkPaint paint;
1867 paint.setAntiAlias(true);
1868 paint.setTextAlign(SkPaint::kCenter_Align);
1869 sk_sp<SkImage> nonTexture(image->makeNonTextureImage());
1870 canvas->drawImage(nonTexture, 0, 0);
1871 canvas->drawString(label, nonTexture->width() / 2, nonTexture->height() / 4, paint);
1872 };
1873 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1874 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001875 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1876 nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001877 drawImage(image, "image");
1878 canvas->translate(image->width(), 0);
1879 drawImage(bitmapImage, "source");
1880 canvas->translate(-image->width(), image->height());
1881 drawImage(textureImage, "backEndTexture");
Cary Clark61ca7c52018-01-02 11:34:14 -05001882##
1883
Cary Clark56356312018-02-08 14:45:18 -05001884#SeeAlso makeTextureImage makeRasterImage MakeBackendTextureFromSkImage
Cary Clark61ca7c52018-01-02 11:34:14 -05001885
1886#Method ##
1887
1888# ------------------------------------------------------------------------------
1889
1890#Method sk_sp<SkImage> makeRasterImage() const
Cary Clark4855f782018-02-06 09:41:53 -05001891#In Constructor
1892#Line # creates Image compatible with Raster_Surface if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001893Returns Raster_Image. Copies Image backed by GPU_Texture into CPU memory,
Cary Clark4855f782018-02-06 09:41:53 -05001894or decodes Image from Lazy_Image. Returns original Image if decoded in
Cary Clarkac47b882018-01-11 10:35:44 -05001895Raster_Bitmap.
Cary Clarka560c472017-11-27 10:44:06 -05001896
Cary Clarkac47b882018-01-11 10:35:44 -05001897Returns nullptr if copy, decode, or pixel read fails.
Cary Clarka560c472017-11-27 10:44:06 -05001898
Cary Clarkac47b882018-01-11 10:35:44 -05001899#Return Raster_Image, or nullptr ##
1900
Cary Clark4855f782018-02-06 09:41:53 -05001901#Bug 7479
Cary Clarka560c472017-11-27 10:44:06 -05001902#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001903#Image 5
1904#Platform gpu
1905 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1906 if (nullptr == image) {
1907 return;
1908 }
1909 SkPaint paint;
1910 paint.setAntiAlias(true);
1911 paint.setTextAlign(SkPaint::kCenter_Align);
1912 sk_sp<SkImage> raster(image->makeRasterImage());
1913 canvas->drawImage(raster, 0, 0);
1914 canvas->drawString(label, raster->width() / 2, raster->height() / 4, paint);
1915 };
1916 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1917 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clark471b6fe2018-03-21 08:52:41 -04001918 kTopLeft_GrSurfaceOrigin, kN32_SkColorType, kOpaque_SkAlphaType,
1919 nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001920 drawImage(image, "image");
1921 canvas->translate(image->width(), 0);
1922 drawImage(bitmapImage, "source");
1923 canvas->translate(-image->width(), image->height());
1924 drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001925##
1926
Cary Clarkac47b882018-01-11 10:35:44 -05001927#SeeAlso isTextureBacked isLazyGenerated MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -05001928
1929#Method ##
1930
1931# ------------------------------------------------------------------------------
1932
1933#Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
1934 const SkIRect& clipBounds, SkIRect* outSubset,
1935 SkIPoint* offset) const
Cary Clark4855f782018-02-06 09:41:53 -05001936#In Constructor
1937#Line # creates filtered, clipped Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001938
Cary Clarkac47b882018-01-11 10:35:44 -05001939Creates filtered Image. filter processes original Image, potentially changing
1940color, position, and size. subset is the bounds of original Image processed
1941by filter. clipBounds is the expected bounds of the filtered Image. outSubset
1942is required storage for the actual bounds of the filtered Image. offset is
1943required storage for translation of returned Image.
Cary Clarka560c472017-11-27 10:44:06 -05001944
Cary Clarkac47b882018-01-11 10:35:44 -05001945Returns nullptr if Image could not be created. If nullptr is returned, outSubset
1946and offset are undefined.
1947
Cary Clark56356312018-02-08 14:45:18 -05001948Useful for animation of SkImageFilter that varies size from frame to frame.
1949Returned Image is created larger than required by filter so that GPU_Texture
1950can be reused with different sized effects. outSubset describes the valid bounds
1951of GPU_Texture returned. offset translates the returned Image to keep subsequent
1952animation frames aligned with respect to each other.
Cary Clarkac47b882018-01-11 10:35:44 -05001953
1954#Param filter how Image is sampled when transformed ##
Cary Clark56356312018-02-08 14:45:18 -05001955#Param subset bounds of Image processed by filter ##
1956#Param clipBounds expected bounds of filtered Image ##
1957#Param outSubset storage for returned Image bounds ##
1958#Param offset storage for returned Image translation ##
Cary Clarka560c472017-11-27 10:44:06 -05001959
Cary Clarkac47b882018-01-11 10:35:44 -05001960#Return filtered Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001961
1962#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001963#Description
1964In each frame of the animation, filtered Image is drawn in a different location.
1965By translating canvas by returned offset, Image appears stationary.
1966##
1967#Image 5
1968#Platform gpu
1969#Duration 5
1970 sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make(
1971 -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE,
1972 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
1973 nullptr);
1974 sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr);
1975 SkIRect subset = image->bounds();
1976 SkIRect clipBounds = image->bounds();
1977 clipBounds.outset(60, 60);
1978 SkIRect outSubset;
1979 SkIPoint offset;
1980 sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds,
1981 &outSubset, &offset));
1982 SkPaint paint;
1983 paint.setAntiAlias(true);
1984 paint.setStyle(SkPaint::kStroke_Style);
1985 canvas->drawLine(0, 0, offset.fX, offset.fY, paint);
1986 canvas->translate(offset.fX, offset.fY);
1987 canvas->drawImage(filtered, 0, 0);
Cary Clark681287e2018-03-16 11:34:15 -04001988 canvas->drawRect(SkRect::Make(outSubset), paint);
Cary Clarka560c472017-11-27 10:44:06 -05001989##
1990
Cary Clark56356312018-02-08 14:45:18 -05001991#SeeAlso makeShader SkPaint::setImageFilter
Cary Clarka560c472017-11-27 10:44:06 -05001992
1993#Method ##
1994
1995# ------------------------------------------------------------------------------
1996
Cary Clarka560c472017-11-27 10:44:06 -05001997#Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc
1998
1999##
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 ##
2027#Param backendTexture storage for backend texture ##
2028#Param backendTextureReleaseProc storage for clean up function ##
Cary Clarka560c472017-11-27 10:44:06 -05002029
Cary Clark56356312018-02-08 14:45:18 -05002030#Return true if backend 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
2048void draw(SkCanvas* canvas) {
2049 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 Clark56356312018-02-08 14:45:18 -05002082Returned 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 ##