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 #include "rs_profiler_cache.h"
17
18 #include "rs_profiler_archive.h"
19 #include "rs_profiler_utils.h"
20
21 namespace OHOS::Rosen {
22
23 std::atomic_uint64_t ImageCache::id_ = 0u;
24 std::mutex ImageCache::mutex_;
25 std::map<uint64_t, Image> ImageCache::cache_;
26 std::atomic_size_t ImageCache::consumption_;
27
IsValid() const28 bool Image::IsValid() const
29 {
30 return (!data.empty()) && (data.size() < maxSize);
31 }
32
Size() const33 size_t Image::Size() const
34 {
35 return data.size();
36 }
37
Serialize(Archive & archive)38 void Image::Serialize(Archive& archive)
39 {
40 archive.Serialize(data);
41 archive.Serialize(parcelSkipBytes);
42 archive.Serialize(dmaSize);
43 archive.Serialize(dmaWidth);
44 archive.Serialize(dmaHeight);
45 archive.Serialize(dmaStride);
46 archive.Serialize(dmaFormat);
47 archive.Serialize(dmaUsage);
48 }
49
50 // ImageCache
New()51 uint64_t ImageCache::New()
52 {
53 return Utils::ComposeNodeId(Utils::GetPid(), id_++);
54 }
55
Exists(uint64_t id)56 bool ImageCache::Exists(uint64_t id)
57 {
58 const std::lock_guard<std::mutex> guard(mutex_);
59 return (cache_.count(id) > 0);
60 }
61
Add(uint64_t id,Image && image)62 bool ImageCache::Add(uint64_t id, Image&& image)
63 {
64 if (image.IsValid() && !Exists(id)) {
65 consumption_ += image.Size();
66 const std::lock_guard<std::mutex> guard(mutex_);
67 cache_.insert({ id, image });
68 return true;
69 }
70 return false;
71 }
72
Get(uint64_t id)73 Image* ImageCache::Get(uint64_t id)
74 {
75 const std::lock_guard<std::mutex> guard(mutex_);
76 const auto item = cache_.find(id);
77 return (item != cache_.end()) ? &item->second : nullptr;
78 }
79
Size()80 size_t ImageCache::Size()
81 {
82 const std::lock_guard<std::mutex> guard(mutex_);
83 return cache_.size();
84 }
85
Consumption()86 size_t ImageCache::Consumption()
87 {
88 return consumption_;
89 }
90
Reset()91 void ImageCache::Reset()
92 {
93 id_ = 0;
94 consumption_ = 0u;
95 const std::lock_guard<std::mutex> guard(mutex_);
96 cache_.clear();
97 }
98
Serialize(Archive & archive)99 void ImageCache::Serialize(Archive& archive)
100 {
101 const std::lock_guard<std::mutex> guard(mutex_);
102 uint32_t count = cache_.size();
103 archive.Serialize(count);
104 for (auto& item : cache_) {
105 archive.Serialize(const_cast<uint64_t&>(item.first));
106 item.second.Serialize(archive);
107 }
108 }
109
110 // temporary: code has to be moved to Serialize due to Archive's architecture
Deserialize(Archive & archive)111 void ImageCache::Deserialize(Archive& archive)
112 {
113 Reset();
114
115 const std::lock_guard<std::mutex> guard(mutex_);
116 uint32_t count = 0u;
117 archive.Serialize(count);
118 for (uint32_t i = 0; i < count; i++) {
119 uint64_t id = 0u;
120 archive.Serialize(id);
121
122 Image image {};
123 image.Serialize(archive);
124 cache_.insert({ id, image });
125 }
126 }
127
128 // deprecated
Serialize(FILE * file)129 void ImageCache::Serialize(FILE* file)
130 {
131 FileWriter archive(file);
132 Serialize(archive);
133 }
134
135 // deprecated
Deserialize(FILE * file)136 void ImageCache::Deserialize(FILE* file)
137 {
138 FileReader archive(file);
139 Deserialize(archive);
140 }
141
142 // deprecated
Serialize(std::stringstream & stream)143 void ImageCache::Serialize(std::stringstream& stream)
144 {
145 StringStreamWriter archive(stream);
146 Serialize(archive);
147 }
148
149 // deprecated
Deserialize(std::stringstream & stream)150 void ImageCache::Deserialize(std::stringstream& stream)
151 {
152 StringStreamReader archive(stream);
153 Deserialize(archive);
154 }
155
Dump()156 std::string ImageCache::Dump()
157 {
158 std::string out;
159
160 const std::lock_guard<std::mutex> guard(mutex_);
161 for (const auto& item : cache_) {
162 out += std::to_string(Utils::ExtractPid(item.first)) + ":" + std::to_string(Utils::ExtractNodeId(item.first)) +
163 " ";
164 }
165
166 return out;
167 }
168
169 } // namespace OHOS::Rosen