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