DSBuffer: implement the DSBufferQueue methods 63/241563/1
authorSooChan Lim <sc1.lim@samsung.com>
Thu, 2 Jul 2020 03:42:40 +0000 (12:42 +0900)
committerSung-Jin Park <sj76.park@samsung.com>
Thu, 20 Aug 2020 09:45:22 +0000 (18:45 +0900)
implement dequeue, enqueue, aquire and release.

Change-Id: I7e90cf7acbb1b36c528c17586a97b266fa440d03

src/DSBuffer/DSBufferQueueTBMImpl.cpp
src/DSBuffer/DSBufferQueueTBMImpl.h
src/DSBuffer/IDSBufferQueue.h
src/DSBuffer/plantuml/IDSBufferQueue.plantuml [changed mode: 0644->0755]
tests/DSBufferTBMImpl-test.cpp

index 2666866..91d3f70 100644 (file)
@@ -1,6 +1,7 @@
 #include "DSBufferQueueTBMImpl.h"
 #include "DSBufferTBMImpl.h"
 #include "DSDebugLog.h"
+#include <algorithm>
 
 namespace display_server
 {
@@ -19,6 +20,7 @@ DSBufferQueueTBMImpl::DSBufferQueueTBMImpl(int slotSize, int bufferWidth, int bu
 
 DSBufferQueueTBMImpl::~DSBufferQueueTBMImpl()
 {
+       __bufferVector.erase(__bufferVector.begin(), __bufferVector.end());
        tbm_surface_queue_destroy(__tqueue);
 }
 
@@ -37,28 +39,81 @@ void *DSBufferQueueTBMImpl::getNativeBufferQueue()
        return (void *)__tqueue;
 }
 
-IDSBuffer *DSBufferQueueTBMImpl::dequeueBuffer()
+std::shared_ptr<IDSBuffer> DSBufferQueueTBMImpl::dequeueBuffer()
 {
-       // TODO:
-       return nullptr;
+       tbm_surface_queue_error_e terror = TBM_SURFACE_QUEUE_ERROR_NONE;
+       tbm_surface_h tsurface;
+
+       terror = tbm_surface_queue_dequeue(__tqueue, &tsurface);
+       if (terror != TBM_SURFACE_QUEUE_ERROR_NONE) {
+               DSLOG_ERR("BuffeQueueTBM", "tbm_surface_queue_dequeue fails.\n");
+               return nullptr;
+       }
+
+       auto isMatchFunc = [tsurface](std::shared_ptr<IDSBuffer> buffer) -> bool {
+               return ((tbm_surface_h)buffer->getNativeBuffer() == tsurface);
+       };
+
+       auto iter= std::find_if(__bufferVector.begin(), __bufferVector.end(), isMatchFunc);
+       if (iter == __bufferVector.end()) {
+               // create a IDSBuffer with tsurface.
+               __bufferVector.push_back(std::make_shared<DSBufferTBMImpl>(__bufferWidth, __bufferWidth, __format, tsurface));
+
+               return __bufferVector.back();
+       }
+
+       return *iter;
 }
 
-bool DSBufferQueueTBMImpl::enqueueBuffer(IDSBuffer *buffer)
+bool DSBufferQueueTBMImpl::enqueueBuffer(std::shared_ptr<IDSBuffer> buffer)
 {
-       // TODO:
-       return false;
+       tbm_surface_queue_error_e terror = TBM_SURFACE_QUEUE_ERROR_NONE;
+       tbm_surface_h tsurface = (tbm_surface_h)buffer->getNativeBuffer();
+
+       terror = tbm_surface_queue_enqueue(__tqueue, tsurface);
+       if (terror != TBM_SURFACE_QUEUE_ERROR_NONE) {
+               DSLOG_ERR("BuffeQueueTBM", "tbm_surface_queue_enqueue fails.");
+               return false;
+       }
+
+       return true;
 }
 
-IDSBuffer *DSBufferQueueTBMImpl::acquireBuffer()
+std::shared_ptr<IDSBuffer> DSBufferQueueTBMImpl::acquireBuffer()
 {
-       // TODO:
-       return nullptr;
+       tbm_surface_queue_error_e terror = TBM_SURFACE_QUEUE_ERROR_NONE;
+       tbm_surface_h tsurface;
+
+       terror = tbm_surface_queue_acquire(__tqueue, &tsurface);
+       if (terror != TBM_SURFACE_QUEUE_ERROR_NONE) {
+               DSLOG_ERR("BuffeQueueTBM", "tbm_surface_queue_enqueue fails.");
+               return nullptr;
+       }
+
+       auto isMatchFunc = [tsurface](std::shared_ptr<IDSBuffer> buffer) -> bool {
+               return ((tbm_surface_h)buffer->getNativeBuffer() == tsurface);
+       };
+
+       auto iter= std::find_if(__bufferVector.begin(), __bufferVector.end(), isMatchFunc);
+       if (iter == __bufferVector.end()) {
+               DSLOG_ERR("BuffeQueueTBM", "No Buffer in this BufferQueue.");
+               return nullptr;
+       }
+
+       return *iter;
 }
 
-bool DSBufferQueueTBMImpl::releaseBuffer(IDSBuffer *buffer)
+bool DSBufferQueueTBMImpl::releaseBuffer(std::shared_ptr<IDSBuffer> buffer)
 {
-       // TODO:
-       return false;
+       tbm_surface_queue_error_e terror = TBM_SURFACE_QUEUE_ERROR_NONE;
+
+       terror = tbm_surface_queue_release(__tqueue, (tbm_surface_h)buffer->getNativeBuffer());
+       if (terror != TBM_SURFACE_QUEUE_ERROR_NONE) {
+               DSLOG_ERR("BuffeQueueTBM", "tbm_surface_queue_release fails.");
+               return false;
+       }
+
+       return true;
 }
 
 }
index 1a7c118..1da8ae0 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "IDSBufferQueue.h"
 #include <tbm_surface_queue.h>
+#include <vector>
 
 namespace display_server
 {
@@ -17,10 +18,10 @@ public:
        int        getBufferHeight() override;
        void       *getNativeBufferQueue() override;
 
-       IDSBuffer  *dequeueBuffer() override;
-       bool       enqueueBuffer(IDSBuffer *buffer) override;
-       IDSBuffer  *acquireBuffer() override;
-       bool       releaseBuffer(IDSBuffer *buffer) override;
+       std::shared_ptr<IDSBuffer> dequeueBuffer() override;
+       bool                       enqueueBuffer(std::shared_ptr<IDSBuffer> buffer) override;
+       std::shared_ptr<IDSBuffer> acquireBuffer() override;
+       bool                       releaseBuffer(std::shared_ptr<IDSBuffer> buffer) override;
 
 private:
        int __slotSize;
@@ -28,6 +29,7 @@ private:
        int __bufferHeight;
        IDSBuffer::Format __format;
        tbm_surface_queue_h __tqueue;
+       std::vector<std::shared_ptr<IDSBuffer>> __bufferVector;
 };
 
 }
index 17cc606..a3e719d 100644 (file)
@@ -2,6 +2,7 @@
 #define __I_DS_BUFFER_QUEUE_H_
 
 #include "IDSBuffer.h"
+#include <memory>
 
 namespace display_server
 {
@@ -14,10 +15,10 @@ public:
        virtual int                        getBufferWidth() = 0;
        virtual int                        getBufferHeight() = 0;
        virtual void                       *getNativeBufferQueue() = 0;
-       virtual IDSBuffer  *dequeueBuffer() = 0;
-       virtual bool       enqueueBuffer(IDSBuffer *buffer) = 0;
-       virtual IDSBuffer  *acquireBuffer() = 0;
-       virtual bool       releaseBuffer(IDSBuffer *buffer) = 0;
+       virtual std::shared_ptr<IDSBuffer> dequeueBuffer() = 0;
+       virtual bool                       enqueueBuffer(std::shared_ptr<IDSBuffer> ) = 0;
+       virtual std::shared_ptr<IDSBuffer> acquireBuffer() = 0;
+       virtual bool                       releaseBuffer(std::shared_ptr<IDSBuffer> ) = 0;
 };
 
 }
old mode 100644 (file)
new mode 100755 (executable)
index fec0c13..65fc114
@@ -1,3 +1,4 @@
+@startuml
 class Client
 {
 }
@@ -7,10 +8,10 @@ Client -[hidden]- IDSBuffer
 
 interface IDSBufferQueue
 {
-       + virtual bool       enqueueBuffer(IDSBuffer *buffer) = 0;
-       + virtual IDSBuffer  *dequeueBuffer() = 0;
-       + virtual IDSBuffer  *acquireBuffer() = 0;
-       + virtual bool       releaseBuffer(IDSBuffer *buffer) = 0;
+       + virtual bool       enqueueBuffer(std::shared_ptr<IDSBuffer> buffer) = 0;
+       + virtual std::shared_ptr<IDSBuffer> dequeueBuffer() = 0;
+       + virtual std::shared_ptr<IDSBuffer> acquireBuffer() = 0;
+       + virtual bool       releaseBuffer(std::shared_ptr<IDSBuffer> buffer) = 0;
 }
 
 IDSBuffer "0..*" *-up-  "1" DSBufferQueueTBMImpl
@@ -28,10 +29,10 @@ class DSBufferQueueTBMImpl
        + DSBufferQueueTBMImpl(int slotSize, int bufferWidth, int bufferHeight, IDSBuffer::Format format);
        + ~DSBufferQueueTBMImpl();
 
-       + bool       enqueueBuffer(IDSBuffer *buffer) override;
+       + bool       enqueueBuffer(std::shared_ptr<IDSBuffer> buffer) override;
        + IDSBuffer  *dequeueBuffer() override;
        + IDSBuffer  *acquireBuffer() override;
-       + bool       releaseBuffer(IDSBuffer *buffer) override;
+       + bool       releaseBuffer(std::shared_ptr<IDSBuffer> buffer) override;
 
        - int __slotSize;
        - int __bufferWidth;
@@ -42,5 +43,5 @@ class DSBufferQueueTBMImpl
 
 IDSBuffer <|-down-- DSBufferTBMImpl
 IDSBuffer <|-down-- DSBufferSHMImpl
-
+@enduml
 
index efc5a5f..f2758e8 100644 (file)
@@ -40,7 +40,7 @@ TEST_F(DSBufferTBMImplTest, DequeueBuffer)
        std::unique_ptr<IDSBufferQueue> bufferQueue = std::make_unique<DSBufferQueueTBMImpl>(3, 100, 100, IDSBuffer::FORMAT_ARGB8888);
        EXPECT_TRUE(bufferQueue.get() != nullptr);
 
-       IDSBuffer *buffer = bufferQueue->dequeueBuffer();
+       std::shared_ptr<IDSBuffer> buffer = bufferQueue->dequeueBuffer();
        EXPECT_TRUE(buffer != nullptr);
 }
 
@@ -49,10 +49,9 @@ TEST_F(DSBufferTBMImplTest, EnqueueBuffer)
        std::unique_ptr<IDSBufferQueue> bufferQueue = std::make_unique<DSBufferQueueTBMImpl>(3, 100, 100, IDSBuffer::FORMAT_ARGB8888);
        EXPECT_TRUE(bufferQueue.get() != nullptr);
 
-       IDSBuffer *buffer = bufferQueue->dequeueBuffer();
+       std::shared_ptr<IDSBuffer> buffer = bufferQueue->dequeueBuffer();
        EXPECT_TRUE(buffer != nullptr);
-       bool ret = bufferQueue->enqueueBuffer(buffer);
-       EXPECT_TRUE(ret);
+       EXPECT_TRUE(bufferQueue->enqueueBuffer(buffer));
 }
 
 TEST_F(DSBufferTBMImplTest, AcquireBuffer)
@@ -60,11 +59,10 @@ TEST_F(DSBufferTBMImplTest, AcquireBuffer)
        std::unique_ptr<IDSBufferQueue> bufferQueue = std::make_unique<DSBufferQueueTBMImpl>(3, 100, 100, IDSBuffer::FORMAT_ARGB8888);
        EXPECT_TRUE(bufferQueue.get() != nullptr);
 
-       IDSBuffer *buffer1 = bufferQueue->dequeueBuffer();
+       std::shared_ptr<IDSBuffer> buffer1 = bufferQueue->dequeueBuffer();
        EXPECT_TRUE(buffer1 != nullptr);
-       bool ret1 = bufferQueue->enqueueBuffer(buffer1);
-       EXPECT_TRUE(ret1);
-       IDSBuffer *buffer2 = bufferQueue->acquireBuffer();
+       EXPECT_TRUE(bufferQueue->enqueueBuffer(buffer1));
+       std::shared_ptr<IDSBuffer> buffer2 = bufferQueue->acquireBuffer();
        EXPECT_TRUE(buffer2 != nullptr);
 }
 
@@ -73,12 +71,36 @@ TEST_F(DSBufferTBMImplTest, ReleaseBuffer)
        std::unique_ptr<IDSBufferQueue> bufferQueue = std::make_unique<DSBufferQueueTBMImpl>(3, 100, 100, IDSBuffer::FORMAT_ARGB8888);
        EXPECT_TRUE(bufferQueue.get() != nullptr);
 
-       IDSBuffer *buffer1 = bufferQueue->dequeueBuffer();
+       std::shared_ptr<IDSBuffer> buffer1 = bufferQueue->dequeueBuffer();
        EXPECT_TRUE(buffer1 != nullptr);
-       bool ret1 = bufferQueue->enqueueBuffer(buffer1);
-       EXPECT_TRUE(ret1);
-       IDSBuffer *buffer2 = bufferQueue->acquireBuffer();
+       EXPECT_TRUE(bufferQueue->enqueueBuffer(buffer1));
+       std::shared_ptr<IDSBuffer> buffer2 = bufferQueue->acquireBuffer();
        EXPECT_TRUE(buffer2 != nullptr);
-       bool ret2 = bufferQueue->releaseBuffer(buffer2);
-       EXPECT_TRUE(ret2);
+       EXPECT_TRUE(bufferQueue->releaseBuffer(buffer2));
+}
+
+TEST_F(DSBufferTBMImplTest, DequeueBuffer_4Times)
+{
+       std::unique_ptr<IDSBufferQueue> bufferQueue = std::make_unique<DSBufferQueueTBMImpl>(3, 100, 100, IDSBuffer::FORMAT_ARGB8888);
+       EXPECT_TRUE(bufferQueue.get() != nullptr);
+
+       std::shared_ptr<IDSBuffer> buffer1 = bufferQueue->dequeueBuffer();
+       EXPECT_TRUE(buffer1 != nullptr);
+       std::shared_ptr<IDSBuffer> buffer2 = bufferQueue->dequeueBuffer();
+       EXPECT_TRUE(buffer2 != nullptr);
+       std::shared_ptr<IDSBuffer> buffer3 = bufferQueue->dequeueBuffer();
+       EXPECT_TRUE(buffer3 != nullptr);
+       EXPECT_TRUE(bufferQueue->releaseBuffer(buffer1));
+       EXPECT_TRUE(bufferQueue->releaseBuffer(buffer2));
+       EXPECT_TRUE(bufferQueue->releaseBuffer(buffer3));
+
+       std::shared_ptr<IDSBuffer> buffer4 = bufferQueue->dequeueBuffer();
+       EXPECT_TRUE(buffer1.get() == buffer4.get());
+       std::shared_ptr<IDSBuffer> buffer5 = bufferQueue->dequeueBuffer();
+       EXPECT_TRUE(buffer2.get() == buffer5.get());
+       std::shared_ptr<IDSBuffer> buffer6 = bufferQueue->dequeueBuffer();
+       EXPECT_TRUE(buffer3.get() == buffer6.get());
+       EXPECT_TRUE(bufferQueue->releaseBuffer(buffer4));
+       EXPECT_TRUE(bufferQueue->releaseBuffer(buffer5));
+       EXPECT_TRUE(bufferQueue->releaseBuffer(buffer6));
 }
\ No newline at end of file