SkImage.h revision 485e1c01281f5c3632dc47d407ed8c7c86f83a74
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 "SkAlpha.h" 12#include "SkImageEncoder.h" 13#include "SkRefCnt.h" 14#include "SkScalar.h" 15 16class SkData; 17class SkCanvas; 18class SkPaint; 19class SkShader; 20class GrContext; 21class GrTexture; 22 23// need for TileMode 24#include "SkShader.h" 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 enum ColorType { 41 kAlpha_8_ColorType, 42 kRGB_565_ColorType, 43 kRGBA_8888_ColorType, 44 kBGRA_8888_ColorType, 45 46#if SK_PMCOLOR_BYTE_ORDER(B,G,R,A) 47 kPMColor_ColorType = kBGRA_8888_ColorType, 48#elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A) 49 kPMColor_ColorType = kRGBA_8888_ColorType, 50#else 51 #error "SK_*32_SHFIT values must correspond to BGRA or RGBA byte order" 52#endif 53 54 kLastEnum_ColorType = kBGRA_8888_ColorType 55 }; 56 57 typedef SkAlphaType AlphaType; 58 59 static const SkAlphaType kIgnore_AlphaType = kIgnore_SkAlphaType; 60 static const SkAlphaType kOpaque_AlphaType = kOpaque_SkAlphaType; 61 static const SkAlphaType kPremul_AlphaType = kPremul_SkAlphaType; 62 static const SkAlphaType kUnpremul_AlphaType = kUnpremul_SkAlphaType; 63 64 struct Info { 65 int fWidth; 66 int fHeight; 67 ColorType fColorType; 68 SkAlphaType fAlphaType; 69 }; 70 71 static SkImage* NewRasterCopy(const Info&, const void* pixels, size_t rowBytes); 72 static SkImage* NewRasterData(const Info&, SkData* pixels, size_t rowBytes); 73 static SkImage* NewEncodedData(SkData*); 74 75 /** 76 * GrTexture is a more logical parameter for this factory, but its 77 * interactions with scratch cache still has issues, so for now we take 78 * SkBitmap instead. This will be changed in the future. skbug.com/1449 79 */ 80 static SkImage* NewTexture(const SkBitmap&); 81 82 int width() const { return fWidth; } 83 int height() const { return fHeight; } 84 uint32_t uniqueID() const { return fUniqueID; } 85 86 /** 87 * Return the GrTexture that stores the image pixels. Calling getTexture 88 * does not affect the reference count of the GrTexture object. 89 * Will return NULL if the image does not use a texture. 90 */ 91 GrTexture* getTexture(); 92 93 SkShader* newShaderClamp() const; 94 SkShader* newShader(SkShader::TileMode, SkShader::TileMode) const; 95 96 void draw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*); 97 98 /** 99 * Draw the image, cropped to the src rect, to the dst rect of a canvas. 100 * If src is larger than the bounds of the image, the rest of the image is 101 * filled with transparent black pixels. 102 * 103 * See SkCanvas::drawBitmapRectToRect for similar behavior. 104 */ 105 void draw(SkCanvas*, const SkRect* src, const SkRect& dst, const SkPaint*); 106 107 /** 108 * Encode the image's pixels and return the result as a new SkData, which 109 * the caller must manage (i.e. call unref() when they are done). 110 * 111 * If the image type cannot be encoded, or the requested encoder type is 112 * not supported, this will return NULL. 113 */ 114 SkData* encode(SkImageEncoder::Type t = SkImageEncoder::kPNG_Type, 115 int quality = 80) const; 116 117protected: 118 SkImage(int width, int height) : 119 fWidth(width), 120 fHeight(height), 121 fUniqueID(NextUniqueID()) { 122 123 SkASSERT(width >= 0); 124 SkASSERT(height >= 0); 125 } 126 127private: 128 const int fWidth; 129 const int fHeight; 130 const uint32_t fUniqueID; 131 132 static uint32_t NextUniqueID(); 133 134 typedef SkRefCnt INHERITED; 135}; 136 137#endif 138