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_EVSCAMERA_H
18 #define ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_0_EVSCAMERA_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 
26 
27 namespace android {
28 namespace hardware {
29 namespace automotive {
30 namespace evs {
31 namespace V1_0 {
32 namespace implementation {
33 
34 
35 // From EvsEnumerator.h
36 class EvsEnumerator;
37 
38 
39 class EvsCamera : public IEvsCamera {
40 public:
41     // Methods from ::android::hardware::automotive::evs::V1_0::IEvsCamera follow.
42     Return<void> getCameraInfo(getCameraInfo_cb _hidl_cb)  override;
43     Return <EvsResult> setMaxFramesInFlight(uint32_t bufferCount) override;
44     Return <EvsResult> startVideoStream(const ::android::sp<IEvsCameraStream>& stream) override;
45     Return<void> doneWithFrame(const BufferDesc& buffer) override;
46     Return<void> stopVideoStream() override;
47     Return <int32_t> getExtendedInfo(uint32_t opaqueIdentifier) override;
48     Return <EvsResult> setExtendedInfo(uint32_t opaqueIdentifier, int32_t opaqueValue) override;
49 
50     // Implementation details
51     EvsCamera(const char *id);
52     virtual ~EvsCamera() override;
53     void forceShutdown();   // This gets called if another caller "steals" ownership of the camera
54 
getDesc()55     const CameraDesc& getDesc() { return mDescription; };
56 
57     static const char kCameraName_Backup[];
58 
59 private:
60     // These three functions are expected to be called while mAccessLock is held
61     bool setAvailableFrames_Locked(unsigned bufferCount);
62     unsigned increaseAvailableFrames_Locked(unsigned numToAdd);
63     unsigned decreaseAvailableFrames_Locked(unsigned numToRemove);
64 
65     void generateFrames();
66     void fillTestFrame(const BufferDesc& buff);
67 
68     sp<EvsEnumerator> mEnumerator;  // The enumerator object that created this camera
69 
70     CameraDesc mDescription = {};   // The properties of this camera
71 
72     std::thread mCaptureThread;     // The thread we'll use to synthesize frames
73 
74     uint32_t mWidth  = 0;       // Horizontal pixel count in the buffers
75     uint32_t mHeight = 0;       // Vertical pixel count in the buffers
76     uint32_t mFormat = 0;       // Values from android_pixel_format_t [TODO: YUV?  Leave opaque?]
77     uint32_t mUsage  = 0;       // Values from from Gralloc.h
78     uint32_t mStride = 0;       // Bytes per line in the buffers
79 
80     sp <IEvsCameraStream> mStream = nullptr;  // The callback used to deliver each frame
81 
82     struct BufferRecord {
83         buffer_handle_t handle;
84         bool inUse;
85 
BufferRecordBufferRecord86         explicit BufferRecord(buffer_handle_t h) : handle(h), inUse(false) {};
87     };
88 
89     std::vector <BufferRecord> mBuffers;           // Graphics buffers to transfer images
90     unsigned mFramesAllowed;     // How many buffers are we currently using
91     unsigned mFramesInUse;       // How many buffers are currently outstanding
92 
93     enum StreamStateValues {
94         STOPPED,
95         RUNNING,
96         STOPPING,
97         DEAD,
98     };
99     StreamStateValues mStreamState;
100 
101     // Synchronization necessary to deconflict mCaptureThread from the main service thread
102     std::mutex mAccessLock;
103 };
104 
105 } // namespace implementation
106 } // namespace V1_0
107 } // namespace evs
108 } // namespace automotive
109 } // namespace hardware
110 } // namespace android
111 
112 #endif  // ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_0_EVSCAMERA_H
113