• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_inner.h"
17 
18 #include <cinttypes>
19 #include "surface_type.h"
20 #include "buffer_log.h"
21 #include "native_window.h"
22 #include "surface_buffer_impl.h"
23 
24 using namespace OHOS;
25 
OH_NativeBufferFromSurfaceBuffer(SurfaceBuffer * buffer)26 static OH_NativeBuffer* OH_NativeBufferFromSurfaceBuffer(SurfaceBuffer* buffer)
27 {
28     return buffer->SurfaceBufferToNativeBuffer();
29 }
30 
OH_NativeBufferToSurfaceBuffer(OH_NativeBuffer * buffer)31 static SurfaceBuffer* OH_NativeBufferToSurfaceBuffer(OH_NativeBuffer *buffer)
32 {
33     return SurfaceBuffer::NativeBufferToSurfaceBuffer(buffer);
34 }
35 
OH_NativeBufferToSurfaceBuffer(const OH_NativeBuffer * buffer)36 static const SurfaceBuffer* OH_NativeBufferToSurfaceBuffer(const OH_NativeBuffer *buffer)
37 {
38     return SurfaceBuffer::NativeBufferToSurfaceBuffer(buffer);
39 }
40 
OH_NativeBuffer_Alloc(const OH_NativeBuffer_Config * config)41 OH_NativeBuffer* OH_NativeBuffer_Alloc(const OH_NativeBuffer_Config* config)
42 {
43     if (config == nullptr) {
44         BLOGE("parameter error, please check input parameter");
45         return nullptr;
46     }
47     BufferRequestConfig bfConfig = {};
48     bfConfig.width = config->width;
49     bfConfig.height = config->height;
50     bfConfig.strideAlignment = 0x8; // set 0x8 as default value to alloc SurfaceBufferImpl
51     bfConfig.format = config->format; // PixelFormat
52     bfConfig.usage = config->usage;
53     bfConfig.timeout = 0;
54     bfConfig.colorGamut = GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB;
55     bfConfig.transform = GraphicTransformType::GRAPHIC_ROTATE_NONE;
56     sptr<SurfaceBuffer> bufferImpl = new SurfaceBufferImpl();
57     GSError ret = bufferImpl->Alloc(bfConfig);
58     if (ret != GSERROR_OK) {
59         BLOGE("Surface Buffer Alloc failed, %{public}s", GSErrorStr(ret).c_str());
60         return nullptr;
61     }
62 
63     OH_NativeBuffer* buffer = OH_NativeBufferFromSurfaceBuffer(bufferImpl);
64     int32_t err = OH_NativeBuffer_Reference(buffer);
65     if (err != OHOS::GSERROR_OK) {
66         BLOGE("NativeBufferReference failed");
67         return nullptr;
68     }
69     return buffer;
70 }
71 
OH_NativeBuffer_Reference(OH_NativeBuffer * buffer)72 int32_t OH_NativeBuffer_Reference(OH_NativeBuffer *buffer)
73 {
74     if (buffer == nullptr) {
75         BLOGE("parameter error, please check input parameter");
76         return OHOS::GSERROR_INVALID_ARGUMENTS;
77     }
78     OHOS::RefBase *ref = reinterpret_cast<OHOS::RefBase *>(buffer);
79     ref->IncStrongRef(ref);
80     return OHOS::GSERROR_OK;
81 }
82 
OH_NativeBuffer_Unreference(OH_NativeBuffer * buffer)83 int32_t OH_NativeBuffer_Unreference(OH_NativeBuffer *buffer)
84 {
85     if (buffer == nullptr) {
86         BLOGE("parameter error, please check input parameter");
87         return OHOS::GSERROR_INVALID_ARGUMENTS;
88     }
89     OHOS::RefBase *ref = reinterpret_cast<OHOS::RefBase *>(buffer);
90     ref->DecStrongRef(ref);
91     return OHOS::GSERROR_OK;
92 }
93 
OH_NativeBuffer_GetConfig(OH_NativeBuffer * buffer,OH_NativeBuffer_Config * config)94 void OH_NativeBuffer_GetConfig(OH_NativeBuffer *buffer, OH_NativeBuffer_Config* config)
95 {
96     if (buffer == nullptr || config == nullptr) {
97         BLOGE("parameter error, please check input parameter");
98         return;
99     }
100     const SurfaceBuffer* sbuffer = OH_NativeBufferToSurfaceBuffer(buffer);
101     config->width = sbuffer->GetWidth();
102     config->height = sbuffer->GetHeight();
103     config->format = sbuffer->GetFormat();
104     config->usage = sbuffer->GetUsage();
105     config->stride = sbuffer->GetStride();
106 }
107 
OH_NativeBuffer_Map(OH_NativeBuffer * buffer,void ** virAddr)108 int32_t OH_NativeBuffer_Map(OH_NativeBuffer *buffer, void **virAddr)
109 {
110     if (buffer == nullptr) {
111         BLOGE("parameter error, please check input parameter");
112         return OHOS::GSERROR_INVALID_ARGUMENTS;
113     }
114     SurfaceBuffer* sbuffer = OH_NativeBufferToSurfaceBuffer(buffer);
115     int32_t ret = sbuffer->Map();
116     if (ret == OHOS::GSERROR_OK) {
117         *virAddr = sbuffer->GetVirAddr();
118     }
119     return ret;
120 }
121 
OH_NativeBuffer_Unmap(OH_NativeBuffer * buffer)122 int32_t OH_NativeBuffer_Unmap(OH_NativeBuffer *buffer)
123 {
124     if (buffer == nullptr) {
125         BLOGE("parameter error, please check input parameter");
126         return OHOS::GSERROR_INVALID_ARGUMENTS;
127     }
128     SurfaceBuffer* sbuffer = OH_NativeBufferToSurfaceBuffer(buffer);
129     return sbuffer->Unmap();
130 }
131 
OH_NativeBuffer_GetSeqNum(OH_NativeBuffer * buffer)132 uint32_t OH_NativeBuffer_GetSeqNum(OH_NativeBuffer *buffer)
133 {
134     if (buffer == nullptr) {
135         BLOGE("parameter error, please check input parameter");
136         return OHOS::GSERROR_INVALID_ARGUMENTS;
137     }
138     const SurfaceBuffer* sbuffer = OH_NativeBufferToSurfaceBuffer(buffer);
139     return sbuffer->GetSeqNum();
140 }
141 
OH_NativeBuffer_GetBufferHandle(const OH_NativeBuffer * buffer)142 const BufferHandle* OH_NativeBuffer_GetBufferHandle(const OH_NativeBuffer *buffer)
143 {
144     if (buffer == nullptr) {
145         BLOGE("parameter error, please check input parameter");
146         return nullptr;
147     }
148     const SurfaceBuffer* sbuffer = OH_NativeBufferToSurfaceBuffer(buffer);
149     return sbuffer->GetBufferHandle();
150 }
151 
OH_NativeBuffer_GetNativeBufferConfig(const OH_NativeBuffer * buffer,OH_NativeBuffer_Config * config)152 void OH_NativeBuffer_GetNativeBufferConfig(const OH_NativeBuffer *buffer, OH_NativeBuffer_Config* config)
153 {
154     if (buffer == nullptr || config == nullptr) {
155         BLOGE("parameter error, please check input parameter");
156         return;
157     }
158     const SurfaceBuffer* sbuffer = OH_NativeBufferToSurfaceBuffer(buffer);
159     config->width = sbuffer->GetWidth();
160     config->height = sbuffer->GetHeight();
161     config->format = sbuffer->GetFormat();
162     config->usage = sbuffer->GetUsage();
163     config->stride = sbuffer->GetStride();
164 }
165 
OH_NativeBufferFromNativeWindowBuffer(OHNativeWindowBuffer * nativeWindowBuffer)166 OH_NativeBuffer* OH_NativeBufferFromNativeWindowBuffer(OHNativeWindowBuffer* nativeWindowBuffer)
167 {
168     if (nativeWindowBuffer == nullptr) {
169         BLOGE("parameter error, please check input parameter");
170         return nullptr;
171     }
172     OH_NativeBuffer* buffer = OH_NativeBufferFromSurfaceBuffer(nativeWindowBuffer->sfbuffer);
173     return buffer;
174 }
175