SkGr.h revision cf6285b89b8820641ffb6871d4b3275bfe783f51
1 2/* 3 * Copyright 2010 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 11#ifndef SkGr_DEFINED 12#define SkGr_DEFINED 13 14#include <stddef.h> 15 16// Gr headers 17#include "GrTypes.h" 18#include "GrContext.h" 19#include "GrFontScaler.h" 20#include "GrClipIterator.h" 21 22// skia headers 23#include "SkBitmap.h" 24#include "SkPath.h" 25#include "SkPoint.h" 26#include "SkRegion.h" 27#include "SkShader.h" 28#include "SkClipStack.h" 29 30#if (GR_DEBUG && defined(SK_RELEASE)) || (GR_RELEASE && defined(SK_DEBUG)) 31// #error "inconsistent GR_DEBUG and SK_DEBUG" 32#endif 33 34//////////////////////////////////////////////////////////////////////////////// 35// Sk to Gr Type conversions 36 37GR_STATIC_ASSERT((int)GrSamplerState::kClamp_WrapMode == (int)SkShader::kClamp_TileMode); 38GR_STATIC_ASSERT((int)GrSamplerState::kRepeat_WrapMode ==( 39 int)SkShader::kRepeat_TileMode); 40GR_STATIC_ASSERT((int)GrSamplerState::kMirror_WrapMode == 41 (int)SkShader::kMirror_TileMode); 42 43#define sk_tile_mode_to_grwrap(X) ((GrSamplerState::WrapMode)(X)) 44 45GR_STATIC_ASSERT((int)kZero_BlendCoeff == (int)SkXfermode::kZero_Coeff); 46GR_STATIC_ASSERT((int)kOne_BlendCoeff == (int)SkXfermode::kOne_Coeff); 47GR_STATIC_ASSERT((int)kSC_BlendCoeff == (int)SkXfermode::kSC_Coeff); 48GR_STATIC_ASSERT((int)kISC_BlendCoeff == (int)SkXfermode::kISC_Coeff); 49GR_STATIC_ASSERT((int)kDC_BlendCoeff == (int)SkXfermode::kDC_Coeff); 50GR_STATIC_ASSERT((int)kIDC_BlendCoeff == (int)SkXfermode::kIDC_Coeff); 51GR_STATIC_ASSERT((int)kSA_BlendCoeff == (int)SkXfermode::kSA_Coeff); 52GR_STATIC_ASSERT((int)kISA_BlendCoeff == (int)SkXfermode::kISA_Coeff); 53GR_STATIC_ASSERT((int)kDA_BlendCoeff == (int)SkXfermode::kDA_Coeff); 54GR_STATIC_ASSERT((int)kIDA_BlendCoeff == (int)SkXfermode::kIDA_Coeff); 55 56#define sk_blend_to_grblend(X) ((GrBlendCoeff)(X)) 57 58GR_STATIC_ASSERT((int)SkPath::kMove_Verb == (int)kMove_PathCmd); 59GR_STATIC_ASSERT((int)SkPath::kLine_Verb == (int)kLine_PathCmd); 60GR_STATIC_ASSERT((int)SkPath::kQuad_Verb == (int)kQuadratic_PathCmd); 61GR_STATIC_ASSERT((int)SkPath::kCubic_Verb == (int)kCubic_PathCmd); 62GR_STATIC_ASSERT((int)SkPath::kClose_Verb == (int)kClose_PathCmd); 63GR_STATIC_ASSERT((int)SkPath::kDone_Verb == (int)kEnd_PathCmd); 64 65#define sk_path_verb_to_gr_path_command(X) ((GrPathCmd)(X)) 66 67/////////////////////////////////////////////////////////////////////////////// 68 69#include "SkColorPriv.h" 70 71class SkGr { 72public: 73 /** 74 * Convert the SkBitmap::Config to the corresponding PixelConfig, or 75 * kUnknown_PixelConfig if the conversion cannot be done. 76 */ 77 static GrPixelConfig BitmapConfig2PixelConfig(SkBitmap::Config); 78 79 static GrColor SkColor2GrColor(SkColor c) { 80 SkPMColor pm = SkPreMultiplyColor(c); 81 unsigned r = SkGetPackedR32(pm); 82 unsigned g = SkGetPackedG32(pm); 83 unsigned b = SkGetPackedB32(pm); 84 unsigned a = SkGetPackedA32(pm); 85 return GrColorPackRGBA(r, g, b, a); 86 } 87}; 88 89//////////////////////////////////////////////////////////////////////////////// 90// Classes 91 92class SkGrClipIterator : public GrClipIterator { 93public: 94 SkGrClipIterator() { fClipStack = NULL; fCurr = NULL; } 95 SkGrClipIterator(const SkClipStack& clipStack) { this->reset(clipStack); } 96 97 void reset(const SkClipStack& clipStack); 98 99 // overrides 100 virtual bool isDone() const SK_OVERRIDE { return NULL == fCurr; } 101 virtual void next() SK_OVERRIDE { fCurr = fIter.next(); } 102 virtual void rewind() SK_OVERRIDE { this->reset(*fClipStack); } 103 virtual GrClipType getType() const SK_OVERRIDE; 104 105 virtual SkRegion::Op getOp() const SK_OVERRIDE; 106 107 virtual bool getDoAA() const SK_OVERRIDE; 108 109 virtual void getRect(GrRect* rect) const SK_OVERRIDE { 110 if (!fCurr->fRect) { 111 rect->setEmpty(); 112 } else { 113 *rect = *fCurr->fRect; 114 } 115 } 116 117 virtual const SkPath* getPath() SK_OVERRIDE { 118 return fCurr->fPath; 119 } 120 121 virtual GrPathFill getPathFill() const SK_OVERRIDE; 122 123private: 124 const SkClipStack* fClipStack; 125 SkClipStack::B2FIter fIter; 126 // SkClipStack's auto advances on each get 127 // so we store the current pos here. 128 const SkClipStack::B2FIter::Clip* fCurr; 129}; 130 131class SkGlyphCache; 132 133class SkGrFontScaler : public GrFontScaler { 134public: 135 explicit SkGrFontScaler(SkGlyphCache* strike); 136 virtual ~SkGrFontScaler(); 137 138 // overrides 139 virtual const GrKey* getKey(); 140 virtual GrMaskFormat getMaskFormat(); 141 virtual bool getPackedGlyphBounds(GrGlyph::PackedID, GrIRect* bounds); 142 virtual bool getPackedGlyphImage(GrGlyph::PackedID, int width, int height, 143 int rowBytes, void* image); 144 virtual bool getGlyphPath(uint16_t glyphID, SkPath*); 145 146private: 147 SkGlyphCache* fStrike; 148 GrKey* fKey; 149// DECLARE_INSTANCE_COUNTER(SkGrFontScaler); 150}; 151 152//////////////////////////////////////////////////////////////////////////////// 153// Helper functions 154 155static const GrTexture::TextureKey gUNCACHED_KEY = ~0U; 156GrContext::TextureCacheEntry sk_gr_create_bitmap_texture(GrContext* ctx, 157 GrTexture::TextureKey key, 158 const GrSamplerState* sampler, 159 const SkBitmap& bitmap); 160 161 162#endif 163