1 /*
2  * Copyright (c) 2023-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 #include "dps.h"
17 
18 #include "dp_catch.h"
19 
20 namespace OHOS {
21 namespace CameraStandard {
22 namespace DeferredProcessing {
23 struct DpsInfo {
24     std::atomic<bool> initialized_ {false};
25     std::mutex mutex;
26     std::shared_ptr<CommandServer> server;
27     std::shared_ptr<SessionManager> session;
28     std::shared_ptr<SchedulerManager> scheduler;
29 };
30 
31 DpsInfo g_dpsInfo;
32 
DPS_Initialize()33 int32_t DPS_Initialize()
34 {
35 PROSS
36     std::unique_lock<std::mutex> lock(g_dpsInfo.mutex);
37     if (g_dpsInfo.initialized_) {
38         DP_DEBUG_LOG("Already initialized.");
39         return DP_OK;
40     }
41     DP_DEBUG_LOG("entered.");
42     g_dpsInfo.server = std::make_shared<CommandServer>();
43     g_dpsInfo.session = SessionManager::Create();
44     g_dpsInfo.scheduler = std::make_shared<SchedulerManager>();
45     JUDEG(DP_NULL_POINTER, g_dpsInfo.server != nullptr);
46     JUDEG(DP_NULL_POINTER, g_dpsInfo.session != nullptr);
47     JUDEG(DP_NULL_POINTER, g_dpsInfo.scheduler != nullptr);
48     EXEC(g_dpsInfo.server->Initialize());
49     EXEC(g_dpsInfo.scheduler->Initialize());
50     g_dpsInfo.initialized_ = true;
51     DP_INFO_LOG("DPS_Initialize success.");
52     return DP_OK;
53 END_PROSS
54 CATCH_ERROR
55     DPS_Destroy();
56     DP_ERR_LOG("DPS_Initialize failed, line: %{public}u, error: %{public}u.", ERROR_LINE(), ERROR_CODE());
57     return ERROR_CODE();
58 END_CATCH_ERROR
59 }
60 
DPS_Destroy()61 void DPS_Destroy()
62 {
63     std::unique_lock<std::mutex> lock(g_dpsInfo.mutex);
64     DP_DEBUG_LOG("entered.");
65     if (!g_dpsInfo.initialized_) {
66         return;
67     }
68     g_dpsInfo.server.reset();
69     g_dpsInfo.initialized_ = false;
70     DP_INFO_LOG("DPS_Destory success.");
71 }
72 
DPS_GetCommandServer()73 std::shared_ptr<CommandServer> DPS_GetCommandServer()
74 {
75     std::unique_lock<std::mutex> lock(g_dpsInfo.mutex);
76     if (g_dpsInfo.server) {
77         return g_dpsInfo.server;
78     }
79     return nullptr;
80 }
81 
DPS_GetSessionManager()82 std::shared_ptr<SessionManager> DPS_GetSessionManager()
83 {
84     std::unique_lock<std::mutex> lock(g_dpsInfo.mutex);
85     if (g_dpsInfo.session) {
86         return g_dpsInfo.session;
87     }
88     return nullptr;
89 }
90 
DPS_GetSchedulerManager()91 std::shared_ptr<SchedulerManager> DPS_GetSchedulerManager()
92 {
93     std::unique_lock<std::mutex> lock(g_dpsInfo.mutex);
94     if (g_dpsInfo.scheduler) {
95         return g_dpsInfo.scheduler;
96     }
97     return nullptr;
98 }
99 } // namespace DeferredProcessing
100 } // namespace CameraStandard
101 } // namespace OHOS