blob: 207560783fb2e66d17a792290f6bc4fed51833e1 [file] [log] [blame]
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "android.hardware.camera.device@3.2-convert-impl"
Steven Moreland4e7a3072017-04-06 12:15:23 -070018#include <log/log.h>
Ravneet Dhanjal2f6f8742023-01-04 21:06:52 +000019#include <system/camera_metadata.h>
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070020
21#include "include/convert.h"
22
23namespace android {
24namespace hardware {
25namespace camera {
26namespace device {
27namespace V3_2 {
28namespace implementation {
29
30using ::android::hardware::graphics::common::V1_0::Dataspace;
31using ::android::hardware::graphics::common::V1_0::PixelFormat;
Chia-I Wu79d13ff2017-03-31 12:48:11 -070032using ::android::hardware::camera::device::V3_2::BufferUsageFlags;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070033
34bool convertFromHidl(const CameraMetadata &src, const camera_metadata_t** dst) {
35 if (src.size() == 0) {
36 // Special case for null metadata
37 *dst = nullptr;
38 return true;
39 }
40
41 const uint8_t* data = src.data();
Eino-Ville Talvalad6346872020-06-19 15:25:01 -070042 // check that the size of CameraMetadata match underlying camera_metadata_t
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070043 if (get_camera_metadata_size((camera_metadata_t*)data) != src.size()) {
44 ALOGE("%s: input CameraMetadata is corrupt!", __FUNCTION__);
45 return false;
46 }
Ravneet Dhanjal2f6f8742023-01-04 21:06:52 +000047
48 if (validate_camera_metadata_structure((camera_metadata_t*)data, /*expected_size=*/NULL) !=
49 OK) {
50 ALOGE("%s: Failed to validate the metadata structure", __FUNCTION__);
51 return false;
52 }
53
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070054 *dst = (camera_metadata_t*) data;
55 return true;
56}
57
58// Note: existing data in dst will be gone. Caller still owns the memory of src
59void convertToHidl(const camera_metadata_t *src, CameraMetadata* dst) {
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -080060 if (src == nullptr) {
61 return;
62 }
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070063 size_t size = get_camera_metadata_size(src);
64 dst->setToExternal((uint8_t *) src, size);
65 return;
66}
67
68void convertFromHidl(const Stream &src, Camera3Stream* dst) {
69 dst->mId = src.id;
70 dst->stream_type = (int) src.streamType;
71 dst->width = src.width;
72 dst->height = src.height;
73 dst->format = (int) src.format;
74 dst->data_space = (android_dataspace_t) src.dataSpace;
75 dst->rotation = (int) src.rotation;
Yin-Chia Yehc25c54f2017-04-04 13:00:35 -070076 dst->usage = (uint32_t) src.usage;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070077 // Fields to be filled by HAL (max_buffers, priv) are initialized to 0
78 dst->max_buffers = 0;
79 dst->priv = 0;
80 return;
81}
82
83void convertToHidl(const Camera3Stream* src, HalStream* dst) {
84 dst->id = src->mId;
85 dst->overrideFormat = (PixelFormat) src->format;
86 dst->maxBuffers = src->max_buffers;
87 if (src->stream_type == CAMERA3_STREAM_OUTPUT) {
Chia-I Wu79d13ff2017-03-31 12:48:11 -070088 dst->consumerUsage = (BufferUsageFlags)0;
89 dst->producerUsage = (BufferUsageFlags)src->usage;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070090 } else if (src->stream_type == CAMERA3_STREAM_INPUT) {
Chia-I Wu79d13ff2017-03-31 12:48:11 -070091 dst->producerUsage = (BufferUsageFlags)0;
92 dst->consumerUsage = (BufferUsageFlags)src->usage;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070093 } else {
94 //Should not reach here per current HIDL spec, but we might end up adding
95 // bi-directional stream to HIDL.
96 ALOGW("%s: Stream type %d is not currently supported!",
97 __FUNCTION__, src->stream_type);
98 }
99}
100
101void convertToHidl(const camera3_stream_configuration_t& src, HalStreamConfiguration* dst) {
102 dst->streams.resize(src.num_streams);
103 for (uint32_t i = 0; i < src.num_streams; i++) {
104 convertToHidl(static_cast<Camera3Stream*>(src.streams[i]), &dst->streams[i]);
105 }
106 return;
107}
108
109void convertFromHidl(
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800110 buffer_handle_t* bufPtr, BufferStatus status, camera3_stream_t* stream, int acquireFence,
111 camera3_stream_buffer_t* dst) {
112 dst->stream = stream;
113 dst->buffer = bufPtr;
114 dst->status = (int) status;
115 dst->acquire_fence = acquireFence;
116 dst->release_fence = -1; // meant for HAL to fill in
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700117}
118
119void convertToHidl(const camera3_notify_msg* src, NotifyMsg* dst) {
120 dst->type = (MsgType) src->type;
121 switch (src->type) {
122 case CAMERA3_MSG_ERROR:
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800123 {
124 // The camera3_stream_t* must be the same as what wrapper HAL passed to conventional
125 // HAL, or the ID lookup will return garbage. Caller should validate the ID here is
126 // indeed one of active stream IDs
127 Camera3Stream* stream = static_cast<Camera3Stream*>(
128 src->message.error.error_stream);
129 dst->msg.error.frameNumber = src->message.error.frame_number;
130 dst->msg.error.errorStreamId = (stream != nullptr) ? stream->mId : -1;
131 dst->msg.error.errorCode = (ErrorCode) src->message.error.error_code;
132 }
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700133 break;
134 case CAMERA3_MSG_SHUTTER:
135 dst->msg.shutter.frameNumber = src->message.shutter.frame_number;
136 dst->msg.shutter.timestamp = src->message.shutter.timestamp;
137 break;
138 default:
139 ALOGE("%s: HIDL type converion failed. Unknown msg type 0x%x",
140 __FUNCTION__, src->type);
141 }
142 return;
143}
144
145} // namespace implementation
146} // namespace V3_2
147} // namespace device
148} // namespace camera
149} // namespace hardware
150} // namespace android