1 /* 2 * Copyright (c) 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 OHOS_NATIVE_BUFFER_ADAPTER_IMPL_H 17 #define OHOS_NATIVE_BUFFER_ADAPTER_IMPL_H 18 19 #include "ohos_native_buffer_adapter.h" 20 #include "external_window.h" 21 #include "native_buffer.h" 22 #include "native_buffer_inner.h" 23 #include "surface_buffer.h" 24 #include "surface/window.h" 25 26 namespace OHOS::NWeb { 27 class OhosNativeBufferAdapterImpl : public OhosNativeBufferAdapter { 28 public: 29 static OhosNativeBufferAdapter& GetInstance(); 30 31 OhosNativeBufferAdapterImpl(); 32 33 ~OhosNativeBufferAdapterImpl() override; 34 35 void AcquireBuffer(void* buffer) override; 36 37 void Release(void* buffer) override; 38 39 int GetEGLBuffer(void* buffer, void** eglBuffer) override; 40 41 int FreeEGLBuffer(void* eglBuffer) override; 42 43 int NativeBufferFromNativeWindowBuffer(void* nativeWindowBuffer, void** nativeBuffer) override; 44 45 uint32_t GetSeqNum(void* nativeBuffer) override; 46 47 void Allocate(const std::shared_ptr<NativeBufferConfigAdapter> bufferConfig, void** outBuffer) override; 48 49 void Describe(std::shared_ptr<NativeBufferConfigAdapter> bufferConfig, void* buffer) override; 50 51 int Lock(void* buffer, uint64_t usage, int32_t fence, void** out_virtual_address) override; 52 53 int RecvHandleFromUnixSocket(int socketFd, void** outBuffer) override; 54 55 int SendHandleToUnixSocket(const void* buffer, int socketFd) override; 56 57 int Unlock(void* buffer, int32_t* fence) override; 58 59 int FreeNativeBuffer(void* nativeBuffer) override; 60 61 private: 62 bool IsBufferLocked(OH_NativeBuffer* buffer) const; 63 64 std::shared_ptr<std::mutex> mutex_; 65 std::unordered_map<OH_NativeBuffer*, bool> lockedBuffers_; 66 }; 67 68 } // namespace OHOS::NWeb 69 70 #endif // OHOS_NATIVE_BUFFER_ADAPTER_IMPL_H 71