1 /*
2 * Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <errno.h>
31 #include <string.h>
32 #include <sys/mman.h>
33 #include <log/log.h>
34 #include <cinttypes>
35 #include <gralloc_priv.h>
36 #include "qdMetaData.h"
37
getMetaDataSize()38 unsigned long getMetaDataSize() {
39 return static_cast<unsigned long>(ROUND_UP_PAGESIZE(sizeof(MetaData_t)));
40 }
41
validateAndMap(private_handle_t * handle)42 static int validateAndMap(private_handle_t* handle) {
43 if (private_handle_t::validate(handle)) {
44 ALOGE("%s: Private handle is invalid - handle:%p", __func__, handle);
45 return -1;
46 }
47 if (handle->fd_metadata == -1) {
48 ALOGE("%s: Invalid metadata fd - handle:%p fd: %d",
49 __func__, handle, handle->fd_metadata);
50 return -1;
51 }
52
53 if (!handle->base_metadata) {
54 auto size = getMetaDataSize();
55 void *base = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED,
56 handle->fd_metadata, 0);
57 if (base == reinterpret_cast<void*>(MAP_FAILED)) {
58 ALOGE("%s: metadata mmap failed - handle:%p fd: %d err: %s",
59 __func__, handle, handle->fd_metadata, strerror(errno));
60
61 return -1;
62 }
63 handle->base_metadata = (uintptr_t) base;
64 }
65 return 0;
66 }
67
setMetaData(private_handle_t * handle,DispParamType paramType,void * param)68 int setMetaData(private_handle_t *handle, DispParamType paramType,
69 void *param) {
70 auto err = validateAndMap(handle);
71 if (err != 0)
72 return err;
73 return setMetaDataVa(reinterpret_cast<MetaData_t*>(handle->base_metadata),
74 paramType, param);
75 }
76
setMetaDataVa(MetaData_t * data,DispParamType paramType,void * param)77 int setMetaDataVa(MetaData_t *data, DispParamType paramType,
78 void *param) {
79 if (data == nullptr)
80 return -EINVAL;
81 // If parameter is NULL reset the specific MetaData Key
82 if (!param) {
83 data->operation &= ~paramType;
84 // param unset
85 return 0;
86 }
87
88 data->operation |= paramType;
89 switch (paramType) {
90 case PP_PARAM_INTERLACED:
91 data->interlaced = *((int32_t *)param);
92 break;
93 case UPDATE_BUFFER_GEOMETRY:
94 data->bufferDim = *((BufferDim_t *)param);
95 break;
96 case UPDATE_REFRESH_RATE:
97 data->refreshrate = *((float *)param);
98 break;
99 case UPDATE_COLOR_SPACE:
100 data->colorSpace = *((ColorSpace_t *)param);
101 break;
102 case MAP_SECURE_BUFFER:
103 data->mapSecureBuffer = *((int32_t *)param);
104 break;
105 case S3D_FORMAT:
106 data->s3dFormat = *((uint32_t *)param);
107 break;
108 case LINEAR_FORMAT:
109 data->linearFormat = *((uint32_t *)param);
110 break;
111 case SET_IGC:
112 data->igc = *((IGC_t *)param);
113 break;
114 case SET_SINGLE_BUFFER_MODE:
115 data->isSingleBufferMode = *((uint32_t *)param);
116 break;
117 case SET_S3D_COMP:
118 data->s3dComp = *((S3DGpuComp_t *)param);
119 break;
120 case SET_VT_TIMESTAMP:
121 data->vtTimeStamp = *((uint64_t *)param);
122 break;
123 case COLOR_METADATA:
124 data->color = *((ColorMetaData *)param);
125 break;
126 default:
127 ALOGE("Unknown paramType %d", paramType);
128 break;
129 }
130 return 0;
131 }
132
clearMetaData(private_handle_t * handle,DispParamType paramType)133 int clearMetaData(private_handle_t *handle, DispParamType paramType) {
134 auto err = validateAndMap(handle);
135 if (err != 0)
136 return err;
137 return clearMetaDataVa(reinterpret_cast<MetaData_t *>(handle->base_metadata),
138 paramType);
139 }
140
clearMetaDataVa(MetaData_t * data,DispParamType paramType)141 int clearMetaDataVa(MetaData_t *data, DispParamType paramType) {
142 if (data == nullptr)
143 return -EINVAL;
144 data->operation &= ~paramType;
145 switch (paramType) {
146 case SET_S3D_COMP:
147 data->s3dComp.displayId = -1;
148 data->s3dComp.s3dMode = 0;
149 break;
150 default:
151 ALOGE("Unknown paramType %d", paramType);
152 break;
153 }
154 return 0;
155 }
156
getMetaData(private_handle_t * handle,DispFetchParamType paramType,void * param)157 int getMetaData(private_handle_t *handle, DispFetchParamType paramType,
158 void *param) {
159 int ret = validateAndMap(handle);
160 if (ret != 0)
161 return ret;
162 return getMetaDataVa(reinterpret_cast<MetaData_t *>(handle->base_metadata),
163 paramType, param);
164 }
165
getMetaDataVa(MetaData_t * data,DispFetchParamType paramType,void * param)166 int getMetaDataVa(MetaData_t *data, DispFetchParamType paramType,
167 void *param) {
168 // Make sure we send 0 only if the operation queried is present
169 int ret = -EINVAL;
170 if (data == nullptr)
171 return ret;
172
173 switch (paramType) {
174 case GET_PP_PARAM_INTERLACED:
175 if (data->operation & PP_PARAM_INTERLACED) {
176 *((int32_t *)param) = data->interlaced;
177 ret = 0;
178 }
179 break;
180 case GET_BUFFER_GEOMETRY:
181 if (data->operation & UPDATE_BUFFER_GEOMETRY) {
182 *((BufferDim_t *)param) = data->bufferDim;
183 ret = 0;
184 }
185 break;
186 case GET_REFRESH_RATE:
187 if (data->operation & UPDATE_REFRESH_RATE) {
188 *((float *)param) = data->refreshrate;
189 ret = 0;
190 }
191 break;
192 case GET_COLOR_SPACE:
193 if (data->operation & UPDATE_COLOR_SPACE) {
194 *((ColorSpace_t *)param) = data->colorSpace;
195 ret = 0;
196 }
197 break;
198 case GET_MAP_SECURE_BUFFER:
199 if (data->operation & MAP_SECURE_BUFFER) {
200 *((int32_t *)param) = data->mapSecureBuffer;
201 ret = 0;
202 }
203 break;
204 case GET_S3D_FORMAT:
205 if (data->operation & S3D_FORMAT) {
206 *((uint32_t *)param) = data->s3dFormat;
207 ret = 0;
208 }
209 break;
210 case GET_LINEAR_FORMAT:
211 if (data->operation & LINEAR_FORMAT) {
212 *((uint32_t *)param) = data->linearFormat;
213 ret = 0;
214 }
215 break;
216 case GET_IGC:
217 if (data->operation & SET_IGC) {
218 *((IGC_t *)param) = data->igc;
219 ret = 0;
220 }
221 break;
222 case GET_SINGLE_BUFFER_MODE:
223 if (data->operation & SET_SINGLE_BUFFER_MODE) {
224 *((uint32_t *)param) = data->isSingleBufferMode;
225 ret = 0;
226 }
227 break;
228 case GET_S3D_COMP:
229 if (data->operation & SET_S3D_COMP) {
230 *((S3DGpuComp_t *)param) = data->s3dComp;
231 ret = 0;
232 }
233 break;
234 case GET_VT_TIMESTAMP:
235 if (data->operation & SET_VT_TIMESTAMP) {
236 *((uint64_t *)param) = data->vtTimeStamp;
237 ret = 0;
238 }
239 break;
240 case GET_COLOR_METADATA:
241 if (data->operation & COLOR_METADATA) {
242 *((ColorMetaData *)param) = data->color;
243 ret = 0;
244 }
245 break;
246 default:
247 ALOGE("Unknown paramType %d", paramType);
248 break;
249 }
250 return ret;
251 }
252
copyMetaData(struct private_handle_t * src,struct private_handle_t * dst)253 int copyMetaData(struct private_handle_t *src, struct private_handle_t *dst) {
254 auto err = validateAndMap(src);
255 if (err != 0)
256 return err;
257
258 err = validateAndMap(dst);
259 if (err != 0)
260 return err;
261
262 MetaData_t *src_data = reinterpret_cast <MetaData_t *>(src->base_metadata);
263 MetaData_t *dst_data = reinterpret_cast <MetaData_t *>(dst->base_metadata);
264 *dst_data = *src_data;
265 return 0;
266 }
267
copyMetaDataVaToHandle(MetaData_t * src_data,struct private_handle_t * dst)268 int copyMetaDataVaToHandle(MetaData_t *src_data, struct private_handle_t *dst) {
269 int err = -EINVAL;
270 if (src_data == nullptr)
271 return err;
272
273 err = validateAndMap(dst);
274 if (err != 0)
275 return err;
276
277 MetaData_t *dst_data = reinterpret_cast <MetaData_t *>(dst->base_metadata);
278 *dst_data = *src_data;
279 return 0;
280 }
281
copyMetaDataHandleToVa(struct private_handle_t * src,MetaData_t * dst_data)282 int copyMetaDataHandleToVa(struct private_handle_t *src, MetaData_t *dst_data) {
283 int err = -EINVAL;
284 if (dst_data == nullptr)
285 return err;
286
287 err = validateAndMap(src);
288 if (err != 0)
289 return err;
290
291 MetaData_t *src_data = reinterpret_cast <MetaData_t *>(src->base_metadata);
292 *dst_data = *src_data;
293 return 0;
294 }
295
copyMetaDataVaToVa(MetaData_t * src_data,MetaData_t * dst_data)296 int copyMetaDataVaToVa(MetaData_t *src_data, MetaData_t *dst_data) {
297 int err = -EINVAL;
298 if (src_data == nullptr)
299 return err;
300
301 if (dst_data == nullptr)
302 return err;
303
304 *dst_data = *src_data;
305 return 0;
306 }
307
308