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 #include "buffer_utils.h"
17
18 #include <fcntl.h>
19 #include <unistd.h>
20
21 #include "surface_buffer_impl.h"
22
23 namespace OHOS {
ReadFence(MessageParcel & parcel,int32_t & fence)24 void ReadFence(MessageParcel &parcel, int32_t &fence)
25 {
26 fence = parcel.ReadInt32();
27 if (fence < 0) {
28 return;
29 }
30
31 fence = parcel.ReadFileDescriptor();
32 }
33
WriteFence(MessageParcel & parcel,int32_t fence)34 void WriteFence(MessageParcel &parcel, int32_t fence)
35 {
36 if (fence >= 0 && fcntl(fence, F_GETFL) == -1 && errno == EBADF) {
37 fence = -1;
38 }
39
40 parcel.WriteInt32(fence);
41
42 if (fence < 0) {
43 return;
44 }
45
46 parcel.WriteFileDescriptor(fence);
47 close(fence);
48 }
49
ReadRequestConfig(MessageParcel & parcel,BufferRequestConfig & config)50 void ReadRequestConfig(MessageParcel &parcel, BufferRequestConfig &config)
51 {
52 config.width = parcel.ReadInt32();
53 config.height = parcel.ReadInt32();
54 config.strideAlignment = parcel.ReadInt32();
55 config.format = parcel.ReadInt32();
56 config.usage = parcel.ReadInt32();
57 config.timeout = parcel.ReadInt32();
58 config.colorGamut = static_cast<SurfaceColorGamut>(parcel.ReadInt32());
59 }
60
WriteRequestConfig(MessageParcel & parcel,BufferRequestConfig const & config)61 void WriteRequestConfig(MessageParcel &parcel, BufferRequestConfig const & config)
62 {
63 parcel.WriteInt32(config.width);
64 parcel.WriteInt32(config.height);
65 parcel.WriteInt32(config.strideAlignment);
66 parcel.WriteInt32(config.format);
67 parcel.WriteInt32(config.usage);
68 parcel.WriteInt32(config.timeout);
69 parcel.WriteInt32(static_cast<int32_t>(config.colorGamut));
70 }
71
ReadFlushConfig(MessageParcel & parcel,BufferFlushConfig & config)72 void ReadFlushConfig(MessageParcel &parcel, BufferFlushConfig &config)
73 {
74 config.damage.x = parcel.ReadInt32();
75 config.damage.y = parcel.ReadInt32();
76 config.damage.w = parcel.ReadInt32();
77 config.damage.h = parcel.ReadInt32();
78 config.timestamp = parcel.ReadInt64();
79 }
80
WriteFlushConfig(MessageParcel & parcel,BufferFlushConfig const & config)81 void WriteFlushConfig(MessageParcel &parcel, BufferFlushConfig const & config)
82 {
83 parcel.WriteInt32(config.damage.x);
84 parcel.WriteInt32(config.damage.y);
85 parcel.WriteInt32(config.damage.w);
86 parcel.WriteInt32(config.damage.h);
87 parcel.WriteInt64(config.timestamp);
88 }
89
ReadSurfaceBufferImpl(MessageParcel & parcel,int32_t & sequence,sptr<SurfaceBuffer> & buffer)90 void ReadSurfaceBufferImpl(MessageParcel &parcel,
91 int32_t &sequence, sptr<SurfaceBuffer>& buffer)
92 {
93 sequence = parcel.ReadInt32();
94 if (parcel.ReadBool()) {
95 sptr<SurfaceBufferImpl> bufferImpl = new SurfaceBufferImpl(sequence);
96 auto handle = ReadBufferHandle(parcel);
97 bufferImpl->SetBufferHandle(handle);
98 int32_t size = parcel.ReadInt32();
99 for (int32_t i = 0; i < size; i++) {
100 uint32_t key = parcel.ReadUint32();
101 int32_t type = parcel.ReadInt32();
102 if (type == EXTRA_DATA_TYPE_INT32) {
103 bufferImpl->SetInt32(key, parcel.ReadInt32());
104 }
105 if (type == EXTRA_DATA_TYPE_INT64) {
106 bufferImpl->SetInt64(key, parcel.ReadInt64());
107 }
108 }
109 buffer = bufferImpl;
110 }
111 }
112
WriteSurfaceBufferImpl(MessageParcel & parcel,int32_t sequence,const sptr<SurfaceBuffer> & buffer)113 void WriteSurfaceBufferImpl(MessageParcel &parcel,
114 int32_t sequence, const sptr<SurfaceBuffer> &buffer)
115 {
116 parcel.WriteInt32(sequence);
117 auto bufferImpl = SurfaceBufferImpl::FromBase(buffer);
118 parcel.WriteBool(bufferImpl != nullptr);
119 if (bufferImpl == nullptr) {
120 return;
121 }
122 bufferImpl->WriteToMessageParcel(parcel);
123 }
124 } // namespace OHOS
125