SkImage.h revision 4af267b11964d4a8acdb232ac46094c84d890e88
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 "SkImageInfo.h" 12#include "SkImageEncoder.h" 13#include "SkRefCnt.h" 14#include "SkScalar.h" 15#include "SkShader.h" 16 17class SkData; 18class SkCanvas; 19class SkImageGenerator; 20class SkPaint; 21class SkSurface; 22class SkSurfaceProps; 23class GrContext; 24class GrTexture; 25 26/** 27 * SkImage is an abstraction for drawing a rectagle of pixels, though the 28 * particular type of image could be actually storing its data on the GPU, or 29 * as drawing commands (picture or PDF or otherwise), ready to be played back 30 * into another canvas. 31 * 32 * The content of SkImage is always immutable, though the actual storage may 33 * change, if for example that image can be re-created via encoded data or 34 * other means. 35 */ 36class SK_API SkImage : public SkRefCnt { 37public: 38 SK_DECLARE_INST_COUNT(SkImage) 39 40 typedef SkImageInfo Info; 41 42 static SkImage* NewRasterCopy(const Info&, const void* pixels, size_t rowBytes); 43 static SkImage* NewRasterData(const Info&, SkData* pixels, size_t rowBytes); 44 45 /** 46 * GrTexture is a more logical parameter for this factory, but its 47 * interactions with scratch cache still has issues, so for now we take 48 * SkBitmap instead. This will be changed in the future. skbug.com/1449 49 */ 50 static SkImage* NewTexture(const SkBitmap&); 51 52 virtual bool isOpaque() const { return false; } 53 54 /** 55 * Construct a new SkImage based on the given ImageGenerator. 56 * This function will always take ownership of the passed 57 * ImageGenerator. Returns NULL on error. 58 */ 59 static SkImage* NewFromGenerator(SkImageGenerator*); 60 61 int width() const { return fWidth; } 62 int height() const { return fHeight; } 63 uint32_t uniqueID() const { return fUniqueID; } 64 65 /** 66 * Return the GrTexture that stores the image pixels. Calling getTexture 67 * does not affect the reference count of the GrTexture object. 68 * Will return NULL if the image does not use a texture. 69 */ 70 GrTexture* getTexture(); 71 72 virtual SkShader* newShader(SkShader::TileMode, 73 SkShader::TileMode, 74 const SkMatrix* localMatrix = NULL) const; 75 76 /** 77 * If the image has direct access to its pixels (i.e. they are in local 78 * RAM) return the (const) address of those pixels, and if not null, return 79 * the ImageInfo and rowBytes. The returned address is only valid while 80 * the image object is in scope. 81 * 82 * On failure, returns NULL and the info and rowBytes parameters are 83 * ignored. 84 */ 85 const void* peekPixels(SkImageInfo* info, size_t* rowBytes) const; 86 87 /** 88 * Encode the image's pixels and return the result as a new SkData, which 89 * the caller must manage (i.e. call unref() when they are done). 90 * 91 * If the image type cannot be encoded, or the requested encoder type is 92 * not supported, this will return NULL. 93 */ 94 SkData* encode(SkImageEncoder::Type t = SkImageEncoder::kPNG_Type, 95 int quality = 80) const; 96 97 /** 98 * Return a new surface that is compatible with this image's internal representation 99 * (e.g. raster or gpu). 100 * 101 * If no surfaceprops are specified, the image will attempt to match the props of when it 102 * was created (if it came from a surface). 103 */ 104 SkSurface* newSurface(const SkImageInfo&, const SkSurfaceProps* = NULL) const; 105 106protected: 107 SkImage(int width, int height) : 108 fWidth(width), 109 fHeight(height), 110 fUniqueID(NextUniqueID()) { 111 112 SkASSERT(width >= 0); 113 SkASSERT(height >= 0); 114 } 115 116private: 117 const int fWidth; 118 const int fHeight; 119 const uint32_t fUniqueID; 120 121 static uint32_t NextUniqueID(); 122 123 typedef SkRefCnt INHERITED; 124 125 friend class SkCanvas; 126 127 void draw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const; 128 129 /** 130 * Draw the image, cropped to the src rect, to the dst rect of a canvas. 131 * If src is larger than the bounds of the image, the rest of the image is 132 * filled with transparent black pixels. 133 * 134 * See SkCanvas::drawBitmapRectToRect for similar behavior. 135 */ 136 void drawRect(SkCanvas*, const SkRect* src, const SkRect& dst, const SkPaint*) const; 137 138 /** 139 * Return a copy of the image's pixels, limiting them to the subset 140 * rectangle's intersection wit the image bounds. If subset is NULL, then 141 * the entire image will be considered. 142 * 143 * If the bitmap's pixels have already been allocated, then readPixels() 144 * will succeed only if it can support converting the image's pixels into 145 * the bitmap's ColorType/AlphaType. Any pixels in the bitmap that do not 146 * intersect with the image's bounds and the subset (if not null) will be 147 * left untouched. 148 * 149 * If the bitmap is initially empty/unallocated, then it will be allocated 150 * using the default allocator, and the ColorType/AlphaType will be chosen 151 * to most closely fit the image's configuration. 152 * 153 * On failure, false will be returned, and bitmap will unmodified. 154 */ 155 // On ice for now: 156 // - should it respect the particular colortype/alphatype of the src 157 // - should it have separate entrypoints for preallocated and not bitmaps? 158 // - isn't it enough to allow the caller to draw() the image into a canvas? 159 bool readPixels(SkBitmap* bitmap, const SkIRect* subset = NULL) const; 160}; 161 162#endif 163