| John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [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 RENDERTASK_H_ | 
|  | 18 | #define RENDERTASK_H_ | 
|  | 19 |  | 
|  | 20 | #include <cutils/compiler.h> | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 21 | #include <utils/Timers.h> | 
| John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 22 |  | 
|  | 23 | namespace android { | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 24 | class Mutex; | 
|  | 25 | class Condition; | 
| John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 26 | namespace uirenderer { | 
|  | 27 | namespace renderthread { | 
|  | 28 |  | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 29 | #define METHOD_INVOKE_PAYLOAD_SIZE (8 * sizeof(void*)) | 
|  | 30 |  | 
|  | 31 | /* | 
|  | 32 | * Notes about memory management | 
|  | 33 | * | 
|  | 34 | * RenderThread will only invoke RenderTask::run(). It is the responsibility | 
|  | 35 | * of the RenderTask to know if it needs to suicide at the end of run() or | 
|  | 36 | * if some other lifecycle is being used. As such, it is not valid to reference | 
|  | 37 | * anything on RenderTask after the first call to run(). | 
|  | 38 | * | 
|  | 39 | * For example SignalingRenderTask | 
|  | 40 | * is expected to be stack allocated by the calling thread, so it does not | 
|  | 41 | * suicide in run() but instead relies on the caller to destroy it. | 
|  | 42 | * | 
|  | 43 | * MethodInvokeRenderTask however is currently allocated with new, so it will | 
|  | 44 | * suicide at the end of run(). TODO: Replace this with a small pool to avoid | 
|  | 45 | * malloc/free churn of small objects? | 
|  | 46 | */ | 
|  | 47 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 48 | class RenderTask { | 
| John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 49 | public: | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 50 | RenderTask() : mNext(nullptr), mRunAt(0) {} | 
|  | 51 | virtual ~RenderTask() {} | 
| John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 52 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 53 | virtual void run() = 0; | 
| John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 54 |  | 
|  | 55 | RenderTask* mNext; | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 56 | nsecs_t mRunAt;  // nano-seconds on the SYSTEM_TIME_MONOTONIC clock | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 57 | }; | 
|  | 58 |  | 
|  | 59 | class SignalingRenderTask : public RenderTask { | 
|  | 60 | public: | 
|  | 61 | // Takes ownership of task, caller owns lock and signal | 
|  | 62 | SignalingRenderTask(RenderTask* task, Mutex* lock, Condition* signal) | 
| Tom Cherry | 298a146 | 2017-02-28 14:07:09 -0800 | [diff] [blame] | 63 | : mTask(task), mLock(lock), mSignal(signal), mHasRun(false) {} | 
| Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 64 | virtual void run() override; | 
| Tom Cherry | 298a146 | 2017-02-28 14:07:09 -0800 | [diff] [blame] | 65 | bool hasRun() const { return mHasRun; } | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 66 |  | 
|  | 67 | private: | 
|  | 68 | RenderTask* mTask; | 
|  | 69 | Mutex* mLock; | 
|  | 70 | Condition* mSignal; | 
| Tom Cherry | 298a146 | 2017-02-28 14:07:09 -0800 | [diff] [blame] | 71 | bool mHasRun; | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 72 | }; | 
|  | 73 |  | 
|  | 74 | typedef void* (*RunnableMethod)(void* data); | 
|  | 75 |  | 
|  | 76 | class MethodInvokeRenderTask : public RenderTask { | 
|  | 77 | public: | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 78 | explicit MethodInvokeRenderTask(RunnableMethod method) : mMethod(method), mReturnPtr(nullptr) {} | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 79 |  | 
|  | 80 | void* payload() { return mData; } | 
|  | 81 | void setReturnPtr(void** retptr) { mReturnPtr = retptr; } | 
|  | 82 |  | 
| Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 83 | virtual void run() override { | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 84 | void* retval = mMethod(mData); | 
|  | 85 | if (mReturnPtr) { | 
|  | 86 | *mReturnPtr = retval; | 
|  | 87 | } | 
|  | 88 | // Commit suicide | 
|  | 89 | delete this; | 
|  | 90 | } | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 91 |  | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 92 | private: | 
|  | 93 | RunnableMethod mMethod; | 
|  | 94 | char mData[METHOD_INVOKE_PAYLOAD_SIZE]; | 
|  | 95 | void** mReturnPtr; | 
| John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame] | 96 | }; | 
|  | 97 |  | 
|  | 98 | } /* namespace renderthread */ | 
|  | 99 | } /* namespace uirenderer */ | 
|  | 100 | } /* namespace android */ | 
|  | 101 | #endif /* RENDERTASK_H_ */ |