RenderScript.cpp revision fda559674cb1bd16b885f97951bda775cdc3af5c
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#include <malloc.h> 18#include <string.h> 19#include <pthread.h> 20 21#include "RenderScript.h" 22#include "rsCppStructs.h" 23#include "rsCppInternal.h" 24 25#include <dlfcn.h> 26#include <unistd.h> 27 28#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB) && defined(__ANDROID__) 29#include <cutils/properties.h> 30#else 31#include "rsCompatibilityLib.h" 32#endif 33 34 35using namespace android; 36using namespace RSC; 37 38bool RS::gInitialized = false; 39bool RS::usingNative = false; 40pthread_mutex_t RS::gInitMutex = PTHREAD_MUTEX_INITIALIZER; 41dispatchTable* RS::dispatch = nullptr; 42static int gInitError = 0; 43 44RS::RS() { 45 mDev = nullptr; 46 mContext = nullptr; 47 mErrorFunc = nullptr; 48 mMessageFunc = nullptr; 49 mMessageRun = false; 50 mInit = false; 51 mCurrentError = RS_SUCCESS; 52 53 memset(&mElements, 0, sizeof(mElements)); 54 memset(&mSamplers, 0, sizeof(mSamplers)); 55} 56 57RS::~RS() { 58 if (mInit == true) { 59 mMessageRun = false; 60 61 if (mContext) { 62 finish(); 63 RS::dispatch->ContextDeinitToClient(mContext); 64 65 void *res = nullptr; 66 int status = pthread_join(mMessageThreadId, &res); 67 68 RS::dispatch->ContextDestroy(mContext); 69 mContext = nullptr; 70 } 71 if (mDev) { 72 RS::dispatch->DeviceDestroy(mDev); 73 mDev = nullptr; 74 } 75 } 76} 77 78bool RS::init(const char * name, uint32_t flags) { 79 return RS::init(name, flags, 0); 80} 81 82// This will only open API 19+ libRS, because that's when 83// we changed libRS to extern "C" entry points. 84static bool loadSO(const char* filename, int targetApi) { 85 void* handle = dlopen(filename, RTLD_LAZY | RTLD_LOCAL); 86 if (handle == nullptr) { 87 ALOGV("couldn't dlopen %s, %s", filename, dlerror()); 88 return false; 89 } 90 91 if (loadSymbols(handle, *RS::dispatch, targetApi) == false) { 92 ALOGV("%s init failed!", filename); 93 return false; 94 } 95 return true; 96} 97 98static uint32_t getProp(const char *str) { 99#if !defined(__LP64__) && !defined(RS_SERVER) && defined(__ANDROID__) 100 char buf[256]; 101 property_get(str, buf, "0"); 102 return atoi(buf); 103#else 104 return 0; 105#endif 106} 107 108bool RS::initDispatch(int targetApi) { 109 pthread_mutex_lock(&gInitMutex); 110 if (gInitError) { 111 goto error; 112 } else if (gInitialized) { 113 pthread_mutex_unlock(&gInitMutex); 114 return true; 115 } 116 117 RS::dispatch = new dispatchTable; 118 119 // Attempt to load libRS, load libRSSupport on failure. 120 // If property is set, proceed directly to libRSSupport. 121 if (getProp("debug.rs.forcecompat") == 0) { 122 usingNative = loadSO("libRS.so", targetApi); 123 } 124 if (usingNative == false) { 125 if (loadSO("libRSSupport.so", targetApi) == false) { 126 ALOGE("Failed to load libRS.so and libRSSupport.so"); 127 goto error; 128 } 129 } 130 131 gInitialized = true; 132 133 pthread_mutex_unlock(&gInitMutex); 134 return true; 135 136 error: 137 gInitError = 1; 138 pthread_mutex_unlock(&gInitMutex); 139 return false; 140} 141 142bool RS::init(const char * name, uint32_t flags, int targetApi) { 143 if (mInit) { 144 return true; 145 } 146 // When using default value 0, set targetApi to RS_VERSION, 147 // to preserve the behavior of existing apps. 148 if (targetApi == 0) { 149 targetApi = RS_VERSION; 150 } 151 152 if (initDispatch(targetApi) == false) { 153 ALOGE("Couldn't initialize dispatch table"); 154 return false; 155 } 156 157 uint32_t nameLen = strlen(name); 158 if (nameLen > PATH_MAX) { 159 ALOGE("The path to the cache directory is too long"); 160 return false; 161 } 162 memcpy(mCacheDir, name, nameLen); 163 // Add the null character even if the user does not. 164 mCacheDir[nameLen] = 0; 165 mCacheDirLen = nameLen + 1; 166 167 mDev = RS::dispatch->DeviceCreate(); 168 if (mDev == 0) { 169 ALOGE("Device creation failed"); 170 return false; 171 } 172 173 if (flags & ~(RS_CONTEXT_SYNCHRONOUS | RS_CONTEXT_LOW_LATENCY | 174 RS_CONTEXT_LOW_POWER | RS_CONTEXT_WAIT_FOR_ATTACH | 175 RS_CONTEXT_OPT_LEVEL_0)) { 176 ALOGE("Invalid flags passed"); 177 return false; 178 } 179 180 mContext = RS::dispatch->ContextCreate(mDev, 0, targetApi, RS_CONTEXT_TYPE_NORMAL, flags); 181 if (mContext == 0) { 182 ALOGE("Context creation failed"); 183 return false; 184 } 185 186 pid_t mNativeMessageThreadId; 187 188 int status = pthread_create(&mMessageThreadId, nullptr, threadProc, this); 189 if (status) { 190 ALOGE("Failed to start RS message thread."); 191 return false; 192 } 193 // Wait for the message thread to be active. 194 while (!mMessageRun) { 195 usleep(1000); 196 } 197 198 mInit = true; 199 200 return true; 201} 202 203void RS::throwError(RSError error, const char *errMsg) { 204 if (mCurrentError == RS_SUCCESS) { 205 mCurrentError = error; 206 ALOGE("RS CPP error: %s", errMsg); 207 } else { 208 ALOGE("RS CPP error (masked by previous error): %s", errMsg); 209 } 210} 211 212RSError RS::getError() { 213 return mCurrentError; 214} 215 216 217void * RS::threadProc(void *vrsc) { 218 RS *rs = static_cast<RS *>(vrsc); 219 size_t rbuf_size = 256; 220 void * rbuf = malloc(rbuf_size); 221 222 RS::dispatch->ContextInitToClient(rs->mContext); 223 rs->mMessageRun = true; 224 225 while (rs->mMessageRun) { 226 size_t receiveLen = 0; 227 uint32_t usrID = 0; 228 uint32_t subID = 0; 229 RsMessageToClientType r = RS::dispatch->ContextPeekMessage(rs->mContext, 230 &receiveLen, sizeof(receiveLen), 231 &usrID, sizeof(usrID)); 232 233 if (receiveLen >= rbuf_size) { 234 rbuf_size = receiveLen + 32; 235 rbuf = realloc(rbuf, rbuf_size); 236 } 237 if (!rbuf) { 238 ALOGE("RS::message handler realloc error %zu", rbuf_size); 239 // No clean way to recover now? 240 } 241 RS::dispatch->ContextGetMessage(rs->mContext, rbuf, rbuf_size, &receiveLen, sizeof(receiveLen), 242 &subID, sizeof(subID)); 243 244 switch(r) { 245 case RS_MESSAGE_TO_CLIENT_ERROR: 246 ALOGE("RS Error %s", (const char *)rbuf); 247 rs->throwError(RS_ERROR_RUNTIME_ERROR, "Error returned from runtime"); 248 if(rs->mMessageFunc != nullptr) { 249 rs->mErrorFunc(usrID, (const char *)rbuf); 250 } 251 break; 252 case RS_MESSAGE_TO_CLIENT_NONE: 253 case RS_MESSAGE_TO_CLIENT_EXCEPTION: 254 case RS_MESSAGE_TO_CLIENT_RESIZE: 255 /* 256 * Teardown. We want to avoid starving other threads during 257 * teardown by yielding until the next line in the destructor can 258 * execute to set mRun = false. Note that the FIFO sends an 259 * empty NONE message when it reaches its destructor. 260 */ 261 usleep(1000); 262 break; 263 case RS_MESSAGE_TO_CLIENT_USER: 264 if(rs->mMessageFunc != nullptr) { 265 rs->mMessageFunc(usrID, rbuf, receiveLen); 266 } else { 267 ALOGE("Received a message from the script with no message handler installed."); 268 } 269 break; 270 271 default: 272 ALOGE("RS unknown message type %i", r); 273 } 274 } 275 276 if (rbuf) { 277 free(rbuf); 278 } 279 ALOGV("RS Message thread exiting."); 280 return nullptr; 281} 282 283void RS::setErrorHandler(ErrorHandlerFunc_t func) { 284 mErrorFunc = func; 285} 286 287void RS::setMessageHandler(MessageHandlerFunc_t func) { 288 mMessageFunc = func; 289} 290 291void RS::finish() { 292 RS::dispatch->ContextFinish(mContext); 293} 294