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