Home | History | Annotate | Download | only in gpu
      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 "GrClipIterator.h"
     28 #include "GrPath.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 
     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);
     76 GR_STATIC_ASSERT((int)GrSamplerState::kMirror_WrapMode ==
     77                  (int)SkShader::kMirror_TileMode);
     78 
     79 #define sk_tile_mode_to_grwrap(X) ((GrSamplerState::WrapMode)(X))
     80 
     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);
     91 
     92 #define sk_blend_to_grblend(X) ((GrBlendCoeff)(X))
     93 
     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);
    100 
    101 #define sk_path_verb_to_gr_path_command(X) ((GrPathCmd)(X))
    102 
    103 ///////////////////////////////////////////////////////////////////////////////
    104 
    105 #include "SkColorPriv.h"
    106 
    107 class SkGr {
    108 public:
    109     /**
    110      *  Convert the SkBitmap::Config to the corresponding PixelConfig, or
    111      *  kUnknown_PixelConfig if the conversion cannot be done.
    112      */
    113     static GrPixelConfig BitmapConfig2PixelConfig(SkBitmap::Config,
    114                                                   bool isOpaque);
    115 
    116     static GrPixelConfig Bitmap2PixelConfig(const SkBitmap& bm) {
    117         return BitmapConfig2PixelConfig(bm.config(), bm.isOpaque());
    118     }
    119 
    120     static GrColor SkColor2GrColor(SkColor c) {
    121         SkPMColor pm = SkPreMultiplyColor(c);
    122         unsigned r = SkGetPackedR32(pm);
    123         unsigned g = SkGetPackedG32(pm);
    124         unsigned b = SkGetPackedB32(pm);
    125         unsigned a = SkGetPackedA32(pm);
    126         return GrColorPackRGBA(r, g, b, a);
    127     }
    128 };
    129 
    130 ////////////////////////////////////////////////////////////////////////////////
    131 // Classes
    132 
    133 class SkGrClipIterator : public GrClipIterator {
    134 public:
    135     SkGrClipIterator() { fClipStack = NULL;  fCurr = NULL; }
    136     SkGrClipIterator(const SkClipStack& clipStack) { this->reset(clipStack); }
    137 
    138     void reset(const SkClipStack& clipStack);
    139 
    140     // overrides
    141     virtual bool isDone() const { return NULL == fCurr; }
    142     virtual void next() { fCurr = fIter.next(); }
    143     virtual void rewind() { this->reset(*fClipStack); }
    144     virtual GrClipType getType() const;
    145 
    146     virtual GrSetOp getOp() const;
    147 
    148     virtual void getRect(GrRect* rect) const {
    149         if (!fCurr->fRect) {
    150             rect->setEmpty();
    151         } else {
    152             *rect = *fCurr->fRect;
    153         }
    154     }
    155 
    156     virtual const GrPath* getPath() {
    157         return fCurr->fPath;
    158     }
    159 
    160     virtual GrPathFill getPathFill() const;
    161 
    162 private:
    163     const SkClipStack*                  fClipStack;
    164     SkClipStack::B2FIter                fIter;
    165     // SkClipStack's auto advances on each get
    166     // so we store the current pos here.
    167     const SkClipStack::B2FIter::Clip*   fCurr;
    168 };
    169 
    170 class SkGrRegionIterator : public GrClipIterator {
    171 public:
    172     SkGrRegionIterator() {}
    173     SkGrRegionIterator(const SkRegion& region) { this->reset(region); }
    174 
    175     void reset(const SkRegion& region) {
    176         fRegion = &region;
    177         fIter.reset(region);
    178     }
    179 
    180     // overrides
    181     virtual bool isDone() const { return fIter.done(); }
    182     virtual void next() { fIter.next(); }
    183     virtual void rewind() { this->reset(*fRegion); }
    184     virtual GrClipType getType() const { return kRect_ClipType; }
    185 
    186     virtual GrSetOp getOp() const { return kUnion_SetOp; }
    187 
    188     virtual void getRect(GrRect* rect) const {
    189         const SkIRect& r = fIter.rect();
    190         rect->fLeft   = GrIntToScalar(r.fLeft);
    191         rect->fTop    = GrIntToScalar(r.fTop);
    192         rect->fRight  = GrIntToScalar(r.fRight);
    193         rect->fBottom = GrIntToScalar(r.fBottom);
    194     }
    195 
    196     virtual const GrPath* getPath() {
    197         SkASSERT(0);
    198         return NULL;
    199     }
    200 
    201     virtual GrPathFill getPathFill() const {
    202         SkASSERT(0);
    203         return kWinding_PathFill;
    204     }
    205 private:
    206     const SkRegion*     fRegion;
    207     SkRegion::Iterator  fIter;
    208 };
    209 
    210 class SkGlyphCache;
    211 
    212 class SkGrFontScaler : public GrFontScaler {
    213 public:
    214     explicit SkGrFontScaler(SkGlyphCache* strike);
    215     virtual ~SkGrFontScaler();
    216 
    217     // overrides
    218     virtual const GrKey* getKey();
    219     virtual GrMaskFormat getMaskFormat();
    220     virtual bool getPackedGlyphBounds(GrGlyph::PackedID, GrIRect* bounds);
    221     virtual bool getPackedGlyphImage(GrGlyph::PackedID, int width, int height,
    222                                      int rowBytes, void* image);
    223     virtual bool getGlyphPath(uint16_t glyphID, GrPath*);
    224 
    225 private:
    226     SkGlyphCache* fStrike;
    227     GrKey*  fKey;
    228 //    DECLARE_INSTANCE_COUNTER(SkGrFontScaler);
    229 };
    230 
    231 ////////////////////////////////////////////////////////////////////////////////
    232 // Helper functions
    233 
    234 GrTextureEntry* sk_gr_create_bitmap_texture(GrContext* ctx,
    235                                             GrTextureKey* key,
    236                                             const GrSamplerState& sampler,
    237                                             const SkBitmap& bitmap);
    238 
    239 
    240 #endif
    241