Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef SkImage_DEFINED | 8 #ifndef SkImage_DEFINED |
| 9 #define SkImage_DEFINED | 9 #define SkImage_DEFINED |
| 10 | 10 |
| 11 #include "SkFilterQuality.h" | 11 #include "SkFilterQuality.h" |
| 12 #include "SkImageInfo.h" | 12 #include "SkImageInfo.h" |
| 13 #include "SkImageEncoder.h" | 13 #include "SkImageEncoder.h" |
| 14 #include "SkRefCnt.h" | 14 #include "SkRefCnt.h" |
| 15 #include "SkScalar.h" | 15 #include "SkScalar.h" |
| 16 #include "SkShader.h" | 16 #include "SkShader.h" |
| 17 | 17 |
| 18 class SkData; | 18 class SkData; |
| 19 class SkCanvas; | 19 class SkCanvas; |
| 20 class SkColorTable; | 20 class SkColorTable; |
| 21 class SkImageGenerator; | 21 class SkImageGenerator; |
| 22 class SkPaint; | 22 class SkPaint; |
| 23 class SkPicture; | 23 class SkPicture; |
| 24 class SkPixelSerializer; | 24 class SkPixelSerializer; |
| 25 class SkString; | 25 class SkString; |
| 26 class SkSurface; | 26 class SkSurface; |
| 27 class GrContext; | 27 class GrContext; |
| 28 class GrTexture; | 28 class GrTexture; |
| 29 | 29 |
| 30 #define SK_SUPPORT_LEGACY_IMAGEFACTORY | |
| 31 | |
| 30 /** | 32 /** |
| 31 * SkImage is an abstraction for drawing a rectagle of pixels, though the | 33 * 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 | 34 * 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 | 35 * as drawing commands (picture or PDF or otherwise), ready to be played back |
| 34 * into another canvas. | 36 * into another canvas. |
| 35 * | 37 * |
| 36 * The content of SkImage is always immutable, though the actual storage may | 38 * 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 | 39 * change, if for example that image can be re-created via encoded data or |
| 38 * other means. | 40 * other means. |
| 39 * | 41 * |
| 40 * SkImage always has a non-zero dimensions. If there is a request to create a new image, either | 42 * 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 | 43 * directly or via SkSurface, and either of the requested dimensions are zero, then NULL will be |
| 42 * returned. | 44 * returned. |
| 43 */ | 45 */ |
| 44 class SK_API SkImage : public SkRefCnt { | 46 class SK_API SkImage : public SkRefCnt { |
| 45 public: | 47 public: |
| 46 typedef SkImageInfo Info; | 48 typedef SkImageInfo Info; |
| 47 typedef void* ReleaseContext; | 49 typedef void* ReleaseContext; |
| 48 | 50 |
| 49 static SkImage* NewRasterCopy(const Info&, const void* pixels, size_t rowByt es, | 51 static sk_sp<SkImage> MakeRasterCopy(const SkPixmap&); |
| 50 SkColorTable* ctable = NULL); | 52 static sk_sp<SkImage> MakeRasterData(const Info&, sk_sp<SkData> pixels, size _t rowBytes); |
| 51 static SkImage* NewRasterData(const Info&, SkData* pixels, size_t rowBytes); | |
| 52 | 53 |
| 53 typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext); | 54 typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext); |
| 54 | 55 |
| 55 /** | 56 /** |
| 56 * Return a new Image referencing the specified pixels. These must remain v alid and unchanged | 57 * Return a new Image referencing the specified pixels. These must remain v alid and unchanged |
| 57 * until the specified release-proc is called, indicating that Skia no long er has a reference | 58 * until the specified release-proc is called, indicating that Skia no long er has a reference |
| 58 * to the pixels. | 59 * to the pixels. |
| 59 * | 60 * |
| 60 * Returns NULL if the requested Info is unsupported. | 61 * Returns NULL if the requested pixmap info is unsupported. |
| 61 */ | 62 */ |
| 62 static SkImage* NewFromRaster(const Info&, const void* pixels, size_t rowByt es, | 63 static sk_sp<SkImage> MakeFromRaster(const SkPixmap&, RasterReleaseProc, Rel easeContext); |
| 63 RasterReleaseProc, ReleaseContext); | |
| 64 | 64 |
| 65 /** | 65 /** |
| 66 * Construct a new image from the specified bitmap. If the bitmap is marked immutable, and | 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. | 67 * its pixel memory is shareable, it may be shared instead of copied. |
| 68 */ | 68 */ |
| 69 static SkImage* NewFromBitmap(const SkBitmap&); | 69 static sk_sp<SkImage> MakeFromBitmap(const SkBitmap&); |
| 70 | 70 |
| 71 /** | 71 /** |
| 72 * Construct a new SkImage based on the given ImageGenerator. | 72 * Construct a new SkImage based on the given ImageGenerator. Returns NULL on error. |
| 73 * This function will always take ownership of the passed | |
| 74 * ImageGenerator. Returns NULL on error. | |
| 75 * | 73 * |
| 76 * If a subset is specified, it must be contained within the generator's bo unds. | 74 * If a subset is specified, it must be contained within the generator's bo unds. |
| 77 */ | 75 */ |
| 78 static SkImage* NewFromGenerator(SkImageGenerator*, const SkIRect* subset = NULL); | 76 static sk_sp<SkImage> MakeFromGenerator(SkImageGenerator*, const SkIRect* su bset = NULL); |
|
f(malita)
2016/03/10 18:12:14
I think we want sk_sp<SkImageGenerator> here, othe
reed1
2016/03/10 19:12:56
Agreed. I was removed when I tried switching to un
| |
| 79 | 77 |
| 80 /** | 78 /** |
| 81 * Construct a new SkImage based on the specified encoded data. Returns NUL L on failure, | 79 * Construct a new SkImage based on the specified encoded data. Returns NUL L on failure, |
| 82 * which can mean that the format of the encoded data was not recognized/su pported. | 80 * which can mean that the format of the encoded data was not recognized/su pported. |
| 83 * | 81 * |
| 84 * If a subset is specified, it must be contained within the encoded data's bounds. | 82 * 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 */ | 83 */ |
| 89 static SkImage* NewFromEncoded(SkData* encoded, const SkIRect* subset = NULL ); | 84 static sk_sp<SkImage> MakeFromEncoded(sk_sp<SkData> encoded, const SkIRect* subset = NULL); |
| 90 | 85 |
| 91 /** | 86 /** |
| 92 * Create a new image from the specified descriptor. Note - the caller is r esponsible for | 87 * Create a new image from the specified descriptor. Note - the caller is r esponsible for |
| 93 * managing the lifetime of the underlying platform texture. | 88 * managing the lifetime of the underlying platform texture. |
| 94 * | 89 * |
| 95 * Will return NULL if the specified descriptor is unsupported. | 90 * Will return NULL if the specified descriptor is unsupported. |
| 96 */ | 91 */ |
| 97 static SkImage* NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& d esc) { | 92 static sk_sp<SkImage> MakeFromTexture(GrContext* ctx, const GrBackendTexture Desc& desc) { |
| 98 return NewFromTexture(ctx, desc, kPremul_SkAlphaType, NULL, NULL); | 93 return MakeFromTexture(ctx, desc, kPremul_SkAlphaType, NULL, NULL); |
| 99 } | 94 } |
| 100 | 95 |
| 101 static SkImage* NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& d e, SkAlphaType at) { | 96 static sk_sp<SkImage> MakeFromTexture(GrContext* ctx, const GrBackendTexture Desc& de, |
| 102 return NewFromTexture(ctx, de, at, NULL, NULL); | 97 SkAlphaType at) { |
| 98 return MakeFromTexture(ctx, de, at, NULL, NULL); | |
| 103 } | 99 } |
| 104 | 100 |
| 105 typedef void (*TextureReleaseProc)(ReleaseContext); | 101 typedef void (*TextureReleaseProc)(ReleaseContext); |
| 106 | 102 |
| 107 /** | 103 /** |
| 108 * Create a new image from the specified descriptor. The underlying platfor m texture must stay | 104 * Create a new image from the specified descriptor. The underlying platfor m texture must stay |
| 109 * valid and unaltered until the specified release-proc is invoked, indicat ing that Skia | 105 * valid and unaltered until the specified release-proc is invoked, indicat ing that Skia |
| 110 * no longer is holding a reference to it. | 106 * no longer is holding a reference to it. |
| 111 * | 107 * |
| 112 * Will return NULL if the specified descriptor is unsupported. | 108 * Will return NULL if the specified descriptor is unsupported. |
| 113 */ | 109 */ |
| 114 static SkImage* NewFromTexture(GrContext*, const GrBackendTextureDesc&, SkAl phaType, | 110 static sk_sp<SkImage> MakeFromTexture(GrContext*, const GrBackendTextureDesc &, SkAlphaType, |
| 115 TextureReleaseProc, ReleaseContext); | 111 TextureReleaseProc, ReleaseContext); |
| 116 | 112 |
| 117 /** | 113 /** |
| 118 * Create a new image from the specified descriptor. Note - Skia will delet e or recycle the | 114 * Create a new image from the specified descriptor. Note - Skia will delet e or recycle the |
| 119 * texture when the image is released. | 115 * texture when the image is released. |
| 120 * | 116 * |
| 121 * Will return NULL if the specified descriptor is unsupported. | 117 * Will return NULL if the specified descriptor is unsupported. |
| 122 */ | 118 */ |
| 123 static SkImage* NewFromAdoptedTexture(GrContext*, const GrBackendTextureDesc &, | 119 static sk_sp<SkImage> MakeFromAdoptedTexture(GrContext*, const GrBackendText ureDesc&, |
| 124 SkAlphaType = kPremul_SkAlphaType); | 120 SkAlphaType = kPremul_SkAlphaTy pe); |
| 125 | 121 |
| 126 /** | 122 /** |
| 127 * Create a new image by copying the pixels from the specified descriptor. No reference is | 123 * Create a new image by copying the pixels from the specified descriptor. No reference is |
| 128 * kept to the original platform texture. | 124 * kept to the original platform texture. |
| 129 * | 125 * |
| 130 * Will return NULL if the specified descriptor is unsupported. | 126 * Will return NULL if the specified descriptor is unsupported. |
| 131 */ | 127 */ |
| 132 static SkImage* NewFromTextureCopy(GrContext*, const GrBackendTextureDesc&, | 128 static sk_sp<SkImage> MakeFromTextureCopy(GrContext*, const GrBackendTexture Desc&, |
| 133 SkAlphaType = kPremul_SkAlphaType); | 129 SkAlphaType = kPremul_SkAlphaType) ; |
| 134 | 130 |
| 135 /** | 131 /** |
| 136 * Create a new image by copying the pixels from the specified y, u, v text ures. The data | 132 * Create a new image by copying the pixels from the specified y, u, v text ures. The data |
| 137 * from the textures is immediately ingested into the image and the texture s can be modified or | 133 * from the textures is immediately ingested into the image and the texture s can be modified or |
| 138 * deleted after the function returns. The image will have the dimensions o f the y texture. | 134 * deleted after the function returns. The image will have the dimensions o f the y texture. |
| 139 */ | 135 */ |
| 140 static SkImage* NewFromYUVTexturesCopy(GrContext*, SkYUVColorSpace, | 136 static sk_sp<SkImage> MakeFromYUVTexturesCopy(GrContext*, SkYUVColorSpace, |
| 141 const GrBackendObject yuvTextureHandl es[3], | 137 const GrBackendObject yuvTextu reHandles[3], |
| 142 const SkISize yuvSizes[3], | 138 const SkISize yuvSizes[3], |
| 143 GrSurfaceOrigin); | 139 GrSurfaceOrigin); |
| 144 | 140 |
| 145 static SkImage* NewFromPicture(const SkPicture*, const SkISize& dimensions, | 141 static sk_sp<SkImage> MakeFromPicture(sk_sp<SkPicture>, const SkISize& dimen sions, |
| 146 const SkMatrix*, const SkPaint*); | 142 const SkMatrix*, const SkPaint*); |
| 147 | 143 |
| 148 static SkImage* NewTextureFromPixmap(GrContext*, const SkPixmap&, SkBudgeted budgeted); | 144 static sk_sp<SkImage> MakeTextureFromPixmap(GrContext*, const SkPixmap&, SkB udgeted budgeted); |
| 149 | 145 |
| 150 //////////////////////////////////////////////////////////////////////////// /////////////////// | 146 //////////////////////////////////////////////////////////////////////////// /////////////////// |
| 151 | 147 |
| 152 int width() const { return fWidth; } | 148 int width() const { return fWidth; } |
| 153 int height() const { return fHeight; } | 149 int height() const { return fHeight; } |
| 154 SkISize dimensions() const { return SkISize::Make(fWidth, fHeight); } | 150 SkISize dimensions() const { return SkISize::Make(fWidth, fHeight); } |
| 155 SkIRect bounds() const { return SkIRect::MakeWH(fWidth, fHeight); } | 151 SkIRect bounds() const { return SkIRect::MakeWH(fWidth, fHeight); } |
| 156 uint32_t uniqueID() const { return fUniqueID; } | 152 uint32_t uniqueID() const { return fUniqueID; } |
| 157 virtual bool isOpaque() const { return false; } | 153 virtual bool isOpaque() const { return false; } |
| 158 | 154 |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 308 | 304 |
| 309 const char* toString(SkString*) const; | 305 const char* toString(SkString*) const; |
| 310 | 306 |
| 311 /** | 307 /** |
| 312 * Return a new image that is a subset of this image. The underlying implem entation may | 308 * Return a new image that is a subset of this image. The underlying implem entation may |
| 313 * share the pixels, or it may make a copy. | 309 * share the pixels, or it may make a copy. |
| 314 * | 310 * |
| 315 * If subset does not intersect the bounds of this image, or the copy/share cannot be made, | 311 * If subset does not intersect the bounds of this image, or the copy/share cannot be made, |
| 316 * NULL will be returned. | 312 * NULL will be returned. |
| 317 */ | 313 */ |
| 318 SkImage* newSubset(const SkIRect& subset) const; | 314 sk_sp<SkImage> makeSubset(const SkIRect& subset) const; |
| 319 | 315 |
| 320 /** | 316 /** |
| 321 * Ensures that an image is backed by a texture (when GrContext is non-null ). If no | 317 * Ensures that an image is backed by a texture (when GrContext is non-null ). If no |
| 322 * transformation is required, the returned image may be the same as this i mage. If the this | 318 * transformation is required, the returned image may be the same as this i mage. If the this |
| 323 * image is from a different GrContext, this will fail. | 319 * image is from a different GrContext, this will fail. |
| 324 */ | 320 */ |
| 325 SkImage* newTextureImage(GrContext*) const; | 321 sk_sp<SkImage> makeTextureImage(GrContext*) const; |
| 326 | 322 |
| 327 // Helper functions to convert to SkBitmap | 323 // Helper functions to convert to SkBitmap |
| 328 | 324 |
| 329 enum LegacyBitmapMode { | 325 enum LegacyBitmapMode { |
| 330 kRO_LegacyBitmapMode, | 326 kRO_LegacyBitmapMode, |
| 331 kRW_LegacyBitmapMode, | 327 kRW_LegacyBitmapMode, |
| 332 }; | 328 }; |
| 333 | 329 |
| 334 /** | 330 /** |
| 335 * Attempt to create a bitmap with the same pixels as the image. The result will always be | 331 * Attempt to create a bitmap with the same pixels as the image. The result will always be |
| 336 * a raster-backed bitmap (texture-backed bitmaps are DEPRECATED, and not s upported here). | 332 * a raster-backed bitmap (texture-backed bitmaps are DEPRECATED, and not s upported here). |
| 337 * | 333 * |
| 338 * If the mode is kRO (read-only), the resulting bitmap will be marked as i mmutable. | 334 * If the mode is kRO (read-only), the resulting bitmap will be marked as i mmutable. |
| 339 * | 335 * |
| 340 * On succcess, returns true. On failure, returns false and the bitmap para meter will be reset | 336 * On succcess, returns true. On failure, returns false and the bitmap para meter will be reset |
| 341 * to empty. | 337 * to empty. |
| 342 */ | 338 */ |
| 343 bool asLegacyBitmap(SkBitmap*, LegacyBitmapMode) const; | 339 bool asLegacyBitmap(SkBitmap*, LegacyBitmapMode) const; |
| 344 | 340 |
| 345 /** | 341 /** |
| 346 * Returns true if the image is backed by an image-generator or other src t hat creates | 342 * Returns true if the image is backed by an image-generator or other src t hat creates |
| 347 * (and caches) its pixels / texture on-demand. | 343 * (and caches) its pixels / texture on-demand. |
| 348 */ | 344 */ |
| 349 bool isLazyGenerated() const; | 345 bool isLazyGenerated() const; |
| 350 | 346 |
| 347 | |
| 348 #ifdef SK_SUPPORT_LEGACY_IMAGEFACTORY | |
| 349 static SkImage* NewRasterCopy(const Info&, const void* pixels, size_t rowByt es, | |
| 350 SkColorTable* ctable = nullptr); | |
| 351 static SkImage* NewRasterData(const Info&, SkData* pixels, size_t rowBytes); | |
| 352 static SkImage* NewFromRaster(const Info&, const void* pixels, size_t rowByt es, | |
| 353 RasterReleaseProc, ReleaseContext); | |
| 354 static SkImage* NewFromBitmap(const SkBitmap&); | |
| 355 static SkImage* NewFromGenerator(SkImageGenerator*, const SkIRect* subset = NULL); | |
| 356 static SkImage* NewFromEncoded(SkData* encoded, const SkIRect* subset = NULL ); | |
| 357 static SkImage* NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& d esc) { | |
| 358 return NewFromTexture(ctx, desc, kPremul_SkAlphaType, NULL, NULL); | |
| 359 } | |
| 360 | |
| 361 static SkImage* NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& d e, SkAlphaType at) { | |
| 362 return NewFromTexture(ctx, de, at, NULL, NULL); | |
| 363 } | |
| 364 static SkImage* NewFromTexture(GrContext*, const GrBackendTextureDesc&, SkAl phaType, | |
| 365 TextureReleaseProc, ReleaseContext); | |
| 366 static SkImage* NewFromAdoptedTexture(GrContext*, const GrBackendTextureDesc &, | |
| 367 SkAlphaType = kPremul_SkAlphaType); | |
| 368 static SkImage* NewFromTextureCopy(GrContext*, const GrBackendTextureDesc&, | |
| 369 SkAlphaType = kPremul_SkAlphaType); | |
| 370 static SkImage* NewFromYUVTexturesCopy(GrContext*, SkYUVColorSpace, | |
| 371 const GrBackendObject yuvTextureHandl es[3], | |
| 372 const SkISize yuvSizes[3], | |
| 373 GrSurfaceOrigin); | |
| 374 static SkImage* NewFromPicture(const SkPicture*, const SkISize& dimensions, | |
| 375 const SkMatrix*, const SkPaint*); | |
| 376 static SkImage* NewTextureFromPixmap(GrContext*, const SkPixmap&, SkBudgeted budgeted); | |
| 377 | |
| 378 SkImage* newSubset(const SkIRect& subset) const { return this->makeSubset(su bset).release(); } | |
| 379 SkImage* newTextureImage(GrContext* ctx) const { return this->makeTextureIma ge(ctx).release(); } | |
| 380 #endif | |
| 381 | |
| 351 protected: | 382 protected: |
| 352 SkImage(int width, int height, uint32_t uniqueID); | 383 SkImage(int width, int height, uint32_t uniqueID); |
| 353 | 384 |
| 354 private: | 385 private: |
| 355 const int fWidth; | 386 const int fWidth; |
| 356 const int fHeight; | 387 const int fHeight; |
| 357 const uint32_t fUniqueID; | 388 const uint32_t fUniqueID; |
| 358 | 389 |
| 359 typedef SkRefCnt INHERITED; | 390 typedef SkRefCnt INHERITED; |
| 360 }; | 391 }; |
| 361 | 392 |
| 362 #endif | 393 #endif |
| OLD | NEW |