Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1 | #Topic Image |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 2 | #Alias Image_Reference ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 3 | |
| 4 | #Class SkImage |
| 5 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 6 | Image describes a two dimensional array of pixels to draw. The pixels may be |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 7 | decoded in a Raster_Bitmap, encoded in a Picture or compressed data stream, |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 8 | or located in GPU memory as a GPU_Texture. |
| 9 | |
| 10 | Image cannot be modified after it is created. Image may allocate additional |
| 11 | storage as needed; for instance, an encoded Image may decode when drawn. |
| 12 | |
| 13 | Image width and height are greater than zero. Creating an Image with zero width |
| 14 | or height returns Image equal to nullptr. |
| 15 | |
| 16 | Image may be created from Bitmap, Pixmap, Surface, Picture, encoded streams, |
| 17 | GPU_Texture, YUV_ColorSpace data, or hardware buffer. Encoded streams supported |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 18 | include BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP. Supported encoding details |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 19 | vary with platform. |
| 20 | |
Cary Clark | 08895c4 | 2018-02-01 09:37:32 -0500 | [diff] [blame] | 21 | #Subtopic Raster_Image |
Cary Clark | 137b874 | 2018-05-30 09:21:49 -0400 | [diff] [blame] | 22 | #Alias Raster_Image ## |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 23 | #Line # pixels decoded in Raster_Bitmap ## |
| 24 | Raster_Image pixels are decoded in a Raster_Bitmap. These pixels may be read |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 25 | directly and in most cases written to, although edited pixels may not be drawn |
| 26 | if Image has been copied internally. |
| 27 | ## |
| 28 | |
Cary Clark | 08895c4 | 2018-02-01 09:37:32 -0500 | [diff] [blame] | 29 | #Subtopic Texture_Image |
| 30 | #Line # pixels located on GPU ## |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 31 | Texture_Image are located on GPU and pixels are not accessible. Texture_Image |
| 32 | are allocated optimally for best performance. Raster_Image may |
| 33 | be drawn to GPU_Surface, but pixels are uploaded from CPU to GPU downgrading |
| 34 | performance. |
| 35 | ## |
| 36 | |
Cary Clark | 08895c4 | 2018-02-01 09:37:32 -0500 | [diff] [blame] | 37 | #Subtopic Lazy_Image |
| 38 | #Line # deferred pixel buffer ## |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 39 | Lazy_Image defer allocating buffer for Image pixels and decoding stream until |
| 40 | Image is drawn. Lazy_Image caches result if possible to speed up repeated |
| 41 | drawing. |
| 42 | ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 43 | |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 44 | #Subtopic Overview |
Cary Clark | 08895c4 | 2018-02-01 09:37:32 -0500 | [diff] [blame] | 45 | #Populate |
| 46 | ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 47 | |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 48 | #Subtopic Related_Function |
Cary Clark | 08895c4 | 2018-02-01 09:37:32 -0500 | [diff] [blame] | 49 | #Populate |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 50 | ## |
| 51 | |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 52 | #Subtopic Constructor |
Cary Clark | 08895c4 | 2018-02-01 09:37:32 -0500 | [diff] [blame] | 53 | #Populate |
| 54 | ## |
Cary Clark | 5081eed | 2018-01-22 07:55:48 -0500 | [diff] [blame] | 55 | |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 56 | #Subtopic Member_Function |
Cary Clark | 08895c4 | 2018-02-01 09:37:32 -0500 | [diff] [blame] | 57 | #Populate |
| 58 | ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 59 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 60 | # ------------------------------------------------------------------------------ |
| 61 | |
| 62 | #Method static sk_sp<SkImage> MakeRasterCopy(const SkPixmap& pixmap) |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 63 | #In Constructor |
| 64 | #Line # creates Image from Pixmap and copied pixels ## |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 65 | Creates Image from Pixmap and copy of pixels. Since pixels are copied, Pixmap |
| 66 | pixels may be modified or deleted without affecting Image. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 67 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 68 | Image is returned if Pixmap is valid. Valid Pixmap parameters include: |
| 69 | dimensions are greater than zero; |
| 70 | each dimension fits in 29 bits; |
| 71 | Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType; |
| 72 | row bytes are large enough to hold one row of pixels; |
| 73 | pixel address is not nullptr. |
| 74 | |
| 75 | #Param pixmap Image_Info, pixel address, and row bytes ## |
| 76 | |
| 77 | #Return copy of Pixmap pixels, or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 78 | |
| 79 | #Example |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 80 | #Height 50 |
| 81 | #Description |
| 82 | Draw a five by five bitmap, and draw a copy in an Image. Editing the pixmap |
| 83 | alters the bitmap draw, but does not alter the Image draw since the Image |
| 84 | contains a copy of the pixels. |
| 85 | ## |
| 86 | uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 }, |
| 87 | { 0xAC, 0xA8, 0x89, 0xA7, 0x87 }, |
| 88 | { 0x9B, 0xB5, 0xE5, 0x95, 0x46 }, |
| 89 | { 0x90, 0x81, 0xC5, 0x71, 0x33 }, |
| 90 | { 0x75, 0x55, 0x44, 0x40, 0x30 }}; |
| 91 | SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType); |
| 92 | SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5); |
| 93 | SkBitmap bitmap; |
| 94 | bitmap.installPixels(pixmap); |
| 95 | sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap); |
| 96 | *pixmap.writable_addr8(2, 2) = 0x00; |
| 97 | canvas->scale(10, 10); |
| 98 | canvas->drawBitmap(bitmap, 0, 0); |
| 99 | canvas->drawImage(image, 10, 0); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 100 | ## |
| 101 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 102 | #SeeAlso MakeRasterData MakeFromGenerator |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 103 | |
| 104 | #Method ## |
| 105 | |
| 106 | # ------------------------------------------------------------------------------ |
| 107 | |
Cary Clark | cc45cc7 | 2018-05-15 16:06:12 -0400 | [diff] [blame] | 108 | #Method static sk_sp<SkImage> MakeRasterData(const SkImageInfo& info, sk_sp<SkData> pixels, size_t rowBytes) |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 109 | #In Constructor |
| 110 | #Line # creates Image from Image_Info and shared pixels ## |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 111 | Creates Image from Image_Info, sharing pixels. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 112 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 113 | Image is returned if Image_Info is valid. Valid Image_Info parameters include: |
| 114 | dimensions are greater than zero; |
| 115 | each dimension fits in 29 bits; |
| 116 | Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType; |
| 117 | rowBytes are large enough to hold one row of pixels; |
| 118 | pixels is not nullptr, and contains enough data for Image. |
| 119 | |
| 120 | #Param info contains width, height, Alpha_Type, Color_Type, Color_Space ## |
| 121 | #Param pixels address or pixel storage ## |
| 122 | #Param rowBytes size of pixel row or larger ## |
| 123 | |
| 124 | #Return Image sharing pixels, or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 125 | |
| 126 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 127 | #Image 3 |
| 128 | size_t rowBytes = image->width() * SkColorTypeBytesPerPixel(kRGBA_8888_SkColorType); |
| 129 | sk_sp<SkData> data = SkData::MakeUninitialized(rowBytes * image->height()); |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 130 | SkImageInfo dstInfo = SkImageInfo::MakeN32(image->width(), image->height(), |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 131 | kPremul_SkAlphaType); |
| 132 | image->readPixels(dstInfo, data->writable_data(), rowBytes, 0, 0, SkImage::kAllow_CachingHint); |
| 133 | sk_sp<SkImage> raw = SkImage::MakeRasterData(dstInfo.makeColorType(kRGBA_8888_SkColorType), |
| 134 | data, rowBytes); |
| 135 | canvas->drawImage(image, 0, 0); |
| 136 | canvas->drawImage(raw.get(), 128, 0); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 137 | ## |
| 138 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 139 | #SeeAlso MakeRasterCopy MakeFromGenerator |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 140 | |
| 141 | #Method ## |
| 142 | |
| 143 | # ------------------------------------------------------------------------------ |
| 144 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 145 | #Typedef void* ReleaseContext |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 146 | #Line # parameter type for MakeFromRaster ## |
Cary Clark | ffb3d68 | 2018-05-17 12:17:28 -0400 | [diff] [blame] | 147 | |
| 148 | #Code |
| 149 | typedef void* ReleaseContext; |
| 150 | ## |
| 151 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 152 | Caller data passed to RasterReleaseProc; may be nullptr. |
| 153 | |
| 154 | #SeeAlso MakeFromRaster RasterReleaseProc |
| 155 | |
| 156 | ## |
| 157 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 158 | #Typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext) |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 159 | #Line # parameter type for MakeFromRaster ## |
Cary Clark | ffb3d68 | 2018-05-17 12:17:28 -0400 | [diff] [blame] | 160 | |
| 161 | #Code |
| 162 | typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext); |
| 163 | ## |
| 164 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 165 | Function called when Image no longer shares pixels. ReleaseContext is |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 166 | provided by caller when Image is created, and may be nullptr. |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 167 | |
| 168 | #SeeAlso ReleaseContext MakeFromRaster |
| 169 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 170 | ## |
| 171 | |
| 172 | #Method static sk_sp<SkImage> MakeFromRaster(const SkPixmap& pixmap, |
| 173 | RasterReleaseProc rasterReleaseProc, |
| 174 | ReleaseContext releaseContext) |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 175 | #In Constructor |
| 176 | #Line # creates Image from Pixmap, with release ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 177 | |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 178 | Creates Image from pixmap, sharing Pixmap pixels. Pixels must remain valid and |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 179 | unchanged until rasterReleaseProc is called. rasterReleaseProc is passed |
| 180 | releaseContext when Image is deleted or no longer refers to pixmap pixels. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 181 | |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 182 | Pass nullptr for rasterReleaseProc to share Pixmap without requiring a callback |
| 183 | when Image is released. Pass nullptr for releaseContext if rasterReleaseProc |
| 184 | does not require state. |
| 185 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 186 | Image is returned if pixmap is valid. Valid Pixmap parameters include: |
| 187 | dimensions are greater than zero; |
| 188 | each dimension fits in 29 bits; |
| 189 | Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType; |
| 190 | row bytes are large enough to hold one row of pixels; |
| 191 | pixel address is not nullptr. |
| 192 | |
| 193 | #Param pixmap Image_Info, pixel address, and row bytes ## |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 194 | #Param rasterReleaseProc function called when pixels can be released; or nullptr ## |
| 195 | #Param releaseContext state passed to rasterReleaseProc; or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 196 | |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 197 | #Return Image sharing pixmap ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 198 | |
| 199 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 200 | #Function |
| 201 | static void releaseProc(const void* pixels, SkImage::ReleaseContext context) { |
| 202 | int* countPtr = static_cast<int*>(context); |
| 203 | *countPtr += 1; |
| 204 | } |
| 205 | ## |
| 206 | |
| 207 | void draw(SkCanvas* canvas) { |
| 208 | SkColor color = 0; |
| 209 | SkPixmap pixmap(SkImageInfo::MakeN32(1, 1, kPremul_SkAlphaType), &color, 4); |
| 210 | int releaseCount = 0; |
| 211 | sk_sp<SkImage> image(SkImage::MakeFromRaster(pixmap, releaseProc, &releaseCount)); |
| 212 | SkDebugf("before reset: %d\n", releaseCount); |
| 213 | image.reset(); |
| 214 | SkDebugf("after reset: %d\n", releaseCount); |
| 215 | } |
| 216 | #StdOut |
| 217 | before reset: 0 |
| 218 | after reset: 1 |
| 219 | ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 220 | ## |
| 221 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 222 | #SeeAlso MakeRasterCopy MakeRasterData MakeFromGenerator RasterReleaseProc ReleaseContext |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 223 | |
| 224 | #Method ## |
| 225 | |
| 226 | # ------------------------------------------------------------------------------ |
| 227 | |
| 228 | #Method static sk_sp<SkImage> MakeFromBitmap(const SkBitmap& bitmap) |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 229 | #In Constructor |
| 230 | #Line # creates Image from Bitmap, sharing or copying pixels ## |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 231 | Creates Image from bitmap, sharing or copying bitmap pixels. If the bitmap |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 232 | is marked immutable, and its pixel memory is shareable, it may be shared |
| 233 | instead of copied. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 234 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 235 | Image is returned if bitmap is valid. Valid Bitmap parameters include: |
| 236 | dimensions are greater than zero; |
| 237 | each dimension fits in 29 bits; |
| 238 | Color_Type and Alpha_Type are valid, and Color_Type is not kUnknown_SkColorType; |
| 239 | row bytes are large enough to hold one row of pixels; |
| 240 | pixel address is not nullptr. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 241 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 242 | #Param bitmap Image_Info, row bytes, and pixels ## |
| 243 | |
| 244 | #Return created Image, or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 245 | |
| 246 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 247 | #Description |
| 248 | The first Bitmap is shared; writing to the pixel memory changes the first |
| 249 | Image. |
| 250 | The second Bitmap is marked immutable, and is copied; writing to the pixel |
| 251 | memory does not alter the second Image. |
| 252 | ## |
| 253 | #Height 50 |
| 254 | uint8_t storage[][5] = {{ 0xCA, 0xDA, 0xCA, 0xC9, 0xA3 }, |
| 255 | { 0xAC, 0xA8, 0x89, 0xA7, 0x87 }, |
| 256 | { 0x9B, 0xB5, 0xE5, 0x95, 0x46 }, |
| 257 | { 0x90, 0x81, 0xC5, 0x71, 0x33 }, |
| 258 | { 0x75, 0x55, 0x44, 0x40, 0x30 }}; |
| 259 | SkImageInfo imageInfo = SkImageInfo::Make(5, 5, kGray_8_SkColorType, kOpaque_SkAlphaType); |
| 260 | SkPixmap pixmap(imageInfo, storage[0], sizeof(storage) / 5); |
| 261 | SkBitmap bitmap; |
| 262 | bitmap.installPixels(pixmap); |
| 263 | sk_sp<SkImage> image1 = SkImage::MakeFromBitmap(bitmap); |
| 264 | bitmap.setImmutable(); |
| 265 | sk_sp<SkImage> image2 = SkImage::MakeFromBitmap(bitmap); |
| 266 | *pixmap.writable_addr8(2, 2) = 0x00; |
| 267 | canvas->scale(10, 10); |
| 268 | canvas->drawImage(image1, 0, 0); |
| 269 | canvas->drawImage(image2, 10, 0); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 270 | ## |
| 271 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 272 | #SeeAlso MakeFromRaster MakeRasterCopy MakeFromGenerator MakeRasterData |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 273 | |
| 274 | #Method ## |
| 275 | |
| 276 | # ------------------------------------------------------------------------------ |
| 277 | |
| 278 | #Method static sk_sp<SkImage> MakeFromGenerator(std::unique_ptr<SkImageGenerator> imageGenerator, |
| 279 | const SkIRect* subset = nullptr) |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 280 | #In Constructor |
| 281 | #Line # creates Image from a stream of data ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 282 | |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 283 | Creates Image from data returned by imageGenerator. Generated data is owned by Image and may not |
| 284 | be shared or accessed. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 285 | |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 286 | subset allows selecting a portion of the full image. Pass nullptr to select the entire image; |
| 287 | otherwise, subset must be contained by image bounds. |
| 288 | |
| 289 | Image is returned if generator data is valid. Valid data parameters vary by type of data |
| 290 | and platform. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 291 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 292 | imageGenerator may wrap Picture data, codec data, or custom data. |
| 293 | |
| 294 | #Param imageGenerator stock or custom routines to retrieve Image ## |
| 295 | #Param subset bounds of returned Image; may be nullptr ## |
| 296 | |
| 297 | #Return created Image, or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 298 | |
| 299 | #Example |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 300 | #Height 128 |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 301 | #Description |
| 302 | The generator returning Picture cannot be shared; std::move transfers ownership to generated Image. |
| 303 | ## |
| 304 | SkPictureRecorder recorder; |
| 305 | recorder.beginRecording(100, 100)->drawColor(SK_ColorRED); |
| 306 | auto picture = recorder.finishRecordingAsPicture(); |
| 307 | auto gen = SkImageGenerator::MakeFromPicture({100, 100}, picture, nullptr, nullptr, |
| 308 | SkImage::BitDepth::kU8, SkColorSpace::MakeSRGB()); |
| 309 | sk_sp<SkImage> image = SkImage::MakeFromGenerator(std::move(gen)); |
| 310 | canvas->drawImage(image, 0, 0); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 311 | ## |
| 312 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 313 | #SeeAlso MakeFromEncoded |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 314 | |
| 315 | #Method ## |
| 316 | |
| 317 | # ------------------------------------------------------------------------------ |
| 318 | |
| 319 | #Method static sk_sp<SkImage> MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset = nullptr) |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 320 | #In Constructor |
| 321 | #Line # creates Image from encoded data ## |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 322 | Creates Image from encoded data. |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 323 | subset allows selecting a portion of the full image. Pass nullptr to select the entire image; |
| 324 | otherwise, subset must be contained by image bounds. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 325 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 326 | Image is returned if format of the encoded data is recognized and supported. |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 327 | Recognized formats vary by platform. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 328 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 329 | #Param encoded data of Image to decode ## |
| 330 | #Param subset bounds of returned Image; may be nullptr ## |
| 331 | |
| 332 | #Return created Image, or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 333 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 334 | #Example |
| 335 | #Image 3 |
| 336 | int x = 0; |
| 337 | for (int quality : { 100, 50, 10, 1} ) { |
| 338 | sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, quality); |
| 339 | sk_sp<SkImage> image = SkImage::MakeFromEncoded(encodedData); |
| 340 | canvas->drawImage(image, x, 0); |
| 341 | x += 64; |
| 342 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 343 | ## |
| 344 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 345 | #SeeAlso MakeFromGenerator |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 346 | |
| 347 | #Method ## |
| 348 | |
| 349 | # ------------------------------------------------------------------------------ |
| 350 | |
| 351 | #Typedef void (*TextureReleaseProc)(ReleaseContext releaseContext) |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 352 | #Line # parameter type for MakeFromTexture ## |
Cary Clark | ffb3d68 | 2018-05-17 12:17:28 -0400 | [diff] [blame] | 353 | |
| 354 | #Code |
| 355 | typedef void (*TextureReleaseProc)(ReleaseContext releaseContext); |
| 356 | ## |
| 357 | |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 358 | User function called when supplied texture may be deleted. |
| 359 | #SeeAlso MakeFromTexture |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 360 | ## |
| 361 | |
| 362 | #Method static sk_sp<SkImage> MakeFromTexture(GrContext* context, |
| 363 | const GrBackendTexture& backendTexture, |
| 364 | GrSurfaceOrigin origin, |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 365 | SkColorType colorType, |
| 366 | SkAlphaType alphaType, |
| 367 | sk_sp<SkColorSpace> colorSpace) |
Cary Clark | f895a42 | 2018-02-27 09:54:21 -0500 | [diff] [blame] | 368 | #In Constructor |
| 369 | #Line # creates Image from GPU_Texture ## |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 370 | Creates Image from GPU_Texture associated with context. Caller is responsible for |
| 371 | managing the lifetime of GPU_Texture. |
| 372 | |
| 373 | Image is returned if format of backendTexture is recognized and supported. |
| 374 | Recognized formats vary by GPU back-end. |
| 375 | |
| 376 | #Param context GPU_Context ## |
| 377 | #Param backendTexture texture residing on GPU ## |
| 378 | #Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ## |
Cary Clark | 1a8d762 | 2018-03-05 13:26:16 -0500 | [diff] [blame] | 379 | #Param colorType one of: #list_of_color_types# |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 380 | ## |
Cary Clark | 681287e | 2018-03-16 11:34:15 -0400 | [diff] [blame] | 381 | #Param alphaType one of: #list_of_alpha_types# |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 382 | ## |
| 383 | #Param colorSpace range of colors; may be nullptr ## |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 384 | |
| 385 | #Return created Image, or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 386 | |
| 387 | #Example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 388 | #Image 3 |
| 389 | #Platform gpu |
| 390 | #Height 128 |
| 391 | #Description |
| 392 | A back-end texture has been created and uploaded to the GPU outside of this example. |
| 393 | ## |
| 394 | GrContext* context = canvas->getGrContext(); |
| 395 | if (!context) { |
| 396 | return; |
| 397 | } |
| 398 | canvas->scale(.25f, .25f); |
| 399 | int x = 0; |
| 400 | for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) { |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 401 | sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture, |
Cary Clark | ae957c4 | 2018-06-07 17:07:17 -0400 | [diff] [blame] | 402 | origin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr); |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 403 | canvas->drawImage(image, x, 0); |
| 404 | x += 512; |
| 405 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 406 | ## |
| 407 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 408 | #SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 409 | |
| 410 | #Method ## |
| 411 | |
| 412 | # ------------------------------------------------------------------------------ |
| 413 | |
| 414 | #Method static sk_sp<SkImage> MakeFromTexture(GrContext* context, |
| 415 | const GrBackendTexture& backendTexture, |
| 416 | GrSurfaceOrigin origin, |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 417 | SkColorType colorType, |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 418 | SkAlphaType alphaType, |
| 419 | sk_sp<SkColorSpace> colorSpace, |
| 420 | TextureReleaseProc textureReleaseProc, |
| 421 | ReleaseContext releaseContext) |
| 422 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 423 | Creates Image from GPU_Texture associated with context. GPU_Texture must stay |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 424 | valid and unchanged until textureReleaseProc is called. textureReleaseProc is |
| 425 | passed releaseContext when Image is deleted or no longer refers to texture. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 426 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 427 | Image is returned if format of backendTexture is recognized and supported. |
| 428 | Recognized formats vary by GPU back-end. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 429 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 430 | #Param context GPU_Context ## |
| 431 | #Param backendTexture texture residing on GPU ## |
| 432 | #Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ## |
Cary Clark | 1a8d762 | 2018-03-05 13:26:16 -0500 | [diff] [blame] | 433 | #Param colorType one of: #list_of_color_types# |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 434 | ## |
Cary Clark | 681287e | 2018-03-16 11:34:15 -0400 | [diff] [blame] | 435 | #Param alphaType one of: #list_of_alpha_types# |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 436 | ## |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 437 | #Param colorSpace range of colors; may be nullptr ## |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 438 | #Param textureReleaseProc function called when texture can be released ## |
| 439 | #Param releaseContext state passed to textureReleaseProc ## |
| 440 | |
| 441 | #Return created Image, or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 442 | |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 443 | #ToDo |
| 444 | This doesn't do anything clever with TextureReleaseProc because it may not get called |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 445 | fwithin the lifetime of the example |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 446 | ## |
| 447 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 448 | #Example |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 449 | #Platform gpu |
| 450 | #Image 4 |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 451 | GrContext* context = canvas->getGrContext(); |
| 452 | if (!context) { |
| 453 | return; |
| 454 | } |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 455 | auto debugster = [](SkImage::ReleaseContext releaseContext) -> void { |
| 456 | *((int *) releaseContext) += 128; |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 457 | }; |
| 458 | int x = 0; |
| 459 | for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) { |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 460 | sk_sp<SkImage> image = SkImage::MakeFromTexture(context, backEndTexture, |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 461 | origin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType, nullptr, debugster, &x); |
Cary Clark | 0c5f546 | 2017-12-15 11:21:51 -0500 | [diff] [blame] | 462 | canvas->drawImage(image, x, 0); |
| 463 | x += 128; |
| 464 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 465 | ## |
| 466 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 467 | #SeeAlso MakeFromAdoptedTexture SkSurface::MakeFromBackendTexture |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 468 | |
| 469 | #Method ## |
| 470 | |
| 471 | # ------------------------------------------------------------------------------ |
| 472 | |
| 473 | #Method static sk_sp<SkImage> MakeCrossContextFromEncoded(GrContext* context, sk_sp<SkData> data, |
| 474 | bool buildMips, |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 475 | SkColorSpace* dstColorSpace, |
| 476 | bool limitToMaxTextureSize = false) |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 477 | #In Constructor |
| 478 | #Line # creates Image from encoded data, and uploads to GPU ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 479 | |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 480 | Creates Image from encoded data. Image is uploaded to GPU back-end using context. |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 481 | |
| 482 | Created Image is available to other GPU contexts, and is available across thread |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 483 | boundaries. All contexts must be in the same GPU_Share_Group, or otherwise |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 484 | share resources. |
| 485 | |
| 486 | When Image is no longer referenced, context releases texture memory |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 487 | asynchronously. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 488 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 489 | Texture decoded from data is uploaded to match Surface created with |
| 490 | dstColorSpace. Color_Space of Image is determined by encoded data. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 491 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 492 | Image is returned if format of data is recognized and supported, and if context |
| 493 | supports moving resources. Recognized formats vary by platform and GPU back-end. |
| 494 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 495 | Image is returned using MakeFromEncoded if context is nullptr or does not support |
| 496 | moving resources between contexts. |
| 497 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 498 | #Param context GPU_Context ## |
| 499 | #Param data Image to decode ## |
| 500 | #Param buildMips create Image as Mip_Map if true ## |
| 501 | #Param dstColorSpace range of colors of matching Surface on GPU ## |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 502 | #Param limitToMaxTextureSize downscale image to GPU maximum texture size, if necessary ## |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 503 | |
| 504 | #Return created Image, or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 505 | |
| 506 | #Example |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 507 | #Image 4 |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 508 | #Height 64 |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 509 | GrContext* context = canvas->getGrContext(); |
| 510 | sk_sp<SkData> encodedData = image->encodeToData(SkEncodedImageFormat::kJPEG, 100); |
| 511 | sk_sp<SkImage> image = SkImage::MakeCrossContextFromEncoded(context, |
| 512 | encodedData, false, nullptr); |
| 513 | canvas->drawImage(image, 0, 0); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 514 | ## |
| 515 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 516 | #SeeAlso MakeCrossContextFromPixmap |
| 517 | |
| 518 | #Method ## |
| 519 | |
| 520 | # ------------------------------------------------------------------------------ |
| 521 | |
| 522 | #Method static sk_sp<SkImage> MakeCrossContextFromPixmap(GrContext* context, const SkPixmap& pixmap, |
| 523 | bool buildMips, |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 524 | SkColorSpace* dstColorSpace, |
| 525 | bool limitToMaxTextureSize = false) |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 526 | #In Constructor |
| 527 | #Line # creates Image from Pixmap, and uploads to GPU ## |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 528 | |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 529 | Creates Image from pixmap. Image is uploaded to GPU back-end using context. |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 530 | |
| 531 | Created Image is available to other GPU contexts, and is available across thread |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 532 | boundaries. All contexts must be in the same GPU_Share_Group, or otherwise |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 533 | share resources. |
| 534 | |
| 535 | When Image is no longer referenced, context releases texture memory |
| 536 | asynchronously. |
| 537 | |
| 538 | Texture created from pixmap is uploaded to match Surface created with |
| 539 | dstColorSpace. Color_Space of Image is determined by pixmap.colorSpace(). |
| 540 | |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 541 | Image is returned referring to GPU back-end if context is not nullptr, |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 542 | format of data is recognized and supported, and if context supports moving |
| 543 | resources between contexts. Otherwise, pixmap pixel data is copied and Image |
| 544 | as returned in raster format if possible; nullptr may be returned. |
| 545 | Recognized GPU formats vary by platform and GPU back-end. |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 546 | |
| 547 | #Param context GPU_Context ## |
| 548 | #Param pixmap Image_Info, pixel address, and row bytes ## |
| 549 | #Param buildMips create Image as Mip_Map if true ## |
| 550 | #Param dstColorSpace range of colors of matching Surface on GPU ## |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 551 | #Param limitToMaxTextureSize downscale image to GPU maximum texture size, if necessary ## |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 552 | |
| 553 | #Return created Image, or nullptr ## |
| 554 | |
| 555 | #Example |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 556 | #Image 4 |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 557 | #Height 64 |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 558 | GrContext* context = canvas->getGrContext(); |
| 559 | SkPixmap pixmap; |
| 560 | if (source.peekPixels(&pixmap)) { |
| 561 | sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(context, pixmap, |
| 562 | false, nullptr); |
| 563 | canvas->drawImage(image, 0, 0); |
| 564 | } |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 565 | ## |
| 566 | |
| 567 | #SeeAlso MakeCrossContextFromEncoded |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 568 | |
| 569 | #Method ## |
| 570 | |
| 571 | # ------------------------------------------------------------------------------ |
| 572 | |
| 573 | #Method static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext* context, |
| 574 | const GrBackendTexture& backendTexture, |
| 575 | GrSurfaceOrigin surfaceOrigin, |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 576 | SkColorType colorType, |
| 577 | SkAlphaType alphaType = kPremul_SkAlphaType, |
| 578 | sk_sp<SkColorSpace> colorSpace = nullptr) |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 579 | #In Constructor |
| 580 | #Line # creates Image from GPU_Texture, managed internally ## |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 581 | Creates Image from backendTexture associated with context. backendTexture and |
| 582 | returned Image are managed internally, and are released when no longer needed. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 583 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 584 | Image is returned if format of backendTexture is recognized and supported. |
| 585 | Recognized formats vary by GPU back-end. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 586 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 587 | #Param context GPU_Context ## |
| 588 | #Param backendTexture texture residing on GPU ## |
| 589 | #Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ## |
Cary Clark | 1a8d762 | 2018-03-05 13:26:16 -0500 | [diff] [blame] | 590 | #Param colorType one of: #list_of_color_types# |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 591 | ## |
Cary Clark | 681287e | 2018-03-16 11:34:15 -0400 | [diff] [blame] | 592 | #Param alphaType one of: #list_of_alpha_types# |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 593 | ## |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 594 | #Param colorSpace range of colors; may be nullptr ## |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 595 | |
| 596 | #Return created Image, or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 597 | |
| 598 | #Example |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 599 | #Image 5 |
| 600 | #Platform gpu |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 601 | if (!canvas->getGrContext()) { |
| 602 | return; |
| 603 | } |
| 604 | canvas->scale(.5f, .5f); |
| 605 | canvas->clear(0x7f3f5f7f); |
| 606 | int x = 0, y = 0; |
| 607 | for (auto origin : { kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin } ) { |
| 608 | for (auto alpha : { kOpaque_SkAlphaType, kPremul_SkAlphaType, kUnpremul_SkAlphaType } ) { |
| 609 | sk_sp<SkImage> image = SkImage::MakeFromAdoptedTexture(canvas->getGrContext(), |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 610 | backEndTexture, origin, |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 611 | kRGBA_8888_SkColorType, alpha); |
| 612 | canvas->drawImage(image, x, y); |
| 613 | x += 160; |
| 614 | } |
| 615 | x -= 160 * 3; |
| 616 | y += 256; |
| 617 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 618 | ## |
| 619 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 620 | #SeeAlso MakeFromTexture MakeFromYUVTexturesCopy |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 621 | |
| 622 | #Method ## |
| 623 | |
| 624 | # ------------------------------------------------------------------------------ |
| 625 | |
| 626 | #Method static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext* context, SkYUVColorSpace yuvColorSpace, |
Brian Salomon | 6a426c1 | 2018-03-15 12:16:02 -0400 | [diff] [blame] | 627 | const GrBackendTexture yuvTextures[3], |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 628 | GrSurfaceOrigin surfaceOrigin, |
| 629 | sk_sp<SkColorSpace> colorSpace = nullptr) |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 630 | #In Constructor |
| 631 | #Line # creates Image from YUV_ColorSpace data in three planes ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 632 | |
Brian Salomon | 6a426c1 | 2018-03-15 12:16:02 -0400 | [diff] [blame] | 633 | Creates Image from copy of yuvTextures, an array of textures on GPU. |
| 634 | yuvTextures contain pixels for YUV planes of Image. Returned Image has the dimensions |
| 635 | yuvTextures[0]. yuvColorSpace describes how YUV colors convert to RGB colors. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 636 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 637 | #Param context GPU_Context ## |
| 638 | #Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace, |
| 639 | kRec709_SkYUVColorSpace |
| 640 | ## |
Brian Salomon | 6a426c1 | 2018-03-15 12:16:02 -0400 | [diff] [blame] | 641 | #Param yuvTextures array of YUV textures on GPU ## |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 642 | #Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ## |
| 643 | #Param colorSpace range of colors; may be nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 644 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 645 | #Return created Image, or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 646 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 647 | # seems too complicated to create an example for this |
| 648 | #ToDo |
| 649 | should this be moved to chrome only? |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 650 | ## |
| 651 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 652 | #NoExample |
| 653 | ## |
| 654 | |
| 655 | #SeeAlso MakeFromNV12TexturesCopy |
| 656 | |
| 657 | #Method ## |
| 658 | |
| 659 | # ------------------------------------------------------------------------------ |
| 660 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 661 | #Method static sk_sp<SkImage> MakeFromNV12TexturesCopy(GrContext* context, |
| 662 | SkYUVColorSpace yuvColorSpace, |
Brian Salomon | 6a426c1 | 2018-03-15 12:16:02 -0400 | [diff] [blame] | 663 | const GrBackendTexture nv12Textures[2], |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 664 | GrSurfaceOrigin surfaceOrigin, |
| 665 | sk_sp<SkColorSpace> colorSpace = nullptr) |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 666 | #In Constructor |
Brian Salomon | 6a426c1 | 2018-03-15 12:16:02 -0400 | [diff] [blame] | 667 | #Line # creates Image from YUV_ColorSpace data in three planes ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 668 | |
Cary Clark | 681287e | 2018-03-16 11:34:15 -0400 | [diff] [blame] | 669 | Creates Image from copy of nv12Textures, an array of textures on GPU. |
Brian Salomon | 6a426c1 | 2018-03-15 12:16:02 -0400 | [diff] [blame] | 670 | nv12Textures[0] contains pixels for YUV_Component_Y plane. |
| 671 | nv12Textures[1] contains pixels for YUV_Component_U plane, |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 672 | followed by pixels for YUV_Component_V plane. |
Cary Clark | 681287e | 2018-03-16 11:34:15 -0400 | [diff] [blame] | 673 | Returned Image has the dimensions nv12Textures[2]. |
| 674 | yuvColorSpace describes how YUV colors convert to RGB colors. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 675 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 676 | #Param context GPU_Context ## |
| 677 | #Param yuvColorSpace one of: kJPEG_SkYUVColorSpace, kRec601_SkYUVColorSpace, |
| 678 | kRec709_SkYUVColorSpace |
| 679 | ## |
Brian Salomon | 6a426c1 | 2018-03-15 12:16:02 -0400 | [diff] [blame] | 680 | #Param nv12Textures array of YUV textures on GPU ## |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 681 | #Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ## |
| 682 | #Param colorSpace range of colors; may be nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 683 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 684 | #Return created Image, or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 685 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 686 | # seems too complicated to create an example for this |
| 687 | #ToDo |
| 688 | should this be moved to chrome only? |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 689 | ## |
| 690 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 691 | #NoExample |
| 692 | ## |
| 693 | |
| 694 | #SeeAlso MakeFromYUVTexturesCopy |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 695 | |
| 696 | #Method ## |
| 697 | |
| 698 | # ------------------------------------------------------------------------------ |
| 699 | |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 700 | # currently uncalled by any test or client ## |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 701 | #Bug 7424 |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 702 | |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 703 | #EnumClass BitDepth |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 704 | #Line # options for MakeFromPicture ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 705 | #Code |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 706 | enum class BitDepth { |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 707 | kU8, |
| 708 | kF16, |
| 709 | }; |
| 710 | ## |
| 711 | |
| 712 | #Const kU8 0 |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 713 | #Line # uses 8-bit unsigned int per Color component ## |
Cary Clark | ffb3d68 | 2018-05-17 12:17:28 -0400 | [diff] [blame] | 714 | Use 8 bits per ARGB component using unsigned integer format. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 715 | ## |
| 716 | #Const kF16 1 |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 717 | #Line # uses 16-bit float per Color component ## |
Cary Clark | ffb3d68 | 2018-05-17 12:17:28 -0400 | [diff] [blame] | 718 | Use 16 bits per ARGB component using half-precision floating point format. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 719 | ## |
| 720 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 721 | #NoExample |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 722 | ## |
| 723 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 724 | #SeeAlso MakeFromPicture |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 725 | |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 726 | #EnumClass ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 727 | |
| 728 | # ------------------------------------------------------------------------------ |
| 729 | |
| 730 | #Method static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture> picture, const SkISize& dimensions, |
| 731 | const SkMatrix* matrix, const SkPaint* paint, |
| 732 | BitDepth bitDepth, |
| 733 | sk_sp<SkColorSpace> colorSpace) |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 734 | #In Constructor |
| 735 | #Line # creates Image from Picture ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 736 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 737 | Creates Image from picture. Returned Image width and height are set by dimensions. |
| 738 | Image draws picture with matrix and paint, set to bitDepth and colorSpace. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 739 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 740 | If matrix is nullptr, draws with identity Matrix. If paint is nullptr, draws |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 741 | with default Paint. colorSpace may be nullptr. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 742 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 743 | #Param picture stream of drawing commands ## |
| 744 | #Param dimensions width and height ## |
| 745 | #Param matrix Matrix to rotate, scale, translate, and so on; may be nullptr ## |
| 746 | #Param paint Paint to apply transparency, filtering, and so on; may be nullptr ## |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 747 | #Param bitDepth 8-bit integer or 16-bit float: per component ## |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 748 | #Param colorSpace range of colors; may be nullptr ## |
| 749 | |
| 750 | #Return created Image, or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 751 | |
| 752 | #Example |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 753 | SkPaint paint; |
| 754 | SkPictureRecorder recorder; |
| 755 | SkCanvas* recordingCanvas = recorder.beginRecording(50, 50); |
| 756 | for (auto color : { SK_ColorRED, SK_ColorBLUE, 0xff007f00 } ) { |
| 757 | paint.setColor(color); |
| 758 | recordingCanvas->drawRect({10, 10, 30, 40}, paint); |
| 759 | recordingCanvas->translate(10, 10); |
| 760 | recordingCanvas->scale(1.2f, 1.4f); |
| 761 | } |
| 762 | sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture(); |
| 763 | int x = 0, y = 0; |
| 764 | for (auto alpha : { 70, 140, 210 } ) { |
| 765 | paint.setAlpha(alpha); |
| 766 | auto srgbColorSpace = SkColorSpace::MakeSRGB(); |
| 767 | sk_sp<SkImage> image = SkImage::MakeFromPicture(playback, {50, 50}, nullptr, &paint, |
| 768 | SkImage::BitDepth::kU8, srgbColorSpace); |
| 769 | canvas->drawImage(image, x, y); |
| 770 | x += 70; y += 70; |
| 771 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 772 | ## |
| 773 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 774 | #SeeAlso SkCanvas::drawPicture |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 775 | |
| 776 | #Method ## |
| 777 | |
| 778 | # ------------------------------------------------------------------------------ |
| 779 | |
| 780 | #Method static sk_sp<SkImage> MakeFromAHardwareBuffer(AHardwareBuffer* hardwareBuffer, |
| 781 | SkAlphaType alphaType = kPremul_SkAlphaType, |
| 782 | sk_sp<SkColorSpace> colorSpace = nullptr) |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 783 | #In Constructor |
| 784 | #Line # creates Image from Android hardware buffer ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 785 | |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 786 | #Bug 7447 |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 787 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 788 | Creates Image from Android hardware buffer. |
| 789 | Returned Image takes a reference on the buffer. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 790 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 791 | Only available on Android, when __ANDROID_API__ is defined to be 26 or greater. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 792 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 793 | #Param hardwareBuffer AHardwareBuffer Android hardware buffer ## |
Cary Clark | 681287e | 2018-03-16 11:34:15 -0400 | [diff] [blame] | 794 | #Param alphaType one of: #list_of_alpha_types# |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 795 | ## |
| 796 | #Param colorSpace range of colors; may be nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 797 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 798 | #Return created Image, or nullptr ## |
| 799 | |
| 800 | #NoExample |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 801 | ## |
| 802 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 803 | #SeeAlso MakeFromRaster |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 804 | |
| 805 | #Method ## |
| 806 | |
| 807 | # ------------------------------------------------------------------------------ |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 808 | #Subtopic Property |
| 809 | #Populate |
| 810 | #Line # values and attributes ## |
| 811 | ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 812 | |
| 813 | #Method int width() const |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 814 | #In Property |
| 815 | #Line # returns pixel column count ## |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 816 | Returns pixel count in each row. |
| 817 | |
| 818 | #Return pixel width in Image ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 819 | |
| 820 | #Example |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 821 | #Image 4 |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 822 | #Height 96 |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 823 | canvas->translate(10, 10); |
| 824 | canvas->drawImage(image, 0, 0); |
| 825 | canvas->translate(0, image->height()); |
| 826 | SkPaint paint; |
| 827 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 828 | canvas->drawLine(0, 10, image->width(), 10, paint); |
| 829 | canvas->drawString("width", image->width() / 2, 25, paint); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 830 | ## |
| 831 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 832 | #SeeAlso dimensions() height() |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 833 | |
| 834 | #Method ## |
| 835 | |
| 836 | # ------------------------------------------------------------------------------ |
| 837 | |
| 838 | #Method int height() const |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 839 | #In Property |
| 840 | #Line # returns pixel row count ## |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 841 | Returns pixel row count. |
| 842 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 843 | #Return pixel height in Image ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 844 | |
| 845 | #Example |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 846 | #Image 4 |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 847 | #Height 96 |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 848 | canvas->translate(10, 10); |
| 849 | canvas->drawImage(image, 0, 0); |
| 850 | canvas->translate(image->width(), 0); |
| 851 | SkPaint paint; |
| 852 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 853 | paint.setVerticalText(true); |
| 854 | canvas->drawLine(10, 0, 10, image->height(), paint); |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 855 | canvas->drawString("height", 25, image->height() / 2, paint); |
| 856 | ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 857 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 858 | #SeeAlso dimensions() width() |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 859 | |
| 860 | #Method ## |
| 861 | |
| 862 | # ------------------------------------------------------------------------------ |
| 863 | |
| 864 | #Method SkISize dimensions() const |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 865 | #In Property |
| 866 | #Line # returns width() and height() ## |
Cary Clark | 681287e | 2018-03-16 11:34:15 -0400 | [diff] [blame] | 867 | |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 868 | Returns ISize { width(), height() }. |
| 869 | |
| 870 | #Return integral size of width() and height() ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 871 | |
| 872 | #Example |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 873 | #Image 4 |
| 874 | SkISize dimensions = image->dimensions(); |
| 875 | SkIRect bounds = image->bounds(); |
| 876 | SkIRect dimensionsAsBounds = SkIRect::MakeSize(dimensions); |
| 877 | SkDebugf("dimensionsAsBounds %c= bounds\n", dimensionsAsBounds == bounds ? '=' : '!'); |
Cary Clark | 681287e | 2018-03-16 11:34:15 -0400 | [diff] [blame] | 878 | #StdOut |
| 879 | dimensionsAsBounds == bounds |
| 880 | ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 881 | ## |
| 882 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 883 | #SeeAlso height() width() bounds() |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 884 | |
| 885 | #Method ## |
| 886 | |
| 887 | # ------------------------------------------------------------------------------ |
| 888 | |
| 889 | #Method SkIRect bounds() const |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 890 | #In Property |
| 891 | #Line # returns width() and height() as Rectangle ## |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 892 | Returns IRect { 0, 0, width(), height() }. |
| 893 | |
| 894 | #Return integral rectangle from origin to width() and height() ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 895 | |
| 896 | #Example |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 897 | #Height 128 |
| 898 | #Image 4 |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 899 | SkIRect bounds = image->bounds(); |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 900 | for (int x : { 0, bounds.width() } ) { |
| 901 | for (int y : { 0, bounds.height() } ) { |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 902 | canvas->drawImage(image, x, y); |
| 903 | } |
| 904 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 905 | ## |
| 906 | |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 907 | #SeeAlso dimensions() |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 908 | |
| 909 | #Method ## |
| 910 | |
| 911 | # ------------------------------------------------------------------------------ |
| 912 | |
| 913 | #Method uint32_t uniqueID() const |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 914 | #In Property |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 915 | #Line # returns identifier for Image ## |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 916 | Returns value unique to image. Image contents cannot change after Image is |
| 917 | created. Any operation to create a new Image will receive generate a new |
| 918 | unique number. |
| 919 | |
| 920 | #Return unique identifier ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 921 | |
| 922 | #Example |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 923 | #Image 5 |
| 924 | #Height 156 |
| 925 | sk_sp<SkImage> subset = image->makeSubset({10, 20, 90, 100}); |
| 926 | canvas->drawImage(image, 0, 0); |
| 927 | canvas->drawImage(subset, 128, 0); |
| 928 | SkPaint paint; |
| 929 | SkString s; |
| 930 | s.printf("original id: %d", image->uniqueID()); |
| 931 | canvas->drawString(s, 20, image->height() + 20, paint); |
| 932 | s.printf("subset id: %d", subset->uniqueID()); |
| 933 | canvas->drawString(s, 148, subset->height() + 20, paint); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 934 | ## |
| 935 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 936 | #SeeAlso isLazyGenerated |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 937 | |
| 938 | #Method ## |
| 939 | |
| 940 | # ------------------------------------------------------------------------------ |
| 941 | |
| 942 | #Method SkAlphaType alphaType() const |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 943 | #In Property |
| 944 | #Line # returns Alpha_Type ## |
Cary Clark | 681287e | 2018-03-16 11:34:15 -0400 | [diff] [blame] | 945 | Returns Alpha_Type, one of: #list_of_alpha_types#. |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 946 | |
| 947 | Alpha_Type returned was a parameter to an Image constructor, |
| 948 | or was parsed from encoded data. |
| 949 | |
| 950 | #Return Alpha_Type in Image ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 951 | |
| 952 | #Example |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 953 | #Image 4 |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 954 | #Height 96 |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 955 | const char* alphaTypeStr[] = { "Unknown", "Opaque", "Premul", "Unpremul" }; |
| 956 | SkAlphaType alphaType = image->alphaType(); |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 957 | canvas->drawImage(image, 16, 0); |
Cary Clark | ffb3d68 | 2018-05-17 12:17:28 -0400 | [diff] [blame] | 958 | canvas->drawString(alphaTypeStr[(int) alphaType], 20, image->height() + 20, SkPaint()); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 959 | ## |
| 960 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 961 | #SeeAlso SkImageInfo::alphaType |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 962 | |
| 963 | #Method ## |
| 964 | |
| 965 | # ------------------------------------------------------------------------------ |
| 966 | |
Greg Daniel | 56008aa | 2018-03-14 15:33:42 -0400 | [diff] [blame] | 967 | #Method SkColorType colorType() const |
| 968 | #In Property |
| 969 | #Line # returns Color_Type ## |
| 970 | |
| 971 | Returns Color_Type if known; otherwise, returns kUnknown_SkColorType. |
| 972 | |
| 973 | #Return Color_Type of Image ## |
| 974 | |
| 975 | #Example |
Cary Clark | ffb3d68 | 2018-05-17 12:17:28 -0400 | [diff] [blame] | 976 | #Image 4 |
| 977 | #Height 96 |
| 978 | const char* colors[] = { "Unknown", "Alpha_8", "RGB_565", "ARGB_4444", "RGBA_8888", "RGB_888x", |
| 979 | "BGRA_8888", "RGBA_1010102", "RGB_101010x", "Gray_8", "RGBA_F16" }; |
| 980 | SkColorType colorType = image->colorType(); |
| 981 | canvas->drawImage(image, 16, 0); |
| 982 | canvas->drawString(colors[(int) colorType], 20, image->height() + 20, SkPaint()); |
Greg Daniel | 56008aa | 2018-03-14 15:33:42 -0400 | [diff] [blame] | 983 | ## |
| 984 | |
| 985 | #SeeAlso SkImageInfo::colorType |
| 986 | |
| 987 | #Method ## |
| 988 | |
| 989 | # ------------------------------------------------------------------------------ |
| 990 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 991 | #Method SkColorSpace* colorSpace() const |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 992 | #In Property |
| 993 | #Line # returns Color_Space ## |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 994 | Returns Color_Space, the range of colors, associated with Image. The |
| 995 | reference count of Color_Space is unchanged. The returned Color_Space is |
| 996 | immutable. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 997 | |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 998 | Color_Space returned was passed to an Image constructor, |
| 999 | or was parsed from encoded data. Color_Space returned may be ignored when Image |
| 1000 | is drawn, depending on the capabilities of the Surface receiving the drawing. |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 1001 | |
| 1002 | #Return Color_Space in Image, or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1003 | |
| 1004 | #Example |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 1005 | #Image 3 |
| 1006 | #Set sRGB |
| 1007 | SkPixmap pixmap; |
| 1008 | source.peekPixels(&pixmap); |
| 1009 | canvas->scale(.25f, .25f); |
| 1010 | int y = 0; |
| 1011 | for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma, |
| 1012 | SkColorSpace::kSRGB_RenderTargetGamma } ) { |
| 1013 | int x = 0; |
| 1014 | sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut); |
| 1015 | for (int index = 0; index < 2; ++index) { |
| 1016 | pixmap.setColorSpace(colorSpace); |
| 1017 | sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap); |
| 1018 | canvas->drawImage(image, x, y); |
| 1019 | colorSpace = image->colorSpace()->makeColorSpin(); |
| 1020 | x += 512; |
| 1021 | } |
| 1022 | y += 512; |
| 1023 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1024 | ## |
| 1025 | |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 1026 | #SeeAlso refColorSpace makeColorSpace |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1027 | |
| 1028 | #Method ## |
| 1029 | |
| 1030 | # ------------------------------------------------------------------------------ |
| 1031 | |
| 1032 | #Method sk_sp<SkColorSpace> refColorSpace() const |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1033 | #In Property |
| 1034 | #Line # returns Image_Info Color_Space ## |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 1035 | Returns a smart pointer to Color_Space, the range of colors, associated with |
| 1036 | Image. The smart pointer tracks the number of objects sharing this |
| 1037 | SkColorSpace reference so the memory is released when the owners destruct. |
| 1038 | |
| 1039 | The returned SkColorSpace is immutable. |
| 1040 | |
| 1041 | Color_Space returned was passed to an Image constructor, |
| 1042 | or was parsed from encoded data. Color_Space returned may be ignored when Image |
| 1043 | is drawn, depending on the capabilities of the Surface receiving the drawing. |
| 1044 | |
| 1045 | #Return Color_Space in Image, or nullptr, wrapped in a smart pointer ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1046 | |
| 1047 | #Example |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 1048 | #Image 3 |
| 1049 | #Set sRGB |
| 1050 | SkPixmap pixmap; |
| 1051 | source.peekPixels(&pixmap); |
| 1052 | canvas->scale(.25f, .25f); |
| 1053 | int y = 0; |
| 1054 | for (auto gamma : { SkColorSpace::kLinear_RenderTargetGamma, |
| 1055 | SkColorSpace::kSRGB_RenderTargetGamma } ) { |
| 1056 | int x = 0; |
| 1057 | sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeRGB(gamma, SkColorSpace::kSRGB_Gamut); |
| 1058 | for (int index = 0; index < 2; ++index) { |
| 1059 | pixmap.setColorSpace(colorSpace); |
| 1060 | sk_sp<SkImage> image = SkImage::MakeRasterCopy(pixmap); |
| 1061 | canvas->drawImage(image, x, y); |
| 1062 | colorSpace = image->refColorSpace()->makeColorSpin(); |
| 1063 | x += 512; |
| 1064 | } |
| 1065 | y += 512; |
| 1066 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1067 | ## |
| 1068 | |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 1069 | #SeeAlso colorSpace makeColorSpace |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1070 | |
| 1071 | #Method ## |
| 1072 | |
| 1073 | # ------------------------------------------------------------------------------ |
| 1074 | |
| 1075 | #Method bool isAlphaOnly() const |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1076 | #In Property |
| 1077 | #Line # returns if pixels represent a transparency mask ## |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 1078 | Returns true if Image pixels represent transparency only. If true, each pixel |
| 1079 | is packed in 8 bits as defined by kAlpha_8_SkColorType. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1080 | |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 1081 | #Return true if pixels represent a transparency mask ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1082 | |
| 1083 | #Example |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 1084 | uint8_t pmColors = 0; |
| 1085 | sk_sp<SkImage> image = SkImage::MakeRasterCopy({SkImageInfo::MakeA8(1, 1), &pmColors, 1}); |
| 1086 | SkDebugf("alphaOnly = %s\n", image->isAlphaOnly() ? "true" : "false"); |
| 1087 | #StdOut |
| 1088 | alphaOnly = true |
| 1089 | ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1090 | ## |
| 1091 | |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 1092 | #SeeAlso alphaType isOpaque |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1093 | |
| 1094 | #Method ## |
| 1095 | |
| 1096 | # ------------------------------------------------------------------------------ |
| 1097 | |
| 1098 | #Method bool isOpaque() const |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1099 | #In Property |
| 1100 | #Line # returns if Alpha_Type is kOpaque_SkAlphaType ## |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 1101 | Returns true if pixels ignore their Alpha value and are treated as fully opaque. |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 1102 | |
| 1103 | #Return true if Alpha_Type is kOpaque_SkAlphaType ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1104 | |
| 1105 | #Example |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 1106 | auto check_isopaque = [](const SkImageInfo& imageInfo) -> void { |
| 1107 | auto surface(SkSurface::MakeRaster(imageInfo)); |
| 1108 | auto image(surface->makeImageSnapshot()); |
| 1109 | SkDebugf("isOpaque = %s\n", image->isOpaque() ? "true" : "false"); |
| 1110 | }; |
| 1111 | |
| 1112 | check_isopaque(SkImageInfo::MakeN32Premul(5, 5)); |
| 1113 | check_isopaque(SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType)); |
| 1114 | #StdOut |
| 1115 | isOpaque = false |
| 1116 | isOpaque = true |
| 1117 | ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1118 | ## |
| 1119 | |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 1120 | #SeeAlso alphaType isAlphaOnly |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1121 | |
| 1122 | #Method ## |
| 1123 | |
| 1124 | # ------------------------------------------------------------------------------ |
| 1125 | |
| 1126 | #Method sk_sp<SkShader> makeShader(SkShader::TileMode tileMode1, SkShader::TileMode tileMode2, |
| 1127 | const SkMatrix* localMatrix = nullptr) const |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1128 | #In Constructor |
| 1129 | #Line # creates Shader, Paint element that can tile Image ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1130 | |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 1131 | Creates Shader from Image. Shader dimensions are taken from Image. Shader uses |
| 1132 | SkShader::TileMode rules to fill drawn area outside Image. localMatrix permits |
| 1133 | transforming Image before Canvas_Matrix is applied. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1134 | |
Cary Clark | 5538c13 | 2018-06-14 12:28:14 -0400 | [diff] [blame] | 1135 | #Param tileMode1 tiling on x-axis, one of: SkShader::kClamp_TileMode, |
| 1136 | SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 1137 | ## |
Cary Clark | 5538c13 | 2018-06-14 12:28:14 -0400 | [diff] [blame] | 1138 | #Param tileMode2 tiling on y-axis, one of: SkShader::kClamp_TileMode, |
| 1139 | SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 1140 | ## |
| 1141 | #Param localMatrix Image transformation, or nullptr ## |
| 1142 | |
| 1143 | #Return Shader containing Image ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1144 | |
| 1145 | #Example |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 1146 | #Image 4 |
| 1147 | SkMatrix matrix; |
| 1148 | matrix.setRotate(45); |
| 1149 | SkPaint paint; |
| 1150 | paint.setShader(image->makeShader(SkShader::kRepeat_TileMode, SkShader::kMirror_TileMode, |
| 1151 | &matrix)); |
| 1152 | canvas->drawPaint(paint); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1153 | ## |
| 1154 | |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 1155 | #SeeAlso scalePixels |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1156 | |
| 1157 | #Method ## |
| 1158 | |
| 1159 | # ------------------------------------------------------------------------------ |
| 1160 | |
| 1161 | #Method sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const |
| 1162 | |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 1163 | Creates Shader from Image. Shader dimensions are taken from Image. Shader uses |
| 1164 | SkShader::kClamp_TileMode to fill drawn area outside Image. localMatrix permits |
| 1165 | transforming Image before Canvas_Matrix is applied. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1166 | |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 1167 | #Param localMatrix Image transformation, or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1168 | |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 1169 | #Return Shader containing Image ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1170 | |
| 1171 | #Example |
Cary Clark | 61dfc3a | 2018-01-03 08:37:53 -0500 | [diff] [blame] | 1172 | #Image 5 |
| 1173 | SkMatrix matrix; |
| 1174 | matrix.setRotate(45); |
| 1175 | matrix.postTranslate(125, 30); |
| 1176 | SkPaint paint; |
| 1177 | paint.setShader(image->makeShader(&matrix)); |
| 1178 | canvas->drawPaint(paint); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1179 | ## |
| 1180 | |
Cary Clark | f5404bb | 2018-01-05 12:10:09 -0500 | [diff] [blame] | 1181 | #SeeAlso scalePixels |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1182 | |
| 1183 | #Method ## |
| 1184 | |
| 1185 | # ------------------------------------------------------------------------------ |
Cary Clark | 78de751 | 2018-02-07 07:27:09 -0500 | [diff] [blame] | 1186 | #Subtopic Pixels |
| 1187 | #Populate |
| 1188 | #Line # read and write pixel values ## |
| 1189 | ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1190 | |
| 1191 | #Method bool peekPixels(SkPixmap* pixmap) const |
Cary Clark | 78de751 | 2018-02-07 07:27:09 -0500 | [diff] [blame] | 1192 | #In Pixels |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1193 | #Line # returns Pixmap if possible ## |
Cary Clark | f5404bb | 2018-01-05 12:10:09 -0500 | [diff] [blame] | 1194 | Copies Image pixel address, row bytes, and Image_Info to pixmap, if address |
| 1195 | is available, and returns true. If pixel address is not available, return |
| 1196 | false and leave pixmap unchanged. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1197 | |
Cary Clark | f5404bb | 2018-01-05 12:10:09 -0500 | [diff] [blame] | 1198 | #Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1199 | |
Cary Clark | f5404bb | 2018-01-05 12:10:09 -0500 | [diff] [blame] | 1200 | #Return true if Image has direct access to pixels ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1201 | |
| 1202 | #Example |
Cary Clark | f5404bb | 2018-01-05 12:10:09 -0500 | [diff] [blame] | 1203 | SkBitmap bitmap; |
| 1204 | bitmap.allocPixels(SkImageInfo::MakeN32Premul(12, 11)); |
| 1205 | SkCanvas offscreen(bitmap); |
| 1206 | offscreen.clear(SK_ColorWHITE); |
| 1207 | SkPaint paint; |
| 1208 | offscreen.drawString("%", 1, 10, paint); |
| 1209 | sk_sp<SkImage> image = SkImage::MakeFromBitmap(bitmap); |
| 1210 | SkPixmap pixmap; |
| 1211 | if (image->peekPixels(&pixmap)) { |
| 1212 | const SkPMColor* pixels = pixmap.addr32(); |
| 1213 | SkPMColor pmWhite = pixels[0]; |
| 1214 | for (int y = 0; y < image->height(); ++y) { |
| 1215 | for (int x = 0; x < image->width(); ++x) { |
| 1216 | SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x'); |
| 1217 | } |
| 1218 | SkDebugf("\n"); |
| 1219 | } |
| 1220 | } |
| 1221 | #StdOut |
| 1222 | ------------ |
| 1223 | --xx----x--- |
| 1224 | -x--x--x---- |
| 1225 | -x--x--x---- |
| 1226 | -x--x-x----- |
| 1227 | --xx-xx-xx-- |
| 1228 | -----x-x--x- |
| 1229 | ----x--x--x- |
| 1230 | ----x--x--x- |
| 1231 | ---x----xx-- |
| 1232 | ------------ |
| 1233 | ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1234 | ## |
| 1235 | |
Cary Clark | f5404bb | 2018-01-05 12:10:09 -0500 | [diff] [blame] | 1236 | #SeeAlso readPixels |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1237 | |
| 1238 | #Method ## |
| 1239 | |
| 1240 | # ------------------------------------------------------------------------------ |
| 1241 | |
| 1242 | #Method GrTexture* getTexture() const |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 1243 | #Deprecated |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1244 | #Method ## |
| 1245 | |
| 1246 | # ------------------------------------------------------------------------------ |
| 1247 | |
| 1248 | #Method bool isTextureBacked() const |
Cary Clark | 78de751 | 2018-02-07 07:27:09 -0500 | [diff] [blame] | 1249 | #In Property |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1250 | #Line # returns if Image was created from GPU_Texture ## |
Cary Clark | f5404bb | 2018-01-05 12:10:09 -0500 | [diff] [blame] | 1251 | Returns true the contents of Image was created on or uploaded to GPU memory, |
| 1252 | and is available as a GPU_Texture. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1253 | |
Cary Clark | f5404bb | 2018-01-05 12:10:09 -0500 | [diff] [blame] | 1254 | #Return true if Image is a GPU_Texture ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1255 | |
| 1256 | #Example |
Cary Clark | f5404bb | 2018-01-05 12:10:09 -0500 | [diff] [blame] | 1257 | #Image 5 |
| 1258 | #Platform gpu |
| 1259 | auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void { |
| 1260 | if (nullptr == image) { |
| 1261 | return; |
| 1262 | } |
| 1263 | SkPaint paint; |
| 1264 | paint.setAntiAlias(true); |
| 1265 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 1266 | canvas->drawImage(image, 0, 0); |
| 1267 | canvas->drawString(label, image->width() / 2, image->height() / 4, paint); |
| 1268 | canvas->drawString(image->isTextureBacked() ? "is GPU texture" : "not GPU texture", |
| 1269 | image->width() / 2, image->height() * 3 / 4, paint); |
| 1270 | }; |
| 1271 | sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source)); |
| 1272 | sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture, |
Cary Clark | ae957c4 | 2018-06-07 17:07:17 -0400 | [diff] [blame] | 1273 | kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, |
| 1274 | kOpaque_SkAlphaType, nullptr)); |
Cary Clark | f5404bb | 2018-01-05 12:10:09 -0500 | [diff] [blame] | 1275 | drawImage(image, "image"); |
| 1276 | canvas->translate(image->width(), 0); |
| 1277 | drawImage(bitmapImage, "source"); |
| 1278 | canvas->translate(-image->width(), image->height()); |
| 1279 | drawImage(textureImage, "backEndTexture"); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1280 | ## |
| 1281 | |
Cary Clark | f5404bb | 2018-01-05 12:10:09 -0500 | [diff] [blame] | 1282 | #SeeAlso MakeFromTexture isValid |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1283 | |
| 1284 | #Method ## |
| 1285 | |
| 1286 | # ------------------------------------------------------------------------------ |
| 1287 | |
| 1288 | #Method bool isValid(GrContext* context) const |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1289 | #In Property |
| 1290 | #Line # returns if Image can draw to Raster_Surface or GPU_Context ## |
Cary Clark | f5404bb | 2018-01-05 12:10:09 -0500 | [diff] [blame] | 1291 | Returns true if Image can be drawn on either Raster_Surface or GPU_Surface. |
| 1292 | If context is nullptr, tests if Image draws on Raster_Surface; |
| 1293 | otherwise, tests if Image draws on GPU_Surface associated with context. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1294 | |
Cary Clark | f5404bb | 2018-01-05 12:10:09 -0500 | [diff] [blame] | 1295 | Image backed by GPU_Texture may become invalid if associated GrContext is |
| 1296 | invalid. Lazy_Image may be invalid and may not draw to Raster_Surface or |
| 1297 | GPU_Surface or both. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1298 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 1299 | #Param context GPU_Context ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1300 | |
Cary Clark | f5404bb | 2018-01-05 12:10:09 -0500 | [diff] [blame] | 1301 | #Return true if Image can be drawn ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1302 | |
| 1303 | #Example |
Cary Clark | f5404bb | 2018-01-05 12:10:09 -0500 | [diff] [blame] | 1304 | #Image 5 |
| 1305 | #Platform gpu |
| 1306 | auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void { |
| 1307 | if (nullptr == image) { |
| 1308 | return; |
| 1309 | } |
| 1310 | SkPaint paint; |
| 1311 | paint.setAntiAlias(true); |
| 1312 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 1313 | canvas->drawImage(image, 0, 0); |
| 1314 | canvas->drawString(label, image->width() / 2, image->height() / 4, paint); |
| 1315 | if (canvas->getGrContext()) { |
| 1316 | canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" : |
| 1317 | "not valid on GPU", image->width() / 2, image->height() * 5 / 8, paint); |
| 1318 | } |
| 1319 | canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" : |
| 1320 | "not valid on CPU", image->width() / 2, image->height() * 7 / 8, paint); |
| 1321 | }; |
| 1322 | sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source)); |
| 1323 | sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture, |
Cary Clark | ae957c4 | 2018-06-07 17:07:17 -0400 | [diff] [blame] | 1324 | kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, |
| 1325 | kOpaque_SkAlphaType, nullptr)); |
Cary Clark | f5404bb | 2018-01-05 12:10:09 -0500 | [diff] [blame] | 1326 | drawImage(image, "image"); |
| 1327 | canvas->translate(image->width(), 0); |
| 1328 | drawImage(bitmapImage, "source"); |
| 1329 | canvas->translate(-image->width(), image->height()); |
| 1330 | drawImage(textureImage, "backEndTexture"); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1331 | ## |
| 1332 | |
Cary Clark | f5404bb | 2018-01-05 12:10:09 -0500 | [diff] [blame] | 1333 | #SeeAlso isTextureBacked isLazyGenerated |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1334 | |
| 1335 | #Method ## |
| 1336 | |
| 1337 | # ------------------------------------------------------------------------------ |
| 1338 | |
Robert Phillips | c550995 | 2018-04-04 15:54:55 -0400 | [diff] [blame] | 1339 | #Method GrBackendTexture getBackendTexture(bool flushPendingGrContextIO, |
| 1340 | GrSurfaceOrigin* origin = nullptr) const |
| 1341 | #In Property |
| 1342 | #Line # returns GPU reference to Image as texture ## |
| 1343 | |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 1344 | Retrieves the back-end texture. If Image has no back-end texture, an invalid |
Cary Clark | ba75aee | 2018-04-05 08:18:41 -0400 | [diff] [blame] | 1345 | object is returned. Call GrBackendTexture::isValid to determine if the result |
| 1346 | is valid. |
| 1347 | |
| 1348 | If flushPendingGrContextIO is true, completes deferred I/O operations. |
Robert Phillips | c550995 | 2018-04-04 15:54:55 -0400 | [diff] [blame] | 1349 | |
| 1350 | If origin in not nullptr, copies location of content drawn into Image. |
| 1351 | |
| 1352 | #Param flushPendingGrContextIO flag to flush outstanding requests ## |
| 1353 | #Param origin storage for one of: kTopLeft_GrSurfaceOrigin, |
| 1354 | kBottomLeft_GrSurfaceOrigin; or nullptr |
| 1355 | ## |
| 1356 | |
Cary Clark | ba75aee | 2018-04-05 08:18:41 -0400 | [diff] [blame] | 1357 | #Return back-end API texture handle; invalid on failure ## |
Robert Phillips | c550995 | 2018-04-04 15:54:55 -0400 | [diff] [blame] | 1358 | |
Cary Clark | ba75aee | 2018-04-05 08:18:41 -0400 | [diff] [blame] | 1359 | #Example |
| 1360 | #Image 3 |
| 1361 | #Platform gpu |
Brian Osman | 584b501 | 2018-04-13 15:48:26 -0400 | [diff] [blame] | 1362 | GrContext* grContext = canvas->getGrContext(); |
| 1363 | if (!grContext) { |
| 1364 | canvas->drawString("GPU only!", 20, 40, SkPaint()); |
| 1365 | return; |
| 1366 | } |
| 1367 | sk_sp<SkImage> imageFromBackend = SkImage::MakeFromAdoptedTexture(grContext, backEndTexture, |
| 1368 | kBottomLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType); |
| 1369 | GrBackendTexture textureFromImage = imageFromBackend->getBackendTexture(false); |
| 1370 | if (!textureFromImage.isValid()) { |
| 1371 | return; |
| 1372 | } |
| 1373 | sk_sp<SkImage> imageFromTexture = SkImage::MakeFromAdoptedTexture(grContext, textureFromImage, |
| 1374 | kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType); |
| 1375 | canvas->drawImage(imageFromTexture, 0, 0); |
Cary Clark | ba75aee | 2018-04-05 08:18:41 -0400 | [diff] [blame] | 1376 | canvas->drawImage(imageFromBackend, 128, 128); |
Robert Phillips | c550995 | 2018-04-04 15:54:55 -0400 | [diff] [blame] | 1377 | ## |
| 1378 | |
| 1379 | #SeeAlso MakeFromTexture isTextureBacked |
| 1380 | |
| 1381 | #Method ## |
| 1382 | |
| 1383 | # ------------------------------------------------------------------------------ |
| 1384 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1385 | #Enum CachingHint |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 1386 | #Line # options for readPixels and scalePixels ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1387 | #Code |
| 1388 | enum CachingHint { |
| 1389 | kAllow_CachingHint, |
| 1390 | kDisallow_CachingHint, |
| 1391 | }; |
| 1392 | ## |
| 1393 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1394 | CachingHint selects whether Skia may internally cache Bitmaps generated by |
| 1395 | decoding Image, or by copying Image from GPU to CPU. The default behavior |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 1396 | allows caching Bitmaps. |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1397 | |
| 1398 | Choose kDisallow_CachingHint if Image pixels are to be used only once, or |
| 1399 | if Image pixels reside in a cache outside of Skia, or to reduce memory pressure. |
| 1400 | |
| 1401 | Choosing kAllow_CachingHint does not ensure that pixels will be cached. |
| 1402 | Image pixels may not be cached if memory requirements are too large or |
| 1403 | pixels are not accessible. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1404 | |
| 1405 | #Const kAllow_CachingHint 0 |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 1406 | #Line # allows internally caching decoded and copied pixels ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1407 | ## |
| 1408 | #Const kDisallow_CachingHint 1 |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 1409 | #Line # disallows internally caching decoded and copied pixels ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1410 | ## |
| 1411 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1412 | #NoExample |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1413 | ## |
| 1414 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1415 | #SeeAlso readPixels scalePixels |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1416 | |
| 1417 | #Enum ## |
| 1418 | |
| 1419 | # ------------------------------------------------------------------------------ |
| 1420 | |
| 1421 | #Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, |
| 1422 | int srcX, int srcY, CachingHint cachingHint = kAllow_CachingHint) const |
Cary Clark | 78de751 | 2018-02-07 07:27:09 -0500 | [diff] [blame] | 1423 | #In Pixels |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1424 | #Line # copies and converts pixels ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1425 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1426 | Copies Rect of pixels from Image to dstPixels. Copy starts at offset (srcX, srcY), |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 1427 | and does not exceed Image (width(), height()). |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1428 | |
| 1429 | dstInfo specifies width, height, Color_Type, Alpha_Type, and Color_Space of |
| 1430 | destination. dstRowBytes specifics the gap from one destination row to the next. |
| 1431 | Returns true if pixels are copied. Returns false if: |
| 1432 | #List |
| 1433 | # dstInfo.addr() equals nullptr ## |
| 1434 | # dstRowBytes is less than dstInfo.minRowBytes ## |
| 1435 | # Pixel_Ref is nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1436 | ## |
| 1437 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1438 | Pixels are copied only if pixel conversion is possible. If Image Color_Type is |
| 1439 | kGray_8_SkColorType, or kAlpha_8_SkColorType; dstInfo.colorType must match. |
| 1440 | If Image Color_Type is kGray_8_SkColorType, dstInfo.colorSpace must match. |
| 1441 | If Image Alpha_Type is kOpaque_SkAlphaType, dstInfo.alphaType must |
| 1442 | match. If Image Color_Space is nullptr, dstInfo.colorSpace must match. Returns |
| 1443 | false if pixel conversion is not possible. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1444 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1445 | srcX and srcY may be negative to copy only top or left of source. Returns |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 1446 | false if width() or height() is zero or negative. |
| 1447 | Returns false if |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1448 | #Formula |
| 1449 | abs(srcX) >= Image width() |
| 1450 | ## |
| 1451 | , or if |
| 1452 | #Formula |
| 1453 | abs(srcY) >= Image height() |
| 1454 | ## |
| 1455 | . |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1456 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1457 | If cachingHint is kAllow_CachingHint, pixels may be retained locally. |
| 1458 | If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache. |
| 1459 | |
| 1460 | #Param dstInfo destination width, height, Color_Type, Alpha_Type, Color_Space ## |
| 1461 | #Param dstPixels destination pixel storage ## |
| 1462 | #Param dstRowBytes destination row length ## |
| 1463 | #Param srcX column index whose absolute value is less than width() ## |
| 1464 | #Param srcY row index whose absolute value is less than height() ## |
| 1465 | #Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ## |
| 1466 | |
| 1467 | #Return true if pixels are copied to dstPixels ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1468 | |
| 1469 | #Example |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1470 | #Image 3 |
| 1471 | canvas->scale(.5f, .5f); |
| 1472 | const int width = 32; |
| 1473 | const int height = 32; |
| 1474 | std::vector<int32_t> dstPixels; |
| 1475 | dstPixels.resize(height * width * 4); |
| 1476 | SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
| 1477 | for (int y = 0; y < 512; y += height ) { |
| 1478 | for (int x = 0; x < 512; x += width ) { |
| 1479 | if (image->readPixels(info, &dstPixels.front(), width * 4, x, y)) { |
| 1480 | SkPixmap dstPixmap(info, &dstPixels.front(), width * 4); |
| 1481 | SkBitmap bitmap; |
| 1482 | bitmap.installPixels(dstPixmap); |
| 1483 | canvas->drawBitmap(bitmap, 0, 0); |
| 1484 | } |
| 1485 | canvas->translate(48, 0); |
| 1486 | } |
| 1487 | canvas->translate(-16 * 48, 48); |
| 1488 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1489 | ## |
| 1490 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1491 | #SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1492 | |
| 1493 | #Method ## |
| 1494 | |
| 1495 | # ------------------------------------------------------------------------------ |
| 1496 | |
| 1497 | #Method bool readPixels(const SkPixmap& dst, int srcX, int srcY, |
| 1498 | CachingHint cachingHint = kAllow_CachingHint) const |
| 1499 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1500 | Copies a Rect of pixels from Image to dst. Copy starts at (srcX, srcY), and |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 1501 | does not exceed Image (width(), height()). |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1502 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1503 | dst specifies width, height, Color_Type, Alpha_Type, Color_Space, pixel storage, |
| 1504 | and row bytes of destination. dst.rowBytes specifics the gap from one destination |
| 1505 | row to the next. Returns true if pixels are copied. Returns false if: |
| 1506 | #List |
| 1507 | # dst pixel storage equals nullptr ## |
| 1508 | # dst.rowBytes is less than SkImageInfo::minRowBytes ## |
| 1509 | # Pixel_Ref is nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1510 | ## |
| 1511 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1512 | Pixels are copied only if pixel conversion is possible. If Image Color_Type is |
| 1513 | kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match. |
| 1514 | If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match. |
| 1515 | If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must |
| 1516 | match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns |
| 1517 | false if pixel conversion is not possible. |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 1518 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1519 | srcX and srcY may be negative to copy only top or left of source. Returns |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 1520 | false if width() or height() is zero or negative. |
| 1521 | Returns false if |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1522 | #Formula |
| 1523 | abs(srcX) >= Image width() |
| 1524 | ## |
| 1525 | , or if |
| 1526 | #Formula |
| 1527 | abs(srcY) >= Image height() |
| 1528 | ## |
| 1529 | . |
| 1530 | |
| 1531 | If cachingHint is kAllow_CachingHint, pixels may be retained locally. |
| 1532 | If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache. |
| 1533 | |
| 1534 | #Param dst destination Pixmap: Image_Info, pixels, row bytes ## |
| 1535 | #Param srcX column index whose absolute value is less than width() ## |
| 1536 | #Param srcY row index whose absolute value is less than height() ## |
| 1537 | #Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ## |
| 1538 | |
| 1539 | #Return true if pixels are copied to dst ## |
| 1540 | |
| 1541 | #Example |
| 1542 | #Image 3 |
| 1543 | std::vector<int32_t> srcPixels; |
| 1544 | int rowBytes = image->width() * 4; |
| 1545 | int quarterWidth = image->width() / 4; |
| 1546 | int quarterHeight = image->height() / 4; |
| 1547 | srcPixels.resize(image->height() * rowBytes); |
| 1548 | for (int y = 0; y < 4; ++y) { |
| 1549 | for (int x = 0; x < 4; ++x) { |
| 1550 | SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight), |
| 1551 | &srcPixels.front() + x * image->height() * quarterWidth + |
| 1552 | y * quarterWidth, rowBytes); |
| 1553 | image->readPixels(pixmap, x * quarterWidth, y * quarterHeight); |
| 1554 | } |
| 1555 | } |
| 1556 | canvas->scale(.5f, .5f); |
| 1557 | SkBitmap bitmap; |
| 1558 | bitmap.installPixels(SkImageInfo::MakeN32Premul(image->width(), image->height()), |
| 1559 | &srcPixels.front(), rowBytes); |
| 1560 | canvas->drawBitmap(bitmap, 0, 0); |
| 1561 | ## |
| 1562 | |
| 1563 | #SeeAlso scalePixels SkBitmap::readPixels SkPixmap::readPixels SkCanvas::readPixels SkSurface::readPixels |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1564 | |
| 1565 | #Method ## |
| 1566 | |
| 1567 | # ------------------------------------------------------------------------------ |
| 1568 | |
| 1569 | #Method bool scalePixels(const SkPixmap& dst, SkFilterQuality filterQuality, |
| 1570 | CachingHint cachingHint = kAllow_CachingHint) const |
Cary Clark | 78de751 | 2018-02-07 07:27:09 -0500 | [diff] [blame] | 1571 | #In Pixels |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1572 | #Line # scales and converts one Image to another ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1573 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1574 | Copies Image to dst, scaling pixels to fit dst.width() and dst.height(), and |
| 1575 | converting pixels to match dst.colorType and dst.alphaType. Returns true if |
| 1576 | pixels are copied. Returns false if dst.addr() is nullptr, or dst.rowBytes is |
| 1577 | less than dst SkImageInfo::minRowBytes. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1578 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1579 | Pixels are copied only if pixel conversion is possible. If Image Color_Type is |
| 1580 | kGray_8_SkColorType, or kAlpha_8_SkColorType; dst.colorType must match. |
| 1581 | If Image Color_Type is kGray_8_SkColorType, dst.colorSpace must match. |
| 1582 | If Image Alpha_Type is kOpaque_SkAlphaType, dst.alphaType must |
| 1583 | match. If Image Color_Space is nullptr, dst.colorSpace must match. Returns |
| 1584 | false if pixel conversion is not possible. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1585 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1586 | Scales the image, with filterQuality, to match dst.width() and dst.height(). |
| 1587 | filterQuality kNone_SkFilterQuality is fastest, typically implemented with |
| 1588 | Filter_Quality_Nearest_Neighbor. kLow_SkFilterQuality is typically implemented with |
| 1589 | Filter_Quality_Bilerp. kMedium_SkFilterQuality is typically implemented with |
| 1590 | Filter_Quality_Bilerp, and Filter_Quality_MipMap when size is reduced. |
| 1591 | kHigh_SkFilterQuality is slowest, typically implemented with Filter_Quality_BiCubic. |
| 1592 | |
| 1593 | If cachingHint is kAllow_CachingHint, pixels may be retained locally. |
| 1594 | If cachingHint is kDisallow_CachingHint, pixels are not added to the local cache. |
| 1595 | |
| 1596 | #Param dst destination Pixmap: Image_Info, pixels, row bytes ## |
| 1597 | #Param filterQuality one of: kNone_SkFilterQuality, kLow_SkFilterQuality, |
| 1598 | kMedium_SkFilterQuality, kHigh_SkFilterQuality |
| 1599 | ## |
| 1600 | #Param cachingHint one of: kAllow_CachingHint, kDisallow_CachingHint ## |
| 1601 | |
| 1602 | #Return true if pixels are scaled to fit dst ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1603 | |
| 1604 | #Example |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1605 | #Image 3 |
| 1606 | #Height 128 |
| 1607 | std::vector<int32_t> srcPixels; |
| 1608 | int quarterWidth = image->width() / 16; |
| 1609 | int rowBytes = quarterWidth * 4; |
| 1610 | int quarterHeight = image->height() / 16; |
| 1611 | srcPixels.resize(quarterHeight * rowBytes); |
| 1612 | SkPixmap pixmap(SkImageInfo::MakeN32Premul(quarterWidth, quarterHeight), |
| 1613 | &srcPixels.front(), rowBytes); |
| 1614 | canvas->scale(4, 4); |
| 1615 | SkFilterQuality qualities[] = { kNone_SkFilterQuality, kLow_SkFilterQuality, |
| 1616 | kMedium_SkFilterQuality, kHigh_SkFilterQuality }; |
| 1617 | for (unsigned index = 0; index < SK_ARRAY_COUNT(qualities); ++index) { |
| 1618 | image->scalePixels(pixmap, qualities[index]); |
| 1619 | sk_sp<SkImage> filtered = SkImage::MakeFromRaster(pixmap, nullptr, nullptr); |
| 1620 | canvas->drawImage(filtered, 16 * index, 0); |
| 1621 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1622 | ## |
| 1623 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1624 | #SeeAlso SkCanvas::drawImage readPixels SkPixmap::scalePixels |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1625 | |
| 1626 | #Method ## |
| 1627 | |
| 1628 | # ------------------------------------------------------------------------------ |
| 1629 | |
| 1630 | #Method sk_sp<SkData> encodeToData(SkEncodedImageFormat encodedImageFormat, int quality) const |
Cary Clark | 78de751 | 2018-02-07 07:27:09 -0500 | [diff] [blame] | 1631 | #In Utility |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1632 | #Line # returns encoded Image as SkData ## |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 1633 | Encodes Image pixels, returning result as SkData. |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 1634 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1635 | Returns nullptr if encoding fails, or if encodedImageFormat is not supported. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1636 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1637 | Image encoding in a format requires both building with one or more of: |
| 1638 | SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY; and platform support |
| 1639 | for the encoded format. |
| 1640 | |
| 1641 | If SK_BUILD_FOR_MAC or SK_BUILD_FOR_IOS is defined, encodedImageFormat can |
| 1642 | additionally be one of: SkEncodedImageFormat::kICO, SkEncodedImageFormat::kBMP, |
| 1643 | SkEncodedImageFormat::kGIF. |
| 1644 | |
| 1645 | quality is a platform and format specific metric trading off size and encoding |
| 1646 | error. When used, quality equaling 100 encodes with the least error. quality may |
| 1647 | be ignored by the encoder. |
| 1648 | |
| 1649 | #Param encodedImageFormat one of: SkEncodedImageFormat::kJPEG, SkEncodedImageFormat::kPNG, |
| 1650 | SkEncodedImageFormat::kWEBP |
| 1651 | ## |
| 1652 | #Param quality encoder specific metric with 100 equaling best ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1653 | |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 1654 | #Return encoded Image, or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1655 | |
| 1656 | #Example |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1657 | #Image 3 |
| 1658 | canvas->scale(4, 4); |
| 1659 | SkIRect subset = {0, 0, 16, 64}; |
| 1660 | int x = 0; |
| 1661 | for (int quality : { 0, 10, 50, 100 } ) { |
| 1662 | sk_sp<SkData> data(image->encodeToData(SkEncodedImageFormat::kJPEG, quality)); |
| 1663 | sk_sp<SkImage> filtered = SkImage::MakeFromEncoded(data, &subset); |
| 1664 | canvas->drawImage(filtered, x, 0); |
| 1665 | x += 16; |
| 1666 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1667 | ## |
| 1668 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1669 | #SeeAlso refEncodedData MakeFromEncoded |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1670 | |
| 1671 | #Method ## |
| 1672 | |
| 1673 | # ------------------------------------------------------------------------------ |
| 1674 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 1675 | #Method sk_sp<SkData> encodeToData() const |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1676 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1677 | Encodes Image pixels, returning result as SkData. Returns existing encoded data |
| 1678 | if present; otherwise, Image is encoded with SkEncodedImageFormat::kPNG. Skia |
| 1679 | must be built with SK_HAS_PNG_LIBRARY to encode Image. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1680 | |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 1681 | Returns nullptr if existing encoded data is missing or invalid, and |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1682 | encoding fails. |
| 1683 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1684 | #Return encoded Image, or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1685 | |
| 1686 | #Example |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1687 | #Image 3 |
| 1688 | canvas->scale(4, 4); |
| 1689 | SkIRect subset = {136, 32, 200, 96}; |
| 1690 | sk_sp<SkData> data(image->encodeToData()); |
| 1691 | sk_sp<SkImage> eye = SkImage::MakeFromEncoded(data, &subset); |
| 1692 | canvas->drawImage(eye, 0, 0); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1693 | ## |
| 1694 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1695 | #SeeAlso refEncodedData MakeFromEncoded |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1696 | |
| 1697 | #Method ## |
| 1698 | |
| 1699 | # ------------------------------------------------------------------------------ |
| 1700 | |
| 1701 | #Method sk_sp<SkData> refEncodedData() const |
Cary Clark | 78de751 | 2018-02-07 07:27:09 -0500 | [diff] [blame] | 1702 | #In Utility |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1703 | #Line # returns Image encoded in SkData if present ## |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1704 | Returns encoded Image pixels as SkData, if Image was created from supported |
| 1705 | encoded stream format. Platform support for formats vary and may require building |
| 1706 | with one or more of: SK_HAS_JPEG_LIBRARY, SK_HAS_PNG_LIBRARY, SK_HAS_WEBP_LIBRARY. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1707 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1708 | Returns nullptr if Image contents are not encoded. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1709 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1710 | #Return encoded Image, or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1711 | |
| 1712 | #Example |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1713 | #Image 3 |
| 1714 | #Platform gpu |
| 1715 | struct { |
| 1716 | const char* name; |
| 1717 | sk_sp<SkImage> image; |
| 1718 | } tests[] = { { "image", image }, { "bitmap", SkImage::MakeFromBitmap(source) }, |
| 1719 | { "texture", SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture, |
Cary Clark | ae957c4 | 2018-06-07 17:07:17 -0400 | [diff] [blame] | 1720 | kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, |
| 1721 | kOpaque_SkAlphaType, nullptr) } }; |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1722 | SkString string; |
| 1723 | SkPaint paint; |
| 1724 | for (const auto& test : tests ) { |
| 1725 | if (!test.image) { |
| 1726 | string.printf("no %s", test.name); |
| 1727 | } else { |
| 1728 | string.printf("%s" "encoded %s", test.image->refEncodedData() ? "" : "no ", test.name); |
| 1729 | } |
| 1730 | canvas->drawString(string, 10, 20, paint); |
| 1731 | canvas->translate(0, 20); |
| 1732 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1733 | ## |
| 1734 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1735 | #SeeAlso encodeToData MakeFromEncoded |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1736 | |
| 1737 | #Method ## |
| 1738 | |
| 1739 | # ------------------------------------------------------------------------------ |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1740 | #Subtopic Utility |
| 1741 | #Populate |
| 1742 | #Line # rarely called management functions ## |
| 1743 | ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1744 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1745 | #Method sk_sp<SkImage> makeSubset(const SkIRect& subset) const |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1746 | #In Constructor |
| 1747 | #Line # creates Image containing part of original ## |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1748 | Returns subset of Image. subset must be fully contained by Image dimensions(). |
| 1749 | The implementation may share pixels, or may copy them. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1750 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1751 | Returns nullptr if subset is empty, or subset is not contained by bounds, or |
| 1752 | pixels in Image could not be read or copied. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1753 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1754 | #Param subset bounds of returned Image ## |
| 1755 | |
| 1756 | #Return partial or full Image, or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1757 | |
| 1758 | #Example |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1759 | #Image 3 |
| 1760 | canvas->scale(.5f, .5f); |
| 1761 | const int width = 32; |
| 1762 | const int height = 32; |
| 1763 | for (int y = 0; y < 512; y += height ) { |
| 1764 | for (int x = 0; x < 512; x += width ) { |
| 1765 | sk_sp<SkImage> subset(image->makeSubset({x, y, x + width, y + height})); |
| 1766 | canvas->drawImage(subset, x * 3 / 2, y * 3 / 2); |
| 1767 | } |
| 1768 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1769 | ## |
| 1770 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1771 | #SeeAlso MakeFromEncoded |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1772 | |
| 1773 | #Method ## |
| 1774 | |
| 1775 | # ------------------------------------------------------------------------------ |
| 1776 | |
Greg Daniel | 5f4b09d | 2018-06-12 16:39:59 -0400 | [diff] [blame] | 1777 | #Method sk_sp<SkImage> makeTextureImage(GrContext* context, SkColorSpace* dstColorSpace, |
| 1778 | GrMipMapped mipMapped = GrMipMapped::kNo) const |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1779 | #In Constructor |
| 1780 | #Line # creates Image matching Color_Space if possible ## |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1781 | Returns Image backed by GPU_Texture associated with context. Returned Image is |
Cary Clark | 5538c13 | 2018-06-14 12:28:14 -0400 | [diff] [blame] | 1782 | compatible with Surface created with dstColorSpace. The returned Image respects |
| 1783 | mipMapped setting; if mipMapped equals GrMipMapped::kYes, the backing texture |
| 1784 | allocates Mip_Map levels. Returns original Image if context |
| 1785 | and dstColorSpace match and mipMapped is compatible with backing GPU_Texture. |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1786 | |
| 1787 | Returns nullptr if context is nullptr, or if Image was created with another |
| 1788 | GrContext. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1789 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 1790 | #Param context GPU_Context ## |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1791 | #Param dstColorSpace range of colors of matching Surface on GPU ## |
Cary Clark | 5538c13 | 2018-06-14 12:28:14 -0400 | [diff] [blame] | 1792 | #Param mipMapped whether created Image texture must allocate Mip_Map levels ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1793 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1794 | #Return created Image, or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1795 | |
| 1796 | #Example |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1797 | #Platform gpu |
| 1798 | #Image 5 |
| 1799 | auto drawImage = [=](sk_sp<SkImage> image, GrContext* context, const char* label) -> void { |
| 1800 | if (nullptr == image || nullptr == context) { |
| 1801 | return; |
| 1802 | } |
| 1803 | SkPaint paint; |
| 1804 | paint.setAntiAlias(true); |
| 1805 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 1806 | sk_sp<SkImage> texture(image->makeTextureImage(context, nullptr)); |
| 1807 | canvas->drawImage(texture, 0, 0); |
| 1808 | canvas->drawString(label, texture->width() / 2, texture->height() / 4, paint); |
| 1809 | }; |
| 1810 | sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source)); |
| 1811 | GrContext* context = canvas->getGrContext(); |
| 1812 | sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(context, backEndTexture, |
Cary Clark | ae957c4 | 2018-06-07 17:07:17 -0400 | [diff] [blame] | 1813 | kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, |
| 1814 | kOpaque_SkAlphaType, nullptr)); |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1815 | drawImage(image, context, "image"); |
| 1816 | canvas->translate(image->width(), 0); |
| 1817 | drawImage(bitmapImage, context, "source"); |
| 1818 | canvas->translate(-image->width(), image->height()); |
| 1819 | drawImage(textureImage, context, "backEndTexture"); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1820 | ## |
| 1821 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1822 | #SeeAlso MakeFromTexture |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1823 | |
| 1824 | #Method ## |
| 1825 | |
| 1826 | # ------------------------------------------------------------------------------ |
| 1827 | |
| 1828 | #Method sk_sp<SkImage> makeNonTextureImage() const |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1829 | #In Constructor |
| 1830 | #Line # creates Image without dependency on GPU_Texture ## |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1831 | Returns Raster_Image or Lazy_Image. Copies Image backed by GPU_Texture into |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1832 | CPU memory if needed. Returns original Image if decoded in Raster_Bitmap, |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1833 | or if encoded in a stream. |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 1834 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1835 | Returns nullptr if backed by GPU_Texture and copy fails. |
| 1836 | |
| 1837 | #Return Raster_Image, Lazy_Image, or nullptr ## |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 1838 | |
| 1839 | #Example |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1840 | #Image 5 |
| 1841 | #Platform gpu |
| 1842 | auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void { |
| 1843 | if (nullptr == image) { |
| 1844 | return; |
| 1845 | } |
| 1846 | SkPaint paint; |
| 1847 | paint.setAntiAlias(true); |
| 1848 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 1849 | sk_sp<SkImage> nonTexture(image->makeNonTextureImage()); |
| 1850 | canvas->drawImage(nonTexture, 0, 0); |
| 1851 | canvas->drawString(label, nonTexture->width() / 2, nonTexture->height() / 4, paint); |
| 1852 | }; |
| 1853 | sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source)); |
| 1854 | sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture, |
Cary Clark | ae957c4 | 2018-06-07 17:07:17 -0400 | [diff] [blame] | 1855 | kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, |
| 1856 | kOpaque_SkAlphaType, nullptr)); |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1857 | drawImage(image, "image"); |
| 1858 | canvas->translate(image->width(), 0); |
| 1859 | drawImage(bitmapImage, "source"); |
| 1860 | canvas->translate(-image->width(), image->height()); |
| 1861 | drawImage(textureImage, "backEndTexture"); |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 1862 | ## |
| 1863 | |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 1864 | #SeeAlso makeTextureImage makeRasterImage MakeBackendTextureFromSkImage |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 1865 | |
| 1866 | #Method ## |
| 1867 | |
| 1868 | # ------------------------------------------------------------------------------ |
| 1869 | |
| 1870 | #Method sk_sp<SkImage> makeRasterImage() const |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1871 | #In Constructor |
| 1872 | #Line # creates Image compatible with Raster_Surface if possible ## |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1873 | Returns Raster_Image. Copies Image backed by GPU_Texture into CPU memory, |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1874 | or decodes Image from Lazy_Image. Returns original Image if decoded in |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1875 | Raster_Bitmap. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1876 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1877 | Returns nullptr if copy, decode, or pixel read fails. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1878 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1879 | #Return Raster_Image, or nullptr ## |
| 1880 | |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1881 | #Bug 7479 |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1882 | #Example |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1883 | #Image 5 |
| 1884 | #Platform gpu |
| 1885 | auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void { |
| 1886 | if (nullptr == image) { |
| 1887 | return; |
| 1888 | } |
| 1889 | SkPaint paint; |
| 1890 | paint.setAntiAlias(true); |
| 1891 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 1892 | sk_sp<SkImage> raster(image->makeRasterImage()); |
| 1893 | canvas->drawImage(raster, 0, 0); |
| 1894 | canvas->drawString(label, raster->width() / 2, raster->height() / 4, paint); |
| 1895 | }; |
| 1896 | sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source)); |
| 1897 | sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture, |
Cary Clark | ae957c4 | 2018-06-07 17:07:17 -0400 | [diff] [blame] | 1898 | kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, |
| 1899 | kOpaque_SkAlphaType, nullptr)); |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1900 | drawImage(image, "image"); |
| 1901 | canvas->translate(image->width(), 0); |
| 1902 | drawImage(bitmapImage, "source"); |
| 1903 | canvas->translate(-image->width(), image->height()); |
| 1904 | drawImage(textureImage, "backEndTexture"); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1905 | ## |
| 1906 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1907 | #SeeAlso isTextureBacked isLazyGenerated MakeFromRaster |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1908 | |
| 1909 | #Method ## |
| 1910 | |
| 1911 | # ------------------------------------------------------------------------------ |
| 1912 | |
| 1913 | #Method sk_sp<SkImage> makeWithFilter(const SkImageFilter* filter, const SkIRect& subset, |
| 1914 | const SkIRect& clipBounds, SkIRect* outSubset, |
| 1915 | SkIPoint* offset) const |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1916 | #In Constructor |
| 1917 | #Line # creates filtered, clipped Image ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1918 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1919 | Creates filtered Image. filter processes original Image, potentially changing |
| 1920 | color, position, and size. subset is the bounds of original Image processed |
| 1921 | by filter. clipBounds is the expected bounds of the filtered Image. outSubset |
| 1922 | is required storage for the actual bounds of the filtered Image. offset is |
| 1923 | required storage for translation of returned Image. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1924 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1925 | Returns nullptr if Image could not be created. If nullptr is returned, outSubset |
| 1926 | and offset are undefined. |
| 1927 | |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 1928 | Useful for animation of SkImageFilter that varies size from frame to frame. |
| 1929 | Returned Image is created larger than required by filter so that GPU_Texture |
| 1930 | can be reused with different sized effects. outSubset describes the valid bounds |
| 1931 | of GPU_Texture returned. offset translates the returned Image to keep subsequent |
| 1932 | animation frames aligned with respect to each other. |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1933 | |
| 1934 | #Param filter how Image is sampled when transformed ## |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 1935 | #Param subset bounds of Image processed by filter ## |
| 1936 | #Param clipBounds expected bounds of filtered Image ## |
| 1937 | #Param outSubset storage for returned Image bounds ## |
| 1938 | #Param offset storage for returned Image translation ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1939 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1940 | #Return filtered Image, or nullptr ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1941 | |
| 1942 | #Example |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 1943 | #Description |
| 1944 | In each frame of the animation, filtered Image is drawn in a different location. |
| 1945 | By translating canvas by returned offset, Image appears stationary. |
| 1946 | ## |
| 1947 | #Image 5 |
| 1948 | #Platform gpu |
| 1949 | #Duration 5 |
| 1950 | sk_sp<SkImageFilter> shadowFilter = SkDropShadowImageFilter::Make( |
| 1951 | -10.0f * frame, 5.0f * frame, 3.0f, 3.0f, SK_ColorBLUE, |
| 1952 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, |
| 1953 | nullptr); |
| 1954 | sk_sp<SkImageFilter> offsetFilter = SkOffsetImageFilter::Make(40, 40, shadowFilter, nullptr); |
| 1955 | SkIRect subset = image->bounds(); |
| 1956 | SkIRect clipBounds = image->bounds(); |
| 1957 | clipBounds.outset(60, 60); |
| 1958 | SkIRect outSubset; |
| 1959 | SkIPoint offset; |
| 1960 | sk_sp<SkImage> filtered(image->makeWithFilter(offsetFilter.get(), subset, clipBounds, |
| 1961 | &outSubset, &offset)); |
| 1962 | SkPaint paint; |
| 1963 | paint.setAntiAlias(true); |
| 1964 | paint.setStyle(SkPaint::kStroke_Style); |
| 1965 | canvas->drawLine(0, 0, offset.fX, offset.fY, paint); |
| 1966 | canvas->translate(offset.fX, offset.fY); |
| 1967 | canvas->drawImage(filtered, 0, 0); |
Cary Clark | 681287e | 2018-03-16 11:34:15 -0400 | [diff] [blame] | 1968 | canvas->drawRect(SkRect::Make(outSubset), paint); |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1969 | ## |
| 1970 | |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 1971 | #SeeAlso makeShader SkPaint::setImageFilter |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1972 | |
| 1973 | #Method ## |
| 1974 | |
| 1975 | # ------------------------------------------------------------------------------ |
| 1976 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1977 | #Typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 1978 | #Line # parameter type for MakeBackendTextureFromSkImage ## |
Cary Clark | ffb3d68 | 2018-05-17 12:17:28 -0400 | [diff] [blame] | 1979 | |
| 1980 | #Code |
| 1981 | typedef std::function<void(GrBackendTexture)> BackendTextureReleaseProc; |
| 1982 | ## |
| 1983 | |
Cary Clark | 0d22539 | 2018-06-07 09:59:07 -0400 | [diff] [blame] | 1984 | Defines a callback function, taking one parameter of type GrBackendTexture with |
| 1985 | no return value. Function is called when back-end texture is to be released. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1986 | ## |
| 1987 | |
| 1988 | # ------------------------------------------------------------------------------ |
| 1989 | |
| 1990 | #Method static bool MakeBackendTextureFromSkImage(GrContext* context, |
| 1991 | sk_sp<SkImage> image, |
| 1992 | GrBackendTexture* backendTexture, |
| 1993 | BackendTextureReleaseProc* backendTextureReleaseProc) |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 1994 | #In Constructor |
| 1995 | #Line # creates GPU_Texture from Image ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 1996 | |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 1997 | Creates a GrBackendTexture from the provided SkImage. Returns true and |
| 1998 | stores result in backendTexture and backendTextureReleaseProc if |
| 1999 | texture is created; otherwise, returns false and leaves |
| 2000 | backendTexture and backendTextureReleaseProc unmodified. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2001 | |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 2002 | Call backendTextureReleaseProc after deleting backendTexture. |
| 2003 | backendTextureReleaseProc cleans up auxiliary data related to returned |
| 2004 | backendTexture. The caller must delete returned backendTexture after use. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2005 | |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 2006 | If Image is both texture backed and singly referenced, image is returned in |
| 2007 | backendTexture without conversion or making a copy. Image is singly referenced |
| 2008 | if its was transferred solely using std::move(). |
| 2009 | |
| 2010 | If Image is not texture backed, returns texture with Image contents. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2011 | |
Cary Clark | 61ca7c5 | 2018-01-02 11:34:14 -0500 | [diff] [blame] | 2012 | #Param context GPU_Context ## |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 2013 | #Param image Image used for texture ## |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 2014 | #Param backendTexture storage for back-end texture ## |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 2015 | #Param backendTextureReleaseProc storage for clean up function ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2016 | |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 2017 | #Return true if back-end texture was created ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2018 | |
| 2019 | #Example |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 2020 | #Platform gpu |
| 2021 | #Height 64 |
| 2022 | #Function |
Brian Salomon | 67f8584 | 2018-02-09 08:50:22 -0500 | [diff] [blame] | 2023 | static sk_sp<SkImage> create_gpu_image(GrContext* grContext) { |
| 2024 | const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType); |
| 2025 | auto surface(SkSurface::MakeRenderTarget(grContext, SkBudgeted::kNo, info)); |
| 2026 | SkCanvas* canvas = surface->getCanvas(); |
| 2027 | canvas->clear(SK_ColorWHITE); |
| 2028 | SkPaint paint; |
| 2029 | paint.setColor(SK_ColorBLACK); |
| 2030 | canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint); |
| 2031 | return surface->makeImageSnapshot(); |
| 2032 | } |
| 2033 | ## |
| 2034 | |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 2035 | void draw(SkCanvas* canvas) { |
Brian Salomon | 67f8584 | 2018-02-09 08:50:22 -0500 | [diff] [blame] | 2036 | GrContext* grContext = canvas->getGrContext(); |
| 2037 | if (!grContext) { |
| 2038 | return; |
| 2039 | } |
| 2040 | sk_sp<SkImage> backEndImage = create_gpu_image(grContext); |
| 2041 | canvas->drawImage(backEndImage, 0, 0); |
| 2042 | GrBackendTexture texture; |
| 2043 | SkImage::BackendTextureReleaseProc proc; |
| 2044 | if (!SkImage::MakeBackendTextureFromSkImage(grContext, std::move(backEndImage), |
| 2045 | &texture, &proc)) { |
| 2046 | return; |
| 2047 | } |
| 2048 | sk_sp<SkImage> i2 = SkImage::MakeFromTexture(grContext, texture, kTopLeft_GrSurfaceOrigin, |
| 2049 | kN32_SkColorType, kOpaque_SkAlphaType, nullptr); |
| 2050 | canvas->drawImage(i2, 30, 30); |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 2051 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2052 | ## |
| 2053 | |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 2054 | #SeeAlso MakeFromTexture makeTextureImage |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2055 | |
| 2056 | #Method ## |
| 2057 | |
| 2058 | # ------------------------------------------------------------------------------ |
| 2059 | |
| 2060 | #Enum LegacyBitmapMode |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 2061 | #Deprecated soon |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2062 | #Code |
| 2063 | enum LegacyBitmapMode { |
| 2064 | kRO_LegacyBitmapMode, |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2065 | }; |
| 2066 | ## |
| 2067 | |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2068 | #Const kRO_LegacyBitmapMode 0 |
Cary Clark | 682c58d | 2018-05-16 07:07:07 -0400 | [diff] [blame] | 2069 | #Line # returned bitmap is read-only and immutable ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2070 | ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2071 | |
| 2072 | #Enum ## |
| 2073 | |
| 2074 | # ------------------------------------------------------------------------------ |
| 2075 | |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 2076 | #Method bool asLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode legacyBitmapMode = kRO_LegacyBitmapMode) const |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 2077 | #In Constructor |
| 2078 | #Line # returns as Raster_Bitmap ## |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 2079 | Creates raster Bitmap with same pixels as Image. If legacyBitmapMode is |
| 2080 | kRO_LegacyBitmapMode, returned bitmap is read-only and immutable. |
| 2081 | Returns true if Bitmap is stored in bitmap. Returns false and resets bitmap if |
| 2082 | Bitmap write did not succeed. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2083 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 2084 | #Param bitmap storage for legacy Bitmap ## |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 2085 | #Param legacyBitmapMode to be deprecated ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2086 | |
Cary Clark | 3cd22cc | 2017-12-01 11:49:58 -0500 | [diff] [blame] | 2087 | #Return true if Bitmap was created ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2088 | |
Cary Clark | ae957c4 | 2018-06-07 17:07:17 -0400 | [diff] [blame] | 2089 | #Example |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 2090 | #Image 4 |
| 2091 | #Platform gpu |
Brian Salomon | 67f8584 | 2018-02-09 08:50:22 -0500 | [diff] [blame] | 2092 | SkBitmap bitImage; |
| 2093 | if (image->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) { |
| 2094 | canvas->drawBitmap(bitImage, 0, 0); |
| 2095 | } |
| 2096 | GrContext* grContext = canvas->getGrContext(); |
| 2097 | if (!grContext) { |
| 2098 | return; |
| 2099 | } |
| 2100 | sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(grContext, backEndTexture, |
Cary Clark | ae957c4 | 2018-06-07 17:07:17 -0400 | [diff] [blame] | 2101 | kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, |
| 2102 | kOpaque_SkAlphaType, nullptr)); |
Brian Salomon | 67f8584 | 2018-02-09 08:50:22 -0500 | [diff] [blame] | 2103 | canvas->drawImage(textureImage, 45, 45); |
| 2104 | if (textureImage->asLegacyBitmap(&bitImage, SkImage::kRO_LegacyBitmapMode)) { |
| 2105 | canvas->drawBitmap(bitImage, 90, 90); |
| 2106 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2107 | ## |
| 2108 | |
Cary Clark | 5635631 | 2018-02-08 14:45:18 -0500 | [diff] [blame] | 2109 | #SeeAlso MakeRasterData makeRasterImage makeNonTextureImage |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2110 | |
| 2111 | #Method ## |
| 2112 | |
| 2113 | # ------------------------------------------------------------------------------ |
| 2114 | |
| 2115 | #Method bool isLazyGenerated() const |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 2116 | #In Property |
| 2117 | #Line # returns if Image is created as needed ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2118 | Returns true if Image is backed by an image-generator or other service that creates |
| 2119 | and caches its pixels or texture on-demand. |
| 2120 | |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 2121 | #Return true if Image is created as needed ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2122 | |
| 2123 | #Example |
Cary Clark | 2f46624 | 2017-12-11 16:03:17 -0500 | [diff] [blame] | 2124 | #Height 80 |
| 2125 | #Function |
| 2126 | class TestImageGenerator : public SkImageGenerator { |
| 2127 | public: |
| 2128 | TestImageGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(10, 10)) {} |
| 2129 | ~TestImageGenerator() override {} |
| 2130 | protected: |
| 2131 | bool onGetPixels(const SkImageInfo& info, void* pixelPtr, size_t rowBytes, |
| 2132 | const Options& options) override { |
| 2133 | SkPMColor* pixels = static_cast<SkPMColor*>(pixelPtr); |
| 2134 | for (int y = 0; y < info.height(); ++y) { |
| 2135 | for (int x = 0; x < info.width(); ++x) { |
| 2136 | pixels[y * info.width() + x] = 0xff223344 + y * 0x000C0811; |
| 2137 | } |
| 2138 | } |
| 2139 | return true; |
| 2140 | } |
| 2141 | }; |
| 2142 | ## |
| 2143 | void draw(SkCanvas* canvas) { |
| 2144 | auto gen = std::unique_ptr<TestImageGenerator>(new TestImageGenerator()); |
| 2145 | sk_sp<SkImage> image(SkImage::MakeFromGenerator(std::move(gen))); |
| 2146 | SkString lazy(image->isLazyGenerated() ? "is lazy" : "not lazy"); |
| 2147 | canvas->scale(8, 8); |
| 2148 | canvas->drawImage(image, 0, 0, nullptr); |
| 2149 | SkPaint paint; |
| 2150 | paint.setTextSize(4); |
| 2151 | canvas->drawString(lazy, 2, 5, paint); |
| 2152 | } |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2153 | ## |
| 2154 | |
Cary Clark | f5404bb | 2018-01-05 12:10:09 -0500 | [diff] [blame] | 2155 | #Example |
| 2156 | #Image 5 |
| 2157 | #Platform gpu |
| 2158 | void draw(SkCanvas* canvas) { |
| 2159 | auto drawImage = [=](sk_sp<SkImage> image, const char* label) -> void { |
| 2160 | if (nullptr == image) { |
| 2161 | return; |
| 2162 | } |
| 2163 | SkPaint paint; |
| 2164 | paint.setAntiAlias(true); |
| 2165 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 2166 | canvas->drawImage(image, 0, 0); |
| 2167 | canvas->drawString(label, image->width() / 2, image->height() / 4, paint); |
| 2168 | canvas->drawString( |
| 2169 | image->isLazyGenerated() ? "is lazily generated" : "not lazily generated", |
| 2170 | image->width() / 2, image->height() * 3 / 4, paint); |
| 2171 | }; |
| 2172 | sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source)); |
| 2173 | sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture, |
Cary Clark | ae957c4 | 2018-06-07 17:07:17 -0400 | [diff] [blame] | 2174 | kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, |
| 2175 | kOpaque_SkAlphaType, nullptr)); |
Cary Clark | f5404bb | 2018-01-05 12:10:09 -0500 | [diff] [blame] | 2176 | drawImage(image, "image"); |
| 2177 | canvas->translate(image->width(), 0); |
| 2178 | drawImage(bitmapImage, "source"); |
| 2179 | canvas->translate(-image->width(), image->height()); |
| 2180 | drawImage(textureImage, "backEndTexture"); |
| 2181 | } |
| 2182 | ## |
| 2183 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 2184 | #SeeAlso isTextureBacked MakeNonTextureImage |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2185 | |
| 2186 | #Method ## |
| 2187 | |
| 2188 | # ------------------------------------------------------------------------------ |
| 2189 | |
Cary Clark | e80cd44 | 2018-07-17 13:19:56 -0400 | [diff] [blame] | 2190 | #Method sk_sp<SkImage> makeColorSpace(sk_sp<SkColorSpace> target) const |
Cary Clark | 4855f78 | 2018-02-06 09:41:53 -0500 | [diff] [blame] | 2191 | #In Constructor |
| 2192 | #Line # creates Image matching Color_Space if possible ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2193 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 2194 | Creates Image in target Color_Space. |
| 2195 | Returns nullptr if Image could not be created. |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2196 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 2197 | Returns original Image if it is in target Color_Space. |
| 2198 | Otherwise, converts pixels from Image Color_Space to target Color_Space. |
| 2199 | If Image colorSpace returns nullptr, Image Color_Space is assumed to be sRGB. |
| 2200 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 2201 | #Param target Color_Space describing color range of returned Image ## |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2202 | |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 2203 | #Return created Image in target Color_Space ## |
| 2204 | |
| 2205 | #Example |
| 2206 | #Image 5 |
| 2207 | #Set sRGB |
| 2208 | sk_sp<SkColorSpace> normalColorSpace = SkColorSpace::MakeRGB( |
| 2209 | SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kSRGB_Gamut); |
| 2210 | sk_sp<SkColorSpace> wackyColorSpace = normalColorSpace->makeColorSpin(); |
| 2211 | for (auto colorSpace : { normalColorSpace, wackyColorSpace } ) { |
Cary Clark | c3c1c31 | 2018-07-18 09:25:15 -0400 | [diff] [blame^] | 2212 | sk_sp<SkImage> colorSpaced = image->makeColorSpace(colorSpace); |
| 2213 | canvas->drawImage(colorSpaced, 0, 0); |
| 2214 | canvas->translate(128, 0); |
Cary Clark | ac47b88 | 2018-01-11 10:35:44 -0500 | [diff] [blame] | 2215 | } |
| 2216 | ## |
| 2217 | |
| 2218 | #SeeAlso MakeFromPixture MakeFromTexture |
Cary Clark | a560c47 | 2017-11-27 10:44:06 -0500 | [diff] [blame] | 2219 | |
| 2220 | #Method ## |
| 2221 | |
| 2222 | #Class SkImage ## |
| 2223 | |
| 2224 | #Topic Image ## |