blob: 0fa24caa0ce442a836b3895544deebe5475dcaa3 [file] [log] [blame]
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -08001/*
Tharaga Balachandrana6480a42020-01-20 13:58:43 -05002 * Copyright (c) 2012-2020, 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
Tharaga Balachandrana069a7e2020-01-08 17:22:12 -050030#include "qdMetaData.h"
31
32#include <QtiGrallocPriv.h>
Ray Zhang566adac2014-04-14 15:32:04 +080033#include <errno.h>
Tharaga Balachandrana069a7e2020-01-08 17:22:12 -050034#include <gralloc_priv.h>
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -040035#include <gralloctypes/Gralloc4.h>
Tharaga Balachandrana069a7e2020-01-08 17:22:12 -050036#include <log/log.h>
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080037#include <string.h>
38#include <sys/mman.h>
Tharaga Balachandrana069a7e2020-01-08 17:22:12 -050039
Naseer Ahmed08c3e402017-03-16 15:51:54 -040040#include <cinttypes>
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080041
Tharaga Balachandrana6480a42020-01-20 13:58:43 -050042static int colorMetaDataToColorSpace(ColorMetaData in, ColorSpace_t *out) {
43 if (in.colorPrimaries == ColorPrimaries_BT601_6_525 ||
44 in.colorPrimaries == ColorPrimaries_BT601_6_625) {
45 if (in.range == Range_Full) {
46 *out = ITU_R_601_FR;
47 } else {
48 *out = ITU_R_601;
49 }
50 } else if (in.colorPrimaries == ColorPrimaries_BT2020) {
51 if (in.range == Range_Full) {
52 *out = ITU_R_2020_FR;
53 } else {
54 *out = ITU_R_2020;
55 }
56 } else if (in.colorPrimaries == ColorPrimaries_BT709_5) {
57 *out = ITU_R_709;
58 } else {
Yichi Chenbddad072020-03-23 12:50:12 +080059 ALOGE(
60 "Cannot convert ColorMetaData to ColorSpace_t. "
61 "Primaries = %d, Range = %d",
62 in.colorPrimaries, in.range);
Tharaga Balachandrana6480a42020-01-20 13:58:43 -050063 return -1;
64 }
65
66 return 0;
67}
68
69static int colorSpaceToColorMetadata(ColorSpace_t in, ColorMetaData *out) {
70 out->transfer = Transfer_sRGB;
71 switch (in) {
72 case ITU_R_601:
73 out->colorPrimaries = ColorPrimaries_BT601_6_525;
74 out->range = Range_Limited;
75 break;
76 case ITU_R_601_FR:
77 out->colorPrimaries = ColorPrimaries_BT601_6_525;
78 out->range = Range_Full;
79 break;
80 case ITU_R_709:
81 out->colorPrimaries = ColorPrimaries_BT709_5;
82 out->range = Range_Limited;
83 break;
84 case ITU_R_2020:
85 out->colorPrimaries = ColorPrimaries_BT2020;
86 out->range = Range_Limited;
87 break;
88 case ITU_R_2020_FR:
89 out->colorPrimaries = ColorPrimaries_BT2020;
90 out->range = Range_Full;
91 break;
92 default:
Yichi Chenbddad072020-03-23 12:50:12 +080093 ALOGE("Cannot convert ColorSpace_t %d to ColorMetaData", in);
Tharaga Balachandrana6480a42020-01-20 13:58:43 -050094 return -1;
95 break;
96 }
97
98 return 0;
99}
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400100
101static bool getGralloc4Array(MetaData_t *metadata, int32_t paramType) {
102 switch (paramType) {
103 case SET_VT_TIMESTAMP:
104 return metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_VT_TIMESTAMP)];
105 case COLOR_METADATA:
106 return metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_COLOR_METADATA)];
107 case PP_PARAM_INTERLACED:
108 return metadata
109 ->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_PP_PARAM_INTERLACED)];
110 case SET_VIDEO_PERF_MODE:
111 return metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_VIDEO_PERF_MODE)];
112 case SET_GRAPHICS_METADATA:
113 return metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_GRAPHICS_METADATA)];
114 case SET_UBWC_CR_STATS_INFO:
115 return metadata
116 ->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_UBWC_CR_STATS_INFO)];
117 case UPDATE_BUFFER_GEOMETRY:
118 return metadata->isStandardMetadataSet[GET_STANDARD_METADATA_STATUS_INDEX(
119 ::android::gralloc4::MetadataType_Crop.value)];
120 case UPDATE_REFRESH_RATE:
121 return metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_REFRESH_RATE)];
122 case UPDATE_COLOR_SPACE:
123 return metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_COLOR_METADATA)];
124 case MAP_SECURE_BUFFER:
125 return metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_MAP_SECURE_BUFFER)];
126 case LINEAR_FORMAT:
127 return metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_LINEAR_FORMAT)];
128 case SET_SINGLE_BUFFER_MODE:
129 return metadata
130 ->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_SINGLE_BUFFER_MODE)];
131 case SET_CVP_METADATA:
132 return metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_CVP_METADATA)];
133 case SET_VIDEO_HISTOGRAM_STATS:
134 return metadata
135 ->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_VIDEO_HISTOGRAM_STATS)];
136 default:
137 ALOGE("paramType %d not supported", paramType);
138 return false;
139 }
140}
141
142static void setGralloc4Array(MetaData_t *metadata, int32_t paramType, bool isSet) {
143 switch (paramType) {
144 case SET_VT_TIMESTAMP:
145 metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_VT_TIMESTAMP)] = isSet;
146 break;
147 case COLOR_METADATA:
148 metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_COLOR_METADATA)] = isSet;
149 break;
150 case PP_PARAM_INTERLACED:
151 metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_PP_PARAM_INTERLACED)] =
152 isSet;
153 break;
154 case SET_VIDEO_PERF_MODE:
155 metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_VIDEO_PERF_MODE)] = isSet;
156 break;
157 case SET_GRAPHICS_METADATA:
158 metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_GRAPHICS_METADATA)] =
159 isSet;
160 break;
161 case SET_UBWC_CR_STATS_INFO:
162 metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_UBWC_CR_STATS_INFO)] =
163 isSet;
164 break;
165 case UPDATE_BUFFER_GEOMETRY:
166 metadata->isStandardMetadataSet[GET_STANDARD_METADATA_STATUS_INDEX(
167 ::android::gralloc4::MetadataType_Crop.value)] = isSet;
168 break;
169 case UPDATE_REFRESH_RATE:
170 metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_REFRESH_RATE)] = isSet;
171 break;
172 case UPDATE_COLOR_SPACE:
173 metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_COLOR_METADATA)] = isSet;
174 break;
175 case MAP_SECURE_BUFFER:
176 metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_MAP_SECURE_BUFFER)] =
177 isSet;
178 break;
179 case LINEAR_FORMAT:
180 metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_LINEAR_FORMAT)] = isSet;
181 break;
182 case SET_SINGLE_BUFFER_MODE:
183 metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_SINGLE_BUFFER_MODE)] =
184 isSet;
185 break;
186 case SET_CVP_METADATA:
187 metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_CVP_METADATA)] = isSet;
188 break;
189 case SET_VIDEO_HISTOGRAM_STATS:
190 metadata->isVendorMetadataSet[GET_VENDOR_METADATA_STATUS_INDEX(QTI_VIDEO_HISTOGRAM_STATS)] =
191 isSet;
192 break;
193 default:
194 ALOGE("paramType %d not supported in Gralloc4", paramType);
195 }
196}
197
Naseer Ahmed2e6bee12017-04-13 22:45:54 -0400198unsigned long getMetaDataSize() {
199 return static_cast<unsigned long>(ROUND_UP_PAGESIZE(sizeof(MetaData_t)));
200}
201
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400202// Cannot add default argument to existing function
203unsigned long getMetaDataSizeWithReservedRegion(uint64_t reserved_size) {
204 return static_cast<unsigned long>(ROUND_UP_PAGESIZE(sizeof(MetaData_t) + reserved_size));
205}
206
Naseer Ahmed3b3a19c2017-03-28 15:17:27 -0400207static int validateAndMap(private_handle_t* handle) {
Prabhanjan Kandula1ef8e6e2016-08-19 11:46:47 +0530208 if (private_handle_t::validate(handle)) {
Naseer Ahmed3b3a19c2017-03-28 15:17:27 -0400209 ALOGE("%s: Private handle is invalid - handle:%p", __func__, handle);
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800210 return -1;
211 }
Naseer Ahmed7aff2612017-10-05 20:39:05 -0400212 if (handle->fd_metadata < 0) {
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400213 // Metadata cannot be used
214 return -1;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800215 }
Naseer Ahmed3b3a19c2017-03-28 15:17:27 -0400216
217 if (!handle->base_metadata) {
Naseer Ahmed2e6bee12017-04-13 22:45:54 -0400218 auto size = getMetaDataSize();
Naseer Ahmed3b3a19c2017-03-28 15:17:27 -0400219 void *base = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED,
220 handle->fd_metadata, 0);
221 if (base == reinterpret_cast<void*>(MAP_FAILED)) {
222 ALOGE("%s: metadata mmap failed - handle:%p fd: %d err: %s",
223 __func__, handle, handle->fd_metadata, strerror(errno));
Naseer Ahmed3b3a19c2017-03-28 15:17:27 -0400224 return -1;
225 }
226 handle->base_metadata = (uintptr_t) base;
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400227 auto metadata = reinterpret_cast<MetaData_t *>(handle->base_metadata);
228 if (metadata->reservedSize) {
229 auto reserved_size = metadata->reservedSize;
230 munmap(reinterpret_cast<void *>(handle->base_metadata), getMetaDataSize());
231 handle->base_metadata = 0;
232 size = getMetaDataSizeWithReservedRegion(reserved_size);
233 void *new_base =
234 mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd_metadata, 0);
235 if (new_base == reinterpret_cast<void *>(MAP_FAILED)) {
236 ALOGE("%s: metadata mmap failed - handle:%p fd: %d err: %s", __func__, handle,
237 handle->fd_metadata, strerror(errno));
238 return -1;
239 }
240 handle->base_metadata = (uintptr_t)new_base;
241 }
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800242 }
Naseer Ahmed3b3a19c2017-03-28 15:17:27 -0400243 return 0;
244}
245
Praveen Chavan5e5679a2018-11-18 11:14:19 -0800246static void unmapAndReset(private_handle_t *handle) {
247 if (private_handle_t::validate(handle) == 0 && handle->base_metadata) {
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400248 // If reservedSize is 0, the return value will be the same as getMetaDataSize
249 auto metadata = reinterpret_cast<MetaData_t *>(handle->base_metadata);
250 auto size = getMetaDataSizeWithReservedRegion(metadata->reservedSize);
251 munmap(reinterpret_cast<void *>(handle->base_metadata), size);
252 handle->base_metadata = 0;
Praveen Chavan5e5679a2018-11-18 11:14:19 -0800253 }
254}
255
Naseer Ahmed3b3a19c2017-03-28 15:17:27 -0400256int setMetaData(private_handle_t *handle, DispParamType paramType,
Naseer Ahmed2e6bee12017-04-13 22:45:54 -0400257 void *param) {
Naseer Ahmed3b3a19c2017-03-28 15:17:27 -0400258 auto err = validateAndMap(handle);
259 if (err != 0)
260 return err;
Naseer Ahmed2e6bee12017-04-13 22:45:54 -0400261 return setMetaDataVa(reinterpret_cast<MetaData_t*>(handle->base_metadata),
262 paramType, param);
263}
Naseer Ahmed3b3a19c2017-03-28 15:17:27 -0400264
Naseer Ahmed2e6bee12017-04-13 22:45:54 -0400265int setMetaDataVa(MetaData_t *data, DispParamType paramType,
266 void *param) {
267 if (data == nullptr)
268 return -EINVAL;
Prabhanjan Kandula1ef8e6e2016-08-19 11:46:47 +0530269 // If parameter is NULL reset the specific MetaData Key
270 if (!param) {
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400271 setGralloc4Array(data, paramType, false);
272 switch (paramType) {
273 case SET_VIDEO_PERF_MODE:
274 data->isVideoPerfMode = 0;
275 break;
276 case SET_CVP_METADATA:
277 data->cvpMetadata.size = 0;
278 break;
279 case SET_VIDEO_HISTOGRAM_STATS:
280 data->video_histogram_stats.stat_len = 0;
281 break;
282 default:
283 ALOGE("Unknown paramType %d", paramType);
284 break;
285 }
Naseer Ahmed8d1434a2017-03-31 15:31:18 -0400286 // param unset
287 return 0;
Prabhanjan Kandula1ef8e6e2016-08-19 11:46:47 +0530288 }
289
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400290 setGralloc4Array(data, paramType, true);
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800291 switch (paramType) {
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800292 case PP_PARAM_INTERLACED:
293 data->interlaced = *((int32_t *)param);
294 break;
Tharaga Balachandrana069a7e2020-01-08 17:22:12 -0500295 case UPDATE_BUFFER_GEOMETRY: {
296 BufferDim_t in = *((BufferDim_t *)param);
297 data->crop = {0, 0, in.sliceWidth, in.sliceHeight};
298 break;
299 }
Arun Kumar K.Ra727a882014-08-20 17:14:26 -0700300 case UPDATE_REFRESH_RATE:
feifanz9cc23032016-03-23 18:48:56 +0800301 data->refreshrate = *((float *)param);
Arun Kumar K.Ra727a882014-08-20 17:14:26 -0700302 break;
Tharaga Balachandrana6480a42020-01-20 13:58:43 -0500303 case UPDATE_COLOR_SPACE: {
Tharaga Balachandranc20aa832020-04-07 19:26:54 -0400304 ColorMetaData color = {};
Tharaga Balachandrana6480a42020-01-20 13:58:43 -0500305 if (!colorSpaceToColorMetadata(*((ColorSpace_t *)param), &color)) {
306 data->color = color;
Yichi Chenbddad072020-03-23 12:50:12 +0800307 break;
Tharaga Balachandrana6480a42020-01-20 13:58:43 -0500308 }
Yichi Chenbddad072020-03-23 12:50:12 +0800309 return -EINVAL;
Tharaga Balachandrana6480a42020-01-20 13:58:43 -0500310 }
Arun Kumar K.Rda2f69b2014-09-30 15:45:37 -0700311 case MAP_SECURE_BUFFER:
312 data->mapSecureBuffer = *((int32_t *)param);
313 break;
Naseer Ahmed3324ff72015-01-21 17:14:44 -0500314 case S3D_FORMAT:
315 data->s3dFormat = *((uint32_t *)param);
316 break;
Sushil Chauhane7acc3c2015-06-23 16:22:30 -0700317 case LINEAR_FORMAT:
318 data->linearFormat = *((uint32_t *)param);
319 break;
Saurabh Shah95f83682015-10-16 10:30:04 -0700320 case SET_SINGLE_BUFFER_MODE:
Saurabh Shahb8067a42015-11-06 16:52:02 -0800321 data->isSingleBufferMode = *((uint32_t *)param);
Saurabh Shah95f83682015-10-16 10:30:04 -0700322 break;
Naseer Ahmede8f254d2016-08-02 20:06:40 -0400323 case SET_VT_TIMESTAMP:
324 data->vtTimeStamp = *((uint64_t *)param);
325 break;
Arun Kumar K.Rb2771bf2016-10-03 21:38:23 -0700326 case COLOR_METADATA:
327 data->color = *((ColorMetaData *)param);
Arun Kumar K.Rb2771bf2016-10-03 21:38:23 -0700328 break;
Gurunath Ramaswamyb494c022017-07-04 13:47:46 -0700329 case SET_UBWC_CR_STATS_INFO: {
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400330 struct UBWCStats *stats = (struct UBWCStats *)param;
331 int numelems = sizeof(data->ubwcCRStats) / sizeof(struct UBWCStats);
332 for (int i = 0; i < numelems; i++) {
333 data->ubwcCRStats[i] = stats[i];
334 }
335 break;
Gurunath Ramaswamyb494c022017-07-04 13:47:46 -0700336 }
Ashwini Rao6e616962017-11-28 12:12:46 -0800337 case SET_VIDEO_PERF_MODE:
338 data->isVideoPerfMode = *((uint32_t *)param);
339 break;
Mohan Pallaka5d814432018-08-01 10:40:13 -0700340 case SET_GRAPHICS_METADATA: {
341 GraphicsMetadata payload = *((GraphicsMetadata*)(param));
342 data->graphics_metadata.size = payload.size;
343 memcpy(data->graphics_metadata.data, payload.data,
344 sizeof(data->graphics_metadata.data));
345 break;
346 }
Maheshwar Ajja10690722019-01-30 14:12:45 -0800347 case SET_CVP_METADATA: {
348 struct CVPMetadata *cvpMetadata = (struct CVPMetadata *)param;
Maheshwar Ajja0de87d72019-03-11 11:44:30 -0700349 if (cvpMetadata->size <= CVP_METADATA_SIZE) {
Maheshwar Ajja10690722019-01-30 14:12:45 -0800350 data->cvpMetadata.size = cvpMetadata->size;
351 memcpy(data->cvpMetadata.payload, cvpMetadata->payload,
352 cvpMetadata->size);
Akshata Sahukaraab1e562019-07-30 13:57:25 -0700353 data->cvpMetadata.capture_frame_rate = cvpMetadata->capture_frame_rate;
354 data->cvpMetadata.cvp_frame_rate = cvpMetadata->cvp_frame_rate;
Akshata Sahukar8a8657c2019-10-04 20:29:25 -0700355 data->cvpMetadata.flags = cvpMetadata->flags;
356 memcpy(data->cvpMetadata.reserved, cvpMetadata->reserved,
357 (8 * sizeof(uint32_t)));
Maheshwar Ajja10690722019-01-30 14:12:45 -0800358 } else {
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400359 setGralloc4Array(data, paramType, false);
360 ALOGE("%s: cvp metadata length %d is more than max size %d", __func__,
361 cvpMetadata->size, CVP_METADATA_SIZE);
362 return -EINVAL;
Maheshwar Ajja10690722019-01-30 14:12:45 -0800363 }
364 break;
365 }
Mihir Ganu3d401542019-04-03 11:08:06 -0700366 case SET_VIDEO_HISTOGRAM_STATS: {
367 struct VideoHistogramMetadata *vidstats = (struct VideoHistogramMetadata *)param;
368 if (vidstats->stat_len <= VIDEO_HISTOGRAM_STATS_SIZE) {
369 memcpy(data->video_histogram_stats.stats_info,
370 vidstats->stats_info, VIDEO_HISTOGRAM_STATS_SIZE);
371 data->video_histogram_stats.stat_len = vidstats->stat_len;
372 data->video_histogram_stats.frame_type = vidstats->frame_type;
373 data->video_histogram_stats.display_width = vidstats->display_width;
374 data->video_histogram_stats.display_height = vidstats->display_height;
375 data->video_histogram_stats.decode_width = vidstats->decode_width;
376 data->video_histogram_stats.decode_height = vidstats->decode_height;
377 } else {
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400378 setGralloc4Array(data, paramType, false);
379 ALOGE("%s: video stats length %u is more than max size %u", __func__,
380 vidstats->stat_len, VIDEO_HISTOGRAM_STATS_SIZE);
381 return -EINVAL;
Mihir Ganu3d401542019-04-03 11:08:06 -0700382 }
383 break;
384 }
385 default:
Ray Zhang8ace84d2016-08-08 19:36:03 +0800386 ALOGE("Unknown paramType %d", paramType);
387 break;
388 }
Ray Zhang8ace84d2016-08-08 19:36:03 +0800389 return 0;
390}
391
392int clearMetaData(private_handle_t *handle, DispParamType paramType) {
Naseer Ahmed3b3a19c2017-03-28 15:17:27 -0400393 auto err = validateAndMap(handle);
394 if (err != 0)
395 return err;
Naseer Ahmed2e6bee12017-04-13 22:45:54 -0400396 return clearMetaDataVa(reinterpret_cast<MetaData_t *>(handle->base_metadata),
397 paramType);
398}
Ray Zhang8ace84d2016-08-08 19:36:03 +0800399
Naseer Ahmed2e6bee12017-04-13 22:45:54 -0400400int clearMetaDataVa(MetaData_t *data, DispParamType paramType) {
401 if (data == nullptr)
402 return -EINVAL;
Ray Zhang8ace84d2016-08-08 19:36:03 +0800403 data->operation &= ~paramType;
404 switch (paramType) {
Priyanka Gujjula1db74712018-11-27 17:38:26 +0530405 case SET_VIDEO_PERF_MODE:
406 data->isVideoPerfMode = 0;
407 break;
Maheshwar Ajja10690722019-01-30 14:12:45 -0800408 case SET_CVP_METADATA:
409 data->cvpMetadata.size = 0;
410 break;
Mihir Ganu3d401542019-04-03 11:08:06 -0700411 case SET_VIDEO_HISTOGRAM_STATS:
412 data->video_histogram_stats.stat_len = 0;
413 break;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800414 default:
415 ALOGE("Unknown paramType %d", paramType);
416 break;
417 }
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800418 return 0;
419}
feifanz9cc23032016-03-23 18:48:56 +0800420
421int getMetaData(private_handle_t *handle, DispFetchParamType paramType,
422 void *param) {
Naseer Ahmed3b3a19c2017-03-28 15:17:27 -0400423 int ret = validateAndMap(handle);
424 if (ret != 0)
425 return ret;
Naseer Ahmed2e6bee12017-04-13 22:45:54 -0400426 return getMetaDataVa(reinterpret_cast<MetaData_t *>(handle->base_metadata),
427 paramType, param);
428}
429
430int getMetaDataVa(MetaData_t *data, DispFetchParamType paramType,
431 void *param) {
Naseer Ahmed3b3a19c2017-03-28 15:17:27 -0400432 // Make sure we send 0 only if the operation queried is present
Naseer Ahmed2e6bee12017-04-13 22:45:54 -0400433 int ret = -EINVAL;
434 if (data == nullptr)
435 return ret;
Gurunath Ramaswamyb494c022017-07-04 13:47:46 -0700436 if (param == nullptr)
437 return ret;
feifanz9cc23032016-03-23 18:48:56 +0800438
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400439 if (!getGralloc4Array(data, paramType)) {
440 return ret;
441 }
442
443 ret = 0;
444
feifanz9cc23032016-03-23 18:48:56 +0800445 switch (paramType) {
446 case GET_PP_PARAM_INTERLACED:
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400447 *((int32_t *)param) = data->interlaced;
448 break;
feifanz9cc23032016-03-23 18:48:56 +0800449 case GET_BUFFER_GEOMETRY:
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400450 *((BufferDim_t *)param) = {data->crop.right, data->crop.bottom};
451 break;
feifanz9cc23032016-03-23 18:48:56 +0800452 case GET_REFRESH_RATE:
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400453 *((float *)param) = data->refreshrate;
454 break;
455 case GET_COLOR_SPACE: {
456 ColorSpace_t color_space;
457 if (!colorMetaDataToColorSpace(data->color, &color_space)) {
458 *((ColorSpace_t *)param) = color_space;
459 } else {
460 ret = -EINVAL;
461 }
462 break;
463 }
feifanz9cc23032016-03-23 18:48:56 +0800464 case GET_MAP_SECURE_BUFFER:
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400465 *((int32_t *)param) = data->mapSecureBuffer;
466 break;
feifanz9cc23032016-03-23 18:48:56 +0800467 case GET_S3D_FORMAT:
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400468 *((uint32_t *)param) = data->s3dFormat;
469 break;
feifanz9cc23032016-03-23 18:48:56 +0800470 case GET_LINEAR_FORMAT:
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400471 *((uint32_t *)param) = data->linearFormat;
472 break;
feifanz9cc23032016-03-23 18:48:56 +0800473 case GET_SINGLE_BUFFER_MODE:
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400474 *((uint32_t *)param) = data->isSingleBufferMode;
475 break;
Naseer Ahmede8f254d2016-08-02 20:06:40 -0400476 case GET_VT_TIMESTAMP:
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400477 *((uint64_t *)param) = data->vtTimeStamp;
478 break;
Arun Kumar K.Rb2771bf2016-10-03 21:38:23 -0700479 case GET_COLOR_METADATA:
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400480 *((ColorMetaData *)param) = data->color;
481 break;
482 case GET_UBWC_CR_STATS_INFO: {
483 struct UBWCStats *stats = (struct UBWCStats *)param;
484 int numelems = sizeof(data->ubwcCRStats) / sizeof(struct UBWCStats);
485 for (int i = 0; i < numelems; i++) {
486 stats[i] = data->ubwcCRStats[i];
487 }
488 break;
489 }
Ashwini Rao6e616962017-11-28 12:12:46 -0800490 case GET_VIDEO_PERF_MODE:
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400491 *((uint32_t *)param) = data->isVideoPerfMode;
492 break;
Mohan Pallaka5d814432018-08-01 10:40:13 -0700493 case GET_GRAPHICS_METADATA:
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400494 memcpy(param, data->graphics_metadata.data, sizeof(data->graphics_metadata.data));
495 break;
496 case GET_CVP_METADATA: {
497 struct CVPMetadata *cvpMetadata = (struct CVPMetadata *)param;
498 cvpMetadata->size = 0;
499 if (data->cvpMetadata.size <= CVP_METADATA_SIZE) {
500 cvpMetadata->size = data->cvpMetadata.size;
501 memcpy(cvpMetadata->payload, data->cvpMetadata.payload, data->cvpMetadata.size);
502 cvpMetadata->capture_frame_rate = data->cvpMetadata.capture_frame_rate;
503 cvpMetadata->cvp_frame_rate = data->cvpMetadata.cvp_frame_rate;
504 cvpMetadata->flags = data->cvpMetadata.flags;
505 memcpy(cvpMetadata->reserved, data->cvpMetadata.reserved, (8 * sizeof(uint32_t)));
506 } else {
507 ret = -EINVAL;
508 }
509 break;
510 }
511 case GET_VIDEO_HISTOGRAM_STATS: {
512 struct VideoHistogramMetadata *vidstats = (struct VideoHistogramMetadata *)param;
513 vidstats->stat_len = 0;
514 if (data->video_histogram_stats.stat_len <= VIDEO_HISTOGRAM_STATS_SIZE) {
515 memcpy(vidstats->stats_info, data->video_histogram_stats.stats_info,
516 VIDEO_HISTOGRAM_STATS_SIZE);
517 vidstats->stat_len = data->video_histogram_stats.stat_len;
518 vidstats->frame_type = data->video_histogram_stats.frame_type;
519 vidstats->display_width = data->video_histogram_stats.display_width;
520 vidstats->display_height = data->video_histogram_stats.display_height;
521 vidstats->decode_width = data->video_histogram_stats.decode_width;
522 vidstats->decode_height = data->video_histogram_stats.decode_height;
523 } else {
524 ret = -EINVAL;
525 }
526 break;
527 }
feifanz9cc23032016-03-23 18:48:56 +0800528 default:
529 ALOGE("Unknown paramType %d", paramType);
Tharaga Balachandrandf242cf2020-06-16 09:34:34 -0400530 ret = -EINVAL;
feifanz9cc23032016-03-23 18:48:56 +0800531 break;
532 }
Arun Kumar K.R3e45ac42017-03-08 19:00:02 -0800533 return ret;
feifanz9cc23032016-03-23 18:48:56 +0800534}
535
536int copyMetaData(struct private_handle_t *src, struct private_handle_t *dst) {
Naseer Ahmed3b3a19c2017-03-28 15:17:27 -0400537 auto err = validateAndMap(src);
538 if (err != 0)
539 return err;
540
541 err = validateAndMap(dst);
542 if (err != 0)
543 return err;
feifanz9cc23032016-03-23 18:48:56 +0800544
Naseer Ahmed3b3a19c2017-03-28 15:17:27 -0400545 MetaData_t *src_data = reinterpret_cast <MetaData_t *>(src->base_metadata);
546 MetaData_t *dst_data = reinterpret_cast <MetaData_t *>(dst->base_metadata);
Naseer Ahmede9b87622017-07-06 12:53:15 -0400547 *dst_data = *src_data;
feifanz9cc23032016-03-23 18:48:56 +0800548 return 0;
549}
Naseer Ahmed2e6bee12017-04-13 22:45:54 -0400550
551int copyMetaDataVaToHandle(MetaData_t *src_data, struct private_handle_t *dst) {
552 int err = -EINVAL;
553 if (src_data == nullptr)
554 return err;
555
556 err = validateAndMap(dst);
557 if (err != 0)
558 return err;
559
560 MetaData_t *dst_data = reinterpret_cast <MetaData_t *>(dst->base_metadata);
Naseer Ahmede9b87622017-07-06 12:53:15 -0400561 *dst_data = *src_data;
Naseer Ahmed2e6bee12017-04-13 22:45:54 -0400562 return 0;
563}
564
565int copyMetaDataHandleToVa(struct private_handle_t *src, MetaData_t *dst_data) {
566 int err = -EINVAL;
567 if (dst_data == nullptr)
568 return err;
569
570 err = validateAndMap(src);
571 if (err != 0)
572 return err;
573
574 MetaData_t *src_data = reinterpret_cast <MetaData_t *>(src->base_metadata);
Naseer Ahmede9b87622017-07-06 12:53:15 -0400575 *dst_data = *src_data;
Naseer Ahmed2e6bee12017-04-13 22:45:54 -0400576 return 0;
577}
578
579int copyMetaDataVaToVa(MetaData_t *src_data, MetaData_t *dst_data) {
580 int err = -EINVAL;
581 if (src_data == nullptr)
582 return err;
583
584 if (dst_data == nullptr)
585 return err;
586
Naseer Ahmede9b87622017-07-06 12:53:15 -0400587 *dst_data = *src_data;
Naseer Ahmed2e6bee12017-04-13 22:45:54 -0400588 return 0;
589}
590
Praveen Chavan5e5679a2018-11-18 11:14:19 -0800591int setMetaDataAndUnmap(struct private_handle_t *handle, enum DispParamType paramType,
592 void *param) {
593 auto ret = setMetaData(handle, paramType, param);
594 unmapAndReset(handle);
595 return ret;
596}
597
598int getMetaDataAndUnmap(struct private_handle_t *handle,
599 enum DispFetchParamType paramType,
600 void *param) {
601 auto ret = getMetaData(handle, paramType, param);
602 unmapAndReset(handle);
603 return ret;
604}