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

Commit e3d33876 authored by Chia-I Wu's avatar Chia-I Wu Committed by android-build-merger
Browse files

graphics: use allocator HAL support library in default impl am: 699df216 am: 4e8ce3ea

am: 0569d7fd

Change-Id: Id09e0c7e8a864c350ea6f4bfa9f24b5485ce60d3
parents aa04f5be 0569d7fd
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -3,10 +3,13 @@ cc_library_shared {
    defaults: ["hidl_defaults"],
    vendor: true,
    relative_install_path: "hw",
    srcs: ["Gralloc.cpp"],
    srcs: ["passthrough.cpp"],
    static_libs: [
        "android.hardware.graphics.allocator@2.0-passthrough",
    ],
    header_libs: [
        "android.hardware.graphics.allocator@2.0-hal",
    ],
    shared_libs: [
        "android.hardware.graphics.allocator@2.0",
        "libbase",
+6 −19
Original line number Diff line number Diff line
@@ -14,25 +14,12 @@
 * limitations under the License.
 */

#ifndef ANDROID_HARDWARE_GRAPHICS_ALLOCATOR_V2_0_GRALLOC_H
#define ANDROID_HARDWARE_GRAPHICS_ALLOCATOR_V2_0_GRALLOC_H

#include <allocator-passthrough/2.0/GrallocLoader.h>
#include <android/hardware/graphics/allocator/2.0/IAllocator.h>

namespace android {
namespace hardware {
namespace graphics {
namespace allocator {
namespace V2_0 {
namespace implementation {

extern "C" IAllocator* HIDL_FETCH_IAllocator(const char* name);

}  // namespace implementation
}  // namespace V2_0
}  // namespace allocator
}  // namespace graphics
}  // namespace hardware
}  // namespace android
using android::hardware::graphics::allocator::V2_0::IAllocator;
using android::hardware::graphics::allocator::V2_0::passthrough::GrallocLoader;

#endif  // ANDROID_HARDWARE_GRAPHICS_ALLOCATOR_V2_0_GRALLOC_H
extern "C" IAllocator* HIDL_FETCH_IAllocator(const char* /* name */) {
    return GrallocLoader::load();
}
+9 −3
Original line number Diff line number Diff line
@@ -3,8 +3,9 @@ cc_library_static {
    defaults: ["hidl_defaults"],
    vendor: true,
    srcs: [
        "Gralloc0Allocator.cpp",
        "Gralloc1Allocator.cpp",
        "Gralloc0Hal.cpp",
        "Gralloc1Hal.cpp",
        "GrallocLoader.cpp",
    ],
    shared_libs: [
        "android.hardware.graphics.allocator@2.0",
@@ -16,8 +17,13 @@ cc_library_static {
        "android.hardware.graphics.mapper@2.0",
        "libhardware",
    ],
    export_include_dirs: ["."],
    header_libs: [
        "android.hardware.graphics.allocator@2.0-hal",
        "libgrallocmapperincludes",
    ],
    export_header_lib_headers: [
        "android.hardware.graphics.allocator@2.0-hal",
    ],
    export_include_dirs: ["include"],
    cflags: ["-DLOG_TAG=\"AllocatorHal\""],
}
+56 −55
Original line number Diff line number Diff line
@@ -14,15 +14,12 @@
 * limitations under the License.
 */

#define LOG_TAG "Gralloc0Allocator"

#include "Gralloc0Allocator.h"
#include "GrallocBufferDescriptor.h"

#include <vector>
#include <allocator-passthrough/2.0/Gralloc0Hal.h>

#include <string.h>

#include <GrallocBufferDescriptor.h>
#include <hardware/gralloc.h>
#include <log/log.h>

namespace android {
@@ -30,95 +27,105 @@ namespace hardware {
namespace graphics {
namespace allocator {
namespace V2_0 {
namespace implementation {
namespace passthrough {

using android::hardware::graphics::mapper::V2_0::implementation::grallocDecodeBufferDescriptor;
using mapper::V2_0::implementation::grallocDecodeBufferDescriptor;

Gralloc0Allocator::Gralloc0Allocator(const hw_module_t* module) {
Gralloc0Hal::~Gralloc0Hal() {
    if (mDevice) {
        gralloc_close(mDevice);
    }
}

bool Gralloc0Hal::initWithModule(const hw_module_t* module) {
    int result = gralloc_open(module, &mDevice);
    if (result) {
        LOG_ALWAYS_FATAL("failed to open gralloc0 device: %s", strerror(-result));
    }
        ALOGE("failed to open gralloc0 device: %s", strerror(-result));
        mDevice = nullptr;
        return false;
    }

Gralloc0Allocator::~Gralloc0Allocator() {
    gralloc_close(mDevice);
    return true;
}

Return<void> Gralloc0Allocator::dumpDebugInfo(dumpDebugInfo_cb hidl_cb) {
std::string Gralloc0Hal::dumpDebugInfo() {
    char buf[4096] = {};
    if (mDevice->dump) {
        mDevice->dump(mDevice, buf, sizeof(buf));
        buf[sizeof(buf) - 1] = '\0';
    }

    hidl_cb(hidl_string(buf));

    return Void();
    return buf;
}

Return<void> Gralloc0Allocator::allocate(const BufferDescriptor& descriptor, uint32_t count,
                                         allocate_cb hidl_cb) {
    IMapper::BufferDescriptorInfo descriptorInfo;
Error Gralloc0Hal::allocateBuffers(const BufferDescriptor& descriptor, uint32_t count,
                                   uint32_t* outStride,
                                   std::vector<const native_handle_t*>* outBuffers) {
    mapper::V2_0::IMapper::BufferDescriptorInfo descriptorInfo;
    if (!grallocDecodeBufferDescriptor(descriptor, &descriptorInfo)) {
        hidl_cb(Error::BAD_DESCRIPTOR, 0, hidl_vec<hidl_handle>());
        return Void();
        return Error::BAD_DESCRIPTOR;
    }

    Error error = Error::NONE;
    uint32_t stride = 0;
    std::vector<hidl_handle> buffers;
    std::vector<const native_handle_t*> buffers;
    buffers.reserve(count);

    // allocate the buffers
    for (uint32_t i = 0; i < count; i++) {
        buffer_handle_t tmpBuffer;
        const native_handle_t* tmpBuffer;
        uint32_t tmpStride;
        error = allocateOne(descriptorInfo, &tmpBuffer, &tmpStride);
        error = allocateOneBuffer(descriptorInfo, &tmpBuffer, &tmpStride);
        if (error != Error::NONE) {
            break;
        }

        buffers.push_back(tmpBuffer);

        if (stride == 0) {
            stride = tmpStride;
        } else if (stride != tmpStride) {
            // non-uniform strides
            mDevice->free(mDevice, tmpBuffer);
            stride = 0;
            error = Error::UNSUPPORTED;
            break;
        }

        buffers.emplace_back(hidl_handle(tmpBuffer));
    }

    // return the buffers
    hidl_vec<hidl_handle> hidl_buffers;
    if (error == Error::NONE) {
        hidl_buffers.setToExternal(buffers.data(), buffers.size());
    if (error != Error::NONE) {
        freeBuffers(buffers);
        return error;
    }
    hidl_cb(error, stride, hidl_buffers);

    // free the buffers
    for (const auto& buffer : buffers) {
        mDevice->free(mDevice, buffer.getNativeHandle());
    *outStride = stride;
    *outBuffers = std::move(buffers);

    return Error::NONE;
}

    return Void();
void Gralloc0Hal::freeBuffers(const std::vector<const native_handle_t*>& buffers) {
    for (auto buffer : buffers) {
        int result = mDevice->free(mDevice, buffer);
        if (result != 0) {
            ALOGE("failed to free buffer %p: %d", buffer, result);
        }
    }
}

Error Gralloc0Allocator::allocateOne(const IMapper::BufferDescriptorInfo& info,
                                     buffer_handle_t* outBuffer, uint32_t* outStride) {
Error Gralloc0Hal::allocateOneBuffer(const mapper::V2_0::IMapper::BufferDescriptorInfo& info,
                                     const native_handle_t** outBuffer, uint32_t* outStride) {
    if (info.layerCount > 1 || (info.usage >> 32) != 0) {
        return Error::BAD_VALUE;
    }

    buffer_handle_t buffer = nullptr;
    const native_handle_t* buffer = nullptr;
    int stride = 0;
    int result = mDevice->alloc(mDevice, info.width, info.height, static_cast<int>(info.format),
                                info.usage, &buffer, &stride);
    if (result) {
    switch (result) {
        case 0:
            *outBuffer = buffer;
            *outStride = stride;
            return Error::NONE;
        case -EINVAL:
            return Error::BAD_VALUE;
        default:
@@ -126,13 +133,7 @@ Error Gralloc0Allocator::allocateOne(const IMapper::BufferDescriptorInfo& info,
    }
}

    *outBuffer = buffer;
    *outStride = stride;

    return Error::NONE;
}

}  // namespace implementation
}  // namespace passthrough
}  // namespace V2_0
}  // namespace allocator
}  // namespace graphics
+82 −72
Original line number Diff line number Diff line
@@ -14,15 +14,11 @@
 * limitations under the License.
 */

#define LOG_TAG "Gralloc1Allocator"

#include "Gralloc1Allocator.h"
#include "GrallocBufferDescriptor.h"

#include <vector>
#include <allocator-passthrough/2.0/Gralloc1Hal.h>

#include <string.h>

#include <GrallocBufferDescriptor.h>
#include <log/log.h>

namespace android {
@@ -30,27 +26,36 @@ namespace hardware {
namespace graphics {
namespace allocator {
namespace V2_0 {
namespace implementation {
namespace passthrough {

using android::hardware::graphics::common::V1_0::BufferUsage;
using android::hardware::graphics::mapper::V2_0::implementation::grallocDecodeBufferDescriptor;
using mapper::V2_0::implementation::grallocDecodeBufferDescriptor;

Gralloc1Hal::~Gralloc1Hal() {
    if (mDevice) {
        gralloc1_close(mDevice);
    }
}

Gralloc1Allocator::Gralloc1Allocator(const hw_module_t* module)
    : mDevice(nullptr), mCapabilities(), mDispatch() {
bool Gralloc1Hal::initWithModule(const hw_module_t* module) {
    int result = gralloc1_open(module, &mDevice);
    if (result) {
        LOG_ALWAYS_FATAL("failed to open gralloc1 device: %s", strerror(-result));
        ALOGE("failed to open gralloc1 device: %s", strerror(-result));
        mDevice = nullptr;
        return false;
    }

    initCapabilities();
    initDispatch();
    if (!initDispatch()) {
        gralloc1_close(mDevice);
        mDevice = nullptr;
        return false;
    }

Gralloc1Allocator::~Gralloc1Allocator() {
    gralloc1_close(mDevice);
    return true;
}

void Gralloc1Allocator::initCapabilities() {
void Gralloc1Hal::initCapabilities() {
    uint32_t count = 0;
    mDevice->getCapabilities(mDevice, &count, nullptr);

@@ -66,33 +71,40 @@ void Gralloc1Allocator::initCapabilities() {
    }
}

template <typename T>
void Gralloc1Allocator::initDispatch(gralloc1_function_descriptor_t desc, T* outPfn) {
gralloc1_function_pointer_t Gralloc1Hal::getDispatchFunction(
    gralloc1_function_descriptor_t desc) const {
    auto pfn = mDevice->getFunction(mDevice, desc);
    if (!pfn) {
        LOG_ALWAYS_FATAL("failed to get gralloc1 function %d", desc);
        ALOGE("failed to get gralloc1 function %d", desc);
        return nullptr;
    }
    return pfn;
}

    *outPfn = reinterpret_cast<T>(pfn);
bool Gralloc1Hal::initDispatch() {
    if (!initDispatchFunction(GRALLOC1_FUNCTION_DUMP, &mDispatch.dump) ||
        !initDispatchFunction(GRALLOC1_FUNCTION_CREATE_DESCRIPTOR, &mDispatch.createDescriptor) ||
        !initDispatchFunction(GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR, &mDispatch.destroyDescriptor) ||
        !initDispatchFunction(GRALLOC1_FUNCTION_SET_DIMENSIONS, &mDispatch.setDimensions) ||
        !initDispatchFunction(GRALLOC1_FUNCTION_SET_FORMAT, &mDispatch.setFormat) ||
        !initDispatchFunction(GRALLOC1_FUNCTION_SET_CONSUMER_USAGE, &mDispatch.setConsumerUsage) ||
        !initDispatchFunction(GRALLOC1_FUNCTION_SET_PRODUCER_USAGE, &mDispatch.setProducerUsage) ||
        !initDispatchFunction(GRALLOC1_FUNCTION_GET_STRIDE, &mDispatch.getStride) ||
        !initDispatchFunction(GRALLOC1_FUNCTION_ALLOCATE, &mDispatch.allocate) ||
        !initDispatchFunction(GRALLOC1_FUNCTION_RELEASE, &mDispatch.release)) {
        return false;
    }

void Gralloc1Allocator::initDispatch() {
    initDispatch(GRALLOC1_FUNCTION_DUMP, &mDispatch.dump);
    initDispatch(GRALLOC1_FUNCTION_CREATE_DESCRIPTOR, &mDispatch.createDescriptor);
    initDispatch(GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR, &mDispatch.destroyDescriptor);
    initDispatch(GRALLOC1_FUNCTION_SET_DIMENSIONS, &mDispatch.setDimensions);
    initDispatch(GRALLOC1_FUNCTION_SET_FORMAT, &mDispatch.setFormat);
    if (mCapabilities.layeredBuffers) {
        initDispatch(GRALLOC1_FUNCTION_SET_LAYER_COUNT, &mDispatch.setLayerCount);
        if (!initDispatchFunction(GRALLOC1_FUNCTION_SET_LAYER_COUNT, &mDispatch.setLayerCount)) {
            return false;
        }
    initDispatch(GRALLOC1_FUNCTION_SET_CONSUMER_USAGE, &mDispatch.setConsumerUsage);
    initDispatch(GRALLOC1_FUNCTION_SET_PRODUCER_USAGE, &mDispatch.setProducerUsage);
    initDispatch(GRALLOC1_FUNCTION_GET_STRIDE, &mDispatch.getStride);
    initDispatch(GRALLOC1_FUNCTION_ALLOCATE, &mDispatch.allocate);
    initDispatch(GRALLOC1_FUNCTION_RELEASE, &mDispatch.release);
    }

Return<void> Gralloc1Allocator::dumpDebugInfo(dumpDebugInfo_cb hidl_cb) {
    return true;
}

std::string Gralloc1Hal::dumpDebugInfo() {
    uint32_t len = 0;
    mDispatch.dump(mDevice, &len, nullptr);

@@ -101,72 +113,70 @@ Return<void> Gralloc1Allocator::dumpDebugInfo(dumpDebugInfo_cb hidl_cb) {
    buf.resize(len + 1);
    buf[len] = '\0';

    hidl_string reply;
    reply.setToExternal(buf.data(), len);
    hidl_cb(reply);

    return Void();
    return buf.data();
}

Return<void> Gralloc1Allocator::allocate(const BufferDescriptor& descriptor, uint32_t count,
                                         allocate_cb hidl_cb) {
    IMapper::BufferDescriptorInfo descriptorInfo;
Error Gralloc1Hal::allocateBuffers(const BufferDescriptor& descriptor, uint32_t count,
                                   uint32_t* outStride,
                                   std::vector<const native_handle_t*>* outBuffers) {
    mapper::V2_0::IMapper::BufferDescriptorInfo descriptorInfo;
    if (!grallocDecodeBufferDescriptor(descriptor, &descriptorInfo)) {
        hidl_cb(Error::BAD_DESCRIPTOR, 0, hidl_vec<hidl_handle>());
        return Void();
        return Error::BAD_DESCRIPTOR;
    }

    gralloc1_buffer_descriptor_t desc;
    Error error = createDescriptor(descriptorInfo, &desc);
    if (error != Error::NONE) {
        hidl_cb(error, 0, hidl_vec<hidl_handle>());
        return Void();
        return error;
    }

    uint32_t stride = 0;
    std::vector<hidl_handle> buffers;
    std::vector<const native_handle_t*> buffers;
    buffers.reserve(count);

    // allocate the buffers
    for (uint32_t i = 0; i < count; i++) {
        buffer_handle_t tmpBuffer;
        const native_handle_t* tmpBuffer;
        uint32_t tmpStride;
        error = allocateOne(desc, &tmpBuffer, &tmpStride);
        error = allocateOneBuffer(desc, &tmpBuffer, &tmpStride);
        if (error != Error::NONE) {
            break;
        }

        buffers.push_back(tmpBuffer);

        if (stride == 0) {
            stride = tmpStride;
        } else if (stride != tmpStride) {
            // non-uniform strides
            mDispatch.release(mDevice, tmpBuffer);
            stride = 0;
            error = Error::UNSUPPORTED;
            break;
        }

        buffers.emplace_back(hidl_handle(tmpBuffer));
    }

    mDispatch.destroyDescriptor(mDevice, desc);

    // return the buffers
    hidl_vec<hidl_handle> hidl_buffers;
    if (error == Error::NONE) {
        hidl_buffers.setToExternal(buffers.data(), buffers.size());
    if (error != Error::NONE) {
        freeBuffers(buffers);
        return error;
    }
    hidl_cb(error, stride, hidl_buffers);

    // free the buffers
    for (const auto& buffer : buffers) {
        mDispatch.release(mDevice, buffer.getNativeHandle());
    *outStride = stride;
    *outBuffers = std::move(buffers);

    return Error::NONE;
}

    return Void();
void Gralloc1Hal::freeBuffers(const std::vector<const native_handle_t*>& buffers) {
    for (auto buffer : buffers) {
        int32_t error = mDispatch.release(mDevice, buffer);
        if (error != GRALLOC1_ERROR_NONE) {
            ALOGE("failed to free buffer %p: %d", buffer, error);
        }
    }
}

Error Gralloc1Allocator::toError(int32_t error) {
Error Gralloc1Hal::toError(int32_t error) {
    switch (error) {
        case GRALLOC1_ERROR_NONE:
            return Error::NONE;
@@ -187,7 +197,7 @@ Error Gralloc1Allocator::toError(int32_t error) {
    }
}

uint64_t Gralloc1Allocator::toProducerUsage(uint64_t usage) {
uint64_t Gralloc1Hal::toProducerUsage(uint64_t usage) {
    // this is potentially broken as we have no idea which private flags
    // should be filtered out
    uint64_t producerUsage =
@@ -221,7 +231,7 @@ uint64_t Gralloc1Allocator::toProducerUsage(uint64_t usage) {
    return producerUsage;
}

uint64_t Gralloc1Allocator::toConsumerUsage(uint64_t usage) {
uint64_t Gralloc1Hal::toConsumerUsage(uint64_t usage) {
    // this is potentially broken as we have no idea which private flags
    // should be filtered out
    uint64_t consumerUsage =
@@ -249,7 +259,7 @@ uint64_t Gralloc1Allocator::toConsumerUsage(uint64_t usage) {
    return consumerUsage;
}

Error Gralloc1Allocator::createDescriptor(const IMapper::BufferDescriptorInfo& info,
Error Gralloc1Hal::createDescriptor(const mapper::V2_0::IMapper::BufferDescriptorInfo& info,
                                    gralloc1_buffer_descriptor_t* outDescriptor) {
    gralloc1_buffer_descriptor_t descriptor;

@@ -284,9 +294,9 @@ Error Gralloc1Allocator::createDescriptor(const IMapper::BufferDescriptorInfo& i
    return toError(error);
}

Error Gralloc1Allocator::allocateOne(gralloc1_buffer_descriptor_t descriptor,
                                     buffer_handle_t* outBuffer, uint32_t* outStride) {
    buffer_handle_t buffer = nullptr;
Error Gralloc1Hal::allocateOneBuffer(gralloc1_buffer_descriptor_t descriptor,
                                     const native_handle_t** outBuffer, uint32_t* outStride) {
    const native_handle_t* buffer = nullptr;
    int32_t error = mDispatch.allocate(mDevice, 1, &descriptor, &buffer);
    if (error != GRALLOC1_ERROR_NONE && error != GRALLOC1_ERROR_NOT_SHARED) {
        return toError(error);
@@ -305,7 +315,7 @@ Error Gralloc1Allocator::allocateOne(gralloc1_buffer_descriptor_t descriptor,
    return Error::NONE;
}

}  // namespace implementation
}  // namespace passthrough
}  // namespace V2_0
}  // namespace allocator
}  // namespace graphics
Loading