Home | History | Annotate | Download | only in gpu
      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 GrTextStrike_DEFINED
     12 #define GrTextStrike_DEFINED
     13 
     14 #include "GrAllocPool.h"
     15 #include "GrFontScaler.h"
     16 #include "GrTHashTable.h"
     17 #include "GrGlyph.h"
     18 #include "GrDrawTarget.h"
     19 #include "GrAtlas.h"
     20 
     21 class GrFontCache;
     22 class GrGpu;
     23 class GrFontPurgeListener;
     24 
     25 /**
     26  *  The textcache maps a hostfontscaler instance to a dictionary of
     27  *  glyphid->strike
     28  */
     29 class GrTextStrike {
     30 public:
     31     GrTextStrike(GrFontCache*, const GrKey* fontScalerKey, GrMaskFormat, GrAtlasMgr*);
     32     ~GrTextStrike();
     33 
     34     const GrKey* getFontScalerKey() const { return fFontScalerKey; }
     35     GrFontCache* getFontCache() const { return fFontCache; }
     36     GrMaskFormat getMaskFormat() const { return fMaskFormat; }
     37 
     38     inline GrGlyph* getGlyph(GrGlyph::PackedID, GrFontScaler*);
     39     bool addGlyphToAtlas(GrGlyph*, GrFontScaler*);
     40 
     41     // testing
     42     int countGlyphs() const { return fCache.getArray().count(); }
     43     const GrGlyph* glyphAt(int index) const {
     44         return fCache.getArray()[index];
     45     }
     46 
     47     // remove any references to this plot
     48     void removePlot(const GrPlot* plot);
     49 
     50 public:
     51     // for easy removal from list
     52     GrTextStrike*   fPrev;
     53     GrTextStrike*   fNext;
     54 
     55 private:
     56     class Key;
     57     GrTHashTable<GrGlyph, Key, 7> fCache;
     58     const GrKey* fFontScalerKey;
     59     GrTAllocPool<GrGlyph> fPool;
     60 
     61     GrFontCache*    fFontCache;
     62     GrAtlasMgr*     fAtlasMgr;
     63     GrMaskFormat    fMaskFormat;
     64     bool            fUseDistanceField;
     65 
     66     GrAtlas         fAtlas;
     67 
     68     GrGlyph* generateGlyph(GrGlyph::PackedID packed, GrFontScaler* scaler);
     69 
     70     friend class GrFontCache;
     71 };
     72 
     73 class GrFontCache {
     74 public:
     75     GrFontCache(GrGpu*);
     76     ~GrFontCache();
     77 
     78     inline GrTextStrike* getStrike(GrFontScaler*, bool useDistanceField);
     79 
     80     void freeAll();
     81 
     82     // make an unused plot available
     83     bool freeUnusedPlot(GrTextStrike* preserveStrike);
     84 
     85     // testing
     86     int countStrikes() const { return fCache.getArray().count(); }
     87     const GrTextStrike* strikeAt(int index) const {
     88         return fCache.getArray()[index];
     89     }
     90     GrTextStrike* getHeadStrike() const { return fHead; }
     91 
     92     void updateTextures() {
     93         for (int i = 0; i < kAtlasCount; ++i) {
     94             if (fAtlasMgr[i]) {
     95                 fAtlasMgr[i]->uploadPlotsToTexture();
     96             }
     97         }
     98     }
     99 
    100 #ifdef SK_DEBUG
    101     void validate() const;
    102 #else
    103     void validate() const {}
    104 #endif
    105 
    106     void dump() const;
    107 
    108     enum AtlasType {
    109         kA8_AtlasType,   //!< 1-byte per pixel
    110         k565_AtlasType,  //!< 2-bytes per pixel
    111         k8888_AtlasType, //!< 4-bytes per pixel
    112 
    113         kLast_AtlasType = k8888_AtlasType
    114     };
    115     static const int kAtlasCount = kLast_AtlasType + 1;
    116 
    117 private:
    118     friend class GrFontPurgeListener;
    119 
    120     class Key;
    121     GrTHashTable<GrTextStrike, Key, 8> fCache;
    122     // for LRU
    123     GrTextStrike* fHead;
    124     GrTextStrike* fTail;
    125 
    126     GrGpu*      fGpu;
    127     GrAtlasMgr* fAtlasMgr[kAtlasCount];
    128 
    129     GrTextStrike* generateStrike(GrFontScaler*, const Key&);
    130     inline void detachStrikeFromList(GrTextStrike*);
    131     void purgeStrike(GrTextStrike* strike);
    132 };
    133 
    134 #endif
    135