• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "native_buffer.h"
17 
18 #include <cstddef>
19 #include <iostream>
20 #include <ostream>
21 #include <thread>
22 #include <unistd.h>
23 
24 #include "surface_buffer_impl.h"
25 #include "buffer_log.h"
26 
27 using namespace OHOS;
28 
29 namespace {
30 #define LOGI(fmt, ...) ::OHOS::HiviewDFX::HiLog::Info(            \
31     ::OHOS::HiviewDFX::HiLogLabel {LOG_CORE, 0, "HelloNativeBuffer"}, \
32     "%{public}s: " fmt, __func__, ##__VA_ARGS__)
33 
34 #define LOGE(fmt, ...) ::OHOS::HiviewDFX::HiLog::Error(           \
35     ::OHOS::HiviewDFX::HiLogLabel {LOG_CORE, 0, "HelloNativeBuffer"}, \
36     "%{public}s: " fmt, __func__, ##__VA_ARGS__)
37 
38 constexpr uint32_t HARDWARE_BUFFER_REFERENCE_TWICE = 2;
39 
40 OH_NativeBuffer_Config config {
41     .width = 0x100,
42     .height = 0x100,
43     .format = GRAPHIC_PIXEL_FMT_RGBA_8888,
44     .usage = BUFFER_USAGE_CPU_READ | BUFFER_USAGE_CPU_WRITE | BUFFER_USAGE_MEM_DMA
45 };
46 
CompareOH_NativeBufferConfig(const OH_NativeBuffer_Config & config,const OH_NativeBuffer_Config & checkConfig)47 void CompareOH_NativeBufferConfig(const OH_NativeBuffer_Config &config, const OH_NativeBuffer_Config &checkConfig)
48 {
49     if (config.width != checkConfig.width) {
50         LOGE("OH_NativeBufferConfig width different");
51     }
52     if (config.height != checkConfig.height) {
53         LOGE("OH_NativeBufferConfig height different");
54     }
55     if (config.format != checkConfig.format) {
56         LOGE("OH_NativeBufferConfig format different");
57     }
58     if (config.usage != checkConfig.usage) {
59         LOGE("OH_NativeBufferConfig usage different");
60     }
61 }
62 
63 }
64 
main(int32_t argc,const char * argv[])65 int32_t main(int32_t argc, const char *argv[])
66 {
67     LOGI("sample start");
68     OH_NativeBuffer* buffer = OH_NativeBuffer_Alloc(&config);
69     if (buffer == nullptr) {
70         LOGE("NativeBufferAlloc failed");
71     }
72 
73     int32_t ret = OH_NativeBuffer_Reference(buffer);
74     if (ret != OHOS::GSERROR_OK) {
75         LOGE("NativeBufferReference failed");
76     }
77 
78     OH_NativeBuffer_Config checkConfig = {};
79     OH_NativeBuffer_GetConfig(buffer, &checkConfig);
80     CompareOH_NativeBufferConfig(config, checkConfig);
81 
82     uint32_t hwBufferID = OH_NativeBuffer_GetSeqNum(buffer);
83     OHOS::SurfaceBuffer *sfBuffer = SurfaceBuffer::NativeBufferToSurfaceBuffer(buffer);
84     uint32_t sfBufferID = sfBuffer->GetSeqNum();
85     if (hwBufferID != sfBufferID) {
86         LOGE("NativeBufferGetSeqNum occured error");
87     }
88 
89     if (sfBuffer->GetSptrRefCount() != HARDWARE_BUFFER_REFERENCE_TWICE) {
90         LOGE("NativeBufferReference occured error");
91     }
92 
93     void *virAddr = nullptr;
94     ret = OH_NativeBuffer_Map(buffer, &virAddr);
95     if (ret != OHOS::GSERROR_OK) {
96         LOGE("NativeBufferMap failed");
97     }
98 
99     void *sfVirAddr = sfBuffer->GetVirAddr();
100     if (sfVirAddr != virAddr) {
101         LOGE("NativeBufferMap occured error");
102     }
103 
104     ret = OH_NativeBuffer_Unreference(buffer);
105     if (ret != OHOS::GSERROR_OK) {
106         LOGE("NativeBufferUnreference failed");
107     }
108 
109     if (sfBuffer->GetSptrRefCount() != 1) {
110         LOGE("NativeBufferUnreference occured error");
111     }
112 
113     ret = OH_NativeBuffer_Unmap(buffer);
114     if (ret != OHOS::GSERROR_OK) {
115         LOGE("NativeBufferUnmap failed");
116     }
117 
118     ret = OH_NativeBuffer_Unreference(buffer);
119     if (ret != OHOS::GSERROR_OK) {
120         LOGE("NativeBufferUnreference failed");
121     }
122     LOGI("sample end");
123     return 0;
124 }