• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <aidl/android/hardware/automotive/vehicle/VehiclePropValues.h>
26 #include <android/hardware/automotive/evs/1.1/IEvsCamera.h>
27 #include <android/hardware/automotive/evs/1.1/IEvsDisplay.h>
28 #include <android/hardware/automotive/evs/1.1/IEvsEnumerator.h>
29 
30 #include <IVhalClient.h>
31 
32 #include <thread>
33 
34 using namespace ::android::hardware::automotive::evs::V1_1;
35 using ::android::hardware::Return;
36 using ::android::hardware::Void;
37 using ::android::hardware::hidl_vec;
38 using ::android::hardware::hidl_handle;
39 using ::android::sp;
40 using ::android::wp;
41 using ::android::hardware::automotive::evs::V1_1::IEvsDisplay;
42 using ::android::hardware::camera::device::V3_2::Stream;
43 
44 
45 /*
46  * This class runs the main update loop for the EVS application.  It will sleep when it has
47  * nothing to do.  It provides a thread safe way for other threads to wake it and pass commands
48  * to it.
49  */
50 class EvsStateControl {
51 public:
52     EvsStateControl(std::shared_ptr<android::frameworks::automotive::vhal::IVhalClient> pVnet,
53                     android::sp<IEvsEnumerator> pEvs, 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     aidl::android::hardware::automotive::vehicle::StatusCode invokeGet(
89             aidl::android::hardware::automotive::vehicle::VehiclePropValue* pRequestedPropValue);
90     bool selectStateForCurrentConditions();
91     bool configureEvsPipeline(State desiredState);  // Only call from one thread!
92 
93     std::shared_ptr<android::frameworks::automotive::vhal::IVhalClient> mVehicle;
94     sp<IEvsEnumerator>          mEvs;
95     wp<IEvsDisplay>             mDisplay;
96     const ConfigManager&        mConfig;
97 
98     aidl::android::hardware::automotive::vehicle::VehiclePropValue mGearValue;
99     aidl::android::hardware::automotive::vehicle::VehiclePropValue mTurnSignalValue;
100 
101     State                       mCurrentState = OFF;
102 
103     // mCameraList is a redundant storage for camera device info, which is also
104     // stored in mCameraDescList and, however, not removed for backward
105     // compatibility.
106     std::vector<ConfigManager::CameraInfo>  mCameraList[NUM_STATES];
107     std::unique_ptr<RenderBase> mCurrentRenderer;
108     std::unique_ptr<RenderBase> mDesiredRenderer;
109     std::vector<CameraDesc>     mCameraDescList[NUM_STATES];
110 
111     std::thread                 mRenderThread;  // The thread that runs the main rendering loop
112 
113     // Other threads may want to spur us into action, so we provide a thread safe way to do that
114     std::mutex                  mLock;
115     std::condition_variable     mWakeSignal;
116     std::queue<Command>         mCommandQueue;
117 
118     EvsStats                    mEvsStats;  // Not thread-safe
119 
120     // True if the first frame displayed on the mCurrentRenderer. Resets to false when
121     // mCurrentRenderer changes.
122     bool                        mFirstFrameIsDisplayed;
123 };
124 
125 
126 #endif //CAR_EVS_APP_EVSSTATECONTROL_H
127