最简单的BufferQueue测试程序(二)

前言

在上一篇《最简单的BufferQueue测试程序(一)》中,我们了解了BufferQueue最基本的操作方法。除了dequeue/queue/acquire/release这些基本操作接口外,BufferQueue还为我们提供了一些特殊函数,如attach/detach/cancel等。本篇我们将一起来学习这些特殊函数的基本用法。

Andriod版本:8.1

伪代码

void main(void)
{
	...
	producer->dequeueBuffer();
	producer->requestBuffer();
	
	producer->cancelBuffer();
	producer->attachBuffer();
	producer->detachBuffer();
	
	producer->dequeueBuffer();
	producer->queueBuffer();
	
	consumer->acquireBuffer();
	consumer->releaseBuffer();
	
	consumer->attachBuffer();
	consumer->detachBuffer();
	consumer->discardFreeBuffers();
}

详细参考代码

mytest2.cpp:

#define LOG_TAG "MyTest"

#include 
#include 
#include 

#include 

#include 

using namespace android;

struct DummyConsumer : public BnConsumerListener {
    void onFrameAvailable(const BufferItem& /* item */) override {}
    void onBuffersReleased() override {}
    void onSidebandStreamChanged() override {}
};

int main(int argc, char** argv)
{
    sp<IGraphicBufferProducer> producer;
    sp<IGraphicBufferConsumer> consumer;

    BufferQueue::createBufferQueue(&producer, &consumer);

    consumer->consumerConnect(new DummyConsumer, false);

    IGraphicBufferProducer::QueueBufferOutput output;
    producer->connect(new DummyProducerListener, NATIVE_WINDOW_API_CPU, false, &output);

    /****************** Producer ***********************/
    int slot;
    sp<Fence> fence;
    sp<GraphicBuffer> buffer;
    producer->dequeueBuffer(&slot, &fence, 0, 0, 0,
		    	     GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr, nullptr);
	printf("dequeueBuffer: get slot%d\n", slot);

    producer->requestBuffer(slot, &buffer);
	printf("requestBuffer: slot%d\n", slot);

	producer->cancelBuffer(slot, fence);
	printf("cancelBuffer: slot%d\n", slot);

	producer->attachBuffer(&Slot, buffer);
	printf("attachBuffer: get slot%d\n", slot);

	producer->detachBuffer(slot);
	printf("detachBuffer: slot%d\n", slot);

    producer->dequeueBuffer(&slot, &fence, 0, 0, 0,
		    	     GRALLOC_USAGE_SW_WRITE_OFTEN, nullptr, nullptr);
	printf("dequeueBuffer: get slot%d\n", slot);

    IGraphicBufferProducer::QueueBufferInput input(0, false, HAL_DATASPACE_UNKNOWN, Rect(1, 1),
				    NATIVE_WINDOW_SCALING_MODE_FREEZE, 0, Fence::NO_FENCE);
    producer->queueBuffer(slot, input, &output);
	printf("queueBuffer: slot%d\n", slot);


    /****************** Consumer ***********************/
    BufferItem item;
    consumer->acquireBuffer(&item, static_cast<nsecs_t>(0));
	printf("acquireBuffer: get slot%d\n", slot);

    consumer->releaseBuffer(item.mSlot, item.mFrameNumber, EGL_NO_DISPLAY,
		    	     EGL_NO_SYNC_KHR, Fence::NO_FENCE);
	printf("releaseBuffer: slot%d\n", slot);

	consumer->attachBuffer(&slot, item.mGraphicBuffer);
	printf("attachBuffer: get slot%d\n", slot);

	consumer->detachBuffer(slot);
	printf("detachBuffer: slot%d\n", slot);

	consumer->discardFreeBuffers();
	printf("discardFreeBuffers\n");

    return 0;
}

Android.bp:

cc_test {
    name: "mytest2",

    srcs: ["mytest2.cpp"],

    clang: true,

    shared_libs: [
        "liblog",
        "libbinder",
        "libgui",
        "libui",
        "libutils",
    ],
}

运行结果

$ adb shell /data/mytest2
dequeueBuffer: get slot0
requestBuffer: slot0
cancelBuffer: slot0
attachBuffer: get slot1
detachBuffer: slot1
dequeueBuffer: get slot0
queueBuffer: slot0
acquireBuffer: get slot0
releaseBuffer: slot0
attachBuffer: get slot1
detachBuffer: slot1
discardFreeBuffers

源码下载

GitHub: mytest2

参考资料

Android BufferQueue自测试程序:BufferQueue_test.cpp —— DetachAndReattachOnProducerSide, DetachAndReattachOnConsumerSide

你可能感兴趣的:(Android)