blob: 99e4cc30c9d736326d01f0a95d33247be0be8d5b [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
651#SeeAlso MakeFromYUVATexturesCopyWithExternalBackend
652
653#Method ##
654
655# ------------------------------------------------------------------------------
656
657#Method static sk_sp<SkImage> MakeFromYUVATexturesCopyWithExternalBackend(
658 GrContext* context,
659 SkYUVColorSpace yuvColorSpace,
660 const GrBackendTexture yuvaTextures[],
661 const SkYUVAIndex yuvaIndices[4],
662 SkISize imageSize,
663 GrSurfaceOrigin imageOrigin,
664 const GrBackendTexture& backendTexture,
665 sk_sp<SkColorSpace> imageColorSpace = nullptr)
666#In Constructor
667#Line # creates Image from planar YUV_ColorSpace, stored in texture ##
668
Cary Clark61313f32018-10-08 14:57:48 -0400669Creates an SkImage by flattening the specified YUVA planes into a single,
670interleaved RGBA image. backendTexture is used to store the result of flattening.
671yuvaTextures points to array of up to four YUVA textures which reside on GPU.
672YUVA textures contain YUVA planes which may be interleaved.
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400673
674#Param context GPU context ##
Cary Clark61313f32018-10-08 14:57:48 -0400675#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
676 kRec709_SkYUVColorSpace
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400677##
Cary Clark61313f32018-10-08 14:57:48 -0400678#Param yuvaTextures array of YUVA textures ##
679#Param yuvaIndices array indicating yuvaTextures element and channel
680 that map to Y, U, V, and A
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400681##
682#Param imageSize size of the resulting image ##
Cary Clark61313f32018-10-08 14:57:48 -0400683#Param imageOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
684#Param backendTexture resource that stores the final pixels ##
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400685#Param imageColorSpace range of colors of the resulting image; may be nullptr ##
686
687#Return created SkImage, or nullptr ##
688
689# seems too complicated to create an example for this
690#ToDo
691should this be moved to chrome only?
692##
693
694#NoExample
695##
696
697#SeeAlso MakeFromYUVATexturesCopy
698
699#Method ##
700
Cary Clarka560c472017-11-27 10:44:06 -0500701#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400702 const GrBackendTexture yuvTextures[3],
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400703 GrSurfaceOrigin imageOrigin,
704 sk_sp<SkColorSpace> imageColorSpace = nullptr)
Cary Clark61313f32018-10-08 14:57:48 -0400705#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500706#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500707
Brian Salomon6a426c12018-03-15 12:16:02 -0400708Creates Image from copy of yuvTextures, an array of textures on GPU.
709yuvTextures contain pixels for YUV planes of Image. Returned Image has the dimensions
710yuvTextures[0]. yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500711
Cary Clark61ca7c52018-01-02 11:34:14 -0500712#Param context GPU_Context ##
713#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
714 kRec709_SkYUVColorSpace
715##
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400716#Param yuvTextures array of YUV textures on GPU ##
717#Param imageOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
718#Param imageColorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500719
Cary Clark61ca7c52018-01-02 11:34:14 -0500720#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500721
Cary Clark61ca7c52018-01-02 11:34:14 -0500722# seems too complicated to create an example for this
723#ToDo
724should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500725##
726
Cary Clark61ca7c52018-01-02 11:34:14 -0500727#NoExample
728##
729
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400730#SeeAlso MakeFromYUVTexturesCopyWithExternalBackend MakeFromNV12TexturesCopy MakeFromYUVATexturesCopy
Cary Clarkcdc371a2018-09-18 07:31:37 -0400731
732#Method ##
733
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400734# ------------------------------------------------------------------------------
735
Cary Clarkcdc371a2018-09-18 07:31:37 -0400736#Method static sk_sp<SkImage> MakeFromYUVTexturesCopyWithExternalBackend(
737 GrContext* context, SkYUVColorSpace yuvColorSpace,
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400738 const GrBackendTexture yuvTextures[3], GrSurfaceOrigin imageOrigin,
739 const GrBackendTexture& backendTexture, sk_sp<SkColorSpace> imageColorSpace = nullptr);
Cary Clark61313f32018-10-08 14:57:48 -0400740#In Constructors
Cary Clarkcdc371a2018-09-18 07:31:37 -0400741#Line # creates Image from planar YUV_ColorSpace, stored in texture ##
742
743Creates Image from copy of yuvTextures, an array of textures on GPU.
744yuvTextures contain pixels for YUV planes of Image. Returned Image has the dimensions
745yuvTextures[0] and stores pixels in backendTexture. yuvColorSpace describes how YUV colors
746convert to RGB colors.
747
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400748#Param context GPU_Context ##
749#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
750 kRec709_SkYUVColorSpace
Cary Clarkcdc371a2018-09-18 07:31:37 -0400751##
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 ##
Cary Clark61313f32018-10-08 14:57:48 -0400754#Param backendTexture resource that stores final pixels ##
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400755#Param imageColorSpace range of colors; may be nullptr ##
Cary Clarkcdc371a2018-09-18 07:31:37 -0400756
757#Return created SkImage, or nullptr ##
758
759# seems too complicated to create an example for this
760#ToDo
761should this be moved to chrome only?
762##
763
764#NoExample
765##
766
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400767#SeeAlso MakeFromYUVTexturesCopy MakeFromNV12TexturesCopy MakeFromYUVATexturesCopyWithExternalBackend
Cary Clark61ca7c52018-01-02 11:34:14 -0500768
769#Method ##
770
771# ------------------------------------------------------------------------------
772
Cary Clarka560c472017-11-27 10:44:06 -0500773#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
774 SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400775 const GrBackendTexture nv12Textures[2],
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400776 GrSurfaceOrigin imageOrigin,
777 sk_sp<SkColorSpace> imageColorSpace = nullptr)
Cary Clark61313f32018-10-08 14:57:48 -0400778#In Constructors
Brian Salomon6a426c12018-03-15 12:16:02 -0400779#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clarka560c472017-11-27 10:44:06 -0500780
Cary Clark681287e2018-03-16 11:34:15 -0400781Creates Image from copy of nv12Textures, an array of textures on GPU.
Brian Salomon6a426c12018-03-15 12:16:02 -0400782nv12Textures[0] contains pixels for YUV_Component_Y plane.
783nv12Textures[1] contains pixels for YUV_Component_U plane,
Cary Clark61ca7c52018-01-02 11:34:14 -0500784followed by pixels for YUV_Component_V plane.
Cary Clark681287e2018-03-16 11:34:15 -0400785Returned Image has the dimensions nv12Textures[2].
786yuvColorSpace describes how YUV colors convert to RGB colors.
Cary Clarka560c472017-11-27 10:44:06 -0500787
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400788#Param context GPU_Context ##
789#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
790 kRec709_SkYUVColorSpace
Cary Clark61ca7c52018-01-02 11:34:14 -0500791##
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400792#Param nv12Textures array of YUV textures on GPU ##
793#Param imageOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
794#Param imageColorSpace range of colors; may be nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500795
Cary Clark61ca7c52018-01-02 11:34:14 -0500796#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500797
Cary Clark61ca7c52018-01-02 11:34:14 -0500798# seems too complicated to create an example for this
799#ToDo
800should this be moved to chrome only?
Cary Clarka560c472017-11-27 10:44:06 -0500801##
802
Cary Clark61ca7c52018-01-02 11:34:14 -0500803#NoExample
804##
805
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400806#SeeAlso MakeFromNV12TexturesCopyWithExternalBackend MakeFromYUVTexturesCopy MakeFromYUVATexturesCopy
Cary Clarkcdc371a2018-09-18 07:31:37 -0400807
808#Method ##
809
810#Method static sk_sp<SkImage> MakeFromNV12TexturesCopyWithExternalBackend(
811 GrContext* context,
812 SkYUVColorSpace yuvColorSpace,
813 const GrBackendTexture nv12Textures[2],
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400814 GrSurfaceOrigin imageOrigin,
815 const GrBackendTexture& backendTexture,
816 sk_sp<SkColorSpace> imageColorSpace = nullptr);
Cary Clark61313f32018-10-08 14:57:48 -0400817#In Constructors
Cary Clarkcdc371a2018-09-18 07:31:37 -0400818#Line # creates Image from planar YUV_ColorSpace, stored in texture ##
819
820Creates Image from copy of nv12Textures, an array of textures on GPU.
821nv12Textures[0] contains pixels for YUV_Component_Y plane.
822nv12Textures[1] contains pixels for YUV_Component_U plane,
823followed by pixels for YUV_Component_V plane.
824Returned Image has the dimensions nv12Textures[2] and stores pixels in backendTexture.
825yuvColorSpace describes how YUV colors convert to RGB colors.
826
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400827#Param context GPU_Context ##
828#Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace,
829 kRec709_SkYUVColorSpace
Cary Clarkcdc371a2018-09-18 07:31:37 -0400830##
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400831#Param nv12Textures array of YUV textures on GPU ##
832#Param imageOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark61313f32018-10-08 14:57:48 -0400833#Param backendTexture resource that stores final pixels ##
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400834#Param imageColorSpace range of colors; may be nullptr ##
Cary Clarkcdc371a2018-09-18 07:31:37 -0400835
836#Return created Image, or nullptr ##
837
838# seems too complicated to create an example for this
839#ToDo
840should this be moved to chrome only?
841##
842
843#NoExample
844##
845
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400846#SeeAlso MakeFromNV12TexturesCopy MakeFromYUVTexturesCopy MakeFromYUVATexturesCopyWithExternalBackend
Cary Clarka560c472017-11-27 10:44:06 -0500847
848#Method ##
849
850# ------------------------------------------------------------------------------
851
Cary Clark4855f782018-02-06 09:41:53 -0500852# currently uncalled by any test or client ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500853#Bug 7424
Cary Clark61ca7c52018-01-02 11:34:14 -0500854
Cary Clark56356312018-02-08 14:45:18 -0500855#EnumClass BitDepth
Cary Clark682c58d2018-05-16 07:07:07 -0400856#Line # options for MakeFromPicture ##
Cary Clarka560c472017-11-27 10:44:06 -0500857#Code
Cary Clarka90ea222018-10-16 10:30:28 -0400858#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500859##
860
861#Const kU8 0
Cary Clark682c58d2018-05-16 07:07:07 -0400862#Line # uses 8-bit unsigned int per Color component ##
Cary Clarkffb3d682018-05-17 12:17:28 -0400863Use 8 bits per ARGB component using unsigned integer format.
Cary Clarka560c472017-11-27 10:44:06 -0500864##
865#Const kF16 1
Cary Clark682c58d2018-05-16 07:07:07 -0400866#Line # uses 16-bit float per Color component ##
Cary Clarkffb3d682018-05-17 12:17:28 -0400867Use 16 bits per ARGB component using half-precision floating point format.
Cary Clarka560c472017-11-27 10:44:06 -0500868##
869
Cary Clark61ca7c52018-01-02 11:34:14 -0500870#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500871##
872
Cary Clark61ca7c52018-01-02 11:34:14 -0500873#SeeAlso MakeFromPicture
Cary Clarka560c472017-11-27 10:44:06 -0500874
Cary Clark56356312018-02-08 14:45:18 -0500875#EnumClass ##
Cary Clarka560c472017-11-27 10:44:06 -0500876
877# ------------------------------------------------------------------------------
878
879#Method static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
880 const SkMatrix* matrix, const SkPaint* paint,
881 BitDepth bitDepth,
882 sk_sp<SkColorSpace> colorSpace)
Cary Clark61313f32018-10-08 14:57:48 -0400883#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500884#Line # creates Image from Picture ##
Cary Clarka560c472017-11-27 10:44:06 -0500885
Cary Clark61ca7c52018-01-02 11:34:14 -0500886Creates Image from picture. Returned Image width and height are set by dimensions.
887Image draws picture with matrix and paint, set to bitDepth and colorSpace.
Cary Clarka560c472017-11-27 10:44:06 -0500888
Cary Clark61ca7c52018-01-02 11:34:14 -0500889If matrix is nullptr, draws with identity Matrix. If paint is nullptr, draws
Cary Clark682c58d2018-05-16 07:07:07 -0400890with default Paint. colorSpace may be nullptr.
Cary Clarka560c472017-11-27 10:44:06 -0500891
Cary Clark61ca7c52018-01-02 11:34:14 -0500892#Param picture stream of drawing commands ##
893#Param dimensions width and height ##
894#Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ##
895#Param paint Paint to apply transparency, filtering, and so on; may be nullptr ##
Cary Clark682c58d2018-05-16 07:07:07 -0400896#Param bitDepth 8-bit integer or 16-bit float: per component ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500897#Param colorSpace range of colors; may be nullptr ##
898
899#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -0500900
901#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500902 SkPaint paint;
903 SkPictureRecorder recorder;
904 SkCanvas* recordingCanvas = recorder.beginRecording(50, 50);
905 for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) {
906 paint.setColor(color);
907 recordingCanvas->drawRect({10, 10, 30, 40}, paint);
908 recordingCanvas->translate(10, 10);
909 recordingCanvas->scale(1.2f, 1.4f);
910 }
911 sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
912 int x = 0, y = 0;
913 for (auto alpha : { 70, 140, 210 } ) {
914 paint.setAlpha(alpha);
915 auto srgbColorSpace = SkColorSpace::MakeSRGB();
916 sk_sp<SkImage> image = SkImage::MakeFromPicture(playback, {50, 50}, nullptr, &paint,
917 SkImage::BitDepth::kU8, srgbColorSpace);
918 canvas->drawImage(image, x, y);
919 x += 70; y += 70;
920 }
Cary Clarka560c472017-11-27 10:44:06 -0500921##
922
Cary Clark61ca7c52018-01-02 11:34:14 -0500923#SeeAlso SkCanvas::drawPicture
Cary Clarka560c472017-11-27 10:44:06 -0500924
925#Method ##
926
927# ------------------------------------------------------------------------------
928
Cary Clark9548ea92018-09-13 15:26:33 -0400929#Method static sk_sp<SkImage> MakeFromAHardwareBuffer(
930 AHardwareBuffer* hardwareBuffer,
931 SkAlphaType alphaType = kPremul_SkAlphaType,
932 sk_sp<SkColorSpace> colorSpace = nullptr,
933 GrSurfaceOrigin surfaceOrigin = kTopLeft_GrSurfaceOrigin)
Cary Clark61313f32018-10-08 14:57:48 -0400934#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500935#Line # creates Image from Android hardware buffer ##
Cary Clarka560c472017-11-27 10:44:06 -0500936
Cary Clark4855f782018-02-06 09:41:53 -0500937#Bug 7447
Cary Clarka560c472017-11-27 10:44:06 -0500938
Cary Clark61ca7c52018-01-02 11:34:14 -0500939Creates Image from Android hardware buffer.
940Returned Image takes a reference on the buffer.
Cary Clarka560c472017-11-27 10:44:06 -0500941
Cary Clark61ca7c52018-01-02 11:34:14 -0500942Only available on Android, when __ANDROID_API__ is defined to be 26 or greater.
Cary Clarka560c472017-11-27 10:44:06 -0500943
Cary Clark61ca7c52018-01-02 11:34:14 -0500944#Param hardwareBuffer AHardwareBuffer Android hardware buffer ##
Cary Clark681287e2018-03-16 11:34:15 -0400945#Param alphaType one of: #list_of_alpha_types#
Cary Clark61ca7c52018-01-02 11:34:14 -0500946##
947#Param colorSpace range of colors; may be nullptr ##
Cary Clark9548ea92018-09-13 15:26:33 -0400948#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clarka560c472017-11-27 10:44:06 -0500949
Cary Clark61ca7c52018-01-02 11:34:14 -0500950#Return created Image, or nullptr ##
951
952#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500953##
954
Cary Clark61ca7c52018-01-02 11:34:14 -0500955#SeeAlso MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -0500956
957#Method ##
958
959# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -0500960#Subtopic Property
Cary Clark4855f782018-02-06 09:41:53 -0500961#Line # values and attributes ##
962##
Cary Clarka560c472017-11-27 10:44:06 -0500963
964#Method int width() const
Cary Clark4855f782018-02-06 09:41:53 -0500965#In Property
966#Line # returns pixel column count ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500967Returns pixel count in each row.
968
969#Return pixel width in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500970
971#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500972#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500973#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500974 canvas->translate(10, 10);
975 canvas->drawImage(image, 0, 0);
976 canvas->translate(0, image->height());
977 SkPaint paint;
978 paint.setTextAlign(SkPaint::kCenter_Align);
979 canvas->drawLine(0, 10, image->width(), 10, paint);
980 canvas->drawString("width", image->width() / 2, 25, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500981##
982
Cary Clark61ca7c52018-01-02 11:34:14 -0500983#SeeAlso dimensions() height()
Cary Clarka560c472017-11-27 10:44:06 -0500984
985#Method ##
986
987# ------------------------------------------------------------------------------
988
989#Method int height() const
Cary Clark4855f782018-02-06 09:41:53 -0500990#In Property
991#Line # returns pixel row count ##
Cary Clark2f466242017-12-11 16:03:17 -0500992Returns pixel row count.
993
Cary Clark61ca7c52018-01-02 11:34:14 -0500994#Return pixel height in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500995
996#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500997#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500998#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500999 canvas->translate(10, 10);
1000 canvas->drawImage(image, 0, 0);
1001 canvas->translate(image->width(), 0);
1002 SkPaint paint;
1003 paint.setTextAlign(SkPaint::kCenter_Align);
Cary Clark61ca7c52018-01-02 11:34:14 -05001004 canvas->drawLine(10, 0, 10, image->height(), paint);
Cary Clark92694be2018-10-25 08:15:36 -04001005 canvas->drawString("height", 34, image->height() / 2, paint);
Cary Clarkac47b882018-01-11 10:35:44 -05001006##
Cary Clarka560c472017-11-27 10:44:06 -05001007
Cary Clark61ca7c52018-01-02 11:34:14 -05001008#SeeAlso dimensions() width()
Cary Clarka560c472017-11-27 10:44:06 -05001009
1010#Method ##
1011
1012# ------------------------------------------------------------------------------
1013
1014#Method SkISize dimensions() const
Cary Clark4855f782018-02-06 09:41:53 -05001015#In Property
1016#Line # returns width() and height() ##
Cary Clark681287e2018-03-16 11:34:15 -04001017
Cary Clark2f466242017-12-11 16:03:17 -05001018Returns ISize { width(), height() }.
1019
1020#Return integral size of width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -05001021
1022#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001023#Image 4
1024 SkISize dimensions = image->dimensions();
1025 SkIRect bounds = image->bounds();
1026 SkIRect dimensionsAsBounds = SkIRect::MakeSize(dimensions);
1027 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clark681287e2018-03-16 11:34:15 -04001028#StdOut
1029dimensionsAsBounds == bounds
1030##
Cary Clarka560c472017-11-27 10:44:06 -05001031##
1032
Cary Clark61ca7c52018-01-02 11:34:14 -05001033#SeeAlso height() width() bounds()
Cary Clarka560c472017-11-27 10:44:06 -05001034
1035#Method ##
1036
1037# ------------------------------------------------------------------------------
1038
1039#Method SkIRect bounds() const
Cary Clark4855f782018-02-06 09:41:53 -05001040#In Property
1041#Line # returns width() and height() as Rectangle ##
Cary Clark2f466242017-12-11 16:03:17 -05001042Returns IRect { 0, 0, width(), height() }.
1043
1044#Return integral rectangle from origin to width() and height() ##
Cary Clarka560c472017-11-27 10:44:06 -05001045
1046#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001047#Height 128
1048#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -05001049 SkIRect bounds = image->bounds();
Cary Clarkac47b882018-01-11 10:35:44 -05001050 for (int x : { 0, bounds.width() } ) {
1051 for (int y : { 0, bounds.height() } ) {
Cary Clark61ca7c52018-01-02 11:34:14 -05001052 canvas->drawImage(image, x, y);
1053 }
1054 }
Cary Clarka560c472017-11-27 10:44:06 -05001055##
1056
Cary Clark682c58d2018-05-16 07:07:07 -04001057#SeeAlso dimensions()
Cary Clarka560c472017-11-27 10:44:06 -05001058
1059#Method ##
1060
1061# ------------------------------------------------------------------------------
1062
1063#Method uint32_t uniqueID() const
Cary Clark4855f782018-02-06 09:41:53 -05001064#In Property
Cary Clark682c58d2018-05-16 07:07:07 -04001065#Line # returns identifier for Image ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001066Returns value unique to image. Image contents cannot change after Image is
1067created. Any operation to create a new Image will receive generate a new
1068unique number.
1069
1070#Return unique identifier ##
Cary Clarka560c472017-11-27 10:44:06 -05001071
1072#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001073#Image 5
1074#Height 156
1075 sk_sp<SkImage> subset = image->makeSubset({10, 20, 90, 100});
1076 canvas->drawImage(image, 0, 0);
1077 canvas->drawImage(subset, 128, 0);
1078 SkPaint paint;
1079 SkString s;
1080 s.printf("original id: %d", image->uniqueID());
1081 canvas->drawString(s, 20, image->height() + 20, paint);
1082 s.printf("subset id: %d", subset->uniqueID());
1083 canvas->drawString(s, 148, subset->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001084##
1085
Cary Clark61ca7c52018-01-02 11:34:14 -05001086#SeeAlso isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001087
1088#Method ##
1089
1090# ------------------------------------------------------------------------------
1091
1092#Method SkAlphaType alphaType() const
Cary Clark4855f782018-02-06 09:41:53 -05001093#In Property
1094#Line # returns Alpha_Type ##
Cary Clark681287e2018-03-16 11:34:15 -04001095Returns Alpha_Type, one of: #list_of_alpha_types#.
Cary Clark61ca7c52018-01-02 11:34:14 -05001096
1097Alpha_Type returned was a parameter to an Image constructor,
1098or was parsed from encoded data.
1099
1100#Return Alpha_Type in Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001101
1102#Example
Cary Clark61ca7c52018-01-02 11:34:14 -05001103#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -05001104#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -05001105 const char* alphaTypeStr[] = { "Unknown", "Opaque", "Premul", "Unpremul" };
1106 SkAlphaType alphaType = image->alphaType();
Cary Clarkac47b882018-01-11 10:35:44 -05001107 canvas->drawImage(image, 16, 0);
Cary Clarkffb3d682018-05-17 12:17:28 -04001108 canvas->drawString(alphaTypeStr[(int) alphaType], 20, image->height() + 20, SkPaint());
Cary Clarka560c472017-11-27 10:44:06 -05001109##
1110
Cary Clark61ca7c52018-01-02 11:34:14 -05001111#SeeAlso SkImageInfo::alphaType
Cary Clarka560c472017-11-27 10:44:06 -05001112
1113#Method ##
1114
1115# ------------------------------------------------------------------------------
1116
Greg Daniel56008aa2018-03-14 15:33:42 -04001117#Method SkColorType colorType() const
1118#In Property
1119#Line # returns Color_Type ##
1120
1121Returns Color_Type if known; otherwise, returns kUnknown_SkColorType.
1122
1123#Return Color_Type of Image ##
1124
1125#Example
Cary Clarkffb3d682018-05-17 12:17:28 -04001126#Image 4
1127#Height 96
1128 const char* colors[] = { "Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
1129 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16" };
1130 SkColorType colorType = image->colorType();
1131 canvas->drawImage(image, 16, 0);
1132 canvas->drawString(colors[(int) colorType], 20, image->height() + 20, SkPaint());
Greg Daniel56008aa2018-03-14 15:33:42 -04001133##
1134
1135#SeeAlso SkImageInfo::colorType
1136
1137#Method ##
1138
1139# ------------------------------------------------------------------------------
1140
Cary Clarka560c472017-11-27 10:44:06 -05001141#Method SkColorSpace* colorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001142#In Property
1143#Line # returns Color_Space ##
Cary Clark2f466242017-12-11 16:03:17 -05001144Returns Color_Space, the range of colors, associated with Image. The
1145reference count of Color_Space is unchanged. The returned Color_Space is
1146immutable.
Cary Clarka560c472017-11-27 10:44:06 -05001147
Cary Clark61dfc3a2018-01-03 08:37:53 -05001148Color_Space returned was passed to an Image constructor,
1149or was parsed from encoded data. Color_Space returned may be ignored when Image
1150is drawn, depending on the capabilities of the Surface receiving the drawing.
Cary Clark2f466242017-12-11 16:03:17 -05001151
1152#Return Color_Space in Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001153
1154#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001155#Image 3
1156#Set sRGB
1157 SkPixmap pixmap;
1158 source.peekPixels(&pixmap);
1159 canvas->scale(.25f, .25f);
1160 int y = 0;
1161 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1162 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1163 int x = 0;
1164 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1165 for (int index = 0; index < 2; ++index) {
1166 pixmap.setColorSpace(colorSpace);
1167 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1168 canvas->drawImage(image, x, y);
1169 colorSpace = image->colorSpace()->makeColorSpin();
1170 x += 512;
1171 }
1172 y += 512;
1173 }
Cary Clarka560c472017-11-27 10:44:06 -05001174##
1175
Cary Clark61dfc3a2018-01-03 08:37:53 -05001176#SeeAlso refColorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001177
1178#Method ##
1179
1180# ------------------------------------------------------------------------------
1181
1182#Method sk_sp<SkColorSpace> refColorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -05001183#In Property
1184#Line # returns Image_Info Color_Space ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001185Returns a smart pointer to Color_Space, the range of colors, associated with
1186Image. The smart pointer tracks the number of objects sharing this
1187SkColorSpace reference so the memory is released when the owners destruct.
1188
1189The returned SkColorSpace is immutable.
1190
1191Color_Space returned was passed to an Image constructor,
1192or was parsed from encoded data. Color_Space returned may be ignored when Image
1193is drawn, depending on the capabilities of the Surface receiving the drawing.
1194
1195#Return Color_Space in Image, or nullptr, wrapped in a smart pointer ##
Cary Clarka560c472017-11-27 10:44:06 -05001196
1197#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001198#Image 3
1199#Set sRGB
1200 SkPixmap pixmap;
1201 source.peekPixels(&pixmap);
1202 canvas->scale(.25f, .25f);
1203 int y = 0;
1204 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
1205 SkColorSpace::kSRGB_RenderTargetGamma } ) {
1206 int x = 0;
1207 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
1208 for (int index = 0; index < 2; ++index) {
1209 pixmap.setColorSpace(colorSpace);
1210 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
1211 canvas->drawImage(image, x, y);
1212 colorSpace = image->refColorSpace()->makeColorSpin();
1213 x += 512;
1214 }
1215 y += 512;
1216 }
Cary Clarka560c472017-11-27 10:44:06 -05001217##
1218
Cary Clark61dfc3a2018-01-03 08:37:53 -05001219#SeeAlso colorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -05001220
1221#Method ##
1222
1223# ------------------------------------------------------------------------------
1224
1225#Method bool isAlphaOnly() const
Cary Clark4855f782018-02-06 09:41:53 -05001226#In Property
1227#Line # returns if pixels represent a transparency mask ##
Cary Clark2f466242017-12-11 16:03:17 -05001228Returns true if Image pixels represent transparency only. If true, each pixel
1229is packed in 8 bits as defined by kAlpha_8_SkColorType.
Cary Clarka560c472017-11-27 10:44:06 -05001230
Cary Clark2f466242017-12-11 16:03:17 -05001231#Return true if pixels represent a transparency mask ##
Cary Clarka560c472017-11-27 10:44:06 -05001232
1233#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001234 uint8_t pmColors = 0;
1235 sk_sp<SkImage> image = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1), &pmColors, 1});
1236 SkDebugf("alphaOnly = %s\n", image->isAlphaOnly() ? "true" : "false");
1237#StdOut
1238alphaOnly = true
1239##
Cary Clarka560c472017-11-27 10:44:06 -05001240##
1241
Cary Clark61dfc3a2018-01-03 08:37:53 -05001242#SeeAlso alphaType isOpaque
Cary Clarka560c472017-11-27 10:44:06 -05001243
1244#Method ##
1245
1246# ------------------------------------------------------------------------------
1247
1248#Method bool isOpaque() const
Cary Clark4855f782018-02-06 09:41:53 -05001249#In Property
1250#Line # returns if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clark61dfc3a2018-01-03 08:37:53 -05001251Returns true if pixels ignore their Alpha value and are treated as fully opaque.
Cary Clark2f466242017-12-11 16:03:17 -05001252
1253#Return true if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clarka560c472017-11-27 10:44:06 -05001254
1255#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001256 auto check_isopaque = [](const SkImageInfo& imageInfo) -> void {
1257 auto surface(SkSurface::MakeRaster(imageInfo));
1258 auto image(surface->makeImageSnapshot());
1259 SkDebugf("isOpaque = %s\n", image->isOpaque() ? "true" : "false");
1260 };
1261
1262 check_isopaque(SkImageInfo::MakeN32Premul(5, 5));
1263 check_isopaque(SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType));
1264#StdOut
1265isOpaque = false
1266isOpaque = true
1267##
Cary Clarka560c472017-11-27 10:44:06 -05001268##
1269
Cary Clark61dfc3a2018-01-03 08:37:53 -05001270#SeeAlso alphaType isAlphaOnly
Cary Clarka560c472017-11-27 10:44:06 -05001271
1272#Method ##
1273
1274# ------------------------------------------------------------------------------
1275
1276#Method sk_sp<SkShader> makeShader(SkShader::TileMode tileMode1, SkShader::TileMode tileMode2,
1277 const SkMatrix* localMatrix = nullptr) const
Cary Clark61313f32018-10-08 14:57:48 -04001278#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05001279#Line # creates Shader, Paint element that can tile Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001280
Cary Clark61dfc3a2018-01-03 08:37:53 -05001281Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1282SkShader::TileMode rules to fill drawn area outside Image. localMatrix permits
1283transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001284
Cary Clark5538c132018-06-14 12:28:14 -04001285#Param tileMode1 tiling on x-axis, one of: SkShader::kClamp_TileMode,
1286 SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
Cary Clark61dfc3a2018-01-03 08:37:53 -05001287##
Cary Clark5538c132018-06-14 12:28:14 -04001288#Param tileMode2 tiling on y-axis, one of: SkShader::kClamp_TileMode,
1289 SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode
Cary Clark61dfc3a2018-01-03 08:37:53 -05001290##
1291#Param localMatrix Image transformation, or nullptr ##
1292
1293#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001294
1295#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001296#Image 4
1297SkMatrix matrix;
1298matrix.setRotate(45);
1299SkPaint paint;
1300paint.setShader(image->makeShader(SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode,
1301 &matrix));
1302canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001303##
1304
Cary Clark61dfc3a2018-01-03 08:37:53 -05001305#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001306
1307#Method ##
1308
1309# ------------------------------------------------------------------------------
1310
1311#Method sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const
1312
Cary Clark61dfc3a2018-01-03 08:37:53 -05001313Creates Shader from Image. Shader dimensions are taken from Image. Shader uses
1314SkShader::kClamp_TileMode to fill drawn area outside Image. localMatrix permits
1315transforming Image before Canvas_Matrix is applied.
Cary Clarka560c472017-11-27 10:44:06 -05001316
Cary Clark61dfc3a2018-01-03 08:37:53 -05001317#Param localMatrix Image transformation, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001318
Cary Clark61dfc3a2018-01-03 08:37:53 -05001319#Return Shader containing Image ##
Cary Clarka560c472017-11-27 10:44:06 -05001320
1321#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -05001322#Image 5
1323SkMatrix matrix;
1324matrix.setRotate(45);
1325matrix.postTranslate(125, 30);
1326SkPaint paint;
1327paint.setShader(image->makeShader(&matrix));
1328canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -05001329##
1330
Cary Clarkf5404bb2018-01-05 12:10:09 -05001331#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001332
1333#Method ##
1334
1335# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -05001336#Subtopic Pixels
Cary Clark78de7512018-02-07 07:27:09 -05001337#Line # read and write pixel values ##
1338##
Cary Clarka560c472017-11-27 10:44:06 -05001339
1340#Method bool peekPixels(SkPixmap* pixmap) const
Cary Clark78de7512018-02-07 07:27:09 -05001341#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001342#Line # returns Pixmap if possible ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001343Copies Image pixel address, row bytes, and Image_Info to pixmap, if address
1344is available, and returns true. If pixel address is not available, return
1345false and leave pixmap unchanged.
Cary Clarka560c472017-11-27 10:44:06 -05001346
Cary Clarkf5404bb2018-01-05 12:10:09 -05001347#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
Cary Clarka560c472017-11-27 10:44:06 -05001348
Cary Clarkf5404bb2018-01-05 12:10:09 -05001349#Return true if Image has direct access to pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001350
1351#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001352 SkBitmap bitmap;
1353 bitmap.allocPixels(SkImageInfo::MakeN32Premul(12, 11));
1354 SkCanvas offscreen(bitmap);
1355 offscreen.clear(SK_ColorWHITE);
1356 SkPaint paint;
1357 offscreen.drawString("%", 1, 10, paint);
1358 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
1359 SkPixmap pixmap;
1360 if (image->peekPixels(&pixmap)) {
1361 const SkPMColor* pixels = pixmap.addr32();
1362 SkPMColor pmWhite = pixels[0];
1363 for (int y = 0; y < image->height(); ++y) {
1364 for (int x = 0; x < image->width(); ++x) {
1365 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
1366 }
1367 SkDebugf("\n");
1368 }
1369 }
1370#StdOut
1371------------
1372--xx----x---
1373-x--x--x----
1374-x--x--x----
1375-x--x-x-----
1376--xx-xx-xx--
1377-----x-x--x-
1378----x--x--x-
1379----x--x--x-
1380---x----xx--
1381------------
1382##
Cary Clarka560c472017-11-27 10:44:06 -05001383##
1384
Cary Clarkf5404bb2018-01-05 12:10:09 -05001385#SeeAlso readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001386
1387#Method ##
1388
1389# ------------------------------------------------------------------------------
1390
1391#Method GrTexture* getTexture() const
Cary Clark682c58d2018-05-16 07:07:07 -04001392#Deprecated
Cary Clarka560c472017-11-27 10:44:06 -05001393#Method ##
1394
1395# ------------------------------------------------------------------------------
1396
1397#Method bool isTextureBacked() const
Cary Clark78de7512018-02-07 07:27:09 -05001398#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001399#Line # returns if Image was created from GPU_Texture ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001400Returns true the contents of Image was created on or uploaded to GPU memory,
1401and is available as a GPU_Texture.
Cary Clarka560c472017-11-27 10:44:06 -05001402
Cary Clarkf5404bb2018-01-05 12:10:09 -05001403#Return true if Image is a GPU_Texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001404
1405#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001406#Image 5
1407#Platform gpu
1408auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1409 if (nullptr == image) {
1410 return;
1411 }
1412 SkPaint paint;
1413 paint.setAntiAlias(true);
1414 paint.setTextAlign(SkPaint::kCenter_Align);
1415 canvas->drawImage(image, 0, 0);
1416 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1417 canvas->drawString(image->isTextureBacked() ? "is GPU texture" : "not GPU texture",
1418 image->width() / 2, image->height() * 3 / 4, paint);
1419};
1420sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1421sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001422 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1423 kOpaque_SkAlphaType, nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05001424drawImage(image, "image");
1425canvas->translate(image->width(), 0);
1426drawImage(bitmapImage, "source");
1427canvas->translate(-image->width(), image->height());
1428drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001429##
1430
Cary Clarkf5404bb2018-01-05 12:10:09 -05001431#SeeAlso MakeFromTexture isValid
Cary Clarka560c472017-11-27 10:44:06 -05001432
1433#Method ##
1434
1435# ------------------------------------------------------------------------------
1436
1437#Method bool isValid(GrContext* context) const
Cary Clark4855f782018-02-06 09:41:53 -05001438#In Property
1439#Line # returns if Image can draw to Raster_Surface or GPU_Context ##
Cary Clarkf5404bb2018-01-05 12:10:09 -05001440Returns true if Image can be drawn on either Raster_Surface or GPU_Surface.
1441If context is nullptr, tests if Image draws on Raster_Surface;
1442otherwise, tests if Image draws on GPU_Surface associated with context.
Cary Clarka560c472017-11-27 10:44:06 -05001443
Cary Clarkf5404bb2018-01-05 12:10:09 -05001444Image backed by GPU_Texture may become invalid if associated GrContext is
1445invalid. Lazy_Image may be invalid and may not draw to Raster_Surface or
1446GPU_Surface or both.
Cary Clarka560c472017-11-27 10:44:06 -05001447
Cary Clark61ca7c52018-01-02 11:34:14 -05001448#Param context GPU_Context ##
Cary Clarka560c472017-11-27 10:44:06 -05001449
Cary Clarkf5404bb2018-01-05 12:10:09 -05001450#Return true if Image can be drawn ##
Cary Clarka560c472017-11-27 10:44:06 -05001451
1452#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001453#Image 5
1454#Platform gpu
1455auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1456 if (nullptr == image) {
1457 return;
1458 }
1459 SkPaint paint;
1460 paint.setAntiAlias(true);
1461 paint.setTextAlign(SkPaint::kCenter_Align);
1462 canvas->drawImage(image, 0, 0);
1463 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1464 if (canvas->getGrContext()) {
1465 canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" :
1466 "not valid on GPU", image->width() / 2, image->height() * 5 / 8, paint);
1467 }
1468 canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" :
1469 "not valid on CPU", image->width() / 2, image->height() * 7 / 8, paint);
1470};
1471sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1472sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001473 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1474 kOpaque_SkAlphaType, nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05001475drawImage(image, "image");
1476canvas->translate(image->width(), 0);
1477drawImage(bitmapImage, "source");
1478canvas->translate(-image->width(), image->height());
1479drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001480##
1481
Cary Clarkf5404bb2018-01-05 12:10:09 -05001482#SeeAlso isTextureBacked isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001483
1484#Method ##
1485
1486# ------------------------------------------------------------------------------
1487
Robert Phillipsc5509952018-04-04 15:54:55 -04001488#Method GrBackendTexture getBackendTexture(bool flushPendingGrContextIO,
1489 GrSurfaceOrigin* origin = nullptr) const
1490#In Property
1491#Line # returns GPU reference to Image as texture ##
1492
Cary Clark682c58d2018-05-16 07:07:07 -04001493Retrieves the back-end texture. If Image has no back-end texture, an invalid
Cary Clarkba75aee2018-04-05 08:18:41 -04001494object is returned. Call GrBackendTexture::isValid to determine if the result
1495is valid.
1496
1497If flushPendingGrContextIO is true, completes deferred I/O operations.
Robert Phillipsc5509952018-04-04 15:54:55 -04001498
1499If origin in not nullptr, copies location of content drawn into Image.
1500
1501#Param flushPendingGrContextIO flag to flush outstanding requests ##
1502#Param origin storage for one of: kTopLeft_GrSurfaceOrigin,
1503 kBottomLeft_GrSurfaceOrigin; or nullptr
1504##
1505
Cary Clarkba75aee2018-04-05 08:18:41 -04001506#Return back-end API texture handle; invalid on failure ##
Robert Phillipsc5509952018-04-04 15:54:55 -04001507
Cary Clarkba75aee2018-04-05 08:18:41 -04001508#Example
1509#Image 3
1510#Platform gpu
Brian Osman584b5012018-04-13 15:48:26 -04001511 GrContext* grContext = canvas->getGrContext();
1512 if (!grContext) {
1513 canvas->drawString("GPU only!", 20, 40, SkPaint());
1514 return;
1515 }
1516 sk_sp<SkImage> imageFromBackend = SkImage::MakeFromAdoptedTexture(grContext, backEndTexture,
1517 kBottomLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1518 GrBackendTexture textureFromImage = imageFromBackend->getBackendTexture(false);
1519 if (!textureFromImage.isValid()) {
1520 return;
1521 }
1522 sk_sp<SkImage> imageFromTexture = SkImage::MakeFromAdoptedTexture(grContext, textureFromImage,
1523 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1524 canvas->drawImage(imageFromTexture, 0, 0);
Cary Clarkba75aee2018-04-05 08:18:41 -04001525 canvas->drawImage(imageFromBackend, 128, 128);
Robert Phillipsc5509952018-04-04 15:54:55 -04001526##
1527
1528#SeeAlso MakeFromTexture isTextureBacked
1529
1530#Method ##
1531
1532# ------------------------------------------------------------------------------
1533
Cary Clarka560c472017-11-27 10:44:06 -05001534#Enum CachingHint
Cary Clark682c58d2018-05-16 07:07:07 -04001535#Line # options for readPixels and scalePixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001536#Code
Cary Clarka90ea222018-10-16 10:30:28 -04001537#Populate
Cary Clarka560c472017-11-27 10:44:06 -05001538##
1539
Cary Clarkac47b882018-01-11 10:35:44 -05001540CachingHint selects whether Skia may internally cache Bitmaps generated by
1541decoding Image, or by copying Image from GPU to CPU. The default behavior
Cary Clark682c58d2018-05-16 07:07:07 -04001542allows caching Bitmaps.
Cary Clarkac47b882018-01-11 10:35:44 -05001543
1544Choose kDisallow_CachingHint if Image pixels are to be used only once, or
1545if Image pixels reside in a cache outside of Skia, or to reduce memory pressure.
1546
1547Choosing kAllow_CachingHint does not ensure that pixels will be cached.
1548Image pixels may not be cached if memory requirements are too large or
1549pixels are not accessible.
Cary Clarka560c472017-11-27 10:44:06 -05001550
1551#Const kAllow_CachingHint 0
Cary Clark682c58d2018-05-16 07:07:07 -04001552#Line # allows internally caching decoded and copied pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001553##
1554#Const kDisallow_CachingHint 1
Cary Clark682c58d2018-05-16 07:07:07 -04001555#Line # disallows internally caching decoded and copied pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001556##
1557
Cary Clarkac47b882018-01-11 10:35:44 -05001558#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001559##
1560
Cary Clarkac47b882018-01-11 10:35:44 -05001561#SeeAlso readPixels scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001562
1563#Enum ##
1564
1565# ------------------------------------------------------------------------------
1566
1567#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1568 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001569#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001570#Line # copies and converts pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001571
Cary Clarkac47b882018-01-11 10:35:44 -05001572Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY),
Cary Clark682c58d2018-05-16 07:07:07 -04001573and does not exceed Image (width(), height()).
Cary Clarkac47b882018-01-11 10:35:44 -05001574
1575dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
1576destination. dstRowBytes specifics the gap from one destination row to the next.
1577Returns true if pixels are copied. Returns false if:
1578#List
1579# dstInfo.addr() equals nullptr ##
1580# dstRowBytes is less than dstInfo.minRowBytes ##
1581# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001582##
1583
Cary Clarkac47b882018-01-11 10:35:44 -05001584Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1585kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match.
1586If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace must match.
1587If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType must
1588match. If Image Color_Space is nullptr, dstInfo.colorSpace must match. Returns
1589false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001590
Cary Clarkac47b882018-01-11 10:35:44 -05001591srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark682c58d2018-05-16 07:07:07 -04001592false if width() or height() is zero or negative.
Cary Clark2be81cf2018-09-13 12:04:30 -04001593Returns false if #Formula # abs(srcX) >= Image width() ##, or if #Formula # abs(srcY) >= Image height() ##.
Cary Clarka560c472017-11-27 10:44:06 -05001594
Cary Clarkac47b882018-01-11 10:35:44 -05001595If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1596If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1597
1598#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1599#Param dstPixels destination pixel storage ##
1600#Param dstRowBytes destination row length ##
1601#Param srcX column index whose absolute value is less than width() ##
1602#Param srcY row index whose absolute value is less than height() ##
1603#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1604
1605#Return true if pixels are copied to dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001606
1607#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001608#Image 3
1609 canvas->scale(.5f, .5f);
1610 const int width = 32;
1611 const int height = 32;
1612 std::vector<int32_t> dstPixels;
1613 dstPixels.resize(height * width * 4);
1614 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1615 for (int y = 0; y < 512; y += height ) {
1616 for (int x = 0; x < 512; x += width ) {
1617 if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) {
1618 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
1619 SkBitmap bitmap;
1620 bitmap.installPixels(dstPixmap);
1621 canvas->drawBitmap(bitmap, 0, 0);
1622 }
1623 canvas->translate(48, 0);
1624 }
1625 canvas->translate(-16 * 48, 48);
1626 }
Cary Clarka560c472017-11-27 10:44:06 -05001627##
1628
Cary Clarkac47b882018-01-11 10:35:44 -05001629#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001630
1631#Method ##
1632
1633# ------------------------------------------------------------------------------
1634
1635#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY,
1636 CachingHint cachingHint = kAllow_CachingHint) const
1637
Cary Clarkac47b882018-01-11 10:35:44 -05001638Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and
Cary Clark682c58d2018-05-16 07:07:07 -04001639does not exceed Image (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001640
Cary Clarkac47b882018-01-11 10:35:44 -05001641dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
1642and row bytes of destination. dst.rowBytes specifics the gap from one destination
1643row to the next. Returns true if pixels are copied. Returns false if:
1644#List
1645# dst pixel storage equals nullptr ##
1646# dst.rowBytes is less than SkImageInfo::minRowBytes ##
1647# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001648##
1649
Cary Clarkac47b882018-01-11 10:35:44 -05001650Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1651kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1652If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1653If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1654match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1655false if pixel conversion is not possible.
Cary Clark682c58d2018-05-16 07:07:07 -04001656
Cary Clarkac47b882018-01-11 10:35:44 -05001657srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark682c58d2018-05-16 07:07:07 -04001658false if width() or height() is zero or negative.
Cary Clark2be81cf2018-09-13 12:04:30 -04001659Returns false if #Formula # abs(srcX) >= Image width() ##, or if #Formula # abs(srcY) >= Image height() ##.
Cary Clarkac47b882018-01-11 10:35:44 -05001660
1661If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1662If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1663
1664#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1665#Param srcX column index whose absolute value is less than width() ##
1666#Param srcY row index whose absolute value is less than height() ##
1667#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1668
1669#Return true if pixels are copied to dst ##
1670
1671#Example
1672#Image 3
1673 std::vector<int32_t> srcPixels;
1674 int rowBytes = image->width() * 4;
1675 int quarterWidth = image->width() / 4;
1676 int quarterHeight = image->height() / 4;
1677 srcPixels.resize(image->height() * rowBytes);
1678 for (int y = 0; y < 4; ++y) {
1679 for (int x = 0; x < 4; ++x) {
1680 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1681 &srcPixels.front() + x * image->height() * quarterWidth +
1682 y * quarterWidth, rowBytes);
1683 image->readPixels(pixmap, x * quarterWidth, y * quarterHeight);
1684 }
1685 }
1686 canvas->scale(.5f, .5f);
1687 SkBitmap bitmap;
1688 bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()),
1689 &srcPixels.front(), rowBytes);
1690 canvas->drawBitmap(bitmap, 0, 0);
1691##
1692
1693#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001694
1695#Method ##
1696
1697# ------------------------------------------------------------------------------
1698
1699#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality,
1700 CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001701#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001702#Line # scales and converts one Image to another ##
Cary Clarka560c472017-11-27 10:44:06 -05001703
Cary Clarkac47b882018-01-11 10:35:44 -05001704Copies Image to dst, scaling pixels to fit dst.width() and dst.height(), and
1705converting pixels to match dst.colorType and dst.alphaType. Returns true if
1706pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is
1707less than dst SkImageInfo::minRowBytes.
Cary Clarka560c472017-11-27 10:44:06 -05001708
Cary Clarkac47b882018-01-11 10:35:44 -05001709Pixels are copied only if pixel conversion is possible. If Image Color_Type is
1710kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match.
1711If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match.
1712If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must
1713match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns
1714false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001715
Cary Clarkac47b882018-01-11 10:35:44 -05001716Scales the image, with filterQuality, to match dst.width() and dst.height().
1717filterQuality kNone_SkFilterQuality is fastest, typically implemented with
1718Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with
1719Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with
1720Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced.
1721kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic.
1722
1723If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1724If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1725
1726#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1727#Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
1728 kMedium_SkFilterQuality, kHigh_SkFilterQuality
1729##
1730#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1731
1732#Return true if pixels are scaled to fit dst ##
Cary Clarka560c472017-11-27 10:44:06 -05001733
1734#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001735#Image 3
1736#Height 128
1737 std::vector<int32_t> srcPixels;
1738 int quarterWidth = image->width() / 16;
1739 int rowBytes = quarterWidth * 4;
1740 int quarterHeight = image->height() / 16;
1741 srcPixels.resize(quarterHeight * rowBytes);
1742 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1743 &srcPixels.front(), rowBytes);
1744 canvas->scale(4, 4);
1745 SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality,
1746 kMedium_SkFilterQuality, kHigh_SkFilterQuality };
1747 for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) {
1748 image->scalePixels(pixmap, qualities[index]);
1749 sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
1750 canvas->drawImage(filtered, 16 * index, 0);
1751 }
Cary Clarka560c472017-11-27 10:44:06 -05001752##
1753
Cary Clarkac47b882018-01-11 10:35:44 -05001754#SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001755
1756#Method ##
1757
1758# ------------------------------------------------------------------------------
1759
1760#Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const
Cary Clark78de7512018-02-07 07:27:09 -05001761#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001762#Line # returns encoded Image as SkData ##
Cary Clark682c58d2018-05-16 07:07:07 -04001763Encodes Image pixels, returning result as SkData.
Cary Clark2f466242017-12-11 16:03:17 -05001764
Cary Clarkac47b882018-01-11 10:35:44 -05001765Returns nullptr if encoding fails, or if encodedImageFormat is not supported.
Cary Clarka560c472017-11-27 10:44:06 -05001766
Cary Clarkac47b882018-01-11 10:35:44 -05001767Image encoding in a format requires both building with one or more of:
1768SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support
1769for the encoded format.
1770
1771If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can
1772additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP,
1773SkEncodedImageFormat::kGIF.
1774
1775quality is a platform and format specific metric trading off size and encoding
1776error. When used, quality equaling 100 encodes with the least error. quality may
1777be ignored by the encoder.
1778
1779#Param encodedImageFormat one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG,
1780 SkEncodedImageFormat::kWEBP
1781 ##
1782#Param quality encoder specific metric with 100 equaling best ##
Cary Clarka560c472017-11-27 10:44:06 -05001783
Cary Clark2f466242017-12-11 16:03:17 -05001784#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001785
1786#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001787#Image 3
1788 canvas->scale(4, 4);
1789 SkIRect subset = {0, 0, 16, 64};
1790 int x = 0;
1791 for (int quality : { 0, 10, 50, 100 } ) {
1792 sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
1793 sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
1794 canvas->drawImage(filtered, x, 0);
1795 x += 16;
1796 }
Cary Clarka560c472017-11-27 10:44:06 -05001797##
1798
Cary Clarkac47b882018-01-11 10:35:44 -05001799#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001800
1801#Method ##
1802
1803# ------------------------------------------------------------------------------
1804
Cary Clark61ca7c52018-01-02 11:34:14 -05001805#Method sk_sp<SkData> encodeToData() const
Cary Clarka560c472017-11-27 10:44:06 -05001806
Cary Clarkac47b882018-01-11 10:35:44 -05001807Encodes Image pixels, returning result as SkData. Returns existing encoded data
1808if present; otherwise, Image is encoded with SkEncodedImageFormat::kPNG. Skia
1809must be built with SK_HAS_PNG_LIBRARY to encode Image.
Cary Clarka560c472017-11-27 10:44:06 -05001810
Cary Clark682c58d2018-05-16 07:07:07 -04001811Returns nullptr if existing encoded data is missing or invalid, and
Cary Clarka560c472017-11-27 10:44:06 -05001812encoding fails.
1813
Cary Clarkac47b882018-01-11 10:35:44 -05001814#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001815
1816#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001817#Image 3
1818 canvas->scale(4, 4);
1819 SkIRect subset = {136, 32, 200, 96};
1820 sk_sp<SkData> data(image->encodeToData());
1821 sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
1822 canvas->drawImage(eye, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001823##
1824
Cary Clarkac47b882018-01-11 10:35:44 -05001825#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001826
1827#Method ##
1828
1829# ------------------------------------------------------------------------------
1830
1831#Method sk_sp<SkData> refEncodedData() const
Cary Clark78de7512018-02-07 07:27:09 -05001832#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001833#Line # returns Image encoded in SkData if present ##
Cary Clarkac47b882018-01-11 10:35:44 -05001834Returns encoded Image pixels as SkData, if Image was created from supported
1835encoded stream format. Platform support for formats vary and may require building
1836with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY.
Cary Clarka560c472017-11-27 10:44:06 -05001837
Cary Clarkac47b882018-01-11 10:35:44 -05001838Returns nullptr if Image contents are not encoded.
Cary Clarka560c472017-11-27 10:44:06 -05001839
Cary Clarkac47b882018-01-11 10:35:44 -05001840#Return encoded Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001841
1842#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001843#Image 3
1844#Platform gpu
1845 struct {
1846 const char* name;
1847 sk_sp<SkImage> image;
1848 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1849 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001850 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1851 kOpaque_SkAlphaType, nullptr) } };
Cary Clarkac47b882018-01-11 10:35:44 -05001852 SkString string;
1853 SkPaint paint;
1854 for (const auto& test : tests ) {
1855 if (!test.image) {
1856 string.printf("no %s", test.name);
1857 } else {
1858 string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name);
1859 }
1860 canvas->drawString(string, 10, 20, paint);
1861 canvas->translate(0, 20);
1862 }
Cary Clarka560c472017-11-27 10:44:06 -05001863##
1864
Cary Clarkac47b882018-01-11 10:35:44 -05001865#SeeAlso encodeToData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001866
1867#Method ##
1868
1869# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001870#Subtopic Utility
Cary Clark4855f782018-02-06 09:41:53 -05001871#Line # rarely called management functions ##
1872##
Cary Clarka560c472017-11-27 10:44:06 -05001873
Cary Clarka560c472017-11-27 10:44:06 -05001874#Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const
Cary Clark61313f32018-10-08 14:57:48 -04001875#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05001876#Line # creates Image containing part of original ##
Cary Clarkac47b882018-01-11 10:35:44 -05001877Returns subset of Image. subset must be fully contained by Image dimensions().
1878The implementation may share pixels, or may copy them.
Cary Clarka560c472017-11-27 10:44:06 -05001879
Cary Clarkac47b882018-01-11 10:35:44 -05001880Returns nullptr if subset is empty, or subset is not contained by bounds, or
1881pixels in Image could not be read or copied.
Cary Clarka560c472017-11-27 10:44:06 -05001882
Cary Clarkac47b882018-01-11 10:35:44 -05001883#Param subset bounds of returned Image ##
1884
1885#Return partial or full Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001886
1887#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001888#Image 3
1889 canvas->scale(.5f, .5f);
Cary Clarkc30382f2018-07-24 08:09:27 -04001890 const int width = 64;
1891 const int height = 64;
Cary Clarkac47b882018-01-11 10:35:44 -05001892 for (int y = 0; y < 512; y += height ) {
1893 for (int x = 0; x < 512; x += width ) {
1894 sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height}));
1895 canvas->drawImage(subset, x * 3 / 2, y * 3 / 2);
1896 }
1897 }
Cary Clarka560c472017-11-27 10:44:06 -05001898##
1899
Cary Clarkac47b882018-01-11 10:35:44 -05001900#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001901
1902#Method ##
1903
1904# ------------------------------------------------------------------------------
1905
Greg Daniel5f4b09d2018-06-12 16:39:59 -04001906#Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace,
1907 GrMipMapped mipMapped = GrMipMapped::kNo) const
Cary Clark61313f32018-10-08 14:57:48 -04001908#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05001909#Line # creates Image matching Color_Space if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05001910Returns Image backed by GPU_Texture associated with context. Returned Image is
Cary Clark5538c132018-06-14 12:28:14 -04001911compatible with Surface created with dstColorSpace. The returned Image respects
1912mipMapped setting; if mipMapped equals GrMipMapped::kYes, the backing texture
1913allocates Mip_Map levels. Returns original Image if context
1914and dstColorSpace match and mipMapped is compatible with backing GPU_Texture.
Cary Clarkac47b882018-01-11 10:35:44 -05001915
1916Returns nullptr if context is nullptr, or if Image was created with another
1917GrContext.
Cary Clarka560c472017-11-27 10:44:06 -05001918
Cary Clark61ca7c52018-01-02 11:34:14 -05001919#Param context GPU_Context ##
Cary Clarkac47b882018-01-11 10:35:44 -05001920#Param dstColorSpace range of colors of matching Surface on GPU ##
Cary Clark5538c132018-06-14 12:28:14 -04001921#Param mipMapped whether created Image texture must allocate Mip_Map levels ##
Cary Clarka560c472017-11-27 10:44:06 -05001922
Cary Clarkac47b882018-01-11 10:35:44 -05001923#Return created Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001924
1925#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001926#Platform gpu
1927#Image 5
1928 auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void {
1929 if (nullptr == image || nullptr == context) {
1930 return;
1931 }
1932 SkPaint paint;
1933 paint.setAntiAlias(true);
1934 paint.setTextAlign(SkPaint::kCenter_Align);
1935 sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr));
1936 canvas->drawImage(texture, 0, 0);
1937 canvas->drawString(label, texture->width() / 2, texture->height() / 4, paint);
1938 };
1939 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1940 GrContext* context = canvas->getGrContext();
1941 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001942 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1943 kOpaque_SkAlphaType, nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001944 drawImage(image, context, "image");
1945 canvas->translate(image->width(), 0);
1946 drawImage(bitmapImage, context, "source");
1947 canvas->translate(-image->width(), image->height());
1948 drawImage(textureImage, context, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001949##
1950
Cary Clarkac47b882018-01-11 10:35:44 -05001951#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05001952
1953#Method ##
1954
1955# ------------------------------------------------------------------------------
1956
1957#Method sk_sp<SkImage> makeNonTextureImage() const
Cary Clark61313f32018-10-08 14:57:48 -04001958#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05001959#Line # creates Image without dependency on GPU_Texture ##
Cary Clarkac47b882018-01-11 10:35:44 -05001960Returns Raster_Image or Lazy_Image. Copies Image backed by GPU_Texture into
Cary Clark4855f782018-02-06 09:41:53 -05001961CPU memory if needed. Returns original Image if decoded in Raster_Bitmap,
Cary Clarkac47b882018-01-11 10:35:44 -05001962or if encoded in a stream.
Cary Clark61ca7c52018-01-02 11:34:14 -05001963
Cary Clarkac47b882018-01-11 10:35:44 -05001964Returns nullptr if backed by GPU_Texture and copy fails.
1965
1966#Return Raster_Image, Lazy_Image, or nullptr ##
Cary Clark61ca7c52018-01-02 11:34:14 -05001967
1968#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001969#Image 5
1970#Platform gpu
1971 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1972 if (nullptr == image) {
1973 return;
1974 }
1975 SkPaint paint;
1976 paint.setAntiAlias(true);
1977 paint.setTextAlign(SkPaint::kCenter_Align);
1978 sk_sp<SkImage> nonTexture(image->makeNonTextureImage());
1979 canvas->drawImage(nonTexture, 0, 0);
1980 canvas->drawString(label, nonTexture->width() / 2, nonTexture->height() / 4, paint);
1981 };
1982 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1983 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001984 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1985 kOpaque_SkAlphaType, nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001986 drawImage(image, "image");
1987 canvas->translate(image->width(), 0);
1988 drawImage(bitmapImage, "source");
1989 canvas->translate(-image->width(), image->height());
1990 drawImage(textureImage, "backEndTexture");
Cary Clark61ca7c52018-01-02 11:34:14 -05001991##
1992
Cary Clark56356312018-02-08 14:45:18 -05001993#SeeAlso makeTextureImage makeRasterImage MakeBackendTextureFromSkImage
Cary Clark61ca7c52018-01-02 11:34:14 -05001994
1995#Method ##
1996
1997# ------------------------------------------------------------------------------
1998
1999#Method sk_sp<SkImage> makeRasterImage() const
Cary Clark61313f32018-10-08 14:57:48 -04002000#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05002001#Line # creates Image compatible with Raster_Surface if possible ##
Cary Clarkac47b882018-01-11 10:35:44 -05002002Returns Raster_Image. Copies Image backed by GPU_Texture into CPU memory,
Cary Clark4855f782018-02-06 09:41:53 -05002003or decodes Image from Lazy_Image. Returns original Image if decoded in
Cary Clarkac47b882018-01-11 10:35:44 -05002004Raster_Bitmap.
Cary Clarka560c472017-11-27 10:44:06 -05002005
Cary Clarkac47b882018-01-11 10:35:44 -05002006Returns nullptr if copy, decode, or pixel read fails.
Cary Clarka560c472017-11-27 10:44:06 -05002007
Cary Clarkac47b882018-01-11 10:35:44 -05002008#Return Raster_Image, or nullptr ##
2009
Cary Clark4855f782018-02-06 09:41:53 -05002010#Bug 7479
Cary Clarka560c472017-11-27 10:44:06 -05002011#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002012#Image 5
2013#Platform gpu
2014 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2015 if (nullptr == image) {
2016 return;
2017 }
2018 SkPaint paint;
2019 paint.setAntiAlias(true);
2020 paint.setTextAlign(SkPaint::kCenter_Align);
2021 sk_sp<SkImage> raster(image->makeRasterImage());
2022 canvas->drawImage(raster, 0, 0);
2023 canvas->drawString(label, raster->width() / 2, raster->height() / 4, paint);
2024 };
2025 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2026 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04002027 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
2028 kOpaque_SkAlphaType, nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05002029 drawImage(image, "image");
2030 canvas->translate(image->width(), 0);
2031 drawImage(bitmapImage, "source");
2032 canvas->translate(-image->width(), image->height());
2033 drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05002034##
2035
Cary Clarkac47b882018-01-11 10:35:44 -05002036#SeeAlso isTextureBacked isLazyGenerated MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -05002037
2038#Method ##
2039
2040# ------------------------------------------------------------------------------
2041
2042#Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
2043 const SkIRect& clipBounds, SkIRect* outSubset,
2044 SkIPoint* offset) const
Cary Clark61313f32018-10-08 14:57:48 -04002045#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05002046#Line # creates filtered, clipped Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002047
Cary Clarkac47b882018-01-11 10:35:44 -05002048Creates filtered Image. filter processes original Image, potentially changing
2049color, position, and size. subset is the bounds of original Image processed
2050by filter. clipBounds is the expected bounds of the filtered Image. outSubset
2051is required storage for the actual bounds of the filtered Image. offset is
2052required storage for translation of returned Image.
Cary Clarka560c472017-11-27 10:44:06 -05002053
Cary Clarkac47b882018-01-11 10:35:44 -05002054Returns nullptr if Image could not be created. If nullptr is returned, outSubset
2055and offset are undefined.
2056
Cary Clark56356312018-02-08 14:45:18 -05002057Useful for animation of SkImageFilter that varies size from frame to frame.
2058Returned Image is created larger than required by filter so that GPU_Texture
2059can be reused with different sized effects. outSubset describes the valid bounds
2060of GPU_Texture returned. offset translates the returned Image to keep subsequent
2061animation frames aligned with respect to each other.
Cary Clarkac47b882018-01-11 10:35:44 -05002062
2063#Param filter how Image is sampled when transformed ##
Cary Clark56356312018-02-08 14:45:18 -05002064#Param subset bounds of Image processed by filter ##
2065#Param clipBounds expected bounds of filtered Image ##
2066#Param outSubset storage for returned Image bounds ##
2067#Param offset storage for returned Image translation ##
Cary Clarka560c472017-11-27 10:44:06 -05002068
Cary Clarkac47b882018-01-11 10:35:44 -05002069#Return filtered Image, or nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05002070
Cary Clark5717e822018-09-21 11:36:41 -04002071# Duration 5 breaks fiddlecli
Cary Clarka560c472017-11-27 10:44:06 -05002072#Example
Cary Clarkac47b882018-01-11 10:35:44 -05002073#Description
2074In each frame of the animation, filtered Image is drawn in a different location.
2075By translating canvas by returned offset, Image appears stationary.
2076##
2077#Image 5
2078#Platform gpu
Cary Clark5717e822018-09-21 11:36:41 -04002079#Duration 1
Cary Clarkac47b882018-01-11 10:35:44 -05002080 sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make(
2081 -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE,
2082 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
2083 nullptr);
2084 sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr);
2085 SkIRect subset = image->bounds();
2086 SkIRect clipBounds = image->bounds();
2087 clipBounds.outset(60, 60);
2088 SkIRect outSubset;
2089 SkIPoint offset;
2090 sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds,
2091 &outSubset, &offset));
2092 SkPaint paint;
2093 paint.setAntiAlias(true);
2094 paint.setStyle(SkPaint::kStroke_Style);
2095 canvas->drawLine(0, 0, offset.fX, offset.fY, paint);
2096 canvas->translate(offset.fX, offset.fY);
2097 canvas->drawImage(filtered, 0, 0);
Cary Clark681287e2018-03-16 11:34:15 -04002098 canvas->drawRect(SkRect::Make(outSubset), paint);
Cary Clarka560c472017-11-27 10:44:06 -05002099##
2100
Cary Clark56356312018-02-08 14:45:18 -05002101#SeeAlso makeShader SkPaint::setImageFilter
Cary Clarka560c472017-11-27 10:44:06 -05002102
2103#Method ##
2104
2105# ------------------------------------------------------------------------------
2106
Cary Clarka560c472017-11-27 10:44:06 -05002107#Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc
Cary Clark682c58d2018-05-16 07:07:07 -04002108#Line # parameter type for MakeBackendTextureFromSkImage ##
Cary Clarkffb3d682018-05-17 12:17:28 -04002109
2110#Code
Cary Clarka90ea222018-10-16 10:30:28 -04002111#Populate
Cary Clarkffb3d682018-05-17 12:17:28 -04002112##
2113
Cary Clark0d225392018-06-07 09:59:07 -04002114Defines a callback function, taking one parameter of type GrBackendTexture with
2115no return value. Function is called when back-end texture is to be released.
Cary Clarka560c472017-11-27 10:44:06 -05002116##
2117
2118# ------------------------------------------------------------------------------
2119
2120#Method static bool MakeBackendTextureFromSkImage(GrContext* context,
2121 sk_sp<SkImage> image,
2122 GrBackendTexture* backendTexture,
2123 BackendTextureReleaseProc* backendTextureReleaseProc)
Cary Clark61313f32018-10-08 14:57:48 -04002124#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05002125#Line # creates GPU_Texture from Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002126
Cary Clark56356312018-02-08 14:45:18 -05002127Creates a GrBackendTexture from the provided SkImage. Returns true and
2128stores result in backendTexture and backendTextureReleaseProc if
2129texture is created; otherwise, returns false and leaves
2130backendTexture and backendTextureReleaseProc unmodified.
Cary Clarka560c472017-11-27 10:44:06 -05002131
Cary Clark56356312018-02-08 14:45:18 -05002132Call backendTextureReleaseProc after deleting backendTexture.
2133backendTextureReleaseProc cleans up auxiliary data related to returned
2134backendTexture. The caller must delete returned backendTexture after use.
Cary Clarka560c472017-11-27 10:44:06 -05002135
Cary Clark56356312018-02-08 14:45:18 -05002136If Image is both texture backed and singly referenced, image is returned in
2137backendTexture without conversion or making a copy. Image is singly referenced
2138if its was transferred solely using std::move().
2139
2140If Image is not texture backed, returns texture with Image contents.
Cary Clarka560c472017-11-27 10:44:06 -05002141
Cary Clark61ca7c52018-01-02 11:34:14 -05002142#Param context GPU_Context ##
Cary Clark56356312018-02-08 14:45:18 -05002143#Param image Image used for texture ##
Cary Clark682c58d2018-05-16 07:07:07 -04002144#Param backendTexture storage for back-end texture ##
Cary Clark56356312018-02-08 14:45:18 -05002145#Param backendTextureReleaseProc storage for clean up function ##
Cary Clarka560c472017-11-27 10:44:06 -05002146
Cary Clark682c58d2018-05-16 07:07:07 -04002147#Return true if back-end texture was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002148
2149#Example
Cary Clark56356312018-02-08 14:45:18 -05002150#Platform gpu
2151#Height 64
2152#Function
Brian Salomon67f85842018-02-09 08:50:22 -05002153static sk_sp<SkImage> create_gpu_image(GrContext* grContext) {
2154 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
2155 auto surface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info));
2156 SkCanvas* canvas = surface->getCanvas();
2157 canvas->clear(SK_ColorWHITE);
2158 SkPaint paint;
2159 paint.setColor(SK_ColorBLACK);
2160 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
2161 return surface->makeImageSnapshot();
2162}
2163##
2164
Cary Clark682c58d2018-05-16 07:07:07 -04002165void draw(SkCanvas* canvas) {
Brian Salomon67f85842018-02-09 08:50:22 -05002166 GrContext* grContext = canvas->getGrContext();
2167 if (!grContext) {
2168 return;
2169 }
2170 sk_sp<SkImage> backEndImage = create_gpu_image(grContext);
2171 canvas->drawImage(backEndImage, 0, 0);
2172 GrBackendTexture texture;
2173 SkImage::BackendTextureReleaseProc proc;
2174 if (!SkImage::MakeBackendTextureFromSkImage(grContext, std::move(backEndImage),
2175 &texture, &proc)) {
2176 return;
2177 }
2178 sk_sp<SkImage> i2 = SkImage::MakeFromTexture(grContext, texture, kTopLeft_GrSurfaceOrigin,
2179 kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
2180 canvas->drawImage(i2, 30, 30);
Cary Clark56356312018-02-08 14:45:18 -05002181}
Cary Clarka560c472017-11-27 10:44:06 -05002182##
2183
Cary Clark56356312018-02-08 14:45:18 -05002184#SeeAlso MakeFromTexture makeTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002185
2186#Method ##
2187
2188# ------------------------------------------------------------------------------
2189
2190#Enum LegacyBitmapMode
Cary Clark56356312018-02-08 14:45:18 -05002191#Deprecated soon
Cary Clarka560c472017-11-27 10:44:06 -05002192#Code
Cary Clarka90ea222018-10-16 10:30:28 -04002193#Populate
Cary Clarka560c472017-11-27 10:44:06 -05002194##
2195
Cary Clarka560c472017-11-27 10:44:06 -05002196#Const kRO_LegacyBitmapMode 0
Cary Clark682c58d2018-05-16 07:07:07 -04002197#Line # returned bitmap is read-only and immutable ##
Cary Clarka560c472017-11-27 10:44:06 -05002198##
Cary Clarka560c472017-11-27 10:44:06 -05002199
2200#Enum ##
2201
2202# ------------------------------------------------------------------------------
2203
Cary Clark56356312018-02-08 14:45:18 -05002204#Method bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode = kRO_LegacyBitmapMode) const
Cary Clark61313f32018-10-08 14:57:48 -04002205#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05002206#Line # returns as Raster_Bitmap ##
Cary Clarkac47b882018-01-11 10:35:44 -05002207Creates raster Bitmap with same pixels as Image. If legacyBitmapMode is
2208kRO_LegacyBitmapMode, returned bitmap is read-only and immutable.
2209Returns true if Bitmap is stored in bitmap. Returns false and resets bitmap if
2210Bitmap write did not succeed.
Cary Clarka560c472017-11-27 10:44:06 -05002211
Cary Clark3cd22cc2017-12-01 11:49:58 -05002212#Param bitmap storage for legacy Bitmap ##
Cary Clark56356312018-02-08 14:45:18 -05002213#Param legacyBitmapMode to be deprecated ##
Cary Clarka560c472017-11-27 10:44:06 -05002214
Cary Clark3cd22cc2017-12-01 11:49:58 -05002215#Return true if Bitmap was created ##
Cary Clarka560c472017-11-27 10:44:06 -05002216
Cary Clarkae957c42018-06-07 17:07:17 -04002217#Example
Cary Clark56356312018-02-08 14:45:18 -05002218#Image 4
2219#Platform gpu
Brian Salomon67f85842018-02-09 08:50:22 -05002220 SkBitmap bitImage;
2221 if (image->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2222 canvas->drawBitmap(bitImage, 0, 0);
2223 }
2224 GrContext* grContext = canvas->getGrContext();
2225 if (!grContext) {
2226 return;
2227 }
2228 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(grContext, backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04002229 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
2230 kOpaque_SkAlphaType, nullptr));
Brian Salomon67f85842018-02-09 08:50:22 -05002231 canvas->drawImage(textureImage, 45, 45);
2232 if (textureImage->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
2233 canvas->drawBitmap(bitImage, 90, 90);
2234 }
Cary Clarka560c472017-11-27 10:44:06 -05002235##
2236
Cary Clark56356312018-02-08 14:45:18 -05002237#SeeAlso MakeRasterData makeRasterImage makeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002238
2239#Method ##
2240
2241# ------------------------------------------------------------------------------
2242
2243#Method bool isLazyGenerated() const
Cary Clark4855f782018-02-06 09:41:53 -05002244#In Property
2245#Line # returns if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002246Returns true if Image is backed by an image-generator or other service that creates
2247and caches its pixels or texture on-demand.
2248
Cary Clark2f466242017-12-11 16:03:17 -05002249#Return true if Image is created as needed ##
Cary Clarka560c472017-11-27 10:44:06 -05002250
2251#Example
Cary Clark2f466242017-12-11 16:03:17 -05002252#Height 80
2253#Function
2254class TestImageGenerator : public SkImageGenerator {
2255public:
2256 TestImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(10, 10)) {}
2257 ~TestImageGenerator() override {}
2258protected:
2259 bool onGetPixels(const SkImageInfo& info, void* pixelPtr, size_t rowBytes,
2260 const Options& options) override {
2261 SkPMColor* pixels = static_cast<SkPMColor*>(pixelPtr);
2262 for (int y = 0; y < info.height(); ++y) {
2263 for (int x = 0; x < info.width(); ++x) {
2264 pixels[y * info.width() + x] = 0xff223344 + y * 0x000C0811;
2265 }
2266 }
2267 return true;
2268 }
2269};
2270##
2271void draw(SkCanvas* canvas) {
2272 auto gen = std::unique_ptr<TestImageGenerator>(new TestImageGenerator());
2273 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
2274 SkString lazy(image->isLazyGenerated() ? "is lazy" : "not lazy");
2275 canvas->scale(8, 8);
2276 canvas->drawImage(image, 0, 0, nullptr);
2277 SkPaint paint;
2278 paint.setTextSize(4);
2279 canvas->drawString(lazy, 2, 5, paint);
2280}
Cary Clarka560c472017-11-27 10:44:06 -05002281##
2282
Cary Clarkf5404bb2018-01-05 12:10:09 -05002283#Example
2284#Image 5
2285#Platform gpu
2286void draw(SkCanvas* canvas) {
2287 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
2288 if (nullptr == image) {
2289 return;
2290 }
2291 SkPaint paint;
2292 paint.setAntiAlias(true);
2293 paint.setTextAlign(SkPaint::kCenter_Align);
2294 canvas->drawImage(image, 0, 0);
2295 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
2296 canvas->drawString(
2297 image->isLazyGenerated() ? "is lazily generated" : "not lazily generated",
2298 image->width() / 2, image->height() * 3 / 4, paint);
2299 };
2300 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
2301 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04002302 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
2303 kOpaque_SkAlphaType, nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05002304 drawImage(image, "image");
2305 canvas->translate(image->width(), 0);
2306 drawImage(bitmapImage, "source");
2307 canvas->translate(-image->width(), image->height());
2308 drawImage(textureImage, "backEndTexture");
2309}
2310##
2311
Cary Clarkac47b882018-01-11 10:35:44 -05002312#SeeAlso isTextureBacked MakeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05002313
2314#Method ##
2315
2316# ------------------------------------------------------------------------------
2317
Cary Clarke80cd442018-07-17 13:19:56 -04002318#Method sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target) const
Cary Clark61313f32018-10-08 14:57:48 -04002319#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05002320#Line # creates Image matching Color_Space if possible ##
Cary Clarka560c472017-11-27 10:44:06 -05002321
Cary Clarkac47b882018-01-11 10:35:44 -05002322Creates Image in target Color_Space.
2323Returns nullptr if Image could not be created.
Cary Clarka560c472017-11-27 10:44:06 -05002324
Cary Clarkac47b882018-01-11 10:35:44 -05002325Returns original Image if it is in target Color_Space.
2326Otherwise, converts pixels from Image Color_Space to target Color_Space.
2327If Image colorSpace returns nullptr, Image Color_Space is assumed to be sRGB.
2328
Cary Clarkac47b882018-01-11 10:35:44 -05002329#Param target Color_Space describing color range of returned Image ##
Cary Clarka560c472017-11-27 10:44:06 -05002330
Cary Clarkac47b882018-01-11 10:35:44 -05002331#Return created Image in target Color_Space ##
2332
2333#Example
2334#Image 5
2335#Set sRGB
2336 sk_sp<SkColorSpace> normalColorSpace = SkColorSpace::MakeRGB(
2337 SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kSRGB_Gamut);
2338 sk_sp<SkColorSpace> wackyColorSpace = normalColorSpace->makeColorSpin();
2339 for (auto colorSpace : { normalColorSpace, wackyColorSpace } ) {
Cary Clarkc3c1c312018-07-18 09:25:15 -04002340 sk_sp<SkImage> colorSpaced = image->makeColorSpace(colorSpace);
2341 canvas->drawImage(colorSpaced, 0, 0);
2342 canvas->translate(128, 0);
Cary Clarkac47b882018-01-11 10:35:44 -05002343 }
2344##
2345
2346#SeeAlso MakeFromPixture MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05002347
2348#Method ##
2349
2350#Class SkImage ##
2351
2352#Topic Image ##