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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
/*
* Copyright (C) 2011 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.
*/
#include "rsdCore.h"
#include "rsdAllocation.h"
#include "rsdBcc.h"
#include "rsdGL.h"
#include "rsdProgramStore.h"
#include "rsdProgramRaster.h"
#include "rsdProgramVertex.h"
#include "rsdProgramFragment.h"
#include "rsdMesh.h"
#include "rsdSampler.h"
#include "rsdFrameBuffer.h"
#include <malloc.h>
#include "rsContext.h"
#include <sys/types.h>
#include <sys/resource.h>
#include <sched.h>
#include <cutils/properties.h>
#include <cutils/sched_policy.h>
#include <sys/syscall.h>
#include <string.h>
#include <bcc/bcc.h>
using namespace android;
using namespace android::renderscript;
static void Shutdown(Context *rsc);
static void SetPriority(const Context *rsc, int32_t priority);
static RsdHalFunctions FunctionTable = {
rsdGLInit,
rsdGLShutdown,
rsdGLSetSurface,
rsdGLSwap,
Shutdown,
NULL,
SetPriority,
{
rsdScriptInit,
rsdScriptInvokeFunction,
rsdScriptInvokeRoot,
rsdScriptInvokeForEach,
rsdScriptInvokeInit,
rsdScriptSetGlobalVar,
rsdScriptSetGlobalBind,
rsdScriptSetGlobalObj,
rsdScriptDestroy
},
{
rsdAllocationInit,
rsdAllocationDestroy,
rsdAllocationResize,
rsdAllocationSyncAll,
rsdAllocationMarkDirty,
rsdAllocationData1D,
rsdAllocationData2D,
rsdAllocationData3D,
rsdAllocationElementData1D,
rsdAllocationElementData2D
},
{
rsdProgramStoreInit,
rsdProgramStoreSetActive,
rsdProgramStoreDestroy
},
{
rsdProgramRasterInit,
rsdProgramRasterSetActive,
rsdProgramRasterDestroy
},
{
rsdProgramVertexInit,
rsdProgramVertexSetActive,
rsdProgramVertexDestroy
},
{
rsdProgramFragmentInit,
rsdProgramFragmentSetActive,
rsdProgramFragmentDestroy
},
{
rsdMeshInit,
rsdMeshDraw,
rsdMeshDestroy
},
{
rsdSamplerInit,
rsdSamplerDestroy
},
{
rsdFrameBufferInit,
rsdFrameBufferSetActive,
rsdFrameBufferDestroy
},
};
pthread_key_t rsdgThreadTLSKey = 0;
uint32_t rsdgThreadTLSKeyCount = 0;
pthread_mutex_t rsdgInitMutex = PTHREAD_MUTEX_INITIALIZER;
static void * HelperThreadProc(void *vrsc) {
Context *rsc = static_cast<Context *>(vrsc);
RsdHal *dc = (RsdHal *)rsc->mHal.drv;
uint32_t idx = (uint32_t)android_atomic_inc(&dc->mWorkers.mLaunchCount);
//LOGV("RS helperThread starting %p idx=%i", rsc, idx);
dc->mWorkers.mLaunchSignals[idx].init();
dc->mWorkers.mNativeThreadId[idx] = gettid();
int status = pthread_setspecific(rsdgThreadTLSKey, &dc->mTlsStruct);
if (status) {
LOGE("pthread_setspecific %i", status);
}
#if 0
typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
cpu_set_t cpuset;
memset(&cpuset, 0, sizeof(cpuset));
cpuset.bits[idx / 64] |= 1ULL << (idx % 64);
int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx],
sizeof(cpuset), &cpuset);
LOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
#endif
while (!dc->mExit) {
dc->mWorkers.mLaunchSignals[idx].wait();
if (dc->mWorkers.mLaunchCallback) {
dc->mWorkers.mLaunchCallback(dc->mWorkers.mLaunchData, idx);
}
android_atomic_dec(&dc->mWorkers.mRunningCount);
dc->mWorkers.mCompleteSignal.set();
}
//LOGV("RS helperThread exited %p idx=%i", rsc, idx);
return NULL;
}
void rsdLaunchThreads(Context *rsc, WorkerCallback_t cbk, void *data) {
RsdHal *dc = (RsdHal *)rsc->mHal.drv;
dc->mWorkers.mLaunchData = data;
dc->mWorkers.mLaunchCallback = cbk;
android_atomic_release_store(dc->mWorkers.mCount, &dc->mWorkers.mRunningCount);
for (uint32_t ct = 0; ct < dc->mWorkers.mCount; ct++) {
dc->mWorkers.mLaunchSignals[ct].set();
}
while (android_atomic_acquire_load(&dc->mWorkers.mRunningCount) != 0) {
dc->mWorkers.mCompleteSignal.wait();
}
}
bool rsdHalInit(Context *rsc, uint32_t version_major, uint32_t version_minor) {
rsc->mHal.funcs = FunctionTable;
RsdHal *dc = (RsdHal *)calloc(1, sizeof(RsdHal));
if (!dc) {
LOGE("Calloc for driver hal failed.");
return false;
}
rsc->mHal.drv = dc;
pthread_mutex_lock(&rsdgInitMutex);
if (!rsdgThreadTLSKeyCount) {
int status = pthread_key_create(&rsdgThreadTLSKey, NULL);
if (status) {
LOGE("Failed to init thread tls key.");
pthread_mutex_unlock(&rsdgInitMutex);
return false;
}
}
rsdgThreadTLSKeyCount++;
pthread_mutex_unlock(&rsdgInitMutex);
dc->mTlsStruct.mContext = rsc;
dc->mTlsStruct.mScript = NULL;
int status = pthread_setspecific(rsdgThreadTLSKey, &dc->mTlsStruct);
if (status) {
LOGE("pthread_setspecific %i", status);
}
int cpu = sysconf(_SC_NPROCESSORS_ONLN);
LOGV("RS Launching thread(s), reported CPU count %i", cpu);
if (cpu < 2) cpu = 0;
dc->mWorkers.mCount = (uint32_t)cpu;
dc->mWorkers.mThreadId = (pthread_t *) calloc(dc->mWorkers.mCount, sizeof(pthread_t));
dc->mWorkers.mNativeThreadId = (pid_t *) calloc(dc->mWorkers.mCount, sizeof(pid_t));
dc->mWorkers.mLaunchSignals = new Signal[dc->mWorkers.mCount];
dc->mWorkers.mLaunchCallback = NULL;
dc->mWorkers.mCompleteSignal.init();
android_atomic_release_store(dc->mWorkers.mCount, &dc->mWorkers.mRunningCount);
android_atomic_release_store(0, &dc->mWorkers.mLaunchCount);
pthread_attr_t threadAttr;
status = pthread_attr_init(&threadAttr);
if (status) {
LOGE("Failed to init thread attribute.");
return false;
}
for (uint32_t ct=0; ct < dc->mWorkers.mCount; ct++) {
status = pthread_create(&dc->mWorkers.mThreadId[ct], &threadAttr, HelperThreadProc, rsc);
if (status) {
dc->mWorkers.mCount = ct;
LOGE("Created fewer than expected number of RS threads.");
break;
}
}
while (android_atomic_acquire_load(&dc->mWorkers.mRunningCount) != 0) {
usleep(100);
}
pthread_attr_destroy(&threadAttr);
return true;
}
void SetPriority(const Context *rsc, int32_t priority) {
RsdHal *dc = (RsdHal *)rsc->mHal.drv;
for (uint32_t ct=0; ct < dc->mWorkers.mCount; ct++) {
setpriority(PRIO_PROCESS, dc->mWorkers.mNativeThreadId[ct], priority);
}
}
void Shutdown(Context *rsc) {
RsdHal *dc = (RsdHal *)rsc->mHal.drv;
dc->mExit = true;
dc->mWorkers.mLaunchData = NULL;
dc->mWorkers.mLaunchCallback = NULL;
android_atomic_release_store(dc->mWorkers.mCount, &dc->mWorkers.mRunningCount);
for (uint32_t ct = 0; ct < dc->mWorkers.mCount; ct++) {
dc->mWorkers.mLaunchSignals[ct].set();
}
int status;
void *res;
for (uint32_t ct = 0; ct < dc->mWorkers.mCount; ct++) {
status = pthread_join(dc->mWorkers.mThreadId[ct], &res);
}
rsAssert(android_atomic_acquire_load(&dc->mWorkers.mRunningCount) == 0);
// Global structure cleanup.
pthread_mutex_lock(&rsdgInitMutex);
--rsdgThreadTLSKeyCount;
if (!rsdgThreadTLSKeyCount) {
pthread_key_delete(rsdgThreadTLSKey);
}
pthread_mutex_unlock(&rsdgInitMutex);
}
|