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