diff --git a/camera/common/1.0/default/Android.bp b/camera/common/1.0/default/Android.bp index 10f9fdb5fd..03a71fad27 100644 --- a/camera/common/1.0/default/Android.bp +++ b/camera/common/1.0/default/Android.bp @@ -15,7 +15,8 @@ cc_library_static { shared_libs: [ "liblog", "libhardware", - "libcamera_metadata"], + "libcamera_metadata", + "android.hardware.graphics.mapper@2.0"], include_dirs: ["system/media/private/camera/include"], export_include_dirs : ["include"] } diff --git a/camera/common/1.0/default/HandleImporter.cpp b/camera/common/1.0/default/HandleImporter.cpp index e22f26f3d1..fd8b943dc3 100644 --- a/camera/common/1.0/default/HandleImporter.cpp +++ b/camera/common/1.0/default/HandleImporter.cpp @@ -25,33 +25,27 @@ namespace common { namespace V1_0 { namespace helper { -HandleImporter HandleImporter::sHandleImporter; +using MapperError = android::hardware::graphics::mapper::V2_0::Error; -HandleImporter& HandleImporter::getInstance() { - sHandleImporter.initialize(); - return sHandleImporter; -} +HandleImporter::HandleImporter() : mInitialized(false) {} -bool HandleImporter::initialize() { - // allow only one client +void HandleImporter::initializeLocked() { if (mInitialized) { - return false; - } - - if (!openGralloc()) { - return false; - } - - mInitialized = true; - return true; -} - -void HandleImporter::cleanup() { - if (!mInitialized) { return; } - closeGralloc(); + mMapper = IMapper::getService(); + if (mMapper == nullptr) { + ALOGE("%s: cannnot acccess graphics mapper HAL!", __FUNCTION__); + return; + } + + mInitialized = true; + return; +} + +void HandleImporter::cleanup() { + mMapper.clear(); mInitialized = false; } @@ -64,12 +58,37 @@ bool HandleImporter::importBuffer(buffer_handle_t& handle) { return true; } - buffer_handle_t clone = cloneBuffer(handle); - if (!clone) { + Mutex::Autolock lock(mLock); + if (!mInitialized) { + initializeLocked(); + } + + if (mMapper == nullptr) { + ALOGE("%s: mMapper is null!", __FUNCTION__); return false; } - handle = clone; + MapperError error; + buffer_handle_t importedHandle; + auto ret = mMapper->importBuffer( + hidl_handle(handle), + [&](const auto& tmpError, const auto& tmpBufferHandle) { + error = tmpError; + importedHandle = static_cast(tmpBufferHandle); + }); + + if (!ret.isOk()) { + ALOGE("%s: mapper importBuffer failed: %s", + __FUNCTION__, ret.description().c_str()); + return false; + } + + if (error != MapperError::NONE) { + return false; + } + + handle = importedHandle; + return true; } @@ -78,10 +97,20 @@ void HandleImporter::freeBuffer(buffer_handle_t handle) { return; } - releaseBuffer(handle); + Mutex::Autolock lock(mLock); + if (mMapper == nullptr) { + ALOGE("%s: mMapper is null!", __FUNCTION__); + return; + } + + auto ret = mMapper->freeBuffer(const_cast(handle)); + if (!ret.isOk()) { + ALOGE("%s: mapper freeBuffer failed: %s", + __FUNCTION__, ret.description().c_str()); + } } -bool HandleImporter::importFence(const native_handle_t* handle, int& fd) { +bool HandleImporter::importFence(const native_handle_t* handle, int& fd) const { if (handle == nullptr || handle->numFds == 0) { fd = -1; } else if (handle->numFds == 1) { @@ -99,89 +128,12 @@ bool HandleImporter::importFence(const native_handle_t* handle, int& fd) { return true; } -void HandleImporter::closeFence(int fd) { +void HandleImporter::closeFence(int fd) const { if (fd >= 0) { close(fd); } } -bool HandleImporter::openGralloc() { - const hw_module_t* module; - int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); - if (err) { - ALOGE("failed to get gralloc module"); - return false; - } - - uint8_t major = (module->module_api_version >> 8) & 0xff; - if (major > 1) { - ALOGE("unknown gralloc module major version %d", major); - return false; - } - - if (major == 1) { - err = gralloc1_open(module, &mDevice); - if (err) { - ALOGE("failed to open gralloc1 device"); - return false; - } - - mRetain = reinterpret_cast( - mDevice->getFunction(mDevice, GRALLOC1_FUNCTION_RETAIN)); - mRelease = reinterpret_cast( - mDevice->getFunction(mDevice, GRALLOC1_FUNCTION_RELEASE)); - if (!mRetain || !mRelease) { - ALOGE("invalid gralloc1 device"); - gralloc1_close(mDevice); - return false; - } - } else { - mModule = reinterpret_cast(module); - } - - return true; -} - -void HandleImporter::closeGralloc() { - if (mDevice) { - gralloc1_close(mDevice); - } -} - -buffer_handle_t HandleImporter::cloneBuffer(buffer_handle_t handle) { - native_handle_t* clone = native_handle_clone(handle); - if (!clone) { - ALOGE("failed to clone buffer %p", handle); - return nullptr; - } - - bool err; - if (mDevice) { - err = (mRetain(mDevice, clone) != GRALLOC1_ERROR_NONE); - } else { - err = (mModule->registerBuffer(mModule, clone) != 0); - } - - if (err) { - ALOGE("failed to retain/register buffer %p", clone); - native_handle_close(clone); - native_handle_delete(clone); - return nullptr; - } - - return clone; -} - -void HandleImporter::releaseBuffer(buffer_handle_t handle) { - if (mDevice) { - mRelease(mDevice, handle); - } else { - mModule->unregisterBuffer(mModule, handle); - } - native_handle_close(handle); - native_handle_delete(const_cast(handle)); -} - } // namespace helper } // namespace V1_0 } // namespace common diff --git a/camera/common/1.0/default/include/HandleImporter.h b/camera/common/1.0/default/include/HandleImporter.h index def898278c..c68cfc0689 100644 --- a/camera/common/1.0/default/include/HandleImporter.h +++ b/camera/common/1.0/default/include/HandleImporter.h @@ -17,9 +17,11 @@ #ifndef CAMERA_COMMON_1_0_HANDLEIMPORTED_H #define CAMERA_COMMON_1_0_HANDLEIMPORTED_H -#include -#include #include +#include +#include + +using android::hardware::graphics::mapper::V2_0::IMapper; namespace android { namespace hardware { @@ -31,36 +33,24 @@ namespace helper { // Borrowed from graphics HAL. Use this until gralloc mapper HAL is working class HandleImporter { public: - static HandleImporter& getInstance(); + HandleImporter(); // In IComposer, any buffer_handle_t is owned by the caller and we need to // make a clone for hwcomposer2. We also need to translate empty handle // to nullptr. This function does that, in-place. bool importBuffer(buffer_handle_t& handle); void freeBuffer(buffer_handle_t handle); - bool importFence(const native_handle_t* handle, int& fd); - void closeFence(int fd); + bool importFence(const native_handle_t* handle, int& fd) const; + void closeFence(int fd) const; private: - - HandleImporter() : mInitialized(false) {} - bool initialize(); + void initializeLocked(); void cleanup(); - bool openGralloc(); - void closeGralloc(); - buffer_handle_t cloneBuffer(buffer_handle_t handle); - void releaseBuffer(buffer_handle_t handle); - static HandleImporter sHandleImporter; + Mutex mLock; bool mInitialized; + sp mMapper; - // gralloc1 - gralloc1_device_t* mDevice; - GRALLOC1_PFN_RETAIN mRetain; - GRALLOC1_PFN_RELEASE mRelease; - - // gralloc0 - const gralloc_module_t* mModule; }; } // namespace helper diff --git a/camera/device/1.0/default/Android.bp b/camera/device/1.0/default/Android.bp index af94b0f379..c2f0c46797 100644 --- a/camera/device/1.0/default/Android.bp +++ b/camera/device/1.0/default/Android.bp @@ -14,6 +14,7 @@ cc_library_shared { "android.hardware.camera.device@1.0", "android.hardware.camera.common@1.0", "android.hardware.graphics.allocator@2.0", + "android.hardware.graphics.mapper@2.0", "android.hardware.graphics.common@1.0", "android.hidl.allocator@1.0", "android.hidl.base@1.0", diff --git a/camera/device/1.0/default/CameraDevice.cpp b/camera/device/1.0/default/CameraDevice.cpp index cb20fec80f..cdc9de2aa0 100644 --- a/camera/device/1.0/default/CameraDevice.cpp +++ b/camera/device/1.0/default/CameraDevice.cpp @@ -33,7 +33,7 @@ namespace implementation { using ::android::hardware::graphics::common::V1_0::BufferUsage; using ::android::hardware::graphics::common::V1_0::PixelFormat; -HandleImporter& CameraDevice::sHandleImporter = HandleImporter::getInstance(); +HandleImporter CameraDevice::sHandleImporter; Status CameraDevice::getHidlStatus(const int& status) { switch (status) { diff --git a/camera/device/1.0/default/CameraDevice_1_0.h b/camera/device/1.0/default/CameraDevice_1_0.h index a9f55c2172..4240d57eb8 100644 --- a/camera/device/1.0/default/CameraDevice_1_0.h +++ b/camera/device/1.0/default/CameraDevice_1_0.h @@ -170,7 +170,7 @@ private: // Set by provider (when external camera is connected/disconnected) bool mDisconnected; - static HandleImporter& sHandleImporter; + static HandleImporter sHandleImporter; const SortedVector>& mCameraDeviceNames; diff --git a/camera/device/3.2/default/Android.bp b/camera/device/3.2/default/Android.bp index d95f8f4e8b..325c0085a4 100644 --- a/camera/device/3.2/default/Android.bp +++ b/camera/device/3.2/default/Android.bp @@ -12,6 +12,7 @@ cc_library_shared { "libcutils", "android.hardware.camera.device@3.2", "android.hardware.camera.provider@2.4", + "android.hardware.graphics.mapper@2.0", "liblog", "libhardware", "libcamera_metadata", diff --git a/camera/device/3.2/default/CameraDeviceSession.cpp b/camera/device/3.2/default/CameraDeviceSession.cpp index b7187232f8..61be82dcaf 100644 --- a/camera/device/3.2/default/CameraDeviceSession.cpp +++ b/camera/device/3.2/default/CameraDeviceSession.cpp @@ -35,7 +35,7 @@ static constexpr size_t CAMERA_REQUEST_METADATA_QUEUE_SIZE = 1 << 20 /* 1MB */; // Size of result metadata fast message queue. Change to 0 to always use hwbinder buffer. static constexpr size_t CAMERA_RESULT_METADATA_QUEUE_SIZE = 1 << 20 /* 1MB */; -HandleImporter& CameraDeviceSession::sHandleImporter = HandleImporter::getInstance(); +HandleImporter CameraDeviceSession::sHandleImporter; const int CameraDeviceSession::ResultBatcher::NOT_BATCHED; CameraDeviceSession::CameraDeviceSession( diff --git a/camera/device/3.2/default/CameraDeviceSession.h b/camera/device/3.2/default/CameraDeviceSession.h index bbf39e60e5..d559c48f73 100644 --- a/camera/device/3.2/default/CameraDeviceSession.h +++ b/camera/device/3.2/default/CameraDeviceSession.h @@ -145,7 +145,7 @@ private: // Stream ID -> circulating buffers map std::map mCirculatingBuffers; - static HandleImporter& sHandleImporter; + static HandleImporter sHandleImporter; bool mInitFail; diff --git a/camera/provider/2.4/default/Android.bp b/camera/provider/2.4/default/Android.bp index 950682700c..7f3fa16240 100644 --- a/camera/provider/2.4/default/Android.bp +++ b/camera/provider/2.4/default/Android.bp @@ -15,6 +15,7 @@ cc_library_shared { "camera.device@3.2-impl", "android.hardware.camera.provider@2.4", "android.hardware.camera.common@1.0", + "android.hardware.graphics.mapper@2.0", "android.hidl.allocator@1.0", "android.hidl.memory@1.0", "liblog",