1 /*
2  * Copyright (c) 2012-2017, 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 <cutils/log.h>
34 #include <gralloc_priv.h>
35 #define __STDC_FORMAT_MACROS 1
36 #include <inttypes.h>
37 #include "qdMetaData.h"
38 
setMetaData(private_handle_t * handle,DispParamType paramType,void * param)39 int setMetaData(private_handle_t *handle, DispParamType paramType,
40                                                     void *param) {
41     if (private_handle_t::validate(handle)) {
42         ALOGE("%s: Private handle is invalid! handle=%p", __func__, handle);
43         return -1;
44     }
45     if (handle->fd_metadata == -1) {
46         ALOGE("%s: Bad fd for extra data!", __func__);
47         return -1;
48     }
49     unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
50     void *base = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED,
51         handle->fd_metadata, 0);
52     if (base == reinterpret_cast<void*>(MAP_FAILED)) {
53         ALOGE("%s: mmap() failed: error is %s!", __func__, strerror(errno));
54         return -1;
55     }
56     MetaData_t *data = reinterpret_cast <MetaData_t *>(base);
57     // If parameter is NULL reset the specific MetaData Key
58     if (!param) {
59        data->operation &= ~paramType;
60        return munmap(base, size);
61     }
62 
63     data->operation |= paramType;
64     switch (paramType) {
65         case PP_PARAM_INTERLACED:
66             data->interlaced = *((int32_t *)param);
67             break;
68         case UPDATE_BUFFER_GEOMETRY:
69             data->bufferDim = *((BufferDim_t *)param);
70             break;
71         case UPDATE_REFRESH_RATE:
72             data->refreshrate = *((float *)param);
73             break;
74         case UPDATE_COLOR_SPACE:
75             data->colorSpace = *((ColorSpace_t *)param);
76             break;
77         case MAP_SECURE_BUFFER:
78             data->mapSecureBuffer = *((int32_t *)param);
79             break;
80         case S3D_FORMAT:
81             data->s3dFormat = *((uint32_t *)param);
82             break;
83         case LINEAR_FORMAT:
84             data->linearFormat = *((uint32_t *)param);
85             break;
86         case SET_IGC:
87             data->igc = *((IGC_t *)param);
88             break;
89         case SET_SINGLE_BUFFER_MODE:
90             data->isSingleBufferMode = *((uint32_t *)param);
91             break;
92         case SET_S3D_COMP:
93             data->s3dComp = *((S3DGpuComp_t *)param);
94             break;
95         case SET_VT_TIMESTAMP:
96             data->vtTimeStamp = *((uint64_t *)param);
97             break;
98 #ifdef USE_COLOR_METADATA
99         case COLOR_METADATA:
100             data->color = *((ColorMetaData *)param);
101 #endif
102             break;
103         default:
104             ALOGE("Unknown paramType %d", paramType);
105             break;
106     }
107     if(munmap(base, size))
108         ALOGE("%s: failed to unmap ptr %p, err %d", __func__, (void*)base,
109                                                                         errno);
110     return 0;
111 }
112 
clearMetaData(private_handle_t * handle,DispParamType paramType)113 int clearMetaData(private_handle_t *handle, DispParamType paramType) {
114     if (!handle) {
115         ALOGE("%s: Private handle is null!", __func__);
116         return -1;
117     }
118     if (handle->fd_metadata == -1) {
119         ALOGE("%s: Bad fd for extra data!", __func__);
120         return -1;
121     }
122 
123     unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
124     void *base = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED,
125         handle->fd_metadata, 0);
126     if (base == reinterpret_cast<void*>(MAP_FAILED)) {
127         ALOGE("%s: mmap() failed: error is %s!", __func__, strerror(errno));
128         return -1;
129     }
130     MetaData_t *data = reinterpret_cast <MetaData_t *>(base);
131     data->operation &= ~paramType;
132     switch (paramType) {
133         case SET_S3D_COMP:
134             data->s3dComp.displayId = -1;
135             data->s3dComp.s3dMode = 0;
136             break;
137         default:
138             ALOGE("Unknown paramType %d", paramType);
139             break;
140     }
141     if(munmap(base, size))
142         ALOGE("%s: failed to unmap ptr %p, err %d", __func__, (void*)base,
143                                                                         errno);
144     return 0;
145 }
146 
getMetaData(private_handle_t * handle,DispFetchParamType paramType,void * param)147 int getMetaData(private_handle_t *handle, DispFetchParamType paramType,
148                                                     void *param) {
149     int ret = -1;
150     if (!handle) {
151         ALOGE("%s: Private handle is null!", __func__);
152         return -1;
153     }
154     if (handle->fd_metadata == -1) {
155         ALOGE("%s: Bad fd for extra data!", __func__);
156         return -1;
157     }
158     if (!param) {
159         ALOGE("%s: input param is null!", __func__);
160         return -1;
161     }
162     unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
163     void *base = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED,
164         handle->fd_metadata, 0);
165     if (base == reinterpret_cast<void*>(MAP_FAILED)) {
166         ALOGE("%s: mmap() failed: error is %s!", __func__, strerror(errno));
167         return -1;
168     }
169 
170     MetaData_t *data = reinterpret_cast <MetaData_t *>(base);
171     switch (paramType) {
172         case GET_PP_PARAM_INTERLACED:
173             if (data->operation & PP_PARAM_INTERLACED) {
174                 *((int32_t *)param) = data->interlaced;
175                 ret = 0;
176             }
177             break;
178         case GET_BUFFER_GEOMETRY:
179             if (data->operation & UPDATE_BUFFER_GEOMETRY) {
180                 *((BufferDim_t *)param) = data->bufferDim;
181                 ret = 0;
182             }
183             break;
184         case GET_REFRESH_RATE:
185             if (data->operation & UPDATE_REFRESH_RATE) {
186                 *((float *)param) = data->refreshrate;
187                 ret = 0;
188             }
189             break;
190         case GET_COLOR_SPACE:
191             if (data->operation & UPDATE_COLOR_SPACE) {
192                 *((ColorSpace_t *)param) = data->colorSpace;
193                 ret = 0;
194             }
195             break;
196         case GET_MAP_SECURE_BUFFER:
197             if (data->operation & MAP_SECURE_BUFFER) {
198                 *((int32_t *)param) = data->mapSecureBuffer;
199                 ret = 0;
200             }
201             break;
202         case GET_S3D_FORMAT:
203             if (data->operation & S3D_FORMAT) {
204                 *((uint32_t *)param) = data->s3dFormat;
205                 ret = 0;
206             }
207             break;
208         case GET_LINEAR_FORMAT:
209             if (data->operation & LINEAR_FORMAT) {
210                 *((uint32_t *)param) = data->linearFormat;
211                 ret = 0;
212             }
213             break;
214         case GET_IGC:
215             if (data->operation & SET_IGC) {
216                 *((IGC_t *)param) = data->igc;
217                 ret = 0;
218             }
219             break;
220         case GET_SINGLE_BUFFER_MODE:
221             if (data->operation & SET_SINGLE_BUFFER_MODE) {
222                 *((uint32_t *)param) = data->isSingleBufferMode;
223                 ret = 0;
224             }
225             break;
226         case GET_S3D_COMP:
227             if (data->operation & SET_S3D_COMP) {
228                 *((S3DGpuComp_t *)param) = data->s3dComp;
229                 ret = 0;
230             }
231             break;
232         case GET_VT_TIMESTAMP:
233             if (data->operation & SET_VT_TIMESTAMP) {
234                 *((uint64_t *)param) = data->vtTimeStamp;
235                 ret = 0;
236             }
237             break;
238 #ifdef USE_COLOR_METADATA
239         case GET_COLOR_METADATA:
240             if (data->operation & COLOR_METADATA) {
241                 *((ColorMetaData *)param) = data->color;
242                 ret = 0;
243             }
244 #endif
245             break;
246         default:
247             ALOGE("Unknown paramType %d", paramType);
248             break;
249     }
250     if(munmap(base, size))
251         ALOGE("%s: failed to unmap ptr %p, err %d", __func__, (void*)base,
252                                                                         errno);
253     return ret;
254 }
255 
copyMetaData(struct private_handle_t * src,struct private_handle_t * dst)256 int copyMetaData(struct private_handle_t *src, struct private_handle_t *dst) {
257     if (!src || !dst) {
258         ALOGE("%s: Private handle is null!", __func__);
259         return -1;
260     }
261     if (src->fd_metadata == -1) {
262         ALOGE("%s: Bad fd for src extra data!", __func__);
263         return -1;
264     }
265     if (dst->fd_metadata == -1) {
266         ALOGE("%s: Bad fd for dst extra data!", __func__);
267         return -1;
268     }
269 
270     unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
271 
272     void *base_src = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED,
273         src->fd_metadata, 0);
274     if (base_src == reinterpret_cast<void*>(MAP_FAILED)) {
275         ALOGE("%s: src mmap() failed: error is %s!", __func__, strerror(errno));
276         return -1;
277     }
278 
279     void *base_dst = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED,
280         dst->fd_metadata, 0);
281     if (base_dst == reinterpret_cast<void*>(MAP_FAILED)) {
282         ALOGE("%s: dst mmap() failed: error is %s!", __func__, strerror(errno));
283         if(munmap(base_src, size))
284             ALOGE("%s: failed to unmap src ptr %p, err %d", __func__,
285                                              (void*)base_src, errno);
286         return -1;
287     }
288 
289     memcpy(base_dst, base_src, size);
290 
291     if(munmap(base_src, size))
292         ALOGE("%s: failed to unmap src ptr %p, err %d", __func__, (void*)base_src,
293                                                                         errno);
294     if(munmap(base_dst, size))
295         ALOGE("%s: failed to unmap src ptr %p, err %d", __func__, (void*)base_dst,
296                                                                         errno);
297     return 0;
298 }
299