blob: 65461107028abebbefe323a1ced609b15f86e10b [file] [log] [blame]
Jason Samse4a06c52011-03-16 16:29:28 -07001/*
2 * Copyright (C) 2009 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#include "rsdCore.h"
18#include "rsdBcc.h"
19
20#include <malloc.h>
21#include "rsContext.h"
22
Jason Sams55d2a252011-03-17 16:12:47 -070023#include <sys/types.h>
24#include <sys/resource.h>
25#include <sched.h>
26#include <cutils/properties.h>
27#include <cutils/sched_policy.h>
28#include <sys/syscall.h>
29#include <string.h>
30
Jason Samse4a06c52011-03-16 16:29:28 -070031using namespace android;
32using namespace android::renderscript;
33
Jason Sams55d2a252011-03-17 16:12:47 -070034static void Shutdown(Context *rsc);
35static void SetPriority(const Context *rsc, int32_t priority);
36
Jason Samse4a06c52011-03-16 16:29:28 -070037static RsdHalFunctions FunctionTable = {
Jason Sams55d2a252011-03-17 16:12:47 -070038 Shutdown,
Jason Samse4a06c52011-03-16 16:29:28 -070039 NULL,
Jason Sams55d2a252011-03-17 16:12:47 -070040 SetPriority,
Jason Samse4a06c52011-03-16 16:29:28 -070041 {
42 rsdScriptInit,
43 rsdScriptInvokeFunction,
44 rsdScriptInvokeRoot,
Jason Sams55d2a252011-03-17 16:12:47 -070045 rsdScriptInvokeForEach,
Jason Samse4a06c52011-03-16 16:29:28 -070046 rsdScriptInvokeInit,
47 rsdScriptSetGlobalVar,
48 rsdScriptSetGlobalBind,
49 rsdScriptSetGlobalObj,
50 rsdScriptDestroy
51 }
52};
53
54
Jason Sams55d2a252011-03-17 16:12:47 -070055
56static void * HelperThreadProc(void *vrsc) {
57 Context *rsc = static_cast<Context *>(vrsc);
58 RsHal *dc = (RsHal *)rsc->mHal.drv;
59
60
61 uint32_t idx = (uint32_t)android_atomic_inc(&dc->mWorkers.mLaunchCount);
62
63 //LOGV("RS helperThread starting %p idx=%i", rsc, idx);
64
65 dc->mWorkers.mLaunchSignals[idx].init();
66 dc->mWorkers.mNativeThreadId[idx] = gettid();
67
68#if 0
69 typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
70 cpu_set_t cpuset;
71 memset(&cpuset, 0, sizeof(cpuset));
72 cpuset.bits[idx / 64] |= 1ULL << (idx % 64);
73 int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx],
74 sizeof(cpuset), &cpuset);
75 LOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
76#endif
77
78 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
79 if (status) {
80 LOGE("pthread_setspecific %i", status);
81 }
82
83 while (!dc->mExit) {
84 dc->mWorkers.mLaunchSignals[idx].wait();
85 if (dc->mWorkers.mLaunchCallback) {
86 dc->mWorkers.mLaunchCallback(dc->mWorkers.mLaunchData, idx);
87 }
88 android_atomic_dec(&dc->mWorkers.mRunningCount);
89 dc->mWorkers.mCompleteSignal.set();
90 }
91
92 //LOGV("RS helperThread exited %p idx=%i", rsc, idx);
93 return NULL;
94}
95
96void rsdLaunchThreads(Context *rsc, WorkerCallback_t cbk, void *data) {
97 RsHal *dc = (RsHal *)rsc->mHal.drv;
98
99 dc->mWorkers.mLaunchData = data;
100 dc->mWorkers.mLaunchCallback = cbk;
101 android_atomic_release_store(dc->mWorkers.mCount, &dc->mWorkers.mRunningCount);
102 for (uint32_t ct = 0; ct < dc->mWorkers.mCount; ct++) {
103 dc->mWorkers.mLaunchSignals[ct].set();
104 }
105 while (android_atomic_acquire_load(&dc->mWorkers.mRunningCount) != 0) {
106 dc->mWorkers.mCompleteSignal.wait();
107 }
108}
109
Jason Samse4a06c52011-03-16 16:29:28 -0700110bool rsdHalInit(Context *rsc, uint32_t version_major, uint32_t version_minor) {
111 rsc->mHal.funcs = FunctionTable;
112
Jason Sams55d2a252011-03-17 16:12:47 -0700113 RsHal *dc = (RsHal *)calloc(1, sizeof(RsHal));
Jason Sams80e29cf2011-03-18 17:08:54 -0700114 if (!dc) {
115 LOGE("Calloc for driver hal failed.");
Jason Samse4a06c52011-03-16 16:29:28 -0700116 return false;
117 }
Jason Sams55d2a252011-03-17 16:12:47 -0700118 rsc->mHal.drv = dc;
Jason Samse4a06c52011-03-16 16:29:28 -0700119
Jason Sams55d2a252011-03-17 16:12:47 -0700120
121 int cpu = sysconf(_SC_NPROCESSORS_ONLN);
122 LOGV("RS Launching thread(s), reported CPU count %i", cpu);
123 if (cpu < 2) cpu = 0;
124
125 dc->mWorkers.mCount = (uint32_t)cpu;
126 dc->mWorkers.mThreadId = (pthread_t *) calloc(dc->mWorkers.mCount, sizeof(pthread_t));
127 dc->mWorkers.mNativeThreadId = (pid_t *) calloc(dc->mWorkers.mCount, sizeof(pid_t));
128 dc->mWorkers.mLaunchSignals = new Signal[dc->mWorkers.mCount];
129 dc->mWorkers.mLaunchCallback = NULL;
130
131 dc->mWorkers.mCompleteSignal.init();
132
133 android_atomic_release_store(dc->mWorkers.mCount, &dc->mWorkers.mRunningCount);
134 android_atomic_release_store(0, &dc->mWorkers.mLaunchCount);
135
136 int status;
137 pthread_attr_t threadAttr;
138 status = pthread_attr_init(&threadAttr);
139 if (status) {
140 LOGE("Failed to init thread attribute.");
141 return false;
142 }
143
144 for (uint32_t ct=0; ct < dc->mWorkers.mCount; ct++) {
145 status = pthread_create(&dc->mWorkers.mThreadId[ct], &threadAttr, HelperThreadProc, rsc);
146 if (status) {
147 dc->mWorkers.mCount = ct;
148 LOGE("Created fewer than expected number of RS threads.");
149 break;
150 }
151 }
152 while (android_atomic_acquire_load(&dc->mWorkers.mRunningCount) != 0) {
153 usleep(100);
154 }
155
156 pthread_attr_destroy(&threadAttr);
Jason Samse4a06c52011-03-16 16:29:28 -0700157 return true;
158}
159
Jason Sams55d2a252011-03-17 16:12:47 -0700160
161void SetPriority(const Context *rsc, int32_t priority) {
162 RsHal *dc = (RsHal *)rsc->mHal.drv;
163 for (uint32_t ct=0; ct < dc->mWorkers.mCount; ct++) {
164 setpriority(PRIO_PROCESS, dc->mWorkers.mNativeThreadId[ct], priority);
165 }
166}
167
168void Shutdown(Context *rsc) {
169 RsHal *dc = (RsHal *)rsc->mHal.drv;
170
171 dc->mExit = true;
172 dc->mWorkers.mLaunchData = NULL;
173 dc->mWorkers.mLaunchCallback = NULL;
174 android_atomic_release_store(dc->mWorkers.mCount, &dc->mWorkers.mRunningCount);
175 for (uint32_t ct = 0; ct < dc->mWorkers.mCount; ct++) {
176 dc->mWorkers.mLaunchSignals[ct].set();
177 }
178 int status;
179 void *res;
180 for (uint32_t ct = 0; ct < dc->mWorkers.mCount; ct++) {
181 status = pthread_join(dc->mWorkers.mThreadId[ct], &res);
182 }
183 rsAssert(android_atomic_acquire_load(&dc->mWorkers.mRunningCount) == 0);
184}
185
186