TextLayoutCache.h revision f62034d89611fbd3e1d41413847241757acd0c10
1/*
2 * Copyright (C) 2011 The Android Open Source Project
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#ifndef ANDROID_TEXT_LAYOUT_CACHE_H
18#define ANDROID_TEXT_LAYOUT_CACHE_H
19
20#include "RtlProperties.h"
21
22#include <stddef.h>
23#include <utils/threads.h>
24#include <utils/String16.h>
25#include <utils/GenerationCache.h>
26#include <utils/KeyedVector.h>
27#include <utils/Compare.h>
28#include <utils/RefBase.h>
29#include <utils/Singleton.h>
30
31#include <SkPaint.h>
32#include <SkTemplates.h>
33#include <SkUtils.h>
34#include <SkAutoKern.h>
35#include "SkTypeface_android.h"
36
37#include <unicode/ubidi.h>
38#include <unicode/ushape.h>
39#include <unicode/unistr.h>
40
41#include "HarfbuzzSkia.h"
42#include "harfbuzz-shaper.h"
43
44#include <android_runtime/AndroidRuntime.h>
45
46#define UNICODE_NOT_A_CHAR              0xffff
47#define UNICODE_ZWSP                    0x200b
48#define UNICODE_FIRST_LOW_SURROGATE     0xdc00
49#define UNICODE_FIRST_HIGH_SURROGATE    0xd800
50#define UNICODE_FIRST_PRIVATE_USE       0xe000
51#define UNICODE_FIRST_RTL_CHAR          0x0590
52
53// Temporary buffer size
54#define CHAR_BUFFER_SIZE 80
55
56// Converts a number of mega-bytes into bytes
57#define MB(s) s * 1024 * 1024
58
59// Define the default cache size in Mb
60#define DEFAULT_TEXT_LAYOUT_CACHE_SIZE_IN_MB 0.250f
61
62// Define the interval in number of cache hits between two statistics dump
63#define DEFAULT_DUMP_STATS_CACHE_HIT_INTERVAL 100
64
65namespace android {
66
67/**
68 * TextLayoutCacheKey is the Cache key
69 */
70class TextLayoutCacheKey {
71public:
72    TextLayoutCacheKey();
73
74    TextLayoutCacheKey(const SkPaint* paint, const UChar* text, size_t start, size_t count,
75            size_t contextCount, int dirFlags);
76
77    TextLayoutCacheKey(const TextLayoutCacheKey& other);
78
79    /**
80     * We need to copy the text when we insert the key into the cache itself.
81     * We don't need to copy the text when we are only comparing keys.
82     */
83    void internalTextCopy();
84
85    /**
86     * Get the size of the Cache key.
87     */
88    size_t getSize() const;
89
90    static int compare(const TextLayoutCacheKey& lhs, const TextLayoutCacheKey& rhs);
91
92private:
93    const UChar* text; // if text is NULL, use textCopy
94    String16 textCopy;
95    size_t start;
96    size_t count;
97    size_t contextCount;
98    int dirFlags;
99    SkTypeface* typeface;
100    SkScalar textSize;
101    SkScalar textSkewX;
102    SkScalar textScaleX;
103    uint32_t flags;
104    SkPaint::Hinting hinting;
105
106    inline const UChar* getText() const { return text ? text : textCopy.string(); }
107
108}; // TextLayoutCacheKey
109
110inline int strictly_order_type(const TextLayoutCacheKey& lhs, const TextLayoutCacheKey& rhs) {
111    return TextLayoutCacheKey::compare(lhs, rhs) < 0;
112}
113
114inline int compare_type(const TextLayoutCacheKey& lhs, const TextLayoutCacheKey& rhs) {
115    return TextLayoutCacheKey::compare(lhs, rhs);
116}
117
118/*
119 * TextLayoutValue is the Cache value
120 */
121class TextLayoutValue : public RefBase {
122public:
123    TextLayoutValue(size_t contextCount);
124
125    void setElapsedTime(uint32_t time);
126    uint32_t getElapsedTime();
127
128    inline const jfloat* getAdvances() const { return mAdvances.array(); }
129    inline size_t getAdvancesCount() const { return mAdvances.size(); }
130    inline jfloat getTotalAdvance() const { return mTotalAdvance; }
131    inline const jchar* getGlyphs() const { return mGlyphs.array(); }
132    inline size_t getGlyphsCount() const { return mGlyphs.size(); }
133
134    /**
135     * Advances vector
136     */
137    Vector<jfloat> mAdvances;
138
139    /**
140     * Total number of advances
141     */
142    jfloat mTotalAdvance;
143
144    /**
145     * Glyphs vector
146     */
147    Vector<jchar> mGlyphs;
148
149    /**
150     * Get the size of the Cache entry
151     */
152    size_t getSize() const;
153
154private:
155    /**
156     * Time for computing the values (in milliseconds)
157     */
158    uint32_t mElapsedTime;
159
160}; // TextLayoutCacheValue
161
162/**
163 * The TextLayoutShaper is responsible for shaping (with the Harfbuzz library)
164 */
165class TextLayoutShaper {
166public:
167    TextLayoutShaper();
168    virtual ~TextLayoutShaper();
169
170    void computeValues(TextLayoutValue* value, const SkPaint* paint, const UChar* chars,
171            size_t start, size_t count, size_t contextCount, int dirFlags);
172
173    void purgeCaches();
174
175private:
176    /**
177     * Harfbuzz shaper item
178     */
179    HB_ShaperItem mShaperItem;
180
181    /**
182     * Harfbuzz font
183     */
184    HB_FontRec mFontRec;
185
186    /**
187     * Skia Paint used for shaping
188     */
189    SkPaint mShapingPaint;
190
191    /**
192     * Skia typefaces cached for shaping
193     */
194    SkTypeface* mDefaultTypeface;
195    SkTypeface* mArabicTypeface;
196    SkTypeface* mHebrewRegularTypeface;
197    SkTypeface* mHebrewBoldTypeface;
198    SkTypeface* mBengaliTypeface;
199    SkTypeface* mThaiTypeface;
200    SkTypeface* mDevanagariRegularTypeface;
201    SkTypeface* mTamilRegularTypeface;
202    SkTypeface* mTamilBoldTypeface;
203
204    /**
205     * Cache of Harfbuzz faces
206     */
207    KeyedVector<SkFontID, HB_Face> mCachedHBFaces;
208
209    /**
210     * Cache of glyph array size
211     */
212    size_t mShaperItemGlyphArraySize;
213
214    /**
215     * Buffer for containing the ICU normalized form of a run
216     */
217    UnicodeString mNormalizedString;
218
219    /**
220     * Buffer for normalizing a piece of a run with ICU
221     */
222    UnicodeString mBuffer;
223
224    void init();
225    void unrefTypefaces();
226
227    SkTypeface* typefaceForUnichar(const SkPaint* paint, SkTypeface* typeface,
228        SkUnichar unichar, HB_Script script);
229
230    size_t shapeFontRun(const SkPaint* paint, bool isRTL);
231
232    void computeValues(const SkPaint* paint, const UChar* chars,
233            size_t start, size_t count, size_t contextCount, int dirFlags,
234            Vector<jfloat>* const outAdvances, jfloat* outTotalAdvance,
235            Vector<jchar>* const outGlyphs);
236
237    void computeRunValues(const SkPaint* paint, const UChar* chars,
238            size_t count, bool isRTL,
239            Vector<jfloat>* const outAdvances, jfloat* outTotalAdvance,
240            Vector<jchar>* const outGlyphs);
241
242    SkTypeface* getCachedTypeface(SkTypeface** typeface, HB_Script script, SkTypeface::Style style);
243    HB_Face getCachedHBFace(SkTypeface* typeface);
244
245    bool doShaping(size_t size);
246    void createShaperItemGlyphArrays(size_t size);
247    void deleteShaperItemGlyphArrays();
248
249}; // TextLayoutShaper
250
251/**
252 * Cache of text layout information.
253 */
254class TextLayoutCache : private OnEntryRemoved<TextLayoutCacheKey, sp<TextLayoutValue> >
255{
256public:
257    TextLayoutCache(TextLayoutShaper* shaper);
258
259    ~TextLayoutCache();
260
261    bool isInitialized() {
262        return mInitialized;
263    }
264
265    /**
266     * Used as a callback when an entry is removed from the cache
267     * Do not invoke directly
268     */
269    void operator()(TextLayoutCacheKey& text, sp<TextLayoutValue>& desc);
270
271    sp<TextLayoutValue> getValue(const SkPaint* paint, const jchar* text, jint start,
272            jint count, jint contextCount, jint dirFlags);
273
274    /**
275     * Clear the cache
276     */
277    void clear();
278
279private:
280    TextLayoutShaper* mShaper;
281    Mutex mLock;
282    bool mInitialized;
283
284    GenerationCache<TextLayoutCacheKey, sp<TextLayoutValue> > mCache;
285
286    uint32_t mSize;
287    uint32_t mMaxSize;
288
289    uint32_t mCacheHitCount;
290    uint64_t mNanosecondsSaved;
291
292    uint64_t mCacheStartTime;
293
294    RtlDebugLevel mDebugLevel;
295    bool mDebugEnabled;
296
297    /*
298     * Class initialization
299     */
300    void init();
301
302    /**
303     * Dump Cache statistics
304     */
305    void dumpCacheStats();
306
307}; // TextLayoutCache
308
309/**
310 * The TextLayoutEngine is reponsible for computing TextLayoutValues
311 */
312class TextLayoutEngine : public Singleton<TextLayoutEngine> {
313public:
314    TextLayoutEngine();
315    virtual ~TextLayoutEngine();
316
317    sp<TextLayoutValue> getValue(const SkPaint* paint, const jchar* text, jint start,
318            jint count, jint contextCount, jint dirFlags);
319
320    void purgeCaches();
321
322private:
323    TextLayoutCache* mTextLayoutCache;
324    TextLayoutShaper* mShaper;
325}; // TextLayoutEngine
326
327} // namespace android
328#endif /* ANDROID_TEXT_LAYOUT_CACHE_H */
329
330