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 SK_RESOURCE_MANAGER_H
17 #define SK_RESOURCE_MANAGER_H
18 #include <list>
19 #include <map>
20 #include <mutex>
21 #include <set>
22 #include <unistd.h>
23 #include "utils/resource_holder.h"
24 #include "draw/surface.h"
25 #include "common/rs_macros.h"
26 
27 namespace OHOS::Rosen {
28 
29 #if defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK)
30 struct SharedTextureContext {
SharedTextureContextSharedTextureContext31     SharedTextureContext(std::shared_ptr<Drawing::Image> sharedImage)
32         : sharedImage_(sharedImage) {}
33 
34 private:
35     std::shared_ptr<Drawing::Image> sharedImage_;
36 };
37 #endif
38 
39 class RSB_EXPORT SKResourceManager final {
40 public:
41     static SKResourceManager& Instance();
42     void HoldResource(const std::shared_ptr<Drawing::Image> &img);
43     void HoldResource(std::shared_ptr<Drawing::Surface> surface);
44     void ReleaseResource();
45 #if defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK)
46     static void DeleteSharedTextureContext(void* context);
47 #endif
48 private:
49     SKResourceManager() = default;
50     ~SKResourceManager() = default;
51 
52     /* @name: HaveReleaseableResourceCheck.
53      * @desc: Check if there are any resources in the skSurfaces_ that can be released.
54      * @return Return False means there are no resources that can be released.
55      *         Return True means there maybe has resources that can be released.
56      */
57     bool HaveReleaseableResourceCheck(const std::list<std::shared_ptr<Drawing::Surface>> &list);
58 
59     std::recursive_mutex mutex_;
60     std::map<pid_t, std::shared_ptr<Drawing::ResourceHolder>> images_;
61     std::map<pid_t, std::list<std::shared_ptr<Drawing::Surface>>> skSurfaces_;
62 };
63 } // OHOS::Rosen
64 #endif // SK_RESOURCE_MANAGER_H