blob: 9a6874065e13654aff4937a18d22bafed09a2635 [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 Clark09d80c02018-10-31 12:14:03 -040053#Populate
Cary Clarka560c472017-11-27 10:44:06 -050054
55#Example
Cary Clark2f466242017-12-11 16:03:17 -050056#Height 50
57#Description
58Draw a five by five bitmap, and draw a copy in an Image. Editing the pixmap
59alters the bitmap draw, but does not alter the Image draw since the Image
60contains a copy of the pixels.
61##
62 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
63 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
64 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
65 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
66 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
67 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
68 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
69 SkBitmap bitmap;
70 bitmap.installPixels(pixmap);
71 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
72 *pixmap.writable_addr8(2, 2) = 0x00;
73 canvas->scale(10, 10);
74 canvas->drawBitmap(bitmap, 0, 0);
75 canvas->drawImage(image, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -050076##
77
Cary Clark3cd22cc2017-12-01 11:49:58 -050078#SeeAlso MakeRasterData MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -050079
80#Method ##
81
82# ------------------------------------------------------------------------------
83
Cary Clarkcc45cc72018-05-15 16:06:12 -040084#Method static sk_sp<SkImage> MakeRasterData(const SkImageInfo& info, sk_sp<SkData> pixels, size_t rowBytes)
Cary Clark61313f32018-10-08 14:57:48 -040085#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -050086#Line # creates Image from Image_Info and shared pixels ##
Cary Clark09d80c02018-10-31 12:14:03 -040087#Populate
Cary Clarka560c472017-11-27 10:44:06 -050088
89#Example
Cary Clark0c5f5462017-12-15 11:21:51 -050090#Image 3
91 size_t rowBytes = image->width() * SkColorTypeBytesPerPixel(kRGBA_8888_SkColorType);
92 sk_sp<SkData> data = SkData::MakeUninitialized(rowBytes * image->height());
Cary Clark682c58d2018-05-16 07:07:07 -040093 SkImageInfo dstInfo = SkImageInfo::MakeN32(image->width(), image->height(),
Cary Clark0c5f5462017-12-15 11:21:51 -050094 kPremul_SkAlphaType);
95 image->readPixels(dstInfo, data->writable_data(), rowBytes, 0, 0, SkImage::kAllow_CachingHint);
96 sk_sp<SkImage> raw = SkImage::MakeRasterData(dstInfo.makeColorType(kRGBA_8888_SkColorType),
97 data, rowBytes);
98 canvas->drawImage(image, 0, 0);
99 canvas->drawImage(raw.get(), 128, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500100##
101
Cary Clark3cd22cc2017-12-01 11:49:58 -0500102#SeeAlso MakeRasterCopy MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500103
104#Method ##
105
106# ------------------------------------------------------------------------------
107
Cary Clark3cd22cc2017-12-01 11:49:58 -0500108#Typedef void* ReleaseContext
Cary Clark682c58d2018-05-16 07:07:07 -0400109#Line # parameter type for MakeFromRaster ##
Cary Clarkffb3d682018-05-17 12:17:28 -0400110
111#Code
Cary Clarka90ea222018-10-16 10:30:28 -0400112#Populate
Cary Clarkffb3d682018-05-17 12:17:28 -0400113##
114
Cary Clark3cd22cc2017-12-01 11:49:58 -0500115Caller data passed to RasterReleaseProc; may be nullptr.
116
117#SeeAlso MakeFromRaster RasterReleaseProc
118
119##
120
Cary Clarka560c472017-11-27 10:44:06 -0500121#Typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext)
Cary Clark682c58d2018-05-16 07:07:07 -0400122#Line # parameter type for MakeFromRaster ##
Cary Clarkffb3d682018-05-17 12:17:28 -0400123
124#Code
Cary Clarka90ea222018-10-16 10:30:28 -0400125#Populate
Cary Clarkffb3d682018-05-17 12:17:28 -0400126##
127
Cary Clark3cd22cc2017-12-01 11:49:58 -0500128Function called when Image no longer shares pixels. ReleaseContext is
Cary Clark682c58d2018-05-16 07:07:07 -0400129provided by caller when Image is created, and may be nullptr.
Cary Clark3cd22cc2017-12-01 11:49:58 -0500130
131#SeeAlso ReleaseContext MakeFromRaster
132
Cary Clarka560c472017-11-27 10:44:06 -0500133##
134
135#Method static sk_sp<SkImage> MakeFromRaster(const SkPixmap& pixmap,
136 RasterReleaseProc rasterReleaseProc,
137 ReleaseContext releaseContext)
Cary Clark61313f32018-10-08 14:57:48 -0400138#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500139#Line # creates Image from Pixmap, with release ##
Cary Clark09d80c02018-10-31 12:14:03 -0400140#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500141
142#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500143#Function
144static void releaseProc(const void* pixels, SkImage::ReleaseContext context) {
145 int* countPtr = static_cast<int*>(context);
146 *countPtr += 1;
147}
148##
149
150void draw(SkCanvas* canvas) {
151 SkColor color = 0;
152 SkPixmap pixmap(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), &color, 4);
153 int releaseCount = 0;
154 sk_sp<SkImage> image(SkImage::MakeFromRaster(pixmap, releaseProc, &releaseCount));
155 SkDebugf("before reset: %d\n", releaseCount);
156 image.reset();
157 SkDebugf("after reset: %d\n", releaseCount);
158}
159#StdOut
160before reset: 0
161after reset: 1
162##
Cary Clarka560c472017-11-27 10:44:06 -0500163##
164
Cary Clark3cd22cc2017-12-01 11:49:58 -0500165#SeeAlso MakeRasterCopy MakeRasterData MakeFromGenerator RasterReleaseProc ReleaseContext
Cary Clarka560c472017-11-27 10:44:06 -0500166
167#Method ##
168
169# ------------------------------------------------------------------------------
170
171#Method static sk_sp<SkImage> MakeFromBitmap(const SkBitmap& bitmap)
Cary Clark61313f32018-10-08 14:57:48 -0400172#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500173#Line # creates Image from Bitmap, sharing or copying pixels ##
Cary Clark09d80c02018-10-31 12:14:03 -0400174#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500175
176#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500177#Description
178The first Bitmap is shared; writing to the pixel memory changes the first
179Image.
180The second Bitmap is marked immutable, and is copied; writing to the pixel
181memory does not alter the second Image.
182##
183#Height 50
184 uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 },
185 { 0xAC, 0xA8, 0x89, 0xA7, 0x87 },
186 { 0x9B, 0xB5, 0xE5, 0x95, 0x46 },
187 { 0x90, 0x81, 0xC5, 0x71, 0x33 },
188 { 0x75, 0x55, 0x44, 0x40, 0x30 }};
189 SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType);
190 SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5);
191 SkBitmap bitmap;
192 bitmap.installPixels(pixmap);
193 sk_sp<SkImage> image1 = SkImage::MakeFromBitmap(bitmap);
194 bitmap.setImmutable();
195 sk_sp<SkImage> image2 = SkImage::MakeFromBitmap(bitmap);
196 *pixmap.writable_addr8(2, 2) = 0x00;
197 canvas->scale(10, 10);
198 canvas->drawImage(image1, 0, 0);
199 canvas->drawImage(image2, 10, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500200##
201
Cary Clark3cd22cc2017-12-01 11:49:58 -0500202#SeeAlso MakeFromRaster MakeRasterCopy MakeFromGenerator MakeRasterData
Cary Clarka560c472017-11-27 10:44:06 -0500203
204#Method ##
205
206# ------------------------------------------------------------------------------
207
208#Method static sk_sp<SkImage> MakeFromGenerator(std::unique_ptr<SkImageGenerator> imageGenerator,
209 const SkIRect* subset = nullptr)
Cary Clark61313f32018-10-08 14:57:48 -0400210#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500211#Line # creates Image from a stream of data ##
Cary Clark09d80c02018-10-31 12:14:03 -0400212#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500213
214#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500215#Height 128
Cary Clark0c5f5462017-12-15 11:21:51 -0500216#Description
217The generator returning Picture cannot be shared; std::move transfers ownership to generated Image.
218##
219 SkPictureRecorder recorder;
220 recorder.beginRecording(100, 100)->drawColor(SK_ColorRED);
221 auto picture = recorder.finishRecordingAsPicture();
222 auto gen = SkImageGenerator::MakeFromPicture({100, 100}, picture, nullptr, nullptr,
223 SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB());
224 sk_sp<SkImage> image = SkImage::MakeFromGenerator(std::move(gen));
225 canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500226##
227
Cary Clark3cd22cc2017-12-01 11:49:58 -0500228#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500229
230#Method ##
231
232# ------------------------------------------------------------------------------
233
234#Method static sk_sp<SkImage> MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset = nullptr)
Cary Clark61313f32018-10-08 14:57:48 -0400235#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500236#Line # creates Image from encoded data ##
Cary Clark09d80c02018-10-31 12:14:03 -0400237#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500238
Cary Clark61ca7c52018-01-02 11:34:14 -0500239#Example
240#Image 3
241int x = 0;
242for (int quality : { 100, 50, 10, 1} ) {
243 sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, quality);
244 sk_sp<SkImage> image = SkImage::MakeFromEncoded(encodedData);
245 canvas->drawImage(image, x, 0);
246 x += 64;
247}
Cary Clarka560c472017-11-27 10:44:06 -0500248##
249
Cary Clark3cd22cc2017-12-01 11:49:58 -0500250#SeeAlso MakeFromGenerator
Cary Clarka560c472017-11-27 10:44:06 -0500251
252#Method ##
253
254# ------------------------------------------------------------------------------
255
256#Typedef void (*TextureReleaseProc)(ReleaseContext releaseContext)
Cary Clark682c58d2018-05-16 07:07:07 -0400257#Line # parameter type for MakeFromTexture ##
Cary Clarkffb3d682018-05-17 12:17:28 -0400258
259#Code
Cary Clarka90ea222018-10-16 10:30:28 -0400260#Populate
Cary Clarkffb3d682018-05-17 12:17:28 -0400261##
262
Cary Clark682c58d2018-05-16 07:07:07 -0400263User function called when supplied texture may be deleted.
264#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -0500265##
266
267#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
268 const GrBackendTexture& backendTexture,
269 GrSurfaceOrigin origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500270 SkColorType colorType,
271 SkAlphaType alphaType,
272 sk_sp<SkColorSpace> colorSpace)
Cary Clark61313f32018-10-08 14:57:48 -0400273#In Constructors
Cary Clarkf895a422018-02-27 09:54:21 -0500274#Line # creates Image from GPU_Texture ##
Cary Clark09d80c02018-10-31 12:14:03 -0400275#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500276
277#Example
Cary Clark0c5f5462017-12-15 11:21:51 -0500278#Image 3
279#Platform gpu
280#Height 128
281#Description
282A back-end texture has been created and uploaded to the GPU outside of this example.
283##
284GrContext* context = canvas->getGrContext();
285if (!context) {
286 return;
287}
288canvas->scale(.25f, .25f);
289int x = 0;
290for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500291 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -0400292 origin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr);
Cary Clark0c5f5462017-12-15 11:21:51 -0500293 canvas->drawImage(image, x, 0);
294x += 512;
295}
Cary Clarka560c472017-11-27 10:44:06 -0500296##
297
Cary Clark3cd22cc2017-12-01 11:49:58 -0500298#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500299
300#Method ##
301
302# ------------------------------------------------------------------------------
303
304#Method static sk_sp<SkImage> MakeFromTexture(GrContext* context,
305 const GrBackendTexture& backendTexture,
306 GrSurfaceOrigin origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500307 SkColorType colorType,
Cary Clarka560c472017-11-27 10:44:06 -0500308 SkAlphaType alphaType,
309 sk_sp<SkColorSpace> colorSpace,
310 TextureReleaseProc textureReleaseProc,
311 ReleaseContext releaseContext)
Cary Clark09d80c02018-10-31 12:14:03 -0400312#Populate
Cary Clark0c5f5462017-12-15 11:21:51 -0500313
Cary Clarka560c472017-11-27 10:44:06 -0500314#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500315#Platform gpu
316#Image 4
Cary Clark0c5f5462017-12-15 11:21:51 -0500317GrContext* context = canvas->getGrContext();
318if (!context) {
319 return;
320}
Cary Clarkac47b882018-01-11 10:35:44 -0500321auto debugster = [](SkImage::ReleaseContext releaseContext) -> void {
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400322 // broken
Cary Clark08417bc2018-10-03 10:44:13 -0400323 // *((int *) releaseContext) += 128;
Cary Clark0c5f5462017-12-15 11:21:51 -0500324};
325int x = 0;
326for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
Cary Clarkac47b882018-01-11 10:35:44 -0500327 sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture,
Cary Clark61ca7c52018-01-02 11:34:14 -0500328 origin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr, debugster, &x);
Cary Clark0c5f5462017-12-15 11:21:51 -0500329 canvas->drawImage(image, x, 0);
330 x += 128;
331}
Cary Clarka560c472017-11-27 10:44:06 -0500332##
333
Cary Clark3cd22cc2017-12-01 11:49:58 -0500334#SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -0500335
336#Method ##
337
338# ------------------------------------------------------------------------------
339
340#Method static sk_sp<SkImage> MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> data,
341 bool buildMips,
Brian Osman584b5012018-04-13 15:48:26 -0400342 SkColorSpace* dstColorSpace,
343 bool limitToMaxTextureSize = false)
Cary Clark61313f32018-10-08 14:57:48 -0400344#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500345#Line # creates Image from encoded data, and uploads to GPU ##
Cary Clark09d80c02018-10-31 12:14:03 -0400346#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500347
348#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500349#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500350#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500351GrContext* context = canvas->getGrContext();
352sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, 100);
353sk_sp<SkImage> image = SkImage::MakeCrossContextFromEncoded(context,
354 encodedData, false, nullptr);
355canvas->drawImage(image, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -0500356##
357
Cary Clark3cd22cc2017-12-01 11:49:58 -0500358#SeeAlso MakeCrossContextFromPixmap
359
360#Method ##
361
362# ------------------------------------------------------------------------------
363
364#Method static sk_sp<SkImage> MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap,
365 bool buildMips,
Brian Osman584b5012018-04-13 15:48:26 -0400366 SkColorSpace* dstColorSpace,
367 bool limitToMaxTextureSize = false)
Cary Clark61313f32018-10-08 14:57:48 -0400368#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500369#Line # creates Image from Pixmap, and uploads to GPU ##
Cary Clark09d80c02018-10-31 12:14:03 -0400370#Populate
Cary Clark3cd22cc2017-12-01 11:49:58 -0500371
372#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500373#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500374#Height 64
Cary Clark61ca7c52018-01-02 11:34:14 -0500375GrContext* context = canvas->getGrContext();
376SkPixmap pixmap;
377if (source.peekPixels(&pixmap)) {
378 sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(context, pixmap,
379 false, nullptr);
380 canvas->drawImage(image, 0, 0);
381}
Cary Clark3cd22cc2017-12-01 11:49:58 -0500382##
383
384#SeeAlso MakeCrossContextFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -0500385
386#Method ##
387
388# ------------------------------------------------------------------------------
389
390#Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context,
391 const GrBackendTexture& backendTexture,
392 GrSurfaceOrigin surfaceOrigin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500393 SkColorType colorType,
394 SkAlphaType alphaType = kPremul_SkAlphaType,
395 sk_sp<SkColorSpace> colorSpace = nullptr)
Cary Clark61313f32018-10-08 14:57:48 -0400396#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500397#Line # creates Image from GPU_Texture, managed internally ##
Cary Clark09d80c02018-10-31 12:14:03 -0400398#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500399
400#Example
Cary Clarkac47b882018-01-11 10:35:44 -0500401#Image 5
402#Platform gpu
Cary Clark61ca7c52018-01-02 11:34:14 -0500403 if (!canvas->getGrContext()) {
404 return;
405 }
406 canvas->scale(.5f, .5f);
407 canvas->clear(0x7f3f5f7f);
408 int x = 0, y = 0;
409 for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) {
410 for (auto alpha : { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType } ) {
411 sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(canvas->getGrContext(),
Cary Clark682c58d2018-05-16 07:07:07 -0400412 backEndTexture, origin,
Cary Clark61ca7c52018-01-02 11:34:14 -0500413 kRGBA_8888_SkColorType, alpha);
414 canvas->drawImage(image, x, y);
415 x += 160;
416 }
417 x -= 160 * 3;
418 y += 256;
419 }
Cary Clarka560c472017-11-27 10:44:06 -0500420##
421
Cary Clark61ca7c52018-01-02 11:34:14 -0500422#SeeAlso MakeFromTexture MakeFromYUVTexturesCopy
Cary Clarka560c472017-11-27 10:44:06 -0500423
424#Method ##
425
426# ------------------------------------------------------------------------------
427
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400428#Method static sk_sp<SkImage> MakeFromYUVATexturesCopy(GrContext* context,
429 SkYUVColorSpace yuvColorSpace,
430 const GrBackendTexture yuvaTextures[],
431 const SkYUVAIndex yuvaIndices[4],
432 SkISize imageSize,
433 GrSurfaceOrigin imageOrigin,
434 sk_sp<SkColorSpace> imageColorSpace = nullptr)
435#In Constructor
436#Line # creates Image from YUV_ColorSpace data ##
Cary Clark09d80c02018-10-31 12:14:03 -0400437#Populate
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400438
439#NoExample
440##
441
Cary Clark1801b942018-10-30 21:10:03 -0400442#SeeAlso MakeFromYUVATexturesCopyWithExternalBackend MakeFromYUVATextures
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400443
444#Method ##
445
Cary Clark1801b942018-10-30 21:10:03 -0400446#Method static sk_sp<SkImage> MakeFromYUVATextures(GrContext* context,
447 SkYUVColorSpace yuvColorSpace,
448 const GrBackendTexture yuvaTextures[],
449 const SkYUVAIndex yuvaIndices[4],
450 SkISize imageSize,
451 GrSurfaceOrigin imageOrigin,
452 sk_sp<SkColorSpace> imageColorSpace = nullptr);
453
454#In Constructor
455#Line # creates Image from YUV_ColorSpace data ##
Cary Clark09d80c02018-10-31 12:14:03 -0400456#Populate
Cary Clark1801b942018-10-30 21:10:03 -0400457
458#NoExample
459##
460
461#SeeAlso MakeFromYUVATexturesCopy MakeFromYUVATexturesCopyWithExternalBackend
462
463#Method ##
464
465
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400466# ------------------------------------------------------------------------------
467
468#Method static sk_sp<SkImage> MakeFromYUVATexturesCopyWithExternalBackend(
469 GrContext* context,
470 SkYUVColorSpace yuvColorSpace,
471 const GrBackendTexture yuvaTextures[],
472 const SkYUVAIndex yuvaIndices[4],
473 SkISize imageSize,
474 GrSurfaceOrigin imageOrigin,
475 const GrBackendTexture& backendTexture,
476 sk_sp<SkColorSpace> imageColorSpace = nullptr)
477#In Constructor
478#Line # creates Image from planar YUV_ColorSpace, stored in texture ##
Cary Clark09d80c02018-10-31 12:14:03 -0400479#Populate
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400480
481#NoExample
482##
483
Cary Clark1801b942018-10-30 21:10:03 -0400484#SeeAlso MakeFromYUVATexturesCopy MakeFromYUVATextures
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400485
486#Method ##
487
Cary Clarka560c472017-11-27 10:44:06 -0500488#Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400489 const GrBackendTexture yuvTextures[3],
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400490 GrSurfaceOrigin imageOrigin,
491 sk_sp<SkColorSpace> imageColorSpace = nullptr)
Cary Clark61313f32018-10-08 14:57:48 -0400492#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500493#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clark09d80c02018-10-31 12:14:03 -0400494#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500495
Cary Clark61ca7c52018-01-02 11:34:14 -0500496#NoExample
497##
498
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400499#SeeAlso MakeFromYUVTexturesCopyWithExternalBackend MakeFromNV12TexturesCopy MakeFromYUVATexturesCopy
Cary Clarkcdc371a2018-09-18 07:31:37 -0400500
501#Method ##
502
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400503# ------------------------------------------------------------------------------
504
Cary Clarkcdc371a2018-09-18 07:31:37 -0400505#Method static sk_sp<SkImage> MakeFromYUVTexturesCopyWithExternalBackend(
506 GrContext* context, SkYUVColorSpace yuvColorSpace,
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400507 const GrBackendTexture yuvTextures[3], GrSurfaceOrigin imageOrigin,
508 const GrBackendTexture& backendTexture, sk_sp<SkColorSpace> imageColorSpace = nullptr);
Cary Clark61313f32018-10-08 14:57:48 -0400509#In Constructors
Cary Clarkcdc371a2018-09-18 07:31:37 -0400510#Line # creates Image from planar YUV_ColorSpace, stored in texture ##
Cary Clark09d80c02018-10-31 12:14:03 -0400511#Populate
Cary Clarkcdc371a2018-09-18 07:31:37 -0400512
513#NoExample
514##
515
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400516#SeeAlso MakeFromYUVTexturesCopy MakeFromNV12TexturesCopy MakeFromYUVATexturesCopyWithExternalBackend
Cary Clark61ca7c52018-01-02 11:34:14 -0500517
518#Method ##
519
520# ------------------------------------------------------------------------------
521
Cary Clarka560c472017-11-27 10:44:06 -0500522#Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context,
523 SkYUVColorSpace yuvColorSpace,
Brian Salomon6a426c12018-03-15 12:16:02 -0400524 const GrBackendTexture nv12Textures[2],
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400525 GrSurfaceOrigin imageOrigin,
526 sk_sp<SkColorSpace> imageColorSpace = nullptr)
Cary Clark61313f32018-10-08 14:57:48 -0400527#In Constructors
Brian Salomon6a426c12018-03-15 12:16:02 -0400528#Line # creates Image from YUV_ColorSpace data in three planes ##
Cary Clark09d80c02018-10-31 12:14:03 -0400529#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500530
Cary Clark61ca7c52018-01-02 11:34:14 -0500531#NoExample
532##
533
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400534#SeeAlso MakeFromNV12TexturesCopyWithExternalBackend MakeFromYUVTexturesCopy MakeFromYUVATexturesCopy
Cary Clarkcdc371a2018-09-18 07:31:37 -0400535
536#Method ##
537
538#Method static sk_sp<SkImage> MakeFromNV12TexturesCopyWithExternalBackend(
539 GrContext* context,
540 SkYUVColorSpace yuvColorSpace,
541 const GrBackendTexture nv12Textures[2],
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400542 GrSurfaceOrigin imageOrigin,
543 const GrBackendTexture& backendTexture,
544 sk_sp<SkColorSpace> imageColorSpace = nullptr);
Cary Clark61313f32018-10-08 14:57:48 -0400545#In Constructors
Cary Clarkcdc371a2018-09-18 07:31:37 -0400546#Line # creates Image from planar YUV_ColorSpace, stored in texture ##
Cary Clark09d80c02018-10-31 12:14:03 -0400547#Populate
Cary Clarkcdc371a2018-09-18 07:31:37 -0400548
549#NoExample
550##
551
Robert Phillipsb6df1c12018-10-05 10:31:34 -0400552#SeeAlso MakeFromNV12TexturesCopy MakeFromYUVTexturesCopy MakeFromYUVATexturesCopyWithExternalBackend
Cary Clarka560c472017-11-27 10:44:06 -0500553
554#Method ##
555
556# ------------------------------------------------------------------------------
557
Cary Clark4855f782018-02-06 09:41:53 -0500558# currently uncalled by any test or client ##
Cary Clark61ca7c52018-01-02 11:34:14 -0500559#Bug 7424
Cary Clark61ca7c52018-01-02 11:34:14 -0500560
Cary Clark56356312018-02-08 14:45:18 -0500561#EnumClass BitDepth
Cary Clark682c58d2018-05-16 07:07:07 -0400562#Line # options for MakeFromPicture ##
Cary Clarka560c472017-11-27 10:44:06 -0500563#Code
Cary Clarka90ea222018-10-16 10:30:28 -0400564#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500565##
566
567#Const kU8 0
Cary Clark682c58d2018-05-16 07:07:07 -0400568#Line # uses 8-bit unsigned int per Color component ##
Cary Clarkffb3d682018-05-17 12:17:28 -0400569Use 8 bits per ARGB component using unsigned integer format.
Cary Clarka560c472017-11-27 10:44:06 -0500570##
571#Const kF16 1
Cary Clark682c58d2018-05-16 07:07:07 -0400572#Line # uses 16-bit float per Color component ##
Cary Clarkffb3d682018-05-17 12:17:28 -0400573Use 16 bits per ARGB component using half-precision floating point format.
Cary Clarka560c472017-11-27 10:44:06 -0500574##
575
Cary Clark61ca7c52018-01-02 11:34:14 -0500576#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500577##
578
Cary Clark61ca7c52018-01-02 11:34:14 -0500579#SeeAlso MakeFromPicture
Cary Clarka560c472017-11-27 10:44:06 -0500580
Cary Clark56356312018-02-08 14:45:18 -0500581#EnumClass ##
Cary Clarka560c472017-11-27 10:44:06 -0500582
583# ------------------------------------------------------------------------------
584
585#Method static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions,
586 const SkMatrix* matrix, const SkPaint* paint,
587 BitDepth bitDepth,
588 sk_sp<SkColorSpace> colorSpace)
Cary Clark61313f32018-10-08 14:57:48 -0400589#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500590#Line # creates Image from Picture ##
Cary Clark09d80c02018-10-31 12:14:03 -0400591#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500592
593#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500594 SkPaint paint;
595 SkPictureRecorder recorder;
596 SkCanvas* recordingCanvas = recorder.beginRecording(50, 50);
597 for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) {
598 paint.setColor(color);
599 recordingCanvas->drawRect({10, 10, 30, 40}, paint);
600 recordingCanvas->translate(10, 10);
601 recordingCanvas->scale(1.2f, 1.4f);
602 }
603 sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
604 int x = 0, y = 0;
605 for (auto alpha : { 70, 140, 210 } ) {
606 paint.setAlpha(alpha);
607 auto srgbColorSpace = SkColorSpace::MakeSRGB();
608 sk_sp<SkImage> image = SkImage::MakeFromPicture(playback, {50, 50}, nullptr, &paint,
609 SkImage::BitDepth::kU8, srgbColorSpace);
610 canvas->drawImage(image, x, y);
611 x += 70; y += 70;
612 }
Cary Clarka560c472017-11-27 10:44:06 -0500613##
614
Cary Clark61ca7c52018-01-02 11:34:14 -0500615#SeeAlso SkCanvas::drawPicture
Cary Clarka560c472017-11-27 10:44:06 -0500616
617#Method ##
618
619# ------------------------------------------------------------------------------
620
Cary Clark9548ea92018-09-13 15:26:33 -0400621#Method static sk_sp<SkImage> MakeFromAHardwareBuffer(
622 AHardwareBuffer* hardwareBuffer,
623 SkAlphaType alphaType = kPremul_SkAlphaType,
624 sk_sp<SkColorSpace> colorSpace = nullptr,
625 GrSurfaceOrigin surfaceOrigin = kTopLeft_GrSurfaceOrigin)
Cary Clark61313f32018-10-08 14:57:48 -0400626#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500627#Line # creates Image from Android hardware buffer ##
Cary Clark09d80c02018-10-31 12:14:03 -0400628#Populate
Cary Clark61ca7c52018-01-02 11:34:14 -0500629
630#NoExample
Cary Clarka560c472017-11-27 10:44:06 -0500631##
632
Cary Clark61ca7c52018-01-02 11:34:14 -0500633#SeeAlso MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -0500634
635#Method ##
636
637# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -0500638#Subtopic Property
Cary Clark4855f782018-02-06 09:41:53 -0500639#Line # values and attributes ##
640##
Cary Clarka560c472017-11-27 10:44:06 -0500641
642#Method int width() const
Cary Clark4855f782018-02-06 09:41:53 -0500643#In Property
644#Line # returns pixel column count ##
Cary Clark09d80c02018-10-31 12:14:03 -0400645#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500646
647#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500648#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500649#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500650 canvas->translate(10, 10);
651 canvas->drawImage(image, 0, 0);
652 canvas->translate(0, image->height());
653 SkPaint paint;
Cary Clark61ca7c52018-01-02 11:34:14 -0500654 canvas->drawLine(0, 10, image->width(), 10, paint);
Cary Clark14768f62018-10-29 20:33:51 -0400655 canvas->drawString("width", image->width() / 2 - 15, 25, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500656##
657
Cary Clark61ca7c52018-01-02 11:34:14 -0500658#SeeAlso dimensions() height()
Cary Clarka560c472017-11-27 10:44:06 -0500659
660#Method ##
661
662# ------------------------------------------------------------------------------
663
664#Method int height() const
Cary Clark4855f782018-02-06 09:41:53 -0500665#In Property
666#Line # returns pixel row count ##
Cary Clark09d80c02018-10-31 12:14:03 -0400667#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500668
669#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500670#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500671#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500672 canvas->translate(10, 10);
673 canvas->drawImage(image, 0, 0);
674 canvas->translate(image->width(), 0);
675 SkPaint paint;
Cary Clark61ca7c52018-01-02 11:34:14 -0500676 canvas->drawLine(10, 0, 10, image->height(), paint);
Cary Clark92694be2018-10-25 08:15:36 -0400677 canvas->drawString("height", 34, image->height() / 2, paint);
Cary Clarkac47b882018-01-11 10:35:44 -0500678##
Cary Clarka560c472017-11-27 10:44:06 -0500679
Cary Clark61ca7c52018-01-02 11:34:14 -0500680#SeeAlso dimensions() width()
Cary Clarka560c472017-11-27 10:44:06 -0500681
682#Method ##
683
684# ------------------------------------------------------------------------------
685
686#Method SkISize dimensions() const
Cary Clark4855f782018-02-06 09:41:53 -0500687#In Property
688#Line # returns width() and height() ##
Cary Clark09d80c02018-10-31 12:14:03 -0400689#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500690
691#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500692#Image 4
693 SkISize dimensions = image->dimensions();
694 SkIRect bounds = image->bounds();
695 SkIRect dimensionsAsBounds = SkIRect::MakeSize(dimensions);
696 SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!');
Cary Clark681287e2018-03-16 11:34:15 -0400697#StdOut
698dimensionsAsBounds == bounds
699##
Cary Clarka560c472017-11-27 10:44:06 -0500700##
701
Cary Clark61ca7c52018-01-02 11:34:14 -0500702#SeeAlso height() width() bounds()
Cary Clarka560c472017-11-27 10:44:06 -0500703
704#Method ##
705
706# ------------------------------------------------------------------------------
707
708#Method SkIRect bounds() const
Cary Clark4855f782018-02-06 09:41:53 -0500709#In Property
710#Line # returns width() and height() as Rectangle ##
Cary Clark09d80c02018-10-31 12:14:03 -0400711#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500712
713#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500714#Height 128
715#Image 4
Cary Clark61ca7c52018-01-02 11:34:14 -0500716 SkIRect bounds = image->bounds();
Cary Clarkac47b882018-01-11 10:35:44 -0500717 for (int x : { 0, bounds.width() } ) {
718 for (int y : { 0, bounds.height() } ) {
Cary Clark61ca7c52018-01-02 11:34:14 -0500719 canvas->drawImage(image, x, y);
720 }
721 }
Cary Clarka560c472017-11-27 10:44:06 -0500722##
723
Cary Clark682c58d2018-05-16 07:07:07 -0400724#SeeAlso dimensions()
Cary Clarka560c472017-11-27 10:44:06 -0500725
726#Method ##
727
728# ------------------------------------------------------------------------------
729
730#Method uint32_t uniqueID() const
Cary Clark4855f782018-02-06 09:41:53 -0500731#In Property
Cary Clark682c58d2018-05-16 07:07:07 -0400732#Line # returns identifier for Image ##
Cary Clark09d80c02018-10-31 12:14:03 -0400733#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500734
735#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500736#Image 5
737#Height 156
738 sk_sp<SkImage> subset = image->makeSubset({10, 20, 90, 100});
739 canvas->drawImage(image, 0, 0);
740 canvas->drawImage(subset, 128, 0);
741 SkPaint paint;
742 SkString s;
743 s.printf("original id: %d", image->uniqueID());
744 canvas->drawString(s, 20, image->height() + 20, paint);
745 s.printf("subset id: %d", subset->uniqueID());
746 canvas->drawString(s, 148, subset->height() + 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500747##
748
Cary Clark61ca7c52018-01-02 11:34:14 -0500749#SeeAlso isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -0500750
751#Method ##
752
753# ------------------------------------------------------------------------------
754
755#Method SkAlphaType alphaType() const
Cary Clark4855f782018-02-06 09:41:53 -0500756#In Property
757#Line # returns Alpha_Type ##
Cary Clark681287e2018-03-16 11:34:15 -0400758Returns Alpha_Type, one of: #list_of_alpha_types#.
Cary Clark61ca7c52018-01-02 11:34:14 -0500759
760Alpha_Type returned was a parameter to an Image constructor,
761or was parsed from encoded data.
762
763#Return Alpha_Type in Image ##
Cary Clarka560c472017-11-27 10:44:06 -0500764
765#Example
Cary Clark61ca7c52018-01-02 11:34:14 -0500766#Image 4
Cary Clarkac47b882018-01-11 10:35:44 -0500767#Height 96
Cary Clark61ca7c52018-01-02 11:34:14 -0500768 const char* alphaTypeStr[] = { "Unknown", "Opaque", "Premul", "Unpremul" };
769 SkAlphaType alphaType = image->alphaType();
Cary Clarkac47b882018-01-11 10:35:44 -0500770 canvas->drawImage(image, 16, 0);
Cary Clarkffb3d682018-05-17 12:17:28 -0400771 canvas->drawString(alphaTypeStr[(int) alphaType], 20, image->height() + 20, SkPaint());
Cary Clarka560c472017-11-27 10:44:06 -0500772##
773
Cary Clark61ca7c52018-01-02 11:34:14 -0500774#SeeAlso SkImageInfo::alphaType
Cary Clarka560c472017-11-27 10:44:06 -0500775
776#Method ##
777
778# ------------------------------------------------------------------------------
779
Greg Daniel56008aa2018-03-14 15:33:42 -0400780#Method SkColorType colorType() const
781#In Property
782#Line # returns Color_Type ##
Cary Clark09d80c02018-10-31 12:14:03 -0400783#Populate
Greg Daniel56008aa2018-03-14 15:33:42 -0400784
785#Example
Cary Clarkffb3d682018-05-17 12:17:28 -0400786#Image 4
787#Height 96
788 const char* colors[] = { "Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x",
789 "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16" };
790 SkColorType colorType = image->colorType();
791 canvas->drawImage(image, 16, 0);
792 canvas->drawString(colors[(int) colorType], 20, image->height() + 20, SkPaint());
Greg Daniel56008aa2018-03-14 15:33:42 -0400793##
794
795#SeeAlso SkImageInfo::colorType
796
797#Method ##
798
799# ------------------------------------------------------------------------------
800
Cary Clarka560c472017-11-27 10:44:06 -0500801#Method SkColorSpace* colorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -0500802#In Property
803#Line # returns Color_Space ##
Cary Clark09d80c02018-10-31 12:14:03 -0400804#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500805
806#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -0500807#Image 3
808#Set sRGB
809 SkPixmap pixmap;
810 source.peekPixels(&pixmap);
811 canvas->scale(.25f, .25f);
812 int y = 0;
813 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
814 SkColorSpace::kSRGB_RenderTargetGamma } ) {
815 int x = 0;
816 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
817 for (int index = 0; index < 2; ++index) {
818 pixmap.setColorSpace(colorSpace);
819 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
820 canvas->drawImage(image, x, y);
821 colorSpace = image->colorSpace()->makeColorSpin();
822 x += 512;
823 }
824 y += 512;
825 }
Cary Clarka560c472017-11-27 10:44:06 -0500826##
827
Cary Clark61dfc3a2018-01-03 08:37:53 -0500828#SeeAlso refColorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -0500829
830#Method ##
831
832# ------------------------------------------------------------------------------
833
834#Method sk_sp<SkColorSpace> refColorSpace() const
Cary Clark4855f782018-02-06 09:41:53 -0500835#In Property
836#Line # returns Image_Info Color_Space ##
Cary Clark09d80c02018-10-31 12:14:03 -0400837#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500838
839#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -0500840#Image 3
841#Set sRGB
842 SkPixmap pixmap;
843 source.peekPixels(&pixmap);
844 canvas->scale(.25f, .25f);
845 int y = 0;
846 for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma,
847 SkColorSpace::kSRGB_RenderTargetGamma } ) {
848 int x = 0;
849 sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut);
850 for (int index = 0; index < 2; ++index) {
851 pixmap.setColorSpace(colorSpace);
852 sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap);
853 canvas->drawImage(image, x, y);
854 colorSpace = image->refColorSpace()->makeColorSpin();
855 x += 512;
856 }
857 y += 512;
858 }
Cary Clarka560c472017-11-27 10:44:06 -0500859##
860
Cary Clark61dfc3a2018-01-03 08:37:53 -0500861#SeeAlso colorSpace makeColorSpace
Cary Clarka560c472017-11-27 10:44:06 -0500862
863#Method ##
864
865# ------------------------------------------------------------------------------
866
867#Method bool isAlphaOnly() const
Cary Clark4855f782018-02-06 09:41:53 -0500868#In Property
869#Line # returns if pixels represent a transparency mask ##
Cary Clark09d80c02018-10-31 12:14:03 -0400870#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500871
872#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -0500873 uint8_t pmColors = 0;
874 sk_sp<SkImage> image = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1), &pmColors, 1});
875 SkDebugf("alphaOnly = %s\n", image->isAlphaOnly() ? "true" : "false");
876#StdOut
877alphaOnly = true
878##
Cary Clarka560c472017-11-27 10:44:06 -0500879##
880
Cary Clark61dfc3a2018-01-03 08:37:53 -0500881#SeeAlso alphaType isOpaque
Cary Clarka560c472017-11-27 10:44:06 -0500882
883#Method ##
884
885# ------------------------------------------------------------------------------
886
887#Method bool isOpaque() const
Cary Clark4855f782018-02-06 09:41:53 -0500888#In Property
889#Line # returns if Alpha_Type is kOpaque_SkAlphaType ##
Cary Clark09d80c02018-10-31 12:14:03 -0400890#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500891
892#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -0500893 auto check_isopaque = [](const SkImageInfo& imageInfo) -> void {
894 auto surface(SkSurface::MakeRaster(imageInfo));
895 auto image(surface->makeImageSnapshot());
896 SkDebugf("isOpaque = %s\n", image->isOpaque() ? "true" : "false");
897 };
898
899 check_isopaque(SkImageInfo::MakeN32Premul(5, 5));
900 check_isopaque(SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType));
901#StdOut
902isOpaque = false
903isOpaque = true
904##
Cary Clarka560c472017-11-27 10:44:06 -0500905##
906
Cary Clark61dfc3a2018-01-03 08:37:53 -0500907#SeeAlso alphaType isAlphaOnly
Cary Clarka560c472017-11-27 10:44:06 -0500908
909#Method ##
910
911# ------------------------------------------------------------------------------
912
913#Method sk_sp<SkShader> makeShader(SkShader::TileMode tileMode1, SkShader::TileMode tileMode2,
914 const SkMatrix* localMatrix = nullptr) const
Cary Clark61313f32018-10-08 14:57:48 -0400915#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -0500916#Line # creates Shader, Paint element that can tile Image ##
Cary Clark09d80c02018-10-31 12:14:03 -0400917#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500918
919#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -0500920#Image 4
921SkMatrix matrix;
922matrix.setRotate(45);
923SkPaint paint;
924paint.setShader(image->makeShader(SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode,
925 &matrix));
926canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -0500927##
928
Cary Clark61dfc3a2018-01-03 08:37:53 -0500929#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -0500930
931#Method ##
932
933# ------------------------------------------------------------------------------
934
935#Method sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const
Cary Clark09d80c02018-10-31 12:14:03 -0400936#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500937
938#Example
Cary Clark61dfc3a2018-01-03 08:37:53 -0500939#Image 5
940SkMatrix matrix;
941matrix.setRotate(45);
942matrix.postTranslate(125, 30);
943SkPaint paint;
944paint.setShader(image->makeShader(&matrix));
945canvas->drawPaint(paint);
Cary Clarka560c472017-11-27 10:44:06 -0500946##
947
Cary Clarkf5404bb2018-01-05 12:10:09 -0500948#SeeAlso scalePixels
Cary Clarka560c472017-11-27 10:44:06 -0500949
950#Method ##
951
952# ------------------------------------------------------------------------------
Cary Clark78de7512018-02-07 07:27:09 -0500953#Subtopic Pixels
Cary Clark78de7512018-02-07 07:27:09 -0500954#Line # read and write pixel values ##
955##
Cary Clarka560c472017-11-27 10:44:06 -0500956
957#Method bool peekPixels(SkPixmap* pixmap) const
Cary Clark78de7512018-02-07 07:27:09 -0500958#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -0500959#Line # returns Pixmap if possible ##
Cary Clark09d80c02018-10-31 12:14:03 -0400960#Populate
Cary Clarka560c472017-11-27 10:44:06 -0500961
962#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -0500963 SkBitmap bitmap;
964 bitmap.allocPixels(SkImageInfo::MakeN32Premul(12, 11));
965 SkCanvas offscreen(bitmap);
966 offscreen.clear(SK_ColorWHITE);
967 SkPaint paint;
968 offscreen.drawString("%", 1, 10, paint);
969 sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap);
970 SkPixmap pixmap;
971 if (image->peekPixels(&pixmap)) {
972 const SkPMColor* pixels = pixmap.addr32();
973 SkPMColor pmWhite = pixels[0];
974 for (int y = 0; y < image->height(); ++y) {
975 for (int x = 0; x < image->width(); ++x) {
976 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
977 }
978 SkDebugf("\n");
979 }
980 }
981#StdOut
982------------
983--xx----x---
984-x--x--x----
985-x--x--x----
986-x--x-x-----
987--xx-xx-xx--
988-----x-x--x-
989----x--x--x-
990----x--x--x-
991---x----xx--
992------------
993##
Cary Clarka560c472017-11-27 10:44:06 -0500994##
995
Cary Clarkf5404bb2018-01-05 12:10:09 -0500996#SeeAlso readPixels
Cary Clarka560c472017-11-27 10:44:06 -0500997
998#Method ##
999
1000# ------------------------------------------------------------------------------
1001
1002#Method GrTexture* getTexture() const
Cary Clark682c58d2018-05-16 07:07:07 -04001003#Deprecated
Cary Clarka560c472017-11-27 10:44:06 -05001004#Method ##
1005
1006# ------------------------------------------------------------------------------
1007
1008#Method bool isTextureBacked() const
Cary Clark78de7512018-02-07 07:27:09 -05001009#In Property
Cary Clark4855f782018-02-06 09:41:53 -05001010#Line # returns if Image was created from GPU_Texture ##
Cary Clark09d80c02018-10-31 12:14:03 -04001011#Populate
Cary Clarka560c472017-11-27 10:44:06 -05001012
1013#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001014#Image 5
1015#Platform gpu
1016auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1017 if (nullptr == image) {
1018 return;
1019 }
1020 SkPaint paint;
1021 paint.setAntiAlias(true);
Cary Clarkf5404bb2018-01-05 12:10:09 -05001022 canvas->drawImage(image, 0, 0);
Cary Clark14768f62018-10-29 20:33:51 -04001023 canvas->drawString(label, 30, image->height() / 4, paint);
Cary Clarkf5404bb2018-01-05 12:10:09 -05001024 canvas->drawString(image->isTextureBacked() ? "is GPU texture" : "not GPU texture",
Cary Clark14768f62018-10-29 20:33:51 -04001025 20, image->height() * 3 / 4, paint);
Cary Clarkf5404bb2018-01-05 12:10:09 -05001026};
1027sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1028sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001029 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1030 kOpaque_SkAlphaType, nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05001031drawImage(image, "image");
1032canvas->translate(image->width(), 0);
1033drawImage(bitmapImage, "source");
1034canvas->translate(-image->width(), image->height());
1035drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001036##
1037
Cary Clarkf5404bb2018-01-05 12:10:09 -05001038#SeeAlso MakeFromTexture isValid
Cary Clarka560c472017-11-27 10:44:06 -05001039
1040#Method ##
1041
1042# ------------------------------------------------------------------------------
1043
1044#Method bool isValid(GrContext* context) const
Cary Clark4855f782018-02-06 09:41:53 -05001045#In Property
1046#Line # returns if Image can draw to Raster_Surface or GPU_Context ##
Cary Clark09d80c02018-10-31 12:14:03 -04001047#Populate
Cary Clarka560c472017-11-27 10:44:06 -05001048
1049#Example
Cary Clarkf5404bb2018-01-05 12:10:09 -05001050#Image 5
1051#Platform gpu
1052auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1053 if (nullptr == image) {
1054 return;
1055 }
1056 SkPaint paint;
1057 paint.setAntiAlias(true);
Cary Clarkf5404bb2018-01-05 12:10:09 -05001058 canvas->drawImage(image, 0, 0);
1059 canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
1060 if (canvas->getGrContext()) {
1061 canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" :
Cary Clark14768f62018-10-29 20:33:51 -04001062 "not valid on GPU", 20, image->height() * 5 / 8, paint);
Cary Clarkf5404bb2018-01-05 12:10:09 -05001063 }
1064 canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" :
Cary Clark14768f62018-10-29 20:33:51 -04001065 "not valid on CPU", 20, image->height() * 7 / 8, paint);
Cary Clarkf5404bb2018-01-05 12:10:09 -05001066};
1067sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1068sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001069 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1070 kOpaque_SkAlphaType, nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05001071drawImage(image, "image");
1072canvas->translate(image->width(), 0);
1073drawImage(bitmapImage, "source");
1074canvas->translate(-image->width(), image->height());
1075drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001076##
1077
Cary Clarkf5404bb2018-01-05 12:10:09 -05001078#SeeAlso isTextureBacked isLazyGenerated
Cary Clarka560c472017-11-27 10:44:06 -05001079
1080#Method ##
1081
1082# ------------------------------------------------------------------------------
1083
Robert Phillipsc5509952018-04-04 15:54:55 -04001084#Method GrBackendTexture getBackendTexture(bool flushPendingGrContextIO,
1085 GrSurfaceOrigin* origin = nullptr) const
1086#In Property
1087#Line # returns GPU reference to Image as texture ##
Cary Clark09d80c02018-10-31 12:14:03 -04001088#Populate
Robert Phillipsc5509952018-04-04 15:54:55 -04001089
Cary Clarkba75aee2018-04-05 08:18:41 -04001090#Example
1091#Image 3
1092#Platform gpu
Brian Osman584b5012018-04-13 15:48:26 -04001093 GrContext* grContext = canvas->getGrContext();
1094 if (!grContext) {
1095 canvas->drawString("GPU only!", 20, 40, SkPaint());
1096 return;
1097 }
1098 sk_sp<SkImage> imageFromBackend = SkImage::MakeFromAdoptedTexture(grContext, backEndTexture,
1099 kBottomLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1100 GrBackendTexture textureFromImage = imageFromBackend->getBackendTexture(false);
1101 if (!textureFromImage.isValid()) {
1102 return;
1103 }
1104 sk_sp<SkImage> imageFromTexture = SkImage::MakeFromAdoptedTexture(grContext, textureFromImage,
1105 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
1106 canvas->drawImage(imageFromTexture, 0, 0);
Cary Clarkba75aee2018-04-05 08:18:41 -04001107 canvas->drawImage(imageFromBackend, 128, 128);
Robert Phillipsc5509952018-04-04 15:54:55 -04001108##
1109
1110#SeeAlso MakeFromTexture isTextureBacked
1111
1112#Method ##
1113
1114# ------------------------------------------------------------------------------
1115
Cary Clarka560c472017-11-27 10:44:06 -05001116#Enum CachingHint
Cary Clark682c58d2018-05-16 07:07:07 -04001117#Line # options for readPixels and scalePixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001118#Code
Cary Clarka90ea222018-10-16 10:30:28 -04001119#Populate
Cary Clarka560c472017-11-27 10:44:06 -05001120##
1121
Cary Clarkac47b882018-01-11 10:35:44 -05001122CachingHint selects whether Skia may internally cache Bitmaps generated by
1123decoding Image, or by copying Image from GPU to CPU. The default behavior
Cary Clark682c58d2018-05-16 07:07:07 -04001124allows caching Bitmaps.
Cary Clarkac47b882018-01-11 10:35:44 -05001125
1126Choose kDisallow_CachingHint if Image pixels are to be used only once, or
1127if Image pixels reside in a cache outside of Skia, or to reduce memory pressure.
1128
1129Choosing kAllow_CachingHint does not ensure that pixels will be cached.
1130Image pixels may not be cached if memory requirements are too large or
1131pixels are not accessible.
Cary Clarka560c472017-11-27 10:44:06 -05001132
1133#Const kAllow_CachingHint 0
Cary Clark682c58d2018-05-16 07:07:07 -04001134#Line # allows internally caching decoded and copied pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001135##
1136#Const kDisallow_CachingHint 1
Cary Clark682c58d2018-05-16 07:07:07 -04001137#Line # disallows internally caching decoded and copied pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001138##
1139
Cary Clarkac47b882018-01-11 10:35:44 -05001140#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001141##
1142
Cary Clarkac47b882018-01-11 10:35:44 -05001143#SeeAlso readPixels scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001144
1145#Enum ##
1146
1147# ------------------------------------------------------------------------------
1148
1149#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1150 int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001151#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001152#Line # copies and converts pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001153
Cary Clarkac47b882018-01-11 10:35:44 -05001154Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY),
Cary Clark682c58d2018-05-16 07:07:07 -04001155and does not exceed Image (width(), height()).
Cary Clarkac47b882018-01-11 10:35:44 -05001156
1157dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of
1158destination. dstRowBytes specifics the gap from one destination row to the next.
1159Returns true if pixels are copied. Returns false if:
1160#List
Cary Clark77b3f3a2018-11-07 14:59:03 -05001161# dstInfo has no address ##
1162# dstRowBytes is less than dstInfo.minRowBytes() ##
Cary Clarkac47b882018-01-11 10:35:44 -05001163# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001164##
1165
Cary Clarkac47b882018-01-11 10:35:44 -05001166Pixels are copied only if pixel conversion is possible. If Image Color_Type is
Cary Clark77b3f3a2018-11-07 14:59:03 -05001167kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType() must match.
1168If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace() must match.
1169If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType() must
1170match. If Image Color_Space is nullptr, dstInfo.colorSpace() must match. Returns
Cary Clarkac47b882018-01-11 10:35:44 -05001171false if pixel conversion is not possible.
Cary Clarka560c472017-11-27 10:44:06 -05001172
Cary Clarkac47b882018-01-11 10:35:44 -05001173srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark682c58d2018-05-16 07:07:07 -04001174false if width() or height() is zero or negative.
Cary Clark2be81cf2018-09-13 12:04:30 -04001175Returns false if #Formula # abs(srcX) >= Image width() ##, or if #Formula # abs(srcY) >= Image height() ##.
Cary Clarka560c472017-11-27 10:44:06 -05001176
Cary Clarkac47b882018-01-11 10:35:44 -05001177If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1178If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1179
1180#Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ##
1181#Param dstPixels destination pixel storage ##
1182#Param dstRowBytes destination row length ##
1183#Param srcX column index whose absolute value is less than width() ##
1184#Param srcY row index whose absolute value is less than height() ##
1185#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1186
1187#Return true if pixels are copied to dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001188
1189#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001190#Image 3
1191 canvas->scale(.5f, .5f);
1192 const int width = 32;
1193 const int height = 32;
1194 std::vector<int32_t> dstPixels;
1195 dstPixels.resize(height * width * 4);
1196 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1197 for (int y = 0; y < 512; y += height ) {
1198 for (int x = 0; x < 512; x += width ) {
1199 if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) {
1200 SkPixmap dstPixmap(info, &dstPixels.front(), width * 4);
1201 SkBitmap bitmap;
1202 bitmap.installPixels(dstPixmap);
1203 canvas->drawBitmap(bitmap, 0, 0);
1204 }
1205 canvas->translate(48, 0);
1206 }
1207 canvas->translate(-16 * 48, 48);
1208 }
Cary Clarka560c472017-11-27 10:44:06 -05001209##
1210
Cary Clarkac47b882018-01-11 10:35:44 -05001211#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001212
1213#Method ##
1214
1215# ------------------------------------------------------------------------------
1216
1217#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY,
1218 CachingHint cachingHint = kAllow_CachingHint) const
1219
Cary Clarkac47b882018-01-11 10:35:44 -05001220Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and
Cary Clark682c58d2018-05-16 07:07:07 -04001221does not exceed Image (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001222
Cary Clarkac47b882018-01-11 10:35:44 -05001223dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage,
Cary Clark77b3f3a2018-11-07 14:59:03 -05001224and row bytes of destination. dst.rowBytes() specifics the gap from one destination
Cary Clarkac47b882018-01-11 10:35:44 -05001225row to the next. Returns true if pixels are copied. Returns false if:
1226#List
1227# dst pixel storage equals nullptr ##
Cary Clark77b3f3a2018-11-07 14:59:03 -05001228# dst.rowBytes() is less than SkImageInfo::minRowBytes ##
Cary Clarkac47b882018-01-11 10:35:44 -05001229# Pixel_Ref is nullptr ##
Cary Clarka560c472017-11-27 10:44:06 -05001230##
1231
Cary Clarkac47b882018-01-11 10:35:44 -05001232Pixels are copied only if pixel conversion is possible. If Image Color_Type is
Cary Clark77b3f3a2018-11-07 14:59:03 -05001233kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType() must match.
1234If Image Color_Type is kGray_8_SkColorType, dst.colorSpace() must match.
1235If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType() must
1236match. If Image Color_Space is nullptr, dst.colorSpace() must match. Returns
Cary Clarkac47b882018-01-11 10:35:44 -05001237false if pixel conversion is not possible.
Cary Clark682c58d2018-05-16 07:07:07 -04001238
Cary Clarkac47b882018-01-11 10:35:44 -05001239srcX and srcY may be negative to copy only top or left of source. Returns
Cary Clark682c58d2018-05-16 07:07:07 -04001240false if width() or height() is zero or negative.
Cary Clark2be81cf2018-09-13 12:04:30 -04001241Returns false if #Formula # abs(srcX) >= Image width() ##, or if #Formula # abs(srcY) >= Image height() ##.
Cary Clarkac47b882018-01-11 10:35:44 -05001242
1243If cachingHint is kAllow_CachingHint, pixels may be retained locally.
1244If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache.
1245
1246#Param dst destination Pixmap: Image_Info, pixels, row bytes ##
1247#Param srcX column index whose absolute value is less than width() ##
1248#Param srcY row index whose absolute value is less than height() ##
1249#Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ##
1250
1251#Return true if pixels are copied to dst ##
1252
1253#Example
1254#Image 3
1255 std::vector<int32_t> srcPixels;
1256 int rowBytes = image->width() * 4;
1257 int quarterWidth = image->width() / 4;
1258 int quarterHeight = image->height() / 4;
1259 srcPixels.resize(image->height() * rowBytes);
1260 for (int y = 0; y < 4; ++y) {
1261 for (int x = 0; x < 4; ++x) {
1262 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1263 &srcPixels.front() + x * image->height() * quarterWidth +
1264 y * quarterWidth, rowBytes);
1265 image->readPixels(pixmap, x * quarterWidth, y * quarterHeight);
1266 }
1267 }
1268 canvas->scale(.5f, .5f);
1269 SkBitmap bitmap;
1270 bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()),
1271 &srcPixels.front(), rowBytes);
1272 canvas->drawBitmap(bitmap, 0, 0);
1273##
1274
1275#SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels
Cary Clarka560c472017-11-27 10:44:06 -05001276
1277#Method ##
1278
1279# ------------------------------------------------------------------------------
1280
1281#Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality,
1282 CachingHint cachingHint = kAllow_CachingHint) const
Cary Clark78de7512018-02-07 07:27:09 -05001283#In Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001284#Line # scales and converts one Image to another ##
Cary Clark09d80c02018-10-31 12:14:03 -04001285#Populate
Cary Clarka560c472017-11-27 10:44:06 -05001286
1287#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001288#Image 3
1289#Height 128
1290 std::vector<int32_t> srcPixels;
1291 int quarterWidth = image->width() / 16;
1292 int rowBytes = quarterWidth * 4;
1293 int quarterHeight = image->height() / 16;
1294 srcPixels.resize(quarterHeight * rowBytes);
1295 SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight),
1296 &srcPixels.front(), rowBytes);
1297 canvas->scale(4, 4);
1298 SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality,
1299 kMedium_SkFilterQuality, kHigh_SkFilterQuality };
1300 for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) {
1301 image->scalePixels(pixmap, qualities[index]);
1302 sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr);
1303 canvas->drawImage(filtered, 16 * index, 0);
1304 }
Cary Clarka560c472017-11-27 10:44:06 -05001305##
1306
Cary Clarkac47b882018-01-11 10:35:44 -05001307#SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels
Cary Clarka560c472017-11-27 10:44:06 -05001308
1309#Method ##
1310
1311# ------------------------------------------------------------------------------
1312
1313#Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const
Cary Clark78de7512018-02-07 07:27:09 -05001314#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001315#Line # returns encoded Image as SkData ##
Cary Clark09d80c02018-10-31 12:14:03 -04001316#Populate
Cary Clarka560c472017-11-27 10:44:06 -05001317
1318#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001319#Image 3
1320 canvas->scale(4, 4);
1321 SkIRect subset = {0, 0, 16, 64};
1322 int x = 0;
1323 for (int quality : { 0, 10, 50, 100 } ) {
1324 sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality));
1325 sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset);
1326 canvas->drawImage(filtered, x, 0);
1327 x += 16;
1328 }
Cary Clarka560c472017-11-27 10:44:06 -05001329##
1330
Cary Clarkac47b882018-01-11 10:35:44 -05001331#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001332
1333#Method ##
1334
1335# ------------------------------------------------------------------------------
1336
Cary Clark61ca7c52018-01-02 11:34:14 -05001337#Method sk_sp<SkData> encodeToData() const
Cary Clark09d80c02018-10-31 12:14:03 -04001338#Populate
Cary Clarka560c472017-11-27 10:44:06 -05001339
1340#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001341#Image 3
1342 canvas->scale(4, 4);
1343 SkIRect subset = {136, 32, 200, 96};
1344 sk_sp<SkData> data(image->encodeToData());
1345 sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset);
1346 canvas->drawImage(eye, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001347##
1348
Cary Clarkac47b882018-01-11 10:35:44 -05001349#SeeAlso refEncodedData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001350
1351#Method ##
1352
1353# ------------------------------------------------------------------------------
1354
1355#Method sk_sp<SkData> refEncodedData() const
Cary Clark78de7512018-02-07 07:27:09 -05001356#In Utility
Cary Clark4855f782018-02-06 09:41:53 -05001357#Line # returns Image encoded in SkData if present ##
Cary Clark09d80c02018-10-31 12:14:03 -04001358#Populate
Cary Clarka560c472017-11-27 10:44:06 -05001359
1360#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001361#Image 3
1362#Platform gpu
1363 struct {
1364 const char* name;
1365 sk_sp<SkImage> image;
1366 } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) },
1367 { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001368 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1369 kOpaque_SkAlphaType, nullptr) } };
Cary Clarkac47b882018-01-11 10:35:44 -05001370 SkString string;
1371 SkPaint paint;
1372 for (const auto& test : tests ) {
1373 if (!test.image) {
1374 string.printf("no %s", test.name);
1375 } else {
1376 string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name);
1377 }
1378 canvas->drawString(string, 10, 20, paint);
1379 canvas->translate(0, 20);
1380 }
Cary Clarka560c472017-11-27 10:44:06 -05001381##
1382
Cary Clarkac47b882018-01-11 10:35:44 -05001383#SeeAlso encodeToData MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001384
1385#Method ##
1386
1387# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001388#Subtopic Utility
Cary Clark4855f782018-02-06 09:41:53 -05001389#Line # rarely called management functions ##
1390##
Cary Clarka560c472017-11-27 10:44:06 -05001391
Cary Clarka560c472017-11-27 10:44:06 -05001392#Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const
Cary Clark61313f32018-10-08 14:57:48 -04001393#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05001394#Line # creates Image containing part of original ##
Cary Clark09d80c02018-10-31 12:14:03 -04001395#Populate
Cary Clarka560c472017-11-27 10:44:06 -05001396
1397#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001398#Image 3
1399 canvas->scale(.5f, .5f);
Cary Clarkc30382f2018-07-24 08:09:27 -04001400 const int width = 64;
1401 const int height = 64;
Cary Clarkac47b882018-01-11 10:35:44 -05001402 for (int y = 0; y < 512; y += height ) {
1403 for (int x = 0; x < 512; x += width ) {
1404 sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height}));
1405 canvas->drawImage(subset, x * 3 / 2, y * 3 / 2);
1406 }
1407 }
Cary Clarka560c472017-11-27 10:44:06 -05001408##
1409
Cary Clarkac47b882018-01-11 10:35:44 -05001410#SeeAlso MakeFromEncoded
Cary Clarka560c472017-11-27 10:44:06 -05001411
1412#Method ##
1413
1414# ------------------------------------------------------------------------------
1415
Greg Daniel5f4b09d2018-06-12 16:39:59 -04001416#Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace,
1417 GrMipMapped mipMapped = GrMipMapped::kNo) const
Cary Clark61313f32018-10-08 14:57:48 -04001418#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05001419#Line # creates Image matching Color_Space if possible ##
Cary Clark09d80c02018-10-31 12:14:03 -04001420#Populate
Cary Clarka560c472017-11-27 10:44:06 -05001421
1422#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001423#Platform gpu
1424#Image 5
1425 auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void {
1426 if (nullptr == image || nullptr == context) {
1427 return;
1428 }
1429 SkPaint paint;
1430 paint.setAntiAlias(true);
Cary Clarkac47b882018-01-11 10:35:44 -05001431 sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr));
1432 canvas->drawImage(texture, 0, 0);
Cary Clark14768f62018-10-29 20:33:51 -04001433 canvas->drawString(label, 20, texture->height() / 4, paint);
Cary Clarkac47b882018-01-11 10:35:44 -05001434 };
1435 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1436 GrContext* context = canvas->getGrContext();
1437 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001438 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1439 kOpaque_SkAlphaType, nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001440 drawImage(image, context, "image");
1441 canvas->translate(image->width(), 0);
1442 drawImage(bitmapImage, context, "source");
1443 canvas->translate(-image->width(), image->height());
1444 drawImage(textureImage, context, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001445##
1446
Cary Clarkac47b882018-01-11 10:35:44 -05001447#SeeAlso MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05001448
1449#Method ##
1450
1451# ------------------------------------------------------------------------------
1452
1453#Method sk_sp<SkImage> makeNonTextureImage() const
Cary Clark61313f32018-10-08 14:57:48 -04001454#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05001455#Line # creates Image without dependency on GPU_Texture ##
Cary Clark09d80c02018-10-31 12:14:03 -04001456#Populate
Cary Clark61ca7c52018-01-02 11:34:14 -05001457
1458#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001459#Image 5
1460#Platform gpu
1461 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1462 if (nullptr == image) {
1463 return;
1464 }
1465 SkPaint paint;
1466 paint.setAntiAlias(true);
Cary Clarkac47b882018-01-11 10:35:44 -05001467 sk_sp<SkImage> nonTexture(image->makeNonTextureImage());
1468 canvas->drawImage(nonTexture, 0, 0);
Cary Clark14768f62018-10-29 20:33:51 -04001469 canvas->drawString(label, 20, nonTexture->height() / 4, paint);
Cary Clarkac47b882018-01-11 10:35:44 -05001470 };
1471 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1472 sk_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 Clarkac47b882018-01-11 10:35:44 -05001475 drawImage(image, "image");
1476 canvas->translate(image->width(), 0);
1477 drawImage(bitmapImage, "source");
1478 canvas->translate(-image->width(), image->height());
1479 drawImage(textureImage, "backEndTexture");
Cary Clark61ca7c52018-01-02 11:34:14 -05001480##
1481
Cary Clark56356312018-02-08 14:45:18 -05001482#SeeAlso makeTextureImage makeRasterImage MakeBackendTextureFromSkImage
Cary Clark61ca7c52018-01-02 11:34:14 -05001483
1484#Method ##
1485
1486# ------------------------------------------------------------------------------
1487
1488#Method sk_sp<SkImage> makeRasterImage() const
Cary Clark61313f32018-10-08 14:57:48 -04001489#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05001490#Line # creates Image compatible with Raster_Surface if possible ##
Cary Clark09d80c02018-10-31 12:14:03 -04001491#Populate
Cary Clarka560c472017-11-27 10:44:06 -05001492
Cary Clarka560c472017-11-27 10:44:06 -05001493#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001494#Image 5
1495#Platform gpu
1496 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1497 if (nullptr == image) {
1498 return;
1499 }
1500 SkPaint paint;
1501 paint.setAntiAlias(true);
Cary Clarkac47b882018-01-11 10:35:44 -05001502 sk_sp<SkImage> raster(image->makeRasterImage());
1503 canvas->drawImage(raster, 0, 0);
Cary Clark14768f62018-10-29 20:33:51 -04001504 canvas->drawString(label, 20, raster->height() / 4, paint);
Cary Clarkac47b882018-01-11 10:35:44 -05001505 };
1506 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1507 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001508 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1509 kOpaque_SkAlphaType, nullptr));
Cary Clarkac47b882018-01-11 10:35:44 -05001510 drawImage(image, "image");
1511 canvas->translate(image->width(), 0);
1512 drawImage(bitmapImage, "source");
1513 canvas->translate(-image->width(), image->height());
1514 drawImage(textureImage, "backEndTexture");
Cary Clarka560c472017-11-27 10:44:06 -05001515##
1516
Cary Clarkac47b882018-01-11 10:35:44 -05001517#SeeAlso isTextureBacked isLazyGenerated MakeFromRaster
Cary Clarka560c472017-11-27 10:44:06 -05001518
1519#Method ##
1520
1521# ------------------------------------------------------------------------------
1522
1523#Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset,
1524 const SkIRect& clipBounds, SkIRect* outSubset,
1525 SkIPoint* offset) const
Cary Clark61313f32018-10-08 14:57:48 -04001526#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05001527#Line # creates filtered, clipped Image ##
Cary Clark09d80c02018-10-31 12:14:03 -04001528#Populate
Cary Clarka560c472017-11-27 10:44:06 -05001529
Cary Clarka560c472017-11-27 10:44:06 -05001530#Example
Cary Clarkac47b882018-01-11 10:35:44 -05001531#Description
1532In each frame of the animation, filtered Image is drawn in a different location.
1533By translating canvas by returned offset, Image appears stationary.
1534##
1535#Image 5
1536#Platform gpu
Cary Clark5717e822018-09-21 11:36:41 -04001537#Duration 1
Cary Clarkac47b882018-01-11 10:35:44 -05001538 sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make(
1539 -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE,
1540 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
1541 nullptr);
1542 sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr);
1543 SkIRect subset = image->bounds();
1544 SkIRect clipBounds = image->bounds();
1545 clipBounds.outset(60, 60);
1546 SkIRect outSubset;
1547 SkIPoint offset;
1548 sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds,
1549 &outSubset, &offset));
1550 SkPaint paint;
1551 paint.setAntiAlias(true);
1552 paint.setStyle(SkPaint::kStroke_Style);
1553 canvas->drawLine(0, 0, offset.fX, offset.fY, paint);
1554 canvas->translate(offset.fX, offset.fY);
1555 canvas->drawImage(filtered, 0, 0);
Cary Clark681287e2018-03-16 11:34:15 -04001556 canvas->drawRect(SkRect::Make(outSubset), paint);
Cary Clarka560c472017-11-27 10:44:06 -05001557##
1558
Cary Clark56356312018-02-08 14:45:18 -05001559#SeeAlso makeShader SkPaint::setImageFilter
Cary Clarka560c472017-11-27 10:44:06 -05001560
1561#Method ##
1562
1563# ------------------------------------------------------------------------------
1564
Cary Clarka560c472017-11-27 10:44:06 -05001565#Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc
Cary Clark682c58d2018-05-16 07:07:07 -04001566#Line # parameter type for MakeBackendTextureFromSkImage ##
Cary Clarkffb3d682018-05-17 12:17:28 -04001567
1568#Code
Cary Clarka90ea222018-10-16 10:30:28 -04001569#Populate
Cary Clarkffb3d682018-05-17 12:17:28 -04001570##
1571
Cary Clark0d225392018-06-07 09:59:07 -04001572Defines a callback function, taking one parameter of type GrBackendTexture with
1573no return value. Function is called when back-end texture is to be released.
Cary Clarka560c472017-11-27 10:44:06 -05001574##
1575
1576# ------------------------------------------------------------------------------
1577
1578#Method static bool MakeBackendTextureFromSkImage(GrContext* context,
1579 sk_sp<SkImage> image,
1580 GrBackendTexture* backendTexture,
1581 BackendTextureReleaseProc* backendTextureReleaseProc)
Cary Clark61313f32018-10-08 14:57:48 -04001582#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05001583#Line # creates GPU_Texture from Image ##
Cary Clark09d80c02018-10-31 12:14:03 -04001584#Populate
Cary Clarka560c472017-11-27 10:44:06 -05001585
1586#Example
Cary Clark56356312018-02-08 14:45:18 -05001587#Platform gpu
1588#Height 64
1589#Function
Brian Salomon67f85842018-02-09 08:50:22 -05001590static sk_sp<SkImage> create_gpu_image(GrContext* grContext) {
1591 const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
1592 auto surface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info));
1593 SkCanvas* canvas = surface->getCanvas();
1594 canvas->clear(SK_ColorWHITE);
1595 SkPaint paint;
1596 paint.setColor(SK_ColorBLACK);
1597 canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
1598 return surface->makeImageSnapshot();
1599}
1600##
1601
Cary Clark682c58d2018-05-16 07:07:07 -04001602void draw(SkCanvas* canvas) {
Brian Salomon67f85842018-02-09 08:50:22 -05001603 GrContext* grContext = canvas->getGrContext();
1604 if (!grContext) {
1605 return;
1606 }
1607 sk_sp<SkImage> backEndImage = create_gpu_image(grContext);
1608 canvas->drawImage(backEndImage, 0, 0);
1609 GrBackendTexture texture;
1610 SkImage::BackendTextureReleaseProc proc;
1611 if (!SkImage::MakeBackendTextureFromSkImage(grContext, std::move(backEndImage),
1612 &texture, &proc)) {
1613 return;
1614 }
1615 sk_sp<SkImage> i2 = SkImage::MakeFromTexture(grContext, texture, kTopLeft_GrSurfaceOrigin,
1616 kN32_SkColorType, kOpaque_SkAlphaType, nullptr);
1617 canvas->drawImage(i2, 30, 30);
Cary Clark56356312018-02-08 14:45:18 -05001618}
Cary Clarka560c472017-11-27 10:44:06 -05001619##
1620
Cary Clark56356312018-02-08 14:45:18 -05001621#SeeAlso MakeFromTexture makeTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05001622
1623#Method ##
1624
1625# ------------------------------------------------------------------------------
1626
1627#Enum LegacyBitmapMode
Cary Clark56356312018-02-08 14:45:18 -05001628#Deprecated soon
Cary Clarka560c472017-11-27 10:44:06 -05001629#Code
Cary Clarka90ea222018-10-16 10:30:28 -04001630#Populate
Cary Clarka560c472017-11-27 10:44:06 -05001631##
1632
Cary Clarka560c472017-11-27 10:44:06 -05001633#Const kRO_LegacyBitmapMode 0
Cary Clark682c58d2018-05-16 07:07:07 -04001634#Line # returned bitmap is read-only and immutable ##
Cary Clarka560c472017-11-27 10:44:06 -05001635##
Cary Clarka560c472017-11-27 10:44:06 -05001636
1637#Enum ##
1638
1639# ------------------------------------------------------------------------------
1640
Cary Clark56356312018-02-08 14:45:18 -05001641#Method bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode = kRO_LegacyBitmapMode) const
Cary Clark61313f32018-10-08 14:57:48 -04001642#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05001643#Line # returns as Raster_Bitmap ##
Cary Clark09d80c02018-10-31 12:14:03 -04001644#Populate
Cary Clarka560c472017-11-27 10:44:06 -05001645
Cary Clarkae957c42018-06-07 17:07:17 -04001646#Example
Cary Clark56356312018-02-08 14:45:18 -05001647#Image 4
1648#Platform gpu
Brian Salomon67f85842018-02-09 08:50:22 -05001649 SkBitmap bitImage;
1650 if (image->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
1651 canvas->drawBitmap(bitImage, 0, 0);
1652 }
1653 GrContext* grContext = canvas->getGrContext();
1654 if (!grContext) {
1655 return;
1656 }
1657 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(grContext, backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001658 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1659 kOpaque_SkAlphaType, nullptr));
Brian Salomon67f85842018-02-09 08:50:22 -05001660 canvas->drawImage(textureImage, 45, 45);
1661 if (textureImage->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) {
1662 canvas->drawBitmap(bitImage, 90, 90);
1663 }
Cary Clarka560c472017-11-27 10:44:06 -05001664##
1665
Cary Clark56356312018-02-08 14:45:18 -05001666#SeeAlso MakeRasterData makeRasterImage makeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05001667
1668#Method ##
1669
1670# ------------------------------------------------------------------------------
1671
1672#Method bool isLazyGenerated() const
Cary Clark4855f782018-02-06 09:41:53 -05001673#In Property
1674#Line # returns if Image is created as needed ##
Cary Clark09d80c02018-10-31 12:14:03 -04001675#Populate
Cary Clarka560c472017-11-27 10:44:06 -05001676
1677#Example
Cary Clark2f466242017-12-11 16:03:17 -05001678#Height 80
1679#Function
1680class TestImageGenerator : public SkImageGenerator {
1681public:
1682 TestImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(10, 10)) {}
1683 ~TestImageGenerator() override {}
1684protected:
1685 bool onGetPixels(const SkImageInfo& info, void* pixelPtr, size_t rowBytes,
1686 const Options& options) override {
1687 SkPMColor* pixels = static_cast<SkPMColor*>(pixelPtr);
1688 for (int y = 0; y < info.height(); ++y) {
1689 for (int x = 0; x < info.width(); ++x) {
1690 pixels[y * info.width() + x] = 0xff223344 + y * 0x000C0811;
1691 }
1692 }
1693 return true;
1694 }
1695};
1696##
1697void draw(SkCanvas* canvas) {
1698 auto gen = std::unique_ptr<TestImageGenerator>(new TestImageGenerator());
1699 sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen)));
1700 SkString lazy(image->isLazyGenerated() ? "is lazy" : "not lazy");
1701 canvas->scale(8, 8);
1702 canvas->drawImage(image, 0, 0, nullptr);
1703 SkPaint paint;
1704 paint.setTextSize(4);
1705 canvas->drawString(lazy, 2, 5, paint);
1706}
Cary Clarka560c472017-11-27 10:44:06 -05001707##
1708
Cary Clarkf5404bb2018-01-05 12:10:09 -05001709#Example
1710#Image 5
1711#Platform gpu
1712void draw(SkCanvas* canvas) {
1713 auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void {
1714 if (nullptr == image) {
1715 return;
1716 }
1717 SkPaint paint;
1718 paint.setAntiAlias(true);
Cary Clarkf5404bb2018-01-05 12:10:09 -05001719 canvas->drawImage(image, 0, 0);
Cary Clark14768f62018-10-29 20:33:51 -04001720 canvas->drawString(label, 30, image->height() / 4, paint);
Cary Clarkf5404bb2018-01-05 12:10:09 -05001721 canvas->drawString(
1722 image->isLazyGenerated() ? "is lazily generated" : "not lazily generated",
Cary Clark14768f62018-10-29 20:33:51 -04001723 20, image->height() * 3 / 4, paint);
Cary Clarkf5404bb2018-01-05 12:10:09 -05001724 };
1725 sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
1726 sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Cary Clarkae957c42018-06-07 17:07:17 -04001727 kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
1728 kOpaque_SkAlphaType, nullptr));
Cary Clarkf5404bb2018-01-05 12:10:09 -05001729 drawImage(image, "image");
1730 canvas->translate(image->width(), 0);
1731 drawImage(bitmapImage, "source");
1732 canvas->translate(-image->width(), image->height());
1733 drawImage(textureImage, "backEndTexture");
1734}
1735##
1736
Cary Clark77b3f3a2018-11-07 14:59:03 -05001737#SeeAlso isTextureBacked makeNonTextureImage
Cary Clarka560c472017-11-27 10:44:06 -05001738
1739#Method ##
1740
1741# ------------------------------------------------------------------------------
1742
Cary Clarke80cd442018-07-17 13:19:56 -04001743#Method sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target) const
Cary Clark61313f32018-10-08 14:57:48 -04001744#In Constructors
Cary Clark4855f782018-02-06 09:41:53 -05001745#Line # creates Image matching Color_Space if possible ##
Cary Clark09d80c02018-10-31 12:14:03 -04001746#Populate
Cary Clarkac47b882018-01-11 10:35:44 -05001747
1748#Example
1749#Image 5
1750#Set sRGB
1751 sk_sp<SkColorSpace> normalColorSpace = SkColorSpace::MakeRGB(
1752 SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kSRGB_Gamut);
1753 sk_sp<SkColorSpace> wackyColorSpace = normalColorSpace->makeColorSpin();
1754 for (auto colorSpace : { normalColorSpace, wackyColorSpace } ) {
Cary Clarkc3c1c312018-07-18 09:25:15 -04001755 sk_sp<SkImage> colorSpaced = image->makeColorSpace(colorSpace);
1756 canvas->drawImage(colorSpaced, 0, 0);
1757 canvas->translate(128, 0);
Cary Clarkac47b882018-01-11 10:35:44 -05001758 }
1759##
1760
Cary Clark77b3f3a2018-11-07 14:59:03 -05001761#SeeAlso MakeFromPicture MakeFromTexture
Cary Clarka560c472017-11-27 10:44:06 -05001762
1763#Method ##
1764
1765#Class SkImage ##
1766
1767#Topic Image ##