blob: ffcd6fe60a51d0d56c0b0709d75e0ed7e2f44482 [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 Clarka560c472017-11-27 10:44:06 -050052#Subtopic Member_Functions
53#Table
54#Legend
55# description # function ##
56#Legend ##
Cary Clark61ca7c52018-01-02 11:34:14 -050057# MakeBackendTextureFromSkImage # Creates GPU_Texture from Image. ##
Cary Clark3cd22cc2017-12-01 11:49:58 -050058# MakeCrossContextFromEncoded # Creates Image from encoded data, and uploads to GPU. ##
59# MakeFromAHardwareBuffer # Creates Image from Android hardware buffer. ##
Cary Clark61ca7c52018-01-02 11:34:14 -050060# MakeFromAdoptedTexture # Creates Image from GPU_Texture, managed internally. ##
Cary Clark3cd22cc2017-12-01 11:49:58 -050061# MakeFromBitmap # Creates Image from Bitmap, sharing or copying pixels. ##
Cary Clark61ca7c52018-01-02 11:34:14 -050062# MakeFromDeferredTextureImageData # ##
Cary Clark3cd22cc2017-12-01 11:49:58 -050063# MakeFromEncoded # Creates Image from encoded data. ##
64# MakeFromGenerator # Creates Image from a stream of data. ##
65# MakeFromNV12TexturesCopy # Creates Image from YUV_ColorSpace data in two planes. ##
66# MakeFromPicture # Creates Image from Picture. ##
67# MakeFromRaster # Creates Image from Pixmap, with release. ##
Cary Clark61ca7c52018-01-02 11:34:14 -050068# MakeFromTexture # Creates Image from GPU_Texture, managed externally. ##
Cary Clark3cd22cc2017-12-01 11:49:58 -050069# MakeFromYUVTexturesCopy # Creates Image from YUV_ColorSpace data in three planes. ##
70# MakeRasterCopy # Creates Image from Pixmap and copied pixels. ##
71# MakeRasterData # Creates Image from Image_Info and shared pixels. ##
Cary Clark61ca7c52018-01-02 11:34:14 -050072# alphaType # Returns Alpha_Type. ##
73# asLegacyBitmap # Returns as Raster_Bitmap. ##
Cary Clark2f466242017-12-11 16:03:17 -050074# bounds() # Returns width() and height() as Rectangle. ##
75# colorSpace # Returns Color_Space. ##
76# dimensions() # Returns width() and height(). ##
77# encodeToData # Returns encoded Image as SkData. ##
Cary Clark61ca7c52018-01-02 11:34:14 -050078# getDeferredTextureImageData # ##
Cary Clark2f466242017-12-11 16:03:17 -050079# getTexture # Deprecated. ##
80# getTextureHandle # Returns GPU reference to Image as texture. ##
81# height() # Returns pixel row count. ##
82# isAlphaOnly # Returns if pixels represent a transparency mask. ##
83# isLazyGenerated # Returns if Image is created as needed. ##
84# isOpaque # Returns if Alpha_Type is kOpaque_SkAlphaType. ##
Cary Clark61ca7c52018-01-02 11:34:14 -050085# isTextureBacked # Returns if Image was created from GPU_Texture. ##
Cary Clark2f466242017-12-11 16:03:17 -050086# isValid # Returns if Image can draw to Raster_Surface or GPU_Context. ##
87# makeColorSpace # Creates Image matching Color_Space if possible. ##
Cary Clark61ca7c52018-01-02 11:34:14 -050088# makeNonTextureImage # Creates Image without dependency on GPU_Texture. ##
89# makeRasterImage # Creates Image compatible with Raster_Surface if possible. ##
Cary Clark2f466242017-12-11 16:03:17 -050090# makeShader # Creates Shader, Paint element that can tile Image. ##
91# makeSubset # Creates Image containing part of original. ##
92# makeTextureImage # Creates Image matching Color_Space if possible. ##
93# makeWithFilter # Creates filtered, clipped Image. ##
94# peekPixels # Returns Pixmap if possible. ##
95# readPixels # Copies and converts pixels. ##
96# refColorSpace # Returns Image_Info Color_Space. ##
97# refEncodedData # Returns Image encoded in SkData if present. ##
98# scalePixels # Scales and converts one Image to another. ##
99# toString # Converts Image to machine readable form. ##
100# uniqueID # Identifier for Image. ##
101# width() # Returns pixel column count. ##
Cary Clarka560c472017-11-27 10:44:06 -0500102#Table ##
103#Subtopic ##
104
105#Topic ##
106
Cary Clarka560c472017-11-27 10:44:06 -0500107#Typedef SkImageInfo Info
108
109##
110
Cary Clarka560c472017-11-27 10:44:06 -0500111# ------------------------------------------------------------------------------
112
113#Method static sk_sp<SkImage> MakeRasterCopy(const SkPixmap& pixmap)
114
Cary Clark2f466242017-12-11 16:03:17 -0500115Creates Image from Pixmap and copy of pixels. Since pixels are copied, Pixmap
116pixels may be modified or deleted without affecting Image.
Cary Clarka560c472017-11-27 10:44:06 -0500117
Cary Clark3cd22cc2017-12-01 11:49:58 -0500118Image is returned if Pixmap is valid. Valid Pixmap parameters include:
119dimensions are greater than zero;
120each dimension fits in 29 bits;
121Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
122row bytes are large enough to hold one row of pixels;
123pixel address is not nullptr.
124
125#Param pixmap Image_Info, pixel address, and row bytes ##
126
127#Return copy of Pixmap pixels, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500128
129#Example
Cary Clark2f466242017-12-11 16:03:17 -0500130#Height 50
131#Description
132Draw a five by five bitmap, and draw a copy in an Image. Editing the pixmap
133alters the bitmap draw, but does not alter the Image draw since the Image
134contains a copy of the pixels.
135##
136 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
137 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
138 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
139 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
140 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
141 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
142 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
143 SkBitmap bitmap;
144 bitmap.installPixels(pixmap);
145 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
146 *pixmap.writable_addr8(2, 2) = 0x00;
147 canvas->scale(10, 10);
148 canvas->drawBitmap(bitmap, 0, 0);
149 canvas->drawImage(image, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500150##
151
Cary Clark3cd22cc2017-12-01 11:49:58 -0500152#SeeAlso MakeRasterData MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500153
154#Method ##
155
156# ------------------------------------------------------------------------------
157
158#Method static sk_sp<SkImage> MakeRasterData(const Info& info, sk_sp<SkData> pixels, size_t rowBytes)
159
Cary Clark3cd22cc2017-12-01 11:49:58 -0500160Creates Image from Image_Info, sharing pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500161
Cary Clark3cd22cc2017-12-01 11:49:58 -0500162Image is returned if Image_Info is valid. Valid Image_Info parameters include:
163dimensions are greater than zero;
164each dimension fits in 29 bits;
165Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
166rowBytes are large enough to hold one row of pixels;
167pixels is not nullptr, and contains enough data for Image.
168
169#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
170#Param pixels address or pixel storage ##
171#Param rowBytes size of pixel row or larger ##
172
173#Return Image sharing pixels, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500174
175#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500176#Image 3
177 size_t rowBytes = image->width() * SkColorTypeBytesPerPixel(kRGBA_8888_SkColorType);
178 sk_sp<SkData> data = SkData::MakeUninitialized(rowBytes * image->height());
179 SkImageInfo dstInfo = SkImageInfo::MakeN32(image->width(), image->height(),
180 kPremul_SkAlphaType);
181 image->readPixels(dstInfo, data->writable_data(), rowBytes, 0, 0, SkImage::kAllow_CachingHint);
182 sk_sp<SkImage> raw = SkImage::MakeRasterData(dstInfo.makeColorType(kRGBA_8888_SkColorType),
183 data, rowBytes);
184 canvas->drawImage(image, 0, 0);
185 canvas->drawImage(raw.get(), 128, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500186##
187
Cary Clark3cd22cc2017-12-01 11:49:58 -0500188#SeeAlso MakeRasterCopy MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500189
190#Method ##
191
192# ------------------------------------------------------------------------------
193
Cary Clark3cd22cc2017-12-01 11:49:58 -0500194#Typedef void* ReleaseContext
195
196Caller data passed to RasterReleaseProc; may be nullptr.
197
198#SeeAlso MakeFromRaster RasterReleaseProc
199
200##
201
Cary Clarka560c472017-11-27 10:44:06 -0500202#Typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext)
203
Cary Clark3cd22cc2017-12-01 11:49:58 -0500204Function called when Image no longer shares pixels. ReleaseContext is
205provided by caller when Image is created, and may be nullptr.
206
207#SeeAlso ReleaseContext MakeFromRaster
208
Cary Clarka560c472017-11-27 10:44:06 -0500209##
210
211#Method static sk_sp<SkImage> MakeFromRaster(const SkPixmap& pixmap,
212 RasterReleaseProc rasterReleaseProc,
213 ReleaseContext releaseContext)
214
Cary Clark0c5f5462017-12-15 11:21:51 -0500215Creates Image from pixmap, sharing Pixmap pixels. Pixels must remain valid and
Cary Clark3cd22cc2017-12-01 11:49:58 -0500216unchanged until rasterReleaseProc is called. rasterReleaseProc is passed
217releaseContext when Image is deleted or no longer refers to pixmap pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500218
Cary Clark0c5f5462017-12-15 11:21:51 -0500219Pass nullptr for rasterReleaseProc to share Pixmap without requiring a callback
220when Image is released. Pass nullptr for releaseContext if rasterReleaseProc
221does not require state.
222
Cary Clark3cd22cc2017-12-01 11:49:58 -0500223Image is returned if pixmap is valid. Valid Pixmap parameters include:
224dimensions are greater than zero;
225each dimension fits in 29 bits;
226Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
227row bytes are large enough to hold one row of pixels;
228pixel address is not nullptr.
229
230#Param pixmap Image_Info, pixel address, and row bytes ##
Cary Clark0c5f5462017-12-15 11:21:51 -0500231#Param rasterReleaseProc function called when pixels can be released; or nullptr ##
232#Param releaseContext state passed to rasterReleaseProc; or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500233
Cary Clark0c5f5462017-12-15 11:21:51 -0500234#Return Image sharing pixmap ##
Cary Clarka560c472017-11-27 10:44:06 -0500235
236#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500237#Function
238static void releaseProc(const void* pixels, SkImage::ReleaseContext context) {
239 int* countPtr = static_cast<int*>(context);
240 *countPtr += 1;
241}
242##
243
244void draw(SkCanvas* canvas) {
245 SkColor color = 0;
246 SkPixmap pixmap(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), &color, 4);
247 int releaseCount = 0;
248 sk_sp<SkImage> image(SkImage::MakeFromRaster(pixmap, releaseProc, &releaseCount));
249 SkDebugf("before reset: %d\n", releaseCount);
250 image.reset();
251 SkDebugf("after reset: %d\n", releaseCount);
252}
253#StdOut
254before reset: 0
255after reset: 1
256##
Cary Clarka560c472017-11-27 10:44:06 -0500257##
258
Cary Clark3cd22cc2017-12-01 11:49:58 -0500259#SeeAlso MakeRasterCopy MakeRasterData MakeFromGenerator RasterReleaseProc ReleaseContext
Cary Clarka560c472017-11-27 10:44:06 -0500260
261#Method ##
262
263# ------------------------------------------------------------------------------
264
265#Method static sk_sp<SkImage> MakeFromBitmap(const SkBitmap& bitmap)
266
Cary Clark3cd22cc2017-12-01 11:49:58 -0500267Creates Image from bitmap, sharing or copying bitmap pixels. If the bitmap
268is marked immutable, and its pixel memory is shareable, it may be shared
269instead of copied.
Cary Clarka560c472017-11-27 10:44:06 -0500270
Cary Clark3cd22cc2017-12-01 11:49:58 -0500271Image is returned if bitmap is valid. Valid Bitmap parameters include:
272dimensions are greater than zero;
273each dimension fits in 29 bits;
274Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
275row bytes are large enough to hold one row of pixels;
276pixel address is not nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500277
Cary Clark3cd22cc2017-12-01 11:49:58 -0500278#Param bitmap Image_Info, row bytes, and pixels ##
279
280#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500281
282#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500283#Description
284The first Bitmap is shared; writing to the pixel memory changes the first
285Image.
286The second Bitmap is marked immutable, and is copied; writing to the pixel
287memory does not alter the second Image.
288##
289#Height 50
290 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
291 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
292 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
293 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
294 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
295 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
296 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
297 SkBitmap bitmap;
298 bitmap.installPixels(pixmap);
299 sk_sp<SkImage> image1 = SkImage::MakeFromBitmap(bitmap);
300 bitmap.setImmutable();
301 sk_sp<SkImage> image2 = SkImage::MakeFromBitmap(bitmap);
302 *pixmap.writable_addr8(2, 2) = 0x00;
303 canvas->scale(10, 10);
304 canvas->drawImage(image1, 0, 0);
305 canvas->drawImage(image2, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500306##
307
Cary Clark3cd22cc2017-12-01 11:49:58 -0500308#SeeAlso MakeFromRaster MakeRasterCopy MakeFromGenerator MakeRasterData
Cary Clarka560c472017-11-27 10:44:06 -0500309
310#Method ##
311
312# ------------------------------------------------------------------------------
313
314#Method static sk_sp<SkImage> MakeFromGenerator(std::unique_ptr<SkImageGenerator> imageGenerator,
315 const SkIRect* subset = nullptr)
316
Cary Clark0c5f5462017-12-15 11:21:51 -0500317Creates Image from data returned by imageGenerator. Generated data is owned by Image and may not
318be shared or accessed.
Cary Clarka560c472017-11-27 10:44:06 -0500319
Cary Clark0c5f5462017-12-15 11:21:51 -0500320subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
321otherwise, subset must be contained by image bounds.
322
323Image is returned if generator data is valid. Valid data parameters vary by type of data
324and platform.
Cary Clarka560c472017-11-27 10:44:06 -0500325
Cary Clark3cd22cc2017-12-01 11:49:58 -0500326imageGenerator may wrap Picture data, codec data, or custom data.
327
328#Param imageGenerator stock or custom routines to retrieve Image ##
329#Param subset bounds of returned Image; may be nullptr ##
330
331#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500332
333#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500334#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500335#Description
336The generator returning Picture cannot be shared; std::move transfers ownership to generated Image.
337##
338 SkPictureRecorder recorder;
339 recorder.beginRecording(100, 100)->drawColor(SK_ColorRED);
340 auto picture = recorder.finishRecordingAsPicture();
341 auto gen = SkImageGenerator::MakeFromPicture({100, 100}, picture, nullptr, nullptr,
342 SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB());
343 sk_sp<SkImage> image = SkImage::MakeFromGenerator(std::move(gen));
344 canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500345##
346
Cary Clark3cd22cc2017-12-01 11:49:58 -0500347#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500348
349#Method ##
350
351# ------------------------------------------------------------------------------
352
353#Method static sk_sp<SkImage> MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset = nullptr)
354
Cary Clark3cd22cc2017-12-01 11:49:58 -0500355Creates Image from encoded data.
Cary Clark0c5f5462017-12-15 11:21:51 -0500356subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
357otherwise, subset must be contained by image bounds.
Cary Clarka560c472017-11-27 10:44:06 -0500358
Cary Clark3cd22cc2017-12-01 11:49:58 -0500359Image is returned if format of the encoded data is recognized and supported.
360Recognized formats vary by platfrom.
Cary Clarka560c472017-11-27 10:44:06 -0500361
Cary Clark3cd22cc2017-12-01 11:49:58 -0500362#Param encoded data of Image to decode ##
363#Param subset bounds of returned Image; may be nullptr ##
364
365#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500366
Cary Clark61ca7c52018-01-02 11:34:14 -0500367#Example
368#Image 3
369int x = 0;
370for (int quality : { 100, 50, 10, 1} ) {
371 sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, quality);
372 sk_sp<SkImage> image = SkImage::MakeFromEncoded(encodedData);
373 canvas->drawImage(image, x, 0);
374 x += 64;
375}
Cary Clarka560c472017-11-27 10:44:06 -0500376##
377
Cary Clark3cd22cc2017-12-01 11:49:58 -0500378#SeeAlso MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500379
380#Method ##
381
382# ------------------------------------------------------------------------------
383
384#Typedef void (*TextureReleaseProc)(ReleaseContext releaseContext)
385
386##
387
388#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
389 const GrBackendTexture& backendTexture,
390 GrSurfaceOrigin origin,
391 SkAlphaType alphaType,
392 sk_sp<SkColorSpace> colorSpace)
393
Cary Clarkac47b882018-01-11 10:35:44 -0500394#Deprecated
Cary Clark61ca7c52018-01-02 11:34:14 -0500395Deprecated.
Cary Clarkac47b882018-01-11 10:35:44 -0500396##
Cary Clarka560c472017-11-27 10:44:06 -0500397
Cary Clark3cd22cc2017-12-01 11:49:58 -0500398#Param context GPU_Context ##
399#Param backendTexture texture residing on GPU ##
400#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
401#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
402 kPremul_SkAlphaType, kUnpremul_SkAlphaType
403##
Cary Clark61ca7c52018-01-02 11:34:14 -0500404#Param colorSpace range of colors; may be nullptr ##
405
406#Return created Image, or nullptr ##
407
Cary Clark61ca7c52018-01-02 11:34:14 -0500408#Method ##
409
410# ------------------------------------------------------------------------------
411
412#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
413 const GrBackendTexture& backendTexture,
414 GrSurfaceOrigin origin,
415 SkAlphaType alphaType,
416 sk_sp<SkColorSpace> colorSpace,
417 TextureReleaseProc textureReleaseProc,
418 ReleaseContext releaseContext)
419
Cary Clarkac47b882018-01-11 10:35:44 -0500420#Deprecated
Cary Clark61ca7c52018-01-02 11:34:14 -0500421Deprecated.
Cary Clarkac47b882018-01-11 10:35:44 -0500422##
Cary Clark61ca7c52018-01-02 11:34:14 -0500423
424#Param context GPU_Context ##
425#Param backendTexture texture residing on GPU ##
426#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
427#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
428 kPremul_SkAlphaType, kUnpremul_SkAlphaType
429##
430#Param colorSpace range of colors; may be nullptr ##
431#Param textureReleaseProc function called when texture can be released ##
432#Param releaseContext state passed to textureReleaseProc ##
433
434#Return created Image, or nullptr ##
435
Cary Clark61ca7c52018-01-02 11:34:14 -0500436#Method ##
437
438# ------------------------------------------------------------------------------
439
440#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
441 const GrBackendTexture& backendTexture,
442 GrSurfaceOrigin origin,
443 SkColorType colorType,
444 SkAlphaType alphaType,
445 sk_sp<SkColorSpace> colorSpace)
446
447Creates Image from GPU_Texture associated with context. Caller is responsible for
448managing the lifetime of GPU_Texture.
449
450Image is returned if format of backendTexture is recognized and supported.
451Recognized formats vary by GPU back-end.
452
453#Param context GPU_Context ##
454#Param backendTexture texture residing on GPU ##
455#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
456#Param colorType one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
457 kRGB_565_SkColorType, kARGB_4444_SkColorType,
458 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
459 kGray_8_SkColorType, kRGBA_F16_SkColorType
460##
461#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
462 kPremul_SkAlphaType, kUnpremul_SkAlphaType
463##
464#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500465
466#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500467
468#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500469#Image 3
470#Platform gpu
471#Height 128
472#Description
473A back-end texture has been created and uploaded to the GPU outside of this example.
474##
475GrContext* context = canvas->getGrContext();
476if (!context) {
477 return;
478}
479canvas->scale(.25f, .25f);
480int x = 0;
481for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500482 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark0c5f5462017-12-15 11:21:51 -0500483 origin, kOpaque_SkAlphaType, nullptr);
484 canvas->drawImage(image, x, 0);
485x += 512;
486}
Cary Clarka560c472017-11-27 10:44:06 -0500487##
488
Cary Clark3cd22cc2017-12-01 11:49:58 -0500489#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500490
491#Method ##
492
493# ------------------------------------------------------------------------------
494
495#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
496 const GrBackendTexture& backendTexture,
497 GrSurfaceOrigin origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500498 SkColorType colorType,
Cary Clarka560c472017-11-27 10:44:06 -0500499 SkAlphaType alphaType,
500 sk_sp<SkColorSpace> colorSpace,
501 TextureReleaseProc textureReleaseProc,
502 ReleaseContext releaseContext)
503
Cary Clark61ca7c52018-01-02 11:34:14 -0500504Creates Image from GPU_Texture associated with context. GPU_Texture must stay
Cary Clark3cd22cc2017-12-01 11:49:58 -0500505valid and unchanged until textureReleaseProc is called. textureReleaseProc is
506passed releaseContext when Image is deleted or no longer refers to texture.
Cary Clarka560c472017-11-27 10:44:06 -0500507
Cary Clark3cd22cc2017-12-01 11:49:58 -0500508Image is returned if format of backendTexture is recognized and supported.
509Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500510
Cary Clark3cd22cc2017-12-01 11:49:58 -0500511#Param context GPU_Context ##
512#Param backendTexture texture residing on GPU ##
513#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500514#Param colorType one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
515 kRGB_565_SkColorType, kARGB_4444_SkColorType,
516 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
517 kGray_8_SkColorType, kRGBA_F16_SkColorType
518##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500519#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
520 kPremul_SkAlphaType, kUnpremul_SkAlphaType
521##
Cary Clark61ca7c52018-01-02 11:34:14 -0500522#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500523#Param textureReleaseProc function called when texture can be released ##
524#Param releaseContext state passed to textureReleaseProc ##
525
526#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500527
Cary Clark0c5f5462017-12-15 11:21:51 -0500528#ToDo
529This doesn't do anything clever with TextureReleaseProc because it may not get called
Cary Clark61ca7c52018-01-02 11:34:14 -0500530fwithin the lifetime of the example
Cary Clark0c5f5462017-12-15 11:21:51 -0500531##
532
Cary Clarka560c472017-11-27 10:44:06 -0500533#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500534#Platform gpu
535#Image 4
Cary Clark0c5f5462017-12-15 11:21:51 -0500536GrContext* context = canvas->getGrContext();
537if (!context) {
538 return;
539}
Cary Clarkac47b882018-01-11 10:35:44 -0500540auto debugster = [](SkImage::ReleaseContext releaseContext) -> void {
541 *((int *) releaseContext) += 128;
Cary Clark0c5f5462017-12-15 11:21:51 -0500542};
543int x = 0;
544for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500545 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark61ca7c52018-01-02 11:34:14 -0500546 origin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr, debugster, &x);
Cary Clark0c5f5462017-12-15 11:21:51 -0500547 canvas->drawImage(image, x, 0);
548 x += 128;
549}
Cary Clarka560c472017-11-27 10:44:06 -0500550##
551
Cary Clark3cd22cc2017-12-01 11:49:58 -0500552#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500553
554#Method ##
555
556# ------------------------------------------------------------------------------
557
558#Method static sk_sp<SkImage> MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> data,
559 bool buildMips,
560 SkColorSpace* dstColorSpace)
561
Cary Clark3cd22cc2017-12-01 11:49:58 -0500562Creates Image from encoded data. Image is uploaded to GPU back-end using context.
563
564Created Image is available to other GPU contexts, and is available across thread
565boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
566share resources.
567
568When Image is no longer referenced, context releases texture memory
Cary Clarka560c472017-11-27 10:44:06 -0500569asynchronously.
Cary Clarka560c472017-11-27 10:44:06 -0500570
Cary Clark3cd22cc2017-12-01 11:49:58 -0500571Texture decoded from data is uploaded to match Surface created with
572dstColorSpace. Color_Space of Image is determined by encoded data.
Cary Clarka560c472017-11-27 10:44:06 -0500573
Cary Clark3cd22cc2017-12-01 11:49:58 -0500574Image is returned if format of data is recognized and supported, and if context
575supports moving resources. Recognized formats vary by platform and GPU back-end.
576
Cary Clark61ca7c52018-01-02 11:34:14 -0500577Image is returned using MakeFromEncoded if context is nullptr or does not support
578moving resources between contexts.
579
Cary Clark3cd22cc2017-12-01 11:49:58 -0500580#Param context GPU_Context ##
581#Param data Image to decode ##
582#Param buildMips create Image as Mip_Map if true ##
583#Param dstColorSpace range of colors of matching Surface on GPU ##
584
585#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500586
587#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500588#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500589#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500590GrContext* context = canvas->getGrContext();
591sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, 100);
592sk_sp<SkImage> image = SkImage::MakeCrossContextFromEncoded(context,
593 encodedData, false, nullptr);
594canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500595##
596
Cary Clark3cd22cc2017-12-01 11:49:58 -0500597#SeeAlso MakeCrossContextFromPixmap
598
599#Method ##
600
601# ------------------------------------------------------------------------------
602
603#Method static sk_sp<SkImage> MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap,
604 bool buildMips,
605 SkColorSpace* dstColorSpace)
606
607Creates Image from pixmap. Image is uploaded to GPU back-end using context.
608
609Created Image is available to other GPU contexts, and is available across thread
610boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
611share resources.
612
613When Image is no longer referenced, context releases texture memory
614asynchronously.
615
616Texture created from pixmap is uploaded to match Surface created with
617dstColorSpace. Color_Space of Image is determined by pixmap.colorSpace().
618
Cary Clark61ca7c52018-01-02 11:34:14 -0500619Image is returned referring to GPU back-end if context is not nullptr,
620format of data is recognized and supported, and if context supports moving
621resources between contexts. Otherwise, pixmap pixel data is copied and Image
622as returned in raster format if possible; nullptr may be returned.
623Recognized GPU formats vary by platform and GPU back-end.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500624
625#Param context GPU_Context ##
626#Param pixmap Image_Info, pixel address, and row bytes ##
627#Param buildMips create Image as Mip_Map if true ##
628#Param dstColorSpace range of colors of matching Surface on GPU ##
629
630#Return created Image, or nullptr ##
631
632#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500633#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500634#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500635GrContext* context = canvas->getGrContext();
636SkPixmap pixmap;
637if (source.peekPixels(&pixmap)) {
638 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(context, pixmap,
639 false, nullptr);
640 canvas->drawImage(image, 0, 0);
641}
Cary Clark3cd22cc2017-12-01 11:49:58 -0500642##
643
644#SeeAlso MakeCrossContextFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500645
646#Method ##
647
648# ------------------------------------------------------------------------------
649
650#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
651 const GrBackendTexture& backendTexture,
652 GrSurfaceOrigin surfaceOrigin,
653 SkAlphaType alphaType = kPremul_SkAlphaType,
654 sk_sp<SkColorSpace> colorSpace = nullptr)
655
Cary Clark61ca7c52018-01-02 11:34:14 -0500656Deprecated.
657
658#Param context GPU_Context ##
659#Param backendTexture texture residing on GPU ##
660#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
661#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
662 kPremul_SkAlphaType, kUnpremul_SkAlphaType
663##
664#Param colorSpace range of colors; may be nullptr ##
665
666#Return created Image, or nullptr ##
667
668#NoExample
669##
670
671#SeeAlso MakeFromTexture MakeFromYUVTexturesCopy
672
673#Method ##
674
675# ------------------------------------------------------------------------------
676
677#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
678 const GrBackendTexture& backendTexture,
679 GrSurfaceOrigin surfaceOrigin,
680 SkColorType colorType,
681 SkAlphaType alphaType = kPremul_SkAlphaType,
682 sk_sp<SkColorSpace> colorSpace = nullptr)
683
Cary Clark3cd22cc2017-12-01 11:49:58 -0500684Creates Image from backendTexture associated with context. backendTexture and
685returned Image are managed internally, and are released when no longer needed.
Cary Clarka560c472017-11-27 10:44:06 -0500686
Cary Clark3cd22cc2017-12-01 11:49:58 -0500687Image is returned if format of backendTexture is recognized and supported.
688Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500689
Cary Clark3cd22cc2017-12-01 11:49:58 -0500690#Param context GPU_Context ##
691#Param backendTexture texture residing on GPU ##
692#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500693#Param colorType one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
694 kRGB_565_SkColorType, kARGB_4444_SkColorType,
695 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
696 kGray_8_SkColorType, kRGBA_F16_SkColorType
697##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500698#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
699 kPremul_SkAlphaType, kUnpremul_SkAlphaType
700##
Cary Clark61ca7c52018-01-02 11:34:14 -0500701#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500702
703#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500704
705#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500706#Image 5
707#Platform gpu
Cary Clark61ca7c52018-01-02 11:34:14 -0500708 if (!canvas->getGrContext()) {
709 return;
710 }
711 canvas->scale(.5f, .5f);
712 canvas->clear(0x7f3f5f7f);
713 int x = 0, y = 0;
714 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
715 for (auto alpha : { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType } ) {
716 sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(canvas->getGrContext(),
717 backEndTexture, origin,
718 kRGBA_8888_SkColorType, alpha);
719 canvas->drawImage(image, x, y);
720 x += 160;
721 }
722 x -= 160 * 3;
723 y += 256;
724 }
Cary Clarka560c472017-11-27 10:44:06 -0500725##
726
Cary Clark61ca7c52018-01-02 11:34:14 -0500727#SeeAlso MakeFromTexture MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500728
729#Method ##
730
731# ------------------------------------------------------------------------------
732
733#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
734 const GrBackendObject yuvTextureHandles[3],
735 const SkISize yuvSizes[3],
736 GrSurfaceOrigin surfaceOrigin,
737 sk_sp<SkColorSpace> colorSpace = nullptr)
738
Cary Clark61ca7c52018-01-02 11:34:14 -0500739Creates Image from copy of yuvTextureHandles, an array of textures on GPU.
740yuvTextureHandles contain pixels for YUV planes of Image.
741yuvSizes conain dimensions for each pixel plane. Dimensions must be greater than
742zero but may differ from plane to plane. Returned Image has the dimensions
743yuvSizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500744
Cary Clark61ca7c52018-01-02 11:34:14 -0500745#Param context GPU_Context ##
746#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
747 kRec709_SkYUVColorSpace
748##
749#Param yuvTextureHandles array of YUV textures on GPU ##
750#Param yuvSizes dimensions of YUV textures ##
751#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
752#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500753
Cary Clark61ca7c52018-01-02 11:34:14 -0500754#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500755
Cary Clark61ca7c52018-01-02 11:34:14 -0500756# seems too complicated to create an example for this
757#ToDo
758should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500759##
760
Cary Clark61ca7c52018-01-02 11:34:14 -0500761#NoExample
762##
763
764#SeeAlso MakeFromNV12TexturesCopy
765
766#Method ##
767
768# ------------------------------------------------------------------------------
769
770#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
771 const GrBackendTexture yuvTextureHandles[3],
772 const SkISize yuvSizes[3],
773 GrSurfaceOrigin surfaceOrigin,
774 sk_sp<SkColorSpace> colorSpace = nullptr)
775
776Creates Image from copy of yuvTextureHandles, an array of textures on GPU.
777yuvTextureHandles contain pixels for YUV planes of Image.
778yuvSizes conain dimensions for each pixel plane. Dimensions must be greater than
779zero but may differ from plane to plane. Returned Image has the dimensions
780yuvSizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
781
782#Param context GPU_Context ##
783#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
784 kRec709_SkYUVColorSpace
785##
786#Param yuvTextureHandles array of YUV textures on GPU ##
787#Param yuvSizes dimensions of YUV textures ##
788#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
789#Param colorSpace range of colors; may be nullptr ##
790
791#Return created Image, or nullptr ##
792
793# seems too complicated to create an example for this
794#ToDo
795should this be moved to chrome only?
796##
797
798#NoExample
799##
800
801#SeeAlso MakeFromNV12TexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500802
803#Method ##
804
805# ------------------------------------------------------------------------------
806
807#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
808 SkYUVColorSpace yuvColorSpace,
809 const GrBackendObject nv12TextureHandles[2],
810 const SkISize nv12Sizes[2],
811 GrSurfaceOrigin surfaceOrigin,
812 sk_sp<SkColorSpace> colorSpace = nullptr)
813
Cary Clark61ca7c52018-01-02 11:34:14 -0500814Creates Image from copy of nv12TextureHandles, an array of textures on GPU.
815nv12TextureHandles[0] contains pixels for YUV_Component_Y plane.
816nv12TextureHandles[1] contains pixels for YUV_Component_U plane,
817followed by pixels for YUV_Component_V plane.
818nv12Sizes conain dimensions for each pixel plane. Dimensions must be greater than
819zero but may differ from plane to plane. Returned Image has the dimensions
820nv12Sizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500821
Cary Clark61ca7c52018-01-02 11:34:14 -0500822#Param context GPU_Context ##
823#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
824 kRec709_SkYUVColorSpace
825##
826#Param nv12TextureHandles array of YUV textures on GPU ##
827#Param nv12Sizes dimensions of YUV textures ##
828#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
829#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500830
Cary Clark61ca7c52018-01-02 11:34:14 -0500831#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500832
Cary Clark61ca7c52018-01-02 11:34:14 -0500833# seems too complicated to create an example for this
834#ToDo
835should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500836##
837
Cary Clark61ca7c52018-01-02 11:34:14 -0500838#NoExample
839##
840
841#SeeAlso MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500842
843#Method ##
844
845# ------------------------------------------------------------------------------
846
Cary Clark61ca7c52018-01-02 11:34:14 -0500847#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
848 SkYUVColorSpace yuvColorSpace,
849 const GrBackendTexture nv12TextureHandles[2],
850 const SkISize nv12Sizes[2],
851 GrSurfaceOrigin surfaceOrigin,
852 sk_sp<SkColorSpace> colorSpace = nullptr)
853
854Creates Image from copy of nv12TextureHandles, an array of textures on GPU.
855nv12TextureHandles[0] contains pixels for YUV_Component_Y plane.
856nv12TextureHandles[1] contains pixels for YUV_Component_U plane,
857followed by pixels for YUV_Component_V plane.
858nv12Sizes conain dimensions for each pixel plane. Dimensions must be greater than
859zero but may differ from plane to plane. Returned Image has the dimensions
860nv12Sizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
861
862#Param context GPU_Context ##
863#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
864 kRec709_SkYUVColorSpace
865##
866#Param nv12TextureHandles array of YUV textures on GPU ##
867#Param nv12Sizes dimensions of YUV textures ##
868#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
869#Param colorSpace range of colors; may be nullptr ##
870
871#Return created Image, or nullptr ##
872
873# seems too complicated to create an example for this
874#ToDo
875should this be moved to chrome only?
876##
877
878#NoExample
879##
880
881#SeeAlso MakeFromYUVTexturesCopy
882
883#Method ##
884
885# ------------------------------------------------------------------------------
886
887#Bug 7424
888currently uncalled by any test or client
889##
890
Cary Clarka560c472017-11-27 10:44:06 -0500891#Enum BitDepth
892
893#Code
Cary Clark61ca7c52018-01-02 11:34:14 -0500894 enum class BitDepth {
Cary Clarka560c472017-11-27 10:44:06 -0500895 kU8,
896 kF16,
897 };
898##
899
900#Const kU8 0
Cary Clark61ca7c52018-01-02 11:34:14 -0500901Use 8 bits per Color_ARGB component using unsigned integer format.
Cary Clarka560c472017-11-27 10:44:06 -0500902##
903#Const kF16 1
Cary Clark61ca7c52018-01-02 11:34:14 -0500904Use 16 bits per Color_ARGB component using half-precision floating point format.
Cary Clarka560c472017-11-27 10:44:06 -0500905##
906
Cary Clark61ca7c52018-01-02 11:34:14 -0500907#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500908##
909
Cary Clark61ca7c52018-01-02 11:34:14 -0500910#SeeAlso MakeFromPicture
Cary Clarka560c472017-11-27 10:44:06 -0500911
912#Enum ##
913
914# ------------------------------------------------------------------------------
915
916#Method static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
917 const SkMatrix* matrix, const SkPaint* paint,
918 BitDepth bitDepth,
919 sk_sp<SkColorSpace> colorSpace)
920
Cary Clark61ca7c52018-01-02 11:34:14 -0500921Creates Image from picture. Returned Image width and height are set by dimensions.
922Image draws picture with matrix and paint, set to bitDepth and colorSpace.
Cary Clarka560c472017-11-27 10:44:06 -0500923
Cary Clark61ca7c52018-01-02 11:34:14 -0500924If matrix is nullptr, draws with identity Matrix. If paint is nullptr, draws
925with default Paint. colorSpace may be nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500926
Cary Clark61ca7c52018-01-02 11:34:14 -0500927#Param picture stream of drawing commands ##
928#Param dimensions width and height ##
929#Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ##
930#Param paint Paint to apply transparency, filtering, and so on; may be nullptr ##
931#Param bitDepth 8 bit integer or 16 bit float: per component ##
932#Param colorSpace range of colors; may be nullptr ##
933
934#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500935
936#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500937 SkPaint paint;
938 SkPictureRecorder recorder;
939 SkCanvas* recordingCanvas = recorder.beginRecording(50, 50);
940 for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) {
941 paint.setColor(color);
942 recordingCanvas->drawRect({10, 10, 30, 40}, paint);
943 recordingCanvas->translate(10, 10);
944 recordingCanvas->scale(1.2f, 1.4f);
945 }
946 sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
947 int x = 0, y = 0;
948 for (auto alpha : { 70, 140, 210 } ) {
949 paint.setAlpha(alpha);
950 auto srgbColorSpace = SkColorSpace::MakeSRGB();
951 sk_sp<SkImage> image = SkImage::MakeFromPicture(playback, {50, 50}, nullptr, &paint,
952 SkImage::BitDepth::kU8, srgbColorSpace);
953 canvas->drawImage(image, x, y);
954 x += 70; y += 70;
955 }
Cary Clarka560c472017-11-27 10:44:06 -0500956##
957
Cary Clark61ca7c52018-01-02 11:34:14 -0500958#SeeAlso SkCanvas::drawPicture
Cary Clarka560c472017-11-27 10:44:06 -0500959
960#Method ##
961
962# ------------------------------------------------------------------------------
963
964#Method static sk_sp<SkImage> MakeFromAHardwareBuffer(AHardwareBuffer* hardwareBuffer,
965 SkAlphaType alphaType = kPremul_SkAlphaType,
966 sk_sp<SkColorSpace> colorSpace = nullptr)
967
Cary Clark61ca7c52018-01-02 11:34:14 -0500968#Bug 7447 ##
Cary Clarka560c472017-11-27 10:44:06 -0500969
Cary Clark61ca7c52018-01-02 11:34:14 -0500970Creates Image from Android hardware buffer.
971Returned Image takes a reference on the buffer.
Cary Clarka560c472017-11-27 10:44:06 -0500972
Cary Clark61ca7c52018-01-02 11:34:14 -0500973Only available on Android, when __ANDROID_API__ is defined to be 26 or greater.
Cary Clarka560c472017-11-27 10:44:06 -0500974
Cary Clark61ca7c52018-01-02 11:34:14 -0500975#Param hardwareBuffer AHardwareBuffer Android hardware buffer ##
976#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
977 kPremul_SkAlphaType, kUnpremul_SkAlphaType
978##
979#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500980
Cary Clark61ca7c52018-01-02 11:34:14 -0500981#Return created Image, or nullptr ##
982
983#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500984##
985
Cary Clark61ca7c52018-01-02 11:34:14 -0500986#SeeAlso MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -0500987
988#Method ##
989
990# ------------------------------------------------------------------------------
991
992#Method int width() const
993
Cary Clark61ca7c52018-01-02 11:34:14 -0500994Returns pixel count in each row.
995
996#Return pixel width in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500997
998#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500999#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -05001000#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -05001001 canvas->translate(10, 10);
1002 canvas->drawImage(image, 0, 0);
1003 canvas->translate(0, image->height());
1004 SkPaint paint;
1005 paint.setTextAlign(SkPaint::kCenter_Align);
1006 canvas->drawLine(0, 10, image->width(), 10, paint);
1007 canvas->drawString("width", image->width() / 2, 25, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001008##
1009
Cary Clark61ca7c52018-01-02 11:34:14 -05001010#SeeAlso dimensions() height()
Cary Clarka560c472017-11-27 10:44:06 -05001011
1012#Method ##
1013
1014# ------------------------------------------------------------------------------
1015
1016#Method int height() const
1017
Cary Clark2f466242017-12-11 16:03:17 -05001018Returns pixel row count.
1019
Cary Clark61ca7c52018-01-02 11:34:14 -05001020#Return pixel height in Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001021
1022#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001023#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -05001024#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -05001025 canvas->translate(10, 10);
1026 canvas->drawImage(image, 0, 0);
1027 canvas->translate(image->width(), 0);
1028 SkPaint paint;
1029 paint.setTextAlign(SkPaint::kCenter_Align);
1030 paint.setVerticalText(true);
1031 canvas->drawLine(10, 0, 10, image->height(), paint);
Cary Clarkac47b882018-01-11 10:35:44 -05001032 canvas->drawString("height", 25, image->height() / 2, paint);
1033##
Cary Clarka560c472017-11-27 10:44:06 -05001034
Cary Clark61ca7c52018-01-02 11:34:14 -05001035#SeeAlso dimensions() width()
Cary Clarka560c472017-11-27 10:44:06 -05001036
1037#Method ##
1038
1039# ------------------------------------------------------------------------------
1040
1041#Method SkISize dimensions() const
1042
Cary Clark2f466242017-12-11 16:03:17 -05001043Returns ISize { width(), height() }.
1044
1045#Return integral size of width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -05001046
1047#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001048#Image 4
1049 SkISize dimensions = image->dimensions();
1050 SkIRect bounds = image->bounds();
1051 SkIRect dimensionsAsBounds = SkIRect::MakeSize(dimensions);
1052 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clarka560c472017-11-27 10:44:06 -05001053##
1054
Cary Clark61ca7c52018-01-02 11:34:14 -05001055#SeeAlso height() width() bounds()
Cary Clarka560c472017-11-27 10:44:06 -05001056
1057#Method ##
1058
1059# ------------------------------------------------------------------------------
1060
1061#Method SkIRect bounds() const
1062
Cary Clark2f466242017-12-11 16:03:17 -05001063Returns IRect { 0, 0, width(), height() }.
1064
1065#Return integral rectangle from origin to width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -05001066
1067#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001068#Height 128
1069#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -05001070 SkIRect bounds = image->bounds();
Cary Clarkac47b882018-01-11 10:35:44 -05001071 for (int x : { 0, bounds.width() } ) {
1072 for (int y : { 0, bounds.height() } ) {
Cary Clark61ca7c52018-01-02 11:34:14 -05001073 canvas->drawImage(image, x, y);
1074 }
1075 }
Cary Clarka560c472017-11-27 10:44:06 -05001076##
1077
Cary Clark61ca7c52018-01-02 11:34:14 -05001078#SeeAlso dimensions()
Cary Clarka560c472017-11-27 10:44:06 -05001079
1080#Method ##
1081
1082# ------------------------------------------------------------------------------
1083
1084#Method uint32_t uniqueID() const
1085
Cary Clark61ca7c52018-01-02 11:34:14 -05001086Returns value unique to image. Image contents cannot change after Image is
1087created. Any operation to create a new Image will receive generate a new
1088unique number.
1089
1090#Return unique identifier ##
Cary Clarka560c472017-11-27 10:44:06 -05001091
1092#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001093#Image 5
1094#Height 156
1095 sk_sp<SkImage> subset = image->makeSubset({10, 20, 90, 100});
1096 canvas->drawImage(image, 0, 0);
1097 canvas->drawImage(subset, 128, 0);
1098 SkPaint paint;
1099 SkString s;
1100 s.printf("original id: %d", image->uniqueID());
1101 canvas->drawString(s, 20, image->height() + 20, paint);
1102 s.printf("subset id: %d", subset->uniqueID());
1103 canvas->drawString(s, 148, subset->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001104##
1105
Cary Clark61ca7c52018-01-02 11:34:14 -05001106#SeeAlso isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001107
1108#Method ##
1109
1110# ------------------------------------------------------------------------------
1111
1112#Method SkAlphaType alphaType() const
1113
Cary Clark61ca7c52018-01-02 11:34:14 -05001114Returns Alpha_Type, one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
1115kPremul_SkAlphaType, kUnpremul_SkAlphaType.
1116
1117Alpha_Type returned was a parameter to an Image constructor,
1118or was parsed from encoded data.
1119
1120#Return Alpha_Type in Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001121
1122#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001123#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -05001124#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -05001125 const char* alphaTypeStr[] = { "Unknown", "Opaque", "Premul", "Unpremul" };
1126 SkAlphaType alphaType = image->alphaType();
Cary Clarkac47b882018-01-11 10:35:44 -05001127 canvas->drawImage(image, 16, 0);
Cary Clark61ca7c52018-01-02 11:34:14 -05001128 SkPaint paint;
1129 canvas->drawString(alphaTypeStr[(int) alphaType], 20, image->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001130##
1131
Cary Clark61ca7c52018-01-02 11:34:14 -05001132#SeeAlso SkImageInfo::alphaType
Cary Clarka560c472017-11-27 10:44:06 -05001133
1134#Method ##
1135
1136# ------------------------------------------------------------------------------
1137
1138#Method SkColorSpace* colorSpace() const
1139
Cary Clark2f466242017-12-11 16:03:17 -05001140Returns Color_Space, the range of colors, associated with Image. The
1141reference count of Color_Space is unchanged. The returned Color_Space is
1142immutable.
Cary Clarka560c472017-11-27 10:44:06 -05001143
Cary Clark61dfc3a2018-01-03 08:37:53 -05001144Color_Space returned was passed to an Image constructor,
1145or was parsed from encoded data. Color_Space returned may be ignored when Image
1146is drawn, depending on the capabilities of the Surface receiving the drawing.
Cary Clark2f466242017-12-11 16:03:17 -05001147
1148#Return Color_Space in Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001149
1150#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001151#Image 3
1152#Set sRGB
1153 SkPixmap pixmap;
1154 source.peekPixels(&pixmap);
1155 canvas->scale(.25f, .25f);
1156 int y = 0;
1157 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1158 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1159 int x = 0;
1160 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1161 for (int index = 0; index < 2; ++index) {
1162 pixmap.setColorSpace(colorSpace);
1163 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1164 canvas->drawImage(image, x, y);
1165 colorSpace = image->colorSpace()->makeColorSpin();
1166 x += 512;
1167 }
1168 y += 512;
1169 }
Cary Clarka560c472017-11-27 10:44:06 -05001170##
1171
Cary Clark61dfc3a2018-01-03 08:37:53 -05001172#SeeAlso refColorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001173
1174#Method ##
1175
1176# ------------------------------------------------------------------------------
1177
1178#Method sk_sp<SkColorSpace> refColorSpace() const
1179
Cary Clark61dfc3a2018-01-03 08:37:53 -05001180Returns a smart pointer to Color_Space, the range of colors, associated with
1181Image. The smart pointer tracks the number of objects sharing this
1182SkColorSpace reference so the memory is released when the owners destruct.
1183
1184The returned SkColorSpace is immutable.
1185
1186Color_Space returned was passed to an Image constructor,
1187or was parsed from encoded data. Color_Space returned may be ignored when Image
1188is drawn, depending on the capabilities of the Surface receiving the drawing.
1189
1190#Return Color_Space in Image, or nullptr, wrapped in a smart pointer ##
Cary Clarka560c472017-11-27 10:44:06 -05001191
1192#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001193#Image 3
1194#Set sRGB
1195 SkPixmap pixmap;
1196 source.peekPixels(&pixmap);
1197 canvas->scale(.25f, .25f);
1198 int y = 0;
1199 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1200 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1201 int x = 0;
1202 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1203 for (int index = 0; index < 2; ++index) {
1204 pixmap.setColorSpace(colorSpace);
1205 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1206 canvas->drawImage(image, x, y);
1207 colorSpace = image->refColorSpace()->makeColorSpin();
1208 x += 512;
1209 }
1210 y += 512;
1211 }
Cary Clarka560c472017-11-27 10:44:06 -05001212##
1213
Cary Clark61dfc3a2018-01-03 08:37:53 -05001214#SeeAlso colorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001215
1216#Method ##
1217
1218# ------------------------------------------------------------------------------
1219
1220#Method bool isAlphaOnly() const
1221
Cary Clark2f466242017-12-11 16:03:17 -05001222Returns true if Image pixels represent transparency only. If true, each pixel
1223is packed in 8 bits as defined by kAlpha_8_SkColorType.
Cary Clarka560c472017-11-27 10:44:06 -05001224
Cary Clark2f466242017-12-11 16:03:17 -05001225#Return true if pixels represent a transparency mask ##
Cary Clarka560c472017-11-27 10:44:06 -05001226
1227#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001228 uint8_t pmColors = 0;
1229 sk_sp<SkImage> image = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1), &pmColors, 1});
1230 SkDebugf("alphaOnly = %s\n", image->isAlphaOnly() ? "true" : "false");
1231#StdOut
1232alphaOnly = true
1233##
Cary Clarka560c472017-11-27 10:44:06 -05001234##
1235
Cary Clark61dfc3a2018-01-03 08:37:53 -05001236#SeeAlso alphaType isOpaque
Cary Clarka560c472017-11-27 10:44:06 -05001237
1238#Method ##
1239
1240# ------------------------------------------------------------------------------
1241
1242#Method bool isOpaque() const
1243
Cary Clark61dfc3a2018-01-03 08:37:53 -05001244Returns true if pixels ignore their Alpha value and are treated as fully opaque.
Cary Clark2f466242017-12-11 16:03:17 -05001245
1246#Return true if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clarka560c472017-11-27 10:44:06 -05001247
1248#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001249 auto check_isopaque = [](const SkImageInfo& imageInfo) -> void {
1250 auto surface(SkSurface::MakeRaster(imageInfo));
1251 auto image(surface->makeImageSnapshot());
1252 SkDebugf("isOpaque = %s\n", image->isOpaque() ? "true" : "false");
1253 };
1254
1255 check_isopaque(SkImageInfo::MakeN32Premul(5, 5));
1256 check_isopaque(SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType));
1257#StdOut
1258isOpaque = false
1259isOpaque = true
1260##
Cary Clarka560c472017-11-27 10:44:06 -05001261##
1262
Cary Clark61dfc3a2018-01-03 08:37:53 -05001263#SeeAlso alphaType isAlphaOnly
Cary Clarka560c472017-11-27 10:44:06 -05001264
1265#Method ##
1266
1267# ------------------------------------------------------------------------------
1268
1269#Method sk_sp<SkShader> makeShader(SkShader::TileMode tileMode1, SkShader::TileMode tileMode2,
1270 const SkMatrix* localMatrix = nullptr) const
1271
Cary Clark61dfc3a2018-01-03 08:37:53 -05001272Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1273SkShader::TileMode rules to fill drawn area outside Image. localMatrix permits
1274transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001275
Cary Clark61dfc3a2018-01-03 08:37:53 -05001276#Param tileMode1 tiling in x, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1277 SkShader::kMirror_TileMode
1278##
1279#Param tileMode2 tiling in y, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1280 SkShader::kMirror_TileMode
1281##
1282#Param localMatrix Image transformation, or nullptr ##
1283
1284#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001285
1286#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001287#Image 4
1288SkMatrix matrix;
1289matrix.setRotate(45);
1290SkPaint paint;
1291paint.setShader(image->makeShader(SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode,
1292 &matrix));
1293canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001294##
1295
Cary Clark61dfc3a2018-01-03 08:37:53 -05001296#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001297
1298#Method ##
1299
1300# ------------------------------------------------------------------------------
1301
1302#Method sk_sp<SkShader> makeShader(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::kClamp_TileMode 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 localMatrix Image transformation, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001309
Cary Clark61dfc3a2018-01-03 08:37:53 -05001310#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001311
1312#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001313#Image 5
1314SkMatrix matrix;
1315matrix.setRotate(45);
1316matrix.postTranslate(125, 30);
1317SkPaint paint;
1318paint.setShader(image->makeShader(&matrix));
1319canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001320##
1321
Cary Clarkf5404bb2018-01-05 12:10:09 -05001322#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001323
1324#Method ##
1325
1326# ------------------------------------------------------------------------------
1327
1328#Method bool peekPixels(SkPixmap* pixmap) const
1329
Cary Clarkf5404bb2018-01-05 12:10:09 -05001330Copies Image pixel address, row bytes, and Image_Info to pixmap, if address
1331is available, and returns true. If pixel address is not available, return
1332false and leave pixmap unchanged.
Cary Clarka560c472017-11-27 10:44:06 -05001333
Cary Clarkf5404bb2018-01-05 12:10:09 -05001334#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
Cary Clarka560c472017-11-27 10:44:06 -05001335
Cary Clarkf5404bb2018-01-05 12:10:09 -05001336#Return true if Image has direct access to pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001337
1338#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001339 SkBitmap bitmap;
1340 bitmap.allocPixels(SkImageInfo::MakeN32Premul(12, 11));
1341 SkCanvas offscreen(bitmap);
1342 offscreen.clear(SK_ColorWHITE);
1343 SkPaint paint;
1344 offscreen.drawString("%", 1, 10, paint);
1345 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
1346 SkPixmap pixmap;
1347 if (image->peekPixels(&pixmap)) {
1348 const SkPMColor* pixels = pixmap.addr32();
1349 SkPMColor pmWhite = pixels[0];
1350 for (int y = 0; y < image->height(); ++y) {
1351 for (int x = 0; x < image->width(); ++x) {
1352 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
1353 }
1354 SkDebugf("\n");
1355 }
1356 }
1357#StdOut
1358------------
1359--xx----x---
1360-x--x--x----
1361-x--x--x----
1362-x--x-x-----
1363--xx-xx-xx--
1364-----x-x--x-
1365----x--x--x-
1366----x--x--x-
1367---x----xx--
1368------------
1369##
Cary Clarka560c472017-11-27 10:44:06 -05001370##
1371
Cary Clarkf5404bb2018-01-05 12:10:09 -05001372#SeeAlso readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001373
1374#Method ##
1375
1376# ------------------------------------------------------------------------------
1377
1378#Method GrTexture* getTexture() const
1379
Cary Clark2f466242017-12-11 16:03:17 -05001380Deprecated.
1381
1382#Deprecated
1383##
1384
Cary Clarka560c472017-11-27 10:44:06 -05001385#Private
Cary Clarkf5404bb2018-01-05 12:10:09 -05001386Currently used by Canvas2DLayerBridge in Chromium.
Cary Clarka560c472017-11-27 10:44:06 -05001387##
1388
Cary Clarka560c472017-11-27 10:44:06 -05001389#Method ##
1390
1391# ------------------------------------------------------------------------------
1392
1393#Method bool isTextureBacked() const
1394
Cary Clarkf5404bb2018-01-05 12:10:09 -05001395Returns true the contents of Image was created on or uploaded to GPU memory,
1396and is available as a GPU_Texture.
Cary Clarka560c472017-11-27 10:44:06 -05001397
Cary Clarkf5404bb2018-01-05 12:10:09 -05001398#Return true if Image is a GPU_Texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001399
1400#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001401#Image 5
1402#Platform gpu
1403auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1404 if (nullptr == image) {
1405 return;
1406 }
1407 SkPaint paint;
1408 paint.setAntiAlias(true);
1409 paint.setTextAlign(SkPaint::kCenter_Align);
1410 canvas->drawImage(image, 0, 0);
1411 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1412 canvas->drawString(image->isTextureBacked() ? "is GPU texture" : "not GPU texture",
1413 image->width() / 2, image->height() * 3 / 4, paint);
1414};
1415sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1416sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1417 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1418drawImage(image, "image");
1419canvas->translate(image->width(), 0);
1420drawImage(bitmapImage, "source");
1421canvas->translate(-image->width(), image->height());
1422drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001423##
1424
Cary Clarkf5404bb2018-01-05 12:10:09 -05001425#SeeAlso MakeFromTexture isValid
Cary Clarka560c472017-11-27 10:44:06 -05001426
1427#Method ##
1428
1429# ------------------------------------------------------------------------------
1430
1431#Method bool isValid(GrContext* context) const
1432
Cary Clarkf5404bb2018-01-05 12:10:09 -05001433Returns true if Image can be drawn on either Raster_Surface or GPU_Surface.
1434If context is nullptr, tests if Image draws on Raster_Surface;
1435otherwise, tests if Image draws on GPU_Surface associated with context.
Cary Clarka560c472017-11-27 10:44:06 -05001436
Cary Clarkf5404bb2018-01-05 12:10:09 -05001437Image backed by GPU_Texture may become invalid if associated GrContext is
1438invalid. Lazy_Image may be invalid and may not draw to Raster_Surface or
1439GPU_Surface or both.
Cary Clarka560c472017-11-27 10:44:06 -05001440
Cary Clark61ca7c52018-01-02 11:34:14 -05001441#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05001442
Cary Clarkf5404bb2018-01-05 12:10:09 -05001443#Return true if Image can be drawn ##
Cary Clarka560c472017-11-27 10:44:06 -05001444
1445#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001446#Image 5
1447#Platform gpu
1448auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1449 if (nullptr == image) {
1450 return;
1451 }
1452 SkPaint paint;
1453 paint.setAntiAlias(true);
1454 paint.setTextAlign(SkPaint::kCenter_Align);
1455 canvas->drawImage(image, 0, 0);
1456 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1457 if (canvas->getGrContext()) {
1458 canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" :
1459 "not valid on GPU", image->width() / 2, image->height() * 5 / 8, paint);
1460 }
1461 canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" :
1462 "not valid on CPU", image->width() / 2, image->height() * 7 / 8, paint);
1463};
1464sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1465sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1466 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1467drawImage(image, "image");
1468canvas->translate(image->width(), 0);
1469drawImage(bitmapImage, "source");
1470canvas->translate(-image->width(), image->height());
1471drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001472##
1473
Cary Clarkf5404bb2018-01-05 12:10:09 -05001474#SeeAlso isTextureBacked isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001475
1476#Method ##
1477
1478# ------------------------------------------------------------------------------
1479
1480#Method GrBackendObject getTextureHandle(bool flushPendingGrContextIO,
1481 GrSurfaceOrigin* origin = nullptr) const
1482
Cary Clark2f466242017-12-11 16:03:17 -05001483Retrieves the back-end API handle of texture. If flushPendingGrContextIO is true,
1484complete deferred I/O operations.
Cary Clarka560c472017-11-27 10:44:06 -05001485
Cary Clark2f466242017-12-11 16:03:17 -05001486If origin in not nullptr, copies location of content drawn into Image.
Cary Clarka560c472017-11-27 10:44:06 -05001487
Cary Clark2f466242017-12-11 16:03:17 -05001488#Param flushPendingGrContextIO flag to flush outstanding requests ##
1489#Param origin storage for one of: kTopLeft_GrSurfaceOrigin,
1490 kBottomLeft_GrSurfaceOrigin; or nullptr
1491##
1492
Cary Clarkac47b882018-01-11 10:35:44 -05001493#Return back-end API texture handle, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001494
1495#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001496#Image 4
Cary Clark2f466242017-12-11 16:03:17 -05001497#Platform gpu
1498GrContext* context = canvas->getGrContext();
1499if (!context) {
1500 return;
1501}
1502SkPaint paint;
1503paint.setAntiAlias(true);
1504SkString str;
Cary Clarkac47b882018-01-11 10:35:44 -05001505int y = -10;
Cary Clark2f466242017-12-11 16:03:17 -05001506for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin } ) {
1507 sk_sp<SkImage> srcImage(SkImage::MakeFromTexture(context,
1508 backEndTexture, origin, kPremul_SkAlphaType, nullptr));
1509 GrSurfaceOrigin readBackOrigin;
1510 GrBackendObject readBackHandle = srcImage->getTextureHandle(false, &readBackOrigin);
Cary Clarkac47b882018-01-11 10:35:44 -05001511 str.printf("readBackHandle: 0x%x", readBackHandle);
1512 canvas->drawString(str, 5, y += 30, paint);
1513 canvas->drawImage(srcImage, 80, y += 10);
Cary Clark2f466242017-12-11 16:03:17 -05001514 str.printf("origin: k%s_GrSurfaceOrigin", readBackOrigin ? "BottomLeft" : "TopLeft");
Cary Clarkac47b882018-01-11 10:35:44 -05001515 canvas->drawString(str, 5, y += srcImage->height() + 10, paint);
Cary Clark2f466242017-12-11 16:03:17 -05001516}
Cary Clarka560c472017-11-27 10:44:06 -05001517##
1518
Cary Clarkac47b882018-01-11 10:35:44 -05001519#Example
1520#Image 5
1521#Platform gpu
1522 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1523 if (nullptr == image) {
1524 return;
1525 }
1526 SkPaint paint;
1527 paint.setAntiAlias(true);
1528 paint.setTextAlign(SkPaint::kCenter_Align);
1529 canvas->drawImage(image, 0, image->height() / 4);
1530 canvas->drawString(label, image->width() / 2, image->height() / 8, paint);
1531 GrSurfaceOrigin readBackOrigin;
1532 GrBackendObject readBackHandle = image->getTextureHandle(false, &readBackOrigin);
1533 canvas->drawString(readBackHandle ? "has readBackHandle" : "no readBackHandle",
1534 image->width() / 2, image->height() * 11 / 8, paint);
1535 };
1536 drawImage(image, "image");
1537 canvas->translate(image->width(), 0);
1538 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1539 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1540 drawImage(textureImage, "backEndTexture");
1541##
1542
1543#SeeAlso MakeFromTexture isTextureBacked
Cary Clarka560c472017-11-27 10:44:06 -05001544
1545#Method ##
1546
1547# ------------------------------------------------------------------------------
1548
1549#Enum CachingHint
1550
1551#Code
1552 enum CachingHint {
1553 kAllow_CachingHint,
1554 kDisallow_CachingHint,
1555 };
1556##
1557
Cary Clarkac47b882018-01-11 10:35:44 -05001558CachingHint selects whether Skia may internally cache Bitmaps generated by
1559decoding Image, or by copying Image from GPU to CPU. The default behavior
1560allows caching Bitmaps.
1561
1562Choose kDisallow_CachingHint if Image pixels are to be used only once, or
1563if Image pixels reside in a cache outside of Skia, or to reduce memory pressure.
1564
1565Choosing kAllow_CachingHint does not ensure that pixels will be cached.
1566Image pixels may not be cached if memory requirements are too large or
1567pixels are not accessible.
Cary Clarka560c472017-11-27 10:44:06 -05001568
1569#Const kAllow_CachingHint 0
Cary Clarkac47b882018-01-11 10:35:44 -05001570Allows Skia to internally cache decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001571##
1572#Const kDisallow_CachingHint 1
Cary Clarkac47b882018-01-11 10:35:44 -05001573Disallows Skia from internally caching decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001574##
1575
Cary Clarkac47b882018-01-11 10:35:44 -05001576#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001577##
1578
Cary Clarkac47b882018-01-11 10:35:44 -05001579#SeeAlso readPixels scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001580
1581#Enum ##
1582
1583# ------------------------------------------------------------------------------
1584
1585#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1586 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const
1587
Cary Clarkac47b882018-01-11 10:35:44 -05001588Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY),
1589and does not exceed Image (width(), height()).
1590
1591dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
1592destination. dstRowBytes specifics the gap from one destination row to the next.
1593Returns true if pixels are copied. Returns false if:
1594#List
1595# dstInfo.addr() equals nullptr ##
1596# dstRowBytes is less than dstInfo.minRowBytes ##
1597# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001598##
1599
Cary Clarkac47b882018-01-11 10:35:44 -05001600Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1601kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1602If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace must match.
1603If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType must
1604match. If Image Color_Space is nullptr, dstInfo.colorSpace must match. Returns
1605false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001606
Cary Clarkac47b882018-01-11 10:35:44 -05001607srcX and srcY may be negative to copy only top or left of source. Returns
1608false if width() or height() is zero or negative.
1609Returns false if
1610#Formula
1611abs(srcX) >= Image width()
1612##
1613, or if
1614#Formula
1615abs(srcY) >= Image height()
1616##
1617.
Cary Clarka560c472017-11-27 10:44:06 -05001618
Cary Clarkac47b882018-01-11 10:35:44 -05001619If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1620If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1621
1622#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1623#Param dstPixels destination pixel storage ##
1624#Param dstRowBytes destination row length ##
1625#Param srcX column index whose absolute value is less than width() ##
1626#Param srcY row index whose absolute value is less than height() ##
1627#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1628
1629#Return true if pixels are copied to dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001630
1631#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001632#Image 3
1633 canvas->scale(.5f, .5f);
1634 const int width = 32;
1635 const int height = 32;
1636 std::vector<int32_t> dstPixels;
1637 dstPixels.resize(height * width * 4);
1638 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1639 for (int y = 0; y < 512; y += height ) {
1640 for (int x = 0; x < 512; x += width ) {
1641 if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) {
1642 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
1643 SkBitmap bitmap;
1644 bitmap.installPixels(dstPixmap);
1645 canvas->drawBitmap(bitmap, 0, 0);
1646 }
1647 canvas->translate(48, 0);
1648 }
1649 canvas->translate(-16 * 48, 48);
1650 }
Cary Clarka560c472017-11-27 10:44:06 -05001651##
1652
Cary Clarkac47b882018-01-11 10:35:44 -05001653#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001654
1655#Method ##
1656
1657# ------------------------------------------------------------------------------
1658
1659#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY,
1660 CachingHint cachingHint = kAllow_CachingHint) const
1661
Cary Clarkac47b882018-01-11 10:35:44 -05001662Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and
1663does not exceed Image (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001664
Cary Clarkac47b882018-01-11 10:35:44 -05001665dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
1666and row bytes of destination. dst.rowBytes specifics the gap from one destination
1667row to the next. Returns true if pixels are copied. Returns false if:
1668#List
1669# dst pixel storage equals nullptr ##
1670# dst.rowBytes is less than SkImageInfo::minRowBytes ##
1671# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001672##
1673
Cary Clarkac47b882018-01-11 10:35:44 -05001674Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1675kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1676If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1677If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1678match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1679false if pixel conversion is not possible.
1680
1681srcX and srcY may be negative to copy only top or left of source. Returns
1682false if width() or height() is zero or negative.
1683Returns false if
1684#Formula
1685abs(srcX) >= Image width()
1686##
1687, or if
1688#Formula
1689abs(srcY) >= Image height()
1690##
1691.
1692
1693If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1694If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1695
1696#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1697#Param srcX column index whose absolute value is less than width() ##
1698#Param srcY row index whose absolute value is less than height() ##
1699#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1700
1701#Return true if pixels are copied to dst ##
1702
1703#Example
1704#Image 3
1705 std::vector<int32_t> srcPixels;
1706 int rowBytes = image->width() * 4;
1707 int quarterWidth = image->width() / 4;
1708 int quarterHeight = image->height() / 4;
1709 srcPixels.resize(image->height() * rowBytes);
1710 for (int y = 0; y < 4; ++y) {
1711 for (int x = 0; x < 4; ++x) {
1712 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1713 &srcPixels.front() + x * image->height() * quarterWidth +
1714 y * quarterWidth, rowBytes);
1715 image->readPixels(pixmap, x * quarterWidth, y * quarterHeight);
1716 }
1717 }
1718 canvas->scale(.5f, .5f);
1719 SkBitmap bitmap;
1720 bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()),
1721 &srcPixels.front(), rowBytes);
1722 canvas->drawBitmap(bitmap, 0, 0);
1723##
1724
1725#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001726
1727#Method ##
1728
1729# ------------------------------------------------------------------------------
1730
1731#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality,
1732 CachingHint cachingHint = kAllow_CachingHint) const
1733
Cary Clarkac47b882018-01-11 10:35:44 -05001734Copies Image to dst, scaling pixels to fit dst.width() and dst.height(), and
1735converting pixels to match dst.colorType and dst.alphaType. Returns true if
1736pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1737less than dst SkImageInfo::minRowBytes.
Cary Clarka560c472017-11-27 10:44:06 -05001738
Cary Clarkac47b882018-01-11 10:35:44 -05001739Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1740kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1741If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1742If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1743match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1744false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001745
Cary Clarkac47b882018-01-11 10:35:44 -05001746Scales the image, with filterQuality, to match dst.width() and dst.height().
1747filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1748Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1749Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1750Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1751kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1752
1753If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1754If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1755
1756#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1757#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1758 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1759##
1760#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1761
1762#Return true if pixels are scaled to fit dst ##
Cary Clarka560c472017-11-27 10:44:06 -05001763
1764#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001765#Image 3
1766#Height 128
1767 std::vector<int32_t> srcPixels;
1768 int quarterWidth = image->width() / 16;
1769 int rowBytes = quarterWidth * 4;
1770 int quarterHeight = image->height() / 16;
1771 srcPixels.resize(quarterHeight * rowBytes);
1772 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1773 &srcPixels.front(), rowBytes);
1774 canvas->scale(4, 4);
1775 SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality,
1776 kMedium_SkFilterQuality, kHigh_SkFilterQuality };
1777 for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) {
1778 image->scalePixels(pixmap, qualities[index]);
1779 sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
1780 canvas->drawImage(filtered, 16 * index, 0);
1781 }
Cary Clarka560c472017-11-27 10:44:06 -05001782##
1783
Cary Clarkac47b882018-01-11 10:35:44 -05001784#SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001785
1786#Method ##
1787
1788# ------------------------------------------------------------------------------
1789
1790#Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const
1791
Cary Clarkac47b882018-01-11 10:35:44 -05001792Encodes Image pixels, returning result as SkData.
Cary Clark2f466242017-12-11 16:03:17 -05001793
Cary Clarkac47b882018-01-11 10:35:44 -05001794Returns nullptr if encoding fails, or if encodedImageFormat is not supported.
Cary Clarka560c472017-11-27 10:44:06 -05001795
Cary Clarkac47b882018-01-11 10:35:44 -05001796Image encoding in a format requires both building with one or more of:
1797SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support
1798for the encoded format.
1799
1800If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can
1801additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP,
1802SkEncodedImageFormat::kGIF.
1803
1804quality is a platform and format specific metric trading off size and encoding
1805error. When used, quality equaling 100 encodes with the least error. quality may
1806be ignored by the encoder.
1807
1808#Param encodedImageFormat one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
1809 SkEncodedImageFormat::kWEBP
1810 ##
1811#Param quality encoder specific metric with 100 equaling best ##
Cary Clarka560c472017-11-27 10:44:06 -05001812
Cary Clark2f466242017-12-11 16:03:17 -05001813#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001814
1815#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001816#Image 3
1817 canvas->scale(4, 4);
1818 SkIRect subset = {0, 0, 16, 64};
1819 int x = 0;
1820 for (int quality : { 0, 10, 50, 100 } ) {
1821 sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
1822 sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
1823 canvas->drawImage(filtered, x, 0);
1824 x += 16;
1825 }
Cary Clarka560c472017-11-27 10:44:06 -05001826##
1827
Cary Clarkac47b882018-01-11 10:35:44 -05001828#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001829
1830#Method ##
1831
1832# ------------------------------------------------------------------------------
1833
Cary Clark61ca7c52018-01-02 11:34:14 -05001834#Method sk_sp<SkData> encodeToData() const
Cary Clarka560c472017-11-27 10:44:06 -05001835
Cary Clarkac47b882018-01-11 10:35:44 -05001836Encodes Image pixels, returning result as SkData. Returns existing encoded data
1837if present; otherwise, Image is encoded with SkEncodedImageFormat::kPNG. Skia
1838must be built with SK_HAS_PNG_LIBRARY to encode Image.
Cary Clarka560c472017-11-27 10:44:06 -05001839
Cary Clarkac47b882018-01-11 10:35:44 -05001840Returns nullptr if existing encoded data is missing or invalid, and
Cary Clarka560c472017-11-27 10:44:06 -05001841encoding fails.
1842
Cary Clarkac47b882018-01-11 10:35:44 -05001843#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001844
1845#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001846#Image 3
1847 canvas->scale(4, 4);
1848 SkIRect subset = {136, 32, 200, 96};
1849 sk_sp<SkData> data(image->encodeToData());
1850 sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
1851 canvas->drawImage(eye, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001852##
1853
Cary Clarkac47b882018-01-11 10:35:44 -05001854#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001855
1856#Method ##
1857
1858# ------------------------------------------------------------------------------
1859
1860#Method sk_sp<SkData> refEncodedData() const
1861
Cary Clarkac47b882018-01-11 10:35:44 -05001862Returns encoded Image pixels as SkData, if Image was created from supported
1863encoded stream format. Platform support for formats vary and may require building
1864with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY.
Cary Clarka560c472017-11-27 10:44:06 -05001865
Cary Clarkac47b882018-01-11 10:35:44 -05001866Returns nullptr if Image contents are not encoded.
Cary Clarka560c472017-11-27 10:44:06 -05001867
Cary Clarkac47b882018-01-11 10:35:44 -05001868#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001869
1870#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001871#Image 3
1872#Platform gpu
1873 struct {
1874 const char* name;
1875 sk_sp<SkImage> image;
1876 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1877 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1878 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr) } };
1879 SkString string;
1880 SkPaint paint;
1881 for (const auto& test : tests ) {
1882 if (!test.image) {
1883 string.printf("no %s", test.name);
1884 } else {
1885 string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name);
1886 }
1887 canvas->drawString(string, 10, 20, paint);
1888 canvas->translate(0, 20);
1889 }
Cary Clarka560c472017-11-27 10:44:06 -05001890##
1891
Cary Clarkac47b882018-01-11 10:35:44 -05001892#SeeAlso encodeToData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001893
1894#Method ##
1895
1896# ------------------------------------------------------------------------------
1897
1898#Method const char* toString(SkString* string) const
1899
Cary Clarkac47b882018-01-11 10:35:44 -05001900Appends Image description to string, including unique ID, width, height, and
1901whether the image is opaque.
Cary Clarka560c472017-11-27 10:44:06 -05001902
Cary Clarkac47b882018-01-11 10:35:44 -05001903#Param string storage for description; existing content is preserved ##
1904
1905#Return string appended with Image description ##
Cary Clarka560c472017-11-27 10:44:06 -05001906
1907#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001908#Image 4
1909 struct {
1910 const char* name;
1911 sk_sp<SkImage> image;
1912 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1913 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1914 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr) } };
1915 SkString string;
1916 SkPaint paint;
1917 for (const auto& test : tests ) {
1918 string.printf("%s: ", test.name);
1919 test.image ? (void) test.image->toString(&string) : string.append("no image");
1920 canvas->drawString(string, 10, 20, paint);
1921 canvas->translate(0, 20);
1922 }
Cary Clarka560c472017-11-27 10:44:06 -05001923##
1924
Cary Clarkac47b882018-01-11 10:35:44 -05001925#SeeAlso SkPaint::toString
Cary Clarka560c472017-11-27 10:44:06 -05001926
1927#Method ##
1928
1929# ------------------------------------------------------------------------------
1930
1931#Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const
1932
Cary Clarkac47b882018-01-11 10:35:44 -05001933Returns subset of Image. subset must be fully contained by Image dimensions().
1934The implementation may share pixels, or may copy them.
Cary Clarka560c472017-11-27 10:44:06 -05001935
Cary Clarkac47b882018-01-11 10:35:44 -05001936Returns nullptr if subset is empty, or subset is not contained by bounds, or
1937pixels in Image could not be read or copied.
Cary Clarka560c472017-11-27 10:44:06 -05001938
Cary Clarkac47b882018-01-11 10:35:44 -05001939#Param subset bounds of returned Image ##
1940
1941#Return partial or full Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001942
1943#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001944#Image 3
1945 canvas->scale(.5f, .5f);
1946 const int width = 32;
1947 const int height = 32;
1948 for (int y = 0; y < 512; y += height ) {
1949 for (int x = 0; x < 512; x += width ) {
1950 sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height}));
1951 canvas->drawImage(subset, x * 3 / 2, y * 3 / 2);
1952 }
1953 }
Cary Clarka560c472017-11-27 10:44:06 -05001954##
1955
Cary Clarkac47b882018-01-11 10:35:44 -05001956#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001957
1958#Method ##
1959
1960# ------------------------------------------------------------------------------
1961
1962#Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const
1963
Cary Clarkac47b882018-01-11 10:35:44 -05001964Returns Image backed by GPU_Texture associated with context. Returned Image is
1965compatible with Surface created with dstColorSpace. Returns original
1966Image if context and dstColorSpace match.
1967
1968Returns nullptr if context is nullptr, or if Image was created with another
1969GrContext.
Cary Clarka560c472017-11-27 10:44:06 -05001970
Cary Clark61ca7c52018-01-02 11:34:14 -05001971#Param context GPU_Context ##
Cary Clarkac47b882018-01-11 10:35:44 -05001972#Param dstColorSpace range of colors of matching Surface on GPU ##
Cary Clarka560c472017-11-27 10:44:06 -05001973
Cary Clarkac47b882018-01-11 10:35:44 -05001974#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001975
1976#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001977#Platform gpu
1978#Image 5
1979 auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void {
1980 if (nullptr == image || nullptr == context) {
1981 return;
1982 }
1983 SkPaint paint;
1984 paint.setAntiAlias(true);
1985 paint.setTextAlign(SkPaint::kCenter_Align);
1986 sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr));
1987 canvas->drawImage(texture, 0, 0);
1988 canvas->drawString(label, texture->width() / 2, texture->height() / 4, paint);
1989 };
1990 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1991 GrContext* context = canvas->getGrContext();
1992 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture,
1993 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1994 drawImage(image, context, "image");
1995 canvas->translate(image->width(), 0);
1996 drawImage(bitmapImage, context, "source");
1997 canvas->translate(-image->width(), image->height());
1998 drawImage(textureImage, context, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001999##
2000
Cary Clarkac47b882018-01-11 10:35:44 -05002001#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05002002
2003#Method ##
2004
2005# ------------------------------------------------------------------------------
2006
2007#Method sk_sp<SkImage> makeNonTextureImage() const
2008
Cary Clarkac47b882018-01-11 10:35:44 -05002009Returns Raster_Image or Lazy_Image. Copies Image backed by GPU_Texture into
2010CPU memory if needed. Returns original Image if unencoded in Raster_Bitmap,
2011or if encoded in a stream.
Cary Clark61ca7c52018-01-02 11:34:14 -05002012
Cary Clarkac47b882018-01-11 10:35:44 -05002013Returns nullptr if backed by GPU_Texture and copy fails.
2014
2015#Return Raster_Image, Lazy_Image, or nullptr ##
Cary Clark61ca7c52018-01-02 11:34:14 -05002016
2017#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002018#Image 5
2019#Platform gpu
2020 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2021 if (nullptr == image) {
2022 return;
2023 }
2024 SkPaint paint;
2025 paint.setAntiAlias(true);
2026 paint.setTextAlign(SkPaint::kCenter_Align);
2027 sk_sp<SkImage> nonTexture(image->makeNonTextureImage());
2028 canvas->drawImage(nonTexture, 0, 0);
2029 canvas->drawString(label, nonTexture->width() / 2, nonTexture->height() / 4, paint);
2030 };
2031 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2032 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2033 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2034 drawImage(image, "image");
2035 canvas->translate(image->width(), 0);
2036 drawImage(bitmapImage, "source");
2037 canvas->translate(-image->width(), image->height());
2038 drawImage(textureImage, "backEndTexture");
Cary Clark61ca7c52018-01-02 11:34:14 -05002039##
2040
2041#SeeAlso incomplete
2042
2043#Method ##
2044
2045# ------------------------------------------------------------------------------
2046
2047#Method sk_sp<SkImage> makeRasterImage() const
2048
Cary Clarkac47b882018-01-11 10:35:44 -05002049Returns Raster_Image. Copies Image backed by GPU_Texture into CPU memory,
2050or decodes Image from Lazy_Image. Returns original Image if unencoded in
2051Raster_Bitmap.
Cary Clarka560c472017-11-27 10:44:06 -05002052
Cary Clarkac47b882018-01-11 10:35:44 -05002053Returns nullptr if copy, decode, or pixel read fails.
Cary Clarka560c472017-11-27 10:44:06 -05002054
Cary Clarkac47b882018-01-11 10:35:44 -05002055#Return Raster_Image, or nullptr ##
2056
2057#Bug 7479 ##
Cary Clarka560c472017-11-27 10:44:06 -05002058#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002059#Image 5
2060#Platform gpu
2061 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2062 if (nullptr == image) {
2063 return;
2064 }
2065 SkPaint paint;
2066 paint.setAntiAlias(true);
2067 paint.setTextAlign(SkPaint::kCenter_Align);
2068 sk_sp<SkImage> raster(image->makeRasterImage());
2069 canvas->drawImage(raster, 0, 0);
2070 canvas->drawString(label, raster->width() / 2, raster->height() / 4, paint);
2071 };
2072 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2073 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2074 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2075 drawImage(image, "image");
2076 canvas->translate(image->width(), 0);
2077 drawImage(bitmapImage, "source");
2078 canvas->translate(-image->width(), image->height());
2079 drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05002080##
2081
Cary Clarkac47b882018-01-11 10:35:44 -05002082#SeeAlso isTextureBacked isLazyGenerated MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -05002083
2084#Method ##
2085
2086# ------------------------------------------------------------------------------
2087
2088#Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
2089 const SkIRect& clipBounds, SkIRect* outSubset,
2090 SkIPoint* offset) const
2091
Cary Clarkac47b882018-01-11 10:35:44 -05002092Creates filtered Image. filter processes original Image, potentially changing
2093color, position, and size. subset is the bounds of original Image processed
2094by filter. clipBounds is the expected bounds of the filtered Image. outSubset
2095is required storage for the actual bounds of the filtered Image. offset is
2096required storage for translation of returned Image.
Cary Clarka560c472017-11-27 10:44:06 -05002097
Cary Clarkac47b882018-01-11 10:35:44 -05002098Returns nullptr if Image could not be created. If nullptr is returned, outSubset
2099and offset are undefined.
2100
2101makeWithFilter is optimized to support Image backed by GPU_Texture drawn in an
2102animation with SkImageFilter that vary in size from one frame to the next. The
2103created Image is drawn at an increased size so that GPU_Texture can be reused
2104with different sized effects. outSubset describes the valid bounds of GPU_Texture
2105returned. The returned Image may be much larger than required for the filter.
2106offset translates the returned Image to keep subsequent animation frames
2107aligned with respect to each other.
2108
2109#Param filter how Image is sampled when transformed ##
Cary Clarka560c472017-11-27 10:44:06 -05002110#Param subset incomplete ##
2111#Param clipBounds incomplete ##
2112#Param outSubset incomplete ##
2113#Param offset incomplete ##
2114
Cary Clarkac47b882018-01-11 10:35:44 -05002115#Return filtered Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05002116
2117#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002118#Description
2119In each frame of the animation, filtered Image is drawn in a different location.
2120By translating canvas by returned offset, Image appears stationary.
2121##
2122#Image 5
2123#Platform gpu
2124#Duration 5
2125 sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make(
2126 -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE,
2127 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
2128 nullptr);
2129 sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr);
2130 SkIRect subset = image->bounds();
2131 SkIRect clipBounds = image->bounds();
2132 clipBounds.outset(60, 60);
2133 SkIRect outSubset;
2134 SkIPoint offset;
2135 sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds,
2136 &outSubset, &offset));
2137 SkPaint paint;
2138 paint.setAntiAlias(true);
2139 paint.setStyle(SkPaint::kStroke_Style);
2140 canvas->drawLine(0, 0, offset.fX, offset.fY, paint);
2141 canvas->translate(offset.fX, offset.fY);
2142 canvas->drawImage(filtered, 0, 0);
2143 canvas->drawRect(SkRect::MakeFromIRect(outSubset), paint);
Cary Clarka560c472017-11-27 10:44:06 -05002144##
2145
Cary Clarkac47b882018-01-11 10:35:44 -05002146#SeeAlso SkPaint::setImageFilter
Cary Clarka560c472017-11-27 10:44:06 -05002147
2148#Method ##
2149
2150# ------------------------------------------------------------------------------
2151
Cary Clark61ca7c52018-01-02 11:34:14 -05002152#Struct DeferredTextureImageUsageParams
2153
Cary Clarkac47b882018-01-11 10:35:44 -05002154Used only by Chrome; to be deprecated.
2155
Cary Clark61ca7c52018-01-02 11:34:14 -05002156#Code
2157 struct DeferredTextureImageUsageParams {
2158 DeferredTextureImageUsageParams(const SkMatrix matrix, const SkFilterQuality quality,
2159 int preScaleMipLevel);
2160 SkMatrix fMatrix;
2161 SkFilterQuality fQuality;
2162 int fPreScaleMipLevel;
2163 };
2164##
2165
2166#Member SkMatrix fMatrix
2167##
2168
2169#Member SkFilterQuality fQuality
2170##
2171
2172#Member int fPreScaleMipLevel
2173##
2174
2175#Method DeferredTextureImageUsageParams(const SkMatrix matrix, const SkFilterQuality quality,
2176 int preScaleMipLevel)
2177
2178#Param matrix incomplete ##
2179#Param quality incomplete ##
2180#Param preScaleMipLevel incomplete ##
2181
2182#Return incomplete ##
2183
2184#Example
2185// incomplete
2186##
2187
2188#SeeAlso incomplete
2189
2190##
2191
2192#Example
2193// incomplete
2194##
2195
2196#SeeAlso incomplete
2197
2198##
2199
2200#Method size_t getDeferredTextureImageData(const GrContextThreadSafeProxy& contextThreadSafeProxy,
2201 const DeferredTextureImageUsageParams deferredTextureImageUsageParams[],
2202 int paramCnt,
2203 void* buffer,
2204 SkColorSpace* dstColorSpace = nullptr,
2205 SkColorType dstColorType = kN32_SkColorType) const
2206
Cary Clarkac47b882018-01-11 10:35:44 -05002207Used only by Chrome; to be deprecated.
2208
Cary Clark61ca7c52018-01-02 11:34:14 -05002209This method allows clients to capture the data necessary to turn a SkImage into a texture-
2210backed image. If the original image is codec-backed this will decode into a format optimized
2211for the context represented by the proxy. This method is thread safe with respect to the
2212GrContext whence the proxy came. Clients allocate and manage the storage of the deferred
2213texture data and control its lifetime. No cleanup is required, thus it is safe to simply free
2214the memory out from under the data.
2215
2216The same method is used both for getting the size necessary for pre-uploaded texture data
2217and for retrieving the data. The params array represents the set of draws over which to
2218optimize the pre-upload data.
2219
2220When called with a null buffer this returns the size that the client must allocate in order
2221to create deferred texture data for this image (or zero if this is an inappropriate
2222candidate). The buffer allocated by the client should be 8 byte aligned.
2223
2224When buffer is not null this fills in the deferred texture data for this image in the
2225provided buffer (assuming this is an appropriate candidate image and the buffer is
2226appropriately aligned). Upon success the size written is returned, otherwise 0.
2227
Cary Clarkac47b882018-01-11 10:35:44 -05002228dstColorSpace is the Color_Space of the surface where this texture will ultimately be used.
Cary Clark61ca7c52018-01-02 11:34:14 -05002229If the method determines that mip-maps are needed, this helps determine the correct strategy
2230for building them (gamma-correct or not).
2231
2232dstColorType is the color type of the surface where this texture will ultimately be used.
2233This determines the format with which the image will be uploaded to the GPU. If dstColorType
Cary Clarkac47b882018-01-11 10:35:44 -05002234does not support Color_Space (low bit depth types such as kARGB_4444_SkColorType),
Cary Clark61ca7c52018-01-02 11:34:14 -05002235then dstColorSpace must be null.
2236
Cary Clarkac47b882018-01-11 10:35:44 -05002237#Param contextThreadSafeProxy thread safe GPU context ##
2238#Param deferredTextureImageUsageParams array of Image transformations ##
2239#Param paramCnt entries in deferredTextureImageUsageParams array ##
2240#Param buffer storage for GPU_Texture data, or nullptr ##
2241#Param dstColorSpace Surface Color_Space, or nullptr ##
2242#Param dstColorType Surface Color_Type ##
Cary Clark61ca7c52018-01-02 11:34:14 -05002243
Cary Clarkac47b882018-01-11 10:35:44 -05002244#Return size of storage for GPU_Texture data ##
Cary Clark61ca7c52018-01-02 11:34:14 -05002245
2246#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002247#Image 5
2248#Platform gpu
2249 GrContext* context = canvas->getGrContext();
2250 if (!context) {
2251 return;
2252 }
2253 sk_sp<GrContextThreadSafeProxy> proxy(context->threadSafeProxy());
2254 auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(2, 2),
2255 kNone_SkFilterQuality, 0);
2256 SkColorSpace* colorSpace = canvas->imageInfo().colorSpace();
2257 size_t requiredMemoryInBytes = image->getDeferredTextureImageData(
2258 *proxy, &params, 1, nullptr, colorSpace);
2259 std::vector<uint8_t> memory;
2260 memory.resize(requiredMemoryInBytes);
2261 image->getDeferredTextureImageData(*proxy, &params, 1, memory.data(), colorSpace);
2262 sk_sp<SkImage> uploadedEncodedImage = SkImage::MakeFromDeferredTextureImageData(
2263 context, memory.data(), SkBudgeted::kNo);
2264 canvas->scale(2, 2);
2265 canvas->drawImage(uploadedEncodedImage, 10, 10);
Cary Clark61ca7c52018-01-02 11:34:14 -05002266##
2267
Cary Clarkac47b882018-01-11 10:35:44 -05002268#SeeAlso MakeFromDeferredTextureImageData
Cary Clark61ca7c52018-01-02 11:34:14 -05002269
2270##
2271
2272#Method static sk_sp<SkImage> MakeFromDeferredTextureImageData(GrContext* context, const void* data,
2273 SkBudgeted budgeted)
2274
Cary Clarkac47b882018-01-11 10:35:44 -05002275Used only by Chrome; to be deprecated.
2276
Cary Clark61ca7c52018-01-02 11:34:14 -05002277Returns a texture-backed image from data produced in SkImage::getDeferredTextureImageData.
2278The context must be the context that provided the proxy passed to
2279getDeferredTextureImageData.
2280
2281#Param context GPU_Context ##
2282#Param data incomplete ##
2283#Param budgeted incomplete ##
2284
2285#Return incomplete ##
2286
2287#Example
2288// incomplete
2289##
2290
2291#SeeAlso incomplete
2292
2293##
2294
2295# ------------------------------------------------------------------------------
2296
Cary Clarka560c472017-11-27 10:44:06 -05002297#Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc
2298
2299##
2300
2301# ------------------------------------------------------------------------------
2302
2303#Method static bool MakeBackendTextureFromSkImage(GrContext* context,
2304 sk_sp<SkImage> image,
2305 GrBackendTexture* backendTexture,
2306 BackendTextureReleaseProc* backendTextureReleaseProc)
2307
2308Creates a GrBackendTexture from the provided SkImage. Returns true on success. The
2309GrBackendTexture and BackendTextureReleaseProc are populated on success. It is the callers
2310responsibility to call the BackendTextureReleaseProc once they have deleted the texture.
2311Note that the BackendTextureReleaseProc allows Skia to clean up auxiliary data related
2312to the GrBackendTexture, and is not a substitute for the client deleting the GrBackendTexture
2313themselves.
2314
2315If image is both texture backed and singly referenced; that is, its only
2316reference was transferred using std::move(): image is returned in backendTexture
2317without conversion or making a copy.
2318
2319If the SkImage is not texture backed, this function will generate a texture with the image's
2320contents and return that.
2321
Cary Clark61ca7c52018-01-02 11:34:14 -05002322#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05002323#Param image incomplete ##
2324#Param backendTexture incomplete ##
2325#Param backendTextureReleaseProc incomplete ##
2326
2327#Return incomplete ##
2328
2329#Example
2330// incomplete
2331##
2332
2333#SeeAlso incomplete
2334
2335#Method ##
2336
2337# ------------------------------------------------------------------------------
2338
2339#Enum LegacyBitmapMode
2340
2341#Code
2342 enum LegacyBitmapMode {
2343 kRO_LegacyBitmapMode,
2344 kRW_LegacyBitmapMode,
2345 };
2346##
2347
2348Helper functions to convert to SkBitmap
2349
2350#Const kRO_LegacyBitmapMode 0
2351##
2352#Const kRW_LegacyBitmapMode 1
2353##
2354
2355#Example
2356// incomplete
2357##
2358
2359#SeeAlso incomplete
2360
2361#Enum ##
2362
2363# ------------------------------------------------------------------------------
2364
2365#Method bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode) const
2366
Cary Clarkac47b882018-01-11 10:35:44 -05002367Creates raster Bitmap with same pixels as Image. If legacyBitmapMode is
2368kRO_LegacyBitmapMode, returned bitmap is read-only and immutable.
2369Returns true if Bitmap is stored in bitmap. Returns false and resets bitmap if
2370Bitmap write did not succeed.
Cary Clarka560c472017-11-27 10:44:06 -05002371
Cary Clark3cd22cc2017-12-01 11:49:58 -05002372#Param bitmap storage for legacy Bitmap ##
2373#Param legacyBitmapMode one of: kRO_LegacyBitmapMode, kRW_LegacyBitmapMode ##
Cary Clarka560c472017-11-27 10:44:06 -05002374
Cary Clark3cd22cc2017-12-01 11:49:58 -05002375#Return true if Bitmap was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002376
2377#Example
2378// incomplete
2379##
2380
2381#SeeAlso incomplete
2382
2383#Method ##
2384
2385# ------------------------------------------------------------------------------
2386
2387#Method bool isLazyGenerated() const
2388
2389Returns true if Image is backed by an image-generator or other service that creates
2390and caches its pixels or texture on-demand.
2391
Cary Clark2f466242017-12-11 16:03:17 -05002392#Return true if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002393
2394#Example
Cary Clark2f466242017-12-11 16:03:17 -05002395#Height 80
2396#Function
2397class TestImageGenerator : public SkImageGenerator {
2398public:
2399 TestImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(10, 10)) {}
2400 ~TestImageGenerator() override {}
2401protected:
2402 bool onGetPixels(const SkImageInfo& info, void* pixelPtr, size_t rowBytes,
2403 const Options& options) override {
2404 SkPMColor* pixels = static_cast<SkPMColor*>(pixelPtr);
2405 for (int y = 0; y < info.height(); ++y) {
2406 for (int x = 0; x < info.width(); ++x) {
2407 pixels[y * info.width() + x] = 0xff223344 + y * 0x000C0811;
2408 }
2409 }
2410 return true;
2411 }
2412};
2413##
2414void draw(SkCanvas* canvas) {
2415 auto gen = std::unique_ptr<TestImageGenerator>(new TestImageGenerator());
2416 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
2417 SkString lazy(image->isLazyGenerated() ? "is lazy" : "not lazy");
2418 canvas->scale(8, 8);
2419 canvas->drawImage(image, 0, 0, nullptr);
2420 SkPaint paint;
2421 paint.setTextSize(4);
2422 canvas->drawString(lazy, 2, 5, paint);
2423}
Cary Clarka560c472017-11-27 10:44:06 -05002424##
2425
Cary Clarkf5404bb2018-01-05 12:10:09 -05002426#Example
2427#Image 5
2428#Platform gpu
2429void draw(SkCanvas* canvas) {
2430 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2431 if (nullptr == image) {
2432 return;
2433 }
2434 SkPaint paint;
2435 paint.setAntiAlias(true);
2436 paint.setTextAlign(SkPaint::kCenter_Align);
2437 canvas->drawImage(image, 0, 0);
2438 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
2439 canvas->drawString(
2440 image->isLazyGenerated() ? "is lazily generated" : "not lazily generated",
2441 image->width() / 2, image->height() * 3 / 4, paint);
2442 };
2443 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2444 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2445 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2446 drawImage(image, "image");
2447 canvas->translate(image->width(), 0);
2448 drawImage(bitmapImage, "source");
2449 canvas->translate(-image->width(), image->height());
2450 drawImage(textureImage, "backEndTexture");
2451}
2452##
2453
Cary Clarkac47b882018-01-11 10:35:44 -05002454#SeeAlso isTextureBacked MakeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002455
2456#Method ##
2457
2458# ------------------------------------------------------------------------------
2459
2460#Method sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target,
2461 SkTransferFunctionBehavior premulBehavior) const
2462
Cary Clarkac47b882018-01-11 10:35:44 -05002463Creates Image in target Color_Space.
2464Returns nullptr if Image could not be created.
Cary Clarka560c472017-11-27 10:44:06 -05002465
Cary Clarkac47b882018-01-11 10:35:44 -05002466Returns original Image if it is in target Color_Space.
2467Otherwise, converts pixels from Image Color_Space to target Color_Space.
2468If Image colorSpace returns nullptr, Image Color_Space is assumed to be sRGB.
2469
2470SkTransferFunctionBehavior is to be deprecated.
2471
2472Set premulBehavior to SkTransferFunctionBehavior::kRespect to convert Image
2473pixels to a linear space, before converting to destination Color_Type
Cary Clarka560c472017-11-27 10:44:06 -05002474and Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -05002475
Cary Clarkac47b882018-01-11 10:35:44 -05002476Set premulBehavior to SkTransferFunctionBehavior::kIgnore to treat Image
2477pixels as linear, when converting to destination Color_Type
2478and Color_Space, ignoring pixel encoding.
Cary Clarka560c472017-11-27 10:44:06 -05002479
Cary Clarkac47b882018-01-11 10:35:44 -05002480#Param target Color_Space describing color range of returned Image ##
2481#Param premulBehavior one of: SkTransferFunctionBehavior::kRespect,
2482 SkTransferFunctionBehavior::kIgnore
Cary Clarka560c472017-11-27 10:44:06 -05002483##
2484
Cary Clarkac47b882018-01-11 10:35:44 -05002485#Return created Image in target Color_Space ##
2486
2487#Example
2488#Image 5
2489#Set sRGB
2490 sk_sp<SkColorSpace> normalColorSpace = SkColorSpace::MakeRGB(
2491 SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kSRGB_Gamut);
2492 sk_sp<SkColorSpace> wackyColorSpace = normalColorSpace->makeColorSpin();
2493 for (auto colorSpace : { normalColorSpace, wackyColorSpace } ) {
2494 for (auto transfer : { SkTransferFunctionBehavior::kRespect,
2495 SkTransferFunctionBehavior::kIgnore } ) {
2496 sk_sp<SkImage> colorSpaced = image->makeColorSpace(colorSpace, transfer);
2497 canvas->drawImage(colorSpaced, 0, 0);
2498 canvas->translate(128, 0);
2499 }
2500 canvas->translate(-256, 128);
2501 }
2502##
2503
2504#SeeAlso MakeFromPixture MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05002505
2506#Method ##
2507
2508#Class SkImage ##
2509
2510#Topic Image ##