Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 2 | * Copyright (C) 2011 The Android Open Source Project |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [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 | |
Alex Sakhartchouk | e23d239 | 2012-03-09 09:24:39 -0800 | [diff] [blame] | 17 | #include "rs.h" |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 18 | #include "rsDevice.h" |
| 19 | #include "rsContext.h" |
| 20 | #include "rsThreadIO.h" |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 21 | |
| 22 | #ifndef RS_COMPATIBILITY_LIB |
Alex Sakhartchouk | 4edf030 | 2012-03-09 10:47:27 -0800 | [diff] [blame] | 23 | #include "rsMesh.h" |
Mathias Agopian | 5ae678f | 2009-06-22 18:01:09 -0700 | [diff] [blame] | 24 | #include <ui/FramebufferNativeWindow.h> |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 25 | #include <gui/DisplayEventReceiver.h> |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 26 | #endif |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 27 | |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 28 | #include <sys/types.h> |
| 29 | #include <sys/resource.h> |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 30 | #include <sched.h> |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 31 | |
Joe Onorato | 76371ff | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 32 | #include <cutils/properties.h> |
| 33 | |
Jason Sams | 8d957fa | 2010-09-28 14:41:22 -0700 | [diff] [blame] | 34 | #include <sys/syscall.h> |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 35 | #include <string.h> |
Stephen Hines | 414a461 | 2012-09-05 18:05:08 -0700 | [diff] [blame] | 36 | #include <dlfcn.h> |
| 37 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 38 | using namespace android; |
| 39 | using namespace android::renderscript; |
| 40 | |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 41 | pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER; |
Stephen Hines | ca3f09c | 2011-01-07 15:11:30 -0800 | [diff] [blame] | 42 | pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 43 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 44 | bool Context::initGLThread() { |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 45 | pthread_mutex_lock(&gInitMutex); |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 46 | |
Jason Sams | 4b3de47 | 2011-04-06 17:52:23 -0700 | [diff] [blame] | 47 | if (!mHal.funcs.initGraphics(this)) { |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 48 | pthread_mutex_unlock(&gInitMutex); |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 49 | ALOGE("%p initGraphics failed", this); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 50 | return false; |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 53 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 54 | return true; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 57 | void Context::deinitEGL() { |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 58 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | 4b3de47 | 2011-04-06 17:52:23 -0700 | [diff] [blame] | 59 | mHal.funcs.shutdownGraphics(this); |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 60 | #endif |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 63 | Context::PushState::PushState(Context *con) { |
| 64 | mRsc = con; |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 65 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | c946b61 | 2011-02-23 14:47:17 -0800 | [diff] [blame] | 66 | if (con->mIsGraphicsContext) { |
| 67 | mFragment.set(con->getProgramFragment()); |
| 68 | mVertex.set(con->getProgramVertex()); |
| 69 | mStore.set(con->getProgramStore()); |
| 70 | mRaster.set(con->getProgramRaster()); |
| 71 | mFont.set(con->getFont()); |
| 72 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 73 | #endif |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | Context::PushState::~PushState() { |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 77 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | c946b61 | 2011-02-23 14:47:17 -0800 | [diff] [blame] | 78 | if (mRsc->mIsGraphicsContext) { |
| 79 | mRsc->setProgramFragment(mFragment.get()); |
| 80 | mRsc->setProgramVertex(mVertex.get()); |
| 81 | mRsc->setProgramStore(mStore.get()); |
| 82 | mRsc->setProgramRaster(mRaster.get()); |
| 83 | mRsc->setFont(mFont.get()); |
| 84 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 85 | #endif |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 88 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 89 | uint32_t Context::runScript(Script *s) { |
Shih-wei Liao | da3b58d | 2012-08-03 04:24:33 -0700 | [diff] [blame] | 90 | PushState ps(this); |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 91 | |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 92 | uint32_t ret = s->run(this); |
Jason Sams | c9d43db | 2009-07-28 12:02:16 -0700 | [diff] [blame] | 93 | return ret; |
Jason Sams | 1030893 | 2009-06-09 12:15:30 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 96 | uint32_t Context::runRootScript() { |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 97 | timerSet(RS_TIMER_SCRIPT); |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 98 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 99 | mStateFragmentStore.mLast.clear(); |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 100 | #endif |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 101 | watchdog.inRoot = true; |
Jason Sams | c61346b | 2010-05-28 18:23:22 -0700 | [diff] [blame] | 102 | uint32_t ret = runScript(mRootScript.get()); |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 103 | watchdog.inRoot = false; |
Jason Sams | 8cfdd24 | 2009-10-14 15:43:53 -0700 | [diff] [blame] | 104 | |
Jason Sams | cfb1d11 | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 105 | return ret; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 108 | uint64_t Context::getTime() const { |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 109 | #ifndef ANDROID_RS_SERIALIZE |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 110 | struct timespec t; |
| 111 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 112 | return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000); |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 113 | #else |
| 114 | return 0; |
| 115 | #endif //ANDROID_RS_SERIALIZE |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 118 | void Context::timerReset() { |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 119 | for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) { |
| 120 | mTimers[ct] = 0; |
| 121 | } |
| 122 | } |
| 123 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 124 | void Context::timerInit() { |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 125 | mTimeLast = getTime(); |
Jason Sams | 1d54f10 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 126 | mTimeFrame = mTimeLast; |
| 127 | mTimeLastFrame = mTimeLast; |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 128 | mTimerActive = RS_TIMER_INTERNAL; |
Alex Sakhartchouk | c8fb69e | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 129 | mAverageFPSFrameCount = 0; |
| 130 | mAverageFPSStartTime = mTimeLast; |
| 131 | mAverageFPS = 0; |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 132 | timerReset(); |
| 133 | } |
| 134 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 135 | void Context::timerFrame() { |
Jason Sams | 1d54f10 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 136 | mTimeLastFrame = mTimeFrame; |
| 137 | mTimeFrame = getTime(); |
Alex Sakhartchouk | c8fb69e | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 138 | // Update average fps |
| 139 | const uint64_t averageFramerateInterval = 1000 * 1000000; |
| 140 | mAverageFPSFrameCount ++; |
| 141 | uint64_t inverval = mTimeFrame - mAverageFPSStartTime; |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 142 | if (inverval >= averageFramerateInterval) { |
Alex Sakhartchouk | c8fb69e | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 143 | inverval = inverval / 1000000; |
| 144 | mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval; |
| 145 | mAverageFPSFrameCount = 0; |
| 146 | mAverageFPSStartTime = mTimeFrame; |
| 147 | } |
Jason Sams | 1d54f10 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 150 | void Context::timerSet(Timers tm) { |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 151 | uint64_t last = mTimeLast; |
| 152 | mTimeLast = getTime(); |
| 153 | mTimers[mTimerActive] += mTimeLast - last; |
| 154 | mTimerActive = tm; |
| 155 | } |
| 156 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 157 | void Context::timerPrint() { |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 158 | double total = 0; |
| 159 | for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) { |
| 160 | total += mTimers[ct]; |
| 161 | } |
Jason Sams | 1d54f10 | 2009-09-03 15:43:13 -0700 | [diff] [blame] | 162 | uint64_t frame = mTimeFrame - mTimeLastFrame; |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 163 | mTimeMSLastFrame = frame / 1000000; |
| 164 | mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000; |
| 165 | mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000; |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 166 | |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 167 | |
| 168 | if (props.mLogTimes) { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 169 | ALOGV("RS: Frame (%i), Script %2.1f%% (%i), Swap %2.1f%% (%i), Idle %2.1f%% (%lli), Internal %2.1f%% (%lli), Avg fps: %u", |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 170 | mTimeMSLastFrame, |
| 171 | 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript, |
| 172 | 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap, |
| 173 | 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000, |
Alex Sakhartchouk | c8fb69e | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 174 | 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000, |
| 175 | mAverageFPS); |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 176 | } |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 179 | bool Context::setupCheck() { |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 180 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | 721acc4 | 2011-04-06 11:23:54 -0700 | [diff] [blame] | 181 | mFragmentStore->setup(this, &mStateFragmentStore); |
Alex Sakhartchouk | c19ff01 | 2011-05-06 14:59:45 -0700 | [diff] [blame] | 182 | mFragment->setup(this, &mStateFragment); |
Jason Sams | 721acc4 | 2011-04-06 11:23:54 -0700 | [diff] [blame] | 183 | mRaster->setup(this, &mStateRaster); |
Alex Sakhartchouk | c19ff01 | 2011-05-06 14:59:45 -0700 | [diff] [blame] | 184 | mVertex->setup(this, &mStateVertex); |
| 185 | mFBOCache.setup(this); |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 186 | #endif |
Jason Sams | a2cf755 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 187 | return true; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 190 | #ifndef RS_COMPATIBILITY_LIB |
Alex Sakhartchouk | 889fe50 | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 191 | void Context::setupProgramStore() { |
Jason Sams | 721acc4 | 2011-04-06 11:23:54 -0700 | [diff] [blame] | 192 | mFragmentStore->setup(this, &mStateFragmentStore); |
Alex Sakhartchouk | 889fe50 | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 193 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 194 | #endif |
Alex Sakhartchouk | 889fe50 | 2010-10-01 10:54:06 -0700 | [diff] [blame] | 195 | |
Jason Sams | d1f7da6 | 2012-03-15 19:18:03 -0700 | [diff] [blame] | 196 | static uint32_t getProp(const char *str) { |
Joe Onorato | 76371ff | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 197 | char buf[PROPERTY_VALUE_MAX]; |
Jason Sams | 1fddd90 | 2009-09-25 15:25:00 -0700 | [diff] [blame] | 198 | property_get(str, buf, "0"); |
Jason Sams | d1f7da6 | 2012-03-15 19:18:03 -0700 | [diff] [blame] | 199 | return atoi(buf); |
Joe Onorato | 76371ff | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 200 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 201 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 202 | void Context::displayDebugStats() { |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 203 | #ifndef RS_COMPATIBILITY_LIB |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 204 | char buffer[128]; |
Alex Sakhartchouk | c8fb69e | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 205 | sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript); |
Alex Sakhartchouk | ca5a454 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 206 | float oldR, oldG, oldB, oldA; |
| 207 | mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA); |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 208 | uint32_t bufferLen = strlen(buffer); |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 209 | |
Alex Sakhartchouk | 1809bde | 2011-03-17 13:49:38 -0700 | [diff] [blame] | 210 | ObjectBaseRef<Font> lastFont(getFont()); |
| 211 | setFont(NULL); |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 212 | float shadowCol = 0.1f; |
Alex Sakhartchouk | ca5a454 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 213 | mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f); |
Alex Sakhartchouk | c8fb69e | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 214 | mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6); |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 215 | |
Alex Sakhartchouk | 09c6735 | 2010-10-05 11:33:27 -0700 | [diff] [blame] | 216 | mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f); |
Alex Sakhartchouk | c8fb69e | 2010-10-05 13:23:55 -0700 | [diff] [blame] | 217 | mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7); |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 218 | |
Alex Sakhartchouk | 1809bde | 2011-03-17 13:49:38 -0700 | [diff] [blame] | 219 | setFont(lastFont.get()); |
Alex Sakhartchouk | ca5a454 | 2010-08-05 11:24:14 -0700 | [diff] [blame] | 220 | mStateFont.setFontColor(oldR, oldG, oldB, oldA); |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 221 | #endif |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Tim Murray | 0c66f07 | 2012-10-23 14:05:02 -0700 | [diff] [blame] | 224 | bool Context::loadRuntime(const char* filename, Context* rsc) { |
Joe Onorato | 76371ff | 2009-09-23 16:37:36 -0700 | [diff] [blame] | 225 | |
Tim Murray | 0c66f07 | 2012-10-23 14:05:02 -0700 | [diff] [blame] | 226 | // TODO: store the driverSO somewhere so we can dlclose later |
Stephen Hines | 91dfcdb | 2012-09-07 18:23:35 -0700 | [diff] [blame] | 227 | void *driverSO = NULL; |
| 228 | |
Tim Murray | 0c66f07 | 2012-10-23 14:05:02 -0700 | [diff] [blame] | 229 | driverSO = dlopen(filename, RTLD_LAZY); |
Stephen Hines | 91dfcdb | 2012-09-07 18:23:35 -0700 | [diff] [blame] | 230 | if (driverSO == NULL) { |
Tim Murray | 0c66f07 | 2012-10-23 14:05:02 -0700 | [diff] [blame] | 231 | ALOGE("Failed loading RS driver: %s", dlerror()); |
| 232 | return false; |
Stephen Hines | 414a461 | 2012-09-05 18:05:08 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // Need to call dlerror() to clear buffer before using it for dlsym(). |
| 236 | (void) dlerror(); |
| 237 | typedef bool (*HalSig)(Context*, uint32_t, uint32_t); |
| 238 | HalSig halInit = (HalSig) dlsym(driverSO, "rsdHalInit"); |
| 239 | |
| 240 | // If we can't find the C variant, we go looking for the C++ version. |
| 241 | if (halInit == NULL) { |
| 242 | ALOGW("Falling back to find C++ rsdHalInit: %s", dlerror()); |
| 243 | halInit = (HalSig) dlsym(driverSO, |
| 244 | "_Z10rsdHalInitPN7android12renderscript7ContextEjj"); |
| 245 | } |
| 246 | |
| 247 | if (halInit == NULL) { |
Stephen Hines | 414a461 | 2012-09-05 18:05:08 -0700 | [diff] [blame] | 248 | dlclose(driverSO); |
| 249 | ALOGE("Failed to find rsdHalInit: %s", dlerror()); |
Tim Murray | 0c66f07 | 2012-10-23 14:05:02 -0700 | [diff] [blame] | 250 | return false; |
Stephen Hines | 414a461 | 2012-09-05 18:05:08 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | if (!(*halInit)(rsc, 0, 0)) { |
Stephen Hines | 414a461 | 2012-09-05 18:05:08 -0700 | [diff] [blame] | 254 | dlclose(driverSO); |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 255 | ALOGE("Hal init failed"); |
Tim Murray | 0c66f07 | 2012-10-23 14:05:02 -0700 | [diff] [blame] | 256 | return false; |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 257 | } |
Tim Murray | 0c66f07 | 2012-10-23 14:05:02 -0700 | [diff] [blame] | 258 | |
| 259 | //validate HAL struct |
| 260 | |
| 261 | |
| 262 | return true; |
| 263 | } |
| 264 | |
| 265 | void * Context::threadProc(void *vrsc) { |
| 266 | Context *rsc = static_cast<Context *>(vrsc); |
| 267 | #ifndef ANDROID_RS_SERIALIZE |
| 268 | rsc->mNativeThreadId = gettid(); |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 269 | #ifndef RS_COMPATIBILITY_LIB |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 270 | if (!rsc->isSynchronous()) { |
| 271 | setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY); |
| 272 | } |
Tim Murray | 0c66f07 | 2012-10-23 14:05:02 -0700 | [diff] [blame] | 273 | rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY; |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 274 | #else |
| 275 | if (!rsc->isSynchronous()) { |
| 276 | setpriority(PRIO_PROCESS, rsc->mNativeThreadId, -4); |
| 277 | } |
| 278 | rsc->mThreadPriority = -4; |
| 279 | #endif |
Tim Murray | 0c66f07 | 2012-10-23 14:05:02 -0700 | [diff] [blame] | 280 | #endif //ANDROID_RS_SERIALIZE |
| 281 | rsc->props.mLogTimes = getProp("debug.rs.profile") != 0; |
| 282 | rsc->props.mLogScripts = getProp("debug.rs.script") != 0; |
| 283 | rsc->props.mLogObjects = getProp("debug.rs.object") != 0; |
| 284 | rsc->props.mLogShaders = getProp("debug.rs.shader") != 0; |
| 285 | rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes") != 0; |
| 286 | rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms") != 0; |
| 287 | rsc->props.mLogVisual = getProp("debug.rs.visual") != 0; |
| 288 | rsc->props.mDebugMaxThreads = getProp("debug.rs.max-threads"); |
| 289 | |
| 290 | bool loadDefault = true; |
| 291 | |
| 292 | // Provide a mechanism for dropping in a different RS driver. |
| 293 | #ifdef OVERRIDE_RS_DRIVER |
| 294 | #define XSTR(S) #S |
| 295 | #define STR(S) XSTR(S) |
| 296 | #define OVERRIDE_RS_DRIVER_STRING STR(OVERRIDE_RS_DRIVER) |
| 297 | |
| 298 | if (getProp("debug.rs.default-CPU-driver") != 0) { |
| 299 | ALOGE("Skipping override driver and loading default CPU driver"); |
Tim Murray | 0e92fa3 | 2012-11-06 14:36:38 -0800 | [diff] [blame] | 300 | } else if (rsc->mForceCpu) { |
| 301 | ALOGV("Application requested CPU execution"); |
Tim Murray | 0c66f07 | 2012-10-23 14:05:02 -0700 | [diff] [blame] | 302 | } else { |
| 303 | if (loadRuntime(OVERRIDE_RS_DRIVER_STRING, rsc)) { |
| 304 | ALOGE("Successfully loaded runtime: %s", OVERRIDE_RS_DRIVER_STRING); |
| 305 | loadDefault = false; |
| 306 | } else { |
| 307 | ALOGE("Failed to load runtime %s, loading default", OVERRIDE_RS_DRIVER_STRING); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | #undef XSTR |
| 312 | #undef STR |
| 313 | #endif // OVERRIDE_RS_DRIVER |
| 314 | |
| 315 | if (loadDefault) { |
| 316 | if (!loadRuntime("libRSDriver.so", rsc)) { |
| 317 | ALOGE("Failed to load default runtime!"); |
Stephen Hines | 6f01bcf | 2012-11-19 15:18:16 -0800 | [diff] [blame] | 318 | rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed loading RS driver"); |
Tim Murray | 0c66f07 | 2012-10-23 14:05:02 -0700 | [diff] [blame] | 319 | return NULL; |
| 320 | } |
| 321 | } |
| 322 | |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 323 | rsc->mHal.funcs.setPriority(rsc, rsc->mThreadPriority); |
Jason Sams | e576910 | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 324 | |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 325 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 326 | if (rsc->mIsGraphicsContext) { |
Jason Sams | d3e7107 | 2011-05-03 15:01:58 -0700 | [diff] [blame] | 327 | if (!rsc->initGLThread()) { |
| 328 | rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL"); |
| 329 | return NULL; |
| 330 | } |
| 331 | |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 332 | rsc->mStateRaster.init(rsc); |
| 333 | rsc->setProgramRaster(NULL); |
| 334 | rsc->mStateVertex.init(rsc); |
| 335 | rsc->setProgramVertex(NULL); |
| 336 | rsc->mStateFragment.init(rsc); |
| 337 | rsc->setProgramFragment(NULL); |
| 338 | rsc->mStateFragmentStore.init(rsc); |
| 339 | rsc->setProgramStore(NULL); |
| 340 | rsc->mStateFont.init(rsc); |
| 341 | rsc->setFont(NULL); |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 342 | rsc->mStateSampler.init(rsc); |
Alex Sakhartchouk | da6d34a | 2011-05-13 14:53:34 -0700 | [diff] [blame] | 343 | rsc->mFBOCache.init(rsc); |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 344 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 345 | #endif |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 346 | |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 347 | rsc->mRunning = true; |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 348 | |
| 349 | if (rsc->isSynchronous()) { |
| 350 | return NULL; |
| 351 | } |
| 352 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 353 | if (!rsc->mIsGraphicsContext) { |
| 354 | while (!rsc->mExit) { |
Jason Sams | 963a2fb | 2012-02-09 14:36:14 -0800 | [diff] [blame] | 355 | rsc->mIO.playCoreCommands(rsc, -1); |
Jason Sams | e0aab4a | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 356 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 357 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 358 | } else { |
| 359 | #ifndef ANDROID_RS_SERIALIZE |
| 360 | DisplayEventReceiver displayEvent; |
| 361 | DisplayEventReceiver::Event eventBuffer[1]; |
| 362 | #endif |
| 363 | int vsyncRate = 0; |
| 364 | int targetRate = 0; |
Jason Sams | e0aab4a | 2011-08-12 15:05:15 -0700 | [diff] [blame] | 365 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 366 | bool drawOnce = false; |
| 367 | while (!rsc->mExit) { |
| 368 | rsc->timerSet(RS_TIMER_IDLE); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 369 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 370 | #ifndef ANDROID_RS_SERIALIZE |
Jason Sams | 9afd9a5 | 2012-02-17 16:59:50 -0800 | [diff] [blame] | 371 | if (!rsc->mRootScript.get() || !rsc->mHasSurface || rsc->mPaused) { |
| 372 | targetRate = 0; |
| 373 | } |
| 374 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 375 | if (vsyncRate != targetRate) { |
| 376 | displayEvent.setVsyncRate(targetRate); |
| 377 | vsyncRate = targetRate; |
| 378 | } |
| 379 | if (targetRate) { |
Jason Sams | 963a2fb | 2012-02-09 14:36:14 -0800 | [diff] [blame] | 380 | drawOnce |= rsc->mIO.playCoreCommands(rsc, displayEvent.getFd()); |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 381 | while (displayEvent.getEvents(eventBuffer, 1) != 0) { |
| 382 | //ALOGE("vs2 time past %lld", (rsc->getTime() - eventBuffer[0].header.timestamp) / 1000000); |
| 383 | } |
| 384 | } else |
| 385 | #endif |
| 386 | { |
Jason Sams | 963a2fb | 2012-02-09 14:36:14 -0800 | [diff] [blame] | 387 | drawOnce |= rsc->mIO.playCoreCommands(rsc, -1); |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 388 | } |
Alex Sakhartchouk | 0cae59f | 2010-08-03 12:03:16 -0700 | [diff] [blame] | 389 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 390 | if ((rsc->mRootScript.get() != NULL) && rsc->mHasSurface && |
| 391 | (targetRate || drawOnce) && !rsc->mPaused) { |
| 392 | |
| 393 | drawOnce = false; |
| 394 | targetRate = ((rsc->runRootScript() + 15) / 16); |
| 395 | |
| 396 | if (rsc->props.mLogVisual) { |
| 397 | rsc->displayDebugStats(); |
| 398 | } |
| 399 | |
| 400 | rsc->timerSet(RS_TIMER_CLEAR_SWAP); |
| 401 | rsc->mHal.funcs.swap(rsc); |
| 402 | rsc->timerFrame(); |
| 403 | rsc->timerSet(RS_TIMER_INTERNAL); |
| 404 | rsc->timerPrint(); |
| 405 | rsc->timerReset(); |
| 406 | } |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 407 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 408 | #endif |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 409 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 410 | |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 411 | ALOGV("%p RS Thread exiting", rsc); |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 412 | |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 413 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 414 | if (rsc->mIsGraphicsContext) { |
| 415 | pthread_mutex_lock(&gInitMutex); |
| 416 | rsc->deinitEGL(); |
| 417 | pthread_mutex_unlock(&gInitMutex); |
| 418 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 419 | #endif |
Jason Sams | 33b6e3b | 2009-10-27 14:44:31 -0700 | [diff] [blame] | 420 | |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 421 | ALOGV("%p RS Thread exited", rsc); |
Jason Sams | 83c451a | 2011-04-21 11:46:50 -0700 | [diff] [blame] | 422 | return NULL; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 425 | void Context::destroyWorkerThreadResources() { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 426 | //ALOGV("destroyWorkerThreadResources 1"); |
Jason Sams | 2e8665d | 2011-01-27 00:14:13 -0800 | [diff] [blame] | 427 | ObjectBase::zeroAllUserRef(this); |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 428 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 429 | if (mIsGraphicsContext) { |
| 430 | mRaster.clear(); |
| 431 | mFragment.clear(); |
| 432 | mVertex.clear(); |
| 433 | mFragmentStore.clear(); |
| 434 | mFont.clear(); |
| 435 | mRootScript.clear(); |
| 436 | mStateRaster.deinit(this); |
| 437 | mStateVertex.deinit(this); |
| 438 | mStateFragment.deinit(this); |
| 439 | mStateFragmentStore.deinit(this); |
| 440 | mStateFont.deinit(this); |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 441 | mStateSampler.deinit(this); |
Alex Sakhartchouk | da6d34a | 2011-05-13 14:53:34 -0700 | [diff] [blame] | 442 | mFBOCache.deinit(this); |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 443 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 444 | #endif |
Jason Sams | c7cec1e | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 445 | ObjectBase::freeAllChildren(this); |
Jason Sams | cf912de | 2011-01-09 16:09:51 -0800 | [diff] [blame] | 446 | mExit = true; |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 447 | //ALOGV("destroyWorkerThreadResources 2"); |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 450 | void Context::printWatchdogInfo(void *ctx) { |
| 451 | Context *rsc = (Context *)ctx; |
Jason Sams | ee80344 | 2011-10-13 16:05:27 -0700 | [diff] [blame] | 452 | if (rsc->watchdog.command && rsc->watchdog.file) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 453 | ALOGE("RS watchdog timeout: %i %s line %i %s", rsc->watchdog.inRoot, |
Jason Sams | ee80344 | 2011-10-13 16:05:27 -0700 | [diff] [blame] | 454 | rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file); |
| 455 | } else { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 456 | ALOGE("RS watchdog timeout: %i", rsc->watchdog.inRoot); |
Jason Sams | ee80344 | 2011-10-13 16:05:27 -0700 | [diff] [blame] | 457 | } |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 461 | void Context::setPriority(int32_t p) { |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 462 | // Note: If we put this in the proper "background" policy |
| 463 | // the wallpapers can become completly unresponsive at times. |
| 464 | // This is probably not what we want for something the user is actively |
| 465 | // looking at. |
Jason Sams | 2dca84d | 2009-12-09 11:05:45 -0800 | [diff] [blame] | 466 | mThreadPriority = p; |
Jason Sams | 7bf29dd | 2010-07-19 15:38:19 -0700 | [diff] [blame] | 467 | setpriority(PRIO_PROCESS, mNativeThreadId, p); |
Jason Sams | 9719bd4 | 2012-01-12 14:22:21 -0800 | [diff] [blame] | 468 | mHal.funcs.setPriority(this, mThreadPriority); |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 469 | } |
| 470 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 471 | Context::Context() { |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 472 | mDev = NULL; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 473 | mRunning = false; |
| 474 | mExit = false; |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 475 | mPaused = false; |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 476 | mObjHead = NULL; |
Jason Sams | a2cf755 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 477 | mError = RS_ERROR_NONE; |
Stephen Hines | cbb0b8a | 2011-08-01 15:02:34 -0700 | [diff] [blame] | 478 | mTargetSdkVersion = 14; |
Alex Sakhartchouk | 7b3e9bd | 2011-03-16 19:28:25 -0700 | [diff] [blame] | 479 | mDPI = 96; |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 480 | mIsContextLite = false; |
Stephen Hines | 86c6b5f | 2011-10-31 14:07:54 -0700 | [diff] [blame] | 481 | memset(&watchdog, 0, sizeof(watchdog)); |
Tim Murray | 0e92fa3 | 2012-11-06 14:36:38 -0800 | [diff] [blame] | 482 | mForceCpu = false; |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 483 | mSynchronous = false; |
Tim Murray | 0e92fa3 | 2012-11-06 14:36:38 -0800 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc, |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 487 | bool forceCpu, bool synchronous) { |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 488 | Context * rsc = new Context(); |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 489 | |
Tim Murray | 0e92fa3 | 2012-11-06 14:36:38 -0800 | [diff] [blame] | 490 | rsc->mForceCpu = forceCpu; |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 491 | rsc->mSynchronous = synchronous; |
Tim Murray | 0e92fa3 | 2012-11-06 14:36:38 -0800 | [diff] [blame] | 492 | |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 493 | if (!rsc->initContext(dev, sc)) { |
| 494 | delete rsc; |
| 495 | return NULL; |
| 496 | } |
| 497 | return rsc; |
| 498 | } |
| 499 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 500 | Context * Context::createContextLite() { |
| 501 | Context * rsc = new Context(); |
| 502 | rsc->mIsContextLite = true; |
| 503 | return rsc; |
| 504 | } |
| 505 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 506 | bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) { |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 507 | pthread_mutex_lock(&gInitMutex); |
| 508 | |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 509 | mIO.init(); |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 510 | mIO.setTimeoutCallback(printWatchdogInfo, this, 2e9); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 511 | |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 512 | dev->addContext(this); |
| 513 | mDev = dev; |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 514 | if (sc) { |
| 515 | mUserSurfaceConfig = *sc; |
| 516 | } else { |
| 517 | memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig)); |
| 518 | } |
Jason Sams | a2cf755 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 519 | |
Jason Sams | 6b8552a | 2010-10-13 15:31:10 -0700 | [diff] [blame] | 520 | mIsGraphicsContext = sc != NULL; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 521 | |
Jason Sams | a658e90 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 522 | int status; |
| 523 | pthread_attr_t threadAttr; |
| 524 | |
Jason Sams | fb03a22 | 2009-10-15 16:47:31 -0700 | [diff] [blame] | 525 | pthread_mutex_unlock(&gInitMutex); |
| 526 | |
| 527 | // Global init done at this point. |
Jason Sams | e576910 | 2009-06-19 16:03:18 -0700 | [diff] [blame] | 528 | |
Jason Sams | a658e90 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 529 | status = pthread_attr_init(&threadAttr); |
| 530 | if (status) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 531 | ALOGE("Failed to init thread attribute."); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 532 | return false; |
Jason Sams | a658e90 | 2009-06-04 14:35:01 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Alex Sakhartchouk | 7257c7e | 2011-05-17 12:32:47 -0700 | [diff] [blame] | 535 | mHasSurface = false; |
Jason Sams | 992a0b7 | 2009-06-23 12:22:47 -0700 | [diff] [blame] | 536 | |
Jason Sams | 24371d9 | 2009-08-19 12:17:14 -0700 | [diff] [blame] | 537 | timerInit(); |
Jason Sams | a891933 | 2009-09-24 15:42:52 -0700 | [diff] [blame] | 538 | timerSet(RS_TIMER_INTERNAL); |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 539 | if (mSynchronous) { |
| 540 | threadProc(this); |
| 541 | } else { |
| 542 | status = pthread_create(&mThreadId, &threadAttr, threadProc, this); |
| 543 | if (status) { |
| 544 | ALOGE("Failed to start rs context thread."); |
| 545 | return false; |
| 546 | } |
| 547 | while (!mRunning && (mError == RS_ERROR_NONE)) { |
| 548 | usleep(100); |
| 549 | } |
Jason Sams | 5086938 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 550 | |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 551 | if (mError != RS_ERROR_NONE) { |
| 552 | ALOGE("Errors during thread init"); |
| 553 | return false; |
| 554 | } |
Jason Sams | 1813340 | 2010-07-20 15:09:00 -0700 | [diff] [blame] | 555 | |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 556 | pthread_attr_destroy(&threadAttr); |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 557 | } |
Jason Sams | 5c1c79a | 2010-11-03 14:27:11 -0700 | [diff] [blame] | 558 | return true; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 561 | Context::~Context() { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 562 | ALOGV("%p Context::~Context", this); |
Jason Sams | cf912de | 2011-01-09 16:09:51 -0800 | [diff] [blame] | 563 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 564 | if (!mIsContextLite) { |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 565 | mPaused = false; |
| 566 | void *res; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 567 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 568 | mIO.shutdown(); |
| 569 | int status = pthread_join(mThreadId, &res); |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 570 | rsAssert(mExit); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 571 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 572 | if (mHal.funcs.shutdownDriver) { |
| 573 | mHal.funcs.shutdownDriver(this); |
| 574 | } |
| 575 | |
| 576 | // Global structure cleanup. |
| 577 | pthread_mutex_lock(&gInitMutex); |
| 578 | if (mDev) { |
| 579 | mDev->removeContext(this); |
| 580 | mDev = NULL; |
| 581 | } |
| 582 | pthread_mutex_unlock(&gInitMutex); |
Jason Sams | 51462c5 | 2011-01-25 00:26:25 -0800 | [diff] [blame] | 583 | } |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 584 | ALOGV("%p Context::~Context done", this); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 587 | #ifndef RS_COMPATIBILITY_LIB |
Alex Sakhartchouk | 7257c7e | 2011-05-17 12:32:47 -0700 | [diff] [blame] | 588 | void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 589 | rsAssert(mIsGraphicsContext); |
Jason Sams | 4b3de47 | 2011-04-06 17:52:23 -0700 | [diff] [blame] | 590 | mHal.funcs.setSurface(this, w, h, sur); |
Jason Sams | 458f2dc | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 591 | |
Alex Sakhartchouk | 7257c7e | 2011-05-17 12:32:47 -0700 | [diff] [blame] | 592 | mHasSurface = sur != NULL; |
Jason Sams | 4b3de47 | 2011-04-06 17:52:23 -0700 | [diff] [blame] | 593 | mWidth = w; |
| 594 | mHeight = h; |
Jason Sams | 613cad1 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 595 | |
Jason Sams | 4b3de47 | 2011-04-06 17:52:23 -0700 | [diff] [blame] | 596 | if (mWidth && mHeight) { |
Jason Sams | 771565f | 2010-05-14 15:30:29 -0700 | [diff] [blame] | 597 | mStateVertex.updateSize(this); |
Alex Sakhartchouk | a544b63 | 2011-07-19 17:50:29 -0700 | [diff] [blame] | 598 | mFBOCache.updateSize(); |
Jason Sams | 458f2dc | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
Alex Sakhartchouk | a74a8f6 | 2011-11-16 12:22:10 -0800 | [diff] [blame] | 602 | uint32_t Context::getCurrentSurfaceWidth() const { |
| 603 | for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) { |
| 604 | if (mFBOCache.mHal.state.colorTargets[i] != NULL) { |
| 605 | return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimX(); |
| 606 | } |
| 607 | } |
| 608 | if (mFBOCache.mHal.state.depthTarget != NULL) { |
| 609 | return mFBOCache.mHal.state.depthTarget->getType()->getDimX(); |
| 610 | } |
| 611 | return mWidth; |
| 612 | } |
| 613 | |
| 614 | uint32_t Context::getCurrentSurfaceHeight() const { |
| 615 | for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) { |
| 616 | if (mFBOCache.mHal.state.colorTargets[i] != NULL) { |
| 617 | return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimY(); |
| 618 | } |
| 619 | } |
| 620 | if (mFBOCache.mHal.state.depthTarget != NULL) { |
| 621 | return mFBOCache.mHal.state.depthTarget->getType()->getDimY(); |
| 622 | } |
| 623 | return mHeight; |
| 624 | } |
| 625 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 626 | void Context::pause() { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 627 | rsAssert(mIsGraphicsContext); |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 628 | mPaused = true; |
| 629 | } |
| 630 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 631 | void Context::resume() { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 632 | rsAssert(mIsGraphicsContext); |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 633 | mPaused = false; |
| 634 | } |
| 635 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 636 | void Context::setRootScript(Script *s) { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 637 | rsAssert(mIsGraphicsContext); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 638 | mRootScript.set(s); |
| 639 | } |
| 640 | |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 641 | void Context::setProgramStore(ProgramStore *pfs) { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 642 | rsAssert(mIsGraphicsContext); |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 643 | if (pfs == NULL) { |
| 644 | mFragmentStore.set(mStateFragmentStore.mDefault); |
| 645 | } else { |
| 646 | mFragmentStore.set(pfs); |
| 647 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 650 | void Context::setProgramFragment(ProgramFragment *pf) { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 651 | rsAssert(mIsGraphicsContext); |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 652 | if (pf == NULL) { |
| 653 | mFragment.set(mStateFragment.mDefault); |
| 654 | } else { |
| 655 | mFragment.set(pf); |
| 656 | } |
Jason Sams | cfb1d11 | 2009-08-05 13:57:03 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 659 | void Context::setProgramRaster(ProgramRaster *pr) { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 660 | rsAssert(mIsGraphicsContext); |
Jason Sams | 5fd09d8 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 661 | if (pr == NULL) { |
| 662 | mRaster.set(mStateRaster.mDefault); |
| 663 | } else { |
| 664 | mRaster.set(pr); |
| 665 | } |
| 666 | } |
| 667 | |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 668 | void Context::setProgramVertex(ProgramVertex *pv) { |
Jason Sams | 4820e8b | 2010-02-09 16:05:07 -0800 | [diff] [blame] | 669 | rsAssert(mIsGraphicsContext); |
Jason Sams | 8ce125b | 2009-06-17 16:52:59 -0700 | [diff] [blame] | 670 | if (pv == NULL) { |
| 671 | mVertex.set(mStateVertex.mDefault); |
| 672 | } else { |
| 673 | mVertex.set(pv); |
| 674 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 677 | void Context::setFont(Font *f) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 678 | rsAssert(mIsGraphicsContext); |
| 679 | if (f == NULL) { |
| 680 | mFont.set(mStateFont.mDefault); |
| 681 | } else { |
| 682 | mFont.set(f); |
| 683 | } |
| 684 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 685 | #endif |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 686 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 687 | void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) { |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 688 | rsAssert(!obj->getName()); |
Jason Sams | a4a54e4 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 689 | obj->setName(name, len); |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 690 | mNames.add(obj); |
| 691 | } |
| 692 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 693 | void Context::removeName(ObjectBase *obj) { |
| 694 | for (size_t ct=0; ct < mNames.size(); ct++) { |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 695 | if (obj == mNames[ct]) { |
| 696 | mNames.removeAt(ct); |
| 697 | return; |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 702 | RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) { |
| 703 | return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID); |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 704 | } |
| 705 | |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 706 | RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) { |
| 707 | return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen); |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Jason Sams | 87319de | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 710 | bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID, |
| 711 | uint32_t subID, size_t len, bool waitForSpace) const { |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 712 | |
| 713 | return mIO.sendToClient(cmdID, subID, data, len, waitForSpace); |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 716 | void Context::initToClient() { |
| 717 | while (!mRunning) { |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 718 | usleep(100); |
| 719 | } |
| 720 | } |
| 721 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 722 | void Context::deinitToClient() { |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 723 | mIO.clientShutdown(); |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 724 | } |
Jason Sams | 5086938 | 2009-08-18 17:07:09 -0700 | [diff] [blame] | 725 | |
Jason Sams | 87319de | 2010-11-22 16:20:16 -0800 | [diff] [blame] | 726 | void Context::setError(RsError e, const char *msg) const { |
Jason Sams | a2cf755 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 727 | mError = e; |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 728 | sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true); |
Jason Sams | a2cf755 | 2010-03-03 13:03:18 -0800 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 732 | void Context::dumpDebug() const { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 733 | ALOGE("RS Context debug %p", this); |
| 734 | ALOGE("RS Context debug"); |
Jason Sams | 13e2634 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 735 | |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 736 | ALOGE(" RS width %i, height %i", mWidth, mHeight); |
| 737 | ALOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused); |
| 738 | ALOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId); |
Jason Sams | 13e2634 | 2009-11-24 12:26:35 -0800 | [diff] [blame] | 739 | } |
Jason Sams | a4a54e4 | 2009-06-10 18:39:40 -0700 | [diff] [blame] | 740 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 741 | /////////////////////////////////////////////////////////////////////////////////////////// |
Jason Sams | a44cb29 | 2009-06-04 17:58:03 -0700 | [diff] [blame] | 742 | // |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 743 | |
| 744 | namespace android { |
| 745 | namespace renderscript { |
| 746 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 747 | void rsi_ContextFinish(Context *rsc) { |
Jason Sams | 8c88090 | 2010-06-15 12:15:57 -0700 | [diff] [blame] | 748 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 749 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 750 | void rsi_ContextBindRootScript(Context *rsc, RsScript vs) { |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 751 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 752 | Script *s = static_cast<Script *>(vs); |
| 753 | rsc->setRootScript(s); |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 754 | #endif |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 755 | } |
| 756 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 757 | void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 758 | Sampler *s = static_cast<Sampler *>(vs); |
| 759 | |
| 760 | if (slot > RS_MAX_SAMPLER_SLOT) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 761 | ALOGE("Invalid sampler slot"); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 762 | return; |
| 763 | } |
| 764 | |
| 765 | s->bindToContext(&rsc->mStateSampler, slot); |
| 766 | } |
| 767 | |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 768 | #ifndef RS_COMPATIBILITY_LIB |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 769 | void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) { |
Jason Sams | ccc010b | 2010-05-13 18:30:11 -0700 | [diff] [blame] | 770 | ProgramStore *pfs = static_cast<ProgramStore *>(vpfs); |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 771 | rsc->setProgramStore(pfs); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 772 | } |
| 773 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 774 | void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 775 | ProgramFragment *pf = static_cast<ProgramFragment *>(vpf); |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 776 | rsc->setProgramFragment(pf); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 779 | void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) { |
Jason Sams | 5fd09d8 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 780 | ProgramRaster *pr = static_cast<ProgramRaster *>(vpr); |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 781 | rsc->setProgramRaster(pr); |
Jason Sams | 5fd09d8 | 2009-09-23 13:57:02 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 784 | void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 785 | ProgramVertex *pv = static_cast<ProgramVertex *>(vpv); |
Jason Sams | 6070925 | 2010-11-17 15:29:32 -0800 | [diff] [blame] | 786 | rsc->setProgramVertex(pv); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 789 | void rsi_ContextBindFont(Context *rsc, RsFont vfont) { |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 790 | Font *font = static_cast<Font *>(vfont); |
| 791 | rsc->setFont(font); |
| 792 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 793 | #endif |
Alex Sakhartchouk | d3e0ad4 | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 794 | |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 795 | void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) { |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 796 | ObjectBase *ob = static_cast<ObjectBase *>(obj); |
Alex Sakhartchouk | 70b83c1 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 797 | rsc->assignName(ob, name, name_length); |
Jason Sams | a0a1b6f | 2009-06-10 15:04:38 -0700 | [diff] [blame] | 798 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 799 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 800 | void rsi_ObjDestroy(Context *rsc, void *optr) { |
Jason Sams | 2353ae3 | 2010-10-14 17:48:46 -0700 | [diff] [blame] | 801 | ObjectBase *ob = static_cast<ObjectBase *>(optr); |
Jason Sams | 707aaf3 | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 802 | rsc->removeName(ob); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 803 | ob->decUserRef(); |
Jason Sams | 707aaf3 | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 806 | #ifndef RS_COMPATIBILITY_LIB |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 807 | void rsi_ContextPause(Context *rsc) { |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 808 | rsc->pause(); |
| 809 | } |
| 810 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 811 | void rsi_ContextResume(Context *rsc) { |
Jason Sams | 86f1b23 | 2009-09-24 17:38:20 -0700 | [diff] [blame] | 812 | rsc->resume(); |
| 813 | } |
| 814 | |
Alex Sakhartchouk | 7257c7e | 2011-05-17 12:32:47 -0700 | [diff] [blame] | 815 | void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) { |
Mathias Agopian | fa40286 | 2010-02-12 14:04:35 -0800 | [diff] [blame] | 816 | rsc->setSurface(w, h, sur); |
Jason Sams | 613cad1 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 817 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 818 | #endif |
Jason Sams | 613cad1 | 2009-11-12 15:10:25 -0800 | [diff] [blame] | 819 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 820 | void rsi_ContextSetPriority(Context *rsc, int32_t p) { |
Jason Sams | 1583244 | 2009-11-15 12:14:26 -0800 | [diff] [blame] | 821 | rsc->setPriority(p); |
Jason Sams | 458f2dc | 2009-11-03 13:58:36 -0800 | [diff] [blame] | 822 | } |
| 823 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 824 | void rsi_ContextDump(Context *rsc, int32_t bits) { |
Jason Sams | c21cf40 | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 825 | ObjectBase::dumpAll(rsc); |
| 826 | } |
| 827 | |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 828 | void rsi_ContextDestroyWorker(Context *rsc) { |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 829 | rsc->destroyWorkerThreadResources(); |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 830 | } |
| 831 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 832 | void rsi_ContextDestroy(Context *rsc) { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 833 | ALOGV("%p rsContextDestroy", rsc); |
Jason Sams | 741aac9 | 2010-12-24 14:38:39 -0800 | [diff] [blame] | 834 | rsContextDestroyWorker(rsc); |
Jason Sams | 1dcefab | 2010-12-09 12:19:46 -0800 | [diff] [blame] | 835 | delete rsc; |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 836 | ALOGV("%p rsContextDestroy done", rsc); |
Jason Sams | 1dcefab | 2010-12-09 12:19:46 -0800 | [diff] [blame] | 837 | } |
| 838 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 839 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 840 | RsMessageToClientType rsi_ContextPeekMessage(Context *rsc, |
Jason Sams | 186e591 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 841 | size_t * receiveLen, size_t receiveLen_length, |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 842 | uint32_t * subID, size_t subID_length) { |
| 843 | return rsc->peekMessageToClient(receiveLen, subID); |
Jason Sams | aad4bc5 | 2010-11-08 17:06:46 -0800 | [diff] [blame] | 844 | } |
| 845 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 846 | RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length, |
Jason Sams | 186e591 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 847 | size_t * receiveLen, size_t receiveLen_length, |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 848 | uint32_t * subID, size_t subID_length) { |
Jason Sams | 186e591 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 849 | rsAssert(subID_length == sizeof(uint32_t)); |
| 850 | rsAssert(receiveLen_length == sizeof(size_t)); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 851 | return rsc->getMessageToClient(data, receiveLen, subID, data_length); |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 854 | void rsi_ContextInitToClient(Context *rsc) { |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 855 | rsc->initToClient(); |
| 856 | } |
| 857 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 858 | void rsi_ContextDeinitToClient(Context *rsc) { |
Jason Sams | 8c401ef | 2009-10-06 13:58:47 -0700 | [diff] [blame] | 859 | rsc->deinitToClient(); |
| 860 | } |
| 861 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | |
Stephen Hines | cbb0b8a | 2011-08-01 15:02:34 -0700 | [diff] [blame] | 865 | RsContext rsContextCreate(RsDevice vdev, uint32_t version, |
| 866 | uint32_t sdkVersion) { |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 867 | return rsContextCreate(vdev, version, sdkVersion, false, false); |
Tim Murray | 0e92fa3 | 2012-11-06 14:36:38 -0800 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | RsContext rsContextCreate(RsDevice vdev, uint32_t version, |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 871 | uint32_t sdkVersion, bool forceCpu, |
| 872 | bool synchronous) { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 873 | ALOGV("rsContextCreate dev=%p", vdev); |
Jason Sams | 789ca83 | 2011-05-18 17:36:02 -0700 | [diff] [blame] | 874 | Device * dev = static_cast<Device *>(vdev); |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 875 | Context *rsc = Context::createContext(dev, NULL, forceCpu, synchronous); |
Stephen Hines | cbb0b8a | 2011-08-01 15:02:34 -0700 | [diff] [blame] | 876 | if (rsc) { |
| 877 | rsc->setTargetSdkVersion(sdkVersion); |
| 878 | } |
Jason Sams | 789ca83 | 2011-05-18 17:36:02 -0700 | [diff] [blame] | 879 | return rsc; |
| 880 | } |
| 881 | |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 882 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | 789ca83 | 2011-05-18 17:36:02 -0700 | [diff] [blame] | 883 | RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, |
Stephen Hines | cbb0b8a | 2011-08-01 15:02:34 -0700 | [diff] [blame] | 884 | uint32_t sdkVersion, RsSurfaceConfig sc, |
| 885 | uint32_t dpi) { |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 886 | ALOGV("rsContextCreateGL dev=%p", vdev); |
Jason Sams | 789ca83 | 2011-05-18 17:36:02 -0700 | [diff] [blame] | 887 | Device * dev = static_cast<Device *>(vdev); |
| 888 | Context *rsc = Context::createContext(dev, &sc); |
Jason Sams | 9544f76 | 2011-07-13 16:09:42 -0700 | [diff] [blame] | 889 | if (rsc) { |
Stephen Hines | cbb0b8a | 2011-08-01 15:02:34 -0700 | [diff] [blame] | 890 | rsc->setTargetSdkVersion(sdkVersion); |
Jason Sams | 9544f76 | 2011-07-13 16:09:42 -0700 | [diff] [blame] | 891 | rsc->setDPI(dpi); |
| 892 | } |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 893 | ALOGV("%p rsContextCreateGL ret", rsc); |
Jason Sams | 789ca83 | 2011-05-18 17:36:02 -0700 | [diff] [blame] | 894 | return rsc; |
| 895 | } |
Jason Sams | 93eacc7 | 2012-12-18 14:26:57 -0800 | [diff] [blame^] | 896 | #endif |
Jason Sams | 789ca83 | 2011-05-18 17:36:02 -0700 | [diff] [blame] | 897 | |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 898 | // Only to be called at a3d load time, before object is visible to user |
| 899 | // not thread safe |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 900 | void rsaGetName(RsContext con, void * obj, const char **name) { |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 901 | ObjectBase *ob = static_cast<ObjectBase *>(obj); |
| 902 | (*name) = ob->getName(); |
| 903 | } |