blob: 134d34ba858b7582618e8e59f3a089de8e0b6b2f [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 Sams221a4b12012-02-22 15:22:41 -080017#include <malloc.h>
18#include <string.h>
Tim Murray84bf2b82012-10-31 16:03:16 -070019#include <pthread.h>
Jason Sams221a4b12012-02-22 15:22:41 -080020
21#include "RenderScript.h"
Alex Sakhartchouke23d2392012-03-09 09:24:39 -080022#include "rs.h"
Jason Sams221a4b12012-02-22 15:22:41 -080023
Jason Sams69cccdf2012-04-02 19:11:49 -070024using namespace android;
Tim Murray9eb7f4b2012-11-16 14:02:18 -080025using namespace RSC;
Jason Sams69cccdf2012-04-02 19:11:49 -070026
Tim Murray84bf2b82012-10-31 16:03:16 -070027bool RS::gInitialized = false;
28pthread_mutex_t RS::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams221a4b12012-02-22 15:22:41 -080029
Tim Murray84bf2b82012-10-31 16:03:16 -070030RS::RS() {
Jason Sams221a4b12012-02-22 15:22:41 -080031 mDev = NULL;
32 mContext = NULL;
33 mErrorFunc = NULL;
34 mMessageFunc = NULL;
35 mMessageRun = false;
36
37 memset(&mElements, 0, sizeof(mElements));
38}
39
Tim Murray84bf2b82012-10-31 16:03:16 -070040RS::~RS() {
Jason Sams221a4b12012-02-22 15:22:41 -080041 mMessageRun = false;
42
43 rsContextDeinitToClient(mContext);
44
45 void *res = NULL;
46 int status = pthread_join(mMessageThreadId, &res);
47
48 rsContextDestroy(mContext);
49 mContext = NULL;
50 rsDeviceDestroy(mDev);
51 mDev = NULL;
52}
53
Tim Murray4d252d62012-11-29 14:37:59 -080054bool RS::init(bool forceCpu, bool synchronous) {
55 return RS::init(RS_VERSION, forceCpu, synchronous);
Tim Murray84bf2b82012-10-31 16:03:16 -070056}
57
Tim Murray4d252d62012-11-29 14:37:59 -080058bool RS::init(int targetApi, bool forceCpu, bool synchronous) {
Jason Sams221a4b12012-02-22 15:22:41 -080059 mDev = rsDeviceCreate();
60 if (mDev == 0) {
61 ALOGE("Device creation failed");
62 return false;
63 }
64
Jason Sams14982c82013-02-22 18:17:05 -080065 mContext = rsContextCreate(mDev, 0, targetApi, RS_CONTEXT_TYPE_NORMAL, forceCpu, synchronous);
Jason Sams221a4b12012-02-22 15:22:41 -080066 if (mContext == 0) {
67 ALOGE("Context creation failed");
68 return false;
69 }
70
Jason Sams221a4b12012-02-22 15:22:41 -080071 pid_t mNativeMessageThreadId;
72
73 int status = pthread_create(&mMessageThreadId, NULL, threadProc, this);
74 if (status) {
Tim Murray84bf2b82012-10-31 16:03:16 -070075 ALOGE("Failed to start RS message thread.");
Jason Sams221a4b12012-02-22 15:22:41 -080076 return false;
77 }
78 // Wait for the message thread to be active.
79 while (!mMessageRun) {
80 usleep(1000);
81 }
82
83 return true;
84}
85
Tim Murray84bf2b82012-10-31 16:03:16 -070086void RS::throwError(const char *err) const {
Jason Samsb2e3dc52012-02-23 17:14:39 -080087 ALOGE("RS CPP error: %s", err);
88 int * v = NULL;
89 v[0] = 0;
90}
91
92
Tim Murray84bf2b82012-10-31 16:03:16 -070093void * RS::threadProc(void *vrsc) {
94 RS *rs = static_cast<RS *>(vrsc);
Jason Sams221a4b12012-02-22 15:22:41 -080095 size_t rbuf_size = 256;
96 void * rbuf = malloc(rbuf_size);
97
98 rsContextInitToClient(rs->mContext);
99 rs->mMessageRun = true;
100
101 while (rs->mMessageRun) {
102 size_t receiveLen = 0;
103 uint32_t usrID = 0;
104 uint32_t subID = 0;
105 RsMessageToClientType r = rsContextPeekMessage(rs->mContext,
106 &receiveLen, sizeof(receiveLen),
107 &usrID, sizeof(usrID));
108
109 if (receiveLen >= rbuf_size) {
110 rbuf_size = receiveLen + 32;
111 rbuf = realloc(rbuf, rbuf_size);
112 }
113 if (!rbuf) {
Tim Murray84bf2b82012-10-31 16:03:16 -0700114 ALOGE("RS::message handler realloc error %zu", rbuf_size);
Jason Sams221a4b12012-02-22 15:22:41 -0800115 // No clean way to recover now?
116 }
117 rsContextGetMessage(rs->mContext, rbuf, rbuf_size, &receiveLen, sizeof(receiveLen),
118 &subID, sizeof(subID));
119
120 switch(r) {
121 case RS_MESSAGE_TO_CLIENT_ERROR:
122 ALOGE("RS Error %s", (const char *)rbuf);
123
124 if(rs->mMessageFunc != NULL) {
125 rs->mErrorFunc(usrID, (const char *)rbuf);
126 }
127 break;
Stephen Hines76a1be42012-11-26 16:26:03 -0800128 case RS_MESSAGE_TO_CLIENT_NONE:
Jason Sams221a4b12012-02-22 15:22:41 -0800129 case RS_MESSAGE_TO_CLIENT_EXCEPTION:
Stephen Hines76a1be42012-11-26 16:26:03 -0800130 case RS_MESSAGE_TO_CLIENT_RESIZE:
Jason Sams221a4b12012-02-22 15:22:41 -0800131 // teardown. But we want to avoid starving other threads during
132 // teardown by yielding until the next line in the destructor can
Stephen Hines76a1be42012-11-26 16:26:03 -0800133 // execute to set mRun = false. Note that the FIFO sends an
134 // empty NONE message when it reaches its destructor.
Jason Sams221a4b12012-02-22 15:22:41 -0800135 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}