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