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 
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::BufferUsageFlags;
32 
convertFromHidl(const CameraMetadata & src,const camera_metadata_t ** dst)33 bool 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
convertToHidl(const camera_metadata_t * src,CameraMetadata * dst)51 void convertToHidl(const camera_metadata_t *src, CameraMetadata* dst) {
52     if (src == nullptr) {
53         return;
54     }
55     size_t size = get_camera_metadata_size(src);
56     dst->setToExternal((uint8_t *) src, size);
57     return;
58 }
59 
convertFromHidl(const Stream & src,Camera3Stream * dst)60 void 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;
68     dst->usage = (uint32_t) src.usage;
69     // 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 
convertToHidl(const Camera3Stream * src,HalStream * dst)75 void 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) {
80         dst->consumerUsage = (BufferUsageFlags)0;
81         dst->producerUsage = (BufferUsageFlags)src->usage;
82     } else if (src->stream_type == CAMERA3_STREAM_INPUT) {
83         dst->producerUsage = (BufferUsageFlags)0;
84         dst->consumerUsage = (BufferUsageFlags)src->usage;
85     } 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 
convertToHidl(const camera3_stream_configuration_t & src,HalStreamConfiguration * dst)93 void 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 
convertFromHidl(buffer_handle_t * bufPtr,BufferStatus status,camera3_stream_t * stream,int acquireFence,camera3_stream_buffer_t * dst)101 void convertFromHidl(
102         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
109 }
110 
convertToHidl(const camera3_notify_msg * src,NotifyMsg * dst)111 void convertToHidl(const camera3_notify_msg* src, NotifyMsg* dst) {
112     dst->type = (MsgType) src->type;
113     switch (src->type) {
114         case CAMERA3_MSG_ERROR:
115             {
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             }
125             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
143