blob: 58d1ce1594da7434484ab3174ef531c068e49225 [file] [log] [blame]
Jason Samsf70b0fc82012-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
79void * RenderScript::threadProc(void *vrsc) {
80 RenderScript *rs = static_cast<RenderScript *>(vrsc);
81 size_t rbuf_size = 256;
82 void * rbuf = malloc(rbuf_size);
83
84 rsContextInitToClient(rs->mContext);
85 rs->mMessageRun = true;
86
87 while (rs->mMessageRun) {
88 size_t receiveLen = 0;
89 uint32_t usrID = 0;
90 uint32_t subID = 0;
91 RsMessageToClientType r = rsContextPeekMessage(rs->mContext,
92 &receiveLen, sizeof(receiveLen),
93 &usrID, sizeof(usrID));
94
95 if (receiveLen >= rbuf_size) {
96 rbuf_size = receiveLen + 32;
97 rbuf = realloc(rbuf, rbuf_size);
98 }
99 if (!rbuf) {
100 ALOGE("RenderScript::message handler realloc error %zu", rbuf_size);
101 // No clean way to recover now?
102 }
103 rsContextGetMessage(rs->mContext, rbuf, rbuf_size, &receiveLen, sizeof(receiveLen),
104 &subID, sizeof(subID));
105
106 switch(r) {
107 case RS_MESSAGE_TO_CLIENT_ERROR:
108 ALOGE("RS Error %s", (const char *)rbuf);
109
110 if(rs->mMessageFunc != NULL) {
111 rs->mErrorFunc(usrID, (const char *)rbuf);
112 }
113 break;
114 case RS_MESSAGE_TO_CLIENT_EXCEPTION:
115 // teardown. But we want to avoid starving other threads during
116 // teardown by yielding until the next line in the destructor can
117 // execute to set mRun = false
118 usleep(1000);
119 break;
120 case RS_MESSAGE_TO_CLIENT_USER:
121 if(rs->mMessageFunc != NULL) {
122 rs->mMessageFunc(usrID, rbuf, receiveLen);
123 } else {
124 ALOGE("Received a message from the script with no message handler installed.");
125 }
126 break;
127
128 default:
129 ALOGE("RenderScript unknown message type %i", r);
130 }
131 }
132
133 if (rbuf) {
134 free(rbuf);
135 }
136 ALOGE("RenderScript Message thread exiting.");
137 return NULL;
138}
139
140void RenderScript::setErrorHandler(ErrorHandlerFunc_t func) {
141 mErrorFunc = func;
142}
143
144void RenderScript::setMessageHandler(MessageHandlerFunc_t func) {
145 mMessageFunc = func;
146}
147
148void RenderScript::contextDump() {
149}
150
151void RenderScript::finish() {
152
153}
154
155