add hardware buffer ndk sample

Signed-off-by: mingzhu_pearl <limingzhu10@huawei.com>
Change-Id: Ia59b22eec4b6874aedea16ec062d9ea813301781
This commit is contained in:
mingzhu_pearl 2022-06-29 11:34:01 +08:00
parent b2d4a69b5d
commit 9dc2768809
9 changed files with 337 additions and 198 deletions

View File

@ -55,6 +55,7 @@
"//foundation/graphic/graphic_2d/interfaces/kits/napi:napi_packages",
"//foundation/graphic/graphic_2d/rosen/samples/composer:hello_composer",
"//foundation/graphic/graphic_2d/rosen/samples/hello_vsync:hello_vsync",
"//foundation/graphic/graphic_2d/rosen/samples/hello_hardware_buffer:hello_hardware_buffer",
"//foundation/graphic/graphic_2d/rosen/modules/composer:libcomposer",
"//foundation/graphic/graphic_2d/rosen/modules/composer/native_vsync:libnative_vsync",
"//foundation/graphic/graphic_2d/rosen/modules/2d_graphics:2d_graphics",
@ -85,7 +86,6 @@
"egl_surface.h",
"external_window.h",
"hardware_buffer.h",
"external_buffer.h",
"ibuffer_consumer_listener.h",
"ibuffer_producer.h",
"surface.h",

View File

@ -78,6 +78,9 @@ public:
HardwareBuffer* SurfaceBufferToHardwareBuffer() override;
static GSError CheckBufferConfig(int32_t width, int32_t height,
int32_t format, int32_t usage);
private:
void FreeBufferHandleLocked();

View File

@ -125,11 +125,6 @@ GSError BufferQueue::PopFromDirtyList(sptr<SurfaceBuffer> &buffer)
GSError BufferQueue::CheckRequestConfig(const BufferRequestConfig &config)
{
if (config.width <= 0 || config.height <= 0) {
BLOGN_INVALID("w or h is greater than 0, now is w %{public}d h %{public}d", config.width, config.height);
return GSERROR_INVALID_ARGUMENTS;
}
uint32_t align = config.strideAlignment;
bool isValidStrideAlignment = true;
isValidStrideAlignment = isValidStrideAlignment && (SURFACE_MIN_STRIDE_ALIGNMENT <= align);
@ -145,11 +140,6 @@ GSError BufferQueue::CheckRequestConfig(const BufferRequestConfig &config)
return GSERROR_INVALID_ARGUMENTS;
}
if (config.format < 0 || config.format > PIXEL_FMT_BUTT) {
BLOGN_INVALID("config.format [0, %{public}d], now is %{public}d", PIXEL_FMT_BUTT, config.format);
return GSERROR_INVALID_ARGUMENTS;
}
if (config.colorGamut <= ColorGamut::COLOR_GAMUT_INVALID ||
config.colorGamut > ColorGamut::COLOR_GAMUT_DISPLAY_BT2020 + 1) {
BLOGN_INVALID("config.colorGamut [0, %{public}d], now is %{public}d",

View File

@ -21,11 +21,6 @@
#include "external_window.h"
#include "surface_buffer_impl.h"
#ifndef weak_alias
#define weak_alias(old, new) \
extern __typeof(old) new __attribute__((__weak__, __alias__(#old)))
#endif
using namespace OHOS;
OHHardwareBuffer* HardwareBufferFromSurfaceBuffer(SurfaceBuffer* buffer)
@ -43,7 +38,7 @@ SurfaceBuffer* OHHardwareBufferToSurfaceBuffer(OHHardwareBuffer *buffer)
return SurfaceBuffer::HardwareBufferToSurfaceBuffer(buffer);
}
OHHardwareBuffer* HardwareBufferAlloc(const OHHardwareBufferConfig* config)
OHHardwareBuffer* OH_HardwareBuffer_HardwareBufferAlloc(const OH_HardwareBuffer_Config* config)
{
if (config == nullptr) {
BLOGE("parameter error, please check input parameter");
@ -52,9 +47,9 @@ OHHardwareBuffer* HardwareBufferAlloc(const OHHardwareBufferConfig* config)
BufferRequestConfig bfConfig = {};
bfConfig.width = config->width;
bfConfig.height = config->height;
bfConfig.strideAlignment = config->strideAlignment;
bfConfig.format = config-> format; // PixelFormat
bfConfig.usage = config-> usage;
bfConfig.strideAlignment = 0x8; // set 0x8 as default value to alloc SurfaceBufferImpl
bfConfig.format = config->format; // PixelFormat
bfConfig.usage = config->usage;
bfConfig.timeout = 0;
bfConfig.colorGamut = ColorGamut::COLOR_GAMUT_SRGB;
bfConfig.transform = TransformType::ROTATE_NONE;
@ -64,12 +59,17 @@ OHHardwareBuffer* HardwareBufferAlloc(const OHHardwareBufferConfig* config)
BLOGE("Surface Buffer Alloc failed, %{public}s", GSErrorStr(ret).c_str());
return nullptr;
}
OHHardwareBuffer* buffer = HardwareBufferFromSurfaceBuffer(bufferImpl);
HardwareBufferReference(buffer);
int32_t err = OH_HardwareBuffer_HardwareBufferReference(buffer);
if (err != OHOS::GSERROR_OK) {
BLOGE("HardwareBufferReference failed");
return nullptr;
}
return buffer;
}
int32_t HardwareBufferReference(OHHardwareBuffer *buffer)
int32_t OH_HardwareBuffer_HardwareBufferReference(OHHardwareBuffer *buffer)
{
if (buffer == nullptr) {
BLOGE("parameter error, please check input parameter");
@ -80,7 +80,7 @@ int32_t HardwareBufferReference(OHHardwareBuffer *buffer)
return OHOS::GSERROR_OK;
}
int32_t HardwareBufferUnreference(OHHardwareBuffer *buffer)
int32_t OH_HardwareBuffer_HardwareBufferUnreference(OHHardwareBuffer *buffer)
{
if (buffer == nullptr) {
BLOGE("parameter error, please check input parameter");
@ -91,7 +91,7 @@ int32_t HardwareBufferUnreference(OHHardwareBuffer *buffer)
return OHOS::GSERROR_OK;
}
void GetHardwareBufferConfig(OHHardwareBuffer *buffer, OHHardwareBufferConfig* config)
void OH_HardwareBuffer_GetHardwareBufferConfig(OHHardwareBuffer *buffer, OH_HardwareBuffer_Config* config)
{
if (buffer == nullptr || config == nullptr) {
BLOGE("parameter error, please check input parameter");
@ -101,21 +101,24 @@ void GetHardwareBufferConfig(OHHardwareBuffer *buffer, OHHardwareBufferConfig* c
config->width = sbuffer->GetWidth();
config->height = sbuffer->GetHeight();
config->format = sbuffer->GetFormat();
config->strideAlignment = sbuffer->GetStride();
config->usage = sbuffer->GetUsage();
}
int32_t HardwareBufferMap(OHHardwareBuffer *buffer)
int32_t OH_HardwareBuffer_HardwareBufferMap(OHHardwareBuffer *buffer, void **virAddr)
{
if (buffer == nullptr) {
BLOGE("parameter error, please check input parameter");
return OHOS::GSERROR_INVALID_ARGUMENTS;
}
SurfaceBuffer* sbuffer = OHHardwareBufferToSurfaceBuffer(buffer);
return sbuffer->Map();
int32_t ret = sbuffer->Map();
if (ret == OHOS::GSERROR_OK) {
*virAddr = sbuffer->GetVirAddr();
}
return ret;
}
int32_t HardwareBufferUnmap(OHHardwareBuffer *buffer)
int32_t OH_HardwareBuffer_HardwareBufferUnmap(OHHardwareBuffer *buffer)
{
if (buffer == nullptr) {
BLOGE("parameter error, please check input parameter");
@ -125,7 +128,7 @@ int32_t HardwareBufferUnmap(OHHardwareBuffer *buffer)
return sbuffer->Unmap();
}
uint32_t HardwareBufferGetSeqNum(OHHardwareBuffer *buffer)
uint32_t OH_HardwareBuffer_HardwareBufferGetSeqNum(OHHardwareBuffer *buffer)
{
if (buffer == nullptr) {
BLOGE("parameter error, please check input parameter");
@ -133,12 +136,4 @@ uint32_t HardwareBufferGetSeqNum(OHHardwareBuffer *buffer)
}
const SurfaceBuffer* sbuffer = OHHardwareBufferToSurfaceBuffer(buffer);
return sbuffer->GetSeqNum();
}
weak_alias(HardwareBufferAlloc, OH_HardwareBuffer_HardwareBufferAlloc);
weak_alias(HardwareBufferReference, OH_HardwareBuffer_HardwareBufferReference);
weak_alias(HardwareBufferUnreference, OH_HardwareBuffer_HardwareBufferUnreference);
weak_alias(GetHardwareBufferConfig, OH_HardwareBuffer_GetHardwareBufferConfig);
weak_alias(HardwareBufferMap, OH_HardwareBuffer_HardwareBufferMap);
weak_alias(HardwareBufferUnmap, OH_HardwareBuffer_HardwareBufferUnmap);
weak_alias(HardwareBufferGetSeqNum, OH_HardwareBuffer_HardwareBufferGetSeqNum);
}

View File

@ -113,6 +113,11 @@ GSError SurfaceBufferImpl::Alloc(const BufferRequestConfig &config)
FreeBufferHandleLocked();
}
}
GSError ret = CheckBufferConfig(config.width, config.height, config.format, config.usage);
if (ret != GSERROR_OK) {
return GSERROR_INVALID_ARGUMENTS;
}
BufferHandle *handle = nullptr;
AllocInfo info = {config.width, config.height, config.usage, (PixelFormat)config.format};
@ -470,4 +475,20 @@ void SurfaceBufferImpl::SetEglData(const sptr<EglData>& data)
{
eglData_ = data;
}
GSError SurfaceBufferImpl::CheckBufferConfig(int32_t width, int32_t height,
int32_t format, int32_t usage)
{
if (width <= 0 || height <= 0) {
BLOGE("width or height is greater than 0, now is w %{public}d h %{public}d", width, height);
return GSERROR_INVALID_ARGUMENTS;
}
if (format < 0 || format > PIXEL_FMT_BUTT) {
BLOGE("format [0, %{public}d], now is %{public}d", PIXEL_FMT_BUTT, format);
return GSERROR_INVALID_ARGUMENTS;
}
return GSERROR_OK;
}
} // namespace OHOS

View File

@ -1,152 +0,0 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NDK_INCLUDE_EXTERNAL_HARDWARE_BUFFER_H_
#define NDK_INCLUDE_EXTERNAL_HARDWARE_BUFFER_H_
/**
* @addtogroup HardwareBuffer
* @{
*
* @brief Provides the hardware buffer capability.
*
* @syscap SystemCapability.Graphic.Graphic2D.HardwareBuffer
* @since 8
* @version 2.0
*/
/**
* @file external_buffer.h
*
* @brief Defines the functions for obtaining and using a hardware buffer.
*
* @since 8
* @version 2.0
*/
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
struct HardwareBuffer;
typedef struct HardwareBuffer OHHardwareBuffer;
/**
* @brief <b>HardwareBuffer</b> config. \n
* Used to allocating new <b>HardwareBuffer</b> andquery parameters if existing ones.
*
* @syscap SystemCapability.Graphic.Graphic2D.HardwareBuffer
* @since 8
* @version 2.0
*/
typedef struct {
int32_t width; ///< width in pixels
int32_t height; ///< width in pixels
int32_t strideAlignment; ///< width in pixels
int32_t format; ///< width in pixels
int32_t usage; ///< width in pixels
} OHHardwareBufferConfig;
/**
* @brief Alloc a <b>HardwareBuffer</b> that matches the passed BufferRequestConfig. \n
* A new <b>HardwareBuffer</b> instance is created each time this function is called.
*
* @syscap SystemCapability.Graphic.Graphic2D.HardwareBuffer
* @param config Indicates the pointer to a <b>BufferRequestConfig</b> instance.
* @return Returns the pointer to the <b>HardwareBuffer</b> instance created if the operation is successful, \n
* returns <b>NULL</b> otherwise.
* @since 8
* @version 2.0
*/
OHHardwareBuffer* OH_HardwareBuffer_HardwareBufferAlloc(const OHHardwareBufferConfig* config);
/**
* @brief Adds the reference count of a HardwareBuffer.
*
* @syscap SystemCapability.Graphic.Graphic2D.HardwareBuffer
* @param buffer Indicates the pointer to a <b>HardwareBuffer</b> instance.
* @return Returns an error code defined in <b>GSError</b>.
* @since 8
* @version 2.0
*/
int32_t OH_HardwareBuffer_HardwareBufferReference(OHHardwareBuffer *buffer);
/**
* @brief Decreases the reference count of a HardwareBuffer and, when the reference count reaches 0, \n
* destroys this HardwareBuffer.
*
* @syscap SystemCapability.Graphic.Graphic2D.HardwareBuffer
* @param buffer Indicates the pointer to a <b>HardwareBuffer</b> instance.
* @return Returns an error code defined in <b>GSError</b>.
* @since 8
* @version 2.0
*/
int32_t OH_HardwareBuffer_HardwareBufferUnreference(OHHardwareBuffer *buffer);
/**
* @brief Return a config of the OHHardwareBuffer in the passed OHHardwareBufferConfig struct.
*
* @syscap SystemCapability.Graphic.Graphic2D.HardwareBuffer
* @param buffer Indicates the pointer to a <b>HardwareBuffer</b> instance.
* @param config Indicates the pointer to the <b>HardwareBufferConfig</b> of the buffer.
* @return Returns the pointer to the <b>BufferRequestConfig</b> instance if the operation is successful, \n
* returns <b>NULL</b> otherwise.
* @since 8
* @version 2.0
*/
void OH_HardwareBuffer_GetHardwareBufferConfig(OHHardwareBuffer *buffer, OHHardwareBufferConfig* config);
/**
* @brief Provide direct cpu access to the OHHardwareBuffer in the process's address space.
*
* @syscap SystemCapability.Graphic.Graphic2D.HardwareBuffer
* @param buffer Indicates the pointer to a <b>HardwareBuffer</b> instance.
* @return Returns an error code defined in <b>GSError</b>.
* @since 8
* @version 2.0
*/
int32_t OH_HardwareBuffer_HardwareBufferMap(OHHardwareBuffer *buffer);
/**
* @brief Remove direct cpu access ability of the HardwareBuffer in the process's address space.
*
* @syscap SystemCapability.Graphic.Graphic2D.HardwareBuffer
* @param buffer Indicates the pointer to a <b>HardwareBuffer</b> instance.
* @return Returns an error code defined in <b>GSError</b>.
* @since 8
* @version 2.0
*/
int32_t OH_HardwareBuffer_HardwareBufferUnmap(OHHardwareBuffer *buffer);
/**
* @brief Get the systen wide unique sequence number of the HardwareBuffer.
*
* @syscap SystemCapability.Graphic.Graphic2D.HardwareBuffer
* @param buffer Indicates the pointer to a <b>HardwareBuffer</b> instance.
* @return Returns the sequence number, which is unique for each HardwareBuffer.
* @since 8
* @version 2.0
*/
uint32_t OH_HardwareBuffer_HardwareBufferGetSeqNum(OHHardwareBuffer *buffer);
#ifdef __cplusplus
}
#endif
/** @} */
#endif

View File

@ -16,28 +16,137 @@
#ifndef NDK_INCLUDE_HARDWARE_BUFFER_H_
#define NDK_INCLUDE_HARDWARE_BUFFER_H_
#include "external_buffer.h"
/**
* @addtogroup HardwareBuffer
* @{
*
* @brief Provides the hardware buffer capability.
*
* @syscap SystemCapability.Graphic.Graphic2D.HardwareBuffer
* @since 8
* @version 2.0
*/
/**
* @file external_buffer.h
*
* @brief Defines the functions for obtaining and using a hardware buffer.
*
* @since 8
* @version 2.0
*/
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
OHHardwareBuffer* HardwareBufferAlloc(const OHHardwareBufferConfig* config);
struct HardwareBuffer;
typedef struct HardwareBuffer OHHardwareBuffer;
int32_t HardwareBufferReference(OHHardwareBuffer *buffer);
/**
* @brief <b>HardwareBuffer</b> config. \n
* Used to allocating new <b>HardwareBuffer</b> andquery parameters if existing ones.
*
* @syscap SystemCapability.Graphic.Graphic2D.HardwareBuffer
* @since 8
* @version 2.0
*/
typedef struct {
int32_t width; ///< Width in pixels
int32_t height; ///< Height in pixels
int32_t format; ///< One of PixelFormat
int32_t usage; ///< Combination of buffer usage
} OH_HardwareBuffer_Config;
int32_t HardwareBufferUnreference(OHHardwareBuffer *buffer);
/**
* @brief Alloc a <b>HardwareBuffer</b> that matches the passed BufferRequestConfig. \n
* A new <b>HardwareBuffer</b> instance is created each time this function is called.
*
* @syscap SystemCapability.Graphic.Graphic2D.HardwareBuffer
* @param config Indicates the pointer to a <b>BufferRequestConfig</b> instance.
* @return Returns the pointer to the <b>HardwareBuffer</b> instance created if the operation is successful, \n
* returns <b>NULL</b> otherwise.
* @since 8
* @version 2.0
*/
OHHardwareBuffer* OH_HardwareBuffer_HardwareBufferAlloc(const OH_HardwareBuffer_Config* config);
void GetHardwareBufferConfig(OHHardwareBuffer *buffer, OHHardwareBufferConfig*);
/**
* @brief Adds the reference count of a HardwareBuffer.
*
* @syscap SystemCapability.Graphic.Graphic2D.HardwareBuffer
* @param buffer Indicates the pointer to a <b>HardwareBuffer</b> instance.
* @return Returns an error code defined in <b>GSError</b>.
* @since 8
* @version 2.0
*/
int32_t OH_HardwareBuffer_HardwareBufferReference(OHHardwareBuffer *buffer);
int32_t HardwareBufferMap(OHHardwareBuffer *buffer);
/**
* @brief Decreases the reference count of a HardwareBuffer and, when the reference count reaches 0, \n
* destroys this HardwareBuffer.
*
* @syscap SystemCapability.Graphic.Graphic2D.HardwareBuffer
* @param buffer Indicates the pointer to a <b>HardwareBuffer</b> instance.
* @return Returns an error code defined in <b>GSError</b>.
* @since 8
* @version 2.0
*/
int32_t OH_HardwareBuffer_HardwareBufferUnreference(OHHardwareBuffer *buffer);
int32_t HardwareBufferUnmap(OHHardwareBuffer *buffer);
/**
* @brief Return a config of the OHHardwareBuffer in the passed OHHardwareBufferConfig struct.
*
* @syscap SystemCapability.Graphic.Graphic2D.HardwareBuffer
* @param buffer Indicates the pointer to a <b>HardwareBuffer</b> instance.
* @param config Indicates the pointer to the <b>HardwareBufferConfig</b> of the buffer.
* @return Returns the pointer to the <b>BufferRequestConfig</b> instance if the operation is successful, \n
* returns <b>NULL</b> otherwise.
* @since 8
* @version 2.0
*/
void OH_HardwareBuffer_GetHardwareBufferConfig(OHHardwareBuffer *buffer, OH_HardwareBuffer_Config* config);
uint32_t HardwareBufferGetSeqNum(OHHardwareBuffer *buffer);
/**
* @brief Provide direct cpu access to the OHHardwareBuffer in the process's address space.
*
* @syscap SystemCapability.Graphic.Graphic2D.HardwareBuffer
* @param buffer Indicates the pointer to a <b>HardwareBuffer</b> instance.
* @param virAddr Indicates the address of the <b>HardwareBuffer</b> in virtual memory.
* @return Returns an error code defined in <b>GSError</b>.
* @since 8
* @version 2.0
*/
int32_t OH_HardwareBuffer_HardwareBufferMap(OHHardwareBuffer *buffer, void **virAddr);
/**
* @brief Remove direct cpu access ability of the HardwareBuffer in the process's address space.
*
* @syscap SystemCapability.Graphic.Graphic2D.HardwareBuffer
* @param buffer Indicates the pointer to a <b>HardwareBuffer</b> instance.
* @return Returns an error code defined in <b>GSError</b>.
* @since 8
* @version 2.0
*/
int32_t OH_HardwareBuffer_HardwareBufferUnmap(OHHardwareBuffer *buffer);
/**
* @brief Get the systen wide unique sequence number of the HardwareBuffer.
*
* @syscap SystemCapability.Graphic.Graphic2D.HardwareBuffer
* @param buffer Indicates the pointer to a <b>HardwareBuffer</b> instance.
* @return Returns the sequence number, which is unique for each HardwareBuffer.
* @since 8
* @version 2.0
*/
uint32_t OH_HardwareBuffer_HardwareBufferGetSeqNum(OHHardwareBuffer *buffer);
#ifdef __cplusplus
}
#endif
/** @} */
#endif

View File

@ -0,0 +1,50 @@
# Copyright (c) 2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/ohos.gni")
## Build hello_hardware_buffer {{{
config("hello_hardware_buffer_config") {
visibility = [ ":*" ]
cflags = [
"-Wall",
"-Werror",
"-g3",
]
}
config("hello_hardware_buffer_public_config") {
include_dirs = [
"//foundation/graphic/graphic_2d/interfaces/inner_api/surface",
"//foundation/graphic/graphic_2d/frameworks/surface/include",
]
}
ohos_executable("hello_hardware_buffer") {
install_enable = true
sources = [ "hello_hardware_buffer.cpp" ]
configs = [ ":hello_hardware_buffer_config" ]
public_configs = [ ":hello_hardware_buffer_public_config" ]
deps = [ "//foundation/graphic/graphic_2d/frameworks/surface:surface" ]
part_name = "graphic_standard"
subsystem_name = "graphic"
}
## Build hello_hardware_buffer }}}

View File

@ -0,0 +1,123 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hardware_buffer.h"
#include <cstddef>
#include <iostream>
#include <ostream>
#include <thread>
#include <unistd.h>
#include "surface_buffer_impl.h"
#include "buffer_log.h"
using namespace OHOS;
namespace {
#define LOGI(fmt, ...) ::OHOS::HiviewDFX::HiLog::Info( \
::OHOS::HiviewDFX::HiLogLabel {LOG_CORE, 0, "HelloHardwareBuffer"}, \
"%{public}s: " fmt, __func__, ##__VA_ARGS__)
#define LOGE(fmt, ...) ::OHOS::HiviewDFX::HiLog::Error( \
::OHOS::HiviewDFX::HiLogLabel {LOG_CORE, 0, "HelloHardwareBuffer"}, \
"%{public}s: " fmt, __func__, ##__VA_ARGS__)
constexpr uint32_t HARDWARE_BUFFER_REFERENCE_TWICE = 2;
OH_HardwareBuffer_Config config {
.width = 0x100,
.height = 0x100,
.format = PIXEL_FMT_RGBA_8888,
.usage = HBM_USE_CPU_READ | HBM_USE_CPU_WRITE | HBM_USE_MEM_DMA
};
}
void CompareOHHardwareBufferConfig(OH_HardwareBuffer_Config &config, OH_HardwareBuffer_Config &checkConfig)
{
if (config.width != checkConfig.width) {
LOGE("OHHardwareBufferConfig width different");
}
if (config.height != checkConfig.height) {
LOGE("OHHardwareBufferConfig height different");
}
if (config.format != checkConfig.format) {
LOGE("OHHardwareBufferConfig format different");
}
if (config.usage != checkConfig.usage) {
LOGE("OHHardwareBufferConfig usage different");
}
}
int32_t main(int32_t argc, const char *argv[])
{
LOGI("sample start");
OHHardwareBuffer* buffer = OH_HardwareBuffer_HardwareBufferAlloc(&config);
if (buffer == nullptr) {
LOGE("HardwareBufferAlloc failed");
}
int32_t ret = OH_HardwareBuffer_HardwareBufferReference(buffer);
if (ret != OHOS::GSERROR_OK) {
LOGE("HardwareBufferReference failed");
}
OH_HardwareBuffer_Config checkConfig = {};
OH_HardwareBuffer_GetHardwareBufferConfig(buffer, &checkConfig);
CompareOHHardwareBufferConfig(config, checkConfig);
uint32_t hwBufferID = OH_HardwareBuffer_HardwareBufferGetSeqNum(buffer);
OHOS::SurfaceBuffer *sfBuffer = SurfaceBuffer::HardwareBufferToSurfaceBuffer(buffer);
uint32_t sfBufferID = sfBuffer->GetSeqNum();
if (hwBufferID != sfBufferID) {
LOGE("HardwareBufferGetSeqNum occured error");
}
if (sfBuffer->GetSptrRefCount() != HARDWARE_BUFFER_REFERENCE_TWICE) {
LOGE("HardwareBufferReference occured error");
}
void *virAddr = nullptr;
ret = OH_HardwareBuffer_HardwareBufferMap(buffer, &virAddr);
if (ret != OHOS::GSERROR_OK) {
LOGE("HardwareBufferMap failed");
}
void *sfVirAddr = sfBuffer->GetVirAddr();
if (sfVirAddr != virAddr) {
LOGE("HardwareBufferMap occured error");
}
ret = OH_HardwareBuffer_HardwareBufferUnreference(buffer);
if (ret != OHOS::GSERROR_OK) {
LOGE("HardwareBufferUnreference failed");
}
if (sfBuffer->GetSptrRefCount() != 1) {
LOGE("HardwareBufferUnreference occured error");
}
ret = OH_HardwareBuffer_HardwareBufferUnmap(buffer);
if (ret != OHOS::GSERROR_OK) {
LOGE("HardwareBufferUnmap failed");
}
ret = OH_HardwareBuffer_HardwareBufferUnreference(buffer);
if (ret != OHOS::GSERROR_OK) {
LOGE("HardwareBufferUnreference failed");
}
LOGI("sample end");
return 0;
}