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

Commit 508d01b8 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "sfplugin: import buffers before metadata handling"

parents e24810e7 e2aa240b
Loading
Loading
Loading
Loading
+64 −37
Original line number Original line Diff line number Diff line
@@ -984,16 +984,47 @@ sp<IMapper4> GetMapper4() {
    return sMapper;
    return sMapper;
}
}


class NativeHandleDeleter {
class Gralloc4Buffer {
public:
public:
    explicit NativeHandleDeleter(native_handle_t *handle) : mHandle(handle) {}
    Gralloc4Buffer(const C2Handle *const handle) : mBuffer(nullptr) {
    ~NativeHandleDeleter() {
        sp<IMapper4> mapper = GetMapper4();
        if (mHandle) {
        if (!mapper) {
            native_handle_delete(mHandle);
            return;
        }
        }
        // Unwrap raw buffer handle from the C2Handle
        native_handle_t *nh = UnwrapNativeCodec2GrallocHandle(handle);
        if (!nh) {
            return;
        }
        // Import the raw handle so IMapper can use the buffer. The imported
        // handle must be freed when the client is done with the buffer.
        mapper->importBuffer(
                hardware::hidl_handle(nh),
                [&](const Error4 &error, void *buffer) {
                    if (error == Error4::NONE) {
                        mBuffer = buffer;
                    }
                });

        // TRICKY: UnwrapNativeCodec2GrallocHandle creates a new handle but
        //         does not clone the fds. Thus we need to delete the handle
        //         without closing it.
        native_handle_delete(nh);
    }

    ~Gralloc4Buffer() {
        sp<IMapper4> mapper = GetMapper4();
        if (mapper && mBuffer) {
            // Free the imported buffer handle. This does not release the
            // underlying buffer itself.
            mapper->freeBuffer(mBuffer);
        }
        }
    }

    void *get() const { return mBuffer; }
    operator bool() const { return (mBuffer != nullptr); }
private:
private:
    native_handle_t *mHandle;
    void *mBuffer;
};
};


}  // namspace
}  // namspace
@@ -1003,24 +1034,15 @@ c2_status_t GetHdrMetadataFromGralloc4Handle(
        std::shared_ptr<C2StreamHdrStaticMetadataInfo::input> *staticInfo,
        std::shared_ptr<C2StreamHdrStaticMetadataInfo::input> *staticInfo,
        std::shared_ptr<C2StreamHdrDynamicMetadataInfo::input> *dynamicInfo) {
        std::shared_ptr<C2StreamHdrDynamicMetadataInfo::input> *dynamicInfo) {
    c2_status_t err = C2_OK;
    c2_status_t err = C2_OK;
    native_handle_t *nativeHandle = UnwrapNativeCodec2GrallocHandle(handle);
    if (nativeHandle == nullptr) {
        // Nothing to do
        return err;
    }
    // TRICKY: UnwrapNativeCodec2GrallocHandle creates a new handle but
    //         does not clone the fds. Thus we need to delete the handle
    //         without closing it when going out of scope.
    //         NativeHandle cannot solve this problem, as it would close and
    //         delete the handle, while we need delete only.
    NativeHandleDeleter nhd(nativeHandle);
    sp<IMapper4> mapper = GetMapper4();
    sp<IMapper4> mapper = GetMapper4();
    if (!mapper) {
    Gralloc4Buffer buffer(handle);
    if (!mapper || !buffer) {
        // Gralloc4 not supported; nothing to do
        // Gralloc4 not supported; nothing to do
        return err;
        return err;
    }
    }
    Error4 mapperErr = Error4::NONE;
    Error4 mapperErr = Error4::NONE;
    if (staticInfo) {
    if (staticInfo) {
        ALOGV("Grabbing static HDR info from gralloc4 metadata");
        staticInfo->reset(new C2StreamHdrStaticMetadataInfo::input(0u));
        staticInfo->reset(new C2StreamHdrStaticMetadataInfo::input(0u));
        memset(&(*staticInfo)->mastering, 0, sizeof((*staticInfo)->mastering));
        memset(&(*staticInfo)->mastering, 0, sizeof((*staticInfo)->mastering));
        (*staticInfo)->maxCll = 0;
        (*staticInfo)->maxCll = 0;
@@ -1049,7 +1071,7 @@ c2_status_t GetHdrMetadataFromGralloc4Handle(
                mapperErr = Error4::BAD_VALUE;
                mapperErr = Error4::BAD_VALUE;
            }
            }
        };
        };
        Return<void> ret = mapper->get(nativeHandle, MetadataType_Smpte2086, cb);
        Return<void> ret = mapper->get(buffer.get(), MetadataType_Smpte2086, cb);
        if (!ret.isOk()) {
        if (!ret.isOk()) {
            err = C2_REFUSED;
            err = C2_REFUSED;
        } else if (mapperErr != Error4::NONE) {
        } else if (mapperErr != Error4::NONE) {
@@ -1070,7 +1092,7 @@ c2_status_t GetHdrMetadataFromGralloc4Handle(
                mapperErr = Error4::BAD_VALUE;
                mapperErr = Error4::BAD_VALUE;
            }
            }
        };
        };
        ret = mapper->get(nativeHandle, MetadataType_Cta861_3, cb);
        ret = mapper->get(buffer.get(), MetadataType_Cta861_3, cb);
        if (!ret.isOk()) {
        if (!ret.isOk()) {
            err = C2_REFUSED;
            err = C2_REFUSED;
        } else if (mapperErr != Error4::NONE) {
        } else if (mapperErr != Error4::NONE) {
@@ -1078,6 +1100,7 @@ c2_status_t GetHdrMetadataFromGralloc4Handle(
        }
        }
    }
    }
    if (dynamicInfo) {
    if (dynamicInfo) {
        ALOGV("Grabbing dynamic HDR info from gralloc4 metadata");
        dynamicInfo->reset();
        dynamicInfo->reset();
        IMapper4::get_cb cb = [&mapperErr, dynamicInfo](Error4 err, const hidl_vec<uint8_t> &vec) {
        IMapper4::get_cb cb = [&mapperErr, dynamicInfo](Error4 err, const hidl_vec<uint8_t> &vec) {
            mapperErr = err;
            mapperErr = err;
@@ -1091,7 +1114,7 @@ c2_status_t GetHdrMetadataFromGralloc4Handle(
                    vec.size(), 0u, C2Config::HDR_DYNAMIC_METADATA_TYPE_SMPTE_2094_40);
                    vec.size(), 0u, C2Config::HDR_DYNAMIC_METADATA_TYPE_SMPTE_2094_40);
            memcpy((*dynamicInfo)->m.data, vec.data(), vec.size());
            memcpy((*dynamicInfo)->m.data, vec.data(), vec.size());
        };
        };
        Return<void> ret = mapper->get(nativeHandle, MetadataType_Smpte2094_40, cb);
        Return<void> ret = mapper->get(buffer.get(), MetadataType_Smpte2094_40, cb);
        if (!ret.isOk() || mapperErr != Error4::NONE) {
        if (!ret.isOk() || mapperErr != Error4::NONE) {
            dynamicInfo->reset();
            dynamicInfo->reset();
        }
        }
@@ -1105,21 +1128,14 @@ c2_status_t SetHdrMetadataToGralloc4Handle(
        const std::shared_ptr<const C2StreamHdrDynamicMetadataInfo::output> &dynamicInfo,
        const std::shared_ptr<const C2StreamHdrDynamicMetadataInfo::output> &dynamicInfo,
        const C2Handle *const handle) {
        const C2Handle *const handle) {
    c2_status_t err = C2_OK;
    c2_status_t err = C2_OK;
    native_handle_t *nativeHandle = UnwrapNativeCodec2GrallocHandle(handle);
    if (nativeHandle == nullptr) {
        // Nothing to do
        return err;
    }
    // TRICKY: UnwrapNativeCodec2GrallocHandle creates a new handle but
    //         does not clone the fds. Thus we need to delete the handle
    //         without closing it when going out of scope.
    NativeHandleDeleter nhd(nativeHandle);
    sp<IMapper4> mapper = GetMapper4();
    sp<IMapper4> mapper = GetMapper4();
    if (!mapper) {
    Gralloc4Buffer buffer(handle);
    if (!mapper || !buffer) {
        // Gralloc4 not supported; nothing to do
        // Gralloc4 not supported; nothing to do
        return err;
        return err;
    }
    }
    if (staticInfo && *staticInfo) {
    if (staticInfo && *staticInfo) {
        ALOGV("Setting static HDR info as gralloc4 metadata");
        std::optional<Smpte2086> smpte2086 = Smpte2086{
        std::optional<Smpte2086> smpte2086 = Smpte2086{
            {staticInfo->mastering.red.x, staticInfo->mastering.red.y},
            {staticInfo->mastering.red.x, staticInfo->mastering.red.y},
            {staticInfo->mastering.green.x, staticInfo->mastering.green.y},
            {staticInfo->mastering.green.x, staticInfo->mastering.green.y},
@@ -1129,8 +1145,17 @@ c2_status_t SetHdrMetadataToGralloc4Handle(
            staticInfo->mastering.minLuminance,
            staticInfo->mastering.minLuminance,
        };
        };
        hidl_vec<uint8_t> vec;
        hidl_vec<uint8_t> vec;
        if (gralloc4::encodeSmpte2086(smpte2086, &vec) == OK) {
        if (0.0 <= smpte2086->primaryRed.x && smpte2086->primaryRed.x <= 1.0
            Return<Error4> ret = mapper->set(nativeHandle, MetadataType_Smpte2086, vec);
                && 0.0 <= smpte2086->primaryRed.y && smpte2086->primaryRed.y <= 1.0
                && 0.0 <= smpte2086->primaryGreen.x && smpte2086->primaryGreen.x <= 1.0
                && 0.0 <= smpte2086->primaryGreen.y && smpte2086->primaryGreen.y <= 1.0
                && 0.0 <= smpte2086->primaryBlue.x && smpte2086->primaryBlue.x <= 1.0
                && 0.0 <= smpte2086->primaryBlue.y && smpte2086->primaryBlue.y <= 1.0
                && 0.0 <= smpte2086->whitePoint.x && smpte2086->whitePoint.x <= 1.0
                && 0.0 <= smpte2086->whitePoint.y && smpte2086->whitePoint.y <= 1.0
                && 0.0 <= smpte2086->maxLuminance && 0.0 <= smpte2086->minLuminance
                && gralloc4::encodeSmpte2086(smpte2086, &vec) == OK) {
            Return<Error4> ret = mapper->set(buffer.get(), MetadataType_Smpte2086, vec);
            if (!ret.isOk()) {
            if (!ret.isOk()) {
                err = C2_REFUSED;
                err = C2_REFUSED;
            } else if (ret != Error4::NONE) {
            } else if (ret != Error4::NONE) {
@@ -1141,8 +1166,9 @@ c2_status_t SetHdrMetadataToGralloc4Handle(
            staticInfo->maxCll,
            staticInfo->maxCll,
            staticInfo->maxFall,
            staticInfo->maxFall,
        };
        };
        if (gralloc4::encodeCta861_3(cta861_3, &vec) == OK) {
        if (0.0 <= cta861_3->maxContentLightLevel && 0.0 <= cta861_3->maxFrameAverageLightLevel
            Return<Error4> ret = mapper->set(nativeHandle, MetadataType_Cta861_3, vec);
                && gralloc4::encodeCta861_3(cta861_3, &vec) == OK) {
            Return<Error4> ret = mapper->set(buffer.get(), MetadataType_Cta861_3, vec);
            if (!ret.isOk()) {
            if (!ret.isOk()) {
                err = C2_REFUSED;
                err = C2_REFUSED;
            } else if (ret != Error4::NONE) {
            } else if (ret != Error4::NONE) {
@@ -1150,7 +1176,8 @@ c2_status_t SetHdrMetadataToGralloc4Handle(
            }
            }
        }
        }
    }
    }
    if (dynamicInfo && *dynamicInfo) {
    if (dynamicInfo && *dynamicInfo && dynamicInfo->flexCount() > 0) {
        ALOGV("Setting dynamic HDR info as gralloc4 metadata");
        hidl_vec<uint8_t> vec;
        hidl_vec<uint8_t> vec;
        vec.resize(dynamicInfo->flexCount());
        vec.resize(dynamicInfo->flexCount());
        memcpy(vec.data(), dynamicInfo->m.data, dynamicInfo->flexCount());
        memcpy(vec.data(), dynamicInfo->m.data, dynamicInfo->flexCount());
@@ -1164,7 +1191,7 @@ c2_status_t SetHdrMetadataToGralloc4Handle(
            break;
            break;
        }
        }
        if (metadataType) {
        if (metadataType) {
            Return<Error4> ret = mapper->set(nativeHandle, *metadataType, vec);
            Return<Error4> ret = mapper->set(buffer.get(), *metadataType, vec);
            if (!ret.isOk()) {
            if (!ret.isOk()) {
                err = C2_REFUSED;
                err = C2_REFUSED;
            } else if (ret != Error4::NONE) {
            } else if (ret != Error4::NONE) {