SkImage.h revision ddbbddabefe865cc52e4b89a9d45455b987129bd
1/* 2 * Copyright 2012 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8#ifndef SkImage_DEFINED 9#define SkImage_DEFINED 10 11#include "SkFilterQuality.h" 12#include "SkImageInfo.h" 13#include "SkImageEncoder.h" 14#include "SkRefCnt.h" 15#include "SkScalar.h" 16#include "SkShader.h" 17 18class SkData; 19class SkCanvas; 20class SkColorTable; 21class SkImageGenerator; 22class SkPaint; 23class SkPicture; 24class SkString; 25class SkSurface; 26class SkSurfaceProps; 27class GrContext; 28class GrTexture; 29 30/** 31 * SkImage is an abstraction for drawing a rectagle of pixels, though the 32 * particular type of image could be actually storing its data on the GPU, or 33 * as drawing commands (picture or PDF or otherwise), ready to be played back 34 * into another canvas. 35 * 36 * The content of SkImage is always immutable, though the actual storage may 37 * change, if for example that image can be re-created via encoded data or 38 * other means. 39 * 40 * SkImage always has a non-zero dimensions. If there is a request to create a new image, either 41 * directly or via SkSurface, and either of the requested dimensions are zero, then NULL will be 42 * returned. 43 */ 44class SK_API SkImage : public SkRefCnt { 45public: 46 typedef SkImageInfo Info; 47 typedef void* ReleaseContext; 48 49 static SkImage* NewRasterCopy(const Info&, const void* pixels, size_t rowBytes, 50 SkColorTable* ctable = NULL); 51 static SkImage* NewRasterData(const Info&, SkData* pixels, size_t rowBytes); 52 53 typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext); 54 55 /** 56 * Return a new Image referencing the specified pixels. These must remain valid and unchanged 57 * until the specified release-proc is called, indicating that Skia no longer has a reference 58 * to the pixels. 59 * 60 * Returns NULL if the requested Info is unsupported. 61 */ 62 static SkImage* NewFromRaster(const Info&, const void* pixels, size_t rowBytes, 63 RasterReleaseProc, ReleaseContext); 64 65 /** 66 * Construct a new image from the specified bitmap. If the bitmap is marked immutable, and 67 * its pixel memory is shareable, it may be shared instead of copied. 68 */ 69 static SkImage* NewFromBitmap(const SkBitmap&); 70 71 /** 72 * Construct a new SkImage based on the given ImageGenerator. 73 * This function will always take ownership of the passed 74 * ImageGenerator. Returns NULL on error. 75 * 76 * If a subset is specified, it must be contained within the generator's bounds. 77 */ 78 static SkImage* NewFromGenerator(SkImageGenerator*, const SkIRect* subset = NULL); 79 80 /** 81 * Construct a new SkImage based on the specified encoded data. Returns NULL on failure, 82 * which can mean that the format of the encoded data was not recognized/supported. 83 * 84 * If a subset is specified, it must be contained within the encoded data's bounds. 85 * 86 * Regardless of success or failure, the caller is responsible for managing their ownership 87 * of the data. 88 */ 89 static SkImage* NewFromEncoded(SkData* encoded, const SkIRect* subset = NULL); 90 91 /** 92 * Create a new image from the specified descriptor. Note - the caller is responsible for 93 * managing the lifetime of the underlying platform texture. 94 * 95 * Will return NULL if the specified descriptor is unsupported. 96 */ 97 static SkImage* NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc) { 98 return NewFromTexture(ctx, desc, kPremul_SkAlphaType, NULL, NULL); 99 } 100 101 static SkImage* NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& de, SkAlphaType at) { 102 return NewFromTexture(ctx, de, at, NULL, NULL); 103 } 104 105 typedef void (*TextureReleaseProc)(ReleaseContext); 106 107 /** 108 * Create a new image from the specified descriptor. The underlying platform texture must stay 109 * valid and unaltered until the specified release-proc is invoked, indicating that Skia 110 * no longer is holding a reference to it. 111 * 112 * Will return NULL if the specified descriptor is unsupported. 113 */ 114 static SkImage* NewFromTexture(GrContext*, const GrBackendTextureDesc&, SkAlphaType, 115 TextureReleaseProc, ReleaseContext); 116 117 /** 118 * Create a new image from the specified descriptor. Note - Skia will delete or recycle the 119 * texture when the image is released. 120 * 121 * Will return NULL if the specified descriptor is unsupported. 122 */ 123 static SkImage* NewFromAdoptedTexture(GrContext*, const GrBackendTextureDesc&, 124 SkAlphaType = kPremul_SkAlphaType); 125 126 /** 127 * Create a new image by copying the pixels from the specified descriptor. No reference is 128 * kept to the original platform texture. 129 * 130 * Will return NULL if the specified descriptor is unsupported. 131 */ 132 static SkImage* NewFromTextureCopy(GrContext*, const GrBackendTextureDesc&, 133 SkAlphaType = kPremul_SkAlphaType); 134 135 /** 136 * Create a new image by copying the pixels from the specified y, u, v textures. The data 137 * from the textures is immediately ingested into the image and the textures can be modified or 138 * deleted after the function returns. The image will have the dimensions of the y texture. 139 */ 140 static SkImage* NewFromYUVTexturesCopy(GrContext*, SkYUVColorSpace, 141 const GrBackendObject yuvTextureHandles[3], 142 const SkISize yuvSizes[3], 143 GrSurfaceOrigin); 144 145 static SkImage* NewFromPicture(const SkPicture*, const SkISize& dimensions, 146 const SkMatrix*, const SkPaint*); 147 148 /////////////////////////////////////////////////////////////////////////////////////////////// 149 150 int width() const { return fWidth; } 151 int height() const { return fHeight; } 152 uint32_t uniqueID() const { return fUniqueID; } 153 virtual bool isOpaque() const { return false; } 154 155 virtual SkShader* newShader(SkShader::TileMode, 156 SkShader::TileMode, 157 const SkMatrix* localMatrix = NULL) const; 158 159 /** 160 * If the image has direct access to its pixels (i.e. they are in local 161 * RAM) return the (const) address of those pixels, and if not null, return 162 * the ImageInfo and rowBytes. The returned address is only valid while 163 * the image object is in scope. 164 * 165 * On failure, returns NULL and the info and rowBytes parameters are 166 * ignored. 167 */ 168 const void* peekPixels(SkImageInfo* info, size_t* rowBytes) const; 169 170 /** 171 * If the image has direct access to its pixels (i.e. they are in local 172 * RAM) return the (const) address of those pixels, and if not null, return 173 * true, and if pixmap is not NULL, set it to point into the image. 174 * 175 * On failure, return false and ignore the pixmap parameter. 176 */ 177 bool peekPixels(SkPixmap* pixmap) const; 178 179 // DEPRECATED 180 GrTexture* getTexture() const; 181 182 /** 183 * Returns true if the image is texture backed. 184 */ 185 bool isTextureBacked() const; 186 187 /** 188 * Retrieves the backend API handle of the texture. If flushPendingGrContextIO then the 189 * GrContext will issue to the backend API any deferred IO operations on the texture before 190 * returning. 191 */ 192 GrBackendObject getTextureHandle(bool flushPendingGrContextIO) const; 193 194 /** 195 * Copy the pixels from the image into the specified buffer (pixels + rowBytes), 196 * converting them into the requested format (dstInfo). The image pixels are read 197 * starting at the specified (srcX,srcY) location. 198 * 199 * The specified ImageInfo and (srcX,srcY) offset specifies a source rectangle 200 * 201 * srcR.setXYWH(srcX, srcY, dstInfo.width(), dstInfo.height()); 202 * 203 * srcR is intersected with the bounds of the image. If this intersection is not empty, 204 * then we have two sets of pixels (of equal size). Replace the dst pixels with the 205 * corresponding src pixels, performing any colortype/alphatype transformations needed 206 * (in the case where the src and dst have different colortypes or alphatypes). 207 * 208 * This call can fail, returning false, for several reasons: 209 * - If srcR does not intersect the image bounds. 210 * - If the requested colortype/alphatype cannot be converted from the image's types. 211 */ 212 bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, 213 int srcX, int srcY) const; 214 215 bool readPixels(const SkPixmap& dst, int srcX, int srcY) const; 216 217 /** 218 * Encode the image's pixels and return the result as a new SkData, which 219 * the caller must manage (i.e. call unref() when they are done). 220 * 221 * If the image type cannot be encoded, or the requested encoder type is 222 * not supported, this will return NULL. 223 */ 224 SkData* encode(SkImageEncoder::Type, int quality) const; 225 226 SkData* encode() const { 227 return this->encode(SkImageEncoder::kPNG_Type, 100); 228 } 229 230 /** 231 * If the image already has its contents in encoded form (e.g. PNG or JPEG), return a ref 232 * to that data (which the caller must call unref() on). The caller is responsible for calling 233 * unref on the data when they are done. 234 * 235 * If the image does not already has its contents in encoded form, return NULL. 236 * 237 * Note: to force the image to return its contents as encoded data, try calling encode(...). 238 */ 239 SkData* refEncoded() const; 240 241 /** 242 * Return a new surface that is compatible with this image's internal representation 243 * (e.g. raster or gpu). 244 * 245 * If no surfaceprops are specified, the image will attempt to match the props of when it 246 * was created (if it came from a surface). 247 */ 248 SkSurface* newSurface(const SkImageInfo&, const SkSurfaceProps* = NULL) const; 249 250 const char* toString(SkString*) const; 251 252 /** 253 * Return an image that is a rescale of this image (using newWidth, newHeight). 254 * 255 * If subset is NULL, then the entire original image is used as the src for the scaling. 256 * If subset is not NULL, then it specifies subset of src-pixels used for scaling. If 257 * subset extends beyond the bounds of the original image, then NULL is returned. 258 * 259 * Notes: 260 * - newWidth and newHeight must be > 0 or NULL will be returned. 261 * 262 * - it is legal for the returned image to be the same instance as the src image 263 * (if the new dimensions == the src dimensions and subset is NULL or == src dimensions). 264 * 265 * - it is legal for the "scaled" image to have changed its SkAlphaType from unpremul 266 * to premul (as required by the impl). The image should draw (nearly) identically, 267 * since during drawing we will "apply the alpha" to the pixels. Future optimizations 268 * may take away this caveat, preserving unpremul. 269 */ 270 SkImage* newImage(int newWidth, int newHeight, const SkIRect* subset = NULL, 271 SkFilterQuality = kNone_SkFilterQuality) const; 272 273 // Helper functions to convert to SkBitmap 274 275 enum LegacyBitmapMode { 276 kRO_LegacyBitmapMode, 277 kRW_LegacyBitmapMode, 278 }; 279 280 /** 281 * Attempt to create a bitmap with the same pixels as the image. The result will always be 282 * a raster-backed bitmap (texture-backed bitmaps are DEPRECATED, and not supported here). 283 * 284 * If the mode is kRO (read-only), the resulting bitmap will be marked as immutable. 285 * 286 * On succcess, returns true. On failure, returns false and the bitmap parameter will be reset 287 * to empty. 288 */ 289 bool asLegacyBitmap(SkBitmap*, LegacyBitmapMode) const; 290 291 /** 292 * Returns true if the image is backed by an image-generator or other src that creates 293 * (and caches) its pixels / texture on-demand. 294 */ 295 bool isLazyGenerated() const; 296 297protected: 298 SkImage(int width, int height, uint32_t uniqueID); 299 300private: 301 const int fWidth; 302 const int fHeight; 303 const uint32_t fUniqueID; 304 305 typedef SkRefCnt INHERITED; 306}; 307 308#endif 309