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

Commit 23a81e89 authored by Emilian Peev's avatar Emilian Peev Committed by android-build-merger
Browse files

Camera: Allocate graphics buffers via 'IAllocator'

am: f4483700

Change-Id: I63a048efd4da24d9c9d153f44b462d0b18be8dc1
parents 9c666901 f4483700
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -36,8 +36,11 @@ cc_test {
        "android.hardware.camera.device@3.2",
        "android.hardware.camera.device@3.3",
        "android.hardware.camera.provider@2.4",
        "android.hardware.graphics.allocator@2.0",
        "android.hardware.graphics.common@1.0",
        "android.hardware.graphics.mapper@2.0",
        "android.hidl.allocator@1.0",
        "libgrallocusage",
        "libhidlmemory",
    ],
}
+67 −20
Original line number Diff line number Diff line
@@ -43,6 +43,13 @@
#include <system/camera_metadata.h>
#include <ui/GraphicBuffer.h>

#include <android/hardware/graphics/allocator/2.0/IAllocator.h>
#include <android/hardware/graphics/mapper/2.0/IMapper.h>
#include <android/hardware/graphics/mapper/2.0/types.h>
#include <android/hidl/allocator/1.0/IAllocator.h>
#include <android/hidl/memory/1.0/IMapper.h>
#include <android/hidl/memory/1.0/IMemory.h>

#include <VtsHalHidlTargetTestBase.h>
#include <VtsHalHidlTargetTestEnvBase.h>

@@ -99,6 +106,9 @@ using ::android::hardware::camera::device::V1_0::FrameCallbackFlag;
using ::android::hardware::camera::device::V1_0::HandleTimestampMessage;
using ::android::hardware::MessageQueue;
using ::android::hardware::kSynchronizedReadWrite;
using ::android::hidl::allocator::V1_0::IAllocator;
using ::android::hidl::memory::V1_0::IMemory;
using ::android::hidl::memory::V1_0::IMapper;
using ResultMetadataQueue = MessageQueue<uint8_t, kSynchronizedReadWrite>;
using ::android::hidl::manager::V1_0::IServiceManager;

@@ -606,6 +616,8 @@ public:
    void setParameters(
            const sp<::android::hardware::camera::device::V1_0::ICameraDevice> &device,
            const CameraParameters &cameraParams);
    void allocateGraphicBuffer(uint32_t width, uint32_t height, uint64_t usage,
            PixelFormat format, hidl_handle *buffer_handle /*out*/);
    void waitForFrameLocked(DataCallbackMsg msgFrame,
            std::unique_lock<std::mutex> &l);
    void openEmptyDeviceSession(const std::string &name,
@@ -2994,15 +3006,15 @@ TEST_F(CameraHidlTest, processCaptureRequestPreview) {
                                                               });
                ASSERT_TRUE(ret.isOk());

                sp<GraphicBuffer> gb = new GraphicBuffer(
                    previewStream.width, previewStream.height,
                    static_cast<int32_t>(halStreamConfig.streams[0].overrideFormat), 1,
                hidl_handle buffer_handle;
                allocateGraphicBuffer(previewStream.width, previewStream.height,
                    android_convertGralloc1To0Usage(halStreamConfig.streams[0].producerUsage,
                                                    halStreamConfig.streams[0].consumerUsage));
                ASSERT_NE(nullptr, gb.get());
                        halStreamConfig.streams[0].consumerUsage),
                    halStreamConfig.streams[0].overrideFormat, &buffer_handle);

                StreamBuffer outputBuffer = {halStreamConfig.streams[0].id,
                                             bufferId,
                                             hidl_handle(gb->getNativeBuffer()->handle),
                                             buffer_handle,
                                             BufferStatus::OK,
                                             nullptr,
                                             nullptr};
@@ -3128,15 +3140,15 @@ TEST_F(CameraHidlTest, processCaptureRequestInvalidSinglePreview) {
                                       &supportsPartialResults /*out*/,
                                       &partialResultCount /*out*/);

                sp<GraphicBuffer> gb = new GraphicBuffer(
                    previewStream.width, previewStream.height,
                    static_cast<int32_t>(halStreamConfig.streams[0].overrideFormat), 1,
                hidl_handle buffer_handle;
                allocateGraphicBuffer(previewStream.width, previewStream.height,
                    android_convertGralloc1To0Usage(halStreamConfig.streams[0].producerUsage,
                                                    halStreamConfig.streams[0].consumerUsage));
                        halStreamConfig.streams[0].consumerUsage),
                    halStreamConfig.streams[0].overrideFormat, &buffer_handle);

                StreamBuffer outputBuffer = {halStreamConfig.streams[0].id,
                                             bufferId,
                                             hidl_handle(gb->getNativeBuffer()->handle),
                                             buffer_handle,
                                             BufferStatus::OK,
                                             nullptr,
                                             nullptr};
@@ -3272,7 +3284,6 @@ TEST_F(CameraHidlTest, flushPreviewRequest) {
                                       &previewStream /*out*/, &halStreamConfig /*out*/,
                                       &supportsPartialResults /*out*/,
                                       &partialResultCount /*out*/);

                std::shared_ptr<ResultMetadataQueue> resultQueue;
                auto resultQueueRet =
                    session->getCaptureResultMetadataQueue(
@@ -3300,15 +3311,15 @@ TEST_F(CameraHidlTest, flushPreviewRequest) {
                                                               });
                ASSERT_TRUE(ret.isOk());

                sp<GraphicBuffer> gb = new GraphicBuffer(
                    previewStream.width, previewStream.height,
                    static_cast<int32_t>(halStreamConfig.streams[0].overrideFormat), 1,
                hidl_handle buffer_handle;
                allocateGraphicBuffer(previewStream.width, previewStream.height,
                    android_convertGralloc1To0Usage(halStreamConfig.streams[0].producerUsage,
                                                    halStreamConfig.streams[0].consumerUsage));
                ASSERT_NE(nullptr, gb.get());
                        halStreamConfig.streams[0].consumerUsage),
                    halStreamConfig.streams[0].overrideFormat, &buffer_handle);

                StreamBuffer outputBuffer = {halStreamConfig.streams[0].id,
                                             bufferId,
                                             hidl_handle(gb->getNativeBuffer()->handle),
                                             buffer_handle,
                                             BufferStatus::OK,
                                             nullptr,
                                             nullptr};
@@ -3341,7 +3352,6 @@ TEST_F(CameraHidlTest, flushPreviewRequest) {
                Return<Status> returnStatus = session->flush();
                ASSERT_TRUE(returnStatus.isOk());
                ASSERT_EQ(Status::OK, returnStatus);

                {
                    std::unique_lock<std::mutex> l(mLock);
                    while (!inflightReq.errorCodeValid &&
@@ -3369,7 +3379,6 @@ TEST_F(CameraHidlTest, flushPreviewRequest) {
                                       << static_cast<uint32_t>(inflightReq.errorCode);
                        }
                    }

                    ret = session->close();
                    ASSERT_TRUE(ret.isOk());
                }
@@ -3894,6 +3903,44 @@ void CameraHidlTest::setParameters(
    ASSERT_EQ(Status::OK, returnStatus);
}

void CameraHidlTest::allocateGraphicBuffer(uint32_t width, uint32_t height, uint64_t usage,
        PixelFormat format, hidl_handle *buffer_handle /*out*/) {
    ASSERT_NE(buffer_handle, nullptr);

    sp<android::hardware::graphics::allocator::V2_0::IAllocator> allocator =
        android::hardware::graphics::allocator::V2_0::IAllocator::getService();
    ASSERT_NE(nullptr, allocator.get());

    sp<android::hardware::graphics::mapper::V2_0::IMapper> mapper =
        android::hardware::graphics::mapper::V2_0::IMapper::getService();
    ASSERT_NE(mapper.get(), nullptr);

    android::hardware::graphics::mapper::V2_0::IMapper::BufferDescriptorInfo descriptorInfo {};
    descriptorInfo.width = width;
    descriptorInfo.height = height;
    descriptorInfo.layerCount = 1;
    descriptorInfo.format = format;
    descriptorInfo.usage = usage;

    ::android::hardware::hidl_vec<uint32_t> descriptor;
    auto ret = mapper->createDescriptor(
        descriptorInfo, [&descriptor](android::hardware::graphics::mapper::V2_0::Error err,
                            ::android::hardware::hidl_vec<uint32_t> desc) {
            ASSERT_EQ(err, android::hardware::graphics::mapper::V2_0::Error::NONE);
            descriptor = desc;
        });
    ASSERT_TRUE(ret.isOk());

    ret = allocator->allocate(descriptor, 1u,
        [&](android::hardware::graphics::mapper::V2_0::Error err, uint32_t /*stride*/,
            const ::android::hardware::hidl_vec<::android::hardware::hidl_handle>& buffers) {
            ASSERT_EQ(android::hardware::graphics::mapper::V2_0::Error::NONE, err);
            ASSERT_EQ(buffers.size(), 1u);
            *buffer_handle = buffers[0];
        });
    ASSERT_TRUE(ret.isOk());
}

int main(int argc, char **argv) {
  ::testing::AddGlobalTestEnvironment(CameraHidlEnvironment::Instance());
  ::testing::InitGoogleTest(&argc, argv);