1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef RENDER_SERVICE_CLIENT_CORE_RENDER_RS_TYPEFACE_CACHE_H
17 #define RENDER_SERVICE_CLIENT_CORE_RENDER_RS_TYPEFACE_CACHE_H
18 
19 #include <cstdint>
20 #include <unordered_map>
21 #include <list>
22 #include "text/typeface.h"
23 
24 #include "memory/rs_dfx_string.h"
25 #include "memory/rs_memory_track.h"
26 
27 namespace OHOS {
28 
29 namespace Rosen {
30 using TypefaceTuple = std::tuple<std::shared_ptr<Drawing::Typeface>, uint32_t>;
31 class RSB_EXPORT RSTypefaceCache {
32 public:
33     static RSTypefaceCache& Instance();
34     static pid_t GetTypefacePid(uint64_t globalUniqueId);
35     static uint32_t GetTypefaceId(uint64_t globalUniqueId);
36     /**
37       * Calculate unique id by combining process id and local unique id
38       * uniqueId = pid(high 32bit) | typefaceId(low 32bit)
39       */
40     static uint64_t GenGlobalUniqueId(uint32_t typefaceId);
41     /**
42       * Checks if the given hash exists in the cache already
43       * provided by someone else. If so, increases ref count to reduce registration cost.
44       */
45     bool HasTypeface(uint64_t globalUniqueId, uint32_t hash);
46     void CacheDrawingTypeface(uint64_t globalUniqueId, std::shared_ptr<Drawing::Typeface> typeface);
47     std::shared_ptr<Drawing::Typeface> GetDrawingTypefaceCache(uint64_t globalUniqueId) const;
48     void RemoveDrawingTypefaceByGlobalUniqueId(uint64_t globalUniqueId);
49     void RemoveDrawingTypefacesByPid(pid_t pid);
50     void AddDelayDestroyQueue(uint64_t globalUniqueId);
51     void HandleDelayDestroyQueue();
52 
53     /**
54      * @brief    Remove patched typeface IDs (used for profiler replay).
55      */
56     void ReplayClear();
57 
58     /**
59      * @brief    Serialize drawing typeface cache (used for profiler replay).
60      * @param ss String stream to write serialized data.
61      */
62     void ReplaySerialize(std::stringstream& ss);
63 
64     /**
65      * @brief    Deserialize drawing typeface cache (used for profiler replay).
66      * @param ss Serialized data.
67      */
68     void ReplayDeserialize(std::stringstream& ss);
69 
70     RSTypefaceCache() = default;
71     ~RSTypefaceCache() = default;
72     class RSTypefaceRef {
73     public:
74         uint64_t globalUniqueId = 0;
75         uint32_t refCount = 0;
76     };
77     void Dump() const;
78 
79 private:
80     bool AddIfFound(uint64_t uniqueId, uint32_t hash);
81     void RemoveHashMap(pid_t pid, std::unordered_map<uint64_t, TypefaceTuple>& typefaceHashMap,
82         uint64_t hash_value);
83     RSTypefaceCache(const RSTypefaceCache&) = delete;
84     RSTypefaceCache(const RSTypefaceCache&&) = delete;
85     RSTypefaceCache& operator=(const RSTypefaceCache&) = delete;
86     RSTypefaceCache& operator=(const RSTypefaceCache&&) = delete;
87     mutable std::mutex mapMutex_;
88     std::unordered_map<uint64_t, uint64_t> typefaceHashCode_;
89     std::unordered_map<uint64_t, TypefaceTuple> typefaceHashMap_;
90 
91     mutable std::mutex listMutex_;
92     std::list<RSTypefaceRef> delayDestroyTypefaces_;
93     std::unordered_map<uint32_t, std::vector<uint64_t>> typefaceHashQueue_;
94 };
95 } // namespace Rosen
96 } // namespace OHOS
97 #endif // RENDER_SERVICE_CLIENT_CORE_RENDER_RS_TYPEFACE_CACHE_H
98