blob: d878deb03dadd77b70041bd99f2582c975929d7a [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>
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070019
20#include "include/convert.h"
21
22namespace android {
23namespace hardware {
24namespace camera {
25namespace device {
26namespace V3_2 {
27namespace implementation {
28
29using ::android::hardware::graphics::common::V1_0::Dataspace;
30using ::android::hardware::graphics::common::V1_0::PixelFormat;
Chia-I Wu79d13ff2017-03-31 12:48:11 -070031using ::android::hardware::camera::device::V3_2::BufferUsageFlags;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070032
33bool convertFromHidl(const CameraMetadata &src, const camera_metadata_t** dst) {
34 if (src.size() == 0) {
35 // Special case for null metadata
36 *dst = nullptr;
37 return true;
38 }
39
40 const uint8_t* data = src.data();
41 // sanity check the size of CameraMetadata match underlying camera_metadata_t
42 if (get_camera_metadata_size((camera_metadata_t*)data) != src.size()) {
43 ALOGE("%s: input CameraMetadata is corrupt!", __FUNCTION__);
44 return false;
45 }
46 *dst = (camera_metadata_t*) data;
47 return true;
48}
49
50// Note: existing data in dst will be gone. Caller still owns the memory of src
51void convertToHidl(const camera_metadata_t *src, CameraMetadata* dst) {
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -080052 if (src == nullptr) {
53 return;
54 }
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070055 size_t size = get_camera_metadata_size(src);
56 dst->setToExternal((uint8_t *) src, size);
57 return;
58}
59
60void convertFromHidl(const Stream &src, Camera3Stream* dst) {
61 dst->mId = src.id;
62 dst->stream_type = (int) src.streamType;
63 dst->width = src.width;
64 dst->height = src.height;
65 dst->format = (int) src.format;
66 dst->data_space = (android_dataspace_t) src.dataSpace;
67 dst->rotation = (int) src.rotation;
Yin-Chia Yehc25c54f2017-04-04 13:00:35 -070068 dst->usage = (uint32_t) src.usage;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070069 // Fields to be filled by HAL (max_buffers, priv) are initialized to 0
70 dst->max_buffers = 0;
71 dst->priv = 0;
72 return;
73}
74
75void convertToHidl(const Camera3Stream* src, HalStream* dst) {
76 dst->id = src->mId;
77 dst->overrideFormat = (PixelFormat) src->format;
78 dst->maxBuffers = src->max_buffers;
79 if (src->stream_type == CAMERA3_STREAM_OUTPUT) {
Chia-I Wu79d13ff2017-03-31 12:48:11 -070080 dst->consumerUsage = (BufferUsageFlags)0;
81 dst->producerUsage = (BufferUsageFlags)src->usage;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070082 } else if (src->stream_type == CAMERA3_STREAM_INPUT) {
Chia-I Wu79d13ff2017-03-31 12:48:11 -070083 dst->producerUsage = (BufferUsageFlags)0;
84 dst->consumerUsage = (BufferUsageFlags)src->usage;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070085 } else {
86 //Should not reach here per current HIDL spec, but we might end up adding
87 // bi-directional stream to HIDL.
88 ALOGW("%s: Stream type %d is not currently supported!",
89 __FUNCTION__, src->stream_type);
90 }
91}
92
93void convertToHidl(const camera3_stream_configuration_t& src, HalStreamConfiguration* dst) {
94 dst->streams.resize(src.num_streams);
95 for (uint32_t i = 0; i < src.num_streams; i++) {
96 convertToHidl(static_cast<Camera3Stream*>(src.streams[i]), &dst->streams[i]);
97 }
98 return;
99}
100
101void convertFromHidl(
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800102 buffer_handle_t* bufPtr, BufferStatus status, camera3_stream_t* stream, int acquireFence,
103 camera3_stream_buffer_t* dst) {
104 dst->stream = stream;
105 dst->buffer = bufPtr;
106 dst->status = (int) status;
107 dst->acquire_fence = acquireFence;
108 dst->release_fence = -1; // meant for HAL to fill in
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700109}
110
111void convertToHidl(const camera3_notify_msg* src, NotifyMsg* dst) {
112 dst->type = (MsgType) src->type;
113 switch (src->type) {
114 case CAMERA3_MSG_ERROR:
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800115 {
116 // The camera3_stream_t* must be the same as what wrapper HAL passed to conventional
117 // HAL, or the ID lookup will return garbage. Caller should validate the ID here is
118 // indeed one of active stream IDs
119 Camera3Stream* stream = static_cast<Camera3Stream*>(
120 src->message.error.error_stream);
121 dst->msg.error.frameNumber = src->message.error.frame_number;
122 dst->msg.error.errorStreamId = (stream != nullptr) ? stream->mId : -1;
123 dst->msg.error.errorCode = (ErrorCode) src->message.error.error_code;
124 }
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700125 break;
126 case CAMERA3_MSG_SHUTTER:
127 dst->msg.shutter.frameNumber = src->message.shutter.frame_number;
128 dst->msg.shutter.timestamp = src->message.shutter.timestamp;
129 break;
130 default:
131 ALOGE("%s: HIDL type converion failed. Unknown msg type 0x%x",
132 __FUNCTION__, src->type);
133 }
134 return;
135}
136
137} // namespace implementation
138} // namespace V3_2
139} // namespace device
140} // namespace camera
141} // namespace hardware
142} // namespace android