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

Commit 79d13ff0 authored by Chia-I Wu's avatar Chia-I Wu
Browse files

graphics: revise gralloc interfaces

Revise IAllocator and IMapper to reduce IPC and to support gralloc0
devices.

Specifically, IAllocator is trimmed down to have essentially only

    allocate(BufferDescriptor descriptor, uint32_t count)
        generates (Error error,
                   uint32_t stride,
                   vec<handle> buffers);

The ability to allocate buffers with shared backing store is
removed.  ProducerUsage and ConsumerUsage are moved to the
graphics.common package and are merged and renamed to BufferUsage.
BufferUsage's bits follow gralloc0.

IMapper gains

    typedef vec<uint32_t> BufferDescriptor;
    createDescriptor(BufferDescriptorInfo descriptorInfo)
          generates (Error error,
                     BufferDescriptor descriptor);

where BufferDescriptor is an implementation-defined blob.  lockFlex
is replaced by lockYCbCr.  All getters are removed.

Reference counting with retain/release is replaced by
importBuffer/freeBuffer.

Most if not all gralloc1 features are not used by the runtime yet.
There is also not too much test written for them.  As such, they
tend to behave differently between implementations and cannot be
used reliably.

Bug: 36481301
Test: builds and boots on Pixel
Change-Id: I1d31105120517ea2c128c7a19297acf3bfd312bb
parent 5ed47334
Loading
Loading
Loading
Loading
+14 −64
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@

#include <android/log.h>
#include <cutils/native_handle.h>
#include <ui/GraphicBufferMapper.h>
#include <ui/GraphicBuffer.h>

#include <algorithm>    // std::min
@@ -135,11 +134,7 @@ Return<void> FrameHandler::deliverFrame(const BufferDesc& bufferArg) {
    // Local flag we use to keep track of when the stream is stopping
    bool timeToStop = false;

    // TODO:  Why do we get a gralloc crash if we don't clone the buffer here?
    BufferDesc buffer(bufferArg);
    ALOGD("Clone the received frame as %p", buffer.memHandle.getNativeHandle());

    if (buffer.memHandle.getNativeHandle() == nullptr) {
    if (bufferArg.memHandle.getNativeHandle() == nullptr) {
        // Signal that the last frame has been received and the stream is stopped
        timeToStop = true;
    } else {
@@ -157,13 +152,8 @@ Return<void> FrameHandler::deliverFrame(const BufferDesc& bufferArg) {
                printf("Didn't get target buffer - frame lost\n");
                ALOGE("Didn't get requested output buffer -- skipping this frame.");
            } else {
                // In order for the handles passed through HIDL and stored in the BufferDesc to
                // be lockable, we must register them with GraphicBufferMapper
                registerBufferHelper(tgtBuffer);
                registerBufferHelper(buffer);

                // Copy the contents of the of buffer.memHandle into tgtBuffer
                copyBufferContents(tgtBuffer, buffer);
                copyBufferContents(tgtBuffer, bufferArg);

                // Send the target buffer back for display
                Return <EvsResult> result = mDisplay->returnTargetBufferForDisplay(tgtBuffer);
@@ -183,10 +173,6 @@ Return<void> FrameHandler::deliverFrame(const BufferDesc& bufferArg) {
                    mFramesDisplayed++;
                    mLock.unlock();
                }

                // Now tell GraphicBufferMapper we won't be using these handles anymore
                unregisterBufferHelper(tgtBuffer);
                unregisterBufferHelper(buffer);
            }
        }

@@ -233,24 +219,22 @@ bool FrameHandler::copyBufferContents(const BufferDesc& tgtBuffer,
    const unsigned height    = std::min(tgtBuffer.height,
                                        srcBuffer.height);

    android::GraphicBufferMapper &mapper = android::GraphicBufferMapper::get();

    sp<android::GraphicBuffer> tgt = new android::GraphicBuffer(
        tgtBuffer.memHandle, android::GraphicBuffer::CLONE_HANDLE,
        tgtBuffer.width, tgtBuffer.height, tgtBuffer.format, 1, tgtBuffer.usage,
        tgtBuffer.stride);
    sp<android::GraphicBuffer> src = new android::GraphicBuffer(
        srcBuffer.memHandle, android::GraphicBuffer::CLONE_HANDLE,
        srcBuffer.width, srcBuffer.height, srcBuffer.format, 1, srcBuffer.usage,
        srcBuffer.stride);

    // Lock our source buffer for reading
    unsigned char* srcPixels = nullptr;
    mapper.registerBuffer(srcBuffer.memHandle);
    mapper.lock(srcBuffer.memHandle,
                GRALLOC_USAGE_SW_READ_OFTEN,
                android::Rect(width, height),
                (void **) &srcPixels);
    src->lock(GRALLOC_USAGE_SW_READ_OFTEN, (void**)&srcPixels);

    // Lock our target buffer for writing
    unsigned char* tgtPixels = nullptr;
    mapper.registerBuffer(tgtBuffer.memHandle);
    mapper.lock(tgtBuffer.memHandle,
                GRALLOC_USAGE_SW_WRITE_OFTEN,
                android::Rect(width, height),
                (void **) &tgtPixels);
    tgt->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)&tgtPixels);

    if (srcPixels && tgtPixels) {
        for (unsigned row = 0; row < height; row++) {
@@ -267,45 +251,11 @@ bool FrameHandler::copyBufferContents(const BufferDesc& tgtBuffer,
    }

    if (srcPixels) {
        mapper.unlock(srcBuffer.memHandle);
        src->unlock();
    }
    if (tgtPixels) {
        mapper.unlock(tgtBuffer.memHandle);
        tgt->unlock();
    }
    mapper.unregisterBuffer(srcBuffer.memHandle);
    mapper.unregisterBuffer(tgtBuffer.memHandle);

    return success;
}


void FrameHandler::registerBufferHelper(const BufferDesc& buffer)
{
    // In order for the handles passed through HIDL and stored in the BufferDesc to
    // be lockable, we must register them with GraphicBufferMapper.
    // If the device upon which we're running supports gralloc1, we could just call
    // registerBuffer directly with the handle.  But that call  is broken for gralloc0 devices
    // (which we care about, at least for now).  As a result, we have to synthesize a GraphicBuffer
    // object around the buffer handle in order to make a call to the overloaded alternate
    // version of the registerBuffer call that does happen to work on gralloc0 devices.
#if REGISTER_BUFFER_ALWAYS_WORKS
    android::GraphicBufferMapper::get().registerBuffer(buffer.memHandle);
#else
    android::sp<android::GraphicBuffer> pGfxBuff = new android::GraphicBuffer(
            buffer.width, buffer.height, buffer.format,
            1, /* we always use exactly one layer */
            buffer.usage, buffer.stride,
            const_cast<native_handle_t*>(buffer.memHandle.getNativeHandle()),
            false /* GraphicBuffer should not try to free the handle */
    );

    android::GraphicBufferMapper::get().registerBuffer(pGfxBuff.get());
#endif
}


void FrameHandler::unregisterBufferHelper(const BufferDesc& buffer)
{
    // Now tell GraphicBufferMapper we won't be using these handles anymore
    android::GraphicBufferMapper::get().unregisterBuffer(buffer.memHandle);
}
+0 −2
Original line number Diff line number Diff line
@@ -68,8 +68,6 @@ private:

    // Local implementation details
    bool copyBufferContents(const BufferDesc& tgtBuffer, const BufferDesc& srcBuffer);
    void registerBufferHelper(const BufferDesc& buffer);
    void unregisterBufferHelper(const BufferDesc& buffer);

    // Values initialized as startup
    android::sp <IEvsCamera>    mCamera;
+0 −2
Original line number Diff line number Diff line
@@ -66,7 +66,6 @@ cc_library_shared {
        "libutils",
        "libcutils",
        "android.hardware.camera.common@1.0",
        "android.hardware.graphics.allocator@2.0",
        "android.hardware.graphics.common@1.0",
        "android.hidl.base@1.0",
    ],
@@ -76,7 +75,6 @@ cc_library_shared {
        "libhwbinder",
        "libutils",
        "android.hardware.camera.common@1.0",
        "android.hardware.graphics.allocator@2.0",
        "android.hardware.graphics.common@1.0",
        "android.hidl.base@1.0",
    ],
+1 −2
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package android.hardware.camera.device@1.0;

import android.hardware.camera.common@1.0::types;
import android.hardware.graphics.allocator@2.0::types;
import android.hardware.graphics.common@1.0::types;

/**
@@ -89,7 +88,7 @@ interface ICameraDevicePreviewCallback {
     *
     * @return Status The status code for this operation.
     */
    setUsage(ProducerUsage usage) generates (Status status);
    setUsage(BufferUsage usage) generates (Status status);

    /**
     * Set the expected buffering mode for the preview output.
+2 −2
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ namespace device {
namespace V1_0 {
namespace implementation {

using ::android::hardware::graphics::allocator::V2_0::ProducerUsage;
using ::android::hardware::graphics::common::V1_0::BufferUsage;
using ::android::hardware::graphics::common::V1_0::PixelFormat;

HandleImporter& CameraDevice::sHandleImporter = HandleImporter::getInstance();
@@ -259,7 +259,7 @@ int CameraDevice::sSetUsage(struct preview_stream_ops* w, int usage) {
    }

    object->cleanUpCirculatingBuffers();
    return getStatusT(object->mPreviewCallback->setUsage((ProducerUsage) usage));
    return getStatusT(object->mPreviewCallback->setUsage((BufferUsage)usage));
}

int CameraDevice::sSetSwapInterval(struct preview_stream_ops *w, int interval) {
Loading