blob: 3741cce975af3ecbb4b71d49a24e4379e84e3790 [file] [log] [blame]
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001/*
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 Yeh87b3ec02019-06-03 10:44:39 -070020#include <string>
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070021#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
31namespace android {
32
33class Camera3Device;
34class Fence;
35
36namespace 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 */
47class StatusTracker: public Thread {
48 public:
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -070049 explicit StatusTracker(wp<Camera3Device> parent);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070050 ~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 Yeh87b3ec02019-06-03 10:44:39 -070058 int addComponent(std::string componentName);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070059
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 Yeh87b3ec02019-06-03 10:44:39 -070072 void dumpActiveComponents();
73
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070074 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 Yeh87b3ec02019-06-03 10:44:39 -0700111 KeyedVector<int, std::string> mComponentNames;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700112 // 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