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 CAR_EVS_APP_EVSSTATECONTROL_H
18 #define CAR_EVS_APP_EVSSTATECONTROL_H
19 
20 #include "StreamHandler.h"
21 #include "ConfigManager.h"
22 #include "RenderBase.h"
23 
24 #include <android/hardware/automotive/vehicle/2.0/IVehicle.h>
25 #include <android/hardware/automotive/evs/1.1/IEvsEnumerator.h>
26 #include <android/hardware/automotive/evs/1.1/IEvsDisplay.h>
27 #include <android/hardware/automotive/evs/1.1/IEvsCamera.h>
28 
29 #include <thread>
30 
31 
32 using namespace ::android::hardware::automotive::evs::V1_1;
33 using namespace ::android::hardware::automotive::vehicle::V2_0;
34 using ::android::hardware::Return;
35 using ::android::hardware::Void;
36 using ::android::hardware::hidl_vec;
37 using ::android::hardware::hidl_handle;
38 using ::android::sp;
39 using ::android::hardware::automotive::evs::V1_1::IEvsDisplay;
40 using ::android::hardware::camera::device::V3_2::Stream;
41 
42 
43 /*
44  * This class runs the main update loop for the EVS application.  It will sleep when it has
45  * nothing to do.  It provides a thread safe way for other threads to wake it and pass commands
46  * to it.
47  */
48 class EvsStateControl {
49 public:
50     EvsStateControl(android::sp <IVehicle>       pVnet,
51                     android::sp <IEvsEnumerator> pEvs,
52                     android::sp <IEvsDisplay>    pDisplay,
53                     const ConfigManager&         config);
54 
55     enum State {
56         OFF = 0,
57         REVERSE,
58         LEFT,
59         RIGHT,
60         PARKING,
61         NUM_STATES  // Must come last
62     };
63 
64     enum class Op {
65         EXIT,
66         CHECK_VEHICLE_STATE,
67         TOUCH_EVENT,
68     };
69 
70     struct Command {
71         Op          operation;
72         uint32_t    arg1;
73         uint32_t    arg2;
74     };
75 
76     // This spawns a new thread that is expected to run continuously
77     bool startUpdateLoop();
78 
79     // This stops a rendering thread
80     void terminateUpdateLoop();
81 
82     // Safe to be called from other threads
83     void postCommand(const Command& cmd, bool clear = false);
84 
85 private:
86     void updateLoop();
87     StatusCode invokeGet(VehiclePropValue *pRequestedPropValue);
88     bool selectStateForCurrentConditions();
89     bool configureEvsPipeline(State desiredState);  // Only call from one thread!
90 
91     sp<IVehicle>                mVehicle;
92     sp<IEvsEnumerator>          mEvs;
93     sp<IEvsDisplay>             mDisplay;
94     const ConfigManager&        mConfig;
95 
96     VehiclePropValue            mGearValue;
97     VehiclePropValue            mTurnSignalValue;
98 
99     State                       mCurrentState = OFF;
100 
101     // mCameraList is a redundant storage for camera device info, which is also
102     // stored in mCameraDescList and, however, not removed for backward
103     // compatibility.
104     std::vector<ConfigManager::CameraInfo>  mCameraList[NUM_STATES];
105     std::unique_ptr<RenderBase> mCurrentRenderer;
106     std::unique_ptr<RenderBase> mDesiredRenderer;
107     std::vector<CameraDesc>     mCameraDescList[NUM_STATES];
108 
109     std::thread                 mRenderThread;  // The thread that runs the main rendering loop
110 
111     // Other threads may want to spur us into action, so we provide a thread safe way to do that
112     std::mutex                  mLock;
113     std::condition_variable     mWakeSignal;
114     std::queue<Command>         mCommandQueue;
115 };
116 
117 
118 #endif //CAR_EVS_APP_EVSSTATECONTROL_H
119