1 /* 2 * Copyright (c) 2012-2016, 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 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 default: 99 ALOGE("Unknown paramType %d", paramType); 100 break; 101 } 102 if(munmap(base, size)) 103 ALOGE("%s: failed to unmap ptr %p, err %d", __func__, (void*)base, 104 errno); 105 return 0; 106 } 107 108 int clearMetaData(private_handle_t *handle, DispParamType paramType) { 109 if (!handle) { 110 ALOGE("%s: Private handle is null!", __func__); 111 return -1; 112 } 113 if (handle->fd_metadata == -1) { 114 ALOGE("%s: Bad fd for extra data!", __func__); 115 return -1; 116 } 117 118 unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); 119 void *base = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, 120 handle->fd_metadata, 0); 121 if (base == reinterpret_cast<void*>(MAP_FAILED)) { 122 ALOGE("%s: mmap() failed: error is %s!", __func__, strerror(errno)); 123 return -1; 124 } 125 MetaData_t *data = reinterpret_cast <MetaData_t *>(base); 126 data->operation &= ~paramType; 127 switch (paramType) { 128 case SET_S3D_COMP: 129 data->s3dComp.displayId = -1; 130 data->s3dComp.s3dMode = 0; 131 break; 132 default: 133 ALOGE("Unknown paramType %d", paramType); 134 break; 135 } 136 if(munmap(base, size)) 137 ALOGE("%s: failed to unmap ptr %p, err %d", __func__, (void*)base, 138 errno); 139 return 0; 140 } 141 142 int getMetaData(private_handle_t *handle, DispFetchParamType paramType, 143 void *param) { 144 if (!handle) { 145 ALOGE("%s: Private handle is null!", __func__); 146 return -1; 147 } 148 if (handle->fd_metadata == -1) { 149 ALOGE("%s: Bad fd for extra data!", __func__); 150 return -1; 151 } 152 if (!param) { 153 ALOGE("%s: input param is null!", __func__); 154 return -1; 155 } 156 unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); 157 void *base = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, 158 handle->fd_metadata, 0); 159 if (base == reinterpret_cast<void*>(MAP_FAILED)) { 160 ALOGE("%s: mmap() failed: error is %s!", __func__, strerror(errno)); 161 return -1; 162 } 163 164 MetaData_t *data = reinterpret_cast <MetaData_t *>(base); 165 switch (paramType) { 166 case GET_PP_PARAM_INTERLACED: 167 *((int32_t *)param) = data->interlaced; 168 break; 169 case GET_BUFFER_GEOMETRY: 170 *((BufferDim_t *)param) = data->bufferDim; 171 break; 172 case GET_REFRESH_RATE: 173 *((float *)param) = data->refreshrate; 174 break; 175 case GET_COLOR_SPACE: 176 *((ColorSpace_t *)param) = data->colorSpace; 177 break; 178 case GET_MAP_SECURE_BUFFER: 179 *((int32_t *)param) = data->mapSecureBuffer; 180 break; 181 case GET_S3D_FORMAT: 182 *((uint32_t *)param) = data->s3dFormat; 183 break; 184 case GET_LINEAR_FORMAT: 185 *((uint32_t *)param) = data->linearFormat; 186 break; 187 case GET_IGC: 188 *((IGC_t *)param) = data->igc; 189 break; 190 case GET_SINGLE_BUFFER_MODE: 191 *((uint32_t *)param) = data->isSingleBufferMode ; 192 break; 193 case GET_S3D_COMP: 194 *((S3DGpuComp_t *)param) = data->s3dComp; 195 break; 196 case GET_VT_TIMESTAMP: 197 *((uint64_t *)param) = data->vtTimeStamp; 198 break; 199 default: 200 ALOGE("Unknown paramType %d", paramType); 201 break; 202 } 203 if(munmap(base, size)) 204 ALOGE("%s: failed to unmap ptr %p, err %d", __func__, (void*)base, 205 errno); 206 return 0; 207 } 208 209 int copyMetaData(struct private_handle_t *src, struct private_handle_t *dst) { 210 if (!src || !dst) { 211 ALOGE("%s: Private handle is null!", __func__); 212 return -1; 213 } 214 if (src->fd_metadata == -1) { 215 ALOGE("%s: Bad fd for src extra data!", __func__); 216 return -1; 217 } 218 if (dst->fd_metadata == -1) { 219 ALOGE("%s: Bad fd for dst extra data!", __func__); 220 return -1; 221 } 222 223 unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); 224 225 void *base_src = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, 226 src->fd_metadata, 0); 227 if (base_src == reinterpret_cast<void*>(MAP_FAILED)) { 228 ALOGE("%s: src mmap() failed: error is %s!", __func__, strerror(errno)); 229 return -1; 230 } 231 232 void *base_dst = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, 233 dst->fd_metadata, 0); 234 if (base_dst == reinterpret_cast<void*>(MAP_FAILED)) { 235 ALOGE("%s: dst mmap() failed: error is %s!", __func__, strerror(errno)); 236 return -1; 237 } 238 239 memcpy(base_dst, base_src, size); 240 241 if(munmap(base_src, size)) 242 ALOGE("%s: failed to unmap src ptr %p, err %d", __func__, (void*)base_src, 243 errno); 244 if(munmap(base_dst, size)) 245 ALOGE("%s: failed to unmap src ptr %p, err %d", __func__, (void*)base_dst, 246 errno); 247 return 0; 248 } 249