14ed2deddSopenharmony_ci/*
24ed2deddSopenharmony_ci * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
34ed2deddSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44ed2deddSopenharmony_ci * you may not use this file except in compliance with the License.
54ed2deddSopenharmony_ci * You may obtain a copy of the License at
64ed2deddSopenharmony_ci *
74ed2deddSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84ed2deddSopenharmony_ci *
94ed2deddSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104ed2deddSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114ed2deddSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124ed2deddSopenharmony_ci * See the License for the specific language governing permissions and
134ed2deddSopenharmony_ci * limitations under the License.
144ed2deddSopenharmony_ci */
154ed2deddSopenharmony_ci
164ed2deddSopenharmony_ci#include "buffer_queue.h"
174ed2deddSopenharmony_ci
184ed2deddSopenharmony_ci#include <list>
194ed2deddSopenharmony_ci#include <string>
204ed2deddSopenharmony_ci
214ed2deddSopenharmony_ci#include "buffer_common.h"
224ed2deddSopenharmony_ci#include "buffer_manager.h"
234ed2deddSopenharmony_ci
244ed2deddSopenharmony_cinamespace OHOS {
254ed2deddSopenharmony_ciconst int32_t BUFFER_STRIDE_ALIGNMENT_DEFAULT = 4;
264ed2deddSopenharmony_ciconst uint8_t BUFFER_QUEUE_SIZE_DEFAULT = 1;
274ed2deddSopenharmony_ciconst uint8_t BUFFER_QUEUE_SIZE_MAX = 10;
284ed2deddSopenharmony_ciconst int32_t BUFFER_CONSUMER_USAGE_DEFAULT = BUFFER_CONSUMER_USAGE_SORTWARE;
294ed2deddSopenharmony_ciconst uint8_t USER_DATA_COUNT = 100;
304ed2deddSopenharmony_ci
314ed2deddSopenharmony_ciBufferQueue::BufferQueue()
324ed2deddSopenharmony_ci    : width_(0),
334ed2deddSopenharmony_ci      height_(0),
344ed2deddSopenharmony_ci      format_(IMAGE_PIXEL_FORMAT_RGB565),
354ed2deddSopenharmony_ci      stride_(0),
364ed2deddSopenharmony_ci      usage_(BUFFER_CONSUMER_USAGE_DEFAULT),
374ed2deddSopenharmony_ci      size_(0),
384ed2deddSopenharmony_ci      queueSize_(BUFFER_QUEUE_SIZE_DEFAULT),
394ed2deddSopenharmony_ci      strideAlignment_(BUFFER_STRIDE_ALIGNMENT_DEFAULT),
404ed2deddSopenharmony_ci      attachCount_(0),
414ed2deddSopenharmony_ci      customSize_(false)
424ed2deddSopenharmony_ci{
434ed2deddSopenharmony_ci}
444ed2deddSopenharmony_ci
454ed2deddSopenharmony_ciBufferQueue::~BufferQueue()
464ed2deddSopenharmony_ci{
474ed2deddSopenharmony_ci    pthread_mutex_lock(&lock_);
484ed2deddSopenharmony_ci    freeList_.clear();
494ed2deddSopenharmony_ci    dirtyList_.clear();
504ed2deddSopenharmony_ci    std::list<SurfaceBufferImpl *>::iterator iterBuffer;
514ed2deddSopenharmony_ci    for (iterBuffer = allBuffers_.begin(); iterBuffer != allBuffers_.end(); ++iterBuffer) {
524ed2deddSopenharmony_ci        SurfaceBufferImpl* tmpBuffer = *iterBuffer;
534ed2deddSopenharmony_ci        BufferManager* bufferManager = BufferManager::GetInstance();
544ed2deddSopenharmony_ci        if (bufferManager == nullptr) {
554ed2deddSopenharmony_ci            continue;
564ed2deddSopenharmony_ci        }
574ed2deddSopenharmony_ci        bufferManager->FreeBuffer(&tmpBuffer);
584ed2deddSopenharmony_ci    }
594ed2deddSopenharmony_ci    allBuffers_.clear();
604ed2deddSopenharmony_ci    pthread_mutex_unlock(&lock_);
614ed2deddSopenharmony_ci    pthread_cond_destroy(&freeCond_);
624ed2deddSopenharmony_ci    pthread_mutex_destroy(&lock_);
634ed2deddSopenharmony_ci}
644ed2deddSopenharmony_ci
654ed2deddSopenharmony_cibool BufferQueue::Init()
664ed2deddSopenharmony_ci{
674ed2deddSopenharmony_ci    if (pthread_mutex_init(&lock_, NULL)) {
684ed2deddSopenharmony_ci        GRAPHIC_LOGE("Failed init mutex");
694ed2deddSopenharmony_ci        return false;
704ed2deddSopenharmony_ci    }
714ed2deddSopenharmony_ci    if (pthread_cond_init(&freeCond_, NULL)) {
724ed2deddSopenharmony_ci        GRAPHIC_LOGE("Failed init cond");
734ed2deddSopenharmony_ci        pthread_mutex_destroy(&lock_);
744ed2deddSopenharmony_ci        return false;
754ed2deddSopenharmony_ci    }
764ed2deddSopenharmony_ci    return true;
774ed2deddSopenharmony_ci}
784ed2deddSopenharmony_ci
794ed2deddSopenharmony_civoid BufferQueue::NeedAttach()
804ed2deddSopenharmony_ci{
814ed2deddSopenharmony_ci    if (queueSize_ == attachCount_) {
824ed2deddSopenharmony_ci        GRAPHIC_LOGI("has alloced %d buffer, could not alloc more.", allBuffers_.size());
834ed2deddSopenharmony_ci        return;
844ed2deddSopenharmony_ci    }
854ed2deddSopenharmony_ci    if (size_ == 0 && isValidAttr(width_, height_, format_, strideAlignment_) != SURFACE_ERROR_OK) {
864ed2deddSopenharmony_ci        GRAPHIC_LOGI("Invalid Attr.");
874ed2deddSopenharmony_ci        return;
884ed2deddSopenharmony_ci    }
894ed2deddSopenharmony_ci    BufferManager* bufferManager = BufferManager::GetInstance();
904ed2deddSopenharmony_ci    RETURN_IF_FAIL(bufferManager);
914ed2deddSopenharmony_ci    SurfaceBufferImpl *buffer = nullptr;
924ed2deddSopenharmony_ci    if (size_ != 0 && customSize_) {
934ed2deddSopenharmony_ci        buffer = bufferManager->AllocBuffer(size_, usage_);
944ed2deddSopenharmony_ci    } else {
954ed2deddSopenharmony_ci        buffer = bufferManager->AllocBuffer(width_, height_, format_, usage_);
964ed2deddSopenharmony_ci    }
974ed2deddSopenharmony_ci    if (buffer == nullptr) {
984ed2deddSopenharmony_ci        GRAPHIC_LOGI("BufferManager alloc memory failed ");
994ed2deddSopenharmony_ci        return;
1004ed2deddSopenharmony_ci    }
1014ed2deddSopenharmony_ci    size_ = buffer->GetSize();
1024ed2deddSopenharmony_ci    stride_ = buffer->GetStride();
1034ed2deddSopenharmony_ci    attachCount_++;
1044ed2deddSopenharmony_ci    freeList_.push_back(buffer);
1054ed2deddSopenharmony_ci    allBuffers_.push_back(buffer);
1064ed2deddSopenharmony_ci}
1074ed2deddSopenharmony_ci
1084ed2deddSopenharmony_cibool BufferQueue::CanRequest(uint8_t wait)
1094ed2deddSopenharmony_ci{
1104ed2deddSopenharmony_ci    bool res = true;
1114ed2deddSopenharmony_ci    if (!freeList_.empty()) {
1124ed2deddSopenharmony_ci        res = true;
1134ed2deddSopenharmony_ci        goto ERROR;
1144ed2deddSopenharmony_ci    }
1154ed2deddSopenharmony_ci    if (attachCount_ < queueSize_) {
1164ed2deddSopenharmony_ci        NeedAttach();
1174ed2deddSopenharmony_ci        res = true;
1184ed2deddSopenharmony_ci        if (freeList_.empty()) {
1194ed2deddSopenharmony_ci            GRAPHIC_LOGI("no buffer in freeQueue for dequeue.");
1204ed2deddSopenharmony_ci            res = false;
1214ed2deddSopenharmony_ci        }
1224ed2deddSopenharmony_ci        goto ERROR;
1234ed2deddSopenharmony_ci    }
1244ed2deddSopenharmony_ci    if (wait) {
1254ed2deddSopenharmony_ci        pthread_cond_wait(&freeCond_, &lock_);
1264ed2deddSopenharmony_ci        res = true;
1274ed2deddSopenharmony_ci    }
1284ed2deddSopenharmony_ciERROR:
1294ed2deddSopenharmony_ci    return res;
1304ed2deddSopenharmony_ci}
1314ed2deddSopenharmony_ci
1324ed2deddSopenharmony_ciSurfaceBufferImpl* BufferQueue::RequestBuffer(uint8_t wait)
1334ed2deddSopenharmony_ci{
1344ed2deddSopenharmony_ci    SurfaceBufferImpl *buffer = nullptr;
1354ed2deddSopenharmony_ci    pthread_mutex_lock(&lock_);
1364ed2deddSopenharmony_ci    if (!CanRequest(wait)) {
1374ed2deddSopenharmony_ci        GRAPHIC_LOGI("No buffer can request now.");
1384ed2deddSopenharmony_ci        goto ERROR;
1394ed2deddSopenharmony_ci    }
1404ed2deddSopenharmony_ci    buffer = freeList_.front();
1414ed2deddSopenharmony_ci    if (buffer == nullptr) {
1424ed2deddSopenharmony_ci        GRAPHIC_LOGI("freeQueue pop buffer failed.");
1434ed2deddSopenharmony_ci        goto ERROR;
1444ed2deddSopenharmony_ci    }
1454ed2deddSopenharmony_ci    freeList_.pop_front();
1464ed2deddSopenharmony_ci    buffer->SetState(BUFFER_STATE_REQUEST);
1474ed2deddSopenharmony_ciERROR:
1484ed2deddSopenharmony_ci    pthread_mutex_unlock(&lock_);
1494ed2deddSopenharmony_ci    return buffer;
1504ed2deddSopenharmony_ci}
1514ed2deddSopenharmony_ci
1524ed2deddSopenharmony_ciSurfaceBufferImpl* BufferQueue::GetBuffer(const SurfaceBufferImpl& buffer)
1534ed2deddSopenharmony_ci{
1544ed2deddSopenharmony_ci    std::list<SurfaceBufferImpl *>::iterator iterBuffer;
1554ed2deddSopenharmony_ci    for (iterBuffer = allBuffers_.begin(); iterBuffer != allBuffers_.end(); ++iterBuffer) {
1564ed2deddSopenharmony_ci        SurfaceBufferImpl *tmpBuffer = *iterBuffer;
1574ed2deddSopenharmony_ci        if (tmpBuffer->equals(buffer)) {
1584ed2deddSopenharmony_ci            return tmpBuffer;
1594ed2deddSopenharmony_ci        }
1604ed2deddSopenharmony_ci    }
1614ed2deddSopenharmony_ci    return nullptr;
1624ed2deddSopenharmony_ci}
1634ed2deddSopenharmony_ci
1644ed2deddSopenharmony_ciint32_t BufferQueue::FlushBuffer(SurfaceBufferImpl& buffer)
1654ed2deddSopenharmony_ci{
1664ed2deddSopenharmony_ci    pthread_mutex_lock(&lock_);
1674ed2deddSopenharmony_ci    SurfaceBufferImpl *tmpBuffer = GetBuffer(buffer);
1684ed2deddSopenharmony_ci    if (tmpBuffer == nullptr || tmpBuffer->GetState() != BUFFER_STATE_REQUEST) {
1694ed2deddSopenharmony_ci        GRAPHIC_LOGI("Buffer is not existed or state invailed.");
1704ed2deddSopenharmony_ci        pthread_mutex_unlock(&lock_);
1714ed2deddSopenharmony_ci        return SURFACE_ERROR_BUFFER_NOT_EXISTED;
1724ed2deddSopenharmony_ci    }
1734ed2deddSopenharmony_ci    dirtyList_.push_back(tmpBuffer);
1744ed2deddSopenharmony_ci    if (&buffer != tmpBuffer) {
1754ed2deddSopenharmony_ci        tmpBuffer->CopyExtraData(buffer);
1764ed2deddSopenharmony_ci    }
1774ed2deddSopenharmony_ci    tmpBuffer->SetState(BUFFER_STATE_FLUSH);
1784ed2deddSopenharmony_ci    pthread_mutex_unlock(&lock_);
1794ed2deddSopenharmony_ci    return 0;
1804ed2deddSopenharmony_ci}
1814ed2deddSopenharmony_ci
1824ed2deddSopenharmony_ciSurfaceBufferImpl* BufferQueue::AcquireBuffer()
1834ed2deddSopenharmony_ci{
1844ed2deddSopenharmony_ci    pthread_mutex_lock(&lock_);
1854ed2deddSopenharmony_ci    if (dirtyList_.empty()) {
1864ed2deddSopenharmony_ci        pthread_mutex_unlock(&lock_);
1874ed2deddSopenharmony_ci        GRAPHIC_LOGD("dirty queue is empty.");
1884ed2deddSopenharmony_ci        return nullptr;
1894ed2deddSopenharmony_ci    }
1904ed2deddSopenharmony_ci    SurfaceBufferImpl *buffer = dirtyList_.front();
1914ed2deddSopenharmony_ci    if (buffer == nullptr) {
1924ed2deddSopenharmony_ci        pthread_mutex_unlock(&lock_);
1934ed2deddSopenharmony_ci        GRAPHIC_LOGW("dirty queue pop buffer failed.");
1944ed2deddSopenharmony_ci        return nullptr;
1954ed2deddSopenharmony_ci    }
1964ed2deddSopenharmony_ci    buffer->SetState(BUFFER_STATE_ACQUIRE);
1974ed2deddSopenharmony_ci    dirtyList_.pop_front();
1984ed2deddSopenharmony_ci    pthread_mutex_unlock(&lock_);
1994ed2deddSopenharmony_ci    return buffer;
2004ed2deddSopenharmony_ci}
2014ed2deddSopenharmony_ci
2024ed2deddSopenharmony_civoid BufferQueue::Detach(SurfaceBufferImpl *buffer)
2034ed2deddSopenharmony_ci{
2044ed2deddSopenharmony_ci    if (buffer == nullptr) {
2054ed2deddSopenharmony_ci        GRAPHIC_LOGW("Detach buffer failed, buffer is null.");
2064ed2deddSopenharmony_ci        return;
2074ed2deddSopenharmony_ci    }
2084ed2deddSopenharmony_ci    freeList_.remove(buffer);
2094ed2deddSopenharmony_ci    dirtyList_.remove(buffer);
2104ed2deddSopenharmony_ci    allBuffers_.remove(buffer);
2114ed2deddSopenharmony_ci    BufferManager* bufferManager = BufferManager::GetInstance();
2124ed2deddSopenharmony_ci    if (bufferManager != nullptr) {
2134ed2deddSopenharmony_ci        bufferManager->FreeBuffer(&buffer);
2144ed2deddSopenharmony_ci    }
2154ed2deddSopenharmony_ci}
2164ed2deddSopenharmony_ci
2174ed2deddSopenharmony_cibool BufferQueue::ReleaseBuffer(const SurfaceBufferImpl& buffer)
2184ed2deddSopenharmony_ci{
2194ed2deddSopenharmony_ci    return ReleaseBuffer(buffer, BUFFER_STATE_ACQUIRE) == SURFACE_ERROR_OK;
2204ed2deddSopenharmony_ci}
2214ed2deddSopenharmony_ci
2224ed2deddSopenharmony_ciint32_t BufferQueue::CancelBuffer(const SurfaceBufferImpl& buffer)
2234ed2deddSopenharmony_ci{
2244ed2deddSopenharmony_ci    return ReleaseBuffer(buffer, BUFFER_STATE_REQUEST);
2254ed2deddSopenharmony_ci}
2264ed2deddSopenharmony_ci
2274ed2deddSopenharmony_ciint32_t BufferQueue::ReleaseBuffer(const SurfaceBufferImpl& buffer, BufferState state)
2284ed2deddSopenharmony_ci{
2294ed2deddSopenharmony_ci    int32_t ret = 0;
2304ed2deddSopenharmony_ci    pthread_mutex_lock(&lock_);
2314ed2deddSopenharmony_ci    SurfaceBufferImpl *tmpBuffer = GetBuffer(buffer);
2324ed2deddSopenharmony_ci    if (tmpBuffer == nullptr || tmpBuffer->GetState() != state) {
2334ed2deddSopenharmony_ci        GRAPHIC_LOGI("Buffer is not existed or state invailed.");
2344ed2deddSopenharmony_ci        ret = SURFACE_ERROR_BUFFER_NOT_EXISTED;
2354ed2deddSopenharmony_ci        goto ERROR;
2364ed2deddSopenharmony_ci    }
2374ed2deddSopenharmony_ci
2384ed2deddSopenharmony_ci    if (tmpBuffer->GetDeletePending() == 1) {
2394ed2deddSopenharmony_ci        GRAPHIC_LOGI("Release the buffer which state is deletePending.");
2404ed2deddSopenharmony_ci        Detach(tmpBuffer);
2414ed2deddSopenharmony_ci        ret = SURFACE_ERROR_OK;
2424ed2deddSopenharmony_ci        goto ERROR;
2434ed2deddSopenharmony_ci    }
2444ed2deddSopenharmony_ci
2454ed2deddSopenharmony_ci    if (allBuffers_.size() > queueSize_) {
2464ed2deddSopenharmony_ci        GRAPHIC_LOGI("Release the buffer: alloc buffer count is more than max queue count.");
2474ed2deddSopenharmony_ci        attachCount_--;
2484ed2deddSopenharmony_ci        Detach(tmpBuffer);
2494ed2deddSopenharmony_ci        ret = SURFACE_ERROR_OK;
2504ed2deddSopenharmony_ci        goto ERROR;
2514ed2deddSopenharmony_ci    }
2524ed2deddSopenharmony_ci
2534ed2deddSopenharmony_ci    freeList_.push_back(tmpBuffer);
2544ed2deddSopenharmony_ci    tmpBuffer->SetState(BUFFER_STATE_RELEASE);
2554ed2deddSopenharmony_ci    tmpBuffer->ClearExtraData();
2564ed2deddSopenharmony_ciERROR:
2574ed2deddSopenharmony_ci    pthread_mutex_unlock(&lock_);
2584ed2deddSopenharmony_ci    pthread_cond_signal(&freeCond_);
2594ed2deddSopenharmony_ci    return ret;
2604ed2deddSopenharmony_ci}
2614ed2deddSopenharmony_ci
2624ed2deddSopenharmony_ciint32_t BufferQueue::isValidAttr(uint32_t width, uint32_t height, uint32_t format, uint32_t strideAlignment)
2634ed2deddSopenharmony_ci{
2644ed2deddSopenharmony_ci    if (width == 0 || height == 0 || strideAlignment <= 0
2654ed2deddSopenharmony_ci        || format == IMAGE_PIXEL_FORMAT_NONE) {
2664ed2deddSopenharmony_ci            return SURFACE_ERROR_INVALID_PARAM;
2674ed2deddSopenharmony_ci    }
2684ed2deddSopenharmony_ci    return SURFACE_ERROR_OK;
2694ed2deddSopenharmony_ci}
2704ed2deddSopenharmony_ci
2714ed2deddSopenharmony_ciint32_t BufferQueue::Reset(uint32_t size)
2724ed2deddSopenharmony_ci{
2734ed2deddSopenharmony_ci    if (size == 0) {
2744ed2deddSopenharmony_ci        if (isValidAttr(width_, height_, format_, strideAlignment_) != SURFACE_ERROR_OK) {
2754ed2deddSopenharmony_ci            GRAPHIC_LOGI("Invalid Attr.");
2764ed2deddSopenharmony_ci            return SURFACE_ERROR_INVALID_PARAM;
2774ed2deddSopenharmony_ci        } else {
2784ed2deddSopenharmony_ci            size_ = 0;
2794ed2deddSopenharmony_ci            customSize_ = false;
2804ed2deddSopenharmony_ci        }
2814ed2deddSopenharmony_ci    }
2824ed2deddSopenharmony_ci    std::list<SurfaceBufferImpl *>::iterator iterBuffer = freeList_.begin();
2834ed2deddSopenharmony_ci    while (iterBuffer != freeList_.end()) {
2844ed2deddSopenharmony_ci        SurfaceBufferImpl *tmpBuffer = *iterBuffer;
2854ed2deddSopenharmony_ci        dirtyList_.remove(tmpBuffer);
2864ed2deddSopenharmony_ci        allBuffers_.remove(tmpBuffer);
2874ed2deddSopenharmony_ci        BufferManager* bufferManager = BufferManager::GetInstance();
2884ed2deddSopenharmony_ci        if (bufferManager == nullptr) {
2894ed2deddSopenharmony_ci             ++iterBuffer;
2904ed2deddSopenharmony_ci            continue;
2914ed2deddSopenharmony_ci        }
2924ed2deddSopenharmony_ci        bufferManager->FreeBuffer(&tmpBuffer);
2934ed2deddSopenharmony_ci        iterBuffer = freeList_.erase(iterBuffer);
2944ed2deddSopenharmony_ci    }
2954ed2deddSopenharmony_ci    for (iterBuffer = allBuffers_.begin(); iterBuffer != allBuffers_.end(); ++iterBuffer) {
2964ed2deddSopenharmony_ci        SurfaceBufferImpl *tmpBuffer = *iterBuffer;
2974ed2deddSopenharmony_ci        tmpBuffer->SetDeletePending(1);
2984ed2deddSopenharmony_ci    }
2994ed2deddSopenharmony_ci    attachCount_ = 0;
3004ed2deddSopenharmony_ci    return 0;
3014ed2deddSopenharmony_ci}
3024ed2deddSopenharmony_ci
3034ed2deddSopenharmony_civoid BufferQueue::SetQueueSize(uint8_t queueSize)
3044ed2deddSopenharmony_ci{
3054ed2deddSopenharmony_ci    if (queueSize > BUFFER_QUEUE_SIZE_MAX || queueSize == queueSize_) {
3064ed2deddSopenharmony_ci        GRAPHIC_LOGI("The queue count(%u) is invalid", queueSize);
3074ed2deddSopenharmony_ci        return;
3084ed2deddSopenharmony_ci    }
3094ed2deddSopenharmony_ci    pthread_mutex_lock(&lock_);
3104ed2deddSopenharmony_ci    if (queueSize_ > queueSize) {
3114ed2deddSopenharmony_ci        uint8_t needDelete = queueSize_ - queueSize;
3124ed2deddSopenharmony_ci        std::list<SurfaceBufferImpl *>::iterator iterBuffer = freeList_.begin();
3134ed2deddSopenharmony_ci        while (iterBuffer != freeList_.end()) {
3144ed2deddSopenharmony_ci            SurfaceBufferImpl *tmpBuffer = *iterBuffer;
3154ed2deddSopenharmony_ci            dirtyList_.remove(tmpBuffer);
3164ed2deddSopenharmony_ci            allBuffers_.remove(tmpBuffer);
3174ed2deddSopenharmony_ci            BufferManager* bufferManager = BufferManager::GetInstance();
3184ed2deddSopenharmony_ci            if (bufferManager == nullptr) {
3194ed2deddSopenharmony_ci                ++iterBuffer;
3204ed2deddSopenharmony_ci                continue;
3214ed2deddSopenharmony_ci            }
3224ed2deddSopenharmony_ci            bufferManager->FreeBuffer(&tmpBuffer);
3234ed2deddSopenharmony_ci            iterBuffer = freeList_.erase(iterBuffer);
3244ed2deddSopenharmony_ci            needDelete--;
3254ed2deddSopenharmony_ci            attachCount_--;
3264ed2deddSopenharmony_ci            if (needDelete == 0) {
3274ed2deddSopenharmony_ci                break;
3284ed2deddSopenharmony_ci            }
3294ed2deddSopenharmony_ci        }
3304ed2deddSopenharmony_ci        queueSize_ = queueSize;
3314ed2deddSopenharmony_ci        pthread_mutex_unlock(&lock_);
3324ed2deddSopenharmony_ci    } else if (queueSize_ < queueSize) {
3334ed2deddSopenharmony_ci        queueSize_ = queueSize;
3344ed2deddSopenharmony_ci        pthread_mutex_unlock(&lock_);
3354ed2deddSopenharmony_ci        pthread_cond_signal(&freeCond_);
3364ed2deddSopenharmony_ci    }
3374ed2deddSopenharmony_ci}
3384ed2deddSopenharmony_ci
3394ed2deddSopenharmony_ciuint8_t BufferQueue::GetQueueSize()
3404ed2deddSopenharmony_ci{
3414ed2deddSopenharmony_ci    return queueSize_;
3424ed2deddSopenharmony_ci}
3434ed2deddSopenharmony_ci
3444ed2deddSopenharmony_civoid BufferQueue::SetWidthAndHeight(uint32_t width, uint32_t height)
3454ed2deddSopenharmony_ci{
3464ed2deddSopenharmony_ci    pthread_mutex_lock(&lock_);
3474ed2deddSopenharmony_ci    width_ = width;
3484ed2deddSopenharmony_ci    height_ = height;
3494ed2deddSopenharmony_ci    Reset();
3504ed2deddSopenharmony_ci    pthread_mutex_unlock(&lock_);
3514ed2deddSopenharmony_ci    pthread_cond_signal(&freeCond_);
3524ed2deddSopenharmony_ci}
3534ed2deddSopenharmony_ci
3544ed2deddSopenharmony_ciint32_t BufferQueue::GetWidth()
3554ed2deddSopenharmony_ci{
3564ed2deddSopenharmony_ci    return width_;
3574ed2deddSopenharmony_ci}
3584ed2deddSopenharmony_ci
3594ed2deddSopenharmony_ciint32_t BufferQueue::GetHeight()
3604ed2deddSopenharmony_ci{
3614ed2deddSopenharmony_ci    return height_;
3624ed2deddSopenharmony_ci}
3634ed2deddSopenharmony_ci
3644ed2deddSopenharmony_civoid BufferQueue::SetSize(uint32_t size)
3654ed2deddSopenharmony_ci{
3664ed2deddSopenharmony_ci    pthread_mutex_lock(&lock_);
3674ed2deddSopenharmony_ci    size_ = size;
3684ed2deddSopenharmony_ci    customSize_ = true;
3694ed2deddSopenharmony_ci    Reset(size);
3704ed2deddSopenharmony_ci    pthread_mutex_unlock(&lock_);
3714ed2deddSopenharmony_ci    pthread_cond_signal(&freeCond_);
3724ed2deddSopenharmony_ci}
3734ed2deddSopenharmony_ci
3744ed2deddSopenharmony_ciint32_t BufferQueue::GetSize()
3754ed2deddSopenharmony_ci{
3764ed2deddSopenharmony_ci    return size_;
3774ed2deddSopenharmony_ci}
3784ed2deddSopenharmony_ci
3794ed2deddSopenharmony_civoid BufferQueue::SetUserData(const std::string& key, const std::string& value)
3804ed2deddSopenharmony_ci{
3814ed2deddSopenharmony_ci    if (usrDataMap_.size() > USER_DATA_COUNT) {
3824ed2deddSopenharmony_ci        return;
3834ed2deddSopenharmony_ci    }
3844ed2deddSopenharmony_ci    usrDataMap_[key] = value;
3854ed2deddSopenharmony_ci}
3864ed2deddSopenharmony_ci
3874ed2deddSopenharmony_cistd::string BufferQueue::GetUserData(const std::string& key)
3884ed2deddSopenharmony_ci{
3894ed2deddSopenharmony_ci    auto p = usrDataMap_.find(key);
3904ed2deddSopenharmony_ci    if (p != usrDataMap_.end()) {
3914ed2deddSopenharmony_ci        return p->second;
3924ed2deddSopenharmony_ci    }
3934ed2deddSopenharmony_ci    return std::string();
3944ed2deddSopenharmony_ci}
3954ed2deddSopenharmony_ci
3964ed2deddSopenharmony_civoid BufferQueue::SetFormat(uint32_t format)
3974ed2deddSopenharmony_ci{
3984ed2deddSopenharmony_ci    if (format == IMAGE_PIXEL_FORMAT_NONE) {
3994ed2deddSopenharmony_ci        GRAPHIC_LOGI("Format is invailed or not supported %u", format);
4004ed2deddSopenharmony_ci        return;
4014ed2deddSopenharmony_ci    }
4024ed2deddSopenharmony_ci    pthread_mutex_lock(&lock_);
4034ed2deddSopenharmony_ci    format_ = format;
4044ed2deddSopenharmony_ci    Reset();
4054ed2deddSopenharmony_ci    pthread_mutex_unlock(&lock_);
4064ed2deddSopenharmony_ci    pthread_cond_signal(&freeCond_);
4074ed2deddSopenharmony_ci}
4084ed2deddSopenharmony_ci
4094ed2deddSopenharmony_ciint32_t BufferQueue::GetFormat()
4104ed2deddSopenharmony_ci{
4114ed2deddSopenharmony_ci    return format_;
4124ed2deddSopenharmony_ci}
4134ed2deddSopenharmony_ci
4144ed2deddSopenharmony_civoid BufferQueue::SetStrideAlignment(uint32_t stride)
4154ed2deddSopenharmony_ci{
4164ed2deddSopenharmony_ci    pthread_mutex_lock(&lock_);
4174ed2deddSopenharmony_ci    strideAlignment_ = stride;
4184ed2deddSopenharmony_ci    Reset();
4194ed2deddSopenharmony_ci    pthread_mutex_unlock(&lock_);
4204ed2deddSopenharmony_ci    pthread_cond_signal(&freeCond_);
4214ed2deddSopenharmony_ci}
4224ed2deddSopenharmony_ci
4234ed2deddSopenharmony_ciint32_t BufferQueue::GetStrideAlignment()
4244ed2deddSopenharmony_ci{
4254ed2deddSopenharmony_ci    return strideAlignment_;
4264ed2deddSopenharmony_ci}
4274ed2deddSopenharmony_ci
4284ed2deddSopenharmony_ciint32_t BufferQueue::GetStride()
4294ed2deddSopenharmony_ci{
4304ed2deddSopenharmony_ci    return stride_;
4314ed2deddSopenharmony_ci}
4324ed2deddSopenharmony_ci
4334ed2deddSopenharmony_civoid BufferQueue::SetUsage(uint32_t usage)
4344ed2deddSopenharmony_ci{
4354ed2deddSopenharmony_ci    pthread_mutex_lock(&lock_);
4364ed2deddSopenharmony_ci    usage_ = usage;
4374ed2deddSopenharmony_ci    Reset();
4384ed2deddSopenharmony_ci    pthread_mutex_unlock(&lock_);
4394ed2deddSopenharmony_ci    pthread_cond_signal(&freeCond_);
4404ed2deddSopenharmony_ci}
4414ed2deddSopenharmony_ci
4424ed2deddSopenharmony_ciint32_t BufferQueue::GetUsage()
4434ed2deddSopenharmony_ci{
4444ed2deddSopenharmony_ci    return usage_;
4454ed2deddSopenharmony_ci}
4464ed2deddSopenharmony_ci} // end namespace
447