Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b47ff3d6 authored by Yin-Chia Yeh's avatar Yin-Chia Yeh Committed by Android (Google) Code Review
Browse files

Merge "Camera: default implementation of device 1.0"

parents 3a3dc8fe 248ed700
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ subdirs = [
    "common/1.0",
    "common/1.0/default",
    "device/1.0",
    "device/1.0/default",
    "device/3.2",
    "device/3.2/default",
    "metadata/3.2",
+6 −1
Original line number Diff line number Diff line
cc_library_static {
    name: "android.hardware.camera.common@1.0-helper",
    srcs: ["CameraModule.cpp", "CameraMetadata.cpp", "VendorTagDescriptor.cpp"],
    srcs: [
        "CameraModule.cpp",
        "CameraMetadata.cpp",
        "VendorTagDescriptor.cpp",
        "HandleImporter.cpp"],
    cflags: [
        "-Werror",
        "-Wextra",
@@ -13,3 +17,4 @@ cc_library_static {
    include_dirs: ["system/media/private/camera/include"],
    export_include_dirs : ["include"]
}
+190 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "HandleImporter"
#include <utils/Log.h>
#include "HandleImporter.h"

namespace android {
namespace hardware {
namespace camera {
namespace common {
namespace V1_0 {
namespace helper {

HandleImporter HandleImporter::sHandleImporter;

HandleImporter& HandleImporter::getInstance() {
    sHandleImporter.initialize();
    return sHandleImporter;
}

bool HandleImporter::initialize() {
    // allow only one client
    if (mInitialized) {
        return false;
    }

    if (!openGralloc()) {
        return false;
    }

    mInitialized = true;
    return true;
}

void HandleImporter::cleanup() {
    if (!mInitialized) {
        return;
    }

    closeGralloc();
    mInitialized = false;
}

// 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 HandleImporter::importBuffer(buffer_handle_t& handle) {
    if (!handle->numFds && !handle->numInts) {
        handle = nullptr;
        return true;
    }

    buffer_handle_t clone = cloneBuffer(handle);
    if (!clone) {
        return false;
    }

    handle = clone;
    return true;
}

void HandleImporter::freeBuffer(buffer_handle_t handle) {
    if (!handle) {
        return;
    }

    releaseBuffer(handle);
}

bool HandleImporter::importFence(const native_handle_t* handle, int& fd) {
    if (handle == nullptr || handle->numFds == 0) {
        fd = -1;
    } else if (handle->numFds == 1) {
        fd = dup(handle->data[0]);
        if (fd < 0) {
            ALOGE("failed to dup fence fd %d", handle->data[0]);
            return false;
        }
    } else {
        ALOGE("invalid fence handle with %d file descriptors",
                handle->numFds);
        return false;
    }

    return true;
}

void HandleImporter::closeFence(int fd) {
    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
} // namespace camera
} // namespace hardware
} // namespace android
+1 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
#include <hardware/camera.h>
#include <utils/Mutex.h>
#include <utils/KeyedVector.h>
#include <utils/RefBase.h>

#include "CameraMetadata.h"

+73 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#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>

namespace android {
namespace hardware {
namespace camera {
namespace common {
namespace V1_0 {
namespace helper {

// Borrowed from graphics HAL. Use this until gralloc mapper HAL is working
class HandleImporter {
public:
    static HandleImporter& getInstance();

    // 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);

private:

    HandleImporter() : mInitialized(false) {}
    bool initialize();
    void cleanup();
    bool openGralloc();
    void closeGralloc();
    buffer_handle_t cloneBuffer(buffer_handle_t handle);
    void releaseBuffer(buffer_handle_t handle);

    static HandleImporter sHandleImporter;
    bool mInitialized;

    // gralloc1
    gralloc1_device_t* mDevice;
    GRALLOC1_PFN_RETAIN mRetain;
    GRALLOC1_PFN_RELEASE mRelease;

    // gralloc0
    const gralloc_module_t* mModule;
};

} // namespace helper
} // namespace V1_0
} // namespace common
} // namespace camera
} // namespace hardware
} // namespace android

#endif // CAMERA_COMMON_1_0_HANDLEIMPORTED_H
 No newline at end of file
Loading