blob: c99d9037c39d2fbb96880e6655b5f8d69ffe7998 [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"
Eino-Ville Talvala2d80c0d2017-03-23 15:39:12 -070018#include <inttypes.h>
19
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070020#include <android/log.h>
21
Eino-Ville Talvala2d80c0d2017-03-23 15:39:12 -070022#include <grallocusage/GrallocUsageConversion.h>
23
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070024#include "include/convert.h"
25
26namespace android {
27namespace hardware {
28namespace camera {
29namespace device {
30namespace V3_2 {
31namespace implementation {
32
33using ::android::hardware::graphics::common::V1_0::Dataspace;
34using ::android::hardware::graphics::common::V1_0::PixelFormat;
Eino-Ville Talvala2d80c0d2017-03-23 15:39:12 -070035using ::android::hardware::graphics::allocator::V2_0::ConsumerUsage;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070036using ::android::hardware::camera::device::V3_2::ConsumerUsageFlags;
37using ::android::hardware::camera::device::V3_2::ProducerUsageFlags;
38
39bool convertFromHidl(const CameraMetadata &src, const camera_metadata_t** dst) {
40 if (src.size() == 0) {
41 // Special case for null metadata
42 *dst = nullptr;
43 return true;
44 }
45
46 const uint8_t* data = src.data();
47 // sanity check the size of CameraMetadata match underlying camera_metadata_t
48 if (get_camera_metadata_size((camera_metadata_t*)data) != src.size()) {
49 ALOGE("%s: input CameraMetadata is corrupt!", __FUNCTION__);
50 return false;
51 }
52 *dst = (camera_metadata_t*) data;
53 return true;
54}
55
56// Note: existing data in dst will be gone. Caller still owns the memory of src
57void convertToHidl(const camera_metadata_t *src, CameraMetadata* dst) {
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -080058 if (src == nullptr) {
59 return;
60 }
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070061 size_t size = get_camera_metadata_size(src);
62 dst->setToExternal((uint8_t *) src, size);
63 return;
64}
65
66void convertFromHidl(const Stream &src, Camera3Stream* dst) {
67 dst->mId = src.id;
68 dst->stream_type = (int) src.streamType;
69 dst->width = src.width;
70 dst->height = src.height;
71 dst->format = (int) src.format;
72 dst->data_space = (android_dataspace_t) src.dataSpace;
73 dst->rotation = (int) src.rotation;
Eino-Ville Talvala2d80c0d2017-03-23 15:39:12 -070074 dst->usage = convertFromHidlUsage(src.usage);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070075 // Fields to be filled by HAL (max_buffers, priv) are initialized to 0
76 dst->max_buffers = 0;
77 dst->priv = 0;
78 return;
79}
80
Eino-Ville Talvala2d80c0d2017-03-23 15:39:12 -070081uint32_t convertFromHidlUsage(ConsumerUsageFlags usage) {
82 uint32_t dstUsage = 0;
83 dstUsage = ::android_convertGralloc1To0Usage(/*producerUsage*/ 0, usage);
84 // Compatibility workaround - add HW_CAMERA_ZSL when ConsumerUsage.CAMERA is set, to
85 // match pre-HIDL expected usage flags
86 if ( (usage & ConsumerUsage::CAMERA) == static_cast<uint64_t>(ConsumerUsage::CAMERA)) {
87 dstUsage |= GRALLOC_USAGE_HW_CAMERA_ZSL;
88 }
89 return dstUsage;
90}
91
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070092void convertToHidl(const Camera3Stream* src, HalStream* dst) {
93 dst->id = src->mId;
94 dst->overrideFormat = (PixelFormat) src->format;
95 dst->maxBuffers = src->max_buffers;
Eino-Ville Talvala2d80c0d2017-03-23 15:39:12 -070096 ConsumerUsageFlags consumerUsage;
97 ProducerUsageFlags producerUsage;
98 ::android_convertGralloc0To1Usage(src->usage, &producerUsage, &consumerUsage);
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -070099 if (src->stream_type == CAMERA3_STREAM_OUTPUT) {
100 dst->consumerUsage = (ConsumerUsageFlags) 0;
Eino-Ville Talvala2d80c0d2017-03-23 15:39:12 -0700101 dst->producerUsage = producerUsage;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700102 } else if (src->stream_type == CAMERA3_STREAM_INPUT) {
103 dst->producerUsage = (ProducerUsageFlags) 0;
Eino-Ville Talvala2d80c0d2017-03-23 15:39:12 -0700104 dst->consumerUsage = consumerUsage;
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700105 } else {
106 //Should not reach here per current HIDL spec, but we might end up adding
107 // bi-directional stream to HIDL.
108 ALOGW("%s: Stream type %d is not currently supported!",
109 __FUNCTION__, src->stream_type);
110 }
111}
112
113void convertToHidl(const camera3_stream_configuration_t& src, HalStreamConfiguration* dst) {
114 dst->streams.resize(src.num_streams);
115 for (uint32_t i = 0; i < src.num_streams; i++) {
116 convertToHidl(static_cast<Camera3Stream*>(src.streams[i]), &dst->streams[i]);
117 }
118 return;
119}
120
121void convertFromHidl(
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800122 buffer_handle_t* bufPtr, BufferStatus status, camera3_stream_t* stream, int acquireFence,
123 camera3_stream_buffer_t* dst) {
124 dst->stream = stream;
125 dst->buffer = bufPtr;
126 dst->status = (int) status;
127 dst->acquire_fence = acquireFence;
128 dst->release_fence = -1; // meant for HAL to fill in
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700129}
130
131void convertToHidl(const camera3_notify_msg* src, NotifyMsg* dst) {
132 dst->type = (MsgType) src->type;
133 switch (src->type) {
134 case CAMERA3_MSG_ERROR:
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800135 {
136 // The camera3_stream_t* must be the same as what wrapper HAL passed to conventional
137 // HAL, or the ID lookup will return garbage. Caller should validate the ID here is
138 // indeed one of active stream IDs
139 Camera3Stream* stream = static_cast<Camera3Stream*>(
140 src->message.error.error_stream);
141 dst->msg.error.frameNumber = src->message.error.frame_number;
142 dst->msg.error.errorStreamId = (stream != nullptr) ? stream->mId : -1;
143 dst->msg.error.errorCode = (ErrorCode) src->message.error.error_code;
144 }
Yin-Chia Yehfaef8f92016-10-31 12:53:56 -0700145 break;
146 case CAMERA3_MSG_SHUTTER:
147 dst->msg.shutter.frameNumber = src->message.shutter.frame_number;
148 dst->msg.shutter.timestamp = src->message.shutter.timestamp;
149 break;
150 default:
151 ALOGE("%s: HIDL type converion failed. Unknown msg type 0x%x",
152 __FUNCTION__, src->type);
153 }
154 return;
155}
156
157} // namespace implementation
158} // namespace V3_2
159} // namespace device
160} // namespace camera
161} // namespace hardware
162} // namespace android