blob: 503798b6b5e6f782a45867b18a5670aad7f13226 [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"
Tim Murray0b575de2013-03-15 15:56:43 -070023#include "rsUtils.h"
Jason Sams221a4b12012-02-22 15:22:41 -080024
Jason Sams69cccdf2012-04-02 19:11:49 -070025using namespace android;
Tim Murray9eb7f4b2012-11-16 14:02:18 -080026using namespace RSC;
Jason Sams69cccdf2012-04-02 19:11:49 -070027
Tim Murray84bf2b82012-10-31 16:03:16 -070028bool RS::gInitialized = false;
29pthread_mutex_t RS::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams221a4b12012-02-22 15:22:41 -080030
Tim Murray84bf2b82012-10-31 16:03:16 -070031RS::RS() {
Jason Sams221a4b12012-02-22 15:22:41 -080032 mDev = NULL;
33 mContext = NULL;
34 mErrorFunc = NULL;
35 mMessageFunc = NULL;
36 mMessageRun = false;
37
38 memset(&mElements, 0, sizeof(mElements));
39}
40
Tim Murray84bf2b82012-10-31 16:03:16 -070041RS::~RS() {
Jason Sams221a4b12012-02-22 15:22:41 -080042 mMessageRun = false;
43
44 rsContextDeinitToClient(mContext);
45
46 void *res = NULL;
47 int status = pthread_join(mMessageThreadId, &res);
48
49 rsContextDestroy(mContext);
50 mContext = NULL;
51 rsDeviceDestroy(mDev);
52 mDev = NULL;
53}
54
Tim Murray4d252d62012-11-29 14:37:59 -080055bool RS::init(bool forceCpu, bool synchronous) {
56 return RS::init(RS_VERSION, forceCpu, synchronous);
Tim Murray84bf2b82012-10-31 16:03:16 -070057}
58
Tim Murray4d252d62012-11-29 14:37:59 -080059bool RS::init(int targetApi, bool forceCpu, bool synchronous) {
Jason Sams221a4b12012-02-22 15:22:41 -080060 mDev = rsDeviceCreate();
61 if (mDev == 0) {
62 ALOGE("Device creation failed");
63 return false;
64 }
65
Jason Sams14982c82013-02-22 18:17:05 -080066 mContext = rsContextCreate(mDev, 0, targetApi, RS_CONTEXT_TYPE_NORMAL, forceCpu, synchronous);
Jason Sams221a4b12012-02-22 15:22:41 -080067 if (mContext == 0) {
68 ALOGE("Context creation failed");
69 return false;
70 }
71
Jason Sams221a4b12012-02-22 15:22:41 -080072 pid_t mNativeMessageThreadId;
73
74 int status = pthread_create(&mMessageThreadId, NULL, threadProc, this);
75 if (status) {
Tim Murray84bf2b82012-10-31 16:03:16 -070076 ALOGE("Failed to start RS message thread.");
Jason Sams221a4b12012-02-22 15:22:41 -080077 return false;
78 }
79 // Wait for the message thread to be active.
80 while (!mMessageRun) {
81 usleep(1000);
82 }
83
84 return true;
85}
86
Tim Murray84bf2b82012-10-31 16:03:16 -070087void RS::throwError(const char *err) const {
Jason Samsb2e3dc52012-02-23 17:14:39 -080088 ALOGE("RS CPP error: %s", err);
89 int * v = NULL;
90 v[0] = 0;
91}
92
93
Tim Murray84bf2b82012-10-31 16:03:16 -070094void * RS::threadProc(void *vrsc) {
95 RS *rs = static_cast<RS *>(vrsc);
Jason Sams221a4b12012-02-22 15:22:41 -080096 size_t rbuf_size = 256;
97 void * rbuf = malloc(rbuf_size);
98
99 rsContextInitToClient(rs->mContext);
100 rs->mMessageRun = true;
101
102 while (rs->mMessageRun) {
103 size_t receiveLen = 0;
104 uint32_t usrID = 0;
105 uint32_t subID = 0;
106 RsMessageToClientType r = rsContextPeekMessage(rs->mContext,
107 &receiveLen, sizeof(receiveLen),
108 &usrID, sizeof(usrID));
109
110 if (receiveLen >= rbuf_size) {
111 rbuf_size = receiveLen + 32;
112 rbuf = realloc(rbuf, rbuf_size);
113 }
114 if (!rbuf) {
Tim Murray84bf2b82012-10-31 16:03:16 -0700115 ALOGE("RS::message handler realloc error %zu", rbuf_size);
Jason Sams221a4b12012-02-22 15:22:41 -0800116 // No clean way to recover now?
117 }
118 rsContextGetMessage(rs->mContext, rbuf, rbuf_size, &receiveLen, sizeof(receiveLen),
119 &subID, sizeof(subID));
120
121 switch(r) {
122 case RS_MESSAGE_TO_CLIENT_ERROR:
123 ALOGE("RS Error %s", (const char *)rbuf);
124
125 if(rs->mMessageFunc != NULL) {
126 rs->mErrorFunc(usrID, (const char *)rbuf);
127 }
128 break;
Stephen Hines76a1be42012-11-26 16:26:03 -0800129 case RS_MESSAGE_TO_CLIENT_NONE:
Jason Sams221a4b12012-02-22 15:22:41 -0800130 case RS_MESSAGE_TO_CLIENT_EXCEPTION:
Stephen Hines76a1be42012-11-26 16:26:03 -0800131 case RS_MESSAGE_TO_CLIENT_RESIZE:
Jason Sams221a4b12012-02-22 15:22:41 -0800132 // teardown. But we want to avoid starving other threads during
133 // teardown by yielding until the next line in the destructor can
Stephen Hines76a1be42012-11-26 16:26:03 -0800134 // execute to set mRun = false. Note that the FIFO sends an
135 // empty NONE message when it reaches its destructor.
Jason Sams221a4b12012-02-22 15:22:41 -0800136 usleep(1000);
137 break;
138 case RS_MESSAGE_TO_CLIENT_USER:
139 if(rs->mMessageFunc != NULL) {
140 rs->mMessageFunc(usrID, rbuf, receiveLen);
141 } else {
142 ALOGE("Received a message from the script with no message handler installed.");
143 }
144 break;
145
146 default:
Tim Murray84bf2b82012-10-31 16:03:16 -0700147 ALOGE("RS unknown message type %i", r);
Jason Sams221a4b12012-02-22 15:22:41 -0800148 }
149 }
150
151 if (rbuf) {
152 free(rbuf);
153 }
Tim Murray84bf2b82012-10-31 16:03:16 -0700154 ALOGE("RS Message thread exiting.");
Jason Sams221a4b12012-02-22 15:22:41 -0800155 return NULL;
156}
157
Tim Murray84bf2b82012-10-31 16:03:16 -0700158void RS::setErrorHandler(ErrorHandlerFunc_t func) {
Jason Sams221a4b12012-02-22 15:22:41 -0800159 mErrorFunc = func;
160}
161
Tim Murray84bf2b82012-10-31 16:03:16 -0700162void RS::setMessageHandler(MessageHandlerFunc_t func) {
Jason Sams221a4b12012-02-22 15:22:41 -0800163 mMessageFunc = func;
164}
Tim Murraybaca6c32012-11-14 16:51:46 -0800165
166void RS::finish() {
167 rsContextFinish(mContext);
168}