michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef SkImage_DEFINED michael@0: #define SkImage_DEFINED michael@0: michael@0: #include "SkImageInfo.h" michael@0: #include "SkImageEncoder.h" michael@0: #include "SkRefCnt.h" michael@0: #include "SkScalar.h" michael@0: michael@0: class SkData; michael@0: class SkCanvas; michael@0: class SkPaint; michael@0: class SkShader; michael@0: class GrContext; michael@0: class GrTexture; michael@0: michael@0: // need for TileMode michael@0: #include "SkShader.h" michael@0: michael@0: /** michael@0: * SkImage is an abstraction for drawing a rectagle of pixels, though the michael@0: * particular type of image could be actually storing its data on the GPU, or michael@0: * as drawing commands (picture or PDF or otherwise), ready to be played back michael@0: * into another canvas. michael@0: * michael@0: * The content of SkImage is always immutable, though the actual storage may michael@0: * change, if for example that image can be re-created via encoded data or michael@0: * other means. michael@0: */ michael@0: class SK_API SkImage : public SkRefCnt { michael@0: public: michael@0: SK_DECLARE_INST_COUNT(SkImage) michael@0: michael@0: typedef SkImageInfo Info; michael@0: michael@0: static SkImage* NewRasterCopy(const Info&, const void* pixels, size_t rowBytes); michael@0: static SkImage* NewRasterData(const Info&, SkData* pixels, size_t rowBytes); michael@0: static SkImage* NewEncodedData(SkData*); michael@0: michael@0: /** michael@0: * GrTexture is a more logical parameter for this factory, but its michael@0: * interactions with scratch cache still has issues, so for now we take michael@0: * SkBitmap instead. This will be changed in the future. skbug.com/1449 michael@0: */ michael@0: static SkImage* NewTexture(const SkBitmap&); michael@0: michael@0: int width() const { return fWidth; } michael@0: int height() const { return fHeight; } michael@0: uint32_t uniqueID() const { return fUniqueID; } michael@0: michael@0: /** michael@0: * Return the GrTexture that stores the image pixels. Calling getTexture michael@0: * does not affect the reference count of the GrTexture object. michael@0: * Will return NULL if the image does not use a texture. michael@0: */ michael@0: GrTexture* getTexture(); michael@0: michael@0: SkShader* newShaderClamp() const; michael@0: SkShader* newShader(SkShader::TileMode, SkShader::TileMode) const; michael@0: michael@0: void draw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*); michael@0: michael@0: /** michael@0: * Draw the image, cropped to the src rect, to the dst rect of a canvas. michael@0: * If src is larger than the bounds of the image, the rest of the image is michael@0: * filled with transparent black pixels. michael@0: * michael@0: * See SkCanvas::drawBitmapRectToRect for similar behavior. michael@0: */ michael@0: void draw(SkCanvas*, const SkRect* src, const SkRect& dst, const SkPaint*); michael@0: michael@0: /** michael@0: * If the image has direct access to its pixels (i.e. they are in local michael@0: * RAM) return the (const) address of those pixels, and if not null, return michael@0: * the ImageInfo and rowBytes. The returned address is only valid while michael@0: * the image object is in scope. michael@0: * michael@0: * On failure, returns NULL and the info and rowBytes parameters are michael@0: * ignored. michael@0: */ michael@0: const void* peekPixels(SkImageInfo* info, size_t* rowBytes) const; michael@0: michael@0: /** michael@0: * Encode the image's pixels and return the result as a new SkData, which michael@0: * the caller must manage (i.e. call unref() when they are done). michael@0: * michael@0: * If the image type cannot be encoded, or the requested encoder type is michael@0: * not supported, this will return NULL. michael@0: */ michael@0: SkData* encode(SkImageEncoder::Type t = SkImageEncoder::kPNG_Type, michael@0: int quality = 80) const; michael@0: michael@0: protected: michael@0: SkImage(int width, int height) : michael@0: fWidth(width), michael@0: fHeight(height), michael@0: fUniqueID(NextUniqueID()) { michael@0: michael@0: SkASSERT(width >= 0); michael@0: SkASSERT(height >= 0); michael@0: } michael@0: michael@0: private: michael@0: const int fWidth; michael@0: const int fHeight; michael@0: const uint32_t fUniqueID; michael@0: michael@0: static uint32_t NextUniqueID(); michael@0: michael@0: typedef SkRefCnt INHERITED; michael@0: michael@0: /** michael@0: * Return a copy of the image's pixels, limiting them to the subset michael@0: * rectangle's intersection wit the image bounds. If subset is NULL, then michael@0: * the entire image will be considered. michael@0: * michael@0: * If the bitmap's pixels have already been allocated, then readPixels() michael@0: * will succeed only if it can support converting the image's pixels into michael@0: * the bitmap's ColorType/AlphaType. Any pixels in the bitmap that do not michael@0: * intersect with the image's bounds and the subset (if not null) will be michael@0: * left untouched. michael@0: * michael@0: * If the bitmap is initially empty/unallocated, then it will be allocated michael@0: * using the default allocator, and the ColorType/AlphaType will be chosen michael@0: * to most closely fit the image's configuration. michael@0: * michael@0: * On failure, false will be returned, and bitmap will unmodified. michael@0: */ michael@0: // On ice for now: michael@0: // - should it respect the particular colortype/alphatype of the src michael@0: // - should it have separate entrypoints for preallocated and not bitmaps? michael@0: // - isn't it enough to allow the caller to draw() the image into a canvas? michael@0: bool readPixels(SkBitmap* bitmap, const SkIRect* subset = NULL) const; michael@0: }; michael@0: michael@0: #endif