blob: 5172ad545947f85ff3a7f7590455285fa83499d0 [file] [log] [blame]
Tharaga Balachandrana069a7e2020-01-08 17:22:12 -05001/*
2 * Copyright (c) 2020, 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 "QtiGralloc.h"
31
32#include <log/log.h>
33namespace qtigralloc {
34
35using android::hardware::graphics::mapper::V4_0::IMapper;
Yichi Chenbddad072020-03-23 12:50:12 +080036
Tharaga Balachandrana069a7e2020-01-08 17:22:12 -050037static sp<IMapper> getInstance() {
38 static sp<IMapper> mapper = IMapper::getService();
39 return mapper;
40}
Tharaga Balachandran39810462020-06-16 09:34:34 -040041
42Error decodeMetadataState(hidl_vec<uint8_t> &in, bool *out) {
43 if (!in.size() || !out) {
44 return Error::BAD_VALUE;
45 }
46 memcpy(out, in.data(), METADATA_SET_SIZE);
47 return Error::NONE;
48}
49
50Error encodeMetadataState(bool *in, hidl_vec<uint8_t> *out) {
51 if (!in || !out) {
52 return Error::BAD_VALUE;
53 }
54 out->resize(sizeof(bool) * METADATA_SET_SIZE);
55 memcpy(out->data(), in, sizeof(bool) * METADATA_SET_SIZE);
56 return Error::NONE;
57}
58
Tharaga Balachandrana069a7e2020-01-08 17:22:12 -050059Error decodeColorMetadata(hidl_vec<uint8_t> &in, ColorMetaData *out) {
60 if (!in.size() || !out) {
61 return Error::BAD_VALUE;
62 }
63 memcpy(out, in.data(), sizeof(ColorMetaData));
64 return Error::NONE;
65}
66
67Error encodeColorMetadata(ColorMetaData &in, hidl_vec<uint8_t> *out) {
68 if (!out) {
69 return Error::BAD_VALUE;
70 }
71 out->resize(sizeof(ColorMetaData));
72 memcpy(out->data(), &in, sizeof(ColorMetaData));
73 return Error::NONE;
74}
75
Ravi Ranjanc74a80e2020-05-04 09:40:39 +053076// decode the raw graphics metadata from bytestream and store it in 'data' member of
77// GraphicsMetadata struct during mapper->set call, 'size' member is unused.
Tharaga Balachandrana069a7e2020-01-08 17:22:12 -050078Error decodeGraphicsMetadata(hidl_vec<uint8_t> &in, GraphicsMetadata *out) {
79 if (!in.size() || !out) {
80 return Error::BAD_VALUE;
81 }
Ravi Ranjanc74a80e2020-05-04 09:40:39 +053082 memcpy(&(out->data), in.data(), GRAPHICS_METADATA_SIZE_IN_BYTES);
Tharaga Balachandrana069a7e2020-01-08 17:22:12 -050083 return Error::NONE;
84}
85
Ravi Ranjanc74a80e2020-05-04 09:40:39 +053086// encode only 'data' member of GraphicsMetadata struct for retrieval of
87// graphics metadata during mapper->get call
Tharaga Balachandrana069a7e2020-01-08 17:22:12 -050088Error encodeGraphicsMetadata(GraphicsMetadata &in, hidl_vec<uint8_t> *out) {
89 if (!out) {
90 return Error::BAD_VALUE;
91 }
Ravi Ranjanc74a80e2020-05-04 09:40:39 +053092 out->resize(GRAPHICS_METADATA_SIZE_IN_BYTES);
93 memcpy(out->data(), &(in.data), GRAPHICS_METADATA_SIZE_IN_BYTES);
94 return Error::NONE;
95}
96
97// decode the raw graphics metadata from bytestream before presenting it to caller
98Error decodeGraphicsMetadataRaw(hidl_vec<uint8_t> &in, void *out) {
99 if (!in.size() || !out) {
100 return Error::BAD_VALUE;
101 }
102 memcpy(out, in.data(), GRAPHICS_METADATA_SIZE_IN_BYTES);
103 return Error::NONE;
104}
105
106// encode the raw graphics metadata in bytestream before calling mapper->set
107Error encodeGraphicsMetadataRaw(void *in, hidl_vec<uint8_t> *out) {
108 if (!in || !out) {
109 return Error::BAD_VALUE;
110 }
111 out->resize(GRAPHICS_METADATA_SIZE_IN_BYTES);
112 memcpy(out->data(), in, GRAPHICS_METADATA_SIZE_IN_BYTES);
Tharaga Balachandrana069a7e2020-01-08 17:22:12 -0500113 return Error::NONE;
114}
115
116Error decodeUBWCStats(hidl_vec<uint8_t> &in, UBWCStats *out) {
117 if (!in.size() || !out) {
118 return Error::BAD_VALUE;
119 }
120 memcpy(out, in.data(), UBWC_STATS_ARRAY_SIZE * sizeof(UBWCStats));
121 return Error::NONE;
122}
123
124Error encodeUBWCStats(UBWCStats *in, hidl_vec<uint8_t> *out) {
125 if (!in || !out) {
126 return Error::BAD_VALUE;
127 }
128 out->resize(UBWC_STATS_ARRAY_SIZE * sizeof(UBWCStats));
Yichi Chenbddad072020-03-23 12:50:12 +0800129 memcpy(out->data(), in, UBWC_STATS_ARRAY_SIZE * sizeof(UBWCStats));
Tharaga Balachandrana069a7e2020-01-08 17:22:12 -0500130 return Error::NONE;
131}
132
133Error decodeCVPMetadata(hidl_vec<uint8_t> &in, CVPMetadata *out) {
134 if (!in.size() || !out) {
135 return Error::BAD_VALUE;
136 }
137 memcpy(out, in.data(), sizeof(CVPMetadata));
138 return Error::NONE;
139}
140
141Error encodeCVPMetadata(CVPMetadata &in, hidl_vec<uint8_t> *out) {
142 if (!out) {
143 return Error::BAD_VALUE;
144 }
145 out->resize(sizeof(CVPMetadata));
146 memcpy(out->data(), &in, sizeof(CVPMetadata));
147 return Error::NONE;
148}
149
150Error decodeVideoHistogramMetadata(hidl_vec<uint8_t> &in, VideoHistogramMetadata *out) {
151 if (!in.size() || !out) {
152 return Error::BAD_VALUE;
153 }
154 memcpy(out, in.data(), sizeof(VideoHistogramMetadata));
155 return Error::NONE;
156}
157
158Error encodeVideoHistogramMetadata(VideoHistogramMetadata &in, hidl_vec<uint8_t> *out) {
159 if (!out) {
160 return Error::BAD_VALUE;
161 }
162 out->resize(sizeof(VideoHistogramMetadata));
163 memcpy(out->data(), &in, sizeof(VideoHistogramMetadata));
164 return Error::NONE;
165}
166
167MetadataType getMetadataType(uint32_t in) {
168 switch (in) {
169 case QTI_VT_TIMESTAMP:
170 return MetadataType_VTTimestamp;
171 case QTI_VIDEO_PERF_MODE:
172 return MetadataType_VideoPerfMode;
173 case QTI_LINEAR_FORMAT:
174 return MetadataType_LinearFormat;
175 case QTI_SINGLE_BUFFER_MODE:
176 return MetadataType_SingleBufferMode;
177 case QTI_PP_PARAM_INTERLACED:
178 return MetadataType_PPParamInterlaced;
179 case QTI_MAP_SECURE_BUFFER:
180 return MetadataType_MapSecureBuffer;
181 case QTI_COLOR_METADATA:
182 return MetadataType_ColorMetadata;
183 case QTI_GRAPHICS_METADATA:
184 return MetadataType_GraphicsMetadata;
185 case QTI_UBWC_CR_STATS_INFO:
186 return MetadataType_UBWCCRStatsInfo;
187 case QTI_REFRESH_RATE:
188 return MetadataType_RefreshRate;
189 case QTI_CVP_METADATA:
190 return MetadataType_CVPMetadata;
191 case QTI_VIDEO_HISTOGRAM_STATS:
192 return MetadataType_VideoHistogramStats;
193 case QTI_FD:
194 return MetadataType_FD;
195 case QTI_PRIVATE_FLAGS:
196 return MetadataType_PrivateFlags;
Tharaga Balachandranaa640f72020-03-10 13:52:04 -0400197 case QTI_ALIGNED_WIDTH_IN_PIXELS:
198 return MetadataType_AlignedWidthInPixels;
199 case QTI_ALIGNED_HEIGHT_IN_PIXELS:
200 return MetadataType_AlignedHeightInPixels;
Tharaga Balachandran39810462020-06-16 09:34:34 -0400201 case QTI_STANDARD_METADATA_STATUS:
202 return MetadataType_StandardMetadataStatus;
203 case QTI_VENDOR_METADATA_STATUS:
204 return MetadataType_VendorMetadataStatus;
Tharaga Balachandranbcd39b92020-04-22 16:40:30 -0400205 case QTI_BUFFER_TYPE:
206 return MetadataType_BufferType;
Tharaga Balachandrana069a7e2020-01-08 17:22:12 -0500207 default:
208 return MetadataType_Invalid;
209 }
210}
211
212Error get(void *buffer, uint32_t type, void *param) {
213 hidl_vec<uint8_t> bytestream;
214 sp<IMapper> mapper = getInstance();
215
216 MetadataType metadata_type = getMetadataType(type);
217 if (metadata_type == MetadataType_Invalid) {
218 param = nullptr;
219 return Error::UNSUPPORTED;
220 }
221
222 auto err = Error::UNSUPPORTED;
223 mapper->get(buffer, metadata_type, [&](const auto &tmpError, const auto &tmpByteStream) {
224 err = tmpError;
225 bytestream = tmpByteStream;
226 });
227
228 if (err != Error::NONE) {
229 return err;
230 }
231
232 switch (type) {
233 case QTI_VT_TIMESTAMP:
234 err = static_cast<Error>(android::gralloc4::decodeUint64(qtigralloc::MetadataType_VTTimestamp,
235 bytestream, (uint64_t *)param));
236 break;
237 case QTI_VIDEO_PERF_MODE:
238 err = static_cast<Error>(android::gralloc4::decodeUint32(
239 qtigralloc::MetadataType_VideoPerfMode, bytestream, (uint32_t *)param));
240 break;
241 case QTI_LINEAR_FORMAT:
242 err = static_cast<Error>(android::gralloc4::decodeUint32(
243 qtigralloc::MetadataType_LinearFormat, bytestream, (uint32_t *)param));
244 break;
245 case QTI_SINGLE_BUFFER_MODE:
246 err = static_cast<Error>(android::gralloc4::decodeUint32(
247 qtigralloc::MetadataType_SingleBufferMode, bytestream, (uint32_t *)param));
248 break;
249 case QTI_PP_PARAM_INTERLACED:
250 err = static_cast<Error>(android::gralloc4::decodeInt32(
251 qtigralloc::MetadataType_PPParamInterlaced, bytestream, (int32_t *)param));
252 break;
253 case QTI_MAP_SECURE_BUFFER:
254 err = static_cast<Error>(android::gralloc4::decodeInt32(
255 qtigralloc::MetadataType_MapSecureBuffer, bytestream, (int32_t *)param));
256 break;
257 case QTI_COLOR_METADATA:
258 err = decodeColorMetadata(bytestream, (ColorMetaData *)param);
259 break;
260 case QTI_GRAPHICS_METADATA:
Ravi Ranjanc74a80e2020-05-04 09:40:39 +0530261 err = decodeGraphicsMetadataRaw(bytestream, param);
Tharaga Balachandrana069a7e2020-01-08 17:22:12 -0500262 break;
263 case QTI_UBWC_CR_STATS_INFO:
264 err = decodeUBWCStats(bytestream, (UBWCStats *)param);
265 break;
266 case QTI_REFRESH_RATE:
267 err = static_cast<Error>(android::gralloc4::decodeFloat(qtigralloc::MetadataType_RefreshRate,
268 bytestream, (float *)param));
269 break;
270 case QTI_CVP_METADATA:
271 err = decodeCVPMetadata(bytestream, (CVPMetadata *)param);
272 break;
273 case QTI_VIDEO_HISTOGRAM_STATS:
274 err = decodeVideoHistogramMetadata(bytestream, (VideoHistogramMetadata *)param);
275 break;
276 case QTI_FD:
277 err = static_cast<Error>(android::gralloc4::decodeInt32(qtigralloc::MetadataType_FD,
278 bytestream, (int32_t *)param));
279 break;
280 case QTI_PRIVATE_FLAGS:
281 err = static_cast<Error>(android::gralloc4::decodeInt32(qtigralloc::MetadataType_PrivateFlags,
282 bytestream, (int32_t *)param));
283 break;
Tharaga Balachandranaa640f72020-03-10 13:52:04 -0400284 case QTI_ALIGNED_WIDTH_IN_PIXELS:
285 err = static_cast<Error>(android::gralloc4::decodeUint32(
286 qtigralloc::MetadataType_AlignedWidthInPixels, bytestream, (uint32_t *)param));
287 break;
288 case QTI_ALIGNED_HEIGHT_IN_PIXELS:
289 err = static_cast<Error>(android::gralloc4::decodeUint32(
290 qtigralloc::MetadataType_AlignedHeightInPixels, bytestream, (uint32_t *)param));
291 break;
Tharaga Balachandran39810462020-06-16 09:34:34 -0400292 case QTI_STANDARD_METADATA_STATUS:
293 case QTI_VENDOR_METADATA_STATUS:
294 err = decodeMetadataState(bytestream, (bool *)param);
295 break;
Tharaga Balachandranbcd39b92020-04-22 16:40:30 -0400296 case QTI_BUFFER_TYPE:
297 err = static_cast<Error>(android::gralloc4::decodeUint32(
298 qtigralloc::MetadataType_BufferType, bytestream, (uint32_t *)param));
299 break;
Tharaga Balachandrana069a7e2020-01-08 17:22:12 -0500300 default:
301 param = nullptr;
302 return Error::UNSUPPORTED;
303 }
304
305 return err;
306}
307
308Error set(void *buffer, uint32_t type, void *param) {
309 hidl_vec<uint8_t> bytestream;
310 sp<IMapper> mapper = getInstance();
311
312 Error err = Error::UNSUPPORTED;
313 MetadataType metadata_type = getMetadataType(type);
314 if (metadata_type == MetadataType_Invalid) {
315 return err;
316 }
317
318 switch (type) {
319 case QTI_VT_TIMESTAMP:
320 err = static_cast<Error>(android::gralloc4::encodeUint64(qtigralloc::MetadataType_VTTimestamp,
321 *(uint64_t *)param, &bytestream));
322 break;
323 case QTI_VIDEO_PERF_MODE:
324 err = static_cast<Error>(android::gralloc4::encodeUint32(
325 qtigralloc::MetadataType_VideoPerfMode, *(uint32_t *)param, &bytestream));
326 break;
327 case QTI_LINEAR_FORMAT:
328 err = static_cast<Error>(android::gralloc4::encodeUint32(
329 qtigralloc::MetadataType_LinearFormat, *(uint32_t *)param, &bytestream));
330 break;
331 case QTI_SINGLE_BUFFER_MODE:
332 err = static_cast<Error>(android::gralloc4::encodeUint32(
333 qtigralloc::MetadataType_SingleBufferMode, *(uint32_t *)param, &bytestream));
334 break;
335 case QTI_PP_PARAM_INTERLACED:
336 err = static_cast<Error>(android::gralloc4::encodeInt32(
337 qtigralloc::MetadataType_PPParamInterlaced, *(int32_t *)param, &bytestream));
338 break;
339 case QTI_MAP_SECURE_BUFFER:
340 err = static_cast<Error>(android::gralloc4::encodeInt32(
341 qtigralloc::MetadataType_MapSecureBuffer, *(int32_t *)param, &bytestream));
342 break;
343 case QTI_COLOR_METADATA:
344 err = encodeColorMetadata(*(ColorMetaData *)param, &bytestream);
345 break;
346 case QTI_GRAPHICS_METADATA:
Ravi Ranjanc74a80e2020-05-04 09:40:39 +0530347 err = encodeGraphicsMetadataRaw(param, &bytestream);
Tharaga Balachandrana069a7e2020-01-08 17:22:12 -0500348 break;
349 case QTI_UBWC_CR_STATS_INFO:
350 err = encodeUBWCStats((UBWCStats *)param, &bytestream);
351 break;
352 case QTI_REFRESH_RATE:
353 err = static_cast<Error>(android::gralloc4::encodeFloat(qtigralloc::MetadataType_RefreshRate,
354 *(float *)param, &bytestream));
355 break;
356 case QTI_CVP_METADATA:
357 err = encodeCVPMetadata(*(CVPMetadata *)param, &bytestream);
358 break;
359 case QTI_VIDEO_HISTOGRAM_STATS:
360 err = encodeVideoHistogramMetadata(*(VideoHistogramMetadata *)param, &bytestream);
361 break;
362 default:
363 param = nullptr;
364 return Error::UNSUPPORTED;
365 }
366
367 if (err != Error::NONE) {
368 return err;
369 }
370
371 return mapper->set((void *)buffer, metadata_type, bytestream);
372}
373
Tharaga Balachandran39810462020-06-16 09:34:34 -0400374int getMetadataState(void *buffer, uint32_t type) {
375 bool metadata_set[METADATA_SET_SIZE];
376 Error err;
377 if (IS_VENDOR_METADATA_TYPE(type)) {
378 err = get(buffer, QTI_VENDOR_METADATA_STATUS, &metadata_set);
379 } else {
380 err = get(buffer, QTI_STANDARD_METADATA_STATUS, &metadata_set);
381 }
382
383 if (err != Error::NONE) {
384 ALOGE("Unable to get metadata state");
385 return -1;
386 }
387
388 if (IS_VENDOR_METADATA_TYPE(type)) {
389 return metadata_set[GET_VENDOR_METADATA_STATUS_INDEX(type)];
Venkat Thogaru6b511cd2020-07-27 23:22:25 +0530390 } else if (GET_STANDARD_METADATA_STATUS_INDEX(type) < METADATA_SET_SIZE) {
Tharaga Balachandran39810462020-06-16 09:34:34 -0400391 return metadata_set[GET_STANDARD_METADATA_STATUS_INDEX(type)];
Venkat Thogaru6b511cd2020-07-27 23:22:25 +0530392 } else {
393 return -1;
Tharaga Balachandran39810462020-06-16 09:34:34 -0400394 }
395}
396
Tharaga Balachandrana069a7e2020-01-08 17:22:12 -0500397} // namespace qtigralloc