Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 1 | /* |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 2 | * Copyright (C) 2012 The Android Open Source Project |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 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 | |
Jason Sams | f1e6d22 | 2012-02-24 14:24:56 -0800 | [diff] [blame] | 17 | #define LOG_TAG "libRS_cpp" |
| 18 | |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 19 | #include <utils/Log.h> |
| 20 | #include <malloc.h> |
| 21 | #include <string.h> |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 22 | #include <pthread.h> |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 23 | |
| 24 | #include "RenderScript.h" |
Alex Sakhartchouk | e23d239 | 2012-03-09 09:24:39 -0800 | [diff] [blame] | 25 | #include "rs.h" |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 26 | |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 27 | using namespace android; |
Tim Murray | 9eb7f4b | 2012-11-16 14:02:18 -0800 | [diff] [blame^] | 28 | using namespace RSC; |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 29 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 30 | bool RS::gInitialized = false; |
| 31 | pthread_mutex_t RS::gInitMutex = PTHREAD_MUTEX_INITIALIZER; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 32 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 33 | RS::RS() { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 34 | mDev = NULL; |
| 35 | mContext = NULL; |
| 36 | mErrorFunc = NULL; |
| 37 | mMessageFunc = NULL; |
| 38 | mMessageRun = false; |
| 39 | |
| 40 | memset(&mElements, 0, sizeof(mElements)); |
| 41 | } |
| 42 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 43 | RS::~RS() { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 44 | 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 Murray | 7e0acab | 2012-11-06 14:37:19 -0800 | [diff] [blame] | 57 | bool RS::init(bool forceCpu) { |
| 58 | return RS::init(RS_VERSION, forceCpu); |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Tim Murray | 7e0acab | 2012-11-06 14:37:19 -0800 | [diff] [blame] | 61 | bool RS::init(int targetApi, bool forceCpu) { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 62 | mDev = rsDeviceCreate(); |
| 63 | if (mDev == 0) { |
| 64 | ALOGE("Device creation failed"); |
| 65 | return false; |
| 66 | } |
| 67 | |
Tim Murray | 7e0acab | 2012-11-06 14:37:19 -0800 | [diff] [blame] | 68 | mContext = rsContextCreate(mDev, 0, targetApi, forceCpu); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 69 | if (mContext == 0) { |
| 70 | ALOGE("Context creation failed"); |
| 71 | return false; |
| 72 | } |
| 73 | |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 74 | pid_t mNativeMessageThreadId; |
| 75 | |
| 76 | int status = pthread_create(&mMessageThreadId, NULL, threadProc, this); |
| 77 | if (status) { |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 78 | ALOGE("Failed to start RS message thread."); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 79 | 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 Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 89 | void RS::throwError(const char *err) const { |
Jason Sams | b2e3dc5 | 2012-02-23 17:14:39 -0800 | [diff] [blame] | 90 | ALOGE("RS CPP error: %s", err); |
| 91 | int * v = NULL; |
| 92 | v[0] = 0; |
| 93 | } |
| 94 | |
| 95 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 96 | void * RS::threadProc(void *vrsc) { |
| 97 | RS *rs = static_cast<RS *>(vrsc); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 98 | 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 Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 117 | ALOGE("RS::message handler realloc error %zu", rbuf_size); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 118 | // 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 Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 146 | ALOGE("RS unknown message type %i", r); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | if (rbuf) { |
| 151 | free(rbuf); |
| 152 | } |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 153 | ALOGE("RS Message thread exiting."); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 154 | return NULL; |
| 155 | } |
| 156 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 157 | void RS::setErrorHandler(ErrorHandlerFunc_t func) { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 158 | mErrorFunc = func; |
| 159 | } |
| 160 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 161 | void RS::setMessageHandler(MessageHandlerFunc_t func) { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 162 | mMessageFunc = func; |
| 163 | } |
Tim Murray | baca6c3 | 2012-11-14 16:51:46 -0800 | [diff] [blame] | 164 | |
| 165 | void RS::finish() { |
| 166 | rsContextFinish(mContext); |
| 167 | } |