Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 1 | /* |
| 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 Talvala | 2d80c0d | 2017-03-23 15:39:12 -0700 | [diff] [blame] | 18 | #include <inttypes.h> |
| 19 | |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 20 | #include <android/log.h> |
| 21 | |
Eino-Ville Talvala | 2d80c0d | 2017-03-23 15:39:12 -0700 | [diff] [blame] | 22 | #include <grallocusage/GrallocUsageConversion.h> |
| 23 | |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 24 | #include "include/convert.h" |
| 25 | |
| 26 | namespace android { |
| 27 | namespace hardware { |
| 28 | namespace camera { |
| 29 | namespace device { |
| 30 | namespace V3_2 { |
| 31 | namespace implementation { |
| 32 | |
| 33 | using ::android::hardware::graphics::common::V1_0::Dataspace; |
| 34 | using ::android::hardware::graphics::common::V1_0::PixelFormat; |
Eino-Ville Talvala | 2d80c0d | 2017-03-23 15:39:12 -0700 | [diff] [blame] | 35 | using ::android::hardware::graphics::allocator::V2_0::ConsumerUsage; |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 36 | using ::android::hardware::camera::device::V3_2::ConsumerUsageFlags; |
| 37 | using ::android::hardware::camera::device::V3_2::ProducerUsageFlags; |
| 38 | |
| 39 | bool 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 |
| 57 | void convertToHidl(const camera_metadata_t *src, CameraMetadata* dst) { |
Yin-Chia Yeh | 9c6dbd5 | 2016-12-22 14:55:02 -0800 | [diff] [blame] | 58 | if (src == nullptr) { |
| 59 | return; |
| 60 | } |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 61 | size_t size = get_camera_metadata_size(src); |
| 62 | dst->setToExternal((uint8_t *) src, size); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | void 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 Talvala | 2d80c0d | 2017-03-23 15:39:12 -0700 | [diff] [blame] | 74 | dst->usage = convertFromHidlUsage(src.usage); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 75 | // 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 Talvala | 2d80c0d | 2017-03-23 15:39:12 -0700 | [diff] [blame] | 81 | uint32_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 Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 92 | void 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 Talvala | 2d80c0d | 2017-03-23 15:39:12 -0700 | [diff] [blame] | 96 | ConsumerUsageFlags consumerUsage; |
| 97 | ProducerUsageFlags producerUsage; |
| 98 | ::android_convertGralloc0To1Usage(src->usage, &producerUsage, &consumerUsage); |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 99 | if (src->stream_type == CAMERA3_STREAM_OUTPUT) { |
| 100 | dst->consumerUsage = (ConsumerUsageFlags) 0; |
Eino-Ville Talvala | 2d80c0d | 2017-03-23 15:39:12 -0700 | [diff] [blame] | 101 | dst->producerUsage = producerUsage; |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 102 | } else if (src->stream_type == CAMERA3_STREAM_INPUT) { |
| 103 | dst->producerUsage = (ProducerUsageFlags) 0; |
Eino-Ville Talvala | 2d80c0d | 2017-03-23 15:39:12 -0700 | [diff] [blame] | 104 | dst->consumerUsage = consumerUsage; |
Yin-Chia Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 105 | } 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 | |
| 113 | void 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 | |
| 121 | void convertFromHidl( |
Yin-Chia Yeh | 9c6dbd5 | 2016-12-22 14:55:02 -0800 | [diff] [blame] | 122 | 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 Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void 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 Yeh | 9c6dbd5 | 2016-12-22 14:55:02 -0800 | [diff] [blame] | 135 | { |
| 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 Yeh | faef8f9 | 2016-10-31 12:53:56 -0700 | [diff] [blame] | 145 | 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 |