blob: cd7727ff00bdb8741c0717762576f088b5b38506 [file] [log] [blame]
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -08001/*
Arun Kumar K.R3e45ac42017-03-08 19:00:02 -08002 * Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -08003 *
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
Ray Zhang566adac2014-04-14 15:32:04 +080030#include <errno.h>
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080031#include <string.h>
32#include <sys/mman.h>
33#include <cutils/log.h>
Naseer Ahmed08c3e402017-03-16 15:51:54 -040034#include <cinttypes>
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080035#include <gralloc_priv.h>
36#include "qdMetaData.h"
37
38int setMetaData(private_handle_t *handle, DispParamType paramType,
39 void *param) {
Prabhanjan Kandula1ef8e6e2016-08-19 11:46:47 +053040 if (private_handle_t::validate(handle)) {
41 ALOGE("%s: Private handle is invalid! handle=%p", __func__, handle);
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080042 return -1;
43 }
44 if (handle->fd_metadata == -1) {
45 ALOGE("%s: Bad fd for extra data!", __func__);
46 return -1;
47 }
48 unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
49 void *base = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED,
50 handle->fd_metadata, 0);
Ray Zhang566adac2014-04-14 15:32:04 +080051 if (base == reinterpret_cast<void*>(MAP_FAILED)) {
52 ALOGE("%s: mmap() failed: error is %s!", __func__, strerror(errno));
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080053 return -1;
54 }
55 MetaData_t *data = reinterpret_cast <MetaData_t *>(base);
Prabhanjan Kandula1ef8e6e2016-08-19 11:46:47 +053056 // If parameter is NULL reset the specific MetaData Key
57 if (!param) {
58 data->operation &= ~paramType;
59 return munmap(base, size);
60 }
61
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080062 data->operation |= paramType;
63 switch (paramType) {
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080064 case PP_PARAM_INTERLACED:
65 data->interlaced = *((int32_t *)param);
66 break;
Ramkumar Radhakrishnan92f3abe2013-06-05 13:52:40 -070067 case UPDATE_BUFFER_GEOMETRY:
Dileep Marchya2295fdd2015-08-12 17:04:14 -070068 data->bufferDim = *((BufferDim_t *)param);
Arun Kumar K.Ra727a882014-08-20 17:14:26 -070069 break;
70 case UPDATE_REFRESH_RATE:
feifanz9cc23032016-03-23 18:48:56 +080071 data->refreshrate = *((float *)param);
Arun Kumar K.Ra727a882014-08-20 17:14:26 -070072 break;
Shuzhen Wang0d113482014-01-28 16:10:22 -080073 case UPDATE_COLOR_SPACE:
74 data->colorSpace = *((ColorSpace_t *)param);
75 break;
Arun Kumar K.Rda2f69b2014-09-30 15:45:37 -070076 case MAP_SECURE_BUFFER:
77 data->mapSecureBuffer = *((int32_t *)param);
78 break;
Naseer Ahmed3324ff72015-01-21 17:14:44 -050079 case S3D_FORMAT:
80 data->s3dFormat = *((uint32_t *)param);
81 break;
Sushil Chauhane7acc3c2015-06-23 16:22:30 -070082 case LINEAR_FORMAT:
83 data->linearFormat = *((uint32_t *)param);
84 break;
Dileep Marchya1a7e1f12015-09-25 19:11:57 -070085 case SET_IGC:
86 data->igc = *((IGC_t *)param);
87 break;
Saurabh Shah95f83682015-10-16 10:30:04 -070088 case SET_SINGLE_BUFFER_MODE:
Saurabh Shahb8067a42015-11-06 16:52:02 -080089 data->isSingleBufferMode = *((uint32_t *)param);
Saurabh Shah95f83682015-10-16 10:30:04 -070090 break;
Ray Zhang8ace84d2016-08-08 19:36:03 +080091 case SET_S3D_COMP:
92 data->s3dComp = *((S3DGpuComp_t *)param);
93 break;
Naseer Ahmede8f254d2016-08-02 20:06:40 -040094 case SET_VT_TIMESTAMP:
95 data->vtTimeStamp = *((uint64_t *)param);
96 break;
Arun Kumar K.Rb2771bf2016-10-03 21:38:23 -070097#ifdef USE_COLOR_METADATA
98 case COLOR_METADATA:
99 data->color = *((ColorMetaData *)param);
100#endif
101 break;
Ray Zhang8ace84d2016-08-08 19:36:03 +0800102 default:
103 ALOGE("Unknown paramType %d", paramType);
104 break;
105 }
106 if(munmap(base, size))
107 ALOGE("%s: failed to unmap ptr %p, err %d", __func__, (void*)base,
108 errno);
109 return 0;
110}
111
112int clearMetaData(private_handle_t *handle, DispParamType paramType) {
113 if (!handle) {
114 ALOGE("%s: Private handle is null!", __func__);
115 return -1;
116 }
117 if (handle->fd_metadata == -1) {
118 ALOGE("%s: Bad fd for extra data!", __func__);
119 return -1;
120 }
121
122 unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
123 void *base = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED,
124 handle->fd_metadata, 0);
125 if (base == reinterpret_cast<void*>(MAP_FAILED)) {
126 ALOGE("%s: mmap() failed: error is %s!", __func__, strerror(errno));
127 return -1;
128 }
129 MetaData_t *data = reinterpret_cast <MetaData_t *>(base);
130 data->operation &= ~paramType;
131 switch (paramType) {
132 case SET_S3D_COMP:
133 data->s3dComp.displayId = -1;
134 data->s3dComp.s3dMode = 0;
135 break;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800136 default:
137 ALOGE("Unknown paramType %d", paramType);
138 break;
139 }
140 if(munmap(base, size))
Dileep Kumar Reddibf333c72014-02-25 14:32:51 +0530141 ALOGE("%s: failed to unmap ptr %p, err %d", __func__, (void*)base,
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800142 errno);
143 return 0;
144}
feifanz9cc23032016-03-23 18:48:56 +0800145
146int getMetaData(private_handle_t *handle, DispFetchParamType paramType,
147 void *param) {
Arun Kumar K.R3e45ac42017-03-08 19:00:02 -0800148 int ret = -1;
149 if (private_handle_t::validate(handle)) {
150 ALOGE("%s: Private handle is invalid! handle=%p", __func__, handle);
feifanz9cc23032016-03-23 18:48:56 +0800151 return -1;
152 }
153 if (handle->fd_metadata == -1) {
154 ALOGE("%s: Bad fd for extra data!", __func__);
155 return -1;
156 }
157 if (!param) {
158 ALOGE("%s: input param is null!", __func__);
159 return -1;
160 }
161 unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
Arun Kumar K.R3e45ac42017-03-08 19:00:02 -0800162 // TODO: must be mmapped once and freed when the handle gets freed rather
163 // than mapping and un-mapping while getting each metadata field
feifanz9cc23032016-03-23 18:48:56 +0800164 void *base = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED,
165 handle->fd_metadata, 0);
166 if (base == reinterpret_cast<void*>(MAP_FAILED)) {
167 ALOGE("%s: mmap() failed: error is %s!", __func__, strerror(errno));
168 return -1;
169 }
170
171 MetaData_t *data = reinterpret_cast <MetaData_t *>(base);
feifanz9cc23032016-03-23 18:48:56 +0800172 switch (paramType) {
173 case GET_PP_PARAM_INTERLACED:
Arun Kumar K.R3e45ac42017-03-08 19:00:02 -0800174 if (data->operation & PP_PARAM_INTERLACED) {
175 *((int32_t *)param) = data->interlaced;
176 ret = 0;
177 }
feifanz9cc23032016-03-23 18:48:56 +0800178 break;
179 case GET_BUFFER_GEOMETRY:
Arun Kumar K.R3e45ac42017-03-08 19:00:02 -0800180 if (data->operation & UPDATE_BUFFER_GEOMETRY) {
181 *((BufferDim_t *)param) = data->bufferDim;
182 ret = 0;
183 }
feifanz9cc23032016-03-23 18:48:56 +0800184 break;
185 case GET_REFRESH_RATE:
Arun Kumar K.R3e45ac42017-03-08 19:00:02 -0800186 if (data->operation & UPDATE_REFRESH_RATE) {
187 *((float *)param) = data->refreshrate;
188 ret = 0;
189 }
feifanz9cc23032016-03-23 18:48:56 +0800190 break;
191 case GET_COLOR_SPACE:
Arun Kumar K.R3e45ac42017-03-08 19:00:02 -0800192 if (data->operation & UPDATE_COLOR_SPACE) {
193 *((ColorSpace_t *)param) = data->colorSpace;
194 ret = 0;
195 }
feifanz9cc23032016-03-23 18:48:56 +0800196 break;
197 case GET_MAP_SECURE_BUFFER:
Arun Kumar K.R3e45ac42017-03-08 19:00:02 -0800198 if (data->operation & MAP_SECURE_BUFFER) {
199 *((int32_t *)param) = data->mapSecureBuffer;
200 ret = 0;
201 }
feifanz9cc23032016-03-23 18:48:56 +0800202 break;
203 case GET_S3D_FORMAT:
Arun Kumar K.R3e45ac42017-03-08 19:00:02 -0800204 if (data->operation & S3D_FORMAT) {
205 *((uint32_t *)param) = data->s3dFormat;
206 ret = 0;
207 }
feifanz9cc23032016-03-23 18:48:56 +0800208 break;
209 case GET_LINEAR_FORMAT:
Arun Kumar K.R3e45ac42017-03-08 19:00:02 -0800210 if (data->operation & LINEAR_FORMAT) {
211 *((uint32_t *)param) = data->linearFormat;
212 ret = 0;
213 }
feifanz9cc23032016-03-23 18:48:56 +0800214 break;
215 case GET_IGC:
Arun Kumar K.R3e45ac42017-03-08 19:00:02 -0800216 if (data->operation & SET_IGC) {
217 *((IGC_t *)param) = data->igc;
218 ret = 0;
219 }
feifanz9cc23032016-03-23 18:48:56 +0800220 break;
221 case GET_SINGLE_BUFFER_MODE:
Arun Kumar K.R3e45ac42017-03-08 19:00:02 -0800222 if (data->operation & SET_SINGLE_BUFFER_MODE) {
223 *((uint32_t *)param) = data->isSingleBufferMode;
224 ret = 0;
225 }
feifanz9cc23032016-03-23 18:48:56 +0800226 break;
Ray Zhang8ace84d2016-08-08 19:36:03 +0800227 case GET_S3D_COMP:
Arun Kumar K.R3e45ac42017-03-08 19:00:02 -0800228 if (data->operation & SET_S3D_COMP) {
229 *((S3DGpuComp_t *)param) = data->s3dComp;
230 ret = 0;
231 }
Ray Zhang8ace84d2016-08-08 19:36:03 +0800232 break;
Naseer Ahmede8f254d2016-08-02 20:06:40 -0400233 case GET_VT_TIMESTAMP:
Arun Kumar K.R3e45ac42017-03-08 19:00:02 -0800234 if (data->operation & SET_VT_TIMESTAMP) {
235 *((uint64_t *)param) = data->vtTimeStamp;
236 ret = 0;
237 }
Naseer Ahmede8f254d2016-08-02 20:06:40 -0400238 break;
Arun Kumar K.Rb2771bf2016-10-03 21:38:23 -0700239#ifdef USE_COLOR_METADATA
240 case GET_COLOR_METADATA:
Arun Kumar K.R3e45ac42017-03-08 19:00:02 -0800241 if (data->operation & COLOR_METADATA) {
242 *((ColorMetaData *)param) = data->color;
243 ret = 0;
244 }
Arun Kumar K.Rb2771bf2016-10-03 21:38:23 -0700245#endif
246 break;
feifanz9cc23032016-03-23 18:48:56 +0800247 default:
248 ALOGE("Unknown paramType %d", paramType);
249 break;
250 }
251 if(munmap(base, size))
252 ALOGE("%s: failed to unmap ptr %p, err %d", __func__, (void*)base,
253 errno);
Arun Kumar K.R3e45ac42017-03-08 19:00:02 -0800254 return ret;
feifanz9cc23032016-03-23 18:48:56 +0800255}
256
257int copyMetaData(struct private_handle_t *src, struct private_handle_t *dst) {
258 if (!src || !dst) {
259 ALOGE("%s: Private handle is null!", __func__);
260 return -1;
261 }
262 if (src->fd_metadata == -1) {
263 ALOGE("%s: Bad fd for src extra data!", __func__);
264 return -1;
265 }
266 if (dst->fd_metadata == -1) {
267 ALOGE("%s: Bad fd for dst extra data!", __func__);
268 return -1;
269 }
270
271 unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
272
273 void *base_src = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED,
274 src->fd_metadata, 0);
275 if (base_src == reinterpret_cast<void*>(MAP_FAILED)) {
276 ALOGE("%s: src mmap() failed: error is %s!", __func__, strerror(errno));
277 return -1;
278 }
279
280 void *base_dst = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED,
281 dst->fd_metadata, 0);
282 if (base_dst == reinterpret_cast<void*>(MAP_FAILED)) {
283 ALOGE("%s: dst mmap() failed: error is %s!", __func__, strerror(errno));
Baldev Sahu2b4f6042016-09-27 12:29:29 +0530284 if(munmap(base_src, size))
285 ALOGE("%s: failed to unmap src ptr %p, err %d", __func__,
286 (void*)base_src, errno);
feifanz9cc23032016-03-23 18:48:56 +0800287 return -1;
288 }
289
290 memcpy(base_dst, base_src, size);
291
292 if(munmap(base_src, size))
293 ALOGE("%s: failed to unmap src ptr %p, err %d", __func__, (void*)base_src,
294 errno);
295 if(munmap(base_dst, size))
296 ALOGE("%s: failed to unmap src ptr %p, err %d", __func__, (void*)base_dst,
297 errno);
298 return 0;
299}