• 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 "external_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 }
106 
OH_NativeBuffer_Map(OH_NativeBuffer * buffer,void ** virAddr)107 int32_t OH_NativeBuffer_Map(OH_NativeBuffer *buffer, void **virAddr)
108 {
109     if (buffer == nullptr) {
110         BLOGE("parameter error, please check input parameter");
111         return OHOS::GSERROR_INVALID_ARGUMENTS;
112     }
113     SurfaceBuffer* sbuffer = OH_NativeBufferToSurfaceBuffer(buffer);
114     int32_t ret = sbuffer->Map();
115     if (ret == OHOS::GSERROR_OK) {
116         *virAddr = sbuffer->GetVirAddr();
117     }
118     return ret;
119 }
120 
OH_NativeBuffer_Unmap(OH_NativeBuffer * buffer)121 int32_t OH_NativeBuffer_Unmap(OH_NativeBuffer *buffer)
122 {
123     if (buffer == nullptr) {
124         BLOGE("parameter error, please check input parameter");
125         return OHOS::GSERROR_INVALID_ARGUMENTS;
126     }
127     SurfaceBuffer* sbuffer = OH_NativeBufferToSurfaceBuffer(buffer);
128     return sbuffer->Unmap();
129 }
130 
OH_NativeBuffer_GetSeqNum(OH_NativeBuffer * buffer)131 uint32_t OH_NativeBuffer_GetSeqNum(OH_NativeBuffer *buffer)
132 {
133     if (buffer == nullptr) {
134         BLOGE("parameter error, please check input parameter");
135         return OHOS::GSERROR_INVALID_ARGUMENTS;
136     }
137     const SurfaceBuffer* sbuffer = OH_NativeBufferToSurfaceBuffer(buffer);
138     return sbuffer->GetSeqNum();
139 }
140 
OH_NativeBuffer_GetBufferHandle(const OH_NativeBuffer * buffer)141 const BufferHandle* OH_NativeBuffer_GetBufferHandle(const OH_NativeBuffer *buffer)
142 {
143     if (buffer == nullptr) {
144         BLOGE("parameter error, please check input parameter");
145         return nullptr;
146     }
147     const SurfaceBuffer* sbuffer = OH_NativeBufferToSurfaceBuffer(buffer);
148     return sbuffer->GetBufferHandle();
149 }
150 
OH_NativeBuffer_GetNativeBufferConfig(const OH_NativeBuffer * buffer,OH_NativeBuffer_Config * config)151 void OH_NativeBuffer_GetNativeBufferConfig(const OH_NativeBuffer *buffer, OH_NativeBuffer_Config* config)
152 {
153     if (buffer == nullptr || config == nullptr) {
154         BLOGE("parameter error, please check input parameter");
155         return;
156     }
157     const SurfaceBuffer* sbuffer = OH_NativeBufferToSurfaceBuffer(buffer);
158     config->width = sbuffer->GetWidth();
159     config->height = sbuffer->GetHeight();
160     config->format = sbuffer->GetFormat();
161     config->usage = sbuffer->GetUsage();
162 }
163 
OH_NativeBufferFromNativeWindowBuffer(OHNativeWindowBuffer * nativeWindowBuffer)164 OH_NativeBuffer* OH_NativeBufferFromNativeWindowBuffer(OHNativeWindowBuffer* nativeWindowBuffer)
165 {
166     if (nativeWindowBuffer == nullptr) {
167         BLOGE("parameter error, please check input parameter");
168         return nullptr;
169     }
170     OH_NativeBuffer* buffer = OH_NativeBufferFromSurfaceBuffer(nativeWindowBuffer->sfbuffer);
171     return buffer;
172 }
173