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 | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 17 | #include <malloc.h> |
| 18 | #include <string.h> |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 19 | #include <pthread.h> |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 20 | |
| 21 | #include "RenderScript.h" |
Alex Sakhartchouk | e23d239 | 2012-03-09 09:24:39 -0800 | [diff] [blame] | 22 | #include "rs.h" |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame^] | 23 | #include "rsUtils.h" |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 24 | |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 25 | using namespace android; |
Tim Murray | 9eb7f4b | 2012-11-16 14:02:18 -0800 | [diff] [blame] | 26 | using namespace RSC; |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 27 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 28 | bool RS::gInitialized = false; |
| 29 | pthread_mutex_t RS::gInitMutex = PTHREAD_MUTEX_INITIALIZER; |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 30 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 31 | RS::RS() { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 32 | mDev = NULL; |
| 33 | mContext = NULL; |
| 34 | mErrorFunc = NULL; |
| 35 | mMessageFunc = NULL; |
| 36 | mMessageRun = false; |
| 37 | |
| 38 | memset(&mElements, 0, sizeof(mElements)); |
| 39 | } |
| 40 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 41 | RS::~RS() { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 42 | 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 Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 55 | bool RS::init(bool forceCpu, bool synchronous) { |
| 56 | return RS::init(RS_VERSION, forceCpu, synchronous); |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 59 | bool RS::init(int targetApi, bool forceCpu, bool synchronous) { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 60 | mDev = rsDeviceCreate(); |
| 61 | if (mDev == 0) { |
| 62 | ALOGE("Device creation failed"); |
| 63 | return false; |
| 64 | } |
| 65 | |
Jason Sams | 14982c8 | 2013-02-22 18:17:05 -0800 | [diff] [blame] | 66 | mContext = rsContextCreate(mDev, 0, targetApi, RS_CONTEXT_TYPE_NORMAL, forceCpu, synchronous); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 67 | if (mContext == 0) { |
| 68 | ALOGE("Context creation failed"); |
| 69 | return false; |
| 70 | } |
| 71 | |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 72 | pid_t mNativeMessageThreadId; |
| 73 | |
| 74 | int status = pthread_create(&mMessageThreadId, NULL, threadProc, this); |
| 75 | if (status) { |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 76 | ALOGE("Failed to start RS message thread."); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 77 | 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 Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 87 | void RS::throwError(const char *err) const { |
Jason Sams | b2e3dc5 | 2012-02-23 17:14:39 -0800 | [diff] [blame] | 88 | ALOGE("RS CPP error: %s", err); |
| 89 | int * v = NULL; |
| 90 | v[0] = 0; |
| 91 | } |
| 92 | |
| 93 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 94 | void * RS::threadProc(void *vrsc) { |
| 95 | RS *rs = static_cast<RS *>(vrsc); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 96 | 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 Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 115 | ALOGE("RS::message handler realloc error %zu", rbuf_size); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 116 | // 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 Hines | 76a1be4 | 2012-11-26 16:26:03 -0800 | [diff] [blame] | 129 | case RS_MESSAGE_TO_CLIENT_NONE: |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 130 | case RS_MESSAGE_TO_CLIENT_EXCEPTION: |
Stephen Hines | 76a1be4 | 2012-11-26 16:26:03 -0800 | [diff] [blame] | 131 | case RS_MESSAGE_TO_CLIENT_RESIZE: |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 132 | // teardown. But we want to avoid starving other threads during |
| 133 | // teardown by yielding until the next line in the destructor can |
Stephen Hines | 76a1be4 | 2012-11-26 16:26:03 -0800 | [diff] [blame] | 134 | // execute to set mRun = false. Note that the FIFO sends an |
| 135 | // empty NONE message when it reaches its destructor. |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 136 | 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 Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 147 | ALOGE("RS unknown message type %i", r); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | if (rbuf) { |
| 152 | free(rbuf); |
| 153 | } |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 154 | ALOGE("RS Message thread exiting."); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 155 | return NULL; |
| 156 | } |
| 157 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 158 | void RS::setErrorHandler(ErrorHandlerFunc_t func) { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 159 | mErrorFunc = func; |
| 160 | } |
| 161 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 162 | void RS::setMessageHandler(MessageHandlerFunc_t func) { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 163 | mMessageFunc = func; |
| 164 | } |
Tim Murray | baca6c3 | 2012-11-14 16:51:46 -0800 | [diff] [blame] | 165 | |
| 166 | void RS::finish() { |
| 167 | rsContextFinish(mContext); |
| 168 | } |