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 HW_EMULATOR_CAMERA_WORKER_THREAD_H 18 #define HW_EMULATOR_CAMERA_WORKER_THREAD_H 19 20 #include <utils/Thread.h> 21 22 namespace android { 23 24 class EmulatedCamera; 25 class EmulatedCameraDevice; 26 27 class WorkerThread : public Thread { 28 public: 29 WorkerThread(const char* threadName, 30 EmulatedCameraDevice* camera_dev, 31 EmulatedCamera* cameraHAL); ~WorkerThread()32 virtual ~WorkerThread() {} 33 34 /* Starts the thread 35 * Param: 36 * oneBurst - Controls how many times thread loop should run. If 37 * this parameter is 'true', thread routine will run only once 38 * If this parameter is 'false', thread routine will run until 39 * stopThread method is called. See startWorkerThread for more 40 * info. 41 * Return: 42 * NO_ERROR on success, or an appropriate error status. 43 */ 44 status_t startThread(bool oneBurst); 45 46 /* Stops the thread, this only requests that the thread exits. The method 47 * will return right after the request has been made. Use joinThread to 48 * wait for the thread to exit. */ 49 status_t stopThread(); 50 51 /* Wake a thread that's currently waiting to timeout or to be awoken */ 52 status_t wakeThread(); 53 54 /* Join the thread, waits until the thread exits before returning. */ 55 status_t joinThread(); 56 57 protected: 58 /* Perform whatever work should be done in the worker thread. A subclass 59 * needs to implement this. 60 * Return: 61 * true To continue thread loop, or false to exit the thread loop and 62 * terminate the thread. 63 */ 64 virtual bool inWorkerThread() = 0; 65 66 /* This provides an opportunity for a subclass to perform some operation 67 * when the thread starts. This is run on the newly started thread. If this 68 * returns an error the thread will exit and inWorkerThread will never be 69 * called. 70 */ onThreadStart()71 virtual status_t onThreadStart() { return NO_ERROR; } 72 73 /* This provides an opportunity for a subclass to perform some operation 74 * when the thread exits. This is run on the worker thread. By default this 75 * does nothing. 76 */ onThreadExit()77 virtual void onThreadExit() { } 78 79 /* Containing camera device object. */ 80 EmulatedCameraDevice* mCameraDevice; 81 /* The camera HAL from the camera device object */ 82 EmulatedCamera* mCameraHAL; 83 84 /* Controls number of times the thread loop runs. 85 * See startThread for more information. */ 86 bool mOneBurst; 87 88 /* Running Condition and mutex, use these to sleep the thread, the 89 * supporting functions will use these to signal wakes and exits. */ 90 Condition mRunningCondition; 91 Mutex mRunningMutex; 92 bool mRunning; 93 private: 94 /* Overriden base class method. 95 * It is overriden in order to provide one-time initialization just 96 * prior to starting the thread routine. 97 */ 98 status_t readyToRun() final; 99 100 /* Implements abstract method of the base Thread class. */ 101 bool threadLoop() final; 102 103 const char* mThreadName; 104 }; 105 106 } // namespace android 107 108 #endif // HW_EMULATOR_CAMERA_WORKER_THREAD_H 109