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