2012-07-25 14:42:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkImage_DEFINED
|
|
|
|
#define SkImage_DEFINED
|
|
|
|
|
2015-01-23 13:58:07 +00:00
|
|
|
#include "SkFilterQuality.h"
|
2013-11-13 19:09:13 +00:00
|
|
|
#include "SkImageInfo.h"
|
2013-05-31 14:00:10 +00:00
|
|
|
#include "SkImageEncoder.h"
|
2012-07-27 18:02:50 +00:00
|
|
|
#include "SkRefCnt.h"
|
|
|
|
#include "SkScalar.h"
|
2014-07-14 14:48:04 +00:00
|
|
|
#include "SkShader.h"
|
2012-07-27 18:02:50 +00:00
|
|
|
|
|
|
|
class SkData;
|
|
|
|
class SkCanvas;
|
2015-07-04 04:01:10 +00:00
|
|
|
class SkColorTable;
|
2014-08-18 15:27:09 +00:00
|
|
|
class SkImageGenerator;
|
2012-07-27 18:02:50 +00:00
|
|
|
class SkPaint;
|
2015-08-13 16:37:45 +00:00
|
|
|
class SkPicture;
|
2015-09-03 14:17:25 +00:00
|
|
|
class SkPixelSerializer;
|
2015-01-05 15:49:08 +00:00
|
|
|
class SkString;
|
2014-11-21 16:46:37 +00:00
|
|
|
class SkSurface;
|
2012-07-31 15:45:27 +00:00
|
|
|
class GrContext;
|
2012-10-31 14:58:16 +00:00
|
|
|
class GrTexture;
|
2012-07-27 18:02:50 +00:00
|
|
|
|
2012-07-25 14:42:15 +00:00
|
|
|
/**
|
|
|
|
* SkImage is an abstraction for drawing a rectagle of pixels, though the
|
|
|
|
* particular type of image could be actually storing its data on the GPU, or
|
|
|
|
* as drawing commands (picture or PDF or otherwise), ready to be played back
|
|
|
|
* into another canvas.
|
|
|
|
*
|
|
|
|
* The content of SkImage is always immutable, though the actual storage may
|
|
|
|
* change, if for example that image can be re-created via encoded data or
|
|
|
|
* other means.
|
2014-12-31 20:31:43 +00:00
|
|
|
*
|
|
|
|
* SkImage always has a non-zero dimensions. If there is a request to create a new image, either
|
|
|
|
* directly or via SkSurface, and either of the requested dimensions are zero, then NULL will be
|
|
|
|
* returned.
|
2012-07-25 14:42:15 +00:00
|
|
|
*/
|
2013-04-18 13:28:19 +00:00
|
|
|
class SK_API SkImage : public SkRefCnt {
|
2012-07-25 14:42:15 +00:00
|
|
|
public:
|
2013-11-01 13:46:54 +00:00
|
|
|
typedef SkImageInfo Info;
|
2015-06-18 20:41:40 +00:00
|
|
|
typedef void* ReleaseContext;
|
2012-07-25 14:42:15 +00:00
|
|
|
|
2015-07-04 04:01:10 +00:00
|
|
|
static SkImage* NewRasterCopy(const Info&, const void* pixels, size_t rowBytes,
|
|
|
|
SkColorTable* ctable = NULL);
|
2012-11-15 02:37:45 +00:00
|
|
|
static SkImage* NewRasterData(const Info&, SkData* pixels, size_t rowBytes);
|
2013-07-25 23:29:40 +00:00
|
|
|
|
2015-06-18 20:41:40 +00:00
|
|
|
typedef void (*RasterReleaseProc)(const void* pixels, ReleaseContext);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a new Image referencing the specified pixels. These must remain valid and unchanged
|
|
|
|
* until the specified release-proc is called, indicating that Skia no longer has a reference
|
|
|
|
* to the pixels.
|
|
|
|
*
|
|
|
|
* Returns NULL if the requested Info is unsupported.
|
|
|
|
*/
|
|
|
|
static SkImage* NewFromRaster(const Info&, const void* pixels, size_t rowBytes,
|
|
|
|
RasterReleaseProc, ReleaseContext);
|
|
|
|
|
2015-07-07 13:11:19 +00:00
|
|
|
/**
|
|
|
|
* Construct a new image from the specified bitmap. If the bitmap is marked immutable, and
|
|
|
|
* its pixel memory is shareable, it may be shared instead of copied.
|
|
|
|
*/
|
|
|
|
static SkImage* NewFromBitmap(const SkBitmap&);
|
|
|
|
|
2014-08-18 15:27:09 +00:00
|
|
|
/**
|
|
|
|
* Construct a new SkImage based on the given ImageGenerator.
|
|
|
|
* This function will always take ownership of the passed
|
|
|
|
* ImageGenerator. Returns NULL on error.
|
2015-06-22 19:48:26 +00:00
|
|
|
*
|
|
|
|
* If a subset is specified, it must be contained within the generator's bounds.
|
2014-08-18 15:27:09 +00:00
|
|
|
*/
|
2015-06-22 19:48:26 +00:00
|
|
|
static SkImage* NewFromGenerator(SkImageGenerator*, const SkIRect* subset = NULL);
|
2014-08-18 15:27:09 +00:00
|
|
|
|
2015-01-08 02:04:45 +00:00
|
|
|
/**
|
|
|
|
* Construct a new SkImage based on the specified encoded data. Returns NULL on failure,
|
|
|
|
* which can mean that the format of the encoded data was not recognized/supported.
|
|
|
|
*
|
2015-06-22 19:48:26 +00:00
|
|
|
* If a subset is specified, it must be contained within the encoded data's bounds.
|
|
|
|
*
|
2015-01-08 02:04:45 +00:00
|
|
|
* Regardless of success or failure, the caller is responsible for managing their ownership
|
|
|
|
* of the data.
|
|
|
|
*/
|
2015-06-22 19:48:26 +00:00
|
|
|
static SkImage* NewFromEncoded(SkData* encoded, const SkIRect* subset = NULL);
|
|
|
|
|
2015-05-07 22:36:17 +00:00
|
|
|
/**
|
|
|
|
* Create a new image from the specified descriptor. Note - the caller is responsible for
|
|
|
|
* managing the lifetime of the underlying platform texture.
|
|
|
|
*
|
|
|
|
* Will return NULL if the specified descriptor is unsupported.
|
|
|
|
*/
|
2015-06-18 20:41:40 +00:00
|
|
|
static SkImage* NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& desc) {
|
|
|
|
return NewFromTexture(ctx, desc, kPremul_SkAlphaType, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkImage* NewFromTexture(GrContext* ctx, const GrBackendTextureDesc& de, SkAlphaType at) {
|
|
|
|
return NewFromTexture(ctx, de, at, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef void (*TextureReleaseProc)(ReleaseContext);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new image from the specified descriptor. The underlying platform texture must stay
|
|
|
|
* valid and unaltered until the specified release-proc is invoked, indicating that Skia
|
2015-07-23 19:22:19 +00:00
|
|
|
* no longer is holding a reference to it.
|
2015-06-18 20:41:40 +00:00
|
|
|
*
|
|
|
|
* Will return NULL if the specified descriptor is unsupported.
|
|
|
|
*/
|
|
|
|
static SkImage* NewFromTexture(GrContext*, const GrBackendTextureDesc&, SkAlphaType,
|
|
|
|
TextureReleaseProc, ReleaseContext);
|
2015-05-07 22:36:17 +00:00
|
|
|
|
2015-06-18 16:12:16 +00:00
|
|
|
/**
|
|
|
|
* Create a new image from the specified descriptor. Note - Skia will delete or recycle the
|
|
|
|
* texture when the image is released.
|
|
|
|
*
|
|
|
|
* Will return NULL if the specified descriptor is unsupported.
|
|
|
|
*/
|
|
|
|
static SkImage* NewFromAdoptedTexture(GrContext*, const GrBackendTextureDesc&,
|
|
|
|
SkAlphaType = kPremul_SkAlphaType);
|
|
|
|
|
2015-05-07 22:36:17 +00:00
|
|
|
/**
|
|
|
|
* Create a new image by copying the pixels from the specified descriptor. No reference is
|
|
|
|
* kept to the original platform texture.
|
|
|
|
*
|
|
|
|
* Will return NULL if the specified descriptor is unsupported.
|
|
|
|
*/
|
|
|
|
static SkImage* NewFromTextureCopy(GrContext*, const GrBackendTextureDesc&,
|
|
|
|
SkAlphaType = kPremul_SkAlphaType);
|
|
|
|
|
2015-05-29 18:37:25 +00:00
|
|
|
/**
|
|
|
|
* Create a new image by copying the pixels from the specified y, u, v textures. The data
|
|
|
|
* from the textures is immediately ingested into the image and the textures can be modified or
|
|
|
|
* deleted after the function returns. The image will have the dimensions of the y texture.
|
|
|
|
*/
|
|
|
|
static SkImage* NewFromYUVTexturesCopy(GrContext*, SkYUVColorSpace,
|
|
|
|
const GrBackendObject yuvTextureHandles[3],
|
|
|
|
const SkISize yuvSizes[3],
|
|
|
|
GrSurfaceOrigin);
|
|
|
|
|
2015-08-13 16:37:45 +00:00
|
|
|
static SkImage* NewFromPicture(const SkPicture*, const SkISize& dimensions,
|
|
|
|
const SkMatrix*, const SkPaint*);
|
|
|
|
|
2016-03-09 19:31:18 +00:00
|
|
|
static SkImage* NewTextureFromPixmap(GrContext*, const SkPixmap&, SkBudgeted budgeted);
|
|
|
|
|
2015-08-13 16:37:45 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-07-27 18:02:50 +00:00
|
|
|
int width() const { return fWidth; }
|
|
|
|
int height() const { return fHeight; }
|
2015-09-10 21:33:38 +00:00
|
|
|
SkISize dimensions() const { return SkISize::Make(fWidth, fHeight); }
|
|
|
|
SkIRect bounds() const { return SkIRect::MakeWH(fWidth, fHeight); }
|
2012-07-27 18:02:50 +00:00
|
|
|
uint32_t uniqueID() const { return fUniqueID; }
|
2015-01-13 12:00:55 +00:00
|
|
|
virtual bool isOpaque() const { return false; }
|
2012-07-27 18:02:50 +00:00
|
|
|
|
2016-02-01 21:16:14 +00:00
|
|
|
/**
|
|
|
|
* Extracts YUV planes from the SkImage and stores them in client-provided memory. The sizes
|
|
|
|
* planes and rowBytes arrays are ordered [y, u, v].
|
|
|
|
*/
|
|
|
|
bool readYUV8Planes(const SkISize[3], void* const planes[3], const size_t rowBytes[3],
|
2016-02-03 22:09:00 +00:00
|
|
|
SkYUVColorSpace) const;
|
2016-02-01 21:16:14 +00:00
|
|
|
|
2014-07-22 22:02:05 +00:00
|
|
|
virtual SkShader* newShader(SkShader::TileMode,
|
|
|
|
SkShader::TileMode,
|
|
|
|
const SkMatrix* localMatrix = NULL) const;
|
2012-07-27 18:02:50 +00:00
|
|
|
|
2014-02-06 14:11:56 +00:00
|
|
|
/**
|
|
|
|
* If the image has direct access to its pixels (i.e. they are in local
|
|
|
|
* RAM) return the (const) address of those pixels, and if not null, return
|
|
|
|
* the ImageInfo and rowBytes. The returned address is only valid while
|
|
|
|
* the image object is in scope.
|
|
|
|
*
|
|
|
|
* On failure, returns NULL and the info and rowBytes parameters are
|
|
|
|
* ignored.
|
|
|
|
*/
|
|
|
|
const void* peekPixels(SkImageInfo* info, size_t* rowBytes) const;
|
2014-02-07 12:20:04 +00:00
|
|
|
|
2015-06-22 19:48:26 +00:00
|
|
|
/**
|
|
|
|
* If the image has direct access to its pixels (i.e. they are in local
|
|
|
|
* RAM) return the (const) address of those pixels, and if not null, return
|
|
|
|
* true, and if pixmap is not NULL, set it to point into the image.
|
|
|
|
*
|
|
|
|
* On failure, return false and ignore the pixmap parameter.
|
|
|
|
*/
|
|
|
|
bool peekPixels(SkPixmap* pixmap) const;
|
|
|
|
|
2015-09-01 19:22:32 +00:00
|
|
|
/**
|
|
|
|
* Some images have to perform preliminary work in preparation for drawing. This can be
|
|
|
|
* decoding, uploading to a GPU, or other tasks. These happen automatically when an image
|
|
|
|
* is drawn, and often they are cached so that the cost is only paid the first time.
|
|
|
|
*
|
|
|
|
* Preroll() can be called before drawing to try to perform this prepatory work ahead of time.
|
|
|
|
* For images that have no such work, this returns instantly. Others may do some thing to
|
|
|
|
* prepare their cache and then return.
|
|
|
|
*
|
|
|
|
* If the image will drawn to a GPU-backed canvas or surface, pass the associated GrContext.
|
|
|
|
* If the image will be drawn to any other type of canvas or surface, pass null.
|
|
|
|
*/
|
|
|
|
void preroll(GrContext* = nullptr) const;
|
|
|
|
|
2016-02-29 19:41:52 +00:00
|
|
|
// DEPRECATED - currently used by Canvas2DLayerBridge in Chromium.
|
2015-06-10 15:49:28 +00:00
|
|
|
GrTexture* getTexture() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the image is texture backed.
|
|
|
|
*/
|
|
|
|
bool isTextureBacked() const;
|
|
|
|
|
2015-06-26 18:45:03 +00:00
|
|
|
/**
|
2015-06-30 18:37:35 +00:00
|
|
|
* Retrieves the backend API handle of the texture. If flushPendingGrContextIO then the
|
|
|
|
* GrContext will issue to the backend API any deferred IO operations on the texture before
|
2015-06-10 15:49:28 +00:00
|
|
|
* returning.
|
|
|
|
*/
|
2015-06-30 18:37:35 +00:00
|
|
|
GrBackendObject getTextureHandle(bool flushPendingGrContextIO) const;
|
2015-06-10 15:49:28 +00:00
|
|
|
|
2015-11-23 20:32:16 +00:00
|
|
|
/**
|
|
|
|
* Hints to image calls where the system might cache computed intermediates (e.g. the results
|
|
|
|
* of decoding or a read-back from the GPU. Passing kAllow signals that the system's default
|
|
|
|
* behavior is fine. Passing kDisallow signals that caching should be avoided.
|
|
|
|
*/
|
|
|
|
enum CachingHint {
|
|
|
|
kAllow_CachingHint,
|
|
|
|
kDisallow_CachingHint,
|
|
|
|
};
|
|
|
|
|
2014-12-10 17:53:42 +00:00
|
|
|
/**
|
|
|
|
* Copy the pixels from the image into the specified buffer (pixels + rowBytes),
|
|
|
|
* converting them into the requested format (dstInfo). The image pixels are read
|
|
|
|
* starting at the specified (srcX,srcY) location.
|
|
|
|
*
|
|
|
|
* The specified ImageInfo and (srcX,srcY) offset specifies a source rectangle
|
|
|
|
*
|
|
|
|
* srcR.setXYWH(srcX, srcY, dstInfo.width(), dstInfo.height());
|
|
|
|
*
|
|
|
|
* srcR is intersected with the bounds of the image. If this intersection is not empty,
|
|
|
|
* then we have two sets of pixels (of equal size). Replace the dst pixels with the
|
|
|
|
* corresponding src pixels, performing any colortype/alphatype transformations needed
|
|
|
|
* (in the case where the src and dst have different colortypes or alphatypes).
|
|
|
|
*
|
|
|
|
* This call can fail, returning false, for several reasons:
|
|
|
|
* - If srcR does not intersect the image bounds.
|
|
|
|
* - If the requested colortype/alphatype cannot be converted from the image's types.
|
|
|
|
*/
|
|
|
|
bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
|
2015-11-23 20:32:16 +00:00
|
|
|
int srcX, int srcY, CachingHint = kAllow_CachingHint) const;
|
|
|
|
|
|
|
|
bool readPixels(const SkPixmap& dst, int srcX, int srcY,
|
|
|
|
CachingHint = kAllow_CachingHint) const;
|
2014-12-10 17:53:42 +00:00
|
|
|
|
2015-11-23 20:32:16 +00:00
|
|
|
/**
|
|
|
|
* Copy the pixels from this image into the dst pixmap, converting as needed into dst's
|
|
|
|
* colortype/alphatype. If the conversion cannot be performed, false is returned.
|
|
|
|
*
|
|
|
|
* If dst's dimensions differ from the src dimension, the image will be scaled, applying the
|
|
|
|
* specified filter-quality.
|
|
|
|
*/
|
|
|
|
bool scalePixels(const SkPixmap& dst, SkFilterQuality, CachingHint = kAllow_CachingHint) const;
|
2015-06-22 19:48:26 +00:00
|
|
|
|
2013-05-20 16:33:41 +00:00
|
|
|
/**
|
|
|
|
* Encode the image's pixels and return the result as a new SkData, which
|
|
|
|
* the caller must manage (i.e. call unref() when they are done).
|
|
|
|
*
|
|
|
|
* If the image type cannot be encoded, or the requested encoder type is
|
|
|
|
* not supported, this will return NULL.
|
2015-09-03 14:17:25 +00:00
|
|
|
*
|
|
|
|
* Note: this will attempt to encode the image's pixels in the specified format,
|
|
|
|
* even if the image returns a data from refEncoded(). That data will be ignored.
|
2013-05-20 16:33:41 +00:00
|
|
|
*/
|
2015-06-22 19:48:26 +00:00
|
|
|
SkData* encode(SkImageEncoder::Type, int quality) const;
|
|
|
|
|
2015-09-03 14:17:25 +00:00
|
|
|
/**
|
|
|
|
* Encode the image and return the result as a caller-managed SkData. This will
|
|
|
|
* attempt to reuse existing encoded data (as returned by refEncoded).
|
|
|
|
*
|
|
|
|
* We defer to the SkPixelSerializer both for vetting existing encoded data
|
2015-12-07 20:42:24 +00:00
|
|
|
* (useEncodedData) and for encoding the image (encode) when no such data is
|
2015-09-03 14:17:25 +00:00
|
|
|
* present or is rejected by the serializer.
|
|
|
|
*
|
|
|
|
* If not specified, we use a default serializer which 1) always accepts existing data
|
|
|
|
* (in any format) and 2) encodes to PNG.
|
|
|
|
*
|
|
|
|
* If no compatible encoded data exists and encoding fails, this method will also
|
|
|
|
* fail (return NULL).
|
|
|
|
*/
|
|
|
|
SkData* encode(SkPixelSerializer* = nullptr) const;
|
2015-06-22 19:48:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If the image already has its contents in encoded form (e.g. PNG or JPEG), return a ref
|
|
|
|
* to that data (which the caller must call unref() on). The caller is responsible for calling
|
|
|
|
* unref on the data when they are done.
|
|
|
|
*
|
|
|
|
* If the image does not already has its contents in encoded form, return NULL.
|
|
|
|
*
|
|
|
|
* Note: to force the image to return its contents as encoded data, try calling encode(...).
|
|
|
|
*/
|
|
|
|
SkData* refEncoded() const;
|
2013-05-20 16:33:41 +00:00
|
|
|
|
2015-01-03 04:45:37 +00:00
|
|
|
const char* toString(SkString*) const;
|
|
|
|
|
2015-09-24 07:50:58 +00:00
|
|
|
/**
|
|
|
|
* Return a new image that is a subset of this image. The underlying implementation may
|
|
|
|
* share the pixels, or it may make a copy.
|
|
|
|
*
|
|
|
|
* If subset does not intersect the bounds of this image, or the copy/share cannot be made,
|
|
|
|
* NULL will be returned.
|
|
|
|
*/
|
|
|
|
SkImage* newSubset(const SkIRect& subset) const;
|
2015-01-23 13:58:07 +00:00
|
|
|
|
2016-01-30 18:01:40 +00:00
|
|
|
/**
|
|
|
|
* Ensures that an image is backed by a texture (when GrContext is non-null). If no
|
|
|
|
* transformation is required, the returned image may be the same as this image. If the this
|
|
|
|
* image is from a different GrContext, this will fail.
|
|
|
|
*/
|
|
|
|
SkImage* newTextureImage(GrContext*) const;
|
|
|
|
|
2015-07-08 19:46:22 +00:00
|
|
|
// Helper functions to convert to SkBitmap
|
|
|
|
|
|
|
|
enum LegacyBitmapMode {
|
|
|
|
kRO_LegacyBitmapMode,
|
|
|
|
kRW_LegacyBitmapMode,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to create a bitmap with the same pixels as the image. The result will always be
|
|
|
|
* a raster-backed bitmap (texture-backed bitmaps are DEPRECATED, and not supported here).
|
|
|
|
*
|
|
|
|
* If the mode is kRO (read-only), the resulting bitmap will be marked as immutable.
|
|
|
|
*
|
|
|
|
* On succcess, returns true. On failure, returns false and the bitmap parameter will be reset
|
|
|
|
* to empty.
|
|
|
|
*/
|
|
|
|
bool asLegacyBitmap(SkBitmap*, LegacyBitmapMode) const;
|
|
|
|
|
2015-08-20 15:47:26 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the image is backed by an image-generator or other src that creates
|
|
|
|
* (and caches) its pixels / texture on-demand.
|
|
|
|
*/
|
|
|
|
bool isLazyGenerated() const;
|
|
|
|
|
2012-07-27 18:02:50 +00:00
|
|
|
protected:
|
2015-07-31 01:58:23 +00:00
|
|
|
SkImage(int width, int height, uint32_t uniqueID);
|
2012-07-27 18:02:50 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
const int fWidth;
|
|
|
|
const int fHeight;
|
|
|
|
const uint32_t fUniqueID;
|
|
|
|
|
2012-08-28 12:48:35 +00:00
|
|
|
typedef SkRefCnt INHERITED;
|
2012-07-25 14:42:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|