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