blob: 65ee4d3d042803fabc754d158a0a0087d555a060 [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
22#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -080023#include "rsMesh.h"
Mathias Agopian5ae678f2009-06-22 18:01:09 -070024#include <ui/FramebufferNativeWindow.h>
Jason Sams5f27d6f2012-02-07 15:32:08 -080025#include <gui/DisplayEventReceiver.h>
Jason Sams93eacc72012-12-18 14:26:57 -080026#endif
Jason Sams326e0dd2009-05-22 14:03:28 -070027
Jason Sams15832442009-11-15 12:14:26 -080028#include <sys/types.h>
29#include <sys/resource.h>
Jason Sams7bf29dd2010-07-19 15:38:19 -070030#include <sched.h>
Jason Sams15832442009-11-15 12:14:26 -080031
Jason Sams8d957fa2010-09-28 14:41:22 -070032#include <sys/syscall.h>
Jason Sams93eacc72012-12-18 14:26:57 -080033#include <string.h>
Stephen Hines414a4612012-09-05 18:05:08 -070034#include <dlfcn.h>
Stephen Hinesb0934b62013-07-03 17:27:38 -070035#include <unistd.h>
Stephen Hines414a4612012-09-05 18:05:08 -070036
Stephen Hines16647802013-08-15 16:28:01 -070037#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB) && \
38 defined(HAVE_ANDROID_OS)
Tim Murray0b575de2013-03-15 15:56:43 -070039#include <cutils/properties.h>
40#endif
41
Stephen Hines6dfe6842013-08-14 17:56:38 -070042#ifdef RS_COMPATIBILITY_LIB
43#include "rsCompatibilityLib.h"
44#endif
45
Tim Murray0b575de2013-03-15 15:56:43 -070046#ifdef RS_SERVER
47// Android exposes gettid(), standard Linux does not
48static pid_t gettid() {
49 return syscall(SYS_gettid);
50}
51#endif
52
Jason Sams326e0dd2009-05-22 14:03:28 -070053using namespace android;
54using namespace android::renderscript;
55
Jason Samsfb03a222009-10-15 16:47:31 -070056pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams4961cce2013-04-11 16:11:46 -070057pthread_mutex_t Context::gMessageMutex = PTHREAD_MUTEX_INITIALIZER;
Stephen Hinesca3f09c2011-01-07 15:11:30 -080058pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070059
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080060bool Context::initGLThread() {
Jason Sams6b8552a2010-10-13 15:31:10 -070061 pthread_mutex_lock(&gInitMutex);
Jason Sams6b8552a2010-10-13 15:31:10 -070062
Jason Sams4b3de472011-04-06 17:52:23 -070063 if (!mHal.funcs.initGraphics(this)) {
Jason Sams5c1c79a2010-11-03 14:27:11 -070064 pthread_mutex_unlock(&gInitMutex);
Steve Blockaf12ac62012-01-06 19:20:56 +000065 ALOGE("%p initGraphics failed", this);
Jason Sams5c1c79a2010-11-03 14:27:11 -070066 return false;
Jason Sams6b8552a2010-10-13 15:31:10 -070067 }
68
Jason Sams6b8552a2010-10-13 15:31:10 -070069 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -070070 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -070071}
72
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080073void Context::deinitEGL() {
Jason Sams93eacc72012-12-18 14:26:57 -080074#ifndef RS_COMPATIBILITY_LIB
Jason Sams4b3de472011-04-06 17:52:23 -070075 mHal.funcs.shutdownGraphics(this);
Jason Sams93eacc72012-12-18 14:26:57 -080076#endif
Jason Sams33b6e3b2009-10-27 14:44:31 -070077}
78
Jason Sams60709252010-11-17 15:29:32 -080079Context::PushState::PushState(Context *con) {
80 mRsc = con;
Jason Sams93eacc72012-12-18 14:26:57 -080081#ifndef RS_COMPATIBILITY_LIB
Jason Samsc946b612011-02-23 14:47:17 -080082 if (con->mIsGraphicsContext) {
83 mFragment.set(con->getProgramFragment());
84 mVertex.set(con->getProgramVertex());
85 mStore.set(con->getProgramStore());
86 mRaster.set(con->getProgramRaster());
87 mFont.set(con->getFont());
88 }
Jason Sams93eacc72012-12-18 14:26:57 -080089#endif
Jason Sams60709252010-11-17 15:29:32 -080090}
91
92Context::PushState::~PushState() {
Jason Sams93eacc72012-12-18 14:26:57 -080093#ifndef RS_COMPATIBILITY_LIB
Jason Samsc946b612011-02-23 14:47:17 -080094 if (mRsc->mIsGraphicsContext) {
95 mRsc->setProgramFragment(mFragment.get());
96 mRsc->setProgramVertex(mVertex.get());
97 mRsc->setProgramStore(mStore.get());
98 mRsc->setProgramRaster(mRaster.get());
99 mRsc->setFont(mFont.get());
100 }
Jason Sams93eacc72012-12-18 14:26:57 -0800101#endif
Jason Sams60709252010-11-17 15:29:32 -0800102}
103
Jason Sams33b6e3b2009-10-27 14:44:31 -0700104
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800105uint32_t Context::runScript(Script *s) {
Shih-wei Liaoda3b58d2012-08-03 04:24:33 -0700106 PushState ps(this);
Jason Sams10308932009-06-09 12:15:30 -0700107
Jason Samsc61346b2010-05-28 18:23:22 -0700108 uint32_t ret = s->run(this);
Jason Samsc9d43db2009-07-28 12:02:16 -0700109 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700110}
111
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800112uint32_t Context::runRootScript() {
Jason Sams2dca84d2009-12-09 11:05:45 -0800113 timerSet(RS_TIMER_SCRIPT);
Jason Sams93eacc72012-12-18 14:26:57 -0800114#ifndef RS_COMPATIBILITY_LIB
Jason Sams8c401ef2009-10-06 13:58:47 -0700115 mStateFragmentStore.mLast.clear();
Jason Sams93eacc72012-12-18 14:26:57 -0800116#endif
Jason Sams2382aba2011-09-13 15:41:01 -0700117 watchdog.inRoot = true;
Jason Samsc61346b2010-05-28 18:23:22 -0700118 uint32_t ret = runScript(mRootScript.get());
Jason Sams2382aba2011-09-13 15:41:01 -0700119 watchdog.inRoot = false;
Jason Sams8cfdd242009-10-14 15:43:53 -0700120
Jason Samscfb1d112009-08-05 13:57:03 -0700121 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700122}
123
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800124uint64_t Context::getTime() const {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700125#ifndef ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -0700126 struct timespec t;
127 clock_gettime(CLOCK_MONOTONIC, &t);
128 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700129#else
130 return 0;
131#endif //ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -0700132}
133
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800134void Context::timerReset() {
Jason Sams24371d92009-08-19 12:17:14 -0700135 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
136 mTimers[ct] = 0;
137 }
138}
139
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800140void Context::timerInit() {
Jason Sams24371d92009-08-19 12:17:14 -0700141 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700142 mTimeFrame = mTimeLast;
143 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700144 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700145 mAverageFPSFrameCount = 0;
146 mAverageFPSStartTime = mTimeLast;
147 mAverageFPS = 0;
Jason Sams24371d92009-08-19 12:17:14 -0700148 timerReset();
149}
150
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800151void Context::timerFrame() {
Jason Sams1d54f102009-09-03 15:43:13 -0700152 mTimeLastFrame = mTimeFrame;
153 mTimeFrame = getTime();
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700154 // Update average fps
155 const uint64_t averageFramerateInterval = 1000 * 1000000;
156 mAverageFPSFrameCount ++;
157 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800158 if (inverval >= averageFramerateInterval) {
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700159 inverval = inverval / 1000000;
160 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
161 mAverageFPSFrameCount = 0;
162 mAverageFPSStartTime = mTimeFrame;
163 }
Jason Sams1d54f102009-09-03 15:43:13 -0700164}
165
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800166void Context::timerSet(Timers tm) {
Jason Sams24371d92009-08-19 12:17:14 -0700167 uint64_t last = mTimeLast;
168 mTimeLast = getTime();
169 mTimers[mTimerActive] += mTimeLast - last;
170 mTimerActive = tm;
171}
172
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800173void Context::timerPrint() {
Jason Sams24371d92009-08-19 12:17:14 -0700174 double total = 0;
175 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
176 total += mTimers[ct];
177 }
Jason Sams1d54f102009-09-03 15:43:13 -0700178 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800179 mTimeMSLastFrame = frame / 1000000;
180 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
181 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700182
Jason Sams2dca84d2009-12-09 11:05:45 -0800183
184 if (props.mLogTimes) {
Steve Block65982012011-10-20 11:56:00 +0100185 ALOGV("RS: Frame (%i), Script %2.1f%% (%i), Swap %2.1f%% (%i), Idle %2.1f%% (%lli), Internal %2.1f%% (%lli), Avg fps: %u",
Jason Sams2dca84d2009-12-09 11:05:45 -0800186 mTimeMSLastFrame,
187 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
188 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
189 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700190 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
191 mAverageFPS);
Jason Sams2dca84d2009-12-09 11:05:45 -0800192 }
Jason Sams24371d92009-08-19 12:17:14 -0700193}
194
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800195bool Context::setupCheck() {
Jason Sams93eacc72012-12-18 14:26:57 -0800196#ifndef RS_COMPATIBILITY_LIB
Jason Sams721acc42011-04-06 11:23:54 -0700197 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700198 mFragment->setup(this, &mStateFragment);
Jason Sams721acc42011-04-06 11:23:54 -0700199 mRaster->setup(this, &mStateRaster);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700200 mVertex->setup(this, &mStateVertex);
201 mFBOCache.setup(this);
Jason Sams93eacc72012-12-18 14:26:57 -0800202#endif
Jason Samsa2cf7552010-03-03 13:03:18 -0800203 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700204}
205
Jason Sams93eacc72012-12-18 14:26:57 -0800206#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700207void Context::setupProgramStore() {
Jason Sams721acc42011-04-06 11:23:54 -0700208 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700209}
Jason Sams93eacc72012-12-18 14:26:57 -0800210#endif
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700211
Jason Samsd1f7da62012-03-15 19:18:03 -0700212static uint32_t getProp(const char *str) {
Nick Kralevich7e85ca22013-05-22 15:04:39 -0700213#if !defined(RS_SERVER) && defined(HAVE_ANDROID_OS)
Joe Onorato76371ff2009-09-23 16:37:36 -0700214 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700215 property_get(str, buf, "0");
Jason Samsd1f7da62012-03-15 19:18:03 -0700216 return atoi(buf);
Tim Murray0b575de2013-03-15 15:56:43 -0700217#else
218 return 0;
219#endif
Joe Onorato76371ff2009-09-23 16:37:36 -0700220}
Jason Sams326e0dd2009-05-22 14:03:28 -0700221
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800222void Context::displayDebugStats() {
Jason Sams93eacc72012-12-18 14:26:57 -0800223#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700224 char buffer[128];
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700225 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700226 float oldR, oldG, oldB, oldA;
227 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700228 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700229
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700230 ObjectBaseRef<Font> lastFont(getFont());
231 setFont(NULL);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700232 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700233 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700234 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700235
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700236 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700237 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700238
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700239 setFont(lastFont.get());
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700240 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Jason Sams93eacc72012-12-18 14:26:57 -0800241#endif
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700242}
243
Tim Murray0c66f072012-10-23 14:05:02 -0700244bool Context::loadRuntime(const char* filename, Context* rsc) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700245
Tim Murray0c66f072012-10-23 14:05:02 -0700246 // TODO: store the driverSO somewhere so we can dlclose later
Stephen Hines91dfcdb2012-09-07 18:23:35 -0700247 void *driverSO = NULL;
248
Tim Murray0c66f072012-10-23 14:05:02 -0700249 driverSO = dlopen(filename, RTLD_LAZY);
Stephen Hines91dfcdb2012-09-07 18:23:35 -0700250 if (driverSO == NULL) {
Tim Murray0c66f072012-10-23 14:05:02 -0700251 ALOGE("Failed loading RS driver: %s", dlerror());
252 return false;
Stephen Hines414a4612012-09-05 18:05:08 -0700253 }
254
255 // Need to call dlerror() to clear buffer before using it for dlsym().
256 (void) dlerror();
257 typedef bool (*HalSig)(Context*, uint32_t, uint32_t);
258 HalSig halInit = (HalSig) dlsym(driverSO, "rsdHalInit");
259
260 // If we can't find the C variant, we go looking for the C++ version.
261 if (halInit == NULL) {
262 ALOGW("Falling back to find C++ rsdHalInit: %s", dlerror());
263 halInit = (HalSig) dlsym(driverSO,
264 "_Z10rsdHalInitPN7android12renderscript7ContextEjj");
265 }
266
267 if (halInit == NULL) {
Stephen Hines414a4612012-09-05 18:05:08 -0700268 dlclose(driverSO);
269 ALOGE("Failed to find rsdHalInit: %s", dlerror());
Tim Murray0c66f072012-10-23 14:05:02 -0700270 return false;
Stephen Hines414a4612012-09-05 18:05:08 -0700271 }
272
273 if (!(*halInit)(rsc, 0, 0)) {
Stephen Hines414a4612012-09-05 18:05:08 -0700274 dlclose(driverSO);
Steve Blockaf12ac62012-01-06 19:20:56 +0000275 ALOGE("Hal init failed");
Tim Murray0c66f072012-10-23 14:05:02 -0700276 return false;
Jason Sams83c451a2011-04-21 11:46:50 -0700277 }
Tim Murray0c66f072012-10-23 14:05:02 -0700278
279 //validate HAL struct
280
281
282 return true;
283}
284
Jason Sams110f1812013-03-14 16:02:18 -0700285extern "C" bool rsdHalInit(RsContext c, uint32_t version_major, uint32_t version_minor);
286
Tim Murray0c66f072012-10-23 14:05:02 -0700287void * Context::threadProc(void *vrsc) {
288 Context *rsc = static_cast<Context *>(vrsc);
289#ifndef ANDROID_RS_SERIALIZE
290 rsc->mNativeThreadId = gettid();
Jason Sams93eacc72012-12-18 14:26:57 -0800291#ifndef RS_COMPATIBILITY_LIB
Tim Murray4d252d62012-11-29 14:37:59 -0800292 if (!rsc->isSynchronous()) {
293 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
294 }
Tim Murray0c66f072012-10-23 14:05:02 -0700295 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams93eacc72012-12-18 14:26:57 -0800296#else
297 if (!rsc->isSynchronous()) {
298 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, -4);
299 }
300 rsc->mThreadPriority = -4;
301#endif
Tim Murray0c66f072012-10-23 14:05:02 -0700302#endif //ANDROID_RS_SERIALIZE
303 rsc->props.mLogTimes = getProp("debug.rs.profile") != 0;
304 rsc->props.mLogScripts = getProp("debug.rs.script") != 0;
305 rsc->props.mLogObjects = getProp("debug.rs.object") != 0;
306 rsc->props.mLogShaders = getProp("debug.rs.shader") != 0;
307 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes") != 0;
308 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms") != 0;
309 rsc->props.mLogVisual = getProp("debug.rs.visual") != 0;
310 rsc->props.mDebugMaxThreads = getProp("debug.rs.max-threads");
311
312 bool loadDefault = true;
313
314 // Provide a mechanism for dropping in a different RS driver.
Jason Sams110f1812013-03-14 16:02:18 -0700315#ifndef RS_COMPATIBILITY_LIB
Tim Murray0c66f072012-10-23 14:05:02 -0700316#ifdef OVERRIDE_RS_DRIVER
317#define XSTR(S) #S
318#define STR(S) XSTR(S)
319#define OVERRIDE_RS_DRIVER_STRING STR(OVERRIDE_RS_DRIVER)
320
321 if (getProp("debug.rs.default-CPU-driver") != 0) {
322 ALOGE("Skipping override driver and loading default CPU driver");
Tim Murray0e92fa32012-11-06 14:36:38 -0800323 } else if (rsc->mForceCpu) {
324 ALOGV("Application requested CPU execution");
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700325 } else if (rsc->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
326 ALOGV("Application requested debug context");
Tim Murray0c66f072012-10-23 14:05:02 -0700327 } else {
328 if (loadRuntime(OVERRIDE_RS_DRIVER_STRING, rsc)) {
329 ALOGE("Successfully loaded runtime: %s", OVERRIDE_RS_DRIVER_STRING);
330 loadDefault = false;
331 } else {
332 ALOGE("Failed to load runtime %s, loading default", OVERRIDE_RS_DRIVER_STRING);
333 }
334 }
335
336#undef XSTR
337#undef STR
338#endif // OVERRIDE_RS_DRIVER
339
340 if (loadDefault) {
341 if (!loadRuntime("libRSDriver.so", rsc)) {
342 ALOGE("Failed to load default runtime!");
Stephen Hines6f01bcf2012-11-19 15:18:16 -0800343 rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed loading RS driver");
Tim Murray0c66f072012-10-23 14:05:02 -0700344 return NULL;
345 }
346 }
Jason Sams110f1812013-03-14 16:02:18 -0700347#else // RS_COMPATIBILITY_LIB
348 if (rsdHalInit(rsc, 0, 0) != true) {
349 return NULL;
350 }
351#endif
352
Tim Murray0c66f072012-10-23 14:05:02 -0700353
Jason Sams83c451a2011-04-21 11:46:50 -0700354 rsc->mHal.funcs.setPriority(rsc, rsc->mThreadPriority);
Jason Samse5769102009-06-19 16:03:18 -0700355
Jason Sams93eacc72012-12-18 14:26:57 -0800356#ifndef RS_COMPATIBILITY_LIB
Jason Sams83c451a2011-04-21 11:46:50 -0700357 if (rsc->mIsGraphicsContext) {
Jason Samsd3e71072011-05-03 15:01:58 -0700358 if (!rsc->initGLThread()) {
359 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
360 return NULL;
361 }
362
Jason Sams83c451a2011-04-21 11:46:50 -0700363 rsc->mStateRaster.init(rsc);
364 rsc->setProgramRaster(NULL);
365 rsc->mStateVertex.init(rsc);
366 rsc->setProgramVertex(NULL);
367 rsc->mStateFragment.init(rsc);
368 rsc->setProgramFragment(NULL);
369 rsc->mStateFragmentStore.init(rsc);
370 rsc->setProgramStore(NULL);
371 rsc->mStateFont.init(rsc);
372 rsc->setFont(NULL);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700373 rsc->mStateSampler.init(rsc);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700374 rsc->mFBOCache.init(rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700375 }
Jason Sams93eacc72012-12-18 14:26:57 -0800376#endif
Jason Sams8ce125b2009-06-17 16:52:59 -0700377
Jason Sams83c451a2011-04-21 11:46:50 -0700378 rsc->mRunning = true;
Tim Murray4d252d62012-11-29 14:37:59 -0800379
380 if (rsc->isSynchronous()) {
381 return NULL;
382 }
383
Jason Sams5f27d6f2012-02-07 15:32:08 -0800384 if (!rsc->mIsGraphicsContext) {
385 while (!rsc->mExit) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800386 rsc->mIO.playCoreCommands(rsc, -1);
Jason Samse0aab4a2011-08-12 15:05:15 -0700387 }
Jason Sams93eacc72012-12-18 14:26:57 -0800388#ifndef RS_COMPATIBILITY_LIB
Jason Sams5f27d6f2012-02-07 15:32:08 -0800389 } else {
390#ifndef ANDROID_RS_SERIALIZE
391 DisplayEventReceiver displayEvent;
392 DisplayEventReceiver::Event eventBuffer[1];
393#endif
394 int vsyncRate = 0;
395 int targetRate = 0;
Jason Samse0aab4a2011-08-12 15:05:15 -0700396
Jason Sams5f27d6f2012-02-07 15:32:08 -0800397 bool drawOnce = false;
398 while (!rsc->mExit) {
399 rsc->timerSet(RS_TIMER_IDLE);
Jason Sams326e0dd2009-05-22 14:03:28 -0700400
Jason Sams5f27d6f2012-02-07 15:32:08 -0800401#ifndef ANDROID_RS_SERIALIZE
Jason Sams9afd9a52012-02-17 16:59:50 -0800402 if (!rsc->mRootScript.get() || !rsc->mHasSurface || rsc->mPaused) {
403 targetRate = 0;
404 }
405
Jason Sams5f27d6f2012-02-07 15:32:08 -0800406 if (vsyncRate != targetRate) {
407 displayEvent.setVsyncRate(targetRate);
408 vsyncRate = targetRate;
409 }
410 if (targetRate) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800411 drawOnce |= rsc->mIO.playCoreCommands(rsc, displayEvent.getFd());
Jason Sams5f27d6f2012-02-07 15:32:08 -0800412 while (displayEvent.getEvents(eventBuffer, 1) != 0) {
413 //ALOGE("vs2 time past %lld", (rsc->getTime() - eventBuffer[0].header.timestamp) / 1000000);
414 }
415 } else
416#endif
417 {
Jason Sams963a2fb2012-02-09 14:36:14 -0800418 drawOnce |= rsc->mIO.playCoreCommands(rsc, -1);
Jason Sams83c451a2011-04-21 11:46:50 -0700419 }
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700420
Jason Sams5f27d6f2012-02-07 15:32:08 -0800421 if ((rsc->mRootScript.get() != NULL) && rsc->mHasSurface &&
422 (targetRate || drawOnce) && !rsc->mPaused) {
423
424 drawOnce = false;
425 targetRate = ((rsc->runRootScript() + 15) / 16);
426
427 if (rsc->props.mLogVisual) {
428 rsc->displayDebugStats();
429 }
430
431 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
432 rsc->mHal.funcs.swap(rsc);
433 rsc->timerFrame();
434 rsc->timerSet(RS_TIMER_INTERNAL);
435 rsc->timerPrint();
436 rsc->timerReset();
437 }
Jason Sams83c451a2011-04-21 11:46:50 -0700438 }
Jason Sams93eacc72012-12-18 14:26:57 -0800439#endif
Jason Sams83c451a2011-04-21 11:46:50 -0700440 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700441
Tim Murray20f62ca2013-05-09 11:57:35 -0700442 //ALOGV("%p RS Thread exiting", rsc);
Jason Samse514b452009-09-25 14:51:22 -0700443
Jason Sams93eacc72012-12-18 14:26:57 -0800444#ifndef RS_COMPATIBILITY_LIB
Jason Sams83c451a2011-04-21 11:46:50 -0700445 if (rsc->mIsGraphicsContext) {
446 pthread_mutex_lock(&gInitMutex);
447 rsc->deinitEGL();
448 pthread_mutex_unlock(&gInitMutex);
449 }
Jason Sams93eacc72012-12-18 14:26:57 -0800450#endif
Jason Sams33b6e3b2009-10-27 14:44:31 -0700451
Tim Murray20f62ca2013-05-09 11:57:35 -0700452 //ALOGV("%p RS Thread exited", rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700453 return NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700454}
455
Jason Sams741aac92010-12-24 14:38:39 -0800456void Context::destroyWorkerThreadResources() {
Steve Block65982012011-10-20 11:56:00 +0100457 //ALOGV("destroyWorkerThreadResources 1");
Jason Sams2e8665d2011-01-27 00:14:13 -0800458 ObjectBase::zeroAllUserRef(this);
Jason Sams93eacc72012-12-18 14:26:57 -0800459#ifndef RS_COMPATIBILITY_LIB
Jason Sams741aac92010-12-24 14:38:39 -0800460 if (mIsGraphicsContext) {
461 mRaster.clear();
462 mFragment.clear();
463 mVertex.clear();
464 mFragmentStore.clear();
465 mFont.clear();
466 mRootScript.clear();
467 mStateRaster.deinit(this);
468 mStateVertex.deinit(this);
469 mStateFragment.deinit(this);
470 mStateFragmentStore.deinit(this);
471 mStateFont.deinit(this);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700472 mStateSampler.deinit(this);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700473 mFBOCache.deinit(this);
Jason Sams741aac92010-12-24 14:38:39 -0800474 }
Jason Sams93eacc72012-12-18 14:26:57 -0800475#endif
Jason Samsc7cec1e2011-08-18 18:01:33 -0700476 ObjectBase::freeAllChildren(this);
Jason Samscf912de2011-01-09 16:09:51 -0800477 mExit = true;
Jason Sams5f27d6f2012-02-07 15:32:08 -0800478 //ALOGV("destroyWorkerThreadResources 2");
Jason Sams741aac92010-12-24 14:38:39 -0800479}
480
Jason Sams2382aba2011-09-13 15:41:01 -0700481void Context::printWatchdogInfo(void *ctx) {
482 Context *rsc = (Context *)ctx;
Jason Samsee803442011-10-13 16:05:27 -0700483 if (rsc->watchdog.command && rsc->watchdog.file) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000484 ALOGE("RS watchdog timeout: %i %s line %i %s", rsc->watchdog.inRoot,
Jason Samsee803442011-10-13 16:05:27 -0700485 rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file);
486 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000487 ALOGE("RS watchdog timeout: %i", rsc->watchdog.inRoot);
Jason Samsee803442011-10-13 16:05:27 -0700488 }
Jason Sams2382aba2011-09-13 15:41:01 -0700489}
490
491
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800492void Context::setPriority(int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800493 // Note: If we put this in the proper "background" policy
494 // the wallpapers can become completly unresponsive at times.
495 // This is probably not what we want for something the user is actively
496 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800497 mThreadPriority = p;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700498 setpriority(PRIO_PROCESS, mNativeThreadId, p);
Jason Sams9719bd42012-01-12 14:22:21 -0800499 mHal.funcs.setPriority(this, mThreadPriority);
Jason Sams15832442009-11-15 12:14:26 -0800500}
501
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800502Context::Context() {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700503 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700504 mRunning = false;
505 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700506 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700507 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800508 mError = RS_ERROR_NONE;
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700509 mTargetSdkVersion = 14;
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700510 mDPI = 96;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700511 mIsContextLite = false;
Stephen Hines86c6b5f2011-10-31 14:07:54 -0700512 memset(&watchdog, 0, sizeof(watchdog));
Tim Murray0e92fa32012-11-06 14:36:38 -0800513 mForceCpu = false;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700514 mContextType = RS_CONTEXT_TYPE_NORMAL;
Tim Murray4d252d62012-11-29 14:37:59 -0800515 mSynchronous = false;
Tim Murray0e92fa32012-11-06 14:36:38 -0800516}
517
518Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc,
Tim Murray84e3dea2013-09-09 16:12:51 -0700519 RsContextType ct, uint32_t flags) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700520 Context * rsc = new Context();
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700521
Tim Murray84e3dea2013-09-09 16:12:51 -0700522 if (flags & RS_CONTEXT_LOW_LATENCY) {
523 rsc->mForceCpu = true;
524 }
525 if (flags & RS_CONTEXT_SYNCHRONOUS) {
526 rsc->mSynchronous = true;
527 }
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700528 rsc->mContextType = ct;
Tim Murray0e92fa32012-11-06 14:36:38 -0800529
Jason Sams5c1c79a2010-11-03 14:27:11 -0700530 if (!rsc->initContext(dev, sc)) {
531 delete rsc;
532 return NULL;
533 }
534 return rsc;
535}
536
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700537Context * Context::createContextLite() {
538 Context * rsc = new Context();
539 rsc->mIsContextLite = true;
540 return rsc;
541}
542
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800543bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700544 pthread_mutex_lock(&gInitMutex);
545
Jason Sams1a4efa32011-05-17 15:01:29 -0700546 mIO.init();
Jason Sams5f27d6f2012-02-07 15:32:08 -0800547 mIO.setTimeoutCallback(printWatchdogInfo, this, 2e9);
Jason Sams1a4efa32011-05-17 15:01:29 -0700548
Jason Sams5c1c79a2010-11-03 14:27:11 -0700549 dev->addContext(this);
550 mDev = dev;
Jason Sams6b8552a2010-10-13 15:31:10 -0700551 if (sc) {
552 mUserSurfaceConfig = *sc;
553 } else {
554 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
555 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800556
Jason Sams6b8552a2010-10-13 15:31:10 -0700557 mIsGraphicsContext = sc != NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700558
Jason Samsa658e902009-06-04 14:35:01 -0700559 int status;
560 pthread_attr_t threadAttr;
561
Jason Samsfb03a222009-10-15 16:47:31 -0700562 pthread_mutex_unlock(&gInitMutex);
563
564 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700565
Jason Samsa658e902009-06-04 14:35:01 -0700566 status = pthread_attr_init(&threadAttr);
567 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000568 ALOGE("Failed to init thread attribute.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700569 return false;
Jason Samsa658e902009-06-04 14:35:01 -0700570 }
571
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700572 mHasSurface = false;
Jason Sams992a0b72009-06-23 12:22:47 -0700573
Jason Sams24371d92009-08-19 12:17:14 -0700574 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700575 timerSet(RS_TIMER_INTERNAL);
Tim Murray4d252d62012-11-29 14:37:59 -0800576 if (mSynchronous) {
577 threadProc(this);
578 } else {
579 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
580 if (status) {
581 ALOGE("Failed to start rs context thread.");
582 return false;
583 }
584 while (!mRunning && (mError == RS_ERROR_NONE)) {
585 usleep(100);
586 }
Jason Sams50869382009-08-18 17:07:09 -0700587
Tim Murray4d252d62012-11-29 14:37:59 -0800588 if (mError != RS_ERROR_NONE) {
589 ALOGE("Errors during thread init");
590 return false;
591 }
Jason Sams18133402010-07-20 15:09:00 -0700592
Tim Murray4d252d62012-11-29 14:37:59 -0800593 pthread_attr_destroy(&threadAttr);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700594 }
Jason Sams5c1c79a2010-11-03 14:27:11 -0700595 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700596}
597
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800598Context::~Context() {
Tim Murray20f62ca2013-05-09 11:57:35 -0700599 //ALOGV("%p Context::~Context", this);
Jason Samscf912de2011-01-09 16:09:51 -0800600
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700601 if (!mIsContextLite) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700602 mPaused = false;
603 void *res;
Jason Sams326e0dd2009-05-22 14:03:28 -0700604
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700605 mIO.shutdown();
606 int status = pthread_join(mThreadId, &res);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800607 rsAssert(mExit);
Jason Sams326e0dd2009-05-22 14:03:28 -0700608
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700609 if (mHal.funcs.shutdownDriver) {
610 mHal.funcs.shutdownDriver(this);
611 }
612
613 // Global structure cleanup.
614 pthread_mutex_lock(&gInitMutex);
615 if (mDev) {
616 mDev->removeContext(this);
617 mDev = NULL;
618 }
619 pthread_mutex_unlock(&gInitMutex);
Jason Sams51462c52011-01-25 00:26:25 -0800620 }
Tim Murray20f62ca2013-05-09 11:57:35 -0700621 //ALOGV("%p Context::~Context done", this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700622}
623
Jason Sams93eacc72012-12-18 14:26:57 -0800624#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700625void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800626 rsAssert(mIsGraphicsContext);
Jason Sams4b3de472011-04-06 17:52:23 -0700627 mHal.funcs.setSurface(this, w, h, sur);
Jason Sams458f2dc2009-11-03 13:58:36 -0800628
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700629 mHasSurface = sur != NULL;
Jason Sams4b3de472011-04-06 17:52:23 -0700630 mWidth = w;
631 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800632
Jason Sams4b3de472011-04-06 17:52:23 -0700633 if (mWidth && mHeight) {
Jason Sams771565f2010-05-14 15:30:29 -0700634 mStateVertex.updateSize(this);
Alex Sakhartchouka544b632011-07-19 17:50:29 -0700635 mFBOCache.updateSize();
Jason Sams458f2dc2009-11-03 13:58:36 -0800636 }
637}
638
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800639uint32_t Context::getCurrentSurfaceWidth() const {
640 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
641 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
642 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimX();
643 }
644 }
645 if (mFBOCache.mHal.state.depthTarget != NULL) {
646 return mFBOCache.mHal.state.depthTarget->getType()->getDimX();
647 }
648 return mWidth;
649}
650
651uint32_t Context::getCurrentSurfaceHeight() const {
652 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
653 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
654 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimY();
655 }
656 }
657 if (mFBOCache.mHal.state.depthTarget != NULL) {
658 return mFBOCache.mHal.state.depthTarget->getType()->getDimY();
659 }
660 return mHeight;
661}
662
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800663void Context::pause() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800664 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700665 mPaused = true;
666}
667
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800668void Context::resume() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800669 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700670 mPaused = false;
671}
672
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800673void Context::setRootScript(Script *s) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800674 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700675 mRootScript.set(s);
676}
677
Jason Sams60709252010-11-17 15:29:32 -0800678void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800679 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700680 if (pfs == NULL) {
681 mFragmentStore.set(mStateFragmentStore.mDefault);
682 } else {
683 mFragmentStore.set(pfs);
684 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700685}
686
Jason Sams60709252010-11-17 15:29:32 -0800687void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800688 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700689 if (pf == NULL) {
690 mFragment.set(mStateFragment.mDefault);
691 } else {
692 mFragment.set(pf);
693 }
Jason Samscfb1d112009-08-05 13:57:03 -0700694}
695
Jason Sams60709252010-11-17 15:29:32 -0800696void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800697 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700698 if (pr == NULL) {
699 mRaster.set(mStateRaster.mDefault);
700 } else {
701 mRaster.set(pr);
702 }
703}
704
Jason Sams60709252010-11-17 15:29:32 -0800705void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800706 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700707 if (pv == NULL) {
708 mVertex.set(mStateVertex.mDefault);
709 } else {
710 mVertex.set(pv);
711 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700712}
713
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800714void Context::setFont(Font *f) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700715 rsAssert(mIsGraphicsContext);
716 if (f == NULL) {
717 mFont.set(mStateFont.mDefault);
718 } else {
719 mFont.set(f);
720 }
721}
Jason Sams93eacc72012-12-18 14:26:57 -0800722#endif
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700723
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800724void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700725 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700726 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700727 mNames.add(obj);
728}
729
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800730void Context::removeName(ObjectBase *obj) {
731 for (size_t ct=0; ct < mNames.size(); ct++) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700732 if (obj == mNames[ct]) {
733 mNames.removeAt(ct);
734 return;
735 }
736 }
737}
738
Jason Sams1a4efa32011-05-17 15:01:29 -0700739RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) {
740 return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800741}
742
Jason Sams1a4efa32011-05-17 15:01:29 -0700743RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) {
744 return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen);
Jason Sams8c401ef2009-10-06 13:58:47 -0700745}
746
Jason Sams87319de2010-11-22 16:20:16 -0800747bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
748 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Sams1a4efa32011-05-17 15:01:29 -0700749
Jason Sams4961cce2013-04-11 16:11:46 -0700750 pthread_mutex_lock(&gMessageMutex);
751 bool ret = mIO.sendToClient(cmdID, subID, data, len, waitForSpace);
752 pthread_mutex_unlock(&gMessageMutex);
753 return ret;
Jason Sams8c401ef2009-10-06 13:58:47 -0700754}
755
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800756void Context::initToClient() {
757 while (!mRunning) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700758 usleep(100);
759 }
760}
761
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800762void Context::deinitToClient() {
Jason Sams1a4efa32011-05-17 15:01:29 -0700763 mIO.clientShutdown();
Jason Sams8c401ef2009-10-06 13:58:47 -0700764}
Jason Sams50869382009-08-18 17:07:09 -0700765
Jason Sams87319de2010-11-22 16:20:16 -0800766void Context::setError(RsError e, const char *msg) const {
Jason Samsa2cf7552010-03-03 13:03:18 -0800767 mError = e;
Jason Samsaad4bc52010-11-08 17:06:46 -0800768 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Samsa2cf7552010-03-03 13:03:18 -0800769}
770
771
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800772void Context::dumpDebug() const {
Steve Blockaf12ac62012-01-06 19:20:56 +0000773 ALOGE("RS Context debug %p", this);
774 ALOGE("RS Context debug");
Jason Sams13e26342009-11-24 12:26:35 -0800775
Steve Blockaf12ac62012-01-06 19:20:56 +0000776 ALOGE(" RS width %i, height %i", mWidth, mHeight);
777 ALOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
778 ALOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId);
Jason Sams13e26342009-11-24 12:26:35 -0800779}
Jason Samsa4a54e42009-06-10 18:39:40 -0700780
Jason Sams326e0dd2009-05-22 14:03:28 -0700781///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700782//
Jason Sams326e0dd2009-05-22 14:03:28 -0700783
784namespace android {
785namespace renderscript {
786
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800787void rsi_ContextFinish(Context *rsc) {
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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800797void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700798 Sampler *s = static_cast<Sampler *>(vs);
799
800 if (slot > RS_MAX_SAMPLER_SLOT) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000801 ALOGE("Invalid sampler slot");
Jason Sams326e0dd2009-05-22 14:03:28 -0700802 return;
803 }
804
805 s->bindToContext(&rsc->mStateSampler, slot);
806}
807
Jason Sams93eacc72012-12-18 14:26:57 -0800808#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800809void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Samsccc010b2010-05-13 18:30:11 -0700810 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams60709252010-11-17 15:29:32 -0800811 rsc->setProgramStore(pfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700812}
813
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800814void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700815 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Sams60709252010-11-17 15:29:32 -0800816 rsc->setProgramFragment(pf);
Jason Sams326e0dd2009-05-22 14:03:28 -0700817}
818
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800819void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Sams5fd09d82009-09-23 13:57:02 -0700820 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Sams60709252010-11-17 15:29:32 -0800821 rsc->setProgramRaster(pr);
Jason Sams5fd09d82009-09-23 13:57:02 -0700822}
823
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800824void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700825 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Sams60709252010-11-17 15:29:32 -0800826 rsc->setProgramVertex(pv);
Jason Sams326e0dd2009-05-22 14:03:28 -0700827}
828
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800829void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700830 Font *font = static_cast<Font *>(vfont);
831 rsc->setFont(font);
832}
Jason Sams93eacc72012-12-18 14:26:57 -0800833#endif
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700834
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700835void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700836 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700837 rsc->assignName(ob, name, name_length);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700838}
Jason Sams326e0dd2009-05-22 14:03:28 -0700839
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800840void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams2353ae32010-10-14 17:48:46 -0700841 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -0700842 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700843 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700844}
845
Jason Sams93eacc72012-12-18 14:26:57 -0800846#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800847void rsi_ContextPause(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700848 rsc->pause();
849}
850
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800851void rsi_ContextResume(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700852 rsc->resume();
853}
854
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700855void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
Mathias Agopianfa402862010-02-12 14:04:35 -0800856 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800857}
Jason Sams93eacc72012-12-18 14:26:57 -0800858#endif
Jason Sams613cad12009-11-12 15:10:25 -0800859
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800860void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800861 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800862}
863
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800864void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Samsc21cf402009-11-17 17:26:46 -0800865 ObjectBase::dumpAll(rsc);
866}
867
Jason Sams741aac92010-12-24 14:38:39 -0800868void rsi_ContextDestroyWorker(Context *rsc) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700869 rsc->destroyWorkerThreadResources();
Jason Sams741aac92010-12-24 14:38:39 -0800870}
871
Jason Samsc975cf42011-04-28 18:26:48 -0700872void rsi_ContextDestroy(Context *rsc) {
Tim Murray20f62ca2013-05-09 11:57:35 -0700873 //ALOGV("%p rsContextDestroy", rsc);
Jason Sams741aac92010-12-24 14:38:39 -0800874 rsContextDestroyWorker(rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800875 delete rsc;
Tim Murray20f62ca2013-05-09 11:57:35 -0700876 //ALOGV("%p rsContextDestroy done", rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800877}
878
Jason Samsc975cf42011-04-28 18:26:48 -0700879RsMessageToClientType rsi_ContextPeekMessage(Context *rsc,
Jason Sams186e5912011-04-26 14:50:00 -0700880 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700881 uint32_t * subID, size_t subID_length) {
882 return rsc->peekMessageToClient(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800883}
884
Jason Samsc975cf42011-04-28 18:26:48 -0700885RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length,
Jason Sams186e5912011-04-26 14:50:00 -0700886 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700887 uint32_t * subID, size_t subID_length) {
Jason Sams186e5912011-04-26 14:50:00 -0700888 rsAssert(subID_length == sizeof(uint32_t));
889 rsAssert(receiveLen_length == sizeof(size_t));
Jason Sams1a4efa32011-05-17 15:01:29 -0700890 return rsc->getMessageToClient(data, receiveLen, subID, data_length);
Jason Sams8c401ef2009-10-06 13:58:47 -0700891}
892
Jason Samsc975cf42011-04-28 18:26:48 -0700893void rsi_ContextInitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700894 rsc->initToClient();
895}
896
Jason Samsc975cf42011-04-28 18:26:48 -0700897void rsi_ContextDeinitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700898 rsc->deinitToClient();
899}
900
Jason Sams70265202013-02-05 19:20:47 -0800901void rsi_ContextSendMessage(Context *rsc, uint32_t id, const uint8_t *data, size_t len) {
902 rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, id, len, true);
903}
904
Jason Samsc975cf42011-04-28 18:26:48 -0700905}
906}
907
Tim Murrayc2ce7072013-07-17 18:38:53 -0700908extern "C" RsContext rsContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion,
Tim Murray84e3dea2013-09-09 16:12:51 -0700909 RsContextType ct, uint32_t flags) {
Tim Murray20f62ca2013-05-09 11:57:35 -0700910 //ALOGV("rsContextCreate dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700911 Device * dev = static_cast<Device *>(vdev);
Tim Murray84e3dea2013-09-09 16:12:51 -0700912 Context *rsc = Context::createContext(dev, NULL, ct, flags);
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700913 if (rsc) {
914 rsc->setTargetSdkVersion(sdkVersion);
915 }
Jason Sams789ca832011-05-18 17:36:02 -0700916 return rsc;
917}
918
Jason Sams93eacc72012-12-18 14:26:57 -0800919#ifndef RS_COMPATIBILITY_LIB
Jason Sams789ca832011-05-18 17:36:02 -0700920RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700921 uint32_t sdkVersion, RsSurfaceConfig sc,
922 uint32_t dpi) {
Tim Murray20f62ca2013-05-09 11:57:35 -0700923 //ALOGV("rsContextCreateGL dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700924 Device * dev = static_cast<Device *>(vdev);
925 Context *rsc = Context::createContext(dev, &sc);
Jason Sams9544f762011-07-13 16:09:42 -0700926 if (rsc) {
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700927 rsc->setTargetSdkVersion(sdkVersion);
Jason Sams9544f762011-07-13 16:09:42 -0700928 rsc->setDPI(dpi);
929 }
Tim Murray20f62ca2013-05-09 11:57:35 -0700930 //ALOGV("%p rsContextCreateGL ret", rsc);
Jason Sams789ca832011-05-18 17:36:02 -0700931 return rsc;
932}
Jason Sams93eacc72012-12-18 14:26:57 -0800933#endif
Jason Sams789ca832011-05-18 17:36:02 -0700934
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700935// Only to be called at a3d load time, before object is visible to user
936// not thread safe
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800937void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700938 ObjectBase *ob = static_cast<ObjectBase *>(obj);
939 (*name) = ob->getName();
940}