1 /*
2  * Copyright (c) 2023 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 SKIA_RESOURCE_HOLDER_H
17 #define SKIA_RESOURCE_HOLDER_H
18 #include <unordered_map>
19 
20 #include "impl_interface/resource_holder_impl.h"
21 #include "skia_image.h"
22 
23 namespace OHOS {
24 namespace Rosen {
25 namespace Drawing {
26 constexpr uint32_t MAX_CHECK_SIZE = 20; // empirical value
27 class SkiaResourceHolder : public ResourceHolderImpl {
28 public:
29     static inline constexpr AdapterType TYPE = AdapterType::SKIA_ADAPTER;
30 
31     SkiaResourceHolder() noexcept = default;
~SkiaResourceHolder()32     ~SkiaResourceHolder() override {};
33 
GetType()34     AdapterType GetType() const override
35     {
36         return AdapterType::SKIA_ADAPTER;
37     }
38 
HoldResource(const std::shared_ptr<Image> & img)39     void HoldResource(const std::shared_ptr<Image>& img) override
40     {
41         uintptr_t id = uintptr_t(img.get());
42         if (images_.find(id) != images_.end()) {
43             return;
44         }
45         images_.emplace(id, img);
46     }
47 
ReleaseResource()48     void ReleaseResource() override
49     {
50         auto iter = images_.begin();
51         while (iter != images_.end()) {
52             if (iter->second.use_count() == 1) {
53                 auto tmp = iter++;
54                 images_.erase(tmp);
55             } else {
56                 ++iter;
57             }
58         }
59     }
60 
IsEmpty()61     bool IsEmpty() const override
62     {
63         return images_.empty();
64     }
65 
HaveReleaseableResourceCheck()66     bool HaveReleaseableResourceCheck() override
67     {
68         if (images_.empty()) {
69             return false;
70         }
71         if (images_.size() > MAX_CHECK_SIZE) {
72             /* to avoid this function taking too long, return true directly,
73              * which means resources may need to be released
74              */
75             return true;
76         }
77         auto iter = images_.begin();
78         while (iter != images_.end()) {
79             if (iter->second.use_count() == 1) {
80                 return true;
81             }
82             ++iter;
83         }
84         return false;
85     }
86 
87 private:
88     std::unordered_map<uintptr_t, const std::shared_ptr<Image>> images_;
89 };
90 } // namespace Drawing
91 } // namespace Rosen
92 } // namespace OHOS
93 #endif // SKIA_RESOURCE_HOLDER_H