Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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_SERVERS_CAMERA3_STATUSTRACKER_H |
| 18 | #define ANDROID_SERVERS_CAMERA3_STATUSTRACKER_H |
| 19 | |
Yin-Chia Yeh | 87b3ec0 | 2019-06-03 10:44:39 -0700 | [diff] [blame^] | 20 | #include <string> |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 21 | #include <utils/Condition.h> |
| 22 | #include <utils/Errors.h> |
| 23 | #include <utils/List.h> |
| 24 | #include <utils/Mutex.h> |
| 25 | #include <utils/Thread.h> |
| 26 | #include <utils/KeyedVector.h> |
| 27 | #include <hardware/camera3.h> |
| 28 | |
| 29 | #include "common/CameraDeviceBase.h" |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | class Camera3Device; |
| 34 | class Fence; |
| 35 | |
| 36 | namespace camera3 { |
| 37 | |
| 38 | /** |
| 39 | * State tracking for idle and other collective state transitions. |
| 40 | * Collects idle notifications from different sources and calls the |
| 41 | * parent when all of them become idle. |
| 42 | * |
| 43 | * The parent is responsible for synchronizing the status updates with its |
| 44 | * internal state correctly, which means the notifyStatus call to the parent may |
| 45 | * block for a while. |
| 46 | */ |
| 47 | class StatusTracker: public Thread { |
| 48 | public: |
Chih-Hung Hsieh | 8b0b971 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 49 | explicit StatusTracker(wp<Camera3Device> parent); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 50 | ~StatusTracker(); |
| 51 | |
| 52 | // An always-invalid component ID |
| 53 | static const int NO_STATUS_ID = -1; |
| 54 | |
| 55 | // Add a component to track; returns non-negative unique ID for the new |
| 56 | // component on success, negative error code on failure. |
| 57 | // New components start in the idle state. |
Yin-Chia Yeh | 87b3ec0 | 2019-06-03 10:44:39 -0700 | [diff] [blame^] | 58 | int addComponent(std::string componentName); |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 59 | |
| 60 | // Remove existing component from idle tracking. Ignores unknown IDs |
| 61 | void removeComponent(int id); |
| 62 | |
| 63 | // Set the state of a tracked component to be idle. Ignores unknown IDs; can |
| 64 | // accept a fence to wait on to complete idle. The fence is merged with any |
| 65 | // previous fences given, which means they all must signal before the |
| 66 | // component is considered idle. |
| 67 | void markComponentIdle(int id, const sp<Fence>& componentFence); |
| 68 | |
| 69 | // Set the state of a tracked component to be active. Ignores unknown IDs. |
| 70 | void markComponentActive(int id); |
| 71 | |
Yin-Chia Yeh | 87b3ec0 | 2019-06-03 10:44:39 -0700 | [diff] [blame^] | 72 | void dumpActiveComponents(); |
| 73 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 74 | virtual void requestExit(); |
| 75 | protected: |
| 76 | |
| 77 | virtual bool threadLoop(); |
| 78 | |
| 79 | private: |
| 80 | enum ComponentState { |
| 81 | IDLE, |
| 82 | ACTIVE |
| 83 | }; |
| 84 | |
| 85 | void markComponent(int id, ComponentState state, |
| 86 | const sp<Fence>& componentFence); |
| 87 | |
| 88 | // Guards mPendingChange, mPendingStates, mComponentsChanged |
| 89 | Mutex mPendingLock; |
| 90 | |
| 91 | Condition mPendingChangeSignal; |
| 92 | |
| 93 | struct StateChange { |
| 94 | int id; |
| 95 | ComponentState state; |
| 96 | sp<Fence> fence; |
| 97 | }; |
| 98 | // A queue of yet-to-be-processed state changes to components |
| 99 | Vector<StateChange> mPendingChangeQueue; |
| 100 | bool mComponentsChanged; |
| 101 | |
| 102 | wp<Camera3Device> mParent; |
| 103 | |
| 104 | // Guards rest of internals. Must be locked after mPendingLock if both used. |
| 105 | Mutex mLock; |
| 106 | |
| 107 | int mNextComponentId; |
| 108 | |
| 109 | // Current component states |
| 110 | KeyedVector<int, ComponentState> mStates; |
Yin-Chia Yeh | 87b3ec0 | 2019-06-03 10:44:39 -0700 | [diff] [blame^] | 111 | KeyedVector<int, std::string> mComponentNames; |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 112 | // Merged fence for all processed state changes |
| 113 | sp<Fence> mIdleFence; |
| 114 | // Current overall device state |
| 115 | ComponentState mDeviceState; |
| 116 | |
| 117 | // Private to threadLoop |
| 118 | |
| 119 | // Determine current overall device state |
| 120 | // We're IDLE iff |
| 121 | // - All components are currently IDLE |
| 122 | // - The merged fence for all component updates has signalled |
| 123 | ComponentState getDeviceStateLocked(); |
| 124 | |
| 125 | Vector<ComponentState> mStateTransitions; |
| 126 | |
| 127 | static const nsecs_t kWaitDuration = 250000000LL; // 250 ms |
| 128 | }; |
| 129 | |
| 130 | } // namespace camera3 |
| 131 | |
| 132 | } // namespace android |
| 133 | |
| 134 | #endif |