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