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 RENDER_PROFILER_APPLICATION 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 size_t Size() const; 41 void Serialize(Archive& archive); 42 bool IsValid() const; 43 44 public: 45 std::vector<uint8_t> data; 46 uint64_t parcelSkipBytes = 0u; 47 int32_t dmaWidth = 0; 48 int32_t dmaHeight = 0; 49 int32_t dmaStride = 0; 50 int32_t dmaFormat = 0; 51 uint64_t dmaUsage = 0u; 52 size_t dmaSize = 0u; 53 }; 54 55 class RSB_EXPORT ImageCache final { 56 public: 57 static uint64_t New(); 58 static bool Add(uint64_t id, Image&& image); 59 static bool Exists(uint64_t id); 60 static Image* Get(uint64_t id); 61 static size_t Size(); 62 static size_t Consumption(); 63 static void Reset(); 64 65 static std::string Dump(); 66 67 static void Serialize(Archive& archive); 68 static void Deserialize(Archive& archive); 69 70 // deprecated 71 static void Serialize(FILE* file); 72 static void Deserialize(FILE* file); 73 static void Serialize(std::stringstream& stream); 74 static void Deserialize(std::stringstream& stream); 75 76 private: 77 static std::atomic_uint64_t id_; 78 static std::mutex mutex_; 79 static std::map<uint64_t, Image> cache_; 80 static std::atomic_size_t consumption_; 81 }; 82 83 } // namespace OHOS::Rosen 84 85 #endif // RS_PROFILER_CACHE_H