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 #include "hardware_buffer.h"
16 #include "buffer_handle_utils.h"
17 #include "display_common.h"
18 #include "display_type.h"
19 #include "securec.h"
20 namespace OHOS {
CloneHandle(const BufferHandle * handle)21 BufferHandle *CloneHandle(const BufferHandle *handle)
22 {
23 if (handle == nullptr) {
24 DISPLAY_LOGW("%{public}s handle is nullptr", __func__);
25 return nullptr;
26 }
27
28 BufferHandle *newHandle = AllocateBufferHandle(handle->reserveFds, handle->reserveInts);
29 if (newHandle == nullptr) {
30 DISPLAY_LOGW("%{public}s AllocateBufferHandle failed, newHandle is nullptr", __func__);
31 return nullptr;
32 }
33
34 if (handle->fd == -1) {
35 newHandle->fd = handle->fd;
36 } else {
37 newHandle->fd = dup(handle->fd);
38 if (newHandle->fd == -1) {
39 DISPLAY_LOGW("CloneBufferHandle dup failed");
40 FreeBufferHandle(newHandle);
41 return nullptr;
42 }
43 }
44 newHandle->width = handle->width;
45 newHandle->stride = handle->stride;
46 newHandle->height = handle->height;
47 newHandle->size = handle->size;
48 newHandle->format = handle->format;
49 newHandle->usage = handle->usage;
50 newHandle->phyAddr = handle->phyAddr;
51 newHandle->key = handle->key;
52
53 for (uint32_t i = 0; i < newHandle->reserveFds; i++) {
54 newHandle->reserve[i] = dup(handle->reserve[i]);
55 if (newHandle->reserve[i] == -1) {
56 DISPLAY_LOGW("CloneBufferHandle dup reserveFds failed");
57 FreeBufferHandle(newHandle);
58 return nullptr;
59 }
60 }
61 if (newHandle->reserveInts > 0) {
62 if (memcpy_s(&newHandle->reserve[newHandle->reserveFds], sizeof(int32_t) * newHandle->reserveInts,
63 &handle->reserve[handle->reserveFds], sizeof(int32_t) * handle->reserveInts) != EOK) {
64 DISPLAY_LOGW("CloneBufferHandle memcpy_s failed");
65 FreeBufferHandle(newHandle);
66 return nullptr;
67 }
68 }
69 return newHandle;
70 }
71
Create(const BufferHandle & handle)72 OHOS::sptr<OHOS::SurfaceBuffer> HardwareBuffer::Create(const BufferHandle &handle)
73 {
74 OHOS::sptr<OHOS::SurfaceBuffer> surfaceBuffer = new HardwareBuffer(handle);
75 return surfaceBuffer;
76 }
77
GetBufferHandle() const78 BufferHandle *HardwareBuffer::GetBufferHandle() const
79 {
80 DISPLAY_LOGD();
81 return handle_;
82 }
83
HardwareBuffer(const BufferHandle & handle)84 HardwareBuffer::HardwareBuffer(const BufferHandle &handle)
85 {
86 handle_ = CloneHandle(&handle);
87 }
88
~HardwareBuffer()89 HardwareBuffer::~HardwareBuffer()
90 {
91 DISPLAY_LOGD();
92 FreeBufferHandle(handle_);
93 }
94
GetWidth() const95 int32_t HardwareBuffer::GetWidth() const
96 {
97 return 0;
98 }
GetHeight() const99 int32_t HardwareBuffer::GetHeight() const
100 {
101 return 0;
102 }
GetStride() const103 int32_t HardwareBuffer::GetStride() const
104 {
105 return 0;
106 }
107
GetSurfaceBufferWidth() const108 int32_t HardwareBuffer::GetSurfaceBufferWidth() const
109 {
110 return 0;
111 }
GetSurfaceBufferHeight() const112 int32_t HardwareBuffer::GetSurfaceBufferHeight() const
113 {
114 return 0;
115 }
GetSurfaceBufferColorGamut() const116 SurfaceColorGamut HardwareBuffer::GetSurfaceBufferColorGamut() const
117 {
118 return COLOR_GAMUT_INVALID;
119 }
SetSurfaceBufferWidth(int32_t width)120 GSError HardwareBuffer::SetSurfaceBufferWidth(int32_t width)
121 {
122 return GSERROR_OK;
123 }
124
SetSurfaceBufferHeight(int32_t height)125 GSError HardwareBuffer::SetSurfaceBufferHeight(int32_t height)
126 {
127 return GSERROR_OK;
128 }
SetSurfaceBufferColorGamut(SurfaceColorGamut colorGamut)129 GSError HardwareBuffer::SetSurfaceBufferColorGamut(SurfaceColorGamut colorGamut)
130 {
131 return GSERROR_OK;
132 }
133
GetFormat() const134 int32_t HardwareBuffer::GetFormat() const
135 {
136 return 0;
137 }
GetUsage() const138 int64_t HardwareBuffer::GetUsage() const
139 {
140 return 0;
141 }
GetPhyAddr() const142 uint64_t HardwareBuffer::GetPhyAddr() const
143 {
144 return 0;
145 }
GetKey() const146 int32_t HardwareBuffer::GetKey() const
147 {
148 return 0;
149 }
GetVirAddr() const150 void *HardwareBuffer::GetVirAddr() const
151 {
152 return nullptr;
153 }
154
GetFileDescriptor() const155 int HardwareBuffer::GetFileDescriptor() const
156 {
157 return -1;
158 }
GetSize() const159 uint32_t HardwareBuffer::GetSize() const
160 {
161 return 0;
162 }
SetInt32(uint32_t key,int32_t value)163 GSError HardwareBuffer::SetInt32(uint32_t key, int32_t value)
164 {
165 return GSERROR_OK;
166 }
GetInt32(uint32_t key,int32_t & value)167 GSError HardwareBuffer::GetInt32(uint32_t key, int32_t &value)
168 {
169 return GSERROR_OK;
170 }
SetInt64(uint32_t key,int64_t value)171 GSError HardwareBuffer::SetInt64(uint32_t key, int64_t value)
172 {
173 return GSERROR_OK;
174 }
GetInt64(uint32_t key,int64_t & value)175 GSError HardwareBuffer::GetInt64(uint32_t key, int64_t &value)
176 {
177 return GSERROR_OK;
178 }
GetEglData() const179 sptr<EglData> HardwareBuffer::GetEglData() const
180 {
181 return nullptr;
182 }
183
184 // support ipc data
ExtraGet(std::string key,int32_t & value) const185 GSError HardwareBuffer::ExtraGet(std::string key, int32_t &value) const
186 {
187 return GSERROR_OK;
188 }
ExtraGet(std::string key,int64_t & value) const189 GSError HardwareBuffer::ExtraGet(std::string key, int64_t &value) const
190 {
191 return GSERROR_OK;
192 }
ExtraGet(std::string key,double & value) const193 GSError HardwareBuffer::ExtraGet(std::string key, double &value) const
194 {
195 return GSERROR_OK;
196 }
ExtraGet(std::string key,std::string & value) const197 GSError HardwareBuffer::ExtraGet(std::string key, std::string &value) const
198 {
199 return GSERROR_OK;
200 }
ExtraSet(std::string key,int32_t value)201 GSError HardwareBuffer::ExtraSet(std::string key, int32_t value)
202 {
203 return GSERROR_OK;
204 }
ExtraSet(std::string key,int64_t value)205 GSError HardwareBuffer::ExtraSet(std::string key, int64_t value)
206 {
207 return GSERROR_OK;
208 }
ExtraSet(std::string key,double value)209 GSError HardwareBuffer::ExtraSet(std::string key, double value)
210 {
211 return GSERROR_OK;
212 }
ExtraSet(std::string key,std::string value)213 GSError HardwareBuffer::ExtraSet(std::string key, std::string value)
214 {
215 return GSERROR_OK;
216 }
217
SetAcquireFence(int32_t fd)218 void HardwareBuffer::SetAcquireFence(int32_t fd)
219 {
220 acquireFence_ = fd;
221 }
222
GetAcquireFence() const223 int32_t HardwareBuffer::GetAcquireFence() const
224 {
225 return acquireFence_;
226 }
227
SetReleaseFence(int32_t fd)228 void HardwareBuffer::SetReleaseFence(int32_t fd)
229 {
230 relesaeFence_ = fd;
231 }
232
GetReleaseFence() const233 int32_t HardwareBuffer::GetReleaseFence() const
234 {
235 return relesaeFence_;
236 }
237 }