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 INTERFACES_KITS_JS_COMMON_INCLUDE_IMAGE_NAPI_H_ 17 #define INTERFACES_KITS_JS_COMMON_INCLUDE_IMAGE_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 #include <map> 28 29 #include <surface.h> 30 #include "napi/native_api.h" 31 #include "napi/native_node_api.h" 32 #include "image_receiver.h" 33 #include "image_creator.h" 34 35 namespace OHOS { 36 namespace Media { 37 struct ImageAsyncContext; 38 struct Component { 39 int32_t rowStride = 0; 40 int32_t pixelStride = 0; 41 std::vector<uint8_t> raw; 42 }; 43 44 class ImageNapi { 45 public: 46 ImageNapi(); 47 ~ImageNapi(); 48 static napi_value Init(napi_env env, napi_value exports); 49 static std::shared_ptr<ImageNapi> GetImageSource(napi_env env, napi_value image); 50 static napi_value Create(napi_env env, sptr<SurfaceBuffer> surfaceBuffer, 51 std::shared_ptr<ImageReceiver> imageReceiver); 52 static napi_value Create(napi_env env, std::shared_ptr<ImageReceiver> imageReceiver); 53 static napi_value CreateBufferToImage(napi_env env, sptr<SurfaceBuffer> surfaceBuffer, 54 std::shared_ptr<ImageCreator> imageCreator); 55 void NativeRelease(); 56 sptr<SurfaceBuffer> sSurfaceBuffer_; 57 Component* CreateComponentData(ComponentType type, size_t size, int32_t rowStride, int32_t pixelStride); 58 Component* GetComponentData(ComponentType type); 59 uint32_t CombineComponentsIntoSurface(); 60 61 private: 62 static napi_value Constructor(napi_env env, napi_callback_info info); 63 static void Destructor(napi_env env, void *nativeObject, void *finalize); 64 65 static napi_value JSGetClipRect(napi_env env, napi_callback_info info); 66 static napi_value JsGetSize(napi_env env, napi_callback_info info); 67 static napi_value JsGetFormat(napi_env env, napi_callback_info info); 68 static napi_value JsGetComponent(napi_env env, napi_callback_info info); 69 static napi_value JsRelease(napi_env env, napi_callback_info info); 70 71 static napi_value BuildComponent(napi_env env, napi_callback_info info); 72 static std::unique_ptr<ImageAsyncContext> UnwarpContext(napi_env env, napi_callback_info info); 73 static void JsGetComponentCallBack(napi_env env, napi_status status, ImageAsyncContext* context); 74 75 void release(); 76 bool isRelease = false; 77 static thread_local napi_ref sConstructor_; 78 static sptr<SurfaceBuffer> staticInstance_; 79 static std::shared_ptr<ImageReceiver> staticImageReceiverInstance_; 80 static std::shared_ptr<ImageCreator> staticImageCreatorInstance_; 81 82 napi_env env_ = nullptr; 83 std::shared_ptr<ImageReceiver> imageReceiver_; 84 std::shared_ptr<ImageCreator> imageCreator_; 85 std::shared_ptr<ImageNapi> nativeImage_; 86 std::map<ComponentType, std::unique_ptr<Component>> componentData_; 87 }; 88 89 struct ImageAsyncContext { 90 napi_env env = nullptr; 91 napi_async_work work = nullptr; 92 napi_deferred deferred = nullptr; 93 napi_ref callbackRef = nullptr; 94 ImageNapi *constructor_ = nullptr; 95 uint32_t status; 96 int32_t componentType; 97 }; 98 } // namespace Media 99 } // namespace OHOS 100 #endif // INTERFACES_KITS_JS_COMMON_INCLUDE_IMAGE_NAPI_H_ 101