summaryrefslogtreecommitdiffstats
path: root/services/camera/libcameraservice/device3/StatusTracker.h
blob: 49cecb3b0005d2d3b154f3b27e6c1d561b406daf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_SERVERS_CAMERA3_STATUSTRACKER_H
#define ANDROID_SERVERS_CAMERA3_STATUSTRACKER_H

#include <utils/Condition.h>
#include <utils/Errors.h>
#include <utils/List.h>
#include <utils/Mutex.h>
#include <utils/Thread.h>
#include <utils/KeyedVector.h>
#include <hardware/camera3.h>

#include "common/CameraDeviceBase.h"

namespace android {

class Camera3Device;
class Fence;

namespace camera3 {

/**
 * State tracking for idle and other collective state transitions.
 * Collects idle notifications from different sources and calls the
 * parent when all of them become idle.
 *
 * The parent is responsible for synchronizing the status updates with its
 * internal state correctly, which means the notifyStatus call to the parent may
 * block for a while.
 */
class StatusTracker: public Thread {
  public:
    StatusTracker(wp<Camera3Device> parent);
    ~StatusTracker();

    // An always-invalid component ID
    static const int NO_STATUS_ID = -1;

    // Add a component to track; returns non-negative unique ID for the new
    // component on success, negative error code on failure.
    // New components start in the idle state.
    int addComponent();

    // Remove existing component from idle tracking. Ignores unknown IDs
    void removeComponent(int id);

    // Set the state of a tracked component to be idle. Ignores unknown IDs; can
    // accept a fence to wait on to complete idle.  The fence is merged with any
    // previous fences given, which means they all must signal before the
    // component is considered idle.
    void markComponentIdle(int id, const sp<Fence>& componentFence);

    // Set the state of a tracked component to be active. Ignores unknown IDs.
    void markComponentActive(int id);

    virtual void requestExit();
  protected:

    virtual bool threadLoop();

  private:
    enum ComponentState {
        IDLE,
        ACTIVE
    };

    void markComponent(int id, ComponentState state,
            const sp<Fence>& componentFence);

    // Guards mPendingChange, mPendingStates, mComponentsChanged
    Mutex mPendingLock;

    Condition mPendingChangeSignal;

    struct StateChange {
        int id;
        ComponentState state;
        sp<Fence> fence;
    };
    // A queue of yet-to-be-processed state changes to components
    Vector<StateChange> mPendingChangeQueue;
    bool mComponentsChanged;

    wp<Camera3Device> mParent;

    // Guards rest of internals. Must be locked after mPendingLock if both used.
    Mutex mLock;

    int mNextComponentId;

    // Current component states
    KeyedVector<int, ComponentState> mStates;
    // Merged fence for all processed state changes
    sp<Fence> mIdleFence;
    // Current overall device state
    ComponentState mDeviceState;

    // Private to threadLoop

    // Determine current overall device state
    // We're IDLE iff
    // - All components are currently IDLE
    // - The merged fence for all component updates has signalled
    ComponentState getDeviceStateLocked();

    Vector<ComponentState> mStateTransitions;

    static const nsecs_t kWaitDuration = 250000000LL; // 250 ms
};

} // namespace camera3

} // namespace android

#endif