RenderScript.cpp revision 84bf2b877024aaa154b66e0f2338d54bdabd855a
1/* 2 * Copyright (C) 2012 The Android Open Source Project 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 17#define LOG_TAG "libRS_cpp" 18 19#include <utils/Log.h> 20#include <malloc.h> 21#include <string.h> 22#include <pthread.h> 23 24#include "RenderScript.h" 25#include "rs.h" 26 27using namespace android; 28using namespace renderscriptCpp; 29 30bool RS::gInitialized = false; 31pthread_mutex_t RS::gInitMutex = PTHREAD_MUTEX_INITIALIZER; 32 33RS::RS() { 34 mDev = NULL; 35 mContext = NULL; 36 mErrorFunc = NULL; 37 mMessageFunc = NULL; 38 mMessageRun = false; 39 40 memset(&mElements, 0, sizeof(mElements)); 41} 42 43RS::~RS() { 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 57bool RS::init() { 58 return RS::init(RS_VERSION); 59} 60 61bool RS::init(int targetApi) { 62 mDev = rsDeviceCreate(); 63 if (mDev == 0) { 64 ALOGE("Device creation failed"); 65 return false; 66 } 67 68 mContext = rsContextCreate(mDev, 0, targetApi); 69 if (mContext == 0) { 70 ALOGE("Context creation failed"); 71 return false; 72 } 73 74 pid_t mNativeMessageThreadId; 75 76 int status = pthread_create(&mMessageThreadId, NULL, threadProc, this); 77 if (status) { 78 ALOGE("Failed to start RS message thread."); 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 89void RS::throwError(const char *err) const { 90 ALOGE("RS CPP error: %s", err); 91 int * v = NULL; 92 v[0] = 0; 93} 94 95 96void * RS::threadProc(void *vrsc) { 97 RS *rs = static_cast<RS *>(vrsc); 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) { 117 ALOGE("RS::message handler realloc error %zu", rbuf_size); 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: 146 ALOGE("RS unknown message type %i", r); 147 } 148 } 149 150 if (rbuf) { 151 free(rbuf); 152 } 153 ALOGE("RS Message thread exiting."); 154 return NULL; 155} 156 157void RS::setErrorHandler(ErrorHandlerFunc_t func) { 158 mErrorFunc = func; 159} 160 161void RS::setMessageHandler(MessageHandlerFunc_t func) { 162 mMessageFunc = func; 163} 164