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