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