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

Commit a7002cb8 authored by John Stultz's avatar John Stultz
Browse files

codec2: C2Store: Integrate C2DmaBufAllocator as a possible allocator



This integrates the C2DmaBufAllocator as one of the possible
allocators for linear allocations, switching its use vs the
ION allocator based on the existance of /dev/ion

Also integrate glue code to handle HAL provided usageMaps for
the C2DmaBufAllocator

Signed-off-by: default avatarJohn Stultz <john.stultz@linaro.org>
Change-Id: Iee2856c0b85d7c5dfe799a2799580a8f69e87d3d
parent 6a40788b
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -254,6 +254,8 @@ enum C2ParamIndexKind : C2Param::type_index_t {
    kParamIndexTunneledMode, // struct
    kParamIndexTunnelHandle, // int32[]
    kParamIndexTunnelSystemTime, // int64

    kParamIndexStoreDmaBufUsage,  // store, struct
};

}
@@ -2040,6 +2042,33 @@ struct C2StoreIonUsageStruct {
typedef C2GlobalParam<C2Info, C2StoreIonUsageStruct, kParamIndexStoreIonUsage>
        C2StoreIonUsageInfo;

/**
 * This structure describes the preferred DMA-Buf allocation parameters for a given memory usage.
 */
struct C2StoreDmaBufUsageStruct {
    inline C2StoreDmaBufUsageStruct() { memset(this, 0, sizeof(*this)); }

    inline C2StoreDmaBufUsageStruct(size_t flexCount, uint64_t usage_, uint32_t capacity_)
        : usage(usage_), capacity(capacity_), allocFlags(0) {
        memset(heapName, 0, flexCount);
    }

    uint64_t usage;                         ///< C2MemoryUsage
    uint32_t capacity;                      ///< capacity
    int32_t allocFlags;                     ///< ion allocation flags
    char heapName[];                        ///< dmabuf heap name

    DEFINE_AND_DESCRIBE_FLEX_C2STRUCT(StoreDmaBufUsage, heapName)
    C2FIELD(usage, "usage")
    C2FIELD(capacity, "capacity")
    C2FIELD(allocFlags, "alloc-flags")
    C2FIELD(heapName, "heap-name")
};

// store, private
typedef C2GlobalParam<C2Info, C2StoreDmaBufUsageStruct, kParamIndexStoreDmaBufUsage>
        C2StoreDmaBufUsageInfo;

/**
 * Flexible pixel format descriptors
 */
+21 −0
Original line number Diff line number Diff line
@@ -122,6 +122,18 @@ private:
                })
                .withSetter(SetIonUsage)
                .build());

            addParameter(
                DefineParam(mDmaBufUsageInfo, "dmabuf-usage")
                .withDefault(new C2StoreDmaBufUsageInfo())
                .withFields({
                    C2F(mDmaBufUsageInfo, usage).flags({C2MemoryUsage::CPU_READ | C2MemoryUsage::CPU_WRITE}),
                    C2F(mDmaBufUsageInfo, capacity).inRange(0, UINT32_MAX, 1024),
                    C2F(mDmaBufUsageInfo, heapName).any(),
                    C2F(mDmaBufUsageInfo, allocFlags).flags({}),
                })
                .withSetter(SetDmaBufUsage)
                .build());
        }

        virtual ~Interface() = default;
@@ -135,7 +147,16 @@ private:
            return C2R::Ok();
        }

        static C2R SetDmaBufUsage(bool /* mayBlock */, C2P<C2StoreDmaBufUsageInfo> &me) {
            // Vendor's TODO: put appropriate mapping logic
            strncpy(me.set().m.heapName, "system", me.v.flexCount());
            me.set().m.allocFlags = 0;
            return C2R::Ok();
        }


        std::shared_ptr<C2StoreIonUsageInfo> mIonUsageInfo;
        std::shared_ptr<C2StoreDmaBufUsageInfo> mDmaBufUsageInfo;
    };
    std::shared_ptr<C2ReflectorHelper> mReflectorHelper;
    Interface mInterface;
+119 −4
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include <C2AllocatorBlob.h>
#include <C2AllocatorGralloc.h>
#include <C2AllocatorIon.h>
#include <C2DmaBufAllocator.h>
#include <C2BufferPriv.h>
#include <C2BqBufferPriv.h>
#include <C2Component.h>
@@ -82,6 +83,7 @@ private:

    /// returns a shared-singleton ion allocator
    std::shared_ptr<C2Allocator> fetchIonAllocator();
    std::shared_ptr<C2Allocator> fetchDmaBufAllocator();

    /// returns a shared-singleton gralloc allocator
    std::shared_ptr<C2Allocator> fetchGrallocAllocator();
@@ -99,6 +101,20 @@ private:
C2PlatformAllocatorStoreImpl::C2PlatformAllocatorStoreImpl() {
}

static bool using_ion(void) {
    static int cached_result = -1;

    if (cached_result == -1) {
        struct stat buffer;
        cached_result = (stat("/dev/ion", &buffer) == 0);
        if (cached_result)
            ALOGD("Using ION\n");
        else
            ALOGD("Using DMABUF Heaps\n");
    }
    return (cached_result == 1);
}

c2_status_t C2PlatformAllocatorStoreImpl::fetchAllocator(
        id_t id, std::shared_ptr<C2Allocator> *const allocator) {
    allocator->reset();
@@ -107,8 +123,11 @@ c2_status_t C2PlatformAllocatorStoreImpl::fetchAllocator(
    }
    switch (id) {
    // TODO: should we implement a generic registry for all, and use that?
    case C2PlatformAllocatorStore::ION:
    case C2PlatformAllocatorStore::ION: /* also ::DMABUFHEAP */
        if (using_ion())
            *allocator = fetchIonAllocator();
        else
            *allocator = fetchDmaBufAllocator();
        break;

    case C2PlatformAllocatorStore::GRALLOC:
@@ -142,7 +161,9 @@ c2_status_t C2PlatformAllocatorStoreImpl::fetchAllocator(
namespace {

std::mutex gIonAllocatorMutex;
std::mutex gDmaBufAllocatorMutex;
std::weak_ptr<C2AllocatorIon> gIonAllocator;
std::weak_ptr<C2DmaBufAllocator> gDmaBufAllocator;

void UseComponentStoreForIonAllocator(
        const std::shared_ptr<C2AllocatorIon> allocator,
@@ -197,6 +218,65 @@ void UseComponentStoreForIonAllocator(
    allocator->setUsageMapper(mapper, minUsage, maxUsage, blockSize);
}

void UseComponentStoreForDmaBufAllocator(const std::shared_ptr<C2DmaBufAllocator> allocator,
                                         std::shared_ptr<C2ComponentStore> store) {
    C2DmaBufAllocator::UsageMapperFn mapper;
    const size_t maxHeapNameLen = 128;
    uint64_t minUsage = 0;
    uint64_t maxUsage = C2MemoryUsage(C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE).expected;
    size_t blockSize = getpagesize();

    // query min and max usage as well as block size via supported values
    std::unique_ptr<C2StoreDmaBufUsageInfo> usageInfo;
    usageInfo = C2StoreDmaBufUsageInfo::AllocUnique(maxHeapNameLen);

    std::vector<C2FieldSupportedValuesQuery> query = {
            C2FieldSupportedValuesQuery::Possible(C2ParamField::Make(*usageInfo, usageInfo->m.usage)),
            C2FieldSupportedValuesQuery::Possible(
                    C2ParamField::Make(*usageInfo, usageInfo->m.capacity)),
    };
    c2_status_t res = store->querySupportedValues_sm(query);
    if (res == C2_OK) {
        if (query[0].status == C2_OK) {
            const C2FieldSupportedValues& fsv = query[0].values;
            if (fsv.type == C2FieldSupportedValues::FLAGS && !fsv.values.empty()) {
                minUsage = fsv.values[0].u64;
                maxUsage = 0;
                for (C2Value::Primitive v : fsv.values) {
                    maxUsage |= v.u64;
                }
            }
        }
        if (query[1].status == C2_OK) {
            const C2FieldSupportedValues& fsv = query[1].values;
            if (fsv.type == C2FieldSupportedValues::RANGE && fsv.range.step.u32 > 0) {
                blockSize = fsv.range.step.u32;
            }
        }

        mapper = [store](C2MemoryUsage usage, size_t capacity, C2String* heapName,
                         unsigned* flags) -> c2_status_t {
            if (capacity > UINT32_MAX) {
                return C2_BAD_VALUE;
            }

            std::unique_ptr<C2StoreDmaBufUsageInfo> usageInfo;
            usageInfo = C2StoreDmaBufUsageInfo::AllocUnique(maxHeapNameLen, usage.expected, capacity);
            std::vector<std::unique_ptr<C2SettingResult>> failures;  // TODO: remove

            c2_status_t res = store->config_sm({&*usageInfo}, &failures);
            if (res == C2_OK) {
                *heapName = C2String(usageInfo->m.heapName);
                *flags = usageInfo->m.allocFlags;
            }

            return res;
        };
    }

    allocator->setUsageMapper(mapper, minUsage, maxUsage, blockSize);
}

}

void C2PlatformAllocatorStoreImpl::setComponentStore(std::shared_ptr<C2ComponentStore> store) {
@@ -233,6 +313,22 @@ std::shared_ptr<C2Allocator> C2PlatformAllocatorStoreImpl::fetchIonAllocator() {
    return allocator;
}

std::shared_ptr<C2Allocator> C2PlatformAllocatorStoreImpl::fetchDmaBufAllocator() {
    std::lock_guard<std::mutex> lock(gDmaBufAllocatorMutex);
    std::shared_ptr<C2DmaBufAllocator> allocator = gDmaBufAllocator.lock();
    if (allocator == nullptr) {
        std::shared_ptr<C2ComponentStore> componentStore;
        {
            std::lock_guard<std::mutex> lock(_mComponentStoreReadLock);
            componentStore = _mComponentStore;
        }
        allocator = std::make_shared<C2DmaBufAllocator>(C2PlatformAllocatorStore::DMABUFHEAP);
        UseComponentStoreForDmaBufAllocator(allocator, componentStore);
        gDmaBufAllocator = allocator;
    }
    return allocator;
}

std::shared_ptr<C2Allocator> C2PlatformAllocatorStoreImpl::fetchBlobAllocator() {
    static std::mutex mutex;
    static std::weak_ptr<C2Allocator> blobAllocator;
@@ -347,7 +443,7 @@ public:
            allocatorId = GetPreferredLinearAllocatorId(GetCodec2PoolMask());
        }
        switch(allocatorId) {
            case C2PlatformAllocatorStore::ION:
            case C2PlatformAllocatorStore::ION: /* also ::DMABUFHEAP */
                res = allocatorStore->fetchAllocator(
                        C2PlatformAllocatorStore::ION, &allocator);
                if (res == C2_OK) {
@@ -645,6 +741,7 @@ private:

    struct Interface : public C2InterfaceHelper {
        std::shared_ptr<C2StoreIonUsageInfo> mIonUsageInfo;
        std::shared_ptr<C2StoreDmaBufUsageInfo> mDmaBufUsageInfo;

        Interface(std::shared_ptr<C2ReflectorHelper> reflector)
            : C2InterfaceHelper(reflector) {
@@ -680,7 +777,13 @@ private:
                    me.set().minAlignment = 0;
#endif
                    return C2R::Ok();
                }
                };

                static C2R setDmaBufUsage(bool /* mayBlock */, C2P<C2StoreDmaBufUsageInfo> &me) {
                    strncpy(me.set().m.heapName, "system", me.v.flexCount());
                    me.set().m.allocFlags = 0;
                    return C2R::Ok();
                };
            };

            addParameter(
@@ -695,6 +798,18 @@ private:
                })
                .withSetter(Setter::setIonUsage)
                .build());

            addParameter(
                DefineParam(mDmaBufUsageInfo, "dmabuf-usage")
                .withDefault(C2StoreDmaBufUsageInfo::AllocShared(0))
                .withFields({
                    C2F(mDmaBufUsageInfo, m.usage).flags({C2MemoryUsage::CPU_READ | C2MemoryUsage::CPU_WRITE}),
                    C2F(mDmaBufUsageInfo, m.capacity).inRange(0, UINT32_MAX, 1024),
                    C2F(mDmaBufUsageInfo, m.allocFlags).flags({}),
                    C2F(mDmaBufUsageInfo, m.heapName).any(),
                })
                .withSetter(Setter::setDmaBufUsage)
                .build());
        }
    };

+11 −0
Original line number Diff line number Diff line
@@ -47,6 +47,17 @@ public:
         */
        ION = PLATFORM_START,

        /*
         * ID of the DMA-Buf Heap (ion replacement) backed platform allocator.
         *
         * C2Handle consists of:
         *   fd  shared dmabuf buffer handle
         *   int size (lo 32 bits)
         *   int size (hi 32 bits)
         *   int magic '\xc2io\x00'
         */
        DMABUFHEAP = ION,

        /**
         * ID of the gralloc backed platform allocator.
         *