blob: a5bb6ae5db0e54514d75e50119a9db80071a72a2 [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/**
26 * The textcache maps a hostfontscaler instance to a dictionary of
27 * glyphid->strike
28 */
29class GrTextStrike {
30public:
jvanverth733f5f52014-07-11 19:45:16 -070031 GrTextStrike(GrFontCache*, const GrFontDescKey* fontScalerKey, GrMaskFormat, GrAtlas*);
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; }
reed@google.com98539c62011-03-15 15:40:16 +000036 GrMaskFormat getMaskFormat() const { return fMaskFormat; }
jvanverthf17bc6c2014-07-25 16:46:53 -070037 GrTexture* getTexture() const { return fAtlas->getTexture(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000038
39 inline GrGlyph* getGlyph(GrGlyph::PackedID, GrFontScaler*);
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000040 bool addGlyphToAtlas(GrGlyph*, GrFontScaler*);
reed@google.comac10a2d2010-12-22 21:39:39 +000041
42 // testing
jvanverthdd6d2272014-07-22 13:25:26 -070043 int countGlyphs() const { return fCache.count(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000044
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000045 // remove any references to this plot
46 void removePlot(const GrPlot* plot);
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000047
jvanverthdd6d2272014-07-22 13:25:26 -070048 static const GrFontDescKey& GetKey(const GrTextStrike& ts) {
49 return *(ts.fFontScalerKey);
50 }
51 static uint32_t Hash(const GrFontDescKey& key) {
52 return key.getHash();
53 }
54
reed@google.comac10a2d2010-12-22 21:39:39 +000055public:
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000056 // for easy removal from list
reed@google.comac10a2d2010-12-22 21:39:39 +000057 GrTextStrike* fPrev;
58 GrTextStrike* fNext;
59
60private:
jvanverthdd6d2272014-07-22 13:25:26 -070061 SkTDynamicHash<GrGlyph, GrGlyph::PackedID> fCache;
jvanverth733f5f52014-07-11 19:45:16 -070062 const GrFontDescKey* fFontScalerKey;
reed@google.comac10a2d2010-12-22 21:39:39 +000063 GrTAllocPool<GrGlyph> fPool;
64
65 GrFontCache* fFontCache;
robertphillips1d86ee82014-06-24 15:08:49 -070066 GrAtlas* fAtlas;
commit-bot@chromium.org95294412013-09-26 15:28:40 +000067 GrMaskFormat fMaskFormat;
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
84 void freeAll();
reed@google.comac10a2d2010-12-22 21:39:39 +000085
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000086 // make an unused plot available
87 bool freeUnusedPlot(GrTextStrike* preserveStrike);
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000088
reed@google.comac10a2d2010-12-22 21:39:39 +000089 // testing
jvanverthdd6d2272014-07-22 13:25:26 -070090 int countStrikes() const { return fCache.count(); }
reed@google.comac10a2d2010-12-22 21:39:39 +000091 GrTextStrike* getHeadStrike() const { return fHead; }
92
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000093 void updateTextures() {
94 for (int i = 0; i < kAtlasCount; ++i) {
robertphillips1d86ee82014-06-24 15:08:49 -070095 if (fAtlases[i]) {
96 fAtlases[i]->uploadPlotsToTexture();
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000097 }
98 }
99 }
100
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000101#ifdef SK_DEBUG
reed@google.comac10a2d2010-12-22 21:39:39 +0000102 void validate() const;
103#else
104 void validate() const {}
105#endif
106
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000107 void dump() const;
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000108
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000109 enum AtlasType {
110 kA8_AtlasType, //!< 1-byte per pixel
111 k565_AtlasType, //!< 2-bytes per pixel
112 k8888_AtlasType, //!< 4-bytes per pixel
113
114 kLast_AtlasType = k8888_AtlasType
115 };
116 static const int kAtlasCount = kLast_AtlasType + 1;
117
reed@google.comac10a2d2010-12-22 21:39:39 +0000118private:
119 friend class GrFontPurgeListener;
120
jvanverthdd6d2272014-07-22 13:25:26 -0700121 SkTDynamicHash<GrTextStrike, GrFontDescKey> fCache;
reed@google.comac10a2d2010-12-22 21:39:39 +0000122 // for LRU
123 GrTextStrike* fHead;
124 GrTextStrike* fTail;
125
126 GrGpu* fGpu;
robertphillips1d86ee82014-06-24 15:08:49 -0700127 GrAtlas* fAtlases[kAtlasCount];
reed@google.comac10a2d2010-12-22 21:39:39 +0000128
jvanverthdd6d2272014-07-22 13:25:26 -0700129 GrTextStrike* generateStrike(GrFontScaler*);
reed@google.comac10a2d2010-12-22 21:39:39 +0000130 inline void detachStrikeFromList(GrTextStrike*);
commit-bot@chromium.orgb2e9fa52013-10-27 20:50:23 +0000131 void purgeStrike(GrTextStrike* strike);
reed@google.comac10a2d2010-12-22 21:39:39 +0000132};
133
134#endif