| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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 | |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 23 | // Gr headers |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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" |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 36 | #include "SkClipStack.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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 | |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 59 | #else |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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 | |
| 73 | GR_STATIC_ASSERT((int)GrSamplerState::kClamp_WrapMode == (int)SkShader::kClamp_TileMode); |
| 74 | GR_STATIC_ASSERT((int)GrSamplerState::kRepeat_WrapMode ==( |
| 75 | int)SkShader::kRepeat_TileMode); |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 76 | GR_STATIC_ASSERT((int)GrSamplerState::kMirror_WrapMode == |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 77 | (int)SkShader::kMirror_TileMode); |
| 78 | |
| 79 | #define sk_tile_mode_to_grwrap(X) ((GrSamplerState::WrapMode)(X)) |
| 80 | |
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 81 | GR_STATIC_ASSERT((int)kZero_BlendCoeff == (int)SkXfermode::kZero_Coeff); |
| 82 | GR_STATIC_ASSERT((int)kOne_BlendCoeff == (int)SkXfermode::kOne_Coeff); |
| 83 | GR_STATIC_ASSERT((int)kSC_BlendCoeff == (int)SkXfermode::kSC_Coeff); |
| 84 | GR_STATIC_ASSERT((int)kISC_BlendCoeff == (int)SkXfermode::kISC_Coeff); |
| 85 | GR_STATIC_ASSERT((int)kDC_BlendCoeff == (int)SkXfermode::kDC_Coeff); |
| 86 | GR_STATIC_ASSERT((int)kIDC_BlendCoeff == (int)SkXfermode::kIDC_Coeff); |
| 87 | GR_STATIC_ASSERT((int)kSA_BlendCoeff == (int)SkXfermode::kSA_Coeff); |
| 88 | GR_STATIC_ASSERT((int)kISA_BlendCoeff == (int)SkXfermode::kISA_Coeff); |
| 89 | GR_STATIC_ASSERT((int)kDA_BlendCoeff == (int)SkXfermode::kDA_Coeff); |
| 90 | GR_STATIC_ASSERT((int)kIDA_BlendCoeff == (int)SkXfermode::kIDA_Coeff); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 91 | |
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 92 | #define sk_blend_to_grblend(X) ((GrBlendCoeff)(X)) |
| 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 | GR_STATIC_ASSERT((int)SkPath::kMove_Verb == (int)kMove_PathCmd); |
| 95 | GR_STATIC_ASSERT((int)SkPath::kLine_Verb == (int)kLine_PathCmd); |
| 96 | GR_STATIC_ASSERT((int)SkPath::kQuad_Verb == (int)kQuadratic_PathCmd); |
| 97 | GR_STATIC_ASSERT((int)SkPath::kCubic_Verb == (int)kCubic_PathCmd); |
| 98 | GR_STATIC_ASSERT((int)SkPath::kClose_Verb == (int)kClose_PathCmd); |
| 99 | GR_STATIC_ASSERT((int)SkPath::kDone_Verb == (int)kEnd_PathCmd); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 100 | |
| bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 101 | #define sk_path_verb_to_gr_path_command(X) ((GrPathCmd)(X)) |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 102 | |
| 103 | /////////////////////////////////////////////////////////////////////////////// |
| 104 | |
| 105 | #include "SkColorPriv.h" |
| 106 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 107 | class SkGr { |
| 108 | public: |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 109 | /** |
| 110 | * Convert the SkBitmap::Config to the corresponding PixelConfig, or |
| 111 | * kUnknown_PixelConfig if the conversion cannot be done. |
| 112 | */ |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 113 | static GrPixelConfig BitmapConfig2PixelConfig(SkBitmap::Config, |
| 114 | bool isOpaque); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 115 | |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 116 | static GrPixelConfig Bitmap2PixelConfig(const SkBitmap& bm) { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 117 | return BitmapConfig2PixelConfig(bm.config(), bm.isOpaque()); |
| 118 | } |
| 119 | |
| 120 | static void SkMatrix2GrMatrix(const SkMatrix& m, GrMatrix* g) { |
| 121 | g->setAll(SkScalarToGrScalar(m[0]), |
| 122 | SkScalarToGrScalar(m[1]), |
| 123 | SkScalarToGrScalar(m[2]), |
| 124 | SkScalarToGrScalar(m[3]), |
| 125 | SkScalarToGrScalar(m[4]), |
| 126 | SkScalarToGrScalar(m[5]), |
| 127 | SkScalarToGrScalar(m[6]), |
| 128 | SkScalarToGrScalar(m[7]), |
| 129 | SkScalarToGrScalar(m[8])); |
| 130 | } |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 131 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 132 | static GrColor SkColor2GrColor(SkColor c) { |
| 133 | SkPMColor pm = SkPreMultiplyColor(c); |
| 134 | unsigned r = SkGetPackedR32(pm); |
| 135 | unsigned g = SkGetPackedG32(pm); |
| 136 | unsigned b = SkGetPackedB32(pm); |
| 137 | unsigned a = SkGetPackedA32(pm); |
| 138 | return GrColorPackRGBA(r, g, b, a); |
| 139 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | //////////////////////////////////////////////////////////////////////////////// |
| 143 | // Classes |
| 144 | |
| 145 | class SkGrPathIter : public GrPathIter { |
| 146 | public: |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 147 | SkGrPathIter() { fPath = NULL; } |
| 148 | SkGrPathIter(const SkPath& path) { reset(path); } |
| bsalomon@google.com | 5aaa69e | 2011-03-04 20:29:08 +0000 | [diff] [blame] | 149 | virtual GrPathCmd next(GrPoint pts[]); |
| 150 | virtual GrPathCmd next(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 151 | virtual void rewind(); |
| bsalomon@google.com | bf4338c | 2011-03-04 22:48:25 +0000 | [diff] [blame] | 152 | virtual GrConvexHint convexHint() const; |
| bsalomon@google.com | 06e1795 | 2011-04-27 21:13:04 +0000 | [diff] [blame] | 153 | virtual bool getConservativeBounds(GrRect* rect) const; |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 154 | |
| 155 | void reset(const SkPath& path) { |
| 156 | fPath = &path; |
| 157 | fIter.setPath(path, false); |
| 158 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 159 | private: |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 160 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 161 | #if !SK_SCALAR_IS_GR_SCALAR |
| 162 | SkPoint fPoints[4]; |
| 163 | #endif |
| 164 | SkPath::Iter fIter; |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 165 | const SkPath* fPath; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | class SkGrClipIterator : public GrClipIterator { |
| 169 | public: |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 170 | SkGrClipIterator() { fClipStack = NULL; fCurr = NULL; } |
| 171 | SkGrClipIterator(const SkClipStack& clipStack) { this->reset(clipStack); } |
| 172 | |
| 173 | void reset(const SkClipStack& clipStack); |
| 174 | |
| 175 | // overrides |
| 176 | virtual bool isDone() const { return NULL == fCurr; } |
| 177 | virtual void next() { fCurr = fIter.next(); } |
| 178 | virtual void rewind() { this->reset(*fClipStack); } |
| 179 | virtual GrClipType getType() const; |
| 180 | |
| 181 | virtual GrSetOp getOp() const; |
| 182 | |
| 183 | virtual void getRect(GrRect* rect) const { |
| scroggo | 7b11807 | 2011-03-23 15:04:26 +0000 | [diff] [blame] | 184 | if (!fCurr->fRect) { |
| 185 | rect->setEmpty(); |
| 186 | } else { |
| reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame^] | 187 | *rect = *fCurr->fRect; |
| scroggo | 7b11807 | 2011-03-23 15:04:26 +0000 | [diff] [blame] | 188 | } |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | virtual GrPathIter* getPathIter() { |
| 192 | fPathIter.reset(*fCurr->fPath); |
| 193 | return &fPathIter; |
| 194 | } |
| 195 | |
| 196 | virtual GrPathFill getPathFill() const; |
| 197 | |
| 198 | private: |
| 199 | const SkClipStack* fClipStack; |
| 200 | SkClipStack::B2FIter fIter; |
| 201 | SkGrPathIter fPathIter; |
| 202 | // SkClipStack's auto advances on each get |
| 203 | // so we store the current pos here. |
| 204 | const SkClipStack::B2FIter::Clip* fCurr; |
| 205 | }; |
| 206 | |
| 207 | class SkGrRegionIterator : public GrClipIterator { |
| 208 | public: |
| 209 | SkGrRegionIterator() {} |
| 210 | SkGrRegionIterator(const SkRegion& region) { this->reset(region); } |
| 211 | |
| reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 212 | void reset(const SkRegion& region) { |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 213 | fRegion = ®ion; |
| 214 | fIter.reset(region); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 215 | } |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 216 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 217 | // overrides |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 218 | virtual bool isDone() const { return fIter.done(); } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 219 | virtual void next() { fIter.next(); } |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 220 | virtual void rewind() { this->reset(*fRegion); } |
| 221 | virtual GrClipType getType() const { return kRect_ClipType; } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 222 | |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 223 | virtual GrSetOp getOp() const { return kUnion_SetOp; } |
| 224 | |
| 225 | virtual void getRect(GrRect* rect) const { |
| 226 | const SkIRect& r = fIter.rect(); |
| 227 | rect->fLeft = GrIntToScalar(r.fLeft); |
| 228 | rect->fTop = GrIntToScalar(r.fTop); |
| 229 | rect->fRight = GrIntToScalar(r.fRight); |
| 230 | rect->fBottom = GrIntToScalar(r.fBottom); |
| 231 | } |
| 232 | |
| 233 | virtual GrPathIter* getPathIter() { |
| 234 | SkASSERT(0); |
| 235 | return NULL; |
| 236 | } |
| 237 | |
| 238 | virtual GrPathFill getPathFill() const { |
| 239 | SkASSERT(0); |
| 240 | return kWinding_PathFill; |
| 241 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 242 | private: |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 243 | const SkRegion* fRegion; |
| 244 | SkRegion::Iterator fIter; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | class SkGlyphCache; |
| 248 | |
| 249 | class SkGrFontScaler : public GrFontScaler { |
| 250 | public: |
| 251 | explicit SkGrFontScaler(SkGlyphCache* strike); |
| 252 | virtual ~SkGrFontScaler(); |
| 253 | |
| 254 | // overrides |
| 255 | virtual const GrKey* getKey(); |
| reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 256 | virtual GrMaskFormat getMaskFormat(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 257 | virtual bool getPackedGlyphBounds(GrGlyph::PackedID, GrIRect* bounds); |
| 258 | virtual bool getPackedGlyphImage(GrGlyph::PackedID, int width, int height, |
| 259 | int rowBytes, void* image); |
| 260 | virtual bool getGlyphPath(uint16_t glyphID, GrPath*); |
| 261 | |
| 262 | private: |
| 263 | SkGlyphCache* fStrike; |
| 264 | GrKey* fKey; |
| 265 | // DECLARE_INSTANCE_COUNTER(SkGrFontScaler); |
| 266 | }; |
| 267 | |
| 268 | //////////////////////////////////////////////////////////////////////////////// |
| 269 | // Helper functions |
| 270 | |
| bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 271 | GrTextureEntry* sk_gr_create_bitmap_texture(GrContext* ctx, |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 272 | GrTextureKey* key, |
| 273 | const GrSamplerState& sampler, |
| 274 | const SkBitmap& bitmap); |
| 275 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 276 | |
| 277 | #endif |