• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <unordered_map>
20 #ifndef USE_ROSEN_DRAWING
21 #include "include/core/SkImage.h"
22 #else
23 #include "image/image.h"
24 #endif
25 
26 #include "memory/rs_dfx_string.h"
27 #include "memory/rs_memory_track.h"
28 
29 namespace OHOS {
30 namespace Media {
31 class PixelMap;
32 }
33 
34 namespace Rosen {
35 class RSB_EXPORT RSImageCache {
36 public:
37     static RSImageCache& Instance();
38 
39 #ifndef USE_ROSEN_DRAWING
40     void CacheSkiaImage(uint64_t uniqueId, sk_sp<SkImage> img);
41     sk_sp<SkImage> GetSkiaImageCache(uint64_t uniqueId) const;
42     void IncreaseSkiaImageCacheRefCount(uint64_t uniqueId);
43     void ReleaseSkiaImageCache(uint64_t uniqueId);
44 #else
45     void CacheDrawingImage(uint64_t uniqueId, std::shared_ptr<Drawing::Image> img);
46     std::shared_ptr<Drawing::Image> GetDrawingImageCache(uint64_t uniqueId) const;
47     void IncreaseDrawingImageCacheRefCount(uint64_t uniqueId);
48     void ReleaseDrawingImageCache(uint64_t uniqueId);
49 #endif
50 
51     void CachePixelMap(uint64_t uniqueId, std::shared_ptr<Media::PixelMap> pixelMap);
52     std::shared_ptr<Media::PixelMap> GetPixelMapCache(uint64_t uniqueId) const;
53     void IncreasePixelMapCacheRefCount(uint64_t uniqueId);
54     void ReleasePixelMapCache(uint64_t uniqueId);
55 
56 #ifndef USE_ROSEN_DRAWING
57     void CacheRenderSkiaImageByPixelMapId(uint64_t uniqueId, sk_sp<SkImage> img);
58     sk_sp<SkImage> GetRenderSkiaImageCacheByPixelMapId(uint64_t uniqueId) const;
59 #else
60     void CacheRenderDrawingImageByPixelMapId(uint64_t uniqueId, std::shared_ptr<Drawing::Image> img);
61     std::shared_ptr<Drawing::Image> GetRenderDrawingImageCacheByPixelMapId(uint64_t uniqueId) const;
62 #endif
63 
64     RSImageCache() = default;
65     ~RSImageCache() = default;
66 
67 private:
68     RSImageCache(const RSImageCache&) = delete;
69     RSImageCache(const RSImageCache&&) = delete;
70     RSImageCache& operator=(const RSImageCache&) = delete;
71     RSImageCache& operator=(const RSImageCache&&) = delete;
72 #ifndef USE_ROSEN_DRAWING
73     void ReleaseSkiaImageCacheByPixelMapId(uint64_t uniqueId);
74 #else
75     void ReleaseDrawingImageCacheByPixelMapId(uint64_t uniqueId);
76 #endif
77 
78     mutable std::mutex mutex_;
79     // the second element of pair indicates ref count of skImage/pixelMap by RSImage
80     // ref count +1 in RSImage Unmarshalling func and -1 in RSImage destruction func
81     // skImage/pixelMap will be removed from cache if ref count decreases to 0
82 #ifndef USE_ROSEN_DRAWING
83     std::unordered_map<uint64_t, std::pair<sk_sp<SkImage>, uint64_t>> skiaImageCache_;
84 #else
85     std::unordered_map<uint64_t, std::pair<std::shared_ptr<Drawing::Image>, uint64_t>> drawingImageCache_;
86 #endif
87     std::unordered_map<uint64_t, std::pair<std::shared_ptr<Media::PixelMap>, uint64_t>> pixelMapCache_;
88     mutable std::mutex mapMutex_;
89 #ifndef USE_ROSEN_DRAWING
90     std::unordered_map<uint64_t, sk_sp<SkImage>> pixelMapIdRelatedSkiaImageCache_;
91 #else
92     std::unordered_map<uint64_t, std::shared_ptr<Drawing::Image>> pixelMapIdRelatedDrawingImageCache_;
93 #endif
94 };
95 } // namespace Rosen
96 } // namespace OHOS
97 #endif // RENDER_SERVICE_CLIENT_CORE_RENDER_RS_IMAGE_CACHE_H
98