blob: 6ab224048d1d08cf0fd9f4d0b81c1e5335a8a5e3 [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"
Jason Sams221a4b12012-02-22 15:22:41 -080023
Tim Murraya4230962013-07-17 16:50:10 -070024#include <dlfcn.h>
25
Tim Murray4a92d122013-07-22 10:56:18 -070026#if !defined(RS_SERVER) && defined(HAVE_ANDROID_OS)
27#include <cutils/properties.h>
28#endif
29
Tim Murraya4230962013-07-17 16:50:10 -070030#define LOG_NDEBUG 0
31#define LOG_TAG "rsC++"
32
Jason Sams69cccdf2012-04-02 19:11:49 -070033using namespace android;
Tim Murray9eb7f4b2012-11-16 14:02:18 -080034using namespace RSC;
Jason Sams69cccdf2012-04-02 19:11:49 -070035
Tim Murray84bf2b82012-10-31 16:03:16 -070036bool RS::gInitialized = false;
Tim Murray4a92d122013-07-22 10:56:18 -070037bool RS::usingNative = false;
Tim Murray84bf2b82012-10-31 16:03:16 -070038pthread_mutex_t RS::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Tim Murraya4230962013-07-17 16:50:10 -070039dispatchTable* RS::dispatch = NULL;
Tim Murraya4230962013-07-17 16:50:10 -070040static int gInitError = 0;
Jason Sams221a4b12012-02-22 15:22:41 -080041
Tim Murray84bf2b82012-10-31 16:03:16 -070042RS::RS() {
Jason Sams221a4b12012-02-22 15:22:41 -080043 mDev = NULL;
44 mContext = NULL;
45 mErrorFunc = NULL;
46 mMessageFunc = NULL;
47 mMessageRun = false;
Tim Murraya4230962013-07-17 16:50:10 -070048 mInit = false;
Tim Murray21fa7a02013-08-15 16:25:03 -070049 mCurrentError = RS_SUCCESS;
Jason Sams221a4b12012-02-22 15:22:41 -080050
51 memset(&mElements, 0, sizeof(mElements));
Tim Murray729b6fe2013-07-23 16:20:42 -070052 memset(&mSamplers, 0, sizeof(mSamplers));
Jason Sams221a4b12012-02-22 15:22:41 -080053}
54
Tim Murray84bf2b82012-10-31 16:03:16 -070055RS::~RS() {
Tim Murraya4230962013-07-17 16:50:10 -070056 if (mInit == true) {
57 mMessageRun = false;
Jason Sams221a4b12012-02-22 15:22:41 -080058
Tim Murraya4230962013-07-17 16:50:10 -070059 RS::dispatch->ContextDeinitToClient(mContext);
Jason Sams221a4b12012-02-22 15:22:41 -080060
Tim Murraya4230962013-07-17 16:50:10 -070061 void *res = NULL;
62 int status = pthread_join(mMessageThreadId, &res);
Jason Sams221a4b12012-02-22 15:22:41 -080063
Tim Murraya4230962013-07-17 16:50:10 -070064 RS::dispatch->ContextDestroy(mContext);
65 mContext = NULL;
66 RS::dispatch->DeviceDestroy(mDev);
67 mDev = NULL;
68 }
Jason Sams221a4b12012-02-22 15:22:41 -080069}
70
Tim Murray4d252d62012-11-29 14:37:59 -080071bool RS::init(bool forceCpu, bool synchronous) {
72 return RS::init(RS_VERSION, forceCpu, synchronous);
Tim Murray84bf2b82012-10-31 16:03:16 -070073}
74
Tim Murray0b8a2be2013-07-23 16:25:41 -070075static bool loadSymbols(void* handle) {
76
77 RS::dispatch->AllocationGetType = (AllocationGetTypeFnPtr)dlsym(handle, "rsaAllocationGetType");
78 if (RS::dispatch->AllocationGetType == NULL) {
79 ALOGE("Couldn't initialize RS::dispatch->AllocationGetType");
80 return false;
81 }
82 RS::dispatch->TypeGetNativeData = (TypeGetNativeDataFnPtr)dlsym(handle, "rsaTypeGetNativeData");
83 if (RS::dispatch->TypeGetNativeData == NULL) {
84 ALOGE("Couldn't initialize RS::dispatch->TypeGetNativeData");
85 return false;
86 }
87 RS::dispatch->ElementGetNativeData = (ElementGetNativeDataFnPtr)dlsym(handle, "rsaElementGetNativeData");
88 if (RS::dispatch->ElementGetNativeData == NULL) {
89 ALOGE("Couldn't initialize RS::dispatch->ElementGetNativeData");
90 return false;
91 }
92 RS::dispatch->ElementGetSubElements = (ElementGetSubElementsFnPtr)dlsym(handle, "rsaElementGetSubElements");
93 if (RS::dispatch->ElementGetSubElements == NULL) {
94 ALOGE("Couldn't initialize RS::dispatch->ElementGetSubElements");
95 return false;
96 }
97 RS::dispatch->DeviceCreate = (DeviceCreateFnPtr)dlsym(handle, "rsDeviceCreate");
98 if (RS::dispatch->DeviceCreate == NULL) {
99 ALOGE("Couldn't initialize RS::dispatch->DeviceCreate");
100 return false;
101 }
102 RS::dispatch->DeviceDestroy = (DeviceDestroyFnPtr)dlsym(handle, "rsDeviceDestroy");
103 if (RS::dispatch->DeviceDestroy == NULL) {
104 ALOGE("Couldn't initialize RS::dispatch->DeviceDestroy");
105 return false;
106 }
107 RS::dispatch->DeviceSetConfig = (DeviceSetConfigFnPtr)dlsym(handle, "rsDeviceSetConfig");
108 if (RS::dispatch->DeviceSetConfig == NULL) {
109 ALOGE("Couldn't initialize RS::dispatch->DeviceSetConfig");
110 return false;
111 }
112 RS::dispatch->ContextCreate = (ContextCreateFnPtr)dlsym(handle, "rsContextCreate");;
113 if (RS::dispatch->ContextCreate == NULL) {
114 ALOGE("Couldn't initialize RS::dispatch->ContextCreate");
115 return false;
116 }
Tim Murray4a92d122013-07-22 10:56:18 -0700117 RS::dispatch->GetName = (GetNameFnPtr)dlsym(handle, "rsaGetName");;
118 if (RS::dispatch->GetName == NULL) {
119 ALOGE("Couldn't initialize RS::dispatch->GetName");
120 return false;
121 }
Tim Murray0b8a2be2013-07-23 16:25:41 -0700122 RS::dispatch->ContextDestroy = (ContextDestroyFnPtr)dlsym(handle, "rsContextDestroy");
123 if (RS::dispatch->ContextDestroy == NULL) {
124 ALOGE("Couldn't initialize RS::dispatch->ContextDestroy");
125 return false;
126 }
127 RS::dispatch->ContextGetMessage = (ContextGetMessageFnPtr)dlsym(handle, "rsContextGetMessage");
128 if (RS::dispatch->ContextGetMessage == NULL) {
129 ALOGE("Couldn't initialize RS::dispatch->ContextGetMessage");
130 return false;
131 }
132 RS::dispatch->ContextPeekMessage = (ContextPeekMessageFnPtr)dlsym(handle, "rsContextPeekMessage");
133 if (RS::dispatch->ContextPeekMessage == NULL) {
134 ALOGE("Couldn't initialize RS::dispatch->ContextPeekMessage");
135 return false;
136 }
137 RS::dispatch->ContextSendMessage = (ContextSendMessageFnPtr)dlsym(handle, "rsContextSendMessage");
138 if (RS::dispatch->ContextSendMessage == NULL) {
139 ALOGE("Couldn't initialize RS::dispatch->ContextSendMessage");
140 return false;
141 }
142 RS::dispatch->ContextInitToClient = (ContextInitToClientFnPtr)dlsym(handle, "rsContextInitToClient");
143 if (RS::dispatch->ContextInitToClient == NULL) {
144 ALOGE("Couldn't initialize RS::dispatch->ContextInitToClient");
145 return false;
146 }
147 RS::dispatch->ContextDeinitToClient = (ContextDeinitToClientFnPtr)dlsym(handle, "rsContextDeinitToClient");
148 if (RS::dispatch->ContextDeinitToClient == NULL) {
149 ALOGE("Couldn't initialize RS::dispatch->ContextDeinitToClient");
150 return false;
151 }
152 RS::dispatch->TypeCreate = (TypeCreateFnPtr)dlsym(handle, "rsTypeCreate");
153 if (RS::dispatch->TypeCreate == NULL) {
154 ALOGE("Couldn't initialize RS::dispatch->TypeCreate");
155 return false;
156 }
157 RS::dispatch->AllocationCreateTyped = (AllocationCreateTypedFnPtr)dlsym(handle, "rsAllocationCreateTyped");
158 if (RS::dispatch->AllocationCreateTyped == NULL) {
159 ALOGE("Couldn't initialize RS::dispatch->AllocationCreateTyped");
160 return false;
161 }
162 RS::dispatch->AllocationCreateFromBitmap = (AllocationCreateFromBitmapFnPtr)dlsym(handle, "rsAllocationCreateFromBitmap");
163 if (RS::dispatch->AllocationCreateFromBitmap == NULL) {
164 ALOGE("Couldn't initialize RS::dispatch->AllocationCreateFromBitmap");
165 return false;
166 }
167 RS::dispatch->AllocationCubeCreateFromBitmap = (AllocationCubeCreateFromBitmapFnPtr)dlsym(handle, "rsAllocationCubeCreateFromBitmap");
168 if (RS::dispatch->AllocationCubeCreateFromBitmap == NULL) {
169 ALOGE("Couldn't initialize RS::dispatch->AllocationCubeCreateFromBitmap");
170 return false;
171 }
172 RS::dispatch->AllocationGetSurface = (AllocationGetSurfaceFnPtr)dlsym(handle, "rsAllocationGetSurface");
173 if (RS::dispatch->AllocationGetSurface == NULL) {
174 ALOGE("Couldn't initialize RS::dispatch->AllocationGetSurface");
175 return false;
176 }
177 RS::dispatch->AllocationSetSurface = (AllocationSetSurfaceFnPtr)dlsym(handle, "rsAllocationSetSurface");
178 if (RS::dispatch->AllocationSetSurface == NULL) {
179 ALOGE("Couldn't initialize RS::dispatch->AllocationSetSurface");
180 return false;
181 }
182 RS::dispatch->ContextFinish = (ContextFinishFnPtr)dlsym(handle, "rsContextFinish");
183 if (RS::dispatch->ContextFinish == NULL) {
184 ALOGE("Couldn't initialize RS::dispatch->ContextFinish");
185 return false;
186 }
187 RS::dispatch->ContextDump = (ContextDumpFnPtr)dlsym(handle, "rsContextDump");
188 if (RS::dispatch->ContextDump == NULL) {
189 ALOGE("Couldn't initialize RS::dispatch->ContextDump");
190 return false;
191 }
192 RS::dispatch->ContextSetPriority = (ContextSetPriorityFnPtr)dlsym(handle, "rsContextSetPriority");
193 if (RS::dispatch->ContextSetPriority == NULL) {
194 ALOGE("Couldn't initialize RS::dispatch->ContextSetPriority");
195 return false;
196 }
197 RS::dispatch->AssignName = (AssignNameFnPtr)dlsym(handle, "rsAssignName");
198 if (RS::dispatch->AssignName == NULL) {
199 ALOGE("Couldn't initialize RS::dispatch->AssignName");
200 return false;
201 }
202 RS::dispatch->ObjDestroy = (ObjDestroyFnPtr)dlsym(handle, "rsObjDestroy");
203 if (RS::dispatch->ObjDestroy == NULL) {
204 ALOGE("Couldn't initialize RS::dispatch->ObjDestroy");
205 return false;
206 }
207 RS::dispatch->ElementCreate = (ElementCreateFnPtr)dlsym(handle, "rsElementCreate");
208 if (RS::dispatch->ElementCreate == NULL) {
209 ALOGE("Couldn't initialize RS::dispatch->ElementCreate");
210 return false;
211 }
212 RS::dispatch->ElementCreate2 = (ElementCreate2FnPtr)dlsym(handle, "rsElementCreate2");
213 if (RS::dispatch->ElementCreate2 == NULL) {
214 ALOGE("Couldn't initialize RS::dispatch->ElementCreate2");
215 return false;
216 }
217 RS::dispatch->AllocationCopyToBitmap = (AllocationCopyToBitmapFnPtr)dlsym(handle, "rsAllocationCopyToBitmap");
218 if (RS::dispatch->AllocationCopyToBitmap == NULL) {
219 ALOGE("Couldn't initialize RS::dispatch->AllocationCopyToBitmap");
220 return false;
221 }
222 RS::dispatch->Allocation1DData = (Allocation1DDataFnPtr)dlsym(handle, "rsAllocation1DData");
223 if (RS::dispatch->Allocation1DData == NULL) {
224 ALOGE("Couldn't initialize RS::dispatch->Allocation1DData");
225 return false;
226 }
227 RS::dispatch->Allocation1DElementData = (Allocation1DElementDataFnPtr)dlsym(handle, "rsAllocation1DElementData");
228 if (RS::dispatch->Allocation1DElementData == NULL) {
229 ALOGE("Couldn't initialize RS::dispatch->Allocation1DElementData");
230 return false;
231 }
232 RS::dispatch->Allocation2DData = (Allocation2DDataFnPtr)dlsym(handle, "rsAllocation2DData");
233 if (RS::dispatch->Allocation2DData == NULL) {
234 ALOGE("Couldn't initialize RS::dispatch->Allocation2DData");
235 return false;
236 }
237 RS::dispatch->Allocation3DData = (Allocation3DDataFnPtr)dlsym(handle, "rsAllocation3DData");
238 if (RS::dispatch->Allocation3DData == NULL) {
239 ALOGE("Couldn't initialize RS::dispatch->Allocation3DData");
240 return false;
241 }
242 RS::dispatch->AllocationGenerateMipmaps = (AllocationGenerateMipmapsFnPtr)dlsym(handle, "rsAllocationGenerateMipmaps");
243 if (RS::dispatch->AllocationGenerateMipmaps == NULL) {
244 ALOGE("Couldn't initialize RS::dispatch->AllocationGenerateMipmaps");
245 return false;
246 }
247 RS::dispatch->AllocationRead = (AllocationReadFnPtr)dlsym(handle, "rsAllocationRead");
248 if (RS::dispatch->AllocationRead == NULL) {
249 ALOGE("Couldn't initialize RS::dispatch->AllocationRead");
250 return false;
251 }
252 RS::dispatch->Allocation1DRead = (Allocation1DReadFnPtr)dlsym(handle, "rsAllocation1DRead");
253 if (RS::dispatch->Allocation1DRead == NULL) {
254 ALOGE("Couldn't initialize RS::dispatch->Allocation1DRead");
255 return false;
256 }
257 RS::dispatch->Allocation2DRead = (Allocation2DReadFnPtr)dlsym(handle, "rsAllocation2DRead");
258 if (RS::dispatch->Allocation2DRead == NULL) {
259 ALOGE("Couldn't initialize RS::dispatch->Allocation2DRead");
260 return false;
261 }
262 RS::dispatch->AllocationSyncAll = (AllocationSyncAllFnPtr)dlsym(handle, "rsAllocationSyncAll");
263 if (RS::dispatch->AllocationSyncAll == NULL) {
264 ALOGE("Couldn't initialize RS::dispatch->AllocationSyncAll");
265 return false;
266 }
267 RS::dispatch->AllocationResize1D = (AllocationResize1DFnPtr)dlsym(handle, "rsAllocationResize1D");
268 if (RS::dispatch->AllocationResize1D == NULL) {
269 ALOGE("Couldn't initialize RS::dispatch->AllocationResize1D");
270 return false;
271 }
272 RS::dispatch->AllocationCopy2DRange = (AllocationCopy2DRangeFnPtr)dlsym(handle, "rsAllocationCopy2DRange");
273 if (RS::dispatch->AllocationCopy2DRange == NULL) {
274 ALOGE("Couldn't initialize RS::dispatch->AllocationCopy2DRange");
275 return false;
276 }
277 RS::dispatch->AllocationCopy3DRange = (AllocationCopy3DRangeFnPtr)dlsym(handle, "rsAllocationCopy3DRange");
278 if (RS::dispatch->AllocationCopy3DRange == NULL) {
279 ALOGE("Couldn't initialize RS::dispatch->AllocationCopy3DRange");
280 return false;
281 }
282 RS::dispatch->SamplerCreate = (SamplerCreateFnPtr)dlsym(handle, "rsSamplerCreate");
283 if (RS::dispatch->SamplerCreate == NULL) {
284 ALOGE("Couldn't initialize RS::dispatch->SamplerCreate");
285 return false;
286 }
287 RS::dispatch->ScriptBindAllocation = (ScriptBindAllocationFnPtr)dlsym(handle, "rsScriptBindAllocation");
288 if (RS::dispatch->ScriptBindAllocation == NULL) {
289 ALOGE("Couldn't initialize RS::dispatch->ScriptBindAllocation");
290 return false;
291 }
292 RS::dispatch->ScriptSetTimeZone = (ScriptSetTimeZoneFnPtr)dlsym(handle, "rsScriptSetTimeZone");
293 if (RS::dispatch->ScriptSetTimeZone == NULL) {
294 ALOGE("Couldn't initialize RS::dispatch->ScriptSetTimeZone");
295 return false;
296 }
297 RS::dispatch->ScriptInvoke = (ScriptInvokeFnPtr)dlsym(handle, "rsScriptInvoke");
298 if (RS::dispatch->ScriptInvoke == NULL) {
299 ALOGE("Couldn't initialize RS::dispatch->ScriptInvoke");
300 return false;
301 }
302 RS::dispatch->ScriptInvokeV = (ScriptInvokeVFnPtr)dlsym(handle, "rsScriptInvokeV");
303 if (RS::dispatch->ScriptInvokeV == NULL) {
304 ALOGE("Couldn't initialize RS::dispatch->ScriptInvokeV");
305 return false;
306 }
307 RS::dispatch->ScriptForEach = (ScriptForEachFnPtr)dlsym(handle, "rsScriptForEach");
308 if (RS::dispatch->ScriptForEach == NULL) {
309 ALOGE("Couldn't initialize RS::dispatch->ScriptForEach");
310 return false;
311 }
312 RS::dispatch->ScriptSetVarI = (ScriptSetVarIFnPtr)dlsym(handle, "rsScriptSetVarI");
313 if (RS::dispatch->ScriptSetVarI == NULL) {
314 ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarI");
315 return false;
316 }
317 RS::dispatch->ScriptSetVarObj = (ScriptSetVarObjFnPtr)dlsym(handle, "rsScriptSetVarObj");
318 if (RS::dispatch->ScriptSetVarObj == NULL) {
319 ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarObj");
320 return false;
321 }
322 RS::dispatch->ScriptSetVarJ = (ScriptSetVarJFnPtr)dlsym(handle, "rsScriptSetVarJ");
323 if (RS::dispatch->ScriptSetVarJ == NULL) {
324 ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarJ");
325 return false;
326 }
327 RS::dispatch->ScriptSetVarF = (ScriptSetVarFFnPtr)dlsym(handle, "rsScriptSetVarF");
328 if (RS::dispatch->ScriptSetVarF == NULL) {
329 ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarF");
330 return false;
331 }
332 RS::dispatch->ScriptSetVarD = (ScriptSetVarDFnPtr)dlsym(handle, "rsScriptSetVarD");
333 if (RS::dispatch->ScriptSetVarD == NULL) {
334 ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarD");
335 return false;
336 }
337 RS::dispatch->ScriptSetVarV = (ScriptSetVarVFnPtr)dlsym(handle, "rsScriptSetVarV");
338 if (RS::dispatch->ScriptSetVarV == NULL) {
339 ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarV");
340 return false;
341 }
342 RS::dispatch->ScriptGetVarV = (ScriptGetVarVFnPtr)dlsym(handle, "rsScriptGetVarV");
343 if (RS::dispatch->ScriptGetVarV == NULL) {
344 ALOGE("Couldn't initialize RS::dispatch->ScriptGetVarV");
345 return false;
346 }
347 RS::dispatch->ScriptSetVarVE = (ScriptSetVarVEFnPtr)dlsym(handle, "rsScriptSetVarVE");
348 if (RS::dispatch->ScriptSetVarVE == NULL) {
349 ALOGE("Couldn't initialize RS::dispatch->ScriptSetVarVE");
350 return false;
351 }
352 RS::dispatch->ScriptCCreate = (ScriptCCreateFnPtr)dlsym(handle, "rsScriptCCreate");
353 if (RS::dispatch->ScriptCCreate == NULL) {
354 ALOGE("Couldn't initialize RS::dispatch->ScriptCCreate");
355 return false;
356 }
357 RS::dispatch->ScriptIntrinsicCreate = (ScriptIntrinsicCreateFnPtr)dlsym(handle, "rsScriptIntrinsicCreate");
358 if (RS::dispatch->ScriptIntrinsicCreate == NULL) {
359 ALOGE("Couldn't initialize RS::dispatch->ScriptIntrinsicCreate");
360 return false;
361 }
362 RS::dispatch->ScriptKernelIDCreate = (ScriptKernelIDCreateFnPtr)dlsym(handle, "rsScriptKernelIDCreate");
363 if (RS::dispatch->ScriptKernelIDCreate == NULL) {
364 ALOGE("Couldn't initialize RS::dispatch->ScriptKernelIDCreate");
365 return false;
366 }
367 RS::dispatch->ScriptFieldIDCreate = (ScriptFieldIDCreateFnPtr)dlsym(handle, "rsScriptFieldIDCreate");
368 if (RS::dispatch->ScriptFieldIDCreate == NULL) {
369 ALOGE("Couldn't initialize RS::dispatch->ScriptFieldIDCreate");
370 return false;
371 }
372 RS::dispatch->ScriptGroupCreate = (ScriptGroupCreateFnPtr)dlsym(handle, "rsScriptGroupCreate");
373 if (RS::dispatch->ScriptGroupCreate == NULL) {
374 ALOGE("Couldn't initialize RS::dispatch->ScriptGroupCreate");
375 return false;
376 }
377 RS::dispatch->ScriptGroupSetOutput = (ScriptGroupSetOutputFnPtr)dlsym(handle, "rsScriptGroupSetOutput");
378 if (RS::dispatch->ScriptGroupSetOutput == NULL) {
379 ALOGE("Couldn't initialize RS::dispatch->ScriptGroupSetOutput");
380 return false;
381 }
382 RS::dispatch->ScriptGroupSetInput = (ScriptGroupSetInputFnPtr)dlsym(handle, "rsScriptGroupSetInput");
383 if (RS::dispatch->ScriptGroupSetInput == NULL) {
384 ALOGE("Couldn't initialize RS::dispatch->ScriptGroupSetInput");
385 return false;
386 }
387 RS::dispatch->ScriptGroupExecute = (ScriptGroupExecuteFnPtr)dlsym(handle, "rsScriptGroupExecute");
388 if (RS::dispatch->ScriptGroupExecute == NULL) {
389 ALOGE("Couldn't initialize RS::dispatch->ScriptGroupExecute");
390 return false;
391 }
392 RS::dispatch->AllocationIoSend = (AllocationIoSendFnPtr)dlsym(handle, "rsAllocationIoSend");
393 if (RS::dispatch->AllocationIoSend == NULL) {
394 ALOGE("Couldn't initialize RS::dispatch->AllocationIoSend");
395 return false;
396 }
397 RS::dispatch->AllocationIoReceive = (AllocationIoReceiveFnPtr)dlsym(handle, "rsAllocationIoReceive");
398 if (RS::dispatch->AllocationIoReceive == NULL) {
399 ALOGE("Couldn't initialize RS::dispatch->AllocationIoReceive");
400 return false;
401 }
402
403 return true;
404}
405
Tim Murray4a92d122013-07-22 10:56:18 -0700406static bool loadSO(const char* filename) {
407 void* handle = dlopen(filename, RTLD_LAZY | RTLD_LOCAL);
408 if (handle == NULL) {
409 ALOGE("couldn't dlopen %s, %s", filename, dlerror());
410 return false;
411 }
Tim Murraya4230962013-07-17 16:50:10 -0700412
Tim Murray4a92d122013-07-22 10:56:18 -0700413 if (loadSymbols(handle) == false) {
414 ALOGE("%s init failed!", filename);
415 return false;
416 }
417 ALOGE("Successfully loaded %s", filename);
418 return true;
419}
420
421static uint32_t getProp(const char *str) {
422#if !defined(RS_SERVER) && defined(HAVE_ANDROID_OS)
423 char buf[256];
424 property_get(str, buf, "0");
425 return atoi(buf);
426#else
427 return 0;
428#endif
429}
430
431bool RS::initDispatch(int targetApi) {
Tim Murraya4230962013-07-17 16:50:10 -0700432 pthread_mutex_lock(&gInitMutex);
433 if (gInitError) {
434 goto error;
435 } else if (gInitialized) {
Tim Murray47666f52013-07-29 14:32:34 -0700436 pthread_mutex_unlock(&gInitMutex);
Tim Murraya4230962013-07-17 16:50:10 -0700437 return true;
438 }
Tim Murraya4230962013-07-17 16:50:10 -0700439
440 RS::dispatch = new dispatchTable;
Tim Murray4a92d122013-07-22 10:56:18 -0700441
442 // attempt to load libRS, load libRSSupport on failure
443 // if property is set, proceed directly to libRSSupport
444 if (getProp("debug.rs.forcecompat") == 0) {
445 usingNative = loadSO("libRS.so");
446 }
447 if (usingNative == false) {
448 if (loadSO("libRSSupport.so") == false) {
449 ALOGE("Failed to load libRS.so and libRSSupport.so");
450 goto error;
451 }
Tim Murraya4230962013-07-17 16:50:10 -0700452 }
453
454 gInitialized = true;
455
456 pthread_mutex_unlock(&gInitMutex);
457 return true;
458
459 error:
460 gInitError = 1;
461 pthread_mutex_unlock(&gInitMutex);
462 return false;
463}
464
Tim Murray4d252d62012-11-29 14:37:59 -0800465bool RS::init(int targetApi, bool forceCpu, bool synchronous) {
Tim Murraya4230962013-07-17 16:50:10 -0700466 if (initDispatch(targetApi) == false) {
467 ALOGE("Couldn't initialize dispatch table");
468 return false;
469 }
470
471 mDev = RS::dispatch->DeviceCreate();
Jason Sams221a4b12012-02-22 15:22:41 -0800472 if (mDev == 0) {
473 ALOGE("Device creation failed");
474 return false;
475 }
476
Tim Murraya4230962013-07-17 16:50:10 -0700477 mContext = RS::dispatch->ContextCreate(mDev, 0, targetApi, RS_CONTEXT_TYPE_NORMAL, forceCpu, synchronous);
Jason Sams221a4b12012-02-22 15:22:41 -0800478 if (mContext == 0) {
479 ALOGE("Context creation failed");
480 return false;
481 }
482
Jason Sams221a4b12012-02-22 15:22:41 -0800483 pid_t mNativeMessageThreadId;
484
485 int status = pthread_create(&mMessageThreadId, NULL, threadProc, this);
486 if (status) {
Tim Murray84bf2b82012-10-31 16:03:16 -0700487 ALOGE("Failed to start RS message thread.");
Jason Sams221a4b12012-02-22 15:22:41 -0800488 return false;
489 }
490 // Wait for the message thread to be active.
491 while (!mMessageRun) {
492 usleep(1000);
493 }
494
Tim Murraya4230962013-07-17 16:50:10 -0700495 mInit = true;
496
Jason Sams221a4b12012-02-22 15:22:41 -0800497 return true;
498}
499
Tim Murray21fa7a02013-08-15 16:25:03 -0700500void RS::throwError(RSError error, const char *errMsg) {
501 if (mCurrentError == RS_SUCCESS) {
502 mCurrentError = error;
503 ALOGE("RS CPP error: %s", errMsg);
504 } else {
505 ALOGE("RS CPP error (masked by previous error): %s", errMsg);
506 }
Jason Samsb2e3dc52012-02-23 17:14:39 -0800507}
508
Tim Murray10913a52013-08-20 17:19:47 -0700509RSError RS::getError() {
510 return mCurrentError;
511}
512
Jason Samsb2e3dc52012-02-23 17:14:39 -0800513
Tim Murray84bf2b82012-10-31 16:03:16 -0700514void * RS::threadProc(void *vrsc) {
515 RS *rs = static_cast<RS *>(vrsc);
Jason Sams221a4b12012-02-22 15:22:41 -0800516 size_t rbuf_size = 256;
517 void * rbuf = malloc(rbuf_size);
518
Tim Murraya4230962013-07-17 16:50:10 -0700519 RS::dispatch->ContextInitToClient(rs->mContext);
Jason Sams221a4b12012-02-22 15:22:41 -0800520 rs->mMessageRun = true;
521
522 while (rs->mMessageRun) {
523 size_t receiveLen = 0;
524 uint32_t usrID = 0;
525 uint32_t subID = 0;
Tim Murraya4230962013-07-17 16:50:10 -0700526 RsMessageToClientType r = RS::dispatch->ContextPeekMessage(rs->mContext,
527 &receiveLen, sizeof(receiveLen),
528 &usrID, sizeof(usrID));
Jason Sams221a4b12012-02-22 15:22:41 -0800529
530 if (receiveLen >= rbuf_size) {
531 rbuf_size = receiveLen + 32;
532 rbuf = realloc(rbuf, rbuf_size);
533 }
534 if (!rbuf) {
Tim Murray84bf2b82012-10-31 16:03:16 -0700535 ALOGE("RS::message handler realloc error %zu", rbuf_size);
Jason Sams221a4b12012-02-22 15:22:41 -0800536 // No clean way to recover now?
537 }
Tim Murraya4230962013-07-17 16:50:10 -0700538 RS::dispatch->ContextGetMessage(rs->mContext, rbuf, rbuf_size, &receiveLen, sizeof(receiveLen),
Jason Sams221a4b12012-02-22 15:22:41 -0800539 &subID, sizeof(subID));
540
541 switch(r) {
542 case RS_MESSAGE_TO_CLIENT_ERROR:
543 ALOGE("RS Error %s", (const char *)rbuf);
Tim Murray21fa7a02013-08-15 16:25:03 -0700544 rs->throwError(RS_ERROR_RUNTIME_ERROR, "Error returned from runtime");
Jason Sams221a4b12012-02-22 15:22:41 -0800545 if(rs->mMessageFunc != NULL) {
546 rs->mErrorFunc(usrID, (const char *)rbuf);
547 }
548 break;
Stephen Hines76a1be42012-11-26 16:26:03 -0800549 case RS_MESSAGE_TO_CLIENT_NONE:
Jason Sams221a4b12012-02-22 15:22:41 -0800550 case RS_MESSAGE_TO_CLIENT_EXCEPTION:
Stephen Hines76a1be42012-11-26 16:26:03 -0800551 case RS_MESSAGE_TO_CLIENT_RESIZE:
Jason Sams221a4b12012-02-22 15:22:41 -0800552 // teardown. But we want to avoid starving other threads during
553 // teardown by yielding until the next line in the destructor can
Stephen Hines76a1be42012-11-26 16:26:03 -0800554 // execute to set mRun = false. Note that the FIFO sends an
555 // empty NONE message when it reaches its destructor.
Jason Sams221a4b12012-02-22 15:22:41 -0800556 usleep(1000);
557 break;
558 case RS_MESSAGE_TO_CLIENT_USER:
559 if(rs->mMessageFunc != NULL) {
560 rs->mMessageFunc(usrID, rbuf, receiveLen);
561 } else {
562 ALOGE("Received a message from the script with no message handler installed.");
563 }
564 break;
565
566 default:
Tim Murray84bf2b82012-10-31 16:03:16 -0700567 ALOGE("RS unknown message type %i", r);
Jason Sams221a4b12012-02-22 15:22:41 -0800568 }
569 }
570
571 if (rbuf) {
572 free(rbuf);
573 }
Tim Murray84bf2b82012-10-31 16:03:16 -0700574 ALOGE("RS Message thread exiting.");
Jason Sams221a4b12012-02-22 15:22:41 -0800575 return NULL;
576}
577
Tim Murray84bf2b82012-10-31 16:03:16 -0700578void RS::setErrorHandler(ErrorHandlerFunc_t func) {
Jason Sams221a4b12012-02-22 15:22:41 -0800579 mErrorFunc = func;
580}
581
Tim Murray84bf2b82012-10-31 16:03:16 -0700582void RS::setMessageHandler(MessageHandlerFunc_t func) {
Jason Sams221a4b12012-02-22 15:22:41 -0800583 mMessageFunc = func;
584}
Tim Murraybaca6c32012-11-14 16:51:46 -0800585
586void RS::finish() {
Tim Murraya4230962013-07-17 16:50:10 -0700587 RS::dispatch->ContextFinish(mContext);
Tim Murraybaca6c32012-11-14 16:51:46 -0800588}