• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 IMAGE_CREATOR_NAPI_H_
17 #define IMAGE_CREATOR_NAPI_H_
18 
19 #include <cerrno>
20 #include <dirent.h>
21 #include <fcntl.h>
22 #include <ftw.h>
23 #include <securec.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <variant>
27 
28 #include "napi/native_api.h"
29 #include "napi/native_node_api.h"
30 #include "pixel_map.h"
31 #include "image_creator.h"
32 #include "image_napi_utils.h"
33 #include "image_source.h"
34 #include "image_napi.h"
35 #include "image_receiver_napi.h"
36 #include "image_receiver.h"
37 
38 
39 namespace OHOS {
40 namespace Media {
41 struct ImageCreatorCommonArgs;
42 struct ImageCreatorAsyncContext;
43 using Contextc = ImageCreatorAsyncContext* ;
44 using CompleteCreatorCallback = void (*)(napi_env env, napi_status status, Contextc context);
45 class ImageCreatorNapi {
46 public:
47     ImageCreatorNapi();
48     ~ImageCreatorNapi();
49     static napi_value Init(napi_env env, napi_value exports);
50     static void DoCallBack(std::shared_ptr<ImageCreatorAsyncContext> context,
51                            std::string name,
52                            CompleteCreatorCallback callBack);
53     void NativeRelease();
54 #ifdef IMAGE_DEBUG_FLAG
55     bool isCallBackTest = false;
56 #endif
57 
58 private:
59     static napi_value Constructor(napi_env env, napi_callback_info info);
60     static void Destructor(napi_env env, void *nativeObject, void *finalize);
61 
62     static napi_value JSCreateImageCreator(napi_env env, napi_callback_info info);
63     static napi_value JsGetCapacity(napi_env env, napi_callback_info info);
64     static napi_value JsGetFormat(napi_env env, napi_callback_info info);
65     static napi_value JsGetSize(napi_env env, napi_callback_info info);
66     static napi_value JsDequeueImage(napi_env env, napi_callback_info info);
67     static napi_value JsQueueImage(napi_env env, napi_callback_info info);
68     static napi_value JsOn(napi_env env, napi_callback_info info);
69     static napi_value JsRelease(napi_env env, napi_callback_info info);
70 
71     static bool GetNativeFromEnv(napi_env env, napi_callback_info info, std::shared_ptr<ImageCreator> &native);
72     static napi_value JSCommonProcess(ImageCreatorCommonArgs &args);
73     static void JsQueueImageCallBack(napi_env env, napi_status status,
74                                      ImageCreatorAsyncContext* context);
75 #ifdef IMAGE_DEBUG_FLAG
76     static napi_value JsTest(napi_env env, napi_callback_info info);
77 #endif
78     void release();
79     bool isRelease = false;
80 
81     static thread_local napi_ref sConstructor_;
82     static std::shared_ptr<ImageCreator> staticInstance_;
83 
84     napi_env env_ = nullptr;
85     std::shared_ptr<ImageCreator> imageCreator_;
86 };
87 struct ImageCreatorAsyncContext {
88     napi_env env = nullptr;
89     napi_async_work work = nullptr;
90     napi_deferred deferred = nullptr;
91     napi_ref callbackRef = nullptr;
92     ImageCreatorNapi *constructor_ = nullptr;
93     std::shared_ptr<ImageCreator> creator_ = nullptr;
94     uint32_t status;
95     sptr<SurfaceBuffer> surfaceBuffer;
96     std::shared_ptr<ImageSource> imageSource_;
97     std::shared_ptr<NativeImage> imageNapi_;
98 };
99 struct ImageCreatorInnerContext {
100     napi_status status;
101     napi_value result = nullptr;
102     napi_value thisVar = nullptr;
103     size_t argc;
104     std::vector<napi_value> argv;
105     std::string onType;
106     int32_t refCount = 1;
107     std::unique_ptr<ImageCreatorAsyncContext> context = nullptr;
108 };
109 
110 using CreatorCommonFunc = bool (*)(ImageCreatorCommonArgs &args, ImageCreatorInnerContext &ic);
111 
112 enum class CreatorCallType : uint32_t {
113     STATIC = 0,
114     GETTER = 1,
115     ASYNC = 2,
116 };
117 
118 struct ImageCreatorCommonArgs {
119     napi_env env;
120     napi_callback_info info;
121     CreatorCallType async;
122     const std::string name;
123     CompleteCreatorCallback callBack;
124     size_t argc;
125     CreatorCommonFunc queryArgs;
126     CreatorCommonFunc nonAsyncBack;
127     bool asyncLater = false;
128 };
129 
130 class ImageCreatorReleaseListener : public SurfaceBufferReleaseListener {
131 public:
~ImageCreatorReleaseListener()132     ~ImageCreatorReleaseListener() override
133     {
134         context = nullptr;
135         callBack = nullptr;
136     }
OnSurfaceBufferRelease()137     void OnSurfaceBufferRelease() override
138     {
139         ImageCreatorNapi::DoCallBack(context, name, callBack);
140     }
141     std::shared_ptr<ImageCreatorAsyncContext> context = nullptr;
142     std::string name;
143     CompleteCreatorCallback callBack = nullptr;
144 };
145 
146 class CreatorSurfaceListener : public SurfaceBufferAvaliableListener {
147 public:
148         void OnSurfaceBufferAvaliable() override;
149     };
150 } // namespace Media
151 } // namespace OHOS
152 #endif /* IMAGE_CREATOR_NAPI_H_ */
153