SkGr.h revision 137a4ca42423bbb6d683067ea544c9a48f18f06c
1/* 2 Copyright 2010 Google Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 18#ifndef SkGr_DEFINED 19#define SkGr_DEFINED 20 21#include <stddef.h> 22 23// Gr headers 24#include "GrConfig.h" 25#include "GrContext.h" 26#include "GrFontScaler.h" 27#include "GrPathIter.h" 28#include "GrClipIterator.h" 29 30// skia headers 31#include "SkBitmap.h" 32#include "SkPath.h" 33#include "SkPoint.h" 34#include "SkRegion.h" 35#include "SkShader.h" 36#include "SkClipStack.h" 37 38#if (GR_DEBUG && defined(SK_RELEASE)) || (GR_RELEASE && defined(SK_DEBUG)) 39// #error "inconsistent GR_DEBUG and SK_DEBUG" 40#endif 41 42#if GR_SCALAR_IS_FIXED 43 #ifdef SK_SCALAR_IS_FIXED 44 #define SK_SCALAR_IS_GR_SCALAR 1 45 #else 46 #define SK_SCALAR_IS_GR_SCALAR 0 47 #endif 48 #define SkScalarToGrScalar(x) SkScalarToFixed(x) 49 50#elif GR_SCALAR_IS_FLOAT 51 52 #ifdef SK_SCALAR_IS_FLOAT 53 #define SK_SCALAR_IS_GR_SCALAR 1 54 #else 55 #define SK_SCALAR_IS_GR_SCALAR 0 56 #endif 57 #define SkScalarToGrScalar(x) SkScalarToFloat(x) 58 59#else 60 #error "Ganesh scalar type not defined" 61#endif 62 63//////////////////////////////////////////////////////////////////////////////// 64// Sk to Gr Type conversions 65 66// Verify that SkPoint and GrPoint are compatible if using the same scalar type 67#if 0/*SK_SCALAR_IS_GR_SCALAR*/ 68 GR_STATIC_ASSERT(sizeof(SkPoint) == sizeof(GrPoint)); 69 GR_STATIC_ASSERT(offsetof(SkPoint,fX) == offsetof(GrPoint,fX))); 70 GR_STATIC_ASSERT(offsetof(SkPoint,fY) == offsetof(GrPoint,fY))); 71#endif 72 73GR_STATIC_ASSERT((int)GrSamplerState::kClamp_WrapMode == (int)SkShader::kClamp_TileMode); 74GR_STATIC_ASSERT((int)GrSamplerState::kRepeat_WrapMode ==( 75 int)SkShader::kRepeat_TileMode); 76GR_STATIC_ASSERT((int)GrSamplerState::kMirror_WrapMode == 77 (int)SkShader::kMirror_TileMode); 78 79#define sk_tile_mode_to_grwrap(X) ((GrSamplerState::WrapMode)(X)) 80 81GR_STATIC_ASSERT((int)kZero_BlendCoeff == (int)SkXfermode::kZero_Coeff); 82GR_STATIC_ASSERT((int)kOne_BlendCoeff == (int)SkXfermode::kOne_Coeff); 83GR_STATIC_ASSERT((int)kSC_BlendCoeff == (int)SkXfermode::kSC_Coeff); 84GR_STATIC_ASSERT((int)kISC_BlendCoeff == (int)SkXfermode::kISC_Coeff); 85GR_STATIC_ASSERT((int)kDC_BlendCoeff == (int)SkXfermode::kDC_Coeff); 86GR_STATIC_ASSERT((int)kIDC_BlendCoeff == (int)SkXfermode::kIDC_Coeff); 87GR_STATIC_ASSERT((int)kSA_BlendCoeff == (int)SkXfermode::kSA_Coeff); 88GR_STATIC_ASSERT((int)kISA_BlendCoeff == (int)SkXfermode::kISA_Coeff); 89GR_STATIC_ASSERT((int)kDA_BlendCoeff == (int)SkXfermode::kDA_Coeff); 90GR_STATIC_ASSERT((int)kIDA_BlendCoeff == (int)SkXfermode::kIDA_Coeff); 91 92#define sk_blend_to_grblend(X) ((GrBlendCoeff)(X)) 93 94GR_STATIC_ASSERT((int)SkPath::kMove_Verb == (int)kMove_PathCmd); 95GR_STATIC_ASSERT((int)SkPath::kLine_Verb == (int)kLine_PathCmd); 96GR_STATIC_ASSERT((int)SkPath::kQuad_Verb == (int)kQuadratic_PathCmd); 97GR_STATIC_ASSERT((int)SkPath::kCubic_Verb == (int)kCubic_PathCmd); 98GR_STATIC_ASSERT((int)SkPath::kClose_Verb == (int)kClose_PathCmd); 99GR_STATIC_ASSERT((int)SkPath::kDone_Verb == (int)kEnd_PathCmd); 100 101#define sk_path_verb_to_gr_path_command(X) ((GrPathCmd)(X)) 102 103/////////////////////////////////////////////////////////////////////////////// 104 105#include "SkColorPriv.h" 106 107static inline GrRect Sk2Gr(const SkRect& src) { 108 return GrRect(SkScalarToGrScalar(src.fLeft), 109 SkScalarToGrScalar(src.fTop), 110 SkScalarToGrScalar(src.fRight), 111 SkScalarToGrScalar(src.fBottom)); 112} 113 114class SkGr { 115public: 116 static inline SkIRect& SetIRect(SkIRect* dst, const GrIRect& src) { 117 GR_STATIC_ASSERT(sizeof(*dst) == sizeof(src)); 118 memcpy(dst, &src, sizeof(*dst)); 119 return *dst; 120 } 121 122 static inline GrIRect& SetIRect(GrIRect* dst, const SkIRect& src) { 123 GR_STATIC_ASSERT(sizeof(*dst) == sizeof(src)); 124 memcpy(dst, &src, sizeof(*dst)); 125 return *dst; 126 } 127 128 /** 129 * Convert the SkBitmap::Config to the corresponding PixelConfig, or 130 * kUnknown_PixelConfig if the conversion cannot be done. 131 */ 132 static GrTexture::PixelConfig BitmapConfig2PixelConfig(SkBitmap::Config, 133 bool isOpaque); 134 135 static GrTexture::PixelConfig Bitmap2PixelConfig(const SkBitmap& bm) { 136 return BitmapConfig2PixelConfig(bm.config(), bm.isOpaque()); 137 } 138 139 static void SkMatrix2GrMatrix(const SkMatrix& m, GrMatrix* g) { 140 g->setAll(SkScalarToGrScalar(m[0]), 141 SkScalarToGrScalar(m[1]), 142 SkScalarToGrScalar(m[2]), 143 SkScalarToGrScalar(m[3]), 144 SkScalarToGrScalar(m[4]), 145 SkScalarToGrScalar(m[5]), 146 SkScalarToGrScalar(m[6]), 147 SkScalarToGrScalar(m[7]), 148 SkScalarToGrScalar(m[8])); 149 } 150 151 static GrColor SkColor2GrColor(SkColor c) { 152 SkPMColor pm = SkPreMultiplyColor(c); 153 unsigned r = SkGetPackedR32(pm); 154 unsigned g = SkGetPackedG32(pm); 155 unsigned b = SkGetPackedB32(pm); 156 unsigned a = SkGetPackedA32(pm); 157 return GrColorPackRGBA(r, g, b, a); 158 } 159 160 /** 161 * This abandons all texture caches (for bitmaps and text) associated with 162 * the gpu, and frees any associated skia caches. It differs from 163 * deleteAllTextures in that it assumes that the gpu has lots its context, 164 * and thus the associated HW textures are no longer valid 165 */ 166 static void AbandonAllTextures(GrContext*); 167}; 168 169//////////////////////////////////////////////////////////////////////////////// 170// Classes 171 172class SkGrPathIter : public GrPathIter { 173public: 174 SkGrPathIter() { fPath = NULL; } 175 SkGrPathIter(const SkPath& path) { reset(path); } 176 virtual GrPathCmd next(GrPoint pts[]); 177 virtual GrPathCmd next(); 178 virtual void rewind(); 179 virtual GrConvexHint convexHint() const; 180 181 void reset(const SkPath& path) { 182 fPath = &path; 183 fIter.setPath(path, false); 184 } 185private: 186 187#if !SK_SCALAR_IS_GR_SCALAR 188 SkPoint fPoints[4]; 189#endif 190 SkPath::Iter fIter; 191 const SkPath* fPath; 192}; 193 194class SkGrClipIterator : public GrClipIterator { 195public: 196 SkGrClipIterator() { fClipStack = NULL; fCurr = NULL; } 197 SkGrClipIterator(const SkClipStack& clipStack) { this->reset(clipStack); } 198 199 void reset(const SkClipStack& clipStack); 200 201 // overrides 202 virtual bool isDone() const { return NULL == fCurr; } 203 virtual void next() { fCurr = fIter.next(); } 204 virtual void rewind() { this->reset(*fClipStack); } 205 virtual GrClipType getType() const; 206 207 virtual GrSetOp getOp() const; 208 209 virtual void getRect(GrRect* rect) const { 210 *rect = Sk2Gr(*fCurr->fRect); 211 } 212 213 virtual GrPathIter* getPathIter() { 214 fPathIter.reset(*fCurr->fPath); 215 return &fPathIter; 216 } 217 218 virtual GrPathFill getPathFill() const; 219 220private: 221 const SkClipStack* fClipStack; 222 SkClipStack::B2FIter fIter; 223 SkGrPathIter fPathIter; 224 // SkClipStack's auto advances on each get 225 // so we store the current pos here. 226 const SkClipStack::B2FIter::Clip* fCurr; 227}; 228 229class SkGrRegionIterator : public GrClipIterator { 230public: 231 SkGrRegionIterator() {} 232 SkGrRegionIterator(const SkRegion& region) { this->reset(region); } 233 234 void reset(const SkRegion& region) { 235 fRegion = ®ion; 236 fIter.reset(region); 237 } 238 239 // overrides 240 virtual bool isDone() const { return fIter.done(); } 241 virtual void next() { fIter.next(); } 242 virtual void rewind() { this->reset(*fRegion); } 243 virtual GrClipType getType() const { return kRect_ClipType; } 244 245 virtual GrSetOp getOp() const { return kUnion_SetOp; } 246 247 virtual void getRect(GrRect* rect) const { 248 const SkIRect& r = fIter.rect(); 249 rect->fLeft = GrIntToScalar(r.fLeft); 250 rect->fTop = GrIntToScalar(r.fTop); 251 rect->fRight = GrIntToScalar(r.fRight); 252 rect->fBottom = GrIntToScalar(r.fBottom); 253 } 254 255 virtual GrPathIter* getPathIter() { 256 SkASSERT(0); 257 return NULL; 258 } 259 260 virtual GrPathFill getPathFill() const { 261 SkASSERT(0); 262 return kWinding_PathFill; 263 } 264private: 265 const SkRegion* fRegion; 266 SkRegion::Iterator fIter; 267}; 268 269class SkGlyphCache; 270 271class SkGrFontScaler : public GrFontScaler { 272public: 273 explicit SkGrFontScaler(SkGlyphCache* strike); 274 virtual ~SkGrFontScaler(); 275 276 // overrides 277 virtual const GrKey* getKey(); 278 virtual GrMaskFormat getMaskFormat(); 279 virtual bool getPackedGlyphBounds(GrGlyph::PackedID, GrIRect* bounds); 280 virtual bool getPackedGlyphImage(GrGlyph::PackedID, int width, int height, 281 int rowBytes, void* image); 282 virtual bool getGlyphPath(uint16_t glyphID, GrPath*); 283 284private: 285 SkGlyphCache* fStrike; 286 GrKey* fKey; 287// DECLARE_INSTANCE_COUNTER(SkGrFontScaler); 288}; 289 290//////////////////////////////////////////////////////////////////////////////// 291// Helper functions 292 293GrTextureEntry* sk_gr_create_bitmap_texture(GrContext* ctx, 294 GrTextureKey* key, 295 const GrSamplerState& sampler, 296 const SkBitmap& bitmap); 297 298 299#endif 300