1 /*
2  * Copyright (c) 2021-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 WEBGL_OBJECT_MANAGER_H
17 #define WEBGL_OBJECT_MANAGER_H
18 
19 #include <map>
20 
21 #include "context/webgl_rendering_context_basic_base.h"
22 
23 namespace OHOS {
24 namespace Rosen {
25 class ObjectManager {
26 public:
GetInstance()27     static ObjectManager& GetInstance()
28     {
29         static ObjectManager manager;
30         return manager;
31     }
32 
~ObjectManager()33     ~ObjectManager() {}
34 
GetWebGLContext(bool webGL2,const std::string & contextId)35     WebGLRenderingContextBasicBase* GetWebGLContext(bool webGL2, const std::string& contextId)
36     {
37         int index = webGL2 ? 1 : 0;
38         auto it = webGLObjects_[index].find(contextId);
39         if (it == webGLObjects_[index].end()) {
40             return nullptr;
41         }
42         return static_cast<WebGLRenderingContextBasicBase*>(it->second);
43     }
44 
AddWebGLObject(bool webGL2,const std::string & contextId,WebGLRenderingContextBasicBase * obj)45     void AddWebGLObject(bool webGL2, const std::string& contextId, WebGLRenderingContextBasicBase* obj)
46     {
47         int index = webGL2 ? 1 : 0;
48         webGLObjects_[index].insert({ contextId, obj });
49     }
50 
DeleteWebGLObject(bool webGL2,WebGLRenderingContextBasicBase * obj)51     void DeleteWebGLObject(bool webGL2, WebGLRenderingContextBasicBase* obj)
52     {
53         int index = webGL2 ? 1 : 0;
54         for (auto iter = webGLObjects_[index].begin(); iter != webGLObjects_[index].end(); iter++) {
55             if (iter->second == obj) {
56                 webGLObjects_[index].erase(iter);
57                 break;
58             }
59         }
60     }
61 
62 private:
63     ObjectManager() = default;
64     ObjectManager(const ObjectManager&) = delete;
65     ObjectManager& operator=(const ObjectManager&) = delete;
66     std::map<std::string, WebGLRenderingContextBasicBase*> webGLObjects_[2] {};
67 };
68 } // namespace Rosen
69 } // namespace OHOS
70 #endif // WEBGL_OBJECT_MANAGER_H
71