RenderScript.cpp revision b8a94e26c0a5e8f58d5b6ed04e46b411e95b77a4
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(HAVE_ANDROID_OS)
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 = NULL;
42static int gInitError = 0;
43
44RS::RS() {
45    mDev = NULL;
46    mContext = NULL;
47    mErrorFunc = NULL;
48    mMessageFunc = NULL;
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        RS::dispatch->ContextDeinitToClient(mContext);
62
63        void *res = NULL;
64        int status = pthread_join(mMessageThreadId, &res);
65
66        RS::dispatch->ContextDestroy(mContext);
67        mContext = NULL;
68        RS::dispatch->DeviceDestroy(mDev);
69        mDev = NULL;
70    }
71}
72
73bool RS::init(std::string name, uint32_t flags) {
74    return RS::init(name, RS_VERSION, flags);
75}
76
77static bool loadSymbols(void* handle) {
78
79    RS::dispatch->AllocationGetType = (AllocationGetTypeFnPtr)dlsym(handle, "rsaAllocationGetType");
80    if (RS::dispatch->AllocationGetType == NULL) {
81        ALOGV("Couldn't initialize RS::dispatch->AllocationGetType");
82        return false;
83    }
84    RS::dispatch->TypeGetNativeData = (TypeGetNativeDataFnPtr)dlsym(handle, "rsaTypeGetNativeData");
85    if (RS::dispatch->TypeGetNativeData == NULL) {
86        ALOGV("Couldn't initialize RS::dispatch->TypeGetNativeData");
87        return false;
88    }
89    RS::dispatch->ElementGetNativeData = (ElementGetNativeDataFnPtr)dlsym(handle, "rsaElementGetNativeData");
90    if (RS::dispatch->ElementGetNativeData == NULL) {
91        ALOGV("Couldn't initialize RS::dispatch->ElementGetNativeData");
92        return false;
93    }
94    RS::dispatch->ElementGetSubElements = (ElementGetSubElementsFnPtr)dlsym(handle, "rsaElementGetSubElements");
95    if (RS::dispatch->ElementGetSubElements == NULL) {
96        ALOGV("Couldn't initialize RS::dispatch->ElementGetSubElements");
97        return false;
98    }
99    RS::dispatch->DeviceCreate = (DeviceCreateFnPtr)dlsym(handle, "rsDeviceCreate");
100    if (RS::dispatch->DeviceCreate == NULL) {
101        ALOGV("Couldn't initialize RS::dispatch->DeviceCreate");
102        return false;
103    }
104    RS::dispatch->DeviceDestroy = (DeviceDestroyFnPtr)dlsym(handle, "rsDeviceDestroy");
105    if (RS::dispatch->DeviceDestroy == NULL) {
106        ALOGV("Couldn't initialize RS::dispatch->DeviceDestroy");
107        return false;
108    }
109    RS::dispatch->DeviceSetConfig = (DeviceSetConfigFnPtr)dlsym(handle, "rsDeviceSetConfig");
110    if (RS::dispatch->DeviceSetConfig == NULL) {
111        ALOGV("Couldn't initialize RS::dispatch->DeviceSetConfig");
112        return false;
113    }
114    RS::dispatch->ContextCreate = (ContextCreateFnPtr)dlsym(handle, "rsContextCreate");;
115    if (RS::dispatch->ContextCreate == NULL) {
116        ALOGV("Couldn't initialize RS::dispatch->ContextCreate");
117        return false;
118    }
119    RS::dispatch->GetName = (GetNameFnPtr)dlsym(handle, "rsaGetName");;
120    if (RS::dispatch->GetName == NULL) {
121        ALOGV("Couldn't initialize RS::dispatch->GetName");
122        return false;
123    }
124    RS::dispatch->ContextDestroy = (ContextDestroyFnPtr)dlsym(handle, "rsContextDestroy");
125    if (RS::dispatch->ContextDestroy == NULL) {
126        ALOGV("Couldn't initialize RS::dispatch->ContextDestroy");
127        return false;
128    }
129    RS::dispatch->ContextGetMessage = (ContextGetMessageFnPtr)dlsym(handle, "rsContextGetMessage");
130    if (RS::dispatch->ContextGetMessage == NULL) {
131        ALOGV("Couldn't initialize RS::dispatch->ContextGetMessage");
132        return false;
133    }
134    RS::dispatch->ContextPeekMessage = (ContextPeekMessageFnPtr)dlsym(handle, "rsContextPeekMessage");
135    if (RS::dispatch->ContextPeekMessage == NULL) {
136        ALOGV("Couldn't initialize RS::dispatch->ContextPeekMessage");
137        return false;
138    }
139    RS::dispatch->ContextSendMessage = (ContextSendMessageFnPtr)dlsym(handle, "rsContextSendMessage");
140    if (RS::dispatch->ContextSendMessage == NULL) {
141        ALOGV("Couldn't initialize RS::dispatch->ContextSendMessage");
142        return false;
143    }
144    RS::dispatch->ContextInitToClient = (ContextInitToClientFnPtr)dlsym(handle, "rsContextInitToClient");
145    if (RS::dispatch->ContextInitToClient == NULL) {
146        ALOGV("Couldn't initialize RS::dispatch->ContextInitToClient");
147        return false;
148    }
149    RS::dispatch->ContextDeinitToClient = (ContextDeinitToClientFnPtr)dlsym(handle, "rsContextDeinitToClient");
150    if (RS::dispatch->ContextDeinitToClient == NULL) {
151        ALOGV("Couldn't initialize RS::dispatch->ContextDeinitToClient");
152        return false;
153    }
154    RS::dispatch->TypeCreate = (TypeCreateFnPtr)dlsym(handle, "rsTypeCreate");
155    if (RS::dispatch->TypeCreate == NULL) {
156        ALOGV("Couldn't initialize RS::dispatch->TypeCreate");
157        return false;
158    }
159    RS::dispatch->AllocationCreateTyped = (AllocationCreateTypedFnPtr)dlsym(handle, "rsAllocationCreateTyped");
160    if (RS::dispatch->AllocationCreateTyped == NULL) {
161        ALOGV("Couldn't initialize RS::dispatch->AllocationCreateTyped");
162        return false;
163    }
164    RS::dispatch->AllocationCreateFromBitmap = (AllocationCreateFromBitmapFnPtr)dlsym(handle, "rsAllocationCreateFromBitmap");
165    if (RS::dispatch->AllocationCreateFromBitmap == NULL) {
166        ALOGV("Couldn't initialize RS::dispatch->AllocationCreateFromBitmap");
167        return false;
168    }
169    RS::dispatch->AllocationCubeCreateFromBitmap = (AllocationCubeCreateFromBitmapFnPtr)dlsym(handle, "rsAllocationCubeCreateFromBitmap");
170    if (RS::dispatch->AllocationCubeCreateFromBitmap == NULL) {
171        ALOGV("Couldn't initialize RS::dispatch->AllocationCubeCreateFromBitmap");
172        return false;
173    }
174    RS::dispatch->AllocationGetSurface = (AllocationGetSurfaceFnPtr)dlsym(handle, "rsAllocationGetSurface");
175    if (RS::dispatch->AllocationGetSurface == NULL) {
176        ALOGV("Couldn't initialize RS::dispatch->AllocationGetSurface");
177        return false;
178    }
179    RS::dispatch->AllocationSetSurface = (AllocationSetSurfaceFnPtr)dlsym(handle, "rsAllocationSetSurface");
180    if (RS::dispatch->AllocationSetSurface == NULL) {
181        ALOGV("Couldn't initialize RS::dispatch->AllocationSetSurface");
182        return false;
183    }
184    RS::dispatch->ContextFinish = (ContextFinishFnPtr)dlsym(handle, "rsContextFinish");
185    if (RS::dispatch->ContextFinish == NULL) {
186        ALOGV("Couldn't initialize RS::dispatch->ContextFinish");
187        return false;
188    }
189    RS::dispatch->ContextDump = (ContextDumpFnPtr)dlsym(handle, "rsContextDump");
190    if (RS::dispatch->ContextDump == NULL) {
191        ALOGV("Couldn't initialize RS::dispatch->ContextDump");
192        return false;
193    }
194    RS::dispatch->ContextSetPriority = (ContextSetPriorityFnPtr)dlsym(handle, "rsContextSetPriority");
195    if (RS::dispatch->ContextSetPriority == NULL) {
196        ALOGV("Couldn't initialize RS::dispatch->ContextSetPriority");
197        return false;
198    }
199    RS::dispatch->AssignName = (AssignNameFnPtr)dlsym(handle, "rsAssignName");
200    if (RS::dispatch->AssignName == NULL) {
201        ALOGV("Couldn't initialize RS::dispatch->AssignName");
202        return false;
203    }
204    RS::dispatch->ObjDestroy = (ObjDestroyFnPtr)dlsym(handle, "rsObjDestroy");
205    if (RS::dispatch->ObjDestroy == NULL) {
206        ALOGV("Couldn't initialize RS::dispatch->ObjDestroy");
207        return false;
208    }
209    RS::dispatch->ElementCreate = (ElementCreateFnPtr)dlsym(handle, "rsElementCreate");
210    if (RS::dispatch->ElementCreate == NULL) {
211        ALOGV("Couldn't initialize RS::dispatch->ElementCreate");
212        return false;
213    }
214    RS::dispatch->ElementCreate2 = (ElementCreate2FnPtr)dlsym(handle, "rsElementCreate2");
215    if (RS::dispatch->ElementCreate2 == NULL) {
216        ALOGV("Couldn't initialize RS::dispatch->ElementCreate2");
217        return false;
218    }
219    RS::dispatch->AllocationCopyToBitmap = (AllocationCopyToBitmapFnPtr)dlsym(handle, "rsAllocationCopyToBitmap");
220    if (RS::dispatch->AllocationCopyToBitmap == NULL) {
221        ALOGV("Couldn't initialize RS::dispatch->AllocationCopyToBitmap");
222        return false;
223    }
224    RS::dispatch->Allocation1DData = (Allocation1DDataFnPtr)dlsym(handle, "rsAllocation1DData");
225    if (RS::dispatch->Allocation1DData == NULL) {
226        ALOGV("Couldn't initialize RS::dispatch->Allocation1DData");
227        return false;
228    }
229    RS::dispatch->Allocation1DElementData = (Allocation1DElementDataFnPtr)dlsym(handle, "rsAllocation1DElementData");
230    if (RS::dispatch->Allocation1DElementData == NULL) {
231        ALOGV("Couldn't initialize RS::dispatch->Allocation1DElementData");
232        return false;
233    }
234    RS::dispatch->Allocation2DData = (Allocation2DDataFnPtr)dlsym(handle, "rsAllocation2DData");
235    if (RS::dispatch->Allocation2DData == NULL) {
236        ALOGV("Couldn't initialize RS::dispatch->Allocation2DData");
237        return false;
238    }
239    RS::dispatch->Allocation3DData = (Allocation3DDataFnPtr)dlsym(handle, "rsAllocation3DData");
240    if (RS::dispatch->Allocation3DData == NULL) {
241        ALOGV("Couldn't initialize RS::dispatch->Allocation3DData");
242        return false;
243    }
244    RS::dispatch->AllocationGenerateMipmaps = (AllocationGenerateMipmapsFnPtr)dlsym(handle, "rsAllocationGenerateMipmaps");
245    if (RS::dispatch->AllocationGenerateMipmaps == NULL) {
246        ALOGV("Couldn't initialize RS::dispatch->AllocationGenerateMipmaps");
247        return false;
248    }
249    RS::dispatch->AllocationRead = (AllocationReadFnPtr)dlsym(handle, "rsAllocationRead");
250    if (RS::dispatch->AllocationRead == NULL) {
251        ALOGV("Couldn't initialize RS::dispatch->AllocationRead");
252        return false;
253    }
254    RS::dispatch->Allocation1DRead = (Allocation1DReadFnPtr)dlsym(handle, "rsAllocation1DRead");
255    if (RS::dispatch->Allocation1DRead == NULL) {
256        ALOGV("Couldn't initialize RS::dispatch->Allocation1DRead");
257        return false;
258    }
259    RS::dispatch->Allocation2DRead = (Allocation2DReadFnPtr)dlsym(handle, "rsAllocation2DRead");
260    if (RS::dispatch->Allocation2DRead == NULL) {
261        ALOGV("Couldn't initialize RS::dispatch->Allocation2DRead");
262        return false;
263    }
264    RS::dispatch->AllocationSyncAll = (AllocationSyncAllFnPtr)dlsym(handle, "rsAllocationSyncAll");
265    if (RS::dispatch->AllocationSyncAll == NULL) {
266        ALOGV("Couldn't initialize RS::dispatch->AllocationSyncAll");
267        return false;
268    }
269    RS::dispatch->AllocationResize1D = (AllocationResize1DFnPtr)dlsym(handle, "rsAllocationResize1D");
270    if (RS::dispatch->AllocationResize1D == NULL) {
271        ALOGV("Couldn't initialize RS::dispatch->AllocationResize1D");
272        return false;
273    }
274    RS::dispatch->AllocationCopy2DRange = (AllocationCopy2DRangeFnPtr)dlsym(handle, "rsAllocationCopy2DRange");
275    if (RS::dispatch->AllocationCopy2DRange == NULL) {
276        ALOGV("Couldn't initialize RS::dispatch->AllocationCopy2DRange");
277        return false;
278    }
279    RS::dispatch->AllocationCopy3DRange = (AllocationCopy3DRangeFnPtr)dlsym(handle, "rsAllocationCopy3DRange");
280    if (RS::dispatch->AllocationCopy3DRange == NULL) {
281        ALOGV("Couldn't initialize RS::dispatch->AllocationCopy3DRange");
282        return false;
283    }
284    RS::dispatch->SamplerCreate = (SamplerCreateFnPtr)dlsym(handle, "rsSamplerCreate");
285    if (RS::dispatch->SamplerCreate == NULL) {
286        ALOGV("Couldn't initialize RS::dispatch->SamplerCreate");
287        return false;
288    }
289    RS::dispatch->ScriptBindAllocation = (ScriptBindAllocationFnPtr)dlsym(handle, "rsScriptBindAllocation");
290    if (RS::dispatch->ScriptBindAllocation == NULL) {
291        ALOGV("Couldn't initialize RS::dispatch->ScriptBindAllocation");
292        return false;
293    }
294    RS::dispatch->ScriptSetTimeZone = (ScriptSetTimeZoneFnPtr)dlsym(handle, "rsScriptSetTimeZone");
295    if (RS::dispatch->ScriptSetTimeZone == NULL) {
296        ALOGV("Couldn't initialize RS::dispatch->ScriptSetTimeZone");
297        return false;
298    }
299    RS::dispatch->ScriptInvoke = (ScriptInvokeFnPtr)dlsym(handle, "rsScriptInvoke");
300    if (RS::dispatch->ScriptInvoke == NULL) {
301        ALOGV("Couldn't initialize RS::dispatch->ScriptInvoke");
302        return false;
303    }
304    RS::dispatch->ScriptInvokeV = (ScriptInvokeVFnPtr)dlsym(handle, "rsScriptInvokeV");
305    if (RS::dispatch->ScriptInvokeV == NULL) {
306        ALOGV("Couldn't initialize RS::dispatch->ScriptInvokeV");
307        return false;
308    }
309    RS::dispatch->ScriptForEach = (ScriptForEachFnPtr)dlsym(handle, "rsScriptForEach");
310    if (RS::dispatch->ScriptForEach == NULL) {
311        ALOGV("Couldn't initialize RS::dispatch->ScriptForEach");
312        return false;
313    }
314    RS::dispatch->ScriptSetVarI = (ScriptSetVarIFnPtr)dlsym(handle, "rsScriptSetVarI");
315    if (RS::dispatch->ScriptSetVarI == NULL) {
316        ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarI");
317        return false;
318    }
319    RS::dispatch->ScriptSetVarObj = (ScriptSetVarObjFnPtr)dlsym(handle, "rsScriptSetVarObj");
320    if (RS::dispatch->ScriptSetVarObj == NULL) {
321        ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarObj");
322        return false;
323    }
324    RS::dispatch->ScriptSetVarJ = (ScriptSetVarJFnPtr)dlsym(handle, "rsScriptSetVarJ");
325    if (RS::dispatch->ScriptSetVarJ == NULL) {
326        ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarJ");
327        return false;
328    }
329    RS::dispatch->ScriptSetVarF = (ScriptSetVarFFnPtr)dlsym(handle, "rsScriptSetVarF");
330    if (RS::dispatch->ScriptSetVarF == NULL) {
331        ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarF");
332        return false;
333    }
334    RS::dispatch->ScriptSetVarD = (ScriptSetVarDFnPtr)dlsym(handle, "rsScriptSetVarD");
335    if (RS::dispatch->ScriptSetVarD == NULL) {
336        ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarD");
337        return false;
338    }
339    RS::dispatch->ScriptSetVarV = (ScriptSetVarVFnPtr)dlsym(handle, "rsScriptSetVarV");
340    if (RS::dispatch->ScriptSetVarV == NULL) {
341        ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarV");
342        return false;
343    }
344    RS::dispatch->ScriptGetVarV = (ScriptGetVarVFnPtr)dlsym(handle, "rsScriptGetVarV");
345    if (RS::dispatch->ScriptGetVarV == NULL) {
346        ALOGV("Couldn't initialize RS::dispatch->ScriptGetVarV");
347        return false;
348    }
349    RS::dispatch->ScriptSetVarVE = (ScriptSetVarVEFnPtr)dlsym(handle, "rsScriptSetVarVE");
350    if (RS::dispatch->ScriptSetVarVE == NULL) {
351        ALOGV("Couldn't initialize RS::dispatch->ScriptSetVarVE");
352        return false;
353    }
354    RS::dispatch->ScriptCCreate = (ScriptCCreateFnPtr)dlsym(handle, "rsScriptCCreate");
355    if (RS::dispatch->ScriptCCreate == NULL) {
356        ALOGV("Couldn't initialize RS::dispatch->ScriptCCreate");
357        return false;
358    }
359    RS::dispatch->ScriptIntrinsicCreate = (ScriptIntrinsicCreateFnPtr)dlsym(handle, "rsScriptIntrinsicCreate");
360    if (RS::dispatch->ScriptIntrinsicCreate == NULL) {
361        ALOGV("Couldn't initialize RS::dispatch->ScriptIntrinsicCreate");
362        return false;
363    }
364    RS::dispatch->ScriptKernelIDCreate = (ScriptKernelIDCreateFnPtr)dlsym(handle, "rsScriptKernelIDCreate");
365    if (RS::dispatch->ScriptKernelIDCreate == NULL) {
366        ALOGV("Couldn't initialize RS::dispatch->ScriptKernelIDCreate");
367        return false;
368    }
369    RS::dispatch->ScriptFieldIDCreate = (ScriptFieldIDCreateFnPtr)dlsym(handle, "rsScriptFieldIDCreate");
370    if (RS::dispatch->ScriptFieldIDCreate == NULL) {
371        ALOGV("Couldn't initialize RS::dispatch->ScriptFieldIDCreate");
372        return false;
373    }
374    RS::dispatch->ScriptGroupCreate = (ScriptGroupCreateFnPtr)dlsym(handle, "rsScriptGroupCreate");
375    if (RS::dispatch->ScriptGroupCreate == NULL) {
376        ALOGV("Couldn't initialize RS::dispatch->ScriptGroupCreate");
377        return false;
378    }
379    RS::dispatch->ScriptGroupSetOutput = (ScriptGroupSetOutputFnPtr)dlsym(handle, "rsScriptGroupSetOutput");
380    if (RS::dispatch->ScriptGroupSetOutput == NULL) {
381        ALOGV("Couldn't initialize RS::dispatch->ScriptGroupSetOutput");
382        return false;
383    }
384    RS::dispatch->ScriptGroupSetInput = (ScriptGroupSetInputFnPtr)dlsym(handle, "rsScriptGroupSetInput");
385    if (RS::dispatch->ScriptGroupSetInput == NULL) {
386        ALOGV("Couldn't initialize RS::dispatch->ScriptGroupSetInput");
387        return false;
388    }
389    RS::dispatch->ScriptGroupExecute = (ScriptGroupExecuteFnPtr)dlsym(handle, "rsScriptGroupExecute");
390    if (RS::dispatch->ScriptGroupExecute == NULL) {
391        ALOGV("Couldn't initialize RS::dispatch->ScriptGroupExecute");
392        return false;
393    }
394    RS::dispatch->AllocationIoSend = (AllocationIoSendFnPtr)dlsym(handle, "rsAllocationIoSend");
395    if (RS::dispatch->AllocationIoSend == NULL) {
396        ALOGV("Couldn't initialize RS::dispatch->AllocationIoSend");
397        return false;
398    }
399    RS::dispatch->AllocationIoReceive = (AllocationIoReceiveFnPtr)dlsym(handle, "rsAllocationIoReceive");
400    if (RS::dispatch->AllocationIoReceive == NULL) {
401        ALOGV("Couldn't initialize RS::dispatch->AllocationIoReceive");
402        return false;
403    }
404    RS::dispatch->AllocationGetPointer = (AllocationGetPointerFnPtr)dlsym(handle, "rsAllocationGetPointer");
405    if (RS::dispatch->AllocationGetPointer == NULL) {
406        ALOGV("Couldn't initialize RS::dispatch->AllocationGetPointer");
407        //return false;
408    }
409
410    return true;
411}
412
413// this will only open API 19+ libRS
414// because that's when we changed libRS to extern "C" entry points
415static bool loadSO(const char* filename) {
416    void* handle = dlopen(filename, RTLD_LAZY | RTLD_LOCAL);
417    if (handle == NULL) {
418        ALOGV("couldn't dlopen %s, %s", filename, dlerror());
419        return false;
420    }
421
422    if (loadSymbols(handle) == false) {
423        ALOGV("%s init failed!", filename);
424        return false;
425    }
426    //ALOGE("Successfully loaded %s", filename);
427    return true;
428}
429
430static uint32_t getProp(const char *str) {
431#if !defined(RS_SERVER) && defined(HAVE_ANDROID_OS)
432    char buf[256];
433    property_get(str, buf, "0");
434    return atoi(buf);
435#else
436    return 0;
437#endif
438}
439
440bool RS::initDispatch(int targetApi) {
441    pthread_mutex_lock(&gInitMutex);
442    if (gInitError) {
443        goto error;
444    } else if (gInitialized) {
445        pthread_mutex_unlock(&gInitMutex);
446        return true;
447    }
448
449    RS::dispatch = new dispatchTable;
450
451    // attempt to load libRS, load libRSSupport on failure
452    // if property is set, proceed directly to libRSSupport
453    if (getProp("debug.rs.forcecompat") == 0) {
454        usingNative = loadSO("libRS.so");
455    }
456    if (usingNative == false) {
457        if (loadSO("libRSSupport.so") == false) {
458            ALOGE("Failed to load libRS.so and libRSSupport.so");
459            goto error;
460        }
461    }
462
463    gInitialized = true;
464
465    pthread_mutex_unlock(&gInitMutex);
466    return true;
467
468 error:
469    gInitError = 1;
470    pthread_mutex_unlock(&gInitMutex);
471    return false;
472}
473
474bool RS::init(std::string &name, int targetApi, uint32_t flags) {
475    if (mInit) {
476        return true;
477    }
478
479    if (initDispatch(targetApi) == false) {
480        ALOGE("Couldn't initialize dispatch table");
481        return false;
482    }
483
484    mCacheDir = name;
485
486    mDev = RS::dispatch->DeviceCreate();
487    if (mDev == 0) {
488        ALOGE("Device creation failed");
489        return false;
490    }
491
492    if (flags >= RS_CONTEXT_MAX) {
493        ALOGE("Invalid flags passed");
494        return false;
495    }
496
497    mContext = RS::dispatch->ContextCreate(mDev, 0, targetApi, RS_CONTEXT_TYPE_NORMAL, flags);
498    if (mContext == 0) {
499        ALOGE("Context creation failed");
500        return false;
501    }
502
503    pid_t mNativeMessageThreadId;
504
505    int status = pthread_create(&mMessageThreadId, NULL, threadProc, this);
506    if (status) {
507        ALOGE("Failed to start RS message thread.");
508        return false;
509    }
510    // Wait for the message thread to be active.
511    while (!mMessageRun) {
512        usleep(1000);
513    }
514
515    mInit = true;
516
517    return true;
518}
519
520void RS::throwError(RSError error, const char *errMsg) {
521    if (mCurrentError == RS_SUCCESS) {
522        mCurrentError = error;
523        ALOGE("RS CPP error: %s", errMsg);
524    } else {
525        ALOGE("RS CPP error (masked by previous error): %s", errMsg);
526    }
527}
528
529RSError RS::getError() {
530    return mCurrentError;
531}
532
533
534void * RS::threadProc(void *vrsc) {
535    RS *rs = static_cast<RS *>(vrsc);
536    size_t rbuf_size = 256;
537    void * rbuf = malloc(rbuf_size);
538
539    RS::dispatch->ContextInitToClient(rs->mContext);
540    rs->mMessageRun = true;
541
542    while (rs->mMessageRun) {
543        size_t receiveLen = 0;
544        uint32_t usrID = 0;
545        uint32_t subID = 0;
546        RsMessageToClientType r = RS::dispatch->ContextPeekMessage(rs->mContext,
547                                                                   &receiveLen, sizeof(receiveLen),
548                                                                   &usrID, sizeof(usrID));
549
550        if (receiveLen >= rbuf_size) {
551            rbuf_size = receiveLen + 32;
552            rbuf = realloc(rbuf, rbuf_size);
553        }
554        if (!rbuf) {
555            ALOGE("RS::message handler realloc error %zu", rbuf_size);
556            // No clean way to recover now?
557        }
558        RS::dispatch->ContextGetMessage(rs->mContext, rbuf, rbuf_size, &receiveLen, sizeof(receiveLen),
559                            &subID, sizeof(subID));
560
561        switch(r) {
562        case RS_MESSAGE_TO_CLIENT_ERROR:
563            ALOGE("RS Error %s", (const char *)rbuf);
564            rs->throwError(RS_ERROR_RUNTIME_ERROR, "Error returned from runtime");
565            if(rs->mMessageFunc != NULL) {
566                rs->mErrorFunc(usrID, (const char *)rbuf);
567            }
568            break;
569        case RS_MESSAGE_TO_CLIENT_NONE:
570        case RS_MESSAGE_TO_CLIENT_EXCEPTION:
571        case RS_MESSAGE_TO_CLIENT_RESIZE:
572            // teardown. But we want to avoid starving other threads during
573            // teardown by yielding until the next line in the destructor can
574            // execute to set mRun = false. Note that the FIFO sends an
575            // empty NONE message when it reaches its destructor.
576            usleep(1000);
577            break;
578        case RS_MESSAGE_TO_CLIENT_USER:
579            if(rs->mMessageFunc != NULL) {
580                rs->mMessageFunc(usrID, rbuf, receiveLen);
581            } else {
582                ALOGE("Received a message from the script with no message handler installed.");
583            }
584            break;
585
586        default:
587            ALOGE("RS unknown message type %i", r);
588        }
589    }
590
591    if (rbuf) {
592        free(rbuf);
593    }
594    ALOGV("RS Message thread exiting.");
595    return NULL;
596}
597
598void RS::setErrorHandler(ErrorHandlerFunc_t func) {
599    mErrorFunc = func;
600}
601
602void RS::setMessageHandler(MessageHandlerFunc_t func) {
603    mMessageFunc  = func;
604}
605
606void RS::finish() {
607    RS::dispatch->ContextFinish(mContext);
608}
609