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     int CheckRefCntAndReleaseImageCache(uint64_t uniqueId, std::shared_ptr<Media::PixelMap>& pixelMapIn,
50         const std::shared_ptr<Drawing::Image>& image);
51 
52     void CacheRenderDrawingImageByPixelMapId(uint64_t uniqueId, std::shared_ptr<Drawing::Image> img, pid_t tid = -1);
53     std::shared_ptr<Drawing::Image> GetRenderDrawingImageCacheByPixelMapId(uint64_t uniqueId, pid_t tid = -1) const;
54 
55     RSImageCache() = default;
56     ~RSImageCache() = default;
57     bool CheckUniqueIdIsEmpty();
58     void CollectUniqueId(uint64_t uniqueId);
59     void ReleaseUniqueIdList();
60 private:
61     RSImageCache(const RSImageCache&) = delete;
62     RSImageCache(const RSImageCache&&) = delete;
63     RSImageCache& operator=(const RSImageCache&) = delete;
64     RSImageCache& operator=(const RSImageCache&&) = delete;
65     void ReleaseDrawingImageCacheByPixelMapId(uint64_t uniqueId);
66 
67     mutable std::mutex mutex_;
68     // the second element of pair indicates ref count of skImage/pixelMap by RSImage
69     // ref count +1 in RSImage Unmarshalling func and -1 in RSImage destruction func
70     // skImage/pixelMap will be removed from cache if ref count decreases to 0
71     std::unordered_map<uint64_t, std::pair<std::shared_ptr<Drawing::Image>, uint64_t>> drawingImageCache_;
72     std::unordered_map<uint64_t, std::pair<std::shared_ptr<Media::PixelMap>, uint64_t>> pixelMapCache_;
73     mutable std::mutex mapMutex_;
74     std::unordered_map<uint64_t, std::unordered_map<pid_t, std::shared_ptr<Drawing::Image>>>
75         pixelMapIdRelatedDrawingImageCache_;
76     std::mutex uniqueIdListMutex_;
77     std::list<uint64_t> uniqueIdList_;
78 };
79 } // namespace Rosen
80 } // namespace OHOS
81 #endif // RENDER_SERVICE_CLIENT_CORE_RENDER_RS_IMAGE_CACHE_H
82