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 = SchedulerManager::Create();
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.session.reset();
70 g_dpsInfo.scheduler.reset();
71 g_dpsInfo.initialized_ = false;
72 DP_INFO_LOG("DPS_Destroy success.");
73 }
74
DPS_GetCommandServer()75 std::shared_ptr<CommandServer> DPS_GetCommandServer()
76 {
77 std::unique_lock<std::mutex> lock(g_dpsInfo.mutex);
78 if (g_dpsInfo.server) {
79 return g_dpsInfo.server;
80 }
81 return nullptr;
82 }
83
DPS_GetSessionManager()84 std::shared_ptr<SessionManager> DPS_GetSessionManager()
85 {
86 std::unique_lock<std::mutex> lock(g_dpsInfo.mutex);
87 if (g_dpsInfo.session) {
88 return g_dpsInfo.session;
89 }
90 return nullptr;
91 }
92
DPS_GetSchedulerManager()93 std::shared_ptr<SchedulerManager> DPS_GetSchedulerManager()
94 {
95 std::unique_lock<std::mutex> lock(g_dpsInfo.mutex);
96 if (g_dpsInfo.scheduler) {
97 return g_dpsInfo.scheduler;
98 }
99 return nullptr;
100 }
101 } // namespace DeferredProcessing
102 } // namespace CameraStandard
103 } // namespace OHOS