blob: 5c79548ef805f6114a64b4f41362436e1dc12b54 [file] [log] [blame]
Cary Clarka560c472017-11-27 10:44:06 -05001#Topic Image
2#Alias Image_Reference
3
Cary Clark08895c42018-02-01 09:37:32 -05004#Subtopic Overview
5 #Subtopic Subtopics
6 #Populate
7 ##
8##
9
Cary Clarka560c472017-11-27 10:44:06 -050010#Class SkImage
11
Cary Clark61ca7c52018-01-02 11:34:14 -050012Image describes a two dimensional array of pixels to draw. The pixels may be
13unencoded in a Raster_Bitmap, encoded in a Picture or compressed data stream,
14or located in GPU memory as a GPU_Texture.
15
16Image cannot be modified after it is created. Image may allocate additional
17storage as needed; for instance, an encoded Image may decode when drawn.
18
19Image width and height are greater than zero. Creating an Image with zero width
20or height returns Image equal to nullptr.
21
22Image may be created from Bitmap, Pixmap, Surface, Picture, encoded streams,
23GPU_Texture, YUV_ColorSpace data, or hardware buffer. Encoded streams supported
24include BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP. Supported encodings details
25vary with platform.
26
Cary Clark08895c42018-02-01 09:37:32 -050027#Subtopic Raster_Image
Cary Clark61ca7c52018-01-02 11:34:14 -050028#Alias Raster_Image
Cary Clark08895c42018-02-01 09:37:32 -050029#Line # pixels unencoded in Raster_Bitmap ##
Cary Clark61ca7c52018-01-02 11:34:14 -050030Raster_Image pixels are unencoded in a Raster_Bitmap. These pixels may be read
31directly and in most cases written to, although edited pixels may not be drawn
32if Image has been copied internally.
33##
34
Cary Clark08895c42018-02-01 09:37:32 -050035#Subtopic Texture_Image
36#Line # pixels located on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -050037Texture_Image are located on GPU and pixels are not accessible. Texture_Image
38are allocated optimally for best performance. Raster_Image may
39be drawn to GPU_Surface, but pixels are uploaded from CPU to GPU downgrading
40performance.
41##
42
Cary Clark08895c42018-02-01 09:37:32 -050043#Subtopic Lazy_Image
44#Line # deferred pixel buffer ##
Cary Clark61ca7c52018-01-02 11:34:14 -050045Lazy_Image defer allocating buffer for Image pixels and decoding stream until
46Image is drawn. Lazy_Image caches result if possible to speed up repeated
47drawing.
48##
Cary Clarka560c472017-11-27 10:44:06 -050049
Cary Clark08895c42018-02-01 09:37:32 -050050#Subtopic Related_Functions
51#Populate
52##
Cary Clarka560c472017-11-27 10:44:06 -050053
Cary Clark08895c42018-02-01 09:37:32 -050054#Subtopic Classes_and_Structs
55#Populate
Cary Clarka560c472017-11-27 10:44:06 -050056##
57
Cary Clark5081eed2018-01-22 07:55:48 -050058#Subtopic Constructors
Cary Clark08895c42018-02-01 09:37:32 -050059#Populate
60##
Cary Clark5081eed2018-01-22 07:55:48 -050061
Cary Clarka560c472017-11-27 10:44:06 -050062#Subtopic Member_Functions
Cary Clark08895c42018-02-01 09:37:32 -050063#Populate
64##
Cary Clarka560c472017-11-27 10:44:06 -050065
Cary Clarka560c472017-11-27 10:44:06 -050066#Typedef SkImageInfo Info
67
68##
69
Cary Clarka560c472017-11-27 10:44:06 -050070# ------------------------------------------------------------------------------
71
72#Method static sk_sp<SkImage> MakeRasterCopy(const SkPixmap& pixmap)
73
Cary Clarkab2621d2018-01-30 10:08:57 -050074#Line # Creates Image from Pixmap and copied pixels. ##
Cary Clark2f466242017-12-11 16:03:17 -050075Creates Image from Pixmap and copy of pixels. Since pixels are copied, Pixmap
76pixels may be modified or deleted without affecting Image.
Cary Clarka560c472017-11-27 10:44:06 -050077
Cary Clark3cd22cc2017-12-01 11:49:58 -050078Image is returned if Pixmap is valid. Valid Pixmap parameters include:
79dimensions are greater than zero;
80each dimension fits in 29 bits;
81Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
82row bytes are large enough to hold one row of pixels;
83pixel address is not nullptr.
84
85#Param pixmap Image_Info, pixel address, and row bytes ##
86
87#Return copy of Pixmap pixels, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -050088
89#Example
Cary Clark2f466242017-12-11 16:03:17 -050090#Height 50
91#Description
92Draw a five by five bitmap, and draw a copy in an Image. Editing the pixmap
93alters the bitmap draw, but does not alter the Image draw since the Image
94contains a copy of the pixels.
95##
96 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
97 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
98 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
99 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
100 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
101 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
102 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
103 SkBitmap bitmap;
104 bitmap.installPixels(pixmap);
105 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
106 *pixmap.writable_addr8(2, 2) = 0x00;
107 canvas->scale(10, 10);
108 canvas->drawBitmap(bitmap, 0, 0);
109 canvas->drawImage(image, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500110##
111
Cary Clark3cd22cc2017-12-01 11:49:58 -0500112#SeeAlso MakeRasterData MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500113
114#Method ##
115
116# ------------------------------------------------------------------------------
117
118#Method static sk_sp<SkImage> MakeRasterData(const Info& info, sk_sp<SkData> pixels, size_t rowBytes)
119
Cary Clarkab2621d2018-01-30 10:08:57 -0500120#Line # Creates Image from Image_Info and shared pixels. ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500121Creates Image from Image_Info, sharing pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500122
Cary Clark3cd22cc2017-12-01 11:49:58 -0500123Image is returned if Image_Info is valid. Valid Image_Info parameters include:
124dimensions are greater than zero;
125each dimension fits in 29 bits;
126Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
127rowBytes are large enough to hold one row of pixels;
128pixels is not nullptr, and contains enough data for Image.
129
130#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
131#Param pixels address or pixel storage ##
132#Param rowBytes size of pixel row or larger ##
133
134#Return Image sharing pixels, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500135
136#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500137#Image 3
138 size_t rowBytes = image->width() * SkColorTypeBytesPerPixel(kRGBA_8888_SkColorType);
139 sk_sp<SkData> data = SkData::MakeUninitialized(rowBytes * image->height());
140 SkImageInfo dstInfo = SkImageInfo::MakeN32(image->width(), image->height(),
141 kPremul_SkAlphaType);
142 image->readPixels(dstInfo, data->writable_data(), rowBytes, 0, 0, SkImage::kAllow_CachingHint);
143 sk_sp<SkImage> raw = SkImage::MakeRasterData(dstInfo.makeColorType(kRGBA_8888_SkColorType),
144 data, rowBytes);
145 canvas->drawImage(image, 0, 0);
146 canvas->drawImage(raw.get(), 128, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500147##
148
Cary Clark3cd22cc2017-12-01 11:49:58 -0500149#SeeAlso MakeRasterCopy MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500150
151#Method ##
152
153# ------------------------------------------------------------------------------
154
Cary Clark3cd22cc2017-12-01 11:49:58 -0500155#Typedef void* ReleaseContext
156
157Caller data passed to RasterReleaseProc; may be nullptr.
158
159#SeeAlso MakeFromRaster RasterReleaseProc
160
161##
162
Cary Clarka560c472017-11-27 10:44:06 -0500163#Typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext)
164
Cary Clark3cd22cc2017-12-01 11:49:58 -0500165Function called when Image no longer shares pixels. ReleaseContext is
166provided by caller when Image is created, and may be nullptr.
167
168#SeeAlso ReleaseContext MakeFromRaster
169
Cary Clarka560c472017-11-27 10:44:06 -0500170##
171
172#Method static sk_sp<SkImage> MakeFromRaster(const SkPixmap& pixmap,
173 RasterReleaseProc rasterReleaseProc,
174 ReleaseContext releaseContext)
Cary Clarkab2621d2018-01-30 10:08:57 -0500175#Line # Creates Image from Pixmap, with release. ##
Cary Clarka560c472017-11-27 10:44:06 -0500176
Cary Clark0c5f5462017-12-15 11:21:51 -0500177Creates Image from pixmap, sharing Pixmap pixels. Pixels must remain valid and
Cary Clark3cd22cc2017-12-01 11:49:58 -0500178unchanged until rasterReleaseProc is called. rasterReleaseProc is passed
179releaseContext when Image is deleted or no longer refers to pixmap pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500180
Cary Clark0c5f5462017-12-15 11:21:51 -0500181Pass nullptr for rasterReleaseProc to share Pixmap without requiring a callback
182when Image is released. Pass nullptr for releaseContext if rasterReleaseProc
183does not require state.
184
Cary Clark3cd22cc2017-12-01 11:49:58 -0500185Image is returned if pixmap is valid. Valid Pixmap parameters include:
186dimensions are greater than zero;
187each dimension fits in 29 bits;
188Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
189row bytes are large enough to hold one row of pixels;
190pixel address is not nullptr.
191
192#Param pixmap Image_Info, pixel address, and row bytes ##
Cary Clark0c5f5462017-12-15 11:21:51 -0500193#Param rasterReleaseProc function called when pixels can be released; or nullptr ##
194#Param releaseContext state passed to rasterReleaseProc; or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500195
Cary Clark0c5f5462017-12-15 11:21:51 -0500196#Return Image sharing pixmap ##
Cary Clarka560c472017-11-27 10:44:06 -0500197
198#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500199#Function
200static void releaseProc(const void* pixels, SkImage::ReleaseContext context) {
201 int* countPtr = static_cast<int*>(context);
202 *countPtr += 1;
203}
204##
205
206void draw(SkCanvas* canvas) {
207 SkColor color = 0;
208 SkPixmap pixmap(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), &color, 4);
209 int releaseCount = 0;
210 sk_sp<SkImage> image(SkImage::MakeFromRaster(pixmap, releaseProc, &releaseCount));
211 SkDebugf("before reset: %d\n", releaseCount);
212 image.reset();
213 SkDebugf("after reset: %d\n", releaseCount);
214}
215#StdOut
216before reset: 0
217after reset: 1
218##
Cary Clarka560c472017-11-27 10:44:06 -0500219##
220
Cary Clark3cd22cc2017-12-01 11:49:58 -0500221#SeeAlso MakeRasterCopy MakeRasterData MakeFromGenerator RasterReleaseProc ReleaseContext
Cary Clarka560c472017-11-27 10:44:06 -0500222
223#Method ##
224
225# ------------------------------------------------------------------------------
226
227#Method static sk_sp<SkImage> MakeFromBitmap(const SkBitmap& bitmap)
228
Cary Clarkab2621d2018-01-30 10:08:57 -0500229#Line # Creates Image from Bitmap, sharing or copying pixels. ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500230Creates Image from bitmap, sharing or copying bitmap pixels. If the bitmap
231is marked immutable, and its pixel memory is shareable, it may be shared
232instead of copied.
Cary Clarka560c472017-11-27 10:44:06 -0500233
Cary Clark3cd22cc2017-12-01 11:49:58 -0500234Image is returned if bitmap is valid. Valid Bitmap parameters include:
235dimensions are greater than zero;
236each dimension fits in 29 bits;
237Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
238row bytes are large enough to hold one row of pixels;
239pixel address is not nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500240
Cary Clark3cd22cc2017-12-01 11:49:58 -0500241#Param bitmap Image_Info, row bytes, and pixels ##
242
243#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500244
245#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500246#Description
247The first Bitmap is shared; writing to the pixel memory changes the first
248Image.
249The second Bitmap is marked immutable, and is copied; writing to the pixel
250memory does not alter the second Image.
251##
252#Height 50
253 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
254 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
255 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
256 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
257 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
258 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
259 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
260 SkBitmap bitmap;
261 bitmap.installPixels(pixmap);
262 sk_sp<SkImage> image1 = SkImage::MakeFromBitmap(bitmap);
263 bitmap.setImmutable();
264 sk_sp<SkImage> image2 = SkImage::MakeFromBitmap(bitmap);
265 *pixmap.writable_addr8(2, 2) = 0x00;
266 canvas->scale(10, 10);
267 canvas->drawImage(image1, 0, 0);
268 canvas->drawImage(image2, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500269##
270
Cary Clark3cd22cc2017-12-01 11:49:58 -0500271#SeeAlso MakeFromRaster MakeRasterCopy MakeFromGenerator MakeRasterData
Cary Clarka560c472017-11-27 10:44:06 -0500272
273#Method ##
274
275# ------------------------------------------------------------------------------
276
277#Method static sk_sp<SkImage> MakeFromGenerator(std::unique_ptr<SkImageGenerator> imageGenerator,
278 const SkIRect* subset = nullptr)
Cary Clarkab2621d2018-01-30 10:08:57 -0500279#Line # Creates Image from a stream of data. ##
Cary Clarka560c472017-11-27 10:44:06 -0500280
Cary Clark0c5f5462017-12-15 11:21:51 -0500281Creates Image from data returned by imageGenerator. Generated data is owned by Image and may not
282be shared or accessed.
Cary Clarka560c472017-11-27 10:44:06 -0500283
Cary Clark0c5f5462017-12-15 11:21:51 -0500284subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
285otherwise, subset must be contained by image bounds.
286
287Image is returned if generator data is valid. Valid data parameters vary by type of data
288and platform.
Cary Clarka560c472017-11-27 10:44:06 -0500289
Cary Clark3cd22cc2017-12-01 11:49:58 -0500290imageGenerator may wrap Picture data, codec data, or custom data.
291
292#Param imageGenerator stock or custom routines to retrieve Image ##
293#Param subset bounds of returned Image; may be nullptr ##
294
295#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500296
297#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500298#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500299#Description
300The generator returning Picture cannot be shared; std::move transfers ownership to generated Image.
301##
302 SkPictureRecorder recorder;
303 recorder.beginRecording(100, 100)->drawColor(SK_ColorRED);
304 auto picture = recorder.finishRecordingAsPicture();
305 auto gen = SkImageGenerator::MakeFromPicture({100, 100}, picture, nullptr, nullptr,
306 SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB());
307 sk_sp<SkImage> image = SkImage::MakeFromGenerator(std::move(gen));
308 canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500309##
310
Cary Clark3cd22cc2017-12-01 11:49:58 -0500311#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500312
313#Method ##
314
315# ------------------------------------------------------------------------------
316
317#Method static sk_sp<SkImage> MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset = nullptr)
318
Cary Clarkab2621d2018-01-30 10:08:57 -0500319#Line # Creates Image from encoded data. ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500320Creates Image from encoded data.
Cary Clark0c5f5462017-12-15 11:21:51 -0500321subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
322otherwise, subset must be contained by image bounds.
Cary Clarka560c472017-11-27 10:44:06 -0500323
Cary Clark3cd22cc2017-12-01 11:49:58 -0500324Image is returned if format of the encoded data is recognized and supported.
325Recognized formats vary by platfrom.
Cary Clarka560c472017-11-27 10:44:06 -0500326
Cary Clark3cd22cc2017-12-01 11:49:58 -0500327#Param encoded data of Image to decode ##
328#Param subset bounds of returned Image; may be nullptr ##
329
330#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500331
Cary Clark61ca7c52018-01-02 11:34:14 -0500332#Example
333#Image 3
334int x = 0;
335for (int quality : { 100, 50, 10, 1} ) {
336 sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, quality);
337 sk_sp<SkImage> image = SkImage::MakeFromEncoded(encodedData);
338 canvas->drawImage(image, x, 0);
339 x += 64;
340}
Cary Clarka560c472017-11-27 10:44:06 -0500341##
342
Cary Clark3cd22cc2017-12-01 11:49:58 -0500343#SeeAlso MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500344
345#Method ##
346
347# ------------------------------------------------------------------------------
348
349#Typedef void (*TextureReleaseProc)(ReleaseContext releaseContext)
350
351##
352
353#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
354 const GrBackendTexture& backendTexture,
355 GrSurfaceOrigin origin,
356 SkAlphaType alphaType,
357 sk_sp<SkColorSpace> colorSpace)
Cary Clarkab2621d2018-01-30 10:08:57 -0500358#Line # Creates Image from GPU_Texture, managed externally. ##
Cary Clarka560c472017-11-27 10:44:06 -0500359
Cary Clarkac47b882018-01-11 10:35:44 -0500360#Deprecated
Cary Clark61ca7c52018-01-02 11:34:14 -0500361Deprecated.
Cary Clarkac47b882018-01-11 10:35:44 -0500362##
Cary Clarka560c472017-11-27 10:44:06 -0500363
Cary Clark3cd22cc2017-12-01 11:49:58 -0500364#Param context GPU_Context ##
365#Param backendTexture texture residing on GPU ##
366#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
367#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
368 kPremul_SkAlphaType, kUnpremul_SkAlphaType
369##
Cary Clark61ca7c52018-01-02 11:34:14 -0500370#Param colorSpace range of colors; may be nullptr ##
371
372#Return created Image, or nullptr ##
373
Cary Clark61ca7c52018-01-02 11:34:14 -0500374#Method ##
375
376# ------------------------------------------------------------------------------
377
378#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
379 const GrBackendTexture& backendTexture,
380 GrSurfaceOrigin origin,
381 SkAlphaType alphaType,
382 sk_sp<SkColorSpace> colorSpace,
383 TextureReleaseProc textureReleaseProc,
384 ReleaseContext releaseContext)
385
Cary Clarkac47b882018-01-11 10:35:44 -0500386#Deprecated
Cary Clark61ca7c52018-01-02 11:34:14 -0500387Deprecated.
Cary Clarkac47b882018-01-11 10:35:44 -0500388##
Cary Clark61ca7c52018-01-02 11:34:14 -0500389
390#Param context GPU_Context ##
391#Param backendTexture texture residing on GPU ##
392#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
393#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
394 kPremul_SkAlphaType, kUnpremul_SkAlphaType
395##
396#Param colorSpace range of colors; may be nullptr ##
397#Param textureReleaseProc function called when texture can be released ##
398#Param releaseContext state passed to textureReleaseProc ##
399
400#Return created Image, or nullptr ##
401
Cary Clark61ca7c52018-01-02 11:34:14 -0500402#Method ##
403
404# ------------------------------------------------------------------------------
405
406#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
407 const GrBackendTexture& backendTexture,
408 GrSurfaceOrigin origin,
409 SkColorType colorType,
410 SkAlphaType alphaType,
411 sk_sp<SkColorSpace> colorSpace)
412
413Creates Image from GPU_Texture associated with context. Caller is responsible for
414managing the lifetime of GPU_Texture.
415
416Image is returned if format of backendTexture is recognized and supported.
417Recognized formats vary by GPU back-end.
418
419#Param context GPU_Context ##
420#Param backendTexture texture residing on GPU ##
421#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
422#Param colorType one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
423 kRGB_565_SkColorType, kARGB_4444_SkColorType,
424 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
425 kGray_8_SkColorType, kRGBA_F16_SkColorType
426##
427#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
428 kPremul_SkAlphaType, kUnpremul_SkAlphaType
429##
430#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500431
432#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500433
434#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500435#Image 3
436#Platform gpu
437#Height 128
438#Description
439A back-end texture has been created and uploaded to the GPU outside of this example.
440##
441GrContext* context = canvas->getGrContext();
442if (!context) {
443 return;
444}
445canvas->scale(.25f, .25f);
446int x = 0;
447for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500448 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark0c5f5462017-12-15 11:21:51 -0500449 origin, kOpaque_SkAlphaType, nullptr);
450 canvas->drawImage(image, x, 0);
451x += 512;
452}
Cary Clarka560c472017-11-27 10:44:06 -0500453##
454
Cary Clark3cd22cc2017-12-01 11:49:58 -0500455#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500456
457#Method ##
458
459# ------------------------------------------------------------------------------
460
461#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
462 const GrBackendTexture& backendTexture,
463 GrSurfaceOrigin origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500464 SkColorType colorType,
Cary Clarka560c472017-11-27 10:44:06 -0500465 SkAlphaType alphaType,
466 sk_sp<SkColorSpace> colorSpace,
467 TextureReleaseProc textureReleaseProc,
468 ReleaseContext releaseContext)
469
Cary Clark61ca7c52018-01-02 11:34:14 -0500470Creates Image from GPU_Texture associated with context. GPU_Texture must stay
Cary Clark3cd22cc2017-12-01 11:49:58 -0500471valid and unchanged until textureReleaseProc is called. textureReleaseProc is
472passed releaseContext when Image is deleted or no longer refers to texture.
Cary Clarka560c472017-11-27 10:44:06 -0500473
Cary Clark3cd22cc2017-12-01 11:49:58 -0500474Image is returned if format of backendTexture is recognized and supported.
475Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500476
Cary Clark3cd22cc2017-12-01 11:49:58 -0500477#Param context GPU_Context ##
478#Param backendTexture texture residing on GPU ##
479#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500480#Param colorType one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
481 kRGB_565_SkColorType, kARGB_4444_SkColorType,
482 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
483 kGray_8_SkColorType, kRGBA_F16_SkColorType
484##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500485#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
486 kPremul_SkAlphaType, kUnpremul_SkAlphaType
487##
Cary Clark61ca7c52018-01-02 11:34:14 -0500488#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500489#Param textureReleaseProc function called when texture can be released ##
490#Param releaseContext state passed to textureReleaseProc ##
491
492#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500493
Cary Clark0c5f5462017-12-15 11:21:51 -0500494#ToDo
495This doesn't do anything clever with TextureReleaseProc because it may not get called
Cary Clark61ca7c52018-01-02 11:34:14 -0500496fwithin the lifetime of the example
Cary Clark0c5f5462017-12-15 11:21:51 -0500497##
498
Cary Clarka560c472017-11-27 10:44:06 -0500499#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500500#Platform gpu
501#Image 4
Cary Clark0c5f5462017-12-15 11:21:51 -0500502GrContext* context = canvas->getGrContext();
503if (!context) {
504 return;
505}
Cary Clarkac47b882018-01-11 10:35:44 -0500506auto debugster = [](SkImage::ReleaseContext releaseContext) -> void {
507 *((int *) releaseContext) += 128;
Cary Clark0c5f5462017-12-15 11:21:51 -0500508};
509int x = 0;
510for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500511 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark61ca7c52018-01-02 11:34:14 -0500512 origin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr, debugster, &x);
Cary Clark0c5f5462017-12-15 11:21:51 -0500513 canvas->drawImage(image, x, 0);
514 x += 128;
515}
Cary Clarka560c472017-11-27 10:44:06 -0500516##
517
Cary Clark3cd22cc2017-12-01 11:49:58 -0500518#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500519
520#Method ##
521
522# ------------------------------------------------------------------------------
523
524#Method static sk_sp<SkImage> MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> data,
525 bool buildMips,
526 SkColorSpace* dstColorSpace)
Cary Clarkab2621d2018-01-30 10:08:57 -0500527#Line # Creates Image from encoded data, and uploads to GPU. ##
Cary Clarka560c472017-11-27 10:44:06 -0500528
Cary Clark3cd22cc2017-12-01 11:49:58 -0500529Creates Image from encoded data. Image is uploaded to GPU back-end using context.
530
531Created Image is available to other GPU contexts, and is available across thread
532boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
533share resources.
534
535When Image is no longer referenced, context releases texture memory
Cary Clarka560c472017-11-27 10:44:06 -0500536asynchronously.
Cary Clarka560c472017-11-27 10:44:06 -0500537
Cary Clark3cd22cc2017-12-01 11:49:58 -0500538Texture decoded from data is uploaded to match Surface created with
539dstColorSpace. Color_Space of Image is determined by encoded data.
Cary Clarka560c472017-11-27 10:44:06 -0500540
Cary Clark3cd22cc2017-12-01 11:49:58 -0500541Image is returned if format of data is recognized and supported, and if context
542supports moving resources. Recognized formats vary by platform and GPU back-end.
543
Cary Clark61ca7c52018-01-02 11:34:14 -0500544Image is returned using MakeFromEncoded if context is nullptr or does not support
545moving resources between contexts.
546
Cary Clark3cd22cc2017-12-01 11:49:58 -0500547#Param context GPU_Context ##
548#Param data Image to decode ##
549#Param buildMips create Image as Mip_Map if true ##
550#Param dstColorSpace range of colors of matching Surface on GPU ##
551
552#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500553
554#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500555#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500556#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500557GrContext* context = canvas->getGrContext();
558sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, 100);
559sk_sp<SkImage> image = SkImage::MakeCrossContextFromEncoded(context,
560 encodedData, false, nullptr);
561canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500562##
563
Cary Clark3cd22cc2017-12-01 11:49:58 -0500564#SeeAlso MakeCrossContextFromPixmap
565
566#Method ##
567
568# ------------------------------------------------------------------------------
569
570#Method static sk_sp<SkImage> MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap,
571 bool buildMips,
572 SkColorSpace* dstColorSpace)
Cary Clarkab2621d2018-01-30 10:08:57 -0500573#Line # Creates Image from Pixmap, and uploads to GPU. ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500574
575Creates Image from pixmap. Image is uploaded to GPU back-end using context.
576
577Created Image is available to other GPU contexts, and is available across thread
578boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
579share resources.
580
581When Image is no longer referenced, context releases texture memory
582asynchronously.
583
584Texture created from pixmap is uploaded to match Surface created with
585dstColorSpace. Color_Space of Image is determined by pixmap.colorSpace().
586
Cary Clark61ca7c52018-01-02 11:34:14 -0500587Image is returned referring to GPU back-end if context is not nullptr,
588format of data is recognized and supported, and if context supports moving
589resources between contexts. Otherwise, pixmap pixel data is copied and Image
590as returned in raster format if possible; nullptr may be returned.
591Recognized GPU formats vary by platform and GPU back-end.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500592
593#Param context GPU_Context ##
594#Param pixmap Image_Info, pixel address, and row bytes ##
595#Param buildMips create Image as Mip_Map if true ##
596#Param dstColorSpace range of colors of matching Surface on GPU ##
597
598#Return created Image, or nullptr ##
599
600#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500601#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500602#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500603GrContext* context = canvas->getGrContext();
604SkPixmap pixmap;
605if (source.peekPixels(&pixmap)) {
606 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(context, pixmap,
607 false, nullptr);
608 canvas->drawImage(image, 0, 0);
609}
Cary Clark3cd22cc2017-12-01 11:49:58 -0500610##
611
612#SeeAlso MakeCrossContextFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500613
614#Method ##
615
616# ------------------------------------------------------------------------------
617
618#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
619 const GrBackendTexture& backendTexture,
620 GrSurfaceOrigin surfaceOrigin,
621 SkAlphaType alphaType = kPremul_SkAlphaType,
622 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clarkab2621d2018-01-30 10:08:57 -0500623#Line # Creates Image from GPU_Texture, managed internally. ##
Cary Clarka560c472017-11-27 10:44:06 -0500624
Cary Clark61ca7c52018-01-02 11:34:14 -0500625Deprecated.
626
627#Param context GPU_Context ##
628#Param backendTexture texture residing on GPU ##
629#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
630#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
631 kPremul_SkAlphaType, kUnpremul_SkAlphaType
632##
633#Param colorSpace range of colors; may be nullptr ##
634
635#Return created Image, or nullptr ##
636
637#NoExample
638##
639
640#SeeAlso MakeFromTexture MakeFromYUVTexturesCopy
641
642#Method ##
643
644# ------------------------------------------------------------------------------
645
646#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
647 const GrBackendTexture& backendTexture,
648 GrSurfaceOrigin surfaceOrigin,
649 SkColorType colorType,
650 SkAlphaType alphaType = kPremul_SkAlphaType,
651 sk_sp<SkColorSpace> colorSpace = nullptr)
652
Cary Clark3cd22cc2017-12-01 11:49:58 -0500653Creates Image from backendTexture associated with context. backendTexture and
654returned Image are managed internally, and are released when no longer needed.
Cary Clarka560c472017-11-27 10:44:06 -0500655
Cary Clark3cd22cc2017-12-01 11:49:58 -0500656Image is returned if format of backendTexture is recognized and supported.
657Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500658
Cary Clark3cd22cc2017-12-01 11:49:58 -0500659#Param context GPU_Context ##
660#Param backendTexture texture residing on GPU ##
661#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500662#Param colorType one of: kUnknown_SkColorType, kAlpha_8_SkColorType,
663 kRGB_565_SkColorType, kARGB_4444_SkColorType,
664 kRGBA_8888_SkColorType, kBGRA_8888_SkColorType,
665 kGray_8_SkColorType, kRGBA_F16_SkColorType
666##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500667#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
668 kPremul_SkAlphaType, kUnpremul_SkAlphaType
669##
Cary Clark61ca7c52018-01-02 11:34:14 -0500670#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500671
672#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500673
674#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500675#Image 5
676#Platform gpu
Cary Clark61ca7c52018-01-02 11:34:14 -0500677 if (!canvas->getGrContext()) {
678 return;
679 }
680 canvas->scale(.5f, .5f);
681 canvas->clear(0x7f3f5f7f);
682 int x = 0, y = 0;
683 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
684 for (auto alpha : { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType } ) {
685 sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(canvas->getGrContext(),
686 backEndTexture, origin,
687 kRGBA_8888_SkColorType, alpha);
688 canvas->drawImage(image, x, y);
689 x += 160;
690 }
691 x -= 160 * 3;
692 y += 256;
693 }
Cary Clarka560c472017-11-27 10:44:06 -0500694##
695
Cary Clark61ca7c52018-01-02 11:34:14 -0500696#SeeAlso MakeFromTexture MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500697
698#Method ##
699
700# ------------------------------------------------------------------------------
701
702#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
703 const GrBackendObject yuvTextureHandles[3],
704 const SkISize yuvSizes[3],
705 GrSurfaceOrigin surfaceOrigin,
706 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clarkab2621d2018-01-30 10:08:57 -0500707#Line # Creates Image from YUV_ColorSpace data in three planes. ##
Cary Clarka560c472017-11-27 10:44:06 -0500708
Cary Clark61ca7c52018-01-02 11:34:14 -0500709Creates Image from copy of yuvTextureHandles, an array of textures on GPU.
710yuvTextureHandles contain pixels for YUV planes of Image.
711yuvSizes conain dimensions for each pixel plane. Dimensions must be greater than
712zero but may differ from plane to plane. Returned Image has the dimensions
713yuvSizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500714
Cary Clark61ca7c52018-01-02 11:34:14 -0500715#Param context GPU_Context ##
716#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
717 kRec709_SkYUVColorSpace
718##
719#Param yuvTextureHandles array of YUV textures on GPU ##
720#Param yuvSizes dimensions of YUV textures ##
721#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
722#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500723
Cary Clark61ca7c52018-01-02 11:34:14 -0500724#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500725
Cary Clark61ca7c52018-01-02 11:34:14 -0500726# seems too complicated to create an example for this
727#ToDo
728should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500729##
730
Cary Clark61ca7c52018-01-02 11:34:14 -0500731#NoExample
732##
733
734#SeeAlso MakeFromNV12TexturesCopy
735
736#Method ##
737
738# ------------------------------------------------------------------------------
739
740#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
741 const GrBackendTexture yuvTextureHandles[3],
742 const SkISize yuvSizes[3],
743 GrSurfaceOrigin surfaceOrigin,
744 sk_sp<SkColorSpace> colorSpace = nullptr)
745
746Creates Image from copy of yuvTextureHandles, an array of textures on GPU.
747yuvTextureHandles contain pixels for YUV planes of Image.
748yuvSizes conain dimensions for each pixel plane. Dimensions must be greater than
749zero but may differ from plane to plane. Returned Image has the dimensions
750yuvSizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
751
752#Param context GPU_Context ##
753#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
754 kRec709_SkYUVColorSpace
755##
756#Param yuvTextureHandles array of YUV textures on GPU ##
757#Param yuvSizes dimensions of YUV textures ##
758#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
759#Param colorSpace range of colors; may be nullptr ##
760
761#Return created Image, or nullptr ##
762
763# seems too complicated to create an example for this
764#ToDo
765should this be moved to chrome only?
766##
767
768#NoExample
769##
770
771#SeeAlso MakeFromNV12TexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500772
773#Method ##
774
775# ------------------------------------------------------------------------------
776
777#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
778 SkYUVColorSpace yuvColorSpace,
779 const GrBackendObject nv12TextureHandles[2],
780 const SkISize nv12Sizes[2],
781 GrSurfaceOrigin surfaceOrigin,
782 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clarkab2621d2018-01-30 10:08:57 -0500783#Line # Creates Image from YUV_ColorSpace data in two planes. ##
Cary Clarka560c472017-11-27 10:44:06 -0500784
Cary Clark61ca7c52018-01-02 11:34:14 -0500785Creates Image from copy of nv12TextureHandles, an array of textures on GPU.
786nv12TextureHandles[0] contains pixels for YUV_Component_Y plane.
787nv12TextureHandles[1] contains pixels for YUV_Component_U plane,
788followed by pixels for YUV_Component_V plane.
789nv12Sizes conain dimensions for each pixel plane. Dimensions must be greater than
790zero but may differ from plane to plane. Returned Image has the dimensions
791nv12Sizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500792
Cary Clark61ca7c52018-01-02 11:34:14 -0500793#Param context GPU_Context ##
794#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
795 kRec709_SkYUVColorSpace
796##
797#Param nv12TextureHandles array of YUV textures on GPU ##
798#Param nv12Sizes dimensions of YUV textures ##
799#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
800#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500801
Cary Clark61ca7c52018-01-02 11:34:14 -0500802#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500803
Cary Clark61ca7c52018-01-02 11:34:14 -0500804# seems too complicated to create an example for this
805#ToDo
806should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500807##
808
Cary Clark61ca7c52018-01-02 11:34:14 -0500809#NoExample
810##
811
812#SeeAlso MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500813
814#Method ##
815
816# ------------------------------------------------------------------------------
817
Cary Clark61ca7c52018-01-02 11:34:14 -0500818#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
819 SkYUVColorSpace yuvColorSpace,
820 const GrBackendTexture nv12TextureHandles[2],
821 const SkISize nv12Sizes[2],
822 GrSurfaceOrigin surfaceOrigin,
823 sk_sp<SkColorSpace> colorSpace = nullptr)
824
825Creates Image from copy of nv12TextureHandles, an array of textures on GPU.
826nv12TextureHandles[0] contains pixels for YUV_Component_Y plane.
827nv12TextureHandles[1] contains pixels for YUV_Component_U plane,
828followed by pixels for YUV_Component_V plane.
829nv12Sizes conain dimensions for each pixel plane. Dimensions must be greater than
830zero but may differ from plane to plane. Returned Image has the dimensions
831nv12Sizes[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
832
833#Param context GPU_Context ##
834#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
835 kRec709_SkYUVColorSpace
836##
837#Param nv12TextureHandles array of YUV textures on GPU ##
838#Param nv12Sizes dimensions of YUV textures ##
839#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
840#Param colorSpace range of colors; may be nullptr ##
841
842#Return created Image, or nullptr ##
843
844# seems too complicated to create an example for this
845#ToDo
846should this be moved to chrome only?
847##
848
849#NoExample
850##
851
852#SeeAlso MakeFromYUVTexturesCopy
853
854#Method ##
855
856# ------------------------------------------------------------------------------
857
858#Bug 7424
859currently uncalled by any test or client
860##
861
Cary Clarka560c472017-11-27 10:44:06 -0500862#Enum BitDepth
863
864#Code
Cary Clark61ca7c52018-01-02 11:34:14 -0500865 enum class BitDepth {
Cary Clarka560c472017-11-27 10:44:06 -0500866 kU8,
867 kF16,
868 };
869##
870
871#Const kU8 0
Cary Clark61ca7c52018-01-02 11:34:14 -0500872Use 8 bits per Color_ARGB component using unsigned integer format.
Cary Clarka560c472017-11-27 10:44:06 -0500873##
874#Const kF16 1
Cary Clark61ca7c52018-01-02 11:34:14 -0500875Use 16 bits per Color_ARGB component using half-precision floating point format.
Cary Clarka560c472017-11-27 10:44:06 -0500876##
877
Cary Clark61ca7c52018-01-02 11:34:14 -0500878#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500879##
880
Cary Clark61ca7c52018-01-02 11:34:14 -0500881#SeeAlso MakeFromPicture
Cary Clarka560c472017-11-27 10:44:06 -0500882
883#Enum ##
884
885# ------------------------------------------------------------------------------
886
887#Method static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
888 const SkMatrix* matrix, const SkPaint* paint,
889 BitDepth bitDepth,
890 sk_sp<SkColorSpace> colorSpace)
Cary Clarkab2621d2018-01-30 10:08:57 -0500891#Line # Creates Image from Picture. ##
Cary Clarka560c472017-11-27 10:44:06 -0500892
Cary Clark61ca7c52018-01-02 11:34:14 -0500893Creates Image from picture. Returned Image width and height are set by dimensions.
894Image draws picture with matrix and paint, set to bitDepth and colorSpace.
Cary Clarka560c472017-11-27 10:44:06 -0500895
Cary Clark61ca7c52018-01-02 11:34:14 -0500896If matrix is nullptr, draws with identity Matrix. If paint is nullptr, draws
897with default Paint. colorSpace may be nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500898
Cary Clark61ca7c52018-01-02 11:34:14 -0500899#Param picture stream of drawing commands ##
900#Param dimensions width and height ##
901#Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ##
902#Param paint Paint to apply transparency, filtering, and so on; may be nullptr ##
903#Param bitDepth 8 bit integer or 16 bit float: per component ##
904#Param colorSpace range of colors; may be nullptr ##
905
906#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500907
908#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500909 SkPaint paint;
910 SkPictureRecorder recorder;
911 SkCanvas* recordingCanvas = recorder.beginRecording(50, 50);
912 for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) {
913 paint.setColor(color);
914 recordingCanvas->drawRect({10, 10, 30, 40}, paint);
915 recordingCanvas->translate(10, 10);
916 recordingCanvas->scale(1.2f, 1.4f);
917 }
918 sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
919 int x = 0, y = 0;
920 for (auto alpha : { 70, 140, 210 } ) {
921 paint.setAlpha(alpha);
922 auto srgbColorSpace = SkColorSpace::MakeSRGB();
923 sk_sp<SkImage> image = SkImage::MakeFromPicture(playback, {50, 50}, nullptr, &paint,
924 SkImage::BitDepth::kU8, srgbColorSpace);
925 canvas->drawImage(image, x, y);
926 x += 70; y += 70;
927 }
Cary Clarka560c472017-11-27 10:44:06 -0500928##
929
Cary Clark61ca7c52018-01-02 11:34:14 -0500930#SeeAlso SkCanvas::drawPicture
Cary Clarka560c472017-11-27 10:44:06 -0500931
932#Method ##
933
934# ------------------------------------------------------------------------------
935
936#Method static sk_sp<SkImage> MakeFromAHardwareBuffer(AHardwareBuffer* hardwareBuffer,
937 SkAlphaType alphaType = kPremul_SkAlphaType,
938 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clarkab2621d2018-01-30 10:08:57 -0500939#Line # Creates Image from Android hardware buffer. ##
Cary Clarka560c472017-11-27 10:44:06 -0500940
Cary Clark61ca7c52018-01-02 11:34:14 -0500941#Bug 7447 ##
Cary Clarka560c472017-11-27 10:44:06 -0500942
Cary Clark61ca7c52018-01-02 11:34:14 -0500943Creates Image from Android hardware buffer.
944Returned Image takes a reference on the buffer.
Cary Clarka560c472017-11-27 10:44:06 -0500945
Cary Clark61ca7c52018-01-02 11:34:14 -0500946Only available on Android, when __ANDROID_API__ is defined to be 26 or greater.
Cary Clarka560c472017-11-27 10:44:06 -0500947
Cary Clark61ca7c52018-01-02 11:34:14 -0500948#Param hardwareBuffer AHardwareBuffer Android hardware buffer ##
949#Param alphaType one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
950 kPremul_SkAlphaType, kUnpremul_SkAlphaType
951##
952#Param colorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500953
Cary Clark61ca7c52018-01-02 11:34:14 -0500954#Return created Image, or nullptr ##
955
956#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500957##
958
Cary Clark61ca7c52018-01-02 11:34:14 -0500959#SeeAlso MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -0500960
961#Method ##
962
963# ------------------------------------------------------------------------------
964
965#Method int width() const
966
Cary Clarkab2621d2018-01-30 10:08:57 -0500967#Line # Returns pixel column count. ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500968Returns pixel count in each row.
969
970#Return pixel width in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500971
972#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500973#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500974#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500975 canvas->translate(10, 10);
976 canvas->drawImage(image, 0, 0);
977 canvas->translate(0, image->height());
978 SkPaint paint;
979 paint.setTextAlign(SkPaint::kCenter_Align);
980 canvas->drawLine(0, 10, image->width(), 10, paint);
981 canvas->drawString("width", image->width() / 2, 25, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500982##
983
Cary Clark61ca7c52018-01-02 11:34:14 -0500984#SeeAlso dimensions() height()
Cary Clarka560c472017-11-27 10:44:06 -0500985
986#Method ##
987
988# ------------------------------------------------------------------------------
989
990#Method int height() const
991
Cary Clarkab2621d2018-01-30 10:08:57 -0500992#Line # Returns pixel row count. ##
Cary Clark2f466242017-12-11 16:03:17 -0500993Returns pixel row count.
994
Cary Clark61ca7c52018-01-02 11:34:14 -0500995#Return pixel height in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500996
997#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500998#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500999#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -05001000 canvas->translate(10, 10);
1001 canvas->drawImage(image, 0, 0);
1002 canvas->translate(image->width(), 0);
1003 SkPaint paint;
1004 paint.setTextAlign(SkPaint::kCenter_Align);
1005 paint.setVerticalText(true);
1006 canvas->drawLine(10, 0, 10, image->height(), paint);
Cary Clarkac47b882018-01-11 10:35:44 -05001007 canvas->drawString("height", 25, image->height() / 2, paint);
1008##
Cary Clarka560c472017-11-27 10:44:06 -05001009
Cary Clark61ca7c52018-01-02 11:34:14 -05001010#SeeAlso dimensions() width()
Cary Clarka560c472017-11-27 10:44:06 -05001011
1012#Method ##
1013
1014# ------------------------------------------------------------------------------
1015
1016#Method SkISize dimensions() const
1017
Cary Clarkab2621d2018-01-30 10:08:57 -05001018#Line # Returns width() and height(). ##
Cary Clark2f466242017-12-11 16:03:17 -05001019Returns ISize { width(), height() }.
1020
1021#Return integral size of width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -05001022
1023#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001024#Image 4
1025 SkISize dimensions = image->dimensions();
1026 SkIRect bounds = image->bounds();
1027 SkIRect dimensionsAsBounds = SkIRect::MakeSize(dimensions);
1028 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clarka560c472017-11-27 10:44:06 -05001029##
1030
Cary Clark61ca7c52018-01-02 11:34:14 -05001031#SeeAlso height() width() bounds()
Cary Clarka560c472017-11-27 10:44:06 -05001032
1033#Method ##
1034
1035# ------------------------------------------------------------------------------
1036
1037#Method SkIRect bounds() const
1038
Cary Clarkab2621d2018-01-30 10:08:57 -05001039#Line # Returns width() and height() as Rectangle. ##
Cary Clark2f466242017-12-11 16:03:17 -05001040Returns IRect { 0, 0, width(), height() }.
1041
1042#Return integral rectangle from origin to width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -05001043
1044#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001045#Height 128
1046#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -05001047 SkIRect bounds = image->bounds();
Cary Clarkac47b882018-01-11 10:35:44 -05001048 for (int x : { 0, bounds.width() } ) {
1049 for (int y : { 0, bounds.height() } ) {
Cary Clark61ca7c52018-01-02 11:34:14 -05001050 canvas->drawImage(image, x, y);
1051 }
1052 }
Cary Clarka560c472017-11-27 10:44:06 -05001053##
1054
Cary Clark61ca7c52018-01-02 11:34:14 -05001055#SeeAlso dimensions()
Cary Clarka560c472017-11-27 10:44:06 -05001056
1057#Method ##
1058
1059# ------------------------------------------------------------------------------
1060
1061#Method uint32_t uniqueID() const
1062
Cary Clarkab2621d2018-01-30 10:08:57 -05001063#Line # Identifier for Image. ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001064Returns value unique to image. Image contents cannot change after Image is
1065created. Any operation to create a new Image will receive generate a new
1066unique number.
1067
1068#Return unique identifier ##
Cary Clarka560c472017-11-27 10:44:06 -05001069
1070#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001071#Image 5
1072#Height 156
1073 sk_sp<SkImage> subset = image->makeSubset({10, 20, 90, 100});
1074 canvas->drawImage(image, 0, 0);
1075 canvas->drawImage(subset, 128, 0);
1076 SkPaint paint;
1077 SkString s;
1078 s.printf("original id: %d", image->uniqueID());
1079 canvas->drawString(s, 20, image->height() + 20, paint);
1080 s.printf("subset id: %d", subset->uniqueID());
1081 canvas->drawString(s, 148, subset->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001082##
1083
Cary Clark61ca7c52018-01-02 11:34:14 -05001084#SeeAlso isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001085
1086#Method ##
1087
1088# ------------------------------------------------------------------------------
1089
1090#Method SkAlphaType alphaType() const
1091
Cary Clarkab2621d2018-01-30 10:08:57 -05001092#Line # Returns Alpha_Type. ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001093Returns Alpha_Type, one of: kUnknown_SkAlphaType, kOpaque_SkAlphaType,
1094kPremul_SkAlphaType, kUnpremul_SkAlphaType.
1095
1096Alpha_Type returned was a parameter to an Image constructor,
1097or was parsed from encoded data.
1098
1099#Return Alpha_Type in Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001100
1101#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001102#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -05001103#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -05001104 const char* alphaTypeStr[] = { "Unknown", "Opaque", "Premul", "Unpremul" };
1105 SkAlphaType alphaType = image->alphaType();
Cary Clarkac47b882018-01-11 10:35:44 -05001106 canvas->drawImage(image, 16, 0);
Cary Clark61ca7c52018-01-02 11:34:14 -05001107 SkPaint paint;
1108 canvas->drawString(alphaTypeStr[(int) alphaType], 20, image->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001109##
1110
Cary Clark61ca7c52018-01-02 11:34:14 -05001111#SeeAlso SkImageInfo::alphaType
Cary Clarka560c472017-11-27 10:44:06 -05001112
1113#Method ##
1114
1115# ------------------------------------------------------------------------------
1116
1117#Method SkColorSpace* colorSpace() const
1118
Cary Clarkab2621d2018-01-30 10:08:57 -05001119#Line # Returns Color_Space. ##
Cary Clark2f466242017-12-11 16:03:17 -05001120Returns Color_Space, the range of colors, associated with Image. The
1121reference count of Color_Space is unchanged. The returned Color_Space is
1122immutable.
Cary Clarka560c472017-11-27 10:44:06 -05001123
Cary Clark61dfc3a2018-01-03 08:37:53 -05001124Color_Space returned was passed to an Image constructor,
1125or was parsed from encoded data. Color_Space returned may be ignored when Image
1126is drawn, depending on the capabilities of the Surface receiving the drawing.
Cary Clark2f466242017-12-11 16:03:17 -05001127
1128#Return Color_Space in Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001129
1130#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001131#Image 3
1132#Set sRGB
1133 SkPixmap pixmap;
1134 source.peekPixels(&pixmap);
1135 canvas->scale(.25f, .25f);
1136 int y = 0;
1137 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1138 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1139 int x = 0;
1140 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1141 for (int index = 0; index < 2; ++index) {
1142 pixmap.setColorSpace(colorSpace);
1143 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1144 canvas->drawImage(image, x, y);
1145 colorSpace = image->colorSpace()->makeColorSpin();
1146 x += 512;
1147 }
1148 y += 512;
1149 }
Cary Clarka560c472017-11-27 10:44:06 -05001150##
1151
Cary Clark61dfc3a2018-01-03 08:37:53 -05001152#SeeAlso refColorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001153
1154#Method ##
1155
1156# ------------------------------------------------------------------------------
1157
1158#Method sk_sp<SkColorSpace> refColorSpace() const
1159
Cary Clarkab2621d2018-01-30 10:08:57 -05001160#Line # Returns Image_Info Color_Space. ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001161Returns a smart pointer to Color_Space, the range of colors, associated with
1162Image. The smart pointer tracks the number of objects sharing this
1163SkColorSpace reference so the memory is released when the owners destruct.
1164
1165The returned SkColorSpace is immutable.
1166
1167Color_Space returned was passed to an Image constructor,
1168or was parsed from encoded data. Color_Space returned may be ignored when Image
1169is drawn, depending on the capabilities of the Surface receiving the drawing.
1170
1171#Return Color_Space in Image, or nullptr, wrapped in a smart pointer ##
Cary Clarka560c472017-11-27 10:44:06 -05001172
1173#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001174#Image 3
1175#Set sRGB
1176 SkPixmap pixmap;
1177 source.peekPixels(&pixmap);
1178 canvas->scale(.25f, .25f);
1179 int y = 0;
1180 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1181 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1182 int x = 0;
1183 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1184 for (int index = 0; index < 2; ++index) {
1185 pixmap.setColorSpace(colorSpace);
1186 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1187 canvas->drawImage(image, x, y);
1188 colorSpace = image->refColorSpace()->makeColorSpin();
1189 x += 512;
1190 }
1191 y += 512;
1192 }
Cary Clarka560c472017-11-27 10:44:06 -05001193##
1194
Cary Clark61dfc3a2018-01-03 08:37:53 -05001195#SeeAlso colorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001196
1197#Method ##
1198
1199# ------------------------------------------------------------------------------
1200
1201#Method bool isAlphaOnly() const
1202
Cary Clarkab2621d2018-01-30 10:08:57 -05001203#Line # Returns if pixels represent a transparency mask. ##
Cary Clark2f466242017-12-11 16:03:17 -05001204Returns true if Image pixels represent transparency only. If true, each pixel
1205is packed in 8 bits as defined by kAlpha_8_SkColorType.
Cary Clarka560c472017-11-27 10:44:06 -05001206
Cary Clark2f466242017-12-11 16:03:17 -05001207#Return true if pixels represent a transparency mask ##
Cary Clarka560c472017-11-27 10:44:06 -05001208
1209#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001210 uint8_t pmColors = 0;
1211 sk_sp<SkImage> image = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1), &pmColors, 1});
1212 SkDebugf("alphaOnly = %s\n", image->isAlphaOnly() ? "true" : "false");
1213#StdOut
1214alphaOnly = true
1215##
Cary Clarka560c472017-11-27 10:44:06 -05001216##
1217
Cary Clark61dfc3a2018-01-03 08:37:53 -05001218#SeeAlso alphaType isOpaque
Cary Clarka560c472017-11-27 10:44:06 -05001219
1220#Method ##
1221
1222# ------------------------------------------------------------------------------
1223
1224#Method bool isOpaque() const
1225
Cary Clarkab2621d2018-01-30 10:08:57 -05001226#Line # Returns if Alpha_Type is kOpaque_SkAlphaType. ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001227Returns true if pixels ignore their Alpha value and are treated as fully opaque.
Cary Clark2f466242017-12-11 16:03:17 -05001228
1229#Return true if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clarka560c472017-11-27 10:44:06 -05001230
1231#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001232 auto check_isopaque = [](const SkImageInfo& imageInfo) -> void {
1233 auto surface(SkSurface::MakeRaster(imageInfo));
1234 auto image(surface->makeImageSnapshot());
1235 SkDebugf("isOpaque = %s\n", image->isOpaque() ? "true" : "false");
1236 };
1237
1238 check_isopaque(SkImageInfo::MakeN32Premul(5, 5));
1239 check_isopaque(SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType));
1240#StdOut
1241isOpaque = false
1242isOpaque = true
1243##
Cary Clarka560c472017-11-27 10:44:06 -05001244##
1245
Cary Clark61dfc3a2018-01-03 08:37:53 -05001246#SeeAlso alphaType isAlphaOnly
Cary Clarka560c472017-11-27 10:44:06 -05001247
1248#Method ##
1249
1250# ------------------------------------------------------------------------------
1251
1252#Method sk_sp<SkShader> makeShader(SkShader::TileMode tileMode1, SkShader::TileMode tileMode2,
1253 const SkMatrix* localMatrix = nullptr) const
Cary Clarkab2621d2018-01-30 10:08:57 -05001254#Line # Creates Shader, Paint element that can tile Image. ##
Cary Clarka560c472017-11-27 10:44:06 -05001255
Cary Clark61dfc3a2018-01-03 08:37:53 -05001256Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1257SkShader::TileMode rules to fill drawn area outside Image. localMatrix permits
1258transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001259
Cary Clark61dfc3a2018-01-03 08:37:53 -05001260#Param tileMode1 tiling in x, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1261 SkShader::kMirror_TileMode
1262##
1263#Param tileMode2 tiling in y, one of: SkShader::kClamp_TileMode, SkShader::kRepeat_TileMode,
1264 SkShader::kMirror_TileMode
1265##
1266#Param localMatrix Image transformation, or nullptr ##
1267
1268#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001269
1270#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001271#Image 4
1272SkMatrix matrix;
1273matrix.setRotate(45);
1274SkPaint paint;
1275paint.setShader(image->makeShader(SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode,
1276 &matrix));
1277canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001278##
1279
Cary Clark61dfc3a2018-01-03 08:37:53 -05001280#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001281
1282#Method ##
1283
1284# ------------------------------------------------------------------------------
1285
1286#Method sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const
1287
Cary Clark61dfc3a2018-01-03 08:37:53 -05001288Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1289SkShader::kClamp_TileMode to fill drawn area outside Image. localMatrix permits
1290transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001291
Cary Clark61dfc3a2018-01-03 08:37:53 -05001292#Param localMatrix Image transformation, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001293
Cary Clark61dfc3a2018-01-03 08:37:53 -05001294#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001295
1296#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001297#Image 5
1298SkMatrix matrix;
1299matrix.setRotate(45);
1300matrix.postTranslate(125, 30);
1301SkPaint paint;
1302paint.setShader(image->makeShader(&matrix));
1303canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001304##
1305
Cary Clarkf5404bb2018-01-05 12:10:09 -05001306#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001307
1308#Method ##
1309
1310# ------------------------------------------------------------------------------
1311
1312#Method bool peekPixels(SkPixmap* pixmap) const
1313
Cary Clarkab2621d2018-01-30 10:08:57 -05001314#Line # Returns Pixmap if possible. ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001315Copies Image pixel address, row bytes, and Image_Info to pixmap, if address
1316is available, and returns true. If pixel address is not available, return
1317false and leave pixmap unchanged.
Cary Clarka560c472017-11-27 10:44:06 -05001318
Cary Clarkf5404bb2018-01-05 12:10:09 -05001319#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
Cary Clarka560c472017-11-27 10:44:06 -05001320
Cary Clarkf5404bb2018-01-05 12:10:09 -05001321#Return true if Image has direct access to pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001322
1323#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001324 SkBitmap bitmap;
1325 bitmap.allocPixels(SkImageInfo::MakeN32Premul(12, 11));
1326 SkCanvas offscreen(bitmap);
1327 offscreen.clear(SK_ColorWHITE);
1328 SkPaint paint;
1329 offscreen.drawString("%", 1, 10, paint);
1330 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
1331 SkPixmap pixmap;
1332 if (image->peekPixels(&pixmap)) {
1333 const SkPMColor* pixels = pixmap.addr32();
1334 SkPMColor pmWhite = pixels[0];
1335 for (int y = 0; y < image->height(); ++y) {
1336 for (int x = 0; x < image->width(); ++x) {
1337 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
1338 }
1339 SkDebugf("\n");
1340 }
1341 }
1342#StdOut
1343------------
1344--xx----x---
1345-x--x--x----
1346-x--x--x----
1347-x--x-x-----
1348--xx-xx-xx--
1349-----x-x--x-
1350----x--x--x-
1351----x--x--x-
1352---x----xx--
1353------------
1354##
Cary Clarka560c472017-11-27 10:44:06 -05001355##
1356
Cary Clarkf5404bb2018-01-05 12:10:09 -05001357#SeeAlso readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001358
1359#Method ##
1360
1361# ------------------------------------------------------------------------------
1362
1363#Method GrTexture* getTexture() const
1364
Cary Clarkab2621d2018-01-30 10:08:57 -05001365#Line # Deprecated. ##
Cary Clark2f466242017-12-11 16:03:17 -05001366Deprecated.
1367
1368#Deprecated
1369##
1370
Cary Clarka560c472017-11-27 10:44:06 -05001371#Private
Cary Clarkf5404bb2018-01-05 12:10:09 -05001372Currently used by Canvas2DLayerBridge in Chromium.
Cary Clarka560c472017-11-27 10:44:06 -05001373##
1374
Cary Clarka560c472017-11-27 10:44:06 -05001375#Method ##
1376
1377# ------------------------------------------------------------------------------
1378
1379#Method bool isTextureBacked() const
1380
Cary Clarkab2621d2018-01-30 10:08:57 -05001381#Line # Returns if Image was created from GPU_Texture. ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001382Returns true the contents of Image was created on or uploaded to GPU memory,
1383and is available as a GPU_Texture.
Cary Clarka560c472017-11-27 10:44:06 -05001384
Cary Clarkf5404bb2018-01-05 12:10:09 -05001385#Return true if Image is a GPU_Texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001386
1387#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001388#Image 5
1389#Platform gpu
1390auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1391 if (nullptr == image) {
1392 return;
1393 }
1394 SkPaint paint;
1395 paint.setAntiAlias(true);
1396 paint.setTextAlign(SkPaint::kCenter_Align);
1397 canvas->drawImage(image, 0, 0);
1398 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1399 canvas->drawString(image->isTextureBacked() ? "is GPU texture" : "not GPU texture",
1400 image->width() / 2, image->height() * 3 / 4, paint);
1401};
1402sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1403sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1404 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1405drawImage(image, "image");
1406canvas->translate(image->width(), 0);
1407drawImage(bitmapImage, "source");
1408canvas->translate(-image->width(), image->height());
1409drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001410##
1411
Cary Clarkf5404bb2018-01-05 12:10:09 -05001412#SeeAlso MakeFromTexture isValid
Cary Clarka560c472017-11-27 10:44:06 -05001413
1414#Method ##
1415
1416# ------------------------------------------------------------------------------
1417
1418#Method bool isValid(GrContext* context) const
1419
Cary Clarkab2621d2018-01-30 10:08:57 -05001420#Line # Returns if Image can draw to Raster_Surface or GPU_Context. ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001421Returns true if Image can be drawn on either Raster_Surface or GPU_Surface.
1422If context is nullptr, tests if Image draws on Raster_Surface;
1423otherwise, tests if Image draws on GPU_Surface associated with context.
Cary Clarka560c472017-11-27 10:44:06 -05001424
Cary Clarkf5404bb2018-01-05 12:10:09 -05001425Image backed by GPU_Texture may become invalid if associated GrContext is
1426invalid. Lazy_Image may be invalid and may not draw to Raster_Surface or
1427GPU_Surface or both.
Cary Clarka560c472017-11-27 10:44:06 -05001428
Cary Clark61ca7c52018-01-02 11:34:14 -05001429#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05001430
Cary Clarkf5404bb2018-01-05 12:10:09 -05001431#Return true if Image can be drawn ##
Cary Clarka560c472017-11-27 10:44:06 -05001432
1433#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001434#Image 5
1435#Platform gpu
1436auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1437 if (nullptr == image) {
1438 return;
1439 }
1440 SkPaint paint;
1441 paint.setAntiAlias(true);
1442 paint.setTextAlign(SkPaint::kCenter_Align);
1443 canvas->drawImage(image, 0, 0);
1444 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1445 if (canvas->getGrContext()) {
1446 canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" :
1447 "not valid on GPU", image->width() / 2, image->height() * 5 / 8, paint);
1448 }
1449 canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" :
1450 "not valid on CPU", image->width() / 2, image->height() * 7 / 8, paint);
1451};
1452sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1453sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1454 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1455drawImage(image, "image");
1456canvas->translate(image->width(), 0);
1457drawImage(bitmapImage, "source");
1458canvas->translate(-image->width(), image->height());
1459drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001460##
1461
Cary Clarkf5404bb2018-01-05 12:10:09 -05001462#SeeAlso isTextureBacked isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001463
1464#Method ##
1465
1466# ------------------------------------------------------------------------------
1467
1468#Method GrBackendObject getTextureHandle(bool flushPendingGrContextIO,
1469 GrSurfaceOrigin* origin = nullptr) const
Cary Clarkab2621d2018-01-30 10:08:57 -05001470#Line # Returns GPU reference to Image as texture. ##
Cary Clarka560c472017-11-27 10:44:06 -05001471
Cary Clark2f466242017-12-11 16:03:17 -05001472Retrieves the back-end API handle of texture. If flushPendingGrContextIO is true,
1473complete deferred I/O operations.
Cary Clarka560c472017-11-27 10:44:06 -05001474
Cary Clark2f466242017-12-11 16:03:17 -05001475If origin in not nullptr, copies location of content drawn into Image.
Cary Clarka560c472017-11-27 10:44:06 -05001476
Cary Clark2f466242017-12-11 16:03:17 -05001477#Param flushPendingGrContextIO flag to flush outstanding requests ##
1478#Param origin storage for one of: kTopLeft_GrSurfaceOrigin,
1479 kBottomLeft_GrSurfaceOrigin; or nullptr
1480##
1481
Cary Clarkac47b882018-01-11 10:35:44 -05001482#Return back-end API texture handle, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001483
1484#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001485#Image 4
Cary Clark2f466242017-12-11 16:03:17 -05001486#Platform gpu
1487GrContext* context = canvas->getGrContext();
1488if (!context) {
1489 return;
1490}
1491SkPaint paint;
1492paint.setAntiAlias(true);
1493SkString str;
Cary Clarkac47b882018-01-11 10:35:44 -05001494int y = -10;
Cary Clark2f466242017-12-11 16:03:17 -05001495for (auto origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin } ) {
1496 sk_sp<SkImage> srcImage(SkImage::MakeFromTexture(context,
1497 backEndTexture, origin, kPremul_SkAlphaType, nullptr));
1498 GrSurfaceOrigin readBackOrigin;
1499 GrBackendObject readBackHandle = srcImage->getTextureHandle(false, &readBackOrigin);
Cary Clarkac47b882018-01-11 10:35:44 -05001500 str.printf("readBackHandle: 0x%x", readBackHandle);
1501 canvas->drawString(str, 5, y += 30, paint);
1502 canvas->drawImage(srcImage, 80, y += 10);
Cary Clark2f466242017-12-11 16:03:17 -05001503 str.printf("origin: k%s_GrSurfaceOrigin", readBackOrigin ? "BottomLeft" : "TopLeft");
Cary Clarkac47b882018-01-11 10:35:44 -05001504 canvas->drawString(str, 5, y += srcImage->height() + 10, paint);
Cary Clark2f466242017-12-11 16:03:17 -05001505}
Cary Clarka560c472017-11-27 10:44:06 -05001506##
1507
Cary Clarkac47b882018-01-11 10:35:44 -05001508#Example
1509#Image 5
1510#Platform gpu
1511 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1512 if (nullptr == image) {
1513 return;
1514 }
1515 SkPaint paint;
1516 paint.setAntiAlias(true);
1517 paint.setTextAlign(SkPaint::kCenter_Align);
1518 canvas->drawImage(image, 0, image->height() / 4);
1519 canvas->drawString(label, image->width() / 2, image->height() / 8, paint);
1520 GrSurfaceOrigin readBackOrigin;
1521 GrBackendObject readBackHandle = image->getTextureHandle(false, &readBackOrigin);
1522 canvas->drawString(readBackHandle ? "has readBackHandle" : "no readBackHandle",
1523 image->width() / 2, image->height() * 11 / 8, paint);
1524 };
1525 drawImage(image, "image");
1526 canvas->translate(image->width(), 0);
1527 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1528 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1529 drawImage(textureImage, "backEndTexture");
1530##
1531
1532#SeeAlso MakeFromTexture isTextureBacked
Cary Clarka560c472017-11-27 10:44:06 -05001533
1534#Method ##
1535
1536# ------------------------------------------------------------------------------
1537
1538#Enum CachingHint
1539
1540#Code
1541 enum CachingHint {
1542 kAllow_CachingHint,
1543 kDisallow_CachingHint,
1544 };
1545##
1546
Cary Clarkac47b882018-01-11 10:35:44 -05001547CachingHint selects whether Skia may internally cache Bitmaps generated by
1548decoding Image, or by copying Image from GPU to CPU. The default behavior
1549allows caching Bitmaps.
1550
1551Choose kDisallow_CachingHint if Image pixels are to be used only once, or
1552if Image pixels reside in a cache outside of Skia, or to reduce memory pressure.
1553
1554Choosing kAllow_CachingHint does not ensure that pixels will be cached.
1555Image pixels may not be cached if memory requirements are too large or
1556pixels are not accessible.
Cary Clarka560c472017-11-27 10:44:06 -05001557
1558#Const kAllow_CachingHint 0
Cary Clarkac47b882018-01-11 10:35:44 -05001559Allows Skia to internally cache decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001560##
1561#Const kDisallow_CachingHint 1
Cary Clarkac47b882018-01-11 10:35:44 -05001562Disallows Skia from internally caching decoded and copied pixels.
Cary Clarka560c472017-11-27 10:44:06 -05001563##
1564
Cary Clarkac47b882018-01-11 10:35:44 -05001565#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001566##
1567
Cary Clarkac47b882018-01-11 10:35:44 -05001568#SeeAlso readPixels scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001569
1570#Enum ##
1571
1572# ------------------------------------------------------------------------------
1573
1574#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1575 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const
Cary Clarkab2621d2018-01-30 10:08:57 -05001576#Line # Copies and converts pixels. ##
Cary Clarka560c472017-11-27 10:44:06 -05001577
Cary Clarkac47b882018-01-11 10:35:44 -05001578Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY),
1579and does not exceed Image (width(), height()).
1580
1581dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
1582destination. dstRowBytes specifics the gap from one destination row to the next.
1583Returns true if pixels are copied. Returns false if:
1584#List
1585# dstInfo.addr() equals nullptr ##
1586# dstRowBytes is less than dstInfo.minRowBytes ##
1587# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001588##
1589
Cary Clarkac47b882018-01-11 10:35:44 -05001590Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1591kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1592If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace must match.
1593If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType must
1594match. If Image Color_Space is nullptr, dstInfo.colorSpace must match. Returns
1595false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001596
Cary Clarkac47b882018-01-11 10:35:44 -05001597srcX and srcY may be negative to copy only top or left of source. Returns
1598false if width() or height() is zero or negative.
1599Returns false if
1600#Formula
1601abs(srcX) >= Image width()
1602##
1603, or if
1604#Formula
1605abs(srcY) >= Image height()
1606##
1607.
Cary Clarka560c472017-11-27 10:44:06 -05001608
Cary Clarkac47b882018-01-11 10:35:44 -05001609If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1610If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1611
1612#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1613#Param dstPixels destination pixel storage ##
1614#Param dstRowBytes destination row length ##
1615#Param srcX column index whose absolute value is less than width() ##
1616#Param srcY row index whose absolute value is less than height() ##
1617#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1618
1619#Return true if pixels are copied to dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001620
1621#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001622#Image 3
1623 canvas->scale(.5f, .5f);
1624 const int width = 32;
1625 const int height = 32;
1626 std::vector<int32_t> dstPixels;
1627 dstPixels.resize(height * width * 4);
1628 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1629 for (int y = 0; y < 512; y += height ) {
1630 for (int x = 0; x < 512; x += width ) {
1631 if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) {
1632 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
1633 SkBitmap bitmap;
1634 bitmap.installPixels(dstPixmap);
1635 canvas->drawBitmap(bitmap, 0, 0);
1636 }
1637 canvas->translate(48, 0);
1638 }
1639 canvas->translate(-16 * 48, 48);
1640 }
Cary Clarka560c472017-11-27 10:44:06 -05001641##
1642
Cary Clarkac47b882018-01-11 10:35:44 -05001643#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001644
1645#Method ##
1646
1647# ------------------------------------------------------------------------------
1648
1649#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY,
1650 CachingHint cachingHint = kAllow_CachingHint) const
1651
Cary Clarkac47b882018-01-11 10:35:44 -05001652Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and
1653does not exceed Image (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001654
Cary Clarkac47b882018-01-11 10:35:44 -05001655dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
1656and row bytes of destination. dst.rowBytes specifics the gap from one destination
1657row to the next. Returns true if pixels are copied. Returns false if:
1658#List
1659# dst pixel storage equals nullptr ##
1660# dst.rowBytes is less than SkImageInfo::minRowBytes ##
1661# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001662##
1663
Cary Clarkac47b882018-01-11 10:35:44 -05001664Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1665kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1666If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1667If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1668match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1669false if pixel conversion is not possible.
1670
1671srcX and srcY may be negative to copy only top or left of source. Returns
1672false if width() or height() is zero or negative.
1673Returns false if
1674#Formula
1675abs(srcX) >= Image width()
1676##
1677, or if
1678#Formula
1679abs(srcY) >= Image height()
1680##
1681.
1682
1683If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1684If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1685
1686#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1687#Param srcX column index whose absolute value is less than width() ##
1688#Param srcY row index whose absolute value is less than height() ##
1689#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1690
1691#Return true if pixels are copied to dst ##
1692
1693#Example
1694#Image 3
1695 std::vector<int32_t> srcPixels;
1696 int rowBytes = image->width() * 4;
1697 int quarterWidth = image->width() / 4;
1698 int quarterHeight = image->height() / 4;
1699 srcPixels.resize(image->height() * rowBytes);
1700 for (int y = 0; y < 4; ++y) {
1701 for (int x = 0; x < 4; ++x) {
1702 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1703 &srcPixels.front() + x * image->height() * quarterWidth +
1704 y * quarterWidth, rowBytes);
1705 image->readPixels(pixmap, x * quarterWidth, y * quarterHeight);
1706 }
1707 }
1708 canvas->scale(.5f, .5f);
1709 SkBitmap bitmap;
1710 bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()),
1711 &srcPixels.front(), rowBytes);
1712 canvas->drawBitmap(bitmap, 0, 0);
1713##
1714
1715#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001716
1717#Method ##
1718
1719# ------------------------------------------------------------------------------
1720
1721#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality,
1722 CachingHint cachingHint = kAllow_CachingHint) const
Cary Clarkab2621d2018-01-30 10:08:57 -05001723#Line # Scales and converts one Image to another. ##
Cary Clarka560c472017-11-27 10:44:06 -05001724
Cary Clarkac47b882018-01-11 10:35:44 -05001725Copies Image to dst, scaling pixels to fit dst.width() and dst.height(), and
1726converting pixels to match dst.colorType and dst.alphaType. Returns true if
1727pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1728less than dst SkImageInfo::minRowBytes.
Cary Clarka560c472017-11-27 10:44:06 -05001729
Cary Clarkac47b882018-01-11 10:35:44 -05001730Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1731kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1732If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1733If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1734match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1735false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001736
Cary Clarkac47b882018-01-11 10:35:44 -05001737Scales the image, with filterQuality, to match dst.width() and dst.height().
1738filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1739Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1740Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1741Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1742kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1743
1744If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1745If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1746
1747#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1748#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1749 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1750##
1751#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1752
1753#Return true if pixels are scaled to fit dst ##
Cary Clarka560c472017-11-27 10:44:06 -05001754
1755#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001756#Image 3
1757#Height 128
1758 std::vector<int32_t> srcPixels;
1759 int quarterWidth = image->width() / 16;
1760 int rowBytes = quarterWidth * 4;
1761 int quarterHeight = image->height() / 16;
1762 srcPixels.resize(quarterHeight * rowBytes);
1763 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1764 &srcPixels.front(), rowBytes);
1765 canvas->scale(4, 4);
1766 SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality,
1767 kMedium_SkFilterQuality, kHigh_SkFilterQuality };
1768 for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) {
1769 image->scalePixels(pixmap, qualities[index]);
1770 sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
1771 canvas->drawImage(filtered, 16 * index, 0);
1772 }
Cary Clarka560c472017-11-27 10:44:06 -05001773##
1774
Cary Clarkac47b882018-01-11 10:35:44 -05001775#SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001776
1777#Method ##
1778
1779# ------------------------------------------------------------------------------
1780
1781#Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const
1782
Cary Clarkab2621d2018-01-30 10:08:57 -05001783#Line # Returns encoded Image as SkData. ##
Cary Clarkac47b882018-01-11 10:35:44 -05001784Encodes Image pixels, returning result as SkData.
Cary Clark2f466242017-12-11 16:03:17 -05001785
Cary Clarkac47b882018-01-11 10:35:44 -05001786Returns nullptr if encoding fails, or if encodedImageFormat is not supported.
Cary Clarka560c472017-11-27 10:44:06 -05001787
Cary Clarkac47b882018-01-11 10:35:44 -05001788Image encoding in a format requires both building with one or more of:
1789SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support
1790for the encoded format.
1791
1792If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can
1793additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP,
1794SkEncodedImageFormat::kGIF.
1795
1796quality is a platform and format specific metric trading off size and encoding
1797error. When used, quality equaling 100 encodes with the least error. quality may
1798be ignored by the encoder.
1799
1800#Param encodedImageFormat one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
1801 SkEncodedImageFormat::kWEBP
1802 ##
1803#Param quality encoder specific metric with 100 equaling best ##
Cary Clarka560c472017-11-27 10:44:06 -05001804
Cary Clark2f466242017-12-11 16:03:17 -05001805#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001806
1807#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001808#Image 3
1809 canvas->scale(4, 4);
1810 SkIRect subset = {0, 0, 16, 64};
1811 int x = 0;
1812 for (int quality : { 0, 10, 50, 100 } ) {
1813 sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
1814 sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
1815 canvas->drawImage(filtered, x, 0);
1816 x += 16;
1817 }
Cary Clarka560c472017-11-27 10:44:06 -05001818##
1819
Cary Clarkac47b882018-01-11 10:35:44 -05001820#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001821
1822#Method ##
1823
1824# ------------------------------------------------------------------------------
1825
Cary Clark61ca7c52018-01-02 11:34:14 -05001826#Method sk_sp<SkData> encodeToData() const
Cary Clarka560c472017-11-27 10:44:06 -05001827
Cary Clarkac47b882018-01-11 10:35:44 -05001828Encodes Image pixels, returning result as SkData. Returns existing encoded data
1829if present; otherwise, Image is encoded with SkEncodedImageFormat::kPNG. Skia
1830must be built with SK_HAS_PNG_LIBRARY to encode Image.
Cary Clarka560c472017-11-27 10:44:06 -05001831
Cary Clarkac47b882018-01-11 10:35:44 -05001832Returns nullptr if existing encoded data is missing or invalid, and
Cary Clarka560c472017-11-27 10:44:06 -05001833encoding fails.
1834
Cary Clarkac47b882018-01-11 10:35:44 -05001835#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001836
1837#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001838#Image 3
1839 canvas->scale(4, 4);
1840 SkIRect subset = {136, 32, 200, 96};
1841 sk_sp<SkData> data(image->encodeToData());
1842 sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
1843 canvas->drawImage(eye, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001844##
1845
Cary Clarkac47b882018-01-11 10:35:44 -05001846#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001847
1848#Method ##
1849
1850# ------------------------------------------------------------------------------
1851
1852#Method sk_sp<SkData> refEncodedData() const
1853
Cary Clarkab2621d2018-01-30 10:08:57 -05001854#Line # Returns Image encoded in SkData if present. ##
Cary Clarkac47b882018-01-11 10:35:44 -05001855Returns encoded Image pixels as SkData, if Image was created from supported
1856encoded stream format. Platform support for formats vary and may require building
1857with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY.
Cary Clarka560c472017-11-27 10:44:06 -05001858
Cary Clarkac47b882018-01-11 10:35:44 -05001859Returns nullptr if Image contents are not encoded.
Cary Clarka560c472017-11-27 10:44:06 -05001860
Cary Clarkac47b882018-01-11 10:35:44 -05001861#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001862
1863#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001864#Image 3
1865#Platform gpu
1866 struct {
1867 const char* name;
1868 sk_sp<SkImage> image;
1869 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1870 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1871 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr) } };
1872 SkString string;
1873 SkPaint paint;
1874 for (const auto& test : tests ) {
1875 if (!test.image) {
1876 string.printf("no %s", test.name);
1877 } else {
1878 string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name);
1879 }
1880 canvas->drawString(string, 10, 20, paint);
1881 canvas->translate(0, 20);
1882 }
Cary Clarka560c472017-11-27 10:44:06 -05001883##
1884
Cary Clarkac47b882018-01-11 10:35:44 -05001885#SeeAlso encodeToData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001886
1887#Method ##
1888
1889# ------------------------------------------------------------------------------
1890
1891#Method const char* toString(SkString* string) const
1892
Cary Clarkab2621d2018-01-30 10:08:57 -05001893#Line # Converts Image to machine readable form. ##
Cary Clarkac47b882018-01-11 10:35:44 -05001894Appends Image description to string, including unique ID, width, height, and
1895whether the image is opaque.
Cary Clarka560c472017-11-27 10:44:06 -05001896
Cary Clarkac47b882018-01-11 10:35:44 -05001897#Param string storage for description; existing content is preserved ##
1898
1899#Return string appended with Image description ##
Cary Clarka560c472017-11-27 10:44:06 -05001900
1901#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001902#Image 4
1903 struct {
1904 const char* name;
1905 sk_sp<SkImage> image;
1906 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1907 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
1908 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr) } };
1909 SkString string;
1910 SkPaint paint;
1911 for (const auto& test : tests ) {
1912 string.printf("%s: ", test.name);
1913 test.image ? (void) test.image->toString(&string) : string.append("no image");
1914 canvas->drawString(string, 10, 20, paint);
1915 canvas->translate(0, 20);
1916 }
Cary Clarka560c472017-11-27 10:44:06 -05001917##
1918
Cary Clarkac47b882018-01-11 10:35:44 -05001919#SeeAlso SkPaint::toString
Cary Clarka560c472017-11-27 10:44:06 -05001920
1921#Method ##
1922
1923# ------------------------------------------------------------------------------
1924
1925#Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const
1926
Cary Clarkab2621d2018-01-30 10:08:57 -05001927#Line # Creates Image containing part of original. ##
Cary Clarkac47b882018-01-11 10:35:44 -05001928Returns subset of Image. subset must be fully contained by Image dimensions().
1929The implementation may share pixels, or may copy them.
Cary Clarka560c472017-11-27 10:44:06 -05001930
Cary Clarkac47b882018-01-11 10:35:44 -05001931Returns nullptr if subset is empty, or subset is not contained by bounds, or
1932pixels in Image could not be read or copied.
Cary Clarka560c472017-11-27 10:44:06 -05001933
Cary Clarkac47b882018-01-11 10:35:44 -05001934#Param subset bounds of returned Image ##
1935
1936#Return partial or full Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001937
1938#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001939#Image 3
1940 canvas->scale(.5f, .5f);
1941 const int width = 32;
1942 const int height = 32;
1943 for (int y = 0; y < 512; y += height ) {
1944 for (int x = 0; x < 512; x += width ) {
1945 sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height}));
1946 canvas->drawImage(subset, x * 3 / 2, y * 3 / 2);
1947 }
1948 }
Cary Clarka560c472017-11-27 10:44:06 -05001949##
1950
Cary Clarkac47b882018-01-11 10:35:44 -05001951#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001952
1953#Method ##
1954
1955# ------------------------------------------------------------------------------
1956
1957#Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace) const
1958
Cary Clarkab2621d2018-01-30 10:08:57 -05001959#Line # Creates Image matching Color_Space if possible. ##
Cary Clarkac47b882018-01-11 10:35:44 -05001960Returns Image backed by GPU_Texture associated with context. Returned Image is
1961compatible with Surface created with dstColorSpace. Returns original
1962Image if context and dstColorSpace match.
1963
1964Returns nullptr if context is nullptr, or if Image was created with another
1965GrContext.
Cary Clarka560c472017-11-27 10:44:06 -05001966
Cary Clark61ca7c52018-01-02 11:34:14 -05001967#Param context GPU_Context ##
Cary Clarkac47b882018-01-11 10:35:44 -05001968#Param dstColorSpace range of colors of matching Surface on GPU ##
Cary Clarka560c472017-11-27 10:44:06 -05001969
Cary Clarkac47b882018-01-11 10:35:44 -05001970#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001971
1972#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001973#Platform gpu
1974#Image 5
1975 auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void {
1976 if (nullptr == image || nullptr == context) {
1977 return;
1978 }
1979 SkPaint paint;
1980 paint.setAntiAlias(true);
1981 paint.setTextAlign(SkPaint::kCenter_Align);
1982 sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr));
1983 canvas->drawImage(texture, 0, 0);
1984 canvas->drawString(label, texture->width() / 2, texture->height() / 4, paint);
1985 };
1986 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1987 GrContext* context = canvas->getGrContext();
1988 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture,
1989 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
1990 drawImage(image, context, "image");
1991 canvas->translate(image->width(), 0);
1992 drawImage(bitmapImage, context, "source");
1993 canvas->translate(-image->width(), image->height());
1994 drawImage(textureImage, context, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001995##
1996
Cary Clarkac47b882018-01-11 10:35:44 -05001997#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05001998
1999#Method ##
2000
2001# ------------------------------------------------------------------------------
2002
2003#Method sk_sp<SkImage> makeNonTextureImage() const
2004
Cary Clarkab2621d2018-01-30 10:08:57 -05002005#Line # Creates Image without dependency on GPU_Texture. ##
Cary Clarkac47b882018-01-11 10:35:44 -05002006Returns Raster_Image or Lazy_Image. Copies Image backed by GPU_Texture into
2007CPU memory if needed. Returns original Image if unencoded in Raster_Bitmap,
2008or if encoded in a stream.
Cary Clark61ca7c52018-01-02 11:34:14 -05002009
Cary Clarkac47b882018-01-11 10:35:44 -05002010Returns nullptr if backed by GPU_Texture and copy fails.
2011
2012#Return Raster_Image, Lazy_Image, or nullptr ##
Cary Clark61ca7c52018-01-02 11:34:14 -05002013
2014#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002015#Image 5
2016#Platform gpu
2017 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2018 if (nullptr == image) {
2019 return;
2020 }
2021 SkPaint paint;
2022 paint.setAntiAlias(true);
2023 paint.setTextAlign(SkPaint::kCenter_Align);
2024 sk_sp<SkImage> nonTexture(image->makeNonTextureImage());
2025 canvas->drawImage(nonTexture, 0, 0);
2026 canvas->drawString(label, nonTexture->width() / 2, nonTexture->height() / 4, paint);
2027 };
2028 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2029 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2030 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2031 drawImage(image, "image");
2032 canvas->translate(image->width(), 0);
2033 drawImage(bitmapImage, "source");
2034 canvas->translate(-image->width(), image->height());
2035 drawImage(textureImage, "backEndTexture");
Cary Clark61ca7c52018-01-02 11:34:14 -05002036##
2037
2038#SeeAlso incomplete
2039
2040#Method ##
2041
2042# ------------------------------------------------------------------------------
2043
2044#Method sk_sp<SkImage> makeRasterImage() const
2045
Cary Clarkab2621d2018-01-30 10:08:57 -05002046#Line # Creates Image compatible with Raster_Surface if possible. ##
Cary Clarkac47b882018-01-11 10:35:44 -05002047Returns Raster_Image. Copies Image backed by GPU_Texture into CPU memory,
2048or decodes Image from Lazy_Image. Returns original Image if unencoded in
2049Raster_Bitmap.
Cary Clarka560c472017-11-27 10:44:06 -05002050
Cary Clarkac47b882018-01-11 10:35:44 -05002051Returns nullptr if copy, decode, or pixel read fails.
Cary Clarka560c472017-11-27 10:44:06 -05002052
Cary Clarkac47b882018-01-11 10:35:44 -05002053#Return Raster_Image, or nullptr ##
2054
2055#Bug 7479 ##
Cary Clarka560c472017-11-27 10:44:06 -05002056#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002057#Image 5
2058#Platform gpu
2059 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2060 if (nullptr == image) {
2061 return;
2062 }
2063 SkPaint paint;
2064 paint.setAntiAlias(true);
2065 paint.setTextAlign(SkPaint::kCenter_Align);
2066 sk_sp<SkImage> raster(image->makeRasterImage());
2067 canvas->drawImage(raster, 0, 0);
2068 canvas->drawString(label, raster->width() / 2, raster->height() / 4, paint);
2069 };
2070 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2071 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2072 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2073 drawImage(image, "image");
2074 canvas->translate(image->width(), 0);
2075 drawImage(bitmapImage, "source");
2076 canvas->translate(-image->width(), image->height());
2077 drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05002078##
2079
Cary Clarkac47b882018-01-11 10:35:44 -05002080#SeeAlso isTextureBacked isLazyGenerated MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -05002081
2082#Method ##
2083
2084# ------------------------------------------------------------------------------
2085
2086#Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
2087 const SkIRect& clipBounds, SkIRect* outSubset,
2088 SkIPoint* offset) const
Cary Clarkab2621d2018-01-30 10:08:57 -05002089#Line # Creates filtered, clipped Image. ##
Cary Clarka560c472017-11-27 10:44:06 -05002090
Cary Clarkac47b882018-01-11 10:35:44 -05002091Creates filtered Image. filter processes original Image, potentially changing
2092color, position, and size. subset is the bounds of original Image processed
2093by filter. clipBounds is the expected bounds of the filtered Image. outSubset
2094is required storage for the actual bounds of the filtered Image. offset is
2095required storage for translation of returned Image.
Cary Clarka560c472017-11-27 10:44:06 -05002096
Cary Clarkac47b882018-01-11 10:35:44 -05002097Returns nullptr if Image could not be created. If nullptr is returned, outSubset
2098and offset are undefined.
2099
2100makeWithFilter is optimized to support Image backed by GPU_Texture drawn in an
2101animation with SkImageFilter that vary in size from one frame to the next. The
2102created Image is drawn at an increased size so that GPU_Texture can be reused
2103with different sized effects. outSubset describes the valid bounds of GPU_Texture
2104returned. The returned Image may be much larger than required for the filter.
2105offset translates the returned Image to keep subsequent animation frames
2106aligned with respect to each other.
2107
2108#Param filter how Image is sampled when transformed ##
Cary Clarka560c472017-11-27 10:44:06 -05002109#Param subset incomplete ##
2110#Param clipBounds incomplete ##
2111#Param outSubset incomplete ##
2112#Param offset incomplete ##
2113
Cary Clarkac47b882018-01-11 10:35:44 -05002114#Return filtered Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05002115
2116#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002117#Description
2118In each frame of the animation, filtered Image is drawn in a different location.
2119By translating canvas by returned offset, Image appears stationary.
2120##
2121#Image 5
2122#Platform gpu
2123#Duration 5
2124 sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make(
2125 -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE,
2126 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
2127 nullptr);
2128 sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr);
2129 SkIRect subset = image->bounds();
2130 SkIRect clipBounds = image->bounds();
2131 clipBounds.outset(60, 60);
2132 SkIRect outSubset;
2133 SkIPoint offset;
2134 sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds,
2135 &outSubset, &offset));
2136 SkPaint paint;
2137 paint.setAntiAlias(true);
2138 paint.setStyle(SkPaint::kStroke_Style);
2139 canvas->drawLine(0, 0, offset.fX, offset.fY, paint);
2140 canvas->translate(offset.fX, offset.fY);
2141 canvas->drawImage(filtered, 0, 0);
2142 canvas->drawRect(SkRect::MakeFromIRect(outSubset), paint);
Cary Clarka560c472017-11-27 10:44:06 -05002143##
2144
Cary Clarkac47b882018-01-11 10:35:44 -05002145#SeeAlso SkPaint::setImageFilter
Cary Clarka560c472017-11-27 10:44:06 -05002146
2147#Method ##
2148
2149# ------------------------------------------------------------------------------
2150
Cary Clark61ca7c52018-01-02 11:34:14 -05002151#Struct DeferredTextureImageUsageParams
Cary Clark08895c42018-02-01 09:37:32 -05002152#Line # to be deprecated ##
Cary Clark61ca7c52018-01-02 11:34:14 -05002153
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
Cary Clarkab2621d2018-01-30 10:08:57 -05002206#Line # To be deprecated. ##
Cary Clark61ca7c52018-01-02 11:34:14 -05002207
Cary Clarkac47b882018-01-11 10:35:44 -05002208Used only by Chrome; to be deprecated.
2209
Cary Clark61ca7c52018-01-02 11:34:14 -05002210This method allows clients to capture the data necessary to turn a SkImage into a texture-
2211backed image. If the original image is codec-backed this will decode into a format optimized
2212for the context represented by the proxy. This method is thread safe with respect to the
2213GrContext whence the proxy came. Clients allocate and manage the storage of the deferred
2214texture data and control its lifetime. No cleanup is required, thus it is safe to simply free
2215the memory out from under the data.
2216
2217The same method is used both for getting the size necessary for pre-uploaded texture data
2218and for retrieving the data. The params array represents the set of draws over which to
2219optimize the pre-upload data.
2220
2221When called with a null buffer this returns the size that the client must allocate in order
2222to create deferred texture data for this image (or zero if this is an inappropriate
2223candidate). The buffer allocated by the client should be 8 byte aligned.
2224
2225When buffer is not null this fills in the deferred texture data for this image in the
2226provided buffer (assuming this is an appropriate candidate image and the buffer is
2227appropriately aligned). Upon success the size written is returned, otherwise 0.
2228
Cary Clarkac47b882018-01-11 10:35:44 -05002229dstColorSpace is the Color_Space of the surface where this texture will ultimately be used.
Cary Clark61ca7c52018-01-02 11:34:14 -05002230If the method determines that mip-maps are needed, this helps determine the correct strategy
2231for building them (gamma-correct or not).
2232
2233dstColorType is the color type of the surface where this texture will ultimately be used.
2234This determines the format with which the image will be uploaded to the GPU. If dstColorType
Cary Clarkac47b882018-01-11 10:35:44 -05002235does not support Color_Space (low bit depth types such as kARGB_4444_SkColorType),
Cary Clark61ca7c52018-01-02 11:34:14 -05002236then dstColorSpace must be null.
2237
Cary Clarkac47b882018-01-11 10:35:44 -05002238#Param contextThreadSafeProxy thread safe GPU context ##
2239#Param deferredTextureImageUsageParams array of Image transformations ##
2240#Param paramCnt entries in deferredTextureImageUsageParams array ##
2241#Param buffer storage for GPU_Texture data, or nullptr ##
2242#Param dstColorSpace Surface Color_Space, or nullptr ##
2243#Param dstColorType Surface Color_Type ##
Cary Clark61ca7c52018-01-02 11:34:14 -05002244
Cary Clarkac47b882018-01-11 10:35:44 -05002245#Return size of storage for GPU_Texture data ##
Cary Clark61ca7c52018-01-02 11:34:14 -05002246
2247#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002248#Image 5
2249#Platform gpu
2250 GrContext* context = canvas->getGrContext();
2251 if (!context) {
2252 return;
2253 }
2254 sk_sp<GrContextThreadSafeProxy> proxy(context->threadSafeProxy());
2255 auto params = SkImage::DeferredTextureImageUsageParams(SkMatrix::MakeScale(2, 2),
2256 kNone_SkFilterQuality, 0);
2257 SkColorSpace* colorSpace = canvas->imageInfo().colorSpace();
2258 size_t requiredMemoryInBytes = image->getDeferredTextureImageData(
2259 *proxy, &params, 1, nullptr, colorSpace);
2260 std::vector<uint8_t> memory;
2261 memory.resize(requiredMemoryInBytes);
2262 image->getDeferredTextureImageData(*proxy, &params, 1, memory.data(), colorSpace);
2263 sk_sp<SkImage> uploadedEncodedImage = SkImage::MakeFromDeferredTextureImageData(
2264 context, memory.data(), SkBudgeted::kNo);
2265 canvas->scale(2, 2);
2266 canvas->drawImage(uploadedEncodedImage, 10, 10);
Cary Clark61ca7c52018-01-02 11:34:14 -05002267##
2268
Cary Clarkac47b882018-01-11 10:35:44 -05002269#SeeAlso MakeFromDeferredTextureImageData
Cary Clark61ca7c52018-01-02 11:34:14 -05002270
2271##
2272
2273#Method static sk_sp<SkImage> MakeFromDeferredTextureImageData(GrContext* context, const void* data,
2274 SkBudgeted budgeted)
Cary Clarkab2621d2018-01-30 10:08:57 -05002275#Line # To be deprecated. ##
Cary Clark61ca7c52018-01-02 11:34:14 -05002276
Cary Clarkac47b882018-01-11 10:35:44 -05002277Used only by Chrome; to be deprecated.
2278
Cary Clark61ca7c52018-01-02 11:34:14 -05002279Returns a texture-backed image from data produced in SkImage::getDeferredTextureImageData.
2280The context must be the context that provided the proxy passed to
2281getDeferredTextureImageData.
2282
2283#Param context GPU_Context ##
2284#Param data incomplete ##
2285#Param budgeted incomplete ##
2286
2287#Return incomplete ##
2288
2289#Example
2290// incomplete
2291##
2292
2293#SeeAlso incomplete
2294
2295##
2296
2297# ------------------------------------------------------------------------------
2298
Cary Clarka560c472017-11-27 10:44:06 -05002299#Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc
2300
2301##
2302
2303# ------------------------------------------------------------------------------
2304
2305#Method static bool MakeBackendTextureFromSkImage(GrContext* context,
2306 sk_sp<SkImage> image,
2307 GrBackendTexture* backendTexture,
2308 BackendTextureReleaseProc* backendTextureReleaseProc)
Cary Clarkab2621d2018-01-30 10:08:57 -05002309#Line # Creates GPU_Texture from Image. ##
Cary Clarka560c472017-11-27 10:44:06 -05002310
2311Creates a GrBackendTexture from the provided SkImage. Returns true on success. The
2312GrBackendTexture and BackendTextureReleaseProc are populated on success. It is the callers
2313responsibility to call the BackendTextureReleaseProc once they have deleted the texture.
2314Note that the BackendTextureReleaseProc allows Skia to clean up auxiliary data related
2315to the GrBackendTexture, and is not a substitute for the client deleting the GrBackendTexture
2316themselves.
2317
2318If image is both texture backed and singly referenced; that is, its only
2319reference was transferred using std::move(): image is returned in backendTexture
2320without conversion or making a copy.
2321
2322If the SkImage is not texture backed, this function will generate a texture with the image's
2323contents and return that.
2324
Cary Clark61ca7c52018-01-02 11:34:14 -05002325#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05002326#Param image incomplete ##
2327#Param backendTexture incomplete ##
2328#Param backendTextureReleaseProc incomplete ##
2329
2330#Return incomplete ##
2331
2332#Example
2333// incomplete
2334##
2335
2336#SeeAlso incomplete
2337
2338#Method ##
2339
2340# ------------------------------------------------------------------------------
2341
2342#Enum LegacyBitmapMode
2343
2344#Code
2345 enum LegacyBitmapMode {
2346 kRO_LegacyBitmapMode,
2347 kRW_LegacyBitmapMode,
2348 };
2349##
2350
2351Helper functions to convert to SkBitmap
2352
2353#Const kRO_LegacyBitmapMode 0
2354##
2355#Const kRW_LegacyBitmapMode 1
2356##
2357
2358#Example
2359// incomplete
2360##
2361
2362#SeeAlso incomplete
2363
2364#Enum ##
2365
2366# ------------------------------------------------------------------------------
2367
2368#Method bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode) const
2369
Cary Clarkab2621d2018-01-30 10:08:57 -05002370#Line # Returns as Raster_Bitmap. ##
Cary Clarkac47b882018-01-11 10:35:44 -05002371Creates raster Bitmap with same pixels as Image. If legacyBitmapMode is
2372kRO_LegacyBitmapMode, returned bitmap is read-only and immutable.
2373Returns true if Bitmap is stored in bitmap. Returns false and resets bitmap if
2374Bitmap write did not succeed.
Cary Clarka560c472017-11-27 10:44:06 -05002375
Cary Clark3cd22cc2017-12-01 11:49:58 -05002376#Param bitmap storage for legacy Bitmap ##
2377#Param legacyBitmapMode one of: kRO_LegacyBitmapMode, kRW_LegacyBitmapMode ##
Cary Clarka560c472017-11-27 10:44:06 -05002378
Cary Clark3cd22cc2017-12-01 11:49:58 -05002379#Return true if Bitmap was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002380
2381#Example
2382// incomplete
2383##
2384
2385#SeeAlso incomplete
2386
2387#Method ##
2388
2389# ------------------------------------------------------------------------------
2390
2391#Method bool isLazyGenerated() const
2392
Cary Clarkab2621d2018-01-30 10:08:57 -05002393#Line # Returns if Image is created as needed. ##
Cary Clarka560c472017-11-27 10:44:06 -05002394Returns true if Image is backed by an image-generator or other service that creates
2395and caches its pixels or texture on-demand.
2396
Cary Clark2f466242017-12-11 16:03:17 -05002397#Return true if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002398
2399#Example
Cary Clark2f466242017-12-11 16:03:17 -05002400#Height 80
2401#Function
2402class TestImageGenerator : public SkImageGenerator {
2403public:
2404 TestImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(10, 10)) {}
2405 ~TestImageGenerator() override {}
2406protected:
2407 bool onGetPixels(const SkImageInfo& info, void* pixelPtr, size_t rowBytes,
2408 const Options& options) override {
2409 SkPMColor* pixels = static_cast<SkPMColor*>(pixelPtr);
2410 for (int y = 0; y < info.height(); ++y) {
2411 for (int x = 0; x < info.width(); ++x) {
2412 pixels[y * info.width() + x] = 0xff223344 + y * 0x000C0811;
2413 }
2414 }
2415 return true;
2416 }
2417};
2418##
2419void draw(SkCanvas* canvas) {
2420 auto gen = std::unique_ptr<TestImageGenerator>(new TestImageGenerator());
2421 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
2422 SkString lazy(image->isLazyGenerated() ? "is lazy" : "not lazy");
2423 canvas->scale(8, 8);
2424 canvas->drawImage(image, 0, 0, nullptr);
2425 SkPaint paint;
2426 paint.setTextSize(4);
2427 canvas->drawString(lazy, 2, 5, paint);
2428}
Cary Clarka560c472017-11-27 10:44:06 -05002429##
2430
Cary Clarkf5404bb2018-01-05 12:10:09 -05002431#Example
2432#Image 5
2433#Platform gpu
2434void draw(SkCanvas* canvas) {
2435 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2436 if (nullptr == image) {
2437 return;
2438 }
2439 SkPaint paint;
2440 paint.setAntiAlias(true);
2441 paint.setTextAlign(SkPaint::kCenter_Align);
2442 canvas->drawImage(image, 0, 0);
2443 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
2444 canvas->drawString(
2445 image->isLazyGenerated() ? "is lazily generated" : "not lazily generated",
2446 image->width() / 2, image->height() * 3 / 4, paint);
2447 };
2448 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2449 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
2450 kTopLeft_GrSurfaceOrigin, kOpaque_SkAlphaType, nullptr));
2451 drawImage(image, "image");
2452 canvas->translate(image->width(), 0);
2453 drawImage(bitmapImage, "source");
2454 canvas->translate(-image->width(), image->height());
2455 drawImage(textureImage, "backEndTexture");
2456}
2457##
2458
Cary Clarkac47b882018-01-11 10:35:44 -05002459#SeeAlso isTextureBacked MakeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002460
2461#Method ##
2462
2463# ------------------------------------------------------------------------------
2464
2465#Method sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target,
2466 SkTransferFunctionBehavior premulBehavior) const
Cary Clarkab2621d2018-01-30 10:08:57 -05002467#Line # Creates Image matching Color_Space if possible. ##
Cary Clarka560c472017-11-27 10:44:06 -05002468
Cary Clarkac47b882018-01-11 10:35:44 -05002469Creates Image in target Color_Space.
2470Returns nullptr if Image could not be created.
Cary Clarka560c472017-11-27 10:44:06 -05002471
Cary Clarkac47b882018-01-11 10:35:44 -05002472Returns original Image if it is in target Color_Space.
2473Otherwise, converts pixels from Image Color_Space to target Color_Space.
2474If Image colorSpace returns nullptr, Image Color_Space is assumed to be sRGB.
2475
2476SkTransferFunctionBehavior is to be deprecated.
2477
2478Set premulBehavior to SkTransferFunctionBehavior::kRespect to convert Image
2479pixels to a linear space, before converting to destination Color_Type
Cary Clarka560c472017-11-27 10:44:06 -05002480and Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -05002481
Cary Clarkac47b882018-01-11 10:35:44 -05002482Set premulBehavior to SkTransferFunctionBehavior::kIgnore to treat Image
2483pixels as linear, when converting to destination Color_Type
2484and Color_Space, ignoring pixel encoding.
Cary Clarka560c472017-11-27 10:44:06 -05002485
Cary Clarkac47b882018-01-11 10:35:44 -05002486#Param target Color_Space describing color range of returned Image ##
2487#Param premulBehavior one of: SkTransferFunctionBehavior::kRespect,
2488 SkTransferFunctionBehavior::kIgnore
Cary Clarka560c472017-11-27 10:44:06 -05002489##
2490
Cary Clarkac47b882018-01-11 10:35:44 -05002491#Return created Image in target Color_Space ##
2492
2493#Example
2494#Image 5
2495#Set sRGB
2496 sk_sp<SkColorSpace> normalColorSpace = SkColorSpace::MakeRGB(
2497 SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kSRGB_Gamut);
2498 sk_sp<SkColorSpace> wackyColorSpace = normalColorSpace->makeColorSpin();
2499 for (auto colorSpace : { normalColorSpace, wackyColorSpace } ) {
2500 for (auto transfer : { SkTransferFunctionBehavior::kRespect,
2501 SkTransferFunctionBehavior::kIgnore } ) {
2502 sk_sp<SkImage> colorSpaced = image->makeColorSpace(colorSpace, transfer);
2503 canvas->drawImage(colorSpaced, 0, 0);
2504 canvas->translate(128, 0);
2505 }
2506 canvas->translate(-256, 128);
2507 }
2508##
2509
2510#SeeAlso MakeFromPixture MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05002511
2512#Method ##
2513
2514#Class SkImage ##
2515
2516#Topic Image ##