Merge "Camera: use gralloc mapper for handle import" into oc-dev am: 02b745ff40

am: 8c0e9f19b2

Change-Id: Ie14587977306c05f1458cb47b6cc2d5c3f152f39
This commit is contained in:
Yin-Chia Yeh 2017-04-28 02:16:01 +00:00 committed by android-build-merger
commit feddd45722
10 changed files with 75 additions and 129 deletions

View file

@ -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"]
}

View file

@ -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<buffer_handle_t>(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<native_handle_t*>(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<GRALLOC1_PFN_RETAIN>(
mDevice->getFunction(mDevice, GRALLOC1_FUNCTION_RETAIN));
mRelease = reinterpret_cast<GRALLOC1_PFN_RELEASE>(
mDevice->getFunction(mDevice, GRALLOC1_FUNCTION_RELEASE));
if (!mRetain || !mRelease) {
ALOGE("invalid gralloc1 device");
gralloc1_close(mDevice);
return false;
}
} else {
mModule = reinterpret_cast<const gralloc_module_t*>(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<native_handle_t*>(handle));
}
} // namespace helper
} // namespace V1_0
} // namespace common

View file

@ -17,9 +17,11 @@
#ifndef CAMERA_COMMON_1_0_HANDLEIMPORTED_H
#define CAMERA_COMMON_1_0_HANDLEIMPORTED_H
#include <hardware/gralloc.h>
#include <hardware/gralloc1.h>
#include <system/window.h>
#include <utils/Mutex.h>
#include <android/hardware/graphics/mapper/2.0/IMapper.h>
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<IMapper> mMapper;
// gralloc1
gralloc1_device_t* mDevice;
GRALLOC1_PFN_RETAIN mRetain;
GRALLOC1_PFN_RELEASE mRelease;
// gralloc0
const gralloc_module_t* mModule;
};
} // namespace helper

View file

@ -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",

View file

@ -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) {

View file

@ -170,7 +170,7 @@ private:
// Set by provider (when external camera is connected/disconnected)
bool mDisconnected;
static HandleImporter& sHandleImporter;
static HandleImporter sHandleImporter;
const SortedVector<std::pair<std::string, std::string>>& mCameraDeviceNames;

View file

@ -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",

View file

@ -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(

View file

@ -145,7 +145,7 @@ private:
// Stream ID -> circulating buffers map
std::map<int, CirculatingBuffers> mCirculatingBuffers;
static HandleImporter& sHandleImporter;
static HandleImporter sHandleImporter;
bool mInitFail;

View file

@ -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",