| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 1 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 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. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 10 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #ifndef SkGr_DEFINED |
| 12 | #define SkGr_DEFINED |
| 13 | |
| 14 | #include <stddef.h> |
| 15 | |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 16 | // Gr headers |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 17 | #include "GrConfig.h" |
| 18 | #include "GrContext.h" |
| 19 | #include "GrFontScaler.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 20 | #include "GrClipIterator.h" |
| reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 21 | #include "GrPath.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 22 | |
| 23 | // skia headers |
| 24 | #include "SkBitmap.h" |
| 25 | #include "SkPath.h" |
| 26 | #include "SkPoint.h" |
| 27 | #include "SkRegion.h" |
| 28 | #include "SkShader.h" |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 29 | #include "SkClipStack.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 30 | |
| 31 | #if (GR_DEBUG && defined(SK_RELEASE)) || (GR_RELEASE && defined(SK_DEBUG)) |
| 32 | // #error "inconsistent GR_DEBUG and SK_DEBUG" |
| 33 | #endif |
| 34 | |
| 35 | #if GR_SCALAR_IS_FIXED |
| 36 | #ifdef SK_SCALAR_IS_FIXED |
| 37 | #define SK_SCALAR_IS_GR_SCALAR 1 |
| 38 | #else |
| 39 | #define SK_SCALAR_IS_GR_SCALAR 0 |
| 40 | #endif |
| 41 | #define SkScalarToGrScalar(x) SkScalarToFixed(x) |
| 42 | |
| 43 | #elif GR_SCALAR_IS_FLOAT |
| 44 | |
| 45 | #ifdef SK_SCALAR_IS_FLOAT |
| 46 | #define SK_SCALAR_IS_GR_SCALAR 1 |
| 47 | #else |
| 48 | #define SK_SCALAR_IS_GR_SCALAR 0 |
| 49 | #endif |
| 50 | #define SkScalarToGrScalar(x) SkScalarToFloat(x) |
| 51 | |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 52 | #else |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 53 | #error "Ganesh scalar type not defined" |
| 54 | #endif |
| 55 | |
| 56 | //////////////////////////////////////////////////////////////////////////////// |
| 57 | // Sk to Gr Type conversions |
| 58 | |
| 59 | // Verify that SkPoint and GrPoint are compatible if using the same scalar type |
| 60 | #if 0/*SK_SCALAR_IS_GR_SCALAR*/ |
| 61 | GR_STATIC_ASSERT(sizeof(SkPoint) == sizeof(GrPoint)); |
| 62 | GR_STATIC_ASSERT(offsetof(SkPoint,fX) == offsetof(GrPoint,fX))); |
| 63 | GR_STATIC_ASSERT(offsetof(SkPoint,fY) == offsetof(GrPoint,fY))); |
| 64 | #endif |
| 65 | |
| 66 | GR_STATIC_ASSERT((int)GrSamplerState::kClamp_WrapMode == (int)SkShader::kClamp_TileMode); |
| 67 | GR_STATIC_ASSERT((int)GrSamplerState::kRepeat_WrapMode ==( |
| 68 | int)SkShader::kRepeat_TileMode); |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 69 | GR_STATIC_ASSERT((int)GrSamplerState::kMirror_WrapMode == |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 70 | (int)SkShader::kMirror_TileMode); |
| 71 | |
| 72 | #define sk_tile_mode_to_grwrap(X) ((GrSamplerState::WrapMode)(X)) |
| 73 | |
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 74 | GR_STATIC_ASSERT((int)kZero_BlendCoeff == (int)SkXfermode::kZero_Coeff); |
| 75 | GR_STATIC_ASSERT((int)kOne_BlendCoeff == (int)SkXfermode::kOne_Coeff); |
| 76 | GR_STATIC_ASSERT((int)kSC_BlendCoeff == (int)SkXfermode::kSC_Coeff); |
| 77 | GR_STATIC_ASSERT((int)kISC_BlendCoeff == (int)SkXfermode::kISC_Coeff); |
| 78 | GR_STATIC_ASSERT((int)kDC_BlendCoeff == (int)SkXfermode::kDC_Coeff); |
| 79 | GR_STATIC_ASSERT((int)kIDC_BlendCoeff == (int)SkXfermode::kIDC_Coeff); |
| 80 | GR_STATIC_ASSERT((int)kSA_BlendCoeff == (int)SkXfermode::kSA_Coeff); |
| 81 | GR_STATIC_ASSERT((int)kISA_BlendCoeff == (int)SkXfermode::kISA_Coeff); |
| 82 | GR_STATIC_ASSERT((int)kDA_BlendCoeff == (int)SkXfermode::kDA_Coeff); |
| 83 | GR_STATIC_ASSERT((int)kIDA_BlendCoeff == (int)SkXfermode::kIDA_Coeff); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 84 | |
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 85 | #define sk_blend_to_grblend(X) ((GrBlendCoeff)(X)) |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 86 | |
| bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 87 | GR_STATIC_ASSERT((int)SkPath::kMove_Verb == (int)kMove_PathCmd); |
| 88 | GR_STATIC_ASSERT((int)SkPath::kLine_Verb == (int)kLine_PathCmd); |
| 89 | GR_STATIC_ASSERT((int)SkPath::kQuad_Verb == (int)kQuadratic_PathCmd); |
| 90 | GR_STATIC_ASSERT((int)SkPath::kCubic_Verb == (int)kCubic_PathCmd); |
| 91 | GR_STATIC_ASSERT((int)SkPath::kClose_Verb == (int)kClose_PathCmd); |
| 92 | GR_STATIC_ASSERT((int)SkPath::kDone_Verb == (int)kEnd_PathCmd); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 93 | |
| bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 94 | #define sk_path_verb_to_gr_path_command(X) ((GrPathCmd)(X)) |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 95 | |
| 96 | /////////////////////////////////////////////////////////////////////////////// |
| 97 | |
| 98 | #include "SkColorPriv.h" |
| 99 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 100 | class SkGr { |
| 101 | public: |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 102 | /** |
| 103 | * Convert the SkBitmap::Config to the corresponding PixelConfig, or |
| 104 | * kUnknown_PixelConfig if the conversion cannot be done. |
| 105 | */ |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 106 | static GrPixelConfig BitmapConfig2PixelConfig(SkBitmap::Config, |
| 107 | bool isOpaque); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 108 | |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 109 | static GrPixelConfig Bitmap2PixelConfig(const SkBitmap& bm) { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 110 | return BitmapConfig2PixelConfig(bm.config(), bm.isOpaque()); |
| 111 | } |
| 112 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 113 | static GrColor SkColor2GrColor(SkColor c) { |
| 114 | SkPMColor pm = SkPreMultiplyColor(c); |
| 115 | unsigned r = SkGetPackedR32(pm); |
| 116 | unsigned g = SkGetPackedG32(pm); |
| 117 | unsigned b = SkGetPackedB32(pm); |
| 118 | unsigned a = SkGetPackedA32(pm); |
| 119 | return GrColorPackRGBA(r, g, b, a); |
| 120 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | //////////////////////////////////////////////////////////////////////////////// |
| 124 | // Classes |
| 125 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 126 | class SkGrClipIterator : public GrClipIterator { |
| 127 | public: |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 128 | SkGrClipIterator() { fClipStack = NULL; fCurr = NULL; } |
| 129 | SkGrClipIterator(const SkClipStack& clipStack) { this->reset(clipStack); } |
| 130 | |
| 131 | void reset(const SkClipStack& clipStack); |
| 132 | |
| 133 | // overrides |
| 134 | virtual bool isDone() const { return NULL == fCurr; } |
| 135 | virtual void next() { fCurr = fIter.next(); } |
| 136 | virtual void rewind() { this->reset(*fClipStack); } |
| 137 | virtual GrClipType getType() const; |
| 138 | |
| 139 | virtual GrSetOp getOp() const; |
| 140 | |
| 141 | virtual void getRect(GrRect* rect) const { |
| scroggo | 7b11807 | 2011-03-23 15:04:26 +0000 | [diff] [blame] | 142 | if (!fCurr->fRect) { |
| 143 | rect->setEmpty(); |
| 144 | } else { |
| reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 145 | *rect = *fCurr->fRect; |
| scroggo | 7b11807 | 2011-03-23 15:04:26 +0000 | [diff] [blame] | 146 | } |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 149 | virtual const GrPath* getPath() { |
| 150 | return fCurr->fPath; |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | virtual GrPathFill getPathFill() const; |
| 154 | |
| 155 | private: |
| 156 | const SkClipStack* fClipStack; |
| 157 | SkClipStack::B2FIter fIter; |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 158 | // SkClipStack's auto advances on each get |
| 159 | // so we store the current pos here. |
| 160 | const SkClipStack::B2FIter::Clip* fCurr; |
| 161 | }; |
| 162 | |
| 163 | class SkGrRegionIterator : public GrClipIterator { |
| 164 | public: |
| 165 | SkGrRegionIterator() {} |
| 166 | SkGrRegionIterator(const SkRegion& region) { this->reset(region); } |
| 167 | |
| reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 168 | void reset(const SkRegion& region) { |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 169 | fRegion = ®ion; |
| 170 | fIter.reset(region); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 171 | } |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 172 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 173 | // overrides |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 174 | virtual bool isDone() const { return fIter.done(); } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 175 | virtual void next() { fIter.next(); } |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 176 | virtual void rewind() { this->reset(*fRegion); } |
| 177 | virtual GrClipType getType() const { return kRect_ClipType; } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 178 | |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 179 | virtual GrSetOp getOp() const { return kUnion_SetOp; } |
| 180 | |
| 181 | virtual void getRect(GrRect* rect) const { |
| 182 | const SkIRect& r = fIter.rect(); |
| 183 | rect->fLeft = GrIntToScalar(r.fLeft); |
| 184 | rect->fTop = GrIntToScalar(r.fTop); |
| 185 | rect->fRight = GrIntToScalar(r.fRight); |
| 186 | rect->fBottom = GrIntToScalar(r.fBottom); |
| 187 | } |
| 188 | |
| reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 189 | virtual const GrPath* getPath() { |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 190 | SkASSERT(0); |
| 191 | return NULL; |
| 192 | } |
| 193 | |
| 194 | virtual GrPathFill getPathFill() const { |
| 195 | SkASSERT(0); |
| 196 | return kWinding_PathFill; |
| 197 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 198 | private: |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 199 | const SkRegion* fRegion; |
| 200 | SkRegion::Iterator fIter; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | class SkGlyphCache; |
| 204 | |
| 205 | class SkGrFontScaler : public GrFontScaler { |
| 206 | public: |
| 207 | explicit SkGrFontScaler(SkGlyphCache* strike); |
| 208 | virtual ~SkGrFontScaler(); |
| 209 | |
| 210 | // overrides |
| 211 | virtual const GrKey* getKey(); |
| reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 212 | virtual GrMaskFormat getMaskFormat(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 213 | virtual bool getPackedGlyphBounds(GrGlyph::PackedID, GrIRect* bounds); |
| 214 | virtual bool getPackedGlyphImage(GrGlyph::PackedID, int width, int height, |
| 215 | int rowBytes, void* image); |
| 216 | virtual bool getGlyphPath(uint16_t glyphID, GrPath*); |
| 217 | |
| 218 | private: |
| 219 | SkGlyphCache* fStrike; |
| 220 | GrKey* fKey; |
| 221 | // DECLARE_INSTANCE_COUNTER(SkGrFontScaler); |
| 222 | }; |
| 223 | |
| 224 | //////////////////////////////////////////////////////////////////////////////// |
| 225 | // Helper functions |
| 226 | |
| bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 227 | static const GrContext::TextureKey gUNCACHED_KEY = ~0; |
| 228 | GrContext::TextureCacheEntry sk_gr_create_bitmap_texture(GrContext* ctx, |
| 229 | GrContext::TextureKey key, |
| 230 | const GrSamplerState& sampler, |
| 231 | const SkBitmap& bitmap); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 232 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 233 | |
| 234 | #endif |