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#ifndef ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_0_EVSV4LCAMERA_H
18#define ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_0_EVSV4LCAMERA_H
19
20#include <android/hardware/automotive/evs/1.0/types.h>
21#include <android/hardware/automotive/evs/1.0/IEvsCamera.h>
22#include <ui/GraphicBuffer.h>
23
24#include <thread>
25#include <functional>
26
27#include "VideoCapture.h"
28
29
30namespace android {
31namespace hardware {
32namespace automotive {
33namespace evs {
34namespace V1_0 {
35namespace implementation {
36
37
38// From EvsEnumerator.h
39class EvsEnumerator;
40
41
42class EvsV4lCamera : public IEvsCamera {
43public:
44    // Methods from ::android::hardware::automotive::evs::V1_0::IEvsCamera follow.
45    Return<void> getCameraInfo(getCameraInfo_cb _hidl_cb)  override;
46    Return <EvsResult> setMaxFramesInFlight(uint32_t bufferCount) override;
47    Return <EvsResult> startVideoStream(const ::android::sp<IEvsCameraStream>& stream) override;
48    Return<void> doneWithFrame(const BufferDesc& buffer) override;
49    Return<void> stopVideoStream() override;
50    Return <int32_t> getExtendedInfo(uint32_t opaqueIdentifier) override;
51    Return <EvsResult> setExtendedInfo(uint32_t opaqueIdentifier, int32_t opaqueValue) override;
52
53    // Implementation details
54    EvsV4lCamera(const char *deviceName);
55    virtual ~EvsV4lCamera() override;
56    void shutdown();
57
58    const CameraDesc& getDesc() { return mDescription; };
59
60private:
61    // These three functions are expected to be called while mAccessLock is held
62    bool setAvailableFrames_Locked(unsigned bufferCount);
63    unsigned increaseAvailableFrames_Locked(unsigned numToAdd);
64    unsigned decreaseAvailableFrames_Locked(unsigned numToRemove);
65
66    void forwardFrame(imageBuffer* tgt, void* data);
67
68    sp <IEvsCameraStream> mStream = nullptr;  // The callback used to deliver each frame
69
70    VideoCapture          mVideo;   // Interface to the v4l device
71
72    CameraDesc mDescription = {};   // The properties of this camera
73    uint32_t mFormat = 0;           // Values from android_pixel_format_t
74    uint32_t mUsage  = 0;           // Values from from Gralloc.h
75    uint32_t mStride = 0;           // Pixels per row (may be greater than image width)
76
77    struct BufferRecord {
78        buffer_handle_t handle;
79        bool inUse;
80
81        explicit BufferRecord(buffer_handle_t h) : handle(h), inUse(false) {};
82    };
83
84    std::vector <BufferRecord> mBuffers;    // Graphics buffers to transfer images
85    unsigned mFramesAllowed;                // How many buffers are we currently using
86    unsigned mFramesInUse;                  // How many buffers are currently outstanding
87
88    // Which format specific function we need to use to move camera imagery into our output buffers
89    void(*mFillBufferFromVideo)(const BufferDesc& tgtBuff, uint8_t* tgt,
90                                void* imgData, unsigned imgStride);
91
92    // Synchronization necessary to deconflict the capture thread from the main service thread
93    // Note that the service interface remains single threaded (ie: not reentrant)
94    std::mutex mAccessLock;
95};
96
97} // namespace implementation
98} // namespace V1_0
99} // namespace evs
100} // namespace automotive
101} // namespace hardware
102} // namespace android
103
104#endif  // ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_0_EVSV4LCAMERA_H
105