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