blob: 9ea671be5b86b2590ef4f93a31af9d73569086a7 [file] [log] [blame]
John Reckcec24ae2013-11-05 13:27:50 -08001/*
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 Reck4f02bf42014-01-03 18:09:17 -080021#include <utils/Timers.h>
John Reckcec24ae2013-11-05 13:27:50 -080022
23namespace android {
John Reck4f02bf42014-01-03 18:09:17 -080024class Mutex;
25class Condition;
John Reckcec24ae2013-11-05 13:27:50 -080026namespace uirenderer {
27namespace renderthread {
28
John Reck4f02bf42014-01-03 18:09:17 -080029#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
John Reckcec24ae2013-11-05 13:27:50 -080048class ANDROID_API RenderTask {
49public:
Chris Craikd41c4d82015-01-05 15:51:13 -080050 ANDROID_API RenderTask() : mNext(nullptr), mRunAt(0) {}
John Reck4f02bf42014-01-03 18:09:17 -080051 ANDROID_API virtual ~RenderTask() {}
John Reckcec24ae2013-11-05 13:27:50 -080052
53 ANDROID_API virtual void run() = 0;
54
55 RenderTask* mNext;
John Recka6260b82014-01-29 18:31:51 -080056 nsecs_t mRunAt; // nano-seconds on the SYSTEM_TIME_MONOTONIC clock
John Reck4f02bf42014-01-03 18:09:17 -080057};
58
59class SignalingRenderTask : public RenderTask {
60public:
61 // Takes ownership of task, caller owns lock and signal
62 SignalingRenderTask(RenderTask* task, Mutex* lock, Condition* signal)
63 : mTask(task), mLock(lock), mSignal(signal) {}
Chris Craikd41c4d82015-01-05 15:51:13 -080064 virtual void run() override;
John Reck4f02bf42014-01-03 18:09:17 -080065
66private:
67 RenderTask* mTask;
68 Mutex* mLock;
69 Condition* mSignal;
70};
71
72typedef void* (*RunnableMethod)(void* data);
73
74class MethodInvokeRenderTask : public RenderTask {
75public:
Chih-Hung Hsiehfaecb782016-07-21 11:23:06 -070076 explicit MethodInvokeRenderTask(RunnableMethod method)
Chris Craikd41c4d82015-01-05 15:51:13 -080077 : mMethod(method), mReturnPtr(nullptr) {}
John Reck4f02bf42014-01-03 18:09:17 -080078
79 void* payload() { return mData; }
80 void setReturnPtr(void** retptr) { mReturnPtr = retptr; }
81
Chris Craikd41c4d82015-01-05 15:51:13 -080082 virtual void run() override {
John Reck4f02bf42014-01-03 18:09:17 -080083 void* retval = mMethod(mData);
84 if (mReturnPtr) {
85 *mReturnPtr = retval;
86 }
87 // Commit suicide
88 delete this;
89 }
90private:
91 RunnableMethod mMethod;
92 char mData[METHOD_INVOKE_PAYLOAD_SIZE];
93 void** mReturnPtr;
John Reckcec24ae2013-11-05 13:27:50 -080094};
95
96} /* namespace renderthread */
97} /* namespace uirenderer */
98} /* namespace android */
99#endif /* RENDERTASK_H_ */