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 #include <inttypes.h>
36 #include "qdMetaData.h"
37
validateAndMap(private_handle_t * handle)38 static int validateAndMap(private_handle_t* handle) {
39 if (private_handle_t::validate(handle)) {
40 ALOGE("%s: Private handle is invalid - handle:%p id: %" PRIu64,
41 __func__, handle, handle->id);
42 return -1;
43 }
44 if (handle->fd_metadata == -1) {
45 ALOGE("%s: Invalid metadata fd - handle:%p id: %" PRIu64 "fd: %d",
46 __func__, handle, handle->id, handle->fd_metadata);
47 return -1;
48 }
49
50 if (!handle->base_metadata) {
51 unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
52 void *base = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED,
53 handle->fd_metadata, 0);
54 if (base == reinterpret_cast<void*>(MAP_FAILED)) {
55 ALOGE("%s: metadata mmap failed - handle:%p id: %" PRIu64 "fd: %d err: %s",
56 __func__, handle, handle->id, handle->fd_metadata, strerror(errno));
57
58 return -1;
59 }
60 handle->base_metadata = (uintptr_t) base;
61 }
62 return 0;
63 }
64
setMetaData(private_handle_t * handle,DispParamType paramType,void * param)65 int setMetaData(private_handle_t *handle, DispParamType paramType,
66 void *param) {
67 auto err = validateAndMap(handle);
68 if (err != 0)
69 return err;
70
71 MetaData_t *data = reinterpret_cast <MetaData_t *>(handle->base_metadata);
72 // If parameter is NULL reset the specific MetaData Key
73 if (!param) {
74 data->operation &= ~paramType;
75 // reset param
76 return 0;
77 }
78
79 data->operation |= paramType;
80 switch (paramType) {
81 case PP_PARAM_INTERLACED:
82 data->interlaced = *((int32_t *)param);
83 break;
84 case UPDATE_BUFFER_GEOMETRY:
85 data->bufferDim = *((BufferDim_t *)param);
86 break;
87 case UPDATE_REFRESH_RATE:
88 data->refreshrate = *((float *)param);
89 break;
90 case UPDATE_COLOR_SPACE:
91 data->colorSpace = *((ColorSpace_t *)param);
92 break;
93 case MAP_SECURE_BUFFER:
94 data->mapSecureBuffer = *((int32_t *)param);
95 break;
96 case S3D_FORMAT:
97 data->s3dFormat = *((uint32_t *)param);
98 break;
99 case LINEAR_FORMAT:
100 data->linearFormat = *((uint32_t *)param);
101 break;
102 case SET_IGC:
103 data->igc = *((IGC_t *)param);
104 break;
105 case SET_SINGLE_BUFFER_MODE:
106 data->isSingleBufferMode = *((uint32_t *)param);
107 break;
108 case SET_VT_TIMESTAMP:
109 data->vtTimeStamp = *((uint64_t *)param);
110 break;
111 default:
112 ALOGE("Unknown paramType %d", paramType);
113 break;
114 }
115 return 0;
116 }
117
getMetaData(private_handle_t * handle,DispFetchParamType paramType,void * param)118 int getMetaData(private_handle_t *handle, DispFetchParamType paramType,
119 void *param) {
120 int ret = validateAndMap(handle);
121 if (ret != 0)
122 return ret;
123 MetaData_t *data = reinterpret_cast <MetaData_t *>(handle->base_metadata);
124 // Make sure we send 0 only if the operation queried is present
125 ret = -EINVAL;
126
127 switch (paramType) {
128 case GET_PP_PARAM_INTERLACED:
129 if (data->operation & PP_PARAM_INTERLACED) {
130 *((int32_t *)param) = data->interlaced;
131 ret = 0;
132 }
133 break;
134 case GET_BUFFER_GEOMETRY:
135 if (data->operation & UPDATE_BUFFER_GEOMETRY) {
136 *((BufferDim_t *)param) = data->bufferDim;
137 ret = 0;
138 }
139 break;
140 case GET_REFRESH_RATE:
141 if (data->operation & UPDATE_REFRESH_RATE) {
142 *((float *)param) = data->refreshrate;
143 ret = 0;
144 }
145 break;
146 case GET_COLOR_SPACE:
147 if (data->operation & UPDATE_COLOR_SPACE) {
148 *((ColorSpace_t *)param) = data->colorSpace;
149 ret = 0;
150 }
151 break;
152 case GET_MAP_SECURE_BUFFER:
153 if (data->operation & MAP_SECURE_BUFFER) {
154 *((int32_t *)param) = data->mapSecureBuffer;
155 ret = 0;
156 }
157 break;
158 case GET_S3D_FORMAT:
159 if (data->operation & S3D_FORMAT) {
160 *((uint32_t *)param) = data->s3dFormat;
161 ret = 0;
162 }
163 break;
164 case GET_LINEAR_FORMAT:
165 if (data->operation & LINEAR_FORMAT) {
166 *((uint32_t *)param) = data->linearFormat;
167 ret = 0;
168 }
169 break;
170 case GET_IGC:
171 if (data->operation & SET_IGC) {
172 *((IGC_t *)param) = data->igc;
173 ret = 0;
174 }
175 break;
176 case GET_SINGLE_BUFFER_MODE:
177 if (data->operation & SET_SINGLE_BUFFER_MODE) {
178 *((uint32_t *)param) = data->isSingleBufferMode;
179 ret = 0;
180 }
181 break;
182 case GET_VT_TIMESTAMP:
183 if (data->operation & SET_VT_TIMESTAMP) {
184 *((uint64_t *)param) = data->vtTimeStamp;
185 ret = 0;
186 }
187 break;
188 default:
189 ALOGE("Unknown paramType %d", paramType);
190 break;
191 }
192 return ret;
193 }
194
copyMetaData(struct private_handle_t * src,struct private_handle_t * dst)195 int copyMetaData(struct private_handle_t *src, struct private_handle_t *dst) {
196 auto err = validateAndMap(src);
197 if (err != 0)
198 return err;
199
200 err = validateAndMap(dst);
201 if (err != 0)
202 return err;
203
204 unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
205 MetaData_t *src_data = reinterpret_cast <MetaData_t *>(src->base_metadata);
206 MetaData_t *dst_data = reinterpret_cast <MetaData_t *>(dst->base_metadata);
207 memcpy(src_data, dst_data, size);
208 return 0;
209 }
210