blob: 889f27b53e79847b4a9dcd6eb4624d6545c25f6f [file] [log] [blame]
Jason Sams221a4b12012-02-22 15:22:41 -08001/*
Jason Sams69cccdf2012-04-02 19:11:49 -07002 * Copyright (C) 2012 The Android Open Source Project
Jason Sams221a4b12012-02-22 15:22:41 -08003 *
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
Jason Samsf1e6d222012-02-24 14:24:56 -080017#define LOG_TAG "libRS_cpp"
18
Jason Sams221a4b12012-02-22 15:22:41 -080019#include <utils/Log.h>
20#include <malloc.h>
21#include <string.h>
22
23#include "RenderScript.h"
Alex Sakhartchouke23d2392012-03-09 09:24:39 -080024#include "rs.h"
Jason Sams221a4b12012-02-22 15:22:41 -080025
Jason Sams69cccdf2012-04-02 19:11:49 -070026using namespace android;
27using namespace renderscriptCpp;
28
Jason Sams221a4b12012-02-22 15:22:41 -080029bool RenderScript::gInitialized = false;
30pthread_mutex_t RenderScript::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
31
32RenderScript::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
42RenderScript::~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
56bool 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
Jason Samsb2e3dc52012-02-23 17:14:39 -080085void RenderScript::throwError(const char *err) const {
86 ALOGE("RS CPP error: %s", err);
87 int * v = NULL;
88 v[0] = 0;
89}
90
91
Jason Sams221a4b12012-02-22 15:22:41 -080092void * 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
153void RenderScript::setErrorHandler(ErrorHandlerFunc_t func) {
154 mErrorFunc = func;
155}
156
157void RenderScript::setMessageHandler(MessageHandlerFunc_t func) {
158 mMessageFunc = func;
159}
160
161void RenderScript::contextDump() {
162}
163
164void RenderScript::finish() {
165
166}
167
168