blob: ef155681ab9db04ce78638fe620e63f3518ed7fe [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) && \
Elliott Hughese5e18cc2015-08-12 16:37:22 -070040 defined(__ANDROID__)
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
Stephen McGroartyd5164d52015-05-08 15:31:49 +010048int gDebuggerPresent = 0;
49
Tim Murray0b575de2013-03-15 15:56:43 -070050#ifdef RS_SERVER
51// Android exposes gettid(), standard Linux does not
52static pid_t gettid() {
53 return syscall(SYS_gettid);
54}
55#endif
56
Jason Sams326e0dd2009-05-22 14:03:28 -070057using namespace android;
58using namespace android::renderscript;
59
Jason Samsfb03a222009-10-15 16:47:31 -070060pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams4961cce2013-04-11 16:11:46 -070061pthread_mutex_t Context::gMessageMutex = PTHREAD_MUTEX_INITIALIZER;
Stephen Hinesca3f09c2011-01-07 15:11:30 -080062pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070063
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080064bool Context::initGLThread() {
Jason Sams6b8552a2010-10-13 15:31:10 -070065 pthread_mutex_lock(&gInitMutex);
Jason Sams6b8552a2010-10-13 15:31:10 -070066
Jason Sams4b3de472011-04-06 17:52:23 -070067 if (!mHal.funcs.initGraphics(this)) {
Jason Sams5c1c79a2010-11-03 14:27:11 -070068 pthread_mutex_unlock(&gInitMutex);
Steve Blockaf12ac62012-01-06 19:20:56 +000069 ALOGE("%p initGraphics failed", this);
Jason Sams5c1c79a2010-11-03 14:27:11 -070070 return false;
Jason Sams6b8552a2010-10-13 15:31:10 -070071 }
72
Jason Sams6b8552a2010-10-13 15:31:10 -070073 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -070074 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -070075}
76
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080077void Context::deinitEGL() {
Jason Sams93eacc72012-12-18 14:26:57 -080078#ifndef RS_COMPATIBILITY_LIB
Jason Sams4b3de472011-04-06 17:52:23 -070079 mHal.funcs.shutdownGraphics(this);
Jason Sams93eacc72012-12-18 14:26:57 -080080#endif
Jason Sams33b6e3b2009-10-27 14:44:31 -070081}
82
Jason Sams60709252010-11-17 15:29:32 -080083Context::PushState::PushState(Context *con) {
84 mRsc = con;
Jason Sams93eacc72012-12-18 14:26:57 -080085#ifndef RS_COMPATIBILITY_LIB
Jason Samsc946b612011-02-23 14:47:17 -080086 if (con->mIsGraphicsContext) {
87 mFragment.set(con->getProgramFragment());
88 mVertex.set(con->getProgramVertex());
89 mStore.set(con->getProgramStore());
90 mRaster.set(con->getProgramRaster());
91 mFont.set(con->getFont());
92 }
Jason Sams93eacc72012-12-18 14:26:57 -080093#endif
Jason Sams60709252010-11-17 15:29:32 -080094}
95
96Context::PushState::~PushState() {
Jason Sams93eacc72012-12-18 14:26:57 -080097#ifndef RS_COMPATIBILITY_LIB
Jason Samsc946b612011-02-23 14:47:17 -080098 if (mRsc->mIsGraphicsContext) {
99 mRsc->setProgramFragment(mFragment.get());
100 mRsc->setProgramVertex(mVertex.get());
101 mRsc->setProgramStore(mStore.get());
102 mRsc->setProgramRaster(mRaster.get());
103 mRsc->setFont(mFont.get());
104 }
Jason Sams93eacc72012-12-18 14:26:57 -0800105#endif
Jason Sams60709252010-11-17 15:29:32 -0800106}
107
Jason Sams33b6e3b2009-10-27 14:44:31 -0700108
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800109uint32_t Context::runScript(Script *s) {
Shih-wei Liaoda3b58d2012-08-03 04:24:33 -0700110 PushState ps(this);
Jason Sams10308932009-06-09 12:15:30 -0700111
Jason Samsc61346b2010-05-28 18:23:22 -0700112 uint32_t ret = s->run(this);
Jason Samsc9d43db2009-07-28 12:02:16 -0700113 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700114}
115
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800116uint32_t Context::runRootScript() {
Jason Sams2dca84d2009-12-09 11:05:45 -0800117 timerSet(RS_TIMER_SCRIPT);
Jason Sams93eacc72012-12-18 14:26:57 -0800118#ifndef RS_COMPATIBILITY_LIB
Jason Sams8c401ef2009-10-06 13:58:47 -0700119 mStateFragmentStore.mLast.clear();
Jason Sams93eacc72012-12-18 14:26:57 -0800120#endif
Jason Sams2382aba2011-09-13 15:41:01 -0700121 watchdog.inRoot = true;
Jason Samsc61346b2010-05-28 18:23:22 -0700122 uint32_t ret = runScript(mRootScript.get());
Jason Sams2382aba2011-09-13 15:41:01 -0700123 watchdog.inRoot = false;
Jason Sams8cfdd242009-10-14 15:43:53 -0700124
Jason Samscfb1d112009-08-05 13:57:03 -0700125 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700126}
127
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800128uint64_t Context::getTime() const {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700129#ifndef ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -0700130 struct timespec t;
131 clock_gettime(CLOCK_MONOTONIC, &t);
132 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700133#else
134 return 0;
135#endif //ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -0700136}
137
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800138void Context::timerReset() {
Jason Sams24371d92009-08-19 12:17:14 -0700139 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
140 mTimers[ct] = 0;
141 }
142}
143
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800144void Context::timerInit() {
Jason Sams24371d92009-08-19 12:17:14 -0700145 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700146 mTimeFrame = mTimeLast;
147 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700148 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700149 mAverageFPSFrameCount = 0;
150 mAverageFPSStartTime = mTimeLast;
151 mAverageFPS = 0;
Jason Sams24371d92009-08-19 12:17:14 -0700152 timerReset();
153}
154
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800155void Context::timerFrame() {
Jason Sams1d54f102009-09-03 15:43:13 -0700156 mTimeLastFrame = mTimeFrame;
157 mTimeFrame = getTime();
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700158 // Update average fps
159 const uint64_t averageFramerateInterval = 1000 * 1000000;
160 mAverageFPSFrameCount ++;
161 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800162 if (inverval >= averageFramerateInterval) {
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700163 inverval = inverval / 1000000;
164 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
165 mAverageFPSFrameCount = 0;
166 mAverageFPSStartTime = mTimeFrame;
167 }
Jason Sams1d54f102009-09-03 15:43:13 -0700168}
169
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800170void Context::timerSet(Timers tm) {
Jason Sams24371d92009-08-19 12:17:14 -0700171 uint64_t last = mTimeLast;
172 mTimeLast = getTime();
173 mTimers[mTimerActive] += mTimeLast - last;
174 mTimerActive = tm;
175}
176
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800177void Context::timerPrint() {
Jason Sams24371d92009-08-19 12:17:14 -0700178 double total = 0;
179 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
180 total += mTimers[ct];
181 }
Jason Sams1d54f102009-09-03 15:43:13 -0700182 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800183 mTimeMSLastFrame = frame / 1000000;
184 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
185 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700186
Jason Sams2dca84d2009-12-09 11:05:45 -0800187
188 if (props.mLogTimes) {
Ian Rogersf8852d02014-01-29 15:35:17 -0800189 ALOGV("RS: Frame (%i), Script %2.1f%% (%i), Swap %2.1f%% (%i), Idle %2.1f%% (%" PRIi64 "), "
190 "Internal %2.1f%% (%" PRIi64 "), Avg fps: %u",
Jason Sams2dca84d2009-12-09 11:05:45 -0800191 mTimeMSLastFrame,
192 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
193 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
194 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700195 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
196 mAverageFPS);
Jason Sams2dca84d2009-12-09 11:05:45 -0800197 }
Jason Sams24371d92009-08-19 12:17:14 -0700198}
199
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800200bool Context::setupCheck() {
Jason Sams93eacc72012-12-18 14:26:57 -0800201#ifndef RS_COMPATIBILITY_LIB
Jason Sams721acc42011-04-06 11:23:54 -0700202 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700203 mFragment->setup(this, &mStateFragment);
Jason Sams721acc42011-04-06 11:23:54 -0700204 mRaster->setup(this, &mStateRaster);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700205 mVertex->setup(this, &mStateVertex);
206 mFBOCache.setup(this);
Jason Sams93eacc72012-12-18 14:26:57 -0800207#endif
Jason Samsa2cf7552010-03-03 13:03:18 -0800208 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700209}
210
Jason Sams93eacc72012-12-18 14:26:57 -0800211#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700212void Context::setupProgramStore() {
Jason Sams721acc42011-04-06 11:23:54 -0700213 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700214}
Jason Sams93eacc72012-12-18 14:26:57 -0800215#endif
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700216
Jason Samsd1f7da62012-03-15 19:18:03 -0700217static uint32_t getProp(const char *str) {
Elliott Hughese5e18cc2015-08-12 16:37:22 -0700218#if !defined(RS_SERVER) && defined(__ANDROID__)
Joe Onorato76371ff2009-09-23 16:37:36 -0700219 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700220 property_get(str, buf, "0");
Jason Samsd1f7da62012-03-15 19:18:03 -0700221 return atoi(buf);
Tim Murray0b575de2013-03-15 15:56:43 -0700222#else
223 return 0;
224#endif
Joe Onorato76371ff2009-09-23 16:37:36 -0700225}
Jason Sams326e0dd2009-05-22 14:03:28 -0700226
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800227void Context::displayDebugStats() {
Jason Sams93eacc72012-12-18 14:26:57 -0800228#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700229 char buffer[128];
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700230 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700231 float oldR, oldG, oldB, oldA;
232 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700233 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700234
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700235 ObjectBaseRef<Font> lastFont(getFont());
Chris Wailes44bef6f2014-08-12 13:51:10 -0700236 setFont(nullptr);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700237 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700238 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700239 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700240
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700241 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700242 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700243
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700244 setFont(lastFont.get());
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700245 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Jason Sams93eacc72012-12-18 14:26:57 -0800246#endif
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700247}
248
Tim Murray0c66f072012-10-23 14:05:02 -0700249void * Context::threadProc(void *vrsc) {
250 Context *rsc = static_cast<Context *>(vrsc);
Jason Samscfaec172015-01-23 13:42:43 -0800251
Tim Murray0c66f072012-10-23 14:05:02 -0700252#ifndef ANDROID_RS_SERIALIZE
253 rsc->mNativeThreadId = gettid();
Tim Murray0c66f072012-10-23 14:05:02 -0700254#endif //ANDROID_RS_SERIALIZE
Jason Samscfaec172015-01-23 13:42:43 -0800255
Tim Murray0c66f072012-10-23 14:05:02 -0700256 rsc->props.mLogTimes = getProp("debug.rs.profile") != 0;
257 rsc->props.mLogScripts = getProp("debug.rs.script") != 0;
Tim Murray0c66f072012-10-23 14:05:02 -0700258 rsc->props.mLogShaders = getProp("debug.rs.shader") != 0;
259 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes") != 0;
260 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms") != 0;
261 rsc->props.mLogVisual = getProp("debug.rs.visual") != 0;
262 rsc->props.mDebugMaxThreads = getProp("debug.rs.max-threads");
263
Jason Sams2d8030a2014-07-24 18:47:34 -0700264 if (getProp("debug.rs.debug") != 0) {
265 ALOGD("Forcing debug context due to debug.rs.debug.");
266 rsc->mContextType = RS_CONTEXT_TYPE_DEBUG;
Jason Sams0ca7cba2015-03-11 15:22:38 -0700267 rsc->mForceCpu = true;
Jason Sams2d8030a2014-07-24 18:47:34 -0700268 }
269
Jason Sams0ca7cba2015-03-11 15:22:38 -0700270 bool forceCpu = getProp("debug.rs.default-CPU-driver") != 0;
271 if (forceCpu) {
272 ALOGD("Skipping hardware driver and loading default CPU driver");
273 rsc->mForceCpu = true;
Tim Murray0c66f072012-10-23 14:05:02 -0700274 }
275
Jason Sams0ca7cba2015-03-11 15:22:38 -0700276 rsc->mForceCpu |= rsc->mIsGraphicsContext;
277 rsc->loadDriver(rsc->mForceCpu);
Jason Sams110f1812013-03-14 16:02:18 -0700278
Jason Samscfaec172015-01-23 13:42:43 -0800279 if (!rsc->isSynchronous()) {
280 // Due to legacy we default to normal_graphics
281 // setPriority will make the adjustments as needed.
282 rsc->setPriority(RS_THREAD_PRIORITY_NORMAL_GRAPHICS);
283 }
Jason Samse5769102009-06-19 16:03:18 -0700284
Jason Sams93eacc72012-12-18 14:26:57 -0800285#ifndef RS_COMPATIBILITY_LIB
Jason Sams83c451a2011-04-21 11:46:50 -0700286 if (rsc->mIsGraphicsContext) {
Jason Samsd3e71072011-05-03 15:01:58 -0700287 if (!rsc->initGLThread()) {
288 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
Chris Wailes44bef6f2014-08-12 13:51:10 -0700289 return nullptr;
Jason Samsd3e71072011-05-03 15:01:58 -0700290 }
291
Jason Sams83c451a2011-04-21 11:46:50 -0700292 rsc->mStateRaster.init(rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700293 rsc->setProgramRaster(nullptr);
Jason Sams83c451a2011-04-21 11:46:50 -0700294 rsc->mStateVertex.init(rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700295 rsc->setProgramVertex(nullptr);
Jason Sams83c451a2011-04-21 11:46:50 -0700296 rsc->mStateFragment.init(rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700297 rsc->setProgramFragment(nullptr);
Jason Sams83c451a2011-04-21 11:46:50 -0700298 rsc->mStateFragmentStore.init(rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700299 rsc->setProgramStore(nullptr);
Jason Sams83c451a2011-04-21 11:46:50 -0700300 rsc->mStateFont.init(rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700301 rsc->setFont(nullptr);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700302 rsc->mStateSampler.init(rsc);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700303 rsc->mFBOCache.init(rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700304 }
Jason Sams93eacc72012-12-18 14:26:57 -0800305#endif
Jason Sams8ce125b2009-06-17 16:52:59 -0700306
Jason Sams83c451a2011-04-21 11:46:50 -0700307 rsc->mRunning = true;
Tim Murray4d252d62012-11-29 14:37:59 -0800308
309 if (rsc->isSynchronous()) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700310 return nullptr;
Tim Murray4d252d62012-11-29 14:37:59 -0800311 }
312
Jason Sams5f27d6f2012-02-07 15:32:08 -0800313 if (!rsc->mIsGraphicsContext) {
314 while (!rsc->mExit) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800315 rsc->mIO.playCoreCommands(rsc, -1);
Jason Samse0aab4a2011-08-12 15:05:15 -0700316 }
Jason Sams93eacc72012-12-18 14:26:57 -0800317#ifndef RS_COMPATIBILITY_LIB
Jason Sams5f27d6f2012-02-07 15:32:08 -0800318 } else {
319#ifndef ANDROID_RS_SERIALIZE
320 DisplayEventReceiver displayEvent;
321 DisplayEventReceiver::Event eventBuffer[1];
322#endif
323 int vsyncRate = 0;
324 int targetRate = 0;
Jason Samse0aab4a2011-08-12 15:05:15 -0700325
Jason Sams5f27d6f2012-02-07 15:32:08 -0800326 bool drawOnce = false;
327 while (!rsc->mExit) {
328 rsc->timerSet(RS_TIMER_IDLE);
Jason Sams326e0dd2009-05-22 14:03:28 -0700329
Jason Sams5f27d6f2012-02-07 15:32:08 -0800330#ifndef ANDROID_RS_SERIALIZE
Jason Sams9afd9a52012-02-17 16:59:50 -0800331 if (!rsc->mRootScript.get() || !rsc->mHasSurface || rsc->mPaused) {
332 targetRate = 0;
333 }
334
Jason Sams5f27d6f2012-02-07 15:32:08 -0800335 if (vsyncRate != targetRate) {
336 displayEvent.setVsyncRate(targetRate);
337 vsyncRate = targetRate;
338 }
339 if (targetRate) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800340 drawOnce |= rsc->mIO.playCoreCommands(rsc, displayEvent.getFd());
Jason Sams5f27d6f2012-02-07 15:32:08 -0800341 while (displayEvent.getEvents(eventBuffer, 1) != 0) {
342 //ALOGE("vs2 time past %lld", (rsc->getTime() - eventBuffer[0].header.timestamp) / 1000000);
343 }
344 } else
345#endif
346 {
Jason Sams963a2fb2012-02-09 14:36:14 -0800347 drawOnce |= rsc->mIO.playCoreCommands(rsc, -1);
Jason Sams83c451a2011-04-21 11:46:50 -0700348 }
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700349
Chris Wailes44bef6f2014-08-12 13:51:10 -0700350 if ((rsc->mRootScript.get() != nullptr) && rsc->mHasSurface &&
Jason Sams5f27d6f2012-02-07 15:32:08 -0800351 (targetRate || drawOnce) && !rsc->mPaused) {
352
353 drawOnce = false;
354 targetRate = ((rsc->runRootScript() + 15) / 16);
355
356 if (rsc->props.mLogVisual) {
357 rsc->displayDebugStats();
358 }
359
360 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
361 rsc->mHal.funcs.swap(rsc);
362 rsc->timerFrame();
363 rsc->timerSet(RS_TIMER_INTERNAL);
364 rsc->timerPrint();
365 rsc->timerReset();
366 }
Jason Sams83c451a2011-04-21 11:46:50 -0700367 }
Jason Sams93eacc72012-12-18 14:26:57 -0800368#endif
Jason Sams83c451a2011-04-21 11:46:50 -0700369 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700370
Tim Murray20f62ca2013-05-09 11:57:35 -0700371 //ALOGV("%p RS Thread exiting", rsc);
Jason Samse514b452009-09-25 14:51:22 -0700372
Jason Sams93eacc72012-12-18 14:26:57 -0800373#ifndef RS_COMPATIBILITY_LIB
Jason Sams83c451a2011-04-21 11:46:50 -0700374 if (rsc->mIsGraphicsContext) {
375 pthread_mutex_lock(&gInitMutex);
376 rsc->deinitEGL();
377 pthread_mutex_unlock(&gInitMutex);
378 }
Jason Sams93eacc72012-12-18 14:26:57 -0800379#endif
Jason Sams33b6e3b2009-10-27 14:44:31 -0700380
Tim Murray20f62ca2013-05-09 11:57:35 -0700381 //ALOGV("%p RS Thread exited", rsc);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700382 return nullptr;
Jason Sams326e0dd2009-05-22 14:03:28 -0700383}
384
Jason Sams741aac92010-12-24 14:38:39 -0800385void Context::destroyWorkerThreadResources() {
Steve Block65982012011-10-20 11:56:00 +0100386 //ALOGV("destroyWorkerThreadResources 1");
Jason Sams2e8665d2011-01-27 00:14:13 -0800387 ObjectBase::zeroAllUserRef(this);
Jason Sams93eacc72012-12-18 14:26:57 -0800388#ifndef RS_COMPATIBILITY_LIB
Jason Sams741aac92010-12-24 14:38:39 -0800389 if (mIsGraphicsContext) {
390 mRaster.clear();
391 mFragment.clear();
392 mVertex.clear();
393 mFragmentStore.clear();
394 mFont.clear();
395 mRootScript.clear();
396 mStateRaster.deinit(this);
397 mStateVertex.deinit(this);
398 mStateFragment.deinit(this);
399 mStateFragmentStore.deinit(this);
400 mStateFont.deinit(this);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700401 mStateSampler.deinit(this);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700402 mFBOCache.deinit(this);
Jason Sams741aac92010-12-24 14:38:39 -0800403 }
Jason Sams93eacc72012-12-18 14:26:57 -0800404#endif
Jason Samsc7cec1e2011-08-18 18:01:33 -0700405 ObjectBase::freeAllChildren(this);
Jason Samscf912de2011-01-09 16:09:51 -0800406 mExit = true;
Jason Sams5f27d6f2012-02-07 15:32:08 -0800407 //ALOGV("destroyWorkerThreadResources 2");
Jason Sams741aac92010-12-24 14:38:39 -0800408}
409
Jason Sams2382aba2011-09-13 15:41:01 -0700410void Context::printWatchdogInfo(void *ctx) {
411 Context *rsc = (Context *)ctx;
Jason Samsee803442011-10-13 16:05:27 -0700412 if (rsc->watchdog.command && rsc->watchdog.file) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000413 ALOGE("RS watchdog timeout: %i %s line %i %s", rsc->watchdog.inRoot,
Jason Samsee803442011-10-13 16:05:27 -0700414 rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file);
415 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000416 ALOGE("RS watchdog timeout: %i", rsc->watchdog.inRoot);
Jason Samsee803442011-10-13 16:05:27 -0700417 }
Jason Sams2382aba2011-09-13 15:41:01 -0700418}
419
420
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800421void Context::setPriority(int32_t p) {
Jason Samscfaec172015-01-23 13:42:43 -0800422 switch (p) {
423 // The public API will always send NORMAL_GRAPHICS
424 // for normal, we adjust here
425 case RS_THREAD_PRIORITY_NORMAL_GRAPHICS:
426 if (mIsGraphicsContext) {
427 break;
428 } else {
429 if (mHal.flags & RS_CONTEXT_LOW_LATENCY) {
430 p = RS_THREAD_PRIORITY_LOW_LATENCY;
431 } else {
432 p = RS_THREAD_PRIORITY_NORMAL;
433 }
434 }
435 case RS_THREAD_PRIORITY_LOW:
436 break;
437 }
438
Jason Sams15832442009-11-15 12:14:26 -0800439 // Note: If we put this in the proper "background" policy
440 // the wallpapers can become completly unresponsive at times.
441 // This is probably not what we want for something the user is actively
442 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800443 mThreadPriority = p;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700444 setpriority(PRIO_PROCESS, mNativeThreadId, p);
Jason Sams9719bd42012-01-12 14:22:21 -0800445 mHal.funcs.setPriority(this, mThreadPriority);
Jason Sams15832442009-11-15 12:14:26 -0800446}
447
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800448Context::Context() {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700449 mDev = nullptr;
Jason Sams326e0dd2009-05-22 14:03:28 -0700450 mRunning = false;
451 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700452 mPaused = false;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700453 mObjHead = nullptr;
Jason Samsa2cf7552010-03-03 13:03:18 -0800454 mError = RS_ERROR_NONE;
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700455 mTargetSdkVersion = 14;
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700456 mDPI = 96;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700457 mIsContextLite = false;
Stephen Hines86c6b5f2011-10-31 14:07:54 -0700458 memset(&watchdog, 0, sizeof(watchdog));
Jason Samsf2748272013-11-26 18:10:59 -0800459 memset(&mHal, 0, sizeof(mHal));
Tim Murray0e92fa32012-11-06 14:36:38 -0800460 mForceCpu = false;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700461 mContextType = RS_CONTEXT_TYPE_NORMAL;
verena beckhamf5029802015-05-22 16:51:42 +0100462 mOptLevel = 3;
Tim Murray4d252d62012-11-29 14:37:59 -0800463 mSynchronous = false;
Jason Sams4efe3d32015-03-18 18:28:06 -0700464 mFatalErrorOccured = false;
Jason Sams95fe6442015-05-18 18:40:47 -0700465
466 memset(mCacheDir, 0, sizeof(mCacheDir));
467#ifdef RS_COMPATIBILITY_LIB
468 memset(nativeLibDir, 0, sizeof(nativeLibDir));
469#endif
Tim Murray0e92fa32012-11-06 14:36:38 -0800470}
471
Jason Sams95fe6442015-05-18 18:40:47 -0700472void Context::setCacheDir(const char * cacheDir_arg, uint32_t length) {
473 if (!hasSetCacheDir) {
474 if (length <= PATH_MAX) {
475 memcpy(mCacheDir, cacheDir_arg, length);
476 mCacheDir[length] = 0;
477 hasSetCacheDir = true;
478 } else {
479 setError(RS_ERROR_BAD_VALUE, "Invalid path");
480 }
481 }
482}
483
Stephen McGroartyd5164d52015-05-08 15:31:49 +0100484void Context::waitForDebugger() {
485 while (!gDebuggerPresent) {
486 sleep(0);
487 }
488}
Jason Sams95fe6442015-05-18 18:40:47 -0700489
Tim Murray0e92fa32012-11-06 14:36:38 -0800490Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc,
Tim Murray84e3dea2013-09-09 16:12:51 -0700491 RsContextType ct, uint32_t flags) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700492 Context * rsc = new Context();
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700493
Tim Murray84e3dea2013-09-09 16:12:51 -0700494 if (flags & RS_CONTEXT_LOW_LATENCY) {
495 rsc->mForceCpu = true;
496 }
497 if (flags & RS_CONTEXT_SYNCHRONOUS) {
498 rsc->mSynchronous = true;
499 }
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700500 rsc->mContextType = ct;
Jason Sams416f4b62014-06-25 15:45:36 -0700501 rsc->mHal.flags = flags;
Tim Murray0e92fa32012-11-06 14:36:38 -0800502
Jason Sams5c1c79a2010-11-03 14:27:11 -0700503 if (!rsc->initContext(dev, sc)) {
504 delete rsc;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700505 return nullptr;
Jason Sams5c1c79a2010-11-03 14:27:11 -0700506 }
Stephen McGroartyd5164d52015-05-08 15:31:49 +0100507
508 if (flags & RS_CONTEXT_WAIT_FOR_ATTACH) {
509 rsc->waitForDebugger();
510 }
511
Jason Sams5c1c79a2010-11-03 14:27:11 -0700512 return rsc;
513}
514
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700515Context * Context::createContextLite() {
516 Context * rsc = new Context();
517 rsc->mIsContextLite = true;
518 return rsc;
519}
520
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800521bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700522 pthread_mutex_lock(&gInitMutex);
523
Jason Sams1a4efa32011-05-17 15:01:29 -0700524 mIO.init();
Jason Sams5f27d6f2012-02-07 15:32:08 -0800525 mIO.setTimeoutCallback(printWatchdogInfo, this, 2e9);
Jason Sams1a4efa32011-05-17 15:01:29 -0700526
Jason Sams5c1c79a2010-11-03 14:27:11 -0700527 dev->addContext(this);
528 mDev = dev;
Jason Sams6b8552a2010-10-13 15:31:10 -0700529 if (sc) {
530 mUserSurfaceConfig = *sc;
531 } else {
532 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
533 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800534
Chris Wailes44bef6f2014-08-12 13:51:10 -0700535 mIsGraphicsContext = sc != nullptr;
Jason Sams326e0dd2009-05-22 14:03:28 -0700536
Jason Samsa658e902009-06-04 14:35:01 -0700537 int status;
538 pthread_attr_t threadAttr;
539
Jason Samsfb03a222009-10-15 16:47:31 -0700540 pthread_mutex_unlock(&gInitMutex);
541
542 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700543
Jason Samsa658e902009-06-04 14:35:01 -0700544 status = pthread_attr_init(&threadAttr);
545 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000546 ALOGE("Failed to init thread attribute.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700547 return false;
Jason Samsa658e902009-06-04 14:35:01 -0700548 }
549
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700550 mHasSurface = false;
Stephen Hines4c368af2015-05-06 00:43:02 -0700551 mDriverName = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700552
Jason Sams24371d92009-08-19 12:17:14 -0700553 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700554 timerSet(RS_TIMER_INTERNAL);
Tim Murray4d252d62012-11-29 14:37:59 -0800555 if (mSynchronous) {
556 threadProc(this);
Xiaofei Wanfea96e82014-03-31 16:43:15 +0800557
558 if (mError != RS_ERROR_NONE) {
559 ALOGE("Errors during thread init (sync mode)");
560 return false;
561 }
Tim Murray4d252d62012-11-29 14:37:59 -0800562 } else {
563 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
564 if (status) {
565 ALOGE("Failed to start rs context thread.");
566 return false;
567 }
568 while (!mRunning && (mError == RS_ERROR_NONE)) {
569 usleep(100);
570 }
Jason Sams50869382009-08-18 17:07:09 -0700571
Tim Murray4d252d62012-11-29 14:37:59 -0800572 if (mError != RS_ERROR_NONE) {
573 ALOGE("Errors during thread init");
574 return false;
575 }
Jason Sams18133402010-07-20 15:09:00 -0700576
Tim Murray4d252d62012-11-29 14:37:59 -0800577 pthread_attr_destroy(&threadAttr);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700578 }
Jason Sams5c1c79a2010-11-03 14:27:11 -0700579 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700580}
581
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800582Context::~Context() {
Tim Murray20f62ca2013-05-09 11:57:35 -0700583 //ALOGV("%p Context::~Context", this);
Jason Samscf912de2011-01-09 16:09:51 -0800584
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700585 if (!mIsContextLite) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700586 mPaused = false;
587 void *res;
Jason Sams326e0dd2009-05-22 14:03:28 -0700588
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700589 mIO.shutdown();
Sangkyu Leebcba6f62014-04-01 15:22:13 +0900590 if (!mSynchronous) {
591 pthread_join(mThreadId, &res);
592 }
Jason Sams5f27d6f2012-02-07 15:32:08 -0800593 rsAssert(mExit);
Jason Sams326e0dd2009-05-22 14:03:28 -0700594
Xiaofei Wanfea96e82014-03-31 16:43:15 +0800595 if (mHal.funcs.shutdownDriver && mHal.drv) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700596 mHal.funcs.shutdownDriver(this);
597 }
598
599 // Global structure cleanup.
600 pthread_mutex_lock(&gInitMutex);
601 if (mDev) {
602 mDev->removeContext(this);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700603 mDev = nullptr;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700604 }
605 pthread_mutex_unlock(&gInitMutex);
Jason Sams51462c52011-01-25 00:26:25 -0800606 }
Tim Murray20f62ca2013-05-09 11:57:35 -0700607 //ALOGV("%p Context::~Context done", this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700608}
609
Jason Sams93eacc72012-12-18 14:26:57 -0800610#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700611void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800612 rsAssert(mIsGraphicsContext);
Jason Sams4b3de472011-04-06 17:52:23 -0700613 mHal.funcs.setSurface(this, w, h, sur);
Jason Sams458f2dc2009-11-03 13:58:36 -0800614
Chris Wailes44bef6f2014-08-12 13:51:10 -0700615 mHasSurface = sur != nullptr;
Jason Sams4b3de472011-04-06 17:52:23 -0700616 mWidth = w;
617 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800618
Jason Sams4b3de472011-04-06 17:52:23 -0700619 if (mWidth && mHeight) {
Jason Sams771565f2010-05-14 15:30:29 -0700620 mStateVertex.updateSize(this);
Alex Sakhartchouka544b632011-07-19 17:50:29 -0700621 mFBOCache.updateSize();
Jason Sams458f2dc2009-11-03 13:58:36 -0800622 }
623}
624
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800625uint32_t Context::getCurrentSurfaceWidth() const {
626 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700627 if (mFBOCache.mHal.state.colorTargets[i] != nullptr) {
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800628 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimX();
629 }
630 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700631 if (mFBOCache.mHal.state.depthTarget != nullptr) {
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800632 return mFBOCache.mHal.state.depthTarget->getType()->getDimX();
633 }
634 return mWidth;
635}
636
637uint32_t Context::getCurrentSurfaceHeight() const {
638 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700639 if (mFBOCache.mHal.state.colorTargets[i] != nullptr) {
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800640 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimY();
641 }
642 }
Chris Wailes44bef6f2014-08-12 13:51:10 -0700643 if (mFBOCache.mHal.state.depthTarget != nullptr) {
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800644 return mFBOCache.mHal.state.depthTarget->getType()->getDimY();
645 }
646 return mHeight;
647}
648
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800649void Context::pause() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800650 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700651 mPaused = true;
652}
653
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800654void Context::resume() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800655 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700656 mPaused = false;
657}
658
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800659void Context::setRootScript(Script *s) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800660 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700661 mRootScript.set(s);
662}
663
Jason Sams60709252010-11-17 15:29:32 -0800664void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800665 rsAssert(mIsGraphicsContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700666 if (pfs == nullptr) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700667 mFragmentStore.set(mStateFragmentStore.mDefault);
668 } else {
669 mFragmentStore.set(pfs);
670 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700671}
672
Jason Sams60709252010-11-17 15:29:32 -0800673void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800674 rsAssert(mIsGraphicsContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700675 if (pf == nullptr) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700676 mFragment.set(mStateFragment.mDefault);
677 } else {
678 mFragment.set(pf);
679 }
Jason Samscfb1d112009-08-05 13:57:03 -0700680}
681
Jason Sams60709252010-11-17 15:29:32 -0800682void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800683 rsAssert(mIsGraphicsContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700684 if (pr == nullptr) {
Jason Sams5fd09d82009-09-23 13:57:02 -0700685 mRaster.set(mStateRaster.mDefault);
686 } else {
687 mRaster.set(pr);
688 }
689}
690
Jason Sams60709252010-11-17 15:29:32 -0800691void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800692 rsAssert(mIsGraphicsContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700693 if (pv == nullptr) {
Jason Sams8ce125b2009-06-17 16:52:59 -0700694 mVertex.set(mStateVertex.mDefault);
695 } else {
696 mVertex.set(pv);
697 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700698}
699
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800700void Context::setFont(Font *f) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700701 rsAssert(mIsGraphicsContext);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700702 if (f == nullptr) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700703 mFont.set(mStateFont.mDefault);
704 } else {
705 mFont.set(f);
706 }
707}
Jason Sams93eacc72012-12-18 14:26:57 -0800708#endif
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700709
Jason Samsf2748272013-11-26 18:10:59 -0800710void Context::finish() {
711 if (mHal.funcs.finish) {
712 mHal.funcs.finish(this);
713 }
714}
715
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800716void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700717 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700718 obj->setName(name, len);
Yang Nib8353c52015-02-14 18:00:59 -0800719 mNames.add(obj);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700720}
721
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800722void Context::removeName(ObjectBase *obj) {
Yang Nib8353c52015-02-14 18:00:59 -0800723 for (size_t ct=0; ct < mNames.size(); ct++) {
724 if (obj == mNames[ct]) {
725 mNames.removeAt(ct);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700726 return;
727 }
728 }
729}
730
Jason Sams1a4efa32011-05-17 15:01:29 -0700731RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) {
732 return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800733}
734
Jason Sams1a4efa32011-05-17 15:01:29 -0700735RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) {
736 return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen);
Jason Sams8c401ef2009-10-06 13:58:47 -0700737}
738
Jason Sams87319de2010-11-22 16:20:16 -0800739bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
740 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Sams1a4efa32011-05-17 15:01:29 -0700741
Jason Sams4961cce2013-04-11 16:11:46 -0700742 pthread_mutex_lock(&gMessageMutex);
743 bool ret = mIO.sendToClient(cmdID, subID, data, len, waitForSpace);
744 pthread_mutex_unlock(&gMessageMutex);
745 return ret;
Jason Sams8c401ef2009-10-06 13:58:47 -0700746}
747
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800748void Context::initToClient() {
749 while (!mRunning) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700750 usleep(100);
751 }
752}
753
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800754void Context::deinitToClient() {
Jason Sams1a4efa32011-05-17 15:01:29 -0700755 mIO.clientShutdown();
Jason Sams8c401ef2009-10-06 13:58:47 -0700756}
Jason Sams50869382009-08-18 17:07:09 -0700757
Jason Sams87319de2010-11-22 16:20:16 -0800758void Context::setError(RsError e, const char *msg) const {
Jason Samsa2cf7552010-03-03 13:03:18 -0800759 mError = e;
Jason Sams4efe3d32015-03-18 18:28:06 -0700760
761 if (mError >= RS_ERROR_FATAL_DEBUG) {
762 // If a FATAL error occurred, set the flag to indicate the process
763 // will be goign down
764 mFatalErrorOccured = true;
765 }
766
Jason Samsaad4bc52010-11-08 17:06:46 -0800767 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Samsa2cf7552010-03-03 13:03:18 -0800768}
769
770
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800771void Context::dumpDebug() const {
Steve Blockaf12ac62012-01-06 19:20:56 +0000772 ALOGE("RS Context debug %p", this);
773 ALOGE("RS Context debug");
Jason Sams13e26342009-11-24 12:26:35 -0800774
Steve Blockaf12ac62012-01-06 19:20:56 +0000775 ALOGE(" RS width %i, height %i", mWidth, mHeight);
776 ALOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
777 ALOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId);
Jason Sams13e26342009-11-24 12:26:35 -0800778}
Jason Samsa4a54e42009-06-10 18:39:40 -0700779
Jason Sams326e0dd2009-05-22 14:03:28 -0700780///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700781//
Jason Sams326e0dd2009-05-22 14:03:28 -0700782
783namespace android {
784namespace renderscript {
785
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800786void rsi_ContextFinish(Context *rsc) {
Jason Samsf2748272013-11-26 18:10:59 -0800787 rsc->finish();
Jason Sams8c880902010-06-15 12:15:57 -0700788}
Jason Sams326e0dd2009-05-22 14:03:28 -0700789
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800790void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Sams93eacc72012-12-18 14:26:57 -0800791#ifndef RS_COMPATIBILITY_LIB
Jason Sams326e0dd2009-05-22 14:03:28 -0700792 Script *s = static_cast<Script *>(vs);
793 rsc->setRootScript(s);
Jason Sams93eacc72012-12-18 14:26:57 -0800794#endif
Jason Sams326e0dd2009-05-22 14:03:28 -0700795}
796
Tim Murrayea6e0622015-04-07 15:43:11 -0700797void rsi_ContextSetCacheDir(Context *rsc, const char *cacheDir, size_t cacheDir_length) {
798 rsc->setCacheDir(cacheDir, cacheDir_length);
799}
800
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800801void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700802 Sampler *s = static_cast<Sampler *>(vs);
803
804 if (slot > RS_MAX_SAMPLER_SLOT) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000805 ALOGE("Invalid sampler slot");
Jason Sams326e0dd2009-05-22 14:03:28 -0700806 return;
807 }
808
809 s->bindToContext(&rsc->mStateSampler, slot);
810}
811
Jason Sams93eacc72012-12-18 14:26:57 -0800812#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800813void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Samsccc010b2010-05-13 18:30:11 -0700814 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams60709252010-11-17 15:29:32 -0800815 rsc->setProgramStore(pfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700816}
817
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800818void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700819 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Sams60709252010-11-17 15:29:32 -0800820 rsc->setProgramFragment(pf);
Jason Sams326e0dd2009-05-22 14:03:28 -0700821}
822
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800823void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Sams5fd09d82009-09-23 13:57:02 -0700824 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Sams60709252010-11-17 15:29:32 -0800825 rsc->setProgramRaster(pr);
Jason Sams5fd09d82009-09-23 13:57:02 -0700826}
827
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800828void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700829 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Sams60709252010-11-17 15:29:32 -0800830 rsc->setProgramVertex(pv);
Jason Sams326e0dd2009-05-22 14:03:28 -0700831}
832
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800833void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700834 Font *font = static_cast<Font *>(vfont);
835 rsc->setFont(font);
836}
Jason Sams93eacc72012-12-18 14:26:57 -0800837#endif
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700838
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700839void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700840 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700841 rsc->assignName(ob, name, name_length);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700842}
Jason Sams326e0dd2009-05-22 14:03:28 -0700843
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800844void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams2353ae32010-10-14 17:48:46 -0700845 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -0700846 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700847 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700848}
849
Jason Sams93eacc72012-12-18 14:26:57 -0800850#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800851void rsi_ContextPause(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700852 rsc->pause();
853}
854
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800855void rsi_ContextResume(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700856 rsc->resume();
857}
858
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700859void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
Mathias Agopianfa402862010-02-12 14:04:35 -0800860 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800861}
Jason Sams93eacc72012-12-18 14:26:57 -0800862#endif
Jason Sams613cad12009-11-12 15:10:25 -0800863
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800864void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800865 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800866}
867
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800868void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Samsc21cf402009-11-17 17:26:46 -0800869 ObjectBase::dumpAll(rsc);
870}
871
Jason Sams741aac92010-12-24 14:38:39 -0800872void rsi_ContextDestroyWorker(Context *rsc) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700873 rsc->destroyWorkerThreadResources();
Jason Sams741aac92010-12-24 14:38:39 -0800874}
875
Jason Samsc975cf42011-04-28 18:26:48 -0700876void rsi_ContextDestroy(Context *rsc) {
Jason Samsa36c50a2014-06-17 12:06:06 -0700877 //ALOGE("%p rsContextDestroy", rsc);
Jason Sams741aac92010-12-24 14:38:39 -0800878 rsContextDestroyWorker(rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800879 delete rsc;
Tim Murray20f62ca2013-05-09 11:57:35 -0700880 //ALOGV("%p rsContextDestroy done", rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800881}
882
Jason Samsc975cf42011-04-28 18:26:48 -0700883RsMessageToClientType rsi_ContextPeekMessage(Context *rsc,
Jason Sams186e5912011-04-26 14:50:00 -0700884 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700885 uint32_t * subID, size_t subID_length) {
886 return rsc->peekMessageToClient(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800887}
888
Jason Samsc975cf42011-04-28 18:26:48 -0700889RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length,
Jason Sams186e5912011-04-26 14:50:00 -0700890 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700891 uint32_t * subID, size_t subID_length) {
Jason Sams186e5912011-04-26 14:50:00 -0700892 rsAssert(subID_length == sizeof(uint32_t));
893 rsAssert(receiveLen_length == sizeof(size_t));
Jason Sams1a4efa32011-05-17 15:01:29 -0700894 return rsc->getMessageToClient(data, receiveLen, subID, data_length);
Jason Sams8c401ef2009-10-06 13:58:47 -0700895}
896
Jason Samsc975cf42011-04-28 18:26:48 -0700897void rsi_ContextInitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700898 rsc->initToClient();
899}
900
Jason Samsc975cf42011-04-28 18:26:48 -0700901void rsi_ContextDeinitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700902 rsc->deinitToClient();
903}
904
Jason Sams70265202013-02-05 19:20:47 -0800905void rsi_ContextSendMessage(Context *rsc, uint32_t id, const uint8_t *data, size_t len) {
906 rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, id, len, true);
907}
908
Tim Murray397b0b12014-01-07 15:35:08 -0800909// implementation of handcode LF_ObjDestroy
910// required so nObjDestroy can be run from finalizer without blocking
911void LF_ObjDestroy_handcode(const Context *rsc, RsAsyncVoidPtr objPtr) {
912 if (((Context *)rsc)->isSynchronous()) {
913 rsi_ObjDestroy((Context *)rsc, objPtr);
914 return;
915 }
916
917 // struct has two parts:
918 // RsPlaybackRemoteHeader (cmdID and bytes)
919 // RS_CMD_ObjDestroy (ptr)
920 struct destroyCmd {
921 uint32_t cmdID;
922 uint32_t bytes;
923 RsAsyncVoidPtr ptr;
924 };
925
926 destroyCmd cmd;
927 cmd.cmdID = RS_CMD_ID_ObjDestroy;
928 cmd.bytes = sizeof(RsAsyncVoidPtr);
929 cmd.ptr = objPtr;
930 ThreadIO *io = &((Context *)rsc)->mIO;
931 io->coreWrite((void*)&cmd, sizeof(destroyCmd));
932
933}
934
Jason Samsc975cf42011-04-28 18:26:48 -0700935}
936}
937
Tim Murrayc2ce7072013-07-17 18:38:53 -0700938extern "C" RsContext rsContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion,
Tim Murray84e3dea2013-09-09 16:12:51 -0700939 RsContextType ct, uint32_t flags) {
Tim Murray20f62ca2013-05-09 11:57:35 -0700940 //ALOGV("rsContextCreate dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700941 Device * dev = static_cast<Device *>(vdev);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700942 Context *rsc = Context::createContext(dev, nullptr, ct, flags);
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700943 if (rsc) {
944 rsc->setTargetSdkVersion(sdkVersion);
945 }
Jason Sams789ca832011-05-18 17:36:02 -0700946 return rsc;
947}
948
Miao Wangf3213d72015-01-14 10:03:07 -0800949extern "C" void rsaContextSetNativeLibDir(RsContext con, char *libDir, size_t length) {
Miao Wang9a6afb62015-03-27 14:24:00 -0700950#ifdef RS_COMPATIBILITY_LIB
Miao Wangf3213d72015-01-14 10:03:07 -0800951 Context *rsc = static_cast<Context *>(con);
952 rsc->setNativeLibDir(libDir, length);
Miao Wangf3213d72015-01-14 10:03:07 -0800953#endif
Miao Wang9a6afb62015-03-27 14:24:00 -0700954}
Miao Wangf3213d72015-01-14 10:03:07 -0800955
Jason Sams93eacc72012-12-18 14:26:57 -0800956#ifndef RS_COMPATIBILITY_LIB
Jason Sams789ca832011-05-18 17:36:02 -0700957RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700958 uint32_t sdkVersion, RsSurfaceConfig sc,
959 uint32_t dpi) {
Tim Murray20f62ca2013-05-09 11:57:35 -0700960 //ALOGV("rsContextCreateGL dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700961 Device * dev = static_cast<Device *>(vdev);
962 Context *rsc = Context::createContext(dev, &sc);
Jason Sams9544f762011-07-13 16:09:42 -0700963 if (rsc) {
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700964 rsc->setTargetSdkVersion(sdkVersion);
Jason Sams9544f762011-07-13 16:09:42 -0700965 rsc->setDPI(dpi);
966 }
Tim Murray20f62ca2013-05-09 11:57:35 -0700967 //ALOGV("%p rsContextCreateGL ret", rsc);
Jason Sams789ca832011-05-18 17:36:02 -0700968 return rsc;
969}
Jason Sams93eacc72012-12-18 14:26:57 -0800970#endif
Jason Sams789ca832011-05-18 17:36:02 -0700971
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700972// Only to be called at a3d load time, before object is visible to user
973// not thread safe
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800974void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700975 ObjectBase *ob = static_cast<ObjectBase *>(obj);
976 (*name) = ob->getName();
977}