1/* 2 * Copyright (C) 2011-2012 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 "rsdAllocation.h" 19#include "rsdBcc.h" 20#include "rsdSampler.h" 21#include "rsdScriptGroup.h" 22 23#include <malloc.h> 24#include "rsContext.h" 25 26#include <sys/types.h> 27#include <sys/resource.h> 28#include <sched.h> 29#include <cutils/properties.h> 30#include <sys/syscall.h> 31#include <string.h> 32 33using namespace android; 34using namespace android::renderscript; 35 36static void Shutdown(Context *rsc); 37static void SetPriority(const Context *rsc, int32_t priority); 38 39static RsdHalFunctions FunctionTable = { 40 NULL, 41 NULL, 42 NULL, 43 NULL, 44 45 Shutdown, 46 NULL, 47 SetPriority, 48 { 49 rsdScriptInit, 50 rsdInitIntrinsic, 51 rsdScriptInvokeFunction, 52 rsdScriptInvokeRoot, 53 rsdScriptInvokeForEach, 54 rsdScriptInvokeInit, 55 rsdScriptInvokeFreeChildren, 56 rsdScriptSetGlobalVar, 57 rsdScriptSetGlobalVarWithElemDims, 58 rsdScriptSetGlobalBind, 59 rsdScriptSetGlobalObj, 60 rsdScriptDestroy 61 }, 62 63 { 64 rsdAllocationInit, 65 rsdAllocationDestroy, 66 rsdAllocationResize, 67 rsdAllocationSyncAll, 68 rsdAllocationMarkDirty, 69 NULL, 70 NULL, 71 NULL, 72 NULL, 73 rsdAllocationData1D, 74 rsdAllocationData2D, 75 rsdAllocationData3D, 76 rsdAllocationRead1D, 77 rsdAllocationRead2D, 78 rsdAllocationRead3D, 79 rsdAllocationLock1D, 80 rsdAllocationUnlock1D, 81 rsdAllocationData1D_alloc, 82 rsdAllocationData2D_alloc, 83 rsdAllocationData3D_alloc, 84 rsdAllocationElementData1D, 85 rsdAllocationElementData2D, 86 rsdAllocationGenerateMipmaps 87 }, 88 89 90 { 91 NULL, 92 NULL, 93 NULL 94 }, 95 96 { 97 NULL, 98 NULL, 99 NULL 100 }, 101 102 { 103 NULL, 104 NULL, 105 NULL 106 }, 107 108 { 109 NULL, 110 NULL, 111 NULL 112 }, 113 114 { 115 NULL, 116 NULL, 117 NULL 118 }, 119 120 { 121 NULL, 122 NULL, 123 NULL, 124 NULL 125 }, 126 127 { 128 rsdSamplerInit, 129 rsdSamplerDestroy 130 }, 131 132 { 133 NULL, 134 NULL, 135 NULL 136 }, 137 138 { 139 rsdScriptGroupInit, 140 rsdScriptGroupSetInput, 141 rsdScriptGroupSetOutput, 142 rsdScriptGroupExecute, 143 rsdScriptGroupDestroy 144 } 145 146 147}; 148 149pthread_key_t rsdgThreadTLSKey = 0; 150uint32_t rsdgThreadTLSKeyCount = 0; 151pthread_mutex_t rsdgInitMutex = PTHREAD_MUTEX_INITIALIZER; 152 153 154static void * HelperThreadProc(void *vrsc) { 155 Context *rsc = static_cast<Context *>(vrsc); 156 RsdHal *dc = (RsdHal *)rsc->mHal.drv; 157 158 159 uint32_t idx = (uint32_t)android_atomic_inc(&dc->mWorkers.mLaunchCount); 160 161 //ALOGV("RS helperThread starting %p idx=%i", rsc, idx); 162 163 dc->mWorkers.mLaunchSignals[idx].init(); 164 dc->mWorkers.mNativeThreadId[idx] = gettid(); 165 166 int status = pthread_setspecific(rsdgThreadTLSKey, &dc->mTlsStruct); 167 if (status) { 168 ALOGE("pthread_setspecific %i", status); 169 } 170 171#if 0 172 typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t; 173 cpu_set_t cpuset; 174 memset(&cpuset, 0, sizeof(cpuset)); 175 cpuset.bits[idx / 64] |= 1ULL << (idx % 64); 176 int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx], 177 sizeof(cpuset), &cpuset); 178 ALOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret)); 179#endif 180 181 while (!dc->mExit) { 182 dc->mWorkers.mLaunchSignals[idx].wait(); 183 if (dc->mWorkers.mLaunchCallback) { 184 dc->mWorkers.mLaunchCallback(dc->mWorkers.mLaunchData, idx); 185 } 186 android_atomic_dec(&dc->mWorkers.mRunningCount); 187 dc->mWorkers.mCompleteSignal.set(); 188 } 189 190 //ALOGV("RS helperThread exited %p idx=%i", rsc, idx); 191 return NULL; 192} 193 194void rsdLaunchThreads(Context *rsc, WorkerCallback_t cbk, void *data) { 195 RsdHal *dc = (RsdHal *)rsc->mHal.drv; 196 197 dc->mWorkers.mLaunchData = data; 198 dc->mWorkers.mLaunchCallback = cbk; 199 android_atomic_release_store(dc->mWorkers.mCount, &dc->mWorkers.mRunningCount); 200 for (uint32_t ct = 0; ct < dc->mWorkers.mCount; ct++) { 201 dc->mWorkers.mLaunchSignals[ct].set(); 202 } 203 while (android_atomic_acquire_load(&dc->mWorkers.mRunningCount) != 0) { 204 dc->mWorkers.mCompleteSignal.wait(); 205 } 206} 207 208extern "C" bool rsdHalInit(RsContext c, uint32_t version_major, 209 uint32_t version_minor) { 210 Context *rsc = (Context*) c; 211 rsc->mHal.funcs = FunctionTable; 212 213 RsdHal *dc = (RsdHal *)calloc(1, sizeof(RsdHal)); 214 if (!dc) { 215 ALOGE("Calloc for driver hal failed."); 216 return false; 217 } 218 rsc->mHal.drv = dc; 219 220 pthread_mutex_lock(&rsdgInitMutex); 221 if (!rsdgThreadTLSKeyCount) { 222 int status = pthread_key_create(&rsdgThreadTLSKey, NULL); 223 if (status) { 224 ALOGE("Failed to init thread tls key."); 225 pthread_mutex_unlock(&rsdgInitMutex); 226 return false; 227 } 228 } 229 rsdgThreadTLSKeyCount++; 230 pthread_mutex_unlock(&rsdgInitMutex); 231 232 dc->mTlsStruct.mContext = rsc; 233 dc->mTlsStruct.mScript = NULL; 234 int status = pthread_setspecific(rsdgThreadTLSKey, &dc->mTlsStruct); 235 if (status) { 236 ALOGE("pthread_setspecific %i", status); 237 } 238 239 240 int cpu = sysconf(_SC_NPROCESSORS_ONLN); 241 if(rsc->props.mDebugMaxThreads) { 242 cpu = rsc->props.mDebugMaxThreads; 243 } 244 if (cpu < 2) { 245 cpu = 0; 246 } 247 ALOGV("%p Launching thread(s), CPUs %i", rsc, cpu); 248 249 dc->mWorkers.mCount = (uint32_t)cpu; 250 dc->mWorkers.mThreadId = (pthread_t *) calloc(dc->mWorkers.mCount, sizeof(pthread_t)); 251 dc->mWorkers.mNativeThreadId = (pid_t *) calloc(dc->mWorkers.mCount, sizeof(pid_t)); 252 dc->mWorkers.mLaunchSignals = new Signal[dc->mWorkers.mCount]; 253 dc->mWorkers.mLaunchCallback = NULL; 254 255 dc->mWorkers.mCompleteSignal.init(); 256 257 android_atomic_release_store(dc->mWorkers.mCount, &dc->mWorkers.mRunningCount); 258 android_atomic_release_store(0, &dc->mWorkers.mLaunchCount); 259 260 pthread_attr_t threadAttr; 261 status = pthread_attr_init(&threadAttr); 262 if (status) { 263 ALOGE("Failed to init thread attribute."); 264 return false; 265 } 266 267 for (uint32_t ct=0; ct < dc->mWorkers.mCount; ct++) { 268 status = pthread_create(&dc->mWorkers.mThreadId[ct], &threadAttr, HelperThreadProc, rsc); 269 if (status) { 270 dc->mWorkers.mCount = ct; 271 ALOGE("Created fewer than expected number of RS threads."); 272 break; 273 } 274 } 275 while (android_atomic_acquire_load(&dc->mWorkers.mRunningCount) != 0) { 276 usleep(100); 277 } 278 279 pthread_attr_destroy(&threadAttr); 280 return true; 281} 282 283 284void SetPriority(const Context *rsc, int32_t priority) { 285 RsdHal *dc = (RsdHal *)rsc->mHal.drv; 286 for (uint32_t ct=0; ct < dc->mWorkers.mCount; ct++) { 287 setpriority(PRIO_PROCESS, dc->mWorkers.mNativeThreadId[ct], priority); 288 } 289 if (dc->mHasGraphics) { 290 //rsdGLSetPriority(rsc, priority); 291 } 292} 293 294void Shutdown(Context *rsc) { 295 RsdHal *dc = (RsdHal *)rsc->mHal.drv; 296 297 dc->mExit = true; 298 dc->mWorkers.mLaunchData = NULL; 299 dc->mWorkers.mLaunchCallback = NULL; 300 android_atomic_release_store(dc->mWorkers.mCount, &dc->mWorkers.mRunningCount); 301 for (uint32_t ct = 0; ct < dc->mWorkers.mCount; ct++) { 302 dc->mWorkers.mLaunchSignals[ct].set(); 303 } 304 void *res; 305 for (uint32_t ct = 0; ct < dc->mWorkers.mCount; ct++) { 306 pthread_join(dc->mWorkers.mThreadId[ct], &res); 307 } 308 rsAssert(android_atomic_acquire_load(&dc->mWorkers.mRunningCount) == 0); 309 310 // Global structure cleanup. 311 pthread_mutex_lock(&rsdgInitMutex); 312 --rsdgThreadTLSKeyCount; 313 if (!rsdgThreadTLSKeyCount) { 314 pthread_key_delete(rsdgThreadTLSKey); 315 } 316 pthread_mutex_unlock(&rsdgInitMutex); 317 318} 319 320