blob: 61d9835b4bf409accf5a237a663df4062d1d0815 [file] [log] [blame]
Cary Clarka560c472017-11-27 10:44:06 -05001#Topic Image
Cary Clark137b8742018-05-30 09:21:49 -04002#Alias Image_Reference ##
Cary Clarka560c472017-11-27 10:44:06 -05003
4#Class SkImage
5
Cary Clark61313f32018-10-08 14:57:48 -04006#Code
7#Populate
8##
9
Cary Clark61ca7c52018-01-02 11:34:14 -050010Image describes a two dimensional array of pixels to draw. The pixels may be
Cary Clark4855f782018-02-06 09:41:53 -050011decoded in a Raster_Bitmap, encoded in a Picture or compressed data stream,
Cary Clark61ca7c52018-01-02 11:34:14 -050012or located in GPU memory as a GPU_Texture.
13
14Image cannot be modified after it is created. Image may allocate additional
15storage as needed; for instance, an encoded Image may decode when drawn.
16
17Image width and height are greater than zero. Creating an Image with zero width
18or height returns Image equal to nullptr.
19
20Image may be created from Bitmap, Pixmap, Surface, Picture, encoded streams,
21GPU_Texture, YUV_ColorSpace data, or hardware buffer. Encoded streams supported
Cary Clark4855f782018-02-06 09:41:53 -050022include BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP. Supported encoding details
Cary Clark61ca7c52018-01-02 11:34:14 -050023vary with platform.
24
Cary Clark08895c42018-02-01 09:37:32 -050025#Subtopic Raster_Image
Cary Clark137b8742018-05-30 09:21:49 -040026#Alias Raster_Image ##
Cary Clark4855f782018-02-06 09:41:53 -050027#Line # pixels decoded in Raster_Bitmap ##
28Raster_Image pixels are decoded in a Raster_Bitmap. These pixels may be read
Cary Clark61ca7c52018-01-02 11:34:14 -050029directly and in most cases written to, although edited pixels may not be drawn
30if Image has been copied internally.
31##
32
Cary Clark08895c42018-02-01 09:37:32 -050033#Subtopic Texture_Image
34#Line # pixels located on GPU ##
Cary Clark61ca7c52018-01-02 11:34:14 -050035Texture_Image are located on GPU and pixels are not accessible. Texture_Image
36are allocated optimally for best performance. Raster_Image may
37be drawn to GPU_Surface, but pixels are uploaded from CPU to GPU downgrading
38performance.
39##
40
Cary Clark08895c42018-02-01 09:37:32 -050041#Subtopic Lazy_Image
42#Line # deferred pixel buffer ##
Cary Clark61ca7c52018-01-02 11:34:14 -050043Lazy_Image defer allocating buffer for Image pixels and decoding stream until
44Image is drawn. Lazy_Image caches result if possible to speed up repeated
45drawing.
46##
Cary Clarka560c472017-11-27 10:44:06 -050047
Cary Clarka560c472017-11-27 10:44:06 -050048# ------------------------------------------------------------------------------
49
50#Method static sk_sp<SkImage> MakeRasterCopy(const SkPixmap& pixmap)
Cary Clark61313f32018-10-08 14:57:48 -040051#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -050052#Line # creates Image from Pixmap and copied pixels ##
Cary Clark2f466242017-12-11 16:03:17 -050053Creates Image from Pixmap and copy of pixels. Since pixels are copied, Pixmap
54pixels may be modified or deleted without affecting Image.
Cary Clarka560c472017-11-27 10:44:06 -050055
Cary Clark3cd22cc2017-12-01 11:49:58 -050056Image is returned if Pixmap is valid. Valid Pixmap parameters include:
57dimensions are greater than zero;
58each dimension fits in 29 bits;
59Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
60row bytes are large enough to hold one row of pixels;
61pixel address is not nullptr.
62
63#Param pixmap Image_Info, pixel address, and row bytes ##
64
65#Return copy of Pixmap pixels, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -050066
67#Example
Cary Clark2f466242017-12-11 16:03:17 -050068#Height 50
69#Description
70Draw a five by five bitmap, and draw a copy in an Image. Editing the pixmap
71alters the bitmap draw, but does not alter the Image draw since the Image
72contains a copy of the pixels.
73##
74 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
75 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
76 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
77 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
78 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
79 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
80 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
81 SkBitmap bitmap;
82 bitmap.installPixels(pixmap);
83 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
84 *pixmap.writable_addr8(2, 2) = 0x00;
85 canvas->scale(10, 10);
86 canvas->drawBitmap(bitmap, 0, 0);
87 canvas->drawImage(image, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -050088##
89
Cary Clark3cd22cc2017-12-01 11:49:58 -050090#SeeAlso MakeRasterData MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -050091
92#Method ##
93
94# ------------------------------------------------------------------------------
95
Cary Clarkcc45cc72018-05-15 16:06:12 -040096#Method static sk_sp<SkImage> MakeRasterData(const SkImageInfo& info, sk_sp<SkData> pixels, size_t rowBytes)
Cary Clark61313f32018-10-08 14:57:48 -040097#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -050098#Line # creates Image from Image_Info and shared pixels ##
Cary Clark3cd22cc2017-12-01 11:49:58 -050099Creates Image from Image_Info, sharing pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500100
Cary Clark3cd22cc2017-12-01 11:49:58 -0500101Image is returned if Image_Info is valid. Valid Image_Info parameters include:
102dimensions are greater than zero;
103each dimension fits in 29 bits;
104Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
105rowBytes are large enough to hold one row of pixels;
106pixels is not nullptr, and contains enough data for Image.
107
108#Param info contains width, height, Alpha_Type, Color_Type, Color_Space ##
109#Param pixels address or pixel storage ##
110#Param rowBytes size of pixel row or larger ##
111
112#Return Image sharing pixels, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500113
114#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500115#Image 3
116 size_t rowBytes = image->width() * SkColorTypeBytesPerPixel(kRGBA_8888_SkColorType);
117 sk_sp<SkData> data = SkData::MakeUninitialized(rowBytes * image->height());
Cary Clark682c58d2018-05-16 07:07:07 -0400118 SkImageInfo dstInfo = SkImageInfo::MakeN32(image->width(), image->height(),
Cary Clark0c5f5462017-12-15 11:21:51 -0500119 kPremul_SkAlphaType);
120 image->readPixels(dstInfo, data->writable_data(), rowBytes, 0, 0, SkImage::kAllow_CachingHint);
121 sk_sp<SkImage> raw = SkImage::MakeRasterData(dstInfo.makeColorType(kRGBA_8888_SkColorType),
122 data, rowBytes);
123 canvas->drawImage(image, 0, 0);
124 canvas->drawImage(raw.get(), 128, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500125##
126
Cary Clark3cd22cc2017-12-01 11:49:58 -0500127#SeeAlso MakeRasterCopy MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500128
129#Method ##
130
131# ------------------------------------------------------------------------------
132
Cary Clark3cd22cc2017-12-01 11:49:58 -0500133#Typedef void* ReleaseContext
Cary Clark682c58d2018-05-16 07:07:07 -0400134#Line # parameter type for MakeFromRaster ##
Cary Clarkffb3d682018-05-17 12:17:28 -0400135
136#Code
Cary Clarka90ea222018-10-16 10:30:28 -0400137#Populate
Cary Clarkffb3d682018-05-17 12:17:28 -0400138##
139
Cary Clark3cd22cc2017-12-01 11:49:58 -0500140Caller data passed to RasterReleaseProc; may be nullptr.
141
142#SeeAlso MakeFromRaster RasterReleaseProc
143
144##
145
Cary Clarka560c472017-11-27 10:44:06 -0500146#Typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext)
Cary Clark682c58d2018-05-16 07:07:07 -0400147#Line # parameter type for MakeFromRaster ##
Cary Clarkffb3d682018-05-17 12:17:28 -0400148
149#Code
Cary Clarka90ea222018-10-16 10:30:28 -0400150#Populate
Cary Clarkffb3d682018-05-17 12:17:28 -0400151##
152
Cary Clark3cd22cc2017-12-01 11:49:58 -0500153Function called when Image no longer shares pixels. ReleaseContext is
Cary Clark682c58d2018-05-16 07:07:07 -0400154provided by caller when Image is created, and may be nullptr.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500155
156#SeeAlso ReleaseContext MakeFromRaster
157
Cary Clarka560c472017-11-27 10:44:06 -0500158##
159
160#Method static sk_sp<SkImage> MakeFromRaster(const SkPixmap& pixmap,
161 RasterReleaseProc rasterReleaseProc,
162 ReleaseContext releaseContext)
Cary Clark61313f32018-10-08 14:57:48 -0400163#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500164#Line # creates Image from Pixmap, with release ##
Cary Clarka560c472017-11-27 10:44:06 -0500165
Cary Clark0c5f5462017-12-15 11:21:51 -0500166Creates Image from pixmap, sharing Pixmap pixels. Pixels must remain valid and
Cary Clark3cd22cc2017-12-01 11:49:58 -0500167unchanged until rasterReleaseProc is called. rasterReleaseProc is passed
168releaseContext when Image is deleted or no longer refers to pixmap pixels.
Cary Clarka560c472017-11-27 10:44:06 -0500169
Cary Clark0c5f5462017-12-15 11:21:51 -0500170Pass nullptr for rasterReleaseProc to share Pixmap without requiring a callback
171when Image is released. Pass nullptr for releaseContext if rasterReleaseProc
172does not require state.
173
Cary Clark3cd22cc2017-12-01 11:49:58 -0500174Image is returned if pixmap is valid. Valid Pixmap parameters include:
175dimensions are greater than zero;
176each dimension fits in 29 bits;
177Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
178row bytes are large enough to hold one row of pixels;
179pixel address is not nullptr.
180
181#Param pixmap Image_Info, pixel address, and row bytes ##
Cary Clark0c5f5462017-12-15 11:21:51 -0500182#Param rasterReleaseProc function called when pixels can be released; or nullptr ##
183#Param releaseContext state passed to rasterReleaseProc; or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500184
Cary Clark0c5f5462017-12-15 11:21:51 -0500185#Return Image sharing pixmap ##
Cary Clarka560c472017-11-27 10:44:06 -0500186
187#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500188#Function
189static void releaseProc(const void* pixels, SkImage::ReleaseContext context) {
190 int* countPtr = static_cast<int*>(context);
191 *countPtr += 1;
192}
193##
194
195void draw(SkCanvas* canvas) {
196 SkColor color = 0;
197 SkPixmap pixmap(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), &color, 4);
198 int releaseCount = 0;
199 sk_sp<SkImage> image(SkImage::MakeFromRaster(pixmap, releaseProc, &releaseCount));
200 SkDebugf("before reset: %d\n", releaseCount);
201 image.reset();
202 SkDebugf("after reset: %d\n", releaseCount);
203}
204#StdOut
205before reset: 0
206after reset: 1
207##
Cary Clarka560c472017-11-27 10:44:06 -0500208##
209
Cary Clark3cd22cc2017-12-01 11:49:58 -0500210#SeeAlso MakeRasterCopy MakeRasterData MakeFromGenerator RasterReleaseProc ReleaseContext
Cary Clarka560c472017-11-27 10:44:06 -0500211
212#Method ##
213
214# ------------------------------------------------------------------------------
215
216#Method static sk_sp<SkImage> MakeFromBitmap(const SkBitmap& bitmap)
Cary Clark61313f32018-10-08 14:57:48 -0400217#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500218#Line # creates Image from Bitmap, sharing or copying pixels ##
Cary Clark682c58d2018-05-16 07:07:07 -0400219Creates Image from bitmap, sharing or copying bitmap pixels. If the bitmap
Cary Clark3cd22cc2017-12-01 11:49:58 -0500220is marked immutable, and its pixel memory is shareable, it may be shared
221instead of copied.
Cary Clarka560c472017-11-27 10:44:06 -0500222
Cary Clark3cd22cc2017-12-01 11:49:58 -0500223Image is returned if bitmap is valid. Valid Bitmap parameters include:
224dimensions are greater than zero;
225each dimension fits in 29 bits;
226Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType;
227row bytes are large enough to hold one row of pixels;
228pixel address is not nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500229
Cary Clark3cd22cc2017-12-01 11:49:58 -0500230#Param bitmap Image_Info, row bytes, and pixels ##
231
232#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500233
234#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500235#Description
236The first Bitmap is shared; writing to the pixel memory changes the first
237Image.
238The second Bitmap is marked immutable, and is copied; writing to the pixel
239memory does not alter the second Image.
240##
241#Height 50
242 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
243 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
244 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
245 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
246 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
247 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
248 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
249 SkBitmap bitmap;
250 bitmap.installPixels(pixmap);
251 sk_sp<SkImage> image1 = SkImage::MakeFromBitmap(bitmap);
252 bitmap.setImmutable();
253 sk_sp<SkImage> image2 = SkImage::MakeFromBitmap(bitmap);
254 *pixmap.writable_addr8(2, 2) = 0x00;
255 canvas->scale(10, 10);
256 canvas->drawImage(image1, 0, 0);
257 canvas->drawImage(image2, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500258##
259
Cary Clark3cd22cc2017-12-01 11:49:58 -0500260#SeeAlso MakeFromRaster MakeRasterCopy MakeFromGenerator MakeRasterData
Cary Clarka560c472017-11-27 10:44:06 -0500261
262#Method ##
263
264# ------------------------------------------------------------------------------
265
266#Method static sk_sp<SkImage> MakeFromGenerator(std::unique_ptr<SkImageGenerator> imageGenerator,
267 const SkIRect* subset = nullptr)
Cary Clark61313f32018-10-08 14:57:48 -0400268#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500269#Line # creates Image from a stream of data ##
Cary Clarka560c472017-11-27 10:44:06 -0500270
Cary Clark0c5f5462017-12-15 11:21:51 -0500271Creates Image from data returned by imageGenerator. Generated data is owned by Image and may not
272be shared or accessed.
Cary Clarka560c472017-11-27 10:44:06 -0500273
Cary Clark0c5f5462017-12-15 11:21:51 -0500274subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
275otherwise, subset must be contained by image bounds.
276
277Image is returned if generator data is valid. Valid data parameters vary by type of data
278and platform.
Cary Clarka560c472017-11-27 10:44:06 -0500279
Cary Clark3cd22cc2017-12-01 11:49:58 -0500280imageGenerator may wrap Picture data, codec data, or custom data.
281
282#Param imageGenerator stock or custom routines to retrieve Image ##
283#Param subset bounds of returned Image; may be nullptr ##
284
285#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500286
287#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500288#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500289#Description
290The generator returning Picture cannot be shared; std::move transfers ownership to generated Image.
291##
292 SkPictureRecorder recorder;
293 recorder.beginRecording(100, 100)->drawColor(SK_ColorRED);
294 auto picture = recorder.finishRecordingAsPicture();
295 auto gen = SkImageGenerator::MakeFromPicture({100, 100}, picture, nullptr, nullptr,
296 SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB());
297 sk_sp<SkImage> image = SkImage::MakeFromGenerator(std::move(gen));
298 canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500299##
300
Cary Clark3cd22cc2017-12-01 11:49:58 -0500301#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500302
303#Method ##
304
305# ------------------------------------------------------------------------------
306
307#Method static sk_sp<SkImage> MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset = nullptr)
Cary Clark61313f32018-10-08 14:57:48 -0400308#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500309#Line # creates Image from encoded data ##
Cary Clark682c58d2018-05-16 07:07:07 -0400310Creates Image from encoded data.
Cary Clark0c5f5462017-12-15 11:21:51 -0500311subset allows selecting a portion of the full image. Pass nullptr to select the entire image;
312otherwise, subset must be contained by image bounds.
Cary Clarka560c472017-11-27 10:44:06 -0500313
Cary Clark3cd22cc2017-12-01 11:49:58 -0500314Image is returned if format of the encoded data is recognized and supported.
Cary Clark4855f782018-02-06 09:41:53 -0500315Recognized formats vary by platform.
Cary Clarka560c472017-11-27 10:44:06 -0500316
Cary Clark3cd22cc2017-12-01 11:49:58 -0500317#Param encoded data of Image to decode ##
318#Param subset bounds of returned Image; may be nullptr ##
319
320#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500321
Cary Clark61ca7c52018-01-02 11:34:14 -0500322#Example
323#Image 3
324int x = 0;
325for (int quality : { 100, 50, 10, 1} ) {
326 sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, quality);
327 sk_sp<SkImage> image = SkImage::MakeFromEncoded(encodedData);
328 canvas->drawImage(image, x, 0);
329 x += 64;
330}
Cary Clarka560c472017-11-27 10:44:06 -0500331##
332
Cary Clark3cd22cc2017-12-01 11:49:58 -0500333#SeeAlso MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500334
335#Method ##
336
337# ------------------------------------------------------------------------------
338
339#Typedef void (*TextureReleaseProc)(ReleaseContext releaseContext)
Cary Clark682c58d2018-05-16 07:07:07 -0400340#Line # parameter type for MakeFromTexture ##
Cary Clarkffb3d682018-05-17 12:17:28 -0400341
342#Code
Cary Clarka90ea222018-10-16 10:30:28 -0400343#Populate
Cary Clarkffb3d682018-05-17 12:17:28 -0400344##
345
Cary Clark682c58d2018-05-16 07:07:07 -0400346User function called when supplied texture may be deleted.
347#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -0500348##
349
350#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
351 const GrBackendTexture& backendTexture,
352 GrSurfaceOrigin origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500353 SkColorType colorType,
354 SkAlphaType alphaType,
355 sk_sp<SkColorSpace> colorSpace)
Cary Clark61313f32018-10-08 14:57:48 -0400356#In Constructors
Cary Clarkf895a422018-02-27 09:54:21 -0500357#Line # creates Image from GPU_Texture ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500358Creates Image from GPU_Texture associated with context. Caller is responsible for
359managing the lifetime of GPU_Texture.
360
361Image is returned if format of backendTexture is recognized and supported.
362Recognized formats vary by GPU back-end.
363
364#Param context GPU_Context ##
365#Param backendTexture texture residing on GPU ##
366#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500367#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500368##
Cary Clark681287e2018-03-16 11:34:15 -0400369#Param alphaType one of: #list_of_alpha_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500370##
371#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500372
373#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500374
375#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500376#Image 3
377#Platform gpu
378#Height 128
379#Description
380A back-end texture has been created and uploaded to the GPU outside of this example.
381##
382GrContext* context = canvas->getGrContext();
383if (!context) {
384 return;
385}
386canvas->scale(.25f, .25f);
387int x = 0;
388for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500389 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -0400390 origin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr);
Cary Clark0c5f5462017-12-15 11:21:51 -0500391 canvas->drawImage(image, x, 0);
392x += 512;
393}
Cary Clarka560c472017-11-27 10:44:06 -0500394##
395
Cary Clark3cd22cc2017-12-01 11:49:58 -0500396#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500397
398#Method ##
399
400# ------------------------------------------------------------------------------
401
402#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
403 const GrBackendTexture& backendTexture,
404 GrSurfaceOrigin origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500405 SkColorType colorType,
Cary Clarka560c472017-11-27 10:44:06 -0500406 SkAlphaType alphaType,
407 sk_sp<SkColorSpace> colorSpace,
408 TextureReleaseProc textureReleaseProc,
409 ReleaseContext releaseContext)
410
Cary Clark61ca7c52018-01-02 11:34:14 -0500411Creates Image from GPU_Texture associated with context. GPU_Texture must stay
Cary Clark3cd22cc2017-12-01 11:49:58 -0500412valid and unchanged until textureReleaseProc is called. textureReleaseProc is
413passed releaseContext when Image is deleted or no longer refers to texture.
Cary Clarka560c472017-11-27 10:44:06 -0500414
Cary Clark3cd22cc2017-12-01 11:49:58 -0500415Image is returned if format of backendTexture is recognized and supported.
416Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500417
Cary Clark3cd22cc2017-12-01 11:49:58 -0500418#Param context GPU_Context ##
419#Param backendTexture texture residing on GPU ##
420#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500421#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500422##
Cary Clark681287e2018-03-16 11:34:15 -0400423#Param alphaType one of: #list_of_alpha_types#
Cary Clark3cd22cc2017-12-01 11:49:58 -0500424##
Cary Clark61ca7c52018-01-02 11:34:14 -0500425#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500426#Param textureReleaseProc function called when texture can be released ##
427#Param releaseContext state passed to textureReleaseProc ##
428
429#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500430
Cary Clark0c5f5462017-12-15 11:21:51 -0500431#ToDo
432This doesn't do anything clever with TextureReleaseProc because it may not get called
Cary Clark61ca7c52018-01-02 11:34:14 -0500433fwithin the lifetime of the example
Cary Clark0c5f5462017-12-15 11:21:51 -0500434##
435
Cary Clarka560c472017-11-27 10:44:06 -0500436#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500437#Platform gpu
438#Image 4
Cary Clark0c5f5462017-12-15 11:21:51 -0500439GrContext* context = canvas->getGrContext();
440if (!context) {
441 return;
442}
Cary Clarkac47b882018-01-11 10:35:44 -0500443auto debugster = [](SkImage::ReleaseContext releaseContext) -> void {
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400444 // broken
Cary Clark08417bc2018-10-03 10:44:13 -0400445 // *((int *) releaseContext) += 128;
Cary Clark0c5f5462017-12-15 11:21:51 -0500446};
447int x = 0;
448for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500449 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark61ca7c52018-01-02 11:34:14 -0500450 origin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr, debugster, &x);
Cary Clark0c5f5462017-12-15 11:21:51 -0500451 canvas->drawImage(image, x, 0);
452 x += 128;
453}
Cary Clarka560c472017-11-27 10:44:06 -0500454##
455
Cary Clark3cd22cc2017-12-01 11:49:58 -0500456#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500457
458#Method ##
459
460# ------------------------------------------------------------------------------
461
462#Method static sk_sp<SkImage> MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> data,
463 bool buildMips,
Brian Osman584b5012018-04-13 15:48:26 -0400464 SkColorSpace* dstColorSpace,
465 bool limitToMaxTextureSize = false)
Cary Clark61313f32018-10-08 14:57:48 -0400466#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500467#Line # creates Image from encoded data, and uploads to GPU ##
Cary Clarka560c472017-11-27 10:44:06 -0500468
Cary Clark682c58d2018-05-16 07:07:07 -0400469Creates Image from encoded data. Image is uploaded to GPU back-end using context.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500470
471Created Image is available to other GPU contexts, and is available across thread
Cary Clark682c58d2018-05-16 07:07:07 -0400472boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
Cary Clark3cd22cc2017-12-01 11:49:58 -0500473share resources.
474
475When Image is no longer referenced, context releases texture memory
Cary Clarka560c472017-11-27 10:44:06 -0500476asynchronously.
Cary Clarka560c472017-11-27 10:44:06 -0500477
Cary Clark3cd22cc2017-12-01 11:49:58 -0500478Texture decoded from data is uploaded to match Surface created with
479dstColorSpace. Color_Space of Image is determined by encoded data.
Cary Clarka560c472017-11-27 10:44:06 -0500480
Cary Clark3cd22cc2017-12-01 11:49:58 -0500481Image is returned if format of data is recognized and supported, and if context
482supports moving resources. Recognized formats vary by platform and GPU back-end.
483
Cary Clark61ca7c52018-01-02 11:34:14 -0500484Image is returned using MakeFromEncoded if context is nullptr or does not support
485moving resources between contexts.
486
Cary Clark3cd22cc2017-12-01 11:49:58 -0500487#Param context GPU_Context ##
488#Param data Image to decode ##
489#Param buildMips create Image as Mip_Map if true ##
490#Param dstColorSpace range of colors of matching Surface on GPU ##
Brian Osman584b5012018-04-13 15:48:26 -0400491#Param limitToMaxTextureSize downscale image to GPU maximum texture size, if necessary ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500492
493#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500494
495#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500496#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500497#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500498GrContext* context = canvas->getGrContext();
499sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, 100);
500sk_sp<SkImage> image = SkImage::MakeCrossContextFromEncoded(context,
501 encodedData, false, nullptr);
502canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500503##
504
Cary Clark3cd22cc2017-12-01 11:49:58 -0500505#SeeAlso MakeCrossContextFromPixmap
506
507#Method ##
508
509# ------------------------------------------------------------------------------
510
511#Method static sk_sp<SkImage> MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap,
512 bool buildMips,
Brian Osman584b5012018-04-13 15:48:26 -0400513 SkColorSpace* dstColorSpace,
514 bool limitToMaxTextureSize = false)
Cary Clark61313f32018-10-08 14:57:48 -0400515#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500516#Line # creates Image from Pixmap, and uploads to GPU ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500517
Cary Clark682c58d2018-05-16 07:07:07 -0400518Creates Image from pixmap. Image is uploaded to GPU back-end using context.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500519
520Created Image is available to other GPU contexts, and is available across thread
Cary Clark682c58d2018-05-16 07:07:07 -0400521boundaries. All contexts must be in the same GPU_Share_Group, or otherwise
Cary Clark3cd22cc2017-12-01 11:49:58 -0500522share resources.
523
524When Image is no longer referenced, context releases texture memory
525asynchronously.
526
527Texture created from pixmap is uploaded to match Surface created with
528dstColorSpace. Color_Space of Image is determined by pixmap.colorSpace().
529
Cary Clark682c58d2018-05-16 07:07:07 -0400530Image is returned referring to GPU back-end if context is not nullptr,
Cary Clark61ca7c52018-01-02 11:34:14 -0500531format of data is recognized and supported, and if context supports moving
532resources between contexts. Otherwise, pixmap pixel data is copied and Image
533as returned in raster format if possible; nullptr may be returned.
534Recognized GPU formats vary by platform and GPU back-end.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500535
536#Param context GPU_Context ##
537#Param pixmap Image_Info, pixel address, and row bytes ##
538#Param buildMips create Image as Mip_Map if true ##
539#Param dstColorSpace range of colors of matching Surface on GPU ##
Brian Osman584b5012018-04-13 15:48:26 -0400540#Param limitToMaxTextureSize downscale image to GPU maximum texture size, if necessary ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500541
542#Return created Image, or nullptr ##
543
544#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500545#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500546#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500547GrContext* context = canvas->getGrContext();
548SkPixmap pixmap;
549if (source.peekPixels(&pixmap)) {
550 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(context, pixmap,
551 false, nullptr);
552 canvas->drawImage(image, 0, 0);
553}
Cary Clark3cd22cc2017-12-01 11:49:58 -0500554##
555
556#SeeAlso MakeCrossContextFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500557
558#Method ##
559
560# ------------------------------------------------------------------------------
561
562#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
563 const GrBackendTexture& backendTexture,
564 GrSurfaceOrigin surfaceOrigin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500565 SkColorType colorType,
566 SkAlphaType alphaType = kPremul_SkAlphaType,
567 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark61313f32018-10-08 14:57:48 -0400568#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500569#Line # creates Image from GPU_Texture, managed internally ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500570Creates Image from backendTexture associated with context. backendTexture and
571returned Image are managed internally, and are released when no longer needed.
Cary Clarka560c472017-11-27 10:44:06 -0500572
Cary Clark3cd22cc2017-12-01 11:49:58 -0500573Image is returned if format of backendTexture is recognized and supported.
574Recognized formats vary by GPU back-end.
Cary Clarka560c472017-11-27 10:44:06 -0500575
Cary Clark3cd22cc2017-12-01 11:49:58 -0500576#Param context GPU_Context ##
577#Param backendTexture texture residing on GPU ##
578#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500579#Param colorType one of: #list_of_color_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500580##
Cary Clark681287e2018-03-16 11:34:15 -0400581#Param alphaType one of: #list_of_alpha_types#
Cary Clark3cd22cc2017-12-01 11:49:58 -0500582##
Cary Clark61ca7c52018-01-02 11:34:14 -0500583#Param colorSpace range of colors; may be nullptr ##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500584
585#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500586
587#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500588#Image 5
589#Platform gpu
Cary Clark61ca7c52018-01-02 11:34:14 -0500590 if (!canvas->getGrContext()) {
591 return;
592 }
593 canvas->scale(.5f, .5f);
594 canvas->clear(0x7f3f5f7f);
595 int x = 0, y = 0;
596 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
597 for (auto alpha : { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType } ) {
598 sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(canvas->getGrContext(),
Cary Clark682c58d2018-05-16 07:07:07 -0400599 backEndTexture, origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500600 kRGBA_8888_SkColorType, alpha);
601 canvas->drawImage(image, x, y);
602 x += 160;
603 }
604 x -= 160 * 3;
605 y += 256;
606 }
Cary Clarka560c472017-11-27 10:44:06 -0500607##
608
Cary Clark61ca7c52018-01-02 11:34:14 -0500609#SeeAlso MakeFromTexture MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500610
611#Method ##
612
613# ------------------------------------------------------------------------------
614
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400615#Method static sk_sp<SkImage> MakeFromYUVATexturesCopy(GrContext* context,
616 SkYUVColorSpace yuvColorSpace,
617 const GrBackendTexture yuvaTextures[],
618 const SkYUVAIndex yuvaIndices[4],
619 SkISize imageSize,
620 GrSurfaceOrigin imageOrigin,
621 sk_sp<SkColorSpace> imageColorSpace = nullptr)
622#In Constructor
623#Line # creates Image from YUV_ColorSpace data ##
Cary Clark61313f32018-10-08 14:57:48 -0400624Creates an SkImage by flattening the specified YUVA planes into a single,
625interleaved RGBA image.
626yuvaTextures points to array of up to four YUVA textures which reside on GPU.
627YUVA textures contain YUVA planes which may be interleaved.
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400628
629#Param context GPU context ##
Cary Clark61313f32018-10-08 14:57:48 -0400630#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
631 kRec709_SkYUVColorSpace
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400632##
Cary Clark61313f32018-10-08 14:57:48 -0400633#Param yuvaTextures array of YUVA textures ##
634#Param yuvaIndices array indicating yuvaTextures element and channel
635 that map to Y, U, V, and A
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400636##
637#Param imageSize size of the resulting image ##
Cary Clark61313f32018-10-08 14:57:48 -0400638#Param imageOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400639#Param imageColorSpace range of colors of the resulting image; may be nullptr ##
640
641#Return created SkImage, or nullptr ##
642
643# seems too complicated to create an example for this
644#ToDo
645should this be moved to chrome only?
646##
647
648#NoExample
649##
650
Cary Clark1801b942018-10-30 21:10:03 -0400651#SeeAlso MakeFromYUVATexturesCopyWithExternalBackend MakeFromYUVATextures
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400652
653#Method ##
654
Cary Clark1801b942018-10-30 21:10:03 -0400655#Method static sk_sp<SkImage> MakeFromYUVATextures(GrContext* context,
656 SkYUVColorSpace yuvColorSpace,
657 const GrBackendTexture yuvaTextures[],
658 const SkYUVAIndex yuvaIndices[4],
659 SkISize imageSize,
660 GrSurfaceOrigin imageOrigin,
661 sk_sp<SkColorSpace> imageColorSpace = nullptr);
662
663#In Constructor
664#Line # creates Image from YUV_ColorSpace data ##
665
666Creates an SkImage by storing the specified YUVA planes into an image, to be rendered
667via multitexturing.
668
669#Param context GPU context ##
670#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
671 kRec709_SkYUVColorSpace
672##
673#Param yuvaTextures array of YUVA textures ##
674#Param yuvaIndices array indicating yuvaTextures element and channel
675 that map to Y, U, V, and A
676##
677#Param imageSize size of the resulting image ##
678#Param imageOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
679#Param imageColorSpace range of colors of the resulting image; may be nullptr ##
680
681#Return created SkImage, or nullptr ##
682
683#NoExample
684##
685
686#SeeAlso MakeFromYUVATexturesCopy MakeFromYUVATexturesCopyWithExternalBackend
687
688#Method ##
689
690
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400691# ------------------------------------------------------------------------------
692
693#Method static sk_sp<SkImage> MakeFromYUVATexturesCopyWithExternalBackend(
694 GrContext* context,
695 SkYUVColorSpace yuvColorSpace,
696 const GrBackendTexture yuvaTextures[],
697 const SkYUVAIndex yuvaIndices[4],
698 SkISize imageSize,
699 GrSurfaceOrigin imageOrigin,
700 const GrBackendTexture& backendTexture,
701 sk_sp<SkColorSpace> imageColorSpace = nullptr)
702#In Constructor
703#Line # creates Image from planar YUV_ColorSpace, stored in texture ##
704
Cary Clark61313f32018-10-08 14:57:48 -0400705Creates an SkImage by flattening the specified YUVA planes into a single,
706interleaved RGBA image. backendTexture is used to store the result of flattening.
707yuvaTextures points to array of up to four YUVA textures which reside on GPU.
708YUVA textures contain YUVA planes which may be interleaved.
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400709
710#Param context GPU context ##
Cary Clark61313f32018-10-08 14:57:48 -0400711#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
712 kRec709_SkYUVColorSpace
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400713##
Cary Clark61313f32018-10-08 14:57:48 -0400714#Param yuvaTextures array of YUVA textures ##
715#Param yuvaIndices array indicating yuvaTextures element and channel
716 that map to Y, U, V, and A
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400717##
718#Param imageSize size of the resulting image ##
Cary Clark61313f32018-10-08 14:57:48 -0400719#Param imageOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
720#Param backendTexture resource that stores the final pixels ##
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400721#Param imageColorSpace range of colors of the resulting image; may be nullptr ##
722
723#Return created SkImage, or nullptr ##
724
725# seems too complicated to create an example for this
726#ToDo
727should this be moved to chrome only?
728##
729
730#NoExample
731##
732
Cary Clark1801b942018-10-30 21:10:03 -0400733#SeeAlso MakeFromYUVATexturesCopy MakeFromYUVATextures
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400734
735#Method ##
736
Cary Clarka560c472017-11-27 10:44:06 -0500737#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400738 const GrBackendTexture yuvTextures[3],
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400739 GrSurfaceOrigin imageOrigin,
740 sk_sp<SkColorSpace> imageColorSpace = nullptr)
Cary Clark61313f32018-10-08 14:57:48 -0400741#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500742#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500743
Brian Salomon6a426c12018-03-15 12:16:02 -0400744Creates Image from copy of yuvTextures, an array of textures on GPU.
745yuvTextures contain pixels for YUV planes of Image. Returned Image has the dimensions
746yuvTextures[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500747
Cary Clark61ca7c52018-01-02 11:34:14 -0500748#Param context GPU_Context ##
749#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
750 kRec709_SkYUVColorSpace
751##
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400752#Param yuvTextures array of YUV textures on GPU ##
753#Param imageOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
754#Param imageColorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500755
Cary Clark61ca7c52018-01-02 11:34:14 -0500756#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500757
Cary Clark61ca7c52018-01-02 11:34:14 -0500758# seems too complicated to create an example for this
759#ToDo
760should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500761##
762
Cary Clark61ca7c52018-01-02 11:34:14 -0500763#NoExample
764##
765
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400766#SeeAlso MakeFromYUVTexturesCopyWithExternalBackend MakeFromNV12TexturesCopy MakeFromYUVATexturesCopy
Cary Clarkcdc371a2018-09-18 07:31:37 -0400767
768#Method ##
769
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400770# ------------------------------------------------------------------------------
771
Cary Clarkcdc371a2018-09-18 07:31:37 -0400772#Method static sk_sp<SkImage> MakeFromYUVTexturesCopyWithExternalBackend(
773 GrContext* context, SkYUVColorSpace yuvColorSpace,
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400774 const GrBackendTexture yuvTextures[3], GrSurfaceOrigin imageOrigin,
775 const GrBackendTexture& backendTexture, sk_sp<SkColorSpace> imageColorSpace = nullptr);
Cary Clark61313f32018-10-08 14:57:48 -0400776#In Constructors
Cary Clarkcdc371a2018-09-18 07:31:37 -0400777#Line # creates Image from planar YUV_ColorSpace, stored in texture ##
778
779Creates Image from copy of yuvTextures, an array of textures on GPU.
780yuvTextures contain pixels for YUV planes of Image. Returned Image has the dimensions
781yuvTextures[0] and stores pixels in backendTexture. yuvColorSpace describes how YUV colors
782convert to RGB colors.
783
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400784#Param context GPU_Context ##
785#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
786 kRec709_SkYUVColorSpace
Cary Clarkcdc371a2018-09-18 07:31:37 -0400787##
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400788#Param yuvTextures array of YUV textures on GPU ##
789#Param imageOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark61313f32018-10-08 14:57:48 -0400790#Param backendTexture resource that stores final pixels ##
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400791#Param imageColorSpace range of colors; may be nullptr ##
Cary Clarkcdc371a2018-09-18 07:31:37 -0400792
793#Return created SkImage, or nullptr ##
794
795# seems too complicated to create an example for this
796#ToDo
797should this be moved to chrome only?
798##
799
800#NoExample
801##
802
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400803#SeeAlso MakeFromYUVTexturesCopy MakeFromNV12TexturesCopy MakeFromYUVATexturesCopyWithExternalBackend
Cary Clark61ca7c52018-01-02 11:34:14 -0500804
805#Method ##
806
807# ------------------------------------------------------------------------------
808
Cary Clarka560c472017-11-27 10:44:06 -0500809#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
810 SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400811 const GrBackendTexture nv12Textures[2],
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400812 GrSurfaceOrigin imageOrigin,
813 sk_sp<SkColorSpace> imageColorSpace = nullptr)
Cary Clark61313f32018-10-08 14:57:48 -0400814#In Constructors
Brian Salomon6a426c12018-03-15 12:16:02 -0400815#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500816
Cary Clark681287e2018-03-16 11:34:15 -0400817Creates Image from copy of nv12Textures, an array of textures on GPU.
Brian Salomon6a426c12018-03-15 12:16:02 -0400818nv12Textures[0] contains pixels for YUV_Component_Y plane.
819nv12Textures[1] contains pixels for YUV_Component_U plane,
Cary Clark61ca7c52018-01-02 11:34:14 -0500820followed by pixels for YUV_Component_V plane.
Cary Clark681287e2018-03-16 11:34:15 -0400821Returned Image has the dimensions nv12Textures[2].
822yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500823
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400824#Param context GPU_Context ##
825#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
826 kRec709_SkYUVColorSpace
Cary Clark61ca7c52018-01-02 11:34:14 -0500827##
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400828#Param nv12Textures array of YUV textures on GPU ##
829#Param imageOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
830#Param imageColorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500831
Cary Clark61ca7c52018-01-02 11:34:14 -0500832#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500833
Cary Clark61ca7c52018-01-02 11:34:14 -0500834# seems too complicated to create an example for this
835#ToDo
836should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500837##
838
Cary Clark61ca7c52018-01-02 11:34:14 -0500839#NoExample
840##
841
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400842#SeeAlso MakeFromNV12TexturesCopyWithExternalBackend MakeFromYUVTexturesCopy MakeFromYUVATexturesCopy
Cary Clarkcdc371a2018-09-18 07:31:37 -0400843
844#Method ##
845
846#Method static sk_sp<SkImage> MakeFromNV12TexturesCopyWithExternalBackend(
847 GrContext* context,
848 SkYUVColorSpace yuvColorSpace,
849 const GrBackendTexture nv12Textures[2],
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400850 GrSurfaceOrigin imageOrigin,
851 const GrBackendTexture& backendTexture,
852 sk_sp<SkColorSpace> imageColorSpace = nullptr);
Cary Clark61313f32018-10-08 14:57:48 -0400853#In Constructors
Cary Clarkcdc371a2018-09-18 07:31:37 -0400854#Line # creates Image from planar YUV_ColorSpace, stored in texture ##
855
856Creates Image from copy of nv12Textures, an array of textures on GPU.
857nv12Textures[0] contains pixels for YUV_Component_Y plane.
858nv12Textures[1] contains pixels for YUV_Component_U plane,
859followed by pixels for YUV_Component_V plane.
860Returned Image has the dimensions nv12Textures[2] and stores pixels in backendTexture.
861yuvColorSpace describes how YUV colors convert to RGB colors.
862
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400863#Param context GPU_Context ##
864#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
865 kRec709_SkYUVColorSpace
Cary Clarkcdc371a2018-09-18 07:31:37 -0400866##
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400867#Param nv12Textures array of YUV textures on GPU ##
868#Param imageOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark61313f32018-10-08 14:57:48 -0400869#Param backendTexture resource that stores final pixels ##
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400870#Param imageColorSpace range of colors; may be nullptr ##
Cary Clarkcdc371a2018-09-18 07:31:37 -0400871
872#Return created Image, or nullptr ##
873
874# seems too complicated to create an example for this
875#ToDo
876should this be moved to chrome only?
877##
878
879#NoExample
880##
881
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400882#SeeAlso MakeFromNV12TexturesCopy MakeFromYUVTexturesCopy MakeFromYUVATexturesCopyWithExternalBackend
Cary Clarka560c472017-11-27 10:44:06 -0500883
884#Method ##
885
886# ------------------------------------------------------------------------------
887
Cary Clark4855f782018-02-06 09:41:53 -0500888# currently uncalled by any test or client ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500889#Bug 7424
Cary Clark61ca7c52018-01-02 11:34:14 -0500890
Cary Clark56356312018-02-08 14:45:18 -0500891#EnumClass BitDepth
Cary Clark682c58d2018-05-16 07:07:07 -0400892#Line # options for MakeFromPicture ##
Cary Clarka560c472017-11-27 10:44:06 -0500893#Code
Cary Clarka90ea222018-10-16 10:30:28 -0400894#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500895##
896
897#Const kU8 0
Cary Clark682c58d2018-05-16 07:07:07 -0400898#Line # uses 8-bit unsigned int per Color component ##
Cary Clarkffb3d682018-05-17 12:17:28 -0400899Use 8 bits per ARGB component using unsigned integer format.
Cary Clarka560c472017-11-27 10:44:06 -0500900##
901#Const kF16 1
Cary Clark682c58d2018-05-16 07:07:07 -0400902#Line # uses 16-bit float per Color component ##
Cary Clarkffb3d682018-05-17 12:17:28 -0400903Use 16 bits per ARGB component using half-precision floating point format.
Cary Clarka560c472017-11-27 10:44:06 -0500904##
905
Cary Clark61ca7c52018-01-02 11:34:14 -0500906#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500907##
908
Cary Clark61ca7c52018-01-02 11:34:14 -0500909#SeeAlso MakeFromPicture
Cary Clarka560c472017-11-27 10:44:06 -0500910
Cary Clark56356312018-02-08 14:45:18 -0500911#EnumClass ##
Cary Clarka560c472017-11-27 10:44:06 -0500912
913# ------------------------------------------------------------------------------
914
915#Method static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
916 const SkMatrix* matrix, const SkPaint* paint,
917 BitDepth bitDepth,
918 sk_sp<SkColorSpace> colorSpace)
Cary Clark61313f32018-10-08 14:57:48 -0400919#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500920#Line # creates Image from Picture ##
Cary Clarka560c472017-11-27 10:44:06 -0500921
Cary Clark61ca7c52018-01-02 11:34:14 -0500922Creates Image from picture. Returned Image width and height are set by dimensions.
923Image draws picture with matrix and paint, set to bitDepth and colorSpace.
Cary Clarka560c472017-11-27 10:44:06 -0500924
Cary Clark61ca7c52018-01-02 11:34:14 -0500925If matrix is nullptr, draws with identity Matrix. If paint is nullptr, draws
Cary Clark682c58d2018-05-16 07:07:07 -0400926with default Paint. colorSpace may be nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500927
Cary Clark61ca7c52018-01-02 11:34:14 -0500928#Param picture stream of drawing commands ##
929#Param dimensions width and height ##
930#Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ##
931#Param paint Paint to apply transparency, filtering, and so on; may be nullptr ##
Cary Clark682c58d2018-05-16 07:07:07 -0400932#Param bitDepth 8-bit integer or 16-bit float: per component ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500933#Param colorSpace range of colors; may be nullptr ##
934
935#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500936
937#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500938 SkPaint paint;
939 SkPictureRecorder recorder;
940 SkCanvas* recordingCanvas = recorder.beginRecording(50, 50);
941 for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) {
942 paint.setColor(color);
943 recordingCanvas->drawRect({10, 10, 30, 40}, paint);
944 recordingCanvas->translate(10, 10);
945 recordingCanvas->scale(1.2f, 1.4f);
946 }
947 sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
948 int x = 0, y = 0;
949 for (auto alpha : { 70, 140, 210 } ) {
950 paint.setAlpha(alpha);
951 auto srgbColorSpace = SkColorSpace::MakeSRGB();
952 sk_sp<SkImage> image = SkImage::MakeFromPicture(playback, {50, 50}, nullptr, &paint,
953 SkImage::BitDepth::kU8, srgbColorSpace);
954 canvas->drawImage(image, x, y);
955 x += 70; y += 70;
956 }
Cary Clarka560c472017-11-27 10:44:06 -0500957##
958
Cary Clark61ca7c52018-01-02 11:34:14 -0500959#SeeAlso SkCanvas::drawPicture
Cary Clarka560c472017-11-27 10:44:06 -0500960
961#Method ##
962
963# ------------------------------------------------------------------------------
964
Cary Clark9548ea92018-09-13 15:26:33 -0400965#Method static sk_sp<SkImage> MakeFromAHardwareBuffer(
966 AHardwareBuffer* hardwareBuffer,
967 SkAlphaType alphaType = kPremul_SkAlphaType,
968 sk_sp<SkColorSpace> colorSpace = nullptr,
969 GrSurfaceOrigin surfaceOrigin = kTopLeft_GrSurfaceOrigin)
Cary Clark61313f32018-10-08 14:57:48 -0400970#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500971#Line # creates Image from Android hardware buffer ##
Cary Clarka560c472017-11-27 10:44:06 -0500972
Cary Clark4855f782018-02-06 09:41:53 -0500973#Bug 7447
Cary Clarka560c472017-11-27 10:44:06 -0500974
Cary Clark61ca7c52018-01-02 11:34:14 -0500975Creates Image from Android hardware buffer.
976Returned Image takes a reference on the buffer.
Cary Clarka560c472017-11-27 10:44:06 -0500977
Cary Clark61ca7c52018-01-02 11:34:14 -0500978Only available on Android, when __ANDROID_API__ is defined to be 26 or greater.
Cary Clarka560c472017-11-27 10:44:06 -0500979
Cary Clark61ca7c52018-01-02 11:34:14 -0500980#Param hardwareBuffer AHardwareBuffer Android hardware buffer ##
Cary Clark681287e2018-03-16 11:34:15 -0400981#Param alphaType one of: #list_of_alpha_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500982##
983#Param colorSpace range of colors; may be nullptr ##
Cary Clark9548ea92018-09-13 15:26:33 -0400984#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clarka560c472017-11-27 10:44:06 -0500985
Cary Clark61ca7c52018-01-02 11:34:14 -0500986#Return created Image, or nullptr ##
987
988#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500989##
990
Cary Clark61ca7c52018-01-02 11:34:14 -0500991#SeeAlso MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -0500992
993#Method ##
994
995# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -0500996#Subtopic Property
Cary Clark4855f782018-02-06 09:41:53 -0500997#Line # values and attributes ##
998##
Cary Clarka560c472017-11-27 10:44:06 -0500999
1000#Method int width() const
Cary Clark4855f782018-02-06 09:41:53 -05001001#In Property
1002#Line # returns pixel column count ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001003Returns pixel count in each row.
1004
1005#Return pixel width in Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001006
1007#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001008#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -05001009#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -05001010 canvas->translate(10, 10);
1011 canvas->drawImage(image, 0, 0);
1012 canvas->translate(0, image->height());
1013 SkPaint paint;
Cary Clark61ca7c52018-01-02 11:34:14 -05001014 canvas->drawLine(0, 10, image->width(), 10, paint);
Cary Clark14768f62018-10-29 20:33:51 -04001015 canvas->drawString("width", image->width() / 2 - 15, 25, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001016##
1017
Cary Clark61ca7c52018-01-02 11:34:14 -05001018#SeeAlso dimensions() height()
Cary Clarka560c472017-11-27 10:44:06 -05001019
1020#Method ##
1021
1022# ------------------------------------------------------------------------------
1023
1024#Method int height() const
Cary Clark4855f782018-02-06 09:41:53 -05001025#In Property
1026#Line # returns pixel row count ##
Cary Clark2f466242017-12-11 16:03:17 -05001027Returns pixel row count.
1028
Cary Clark61ca7c52018-01-02 11:34:14 -05001029#Return pixel height in Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001030
1031#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001032#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -05001033#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -05001034 canvas->translate(10, 10);
1035 canvas->drawImage(image, 0, 0);
1036 canvas->translate(image->width(), 0);
1037 SkPaint paint;
Cary Clark61ca7c52018-01-02 11:34:14 -05001038 canvas->drawLine(10, 0, 10, image->height(), paint);
Cary Clark92694be2018-10-25 08:15:36 -04001039 canvas->drawString("height", 34, image->height() / 2, paint);
Cary Clarkac47b882018-01-11 10:35:44 -05001040##
Cary Clarka560c472017-11-27 10:44:06 -05001041
Cary Clark61ca7c52018-01-02 11:34:14 -05001042#SeeAlso dimensions() width()
Cary Clarka560c472017-11-27 10:44:06 -05001043
1044#Method ##
1045
1046# ------------------------------------------------------------------------------
1047
1048#Method SkISize dimensions() const
Cary Clark4855f782018-02-06 09:41:53 -05001049#In Property
1050#Line # returns width() and height() ##
Cary Clark681287e2018-03-16 11:34:15 -04001051
Cary Clark2f466242017-12-11 16:03:17 -05001052Returns ISize { width(), height() }.
1053
1054#Return integral size of width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -05001055
1056#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001057#Image 4
1058 SkISize dimensions = image->dimensions();
1059 SkIRect bounds = image->bounds();
1060 SkIRect dimensionsAsBounds = SkIRect::MakeSize(dimensions);
1061 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clark681287e2018-03-16 11:34:15 -04001062#StdOut
1063dimensionsAsBounds == bounds
1064##
Cary Clarka560c472017-11-27 10:44:06 -05001065##
1066
Cary Clark61ca7c52018-01-02 11:34:14 -05001067#SeeAlso height() width() bounds()
Cary Clarka560c472017-11-27 10:44:06 -05001068
1069#Method ##
1070
1071# ------------------------------------------------------------------------------
1072
1073#Method SkIRect bounds() const
Cary Clark4855f782018-02-06 09:41:53 -05001074#In Property
1075#Line # returns width() and height() as Rectangle ##
Cary Clark2f466242017-12-11 16:03:17 -05001076Returns IRect { 0, 0, width(), height() }.
1077
1078#Return integral rectangle from origin to width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -05001079
1080#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001081#Height 128
1082#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -05001083 SkIRect bounds = image->bounds();
Cary Clarkac47b882018-01-11 10:35:44 -05001084 for (int x : { 0, bounds.width() } ) {
1085 for (int y : { 0, bounds.height() } ) {
Cary Clark61ca7c52018-01-02 11:34:14 -05001086 canvas->drawImage(image, x, y);
1087 }
1088 }
Cary Clarka560c472017-11-27 10:44:06 -05001089##
1090
Cary Clark682c58d2018-05-16 07:07:07 -04001091#SeeAlso dimensions()
Cary Clarka560c472017-11-27 10:44:06 -05001092
1093#Method ##
1094
1095# ------------------------------------------------------------------------------
1096
1097#Method uint32_t uniqueID() const
Cary Clark4855f782018-02-06 09:41:53 -05001098#In Property
Cary Clark682c58d2018-05-16 07:07:07 -04001099#Line # returns identifier for Image ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001100Returns value unique to image. Image contents cannot change after Image is
1101created. Any operation to create a new Image will receive generate a new
1102unique number.
1103
1104#Return unique identifier ##
Cary Clarka560c472017-11-27 10:44:06 -05001105
1106#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001107#Image 5
1108#Height 156
1109 sk_sp<SkImage> subset = image->makeSubset({10, 20, 90, 100});
1110 canvas->drawImage(image, 0, 0);
1111 canvas->drawImage(subset, 128, 0);
1112 SkPaint paint;
1113 SkString s;
1114 s.printf("original id: %d", image->uniqueID());
1115 canvas->drawString(s, 20, image->height() + 20, paint);
1116 s.printf("subset id: %d", subset->uniqueID());
1117 canvas->drawString(s, 148, subset->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001118##
1119
Cary Clark61ca7c52018-01-02 11:34:14 -05001120#SeeAlso isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001121
1122#Method ##
1123
1124# ------------------------------------------------------------------------------
1125
1126#Method SkAlphaType alphaType() const
Cary Clark4855f782018-02-06 09:41:53 -05001127#In Property
1128#Line # returns Alpha_Type ##
Cary Clark681287e2018-03-16 11:34:15 -04001129Returns Alpha_Type, one of: #list_of_alpha_types#.
Cary Clark61ca7c52018-01-02 11:34:14 -05001130
1131Alpha_Type returned was a parameter to an Image constructor,
1132or was parsed from encoded data.
1133
1134#Return Alpha_Type in Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001135
1136#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001137#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -05001138#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -05001139 const char* alphaTypeStr[] = { "Unknown", "Opaque", "Premul", "Unpremul" };
1140 SkAlphaType alphaType = image->alphaType();
Cary Clarkac47b882018-01-11 10:35:44 -05001141 canvas->drawImage(image, 16, 0);
Cary Clarkffb3d682018-05-17 12:17:28 -04001142 canvas->drawString(alphaTypeStr[(int) alphaType], 20, image->height() + 20, SkPaint());
Cary Clarka560c472017-11-27 10:44:06 -05001143##
1144
Cary Clark61ca7c52018-01-02 11:34:14 -05001145#SeeAlso SkImageInfo::alphaType
Cary Clarka560c472017-11-27 10:44:06 -05001146
1147#Method ##
1148
1149# ------------------------------------------------------------------------------
1150
Greg Daniel56008aa2018-03-14 15:33:42 -04001151#Method SkColorType colorType() const
1152#In Property
1153#Line # returns Color_Type ##
1154
1155Returns Color_Type if known; otherwise, returns kUnknown_SkColorType.
1156
1157#Return Color_Type of Image ##
1158
1159#Example
Cary Clarkffb3d682018-05-17 12:17:28 -04001160#Image 4
1161#Height 96
1162 const char* colors[] = { "Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
1163 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16" };
1164 SkColorType colorType = image->colorType();
1165 canvas->drawImage(image, 16, 0);
1166 canvas->drawString(colors[(int) colorType], 20, image->height() + 20, SkPaint());
Greg Daniel56008aa2018-03-14 15:33:42 -04001167##
1168
1169#SeeAlso SkImageInfo::colorType
1170
1171#Method ##
1172
1173# ------------------------------------------------------------------------------
1174
Cary Clarka560c472017-11-27 10:44:06 -05001175#Method SkColorSpace* colorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001176#In Property
1177#Line # returns Color_Space ##
Cary Clark2f466242017-12-11 16:03:17 -05001178Returns Color_Space, the range of colors, associated with Image. The
1179reference count of Color_Space is unchanged. The returned Color_Space is
1180immutable.
Cary Clarka560c472017-11-27 10:44:06 -05001181
Cary Clark61dfc3a2018-01-03 08:37:53 -05001182Color_Space returned was passed to an Image constructor,
1183or was parsed from encoded data. Color_Space returned may be ignored when Image
1184is drawn, depending on the capabilities of the Surface receiving the drawing.
Cary Clark2f466242017-12-11 16:03:17 -05001185
1186#Return Color_Space in Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001187
1188#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001189#Image 3
1190#Set sRGB
1191 SkPixmap pixmap;
1192 source.peekPixels(&pixmap);
1193 canvas->scale(.25f, .25f);
1194 int y = 0;
1195 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1196 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1197 int x = 0;
1198 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1199 for (int index = 0; index < 2; ++index) {
1200 pixmap.setColorSpace(colorSpace);
1201 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1202 canvas->drawImage(image, x, y);
1203 colorSpace = image->colorSpace()->makeColorSpin();
1204 x += 512;
1205 }
1206 y += 512;
1207 }
Cary Clarka560c472017-11-27 10:44:06 -05001208##
1209
Cary Clark61dfc3a2018-01-03 08:37:53 -05001210#SeeAlso refColorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001211
1212#Method ##
1213
1214# ------------------------------------------------------------------------------
1215
1216#Method sk_sp<SkColorSpace> refColorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001217#In Property
1218#Line # returns Image_Info Color_Space ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001219Returns a smart pointer to Color_Space, the range of colors, associated with
1220Image. The smart pointer tracks the number of objects sharing this
1221SkColorSpace reference so the memory is released when the owners destruct.
1222
1223The returned SkColorSpace is immutable.
1224
1225Color_Space returned was passed to an Image constructor,
1226or was parsed from encoded data. Color_Space returned may be ignored when Image
1227is drawn, depending on the capabilities of the Surface receiving the drawing.
1228
1229#Return Color_Space in Image, or nullptr, wrapped in a smart pointer ##
Cary Clarka560c472017-11-27 10:44:06 -05001230
1231#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001232#Image 3
1233#Set sRGB
1234 SkPixmap pixmap;
1235 source.peekPixels(&pixmap);
1236 canvas->scale(.25f, .25f);
1237 int y = 0;
1238 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1239 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1240 int x = 0;
1241 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1242 for (int index = 0; index < 2; ++index) {
1243 pixmap.setColorSpace(colorSpace);
1244 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1245 canvas->drawImage(image, x, y);
1246 colorSpace = image->refColorSpace()->makeColorSpin();
1247 x += 512;
1248 }
1249 y += 512;
1250 }
Cary Clarka560c472017-11-27 10:44:06 -05001251##
1252
Cary Clark61dfc3a2018-01-03 08:37:53 -05001253#SeeAlso colorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001254
1255#Method ##
1256
1257# ------------------------------------------------------------------------------
1258
1259#Method bool isAlphaOnly() const
Cary Clark4855f782018-02-06 09:41:53 -05001260#In Property
1261#Line # returns if pixels represent a transparency mask ##
Cary Clark2f466242017-12-11 16:03:17 -05001262Returns true if Image pixels represent transparency only. If true, each pixel
1263is packed in 8 bits as defined by kAlpha_8_SkColorType.
Cary Clarka560c472017-11-27 10:44:06 -05001264
Cary Clark2f466242017-12-11 16:03:17 -05001265#Return true if pixels represent a transparency mask ##
Cary Clarka560c472017-11-27 10:44:06 -05001266
1267#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001268 uint8_t pmColors = 0;
1269 sk_sp<SkImage> image = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1), &pmColors, 1});
1270 SkDebugf("alphaOnly = %s\n", image->isAlphaOnly() ? "true" : "false");
1271#StdOut
1272alphaOnly = true
1273##
Cary Clarka560c472017-11-27 10:44:06 -05001274##
1275
Cary Clark61dfc3a2018-01-03 08:37:53 -05001276#SeeAlso alphaType isOpaque
Cary Clarka560c472017-11-27 10:44:06 -05001277
1278#Method ##
1279
1280# ------------------------------------------------------------------------------
1281
1282#Method bool isOpaque() const
Cary Clark4855f782018-02-06 09:41:53 -05001283#In Property
1284#Line # returns if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001285Returns true if pixels ignore their Alpha value and are treated as fully opaque.
Cary Clark2f466242017-12-11 16:03:17 -05001286
1287#Return true if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clarka560c472017-11-27 10:44:06 -05001288
1289#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001290 auto check_isopaque = [](const SkImageInfo& imageInfo) -> void {
1291 auto surface(SkSurface::MakeRaster(imageInfo));
1292 auto image(surface->makeImageSnapshot());
1293 SkDebugf("isOpaque = %s\n", image->isOpaque() ? "true" : "false");
1294 };
1295
1296 check_isopaque(SkImageInfo::MakeN32Premul(5, 5));
1297 check_isopaque(SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType));
1298#StdOut
1299isOpaque = false
1300isOpaque = true
1301##
Cary Clarka560c472017-11-27 10:44:06 -05001302##
1303
Cary Clark61dfc3a2018-01-03 08:37:53 -05001304#SeeAlso alphaType isAlphaOnly
Cary Clarka560c472017-11-27 10:44:06 -05001305
1306#Method ##
1307
1308# ------------------------------------------------------------------------------
1309
1310#Method sk_sp<SkShader> makeShader(SkShader::TileMode tileMode1, SkShader::TileMode tileMode2,
1311 const SkMatrix* localMatrix = nullptr) const
Cary Clark61313f32018-10-08 14:57:48 -04001312#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05001313#Line # creates Shader, Paint element that can tile Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001314
Cary Clark61dfc3a2018-01-03 08:37:53 -05001315Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1316SkShader::TileMode rules to fill drawn area outside Image. localMatrix permits
1317transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001318
Cary Clark5538c132018-06-14 12:28:14 -04001319#Param tileMode1 tiling on x-axis, one of: SkShader::kClamp_TileMode,
1320 SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
Cary Clark61dfc3a2018-01-03 08:37:53 -05001321##
Cary Clark5538c132018-06-14 12:28:14 -04001322#Param tileMode2 tiling on y-axis, one of: SkShader::kClamp_TileMode,
1323 SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
Cary Clark61dfc3a2018-01-03 08:37:53 -05001324##
1325#Param localMatrix Image transformation, or nullptr ##
1326
1327#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001328
1329#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001330#Image 4
1331SkMatrix matrix;
1332matrix.setRotate(45);
1333SkPaint paint;
1334paint.setShader(image->makeShader(SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode,
1335 &matrix));
1336canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001337##
1338
Cary Clark61dfc3a2018-01-03 08:37:53 -05001339#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001340
1341#Method ##
1342
1343# ------------------------------------------------------------------------------
1344
1345#Method sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const
1346
Cary Clark61dfc3a2018-01-03 08:37:53 -05001347Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1348SkShader::kClamp_TileMode to fill drawn area outside Image. localMatrix permits
1349transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001350
Cary Clark61dfc3a2018-01-03 08:37:53 -05001351#Param localMatrix Image transformation, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001352
Cary Clark61dfc3a2018-01-03 08:37:53 -05001353#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001354
1355#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001356#Image 5
1357SkMatrix matrix;
1358matrix.setRotate(45);
1359matrix.postTranslate(125, 30);
1360SkPaint paint;
1361paint.setShader(image->makeShader(&matrix));
1362canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001363##
1364
Cary Clarkf5404bb2018-01-05 12:10:09 -05001365#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001366
1367#Method ##
1368
1369# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001370#Subtopic Pixels
Cary Clark78de7512018-02-07 07:27:09 -05001371#Line # read and write pixel values ##
1372##
Cary Clarka560c472017-11-27 10:44:06 -05001373
1374#Method bool peekPixels(SkPixmap* pixmap) const
Cary Clark78de7512018-02-07 07:27:09 -05001375#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001376#Line # returns Pixmap if possible ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001377Copies Image pixel address, row bytes, and Image_Info to pixmap, if address
1378is available, and returns true. If pixel address is not available, return
1379false and leave pixmap unchanged.
Cary Clarka560c472017-11-27 10:44:06 -05001380
Cary Clarkf5404bb2018-01-05 12:10:09 -05001381#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
Cary Clarka560c472017-11-27 10:44:06 -05001382
Cary Clarkf5404bb2018-01-05 12:10:09 -05001383#Return true if Image has direct access to pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001384
1385#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001386 SkBitmap bitmap;
1387 bitmap.allocPixels(SkImageInfo::MakeN32Premul(12, 11));
1388 SkCanvas offscreen(bitmap);
1389 offscreen.clear(SK_ColorWHITE);
1390 SkPaint paint;
1391 offscreen.drawString("%", 1, 10, paint);
1392 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
1393 SkPixmap pixmap;
1394 if (image->peekPixels(&pixmap)) {
1395 const SkPMColor* pixels = pixmap.addr32();
1396 SkPMColor pmWhite = pixels[0];
1397 for (int y = 0; y < image->height(); ++y) {
1398 for (int x = 0; x < image->width(); ++x) {
1399 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
1400 }
1401 SkDebugf("\n");
1402 }
1403 }
1404#StdOut
1405------------
1406--xx----x---
1407-x--x--x----
1408-x--x--x----
1409-x--x-x-----
1410--xx-xx-xx--
1411-----x-x--x-
1412----x--x--x-
1413----x--x--x-
1414---x----xx--
1415------------
1416##
Cary Clarka560c472017-11-27 10:44:06 -05001417##
1418
Cary Clarkf5404bb2018-01-05 12:10:09 -05001419#SeeAlso readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001420
1421#Method ##
1422
1423# ------------------------------------------------------------------------------
1424
1425#Method GrTexture* getTexture() const
Cary Clark682c58d2018-05-16 07:07:07 -04001426#Deprecated
Cary Clarka560c472017-11-27 10:44:06 -05001427#Method ##
1428
1429# ------------------------------------------------------------------------------
1430
1431#Method bool isTextureBacked() const
Cary Clark78de7512018-02-07 07:27:09 -05001432#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001433#Line # returns if Image was created from GPU_Texture ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001434Returns true the contents of Image was created on or uploaded to GPU memory,
1435and is available as a GPU_Texture.
Cary Clarka560c472017-11-27 10:44:06 -05001436
Cary Clarkf5404bb2018-01-05 12:10:09 -05001437#Return true if Image is a GPU_Texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001438
1439#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001440#Image 5
1441#Platform gpu
1442auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1443 if (nullptr == image) {
1444 return;
1445 }
1446 SkPaint paint;
1447 paint.setAntiAlias(true);
Cary Clarkf5404bb2018-01-05 12:10:09 -05001448 canvas->drawImage(image, 0, 0);
Cary Clark14768f62018-10-29 20:33:51 -04001449 canvas->drawString(label, 30, image->height() / 4, paint);
Cary Clarkf5404bb2018-01-05 12:10:09 -05001450 canvas->drawString(image->isTextureBacked() ? "is GPU texture" : "not GPU texture",
Cary Clark14768f62018-10-29 20:33:51 -04001451 20, image->height() * 3 / 4, paint);
Cary Clarkf5404bb2018-01-05 12:10:09 -05001452};
1453sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1454sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001455 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1456 kOpaque_SkAlphaType, nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05001457drawImage(image, "image");
1458canvas->translate(image->width(), 0);
1459drawImage(bitmapImage, "source");
1460canvas->translate(-image->width(), image->height());
1461drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001462##
1463
Cary Clarkf5404bb2018-01-05 12:10:09 -05001464#SeeAlso MakeFromTexture isValid
Cary Clarka560c472017-11-27 10:44:06 -05001465
1466#Method ##
1467
1468# ------------------------------------------------------------------------------
1469
1470#Method bool isValid(GrContext* context) const
Cary Clark4855f782018-02-06 09:41:53 -05001471#In Property
1472#Line # returns if Image can draw to Raster_Surface or GPU_Context ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001473Returns true if Image can be drawn on either Raster_Surface or GPU_Surface.
1474If context is nullptr, tests if Image draws on Raster_Surface;
1475otherwise, tests if Image draws on GPU_Surface associated with context.
Cary Clarka560c472017-11-27 10:44:06 -05001476
Cary Clarkf5404bb2018-01-05 12:10:09 -05001477Image backed by GPU_Texture may become invalid if associated GrContext is
1478invalid. Lazy_Image may be invalid and may not draw to Raster_Surface or
1479GPU_Surface or both.
Cary Clarka560c472017-11-27 10:44:06 -05001480
Cary Clark61ca7c52018-01-02 11:34:14 -05001481#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05001482
Cary Clarkf5404bb2018-01-05 12:10:09 -05001483#Return true if Image can be drawn ##
Cary Clarka560c472017-11-27 10:44:06 -05001484
1485#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001486#Image 5
1487#Platform gpu
1488auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1489 if (nullptr == image) {
1490 return;
1491 }
1492 SkPaint paint;
1493 paint.setAntiAlias(true);
Cary Clarkf5404bb2018-01-05 12:10:09 -05001494 canvas->drawImage(image, 0, 0);
1495 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1496 if (canvas->getGrContext()) {
1497 canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" :
Cary Clark14768f62018-10-29 20:33:51 -04001498 "not valid on GPU", 20, image->height() * 5 / 8, paint);
Cary Clarkf5404bb2018-01-05 12:10:09 -05001499 }
1500 canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" :
Cary Clark14768f62018-10-29 20:33:51 -04001501 "not valid on CPU", 20, image->height() * 7 / 8, paint);
Cary Clarkf5404bb2018-01-05 12:10:09 -05001502};
1503sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1504sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001505 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1506 kOpaque_SkAlphaType, nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05001507drawImage(image, "image");
1508canvas->translate(image->width(), 0);
1509drawImage(bitmapImage, "source");
1510canvas->translate(-image->width(), image->height());
1511drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001512##
1513
Cary Clarkf5404bb2018-01-05 12:10:09 -05001514#SeeAlso isTextureBacked isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001515
1516#Method ##
1517
1518# ------------------------------------------------------------------------------
1519
Robert Phillipsc5509952018-04-04 15:54:55 -04001520#Method GrBackendTexture getBackendTexture(bool flushPendingGrContextIO,
1521 GrSurfaceOrigin* origin = nullptr) const
1522#In Property
1523#Line # returns GPU reference to Image as texture ##
1524
Cary Clark682c58d2018-05-16 07:07:07 -04001525Retrieves the back-end texture. If Image has no back-end texture, an invalid
Cary Clarkba75aee2018-04-05 08:18:41 -04001526object is returned. Call GrBackendTexture::isValid to determine if the result
1527is valid.
1528
1529If flushPendingGrContextIO is true, completes deferred I/O operations.
Robert Phillipsc5509952018-04-04 15:54:55 -04001530
1531If origin in not nullptr, copies location of content drawn into Image.
1532
1533#Param flushPendingGrContextIO flag to flush outstanding requests ##
1534#Param origin storage for one of: kTopLeft_GrSurfaceOrigin,
1535 kBottomLeft_GrSurfaceOrigin; or nullptr
1536##
1537
Cary Clarkba75aee2018-04-05 08:18:41 -04001538#Return back-end API texture handle; invalid on failure ##
Robert Phillipsc5509952018-04-04 15:54:55 -04001539
Cary Clarkba75aee2018-04-05 08:18:41 -04001540#Example
1541#Image 3
1542#Platform gpu
Brian Osman584b5012018-04-13 15:48:26 -04001543 GrContext* grContext = canvas->getGrContext();
1544 if (!grContext) {
1545 canvas->drawString("GPU only!", 20, 40, SkPaint());
1546 return;
1547 }
1548 sk_sp<SkImage> imageFromBackend = SkImage::MakeFromAdoptedTexture(grContext, backEndTexture,
1549 kBottomLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1550 GrBackendTexture textureFromImage = imageFromBackend->getBackendTexture(false);
1551 if (!textureFromImage.isValid()) {
1552 return;
1553 }
1554 sk_sp<SkImage> imageFromTexture = SkImage::MakeFromAdoptedTexture(grContext, textureFromImage,
1555 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1556 canvas->drawImage(imageFromTexture, 0, 0);
Cary Clarkba75aee2018-04-05 08:18:41 -04001557 canvas->drawImage(imageFromBackend, 128, 128);
Robert Phillipsc5509952018-04-04 15:54:55 -04001558##
1559
1560#SeeAlso MakeFromTexture isTextureBacked
1561
1562#Method ##
1563
1564# ------------------------------------------------------------------------------
1565
Cary Clarka560c472017-11-27 10:44:06 -05001566#Enum CachingHint
Cary Clark682c58d2018-05-16 07:07:07 -04001567#Line # options for readPixels and scalePixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001568#Code
Cary Clarka90ea222018-10-16 10:30:28 -04001569#Populate
Cary Clarka560c472017-11-27 10:44:06 -05001570##
1571
Cary Clarkac47b882018-01-11 10:35:44 -05001572CachingHint selects whether Skia may internally cache Bitmaps generated by
1573decoding Image, or by copying Image from GPU to CPU. The default behavior
Cary Clark682c58d2018-05-16 07:07:07 -04001574allows caching Bitmaps.
Cary Clarkac47b882018-01-11 10:35:44 -05001575
1576Choose kDisallow_CachingHint if Image pixels are to be used only once, or
1577if Image pixels reside in a cache outside of Skia, or to reduce memory pressure.
1578
1579Choosing kAllow_CachingHint does not ensure that pixels will be cached.
1580Image pixels may not be cached if memory requirements are too large or
1581pixels are not accessible.
Cary Clarka560c472017-11-27 10:44:06 -05001582
1583#Const kAllow_CachingHint 0
Cary Clark682c58d2018-05-16 07:07:07 -04001584#Line # allows internally caching decoded and copied pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001585##
1586#Const kDisallow_CachingHint 1
Cary Clark682c58d2018-05-16 07:07:07 -04001587#Line # disallows internally caching decoded and copied pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001588##
1589
Cary Clarkac47b882018-01-11 10:35:44 -05001590#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001591##
1592
Cary Clarkac47b882018-01-11 10:35:44 -05001593#SeeAlso readPixels scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001594
1595#Enum ##
1596
1597# ------------------------------------------------------------------------------
1598
1599#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1600 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001601#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001602#Line # copies and converts pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001603
Cary Clarkac47b882018-01-11 10:35:44 -05001604Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY),
Cary Clark682c58d2018-05-16 07:07:07 -04001605and does not exceed Image (width(), height()).
Cary Clarkac47b882018-01-11 10:35:44 -05001606
1607dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
1608destination. dstRowBytes specifics the gap from one destination row to the next.
1609Returns true if pixels are copied. Returns false if:
1610#List
1611# dstInfo.addr() equals nullptr ##
1612# dstRowBytes is less than dstInfo.minRowBytes ##
1613# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001614##
1615
Cary Clarkac47b882018-01-11 10:35:44 -05001616Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1617kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1618If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace must match.
1619If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType must
1620match. If Image Color_Space is nullptr, dstInfo.colorSpace must match. Returns
1621false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001622
Cary Clarkac47b882018-01-11 10:35:44 -05001623srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark682c58d2018-05-16 07:07:07 -04001624false if width() or height() is zero or negative.
Cary Clark2be81cf2018-09-13 12:04:30 -04001625Returns false if #Formula # abs(srcX) >= Image width() ##, or if #Formula # abs(srcY) >= Image height() ##.
Cary Clarka560c472017-11-27 10:44:06 -05001626
Cary Clarkac47b882018-01-11 10:35:44 -05001627If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1628If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1629
1630#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1631#Param dstPixels destination pixel storage ##
1632#Param dstRowBytes destination row length ##
1633#Param srcX column index whose absolute value is less than width() ##
1634#Param srcY row index whose absolute value is less than height() ##
1635#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1636
1637#Return true if pixels are copied to dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001638
1639#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001640#Image 3
1641 canvas->scale(.5f, .5f);
1642 const int width = 32;
1643 const int height = 32;
1644 std::vector<int32_t> dstPixels;
1645 dstPixels.resize(height * width * 4);
1646 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1647 for (int y = 0; y < 512; y += height ) {
1648 for (int x = 0; x < 512; x += width ) {
1649 if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) {
1650 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
1651 SkBitmap bitmap;
1652 bitmap.installPixels(dstPixmap);
1653 canvas->drawBitmap(bitmap, 0, 0);
1654 }
1655 canvas->translate(48, 0);
1656 }
1657 canvas->translate(-16 * 48, 48);
1658 }
Cary Clarka560c472017-11-27 10:44:06 -05001659##
1660
Cary Clarkac47b882018-01-11 10:35:44 -05001661#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001662
1663#Method ##
1664
1665# ------------------------------------------------------------------------------
1666
1667#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY,
1668 CachingHint cachingHint = kAllow_CachingHint) const
1669
Cary Clarkac47b882018-01-11 10:35:44 -05001670Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and
Cary Clark682c58d2018-05-16 07:07:07 -04001671does not exceed Image (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001672
Cary Clarkac47b882018-01-11 10:35:44 -05001673dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
1674and row bytes of destination. dst.rowBytes specifics the gap from one destination
1675row to the next. Returns true if pixels are copied. Returns false if:
1676#List
1677# dst pixel storage equals nullptr ##
1678# dst.rowBytes is less than SkImageInfo::minRowBytes ##
1679# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001680##
1681
Cary Clarkac47b882018-01-11 10:35:44 -05001682Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1683kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1684If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1685If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1686match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1687false if pixel conversion is not possible.
Cary Clark682c58d2018-05-16 07:07:07 -04001688
Cary Clarkac47b882018-01-11 10:35:44 -05001689srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark682c58d2018-05-16 07:07:07 -04001690false if width() or height() is zero or negative.
Cary Clark2be81cf2018-09-13 12:04:30 -04001691Returns false if #Formula # abs(srcX) >= Image width() ##, or if #Formula # abs(srcY) >= Image height() ##.
Cary Clarkac47b882018-01-11 10:35:44 -05001692
1693If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1694If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1695
1696#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1697#Param srcX column index whose absolute value is less than width() ##
1698#Param srcY row index whose absolute value is less than height() ##
1699#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1700
1701#Return true if pixels are copied to dst ##
1702
1703#Example
1704#Image 3
1705 std::vector<int32_t> srcPixels;
1706 int rowBytes = image->width() * 4;
1707 int quarterWidth = image->width() / 4;
1708 int quarterHeight = image->height() / 4;
1709 srcPixels.resize(image->height() * rowBytes);
1710 for (int y = 0; y < 4; ++y) {
1711 for (int x = 0; x < 4; ++x) {
1712 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1713 &srcPixels.front() + x * image->height() * quarterWidth +
1714 y * quarterWidth, rowBytes);
1715 image->readPixels(pixmap, x * quarterWidth, y * quarterHeight);
1716 }
1717 }
1718 canvas->scale(.5f, .5f);
1719 SkBitmap bitmap;
1720 bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()),
1721 &srcPixels.front(), rowBytes);
1722 canvas->drawBitmap(bitmap, 0, 0);
1723##
1724
1725#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001726
1727#Method ##
1728
1729# ------------------------------------------------------------------------------
1730
1731#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality,
1732 CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001733#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001734#Line # scales and converts one Image to another ##
Cary Clarka560c472017-11-27 10:44:06 -05001735
Cary Clarkac47b882018-01-11 10:35:44 -05001736Copies Image to dst, scaling pixels to fit dst.width() and dst.height(), and
1737converting pixels to match dst.colorType and dst.alphaType. Returns true if
1738pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1739less than dst SkImageInfo::minRowBytes.
Cary Clarka560c472017-11-27 10:44:06 -05001740
Cary Clarkac47b882018-01-11 10:35:44 -05001741Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1742kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1743If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1744If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1745match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1746false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001747
Cary Clarkac47b882018-01-11 10:35:44 -05001748Scales the image, with filterQuality, to match dst.width() and dst.height().
1749filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1750Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1751Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1752Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1753kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1754
1755If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1756If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1757
1758#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1759#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1760 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1761##
1762#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1763
1764#Return true if pixels are scaled to fit dst ##
Cary Clarka560c472017-11-27 10:44:06 -05001765
1766#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001767#Image 3
1768#Height 128
1769 std::vector<int32_t> srcPixels;
1770 int quarterWidth = image->width() / 16;
1771 int rowBytes = quarterWidth * 4;
1772 int quarterHeight = image->height() / 16;
1773 srcPixels.resize(quarterHeight * rowBytes);
1774 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1775 &srcPixels.front(), rowBytes);
1776 canvas->scale(4, 4);
1777 SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality,
1778 kMedium_SkFilterQuality, kHigh_SkFilterQuality };
1779 for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) {
1780 image->scalePixels(pixmap, qualities[index]);
1781 sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
1782 canvas->drawImage(filtered, 16 * index, 0);
1783 }
Cary Clarka560c472017-11-27 10:44:06 -05001784##
1785
Cary Clarkac47b882018-01-11 10:35:44 -05001786#SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001787
1788#Method ##
1789
1790# ------------------------------------------------------------------------------
1791
1792#Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const
Cary Clark78de7512018-02-07 07:27:09 -05001793#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001794#Line # returns encoded Image as SkData ##
Cary Clark682c58d2018-05-16 07:07:07 -04001795Encodes Image pixels, returning result as SkData.
Cary Clark2f466242017-12-11 16:03:17 -05001796
Cary Clarkac47b882018-01-11 10:35:44 -05001797Returns nullptr if encoding fails, or if encodedImageFormat is not supported.
Cary Clarka560c472017-11-27 10:44:06 -05001798
Cary Clarkac47b882018-01-11 10:35:44 -05001799Image encoding in a format requires both building with one or more of:
1800SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support
1801for the encoded format.
1802
1803If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can
1804additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP,
1805SkEncodedImageFormat::kGIF.
1806
1807quality is a platform and format specific metric trading off size and encoding
1808error. When used, quality equaling 100 encodes with the least error. quality may
1809be ignored by the encoder.
1810
1811#Param encodedImageFormat one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
1812 SkEncodedImageFormat::kWEBP
1813 ##
1814#Param quality encoder specific metric with 100 equaling best ##
Cary Clarka560c472017-11-27 10:44:06 -05001815
Cary Clark2f466242017-12-11 16:03:17 -05001816#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001817
1818#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001819#Image 3
1820 canvas->scale(4, 4);
1821 SkIRect subset = {0, 0, 16, 64};
1822 int x = 0;
1823 for (int quality : { 0, 10, 50, 100 } ) {
1824 sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
1825 sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
1826 canvas->drawImage(filtered, x, 0);
1827 x += 16;
1828 }
Cary Clarka560c472017-11-27 10:44:06 -05001829##
1830
Cary Clarkac47b882018-01-11 10:35:44 -05001831#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001832
1833#Method ##
1834
1835# ------------------------------------------------------------------------------
1836
Cary Clark61ca7c52018-01-02 11:34:14 -05001837#Method sk_sp<SkData> encodeToData() const
Cary Clarka560c472017-11-27 10:44:06 -05001838
Cary Clarkac47b882018-01-11 10:35:44 -05001839Encodes Image pixels, returning result as SkData. Returns existing encoded data
1840if present; otherwise, Image is encoded with SkEncodedImageFormat::kPNG. Skia
1841must be built with SK_HAS_PNG_LIBRARY to encode Image.
Cary Clarka560c472017-11-27 10:44:06 -05001842
Cary Clark682c58d2018-05-16 07:07:07 -04001843Returns nullptr if existing encoded data is missing or invalid, and
Cary Clarka560c472017-11-27 10:44:06 -05001844encoding fails.
1845
Cary Clarkac47b882018-01-11 10:35:44 -05001846#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001847
1848#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001849#Image 3
1850 canvas->scale(4, 4);
1851 SkIRect subset = {136, 32, 200, 96};
1852 sk_sp<SkData> data(image->encodeToData());
1853 sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
1854 canvas->drawImage(eye, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001855##
1856
Cary Clarkac47b882018-01-11 10:35:44 -05001857#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001858
1859#Method ##
1860
1861# ------------------------------------------------------------------------------
1862
1863#Method sk_sp<SkData> refEncodedData() const
Cary Clark78de7512018-02-07 07:27:09 -05001864#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001865#Line # returns Image encoded in SkData if present ##
Cary Clarkac47b882018-01-11 10:35:44 -05001866Returns encoded Image pixels as SkData, if Image was created from supported
1867encoded stream format. Platform support for formats vary and may require building
1868with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY.
Cary Clarka560c472017-11-27 10:44:06 -05001869
Cary Clarkac47b882018-01-11 10:35:44 -05001870Returns nullptr if Image contents are not encoded.
Cary Clarka560c472017-11-27 10:44:06 -05001871
Cary Clarkac47b882018-01-11 10:35:44 -05001872#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001873
1874#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001875#Image 3
1876#Platform gpu
1877 struct {
1878 const char* name;
1879 sk_sp<SkImage> image;
1880 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1881 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001882 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1883 kOpaque_SkAlphaType, nullptr) } };
Cary Clarkac47b882018-01-11 10:35:44 -05001884 SkString string;
1885 SkPaint paint;
1886 for (const auto& test : tests ) {
1887 if (!test.image) {
1888 string.printf("no %s", test.name);
1889 } else {
1890 string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name);
1891 }
1892 canvas->drawString(string, 10, 20, paint);
1893 canvas->translate(0, 20);
1894 }
Cary Clarka560c472017-11-27 10:44:06 -05001895##
1896
Cary Clarkac47b882018-01-11 10:35:44 -05001897#SeeAlso encodeToData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001898
1899#Method ##
1900
1901# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001902#Subtopic Utility
Cary Clark4855f782018-02-06 09:41:53 -05001903#Line # rarely called management functions ##
1904##
Cary Clarka560c472017-11-27 10:44:06 -05001905
Cary Clarka560c472017-11-27 10:44:06 -05001906#Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const
Cary Clark61313f32018-10-08 14:57:48 -04001907#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05001908#Line # creates Image containing part of original ##
Cary Clarkac47b882018-01-11 10:35:44 -05001909Returns subset of Image. subset must be fully contained by Image dimensions().
1910The implementation may share pixels, or may copy them.
Cary Clarka560c472017-11-27 10:44:06 -05001911
Cary Clarkac47b882018-01-11 10:35:44 -05001912Returns nullptr if subset is empty, or subset is not contained by bounds, or
1913pixels in Image could not be read or copied.
Cary Clarka560c472017-11-27 10:44:06 -05001914
Cary Clarkac47b882018-01-11 10:35:44 -05001915#Param subset bounds of returned Image ##
1916
1917#Return partial or full Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001918
1919#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001920#Image 3
1921 canvas->scale(.5f, .5f);
Cary Clarkc30382f2018-07-24 08:09:27 -04001922 const int width = 64;
1923 const int height = 64;
Cary Clarkac47b882018-01-11 10:35:44 -05001924 for (int y = 0; y < 512; y += height ) {
1925 for (int x = 0; x < 512; x += width ) {
1926 sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height}));
1927 canvas->drawImage(subset, x * 3 / 2, y * 3 / 2);
1928 }
1929 }
Cary Clarka560c472017-11-27 10:44:06 -05001930##
1931
Cary Clarkac47b882018-01-11 10:35:44 -05001932#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001933
1934#Method ##
1935
1936# ------------------------------------------------------------------------------
1937
Greg Daniel5f4b09d2018-06-12 16:39:59 -04001938#Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace,
1939 GrMipMapped mipMapped = GrMipMapped::kNo) const
Cary Clark61313f32018-10-08 14:57:48 -04001940#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05001941#Line # creates Image matching Color_Space if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001942Returns Image backed by GPU_Texture associated with context. Returned Image is
Cary Clark5538c132018-06-14 12:28:14 -04001943compatible with Surface created with dstColorSpace. The returned Image respects
1944mipMapped setting; if mipMapped equals GrMipMapped::kYes, the backing texture
1945allocates Mip_Map levels. Returns original Image if context
1946and dstColorSpace match and mipMapped is compatible with backing GPU_Texture.
Cary Clarkac47b882018-01-11 10:35:44 -05001947
1948Returns nullptr if context is nullptr, or if Image was created with another
1949GrContext.
Cary Clarka560c472017-11-27 10:44:06 -05001950
Cary Clark61ca7c52018-01-02 11:34:14 -05001951#Param context GPU_Context ##
Cary Clarkac47b882018-01-11 10:35:44 -05001952#Param dstColorSpace range of colors of matching Surface on GPU ##
Cary Clark5538c132018-06-14 12:28:14 -04001953#Param mipMapped whether created Image texture must allocate Mip_Map levels ##
Cary Clarka560c472017-11-27 10:44:06 -05001954
Cary Clarkac47b882018-01-11 10:35:44 -05001955#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001956
1957#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001958#Platform gpu
1959#Image 5
1960 auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void {
1961 if (nullptr == image || nullptr == context) {
1962 return;
1963 }
1964 SkPaint paint;
1965 paint.setAntiAlias(true);
Cary Clarkac47b882018-01-11 10:35:44 -05001966 sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr));
1967 canvas->drawImage(texture, 0, 0);
Cary Clark14768f62018-10-29 20:33:51 -04001968 canvas->drawString(label, 20, texture->height() / 4, paint);
Cary Clarkac47b882018-01-11 10:35:44 -05001969 };
1970 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1971 GrContext* context = canvas->getGrContext();
1972 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001973 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1974 kOpaque_SkAlphaType, nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001975 drawImage(image, context, "image");
1976 canvas->translate(image->width(), 0);
1977 drawImage(bitmapImage, context, "source");
1978 canvas->translate(-image->width(), image->height());
1979 drawImage(textureImage, context, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001980##
1981
Cary Clarkac47b882018-01-11 10:35:44 -05001982#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05001983
1984#Method ##
1985
1986# ------------------------------------------------------------------------------
1987
1988#Method sk_sp<SkImage> makeNonTextureImage() const
Cary Clark61313f32018-10-08 14:57:48 -04001989#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05001990#Line # creates Image without dependency on GPU_Texture ##
Cary Clarkac47b882018-01-11 10:35:44 -05001991Returns Raster_Image or Lazy_Image. Copies Image backed by GPU_Texture into
Cary Clark4855f782018-02-06 09:41:53 -05001992CPU memory if needed. Returns original Image if decoded in Raster_Bitmap,
Cary Clarkac47b882018-01-11 10:35:44 -05001993or if encoded in a stream.
Cary Clark61ca7c52018-01-02 11:34:14 -05001994
Cary Clarkac47b882018-01-11 10:35:44 -05001995Returns nullptr if backed by GPU_Texture and copy fails.
1996
1997#Return Raster_Image, Lazy_Image, or nullptr ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001998
1999#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002000#Image 5
2001#Platform gpu
2002 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2003 if (nullptr == image) {
2004 return;
2005 }
2006 SkPaint paint;
2007 paint.setAntiAlias(true);
Cary Clarkac47b882018-01-11 10:35:44 -05002008 sk_sp<SkImage> nonTexture(image->makeNonTextureImage());
2009 canvas->drawImage(nonTexture, 0, 0);
Cary Clark14768f62018-10-29 20:33:51 -04002010 canvas->drawString(label, 20, nonTexture->height() / 4, paint);
Cary Clarkac47b882018-01-11 10:35:44 -05002011 };
2012 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2013 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04002014 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
2015 kOpaque_SkAlphaType, nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05002016 drawImage(image, "image");
2017 canvas->translate(image->width(), 0);
2018 drawImage(bitmapImage, "source");
2019 canvas->translate(-image->width(), image->height());
2020 drawImage(textureImage, "backEndTexture");
Cary Clark61ca7c52018-01-02 11:34:14 -05002021##
2022
Cary Clark56356312018-02-08 14:45:18 -05002023#SeeAlso makeTextureImage makeRasterImage MakeBackendTextureFromSkImage
Cary Clark61ca7c52018-01-02 11:34:14 -05002024
2025#Method ##
2026
2027# ------------------------------------------------------------------------------
2028
2029#Method sk_sp<SkImage> makeRasterImage() const
Cary Clark61313f32018-10-08 14:57:48 -04002030#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05002031#Line # creates Image compatible with Raster_Surface if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05002032Returns Raster_Image. Copies Image backed by GPU_Texture into CPU memory,
Cary Clark4855f782018-02-06 09:41:53 -05002033or decodes Image from Lazy_Image. Returns original Image if decoded in
Cary Clarkac47b882018-01-11 10:35:44 -05002034Raster_Bitmap.
Cary Clarka560c472017-11-27 10:44:06 -05002035
Cary Clarkac47b882018-01-11 10:35:44 -05002036Returns nullptr if copy, decode, or pixel read fails.
Cary Clarka560c472017-11-27 10:44:06 -05002037
Cary Clarkac47b882018-01-11 10:35:44 -05002038#Return Raster_Image, or nullptr ##
2039
Cary Clark4855f782018-02-06 09:41:53 -05002040#Bug 7479
Cary Clarka560c472017-11-27 10:44:06 -05002041#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002042#Image 5
2043#Platform gpu
2044 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2045 if (nullptr == image) {
2046 return;
2047 }
2048 SkPaint paint;
2049 paint.setAntiAlias(true);
Cary Clarkac47b882018-01-11 10:35:44 -05002050 sk_sp<SkImage> raster(image->makeRasterImage());
2051 canvas->drawImage(raster, 0, 0);
Cary Clark14768f62018-10-29 20:33:51 -04002052 canvas->drawString(label, 20, raster->height() / 4, paint);
Cary Clarkac47b882018-01-11 10:35:44 -05002053 };
2054 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2055 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04002056 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
2057 kOpaque_SkAlphaType, nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05002058 drawImage(image, "image");
2059 canvas->translate(image->width(), 0);
2060 drawImage(bitmapImage, "source");
2061 canvas->translate(-image->width(), image->height());
2062 drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05002063##
2064
Cary Clarkac47b882018-01-11 10:35:44 -05002065#SeeAlso isTextureBacked isLazyGenerated MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -05002066
2067#Method ##
2068
2069# ------------------------------------------------------------------------------
2070
2071#Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
2072 const SkIRect& clipBounds, SkIRect* outSubset,
2073 SkIPoint* offset) const
Cary Clark61313f32018-10-08 14:57:48 -04002074#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05002075#Line # creates filtered, clipped Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002076
Cary Clarkac47b882018-01-11 10:35:44 -05002077Creates filtered Image. filter processes original Image, potentially changing
2078color, position, and size. subset is the bounds of original Image processed
2079by filter. clipBounds is the expected bounds of the filtered Image. outSubset
2080is required storage for the actual bounds of the filtered Image. offset is
2081required storage for translation of returned Image.
Cary Clarka560c472017-11-27 10:44:06 -05002082
Cary Clarkac47b882018-01-11 10:35:44 -05002083Returns nullptr if Image could not be created. If nullptr is returned, outSubset
2084and offset are undefined.
2085
Cary Clark56356312018-02-08 14:45:18 -05002086Useful for animation of SkImageFilter that varies size from frame to frame.
2087Returned Image is created larger than required by filter so that GPU_Texture
2088can be reused with different sized effects. outSubset describes the valid bounds
2089of GPU_Texture returned. offset translates the returned Image to keep subsequent
2090animation frames aligned with respect to each other.
Cary Clarkac47b882018-01-11 10:35:44 -05002091
2092#Param filter how Image is sampled when transformed ##
Cary Clark56356312018-02-08 14:45:18 -05002093#Param subset bounds of Image processed by filter ##
2094#Param clipBounds expected bounds of filtered Image ##
2095#Param outSubset storage for returned Image bounds ##
2096#Param offset storage for returned Image translation ##
Cary Clarka560c472017-11-27 10:44:06 -05002097
Cary Clarkac47b882018-01-11 10:35:44 -05002098#Return filtered Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05002099
Cary Clark5717e822018-09-21 11:36:41 -04002100# Duration 5 breaks fiddlecli
Cary Clarka560c472017-11-27 10:44:06 -05002101#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002102#Description
2103In each frame of the animation, filtered Image is drawn in a different location.
2104By translating canvas by returned offset, Image appears stationary.
2105##
2106#Image 5
2107#Platform gpu
Cary Clark5717e822018-09-21 11:36:41 -04002108#Duration 1
Cary Clarkac47b882018-01-11 10:35:44 -05002109 sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make(
2110 -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE,
2111 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
2112 nullptr);
2113 sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr);
2114 SkIRect subset = image->bounds();
2115 SkIRect clipBounds = image->bounds();
2116 clipBounds.outset(60, 60);
2117 SkIRect outSubset;
2118 SkIPoint offset;
2119 sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds,
2120 &outSubset, &offset));
2121 SkPaint paint;
2122 paint.setAntiAlias(true);
2123 paint.setStyle(SkPaint::kStroke_Style);
2124 canvas->drawLine(0, 0, offset.fX, offset.fY, paint);
2125 canvas->translate(offset.fX, offset.fY);
2126 canvas->drawImage(filtered, 0, 0);
Cary Clark681287e2018-03-16 11:34:15 -04002127 canvas->drawRect(SkRect::Make(outSubset), paint);
Cary Clarka560c472017-11-27 10:44:06 -05002128##
2129
Cary Clark56356312018-02-08 14:45:18 -05002130#SeeAlso makeShader SkPaint::setImageFilter
Cary Clarka560c472017-11-27 10:44:06 -05002131
2132#Method ##
2133
2134# ------------------------------------------------------------------------------
2135
Cary Clarka560c472017-11-27 10:44:06 -05002136#Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc
Cary Clark682c58d2018-05-16 07:07:07 -04002137#Line # parameter type for MakeBackendTextureFromSkImage ##
Cary Clarkffb3d682018-05-17 12:17:28 -04002138
2139#Code
Cary Clarka90ea222018-10-16 10:30:28 -04002140#Populate
Cary Clarkffb3d682018-05-17 12:17:28 -04002141##
2142
Cary Clark0d225392018-06-07 09:59:07 -04002143Defines a callback function, taking one parameter of type GrBackendTexture with
2144no return value. Function is called when back-end texture is to be released.
Cary Clarka560c472017-11-27 10:44:06 -05002145##
2146
2147# ------------------------------------------------------------------------------
2148
2149#Method static bool MakeBackendTextureFromSkImage(GrContext* context,
2150 sk_sp<SkImage> image,
2151 GrBackendTexture* backendTexture,
2152 BackendTextureReleaseProc* backendTextureReleaseProc)
Cary Clark61313f32018-10-08 14:57:48 -04002153#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05002154#Line # creates GPU_Texture from Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002155
Cary Clark56356312018-02-08 14:45:18 -05002156Creates a GrBackendTexture from the provided SkImage. Returns true and
2157stores result in backendTexture and backendTextureReleaseProc if
2158texture is created; otherwise, returns false and leaves
2159backendTexture and backendTextureReleaseProc unmodified.
Cary Clarka560c472017-11-27 10:44:06 -05002160
Cary Clark56356312018-02-08 14:45:18 -05002161Call backendTextureReleaseProc after deleting backendTexture.
2162backendTextureReleaseProc cleans up auxiliary data related to returned
2163backendTexture. The caller must delete returned backendTexture after use.
Cary Clarka560c472017-11-27 10:44:06 -05002164
Cary Clark56356312018-02-08 14:45:18 -05002165If Image is both texture backed and singly referenced, image is returned in
2166backendTexture without conversion or making a copy. Image is singly referenced
2167if its was transferred solely using std::move().
2168
2169If Image is not texture backed, returns texture with Image contents.
Cary Clarka560c472017-11-27 10:44:06 -05002170
Cary Clark61ca7c52018-01-02 11:34:14 -05002171#Param context GPU_Context ##
Cary Clark56356312018-02-08 14:45:18 -05002172#Param image Image used for texture ##
Cary Clark682c58d2018-05-16 07:07:07 -04002173#Param backendTexture storage for back-end texture ##
Cary Clark56356312018-02-08 14:45:18 -05002174#Param backendTextureReleaseProc storage for clean up function ##
Cary Clarka560c472017-11-27 10:44:06 -05002175
Cary Clark682c58d2018-05-16 07:07:07 -04002176#Return true if back-end texture was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002177
2178#Example
Cary Clark56356312018-02-08 14:45:18 -05002179#Platform gpu
2180#Height 64
2181#Function
Brian Salomon67f85842018-02-09 08:50:22 -05002182static sk_sp<SkImage> create_gpu_image(GrContext* grContext) {
2183 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
2184 auto surface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info));
2185 SkCanvas* canvas = surface->getCanvas();
2186 canvas->clear(SK_ColorWHITE);
2187 SkPaint paint;
2188 paint.setColor(SK_ColorBLACK);
2189 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
2190 return surface->makeImageSnapshot();
2191}
2192##
2193
Cary Clark682c58d2018-05-16 07:07:07 -04002194void draw(SkCanvas* canvas) {
Brian Salomon67f85842018-02-09 08:50:22 -05002195 GrContext* grContext = canvas->getGrContext();
2196 if (!grContext) {
2197 return;
2198 }
2199 sk_sp<SkImage> backEndImage = create_gpu_image(grContext);
2200 canvas->drawImage(backEndImage, 0, 0);
2201 GrBackendTexture texture;
2202 SkImage::BackendTextureReleaseProc proc;
2203 if (!SkImage::MakeBackendTextureFromSkImage(grContext, std::move(backEndImage),
2204 &texture, &proc)) {
2205 return;
2206 }
2207 sk_sp<SkImage> i2 = SkImage::MakeFromTexture(grContext, texture, kTopLeft_GrSurfaceOrigin,
2208 kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
2209 canvas->drawImage(i2, 30, 30);
Cary Clark56356312018-02-08 14:45:18 -05002210}
Cary Clarka560c472017-11-27 10:44:06 -05002211##
2212
Cary Clark56356312018-02-08 14:45:18 -05002213#SeeAlso MakeFromTexture makeTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002214
2215#Method ##
2216
2217# ------------------------------------------------------------------------------
2218
2219#Enum LegacyBitmapMode
Cary Clark56356312018-02-08 14:45:18 -05002220#Deprecated soon
Cary Clarka560c472017-11-27 10:44:06 -05002221#Code
Cary Clarka90ea222018-10-16 10:30:28 -04002222#Populate
Cary Clarka560c472017-11-27 10:44:06 -05002223##
2224
Cary Clarka560c472017-11-27 10:44:06 -05002225#Const kRO_LegacyBitmapMode 0
Cary Clark682c58d2018-05-16 07:07:07 -04002226#Line # returned bitmap is read-only and immutable ##
Cary Clarka560c472017-11-27 10:44:06 -05002227##
Cary Clarka560c472017-11-27 10:44:06 -05002228
2229#Enum ##
2230
2231# ------------------------------------------------------------------------------
2232
Cary Clark56356312018-02-08 14:45:18 -05002233#Method bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode = kRO_LegacyBitmapMode) const
Cary Clark61313f32018-10-08 14:57:48 -04002234#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05002235#Line # returns as Raster_Bitmap ##
Cary Clarkac47b882018-01-11 10:35:44 -05002236Creates raster Bitmap with same pixels as Image. If legacyBitmapMode is
2237kRO_LegacyBitmapMode, returned bitmap is read-only and immutable.
2238Returns true if Bitmap is stored in bitmap. Returns false and resets bitmap if
2239Bitmap write did not succeed.
Cary Clarka560c472017-11-27 10:44:06 -05002240
Cary Clark3cd22cc2017-12-01 11:49:58 -05002241#Param bitmap storage for legacy Bitmap ##
Cary Clark56356312018-02-08 14:45:18 -05002242#Param legacyBitmapMode to be deprecated ##
Cary Clarka560c472017-11-27 10:44:06 -05002243
Cary Clark3cd22cc2017-12-01 11:49:58 -05002244#Return true if Bitmap was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002245
Cary Clarkae957c42018-06-07 17:07:17 -04002246#Example
Cary Clark56356312018-02-08 14:45:18 -05002247#Image 4
2248#Platform gpu
Brian Salomon67f85842018-02-09 08:50:22 -05002249 SkBitmap bitImage;
2250 if (image->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2251 canvas->drawBitmap(bitImage, 0, 0);
2252 }
2253 GrContext* grContext = canvas->getGrContext();
2254 if (!grContext) {
2255 return;
2256 }
2257 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(grContext, backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04002258 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
2259 kOpaque_SkAlphaType, nullptr));
Brian Salomon67f85842018-02-09 08:50:22 -05002260 canvas->drawImage(textureImage, 45, 45);
2261 if (textureImage->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2262 canvas->drawBitmap(bitImage, 90, 90);
2263 }
Cary Clarka560c472017-11-27 10:44:06 -05002264##
2265
Cary Clark56356312018-02-08 14:45:18 -05002266#SeeAlso MakeRasterData makeRasterImage makeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002267
2268#Method ##
2269
2270# ------------------------------------------------------------------------------
2271
2272#Method bool isLazyGenerated() const
Cary Clark4855f782018-02-06 09:41:53 -05002273#In Property
2274#Line # returns if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002275Returns true if Image is backed by an image-generator or other service that creates
2276and caches its pixels or texture on-demand.
2277
Cary Clark2f466242017-12-11 16:03:17 -05002278#Return true if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002279
2280#Example
Cary Clark2f466242017-12-11 16:03:17 -05002281#Height 80
2282#Function
2283class TestImageGenerator : public SkImageGenerator {
2284public:
2285 TestImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(10, 10)) {}
2286 ~TestImageGenerator() override {}
2287protected:
2288 bool onGetPixels(const SkImageInfo& info, void* pixelPtr, size_t rowBytes,
2289 const Options& options) override {
2290 SkPMColor* pixels = static_cast<SkPMColor*>(pixelPtr);
2291 for (int y = 0; y < info.height(); ++y) {
2292 for (int x = 0; x < info.width(); ++x) {
2293 pixels[y * info.width() + x] = 0xff223344 + y * 0x000C0811;
2294 }
2295 }
2296 return true;
2297 }
2298};
2299##
2300void draw(SkCanvas* canvas) {
2301 auto gen = std::unique_ptr<TestImageGenerator>(new TestImageGenerator());
2302 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
2303 SkString lazy(image->isLazyGenerated() ? "is lazy" : "not lazy");
2304 canvas->scale(8, 8);
2305 canvas->drawImage(image, 0, 0, nullptr);
2306 SkPaint paint;
2307 paint.setTextSize(4);
2308 canvas->drawString(lazy, 2, 5, paint);
2309}
Cary Clarka560c472017-11-27 10:44:06 -05002310##
2311
Cary Clarkf5404bb2018-01-05 12:10:09 -05002312#Example
2313#Image 5
2314#Platform gpu
2315void draw(SkCanvas* canvas) {
2316 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2317 if (nullptr == image) {
2318 return;
2319 }
2320 SkPaint paint;
2321 paint.setAntiAlias(true);
Cary Clarkf5404bb2018-01-05 12:10:09 -05002322 canvas->drawImage(image, 0, 0);
Cary Clark14768f62018-10-29 20:33:51 -04002323 canvas->drawString(label, 30, image->height() / 4, paint);
Cary Clarkf5404bb2018-01-05 12:10:09 -05002324 canvas->drawString(
2325 image->isLazyGenerated() ? "is lazily generated" : "not lazily generated",
Cary Clark14768f62018-10-29 20:33:51 -04002326 20, image->height() * 3 / 4, paint);
Cary Clarkf5404bb2018-01-05 12:10:09 -05002327 };
2328 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2329 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04002330 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
2331 kOpaque_SkAlphaType, nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05002332 drawImage(image, "image");
2333 canvas->translate(image->width(), 0);
2334 drawImage(bitmapImage, "source");
2335 canvas->translate(-image->width(), image->height());
2336 drawImage(textureImage, "backEndTexture");
2337}
2338##
2339
Cary Clarkac47b882018-01-11 10:35:44 -05002340#SeeAlso isTextureBacked MakeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002341
2342#Method ##
2343
2344# ------------------------------------------------------------------------------
2345
Cary Clarke80cd442018-07-17 13:19:56 -04002346#Method sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target) const
Cary Clark61313f32018-10-08 14:57:48 -04002347#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05002348#Line # creates Image matching Color_Space if possible ##
Cary Clarka560c472017-11-27 10:44:06 -05002349
Cary Clarkac47b882018-01-11 10:35:44 -05002350Creates Image in target Color_Space.
2351Returns nullptr if Image could not be created.
Cary Clarka560c472017-11-27 10:44:06 -05002352
Cary Clarkac47b882018-01-11 10:35:44 -05002353Returns original Image if it is in target Color_Space.
2354Otherwise, converts pixels from Image Color_Space to target Color_Space.
2355If Image colorSpace returns nullptr, Image Color_Space is assumed to be sRGB.
2356
Cary Clarkac47b882018-01-11 10:35:44 -05002357#Param target Color_Space describing color range of returned Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002358
Cary Clarkac47b882018-01-11 10:35:44 -05002359#Return created Image in target Color_Space ##
2360
2361#Example
2362#Image 5
2363#Set sRGB
2364 sk_sp<SkColorSpace> normalColorSpace = SkColorSpace::MakeRGB(
2365 SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kSRGB_Gamut);
2366 sk_sp<SkColorSpace> wackyColorSpace = normalColorSpace->makeColorSpin();
2367 for (auto colorSpace : { normalColorSpace, wackyColorSpace } ) {
Cary Clarkc3c1c312018-07-18 09:25:15 -04002368 sk_sp<SkImage> colorSpaced = image->makeColorSpace(colorSpace);
2369 canvas->drawImage(colorSpaced, 0, 0);
2370 canvas->translate(128, 0);
Cary Clarkac47b882018-01-11 10:35:44 -05002371 }
2372##
2373
2374#SeeAlso MakeFromPixture MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05002375
2376#Method ##
2377
2378#Class SkImage ##
2379
2380#Topic Image ##