blob: e7ff33de5760d71476196d954ca0fa5eb9583985 [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);
292#ifndef ANDROID_RS_SERIALIZE
293 rsc->mNativeThreadId = gettid();
Jason Sams93eacc72012-12-18 14:26:57 -0800294#ifndef RS_COMPATIBILITY_LIB
Tim Murray4d252d62012-11-29 14:37:59 -0800295 if (!rsc->isSynchronous()) {
296 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
297 }
Tim Murray0c66f072012-10-23 14:05:02 -0700298 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams93eacc72012-12-18 14:26:57 -0800299#else
300 if (!rsc->isSynchronous()) {
301 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, -4);
302 }
303 rsc->mThreadPriority = -4;
304#endif
Tim Murray0c66f072012-10-23 14:05:02 -0700305#endif //ANDROID_RS_SERIALIZE
306 rsc->props.mLogTimes = getProp("debug.rs.profile") != 0;
307 rsc->props.mLogScripts = getProp("debug.rs.script") != 0;
308 rsc->props.mLogObjects = getProp("debug.rs.object") != 0;
309 rsc->props.mLogShaders = getProp("debug.rs.shader") != 0;
310 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes") != 0;
311 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms") != 0;
312 rsc->props.mLogVisual = getProp("debug.rs.visual") != 0;
313 rsc->props.mDebugMaxThreads = getProp("debug.rs.max-threads");
314
Jason Sams2d8030a2014-07-24 18:47:34 -0700315 if (getProp("debug.rs.debug") != 0) {
316 ALOGD("Forcing debug context due to debug.rs.debug.");
317 rsc->mContextType = RS_CONTEXT_TYPE_DEBUG;
318 }
319
Tim Murray0c66f072012-10-23 14:05:02 -0700320 bool loadDefault = true;
321
322 // Provide a mechanism for dropping in a different RS driver.
Jason Sams110f1812013-03-14 16:02:18 -0700323#ifndef RS_COMPATIBILITY_LIB
Tim Murray0c66f072012-10-23 14:05:02 -0700324#ifdef OVERRIDE_RS_DRIVER
325#define XSTR(S) #S
326#define STR(S) XSTR(S)
327#define OVERRIDE_RS_DRIVER_STRING STR(OVERRIDE_RS_DRIVER)
328
329 if (getProp("debug.rs.default-CPU-driver") != 0) {
Stephen Hines43463922013-11-14 13:57:50 -0800330 ALOGD("Skipping override driver and loading default CPU driver");
Jason Samscd5f1ce2014-03-31 14:44:53 -0700331 } else if (rsc->mForceCpu || rsc->mIsGraphicsContext) {
Tim Murray0e92fa32012-11-06 14:36:38 -0800332 ALOGV("Application requested CPU execution");
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700333 } else if (rsc->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
334 ALOGV("Application requested debug context");
Tim Murray0c66f072012-10-23 14:05:02 -0700335 } else {
Tim Murray6e6cdbc2014-09-10 15:58:54 -0700336#if defined(__LP64__) && defined(DISABLE_RS_64_BIT_DRIVER)
337 // skip load
338#else
Tim Murray0c66f072012-10-23 14:05:02 -0700339 if (loadRuntime(OVERRIDE_RS_DRIVER_STRING, rsc)) {
Stephen Hines43463922013-11-14 13:57:50 -0800340 ALOGV("Successfully loaded runtime: %s", OVERRIDE_RS_DRIVER_STRING);
Tim Murray0c66f072012-10-23 14:05:02 -0700341 loadDefault = false;
342 } else {
343 ALOGE("Failed to load runtime %s, loading default", OVERRIDE_RS_DRIVER_STRING);
344 }
Tim Murray6e6cdbc2014-09-10 15:58:54 -0700345#endif
Tim Murray0c66f072012-10-23 14:05:02 -0700346 }
347
348#undef XSTR
349#undef STR
350#endif // OVERRIDE_RS_DRIVER
351
352 if (loadDefault) {
353 if (!loadRuntime("libRSDriver.so", rsc)) {
354 ALOGE("Failed to load default runtime!");
Stephen Hines6f01bcf2012-11-19 15:18:16 -0800355 rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed loading RS driver");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700356 return nullptr;
Tim Murray0c66f072012-10-23 14:05:02 -0700357 }
358 }
Jason Sams110f1812013-03-14 16:02:18 -0700359#else // RS_COMPATIBILITY_LIB
360 if (rsdHalInit(rsc, 0, 0) != true) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700361 return nullptr;
Jason Sams110f1812013-03-14 16:02:18 -0700362 }
363#endif
364
Tim Murray0c66f072012-10-23 14:05:02 -0700365
Jason Sams83c451a2011-04-21 11:46:50 -0700366 rsc->mHal.funcs.setPriority(rsc, rsc->mThreadPriority);
Jason Samse5769102009-06-19 16:03:18 -0700367
Jason Sams93eacc72012-12-18 14:26:57 -0800368#ifndef RS_COMPATIBILITY_LIB
Jason Sams83c451a2011-04-21 11:46:50 -0700369 if (rsc->mIsGraphicsContext) {
Jason Samsd3e71072011-05-03 15:01:58 -0700370 if (!rsc->initGLThread()) {
371 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700372 return nullptr;
Jason Samsd3e71072011-05-03 15:01:58 -0700373 }
374
Jason Sams83c451a2011-04-21 11:46:50 -0700375 rsc->mStateRaster.init(rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700376 rsc->setProgramRaster(nullptr);
Jason Sams83c451a2011-04-21 11:46:50 -0700377 rsc->mStateVertex.init(rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700378 rsc->setProgramVertex(nullptr);
Jason Sams83c451a2011-04-21 11:46:50 -0700379 rsc->mStateFragment.init(rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700380 rsc->setProgramFragment(nullptr);
Jason Sams83c451a2011-04-21 11:46:50 -0700381 rsc->mStateFragmentStore.init(rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700382 rsc->setProgramStore(nullptr);
Jason Sams83c451a2011-04-21 11:46:50 -0700383 rsc->mStateFont.init(rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700384 rsc->setFont(nullptr);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700385 rsc->mStateSampler.init(rsc);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700386 rsc->mFBOCache.init(rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700387 }
Jason Sams93eacc72012-12-18 14:26:57 -0800388#endif
Jason Sams8ce125b2009-06-17 16:52:59 -0700389
Jason Sams83c451a2011-04-21 11:46:50 -0700390 rsc->mRunning = true;
Tim Murray4d252d62012-11-29 14:37:59 -0800391
392 if (rsc->isSynchronous()) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700393 return nullptr;
Tim Murray4d252d62012-11-29 14:37:59 -0800394 }
395
Jason Sams5f27d6f2012-02-07 15:32:08 -0800396 if (!rsc->mIsGraphicsContext) {
397 while (!rsc->mExit) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800398 rsc->mIO.playCoreCommands(rsc, -1);
Jason Samse0aab4a2011-08-12 15:05:15 -0700399 }
Jason Sams93eacc72012-12-18 14:26:57 -0800400#ifndef RS_COMPATIBILITY_LIB
Jason Sams5f27d6f2012-02-07 15:32:08 -0800401 } else {
402#ifndef ANDROID_RS_SERIALIZE
403 DisplayEventReceiver displayEvent;
404 DisplayEventReceiver::Event eventBuffer[1];
405#endif
406 int vsyncRate = 0;
407 int targetRate = 0;
Jason Samse0aab4a2011-08-12 15:05:15 -0700408
Jason Sams5f27d6f2012-02-07 15:32:08 -0800409 bool drawOnce = false;
410 while (!rsc->mExit) {
411 rsc->timerSet(RS_TIMER_IDLE);
Jason Sams326e0dd2009-05-22 14:03:28 -0700412
Jason Sams5f27d6f2012-02-07 15:32:08 -0800413#ifndef ANDROID_RS_SERIALIZE
Jason Sams9afd9a52012-02-17 16:59:50 -0800414 if (!rsc->mRootScript.get() || !rsc->mHasSurface || rsc->mPaused) {
415 targetRate = 0;
416 }
417
Jason Sams5f27d6f2012-02-07 15:32:08 -0800418 if (vsyncRate != targetRate) {
419 displayEvent.setVsyncRate(targetRate);
420 vsyncRate = targetRate;
421 }
422 if (targetRate) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800423 drawOnce |= rsc->mIO.playCoreCommands(rsc, displayEvent.getFd());
Jason Sams5f27d6f2012-02-07 15:32:08 -0800424 while (displayEvent.getEvents(eventBuffer, 1) != 0) {
425 //ALOGE("vs2 time past %lld", (rsc->getTime() - eventBuffer[0].header.timestamp) / 1000000);
426 }
427 } else
428#endif
429 {
Jason Sams963a2fb2012-02-09 14:36:14 -0800430 drawOnce |= rsc->mIO.playCoreCommands(rsc, -1);
Jason Sams83c451a2011-04-21 11:46:50 -0700431 }
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700432
Chris Wailes44bef6f2014-08-12 13:51:10 -0700433 if ((rsc->mRootScript.get() != nullptr) && rsc->mHasSurface &&
Jason Sams5f27d6f2012-02-07 15:32:08 -0800434 (targetRate || drawOnce) && !rsc->mPaused) {
435
436 drawOnce = false;
437 targetRate = ((rsc->runRootScript() + 15) / 16);
438
439 if (rsc->props.mLogVisual) {
440 rsc->displayDebugStats();
441 }
442
443 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
444 rsc->mHal.funcs.swap(rsc);
445 rsc->timerFrame();
446 rsc->timerSet(RS_TIMER_INTERNAL);
447 rsc->timerPrint();
448 rsc->timerReset();
449 }
Jason Sams83c451a2011-04-21 11:46:50 -0700450 }
Jason Sams93eacc72012-12-18 14:26:57 -0800451#endif
Jason Sams83c451a2011-04-21 11:46:50 -0700452 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700453
Tim Murray20f62ca2013-05-09 11:57:35 -0700454 //ALOGV("%p RS Thread exiting", rsc);
Jason Samse514b452009-09-25 14:51:22 -0700455
Jason Sams93eacc72012-12-18 14:26:57 -0800456#ifndef RS_COMPATIBILITY_LIB
Jason Sams83c451a2011-04-21 11:46:50 -0700457 if (rsc->mIsGraphicsContext) {
458 pthread_mutex_lock(&gInitMutex);
459 rsc->deinitEGL();
460 pthread_mutex_unlock(&gInitMutex);
461 }
Jason Sams93eacc72012-12-18 14:26:57 -0800462#endif
Jason Sams33b6e3b2009-10-27 14:44:31 -0700463
Tim Murray20f62ca2013-05-09 11:57:35 -0700464 //ALOGV("%p RS Thread exited", rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700465 return nullptr;
Jason Sams326e0dd2009-05-22 14:03:28 -0700466}
467
Jason Sams741aac92010-12-24 14:38:39 -0800468void Context::destroyWorkerThreadResources() {
Steve Block65982012011-10-20 11:56:00 +0100469 //ALOGV("destroyWorkerThreadResources 1");
Jason Sams2e8665d2011-01-27 00:14:13 -0800470 ObjectBase::zeroAllUserRef(this);
Jason Sams93eacc72012-12-18 14:26:57 -0800471#ifndef RS_COMPATIBILITY_LIB
Jason Sams741aac92010-12-24 14:38:39 -0800472 if (mIsGraphicsContext) {
473 mRaster.clear();
474 mFragment.clear();
475 mVertex.clear();
476 mFragmentStore.clear();
477 mFont.clear();
478 mRootScript.clear();
479 mStateRaster.deinit(this);
480 mStateVertex.deinit(this);
481 mStateFragment.deinit(this);
482 mStateFragmentStore.deinit(this);
483 mStateFont.deinit(this);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700484 mStateSampler.deinit(this);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700485 mFBOCache.deinit(this);
Jason Sams741aac92010-12-24 14:38:39 -0800486 }
Jason Sams93eacc72012-12-18 14:26:57 -0800487#endif
Jason Samsc7cec1e2011-08-18 18:01:33 -0700488 ObjectBase::freeAllChildren(this);
Jason Samscf912de2011-01-09 16:09:51 -0800489 mExit = true;
Jason Sams5f27d6f2012-02-07 15:32:08 -0800490 //ALOGV("destroyWorkerThreadResources 2");
Jason Sams741aac92010-12-24 14:38:39 -0800491}
492
Jason Sams2382aba2011-09-13 15:41:01 -0700493void Context::printWatchdogInfo(void *ctx) {
494 Context *rsc = (Context *)ctx;
Jason Samsee803442011-10-13 16:05:27 -0700495 if (rsc->watchdog.command && rsc->watchdog.file) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000496 ALOGE("RS watchdog timeout: %i %s line %i %s", rsc->watchdog.inRoot,
Jason Samsee803442011-10-13 16:05:27 -0700497 rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file);
498 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000499 ALOGE("RS watchdog timeout: %i", rsc->watchdog.inRoot);
Jason Samsee803442011-10-13 16:05:27 -0700500 }
Jason Sams2382aba2011-09-13 15:41:01 -0700501}
502
503
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800504void Context::setPriority(int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800505 // Note: If we put this in the proper "background" policy
506 // the wallpapers can become completly unresponsive at times.
507 // This is probably not what we want for something the user is actively
508 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800509 mThreadPriority = p;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700510 setpriority(PRIO_PROCESS, mNativeThreadId, p);
Jason Sams9719bd42012-01-12 14:22:21 -0800511 mHal.funcs.setPriority(this, mThreadPriority);
Jason Sams15832442009-11-15 12:14:26 -0800512}
513
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800514Context::Context() {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700515 mDev = nullptr;
Jason Sams326e0dd2009-05-22 14:03:28 -0700516 mRunning = false;
517 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700518 mPaused = false;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700519 mObjHead = nullptr;
Jason Samsa2cf7552010-03-03 13:03:18 -0800520 mError = RS_ERROR_NONE;
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700521 mTargetSdkVersion = 14;
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700522 mDPI = 96;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700523 mIsContextLite = false;
Stephen Hines86c6b5f2011-10-31 14:07:54 -0700524 memset(&watchdog, 0, sizeof(watchdog));
Jason Samsf2748272013-11-26 18:10:59 -0800525 memset(&mHal, 0, sizeof(mHal));
Tim Murray0e92fa32012-11-06 14:36:38 -0800526 mForceCpu = false;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700527 mContextType = RS_CONTEXT_TYPE_NORMAL;
Tim Murray4d252d62012-11-29 14:37:59 -0800528 mSynchronous = false;
Tim Murray0e92fa32012-11-06 14:36:38 -0800529}
530
531Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc,
Tim Murray84e3dea2013-09-09 16:12:51 -0700532 RsContextType ct, uint32_t flags) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700533 Context * rsc = new Context();
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700534
Tim Murray84e3dea2013-09-09 16:12:51 -0700535 if (flags & RS_CONTEXT_LOW_LATENCY) {
536 rsc->mForceCpu = true;
537 }
538 if (flags & RS_CONTEXT_SYNCHRONOUS) {
539 rsc->mSynchronous = true;
540 }
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700541 rsc->mContextType = ct;
Jason Sams416f4b62014-06-25 15:45:36 -0700542 rsc->mHal.flags = flags;
Tim Murray0e92fa32012-11-06 14:36:38 -0800543
Jason Sams5c1c79a2010-11-03 14:27:11 -0700544 if (!rsc->initContext(dev, sc)) {
545 delete rsc;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700546 return nullptr;
Jason Sams5c1c79a2010-11-03 14:27:11 -0700547 }
548 return rsc;
549}
550
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700551Context * Context::createContextLite() {
552 Context * rsc = new Context();
553 rsc->mIsContextLite = true;
554 return rsc;
555}
556
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800557bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700558 pthread_mutex_lock(&gInitMutex);
559
Jason Sams1a4efa32011-05-17 15:01:29 -0700560 mIO.init();
Jason Sams5f27d6f2012-02-07 15:32:08 -0800561 mIO.setTimeoutCallback(printWatchdogInfo, this, 2e9);
Jason Sams1a4efa32011-05-17 15:01:29 -0700562
Jason Sams5c1c79a2010-11-03 14:27:11 -0700563 dev->addContext(this);
564 mDev = dev;
Jason Sams6b8552a2010-10-13 15:31:10 -0700565 if (sc) {
566 mUserSurfaceConfig = *sc;
567 } else {
568 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
569 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800570
Chris Wailes44bef6f2014-08-12 13:51:10 -0700571 mIsGraphicsContext = sc != nullptr;
Jason Sams326e0dd2009-05-22 14:03:28 -0700572
Jason Samsa658e902009-06-04 14:35:01 -0700573 int status;
574 pthread_attr_t threadAttr;
575
Jason Samsfb03a222009-10-15 16:47:31 -0700576 pthread_mutex_unlock(&gInitMutex);
577
578 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700579
Jason Samsa658e902009-06-04 14:35:01 -0700580 status = pthread_attr_init(&threadAttr);
581 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000582 ALOGE("Failed to init thread attribute.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700583 return false;
Jason Samsa658e902009-06-04 14:35:01 -0700584 }
585
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700586 mHasSurface = false;
Jason Sams992a0b72009-06-23 12:22:47 -0700587
Jason Sams24371d92009-08-19 12:17:14 -0700588 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700589 timerSet(RS_TIMER_INTERNAL);
Tim Murray4d252d62012-11-29 14:37:59 -0800590 if (mSynchronous) {
591 threadProc(this);
Xiaofei Wanfea96e82014-03-31 16:43:15 +0800592
593 if (mError != RS_ERROR_NONE) {
594 ALOGE("Errors during thread init (sync mode)");
595 return false;
596 }
Tim Murray4d252d62012-11-29 14:37:59 -0800597 } else {
598 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
599 if (status) {
600 ALOGE("Failed to start rs context thread.");
601 return false;
602 }
603 while (!mRunning && (mError == RS_ERROR_NONE)) {
604 usleep(100);
605 }
Jason Sams50869382009-08-18 17:07:09 -0700606
Tim Murray4d252d62012-11-29 14:37:59 -0800607 if (mError != RS_ERROR_NONE) {
608 ALOGE("Errors during thread init");
609 return false;
610 }
Jason Sams18133402010-07-20 15:09:00 -0700611
Tim Murray4d252d62012-11-29 14:37:59 -0800612 pthread_attr_destroy(&threadAttr);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700613 }
Jason Sams5c1c79a2010-11-03 14:27:11 -0700614 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700615}
616
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800617Context::~Context() {
Tim Murray20f62ca2013-05-09 11:57:35 -0700618 //ALOGV("%p Context::~Context", this);
Jason Samscf912de2011-01-09 16:09:51 -0800619
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700620 if (!mIsContextLite) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700621 mPaused = false;
622 void *res;
Jason Sams326e0dd2009-05-22 14:03:28 -0700623
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700624 mIO.shutdown();
Sangkyu Leebcba6f62014-04-01 15:22:13 +0900625 if (!mSynchronous) {
626 pthread_join(mThreadId, &res);
627 }
Jason Sams5f27d6f2012-02-07 15:32:08 -0800628 rsAssert(mExit);
Jason Sams326e0dd2009-05-22 14:03:28 -0700629
Xiaofei Wanfea96e82014-03-31 16:43:15 +0800630 if (mHal.funcs.shutdownDriver && mHal.drv) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700631 mHal.funcs.shutdownDriver(this);
632 }
633
634 // Global structure cleanup.
635 pthread_mutex_lock(&gInitMutex);
636 if (mDev) {
637 mDev->removeContext(this);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700638 mDev = nullptr;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700639 }
640 pthread_mutex_unlock(&gInitMutex);
Jason Sams51462c52011-01-25 00:26:25 -0800641 }
Tim Murray20f62ca2013-05-09 11:57:35 -0700642 //ALOGV("%p Context::~Context done", this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700643}
644
Jason Sams93eacc72012-12-18 14:26:57 -0800645#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700646void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800647 rsAssert(mIsGraphicsContext);
Jason Sams4b3de472011-04-06 17:52:23 -0700648 mHal.funcs.setSurface(this, w, h, sur);
Jason Sams458f2dc2009-11-03 13:58:36 -0800649
Chris Wailes44bef6f2014-08-12 13:51:10 -0700650 mHasSurface = sur != nullptr;
Jason Sams4b3de472011-04-06 17:52:23 -0700651 mWidth = w;
652 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800653
Jason Sams4b3de472011-04-06 17:52:23 -0700654 if (mWidth && mHeight) {
Jason Sams771565f2010-05-14 15:30:29 -0700655 mStateVertex.updateSize(this);
Alex Sakhartchouka544b632011-07-19 17:50:29 -0700656 mFBOCache.updateSize();
Jason Sams458f2dc2009-11-03 13:58:36 -0800657 }
658}
659
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800660uint32_t Context::getCurrentSurfaceWidth() const {
661 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700662 if (mFBOCache.mHal.state.colorTargets[i] != nullptr) {
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800663 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimX();
664 }
665 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700666 if (mFBOCache.mHal.state.depthTarget != nullptr) {
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800667 return mFBOCache.mHal.state.depthTarget->getType()->getDimX();
668 }
669 return mWidth;
670}
671
672uint32_t Context::getCurrentSurfaceHeight() const {
673 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700674 if (mFBOCache.mHal.state.colorTargets[i] != nullptr) {
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800675 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimY();
676 }
677 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700678 if (mFBOCache.mHal.state.depthTarget != nullptr) {
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800679 return mFBOCache.mHal.state.depthTarget->getType()->getDimY();
680 }
681 return mHeight;
682}
683
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800684void Context::pause() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800685 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700686 mPaused = true;
687}
688
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800689void Context::resume() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800690 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700691 mPaused = false;
692}
693
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800694void Context::setRootScript(Script *s) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800695 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700696 mRootScript.set(s);
697}
698
Jason Sams60709252010-11-17 15:29:32 -0800699void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800700 rsAssert(mIsGraphicsContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700701 if (pfs == nullptr) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700702 mFragmentStore.set(mStateFragmentStore.mDefault);
703 } else {
704 mFragmentStore.set(pfs);
705 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700706}
707
Jason Sams60709252010-11-17 15:29:32 -0800708void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800709 rsAssert(mIsGraphicsContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700710 if (pf == nullptr) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700711 mFragment.set(mStateFragment.mDefault);
712 } else {
713 mFragment.set(pf);
714 }
Jason Samscfb1d112009-08-05 13:57:03 -0700715}
716
Jason Sams60709252010-11-17 15:29:32 -0800717void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800718 rsAssert(mIsGraphicsContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700719 if (pr == nullptr) {
Jason Sams5fd09d82009-09-23 13:57:02 -0700720 mRaster.set(mStateRaster.mDefault);
721 } else {
722 mRaster.set(pr);
723 }
724}
725
Jason Sams60709252010-11-17 15:29:32 -0800726void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800727 rsAssert(mIsGraphicsContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700728 if (pv == nullptr) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700729 mVertex.set(mStateVertex.mDefault);
730 } else {
731 mVertex.set(pv);
732 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700733}
734
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800735void Context::setFont(Font *f) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700736 rsAssert(mIsGraphicsContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700737 if (f == nullptr) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700738 mFont.set(mStateFont.mDefault);
739 } else {
740 mFont.set(f);
741 }
742}
Jason Sams93eacc72012-12-18 14:26:57 -0800743#endif
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700744
Jason Samsf2748272013-11-26 18:10:59 -0800745void Context::finish() {
746 if (mHal.funcs.finish) {
747 mHal.funcs.finish(this);
748 }
749}
750
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800751void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700752 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700753 obj->setName(name, len);
Chris Wailes93d6bc82014-07-28 16:54:38 -0700754 mNames.push_back(obj);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700755}
756
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800757void Context::removeName(ObjectBase *obj) {
Chris Wailes93d6bc82014-07-28 16:54:38 -0700758 for (auto nameIter = mNames.begin(), endIter = mNames.end();
759 nameIter != endIter; nameIter++) {
760
761 if (obj == *nameIter) {
762 mNames.erase(nameIter);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700763 return;
764 }
765 }
766}
767
Jason Sams1a4efa32011-05-17 15:01:29 -0700768RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) {
769 return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800770}
771
Jason Sams1a4efa32011-05-17 15:01:29 -0700772RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) {
773 return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen);
Jason Sams8c401ef2009-10-06 13:58:47 -0700774}
775
Jason Sams87319de2010-11-22 16:20:16 -0800776bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
777 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Sams1a4efa32011-05-17 15:01:29 -0700778
Jason Sams4961cce2013-04-11 16:11:46 -0700779 pthread_mutex_lock(&gMessageMutex);
780 bool ret = mIO.sendToClient(cmdID, subID, data, len, waitForSpace);
781 pthread_mutex_unlock(&gMessageMutex);
782 return ret;
Jason Sams8c401ef2009-10-06 13:58:47 -0700783}
784
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800785void Context::initToClient() {
786 while (!mRunning) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700787 usleep(100);
788 }
789}
790
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800791void Context::deinitToClient() {
Jason Sams1a4efa32011-05-17 15:01:29 -0700792 mIO.clientShutdown();
Jason Sams8c401ef2009-10-06 13:58:47 -0700793}
Jason Sams50869382009-08-18 17:07:09 -0700794
Jason Sams87319de2010-11-22 16:20:16 -0800795void Context::setError(RsError e, const char *msg) const {
Jason Samsa2cf7552010-03-03 13:03:18 -0800796 mError = e;
Jason Samsaad4bc52010-11-08 17:06:46 -0800797 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Samsa2cf7552010-03-03 13:03:18 -0800798}
799
800
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800801void Context::dumpDebug() const {
Steve Blockaf12ac62012-01-06 19:20:56 +0000802 ALOGE("RS Context debug %p", this);
803 ALOGE("RS Context debug");
Jason Sams13e26342009-11-24 12:26:35 -0800804
Steve Blockaf12ac62012-01-06 19:20:56 +0000805 ALOGE(" RS width %i, height %i", mWidth, mHeight);
806 ALOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
807 ALOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId);
Jason Sams13e26342009-11-24 12:26:35 -0800808}
Jason Samsa4a54e42009-06-10 18:39:40 -0700809
Jason Sams326e0dd2009-05-22 14:03:28 -0700810///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700811//
Jason Sams326e0dd2009-05-22 14:03:28 -0700812
813namespace android {
814namespace renderscript {
815
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800816void rsi_ContextFinish(Context *rsc) {
Jason Samsf2748272013-11-26 18:10:59 -0800817 rsc->finish();
Jason Sams8c880902010-06-15 12:15:57 -0700818}
Jason Sams326e0dd2009-05-22 14:03:28 -0700819
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800820void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Sams93eacc72012-12-18 14:26:57 -0800821#ifndef RS_COMPATIBILITY_LIB
Jason Sams326e0dd2009-05-22 14:03:28 -0700822 Script *s = static_cast<Script *>(vs);
823 rsc->setRootScript(s);
Jason Sams93eacc72012-12-18 14:26:57 -0800824#endif
Jason Sams326e0dd2009-05-22 14:03:28 -0700825}
826
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800827void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700828 Sampler *s = static_cast<Sampler *>(vs);
829
830 if (slot > RS_MAX_SAMPLER_SLOT) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000831 ALOGE("Invalid sampler slot");
Jason Sams326e0dd2009-05-22 14:03:28 -0700832 return;
833 }
834
835 s->bindToContext(&rsc->mStateSampler, slot);
836}
837
Jason Sams93eacc72012-12-18 14:26:57 -0800838#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800839void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Samsccc010b2010-05-13 18:30:11 -0700840 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams60709252010-11-17 15:29:32 -0800841 rsc->setProgramStore(pfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700842}
843
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800844void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700845 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Sams60709252010-11-17 15:29:32 -0800846 rsc->setProgramFragment(pf);
Jason Sams326e0dd2009-05-22 14:03:28 -0700847}
848
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800849void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Sams5fd09d82009-09-23 13:57:02 -0700850 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Sams60709252010-11-17 15:29:32 -0800851 rsc->setProgramRaster(pr);
Jason Sams5fd09d82009-09-23 13:57:02 -0700852}
853
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800854void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700855 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Sams60709252010-11-17 15:29:32 -0800856 rsc->setProgramVertex(pv);
Jason Sams326e0dd2009-05-22 14:03:28 -0700857}
858
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800859void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700860 Font *font = static_cast<Font *>(vfont);
861 rsc->setFont(font);
862}
Jason Sams93eacc72012-12-18 14:26:57 -0800863#endif
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700864
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700865void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700866 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700867 rsc->assignName(ob, name, name_length);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700868}
Jason Sams326e0dd2009-05-22 14:03:28 -0700869
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800870void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams2353ae32010-10-14 17:48:46 -0700871 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -0700872 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700873 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700874}
875
Jason Sams93eacc72012-12-18 14:26:57 -0800876#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800877void rsi_ContextPause(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700878 rsc->pause();
879}
880
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800881void rsi_ContextResume(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700882 rsc->resume();
883}
884
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700885void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
Mathias Agopianfa402862010-02-12 14:04:35 -0800886 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800887}
Jason Sams93eacc72012-12-18 14:26:57 -0800888#endif
Jason Sams613cad12009-11-12 15:10:25 -0800889
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800890void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800891 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800892}
893
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800894void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Samsc21cf402009-11-17 17:26:46 -0800895 ObjectBase::dumpAll(rsc);
896}
897
Jason Sams741aac92010-12-24 14:38:39 -0800898void rsi_ContextDestroyWorker(Context *rsc) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700899 rsc->destroyWorkerThreadResources();
Jason Sams741aac92010-12-24 14:38:39 -0800900}
901
Jason Samsc975cf42011-04-28 18:26:48 -0700902void rsi_ContextDestroy(Context *rsc) {
Jason Samsa36c50a2014-06-17 12:06:06 -0700903 //ALOGE("%p rsContextDestroy", rsc);
Jason Sams741aac92010-12-24 14:38:39 -0800904 rsContextDestroyWorker(rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800905 delete rsc;
Tim Murray20f62ca2013-05-09 11:57:35 -0700906 //ALOGV("%p rsContextDestroy done", rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800907}
908
Jason Samsc975cf42011-04-28 18:26:48 -0700909RsMessageToClientType rsi_ContextPeekMessage(Context *rsc,
Jason Sams186e5912011-04-26 14:50:00 -0700910 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700911 uint32_t * subID, size_t subID_length) {
912 return rsc->peekMessageToClient(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800913}
914
Jason Samsc975cf42011-04-28 18:26:48 -0700915RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length,
Jason Sams186e5912011-04-26 14:50:00 -0700916 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700917 uint32_t * subID, size_t subID_length) {
Jason Sams186e5912011-04-26 14:50:00 -0700918 rsAssert(subID_length == sizeof(uint32_t));
919 rsAssert(receiveLen_length == sizeof(size_t));
Jason Sams1a4efa32011-05-17 15:01:29 -0700920 return rsc->getMessageToClient(data, receiveLen, subID, data_length);
Jason Sams8c401ef2009-10-06 13:58:47 -0700921}
922
Jason Samsc975cf42011-04-28 18:26:48 -0700923void rsi_ContextInitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700924 rsc->initToClient();
925}
926
Jason Samsc975cf42011-04-28 18:26:48 -0700927void rsi_ContextDeinitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700928 rsc->deinitToClient();
929}
930
Jason Sams70265202013-02-05 19:20:47 -0800931void rsi_ContextSendMessage(Context *rsc, uint32_t id, const uint8_t *data, size_t len) {
932 rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, id, len, true);
933}
934
Tim Murray397b0b12014-01-07 15:35:08 -0800935// implementation of handcode LF_ObjDestroy
936// required so nObjDestroy can be run from finalizer without blocking
937void LF_ObjDestroy_handcode(const Context *rsc, RsAsyncVoidPtr objPtr) {
938 if (((Context *)rsc)->isSynchronous()) {
939 rsi_ObjDestroy((Context *)rsc, objPtr);
940 return;
941 }
942
943 // struct has two parts:
944 // RsPlaybackRemoteHeader (cmdID and bytes)
945 // RS_CMD_ObjDestroy (ptr)
946 struct destroyCmd {
947 uint32_t cmdID;
948 uint32_t bytes;
949 RsAsyncVoidPtr ptr;
950 };
951
952 destroyCmd cmd;
953 cmd.cmdID = RS_CMD_ID_ObjDestroy;
954 cmd.bytes = sizeof(RsAsyncVoidPtr);
955 cmd.ptr = objPtr;
956 ThreadIO *io = &((Context *)rsc)->mIO;
957 io->coreWrite((void*)&cmd, sizeof(destroyCmd));
958
959}
960
Jason Samsc975cf42011-04-28 18:26:48 -0700961}
962}
963
Tim Murrayc2ce7072013-07-17 18:38:53 -0700964extern "C" RsContext rsContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion,
Tim Murray84e3dea2013-09-09 16:12:51 -0700965 RsContextType ct, uint32_t flags) {
Tim Murray20f62ca2013-05-09 11:57:35 -0700966 //ALOGV("rsContextCreate dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700967 Device * dev = static_cast<Device *>(vdev);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700968 Context *rsc = Context::createContext(dev, nullptr, ct, flags);
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700969 if (rsc) {
970 rsc->setTargetSdkVersion(sdkVersion);
971 }
Jason Sams789ca832011-05-18 17:36:02 -0700972 return rsc;
973}
974
Jason Sams93eacc72012-12-18 14:26:57 -0800975#ifndef RS_COMPATIBILITY_LIB
Jason Sams789ca832011-05-18 17:36:02 -0700976RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700977 uint32_t sdkVersion, RsSurfaceConfig sc,
978 uint32_t dpi) {
Tim Murray20f62ca2013-05-09 11:57:35 -0700979 //ALOGV("rsContextCreateGL dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700980 Device * dev = static_cast<Device *>(vdev);
981 Context *rsc = Context::createContext(dev, &sc);
Jason Sams9544f762011-07-13 16:09:42 -0700982 if (rsc) {
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700983 rsc->setTargetSdkVersion(sdkVersion);
Jason Sams9544f762011-07-13 16:09:42 -0700984 rsc->setDPI(dpi);
985 }
Tim Murray20f62ca2013-05-09 11:57:35 -0700986 //ALOGV("%p rsContextCreateGL ret", rsc);
Jason Sams789ca832011-05-18 17:36:02 -0700987 return rsc;
988}
Jason Sams93eacc72012-12-18 14:26:57 -0800989#endif
Jason Sams789ca832011-05-18 17:36:02 -0700990
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700991// Only to be called at a3d load time, before object is visible to user
992// not thread safe
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800993void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700994 ObjectBase *ob = static_cast<ObjectBase *>(obj);
995 (*name) = ob->getName();
996}