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 #ifndef FRAMEWORKS_SURFACE_INCLUDE_BUFFER_UTILS_H
17 #define FRAMEWORKS_SURFACE_INCLUDE_BUFFER_UTILS_H
18
19 #include <errno.h>
20 #include <message_parcel.h>
21 #include "surface_type.h"
22 #include <surface_tunnel_handle.h>
23 #include <ibuffer_producer.h>
24 #include "surface_buffer.h"
25
26 namespace OHOS {
ReadFileDescriptor(MessageParcel & parcel,int32_t & fd)27 inline void ReadFileDescriptor(MessageParcel &parcel, int32_t &fd)
28 {
29 fd = parcel.ReadInt32();
30 if (fd < 0) {
31 return;
32 }
33
34 fd = parcel.ReadFileDescriptor();
35 }
36 GSError WriteFileDescriptor(MessageParcel &parcel, int32_t fd);
37
38 bool GetBoolParameter(const std::string &name, const std::string &defaultValue);
39
40 void ReadRequestConfig(MessageParcel &parcel, BufferRequestConfig &config);
WriteRequestConfig(MessageParcel & parcel,BufferRequestConfig const & config)41 static inline GSError WriteRequestConfig(MessageParcel &parcel, BufferRequestConfig const &config)
42 {
43 if (!parcel.WriteInt32(config.width) || !parcel.WriteInt32(config.height) ||
44 !parcel.WriteInt32(config.strideAlignment) || !parcel.WriteInt32(config.format) ||
45 !parcel.WriteUint64(config.usage) || !parcel.WriteInt32(config.timeout) ||
46 !parcel.WriteInt32(static_cast<int32_t>(config.colorGamut)) ||
47 !parcel.WriteInt32(static_cast<int32_t>(config.transform))) {
48 return GSERROR_BINDER;
49 }
50 return GSERROR_OK;
51 }
52
53 GSError ReadFlushConfig(MessageParcel &parcel, BufferFlushConfigWithDamages &config);
54 GSError WriteFlushConfig(MessageParcel &parcel, const BufferFlushConfigWithDamages &config);
55
56 GSError ReadSurfaceBufferImpl(MessageParcel &parcel, uint32_t &sequence, sptr<SurfaceBuffer> &buffer,
57 std::function<int(MessageParcel &parcel, std::function<int(Parcel &)>readFdDefaultFunc)> readSafeFdFunc = nullptr);
58 GSError WriteSurfaceBufferImpl(MessageParcel &parcel, uint32_t sequence, const sptr<SurfaceBuffer> &buffer);
59
60 void ReadVerifyAllocInfo(MessageParcel &parcel, std::vector<BufferVerifyAllocInfo> &infos);
61 GSError WriteVerifyAllocInfo(MessageParcel &parcel, const std::vector<BufferVerifyAllocInfo> &infos);
62
63 GSError ReadHDRMetaData(MessageParcel &parcel, std::vector<GraphicHDRMetaData> &metaData);
64 GSError WriteHDRMetaData(MessageParcel &parcel, const std::vector<GraphicHDRMetaData> &metaData);
65
66 GSError ReadHDRMetaDataSet(MessageParcel &parcel, std::vector<uint8_t> &metaData);
67 GSError WriteHDRMetaDataSet(MessageParcel &parcel, const std::vector<uint8_t> &metaData);
68
69 GSError ReadExtDataHandle(MessageParcel &parcel, sptr<SurfaceTunnelHandle> &handle);
70 GSError WriteExtDataHandle(MessageParcel &parcel, const GraphicExtDataHandle *handle);
71
72 GSError DumpToFileAsync(pid_t pid, std::string name, sptr<SurfaceBuffer> &buffer);
73
ReadSurfaceProperty(MessageParcel & parcel,SurfaceProperty & property)74 static inline GSError ReadSurfaceProperty(MessageParcel &parcel, SurfaceProperty& property)
75 {
76 uint32_t val = parcel.ReadUint32();
77 if (val > GraphicTransformType::GRAPHIC_ROTATE_BUTT) {
78 return GSERROR_BINDER;
79 }
80 property.transformHint = static_cast<GraphicTransformType>(val);
81 return GSERROR_OK;
82 }
83
WriteSurfaceProperty(MessageParcel & parcel,const SurfaceProperty & property)84 static inline GSError WriteSurfaceProperty(MessageParcel &parcel, const SurfaceProperty& property)
85 {
86 uint32_t tmp = static_cast<uint32_t>(property.transformHint);
87 if (!parcel.WriteUint32(tmp)) {
88 return GSERROR_BINDER;
89 }
90 return GSERROR_OK;
91 }
92
BufferUtilRegisterPropertyListener(sptr<IProducerListener> listener,uint64_t producerId,std::map<uint64_t,sptr<IProducerListener>> & propertyChangeListeners)93 static inline GSError BufferUtilRegisterPropertyListener(sptr<IProducerListener> listener,
94 uint64_t producerId, std::map<uint64_t, sptr<IProducerListener>>& propertyChangeListeners)
95 {
96 const size_t propertyChangeListenerMaxNum = 50; // 50 : limit producer num
97 if (propertyChangeListeners.size() > propertyChangeListenerMaxNum) {
98 return GSERROR_API_FAILED;
99 }
100
101 if (propertyChangeListeners.find(producerId) == propertyChangeListeners.end()) {
102 propertyChangeListeners[producerId] = listener;
103 }
104 return GSERROR_OK;
105 }
106
BufferUtilUnRegisterPropertyListener(uint64_t producerId,std::map<uint64_t,sptr<IProducerListener>> & propertyChangeListeners)107 static inline GSError BufferUtilUnRegisterPropertyListener(uint64_t producerId,
108 std::map<uint64_t, sptr<IProducerListener>>& propertyChangeListeners)
109 {
110 propertyChangeListeners.erase(producerId);
111 return GSERROR_OK;
112 }
113
isBufferUtilPresentTimestampReady(int64_t desiredPresentTimestamp,int64_t expectPresentTimestamp)114 static inline bool isBufferUtilPresentTimestampReady(int64_t desiredPresentTimestamp,
115 int64_t expectPresentTimestamp)
116 {
117 if (desiredPresentTimestamp <= expectPresentTimestamp) {
118 return true;
119 }
120 int32_t oneSecondTimestamp = 1e9;
121 if (desiredPresentTimestamp - oneSecondTimestamp > expectPresentTimestamp) {
122 return true;
123 }
124 return false;
125 }
126
BufferUtilGetCycleBuffersNumber(uint32_t & cycleBuffersNumber,uint32_t rotatingBufferNumber,uint32_t bufferQueueSize)127 static inline GSError BufferUtilGetCycleBuffersNumber(uint32_t& cycleBuffersNumber,
128 uint32_t rotatingBufferNumber, uint32_t bufferQueueSize)
129 {
130 if (rotatingBufferNumber == 0) {
131 cycleBuffersNumber = bufferQueueSize;
132 } else {
133 cycleBuffersNumber = rotatingBufferNumber;
134 }
135 return GSERROR_OK;
136 }
137 } // namespace OHOS
138
139 #endif // FRAMEWORKS_SURFACE_INCLUDE_BUFFER_UTILS_H
140