blob: c05e0f902472b20e384da78657038f66e43a0f54 [file] [log] [blame]
Jason Sams221a4b12012-02-22 15:22:41 -08001/*
Tim Murraya4230962013-07-17 16:50:10 -07002 * Copyright (C) 2013 The Android Open Source Project
Jason Sams221a4b12012-02-22 15:22:41 -08003 *
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
Jason Sams221a4b12012-02-22 15:22:41 -080017#include <malloc.h>
18#include <string.h>
Tim Murray84bf2b82012-10-31 16:03:16 -070019#include <pthread.h>
Jason Sams221a4b12012-02-22 15:22:41 -080020
21#include "RenderScript.h"
Tim Murray89daad62013-07-29 14:30:02 -070022#include "rsCppStructs.h"
Tim Murrayeeaf7142013-09-09 15:03:50 -070023#include "rsCppInternal.h"
Jason Sams221a4b12012-02-22 15:22:41 -080024
Tim Murraya4230962013-07-17 16:50:10 -070025#include <dlfcn.h>
Tim Murray0f98d502014-01-15 14:35:31 -080026#include <unistd.h>
Tim Murraya4230962013-07-17 16:50:10 -070027
Tim Murray0f98d502014-01-15 14:35:31 -080028#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB) && defined(HAVE_ANDROID_OS)
Tim Murray4a92d122013-07-22 10:56:18 -070029#include <cutils/properties.h>
Tim Murray0f98d502014-01-15 14:35:31 -080030#else
31#include "rsCompatibilityLib.h"
Tim Murray4a92d122013-07-22 10:56:18 -070032#endif
33
Tim Murray0f98d502014-01-15 14:35:31 -080034
Jason Sams69cccdf2012-04-02 19:11:49 -070035using namespace android;
Tim Murray9eb7f4b2012-11-16 14:02:18 -080036using namespace RSC;
Jason Sams69cccdf2012-04-02 19:11:49 -070037
Tim Murray84bf2b82012-10-31 16:03:16 -070038bool RS::gInitialized = false;
Tim Murray4a92d122013-07-22 10:56:18 -070039bool RS::usingNative = false;
Tim Murray84bf2b82012-10-31 16:03:16 -070040pthread_mutex_t RS::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Chris Wailes44bef6f2014-08-12 13:51:10 -070041dispatchTable* RS::dispatch = nullptr;
Tim Murraya4230962013-07-17 16:50:10 -070042static int gInitError = 0;
Jason Sams221a4b12012-02-22 15:22:41 -080043
Tim Murray84bf2b82012-10-31 16:03:16 -070044RS::RS() {
Chris Wailes44bef6f2014-08-12 13:51:10 -070045 mDev = nullptr;
46 mContext = nullptr;
47 mErrorFunc = nullptr;
48 mMessageFunc = nullptr;
Jason Sams221a4b12012-02-22 15:22:41 -080049 mMessageRun = false;
Tim Murraya4230962013-07-17 16:50:10 -070050 mInit = false;
Tim Murray21fa7a02013-08-15 16:25:03 -070051 mCurrentError = RS_SUCCESS;
Jason Sams221a4b12012-02-22 15:22:41 -080052
53 memset(&mElements, 0, sizeof(mElements));
Tim Murray729b6fe2013-07-23 16:20:42 -070054 memset(&mSamplers, 0, sizeof(mSamplers));
Jason Sams221a4b12012-02-22 15:22:41 -080055}
56
Tim Murray84bf2b82012-10-31 16:03:16 -070057RS::~RS() {
Tim Murraya4230962013-07-17 16:50:10 -070058 if (mInit == true) {
59 mMessageRun = false;
Jason Sams221a4b12012-02-22 15:22:41 -080060
Xiaofei Wanfea96e82014-03-31 16:43:15 +080061 if (mContext) {
62 RS::dispatch->ContextDeinitToClient(mContext);
Jason Sams221a4b12012-02-22 15:22:41 -080063
Chris Wailes44bef6f2014-08-12 13:51:10 -070064 void *res = nullptr;
Xiaofei Wanfea96e82014-03-31 16:43:15 +080065 int status = pthread_join(mMessageThreadId, &res);
Jason Sams221a4b12012-02-22 15:22:41 -080066
Xiaofei Wanfea96e82014-03-31 16:43:15 +080067 RS::dispatch->ContextDestroy(mContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -070068 mContext = nullptr;
Xiaofei Wanfea96e82014-03-31 16:43:15 +080069 }
70 if (mDev) {
71 RS::dispatch->DeviceDestroy(mDev);
Chris Wailes44bef6f2014-08-12 13:51:10 -070072 mDev = nullptr;
Xiaofei Wanfea96e82014-03-31 16:43:15 +080073 }
Tim Murraya4230962013-07-17 16:50:10 -070074 }
Jason Sams221a4b12012-02-22 15:22:41 -080075}
76
Miao Wangbc10dff2015-04-03 17:44:55 -070077bool RS::init(const char * name, uint32_t flags) {
Tim Murraycaf41262013-12-13 12:54:37 -080078 return RS::init(name, RS_VERSION, flags);
Tim Murray84bf2b82012-10-31 16:03:16 -070079}
80
Tim Murray75e877d2013-09-11 14:45:20 -070081// this will only open API 19+ libRS
82// because that's when we changed libRS to extern "C" entry points
Miao Wange5428e62015-03-10 15:29:40 -070083static bool loadSO(const char* filename, int targetApi) {
Tim Murray4a92d122013-07-22 10:56:18 -070084 void* handle = dlopen(filename, RTLD_LAZY | RTLD_LOCAL);
Chris Wailes44bef6f2014-08-12 13:51:10 -070085 if (handle == nullptr) {
Tim Murray87c9d772013-12-03 12:42:00 -080086 ALOGV("couldn't dlopen %s, %s", filename, dlerror());
Tim Murray4a92d122013-07-22 10:56:18 -070087 return false;
88 }
Tim Murraya4230962013-07-17 16:50:10 -070089
Miao Wange5428e62015-03-10 15:29:40 -070090 if (loadSymbols(handle, *RS::dispatch, targetApi) == false) {
Tim Murray87c9d772013-12-03 12:42:00 -080091 ALOGV("%s init failed!", filename);
Tim Murray4a92d122013-07-22 10:56:18 -070092 return false;
93 }
Tim Murray84e3dea2013-09-09 16:12:51 -070094 //ALOGE("Successfully loaded %s", filename);
Tim Murray4a92d122013-07-22 10:56:18 -070095 return true;
96}
97
98static uint32_t getProp(const char *str) {
Tim Murraycbbac9f2014-09-10 15:42:29 -070099#if !defined(__LP64__) && !defined(RS_SERVER) && defined(HAVE_ANDROID_OS)
Tim Murray4a92d122013-07-22 10:56:18 -0700100 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) {
Tim Murraya4230962013-07-17 16:50:10 -0700109 pthread_mutex_lock(&gInitMutex);
110 if (gInitError) {
111 goto error;
112 } else if (gInitialized) {
Tim Murray47666f52013-07-29 14:32:34 -0700113 pthread_mutex_unlock(&gInitMutex);
Tim Murraya4230962013-07-17 16:50:10 -0700114 return true;
115 }
Tim Murraya4230962013-07-17 16:50:10 -0700116
117 RS::dispatch = new dispatchTable;
Tim Murray4a92d122013-07-22 10:56:18 -0700118
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) {
Miao Wange5428e62015-03-10 15:29:40 -0700122 usingNative = loadSO("libRS.so", targetApi);
Tim Murray4a92d122013-07-22 10:56:18 -0700123 }
124 if (usingNative == false) {
Miao Wange5428e62015-03-10 15:29:40 -0700125 if (loadSO("libRSSupport.so", targetApi) == false) {
Tim Murray4a92d122013-07-22 10:56:18 -0700126 ALOGE("Failed to load libRS.so and libRSSupport.so");
127 goto error;
128 }
Tim Murraya4230962013-07-17 16:50:10 -0700129 }
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
Miao Wangbc10dff2015-04-03 17:44:55 -0700142bool RS::init(const char * name, int targetApi, uint32_t flags) {
Tim Murraycaf41262013-12-13 12:54:37 -0800143 if (mInit) {
144 return true;
145 }
146
Tim Murraya4230962013-07-17 16:50:10 -0700147 if (initDispatch(targetApi) == false) {
148 ALOGE("Couldn't initialize dispatch table");
149 return false;
150 }
151
Miao Wangbc10dff2015-04-03 17:44:55 -0700152 uint32_t nameLen = strlen(name);
153 if (nameLen > PATH_MAX) {
154 ALOGE("The path to the cache directory is too long");
155 return false;
156 }
157 memcpy(mCacheDir, name, nameLen);
158 mCacheDir[nameLen] = 0; //add the null character even if the user does not.
159 mCacheDirLen = nameLen + 1;
Tim Murraycaf41262013-12-13 12:54:37 -0800160
Tim Murraya4230962013-07-17 16:50:10 -0700161 mDev = RS::dispatch->DeviceCreate();
Jason Sams221a4b12012-02-22 15:22:41 -0800162 if (mDev == 0) {
163 ALOGE("Device creation failed");
164 return false;
165 }
166
Jason Samsbfa5a8e2014-05-20 18:16:20 -0700167 if (flags & ~(RS_CONTEXT_SYNCHRONOUS | RS_CONTEXT_LOW_LATENCY |
168 RS_CONTEXT_LOW_POWER)) {
Tim Murray84e3dea2013-09-09 16:12:51 -0700169 ALOGE("Invalid flags passed");
170 return false;
171 }
172
173 mContext = RS::dispatch->ContextCreate(mDev, 0, targetApi, RS_CONTEXT_TYPE_NORMAL, flags);
Jason Sams221a4b12012-02-22 15:22:41 -0800174 if (mContext == 0) {
175 ALOGE("Context creation failed");
176 return false;
177 }
178
Jason Sams221a4b12012-02-22 15:22:41 -0800179 pid_t mNativeMessageThreadId;
180
Chris Wailes44bef6f2014-08-12 13:51:10 -0700181 int status = pthread_create(&mMessageThreadId, nullptr, threadProc, this);
Jason Sams221a4b12012-02-22 15:22:41 -0800182 if (status) {
Tim Murray84bf2b82012-10-31 16:03:16 -0700183 ALOGE("Failed to start RS message thread.");
Jason Sams221a4b12012-02-22 15:22:41 -0800184 return false;
185 }
186 // Wait for the message thread to be active.
187 while (!mMessageRun) {
188 usleep(1000);
189 }
190
Tim Murraya4230962013-07-17 16:50:10 -0700191 mInit = true;
192
Jason Sams221a4b12012-02-22 15:22:41 -0800193 return true;
194}
195
Tim Murray21fa7a02013-08-15 16:25:03 -0700196void RS::throwError(RSError error, const char *errMsg) {
197 if (mCurrentError == RS_SUCCESS) {
198 mCurrentError = error;
199 ALOGE("RS CPP error: %s", errMsg);
200 } else {
201 ALOGE("RS CPP error (masked by previous error): %s", errMsg);
202 }
Jason Samsb2e3dc52012-02-23 17:14:39 -0800203}
204
Tim Murray10913a52013-08-20 17:19:47 -0700205RSError RS::getError() {
206 return mCurrentError;
207}
208
Jason Samsb2e3dc52012-02-23 17:14:39 -0800209
Tim Murray84bf2b82012-10-31 16:03:16 -0700210void * RS::threadProc(void *vrsc) {
211 RS *rs = static_cast<RS *>(vrsc);
Jason Sams221a4b12012-02-22 15:22:41 -0800212 size_t rbuf_size = 256;
213 void * rbuf = malloc(rbuf_size);
214
Tim Murraya4230962013-07-17 16:50:10 -0700215 RS::dispatch->ContextInitToClient(rs->mContext);
Jason Sams221a4b12012-02-22 15:22:41 -0800216 rs->mMessageRun = true;
217
218 while (rs->mMessageRun) {
219 size_t receiveLen = 0;
220 uint32_t usrID = 0;
221 uint32_t subID = 0;
Tim Murraya4230962013-07-17 16:50:10 -0700222 RsMessageToClientType r = RS::dispatch->ContextPeekMessage(rs->mContext,
223 &receiveLen, sizeof(receiveLen),
224 &usrID, sizeof(usrID));
Jason Sams221a4b12012-02-22 15:22:41 -0800225
226 if (receiveLen >= rbuf_size) {
227 rbuf_size = receiveLen + 32;
228 rbuf = realloc(rbuf, rbuf_size);
229 }
230 if (!rbuf) {
Tim Murray84bf2b82012-10-31 16:03:16 -0700231 ALOGE("RS::message handler realloc error %zu", rbuf_size);
Jason Sams221a4b12012-02-22 15:22:41 -0800232 // No clean way to recover now?
233 }
Tim Murraya4230962013-07-17 16:50:10 -0700234 RS::dispatch->ContextGetMessage(rs->mContext, rbuf, rbuf_size, &receiveLen, sizeof(receiveLen),
Jason Sams221a4b12012-02-22 15:22:41 -0800235 &subID, sizeof(subID));
236
237 switch(r) {
238 case RS_MESSAGE_TO_CLIENT_ERROR:
239 ALOGE("RS Error %s", (const char *)rbuf);
Tim Murray21fa7a02013-08-15 16:25:03 -0700240 rs->throwError(RS_ERROR_RUNTIME_ERROR, "Error returned from runtime");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700241 if(rs->mMessageFunc != nullptr) {
Jason Sams221a4b12012-02-22 15:22:41 -0800242 rs->mErrorFunc(usrID, (const char *)rbuf);
243 }
244 break;
Stephen Hines76a1be42012-11-26 16:26:03 -0800245 case RS_MESSAGE_TO_CLIENT_NONE:
Jason Sams221a4b12012-02-22 15:22:41 -0800246 case RS_MESSAGE_TO_CLIENT_EXCEPTION:
Stephen Hines76a1be42012-11-26 16:26:03 -0800247 case RS_MESSAGE_TO_CLIENT_RESIZE:
Jason Sams221a4b12012-02-22 15:22:41 -0800248 // teardown. But we want to avoid starving other threads during
249 // teardown by yielding until the next line in the destructor can
Stephen Hines76a1be42012-11-26 16:26:03 -0800250 // execute to set mRun = false. Note that the FIFO sends an
251 // empty NONE message when it reaches its destructor.
Jason Sams221a4b12012-02-22 15:22:41 -0800252 usleep(1000);
253 break;
254 case RS_MESSAGE_TO_CLIENT_USER:
Chris Wailes44bef6f2014-08-12 13:51:10 -0700255 if(rs->mMessageFunc != nullptr) {
Jason Sams221a4b12012-02-22 15:22:41 -0800256 rs->mMessageFunc(usrID, rbuf, receiveLen);
257 } else {
258 ALOGE("Received a message from the script with no message handler installed.");
259 }
260 break;
261
262 default:
Tim Murray84bf2b82012-10-31 16:03:16 -0700263 ALOGE("RS unknown message type %i", r);
Jason Sams221a4b12012-02-22 15:22:41 -0800264 }
265 }
266
267 if (rbuf) {
268 free(rbuf);
269 }
Tim Murray87c9d772013-12-03 12:42:00 -0800270 ALOGV("RS Message thread exiting.");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700271 return nullptr;
Jason Sams221a4b12012-02-22 15:22:41 -0800272}
273
Tim Murray84bf2b82012-10-31 16:03:16 -0700274void RS::setErrorHandler(ErrorHandlerFunc_t func) {
Jason Sams221a4b12012-02-22 15:22:41 -0800275 mErrorFunc = func;
276}
277
Tim Murray84bf2b82012-10-31 16:03:16 -0700278void RS::setMessageHandler(MessageHandlerFunc_t func) {
Jason Sams221a4b12012-02-22 15:22:41 -0800279 mMessageFunc = func;
280}
Tim Murraybaca6c32012-11-14 16:51:46 -0800281
282void RS::finish() {
Tim Murraya4230962013-07-17 16:50:10 -0700283 RS::dispatch->ContextFinish(mContext);
Tim Murraybaca6c32012-11-14 16:51:46 -0800284}