blob: b63d01d449e4a97a246026ada0bbc65e4f695eb3 [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 -0700247bool Context::loadRuntime(const char* filename, Context* rsc) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700248
Tim Murray0c66f072012-10-23 14:05:02 -0700249 // TODO: store the driverSO somewhere so we can dlclose later
Chris Wailes44bef6f2014-08-12 13:51:10 -0700250 void *driverSO = nullptr;
Stephen Hines91dfcdb2012-09-07 18:23:35 -0700251
Tim Murray0c66f072012-10-23 14:05:02 -0700252 driverSO = dlopen(filename, RTLD_LAZY);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700253 if (driverSO == nullptr) {
Tim Murray0c66f072012-10-23 14:05:02 -0700254 ALOGE("Failed loading RS driver: %s", dlerror());
255 return false;
Stephen Hines414a4612012-09-05 18:05:08 -0700256 }
257
258 // Need to call dlerror() to clear buffer before using it for dlsym().
259 (void) dlerror();
260 typedef bool (*HalSig)(Context*, uint32_t, uint32_t);
261 HalSig halInit = (HalSig) dlsym(driverSO, "rsdHalInit");
262
263 // If we can't find the C variant, we go looking for the C++ version.
Chris Wailes44bef6f2014-08-12 13:51:10 -0700264 if (halInit == nullptr) {
Stephen Hines414a4612012-09-05 18:05:08 -0700265 ALOGW("Falling back to find C++ rsdHalInit: %s", dlerror());
266 halInit = (HalSig) dlsym(driverSO,
267 "_Z10rsdHalInitPN7android12renderscript7ContextEjj");
268 }
269
Chris Wailes44bef6f2014-08-12 13:51:10 -0700270 if (halInit == nullptr) {
Stephen Hines414a4612012-09-05 18:05:08 -0700271 dlclose(driverSO);
272 ALOGE("Failed to find rsdHalInit: %s", dlerror());
Tim Murray0c66f072012-10-23 14:05:02 -0700273 return false;
Stephen Hines414a4612012-09-05 18:05:08 -0700274 }
275
276 if (!(*halInit)(rsc, 0, 0)) {
Stephen Hines414a4612012-09-05 18:05:08 -0700277 dlclose(driverSO);
Steve Blockaf12ac62012-01-06 19:20:56 +0000278 ALOGE("Hal init failed");
Tim Murray0c66f072012-10-23 14:05:02 -0700279 return false;
Jason Sams83c451a2011-04-21 11:46:50 -0700280 }
Tim Murray0c66f072012-10-23 14:05:02 -0700281
282 //validate HAL struct
283
284
285 return true;
286}
287
Jason Sams110f1812013-03-14 16:02:18 -0700288extern "C" bool rsdHalInit(RsContext c, uint32_t version_major, uint32_t version_minor);
289
Tim Murray0c66f072012-10-23 14:05:02 -0700290void * Context::threadProc(void *vrsc) {
291 Context *rsc = static_cast<Context *>(vrsc);
Jason Samscfaec172015-01-23 13:42:43 -0800292
Tim Murray0c66f072012-10-23 14:05:02 -0700293#ifndef ANDROID_RS_SERIALIZE
294 rsc->mNativeThreadId = gettid();
Tim Murray0c66f072012-10-23 14:05:02 -0700295#endif //ANDROID_RS_SERIALIZE
Jason Samscfaec172015-01-23 13:42:43 -0800296
Tim Murray0c66f072012-10-23 14:05:02 -0700297 rsc->props.mLogTimes = getProp("debug.rs.profile") != 0;
298 rsc->props.mLogScripts = getProp("debug.rs.script") != 0;
299 rsc->props.mLogObjects = getProp("debug.rs.object") != 0;
300 rsc->props.mLogShaders = getProp("debug.rs.shader") != 0;
301 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes") != 0;
302 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms") != 0;
303 rsc->props.mLogVisual = getProp("debug.rs.visual") != 0;
304 rsc->props.mDebugMaxThreads = getProp("debug.rs.max-threads");
305
Jason Sams2d8030a2014-07-24 18:47:34 -0700306 if (getProp("debug.rs.debug") != 0) {
307 ALOGD("Forcing debug context due to debug.rs.debug.");
308 rsc->mContextType = RS_CONTEXT_TYPE_DEBUG;
309 }
310
Tim Murray0c66f072012-10-23 14:05:02 -0700311 bool loadDefault = true;
312
313 // Provide a mechanism for dropping in a different RS driver.
Jason Sams110f1812013-03-14 16:02:18 -0700314#ifndef RS_COMPATIBILITY_LIB
Tim Murray0c66f072012-10-23 14:05:02 -0700315#ifdef OVERRIDE_RS_DRIVER
316#define XSTR(S) #S
317#define STR(S) XSTR(S)
318#define OVERRIDE_RS_DRIVER_STRING STR(OVERRIDE_RS_DRIVER)
319
320 if (getProp("debug.rs.default-CPU-driver") != 0) {
Stephen Hines43463922013-11-14 13:57:50 -0800321 ALOGD("Skipping override driver and loading default CPU driver");
Jason Samscd5f1ce2014-03-31 14:44:53 -0700322 } else if (rsc->mForceCpu || rsc->mIsGraphicsContext) {
Tim Murray0e92fa32012-11-06 14:36:38 -0800323 ALOGV("Application requested CPU execution");
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700324 } else if (rsc->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
325 ALOGV("Application requested debug context");
Tim Murray0c66f072012-10-23 14:05:02 -0700326 } else {
Tim Murray6e6cdbc2014-09-10 15:58:54 -0700327#if defined(__LP64__) && defined(DISABLE_RS_64_BIT_DRIVER)
328 // skip load
329#else
Tim Murray0c66f072012-10-23 14:05:02 -0700330 if (loadRuntime(OVERRIDE_RS_DRIVER_STRING, rsc)) {
Stephen Hines43463922013-11-14 13:57:50 -0800331 ALOGV("Successfully loaded runtime: %s", OVERRIDE_RS_DRIVER_STRING);
Tim Murray0c66f072012-10-23 14:05:02 -0700332 loadDefault = false;
333 } else {
334 ALOGE("Failed to load runtime %s, loading default", OVERRIDE_RS_DRIVER_STRING);
335 }
Tim Murray6e6cdbc2014-09-10 15:58:54 -0700336#endif
Tim Murray0c66f072012-10-23 14:05:02 -0700337 }
338
339#undef XSTR
340#undef STR
341#endif // OVERRIDE_RS_DRIVER
342
343 if (loadDefault) {
344 if (!loadRuntime("libRSDriver.so", rsc)) {
345 ALOGE("Failed to load default runtime!");
Stephen Hines6f01bcf2012-11-19 15:18:16 -0800346 rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed loading RS driver");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700347 return nullptr;
Tim Murray0c66f072012-10-23 14:05:02 -0700348 }
349 }
Jason Sams110f1812013-03-14 16:02:18 -0700350#else // RS_COMPATIBILITY_LIB
351 if (rsdHalInit(rsc, 0, 0) != true) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700352 return nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700353 }
354#endif
355
Jason Samscfaec172015-01-23 13:42:43 -0800356 if (!rsc->isSynchronous()) {
357 // Due to legacy we default to normal_graphics
358 // setPriority will make the adjustments as needed.
359 rsc->setPriority(RS_THREAD_PRIORITY_NORMAL_GRAPHICS);
360 }
Jason Samse5769102009-06-19 16:03:18 -0700361
Jason Sams93eacc72012-12-18 14:26:57 -0800362#ifndef RS_COMPATIBILITY_LIB
Jason Sams83c451a2011-04-21 11:46:50 -0700363 if (rsc->mIsGraphicsContext) {
Jason Samsd3e71072011-05-03 15:01:58 -0700364 if (!rsc->initGLThread()) {
365 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700366 return nullptr;
Jason Samsd3e71072011-05-03 15:01:58 -0700367 }
368
Jason Sams83c451a2011-04-21 11:46:50 -0700369 rsc->mStateRaster.init(rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700370 rsc->setProgramRaster(nullptr);
Jason Sams83c451a2011-04-21 11:46:50 -0700371 rsc->mStateVertex.init(rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700372 rsc->setProgramVertex(nullptr);
Jason Sams83c451a2011-04-21 11:46:50 -0700373 rsc->mStateFragment.init(rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700374 rsc->setProgramFragment(nullptr);
Jason Sams83c451a2011-04-21 11:46:50 -0700375 rsc->mStateFragmentStore.init(rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700376 rsc->setProgramStore(nullptr);
Jason Sams83c451a2011-04-21 11:46:50 -0700377 rsc->mStateFont.init(rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700378 rsc->setFont(nullptr);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700379 rsc->mStateSampler.init(rsc);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700380 rsc->mFBOCache.init(rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700381 }
Jason Sams93eacc72012-12-18 14:26:57 -0800382#endif
Jason Sams8ce125b2009-06-17 16:52:59 -0700383
Jason Sams83c451a2011-04-21 11:46:50 -0700384 rsc->mRunning = true;
Tim Murray4d252d62012-11-29 14:37:59 -0800385
386 if (rsc->isSynchronous()) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700387 return nullptr;
Tim Murray4d252d62012-11-29 14:37:59 -0800388 }
389
Jason Sams5f27d6f2012-02-07 15:32:08 -0800390 if (!rsc->mIsGraphicsContext) {
391 while (!rsc->mExit) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800392 rsc->mIO.playCoreCommands(rsc, -1);
Jason Samse0aab4a2011-08-12 15:05:15 -0700393 }
Jason Sams93eacc72012-12-18 14:26:57 -0800394#ifndef RS_COMPATIBILITY_LIB
Jason Sams5f27d6f2012-02-07 15:32:08 -0800395 } else {
396#ifndef ANDROID_RS_SERIALIZE
397 DisplayEventReceiver displayEvent;
398 DisplayEventReceiver::Event eventBuffer[1];
399#endif
400 int vsyncRate = 0;
401 int targetRate = 0;
Jason Samse0aab4a2011-08-12 15:05:15 -0700402
Jason Sams5f27d6f2012-02-07 15:32:08 -0800403 bool drawOnce = false;
404 while (!rsc->mExit) {
405 rsc->timerSet(RS_TIMER_IDLE);
Jason Sams326e0dd2009-05-22 14:03:28 -0700406
Jason Sams5f27d6f2012-02-07 15:32:08 -0800407#ifndef ANDROID_RS_SERIALIZE
Jason Sams9afd9a52012-02-17 16:59:50 -0800408 if (!rsc->mRootScript.get() || !rsc->mHasSurface || rsc->mPaused) {
409 targetRate = 0;
410 }
411
Jason Sams5f27d6f2012-02-07 15:32:08 -0800412 if (vsyncRate != targetRate) {
413 displayEvent.setVsyncRate(targetRate);
414 vsyncRate = targetRate;
415 }
416 if (targetRate) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800417 drawOnce |= rsc->mIO.playCoreCommands(rsc, displayEvent.getFd());
Jason Sams5f27d6f2012-02-07 15:32:08 -0800418 while (displayEvent.getEvents(eventBuffer, 1) != 0) {
419 //ALOGE("vs2 time past %lld", (rsc->getTime() - eventBuffer[0].header.timestamp) / 1000000);
420 }
421 } else
422#endif
423 {
Jason Sams963a2fb2012-02-09 14:36:14 -0800424 drawOnce |= rsc->mIO.playCoreCommands(rsc, -1);
Jason Sams83c451a2011-04-21 11:46:50 -0700425 }
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700426
Chris Wailes44bef6f2014-08-12 13:51:10 -0700427 if ((rsc->mRootScript.get() != nullptr) && rsc->mHasSurface &&
Jason Sams5f27d6f2012-02-07 15:32:08 -0800428 (targetRate || drawOnce) && !rsc->mPaused) {
429
430 drawOnce = false;
431 targetRate = ((rsc->runRootScript() + 15) / 16);
432
433 if (rsc->props.mLogVisual) {
434 rsc->displayDebugStats();
435 }
436
437 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
438 rsc->mHal.funcs.swap(rsc);
439 rsc->timerFrame();
440 rsc->timerSet(RS_TIMER_INTERNAL);
441 rsc->timerPrint();
442 rsc->timerReset();
443 }
Jason Sams83c451a2011-04-21 11:46:50 -0700444 }
Jason Sams93eacc72012-12-18 14:26:57 -0800445#endif
Jason Sams83c451a2011-04-21 11:46:50 -0700446 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700447
Tim Murray20f62ca2013-05-09 11:57:35 -0700448 //ALOGV("%p RS Thread exiting", rsc);
Jason Samse514b452009-09-25 14:51:22 -0700449
Jason Sams93eacc72012-12-18 14:26:57 -0800450#ifndef RS_COMPATIBILITY_LIB
Jason Sams83c451a2011-04-21 11:46:50 -0700451 if (rsc->mIsGraphicsContext) {
452 pthread_mutex_lock(&gInitMutex);
453 rsc->deinitEGL();
454 pthread_mutex_unlock(&gInitMutex);
455 }
Jason Sams93eacc72012-12-18 14:26:57 -0800456#endif
Jason Sams33b6e3b2009-10-27 14:44:31 -0700457
Tim Murray20f62ca2013-05-09 11:57:35 -0700458 //ALOGV("%p RS Thread exited", rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700459 return nullptr;
Jason Sams326e0dd2009-05-22 14:03:28 -0700460}
461
Jason Sams741aac92010-12-24 14:38:39 -0800462void Context::destroyWorkerThreadResources() {
Steve Block65982012011-10-20 11:56:00 +0100463 //ALOGV("destroyWorkerThreadResources 1");
Jason Sams2e8665d2011-01-27 00:14:13 -0800464 ObjectBase::zeroAllUserRef(this);
Jason Sams93eacc72012-12-18 14:26:57 -0800465#ifndef RS_COMPATIBILITY_LIB
Jason Sams741aac92010-12-24 14:38:39 -0800466 if (mIsGraphicsContext) {
467 mRaster.clear();
468 mFragment.clear();
469 mVertex.clear();
470 mFragmentStore.clear();
471 mFont.clear();
472 mRootScript.clear();
473 mStateRaster.deinit(this);
474 mStateVertex.deinit(this);
475 mStateFragment.deinit(this);
476 mStateFragmentStore.deinit(this);
477 mStateFont.deinit(this);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700478 mStateSampler.deinit(this);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700479 mFBOCache.deinit(this);
Jason Sams741aac92010-12-24 14:38:39 -0800480 }
Jason Sams93eacc72012-12-18 14:26:57 -0800481#endif
Jason Samsc7cec1e2011-08-18 18:01:33 -0700482 ObjectBase::freeAllChildren(this);
Jason Samscf912de2011-01-09 16:09:51 -0800483 mExit = true;
Jason Sams5f27d6f2012-02-07 15:32:08 -0800484 //ALOGV("destroyWorkerThreadResources 2");
Jason Sams741aac92010-12-24 14:38:39 -0800485}
486
Jason Sams2382aba2011-09-13 15:41:01 -0700487void Context::printWatchdogInfo(void *ctx) {
488 Context *rsc = (Context *)ctx;
Jason Samsee803442011-10-13 16:05:27 -0700489 if (rsc->watchdog.command && rsc->watchdog.file) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000490 ALOGE("RS watchdog timeout: %i %s line %i %s", rsc->watchdog.inRoot,
Jason Samsee803442011-10-13 16:05:27 -0700491 rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file);
492 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000493 ALOGE("RS watchdog timeout: %i", rsc->watchdog.inRoot);
Jason Samsee803442011-10-13 16:05:27 -0700494 }
Jason Sams2382aba2011-09-13 15:41:01 -0700495}
496
497
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800498void Context::setPriority(int32_t p) {
Jason Samscfaec172015-01-23 13:42:43 -0800499 switch (p) {
500 // The public API will always send NORMAL_GRAPHICS
501 // for normal, we adjust here
502 case RS_THREAD_PRIORITY_NORMAL_GRAPHICS:
503 if (mIsGraphicsContext) {
504 break;
505 } else {
506 if (mHal.flags & RS_CONTEXT_LOW_LATENCY) {
507 p = RS_THREAD_PRIORITY_LOW_LATENCY;
508 } else {
509 p = RS_THREAD_PRIORITY_NORMAL;
510 }
511 }
512 case RS_THREAD_PRIORITY_LOW:
513 break;
514 }
515
Jason Sams15832442009-11-15 12:14:26 -0800516 // Note: If we put this in the proper "background" policy
517 // the wallpapers can become completly unresponsive at times.
518 // This is probably not what we want for something the user is actively
519 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800520 mThreadPriority = p;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700521 setpriority(PRIO_PROCESS, mNativeThreadId, p);
Jason Sams9719bd42012-01-12 14:22:21 -0800522 mHal.funcs.setPriority(this, mThreadPriority);
Jason Sams15832442009-11-15 12:14:26 -0800523}
524
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800525Context::Context() {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700526 mDev = nullptr;
Jason Sams326e0dd2009-05-22 14:03:28 -0700527 mRunning = false;
528 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700529 mPaused = false;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700530 mObjHead = nullptr;
Jason Samsa2cf7552010-03-03 13:03:18 -0800531 mError = RS_ERROR_NONE;
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700532 mTargetSdkVersion = 14;
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700533 mDPI = 96;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700534 mIsContextLite = false;
Stephen Hines86c6b5f2011-10-31 14:07:54 -0700535 memset(&watchdog, 0, sizeof(watchdog));
Jason Samsf2748272013-11-26 18:10:59 -0800536 memset(&mHal, 0, sizeof(mHal));
Tim Murray0e92fa32012-11-06 14:36:38 -0800537 mForceCpu = false;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700538 mContextType = RS_CONTEXT_TYPE_NORMAL;
Tim Murray4d252d62012-11-29 14:37:59 -0800539 mSynchronous = false;
Tim Murray0e92fa32012-11-06 14:36:38 -0800540}
541
542Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc,
Tim Murray84e3dea2013-09-09 16:12:51 -0700543 RsContextType ct, uint32_t flags) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700544 Context * rsc = new Context();
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700545
Tim Murray84e3dea2013-09-09 16:12:51 -0700546 if (flags & RS_CONTEXT_LOW_LATENCY) {
547 rsc->mForceCpu = true;
548 }
549 if (flags & RS_CONTEXT_SYNCHRONOUS) {
550 rsc->mSynchronous = true;
551 }
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700552 rsc->mContextType = ct;
Jason Sams416f4b62014-06-25 15:45:36 -0700553 rsc->mHal.flags = flags;
Tim Murray0e92fa32012-11-06 14:36:38 -0800554
Jason Sams5c1c79a2010-11-03 14:27:11 -0700555 if (!rsc->initContext(dev, sc)) {
556 delete rsc;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700557 return nullptr;
Jason Sams5c1c79a2010-11-03 14:27:11 -0700558 }
559 return rsc;
560}
561
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700562Context * Context::createContextLite() {
563 Context * rsc = new Context();
564 rsc->mIsContextLite = true;
565 return rsc;
566}
567
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800568bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700569 pthread_mutex_lock(&gInitMutex);
570
Jason Sams1a4efa32011-05-17 15:01:29 -0700571 mIO.init();
Jason Sams5f27d6f2012-02-07 15:32:08 -0800572 mIO.setTimeoutCallback(printWatchdogInfo, this, 2e9);
Jason Sams1a4efa32011-05-17 15:01:29 -0700573
Jason Sams5c1c79a2010-11-03 14:27:11 -0700574 dev->addContext(this);
575 mDev = dev;
Jason Sams6b8552a2010-10-13 15:31:10 -0700576 if (sc) {
577 mUserSurfaceConfig = *sc;
578 } else {
579 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
580 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800581
Chris Wailes44bef6f2014-08-12 13:51:10 -0700582 mIsGraphicsContext = sc != nullptr;
Jason Sams326e0dd2009-05-22 14:03:28 -0700583
Jason Samsa658e902009-06-04 14:35:01 -0700584 int status;
585 pthread_attr_t threadAttr;
586
Jason Samsfb03a222009-10-15 16:47:31 -0700587 pthread_mutex_unlock(&gInitMutex);
588
589 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700590
Jason Samsa658e902009-06-04 14:35:01 -0700591 status = pthread_attr_init(&threadAttr);
592 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000593 ALOGE("Failed to init thread attribute.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700594 return false;
Jason Samsa658e902009-06-04 14:35:01 -0700595 }
596
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700597 mHasSurface = false;
Jason Sams992a0b72009-06-23 12:22:47 -0700598
Jason Sams24371d92009-08-19 12:17:14 -0700599 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700600 timerSet(RS_TIMER_INTERNAL);
Tim Murray4d252d62012-11-29 14:37:59 -0800601 if (mSynchronous) {
602 threadProc(this);
Xiaofei Wanfea96e82014-03-31 16:43:15 +0800603
604 if (mError != RS_ERROR_NONE) {
605 ALOGE("Errors during thread init (sync mode)");
606 return false;
607 }
Tim Murray4d252d62012-11-29 14:37:59 -0800608 } else {
609 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
610 if (status) {
611 ALOGE("Failed to start rs context thread.");
612 return false;
613 }
614 while (!mRunning && (mError == RS_ERROR_NONE)) {
615 usleep(100);
616 }
Jason Sams50869382009-08-18 17:07:09 -0700617
Tim Murray4d252d62012-11-29 14:37:59 -0800618 if (mError != RS_ERROR_NONE) {
619 ALOGE("Errors during thread init");
620 return false;
621 }
Jason Sams18133402010-07-20 15:09:00 -0700622
Tim Murray4d252d62012-11-29 14:37:59 -0800623 pthread_attr_destroy(&threadAttr);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700624 }
Jason Sams5c1c79a2010-11-03 14:27:11 -0700625 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700626}
627
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800628Context::~Context() {
Tim Murray20f62ca2013-05-09 11:57:35 -0700629 //ALOGV("%p Context::~Context", this);
Jason Samscf912de2011-01-09 16:09:51 -0800630
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700631 if (!mIsContextLite) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700632 mPaused = false;
633 void *res;
Jason Sams326e0dd2009-05-22 14:03:28 -0700634
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700635 mIO.shutdown();
Sangkyu Leebcba6f62014-04-01 15:22:13 +0900636 if (!mSynchronous) {
637 pthread_join(mThreadId, &res);
638 }
Jason Sams5f27d6f2012-02-07 15:32:08 -0800639 rsAssert(mExit);
Jason Sams326e0dd2009-05-22 14:03:28 -0700640
Xiaofei Wanfea96e82014-03-31 16:43:15 +0800641 if (mHal.funcs.shutdownDriver && mHal.drv) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700642 mHal.funcs.shutdownDriver(this);
643 }
644
645 // Global structure cleanup.
646 pthread_mutex_lock(&gInitMutex);
647 if (mDev) {
648 mDev->removeContext(this);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700649 mDev = nullptr;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700650 }
651 pthread_mutex_unlock(&gInitMutex);
Jason Sams51462c52011-01-25 00:26:25 -0800652 }
Tim Murray20f62ca2013-05-09 11:57:35 -0700653 //ALOGV("%p Context::~Context done", this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700654}
655
Jason Sams93eacc72012-12-18 14:26:57 -0800656#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700657void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800658 rsAssert(mIsGraphicsContext);
Jason Sams4b3de472011-04-06 17:52:23 -0700659 mHal.funcs.setSurface(this, w, h, sur);
Jason Sams458f2dc2009-11-03 13:58:36 -0800660
Chris Wailes44bef6f2014-08-12 13:51:10 -0700661 mHasSurface = sur != nullptr;
Jason Sams4b3de472011-04-06 17:52:23 -0700662 mWidth = w;
663 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800664
Jason Sams4b3de472011-04-06 17:52:23 -0700665 if (mWidth && mHeight) {
Jason Sams771565f2010-05-14 15:30:29 -0700666 mStateVertex.updateSize(this);
Alex Sakhartchouka544b632011-07-19 17:50:29 -0700667 mFBOCache.updateSize();
Jason Sams458f2dc2009-11-03 13:58:36 -0800668 }
669}
670
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800671uint32_t Context::getCurrentSurfaceWidth() const {
672 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700673 if (mFBOCache.mHal.state.colorTargets[i] != nullptr) {
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800674 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimX();
675 }
676 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700677 if (mFBOCache.mHal.state.depthTarget != nullptr) {
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800678 return mFBOCache.mHal.state.depthTarget->getType()->getDimX();
679 }
680 return mWidth;
681}
682
683uint32_t Context::getCurrentSurfaceHeight() const {
684 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700685 if (mFBOCache.mHal.state.colorTargets[i] != nullptr) {
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800686 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimY();
687 }
688 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700689 if (mFBOCache.mHal.state.depthTarget != nullptr) {
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800690 return mFBOCache.mHal.state.depthTarget->getType()->getDimY();
691 }
692 return mHeight;
693}
694
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800695void Context::pause() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800696 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700697 mPaused = true;
698}
699
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800700void Context::resume() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800701 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700702 mPaused = false;
703}
704
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800705void Context::setRootScript(Script *s) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800706 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700707 mRootScript.set(s);
708}
709
Jason Sams60709252010-11-17 15:29:32 -0800710void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800711 rsAssert(mIsGraphicsContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700712 if (pfs == nullptr) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700713 mFragmentStore.set(mStateFragmentStore.mDefault);
714 } else {
715 mFragmentStore.set(pfs);
716 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700717}
718
Jason Sams60709252010-11-17 15:29:32 -0800719void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800720 rsAssert(mIsGraphicsContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700721 if (pf == nullptr) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700722 mFragment.set(mStateFragment.mDefault);
723 } else {
724 mFragment.set(pf);
725 }
Jason Samscfb1d112009-08-05 13:57:03 -0700726}
727
Jason Sams60709252010-11-17 15:29:32 -0800728void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800729 rsAssert(mIsGraphicsContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700730 if (pr == nullptr) {
Jason Sams5fd09d82009-09-23 13:57:02 -0700731 mRaster.set(mStateRaster.mDefault);
732 } else {
733 mRaster.set(pr);
734 }
735}
736
Jason Sams60709252010-11-17 15:29:32 -0800737void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800738 rsAssert(mIsGraphicsContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700739 if (pv == nullptr) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700740 mVertex.set(mStateVertex.mDefault);
741 } else {
742 mVertex.set(pv);
743 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700744}
745
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800746void Context::setFont(Font *f) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700747 rsAssert(mIsGraphicsContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700748 if (f == nullptr) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700749 mFont.set(mStateFont.mDefault);
750 } else {
751 mFont.set(f);
752 }
753}
Jason Sams93eacc72012-12-18 14:26:57 -0800754#endif
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700755
Jason Samsf2748272013-11-26 18:10:59 -0800756void Context::finish() {
757 if (mHal.funcs.finish) {
758 mHal.funcs.finish(this);
759 }
760}
761
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800762void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700763 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700764 obj->setName(name, len);
Chris Wailes93d6bc82014-07-28 16:54:38 -0700765 mNames.push_back(obj);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700766}
767
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800768void Context::removeName(ObjectBase *obj) {
Chris Wailes93d6bc82014-07-28 16:54:38 -0700769 for (auto nameIter = mNames.begin(), endIter = mNames.end();
770 nameIter != endIter; nameIter++) {
771
772 if (obj == *nameIter) {
773 mNames.erase(nameIter);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700774 return;
775 }
776 }
777}
778
Jason Sams1a4efa32011-05-17 15:01:29 -0700779RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) {
780 return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800781}
782
Jason Sams1a4efa32011-05-17 15:01:29 -0700783RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) {
784 return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen);
Jason Sams8c401ef2009-10-06 13:58:47 -0700785}
786
Jason Sams87319de2010-11-22 16:20:16 -0800787bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
788 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Sams1a4efa32011-05-17 15:01:29 -0700789
Jason Sams4961cce2013-04-11 16:11:46 -0700790 pthread_mutex_lock(&gMessageMutex);
791 bool ret = mIO.sendToClient(cmdID, subID, data, len, waitForSpace);
792 pthread_mutex_unlock(&gMessageMutex);
793 return ret;
Jason Sams8c401ef2009-10-06 13:58:47 -0700794}
795
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800796void Context::initToClient() {
797 while (!mRunning) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700798 usleep(100);
799 }
800}
801
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800802void Context::deinitToClient() {
Jason Sams1a4efa32011-05-17 15:01:29 -0700803 mIO.clientShutdown();
Jason Sams8c401ef2009-10-06 13:58:47 -0700804}
Jason Sams50869382009-08-18 17:07:09 -0700805
Jason Sams87319de2010-11-22 16:20:16 -0800806void Context::setError(RsError e, const char *msg) const {
Jason Samsa2cf7552010-03-03 13:03:18 -0800807 mError = e;
Jason Samsaad4bc52010-11-08 17:06:46 -0800808 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Samsa2cf7552010-03-03 13:03:18 -0800809}
810
811
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800812void Context::dumpDebug() const {
Steve Blockaf12ac62012-01-06 19:20:56 +0000813 ALOGE("RS Context debug %p", this);
814 ALOGE("RS Context debug");
Jason Sams13e26342009-11-24 12:26:35 -0800815
Steve Blockaf12ac62012-01-06 19:20:56 +0000816 ALOGE(" RS width %i, height %i", mWidth, mHeight);
817 ALOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
818 ALOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId);
Jason Sams13e26342009-11-24 12:26:35 -0800819}
Jason Samsa4a54e42009-06-10 18:39:40 -0700820
Jason Sams326e0dd2009-05-22 14:03:28 -0700821///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700822//
Jason Sams326e0dd2009-05-22 14:03:28 -0700823
824namespace android {
825namespace renderscript {
826
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800827void rsi_ContextFinish(Context *rsc) {
Jason Samsf2748272013-11-26 18:10:59 -0800828 rsc->finish();
Jason Sams8c880902010-06-15 12:15:57 -0700829}
Jason Sams326e0dd2009-05-22 14:03:28 -0700830
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800831void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Sams93eacc72012-12-18 14:26:57 -0800832#ifndef RS_COMPATIBILITY_LIB
Jason Sams326e0dd2009-05-22 14:03:28 -0700833 Script *s = static_cast<Script *>(vs);
834 rsc->setRootScript(s);
Jason Sams93eacc72012-12-18 14:26:57 -0800835#endif
Jason Sams326e0dd2009-05-22 14:03:28 -0700836}
837
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800838void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700839 Sampler *s = static_cast<Sampler *>(vs);
840
841 if (slot > RS_MAX_SAMPLER_SLOT) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000842 ALOGE("Invalid sampler slot");
Jason Sams326e0dd2009-05-22 14:03:28 -0700843 return;
844 }
845
846 s->bindToContext(&rsc->mStateSampler, slot);
847}
848
Jason Sams93eacc72012-12-18 14:26:57 -0800849#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800850void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Samsccc010b2010-05-13 18:30:11 -0700851 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams60709252010-11-17 15:29:32 -0800852 rsc->setProgramStore(pfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700853}
854
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800855void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700856 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Sams60709252010-11-17 15:29:32 -0800857 rsc->setProgramFragment(pf);
Jason Sams326e0dd2009-05-22 14:03:28 -0700858}
859
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800860void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Sams5fd09d82009-09-23 13:57:02 -0700861 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Sams60709252010-11-17 15:29:32 -0800862 rsc->setProgramRaster(pr);
Jason Sams5fd09d82009-09-23 13:57:02 -0700863}
864
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800865void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700866 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Sams60709252010-11-17 15:29:32 -0800867 rsc->setProgramVertex(pv);
Jason Sams326e0dd2009-05-22 14:03:28 -0700868}
869
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800870void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700871 Font *font = static_cast<Font *>(vfont);
872 rsc->setFont(font);
873}
Jason Sams93eacc72012-12-18 14:26:57 -0800874#endif
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700875
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700876void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700877 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700878 rsc->assignName(ob, name, name_length);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700879}
Jason Sams326e0dd2009-05-22 14:03:28 -0700880
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800881void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams2353ae32010-10-14 17:48:46 -0700882 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -0700883 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700884 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700885}
886
Jason Sams93eacc72012-12-18 14:26:57 -0800887#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800888void rsi_ContextPause(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700889 rsc->pause();
890}
891
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800892void rsi_ContextResume(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700893 rsc->resume();
894}
895
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700896void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
Mathias Agopianfa402862010-02-12 14:04:35 -0800897 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800898}
Jason Sams93eacc72012-12-18 14:26:57 -0800899#endif
Jason Sams613cad12009-11-12 15:10:25 -0800900
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800901void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800902 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800903}
904
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800905void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Samsc21cf402009-11-17 17:26:46 -0800906 ObjectBase::dumpAll(rsc);
907}
908
Jason Sams741aac92010-12-24 14:38:39 -0800909void rsi_ContextDestroyWorker(Context *rsc) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700910 rsc->destroyWorkerThreadResources();
Jason Sams741aac92010-12-24 14:38:39 -0800911}
912
Jason Samsc975cf42011-04-28 18:26:48 -0700913void rsi_ContextDestroy(Context *rsc) {
Jason Samsa36c50a2014-06-17 12:06:06 -0700914 //ALOGE("%p rsContextDestroy", rsc);
Jason Sams741aac92010-12-24 14:38:39 -0800915 rsContextDestroyWorker(rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800916 delete rsc;
Tim Murray20f62ca2013-05-09 11:57:35 -0700917 //ALOGV("%p rsContextDestroy done", rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800918}
919
Jason Samsc975cf42011-04-28 18:26:48 -0700920RsMessageToClientType rsi_ContextPeekMessage(Context *rsc,
Jason Sams186e5912011-04-26 14:50:00 -0700921 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700922 uint32_t * subID, size_t subID_length) {
923 return rsc->peekMessageToClient(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800924}
925
Jason Samsc975cf42011-04-28 18:26:48 -0700926RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length,
Jason Sams186e5912011-04-26 14:50:00 -0700927 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700928 uint32_t * subID, size_t subID_length) {
Jason Sams186e5912011-04-26 14:50:00 -0700929 rsAssert(subID_length == sizeof(uint32_t));
930 rsAssert(receiveLen_length == sizeof(size_t));
Jason Sams1a4efa32011-05-17 15:01:29 -0700931 return rsc->getMessageToClient(data, receiveLen, subID, data_length);
Jason Sams8c401ef2009-10-06 13:58:47 -0700932}
933
Jason Samsc975cf42011-04-28 18:26:48 -0700934void rsi_ContextInitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700935 rsc->initToClient();
936}
937
Jason Samsc975cf42011-04-28 18:26:48 -0700938void rsi_ContextDeinitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700939 rsc->deinitToClient();
940}
941
Jason Sams70265202013-02-05 19:20:47 -0800942void rsi_ContextSendMessage(Context *rsc, uint32_t id, const uint8_t *data, size_t len) {
943 rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, id, len, true);
944}
945
Tim Murray397b0b12014-01-07 15:35:08 -0800946// implementation of handcode LF_ObjDestroy
947// required so nObjDestroy can be run from finalizer without blocking
948void LF_ObjDestroy_handcode(const Context *rsc, RsAsyncVoidPtr objPtr) {
949 if (((Context *)rsc)->isSynchronous()) {
950 rsi_ObjDestroy((Context *)rsc, objPtr);
951 return;
952 }
953
954 // struct has two parts:
955 // RsPlaybackRemoteHeader (cmdID and bytes)
956 // RS_CMD_ObjDestroy (ptr)
957 struct destroyCmd {
958 uint32_t cmdID;
959 uint32_t bytes;
960 RsAsyncVoidPtr ptr;
961 };
962
963 destroyCmd cmd;
964 cmd.cmdID = RS_CMD_ID_ObjDestroy;
965 cmd.bytes = sizeof(RsAsyncVoidPtr);
966 cmd.ptr = objPtr;
967 ThreadIO *io = &((Context *)rsc)->mIO;
968 io->coreWrite((void*)&cmd, sizeof(destroyCmd));
969
970}
971
Jason Samsc975cf42011-04-28 18:26:48 -0700972}
973}
974
Tim Murrayc2ce7072013-07-17 18:38:53 -0700975extern "C" RsContext rsContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion,
Tim Murray84e3dea2013-09-09 16:12:51 -0700976 RsContextType ct, uint32_t flags) {
Tim Murray20f62ca2013-05-09 11:57:35 -0700977 //ALOGV("rsContextCreate dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700978 Device * dev = static_cast<Device *>(vdev);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700979 Context *rsc = Context::createContext(dev, nullptr, ct, flags);
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700980 if (rsc) {
981 rsc->setTargetSdkVersion(sdkVersion);
982 }
Jason Sams789ca832011-05-18 17:36:02 -0700983 return rsc;
984}
985
Jason Sams93eacc72012-12-18 14:26:57 -0800986#ifndef RS_COMPATIBILITY_LIB
Jason Sams789ca832011-05-18 17:36:02 -0700987RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700988 uint32_t sdkVersion, RsSurfaceConfig sc,
989 uint32_t dpi) {
Tim Murray20f62ca2013-05-09 11:57:35 -0700990 //ALOGV("rsContextCreateGL dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700991 Device * dev = static_cast<Device *>(vdev);
992 Context *rsc = Context::createContext(dev, &sc);
Jason Sams9544f762011-07-13 16:09:42 -0700993 if (rsc) {
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700994 rsc->setTargetSdkVersion(sdkVersion);
Jason Sams9544f762011-07-13 16:09:42 -0700995 rsc->setDPI(dpi);
996 }
Tim Murray20f62ca2013-05-09 11:57:35 -0700997 //ALOGV("%p rsContextCreateGL ret", rsc);
Jason Sams789ca832011-05-18 17:36:02 -0700998 return rsc;
999}
Jason Sams93eacc72012-12-18 14:26:57 -08001000#endif
Jason Sams789ca832011-05-18 17:36:02 -07001001
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -07001002// Only to be called at a3d load time, before object is visible to user
1003// not thread safe
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08001004void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -07001005 ObjectBase *ob = static_cast<ObjectBase *>(obj);
1006 (*name) = ob->getName();
1007}