1 /*
2  * Copyright (C) 2020 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 MINIKIN_BOUNDS_CACHE_H
18 #define MINIKIN_BOUNDS_CACHE_H
19 
20 #include "minikin/LayoutCache.h"
21 
22 #include <mutex>
23 
24 #include <utils/LruCache.h>
25 
26 #include "minikin/BoundsCache.h"
27 #include "minikin/FontCollection.h"
28 #include "minikin/Hasher.h"
29 #include "minikin/MinikinPaint.h"
30 
31 namespace minikin {
32 
33 // Cache entry
34 struct BoundsValue {
35     MinikinRect rect;
36     float advance;
37 };
38 
39 // Used for callback for LayoutCache.
40 struct ValueExtractor {
41     void operator()(const LayoutPiece& layoutPiece, const MinikinPaint& paint);
42     std::unique_ptr<BoundsValue> value;
43 };
44 
45 class BoundsCache : private android::OnEntryRemoved<LayoutCacheKey, BoundsValue*> {
46 public:
clear()47     void clear() {
48         std::lock_guard<std::mutex> lock(mMutex);
49         mCache.clear();
50     }
51 
52     // Do not use BoundsCache inside the callback function, otherwise dead-lock may happen.
53     template <typename F>
getOrCreate(const U16StringPiece & text,const Range & range,const MinikinPaint & paint,bool dir,StartHyphenEdit startHyphen,EndHyphenEdit endHyphen,F & f)54     void getOrCreate(const U16StringPiece& text, const Range& range, const MinikinPaint& paint,
55                      bool dir, StartHyphenEdit startHyphen, EndHyphenEdit endHyphen, F& f) {
56         LayoutCacheKey key(text, range, paint, dir, startHyphen, endHyphen);
57         if (paint.skipCache() || range.getLength() >= LENGTH_LIMIT_CACHE) {
58             LayoutPiece piece = LayoutPiece(text, range, dir, paint, startHyphen, endHyphen);
59             f(getBounds(piece, paint), piece.advance());
60             return;
61         }
62         {
63             std::lock_guard<std::mutex> lock(mMutex);
64             BoundsValue* value = mCache.get(key);
65             if (value != nullptr) {
66                 f(value->rect, value->advance);
67                 return;
68             }
69         }
70         // Doing text layout takes long time, so releases the mutex during doing layout.
71         // Don't care even if we do the same layout in other thread.
72         key.copyText();
73         ValueExtractor ve;
74         LayoutCache::getInstance().getOrCreate(text, range, paint, dir, startHyphen, endHyphen, ve);
75         f(ve.value->rect, ve.value->advance);
76         {
77             std::lock_guard<std::mutex> lock(mMutex);
78             mCache.put(key, ve.value.release());
79         }
80     }
81 
getInstance()82     static BoundsCache& getInstance() {
83         static BoundsCache cache(kMaxEntries);
84         return cache;
85     }
86 
87     // Compute new bounding box for the layout piece.
88     static MinikinRect getBounds(const LayoutPiece& layoutPiece, const MinikinPaint& paint);
89 
90 protected:
BoundsCache(uint32_t maxEntries)91     BoundsCache(uint32_t maxEntries) : mCache(maxEntries) {
92         mCache.setOnEntryRemovedListener(this);
93     }
94 
95 private:
96     // callback for OnEntryRemoved
operator()97     void operator()(LayoutCacheKey& key, BoundsValue*& value) {
98         key.freeText();
99         delete value;
100     }
101 
102     std::mutex mMutex;
103     android::LruCache<LayoutCacheKey, BoundsValue*> mCache GUARDED_BY(mMutex) GUARDED_BY(mMutex);
104     // LRU cache capacity. Should be fine to be less than LayoutCache#kMaxEntries since bbox
105     // calculation happens less than layout calculation.
106     static const size_t kMaxEntries = 500;
107 };
108 
109 }  // namespace minikin
110 #endif  // MINIKIN_BOUNDS_CACHE_H
111