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