1 /*
2  * Copyright (c) 2024-2024 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 CAMERA_NAPI_CONST_UTILS_H
17 #define CAMERA_NAPI_CONST_UTILS_H
18 
19 #include <chrono>
20 #include <cstddef>
21 #include <cstdint>
22 #include <list>
23 #include <memory>
24 #include <string>
25 
26 #include "js_native_api.h"
27 #include "js_native_api_types.h"
28 #include "napi/native_api.h"
29 
30 namespace OHOS {
31 namespace CameraStandard {
32 /* Constants for array index */
33 const int32_t PARAM0 = 0;
34 const int32_t PARAM1 = 1;
35 const int32_t PARAM2 = 2;
36 
37 /* Constants for array size */
38 const int32_t ARGS_ZERO = 0;
39 const int32_t ARGS_ONE = 1;
40 const int32_t ARGS_TWO = 2;
41 const int32_t ARGS_THREE = 3;
42 const size_t ARGS_MAX_SIZE = 20;
43 const int32_t SIZE = 100;
44 
45 typedef std::chrono::time_point<std::chrono::steady_clock, std::chrono::nanoseconds> NapiWorkerQueueTaskTimePoint;
46 enum NapiWorkerQueueStatus : int32_t { INIT = 0, RUNNING, DONE };
47 struct NapiWorkerQueueTask {
NapiWorkerQueueTaskNapiWorkerQueueTask48     explicit NapiWorkerQueueTask(std::string taskName) : taskName(taskName)
49     {
50         createTimePoint = std::chrono::steady_clock::now();
51     }
52 
53     std::string taskName;
54     int32_t waitCount = 0;
55     NapiWorkerQueueTaskTimePoint createTimePoint;
56     NapiWorkerQueueStatus queueStatus = INIT;
57 };
58 
59 struct AsyncContext {
60 public:
61     AsyncContext() = default;
AsyncContextAsyncContext62     AsyncContext(std::string funcName, int32_t taskId) : funcName(funcName), taskId(taskId) {};
63 
64     virtual ~AsyncContext() = default;
65 
HoldNapiValueAsyncContext66     void HoldNapiValue(napi_env env, napi_value napiValue)
67     {
68         if (env == nullptr || napiValue == nullptr) {
69             return;
70         }
71         napi_ref ref = nullptr;
72         napi_status status = napi_create_reference(env, napiValue, 1, &ref);
73         if (status == napi_ok && ref != nullptr) {
74             heldRefs.emplace_back(ref);
75         }
76     }
77 
FreeHeldNapiValueAsyncContext78     void FreeHeldNapiValue(napi_env env)
79     {
80         if (env == nullptr) {
81             return;
82         }
83         for (auto& ref : heldRefs) {
84             napi_delete_reference(env, ref);
85         }
86         heldRefs.clear();
87     }
88 
89     std::string funcName;
90     int32_t taskId = 0;
91     napi_async_work work = nullptr;
92     napi_deferred deferred = nullptr;
93     napi_ref callbackRef = nullptr;
94     bool status = false;
95 
96     int32_t errorCode = 0;
97     std::shared_ptr<NapiWorkerQueueTask> queueTask;
98     std::string errorMsg;
99 
100 private:
101     std::list<napi_ref> heldRefs;
102 };
103 
104 struct JSAsyncContextOutput {
105     napi_value error;
106     napi_value data;
107     bool status;
108     std::string funcName;
109 };
110 
111 enum JSQualityLevel { QUALITY_LEVEL_HIGH = 0, QUALITY_LEVEL_MEDIUM, QUALITY_LEVEL_LOW };
112 
113 enum JSImageRotation { ROTATION_0 = 0, ROTATION_90 = 90, ROTATION_180 = 180, ROTATION_270 = 270 };
114 
115 enum JSCameraStatus {
116     JS_CAMERA_STATUS_APPEAR = 0,
117     JS_CAMERA_STATUS_DISAPPEAR = 1,
118     JS_CAMERA_STATUS_AVAILABLE = 2,
119     JS_CAMERA_STATUS_UNAVAILABLE = 3
120 };
121 
122 enum CameraTaskId {
123     CAMERA_MANAGER_TASKID = 0x01000000,
124     CAMERA_INPUT_TASKID = 0x02000000,
125     CAMERA_PHOTO_OUTPUT_TASKID = 0x03000000,
126     CAMERA_PREVIEW_OUTPUT_TASKID = 0x04000000,
127     CAMERA_VIDEO_OUTPUT_TASKID = 0x05000000,
128     CAMERA_SESSION_TASKID = 0x06000000,
129     MODE_MANAGER_TASKID = 0x07000000,
130     CAMERA_PICKER_TASKID = 0x08000000,
131     PHOTO_TASKID = 0x09000000,
132     DEFERRED_PHOTO_PROXY_TASKID = 0x0A000000,
133     CAMERA_DEPTH_DATA_OUTPUT_TASKID = 0x0B000000,
134     DEPTH_DATA_TASKID = 0x0C000000,
135     CAMERA_METADATA_OUTPUT_TASKID = 0x0D000000,
136 };
137 
138 enum JSMetadataObjectType {
139     INVALID = -1,
140     FACE_DETECTION = 0,
141     HUMAN_BODY,
142     CAT_FACE,
143     CAT_BODY,
144     DOG_FACE,
145     DOG_BODY,
146     SALIENT_DETECTION,
147     BAR_CODE_DETECTION
148 };
149 
150 enum CameraSteps {
151     CREATE_CAMERA_INPUT_INSTANCE,
152     CREATE_PREVIEW_OUTPUT_INSTANCE,
153     CREATE_PHOTO_OUTPUT_INSTANCE,
154     CREATE_VIDEO_OUTPUT_INSTANCE,
155     CREATE_METADATA_OUTPUT_INSTANCE,
156     ADD_INPUT,
157     REMOVE_INPUT,
158     ADD_OUTPUT,
159     REMOVE_OUTPUT,
160     PHOTO_OUT_CAPTURE,
161     PHOTO_OUT_BURST_CAPTURE,
162     ADD_DEFERRED_SURFACE,
163     CREATE_DEFERRED_PREVIEW_OUTPUT
164 };
165 } // namespace CameraStandard
166 } // namespace OHOS
167 #endif