blob: a0e29c2ff5ab5eedb0a893e5050cbe7e330becc7 [file] [log] [blame]
Cary Clarka560c472017-11-27 10:44:06 -05001#Topic Image
2#Alias Image_Reference
3
4#Class SkImage
5
Cary Clark61ca7c52018-01-02 11:34:14 -05006Image describes a two dimensional array of pixels to draw. The pixels may be
7unencoded in a Raster_Bitmap, encoded in a Picture or compressed data stream,
8or located in GPU memory as a GPU_Texture.
9
10Image cannot be modified after it is created. Image may allocate additional
11storage as needed; for instance, an encoded Image may decode when drawn.
12
13Image width and height are greater than zero. Creating an Image with zero width
14or height returns Image equal to nullptr.
15
16Image may be created from Bitmap, Pixmap, Surface, Picture, encoded streams,
17GPU_Texture, YUV_ColorSpace data, or hardware buffer. Encoded streams supported
18include BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP. Supported encodings details
19vary with platform.
20
21#Topic Raster_Image
22#Alias Raster_Image
23Raster_Image pixels are unencoded in a Raster_Bitmap. These pixels may be read
24directly and in most cases written to, although edited pixels may not be drawn
25if Image has been copied internally.
26##
27
28#Topic Texture_Image
29Texture_Image are located on GPU and pixels are not accessible. Texture_Image
30are allocated optimally for best performance. Raster_Image may
31be drawn to GPU_Surface, but pixels are uploaded from CPU to GPU downgrading
32performance.
33##
34
35#Topic Lazy_Image
36Lazy_Image defer allocating buffer for Image pixels and decoding stream until
37Image is drawn. Lazy_Image caches result if possible to speed up repeated
38drawing.
39##
Cary Clarka560c472017-11-27 10:44:06 -050040
41#Topic Overview
42
43#Subtopic Subtopics
44#ToDo manually add subtopics ##
45#Table
46#Legend
47# topics # description ##
48#Legend ##
49#Table ##
50##
51
Cary Clark5081eed2018-01-22 07:55:48 -050052#Subtopic Constructors
53#Table
54#Legend
55# name # description ##
56#Legend ##
57# MakeBackendTextureFromSkImage # Creates GPU_Texture from Image. ##
58# MakeCrossContextFromEncoded # Creates Image from encoded data, and uploads to GPU. ##
59# MakeCrossContextFromPixmap # Creates Image from Pixmap, and uploads to GPU. ##
60# MakeFromAHardwareBuffer # Creates Image from Android hardware buffer. ##
61# MakeFromAdoptedTexture # Creates Image from GPU_Texture, managed internally. ##
62# MakeFromBitmap # Creates Image from Bitmap, sharing or copying pixels. ##
63# MakeFromDeferredTextureImageData # To be deprecated. ##
64# MakeFromEncoded # Creates Image from encoded data. ##
65# MakeFromGenerator # Creates Image from a stream of data. ##
66# MakeFromNV12TexturesCopy # Creates Image from YUV_ColorSpace data in two planes. ##
67# MakeFromPicture # Creates Image from Picture. ##
68# MakeFromRaster # Creates Image from Pixmap, with release. ##
69# MakeFromTexture # Creates Image from GPU_Texture, managed externally. ##
70# MakeFromYUVTexturesCopy # Creates Image from YUV_ColorSpace data in three planes. ##
71# MakeRasterCopy # Creates Image from Pixmap and copied pixels. ##
72# MakeRasterData # Creates Image from Image_Info and shared pixels. ##
73# SkSurface::makeImageSnapshot # Creates Image capturing Surface contents. ##
74# makeColorSpace # Creates Image matching Color_Space if possible. ##
75# makeNonTextureImage # Creates Image without dependency on GPU_Texture. ##
76# makeRasterImage # Creates Image compatible with Raster_Surface if possible. ##
77# makeSubset # Creates Image containing part of original. ##
78# makeTextureImage # Creates Image matching Color_Space if possible. ##
79# makeWithFilter # Creates filtered, clipped Image. ##
80#Table ##
81#Subtopic ##
82
Cary Clarka560c472017-11-27 10:44:06 -050083#Subtopic Member_Functions
84#Table
85#Legend
86# description # function ##
87#Legend ##
Cary Clark61ca7c52018-01-02 11:34:14 -050088# MakeBackendTextureFromSkImage # Creates GPU_Texture from Image. ##
Cary Clark3cd22cc2017-12-01 11:49:58 -050089# MakeCrossContextFromEncoded # Creates Image from encoded data, and uploads to GPU. ##
Cary Clark5081eed2018-01-22 07:55:48 -050090# MakeCrossContextFromPixmap # Creates Image from Pixmap, and uploads to GPU. ##
Cary Clark3cd22cc2017-12-01 11:49:58 -050091# MakeFromAHardwareBuffer # Creates Image from Android hardware buffer. ##
Cary Clark61ca7c52018-01-02 11:34:14 -050092# MakeFromAdoptedTexture # Creates Image from GPU_Texture, managed internally. ##
Cary Clark3cd22cc2017-12-01 11:49:58 -050093# MakeFromBitmap # Creates Image from Bitmap, sharing or copying pixels. ##
Cary Clark5081eed2018-01-22 07:55:48 -050094# MakeFromDeferredTextureImageData # To be deprecated. ##
Cary Clark3cd22cc2017-12-01 11:49:58 -050095# MakeFromEncoded # Creates Image from encoded data. ##
96# MakeFromGenerator # Creates Image from a stream of data. ##
97# MakeFromNV12TexturesCopy # Creates Image from YUV_ColorSpace data in two planes. ##
98# MakeFromPicture # Creates Image from Picture. ##
99# MakeFromRaster # Creates Image from Pixmap, with release. ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500100# MakeFromTexture # Creates Image from GPU_Texture, managed externally. ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500101# MakeFromYUVTexturesCopy # Creates Image from YUV_ColorSpace data in three planes. ##
102# MakeRasterCopy # Creates Image from Pixmap and copied pixels. ##
103# MakeRasterData # Creates Image from Image_Info and shared pixels. ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500104# alphaType # Returns Alpha_Type. ##
105# asLegacyBitmap # Returns as Raster_Bitmap. ##
Cary Clark2f466242017-12-11 16:03:17 -0500106# bounds() # Returns width() and height() as Rectangle. ##
107# colorSpace # Returns Color_Space. ##
108# dimensions() # Returns width() and height(). ##
109# encodeToData # Returns encoded Image as SkData. ##
Cary Clark5081eed2018-01-22 07:55:48 -0500110# getDeferredTextureImageData # To be deprecated. ##
Cary Clark2f466242017-12-11 16:03:17 -0500111# getTexture # Deprecated. ##
112# getTextureHandle # Returns GPU reference to Image as texture. ##
113# height() # Returns pixel row count. ##
114# isAlphaOnly # Returns if pixels represent a transparency mask. ##
115# isLazyGenerated # Returns if Image is created as needed. ##
116# isOpaque # Returns if Alpha_Type is kOpaque_SkAlphaType. ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500117# isTextureBacked # Returns if Image was created from GPU_Texture. ##
Cary Clark2f466242017-12-11 16:03:17 -0500118# isValid # Returns if Image can draw to Raster_Surface or GPU_Context. ##
119# makeColorSpace # Creates Image matching Color_Space if possible. ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500120# makeNonTextureImage # Creates Image without dependency on GPU_Texture. ##
121# makeRasterImage # Creates Image compatible with Raster_Surface if possible. ##
Cary Clark2f466242017-12-11 16:03:17 -0500122# makeShader # Creates Shader, Paint element that can tile Image. ##
123# makeSubset # Creates Image containing part of original. ##
124# makeTextureImage # Creates Image matching Color_Space if possible. ##
125# makeWithFilter # Creates filtered, clipped Image. ##
126# peekPixels # Returns Pixmap if possible. ##
127# readPixels # Copies and converts pixels. ##
128# refColorSpace # Returns Image_Info Color_Space. ##
129# refEncodedData # Returns Image encoded in SkData if present. ##
130# scalePixels # Scales and converts one Image to another. ##
131# toString # Converts Image to machine readable form. ##
132# uniqueID # Identifier for Image. ##
133# width() # Returns pixel column count. ##
Cary Clarka560c472017-11-27 10:44:06 -0500134#Table ##
135#Subtopic ##
136
137#Topic ##
138
Cary Clarka560c472017-11-27 10:44:06 -0500139#Typedef SkImageInfo Info
140
141##
142
Cary Clarka560c472017-11-27 10:44:06 -0500143# ------------------------------------------------------------------------------
144
145#Method static sk_sp<SkImage> MakeRasterCopy(const SkPixmap& pixmap)
146
Cary Clark2f466242017-12-11 16:03:17 -0500147Creates Image from Pixmap and copy of pixels. Since pixels are copied, Pixmap
148pixels may be modified or deleted without affecting Image.
Cary Clarka560c472017-11-27 10:44:06 -0500149
Cary Clark3cd22cc2017-12-01 11:49:58 -0500150Image is returned if Pixmap is valid. Valid Pixmap parameters include:
151dimensions are greater than zero;
152each dimension fits in 29 bits;
153Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
154row bytes are large enough to hold one row of pixels;
155pixel address is not nullptr.
156
157#Param pixmap Image_Info, pixel address, and row bytes ##
158
159#Return copy of Pixmap pixels, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500160
161#Example
Cary Clark2f466242017-12-11 16:03:17 -0500162#Height 50
163#Description
164Draw a five by five bitmap, and draw a copy in an Image. Editing the pixmap
165alters the bitmap draw, but does not alter the Image draw since the Image
166contains a copy of the pixels.
167##
168 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
169 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
170 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
171 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
172 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
173 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
174 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
175 SkBitmap bitmap;
176 bitmap.installPixels(pixmap);
177 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
178 *pixmap.writable_addr8(2, 2) = 0x00;
179 canvas->scale(10, 10);
180 canvas->drawBitmap(bitmap, 0, 0);
181 canvas->drawImage(image, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500182##
183
Cary Clark3cd22cc2017-12-01 11:49:58 -0500184#SeeAlso MakeRasterData MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500185
186#Method ##
187
188# ------------------------------------------------------------------------------
189
190#Method static sk_sp<SkImage> MakeRasterData(const Info& info, sk_sp<SkData> pixels, size_t rowBytes)
191
Cary Clark3cd22cc2017-12-01 11:49:58 -0500192Creates Image from Image_Info, sharing pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500193
Cary Clark3cd22cc2017-12-01 11:49:58 -0500194Image is returned if Image_Info is valid. Valid Image_Info parameters include:
195dimensions are greater than zero;
196each dimension fits in 29 bits;
197Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
198rowBytes are large enough to hold one row of pixels;
199pixels is not nullptr, and contains enough data for Image.
200
201#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
202#Param pixels address or pixel storage ##
203#Param rowBytes size of pixel row or larger ##
204
205#Return Image sharing pixels, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500206
207#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500208#Image 3
209 size_t rowBytes = image->width() * SkColorTypeBytesPerPixel(kRGBA_8888_SkColorType);
210 sk_sp<SkData> data = SkData::MakeUninitialized(rowBytes * image->height());
211 SkImageInfo dstInfo = SkImageInfo::MakeN32(image->width(), image->height(),
212 kPremul_SkAlphaType);
213 image->readPixels(dstInfo, data->writable_data(), rowBytes, 0, 0, SkImage::kAllow_CachingHint);
214 sk_sp<SkImage> raw = SkImage::MakeRasterData(dstInfo.makeColorType(kRGBA_8888_SkColorType),
215 data, rowBytes);
216 canvas->drawImage(image, 0, 0);
217 canvas->drawImage(raw.get(), 128, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500218##
219
Cary Clark3cd22cc2017-12-01 11:49:58 -0500220#SeeAlso MakeRasterCopy MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500221
222#Method ##
223
224# ------------------------------------------------------------------------------
225
Cary Clark3cd22cc2017-12-01 11:49:58 -0500226#Typedef void* ReleaseContext
227
228Caller data passed to RasterReleaseProc; may be nullptr.
229
230#SeeAlso MakeFromRaster RasterReleaseProc
231
232##
233
Cary Clarka560c472017-11-27 10:44:06 -0500234#Typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext)
235
Cary Clark3cd22cc2017-12-01 11:49:58 -0500236Function called when Image no longer shares pixels. ReleaseContext is
237provided by caller when Image is created, and may be nullptr.
238
239#SeeAlso ReleaseContext MakeFromRaster
240
Cary Clarka560c472017-11-27 10:44:06 -0500241##
242
243#Method static sk_sp<SkImage> MakeFromRaster(const SkPixmap& pixmap,
244 RasterReleaseProc rasterReleaseProc,
245 ReleaseContext releaseContext)
246
Cary Clark0c5f5462017-12-15 11:21:51 -0500247Creates Image from pixmap, sharing Pixmap pixels. Pixels must remain valid and
Cary Clark3cd22cc2017-12-01 11:49:58 -0500248unchanged until rasterReleaseProc is called. rasterReleaseProc is passed
249releaseContext when Image is deleted or no longer refers to pixmap pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500250
Cary Clark0c5f5462017-12-15 11:21:51 -0500251Pass nullptr for rasterReleaseProc to share Pixmap without requiring a callback
252when Image is released. Pass nullptr for releaseContext if rasterReleaseProc
253does not require state.
254
Cary Clark3cd22cc2017-12-01 11:49:58 -0500255Image is returned if pixmap is valid. Valid Pixmap parameters include:
256dimensions are greater than zero;
257each dimension fits in 29 bits;
258Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
259row bytes are large enough to hold one row of pixels;
260pixel address is not nullptr.
261
262#Param pixmap Image_Info, pixel address, and row bytes ##
Cary Clark0c5f5462017-12-15 11:21:51 -0500263#Param rasterReleaseProc function called when pixels can be released; or nullptr ##
264#Param releaseContext state passed to rasterReleaseProc; or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500265
Cary Clark0c5f5462017-12-15 11:21:51 -0500266#Return Image sharing pixmap ##
Cary Clarka560c472017-11-27 10:44:06 -0500267
268#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500269#Function
270static void releaseProc(const void* pixels, SkImage::ReleaseContext context) {
271 int* countPtr = static_cast<int*>(context);
272 *countPtr += 1;
273}
274##
275
276void draw(SkCanvas* canvas) {
277 SkColor color = 0;
278 SkPixmap pixmap(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), &color, 4);
279 int releaseCount = 0;
280 sk_sp<SkImage> image(SkImage::MakeFromRaster(pixmap, releaseProc, &releaseCount));
281 SkDebugf("before reset: %d\n", releaseCount);
282 image.reset();
283 SkDebugf("after reset: %d\n", releaseCount);
284}
285#StdOut
286before reset: 0
287after reset: 1
288##
Cary Clarka560c472017-11-27 10:44:06 -0500289##
290
Cary Clark3cd22cc2017-12-01 11:49:58 -0500291#SeeAlso MakeRasterCopy MakeRasterData MakeFromGenerator RasterReleaseProc ReleaseContext
Cary Clarka560c472017-11-27 10:44:06 -0500292
293#Method ##
294
295# ------------------------------------------------------------------------------
296
297#Method static sk_sp<SkImage> MakeFromBitmap(const SkBitmap& bitmap)
298
Cary Clark3cd22cc2017-12-01 11:49:58 -0500299Creates Image from bitmap, sharing or copying bitmap pixels. If the bitmap
300is marked immutable, and its pixel memory is shareable, it may be shared
301instead of copied.
Cary Clarka560c472017-11-27 10:44:06 -0500302
Cary Clark3cd22cc2017-12-01 11:49:58 -0500303Image is returned if bitmap is valid. Valid Bitmap parameters include:
304dimensions are greater than zero;
305each dimension fits in 29 bits;
306Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
307row bytes are large enough to hold one row of pixels;
308pixel address is not nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500309
Cary Clark3cd22cc2017-12-01 11:49:58 -0500310#Param bitmap Image_Info, row bytes, and pixels ##
311
312#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500313
314#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500315#Description
316The first Bitmap is shared; writing to the pixel memory changes the first
317Image.
318The second Bitmap is marked immutable, and is copied; writing to the pixel
319memory does not alter the second Image.
320##
321#Height 50
322 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
323 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
324 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
325 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
326 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
327 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
328 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
329 SkBitmap bitmap;
330 bitmap.installPixels(pixmap);
331 sk_sp<SkImage> image1 = SkImage::MakeFromBitmap(bitmap);
332 bitmap.setImmutable();
333 sk_sp<SkImage> image2 = SkImage::MakeFromBitmap(bitmap);
334 *pixmap.writable_addr8(2, 2) = 0x00;
335 canvas->scale(10, 10);
336 canvas->drawImage(image1, 0, 0);
337 canvas->drawImage(image2, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500338##
339
Cary Clark3cd22cc2017-12-01 11:49:58 -0500340#SeeAlso MakeFromRaster MakeRasterCopy MakeFromGenerator MakeRasterData
Cary Clarka560c472017-11-27 10:44:06 -0500341
342#Method ##
343
344# ------------------------------------------------------------------------------
345
346#Method static sk_sp<SkImage> MakeFromGenerator(std::unique_ptr<SkImageGenerator> imageGenerator,
347 const SkIRect* subset = nullptr)
348
Cary Clark0c5f5462017-12-15 11:21:51 -0500349Creates Image from data returned by imageGenerator. Generated data is owned by Image and may not
350be shared or accessed.
Cary Clarka560c472017-11-27 10:44:06 -0500351
Cary Clark0c5f5462017-12-15 11:21:51 -0500352subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
353otherwise, subset must be contained by image bounds.
354
355Image is returned if generator data is valid. Valid data parameters vary by type of data
356and platform.
Cary Clarka560c472017-11-27 10:44:06 -0500357
Cary Clark3cd22cc2017-12-01 11:49:58 -0500358imageGenerator may wrap Picture data, codec data, or custom data.
359
360#Param imageGenerator stock or custom routines to retrieve Image ##
361#Param subset bounds of returned Image; may be nullptr ##
362
363#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500364
365#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500366#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500367#Description
368The generator returning Picture cannot be shared; std::move transfers ownership to generated Image.
369##
370 SkPictureRecorder recorder;
371 recorder.beginRecording(100, 100)->drawColor(SK_ColorRED);
372 auto picture = recorder.finishRecordingAsPicture();
373 auto gen = SkImageGenerator::MakeFromPicture({100, 100}, picture, nullptr, nullptr,
374 SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB());
375 sk_sp<SkImage> image = SkImage::MakeFromGenerator(std::move(gen));
376 canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500377##
378
Cary Clark3cd22cc2017-12-01 11:49:58 -0500379#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500380
381#Method ##
382
383# ------------------------------------------------------------------------------
384
385#Method static sk_sp<SkImage> MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset = nullptr)
386
Cary Clark3cd22cc2017-12-01 11:49:58 -0500387Creates Image from encoded data.
Cary Clark0c5f5462017-12-15 11:21:51 -0500388subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
389otherwise, subset must be contained by image bounds.
Cary Clarka560c472017-11-27 10:44:06 -0500390
Cary Clark3cd22cc2017-12-01 11:49:58 -0500391Image is returned if format of the encoded data is recognized and supported.
392Recognized formats vary by platfrom.
Cary Clarka560c472017-11-27 10:44:06 -0500393
Cary Clark3cd22cc2017-12-01 11:49:58 -0500394#Param encoded data of Image to decode ##
395#Param subset bounds of returned Image; may be nullptr ##
396
397#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500398
Cary Clark61ca7c52018-01-02 11:34:14 -0500399#Example
400#Image 3
401int x = 0;
402for (int quality : { 100, 50, 10, 1} ) {
403 sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, quality);
404 sk_sp<SkImage> image = SkImage::MakeFromEncoded(encodedData);
405 canvas->drawImage(image, x, 0);
406 x += 64;
407}
Cary Clarka560c472017-11-27 10:44:06 -0500408##
409
Cary Clark3cd22cc2017-12-01 11:49:58 -0500410#SeeAlso MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500411
412#Method ##
413
414# ------------------------------------------------------------------------------
415
416#Typedef void (*TextureReleaseProc)(ReleaseContext releaseContext)
417
418##
419
420#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
421 const GrBackendTexture& backendTexture,
422 GrSurfaceOrigin origin,
423 SkAlphaType alphaType,
424 sk_sp<SkColorSpace> colorSpace)
425
Cary Clarkac47b882018-01-11 10:35:44 -0500426#Deprecated
Cary Clark61ca7c52018-01-02 11:34:14 -0500427Deprecated.
Cary Clarkac47b882018-01-11 10:35:44 -0500428##
Cary Clarka560c472017-11-27 10:44:06 -0500429
Cary Clark3cd22cc2017-12-01 11:49:58 -0500430#Param context GPU_Context ##
431#Param backendTexture texture residing on GPU ##
432#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
433#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
434 kPremul_SkAlphaType, kUnpremul_SkAlphaType
435##
Cary Clark61ca7c52018-01-02 11:34:14 -0500436#Param colorSpace range of colors; may be nullptr ##
437
438#Return created Image, or nullptr ##
439
Cary Clark61ca7c52018-01-02 11:34:14 -0500440#Method ##
441
442# ------------------------------------------------------------------------------
443
444#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
445 const GrBackendTexture& backendTexture,
446 GrSurfaceOrigin origin,
447 SkAlphaType alphaType,
448 sk_sp<SkColorSpace> colorSpace,
449 TextureReleaseProc textureReleaseProc,
450 ReleaseContext releaseContext)
451
Cary Clarkac47b882018-01-11 10:35:44 -0500452#Deprecated
Cary Clark61ca7c52018-01-02 11:34:14 -0500453Deprecated.
Cary Clarkac47b882018-01-11 10:35:44 -0500454##
Cary Clark61ca7c52018-01-02 11:34:14 -0500455
456#Param context GPU_Context ##
457#Param backendTexture texture residing on GPU ##
458#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
459#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
460 kPremul_SkAlphaType, kUnpremul_SkAlphaType
461##
462#Param colorSpace range of colors; may be nullptr ##
463#Param textureReleaseProc function called when texture can be released ##
464#Param releaseContext state passed to textureReleaseProc ##
465
466#Return created Image, or nullptr ##
467
Cary Clark61ca7c52018-01-02 11:34:14 -0500468#Method ##
469
470# ------------------------------------------------------------------------------
471
472#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
473 const GrBackendTexture& backendTexture,
474 GrSurfaceOrigin origin,
475 SkColorType colorType,
476 SkAlphaType alphaType,
477 sk_sp<SkColorSpace> colorSpace)
478
479Creates Image from GPU_Texture associated with context. Caller is responsible for
480managing the lifetime of GPU_Texture.
481
482Image is returned if format of backendTexture is recognized and supported.
483Recognized formats vary by GPU back-end.
484
485#Param context GPU_Context ##
486#Param backendTexture texture residing on GPU ##
487#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
488#Param colorType one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
489 kRGB_565_SkColorType, kARGB_4444_SkColorType,
490 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
491 kGray_8_SkColorType, kRGBA_F16_SkColorType
492##
493#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
494 kPremul_SkAlphaType, kUnpremul_SkAlphaType
495##
496#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500497
498#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500499
500#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500501#Image 3
502#Platform gpu
503#Height 128
504#Description
505A back-end texture has been created and uploaded to the GPU outside of this example.
506##
507GrContext* context = canvas->getGrContext();
508if (!context) {
509 return;
510}
511canvas->scale(.25f, .25f);
512int x = 0;
513for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500514 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark0c5f5462017-12-15 11:21:51 -0500515 origin, kOpaque_SkAlphaType, nullptr);
516 canvas->drawImage(image, x, 0);
517x += 512;
518}
Cary Clarka560c472017-11-27 10:44:06 -0500519##
520
Cary Clark3cd22cc2017-12-01 11:49:58 -0500521#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500522
523#Method ##
524
525# ------------------------------------------------------------------------------
526
527#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
528 const GrBackendTexture& backendTexture,
529 GrSurfaceOrigin origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500530 SkColorType colorType,
Cary Clarka560c472017-11-27 10:44:06 -0500531 SkAlphaType alphaType,
532 sk_sp<SkColorSpace> colorSpace,
533 TextureReleaseProc textureReleaseProc,
534 ReleaseContext releaseContext)
535
Cary Clark61ca7c52018-01-02 11:34:14 -0500536Creates Image from GPU_Texture associated with context. GPU_Texture must stay
Cary Clark3cd22cc2017-12-01 11:49:58 -0500537valid and unchanged until textureReleaseProc is called. textureReleaseProc is
538passed releaseContext when Image is deleted or no longer refers to texture.
Cary Clarka560c472017-11-27 10:44:06 -0500539
Cary Clark3cd22cc2017-12-01 11:49:58 -0500540Image is returned if format of backendTexture is recognized and supported.
541Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500542
Cary Clark3cd22cc2017-12-01 11:49:58 -0500543#Param context GPU_Context ##
544#Param backendTexture texture residing on GPU ##
545#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500546#Param colorType one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
547 kRGB_565_SkColorType, kARGB_4444_SkColorType,
548 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
549 kGray_8_SkColorType, kRGBA_F16_SkColorType
550##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500551#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
552 kPremul_SkAlphaType, kUnpremul_SkAlphaType
553##
Cary Clark61ca7c52018-01-02 11:34:14 -0500554#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500555#Param textureReleaseProc function called when texture can be released ##
556#Param releaseContext state passed to textureReleaseProc ##
557
558#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500559
Cary Clark0c5f5462017-12-15 11:21:51 -0500560#ToDo
561This doesn't do anything clever with TextureReleaseProc because it may not get called
Cary Clark61ca7c52018-01-02 11:34:14 -0500562fwithin the lifetime of the example
Cary Clark0c5f5462017-12-15 11:21:51 -0500563##
564
Cary Clarka560c472017-11-27 10:44:06 -0500565#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500566#Platform gpu
567#Image 4
Cary Clark0c5f5462017-12-15 11:21:51 -0500568GrContext* context = canvas->getGrContext();
569if (!context) {
570 return;
571}
Cary Clarkac47b882018-01-11 10:35:44 -0500572auto debugster = [](SkImage::ReleaseContext releaseContext) -> void {
573 *((int *) releaseContext) += 128;
Cary Clark0c5f5462017-12-15 11:21:51 -0500574};
575int x = 0;
576for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500577 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark61ca7c52018-01-02 11:34:14 -0500578 origin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr, debugster, &x);
Cary Clark0c5f5462017-12-15 11:21:51 -0500579 canvas->drawImage(image, x, 0);
580 x += 128;
581}
Cary Clarka560c472017-11-27 10:44:06 -0500582##
583
Cary Clark3cd22cc2017-12-01 11:49:58 -0500584#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500585
586#Method ##
587
588# ------------------------------------------------------------------------------
589
590#Method static sk_sp<SkImage> MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> data,
591 bool buildMips,
592 SkColorSpace* dstColorSpace)
593
Cary Clark3cd22cc2017-12-01 11:49:58 -0500594Creates Image from encoded data. Image is uploaded to GPU back-end using context.
595
596Created Image is available to other GPU contexts, and is available across thread
597boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
598share resources.
599
600When Image is no longer referenced, context releases texture memory
Cary Clarka560c472017-11-27 10:44:06 -0500601asynchronously.
Cary Clarka560c472017-11-27 10:44:06 -0500602
Cary Clark3cd22cc2017-12-01 11:49:58 -0500603Texture decoded from data is uploaded to match Surface created with
604dstColorSpace. Color_Space of Image is determined by encoded data.
Cary Clarka560c472017-11-27 10:44:06 -0500605
Cary Clark3cd22cc2017-12-01 11:49:58 -0500606Image is returned if format of data is recognized and supported, and if context
607supports moving resources. Recognized formats vary by platform and GPU back-end.
608
Cary Clark61ca7c52018-01-02 11:34:14 -0500609Image is returned using MakeFromEncoded if context is nullptr or does not support
610moving resources between contexts.
611
Cary Clark3cd22cc2017-12-01 11:49:58 -0500612#Param context GPU_Context ##
613#Param data Image to decode ##
614#Param buildMips create Image as Mip_Map if true ##
615#Param dstColorSpace range of colors of matching Surface on GPU ##
616
617#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500618
619#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500620#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500621#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500622GrContext* context = canvas->getGrContext();
623sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, 100);
624sk_sp<SkImage> image = SkImage::MakeCrossContextFromEncoded(context,
625 encodedData, false, nullptr);
626canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500627##
628
Cary Clark3cd22cc2017-12-01 11:49:58 -0500629#SeeAlso MakeCrossContextFromPixmap
630
631#Method ##
632
633# ------------------------------------------------------------------------------
634
635#Method static sk_sp<SkImage> MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap,
636 bool buildMips,
637 SkColorSpace* dstColorSpace)
638
639Creates Image from pixmap. Image is uploaded to GPU back-end using context.
640
641Created Image is available to other GPU contexts, and is available across thread
642boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
643share resources.
644
645When Image is no longer referenced, context releases texture memory
646asynchronously.
647
648Texture created from pixmap is uploaded to match Surface created with
649dstColorSpace. Color_Space of Image is determined by pixmap.colorSpace().
650
Cary Clark61ca7c52018-01-02 11:34:14 -0500651Image is returned referring to GPU back-end if context is not nullptr,
652format of data is recognized and supported, and if context supports moving
653resources between contexts. Otherwise, pixmap pixel data is copied and Image
654as returned in raster format if possible; nullptr may be returned.
655Recognized GPU formats vary by platform and GPU back-end.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500656
657#Param context GPU_Context ##
658#Param pixmap Image_Info, pixel address, and row bytes ##
659#Param buildMips create Image as Mip_Map if true ##
660#Param dstColorSpace range of colors of matching Surface on GPU ##
661
662#Return created Image, or nullptr ##
663
664#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500665#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500666#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500667GrContext* context = canvas->getGrContext();
668SkPixmap pixmap;
669if (source.peekPixels(&pixmap)) {
670 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(context, pixmap,
671 false, nullptr);
672 canvas->drawImage(image, 0, 0);
673}
Cary Clark3cd22cc2017-12-01 11:49:58 -0500674##
675
676#SeeAlso MakeCrossContextFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500677
678#Method ##
679
680# ------------------------------------------------------------------------------
681
682#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
683 const GrBackendTexture& backendTexture,
684 GrSurfaceOrigin surfaceOrigin,
685 SkAlphaType alphaType = kPremul_SkAlphaType,
686 sk_sp<SkColorSpace> colorSpace = nullptr)
687
Cary Clark61ca7c52018-01-02 11:34:14 -0500688Deprecated.
689
690#Param context GPU_Context ##
691#Param backendTexture texture residing on GPU ##
692#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
693#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
694 kPremul_SkAlphaType, kUnpremul_SkAlphaType
695##
696#Param colorSpace range of colors; may be nullptr ##
697
698#Return created Image, or nullptr ##
699
700#NoExample
701##
702
703#SeeAlso MakeFromTexture MakeFromYUVTexturesCopy
704
705#Method ##
706
707# ------------------------------------------------------------------------------
708
709#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
710 const GrBackendTexture& backendTexture,
711 GrSurfaceOrigin surfaceOrigin,
712 SkColorType colorType,
713 SkAlphaType alphaType = kPremul_SkAlphaType,
714 sk_sp<SkColorSpace> colorSpace = nullptr)
715
Cary Clark3cd22cc2017-12-01 11:49:58 -0500716Creates Image from backendTexture associated with context. backendTexture and
717returned Image are managed internally, and are released when no longer needed.
Cary Clarka560c472017-11-27 10:44:06 -0500718
Cary Clark3cd22cc2017-12-01 11:49:58 -0500719Image is returned if format of backendTexture is recognized and supported.
720Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500721
Cary Clark3cd22cc2017-12-01 11:49:58 -0500722#Param context GPU_Context ##
723#Param backendTexture texture residing on GPU ##
724#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500725#Param colorType one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
726 kRGB_565_SkColorType, kARGB_4444_SkColorType,
727 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
728 kGray_8_SkColorType, kRGBA_F16_SkColorType
729##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500730#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
731 kPremul_SkAlphaType, kUnpremul_SkAlphaType
732##
Cary Clark61ca7c52018-01-02 11:34:14 -0500733#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500734
735#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500736
737#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500738#Image 5
739#Platform gpu
Cary Clark61ca7c52018-01-02 11:34:14 -0500740 if (!canvas->getGrContext()) {
741 return;
742 }
743 canvas->scale(.5f, .5f);
744 canvas->clear(0x7f3f5f7f);
745 int x = 0, y = 0;
746 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
747 for (auto alpha : { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType } ) {
748 sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(canvas->getGrContext(),
749 backEndTexture, origin,
750 kRGBA_8888_SkColorType, alpha);
751 canvas->drawImage(image, x, y);
752 x += 160;
753 }
754 x -= 160 * 3;
755 y += 256;
756 }
Cary Clarka560c472017-11-27 10:44:06 -0500757##
758
Cary Clark61ca7c52018-01-02 11:34:14 -0500759#SeeAlso MakeFromTexture MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500760
761#Method ##
762
763# ------------------------------------------------------------------------------
764
765#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
766 const GrBackendObject yuvTextureHandles[3],
767 const SkISize yuvSizes[3],
768 GrSurfaceOrigin surfaceOrigin,
769 sk_sp<SkColorSpace> colorSpace = nullptr)
770
Cary Clark61ca7c52018-01-02 11:34:14 -0500771Creates Image from copy of yuvTextureHandles, an array of textures on GPU.
772yuvTextureHandles contain pixels for YUV planes of Image.
773yuvSizes conain dimensions for each pixel plane. Dimensions must be greater than
774zero but may differ from plane to plane. Returned Image has the dimensions
775yuvSizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500776
Cary Clark61ca7c52018-01-02 11:34:14 -0500777#Param context GPU_Context ##
778#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
779 kRec709_SkYUVColorSpace
780##
781#Param yuvTextureHandles array of YUV textures on GPU ##
782#Param yuvSizes dimensions of YUV textures ##
783#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
784#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500785
Cary Clark61ca7c52018-01-02 11:34:14 -0500786#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500787
Cary Clark61ca7c52018-01-02 11:34:14 -0500788# seems too complicated to create an example for this
789#ToDo
790should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500791##
792
Cary Clark61ca7c52018-01-02 11:34:14 -0500793#NoExample
794##
795
796#SeeAlso MakeFromNV12TexturesCopy
797
798#Method ##
799
800# ------------------------------------------------------------------------------
801
802#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
803 const GrBackendTexture yuvTextureHandles[3],
804 const SkISize yuvSizes[3],
805 GrSurfaceOrigin surfaceOrigin,
806 sk_sp<SkColorSpace> colorSpace = nullptr)
807
808Creates Image from copy of yuvTextureHandles, an array of textures on GPU.
809yuvTextureHandles contain pixels for YUV planes of Image.
810yuvSizes conain dimensions for each pixel plane. Dimensions must be greater than
811zero but may differ from plane to plane. Returned Image has the dimensions
812yuvSizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
813
814#Param context GPU_Context ##
815#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
816 kRec709_SkYUVColorSpace
817##
818#Param yuvTextureHandles array of YUV textures on GPU ##
819#Param yuvSizes dimensions of YUV textures ##
820#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
821#Param colorSpace range of colors; may be nullptr ##
822
823#Return created Image, or nullptr ##
824
825# seems too complicated to create an example for this
826#ToDo
827should this be moved to chrome only?
828##
829
830#NoExample
831##
832
833#SeeAlso MakeFromNV12TexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500834
835#Method ##
836
837# ------------------------------------------------------------------------------
838
839#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
840 SkYUVColorSpace yuvColorSpace,
841 const GrBackendObject nv12TextureHandles[2],
842 const SkISize nv12Sizes[2],
843 GrSurfaceOrigin surfaceOrigin,
844 sk_sp<SkColorSpace> colorSpace = nullptr)
845
Cary Clark61ca7c52018-01-02 11:34:14 -0500846Creates Image from copy of nv12TextureHandles, an array of textures on GPU.
847nv12TextureHandles[0] contains pixels for YUV_Component_Y plane.
848nv12TextureHandles[1] contains pixels for YUV_Component_U plane,
849followed by pixels for YUV_Component_V plane.
850nv12Sizes conain dimensions for each pixel plane. Dimensions must be greater than
851zero but may differ from plane to plane. Returned Image has the dimensions
852nv12Sizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500853
Cary Clark61ca7c52018-01-02 11:34:14 -0500854#Param context GPU_Context ##
855#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
856 kRec709_SkYUVColorSpace
857##
858#Param nv12TextureHandles array of YUV textures on GPU ##
859#Param nv12Sizes dimensions of YUV textures ##
860#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
861#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500862
Cary Clark61ca7c52018-01-02 11:34:14 -0500863#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500864
Cary Clark61ca7c52018-01-02 11:34:14 -0500865# seems too complicated to create an example for this
866#ToDo
867should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500868##
869
Cary Clark61ca7c52018-01-02 11:34:14 -0500870#NoExample
871##
872
873#SeeAlso MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500874
875#Method ##
876
877# ------------------------------------------------------------------------------
878
Cary Clark61ca7c52018-01-02 11:34:14 -0500879#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
880 SkYUVColorSpace yuvColorSpace,
881 const GrBackendTexture nv12TextureHandles[2],
882 const SkISize nv12Sizes[2],
883 GrSurfaceOrigin surfaceOrigin,
884 sk_sp<SkColorSpace> colorSpace = nullptr)
885
886Creates Image from copy of nv12TextureHandles, an array of textures on GPU.
887nv12TextureHandles[0] contains pixels for YUV_Component_Y plane.
888nv12TextureHandles[1] contains pixels for YUV_Component_U plane,
889followed by pixels for YUV_Component_V plane.
890nv12Sizes conain dimensions for each pixel plane. Dimensions must be greater than
891zero but may differ from plane to plane. Returned Image has the dimensions
892nv12Sizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
893
894#Param context GPU_Context ##
895#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
896 kRec709_SkYUVColorSpace
897##
898#Param nv12TextureHandles array of YUV textures on GPU ##
899#Param nv12Sizes dimensions of YUV textures ##
900#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
901#Param colorSpace range of colors; may be nullptr ##
902
903#Return created Image, or nullptr ##
904
905# seems too complicated to create an example for this
906#ToDo
907should this be moved to chrome only?
908##
909
910#NoExample
911##
912
913#SeeAlso MakeFromYUVTexturesCopy
914
915#Method ##
916
917# ------------------------------------------------------------------------------
918
919#Bug 7424
920currently uncalled by any test or client
921##
922
Cary Clarka560c472017-11-27 10:44:06 -0500923#Enum BitDepth
924
925#Code
Cary Clark61ca7c52018-01-02 11:34:14 -0500926 enum class BitDepth {
Cary Clarka560c472017-11-27 10:44:06 -0500927 kU8,
928 kF16,
929 };
930##
931
932#Const kU8 0
Cary Clark61ca7c52018-01-02 11:34:14 -0500933Use 8 bits per Color_ARGB component using unsigned integer format.
Cary Clarka560c472017-11-27 10:44:06 -0500934##
935#Const kF16 1
Cary Clark61ca7c52018-01-02 11:34:14 -0500936Use 16 bits per Color_ARGB component using half-precision floating point format.
Cary Clarka560c472017-11-27 10:44:06 -0500937##
938
Cary Clark61ca7c52018-01-02 11:34:14 -0500939#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500940##
941
Cary Clark61ca7c52018-01-02 11:34:14 -0500942#SeeAlso MakeFromPicture
Cary Clarka560c472017-11-27 10:44:06 -0500943
944#Enum ##
945
946# ------------------------------------------------------------------------------
947
948#Method static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
949 const SkMatrix* matrix, const SkPaint* paint,
950 BitDepth bitDepth,
951 sk_sp<SkColorSpace> colorSpace)
952
Cary Clark61ca7c52018-01-02 11:34:14 -0500953Creates Image from picture. Returned Image width and height are set by dimensions.
954Image draws picture with matrix and paint, set to bitDepth and colorSpace.
Cary Clarka560c472017-11-27 10:44:06 -0500955
Cary Clark61ca7c52018-01-02 11:34:14 -0500956If matrix is nullptr, draws with identity Matrix. If paint is nullptr, draws
957with default Paint. colorSpace may be nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500958
Cary Clark61ca7c52018-01-02 11:34:14 -0500959#Param picture stream of drawing commands ##
960#Param dimensions width and height ##
961#Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ##
962#Param paint Paint to apply transparency, filtering, and so on; may be nullptr ##
963#Param bitDepth 8 bit integer or 16 bit float: per component ##
964#Param colorSpace range of colors; may be nullptr ##
965
966#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500967
968#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500969 SkPaint paint;
970 SkPictureRecorder recorder;
971 SkCanvas* recordingCanvas = recorder.beginRecording(50, 50);
972 for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) {
973 paint.setColor(color);
974 recordingCanvas->drawRect({10, 10, 30, 40}, paint);
975 recordingCanvas->translate(10, 10);
976 recordingCanvas->scale(1.2f, 1.4f);
977 }
978 sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
979 int x = 0, y = 0;
980 for (auto alpha : { 70, 140, 210 } ) {
981 paint.setAlpha(alpha);
982 auto srgbColorSpace = SkColorSpace::MakeSRGB();
983 sk_sp<SkImage> image = SkImage::MakeFromPicture(playback, {50, 50}, nullptr, &paint,
984 SkImage::BitDepth::kU8, srgbColorSpace);
985 canvas->drawImage(image, x, y);
986 x += 70; y += 70;
987 }
Cary Clarka560c472017-11-27 10:44:06 -0500988##
989
Cary Clark61ca7c52018-01-02 11:34:14 -0500990#SeeAlso SkCanvas::drawPicture
Cary Clarka560c472017-11-27 10:44:06 -0500991
992#Method ##
993
994# ------------------------------------------------------------------------------
995
996#Method static sk_sp<SkImage> MakeFromAHardwareBuffer(AHardwareBuffer* hardwareBuffer,
997 SkAlphaType alphaType = kPremul_SkAlphaType,
998 sk_sp<SkColorSpace> colorSpace = nullptr)
999
Cary Clark61ca7c52018-01-02 11:34:14 -05001000#Bug 7447 ##
Cary Clarka560c472017-11-27 10:44:06 -05001001
Cary Clark61ca7c52018-01-02 11:34:14 -05001002Creates Image from Android hardware buffer.
1003Returned Image takes a reference on the buffer.
Cary Clarka560c472017-11-27 10:44:06 -05001004
Cary Clark61ca7c52018-01-02 11:34:14 -05001005Only available on Android, when __ANDROID_API__ is defined to be 26 or greater.
Cary Clarka560c472017-11-27 10:44:06 -05001006
Cary Clark61ca7c52018-01-02 11:34:14 -05001007#Param hardwareBuffer AHardwareBuffer Android hardware buffer ##
1008#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
1009 kPremul_SkAlphaType, kUnpremul_SkAlphaType
1010##
1011#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001012
Cary Clark61ca7c52018-01-02 11:34:14 -05001013#Return created Image, or nullptr ##
1014
1015#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001016##
1017
Cary Clark61ca7c52018-01-02 11:34:14 -05001018#SeeAlso MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -05001019
1020#Method ##
1021
1022# ------------------------------------------------------------------------------
1023
1024#Method int width() const
1025
Cary Clark61ca7c52018-01-02 11:34:14 -05001026Returns pixel count in each row.
1027
1028#Return pixel width in Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001029
1030#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001031#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -05001032#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -05001033 canvas->translate(10, 10);
1034 canvas->drawImage(image, 0, 0);
1035 canvas->translate(0, image->height());
1036 SkPaint paint;
1037 paint.setTextAlign(SkPaint::kCenter_Align);
1038 canvas->drawLine(0, 10, image->width(), 10, paint);
1039 canvas->drawString("width", image->width() / 2, 25, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001040##
1041
Cary Clark61ca7c52018-01-02 11:34:14 -05001042#SeeAlso dimensions() height()
Cary Clarka560c472017-11-27 10:44:06 -05001043
1044#Method ##
1045
1046# ------------------------------------------------------------------------------
1047
1048#Method int height() const
1049
Cary Clark2f466242017-12-11 16:03:17 -05001050Returns pixel row count.
1051
Cary Clark61ca7c52018-01-02 11:34:14 -05001052#Return pixel height in Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001053
1054#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001055#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -05001056#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -05001057 canvas->translate(10, 10);
1058 canvas->drawImage(image, 0, 0);
1059 canvas->translate(image->width(), 0);
1060 SkPaint paint;
1061 paint.setTextAlign(SkPaint::kCenter_Align);
1062 paint.setVerticalText(true);
1063 canvas->drawLine(10, 0, 10, image->height(), paint);
Cary Clarkac47b882018-01-11 10:35:44 -05001064 canvas->drawString("height", 25, image->height() / 2, paint);
1065##
Cary Clarka560c472017-11-27 10:44:06 -05001066
Cary Clark61ca7c52018-01-02 11:34:14 -05001067#SeeAlso dimensions() width()
Cary Clarka560c472017-11-27 10:44:06 -05001068
1069#Method ##
1070
1071# ------------------------------------------------------------------------------
1072
1073#Method SkISize dimensions() const
1074
Cary Clark2f466242017-12-11 16:03:17 -05001075Returns ISize { width(), height() }.
1076
1077#Return integral size of width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -05001078
1079#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001080#Image 4
1081 SkISize dimensions = image->dimensions();
1082 SkIRect bounds = image->bounds();
1083 SkIRect dimensionsAsBounds = SkIRect::MakeSize(dimensions);
1084 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clarka560c472017-11-27 10:44:06 -05001085##
1086
Cary Clark61ca7c52018-01-02 11:34:14 -05001087#SeeAlso height() width() bounds()
Cary Clarka560c472017-11-27 10:44:06 -05001088
1089#Method ##
1090
1091# ------------------------------------------------------------------------------
1092
1093#Method SkIRect bounds() const
1094
Cary Clark2f466242017-12-11 16:03:17 -05001095Returns IRect { 0, 0, width(), height() }.
1096
1097#Return integral rectangle from origin to width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -05001098
1099#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001100#Height 128
1101#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -05001102 SkIRect bounds = image->bounds();
Cary Clarkac47b882018-01-11 10:35:44 -05001103 for (int x : { 0, bounds.width() } ) {
1104 for (int y : { 0, bounds.height() } ) {
Cary Clark61ca7c52018-01-02 11:34:14 -05001105 canvas->drawImage(image, x, y);
1106 }
1107 }
Cary Clarka560c472017-11-27 10:44:06 -05001108##
1109
Cary Clark61ca7c52018-01-02 11:34:14 -05001110#SeeAlso dimensions()
Cary Clarka560c472017-11-27 10:44:06 -05001111
1112#Method ##
1113
1114# ------------------------------------------------------------------------------
1115
1116#Method uint32_t uniqueID() const
1117
Cary Clark61ca7c52018-01-02 11:34:14 -05001118Returns value unique to image. Image contents cannot change after Image is
1119created. Any operation to create a new Image will receive generate a new
1120unique number.
1121
1122#Return unique identifier ##
Cary Clarka560c472017-11-27 10:44:06 -05001123
1124#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001125#Image 5
1126#Height 156
1127 sk_sp<SkImage> subset = image->makeSubset({10, 20, 90, 100});
1128 canvas->drawImage(image, 0, 0);
1129 canvas->drawImage(subset, 128, 0);
1130 SkPaint paint;
1131 SkString s;
1132 s.printf("original id: %d", image->uniqueID());
1133 canvas->drawString(s, 20, image->height() + 20, paint);
1134 s.printf("subset id: %d", subset->uniqueID());
1135 canvas->drawString(s, 148, subset->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001136##
1137
Cary Clark61ca7c52018-01-02 11:34:14 -05001138#SeeAlso isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001139
1140#Method ##
1141
1142# ------------------------------------------------------------------------------
1143
1144#Method SkAlphaType alphaType() const
1145
Cary Clark61ca7c52018-01-02 11:34:14 -05001146Returns Alpha_Type, one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
1147kPremul_SkAlphaType, kUnpremul_SkAlphaType.
1148
1149Alpha_Type returned was a parameter to an Image constructor,
1150or was parsed from encoded data.
1151
1152#Return Alpha_Type in Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001153
1154#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001155#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -05001156#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -05001157 const char* alphaTypeStr[] = { "Unknown", "Opaque", "Premul", "Unpremul" };
1158 SkAlphaType alphaType = image->alphaType();
Cary Clarkac47b882018-01-11 10:35:44 -05001159 canvas->drawImage(image, 16, 0);
Cary Clark61ca7c52018-01-02 11:34:14 -05001160 SkPaint paint;
1161 canvas->drawString(alphaTypeStr[(int) alphaType], 20, image->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001162##
1163
Cary Clark61ca7c52018-01-02 11:34:14 -05001164#SeeAlso SkImageInfo::alphaType
Cary Clarka560c472017-11-27 10:44:06 -05001165
1166#Method ##
1167
1168# ------------------------------------------------------------------------------
1169
1170#Method SkColorSpace* colorSpace() const
1171
Cary Clark2f466242017-12-11 16:03:17 -05001172Returns Color_Space, the range of colors, associated with Image. The
1173reference count of Color_Space is unchanged. The returned Color_Space is
1174immutable.
Cary Clarka560c472017-11-27 10:44:06 -05001175
Cary Clark61dfc3a2018-01-03 08:37:53 -05001176Color_Space returned was passed to an Image constructor,
1177or was parsed from encoded data. Color_Space returned may be ignored when Image
1178is drawn, depending on the capabilities of the Surface receiving the drawing.
Cary Clark2f466242017-12-11 16:03:17 -05001179
1180#Return Color_Space in Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001181
1182#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001183#Image 3
1184#Set sRGB
1185 SkPixmap pixmap;
1186 source.peekPixels(&pixmap);
1187 canvas->scale(.25f, .25f);
1188 int y = 0;
1189 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1190 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1191 int x = 0;
1192 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1193 for (int index = 0; index < 2; ++index) {
1194 pixmap.setColorSpace(colorSpace);
1195 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1196 canvas->drawImage(image, x, y);
1197 colorSpace = image->colorSpace()->makeColorSpin();
1198 x += 512;
1199 }
1200 y += 512;
1201 }
Cary Clarka560c472017-11-27 10:44:06 -05001202##
1203
Cary Clark61dfc3a2018-01-03 08:37:53 -05001204#SeeAlso refColorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001205
1206#Method ##
1207
1208# ------------------------------------------------------------------------------
1209
1210#Method sk_sp<SkColorSpace> refColorSpace() const
1211
Cary Clark61dfc3a2018-01-03 08:37:53 -05001212Returns a smart pointer to Color_Space, the range of colors, associated with
1213Image. The smart pointer tracks the number of objects sharing this
1214SkColorSpace reference so the memory is released when the owners destruct.
1215
1216The returned SkColorSpace is immutable.
1217
1218Color_Space returned was passed to an Image constructor,
1219or was parsed from encoded data. Color_Space returned may be ignored when Image
1220is drawn, depending on the capabilities of the Surface receiving the drawing.
1221
1222#Return Color_Space in Image, or nullptr, wrapped in a smart pointer ##
Cary Clarka560c472017-11-27 10:44:06 -05001223
1224#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001225#Image 3
1226#Set sRGB
1227 SkPixmap pixmap;
1228 source.peekPixels(&pixmap);
1229 canvas->scale(.25f, .25f);
1230 int y = 0;
1231 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1232 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1233 int x = 0;
1234 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1235 for (int index = 0; index < 2; ++index) {
1236 pixmap.setColorSpace(colorSpace);
1237 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1238 canvas->drawImage(image, x, y);
1239 colorSpace = image->refColorSpace()->makeColorSpin();
1240 x += 512;
1241 }
1242 y += 512;
1243 }
Cary Clarka560c472017-11-27 10:44:06 -05001244##
1245
Cary Clark61dfc3a2018-01-03 08:37:53 -05001246#SeeAlso colorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001247
1248#Method ##
1249
1250# ------------------------------------------------------------------------------
1251
1252#Method bool isAlphaOnly() const
1253
Cary Clark2f466242017-12-11 16:03:17 -05001254Returns true if Image pixels represent transparency only. If true, each pixel
1255is packed in 8 bits as defined by kAlpha_8_SkColorType.
Cary Clarka560c472017-11-27 10:44:06 -05001256
Cary Clark2f466242017-12-11 16:03:17 -05001257#Return true if pixels represent a transparency mask ##
Cary Clarka560c472017-11-27 10:44:06 -05001258
1259#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001260 uint8_t pmColors = 0;
1261 sk_sp<SkImage> image = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1), &pmColors, 1});
1262 SkDebugf("alphaOnly = %s\n", image->isAlphaOnly() ? "true" : "false");
1263#StdOut
1264alphaOnly = true
1265##
Cary Clarka560c472017-11-27 10:44:06 -05001266##
1267
Cary Clark61dfc3a2018-01-03 08:37:53 -05001268#SeeAlso alphaType isOpaque
Cary Clarka560c472017-11-27 10:44:06 -05001269
1270#Method ##
1271
1272# ------------------------------------------------------------------------------
1273
1274#Method bool isOpaque() const
1275
Cary Clark61dfc3a2018-01-03 08:37:53 -05001276Returns true if pixels ignore their Alpha value and are treated as fully opaque.
Cary Clark2f466242017-12-11 16:03:17 -05001277
1278#Return true if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clarka560c472017-11-27 10:44:06 -05001279
1280#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001281 auto check_isopaque = [](const SkImageInfo& imageInfo) -> void {
1282 auto surface(SkSurface::MakeRaster(imageInfo));
1283 auto image(surface->makeImageSnapshot());
1284 SkDebugf("isOpaque = %s\n", image->isOpaque() ? "true" : "false");
1285 };
1286
1287 check_isopaque(SkImageInfo::MakeN32Premul(5, 5));
1288 check_isopaque(SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType));
1289#StdOut
1290isOpaque = false
1291isOpaque = true
1292##
Cary Clarka560c472017-11-27 10:44:06 -05001293##
1294
Cary Clark61dfc3a2018-01-03 08:37:53 -05001295#SeeAlso alphaType isAlphaOnly
Cary Clarka560c472017-11-27 10:44:06 -05001296
1297#Method ##
1298
1299# ------------------------------------------------------------------------------
1300
1301#Method sk_sp<SkShader> makeShader(SkShader::TileMode tileMode1, SkShader::TileMode tileMode2,
1302 const SkMatrix* localMatrix = nullptr) const
1303
Cary Clark61dfc3a2018-01-03 08:37:53 -05001304Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1305SkShader::TileMode rules to fill drawn area outside Image. localMatrix permits
1306transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001307
Cary Clark61dfc3a2018-01-03 08:37:53 -05001308#Param tileMode1 tiling in x, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1309 SkShader::kMirror_TileMode
1310##
1311#Param tileMode2 tiling in y, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1312 SkShader::kMirror_TileMode
1313##
1314#Param localMatrix Image transformation, or nullptr ##
1315
1316#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001317
1318#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001319#Image 4
1320SkMatrix matrix;
1321matrix.setRotate(45);
1322SkPaint paint;
1323paint.setShader(image->makeShader(SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode,
1324 &matrix));
1325canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001326##
1327
Cary Clark61dfc3a2018-01-03 08:37:53 -05001328#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001329
1330#Method ##
1331
1332# ------------------------------------------------------------------------------
1333
1334#Method sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const
1335
Cary Clark61dfc3a2018-01-03 08:37:53 -05001336Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1337SkShader::kClamp_TileMode to fill drawn area outside Image. localMatrix permits
1338transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001339
Cary Clark61dfc3a2018-01-03 08:37:53 -05001340#Param localMatrix Image transformation, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001341
Cary Clark61dfc3a2018-01-03 08:37:53 -05001342#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001343
1344#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001345#Image 5
1346SkMatrix matrix;
1347matrix.setRotate(45);
1348matrix.postTranslate(125, 30);
1349SkPaint paint;
1350paint.setShader(image->makeShader(&matrix));
1351canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001352##
1353
Cary Clarkf5404bb2018-01-05 12:10:09 -05001354#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001355
1356#Method ##
1357
1358# ------------------------------------------------------------------------------
1359
1360#Method bool peekPixels(SkPixmap* pixmap) const
1361
Cary Clarkf5404bb2018-01-05 12:10:09 -05001362Copies Image pixel address, row bytes, and Image_Info to pixmap, if address
1363is available, and returns true. If pixel address is not available, return
1364false and leave pixmap unchanged.
Cary Clarka560c472017-11-27 10:44:06 -05001365
Cary Clarkf5404bb2018-01-05 12:10:09 -05001366#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
Cary Clarka560c472017-11-27 10:44:06 -05001367
Cary Clarkf5404bb2018-01-05 12:10:09 -05001368#Return true if Image has direct access to pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001369
1370#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001371 SkBitmap bitmap;
1372 bitmap.allocPixels(SkImageInfo::MakeN32Premul(12, 11));
1373 SkCanvas offscreen(bitmap);
1374 offscreen.clear(SK_ColorWHITE);
1375 SkPaint paint;
1376 offscreen.drawString("%", 1, 10, paint);
1377 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
1378 SkPixmap pixmap;
1379 if (image->peekPixels(&pixmap)) {
1380 const SkPMColor* pixels = pixmap.addr32();
1381 SkPMColor pmWhite = pixels[0];
1382 for (int y = 0; y < image->height(); ++y) {
1383 for (int x = 0; x < image->width(); ++x) {
1384 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
1385 }
1386 SkDebugf("\n");
1387 }
1388 }
1389#StdOut
1390------------
1391--xx----x---
1392-x--x--x----
1393-x--x--x----
1394-x--x-x-----
1395--xx-xx-xx--
1396-----x-x--x-
1397----x--x--x-
1398----x--x--x-
1399---x----xx--
1400------------
1401##
Cary Clarka560c472017-11-27 10:44:06 -05001402##
1403
Cary Clarkf5404bb2018-01-05 12:10:09 -05001404#SeeAlso readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001405
1406#Method ##
1407
1408# ------------------------------------------------------------------------------
1409
1410#Method GrTexture* getTexture() const
1411
Cary Clark2f466242017-12-11 16:03:17 -05001412Deprecated.
1413
1414#Deprecated
1415##
1416
Cary Clarka560c472017-11-27 10:44:06 -05001417#Private
Cary Clarkf5404bb2018-01-05 12:10:09 -05001418Currently used by Canvas2DLayerBridge in Chromium.
Cary Clarka560c472017-11-27 10:44:06 -05001419##
1420
Cary Clarka560c472017-11-27 10:44:06 -05001421#Method ##
1422
1423# ------------------------------------------------------------------------------
1424
1425#Method bool isTextureBacked() const
1426
Cary Clarkf5404bb2018-01-05 12:10:09 -05001427Returns true the contents of Image was created on or uploaded to GPU memory,
1428and is available as a GPU_Texture.
Cary Clarka560c472017-11-27 10:44:06 -05001429
Cary Clarkf5404bb2018-01-05 12:10:09 -05001430#Return true if Image is a GPU_Texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001431
1432#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001433#Image 5
1434#Platform gpu
1435auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1436 if (nullptr == image) {
1437 return;
1438 }
1439 SkPaint paint;
1440 paint.setAntiAlias(true);
1441 paint.setTextAlign(SkPaint::kCenter_Align);
1442 canvas->drawImage(image, 0, 0);
1443 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1444 canvas->drawString(image->isTextureBacked() ? "is GPU texture" : "not GPU texture",
1445 image->width() / 2, image->height() * 3 / 4, paint);
1446};
1447sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1448sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1449 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1450drawImage(image, "image");
1451canvas->translate(image->width(), 0);
1452drawImage(bitmapImage, "source");
1453canvas->translate(-image->width(), image->height());
1454drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001455##
1456
Cary Clarkf5404bb2018-01-05 12:10:09 -05001457#SeeAlso MakeFromTexture isValid
Cary Clarka560c472017-11-27 10:44:06 -05001458
1459#Method ##
1460
1461# ------------------------------------------------------------------------------
1462
1463#Method bool isValid(GrContext* context) const
1464
Cary Clarkf5404bb2018-01-05 12:10:09 -05001465Returns true if Image can be drawn on either Raster_Surface or GPU_Surface.
1466If context is nullptr, tests if Image draws on Raster_Surface;
1467otherwise, tests if Image draws on GPU_Surface associated with context.
Cary Clarka560c472017-11-27 10:44:06 -05001468
Cary Clarkf5404bb2018-01-05 12:10:09 -05001469Image backed by GPU_Texture may become invalid if associated GrContext is
1470invalid. Lazy_Image may be invalid and may not draw to Raster_Surface or
1471GPU_Surface or both.
Cary Clarka560c472017-11-27 10:44:06 -05001472
Cary Clark61ca7c52018-01-02 11:34:14 -05001473#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05001474
Cary Clarkf5404bb2018-01-05 12:10:09 -05001475#Return true if Image can be drawn ##
Cary Clarka560c472017-11-27 10:44:06 -05001476
1477#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001478#Image 5
1479#Platform gpu
1480auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1481 if (nullptr == image) {
1482 return;
1483 }
1484 SkPaint paint;
1485 paint.setAntiAlias(true);
1486 paint.setTextAlign(SkPaint::kCenter_Align);
1487 canvas->drawImage(image, 0, 0);
1488 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1489 if (canvas->getGrContext()) {
1490 canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" :
1491 "not valid on GPU", image->width() / 2, image->height() * 5 / 8, paint);
1492 }
1493 canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" :
1494 "not valid on CPU", image->width() / 2, image->height() * 7 / 8, paint);
1495};
1496sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1497sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1498 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1499drawImage(image, "image");
1500canvas->translate(image->width(), 0);
1501drawImage(bitmapImage, "source");
1502canvas->translate(-image->width(), image->height());
1503drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001504##
1505
Cary Clarkf5404bb2018-01-05 12:10:09 -05001506#SeeAlso isTextureBacked isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001507
1508#Method ##
1509
1510# ------------------------------------------------------------------------------
1511
1512#Method GrBackendObject getTextureHandle(bool flushPendingGrContextIO,
1513 GrSurfaceOrigin* origin = nullptr) const
1514
Cary Clark2f466242017-12-11 16:03:17 -05001515Retrieves the back-end API handle of texture. If flushPendingGrContextIO is true,
1516complete deferred I/O operations.
Cary Clarka560c472017-11-27 10:44:06 -05001517
Cary Clark2f466242017-12-11 16:03:17 -05001518If origin in not nullptr, copies location of content drawn into Image.
Cary Clarka560c472017-11-27 10:44:06 -05001519
Cary Clark2f466242017-12-11 16:03:17 -05001520#Param flushPendingGrContextIO flag to flush outstanding requests ##
1521#Param origin storage for one of: kTopLeft_GrSurfaceOrigin,
1522 kBottomLeft_GrSurfaceOrigin; or nullptr
1523##
1524
Cary Clarkac47b882018-01-11 10:35:44 -05001525#Return back-end API texture handle, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001526
1527#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001528#Image 4
Cary Clark2f466242017-12-11 16:03:17 -05001529#Platform gpu
1530GrContext* context = canvas->getGrContext();
1531if (!context) {
1532 return;
1533}
1534SkPaint paint;
1535paint.setAntiAlias(true);
1536SkString str;
Cary Clarkac47b882018-01-11 10:35:44 -05001537int y = -10;
Cary Clark2f466242017-12-11 16:03:17 -05001538for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin } ) {
1539 sk_sp<SkImage> srcImage(SkImage::MakeFromTexture(context,
1540 backEndTexture, origin, kPremul_SkAlphaType, nullptr));
1541 GrSurfaceOrigin readBackOrigin;
1542 GrBackendObject readBackHandle = srcImage->getTextureHandle(false, &readBackOrigin);
Cary Clarkac47b882018-01-11 10:35:44 -05001543 str.printf("readBackHandle: 0x%x", readBackHandle);
1544 canvas->drawString(str, 5, y += 30, paint);
1545 canvas->drawImage(srcImage, 80, y += 10);
Cary Clark2f466242017-12-11 16:03:17 -05001546 str.printf("origin: k%s_GrSurfaceOrigin", readBackOrigin ? "BottomLeft" : "TopLeft");
Cary Clarkac47b882018-01-11 10:35:44 -05001547 canvas->drawString(str, 5, y += srcImage->height() + 10, paint);
Cary Clark2f466242017-12-11 16:03:17 -05001548}
Cary Clarka560c472017-11-27 10:44:06 -05001549##
1550
Cary Clarkac47b882018-01-11 10:35:44 -05001551#Example
1552#Image 5
1553#Platform gpu
1554 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1555 if (nullptr == image) {
1556 return;
1557 }
1558 SkPaint paint;
1559 paint.setAntiAlias(true);
1560 paint.setTextAlign(SkPaint::kCenter_Align);
1561 canvas->drawImage(image, 0, image->height() / 4);
1562 canvas->drawString(label, image->width() / 2, image->height() / 8, paint);
1563 GrSurfaceOrigin readBackOrigin;
1564 GrBackendObject readBackHandle = image->getTextureHandle(false, &readBackOrigin);
1565 canvas->drawString(readBackHandle ? "has readBackHandle" : "no readBackHandle",
1566 image->width() / 2, image->height() * 11 / 8, paint);
1567 };
1568 drawImage(image, "image");
1569 canvas->translate(image->width(), 0);
1570 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1571 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1572 drawImage(textureImage, "backEndTexture");
1573##
1574
1575#SeeAlso MakeFromTexture isTextureBacked
Cary Clarka560c472017-11-27 10:44:06 -05001576
1577#Method ##
1578
1579# ------------------------------------------------------------------------------
1580
1581#Enum CachingHint
1582
1583#Code
1584 enum CachingHint {
1585 kAllow_CachingHint,
1586 kDisallow_CachingHint,
1587 };
1588##
1589
Cary Clarkac47b882018-01-11 10:35:44 -05001590CachingHint selects whether Skia may internally cache Bitmaps generated by
1591decoding Image, or by copying Image from GPU to CPU. The default behavior
1592allows caching Bitmaps.
1593
1594Choose kDisallow_CachingHint if Image pixels are to be used only once, or
1595if Image pixels reside in a cache outside of Skia, or to reduce memory pressure.
1596
1597Choosing kAllow_CachingHint does not ensure that pixels will be cached.
1598Image pixels may not be cached if memory requirements are too large or
1599pixels are not accessible.
Cary Clarka560c472017-11-27 10:44:06 -05001600
1601#Const kAllow_CachingHint 0
Cary Clarkac47b882018-01-11 10:35:44 -05001602Allows Skia to internally cache decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001603##
1604#Const kDisallow_CachingHint 1
Cary Clarkac47b882018-01-11 10:35:44 -05001605Disallows Skia from internally caching decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001606##
1607
Cary Clarkac47b882018-01-11 10:35:44 -05001608#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001609##
1610
Cary Clarkac47b882018-01-11 10:35:44 -05001611#SeeAlso readPixels scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001612
1613#Enum ##
1614
1615# ------------------------------------------------------------------------------
1616
1617#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1618 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const
1619
Cary Clarkac47b882018-01-11 10:35:44 -05001620Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY),
1621and does not exceed Image (width(), height()).
1622
1623dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
1624destination. dstRowBytes specifics the gap from one destination row to the next.
1625Returns true if pixels are copied. Returns false if:
1626#List
1627# dstInfo.addr() equals nullptr ##
1628# dstRowBytes is less than dstInfo.minRowBytes ##
1629# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001630##
1631
Cary Clarkac47b882018-01-11 10:35:44 -05001632Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1633kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1634If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace must match.
1635If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType must
1636match. If Image Color_Space is nullptr, dstInfo.colorSpace must match. Returns
1637false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001638
Cary Clarkac47b882018-01-11 10:35:44 -05001639srcX and srcY may be negative to copy only top or left of source. Returns
1640false if width() or height() is zero or negative.
1641Returns false if
1642#Formula
1643abs(srcX) >= Image width()
1644##
1645, or if
1646#Formula
1647abs(srcY) >= Image height()
1648##
1649.
Cary Clarka560c472017-11-27 10:44:06 -05001650
Cary Clarkac47b882018-01-11 10:35:44 -05001651If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1652If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1653
1654#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1655#Param dstPixels destination pixel storage ##
1656#Param dstRowBytes destination row length ##
1657#Param srcX column index whose absolute value is less than width() ##
1658#Param srcY row index whose absolute value is less than height() ##
1659#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1660
1661#Return true if pixels are copied to dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001662
1663#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001664#Image 3
1665 canvas->scale(.5f, .5f);
1666 const int width = 32;
1667 const int height = 32;
1668 std::vector<int32_t> dstPixels;
1669 dstPixels.resize(height * width * 4);
1670 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1671 for (int y = 0; y < 512; y += height ) {
1672 for (int x = 0; x < 512; x += width ) {
1673 if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) {
1674 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
1675 SkBitmap bitmap;
1676 bitmap.installPixels(dstPixmap);
1677 canvas->drawBitmap(bitmap, 0, 0);
1678 }
1679 canvas->translate(48, 0);
1680 }
1681 canvas->translate(-16 * 48, 48);
1682 }
Cary Clarka560c472017-11-27 10:44:06 -05001683##
1684
Cary Clarkac47b882018-01-11 10:35:44 -05001685#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001686
1687#Method ##
1688
1689# ------------------------------------------------------------------------------
1690
1691#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY,
1692 CachingHint cachingHint = kAllow_CachingHint) const
1693
Cary Clarkac47b882018-01-11 10:35:44 -05001694Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and
1695does not exceed Image (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001696
Cary Clarkac47b882018-01-11 10:35:44 -05001697dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
1698and row bytes of destination. dst.rowBytes specifics the gap from one destination
1699row to the next. Returns true if pixels are copied. Returns false if:
1700#List
1701# dst pixel storage equals nullptr ##
1702# dst.rowBytes is less than SkImageInfo::minRowBytes ##
1703# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001704##
1705
Cary Clarkac47b882018-01-11 10:35:44 -05001706Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1707kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1708If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1709If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1710match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1711false if pixel conversion is not possible.
1712
1713srcX and srcY may be negative to copy only top or left of source. Returns
1714false if width() or height() is zero or negative.
1715Returns false if
1716#Formula
1717abs(srcX) >= Image width()
1718##
1719, or if
1720#Formula
1721abs(srcY) >= Image height()
1722##
1723.
1724
1725If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1726If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1727
1728#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1729#Param srcX column index whose absolute value is less than width() ##
1730#Param srcY row index whose absolute value is less than height() ##
1731#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1732
1733#Return true if pixels are copied to dst ##
1734
1735#Example
1736#Image 3
1737 std::vector<int32_t> srcPixels;
1738 int rowBytes = image->width() * 4;
1739 int quarterWidth = image->width() / 4;
1740 int quarterHeight = image->height() / 4;
1741 srcPixels.resize(image->height() * rowBytes);
1742 for (int y = 0; y < 4; ++y) {
1743 for (int x = 0; x < 4; ++x) {
1744 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1745 &srcPixels.front() + x * image->height() * quarterWidth +
1746 y * quarterWidth, rowBytes);
1747 image->readPixels(pixmap, x * quarterWidth, y * quarterHeight);
1748 }
1749 }
1750 canvas->scale(.5f, .5f);
1751 SkBitmap bitmap;
1752 bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()),
1753 &srcPixels.front(), rowBytes);
1754 canvas->drawBitmap(bitmap, 0, 0);
1755##
1756
1757#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001758
1759#Method ##
1760
1761# ------------------------------------------------------------------------------
1762
1763#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality,
1764 CachingHint cachingHint = kAllow_CachingHint) const
1765
Cary Clarkac47b882018-01-11 10:35:44 -05001766Copies Image to dst, scaling pixels to fit dst.width() and dst.height(), and
1767converting pixels to match dst.colorType and dst.alphaType. Returns true if
1768pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1769less than dst SkImageInfo::minRowBytes.
Cary Clarka560c472017-11-27 10:44:06 -05001770
Cary Clarkac47b882018-01-11 10:35:44 -05001771Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1772kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1773If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1774If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1775match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1776false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001777
Cary Clarkac47b882018-01-11 10:35:44 -05001778Scales the image, with filterQuality, to match dst.width() and dst.height().
1779filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1780Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1781Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1782Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1783kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1784
1785If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1786If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1787
1788#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1789#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1790 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1791##
1792#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1793
1794#Return true if pixels are scaled to fit dst ##
Cary Clarka560c472017-11-27 10:44:06 -05001795
1796#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001797#Image 3
1798#Height 128
1799 std::vector<int32_t> srcPixels;
1800 int quarterWidth = image->width() / 16;
1801 int rowBytes = quarterWidth * 4;
1802 int quarterHeight = image->height() / 16;
1803 srcPixels.resize(quarterHeight * rowBytes);
1804 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1805 &srcPixels.front(), rowBytes);
1806 canvas->scale(4, 4);
1807 SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality,
1808 kMedium_SkFilterQuality, kHigh_SkFilterQuality };
1809 for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) {
1810 image->scalePixels(pixmap, qualities[index]);
1811 sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
1812 canvas->drawImage(filtered, 16 * index, 0);
1813 }
Cary Clarka560c472017-11-27 10:44:06 -05001814##
1815
Cary Clarkac47b882018-01-11 10:35:44 -05001816#SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001817
1818#Method ##
1819
1820# ------------------------------------------------------------------------------
1821
1822#Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const
1823
Cary Clarkac47b882018-01-11 10:35:44 -05001824Encodes Image pixels, returning result as SkData.
Cary Clark2f466242017-12-11 16:03:17 -05001825
Cary Clarkac47b882018-01-11 10:35:44 -05001826Returns nullptr if encoding fails, or if encodedImageFormat is not supported.
Cary Clarka560c472017-11-27 10:44:06 -05001827
Cary Clarkac47b882018-01-11 10:35:44 -05001828Image encoding in a format requires both building with one or more of:
1829SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support
1830for the encoded format.
1831
1832If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can
1833additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP,
1834SkEncodedImageFormat::kGIF.
1835
1836quality is a platform and format specific metric trading off size and encoding
1837error. When used, quality equaling 100 encodes with the least error. quality may
1838be ignored by the encoder.
1839
1840#Param encodedImageFormat one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
1841 SkEncodedImageFormat::kWEBP
1842 ##
1843#Param quality encoder specific metric with 100 equaling best ##
Cary Clarka560c472017-11-27 10:44:06 -05001844
Cary Clark2f466242017-12-11 16:03:17 -05001845#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001846
1847#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001848#Image 3
1849 canvas->scale(4, 4);
1850 SkIRect subset = {0, 0, 16, 64};
1851 int x = 0;
1852 for (int quality : { 0, 10, 50, 100 } ) {
1853 sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
1854 sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
1855 canvas->drawImage(filtered, x, 0);
1856 x += 16;
1857 }
Cary Clarka560c472017-11-27 10:44:06 -05001858##
1859
Cary Clarkac47b882018-01-11 10:35:44 -05001860#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001861
1862#Method ##
1863
1864# ------------------------------------------------------------------------------
1865
Cary Clark61ca7c52018-01-02 11:34:14 -05001866#Method sk_sp<SkData> encodeToData() const
Cary Clarka560c472017-11-27 10:44:06 -05001867
Cary Clarkac47b882018-01-11 10:35:44 -05001868Encodes Image pixels, returning result as SkData. Returns existing encoded data
1869if present; otherwise, Image is encoded with SkEncodedImageFormat::kPNG. Skia
1870must be built with SK_HAS_PNG_LIBRARY to encode Image.
Cary Clarka560c472017-11-27 10:44:06 -05001871
Cary Clarkac47b882018-01-11 10:35:44 -05001872Returns nullptr if existing encoded data is missing or invalid, and
Cary Clarka560c472017-11-27 10:44:06 -05001873encoding fails.
1874
Cary Clarkac47b882018-01-11 10:35:44 -05001875#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001876
1877#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001878#Image 3
1879 canvas->scale(4, 4);
1880 SkIRect subset = {136, 32, 200, 96};
1881 sk_sp<SkData> data(image->encodeToData());
1882 sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
1883 canvas->drawImage(eye, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001884##
1885
Cary Clarkac47b882018-01-11 10:35:44 -05001886#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001887
1888#Method ##
1889
1890# ------------------------------------------------------------------------------
1891
1892#Method sk_sp<SkData> refEncodedData() const
1893
Cary Clarkac47b882018-01-11 10:35:44 -05001894Returns encoded Image pixels as SkData, if Image was created from supported
1895encoded stream format. Platform support for formats vary and may require building
1896with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY.
Cary Clarka560c472017-11-27 10:44:06 -05001897
Cary Clarkac47b882018-01-11 10:35:44 -05001898Returns nullptr if Image contents are not encoded.
Cary Clarka560c472017-11-27 10:44:06 -05001899
Cary Clarkac47b882018-01-11 10:35:44 -05001900#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001901
1902#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001903#Image 3
1904#Platform gpu
1905 struct {
1906 const char* name;
1907 sk_sp<SkImage> image;
1908 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1909 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1910 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr) } };
1911 SkString string;
1912 SkPaint paint;
1913 for (const auto& test : tests ) {
1914 if (!test.image) {
1915 string.printf("no %s", test.name);
1916 } else {
1917 string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name);
1918 }
1919 canvas->drawString(string, 10, 20, paint);
1920 canvas->translate(0, 20);
1921 }
Cary Clarka560c472017-11-27 10:44:06 -05001922##
1923
Cary Clarkac47b882018-01-11 10:35:44 -05001924#SeeAlso encodeToData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001925
1926#Method ##
1927
1928# ------------------------------------------------------------------------------
1929
1930#Method const char* toString(SkString* string) const
1931
Cary Clarkac47b882018-01-11 10:35:44 -05001932Appends Image description to string, including unique ID, width, height, and
1933whether the image is opaque.
Cary Clarka560c472017-11-27 10:44:06 -05001934
Cary Clarkac47b882018-01-11 10:35:44 -05001935#Param string storage for description; existing content is preserved ##
1936
1937#Return string appended with Image description ##
Cary Clarka560c472017-11-27 10:44:06 -05001938
1939#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001940#Image 4
1941 struct {
1942 const char* name;
1943 sk_sp<SkImage> image;
1944 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1945 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1946 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr) } };
1947 SkString string;
1948 SkPaint paint;
1949 for (const auto& test : tests ) {
1950 string.printf("%s: ", test.name);
1951 test.image ? (void) test.image->toString(&string) : string.append("no image");
1952 canvas->drawString(string, 10, 20, paint);
1953 canvas->translate(0, 20);
1954 }
Cary Clarka560c472017-11-27 10:44:06 -05001955##
1956
Cary Clarkac47b882018-01-11 10:35:44 -05001957#SeeAlso SkPaint::toString
Cary Clarka560c472017-11-27 10:44:06 -05001958
1959#Method ##
1960
1961# ------------------------------------------------------------------------------
1962
1963#Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const
1964
Cary Clarkac47b882018-01-11 10:35:44 -05001965Returns subset of Image. subset must be fully contained by Image dimensions().
1966The implementation may share pixels, or may copy them.
Cary Clarka560c472017-11-27 10:44:06 -05001967
Cary Clarkac47b882018-01-11 10:35:44 -05001968Returns nullptr if subset is empty, or subset is not contained by bounds, or
1969pixels in Image could not be read or copied.
Cary Clarka560c472017-11-27 10:44:06 -05001970
Cary Clarkac47b882018-01-11 10:35:44 -05001971#Param subset bounds of returned Image ##
1972
1973#Return partial or full Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001974
1975#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001976#Image 3
1977 canvas->scale(.5f, .5f);
1978 const int width = 32;
1979 const int height = 32;
1980 for (int y = 0; y < 512; y += height ) {
1981 for (int x = 0; x < 512; x += width ) {
1982 sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height}));
1983 canvas->drawImage(subset, x * 3 / 2, y * 3 / 2);
1984 }
1985 }
Cary Clarka560c472017-11-27 10:44:06 -05001986##
1987
Cary Clarkac47b882018-01-11 10:35:44 -05001988#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001989
1990#Method ##
1991
1992# ------------------------------------------------------------------------------
1993
1994#Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const
1995
Cary Clarkac47b882018-01-11 10:35:44 -05001996Returns Image backed by GPU_Texture associated with context. Returned Image is
1997compatible with Surface created with dstColorSpace. Returns original
1998Image if context and dstColorSpace match.
1999
2000Returns nullptr if context is nullptr, or if Image was created with another
2001GrContext.
Cary Clarka560c472017-11-27 10:44:06 -05002002
Cary Clark61ca7c52018-01-02 11:34:14 -05002003#Param context GPU_Context ##
Cary Clarkac47b882018-01-11 10:35:44 -05002004#Param dstColorSpace range of colors of matching Surface on GPU ##
Cary Clarka560c472017-11-27 10:44:06 -05002005
Cary Clarkac47b882018-01-11 10:35:44 -05002006#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05002007
2008#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002009#Platform gpu
2010#Image 5
2011 auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void {
2012 if (nullptr == image || nullptr == context) {
2013 return;
2014 }
2015 SkPaint paint;
2016 paint.setAntiAlias(true);
2017 paint.setTextAlign(SkPaint::kCenter_Align);
2018 sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr));
2019 canvas->drawImage(texture, 0, 0);
2020 canvas->drawString(label, texture->width() / 2, texture->height() / 4, paint);
2021 };
2022 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2023 GrContext* context = canvas->getGrContext();
2024 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture,
2025 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2026 drawImage(image, context, "image");
2027 canvas->translate(image->width(), 0);
2028 drawImage(bitmapImage, context, "source");
2029 canvas->translate(-image->width(), image->height());
2030 drawImage(textureImage, context, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05002031##
2032
Cary Clarkac47b882018-01-11 10:35:44 -05002033#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05002034
2035#Method ##
2036
2037# ------------------------------------------------------------------------------
2038
2039#Method sk_sp<SkImage> makeNonTextureImage() const
2040
Cary Clarkac47b882018-01-11 10:35:44 -05002041Returns Raster_Image or Lazy_Image. Copies Image backed by GPU_Texture into
2042CPU memory if needed. Returns original Image if unencoded in Raster_Bitmap,
2043or if encoded in a stream.
Cary Clark61ca7c52018-01-02 11:34:14 -05002044
Cary Clarkac47b882018-01-11 10:35:44 -05002045Returns nullptr if backed by GPU_Texture and copy fails.
2046
2047#Return Raster_Image, Lazy_Image, or nullptr ##
Cary Clark61ca7c52018-01-02 11:34:14 -05002048
2049#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002050#Image 5
2051#Platform gpu
2052 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2053 if (nullptr == image) {
2054 return;
2055 }
2056 SkPaint paint;
2057 paint.setAntiAlias(true);
2058 paint.setTextAlign(SkPaint::kCenter_Align);
2059 sk_sp<SkImage> nonTexture(image->makeNonTextureImage());
2060 canvas->drawImage(nonTexture, 0, 0);
2061 canvas->drawString(label, nonTexture->width() / 2, nonTexture->height() / 4, paint);
2062 };
2063 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2064 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2065 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2066 drawImage(image, "image");
2067 canvas->translate(image->width(), 0);
2068 drawImage(bitmapImage, "source");
2069 canvas->translate(-image->width(), image->height());
2070 drawImage(textureImage, "backEndTexture");
Cary Clark61ca7c52018-01-02 11:34:14 -05002071##
2072
2073#SeeAlso incomplete
2074
2075#Method ##
2076
2077# ------------------------------------------------------------------------------
2078
2079#Method sk_sp<SkImage> makeRasterImage() const
2080
Cary Clarkac47b882018-01-11 10:35:44 -05002081Returns Raster_Image. Copies Image backed by GPU_Texture into CPU memory,
2082or decodes Image from Lazy_Image. Returns original Image if unencoded in
2083Raster_Bitmap.
Cary Clarka560c472017-11-27 10:44:06 -05002084
Cary Clarkac47b882018-01-11 10:35:44 -05002085Returns nullptr if copy, decode, or pixel read fails.
Cary Clarka560c472017-11-27 10:44:06 -05002086
Cary Clarkac47b882018-01-11 10:35:44 -05002087#Return Raster_Image, or nullptr ##
2088
2089#Bug 7479 ##
Cary Clarka560c472017-11-27 10:44:06 -05002090#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002091#Image 5
2092#Platform gpu
2093 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2094 if (nullptr == image) {
2095 return;
2096 }
2097 SkPaint paint;
2098 paint.setAntiAlias(true);
2099 paint.setTextAlign(SkPaint::kCenter_Align);
2100 sk_sp<SkImage> raster(image->makeRasterImage());
2101 canvas->drawImage(raster, 0, 0);
2102 canvas->drawString(label, raster->width() / 2, raster->height() / 4, paint);
2103 };
2104 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2105 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2106 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2107 drawImage(image, "image");
2108 canvas->translate(image->width(), 0);
2109 drawImage(bitmapImage, "source");
2110 canvas->translate(-image->width(), image->height());
2111 drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05002112##
2113
Cary Clarkac47b882018-01-11 10:35:44 -05002114#SeeAlso isTextureBacked isLazyGenerated MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -05002115
2116#Method ##
2117
2118# ------------------------------------------------------------------------------
2119
2120#Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
2121 const SkIRect& clipBounds, SkIRect* outSubset,
2122 SkIPoint* offset) const
2123
Cary Clarkac47b882018-01-11 10:35:44 -05002124Creates filtered Image. filter processes original Image, potentially changing
2125color, position, and size. subset is the bounds of original Image processed
2126by filter. clipBounds is the expected bounds of the filtered Image. outSubset
2127is required storage for the actual bounds of the filtered Image. offset is
2128required storage for translation of returned Image.
Cary Clarka560c472017-11-27 10:44:06 -05002129
Cary Clarkac47b882018-01-11 10:35:44 -05002130Returns nullptr if Image could not be created. If nullptr is returned, outSubset
2131and offset are undefined.
2132
2133makeWithFilter is optimized to support Image backed by GPU_Texture drawn in an
2134animation with SkImageFilter that vary in size from one frame to the next. The
2135created Image is drawn at an increased size so that GPU_Texture can be reused
2136with different sized effects. outSubset describes the valid bounds of GPU_Texture
2137returned. The returned Image may be much larger than required for the filter.
2138offset translates the returned Image to keep subsequent animation frames
2139aligned with respect to each other.
2140
2141#Param filter how Image is sampled when transformed ##
Cary Clarka560c472017-11-27 10:44:06 -05002142#Param subset incomplete ##
2143#Param clipBounds incomplete ##
2144#Param outSubset incomplete ##
2145#Param offset incomplete ##
2146
Cary Clarkac47b882018-01-11 10:35:44 -05002147#Return filtered Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05002148
2149#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002150#Description
2151In each frame of the animation, filtered Image is drawn in a different location.
2152By translating canvas by returned offset, Image appears stationary.
2153##
2154#Image 5
2155#Platform gpu
2156#Duration 5
2157 sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make(
2158 -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE,
2159 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
2160 nullptr);
2161 sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr);
2162 SkIRect subset = image->bounds();
2163 SkIRect clipBounds = image->bounds();
2164 clipBounds.outset(60, 60);
2165 SkIRect outSubset;
2166 SkIPoint offset;
2167 sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds,
2168 &outSubset, &offset));
2169 SkPaint paint;
2170 paint.setAntiAlias(true);
2171 paint.setStyle(SkPaint::kStroke_Style);
2172 canvas->drawLine(0, 0, offset.fX, offset.fY, paint);
2173 canvas->translate(offset.fX, offset.fY);
2174 canvas->drawImage(filtered, 0, 0);
2175 canvas->drawRect(SkRect::MakeFromIRect(outSubset), paint);
Cary Clarka560c472017-11-27 10:44:06 -05002176##
2177
Cary Clarkac47b882018-01-11 10:35:44 -05002178#SeeAlso SkPaint::setImageFilter
Cary Clarka560c472017-11-27 10:44:06 -05002179
2180#Method ##
2181
2182# ------------------------------------------------------------------------------
2183
Cary Clark61ca7c52018-01-02 11:34:14 -05002184#Struct DeferredTextureImageUsageParams
2185
Cary Clarkac47b882018-01-11 10:35:44 -05002186Used only by Chrome; to be deprecated.
2187
Cary Clark61ca7c52018-01-02 11:34:14 -05002188#Code
2189 struct DeferredTextureImageUsageParams {
2190 DeferredTextureImageUsageParams(const SkMatrix matrix, const SkFilterQuality quality,
2191 int preScaleMipLevel);
2192 SkMatrix fMatrix;
2193 SkFilterQuality fQuality;
2194 int fPreScaleMipLevel;
2195 };
2196##
2197
2198#Member SkMatrix fMatrix
2199##
2200
2201#Member SkFilterQuality fQuality
2202##
2203
2204#Member int fPreScaleMipLevel
2205##
2206
2207#Method DeferredTextureImageUsageParams(const SkMatrix matrix, const SkFilterQuality quality,
2208 int preScaleMipLevel)
2209
2210#Param matrix incomplete ##
2211#Param quality incomplete ##
2212#Param preScaleMipLevel incomplete ##
2213
2214#Return incomplete ##
2215
2216#Example
2217// incomplete
2218##
2219
2220#SeeAlso incomplete
2221
2222##
2223
2224#Example
2225// incomplete
2226##
2227
2228#SeeAlso incomplete
2229
2230##
2231
2232#Method size_t getDeferredTextureImageData(const GrContextThreadSafeProxy& contextThreadSafeProxy,
2233 const DeferredTextureImageUsageParams deferredTextureImageUsageParams[],
2234 int paramCnt,
2235 void* buffer,
2236 SkColorSpace* dstColorSpace = nullptr,
2237 SkColorType dstColorType = kN32_SkColorType) const
2238
Cary Clarkac47b882018-01-11 10:35:44 -05002239Used only by Chrome; to be deprecated.
2240
Cary Clark61ca7c52018-01-02 11:34:14 -05002241This method allows clients to capture the data necessary to turn a SkImage into a texture-
2242backed image. If the original image is codec-backed this will decode into a format optimized
2243for the context represented by the proxy. This method is thread safe with respect to the
2244GrContext whence the proxy came. Clients allocate and manage the storage of the deferred
2245texture data and control its lifetime. No cleanup is required, thus it is safe to simply free
2246the memory out from under the data.
2247
2248The same method is used both for getting the size necessary for pre-uploaded texture data
2249and for retrieving the data. The params array represents the set of draws over which to
2250optimize the pre-upload data.
2251
2252When called with a null buffer this returns the size that the client must allocate in order
2253to create deferred texture data for this image (or zero if this is an inappropriate
2254candidate). The buffer allocated by the client should be 8 byte aligned.
2255
2256When buffer is not null this fills in the deferred texture data for this image in the
2257provided buffer (assuming this is an appropriate candidate image and the buffer is
2258appropriately aligned). Upon success the size written is returned, otherwise 0.
2259
Cary Clarkac47b882018-01-11 10:35:44 -05002260dstColorSpace is the Color_Space of the surface where this texture will ultimately be used.
Cary Clark61ca7c52018-01-02 11:34:14 -05002261If the method determines that mip-maps are needed, this helps determine the correct strategy
2262for building them (gamma-correct or not).
2263
2264dstColorType is the color type of the surface where this texture will ultimately be used.
2265This determines the format with which the image will be uploaded to the GPU. If dstColorType
Cary Clarkac47b882018-01-11 10:35:44 -05002266does not support Color_Space (low bit depth types such as kARGB_4444_SkColorType),
Cary Clark61ca7c52018-01-02 11:34:14 -05002267then dstColorSpace must be null.
2268
Cary Clarkac47b882018-01-11 10:35:44 -05002269#Param contextThreadSafeProxy thread safe GPU context ##
2270#Param deferredTextureImageUsageParams array of Image transformations ##
2271#Param paramCnt entries in deferredTextureImageUsageParams array ##
2272#Param buffer storage for GPU_Texture data, or nullptr ##
2273#Param dstColorSpace Surface Color_Space, or nullptr ##
2274#Param dstColorType Surface Color_Type ##
Cary Clark61ca7c52018-01-02 11:34:14 -05002275
Cary Clarkac47b882018-01-11 10:35:44 -05002276#Return size of storage for GPU_Texture data ##
Cary Clark61ca7c52018-01-02 11:34:14 -05002277
2278#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002279#Image 5
2280#Platform gpu
2281 GrContext* context = canvas->getGrContext();
2282 if (!context) {
2283 return;
2284 }
2285 sk_sp<GrContextThreadSafeProxy> proxy(context->threadSafeProxy());
2286 auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(2, 2),
2287 kNone_SkFilterQuality, 0);
2288 SkColorSpace* colorSpace = canvas->imageInfo().colorSpace();
2289 size_t requiredMemoryInBytes = image->getDeferredTextureImageData(
2290 *proxy, &params, 1, nullptr, colorSpace);
2291 std::vector<uint8_t> memory;
2292 memory.resize(requiredMemoryInBytes);
2293 image->getDeferredTextureImageData(*proxy, &params, 1, memory.data(), colorSpace);
2294 sk_sp<SkImage> uploadedEncodedImage = SkImage::MakeFromDeferredTextureImageData(
2295 context, memory.data(), SkBudgeted::kNo);
2296 canvas->scale(2, 2);
2297 canvas->drawImage(uploadedEncodedImage, 10, 10);
Cary Clark61ca7c52018-01-02 11:34:14 -05002298##
2299
Cary Clarkac47b882018-01-11 10:35:44 -05002300#SeeAlso MakeFromDeferredTextureImageData
Cary Clark61ca7c52018-01-02 11:34:14 -05002301
2302##
2303
2304#Method static sk_sp<SkImage> MakeFromDeferredTextureImageData(GrContext* context, const void* data,
2305 SkBudgeted budgeted)
2306
Cary Clarkac47b882018-01-11 10:35:44 -05002307Used only by Chrome; to be deprecated.
2308
Cary Clark61ca7c52018-01-02 11:34:14 -05002309Returns a texture-backed image from data produced in SkImage::getDeferredTextureImageData.
2310The context must be the context that provided the proxy passed to
2311getDeferredTextureImageData.
2312
2313#Param context GPU_Context ##
2314#Param data incomplete ##
2315#Param budgeted incomplete ##
2316
2317#Return incomplete ##
2318
2319#Example
2320// incomplete
2321##
2322
2323#SeeAlso incomplete
2324
2325##
2326
2327# ------------------------------------------------------------------------------
2328
Cary Clarka560c472017-11-27 10:44:06 -05002329#Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc
2330
2331##
2332
2333# ------------------------------------------------------------------------------
2334
2335#Method static bool MakeBackendTextureFromSkImage(GrContext* context,
2336 sk_sp<SkImage> image,
2337 GrBackendTexture* backendTexture,
2338 BackendTextureReleaseProc* backendTextureReleaseProc)
2339
2340Creates a GrBackendTexture from the provided SkImage. Returns true on success. The
2341GrBackendTexture and BackendTextureReleaseProc are populated on success. It is the callers
2342responsibility to call the BackendTextureReleaseProc once they have deleted the texture.
2343Note that the BackendTextureReleaseProc allows Skia to clean up auxiliary data related
2344to the GrBackendTexture, and is not a substitute for the client deleting the GrBackendTexture
2345themselves.
2346
2347If image is both texture backed and singly referenced; that is, its only
2348reference was transferred using std::move(): image is returned in backendTexture
2349without conversion or making a copy.
2350
2351If the SkImage is not texture backed, this function will generate a texture with the image's
2352contents and return that.
2353
Cary Clark61ca7c52018-01-02 11:34:14 -05002354#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05002355#Param image incomplete ##
2356#Param backendTexture incomplete ##
2357#Param backendTextureReleaseProc incomplete ##
2358
2359#Return incomplete ##
2360
2361#Example
2362// incomplete
2363##
2364
2365#SeeAlso incomplete
2366
2367#Method ##
2368
2369# ------------------------------------------------------------------------------
2370
2371#Enum LegacyBitmapMode
2372
2373#Code
2374 enum LegacyBitmapMode {
2375 kRO_LegacyBitmapMode,
2376 kRW_LegacyBitmapMode,
2377 };
2378##
2379
2380Helper functions to convert to SkBitmap
2381
2382#Const kRO_LegacyBitmapMode 0
2383##
2384#Const kRW_LegacyBitmapMode 1
2385##
2386
2387#Example
2388// incomplete
2389##
2390
2391#SeeAlso incomplete
2392
2393#Enum ##
2394
2395# ------------------------------------------------------------------------------
2396
2397#Method bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode) const
2398
Cary Clarkac47b882018-01-11 10:35:44 -05002399Creates raster Bitmap with same pixels as Image. If legacyBitmapMode is
2400kRO_LegacyBitmapMode, returned bitmap is read-only and immutable.
2401Returns true if Bitmap is stored in bitmap. Returns false and resets bitmap if
2402Bitmap write did not succeed.
Cary Clarka560c472017-11-27 10:44:06 -05002403
Cary Clark3cd22cc2017-12-01 11:49:58 -05002404#Param bitmap storage for legacy Bitmap ##
2405#Param legacyBitmapMode one of: kRO_LegacyBitmapMode, kRW_LegacyBitmapMode ##
Cary Clarka560c472017-11-27 10:44:06 -05002406
Cary Clark3cd22cc2017-12-01 11:49:58 -05002407#Return true if Bitmap was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002408
2409#Example
2410// incomplete
2411##
2412
2413#SeeAlso incomplete
2414
2415#Method ##
2416
2417# ------------------------------------------------------------------------------
2418
2419#Method bool isLazyGenerated() const
2420
2421Returns true if Image is backed by an image-generator or other service that creates
2422and caches its pixels or texture on-demand.
2423
Cary Clark2f466242017-12-11 16:03:17 -05002424#Return true if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002425
2426#Example
Cary Clark2f466242017-12-11 16:03:17 -05002427#Height 80
2428#Function
2429class TestImageGenerator : public SkImageGenerator {
2430public:
2431 TestImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(10, 10)) {}
2432 ~TestImageGenerator() override {}
2433protected:
2434 bool onGetPixels(const SkImageInfo& info, void* pixelPtr, size_t rowBytes,
2435 const Options& options) override {
2436 SkPMColor* pixels = static_cast<SkPMColor*>(pixelPtr);
2437 for (int y = 0; y < info.height(); ++y) {
2438 for (int x = 0; x < info.width(); ++x) {
2439 pixels[y * info.width() + x] = 0xff223344 + y * 0x000C0811;
2440 }
2441 }
2442 return true;
2443 }
2444};
2445##
2446void draw(SkCanvas* canvas) {
2447 auto gen = std::unique_ptr<TestImageGenerator>(new TestImageGenerator());
2448 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
2449 SkString lazy(image->isLazyGenerated() ? "is lazy" : "not lazy");
2450 canvas->scale(8, 8);
2451 canvas->drawImage(image, 0, 0, nullptr);
2452 SkPaint paint;
2453 paint.setTextSize(4);
2454 canvas->drawString(lazy, 2, 5, paint);
2455}
Cary Clarka560c472017-11-27 10:44:06 -05002456##
2457
Cary Clarkf5404bb2018-01-05 12:10:09 -05002458#Example
2459#Image 5
2460#Platform gpu
2461void draw(SkCanvas* canvas) {
2462 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2463 if (nullptr == image) {
2464 return;
2465 }
2466 SkPaint paint;
2467 paint.setAntiAlias(true);
2468 paint.setTextAlign(SkPaint::kCenter_Align);
2469 canvas->drawImage(image, 0, 0);
2470 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
2471 canvas->drawString(
2472 image->isLazyGenerated() ? "is lazily generated" : "not lazily generated",
2473 image->width() / 2, image->height() * 3 / 4, paint);
2474 };
2475 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2476 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2477 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2478 drawImage(image, "image");
2479 canvas->translate(image->width(), 0);
2480 drawImage(bitmapImage, "source");
2481 canvas->translate(-image->width(), image->height());
2482 drawImage(textureImage, "backEndTexture");
2483}
2484##
2485
Cary Clarkac47b882018-01-11 10:35:44 -05002486#SeeAlso isTextureBacked MakeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002487
2488#Method ##
2489
2490# ------------------------------------------------------------------------------
2491
2492#Method sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target,
2493 SkTransferFunctionBehavior premulBehavior) const
2494
Cary Clarkac47b882018-01-11 10:35:44 -05002495Creates Image in target Color_Space.
2496Returns nullptr if Image could not be created.
Cary Clarka560c472017-11-27 10:44:06 -05002497
Cary Clarkac47b882018-01-11 10:35:44 -05002498Returns original Image if it is in target Color_Space.
2499Otherwise, converts pixels from Image Color_Space to target Color_Space.
2500If Image colorSpace returns nullptr, Image Color_Space is assumed to be sRGB.
2501
2502SkTransferFunctionBehavior is to be deprecated.
2503
2504Set premulBehavior to SkTransferFunctionBehavior::kRespect to convert Image
2505pixels to a linear space, before converting to destination Color_Type
Cary Clarka560c472017-11-27 10:44:06 -05002506and Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -05002507
Cary Clarkac47b882018-01-11 10:35:44 -05002508Set premulBehavior to SkTransferFunctionBehavior::kIgnore to treat Image
2509pixels as linear, when converting to destination Color_Type
2510and Color_Space, ignoring pixel encoding.
Cary Clarka560c472017-11-27 10:44:06 -05002511
Cary Clarkac47b882018-01-11 10:35:44 -05002512#Param target Color_Space describing color range of returned Image ##
2513#Param premulBehavior one of: SkTransferFunctionBehavior::kRespect,
2514 SkTransferFunctionBehavior::kIgnore
Cary Clarka560c472017-11-27 10:44:06 -05002515##
2516
Cary Clarkac47b882018-01-11 10:35:44 -05002517#Return created Image in target Color_Space ##
2518
2519#Example
2520#Image 5
2521#Set sRGB
2522 sk_sp<SkColorSpace> normalColorSpace = SkColorSpace::MakeRGB(
2523 SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kSRGB_Gamut);
2524 sk_sp<SkColorSpace> wackyColorSpace = normalColorSpace->makeColorSpin();
2525 for (auto colorSpace : { normalColorSpace, wackyColorSpace } ) {
2526 for (auto transfer : { SkTransferFunctionBehavior::kRespect,
2527 SkTransferFunctionBehavior::kIgnore } ) {
2528 sk_sp<SkImage> colorSpaced = image->makeColorSpace(colorSpace, transfer);
2529 canvas->drawImage(colorSpaced, 0, 0);
2530 canvas->translate(128, 0);
2531 }
2532 canvas->translate(-256, 128);
2533 }
2534##
2535
2536#SeeAlso MakeFromPixture MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05002537
2538#Method ##
2539
2540#Class SkImage ##
2541
2542#Topic Image ##