blob: 779d676e433f2a50e3185898396db9c221c05ee6 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrTextStrike_DEFINED
12#define GrTextStrike_DEFINED
13
14#include "GrAllocPool.h"
15#include "GrFontScaler.h"
jvanverthdd6d2272014-07-22 13:25:26 -070016#include "SkTDynamicHash.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017#include "GrGlyph.h"
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000018#include "GrDrawTarget.h"
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000019#include "GrAtlas.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020
reed@google.comac10a2d2010-12-22 21:39:39 +000021class GrFontCache;
22class GrGpu;
23class GrFontPurgeListener;
24
25/**
jvanverth294c3262014-10-10 11:36:12 -070026 * The textstrike maps a hostfontscaler instance to a dictionary of
reed@google.comac10a2d2010-12-22 21:39:39 +000027 * glyphid->strike
28 */
29class GrTextStrike {
30public:
jvanverth294c3262014-10-10 11:36:12 -070031 GrTextStrike(GrFontCache*, const GrFontDescKey* fontScalerKey);
reed@google.comac10a2d2010-12-22 21:39:39 +000032 ~GrTextStrike();
33
jvanverth733f5f52014-07-11 19:45:16 -070034 const GrFontDescKey* getFontScalerKey() const { return fFontScalerKey; }
reed@google.comac10a2d2010-12-22 21:39:39 +000035 GrFontCache* getFontCache() const { return fFontCache; }
36
37 inline GrGlyph* getGlyph(GrGlyph::PackedID, GrFontScaler*);
jvanverth681e65b2014-09-19 13:07:38 -070038 // returns true if glyph (or glyph+padding for distance field)
39 // is too large to ever fit in texture atlas subregions (GrPlots)
40 bool glyphTooLargeForAtlas(GrGlyph*);
41 // returns true if glyph successfully added to texture atlas, false otherwise
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000042 bool addGlyphToAtlas(GrGlyph*, GrFontScaler*);
reed@google.comac10a2d2010-12-22 21:39:39 +000043
44 // testing
jvanverthdd6d2272014-07-22 13:25:26 -070045 int countGlyphs() const { return fCache.count(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000046
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000047 // remove any references to this plot
48 void removePlot(const GrPlot* plot);
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000049
jvanverthdd6d2272014-07-22 13:25:26 -070050 static const GrFontDescKey& GetKey(const GrTextStrike& ts) {
51 return *(ts.fFontScalerKey);
52 }
53 static uint32_t Hash(const GrFontDescKey& key) {
54 return key.getHash();
55 }
56
reed@google.comac10a2d2010-12-22 21:39:39 +000057public:
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000058 // for easy removal from list
reed@google.comac10a2d2010-12-22 21:39:39 +000059 GrTextStrike* fPrev;
60 GrTextStrike* fNext;
61
62private:
jvanverthdd6d2272014-07-22 13:25:26 -070063 SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache;
jvanverth733f5f52014-07-11 19:45:16 -070064 const GrFontDescKey* fFontScalerKey;
reed@google.comac10a2d2010-12-22 21:39:39 +000065 GrTAllocPool<GrGlyph> fPool;
66
67 GrFontCache* fFontCache;
jvanverth@google.comd830d132013-11-11 20:54:09 +000068 bool fUseDistanceField;
jvanverth@google.comd830d132013-11-11 20:54:09 +000069
robertphillips1d86ee82014-06-24 15:08:49 -070070 GrAtlas::ClientPlotUsage fPlotUsage;
reed@google.com98539c62011-03-15 15:40:16 +000071
reed@google.comac10a2d2010-12-22 21:39:39 +000072 GrGlyph* generateGlyph(GrGlyph::PackedID packed, GrFontScaler* scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +000073
74 friend class GrFontCache;
75};
76
77class GrFontCache {
78public:
79 GrFontCache(GrGpu*);
80 ~GrFontCache();
81
jvanverth@google.comd830d132013-11-11 20:54:09 +000082 inline GrTextStrike* getStrike(GrFontScaler*, bool useDistanceField);
reed@google.comac10a2d2010-12-22 21:39:39 +000083
jvanverth294c3262014-10-10 11:36:12 -070084 // add to texture atlas that matches this format
85 GrPlot* addToAtlas(GrMaskFormat format, GrAtlas::ClientPlotUsage* usage,
86 int width, int height, const void* image,
87 SkIPoint16* loc);
88
reed@google.comac10a2d2010-12-22 21:39:39 +000089 void freeAll();
reed@google.comac10a2d2010-12-22 21:39:39 +000090
jvanverth294c3262014-10-10 11:36:12 -070091 // make an unused plot available for this glyph
92 bool freeUnusedPlot(GrTextStrike* preserveStrike, const GrGlyph* glyph);
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000093
reed@google.comac10a2d2010-12-22 21:39:39 +000094 // testing
jvanverthdd6d2272014-07-22 13:25:26 -070095 int countStrikes() const { return fCache.count(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000096 GrTextStrike* getHeadStrike() const { return fHead; }
97
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000098 void updateTextures() {
99 for (int i = 0; i < kAtlasCount; ++i) {
robertphillips1d86ee82014-06-24 15:08:49 -0700100 if (fAtlases[i]) {
101 fAtlases[i]->uploadPlotsToTexture();
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000102 }
103 }
104 }
105
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000106#ifdef SK_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +0000107 void validate() const;
108#else
109 void validate() const {}
110#endif
111
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000112 void dump() const;
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000113
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000114 enum AtlasType {
115 kA8_AtlasType, //!< 1-byte per pixel
116 k565_AtlasType, //!< 2-bytes per pixel
117 k8888_AtlasType, //!< 4-bytes per pixel
118
119 kLast_AtlasType = k8888_AtlasType
120 };
121 static const int kAtlasCount = kLast_AtlasType + 1;
122
reed@google.comac10a2d2010-12-22 21:39:39 +0000123private:
124 friend class GrFontPurgeListener;
125
jvanverthdd6d2272014-07-22 13:25:26 -0700126 SkTDynamicHash<GrTextStrike, GrFontDescKey> fCache;
reed@google.comac10a2d2010-12-22 21:39:39 +0000127 // for LRU
128 GrTextStrike* fHead;
129 GrTextStrike* fTail;
130
131 GrGpu* fGpu;
robertphillips1d86ee82014-06-24 15:08:49 -0700132 GrAtlas* fAtlases[kAtlasCount];
reed@google.comac10a2d2010-12-22 21:39:39 +0000133
jvanverthdd6d2272014-07-22 13:25:26 -0700134 GrTextStrike* generateStrike(GrFontScaler*);
reed@google.comac10a2d2010-12-22 21:39:39 +0000135 inline void detachStrikeFromList(GrTextStrike*);
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +0000136 void purgeStrike(GrTextStrike* strike);
reed@google.comac10a2d2010-12-22 21:39:39 +0000137};
138
139#endif