1b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph/*
2b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph * Copyright (C) 2016 The Android Open Source Project
3b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph *
4b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph * Licensed under the Apache License, Version 2.0 (the "License");
5b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph * you may not use this file except in compliance with the License.
6b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph * You may obtain a copy of the License at
7b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph *
8b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph *      http://www.apache.org/licenses/LICENSE-2.0
9b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph *
10b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph * Unless required by applicable law or agreed to in writing, software
11b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph * distributed under the License is distributed on an "AS IS" BASIS,
12b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph * See the License for the specific language governing permissions and
14b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph * limitations under the License.
15b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph */
16b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
17b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph#ifndef ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_0_EVSV4LCAMERA_H
18b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph#define ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_0_EVSV4LCAMERA_H
19b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
20b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph#include <android/hardware/automotive/evs/1.0/types.h>
21b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph#include <android/hardware/automotive/evs/1.0/IEvsCamera.h>
22b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph#include <ui/GraphicBuffer.h>
23b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
24b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph#include <thread>
25b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph#include <functional>
26b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
27b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph#include "VideoCapture.h"
28b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
29b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
30b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolphnamespace android {
31b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolphnamespace hardware {
32b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolphnamespace automotive {
33b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolphnamespace evs {
34b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolphnamespace V1_0 {
35b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolphnamespace implementation {
36b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
37b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
38b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph// From EvsEnumerator.h
39b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolphclass EvsEnumerator;
40b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
41b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
42b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolphclass EvsV4lCamera : public IEvsCamera {
43b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolphpublic:
44b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    // Methods from ::android::hardware::automotive::evs::V1_0::IEvsCamera follow.
45b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    Return<void> getCameraInfo(getCameraInfo_cb _hidl_cb)  override;
46b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    Return <EvsResult> setMaxFramesInFlight(uint32_t bufferCount) override;
47b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    Return <EvsResult> startVideoStream(const ::android::sp<IEvsCameraStream>& stream) override;
48b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    Return<void> doneWithFrame(const BufferDesc& buffer) override;
49b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    Return<void> stopVideoStream() override;
50b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    Return <int32_t> getExtendedInfo(uint32_t opaqueIdentifier) override;
51b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    Return <EvsResult> setExtendedInfo(uint32_t opaqueIdentifier, int32_t opaqueValue) override;
52b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
53b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    // Implementation details
54b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    EvsV4lCamera(const char *deviceName);
55b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    virtual ~EvsV4lCamera() override;
56b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    void shutdown();
57b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
58b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    const CameraDesc& getDesc() { return mDescription; };
59b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
60b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolphprivate:
61b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    // These three functions are expected to be called while mAccessLock is held
62b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    bool setAvailableFrames_Locked(unsigned bufferCount);
63b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    unsigned increaseAvailableFrames_Locked(unsigned numToAdd);
64b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    unsigned decreaseAvailableFrames_Locked(unsigned numToRemove);
65b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
66b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    void forwardFrame(imageBuffer* tgt, void* data);
67b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
68b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    sp <IEvsCameraStream> mStream = nullptr;  // The callback used to deliver each frame
69b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
70b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    VideoCapture          mVideo;   // Interface to the v4l device
71b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
72b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    CameraDesc mDescription = {};   // The properties of this camera
73b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    uint32_t mFormat = 0;           // Values from android_pixel_format_t
74b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    uint32_t mUsage  = 0;           // Values from from Gralloc.h
75b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    uint32_t mStride = 0;           // Pixels per row (may be greater than image width)
76b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
77b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    struct BufferRecord {
78b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph        buffer_handle_t handle;
79b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph        bool inUse;
80b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
81b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph        explicit BufferRecord(buffer_handle_t h) : handle(h), inUse(false) {};
82b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    };
83b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
84b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    std::vector <BufferRecord> mBuffers;    // Graphics buffers to transfer images
85b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    unsigned mFramesAllowed;                // How many buffers are we currently using
86b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    unsigned mFramesInUse;                  // How many buffers are currently outstanding
87b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
88b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    // Which format specific function we need to use to move camera imagery into our output buffers
89b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    void(*mFillBufferFromVideo)(const BufferDesc& tgtBuff, uint8_t* tgt,
90b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph                                void* imgData, unsigned imgStride);
91b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
92b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    // Synchronization necessary to deconflict the capture thread from the main service thread
93b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    // Note that the service interface remains single threaded (ie: not reentrant)
94b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph    std::mutex mAccessLock;
95b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph};
96b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
97b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph} // namespace implementation
98b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph} // namespace V1_0
99b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph} // namespace evs
100b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph} // namespace automotive
101b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph} // namespace hardware
102b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph} // namespace android
103b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph
104b342cb163393317d8af77339ff8ff5e921bd4a8fScott Randolph#endif  // ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_0_EVSV4LCAMERA_H
105