blob: de22dea666e4ef99cbf7a1230b78059dc4fdc13a [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
Tim Murraycaf41262013-12-13 12:54:37 -080077bool RS::init(std::string name, uint32_t flags) {
78 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
Tim Murray4a92d122013-07-22 10:56:18 -070083static bool loadSO(const char* filename) {
84 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 Wang48d2eae2014-10-16 11:48:28 -070090 if (loadSymbols(handle, *RS::dispatch) == 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) {
122 usingNative = loadSO("libRS.so");
123 }
124 if (usingNative == false) {
125 if (loadSO("libRSSupport.so") == false) {
126 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
Tim Murraycaf41262013-12-13 12:54:37 -0800142bool RS::init(std::string &name, int targetApi, uint32_t flags) {
143 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
Tim Murraycaf41262013-12-13 12:54:37 -0800152 mCacheDir = name;
Tim Murraycaf41262013-12-13 12:54:37 -0800153
Tim Murraya4230962013-07-17 16:50:10 -0700154 mDev = RS::dispatch->DeviceCreate();
Jason Sams221a4b12012-02-22 15:22:41 -0800155 if (mDev == 0) {
156 ALOGE("Device creation failed");
157 return false;
158 }
159
Jason Samsbfa5a8e2014-05-20 18:16:20 -0700160 if (flags & ~(RS_CONTEXT_SYNCHRONOUS | RS_CONTEXT_LOW_LATENCY |
161 RS_CONTEXT_LOW_POWER)) {
Tim Murray84e3dea2013-09-09 16:12:51 -0700162 ALOGE("Invalid flags passed");
163 return false;
164 }
165
166 mContext = RS::dispatch->ContextCreate(mDev, 0, targetApi, RS_CONTEXT_TYPE_NORMAL, flags);
Jason Sams221a4b12012-02-22 15:22:41 -0800167 if (mContext == 0) {
168 ALOGE("Context creation failed");
169 return false;
170 }
171
Jason Sams221a4b12012-02-22 15:22:41 -0800172 pid_t mNativeMessageThreadId;
173
Chris Wailes44bef6f2014-08-12 13:51:10 -0700174 int status = pthread_create(&mMessageThreadId, nullptr, threadProc, this);
Jason Sams221a4b12012-02-22 15:22:41 -0800175 if (status) {
Tim Murray84bf2b82012-10-31 16:03:16 -0700176 ALOGE("Failed to start RS message thread.");
Jason Sams221a4b12012-02-22 15:22:41 -0800177 return false;
178 }
179 // Wait for the message thread to be active.
180 while (!mMessageRun) {
181 usleep(1000);
182 }
183
Tim Murraya4230962013-07-17 16:50:10 -0700184 mInit = true;
185
Jason Sams221a4b12012-02-22 15:22:41 -0800186 return true;
187}
188
Tim Murray21fa7a02013-08-15 16:25:03 -0700189void RS::throwError(RSError error, const char *errMsg) {
190 if (mCurrentError == RS_SUCCESS) {
191 mCurrentError = error;
192 ALOGE("RS CPP error: %s", errMsg);
193 } else {
194 ALOGE("RS CPP error (masked by previous error): %s", errMsg);
195 }
Jason Samsb2e3dc52012-02-23 17:14:39 -0800196}
197
Tim Murray10913a52013-08-20 17:19:47 -0700198RSError RS::getError() {
199 return mCurrentError;
200}
201
Jason Samsb2e3dc52012-02-23 17:14:39 -0800202
Tim Murray84bf2b82012-10-31 16:03:16 -0700203void * RS::threadProc(void *vrsc) {
204 RS *rs = static_cast<RS *>(vrsc);
Jason Sams221a4b12012-02-22 15:22:41 -0800205 size_t rbuf_size = 256;
206 void * rbuf = malloc(rbuf_size);
207
Tim Murraya4230962013-07-17 16:50:10 -0700208 RS::dispatch->ContextInitToClient(rs->mContext);
Jason Sams221a4b12012-02-22 15:22:41 -0800209 rs->mMessageRun = true;
210
211 while (rs->mMessageRun) {
212 size_t receiveLen = 0;
213 uint32_t usrID = 0;
214 uint32_t subID = 0;
Tim Murraya4230962013-07-17 16:50:10 -0700215 RsMessageToClientType r = RS::dispatch->ContextPeekMessage(rs->mContext,
216 &receiveLen, sizeof(receiveLen),
217 &usrID, sizeof(usrID));
Jason Sams221a4b12012-02-22 15:22:41 -0800218
219 if (receiveLen >= rbuf_size) {
220 rbuf_size = receiveLen + 32;
221 rbuf = realloc(rbuf, rbuf_size);
222 }
223 if (!rbuf) {
Tim Murray84bf2b82012-10-31 16:03:16 -0700224 ALOGE("RS::message handler realloc error %zu", rbuf_size);
Jason Sams221a4b12012-02-22 15:22:41 -0800225 // No clean way to recover now?
226 }
Tim Murraya4230962013-07-17 16:50:10 -0700227 RS::dispatch->ContextGetMessage(rs->mContext, rbuf, rbuf_size, &receiveLen, sizeof(receiveLen),
Jason Sams221a4b12012-02-22 15:22:41 -0800228 &subID, sizeof(subID));
229
230 switch(r) {
231 case RS_MESSAGE_TO_CLIENT_ERROR:
232 ALOGE("RS Error %s", (const char *)rbuf);
Tim Murray21fa7a02013-08-15 16:25:03 -0700233 rs->throwError(RS_ERROR_RUNTIME_ERROR, "Error returned from runtime");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700234 if(rs->mMessageFunc != nullptr) {
Jason Sams221a4b12012-02-22 15:22:41 -0800235 rs->mErrorFunc(usrID, (const char *)rbuf);
236 }
237 break;
Stephen Hines76a1be42012-11-26 16:26:03 -0800238 case RS_MESSAGE_TO_CLIENT_NONE:
Jason Sams221a4b12012-02-22 15:22:41 -0800239 case RS_MESSAGE_TO_CLIENT_EXCEPTION:
Stephen Hines76a1be42012-11-26 16:26:03 -0800240 case RS_MESSAGE_TO_CLIENT_RESIZE:
Jason Sams221a4b12012-02-22 15:22:41 -0800241 // teardown. But we want to avoid starving other threads during
242 // teardown by yielding until the next line in the destructor can
Stephen Hines76a1be42012-11-26 16:26:03 -0800243 // execute to set mRun = false. Note that the FIFO sends an
244 // empty NONE message when it reaches its destructor.
Jason Sams221a4b12012-02-22 15:22:41 -0800245 usleep(1000);
246 break;
247 case RS_MESSAGE_TO_CLIENT_USER:
Chris Wailes44bef6f2014-08-12 13:51:10 -0700248 if(rs->mMessageFunc != nullptr) {
Jason Sams221a4b12012-02-22 15:22:41 -0800249 rs->mMessageFunc(usrID, rbuf, receiveLen);
250 } else {
251 ALOGE("Received a message from the script with no message handler installed.");
252 }
253 break;
254
255 default:
Tim Murray84bf2b82012-10-31 16:03:16 -0700256 ALOGE("RS unknown message type %i", r);
Jason Sams221a4b12012-02-22 15:22:41 -0800257 }
258 }
259
260 if (rbuf) {
261 free(rbuf);
262 }
Tim Murray87c9d772013-12-03 12:42:00 -0800263 ALOGV("RS Message thread exiting.");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700264 return nullptr;
Jason Sams221a4b12012-02-22 15:22:41 -0800265}
266
Tim Murray84bf2b82012-10-31 16:03:16 -0700267void RS::setErrorHandler(ErrorHandlerFunc_t func) {
Jason Sams221a4b12012-02-22 15:22:41 -0800268 mErrorFunc = func;
269}
270
Tim Murray84bf2b82012-10-31 16:03:16 -0700271void RS::setMessageHandler(MessageHandlerFunc_t func) {
Jason Sams221a4b12012-02-22 15:22:41 -0800272 mMessageFunc = func;
273}
Tim Murraybaca6c32012-11-14 16:51:46 -0800274
275void RS::finish() {
Tim Murraya4230962013-07-17 16:50:10 -0700276 RS::dispatch->ContextFinish(mContext);
Tim Murraybaca6c32012-11-14 16:51:46 -0800277}