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

Commit 4b13ec99 authored by Marin Shalamanov's avatar Marin Shalamanov Committed by Automerger Merge Worker
Browse files

Merge "VTS: Free allocated buffers" into sc-dev am: 3d8bfc7f

Original change: https://googleplex-android-review.googlesource.com/c/platform/hardware/interfaces/+/14964376

Change-Id: I69d22b6627acdd9e05088597e3e0e81fd25b91e4
parents 411c011f 3d8bfc7f
Loading
Loading
Loading
Loading
+14 −6
Original line number Diff line number Diff line
@@ -308,6 +308,12 @@ void ComposerClient::execute(TestCommandReader* reader, CommandWriterBase* write
    writer->reset();
}

NativeHandleWrapper::~NativeHandleWrapper() {
    if (mHandle) {
        mGralloc.freeBuffer(mHandle);
    }
}

Gralloc::Gralloc() {
    [this] {
        ASSERT_NO_FATAL_FAILURE(mGralloc4 = std::make_shared<Gralloc4>("default", "default",
@@ -324,9 +330,10 @@ Gralloc::Gralloc() {
    }();
}

const native_handle_t* Gralloc::allocate(uint32_t width, uint32_t height, uint32_t layerCount,
const NativeHandleWrapper Gralloc::allocate(uint32_t width, uint32_t height, uint32_t layerCount,
                                            PixelFormat format, uint64_t usage, bool import,
                                            uint32_t* outStride) {
    const native_handle_t* handle;
    if (mGralloc4) {
        IMapper4::BufferDescriptorInfo info{};
        info.width = width;
@@ -334,7 +341,7 @@ const native_handle_t* Gralloc::allocate(uint32_t width, uint32_t height, uint32
        info.layerCount = layerCount;
        info.format = static_cast<android::hardware::graphics::common::V1_2::PixelFormat>(format);
        info.usage = usage;
        return mGralloc4->allocate(info, import, outStride);
        handle = mGralloc4->allocate(info, import, outStride);
    } else if (mGralloc3) {
        IMapper3::BufferDescriptorInfo info{};
        info.width = width;
@@ -342,7 +349,7 @@ const native_handle_t* Gralloc::allocate(uint32_t width, uint32_t height, uint32
        info.layerCount = layerCount;
        info.format = static_cast<android::hardware::graphics::common::V1_2::PixelFormat>(format);
        info.usage = usage;
        return mGralloc3->allocate(info, import, outStride);
        handle = mGralloc3->allocate(info, import, outStride);
    } else {
        IMapper2::BufferDescriptorInfo info{};
        info.width = width;
@@ -350,8 +357,9 @@ const native_handle_t* Gralloc::allocate(uint32_t width, uint32_t height, uint32
        info.layerCount = layerCount;
        info.format = format;
        info.usage = usage;
        return mGralloc2->allocate(info, import, outStride);
        handle = mGralloc2->allocate(info, import, outStride);
    }
    return NativeHandleWrapper(*this, handle);
}

void* Gralloc::lock(const native_handle_t* bufferHandle, uint64_t cpuUsage,
+20 −3
Original line number Diff line number Diff line
@@ -136,11 +136,28 @@ class AccessRegion {
    int32_t height;
};

class Gralloc;

// RAII wrapper around native_handle_t*
class NativeHandleWrapper {
  public:
    NativeHandleWrapper(Gralloc& gralloc, const native_handle_t* handle)
        : mGralloc(gralloc), mHandle(handle) {}

    ~NativeHandleWrapper();

    const native_handle_t* get() { return mHandle; }

  private:
    Gralloc& mGralloc;
    const native_handle_t* mHandle;
};

class Gralloc {
  public:
    explicit Gralloc();

    const native_handle_t* allocate(uint32_t width, uint32_t height, uint32_t layerCount,
    const NativeHandleWrapper allocate(uint32_t width, uint32_t height, uint32_t layerCount,
                                       PixelFormat format, uint64_t usage, bool import = true,
                                       uint32_t* outStride = nullptr);

+14 −14
Original line number Diff line number Diff line
@@ -666,7 +666,7 @@ class GraphicsComposerHidlCommandTest : public GraphicsComposerHidlTest {
        ASSERT_NO_FATAL_FAILURE(GraphicsComposerHidlTest::TearDown());
    }

    const native_handle_t* allocate() {
    NativeHandleWrapper allocate() {
        uint64_t usage =
                static_cast<uint64_t>(BufferUsage::CPU_WRITE_OFTEN | BufferUsage::CPU_READ_OFTEN |
                                      BufferUsage::COMPOSER_OVERLAY);
@@ -727,11 +727,11 @@ TEST_P(GraphicsComposerHidlCommandTest, SET_OUTPUT_BUFFER) {
        display = mComposerClient->createVirtualDisplay(64, 64, PixelFormat::IMPLEMENTATION_DEFINED,
                                                        kBufferSlotCount, &format));

    const native_handle_t* handle;
    ASSERT_NO_FATAL_FAILURE(handle = allocate());
    std::unique_ptr<NativeHandleWrapper> handle;
    ASSERT_NO_FATAL_FAILURE(handle.reset(new NativeHandleWrapper(allocate())));

    mWriter->selectDisplay(display);
    mWriter->setOutputBuffer(0, handle, -1);
    mWriter->setOutputBuffer(0, handle->get(), -1);
    execute();
}

@@ -783,7 +783,7 @@ TEST_P(GraphicsComposerHidlCommandTest, PRESENT_DISPLAY_NO_LAYER_STATE_CHANGES)
    mComposerClient->setColorMode(mPrimaryDisplay, ColorMode::NATIVE);

    auto handle = allocate();
    ASSERT_NE(nullptr, handle);
    ASSERT_NE(nullptr, handle.get());

    IComposerClient::Rect displayFrame{0, 0, mDisplayWidth, mDisplayHeight};

@@ -800,7 +800,7 @@ TEST_P(GraphicsComposerHidlCommandTest, PRESENT_DISPLAY_NO_LAYER_STATE_CHANGES)
    mWriter->setLayerZOrder(10);
    mWriter->setLayerBlendMode(IComposerClient::BlendMode::NONE);
    mWriter->setLayerSurfaceDamage(std::vector<IComposerClient::Rect>(1, displayFrame));
    mWriter->setLayerBuffer(0, handle, -1);
    mWriter->setLayerBuffer(0, handle.get(), -1);
    mWriter->setLayerDataspace(Dataspace::UNKNOWN);

    mWriter->validateDisplay();
@@ -817,8 +817,8 @@ TEST_P(GraphicsComposerHidlCommandTest, PRESENT_DISPLAY_NO_LAYER_STATE_CHANGES)

    mWriter->selectLayer(layer);
    auto handle2 = allocate();
    ASSERT_NE(nullptr, handle2);
    mWriter->setLayerBuffer(0, handle2, -1);
    ASSERT_NE(nullptr, handle2.get());
    mWriter->setLayerBuffer(0, handle2.get(), -1);
    mWriter->setLayerSurfaceDamage(std::vector<IComposerClient::Rect>(1, {0, 0, 10, 10}));
    mWriter->presentDisplay();
    execute();
@@ -833,12 +833,12 @@ TEST_P(GraphicsComposerHidlCommandTest, SET_LAYER_CURSOR_POSITION) {
                                mComposerClient->createLayer(mPrimaryDisplay, kBufferSlotCount));

    auto handle = allocate();
    ASSERT_NE(nullptr, handle);
    ASSERT_NE(nullptr, handle.get());
    IComposerClient::Rect displayFrame{0, 0, mDisplayWidth, mDisplayHeight};

    mWriter->selectDisplay(mPrimaryDisplay);
    mWriter->selectLayer(layer);
    mWriter->setLayerBuffer(0, handle, -1);
    mWriter->setLayerBuffer(0, handle.get(), -1);
    mWriter->setLayerCompositionType(IComposerClient::Composition::CURSOR);
    mWriter->setLayerDisplayFrame(displayFrame);
    mWriter->setLayerPlaneAlpha(1);
@@ -871,7 +871,7 @@ TEST_P(GraphicsComposerHidlCommandTest, SET_LAYER_CURSOR_POSITION) {
 */
TEST_P(GraphicsComposerHidlCommandTest, SET_LAYER_BUFFER) {
    auto handle = allocate();
    ASSERT_NE(nullptr, handle);
    ASSERT_NE(nullptr, handle.get());

    Layer layer;
    ASSERT_NO_FATAL_FAILURE(layer =
@@ -879,7 +879,7 @@ TEST_P(GraphicsComposerHidlCommandTest, SET_LAYER_BUFFER) {

    mWriter->selectDisplay(mPrimaryDisplay);
    mWriter->selectLayer(layer);
    mWriter->setLayerBuffer(0, handle, -1);
    mWriter->setLayerBuffer(0, handle.get(), -1);
    execute();
}

@@ -1003,7 +1003,7 @@ TEST_P(GraphicsComposerHidlCommandTest, SET_LAYER_SIDEBAND_STREAM) {
    }

    auto handle = allocate();
    ASSERT_NE(nullptr, handle);
    ASSERT_NE(nullptr, handle.get());

    Layer layer;
    ASSERT_NO_FATAL_FAILURE(layer =
@@ -1011,7 +1011,7 @@ TEST_P(GraphicsComposerHidlCommandTest, SET_LAYER_SIDEBAND_STREAM) {

    mWriter->selectDisplay(mPrimaryDisplay);
    mWriter->selectLayer(layer);
    mWriter->setLayerSidebandStream(handle);
    mWriter->setLayerSidebandStream(handle.get());
    execute();
}

+18 −36
Original line number Diff line number Diff line
@@ -208,22 +208,13 @@ ReadbackBuffer::ReadbackBuffer(Display display, const std::shared_ptr<ComposerCl
    mAccessRegion.height = height;
}

ReadbackBuffer::~ReadbackBuffer() {
    if (mBufferHandle != nullptr) {
        mGralloc->freeBuffer(mBufferHandle);
    }
}

void ReadbackBuffer::setReadbackBuffer() {
    if (mBufferHandle != nullptr) {
        mGralloc->freeBuffer(mBufferHandle);
        mBufferHandle = nullptr;
    }
    mBufferHandle = mGralloc->allocate(mWidth, mHeight, mLayerCount, mFormat, mUsage,
                                       /*import*/ true, &mStride);
    ASSERT_NE(false, mGralloc->validateBufferSize(mBufferHandle, mWidth, mHeight, mLayerCount,
                                                  mFormat, mUsage, mStride));
    ASSERT_NO_FATAL_FAILURE(mComposerClient->setReadbackBuffer(mDisplay, mBufferHandle, -1));
    mBufferHandle.reset(new Gralloc::NativeHandleWrapper(
            mGralloc->allocate(mWidth, mHeight, mLayerCount, mFormat, mUsage,
                               /*import*/ true, &mStride)));
    ASSERT_NE(false, mGralloc->validateBufferSize(mBufferHandle->get(), mWidth, mHeight,
                                                  mLayerCount, mFormat, mUsage, mStride));
    ASSERT_NO_FATAL_FAILURE(mComposerClient->setReadbackBuffer(mDisplay, mBufferHandle->get(), -1));
}

void ReadbackBuffer::checkReadbackBuffer(std::vector<IComposerClient::Color> expectedColors) {
@@ -231,11 +222,11 @@ void ReadbackBuffer::checkReadbackBuffer(std::vector<IComposerClient::Color> exp
    int32_t fenceHandle;
    ASSERT_NO_FATAL_FAILURE(mComposerClient->getReadbackBufferFence(mDisplay, &fenceHandle));

    void* bufData = mGralloc->lock(mBufferHandle, mUsage, mAccessRegion, fenceHandle);
    void* bufData = mGralloc->lock(mBufferHandle->get(), mUsage, mAccessRegion, fenceHandle);
    ASSERT_TRUE(mPixelFormat == PixelFormat::RGB_888 || mPixelFormat == PixelFormat::RGBA_8888);
    ReadbackHelper::compareColorBuffers(expectedColors, bufData, mStride, mWidth, mHeight,
                                        mPixelFormat);
    int32_t unlockFence = mGralloc->unlock(mBufferHandle);
    int32_t unlockFence = mGralloc->unlock(mBufferHandle->get());
    if (unlockFence != -1) {
        sync_wait(unlockFence, -1);
        close(unlockFence);
@@ -281,23 +272,17 @@ TestBufferLayer::TestBufferLayer(const std::shared_ptr<ComposerClient>& client,
    setSourceCrop({0, 0, (float)width, (float)height});
}

TestBufferLayer::~TestBufferLayer() {
    if (mBufferHandle != nullptr) {
        mGralloc->freeBuffer(mBufferHandle);
    }
}

void TestBufferLayer::write(const std::shared_ptr<CommandWriterBase>& writer) {
    TestLayer::write(writer);
    writer->setLayerCompositionType(mComposition);
    writer->setLayerVisibleRegion(std::vector<IComposerClient::Rect>(1, mDisplayFrame));
    if (mBufferHandle != nullptr) writer->setLayerBuffer(0, mBufferHandle, mFillFence);
    if (mBufferHandle != nullptr) writer->setLayerBuffer(0, mBufferHandle->get(), mFillFence);
}

LayerSettings TestBufferLayer::toRenderEngineLayerSettings() {
    LayerSettings layerSettings = TestLayer::toRenderEngineLayerSettings();
    layerSettings.source.buffer.buffer = std::make_shared<renderengine::ExternalTexture>(
            new GraphicBuffer(mBufferHandle, GraphicBuffer::CLONE_HANDLE, mWidth, mHeight,
            new GraphicBuffer(mBufferHandle->get(), GraphicBuffer::CLONE_HANDLE, mWidth, mHeight,
                              static_cast<int32_t>(mFormat), 1, mUsage, mStride),
            mRenderEngine.getInternalRenderEngine(),
            renderengine::ExternalTexture::Usage::READABLE);
@@ -318,10 +303,10 @@ LayerSettings TestBufferLayer::toRenderEngineLayerSettings() {
}

void TestBufferLayer::fillBuffer(std::vector<IComposerClient::Color> expectedColors) {
    void* bufData = mGralloc->lock(mBufferHandle, mUsage, mAccessRegion, -1);
    void* bufData = mGralloc->lock(mBufferHandle->get(), mUsage, mAccessRegion, -1);
    ASSERT_NO_FATAL_FAILURE(
            ReadbackHelper::fillBuffer(mWidth, mHeight, mStride, bufData, mFormat, expectedColors));
    mFillFence = mGralloc->unlock(mBufferHandle);
    mFillFence = mGralloc->unlock(mBufferHandle->get());
    if (mFillFence != -1) {
        sync_wait(mFillFence, -1);
        close(mFillFence);
@@ -329,16 +314,13 @@ void TestBufferLayer::fillBuffer(std::vector<IComposerClient::Color> expectedCol
}

void TestBufferLayer::setBuffer(std::vector<IComposerClient::Color> colors) {
    if (mBufferHandle != nullptr) {
        mGralloc->freeBuffer(mBufferHandle);
        mBufferHandle = nullptr;
    }
    mBufferHandle = mGralloc->allocate(mWidth, mHeight, mLayerCount, mFormat, mUsage,
                                       /*import*/ true, &mStride);
    ASSERT_NE(nullptr, mBufferHandle);
    mBufferHandle.reset(new Gralloc::NativeHandleWrapper(
            mGralloc->allocate(mWidth, mHeight, mLayerCount, mFormat, mUsage,
                               /*import*/ true, &mStride)));
    ASSERT_NE(nullptr, mBufferHandle->get());
    ASSERT_NO_FATAL_FAILURE(fillBuffer(colors));
    ASSERT_NE(false, mGralloc->validateBufferSize(mBufferHandle, mWidth, mHeight, mLayerCount,
                                                  mFormat, mUsage, mStride));
    ASSERT_NE(false, mGralloc->validateBufferSize(mBufferHandle->get(), mWidth, mHeight,
                                                  mLayerCount, mFormat, mUsage, mStride));
}

void TestBufferLayer::setDataspace(Dataspace dataspace,
+5 −3
Original line number Diff line number Diff line
@@ -93,8 +93,10 @@ class ComposerClient : public V2_1::vts::ComposerClient {

class Gralloc : public V2_1::vts::Gralloc {
  public:
    using NativeHandleWrapper = V2_1::vts::NativeHandleWrapper;

    Gralloc();
    const native_handle_t* allocate(uint32_t width, uint32_t height, uint32_t layerCount,
    const NativeHandleWrapper allocate(uint32_t width, uint32_t height, uint32_t layerCount,
                                       PixelFormat format, uint64_t usage, bool import = true,
                                       uint32_t* outStride = nullptr) {
        return V2_1::vts::Gralloc::allocate(
Loading