RenderScript.cpp revision a662edd85843c2eb7078900b0279e38d6635795e
1/*
2 * Copyright (C) 2008-2012 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#define LOG_TAG "libRS_cpp"
18
19#include <utils/Log.h>
20#include <malloc.h>
21#include <string.h>
22
23#include "RenderScript.h"
24#include "rs.h"
25
26bool RenderScript::gInitialized = false;
27pthread_mutex_t RenderScript::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
28
29RenderScript::RenderScript() {
30    mDev = NULL;
31    mContext = NULL;
32    mErrorFunc = NULL;
33    mMessageFunc = NULL;
34    mMessageRun = false;
35
36    memset(&mElements, 0, sizeof(mElements));
37}
38
39RenderScript::~RenderScript() {
40    mMessageRun = false;
41
42    rsContextDeinitToClient(mContext);
43
44    void *res = NULL;
45    int status = pthread_join(mMessageThreadId, &res);
46
47    rsContextDestroy(mContext);
48    mContext = NULL;
49    rsDeviceDestroy(mDev);
50    mDev = NULL;
51}
52
53bool RenderScript::init(int targetApi) {
54    mDev = rsDeviceCreate();
55    if (mDev == 0) {
56        ALOGE("Device creation failed");
57        return false;
58    }
59
60    mContext = rsContextCreate(mDev, 0, targetApi);
61    if (mContext == 0) {
62        ALOGE("Context creation failed");
63        return false;
64    }
65
66
67    pid_t mNativeMessageThreadId;
68
69    int status = pthread_create(&mMessageThreadId, NULL, threadProc, this);
70    if (status) {
71        ALOGE("Failed to start RenderScript message thread.");
72        return false;
73    }
74    // Wait for the message thread to be active.
75    while (!mMessageRun) {
76        usleep(1000);
77    }
78
79    return true;
80}
81
82void RenderScript::throwError(const char *err) const {
83    ALOGE("RS CPP error: %s", err);
84    int * v = NULL;
85    v[0] = 0;
86}
87
88
89void * RenderScript::threadProc(void *vrsc) {
90    RenderScript *rs = static_cast<RenderScript *>(vrsc);
91    size_t rbuf_size = 256;
92    void * rbuf = malloc(rbuf_size);
93
94    rsContextInitToClient(rs->mContext);
95    rs->mMessageRun = true;
96
97    while (rs->mMessageRun) {
98        size_t receiveLen = 0;
99        uint32_t usrID = 0;
100        uint32_t subID = 0;
101        RsMessageToClientType r = rsContextPeekMessage(rs->mContext,
102                                                       &receiveLen, sizeof(receiveLen),
103                                                       &usrID, sizeof(usrID));
104
105        if (receiveLen >= rbuf_size) {
106            rbuf_size = receiveLen + 32;
107            rbuf = realloc(rbuf, rbuf_size);
108        }
109        if (!rbuf) {
110            ALOGE("RenderScript::message handler realloc error %zu", rbuf_size);
111            // No clean way to recover now?
112        }
113        rsContextGetMessage(rs->mContext, rbuf, rbuf_size, &receiveLen, sizeof(receiveLen),
114                            &subID, sizeof(subID));
115
116        switch(r) {
117        case RS_MESSAGE_TO_CLIENT_ERROR:
118            ALOGE("RS Error %s", (const char *)rbuf);
119
120            if(rs->mMessageFunc != NULL) {
121                rs->mErrorFunc(usrID, (const char *)rbuf);
122            }
123            break;
124        case RS_MESSAGE_TO_CLIENT_EXCEPTION:
125            // teardown. But we want to avoid starving other threads during
126            // teardown by yielding until the next line in the destructor can
127            // execute to set mRun = false
128            usleep(1000);
129            break;
130        case RS_MESSAGE_TO_CLIENT_USER:
131            if(rs->mMessageFunc != NULL) {
132                rs->mMessageFunc(usrID, rbuf, receiveLen);
133            } else {
134                ALOGE("Received a message from the script with no message handler installed.");
135            }
136            break;
137
138        default:
139            ALOGE("RenderScript unknown message type %i", r);
140        }
141    }
142
143    if (rbuf) {
144        free(rbuf);
145    }
146    ALOGE("RenderScript Message thread exiting.");
147    return NULL;
148}
149
150void RenderScript::setErrorHandler(ErrorHandlerFunc_t func) {
151    mErrorFunc = func;
152}
153
154void RenderScript::setMessageHandler(MessageHandlerFunc_t func) {
155    mMessageFunc  = func;
156}
157
158void RenderScript::contextDump() {
159}
160
161void RenderScript::finish() {
162
163}
164
165
166