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

Commit 9c604e3c authored by Alec Mouri's avatar Alec Mouri
Browse files

Polish up metadata propagation for native window

Allow for native window apis to attempt to apply gralloc4 metadata
whenever a buffer is queued onto a native window for dataspace and
existing apis for HDR metadata.

Motivation:
1. We are attempting to move to Gralloc 4 metadata as the source of
   truth for this information, rather than a separate side-channel.
2. ANativeWindow_setBuffersDataspace is a public api, but gralloc4
   metadata is not public, so the only way to keep the gralloc4 metadata
   in sync for dataspace is to intercept on Surface.
3. HDR Metadata may be applied on eglSurface and vkSwapchain, which must
   be propagated to gralloc4 metadata to ensure consistency.
4. None of the plumbing for setting gralloc4 metadata existed, so now's
   a good time as any to add the plumbing so that we can start at least
   best-effort using this metadata infrastructure in the framework and
   so that vendor's don't have to reinvent setting metadata.

Bug: 210498094
Test: builds, boots
Change-Id: I6d19dc4ceca8fa885363faf84f57885e58e88240
parent 56becd9a
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -1096,6 +1096,17 @@ void Surface::getQueueBufferInputLocked(android_native_buffer_t* buffer, int fen
    *out = input;
}

void Surface::applyGrallocMetadataLocked(
        android_native_buffer_t* buffer,
        const IGraphicBufferProducer::QueueBufferInput& queueBufferInput) {
    ATRACE_CALL();
    auto& mapper = GraphicBufferMapper::get();
    mapper.setDataspace(buffer->handle, static_cast<ui::Dataspace>(queueBufferInput.dataSpace));
    mapper.setSmpte2086(buffer->handle, queueBufferInput.getHdrMetadata().getSmpte2086());
    mapper.setCta861_3(buffer->handle, queueBufferInput.getHdrMetadata().getCta8613());
    mapper.setSmpte2094_40(buffer->handle, queueBufferInput.getHdrMetadata().getHdr10Plus());
}

void Surface::onBufferQueuedLocked(int slot, sp<Fence> fence,
        const IGraphicBufferProducer::QueueBufferOutput& output) {
    mDequeuedSlots.erase(slot);
@@ -1166,9 +1177,11 @@ int Surface::queueBuffer(android_native_buffer_t* buffer, int fenceFd) {
    IGraphicBufferProducer::QueueBufferOutput output;
    IGraphicBufferProducer::QueueBufferInput input;
    getQueueBufferInputLocked(buffer, fenceFd, mTimestamp, &input);
    applyGrallocMetadataLocked(buffer, input);
    sp<Fence> fence = input.fence;

    nsecs_t now = systemTime();

    status_t err = mGraphicBufferProducer->queueBuffer(i, input, &output);
    mLastQueueDuration = systemTime() - now;
    if (err != OK)  {
+23 −0
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@
#pragma once

#include <stdint.h>
#include <ui/GraphicTypes.h>
#include <optional>
#include <vector>

#include <system/graphics.h>
@@ -43,6 +45,27 @@ struct HdrMetadata : public LightFlattenable<HdrMetadata> {
    status_t flatten(void* buffer, size_t size) const;
    status_t unflatten(void const* buffer, size_t size);

    std::optional<ui::Smpte2086> getSmpte2086() const {
        if (validTypes & Type::SMPTE2086) {
            return ui::translate(smpte2086);
        }
        return {};
    }

    std::optional<ui::Cta861_3> getCta8613() const {
        if (validTypes & Type::CTA861_3) {
            return ui::translate(cta8613);
        }
        return {};
    }

    std::optional<std::vector<uint8_t>> getHdr10Plus() const {
        if (validTypes & Type::HDR10PLUS) {
            return hdr10plus;
        }
        return {};
    }

    bool operator==(const HdrMetadata& rhs) const;
    bool operator!=(const HdrMetadata& rhs) const { return !(*this == rhs); }
};
+7 −0
Original line number Diff line number Diff line
@@ -398,6 +398,13 @@ protected:
    void getQueueBufferInputLocked(android_native_buffer_t* buffer, int fenceFd, nsecs_t timestamp,
            IGraphicBufferProducer::QueueBufferInput* out);

    // For easing in adoption of gralloc4 metadata by vendor components, as well as for supporting
    // the public ANativeWindow api, allow setting relevant metadata when queueing a buffer through
    // a native window
    void applyGrallocMetadataLocked(
            android_native_buffer_t* buffer,
            const IGraphicBufferProducer::QueueBufferInput& queueBufferInput);

    void onBufferQueuedLocked(int slot, sp<Fence> fence,
            const IGraphicBufferProducer::QueueBufferOutput& output);

+61 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@
#include <aidlcommonsupport/NativeHandle.h>
#include <android/binder_enums.h>
#include <android/binder_manager.h>
#include <gralloctypes/Gralloc4.h>
#include <hidl/ServiceManagement.h>
#include <hwbinder/IPCThreadState.h>
#include <ui/Gralloc4.h>
@@ -524,6 +525,37 @@ status_t Gralloc4Mapper::get(buffer_handle_t bufferHandle, const MetadataType& m
    return decodeFunction(vec, outMetadata);
}

template <class T>
status_t Gralloc4Mapper::set(buffer_handle_t bufferHandle, const MetadataType& metadataType,
                             const T& metadata, EncodeFunction<T> encodeFunction) const {
    hidl_vec<uint8_t> encodedMetadata;
    if (const status_t status = encodeFunction(metadata, &encodedMetadata); status != OK) {
        ALOGE("Encoding metadata(%s) failed with %d", metadataType.name.c_str(), status);
        return status;
    }
    hidl_vec<uint8_t> vec;
    auto ret =
            mMapper->set(const_cast<native_handle_t*>(bufferHandle), metadataType, encodedMetadata);

    const Error error = ret.withDefault(kTransactionError);
    switch (error) {
        case Error::BAD_DESCRIPTOR:
        case Error::BAD_BUFFER:
        case Error::BAD_VALUE:
        case Error::NO_RESOURCES:
            ALOGE("set(%s, %" PRIu64 ", ...) failed with %d", metadataType.name.c_str(),
                  metadataType.value, error);
            break;
        // It is not an error to attempt to set metadata that a particular gralloc implementation
        // happens to not support.
        case Error::UNSUPPORTED:
        case Error::NONE:
            break;
    }

    return static_cast<status_t>(error);
}

status_t Gralloc4Mapper::getBufferId(buffer_handle_t bufferHandle, uint64_t* outBufferId) const {
    return get(bufferHandle, gralloc4::MetadataType_BufferId, gralloc4::decodeBufferId,
               outBufferId);
@@ -673,6 +705,12 @@ status_t Gralloc4Mapper::getDataspace(buffer_handle_t bufferHandle,
    return NO_ERROR;
}

status_t Gralloc4Mapper::setDataspace(buffer_handle_t bufferHandle, ui::Dataspace dataspace) const {
    return set(bufferHandle, gralloc4::MetadataType_Dataspace,
               static_cast<aidl::android::hardware::graphics::common::Dataspace>(dataspace),
               gralloc4::encodeDataspace);
}

status_t Gralloc4Mapper::getBlendMode(buffer_handle_t bufferHandle,
                                      ui::BlendMode* outBlendMode) const {
    return get(bufferHandle, gralloc4::MetadataType_BlendMode, gralloc4::decodeBlendMode,
@@ -685,24 +723,47 @@ status_t Gralloc4Mapper::getSmpte2086(buffer_handle_t bufferHandle,
               outSmpte2086);
}

status_t Gralloc4Mapper::setSmpte2086(buffer_handle_t bufferHandle,
                                      std::optional<ui::Smpte2086> smpte2086) const {
    return set(bufferHandle, gralloc4::MetadataType_Smpte2086, smpte2086,
               gralloc4::encodeSmpte2086);
}

status_t Gralloc4Mapper::getCta861_3(buffer_handle_t bufferHandle,
                                     std::optional<ui::Cta861_3>* outCta861_3) const {
    return get(bufferHandle, gralloc4::MetadataType_Cta861_3, gralloc4::decodeCta861_3,
               outCta861_3);
}

status_t Gralloc4Mapper::setCta861_3(buffer_handle_t bufferHandle,
                                     std::optional<ui::Cta861_3> cta861_3) const {
    return set(bufferHandle, gralloc4::MetadataType_Cta861_3, cta861_3, gralloc4::encodeCta861_3);
}

status_t Gralloc4Mapper::getSmpte2094_40(
        buffer_handle_t bufferHandle, std::optional<std::vector<uint8_t>>* outSmpte2094_40) const {
    return get(bufferHandle, gralloc4::MetadataType_Smpte2094_40, gralloc4::decodeSmpte2094_40,
               outSmpte2094_40);
}

status_t Gralloc4Mapper::setSmpte2094_40(buffer_handle_t bufferHandle,
                                         std::optional<std::vector<uint8_t>> smpte2094_40) const {
    return set(bufferHandle, gralloc4::MetadataType_Smpte2094_40, smpte2094_40,
               gralloc4::encodeSmpte2094_40);
}

status_t Gralloc4Mapper::getSmpte2094_10(
        buffer_handle_t bufferHandle, std::optional<std::vector<uint8_t>>* outSmpte2094_10) const {
    return get(bufferHandle, gralloc4::MetadataType_Smpte2094_10, gralloc4::decodeSmpte2094_10,
               outSmpte2094_10);
}

status_t Gralloc4Mapper::setSmpte2094_10(buffer_handle_t bufferHandle,
                                         std::optional<std::vector<uint8_t>> smpte2094_10) const {
    return set(bufferHandle, gralloc4::MetadataType_Smpte2094_10, smpte2094_10,
               gralloc4::encodeSmpte2094_10);
}

template <class T>
status_t Gralloc4Mapper::getDefault(uint32_t width, uint32_t height, PixelFormat format,
                                    uint32_t layerCount, uint64_t usage,
+24 −0
Original line number Diff line number Diff line
@@ -281,6 +281,10 @@ status_t GraphicBufferMapper::getDataspace(buffer_handle_t bufferHandle,
    return mMapper->getDataspace(bufferHandle, outDataspace);
}

status_t GraphicBufferMapper::setDataspace(buffer_handle_t bufferHandle, ui::Dataspace dataspace) {
    return mMapper->setDataspace(bufferHandle, dataspace);
}

status_t GraphicBufferMapper::getBlendMode(buffer_handle_t bufferHandle,
                                           ui::BlendMode* outBlendMode) {
    return mMapper->getBlendMode(bufferHandle, outBlendMode);
@@ -291,21 +295,41 @@ status_t GraphicBufferMapper::getSmpte2086(buffer_handle_t bufferHandle,
    return mMapper->getSmpte2086(bufferHandle, outSmpte2086);
}

status_t GraphicBufferMapper::setSmpte2086(buffer_handle_t bufferHandle,
                                           std::optional<ui::Smpte2086> smpte2086) {
    return mMapper->setSmpte2086(bufferHandle, smpte2086);
}

status_t GraphicBufferMapper::getCta861_3(buffer_handle_t bufferHandle,
                                          std::optional<ui::Cta861_3>* outCta861_3) {
    return mMapper->getCta861_3(bufferHandle, outCta861_3);
}

status_t GraphicBufferMapper::setCta861_3(buffer_handle_t bufferHandle,
                                          std::optional<ui::Cta861_3> cta861_3) {
    return mMapper->setCta861_3(bufferHandle, cta861_3);
}

status_t GraphicBufferMapper::getSmpte2094_40(
        buffer_handle_t bufferHandle, std::optional<std::vector<uint8_t>>* outSmpte2094_40) {
    return mMapper->getSmpte2094_40(bufferHandle, outSmpte2094_40);
}

status_t GraphicBufferMapper::setSmpte2094_40(buffer_handle_t bufferHandle,
                                              std::optional<std::vector<uint8_t>> smpte2094_40) {
    return mMapper->setSmpte2094_40(bufferHandle, smpte2094_40);
}

status_t GraphicBufferMapper::getSmpte2094_10(
        buffer_handle_t bufferHandle, std::optional<std::vector<uint8_t>>* outSmpte2094_10) {
    return mMapper->getSmpte2094_10(bufferHandle, outSmpte2094_10);
}

status_t GraphicBufferMapper::setSmpte2094_10(buffer_handle_t bufferHandle,
                                              std::optional<std::vector<uint8_t>> smpte2094_10) {
    return mMapper->setSmpte2094_10(bufferHandle, smpte2094_10);
}

status_t GraphicBufferMapper::getDefaultPixelFormatFourCC(uint32_t width, uint32_t height,
                                                          PixelFormat format, uint32_t layerCount,
                                                          uint64_t usage,
Loading