blob: 75f927e057b1c8bd28bb200214fcea845010dbfe [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -07002 * Copyright (C) 2011 The Android Open Source Project
Jason Sams326e0dd2009-05-22 14:03:28 -07003 *
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 Sakhartchouke23d2392012-03-09 09:24:39 -080017#include "rs.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070018#include "rsDevice.h"
19#include "rsContext.h"
20#include "rsThreadIO.h"
Jason Sams93eacc72012-12-18 14:26:57 -080021
Tim Murray397b0b12014-01-07 15:35:08 -080022#include "rsgApiStructs.h"
23
Jason Sams93eacc72012-12-18 14:26:57 -080024#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -080025#include "rsMesh.h"
Jason Sams5f27d6f2012-02-07 15:32:08 -080026#include <gui/DisplayEventReceiver.h>
Jason Sams93eacc72012-12-18 14:26:57 -080027#endif
Jason Sams326e0dd2009-05-22 14:03:28 -070028
Jason Sams15832442009-11-15 12:14:26 -080029#include <sys/types.h>
30#include <sys/resource.h>
Jason Sams7bf29dd2010-07-19 15:38:19 -070031#include <sched.h>
Jason Sams15832442009-11-15 12:14:26 -080032
Jason Sams8d957fa2010-09-28 14:41:22 -070033#include <sys/syscall.h>
Jason Sams93eacc72012-12-18 14:26:57 -080034#include <string.h>
Stephen Hines414a4612012-09-05 18:05:08 -070035#include <dlfcn.h>
Ian Rogersf8852d02014-01-29 15:35:17 -080036#include <inttypes.h>
Stephen Hinesb0934b62013-07-03 17:27:38 -070037#include <unistd.h>
Stephen Hines414a4612012-09-05 18:05:08 -070038
Stephen Hines16647802013-08-15 16:28:01 -070039#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB) && \
40 defined(HAVE_ANDROID_OS)
Tim Murray0b575de2013-03-15 15:56:43 -070041#include <cutils/properties.h>
42#endif
43
Stephen Hines6dfe6842013-08-14 17:56:38 -070044#ifdef RS_COMPATIBILITY_LIB
45#include "rsCompatibilityLib.h"
46#endif
47
Tim Murray0b575de2013-03-15 15:56:43 -070048#ifdef RS_SERVER
49// Android exposes gettid(), standard Linux does not
50static pid_t gettid() {
51 return syscall(SYS_gettid);
52}
53#endif
54
Jason Sams326e0dd2009-05-22 14:03:28 -070055using namespace android;
56using namespace android::renderscript;
57
Jason Samsfb03a222009-10-15 16:47:31 -070058pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams4961cce2013-04-11 16:11:46 -070059pthread_mutex_t Context::gMessageMutex = PTHREAD_MUTEX_INITIALIZER;
Stephen Hinesca3f09c2011-01-07 15:11:30 -080060pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070061
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080062bool Context::initGLThread() {
Jason Sams6b8552a2010-10-13 15:31:10 -070063 pthread_mutex_lock(&gInitMutex);
Jason Sams6b8552a2010-10-13 15:31:10 -070064
Jason Sams4b3de472011-04-06 17:52:23 -070065 if (!mHal.funcs.initGraphics(this)) {
Jason Sams5c1c79a2010-11-03 14:27:11 -070066 pthread_mutex_unlock(&gInitMutex);
Steve Blockaf12ac62012-01-06 19:20:56 +000067 ALOGE("%p initGraphics failed", this);
Jason Sams5c1c79a2010-11-03 14:27:11 -070068 return false;
Jason Sams6b8552a2010-10-13 15:31:10 -070069 }
70
Jason Sams6b8552a2010-10-13 15:31:10 -070071 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -070072 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -070073}
74
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080075void Context::deinitEGL() {
Jason Sams93eacc72012-12-18 14:26:57 -080076#ifndef RS_COMPATIBILITY_LIB
Jason Sams4b3de472011-04-06 17:52:23 -070077 mHal.funcs.shutdownGraphics(this);
Jason Sams93eacc72012-12-18 14:26:57 -080078#endif
Jason Sams33b6e3b2009-10-27 14:44:31 -070079}
80
Jason Sams60709252010-11-17 15:29:32 -080081Context::PushState::PushState(Context *con) {
82 mRsc = con;
Jason Sams93eacc72012-12-18 14:26:57 -080083#ifndef RS_COMPATIBILITY_LIB
Jason Samsc946b612011-02-23 14:47:17 -080084 if (con->mIsGraphicsContext) {
85 mFragment.set(con->getProgramFragment());
86 mVertex.set(con->getProgramVertex());
87 mStore.set(con->getProgramStore());
88 mRaster.set(con->getProgramRaster());
89 mFont.set(con->getFont());
90 }
Jason Sams93eacc72012-12-18 14:26:57 -080091#endif
Jason Sams60709252010-11-17 15:29:32 -080092}
93
94Context::PushState::~PushState() {
Jason Sams93eacc72012-12-18 14:26:57 -080095#ifndef RS_COMPATIBILITY_LIB
Jason Samsc946b612011-02-23 14:47:17 -080096 if (mRsc->mIsGraphicsContext) {
97 mRsc->setProgramFragment(mFragment.get());
98 mRsc->setProgramVertex(mVertex.get());
99 mRsc->setProgramStore(mStore.get());
100 mRsc->setProgramRaster(mRaster.get());
101 mRsc->setFont(mFont.get());
102 }
Jason Sams93eacc72012-12-18 14:26:57 -0800103#endif
Jason Sams60709252010-11-17 15:29:32 -0800104}
105
Jason Sams33b6e3b2009-10-27 14:44:31 -0700106
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800107uint32_t Context::runScript(Script *s) {
Shih-wei Liaoda3b58d2012-08-03 04:24:33 -0700108 PushState ps(this);
Jason Sams10308932009-06-09 12:15:30 -0700109
Jason Samsc61346b2010-05-28 18:23:22 -0700110 uint32_t ret = s->run(this);
Jason Samsc9d43db2009-07-28 12:02:16 -0700111 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700112}
113
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800114uint32_t Context::runRootScript() {
Jason Sams2dca84d2009-12-09 11:05:45 -0800115 timerSet(RS_TIMER_SCRIPT);
Jason Sams93eacc72012-12-18 14:26:57 -0800116#ifndef RS_COMPATIBILITY_LIB
Jason Sams8c401ef2009-10-06 13:58:47 -0700117 mStateFragmentStore.mLast.clear();
Jason Sams93eacc72012-12-18 14:26:57 -0800118#endif
Jason Sams2382aba2011-09-13 15:41:01 -0700119 watchdog.inRoot = true;
Jason Samsc61346b2010-05-28 18:23:22 -0700120 uint32_t ret = runScript(mRootScript.get());
Jason Sams2382aba2011-09-13 15:41:01 -0700121 watchdog.inRoot = false;
Jason Sams8cfdd242009-10-14 15:43:53 -0700122
Jason Samscfb1d112009-08-05 13:57:03 -0700123 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700124}
125
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800126uint64_t Context::getTime() const {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700127#ifndef ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -0700128 struct timespec t;
129 clock_gettime(CLOCK_MONOTONIC, &t);
130 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700131#else
132 return 0;
133#endif //ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -0700134}
135
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800136void Context::timerReset() {
Jason Sams24371d92009-08-19 12:17:14 -0700137 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
138 mTimers[ct] = 0;
139 }
140}
141
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800142void Context::timerInit() {
Jason Sams24371d92009-08-19 12:17:14 -0700143 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700144 mTimeFrame = mTimeLast;
145 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700146 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700147 mAverageFPSFrameCount = 0;
148 mAverageFPSStartTime = mTimeLast;
149 mAverageFPS = 0;
Jason Sams24371d92009-08-19 12:17:14 -0700150 timerReset();
151}
152
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800153void Context::timerFrame() {
Jason Sams1d54f102009-09-03 15:43:13 -0700154 mTimeLastFrame = mTimeFrame;
155 mTimeFrame = getTime();
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700156 // Update average fps
157 const uint64_t averageFramerateInterval = 1000 * 1000000;
158 mAverageFPSFrameCount ++;
159 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800160 if (inverval >= averageFramerateInterval) {
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700161 inverval = inverval / 1000000;
162 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
163 mAverageFPSFrameCount = 0;
164 mAverageFPSStartTime = mTimeFrame;
165 }
Jason Sams1d54f102009-09-03 15:43:13 -0700166}
167
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800168void Context::timerSet(Timers tm) {
Jason Sams24371d92009-08-19 12:17:14 -0700169 uint64_t last = mTimeLast;
170 mTimeLast = getTime();
171 mTimers[mTimerActive] += mTimeLast - last;
172 mTimerActive = tm;
173}
174
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800175void Context::timerPrint() {
Jason Sams24371d92009-08-19 12:17:14 -0700176 double total = 0;
177 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
178 total += mTimers[ct];
179 }
Jason Sams1d54f102009-09-03 15:43:13 -0700180 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800181 mTimeMSLastFrame = frame / 1000000;
182 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
183 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700184
Jason Sams2dca84d2009-12-09 11:05:45 -0800185
186 if (props.mLogTimes) {
Ian Rogersf8852d02014-01-29 15:35:17 -0800187 ALOGV("RS: Frame (%i), Script %2.1f%% (%i), Swap %2.1f%% (%i), Idle %2.1f%% (%" PRIi64 "), "
188 "Internal %2.1f%% (%" PRIi64 "), Avg fps: %u",
Jason Sams2dca84d2009-12-09 11:05:45 -0800189 mTimeMSLastFrame,
190 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
191 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
192 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700193 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
194 mAverageFPS);
Jason Sams2dca84d2009-12-09 11:05:45 -0800195 }
Jason Sams24371d92009-08-19 12:17:14 -0700196}
197
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800198bool Context::setupCheck() {
Jason Sams93eacc72012-12-18 14:26:57 -0800199#ifndef RS_COMPATIBILITY_LIB
Jason Sams721acc42011-04-06 11:23:54 -0700200 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700201 mFragment->setup(this, &mStateFragment);
Jason Sams721acc42011-04-06 11:23:54 -0700202 mRaster->setup(this, &mStateRaster);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700203 mVertex->setup(this, &mStateVertex);
204 mFBOCache.setup(this);
Jason Sams93eacc72012-12-18 14:26:57 -0800205#endif
Jason Samsa2cf7552010-03-03 13:03:18 -0800206 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700207}
208
Jason Sams93eacc72012-12-18 14:26:57 -0800209#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700210void Context::setupProgramStore() {
Jason Sams721acc42011-04-06 11:23:54 -0700211 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700212}
Jason Sams93eacc72012-12-18 14:26:57 -0800213#endif
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700214
Jason Samsd1f7da62012-03-15 19:18:03 -0700215static uint32_t getProp(const char *str) {
Nick Kralevich7e85ca22013-05-22 15:04:39 -0700216#if !defined(RS_SERVER) && defined(HAVE_ANDROID_OS)
Joe Onorato76371ff2009-09-23 16:37:36 -0700217 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700218 property_get(str, buf, "0");
Jason Samsd1f7da62012-03-15 19:18:03 -0700219 return atoi(buf);
Tim Murray0b575de2013-03-15 15:56:43 -0700220#else
221 return 0;
222#endif
Joe Onorato76371ff2009-09-23 16:37:36 -0700223}
Jason Sams326e0dd2009-05-22 14:03:28 -0700224
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800225void Context::displayDebugStats() {
Jason Sams93eacc72012-12-18 14:26:57 -0800226#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700227 char buffer[128];
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700228 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700229 float oldR, oldG, oldB, oldA;
230 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700231 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700232
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700233 ObjectBaseRef<Font> lastFont(getFont());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700234 setFont(nullptr);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700235 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700236 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700237 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700238
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700239 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700240 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700241
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700242 setFont(lastFont.get());
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700243 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Jason Sams93eacc72012-12-18 14:26:57 -0800244#endif
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700245}
246
Tim Murray0c66f072012-10-23 14:05:02 -0700247void * Context::threadProc(void *vrsc) {
248 Context *rsc = static_cast<Context *>(vrsc);
Jason Samscfaec172015-01-23 13:42:43 -0800249
Tim Murray0c66f072012-10-23 14:05:02 -0700250#ifndef ANDROID_RS_SERIALIZE
251 rsc->mNativeThreadId = gettid();
Tim Murray0c66f072012-10-23 14:05:02 -0700252#endif //ANDROID_RS_SERIALIZE
Jason Samscfaec172015-01-23 13:42:43 -0800253
Tim Murray0c66f072012-10-23 14:05:02 -0700254 rsc->props.mLogTimes = getProp("debug.rs.profile") != 0;
255 rsc->props.mLogScripts = getProp("debug.rs.script") != 0;
256 rsc->props.mLogObjects = getProp("debug.rs.object") != 0;
257 rsc->props.mLogShaders = getProp("debug.rs.shader") != 0;
258 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes") != 0;
259 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms") != 0;
260 rsc->props.mLogVisual = getProp("debug.rs.visual") != 0;
261 rsc->props.mDebugMaxThreads = getProp("debug.rs.max-threads");
262
Jason Sams2d8030a2014-07-24 18:47:34 -0700263 if (getProp("debug.rs.debug") != 0) {
264 ALOGD("Forcing debug context due to debug.rs.debug.");
265 rsc->mContextType = RS_CONTEXT_TYPE_DEBUG;
Jason Sams0ca7cba2015-03-11 15:22:38 -0700266 rsc->mForceCpu = true;
Jason Sams2d8030a2014-07-24 18:47:34 -0700267 }
268
Jason Sams0ca7cba2015-03-11 15:22:38 -0700269 bool forceCpu = getProp("debug.rs.default-CPU-driver") != 0;
270 if (forceCpu) {
271 ALOGD("Skipping hardware driver and loading default CPU driver");
272 rsc->mForceCpu = true;
Tim Murray0c66f072012-10-23 14:05:02 -0700273 }
274
Jason Sams0ca7cba2015-03-11 15:22:38 -0700275 rsc->mForceCpu |= rsc->mIsGraphicsContext;
276 rsc->loadDriver(rsc->mForceCpu);
Jason Sams110f1812013-03-14 16:02:18 -0700277
Jason Samscfaec172015-01-23 13:42:43 -0800278 if (!rsc->isSynchronous()) {
279 // Due to legacy we default to normal_graphics
280 // setPriority will make the adjustments as needed.
281 rsc->setPriority(RS_THREAD_PRIORITY_NORMAL_GRAPHICS);
282 }
Jason Samse5769102009-06-19 16:03:18 -0700283
Jason Sams93eacc72012-12-18 14:26:57 -0800284#ifndef RS_COMPATIBILITY_LIB
Jason Sams83c451a2011-04-21 11:46:50 -0700285 if (rsc->mIsGraphicsContext) {
Jason Samsd3e71072011-05-03 15:01:58 -0700286 if (!rsc->initGLThread()) {
287 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700288 return nullptr;
Jason Samsd3e71072011-05-03 15:01:58 -0700289 }
290
Jason Sams83c451a2011-04-21 11:46:50 -0700291 rsc->mStateRaster.init(rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700292 rsc->setProgramRaster(nullptr);
Jason Sams83c451a2011-04-21 11:46:50 -0700293 rsc->mStateVertex.init(rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700294 rsc->setProgramVertex(nullptr);
Jason Sams83c451a2011-04-21 11:46:50 -0700295 rsc->mStateFragment.init(rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700296 rsc->setProgramFragment(nullptr);
Jason Sams83c451a2011-04-21 11:46:50 -0700297 rsc->mStateFragmentStore.init(rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700298 rsc->setProgramStore(nullptr);
Jason Sams83c451a2011-04-21 11:46:50 -0700299 rsc->mStateFont.init(rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700300 rsc->setFont(nullptr);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700301 rsc->mStateSampler.init(rsc);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700302 rsc->mFBOCache.init(rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700303 }
Jason Sams93eacc72012-12-18 14:26:57 -0800304#endif
Jason Sams8ce125b2009-06-17 16:52:59 -0700305
Jason Sams83c451a2011-04-21 11:46:50 -0700306 rsc->mRunning = true;
Tim Murray4d252d62012-11-29 14:37:59 -0800307
308 if (rsc->isSynchronous()) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700309 return nullptr;
Tim Murray4d252d62012-11-29 14:37:59 -0800310 }
311
Jason Sams5f27d6f2012-02-07 15:32:08 -0800312 if (!rsc->mIsGraphicsContext) {
313 while (!rsc->mExit) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800314 rsc->mIO.playCoreCommands(rsc, -1);
Jason Samse0aab4a2011-08-12 15:05:15 -0700315 }
Jason Sams93eacc72012-12-18 14:26:57 -0800316#ifndef RS_COMPATIBILITY_LIB
Jason Sams5f27d6f2012-02-07 15:32:08 -0800317 } else {
318#ifndef ANDROID_RS_SERIALIZE
319 DisplayEventReceiver displayEvent;
320 DisplayEventReceiver::Event eventBuffer[1];
321#endif
322 int vsyncRate = 0;
323 int targetRate = 0;
Jason Samse0aab4a2011-08-12 15:05:15 -0700324
Jason Sams5f27d6f2012-02-07 15:32:08 -0800325 bool drawOnce = false;
326 while (!rsc->mExit) {
327 rsc->timerSet(RS_TIMER_IDLE);
Jason Sams326e0dd2009-05-22 14:03:28 -0700328
Jason Sams5f27d6f2012-02-07 15:32:08 -0800329#ifndef ANDROID_RS_SERIALIZE
Jason Sams9afd9a52012-02-17 16:59:50 -0800330 if (!rsc->mRootScript.get() || !rsc->mHasSurface || rsc->mPaused) {
331 targetRate = 0;
332 }
333
Jason Sams5f27d6f2012-02-07 15:32:08 -0800334 if (vsyncRate != targetRate) {
335 displayEvent.setVsyncRate(targetRate);
336 vsyncRate = targetRate;
337 }
338 if (targetRate) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800339 drawOnce |= rsc->mIO.playCoreCommands(rsc, displayEvent.getFd());
Jason Sams5f27d6f2012-02-07 15:32:08 -0800340 while (displayEvent.getEvents(eventBuffer, 1) != 0) {
341 //ALOGE("vs2 time past %lld", (rsc->getTime() - eventBuffer[0].header.timestamp) / 1000000);
342 }
343 } else
344#endif
345 {
Jason Sams963a2fb2012-02-09 14:36:14 -0800346 drawOnce |= rsc->mIO.playCoreCommands(rsc, -1);
Jason Sams83c451a2011-04-21 11:46:50 -0700347 }
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700348
Chris Wailes44bef6f2014-08-12 13:51:10 -0700349 if ((rsc->mRootScript.get() != nullptr) && rsc->mHasSurface &&
Jason Sams5f27d6f2012-02-07 15:32:08 -0800350 (targetRate || drawOnce) && !rsc->mPaused) {
351
352 drawOnce = false;
353 targetRate = ((rsc->runRootScript() + 15) / 16);
354
355 if (rsc->props.mLogVisual) {
356 rsc->displayDebugStats();
357 }
358
359 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
360 rsc->mHal.funcs.swap(rsc);
361 rsc->timerFrame();
362 rsc->timerSet(RS_TIMER_INTERNAL);
363 rsc->timerPrint();
364 rsc->timerReset();
365 }
Jason Sams83c451a2011-04-21 11:46:50 -0700366 }
Jason Sams93eacc72012-12-18 14:26:57 -0800367#endif
Jason Sams83c451a2011-04-21 11:46:50 -0700368 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700369
Tim Murray20f62ca2013-05-09 11:57:35 -0700370 //ALOGV("%p RS Thread exiting", rsc);
Jason Samse514b452009-09-25 14:51:22 -0700371
Jason Sams93eacc72012-12-18 14:26:57 -0800372#ifndef RS_COMPATIBILITY_LIB
Jason Sams83c451a2011-04-21 11:46:50 -0700373 if (rsc->mIsGraphicsContext) {
374 pthread_mutex_lock(&gInitMutex);
375 rsc->deinitEGL();
376 pthread_mutex_unlock(&gInitMutex);
377 }
Jason Sams93eacc72012-12-18 14:26:57 -0800378#endif
Jason Sams33b6e3b2009-10-27 14:44:31 -0700379
Tim Murray20f62ca2013-05-09 11:57:35 -0700380 //ALOGV("%p RS Thread exited", rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700381 return nullptr;
Jason Sams326e0dd2009-05-22 14:03:28 -0700382}
383
Jason Sams741aac92010-12-24 14:38:39 -0800384void Context::destroyWorkerThreadResources() {
Steve Block65982012011-10-20 11:56:00 +0100385 //ALOGV("destroyWorkerThreadResources 1");
Jason Sams2e8665d2011-01-27 00:14:13 -0800386 ObjectBase::zeroAllUserRef(this);
Jason Sams93eacc72012-12-18 14:26:57 -0800387#ifndef RS_COMPATIBILITY_LIB
Jason Sams741aac92010-12-24 14:38:39 -0800388 if (mIsGraphicsContext) {
389 mRaster.clear();
390 mFragment.clear();
391 mVertex.clear();
392 mFragmentStore.clear();
393 mFont.clear();
394 mRootScript.clear();
395 mStateRaster.deinit(this);
396 mStateVertex.deinit(this);
397 mStateFragment.deinit(this);
398 mStateFragmentStore.deinit(this);
399 mStateFont.deinit(this);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700400 mStateSampler.deinit(this);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700401 mFBOCache.deinit(this);
Jason Sams741aac92010-12-24 14:38:39 -0800402 }
Jason Sams93eacc72012-12-18 14:26:57 -0800403#endif
Jason Samsc7cec1e2011-08-18 18:01:33 -0700404 ObjectBase::freeAllChildren(this);
Jason Samscf912de2011-01-09 16:09:51 -0800405 mExit = true;
Jason Sams5f27d6f2012-02-07 15:32:08 -0800406 //ALOGV("destroyWorkerThreadResources 2");
Jason Sams741aac92010-12-24 14:38:39 -0800407}
408
Jason Sams2382aba2011-09-13 15:41:01 -0700409void Context::printWatchdogInfo(void *ctx) {
410 Context *rsc = (Context *)ctx;
Jason Samsee803442011-10-13 16:05:27 -0700411 if (rsc->watchdog.command && rsc->watchdog.file) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000412 ALOGE("RS watchdog timeout: %i %s line %i %s", rsc->watchdog.inRoot,
Jason Samsee803442011-10-13 16:05:27 -0700413 rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file);
414 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000415 ALOGE("RS watchdog timeout: %i", rsc->watchdog.inRoot);
Jason Samsee803442011-10-13 16:05:27 -0700416 }
Jason Sams2382aba2011-09-13 15:41:01 -0700417}
418
419
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800420void Context::setPriority(int32_t p) {
Jason Samscfaec172015-01-23 13:42:43 -0800421 switch (p) {
422 // The public API will always send NORMAL_GRAPHICS
423 // for normal, we adjust here
424 case RS_THREAD_PRIORITY_NORMAL_GRAPHICS:
425 if (mIsGraphicsContext) {
426 break;
427 } else {
428 if (mHal.flags & RS_CONTEXT_LOW_LATENCY) {
429 p = RS_THREAD_PRIORITY_LOW_LATENCY;
430 } else {
431 p = RS_THREAD_PRIORITY_NORMAL;
432 }
433 }
434 case RS_THREAD_PRIORITY_LOW:
435 break;
436 }
437
Jason Sams15832442009-11-15 12:14:26 -0800438 // Note: If we put this in the proper "background" policy
439 // the wallpapers can become completly unresponsive at times.
440 // This is probably not what we want for something the user is actively
441 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800442 mThreadPriority = p;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700443 setpriority(PRIO_PROCESS, mNativeThreadId, p);
Jason Sams9719bd42012-01-12 14:22:21 -0800444 mHal.funcs.setPriority(this, mThreadPriority);
Jason Sams15832442009-11-15 12:14:26 -0800445}
446
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800447Context::Context() {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700448 mDev = nullptr;
Jason Sams326e0dd2009-05-22 14:03:28 -0700449 mRunning = false;
450 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700451 mPaused = false;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700452 mObjHead = nullptr;
Jason Samsa2cf7552010-03-03 13:03:18 -0800453 mError = RS_ERROR_NONE;
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700454 mTargetSdkVersion = 14;
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700455 mDPI = 96;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700456 mIsContextLite = false;
Stephen Hines86c6b5f2011-10-31 14:07:54 -0700457 memset(&watchdog, 0, sizeof(watchdog));
Jason Samsf2748272013-11-26 18:10:59 -0800458 memset(&mHal, 0, sizeof(mHal));
Tim Murray0e92fa32012-11-06 14:36:38 -0800459 mForceCpu = false;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700460 mContextType = RS_CONTEXT_TYPE_NORMAL;
Tim Murray4d252d62012-11-29 14:37:59 -0800461 mSynchronous = false;
Tim Murray0e92fa32012-11-06 14:36:38 -0800462}
463
464Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc,
Tim Murray84e3dea2013-09-09 16:12:51 -0700465 RsContextType ct, uint32_t flags) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700466 Context * rsc = new Context();
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700467
Tim Murray84e3dea2013-09-09 16:12:51 -0700468 if (flags & RS_CONTEXT_LOW_LATENCY) {
469 rsc->mForceCpu = true;
470 }
471 if (flags & RS_CONTEXT_SYNCHRONOUS) {
472 rsc->mSynchronous = true;
473 }
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700474 rsc->mContextType = ct;
Jason Sams416f4b62014-06-25 15:45:36 -0700475 rsc->mHal.flags = flags;
Tim Murray0e92fa32012-11-06 14:36:38 -0800476
Jason Sams5c1c79a2010-11-03 14:27:11 -0700477 if (!rsc->initContext(dev, sc)) {
478 delete rsc;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700479 return nullptr;
Jason Sams5c1c79a2010-11-03 14:27:11 -0700480 }
481 return rsc;
482}
483
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700484Context * Context::createContextLite() {
485 Context * rsc = new Context();
486 rsc->mIsContextLite = true;
487 return rsc;
488}
489
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800490bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700491 pthread_mutex_lock(&gInitMutex);
492
Jason Sams1a4efa32011-05-17 15:01:29 -0700493 mIO.init();
Jason Sams5f27d6f2012-02-07 15:32:08 -0800494 mIO.setTimeoutCallback(printWatchdogInfo, this, 2e9);
Jason Sams1a4efa32011-05-17 15:01:29 -0700495
Jason Sams5c1c79a2010-11-03 14:27:11 -0700496 dev->addContext(this);
497 mDev = dev;
Jason Sams6b8552a2010-10-13 15:31:10 -0700498 if (sc) {
499 mUserSurfaceConfig = *sc;
500 } else {
501 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
502 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800503
Chris Wailes44bef6f2014-08-12 13:51:10 -0700504 mIsGraphicsContext = sc != nullptr;
Jason Sams326e0dd2009-05-22 14:03:28 -0700505
Jason Samsa658e902009-06-04 14:35:01 -0700506 int status;
507 pthread_attr_t threadAttr;
508
Jason Samsfb03a222009-10-15 16:47:31 -0700509 pthread_mutex_unlock(&gInitMutex);
510
511 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700512
Jason Samsa658e902009-06-04 14:35:01 -0700513 status = pthread_attr_init(&threadAttr);
514 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000515 ALOGE("Failed to init thread attribute.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700516 return false;
Jason Samsa658e902009-06-04 14:35:01 -0700517 }
518
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700519 mHasSurface = false;
Jason Sams992a0b72009-06-23 12:22:47 -0700520
Jason Sams24371d92009-08-19 12:17:14 -0700521 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700522 timerSet(RS_TIMER_INTERNAL);
Tim Murray4d252d62012-11-29 14:37:59 -0800523 if (mSynchronous) {
524 threadProc(this);
Xiaofei Wanfea96e82014-03-31 16:43:15 +0800525
526 if (mError != RS_ERROR_NONE) {
527 ALOGE("Errors during thread init (sync mode)");
528 return false;
529 }
Tim Murray4d252d62012-11-29 14:37:59 -0800530 } else {
531 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
532 if (status) {
533 ALOGE("Failed to start rs context thread.");
534 return false;
535 }
536 while (!mRunning && (mError == RS_ERROR_NONE)) {
537 usleep(100);
538 }
Jason Sams50869382009-08-18 17:07:09 -0700539
Tim Murray4d252d62012-11-29 14:37:59 -0800540 if (mError != RS_ERROR_NONE) {
541 ALOGE("Errors during thread init");
542 return false;
543 }
Jason Sams18133402010-07-20 15:09:00 -0700544
Tim Murray4d252d62012-11-29 14:37:59 -0800545 pthread_attr_destroy(&threadAttr);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700546 }
Jason Sams5c1c79a2010-11-03 14:27:11 -0700547 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700548}
549
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800550Context::~Context() {
Tim Murray20f62ca2013-05-09 11:57:35 -0700551 //ALOGV("%p Context::~Context", this);
Jason Samscf912de2011-01-09 16:09:51 -0800552
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700553 if (!mIsContextLite) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700554 mPaused = false;
555 void *res;
Jason Sams326e0dd2009-05-22 14:03:28 -0700556
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700557 mIO.shutdown();
Sangkyu Leebcba6f62014-04-01 15:22:13 +0900558 if (!mSynchronous) {
559 pthread_join(mThreadId, &res);
560 }
Jason Sams5f27d6f2012-02-07 15:32:08 -0800561 rsAssert(mExit);
Jason Sams326e0dd2009-05-22 14:03:28 -0700562
Xiaofei Wanfea96e82014-03-31 16:43:15 +0800563 if (mHal.funcs.shutdownDriver && mHal.drv) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700564 mHal.funcs.shutdownDriver(this);
565 }
566
567 // Global structure cleanup.
568 pthread_mutex_lock(&gInitMutex);
569 if (mDev) {
570 mDev->removeContext(this);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700571 mDev = nullptr;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700572 }
573 pthread_mutex_unlock(&gInitMutex);
Jason Sams51462c52011-01-25 00:26:25 -0800574 }
Tim Murray20f62ca2013-05-09 11:57:35 -0700575 //ALOGV("%p Context::~Context done", this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700576}
577
Jason Sams93eacc72012-12-18 14:26:57 -0800578#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700579void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800580 rsAssert(mIsGraphicsContext);
Jason Sams4b3de472011-04-06 17:52:23 -0700581 mHal.funcs.setSurface(this, w, h, sur);
Jason Sams458f2dc2009-11-03 13:58:36 -0800582
Chris Wailes44bef6f2014-08-12 13:51:10 -0700583 mHasSurface = sur != nullptr;
Jason Sams4b3de472011-04-06 17:52:23 -0700584 mWidth = w;
585 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800586
Jason Sams4b3de472011-04-06 17:52:23 -0700587 if (mWidth && mHeight) {
Jason Sams771565f2010-05-14 15:30:29 -0700588 mStateVertex.updateSize(this);
Alex Sakhartchouka544b632011-07-19 17:50:29 -0700589 mFBOCache.updateSize();
Jason Sams458f2dc2009-11-03 13:58:36 -0800590 }
591}
592
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800593uint32_t Context::getCurrentSurfaceWidth() const {
594 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700595 if (mFBOCache.mHal.state.colorTargets[i] != nullptr) {
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800596 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimX();
597 }
598 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700599 if (mFBOCache.mHal.state.depthTarget != nullptr) {
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800600 return mFBOCache.mHal.state.depthTarget->getType()->getDimX();
601 }
602 return mWidth;
603}
604
605uint32_t Context::getCurrentSurfaceHeight() const {
606 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700607 if (mFBOCache.mHal.state.colorTargets[i] != nullptr) {
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800608 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimY();
609 }
610 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700611 if (mFBOCache.mHal.state.depthTarget != nullptr) {
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800612 return mFBOCache.mHal.state.depthTarget->getType()->getDimY();
613 }
614 return mHeight;
615}
616
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800617void Context::pause() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800618 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700619 mPaused = true;
620}
621
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800622void Context::resume() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800623 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700624 mPaused = false;
625}
626
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800627void Context::setRootScript(Script *s) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800628 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700629 mRootScript.set(s);
630}
631
Jason Sams60709252010-11-17 15:29:32 -0800632void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800633 rsAssert(mIsGraphicsContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700634 if (pfs == nullptr) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700635 mFragmentStore.set(mStateFragmentStore.mDefault);
636 } else {
637 mFragmentStore.set(pfs);
638 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700639}
640
Jason Sams60709252010-11-17 15:29:32 -0800641void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800642 rsAssert(mIsGraphicsContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700643 if (pf == nullptr) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700644 mFragment.set(mStateFragment.mDefault);
645 } else {
646 mFragment.set(pf);
647 }
Jason Samscfb1d112009-08-05 13:57:03 -0700648}
649
Jason Sams60709252010-11-17 15:29:32 -0800650void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800651 rsAssert(mIsGraphicsContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700652 if (pr == nullptr) {
Jason Sams5fd09d82009-09-23 13:57:02 -0700653 mRaster.set(mStateRaster.mDefault);
654 } else {
655 mRaster.set(pr);
656 }
657}
658
Jason Sams60709252010-11-17 15:29:32 -0800659void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800660 rsAssert(mIsGraphicsContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700661 if (pv == nullptr) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700662 mVertex.set(mStateVertex.mDefault);
663 } else {
664 mVertex.set(pv);
665 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700666}
667
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800668void Context::setFont(Font *f) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700669 rsAssert(mIsGraphicsContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700670 if (f == nullptr) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700671 mFont.set(mStateFont.mDefault);
672 } else {
673 mFont.set(f);
674 }
675}
Jason Sams93eacc72012-12-18 14:26:57 -0800676#endif
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700677
Jason Samsf2748272013-11-26 18:10:59 -0800678void Context::finish() {
679 if (mHal.funcs.finish) {
680 mHal.funcs.finish(this);
681 }
682}
683
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800684void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700685 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700686 obj->setName(name, len);
Yang Nib8353c52015-02-14 18:00:59 -0800687 mNames.add(obj);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700688}
689
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800690void Context::removeName(ObjectBase *obj) {
Yang Nib8353c52015-02-14 18:00:59 -0800691 for (size_t ct=0; ct < mNames.size(); ct++) {
692 if (obj == mNames[ct]) {
693 mNames.removeAt(ct);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700694 return;
695 }
696 }
697}
698
Jason Sams1a4efa32011-05-17 15:01:29 -0700699RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) {
700 return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800701}
702
Jason Sams1a4efa32011-05-17 15:01:29 -0700703RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) {
704 return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen);
Jason Sams8c401ef2009-10-06 13:58:47 -0700705}
706
Jason Sams87319de2010-11-22 16:20:16 -0800707bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
708 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Sams1a4efa32011-05-17 15:01:29 -0700709
Jason Sams4961cce2013-04-11 16:11:46 -0700710 pthread_mutex_lock(&gMessageMutex);
711 bool ret = mIO.sendToClient(cmdID, subID, data, len, waitForSpace);
712 pthread_mutex_unlock(&gMessageMutex);
713 return ret;
Jason Sams8c401ef2009-10-06 13:58:47 -0700714}
715
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800716void Context::initToClient() {
717 while (!mRunning) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700718 usleep(100);
719 }
720}
721
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800722void Context::deinitToClient() {
Jason Sams1a4efa32011-05-17 15:01:29 -0700723 mIO.clientShutdown();
Jason Sams8c401ef2009-10-06 13:58:47 -0700724}
Jason Sams50869382009-08-18 17:07:09 -0700725
Jason Sams87319de2010-11-22 16:20:16 -0800726void Context::setError(RsError e, const char *msg) const {
Jason Samsa2cf7552010-03-03 13:03:18 -0800727 mError = e;
Jason Samsaad4bc52010-11-08 17:06:46 -0800728 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Samsa2cf7552010-03-03 13:03:18 -0800729}
730
731
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800732void Context::dumpDebug() const {
Steve Blockaf12ac62012-01-06 19:20:56 +0000733 ALOGE("RS Context debug %p", this);
734 ALOGE("RS Context debug");
Jason Sams13e26342009-11-24 12:26:35 -0800735
Steve Blockaf12ac62012-01-06 19:20:56 +0000736 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 Sams13e26342009-11-24 12:26:35 -0800739}
Jason Samsa4a54e42009-06-10 18:39:40 -0700740
Jason Sams326e0dd2009-05-22 14:03:28 -0700741///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700742//
Jason Sams326e0dd2009-05-22 14:03:28 -0700743
744namespace android {
745namespace renderscript {
746
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800747void rsi_ContextFinish(Context *rsc) {
Jason Samsf2748272013-11-26 18:10:59 -0800748 rsc->finish();
Jason Sams8c880902010-06-15 12:15:57 -0700749}
Jason Sams326e0dd2009-05-22 14:03:28 -0700750
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800751void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Sams93eacc72012-12-18 14:26:57 -0800752#ifndef RS_COMPATIBILITY_LIB
Jason Sams326e0dd2009-05-22 14:03:28 -0700753 Script *s = static_cast<Script *>(vs);
754 rsc->setRootScript(s);
Jason Sams93eacc72012-12-18 14:26:57 -0800755#endif
Jason Sams326e0dd2009-05-22 14:03:28 -0700756}
757
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800758void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700759 Sampler *s = static_cast<Sampler *>(vs);
760
761 if (slot > RS_MAX_SAMPLER_SLOT) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000762 ALOGE("Invalid sampler slot");
Jason Sams326e0dd2009-05-22 14:03:28 -0700763 return;
764 }
765
766 s->bindToContext(&rsc->mStateSampler, slot);
767}
768
Jason Sams93eacc72012-12-18 14:26:57 -0800769#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800770void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Samsccc010b2010-05-13 18:30:11 -0700771 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams60709252010-11-17 15:29:32 -0800772 rsc->setProgramStore(pfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700773}
774
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800775void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700776 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Sams60709252010-11-17 15:29:32 -0800777 rsc->setProgramFragment(pf);
Jason Sams326e0dd2009-05-22 14:03:28 -0700778}
779
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800780void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Sams5fd09d82009-09-23 13:57:02 -0700781 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Sams60709252010-11-17 15:29:32 -0800782 rsc->setProgramRaster(pr);
Jason Sams5fd09d82009-09-23 13:57:02 -0700783}
784
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800785void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700786 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Sams60709252010-11-17 15:29:32 -0800787 rsc->setProgramVertex(pv);
Jason Sams326e0dd2009-05-22 14:03:28 -0700788}
789
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800790void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700791 Font *font = static_cast<Font *>(vfont);
792 rsc->setFont(font);
793}
Jason Sams93eacc72012-12-18 14:26:57 -0800794#endif
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700795
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700796void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700797 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700798 rsc->assignName(ob, name, name_length);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700799}
Jason Sams326e0dd2009-05-22 14:03:28 -0700800
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800801void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams2353ae32010-10-14 17:48:46 -0700802 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -0700803 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700804 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700805}
806
Jason Sams93eacc72012-12-18 14:26:57 -0800807#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800808void rsi_ContextPause(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700809 rsc->pause();
810}
811
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800812void rsi_ContextResume(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700813 rsc->resume();
814}
815
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700816void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
Mathias Agopianfa402862010-02-12 14:04:35 -0800817 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800818}
Jason Sams93eacc72012-12-18 14:26:57 -0800819#endif
Jason Sams613cad12009-11-12 15:10:25 -0800820
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800821void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800822 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800823}
824
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800825void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Samsc21cf402009-11-17 17:26:46 -0800826 ObjectBase::dumpAll(rsc);
827}
828
Jason Sams741aac92010-12-24 14:38:39 -0800829void rsi_ContextDestroyWorker(Context *rsc) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700830 rsc->destroyWorkerThreadResources();
Jason Sams741aac92010-12-24 14:38:39 -0800831}
832
Jason Samsc975cf42011-04-28 18:26:48 -0700833void rsi_ContextDestroy(Context *rsc) {
Jason Samsa36c50a2014-06-17 12:06:06 -0700834 //ALOGE("%p rsContextDestroy", rsc);
Jason Sams741aac92010-12-24 14:38:39 -0800835 rsContextDestroyWorker(rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800836 delete rsc;
Tim Murray20f62ca2013-05-09 11:57:35 -0700837 //ALOGV("%p rsContextDestroy done", rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800838}
839
Jason Samsc975cf42011-04-28 18:26:48 -0700840RsMessageToClientType rsi_ContextPeekMessage(Context *rsc,
Jason Sams186e5912011-04-26 14:50:00 -0700841 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700842 uint32_t * subID, size_t subID_length) {
843 return rsc->peekMessageToClient(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800844}
845
Jason Samsc975cf42011-04-28 18:26:48 -0700846RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length,
Jason Sams186e5912011-04-26 14:50:00 -0700847 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700848 uint32_t * subID, size_t subID_length) {
Jason Sams186e5912011-04-26 14:50:00 -0700849 rsAssert(subID_length == sizeof(uint32_t));
850 rsAssert(receiveLen_length == sizeof(size_t));
Jason Sams1a4efa32011-05-17 15:01:29 -0700851 return rsc->getMessageToClient(data, receiveLen, subID, data_length);
Jason Sams8c401ef2009-10-06 13:58:47 -0700852}
853
Jason Samsc975cf42011-04-28 18:26:48 -0700854void rsi_ContextInitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700855 rsc->initToClient();
856}
857
Jason Samsc975cf42011-04-28 18:26:48 -0700858void rsi_ContextDeinitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700859 rsc->deinitToClient();
860}
861
Jason Sams70265202013-02-05 19:20:47 -0800862void rsi_ContextSendMessage(Context *rsc, uint32_t id, const uint8_t *data, size_t len) {
863 rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, id, len, true);
864}
865
Tim Murray397b0b12014-01-07 15:35:08 -0800866// implementation of handcode LF_ObjDestroy
867// required so nObjDestroy can be run from finalizer without blocking
868void LF_ObjDestroy_handcode(const Context *rsc, RsAsyncVoidPtr objPtr) {
869 if (((Context *)rsc)->isSynchronous()) {
870 rsi_ObjDestroy((Context *)rsc, objPtr);
871 return;
872 }
873
874 // struct has two parts:
875 // RsPlaybackRemoteHeader (cmdID and bytes)
876 // RS_CMD_ObjDestroy (ptr)
877 struct destroyCmd {
878 uint32_t cmdID;
879 uint32_t bytes;
880 RsAsyncVoidPtr ptr;
881 };
882
883 destroyCmd cmd;
884 cmd.cmdID = RS_CMD_ID_ObjDestroy;
885 cmd.bytes = sizeof(RsAsyncVoidPtr);
886 cmd.ptr = objPtr;
887 ThreadIO *io = &((Context *)rsc)->mIO;
888 io->coreWrite((void*)&cmd, sizeof(destroyCmd));
889
890}
891
Jason Samsc975cf42011-04-28 18:26:48 -0700892}
893}
894
Tim Murrayc2ce7072013-07-17 18:38:53 -0700895extern "C" RsContext rsContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion,
Tim Murray84e3dea2013-09-09 16:12:51 -0700896 RsContextType ct, uint32_t flags) {
Tim Murray20f62ca2013-05-09 11:57:35 -0700897 //ALOGV("rsContextCreate dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700898 Device * dev = static_cast<Device *>(vdev);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700899 Context *rsc = Context::createContext(dev, nullptr, ct, flags);
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700900 if (rsc) {
901 rsc->setTargetSdkVersion(sdkVersion);
902 }
Jason Sams789ca832011-05-18 17:36:02 -0700903 return rsc;
904}
905
Miao Wangf3213d72015-01-14 10:03:07 -0800906#ifdef RS_COMPATIBILITY_LIB
907extern "C" void rsaContextSetNativeLibDir(RsContext con, char *libDir, size_t length) {
908 Context *rsc = static_cast<Context *>(con);
909 rsc->setNativeLibDir(libDir, length);
910}
911#endif
912
Jason Sams93eacc72012-12-18 14:26:57 -0800913#ifndef RS_COMPATIBILITY_LIB
Jason Sams789ca832011-05-18 17:36:02 -0700914RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700915 uint32_t sdkVersion, RsSurfaceConfig sc,
916 uint32_t dpi) {
Tim Murray20f62ca2013-05-09 11:57:35 -0700917 //ALOGV("rsContextCreateGL dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700918 Device * dev = static_cast<Device *>(vdev);
919 Context *rsc = Context::createContext(dev, &sc);
Jason Sams9544f762011-07-13 16:09:42 -0700920 if (rsc) {
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700921 rsc->setTargetSdkVersion(sdkVersion);
Jason Sams9544f762011-07-13 16:09:42 -0700922 rsc->setDPI(dpi);
923 }
Tim Murray20f62ca2013-05-09 11:57:35 -0700924 //ALOGV("%p rsContextCreateGL ret", rsc);
Jason Sams789ca832011-05-18 17:36:02 -0700925 return rsc;
926}
Jason Sams93eacc72012-12-18 14:26:57 -0800927#endif
Jason Sams789ca832011-05-18 17:36:02 -0700928
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700929// Only to be called at a3d load time, before object is visible to user
930// not thread safe
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800931void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700932 ObjectBase *ob = static_cast<ObjectBase *>(obj);
933 (*name) = ob->getName();
934}
Yang Nib8353c52015-02-14 18:00:59 -0800935