1 /* 2 * Copyright (c) 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 #ifndef NdkNativeWindow_NativeImageAdaptor_H 17 #define NdkNativeWindow_NativeImageAdaptor_H 18 19 #include <ace/xcomponent/native_interface_xcomponent.h> 20 #include <bits/alltypes.h> 21 #include <napi/native_api.h> 22 #include <native_buffer/native_buffer.h> 23 #include <native_image/native_image.h> 24 #include <native_window/external_window.h> 25 #include <EGL/egl.h> 26 #include <EGL/eglext.h> 27 #include <GLES3/gl3.h> 28 #include <sys/mman.h> 29 #include <mutex> 30 #include <queue> 31 32 namespace NativeWindowSample { 33 class NativeImageAdaptor { 34 public: 35 ~NativeImageAdaptor(); 36 static NativeImageAdaptor *GetInstance(); 37 static napi_value GetAvailableCount(napi_env env, napi_callback_info info); 38 static napi_value NapiOnChangeIsAutoConsumer(napi_env env, napi_callback_info info); 39 static napi_value NapiOnGetAttachBufferCount(napi_env env, napi_callback_info info); 40 static napi_value NapiOnGetBufferQueueSize(napi_env env, napi_callback_info info); 41 static napi_value NapiOnGetCacheBufferCount(napi_env env, napi_callback_info info); 42 static napi_value NapiOnProduceBuffer(napi_env env, napi_callback_info info); 43 static napi_value NapiOnConsumerBuffer(napi_env env, napi_callback_info info); 44 static napi_value NapiOnAttachBuffer(napi_env env, napi_callback_info info); 45 static napi_value NapiOnDettachBuffer(napi_env env, napi_callback_info info); 46 47 static void OnFrameAvailable(void *context); 48 void DealCallback(void *context); 49 bool Export(napi_env env, napi_value exports); 50 int32_t ProduceBuffer(uint32_t value, OHNativeWindow *InNativeWindow); 51 int32_t ConsumerBuffer(uint32_t value, OHNativeWindow *InNativeWindow); 52 void InitEGLEnv(); 53 bool InitNativeWindow(); 54 bool InitNativeWindowCache(); 55 void AttachBuffer(); 56 void DettachBuffer(); 57 EGLDisplay GetPlatformEglDisplay(EGLenum platform, void *native_display, const EGLint *attrib_list); 58 bool CheckEglExtension(const char *eglExtensions, const char *eglExtension); 59 int32_t GetCount(); 60 int32_t GetAttachBufferCount(); 61 int32_t GetBufferQueueSize(); 62 int32_t GetCacheBufferCount(); 63 bool ChangeIsAutoConsumer(); 64 65 private: 66 void SetConfigAndGetValue(); 67 void GetBufferMapPlanes(NativeWindowBuffer *buffer); 68 OHNativeWindow *nativeWindow_; 69 OHNativeWindow *nativeWindowCache_; 70 OH_NativeImage *image_; 71 OH_NativeImage *imageCache_; 72 int32_t height_; 73 int32_t width_; 74 int32_t availableBufferCount_; 75 EGLConfig config_; 76 EGLContext eglContext_; 77 EGLDisplay eglDisplay_; 78 std::mutex opMutex_; 79 std::queue<NativeWindowBuffer *> bufferCache_; 80 std::queue<NativeWindowBuffer *> bufferAttached_; 81 bool isAutoConsumer_; 82 }; 83 } 84 #endif // NdkNativeWindow_NativeImageAdaptor_H 85