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