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