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 JsOff(napi_env env, napi_callback_info info); 70 static napi_value JsRelease(napi_env env, napi_callback_info info); 71 72 static bool GetNativeFromEnv(napi_env env, napi_callback_info info, std::shared_ptr<ImageCreator> &native); 73 static napi_value JSCommonProcess(ImageCreatorCommonArgs &args); 74 static void JsQueueImageCallBack(napi_env env, napi_status status, 75 ImageCreatorAsyncContext* context); 76 static napi_value JsOffOneArg(napi_env env, napi_callback_info info); 77 static napi_value JsOffTwoArgs(napi_env env, napi_callback_info info); 78 #ifdef IMAGE_DEBUG_FLAG 79 static napi_value JsTest(napi_env env, napi_callback_info info); 80 #endif 81 void release(); 82 bool isRelease = false; 83 84 static thread_local napi_ref sConstructor_; 85 static std::shared_ptr<ImageCreator> staticInstance_; 86 87 napi_env env_ = nullptr; 88 std::shared_ptr<ImageCreator> imageCreator_; 89 }; 90 struct ImageCreatorAsyncContext { 91 napi_env env = nullptr; 92 napi_async_work work = nullptr; 93 napi_deferred deferred = nullptr; 94 napi_ref callbackRef = nullptr; 95 ImageCreatorNapi *constructor_ = nullptr; 96 std::shared_ptr<ImageCreator> creator_ = nullptr; 97 uint32_t status; 98 sptr<SurfaceBuffer> surfaceBuffer; 99 std::shared_ptr<ImageSource> imageSource_; 100 std::shared_ptr<NativeImage> imageNapi_; 101 }; 102 struct ImageCreatorInnerContext { 103 napi_status status; 104 napi_value result = nullptr; 105 napi_value thisVar = nullptr; 106 size_t argc; 107 std::vector<napi_value> argv; 108 std::string onType; 109 int32_t refCount = 1; 110 std::unique_ptr<ImageCreatorAsyncContext> context = nullptr; 111 }; 112 113 using CreatorCommonFunc = bool (*)(ImageCreatorCommonArgs &args, ImageCreatorInnerContext &ic); 114 115 enum class CreatorCallType : uint32_t { 116 STATIC = 0, 117 GETTER = 1, 118 ASYNC = 2, 119 }; 120 121 struct ImageCreatorCommonArgs { 122 napi_env env; 123 napi_callback_info info; 124 CreatorCallType async; 125 const std::string name; 126 CompleteCreatorCallback callBack; 127 size_t argc; 128 CreatorCommonFunc queryArgs; 129 CreatorCommonFunc nonAsyncBack; 130 bool asyncLater = false; 131 }; 132 133 class ImageCreatorReleaseListener : public SurfaceBufferReleaseListener { 134 public: ~ImageCreatorReleaseListener()135 ~ImageCreatorReleaseListener() override 136 { 137 context = nullptr; 138 callBack = nullptr; 139 } OnSurfaceBufferRelease()140 void OnSurfaceBufferRelease() override 141 { 142 ImageCreatorNapi::DoCallBack(context, name, callBack); 143 } 144 std::shared_ptr<ImageCreatorAsyncContext> context = nullptr; 145 std::string name; 146 CompleteCreatorCallback callBack = nullptr; 147 }; 148 149 class CreatorSurfaceListener : public SurfaceBufferAvaliableListener { 150 public: 151 void OnSurfaceBufferAvaliable() override; 152 }; 153 } // namespace Media 154 } // namespace OHOS 155 #endif /* IMAGE_CREATOR_NAPI_H */ 156