summaryrefslogtreecommitdiffstats
path: root/camera/device/3.2/default/convert.cpp
blob: 35676df3af22274f7fc50d4afea3c1764120b5eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "android.hardware.camera.device@3.2-convert-impl"
#include <android/log.h>

#include "include/convert.h"

namespace android {
namespace hardware {
namespace camera {
namespace device {
namespace V3_2 {
namespace implementation {

using ::android::hardware::graphics::common::V1_0::Dataspace;
using ::android::hardware::graphics::common::V1_0::PixelFormat;
using ::android::hardware::camera::device::V3_2::ConsumerUsageFlags;
using ::android::hardware::camera::device::V3_2::ProducerUsageFlags;

bool convertFromHidl(const CameraMetadata &src, const camera_metadata_t** dst) {
    if (src.size() == 0) {
        // Special case for null metadata
        *dst = nullptr;
        return true;
    }

    const uint8_t* data = src.data();
    // sanity check the size of CameraMetadata match underlying camera_metadata_t
    if (get_camera_metadata_size((camera_metadata_t*)data) != src.size()) {
        ALOGE("%s: input CameraMetadata is corrupt!", __FUNCTION__);
        return false;
    }
    *dst = (camera_metadata_t*) data;
    return true;
}

// Note: existing data in dst will be gone. Caller still owns the memory of src
void convertToHidl(const camera_metadata_t *src, CameraMetadata* dst) {
    if (src == nullptr) {
        return;
    }
    size_t size = get_camera_metadata_size(src);
    dst->setToExternal((uint8_t *) src, size);
    return;
}

void convertFromHidl(const Stream &src, Camera3Stream* dst) {
    dst->mId = src.id;
    dst->stream_type = (int) src.streamType;
    dst->width = src.width;
    dst->height = src.height;
    dst->format = (int) src.format;
    dst->data_space = (android_dataspace_t) src.dataSpace;
    dst->rotation = (int) src.rotation;
    dst->usage = (uint32_t) src.usage;
    // Fields to be filled by HAL (max_buffers, priv) are initialized to 0
    dst->max_buffers = 0;
    dst->priv = 0;
    return;
}

void convertToHidl(const Camera3Stream* src, HalStream* dst) {
    dst->id = src->mId;
    dst->overrideFormat = (PixelFormat) src->format;
    dst->maxBuffers = src->max_buffers;
    if (src->stream_type == CAMERA3_STREAM_OUTPUT) {
        dst->consumerUsage = (ConsumerUsageFlags) 0;
        dst->producerUsage = (ProducerUsageFlags) src->usage;
    } else if (src->stream_type == CAMERA3_STREAM_INPUT) {
        dst->producerUsage = (ProducerUsageFlags) 0;
        dst->consumerUsage = (ConsumerUsageFlags) src->usage;
    } else {
        //Should not reach here per current HIDL spec, but we might end up adding
        // bi-directional stream to HIDL.
        ALOGW("%s: Stream type %d is not currently supported!",
                __FUNCTION__, src->stream_type);
    }
}

void convertToHidl(const camera3_stream_configuration_t& src, HalStreamConfiguration* dst) {
    dst->streams.resize(src.num_streams);
    for (uint32_t i = 0; i < src.num_streams; i++) {
        convertToHidl(static_cast<Camera3Stream*>(src.streams[i]), &dst->streams[i]);
    }
    return;
}

void convertFromHidl(
        buffer_handle_t* bufPtr, BufferStatus status, camera3_stream_t* stream, int acquireFence,
        camera3_stream_buffer_t* dst) {
    dst->stream = stream;
    dst->buffer = bufPtr;
    dst->status = (int) status;
    dst->acquire_fence = acquireFence;
    dst->release_fence = -1; // meant for HAL to fill in
}

void convertToHidl(const camera3_notify_msg* src, NotifyMsg* dst) {
    dst->type = (MsgType) src->type;
    switch (src->type) {
        case CAMERA3_MSG_ERROR:
            {
                // The camera3_stream_t* must be the same as what wrapper HAL passed to conventional
                // HAL, or the ID lookup will return garbage. Caller should validate the ID here is
                // indeed one of active stream IDs
                Camera3Stream* stream = static_cast<Camera3Stream*>(
                        src->message.error.error_stream);
                dst->msg.error.frameNumber = src->message.error.frame_number;
                dst->msg.error.errorStreamId = (stream != nullptr) ? stream->mId : -1;
                dst->msg.error.errorCode = (ErrorCode) src->message.error.error_code;
            }
            break;
        case CAMERA3_MSG_SHUTTER:
            dst->msg.shutter.frameNumber = src->message.shutter.frame_number;
            dst->msg.shutter.timestamp = src->message.shutter.timestamp;
            break;
        default:
            ALOGE("%s: HIDL type converion failed. Unknown msg type 0x%x",
                    __FUNCTION__, src->type);
    }
    return;
}

}  // namespace implementation
}  // namespace V3_2
}  // namespace device
}  // namespace camera
}  // namespace hardware
}  // namespace android