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 "GrTHashCache.h"
     17 #include "GrPoint.h"
     18 #include "GrGlyph.h"
     19 
     20 class GrAtlasMgr;
     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,
     32                  GrAtlasMgr*);
     33     ~GrTextStrike();
     34 
     35     const GrKey* getFontScalerKey() const { return fFontScalerKey; }
     36     GrFontCache* getFontCache() const { return fFontCache; }
     37     GrMaskFormat getMaskFormat() const { return fMaskFormat; }
     38 
     39     inline GrGlyph* getGlyph(GrGlyph::PackedID, GrFontScaler*);
     40     bool getGlyphAtlas(GrGlyph*, GrFontScaler*);
     41 
     42     // testing
     43     int countGlyphs() const { return fCache.getArray().count(); }
     44     const GrGlyph* glyphAt(int index) const {
     45         return fCache.getArray()[index];
     46     }
     47     GrAtlas* getAtlas() const { return fAtlas; }
     48 
     49     // returns true if an atlas was removed
     50     bool removeUnusedAtlases();
     51 
     52 public:
     53     // for LRU
     54     GrTextStrike*   fPrev;
     55     GrTextStrike*   fNext;
     56 
     57 private:
     58     class Key;
     59     GrTHashTable<GrGlyph, Key, 7> fCache;
     60     const GrKey* fFontScalerKey;
     61     GrTAllocPool<GrGlyph> fPool;
     62 
     63     GrFontCache*    fFontCache;
     64     GrAtlasMgr*     fAtlasMgr;
     65     GrAtlas*        fAtlas;     // linklist
     66 
     67     GrMaskFormat fMaskFormat;
     68 
     69     GrGlyph* generateGlyph(GrGlyph::PackedID packed, GrFontScaler* scaler);
     70     // returns true if after the purge, the strike is empty
     71     bool purgeAtlasAtY(GrAtlas* atlas, int yCoord);
     72 
     73     friend class GrFontCache;
     74 };
     75 
     76 class GrFontCache {
     77 public:
     78     GrFontCache(GrGpu*);
     79     ~GrFontCache();
     80 
     81     inline GrTextStrike* getStrike(GrFontScaler*);
     82 
     83     void freeAll();
     84 
     85     void purgeExceptFor(GrTextStrike*);
     86 
     87     // remove an unused atlas and its strike (if necessary)
     88     void freeAtlasExceptFor(GrTextStrike*);
     89 
     90     // testing
     91     int countStrikes() const { return fCache.getArray().count(); }
     92     const GrTextStrike* strikeAt(int index) const {
     93         return fCache.getArray()[index];
     94     }
     95     GrTextStrike* getHeadStrike() const { return fHead; }
     96 
     97 #if GR_DEBUG
     98     void validate() const;
     99 #else
    100     void validate() const {}
    101 #endif
    102 
    103 private:
    104     friend class GrFontPurgeListener;
    105 
    106     class Key;
    107     GrTHashTable<GrTextStrike, Key, 8> fCache;
    108     // for LRU
    109     GrTextStrike* fHead;
    110     GrTextStrike* fTail;
    111 
    112     GrGpu*      fGpu;
    113     GrAtlasMgr* fAtlasMgr;
    114 
    115 
    116     GrTextStrike* generateStrike(GrFontScaler*, const Key&);
    117     inline void detachStrikeFromList(GrTextStrike*);
    118 };
    119 
    120 #endif
    121