1 /* 2 * Copyright (c) 2022 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_IMAGE_CACHE_H 17 #define RENDER_SERVICE_CLIENT_CORE_RENDER_RS_IMAGE_CACHE_H 18 19 #include <list> 20 #include <memory> 21 #include <unordered_map> 22 #include <vector> 23 #include "image/image.h" 24 25 #include "memory/rs_dfx_string.h" 26 #include "memory/rs_memory_track.h" 27 #include "render/rs_image_base.h" 28 29 namespace OHOS { 30 namespace Media { 31 class PixelMap; 32 } 33 34 namespace Rosen { 35 class RSImageBase; 36 class RSB_EXPORT RSImageCache { 37 public: 38 static RSImageCache& Instance(); 39 40 void CacheDrawingImage(uint64_t uniqueId, std::shared_ptr<Drawing::Image> img); 41 std::shared_ptr<Drawing::Image> GetDrawingImageCache(uint64_t uniqueId) const; 42 void IncreaseDrawingImageCacheRefCount(uint64_t uniqueId); 43 void ReleaseDrawingImageCache(uint64_t uniqueId); 44 45 void CachePixelMap(uint64_t uniqueId, std::shared_ptr<Media::PixelMap> pixelMap); 46 std::shared_ptr<Media::PixelMap> GetPixelMapCache(uint64_t uniqueId) const; 47 void IncreasePixelMapCacheRefCount(uint64_t uniqueId); 48 void ReleasePixelMapCache(uint64_t uniqueId); 49 bool CheckRefCntAndReleaseImageCache(uint64_t uniqueId, std::shared_ptr<Media::PixelMap>& pixelMapIn); 50 51 void CacheRenderDrawingImageByPixelMapId(uint64_t uniqueId, std::shared_ptr<Drawing::Image> img, pid_t tid = -1); 52 std::shared_ptr<Drawing::Image> GetRenderDrawingImageCacheByPixelMapId(uint64_t uniqueId, pid_t tid = -1) const; 53 54 RSImageCache() = default; 55 ~RSImageCache() = default; 56 bool CheckUniqueIdIsEmpty(); 57 void CollectUniqueId(uint64_t uniqueId); 58 void ReleaseUniqueIdList(); 59 private: 60 RSImageCache(const RSImageCache&) = delete; 61 RSImageCache(const RSImageCache&&) = delete; 62 RSImageCache& operator=(const RSImageCache&) = delete; 63 RSImageCache& operator=(const RSImageCache&&) = delete; 64 void ReleaseDrawingImageCacheByPixelMapId(uint64_t uniqueId); 65 66 mutable std::mutex mutex_; 67 // the second element of pair indicates ref count of skImage/pixelMap by RSImage 68 // ref count +1 in RSImage Unmarshalling func and -1 in RSImage destruction func 69 // skImage/pixelMap will be removed from cache if ref count decreases to 0 70 std::unordered_map<uint64_t, std::pair<std::shared_ptr<Drawing::Image>, uint64_t>> drawingImageCache_; 71 std::unordered_map<uint64_t, std::pair<std::shared_ptr<Media::PixelMap>, uint64_t>> pixelMapCache_; 72 mutable std::mutex mapMutex_; 73 std::unordered_map<uint64_t, std::unordered_map<pid_t, std::shared_ptr<Drawing::Image>>> 74 pixelMapIdRelatedDrawingImageCache_; 75 std::mutex uniqueIdListMutex_; 76 std::list<uint64_t> uniqueIdList_; 77 }; 78 } // namespace Rosen 79 } // namespace OHOS 80 #endif // RENDER_SERVICE_CLIENT_CORE_RENDER_RS_IMAGE_CACHE_H 81