huangcm
2025-02-26 a813214788f6e7b512df54f1c659cd0bdc9ac175
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
/*
 * Copyright (C) 2016 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 HW_EMULATOR_CAMERA_WORKER_THREAD_H
#define HW_EMULATOR_CAMERA_WORKER_THREAD_H
 
#include <utils/Thread.h>
 
namespace android {
 
class EmulatedCamera;
class EmulatedCameraDevice;
 
class WorkerThread : public Thread {
public:
    WorkerThread(const char* threadName,
                 EmulatedCameraDevice* camera_dev,
                 EmulatedCamera* cameraHAL);
    virtual ~WorkerThread() {}
 
    /* Starts the thread
     * Param:
     *  oneBurst - Controls how many times thread loop should run. If
     *      this parameter is 'true', thread routine will run only once
     *      If this parameter is 'false', thread routine will run until
     *      stopThread method is called. See startWorkerThread for more
     *      info.
     * Return:
     *  NO_ERROR on success, or an appropriate error status.
     */
    status_t startThread(bool oneBurst);
 
    /* Stops the thread, this only requests that the thread exits. The method
     * will return right after the request has been made. Use joinThread to
     * wait for the thread to exit. */
    status_t stopThread();
 
    /* Wake a thread that's currently waiting to timeout or to be awoken */
    status_t wakeThread();
 
    /* Join the thread, waits until the thread exits before returning. */
    status_t joinThread();
 
protected:
    /* Perform whatever work should be done in the worker thread. A subclass
     * needs to implement this.
     * Return:
     *  true To continue thread loop, or false to exit the thread loop and
     *  terminate the thread.
     */
    virtual bool inWorkerThread() = 0;
 
    /* This provides an opportunity for a subclass to perform some operation
     * when the thread starts. This is run on the newly started thread. If this
     * returns an error the thread will exit and inWorkerThread will never be
     * called.
     */
    virtual status_t onThreadStart() { return NO_ERROR; }
 
    /* This provides an opportunity for a subclass to perform some operation
     * when the thread exits. This is run on the worker thread. By default this
     * does nothing.
     */
    virtual void onThreadExit() { }
 
    /* Containing camera device object. */
    EmulatedCameraDevice* mCameraDevice;
    /* The camera HAL from the camera device object */
    EmulatedCamera* mCameraHAL;
 
    /* Controls number of times the thread loop runs.
     * See startThread for more information. */
    bool mOneBurst;
 
    /* Running Condition and mutex, use these to sleep the thread, the
     * supporting functions will use these to signal wakes and exits. */
    Condition mRunningCondition;
    Mutex mRunningMutex;
    bool mRunning;
private:
    /* Overriden base class method.
     * It is overriden in order to provide one-time initialization just
     * prior to starting the thread routine.
     */
    status_t readyToRun() final;
 
    /* Implements abstract method of the base Thread class. */
    bool threadLoop() final;
 
    const char* mThreadName;
};
 
}  // namespace android
 
#endif  // HW_EMULATOR_CAMERA_WORKER_THREAD_H