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 RS_PROFILER_CACHE_H
17 #define RS_PROFILER_CACHE_H
18 
19 #include <map>
20 #include <mutex>
21 #include <sstream>
22 
23 #ifdef REPLAY_TOOL_CLIENT
24 #include <vector>
25 #else
26 #include "common/rs_macros.h"
27 #endif
28 
29 namespace OHOS::Rosen {
30 
31 class Archive;
32 
33 struct Image final {
34 public:
35     static constexpr size_t maxSize = 600 * 1024 * 1024; // NOLINT
36 
37 public:
38     explicit Image() = default;
39 
40     void Serialize(Archive& archive);
41     bool IsValid() const;
42 
43 public:
44     std::vector<uint8_t> data;
45     uint64_t parcelSkipBytes = 0u;
46     int32_t dmaWidth = 0;
47     int32_t dmaHeight = 0;
48     int32_t dmaStride = 0;
49     int32_t dmaFormat = 0;
50     uint64_t dmaUsage = 0u;
51     size_t dmaSize = 0u;
52 };
53 
54 class RSB_EXPORT ImageCache final {
55 public:
56     static uint64_t New();
57     static bool Add(uint64_t id, Image&& image);
58     static bool Exists(uint64_t id);
59     static Image* Get(uint64_t id);
60     static size_t Size();
61     static void Reset();
62 
63     static std::string Dump();
64 
65     static void Serialize(Archive& archive);
66     static void Deserialize(Archive& archive);
67 
68     // deprecated
69     static void Serialize(FILE* file);
70     static void Deserialize(FILE* file);
71     static void Serialize(std::stringstream& stream);
72     static void Deserialize(std::stringstream& stream);
73 
74 private:
75     static std::mutex mutex_;
76     static std::map<uint64_t, Image> cache_;
77 };
78 
79 } // namespace OHOS::Rosen
80 
81 #endif // RS_PROFILER_CACHE_H