blob: ffc0ce672a22e7ca98a7aa3f2d0e96bcea10ca26 [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>
Tim Murray84bf2b82012-10-31 16:03:16 -070022#include <pthread.h>
Jason Sams221a4b12012-02-22 15:22:41 -080023
24#include "RenderScript.h"
Alex Sakhartchouke23d2392012-03-09 09:24:39 -080025#include "rs.h"
Jason Sams221a4b12012-02-22 15:22:41 -080026
Jason Sams69cccdf2012-04-02 19:11:49 -070027using namespace android;
Tim Murray9eb7f4b2012-11-16 14:02:18 -080028using namespace RSC;
Jason Sams69cccdf2012-04-02 19:11:49 -070029
Tim Murray84bf2b82012-10-31 16:03:16 -070030bool RS::gInitialized = false;
31pthread_mutex_t RS::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams221a4b12012-02-22 15:22:41 -080032
Tim Murray84bf2b82012-10-31 16:03:16 -070033RS::RS() {
Jason Sams221a4b12012-02-22 15:22:41 -080034 mDev = NULL;
35 mContext = NULL;
36 mErrorFunc = NULL;
37 mMessageFunc = NULL;
38 mMessageRun = false;
39
40 memset(&mElements, 0, sizeof(mElements));
41}
42
Tim Murray84bf2b82012-10-31 16:03:16 -070043RS::~RS() {
Jason Sams221a4b12012-02-22 15:22:41 -080044 mMessageRun = false;
45
46 rsContextDeinitToClient(mContext);
47
48 void *res = NULL;
49 int status = pthread_join(mMessageThreadId, &res);
50
51 rsContextDestroy(mContext);
52 mContext = NULL;
53 rsDeviceDestroy(mDev);
54 mDev = NULL;
55}
56
Tim Murray7e0acab2012-11-06 14:37:19 -080057bool RS::init(bool forceCpu) {
58 return RS::init(RS_VERSION, forceCpu);
Tim Murray84bf2b82012-10-31 16:03:16 -070059}
60
Tim Murray7e0acab2012-11-06 14:37:19 -080061bool RS::init(int targetApi, bool forceCpu) {
Jason Sams221a4b12012-02-22 15:22:41 -080062 mDev = rsDeviceCreate();
63 if (mDev == 0) {
64 ALOGE("Device creation failed");
65 return false;
66 }
67
Tim Murray7e0acab2012-11-06 14:37:19 -080068 mContext = rsContextCreate(mDev, 0, targetApi, forceCpu);
Jason Sams221a4b12012-02-22 15:22:41 -080069 if (mContext == 0) {
70 ALOGE("Context creation failed");
71 return false;
72 }
73
Jason Sams221a4b12012-02-22 15:22:41 -080074 pid_t mNativeMessageThreadId;
75
76 int status = pthread_create(&mMessageThreadId, NULL, threadProc, this);
77 if (status) {
Tim Murray84bf2b82012-10-31 16:03:16 -070078 ALOGE("Failed to start RS message thread.");
Jason Sams221a4b12012-02-22 15:22:41 -080079 return false;
80 }
81 // Wait for the message thread to be active.
82 while (!mMessageRun) {
83 usleep(1000);
84 }
85
86 return true;
87}
88
Tim Murray84bf2b82012-10-31 16:03:16 -070089void RS::throwError(const char *err) const {
Jason Samsb2e3dc52012-02-23 17:14:39 -080090 ALOGE("RS CPP error: %s", err);
91 int * v = NULL;
92 v[0] = 0;
93}
94
95
Tim Murray84bf2b82012-10-31 16:03:16 -070096void * RS::threadProc(void *vrsc) {
97 RS *rs = static_cast<RS *>(vrsc);
Jason Sams221a4b12012-02-22 15:22:41 -080098 size_t rbuf_size = 256;
99 void * rbuf = malloc(rbuf_size);
100
101 rsContextInitToClient(rs->mContext);
102 rs->mMessageRun = true;
103
104 while (rs->mMessageRun) {
105 size_t receiveLen = 0;
106 uint32_t usrID = 0;
107 uint32_t subID = 0;
108 RsMessageToClientType r = rsContextPeekMessage(rs->mContext,
109 &receiveLen, sizeof(receiveLen),
110 &usrID, sizeof(usrID));
111
112 if (receiveLen >= rbuf_size) {
113 rbuf_size = receiveLen + 32;
114 rbuf = realloc(rbuf, rbuf_size);
115 }
116 if (!rbuf) {
Tim Murray84bf2b82012-10-31 16:03:16 -0700117 ALOGE("RS::message handler realloc error %zu", rbuf_size);
Jason Sams221a4b12012-02-22 15:22:41 -0800118 // No clean way to recover now?
119 }
120 rsContextGetMessage(rs->mContext, rbuf, rbuf_size, &receiveLen, sizeof(receiveLen),
121 &subID, sizeof(subID));
122
123 switch(r) {
124 case RS_MESSAGE_TO_CLIENT_ERROR:
125 ALOGE("RS Error %s", (const char *)rbuf);
126
127 if(rs->mMessageFunc != NULL) {
128 rs->mErrorFunc(usrID, (const char *)rbuf);
129 }
130 break;
131 case RS_MESSAGE_TO_CLIENT_EXCEPTION:
132 // teardown. But we want to avoid starving other threads during
133 // teardown by yielding until the next line in the destructor can
134 // execute to set mRun = false
135 usleep(1000);
136 break;
137 case RS_MESSAGE_TO_CLIENT_USER:
138 if(rs->mMessageFunc != NULL) {
139 rs->mMessageFunc(usrID, rbuf, receiveLen);
140 } else {
141 ALOGE("Received a message from the script with no message handler installed.");
142 }
143 break;
144
145 default:
Tim Murray84bf2b82012-10-31 16:03:16 -0700146 ALOGE("RS unknown message type %i", r);
Jason Sams221a4b12012-02-22 15:22:41 -0800147 }
148 }
149
150 if (rbuf) {
151 free(rbuf);
152 }
Tim Murray84bf2b82012-10-31 16:03:16 -0700153 ALOGE("RS Message thread exiting.");
Jason Sams221a4b12012-02-22 15:22:41 -0800154 return NULL;
155}
156
Tim Murray84bf2b82012-10-31 16:03:16 -0700157void RS::setErrorHandler(ErrorHandlerFunc_t func) {
Jason Sams221a4b12012-02-22 15:22:41 -0800158 mErrorFunc = func;
159}
160
Tim Murray84bf2b82012-10-31 16:03:16 -0700161void RS::setMessageHandler(MessageHandlerFunc_t func) {
Jason Sams221a4b12012-02-22 15:22:41 -0800162 mMessageFunc = func;
163}
Tim Murraybaca6c32012-11-14 16:51:46 -0800164
165void RS::finish() {
166 rsContextFinish(mContext);
167}