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 <log/log.h>
19 #include <system/camera_metadata.h>
20
21 #include "include/convert.h"
22
23 namespace android {
24 namespace hardware {
25 namespace camera {
26 namespace device {
27 namespace V3_2 {
28 namespace implementation {
29
30 using ::android::hardware::graphics::common::V1_0::Dataspace;
31 using ::android::hardware::graphics::common::V1_0::PixelFormat;
32 using ::android::hardware::camera::device::V3_2::BufferUsageFlags;
33
convertFromHidl(const CameraMetadata & src,const camera_metadata_t ** dst)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 // check that 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
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
54 *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
convertToHidl(const camera_metadata_t * src,CameraMetadata * dst)59 void convertToHidl(const camera_metadata_t *src, CameraMetadata* dst) {
60 if (src == nullptr) {
61 return;
62 }
63 size_t size = get_camera_metadata_size(src);
64 dst->setToExternal((uint8_t *) src, size);
65 return;
66 }
67
convertFromHidl(const Stream & src,Camera3Stream * dst)68 void 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;
76 dst->usage = (uint32_t) src.usage;
77 // 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
convertToHidl(const Camera3Stream * src,HalStream * dst)83 void 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) {
88 dst->consumerUsage = (BufferUsageFlags)0;
89 dst->producerUsage = (BufferUsageFlags)src->usage;
90 } else if (src->stream_type == CAMERA3_STREAM_INPUT) {
91 dst->producerUsage = (BufferUsageFlags)0;
92 dst->consumerUsage = (BufferUsageFlags)src->usage;
93 } 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
convertToHidl(const camera3_stream_configuration_t & src,HalStreamConfiguration * dst)101 void 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
convertFromHidl(buffer_handle_t * bufPtr,BufferStatus status,camera3_stream_t * stream,int acquireFence,camera3_stream_buffer_t * dst)109 void convertFromHidl(
110 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
117 }
118
convertToHidl(const camera3_notify_msg * src,NotifyMsg * dst)119 void convertToHidl(const camera3_notify_msg* src, NotifyMsg* dst) {
120 dst->type = (MsgType) src->type;
121 switch (src->type) {
122 case CAMERA3_MSG_ERROR:
123 {
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 }
133 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
151