blob: eec2dcd09611edf275cb86ae55042e079be70d7c [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>
35
Tim Murray0b575de2013-03-15 15:56:43 -070036#ifndef RS_SERVER
37#include <cutils/properties.h>
38#endif
39
40#ifdef RS_SERVER
41// Android exposes gettid(), standard Linux does not
42static pid_t gettid() {
43 return syscall(SYS_gettid);
44}
45#endif
46
Jason Sams326e0dd2009-05-22 14:03:28 -070047using namespace android;
48using namespace android::renderscript;
49
Jason Samsfb03a222009-10-15 16:47:31 -070050pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Stephen Hinesca3f09c2011-01-07 15:11:30 -080051pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070052
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080053bool Context::initGLThread() {
Jason Sams6b8552a2010-10-13 15:31:10 -070054 pthread_mutex_lock(&gInitMutex);
Jason Sams6b8552a2010-10-13 15:31:10 -070055
Jason Sams4b3de472011-04-06 17:52:23 -070056 if (!mHal.funcs.initGraphics(this)) {
Jason Sams5c1c79a2010-11-03 14:27:11 -070057 pthread_mutex_unlock(&gInitMutex);
Steve Blockaf12ac62012-01-06 19:20:56 +000058 ALOGE("%p initGraphics failed", this);
Jason Sams5c1c79a2010-11-03 14:27:11 -070059 return false;
Jason Sams6b8552a2010-10-13 15:31:10 -070060 }
61
Jason Sams6b8552a2010-10-13 15:31:10 -070062 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -070063 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -070064}
65
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080066void Context::deinitEGL() {
Jason Sams93eacc72012-12-18 14:26:57 -080067#ifndef RS_COMPATIBILITY_LIB
Jason Sams4b3de472011-04-06 17:52:23 -070068 mHal.funcs.shutdownGraphics(this);
Jason Sams93eacc72012-12-18 14:26:57 -080069#endif
Jason Sams33b6e3b2009-10-27 14:44:31 -070070}
71
Jason Sams60709252010-11-17 15:29:32 -080072Context::PushState::PushState(Context *con) {
73 mRsc = con;
Jason Sams93eacc72012-12-18 14:26:57 -080074#ifndef RS_COMPATIBILITY_LIB
Jason Samsc946b612011-02-23 14:47:17 -080075 if (con->mIsGraphicsContext) {
76 mFragment.set(con->getProgramFragment());
77 mVertex.set(con->getProgramVertex());
78 mStore.set(con->getProgramStore());
79 mRaster.set(con->getProgramRaster());
80 mFont.set(con->getFont());
81 }
Jason Sams93eacc72012-12-18 14:26:57 -080082#endif
Jason Sams60709252010-11-17 15:29:32 -080083}
84
85Context::PushState::~PushState() {
Jason Sams93eacc72012-12-18 14:26:57 -080086#ifndef RS_COMPATIBILITY_LIB
Jason Samsc946b612011-02-23 14:47:17 -080087 if (mRsc->mIsGraphicsContext) {
88 mRsc->setProgramFragment(mFragment.get());
89 mRsc->setProgramVertex(mVertex.get());
90 mRsc->setProgramStore(mStore.get());
91 mRsc->setProgramRaster(mRaster.get());
92 mRsc->setFont(mFont.get());
93 }
Jason Sams93eacc72012-12-18 14:26:57 -080094#endif
Jason Sams60709252010-11-17 15:29:32 -080095}
96
Jason Sams33b6e3b2009-10-27 14:44:31 -070097
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080098uint32_t Context::runScript(Script *s) {
Shih-wei Liaoda3b58d2012-08-03 04:24:33 -070099 PushState ps(this);
Jason Sams10308932009-06-09 12:15:30 -0700100
Jason Samsc61346b2010-05-28 18:23:22 -0700101 uint32_t ret = s->run(this);
Jason Samsc9d43db2009-07-28 12:02:16 -0700102 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700103}
104
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800105uint32_t Context::runRootScript() {
Jason Sams2dca84d2009-12-09 11:05:45 -0800106 timerSet(RS_TIMER_SCRIPT);
Jason Sams93eacc72012-12-18 14:26:57 -0800107#ifndef RS_COMPATIBILITY_LIB
Jason Sams8c401ef2009-10-06 13:58:47 -0700108 mStateFragmentStore.mLast.clear();
Jason Sams93eacc72012-12-18 14:26:57 -0800109#endif
Jason Sams2382aba2011-09-13 15:41:01 -0700110 watchdog.inRoot = true;
Jason Samsc61346b2010-05-28 18:23:22 -0700111 uint32_t ret = runScript(mRootScript.get());
Jason Sams2382aba2011-09-13 15:41:01 -0700112 watchdog.inRoot = false;
Jason Sams8cfdd242009-10-14 15:43:53 -0700113
Jason Samscfb1d112009-08-05 13:57:03 -0700114 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700115}
116
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800117uint64_t Context::getTime() const {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700118#ifndef ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -0700119 struct timespec t;
120 clock_gettime(CLOCK_MONOTONIC, &t);
121 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700122#else
123 return 0;
124#endif //ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -0700125}
126
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800127void Context::timerReset() {
Jason Sams24371d92009-08-19 12:17:14 -0700128 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
129 mTimers[ct] = 0;
130 }
131}
132
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800133void Context::timerInit() {
Jason Sams24371d92009-08-19 12:17:14 -0700134 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700135 mTimeFrame = mTimeLast;
136 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700137 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700138 mAverageFPSFrameCount = 0;
139 mAverageFPSStartTime = mTimeLast;
140 mAverageFPS = 0;
Jason Sams24371d92009-08-19 12:17:14 -0700141 timerReset();
142}
143
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800144void Context::timerFrame() {
Jason Sams1d54f102009-09-03 15:43:13 -0700145 mTimeLastFrame = mTimeFrame;
146 mTimeFrame = getTime();
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700147 // Update average fps
148 const uint64_t averageFramerateInterval = 1000 * 1000000;
149 mAverageFPSFrameCount ++;
150 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800151 if (inverval >= averageFramerateInterval) {
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700152 inverval = inverval / 1000000;
153 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
154 mAverageFPSFrameCount = 0;
155 mAverageFPSStartTime = mTimeFrame;
156 }
Jason Sams1d54f102009-09-03 15:43:13 -0700157}
158
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800159void Context::timerSet(Timers tm) {
Jason Sams24371d92009-08-19 12:17:14 -0700160 uint64_t last = mTimeLast;
161 mTimeLast = getTime();
162 mTimers[mTimerActive] += mTimeLast - last;
163 mTimerActive = tm;
164}
165
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800166void Context::timerPrint() {
Jason Sams24371d92009-08-19 12:17:14 -0700167 double total = 0;
168 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
169 total += mTimers[ct];
170 }
Jason Sams1d54f102009-09-03 15:43:13 -0700171 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800172 mTimeMSLastFrame = frame / 1000000;
173 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
174 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700175
Jason Sams2dca84d2009-12-09 11:05:45 -0800176
177 if (props.mLogTimes) {
Steve Block65982012011-10-20 11:56:00 +0100178 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 -0800179 mTimeMSLastFrame,
180 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
181 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
182 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700183 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
184 mAverageFPS);
Jason Sams2dca84d2009-12-09 11:05:45 -0800185 }
Jason Sams24371d92009-08-19 12:17:14 -0700186}
187
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800188bool Context::setupCheck() {
Jason Sams93eacc72012-12-18 14:26:57 -0800189#ifndef RS_COMPATIBILITY_LIB
Jason Sams721acc42011-04-06 11:23:54 -0700190 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700191 mFragment->setup(this, &mStateFragment);
Jason Sams721acc42011-04-06 11:23:54 -0700192 mRaster->setup(this, &mStateRaster);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700193 mVertex->setup(this, &mStateVertex);
194 mFBOCache.setup(this);
Jason Sams93eacc72012-12-18 14:26:57 -0800195#endif
Jason Samsa2cf7552010-03-03 13:03:18 -0800196 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700197}
198
Jason Sams93eacc72012-12-18 14:26:57 -0800199#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700200void Context::setupProgramStore() {
Jason Sams721acc42011-04-06 11:23:54 -0700201 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700202}
Jason Sams93eacc72012-12-18 14:26:57 -0800203#endif
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700204
Jason Samsd1f7da62012-03-15 19:18:03 -0700205static uint32_t getProp(const char *str) {
Tim Murray0b575de2013-03-15 15:56:43 -0700206#ifndef RS_SERVER
Joe Onorato76371ff2009-09-23 16:37:36 -0700207 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700208 property_get(str, buf, "0");
Jason Samsd1f7da62012-03-15 19:18:03 -0700209 return atoi(buf);
Tim Murray0b575de2013-03-15 15:56:43 -0700210#else
211 return 0;
212#endif
Joe Onorato76371ff2009-09-23 16:37:36 -0700213}
Jason Sams326e0dd2009-05-22 14:03:28 -0700214
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800215void Context::displayDebugStats() {
Jason Sams93eacc72012-12-18 14:26:57 -0800216#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700217 char buffer[128];
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700218 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700219 float oldR, oldG, oldB, oldA;
220 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700221 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700222
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700223 ObjectBaseRef<Font> lastFont(getFont());
224 setFont(NULL);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700225 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700226 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700227 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700228
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700229 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700230 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700231
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700232 setFont(lastFont.get());
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700233 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Jason Sams93eacc72012-12-18 14:26:57 -0800234#endif
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700235}
236
Tim Murray0c66f072012-10-23 14:05:02 -0700237bool Context::loadRuntime(const char* filename, Context* rsc) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700238
Tim Murray0c66f072012-10-23 14:05:02 -0700239 // TODO: store the driverSO somewhere so we can dlclose later
Stephen Hines91dfcdb2012-09-07 18:23:35 -0700240 void *driverSO = NULL;
241
Tim Murray0c66f072012-10-23 14:05:02 -0700242 driverSO = dlopen(filename, RTLD_LAZY);
Stephen Hines91dfcdb2012-09-07 18:23:35 -0700243 if (driverSO == NULL) {
Tim Murray0c66f072012-10-23 14:05:02 -0700244 ALOGE("Failed loading RS driver: %s", dlerror());
245 return false;
Stephen Hines414a4612012-09-05 18:05:08 -0700246 }
247
248 // Need to call dlerror() to clear buffer before using it for dlsym().
249 (void) dlerror();
250 typedef bool (*HalSig)(Context*, uint32_t, uint32_t);
251 HalSig halInit = (HalSig) dlsym(driverSO, "rsdHalInit");
252
253 // If we can't find the C variant, we go looking for the C++ version.
254 if (halInit == NULL) {
255 ALOGW("Falling back to find C++ rsdHalInit: %s", dlerror());
256 halInit = (HalSig) dlsym(driverSO,
257 "_Z10rsdHalInitPN7android12renderscript7ContextEjj");
258 }
259
260 if (halInit == NULL) {
Stephen Hines414a4612012-09-05 18:05:08 -0700261 dlclose(driverSO);
262 ALOGE("Failed to find rsdHalInit: %s", dlerror());
Tim Murray0c66f072012-10-23 14:05:02 -0700263 return false;
Stephen Hines414a4612012-09-05 18:05:08 -0700264 }
265
266 if (!(*halInit)(rsc, 0, 0)) {
Stephen Hines414a4612012-09-05 18:05:08 -0700267 dlclose(driverSO);
Steve Blockaf12ac62012-01-06 19:20:56 +0000268 ALOGE("Hal init failed");
Tim Murray0c66f072012-10-23 14:05:02 -0700269 return false;
Jason Sams83c451a2011-04-21 11:46:50 -0700270 }
Tim Murray0c66f072012-10-23 14:05:02 -0700271
272 //validate HAL struct
273
274
275 return true;
276}
277
Jason Sams110f1812013-03-14 16:02:18 -0700278extern "C" bool rsdHalInit(RsContext c, uint32_t version_major, uint32_t version_minor);
279
Tim Murray0c66f072012-10-23 14:05:02 -0700280void * Context::threadProc(void *vrsc) {
281 Context *rsc = static_cast<Context *>(vrsc);
282#ifndef ANDROID_RS_SERIALIZE
283 rsc->mNativeThreadId = gettid();
Jason Sams93eacc72012-12-18 14:26:57 -0800284#ifndef RS_COMPATIBILITY_LIB
Tim Murray4d252d62012-11-29 14:37:59 -0800285 if (!rsc->isSynchronous()) {
286 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
287 }
Tim Murray0c66f072012-10-23 14:05:02 -0700288 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams93eacc72012-12-18 14:26:57 -0800289#else
290 if (!rsc->isSynchronous()) {
291 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, -4);
292 }
293 rsc->mThreadPriority = -4;
294#endif
Tim Murray0c66f072012-10-23 14:05:02 -0700295#endif //ANDROID_RS_SERIALIZE
296 rsc->props.mLogTimes = getProp("debug.rs.profile") != 0;
297 rsc->props.mLogScripts = getProp("debug.rs.script") != 0;
298 rsc->props.mLogObjects = getProp("debug.rs.object") != 0;
299 rsc->props.mLogShaders = getProp("debug.rs.shader") != 0;
300 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes") != 0;
301 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms") != 0;
302 rsc->props.mLogVisual = getProp("debug.rs.visual") != 0;
303 rsc->props.mDebugMaxThreads = getProp("debug.rs.max-threads");
304
305 bool loadDefault = true;
306
307 // Provide a mechanism for dropping in a different RS driver.
Jason Sams110f1812013-03-14 16:02:18 -0700308#ifndef RS_COMPATIBILITY_LIB
Tim Murray0c66f072012-10-23 14:05:02 -0700309#ifdef OVERRIDE_RS_DRIVER
310#define XSTR(S) #S
311#define STR(S) XSTR(S)
312#define OVERRIDE_RS_DRIVER_STRING STR(OVERRIDE_RS_DRIVER)
313
314 if (getProp("debug.rs.default-CPU-driver") != 0) {
315 ALOGE("Skipping override driver and loading default CPU driver");
Tim Murray0e92fa32012-11-06 14:36:38 -0800316 } else if (rsc->mForceCpu) {
317 ALOGV("Application requested CPU execution");
Tim Murray0c66f072012-10-23 14:05:02 -0700318 } else {
319 if (loadRuntime(OVERRIDE_RS_DRIVER_STRING, rsc)) {
320 ALOGE("Successfully loaded runtime: %s", OVERRIDE_RS_DRIVER_STRING);
321 loadDefault = false;
322 } else {
323 ALOGE("Failed to load runtime %s, loading default", OVERRIDE_RS_DRIVER_STRING);
324 }
325 }
326
327#undef XSTR
328#undef STR
329#endif // OVERRIDE_RS_DRIVER
330
331 if (loadDefault) {
332 if (!loadRuntime("libRSDriver.so", rsc)) {
333 ALOGE("Failed to load default runtime!");
Stephen Hines6f01bcf2012-11-19 15:18:16 -0800334 rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed loading RS driver");
Tim Murray0c66f072012-10-23 14:05:02 -0700335 return NULL;
336 }
337 }
Jason Sams110f1812013-03-14 16:02:18 -0700338#else // RS_COMPATIBILITY_LIB
339 if (rsdHalInit(rsc, 0, 0) != true) {
340 return NULL;
341 }
342#endif
343
Tim Murray0c66f072012-10-23 14:05:02 -0700344
Jason Sams83c451a2011-04-21 11:46:50 -0700345 rsc->mHal.funcs.setPriority(rsc, rsc->mThreadPriority);
Jason Samse5769102009-06-19 16:03:18 -0700346
Jason Sams93eacc72012-12-18 14:26:57 -0800347#ifndef RS_COMPATIBILITY_LIB
Jason Sams83c451a2011-04-21 11:46:50 -0700348 if (rsc->mIsGraphicsContext) {
Jason Samsd3e71072011-05-03 15:01:58 -0700349 if (!rsc->initGLThread()) {
350 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
351 return NULL;
352 }
353
Jason Sams83c451a2011-04-21 11:46:50 -0700354 rsc->mStateRaster.init(rsc);
355 rsc->setProgramRaster(NULL);
356 rsc->mStateVertex.init(rsc);
357 rsc->setProgramVertex(NULL);
358 rsc->mStateFragment.init(rsc);
359 rsc->setProgramFragment(NULL);
360 rsc->mStateFragmentStore.init(rsc);
361 rsc->setProgramStore(NULL);
362 rsc->mStateFont.init(rsc);
363 rsc->setFont(NULL);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700364 rsc->mStateSampler.init(rsc);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700365 rsc->mFBOCache.init(rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700366 }
Jason Sams93eacc72012-12-18 14:26:57 -0800367#endif
Jason Sams8ce125b2009-06-17 16:52:59 -0700368
Jason Sams83c451a2011-04-21 11:46:50 -0700369 rsc->mRunning = true;
Tim Murray4d252d62012-11-29 14:37:59 -0800370
371 if (rsc->isSynchronous()) {
372 return NULL;
373 }
374
Jason Sams5f27d6f2012-02-07 15:32:08 -0800375 if (!rsc->mIsGraphicsContext) {
376 while (!rsc->mExit) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800377 rsc->mIO.playCoreCommands(rsc, -1);
Jason Samse0aab4a2011-08-12 15:05:15 -0700378 }
Jason Sams93eacc72012-12-18 14:26:57 -0800379#ifndef RS_COMPATIBILITY_LIB
Jason Sams5f27d6f2012-02-07 15:32:08 -0800380 } else {
381#ifndef ANDROID_RS_SERIALIZE
382 DisplayEventReceiver displayEvent;
383 DisplayEventReceiver::Event eventBuffer[1];
384#endif
385 int vsyncRate = 0;
386 int targetRate = 0;
Jason Samse0aab4a2011-08-12 15:05:15 -0700387
Jason Sams5f27d6f2012-02-07 15:32:08 -0800388 bool drawOnce = false;
389 while (!rsc->mExit) {
390 rsc->timerSet(RS_TIMER_IDLE);
Jason Sams326e0dd2009-05-22 14:03:28 -0700391
Jason Sams5f27d6f2012-02-07 15:32:08 -0800392#ifndef ANDROID_RS_SERIALIZE
Jason Sams9afd9a52012-02-17 16:59:50 -0800393 if (!rsc->mRootScript.get() || !rsc->mHasSurface || rsc->mPaused) {
394 targetRate = 0;
395 }
396
Jason Sams5f27d6f2012-02-07 15:32:08 -0800397 if (vsyncRate != targetRate) {
398 displayEvent.setVsyncRate(targetRate);
399 vsyncRate = targetRate;
400 }
401 if (targetRate) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800402 drawOnce |= rsc->mIO.playCoreCommands(rsc, displayEvent.getFd());
Jason Sams5f27d6f2012-02-07 15:32:08 -0800403 while (displayEvent.getEvents(eventBuffer, 1) != 0) {
404 //ALOGE("vs2 time past %lld", (rsc->getTime() - eventBuffer[0].header.timestamp) / 1000000);
405 }
406 } else
407#endif
408 {
Jason Sams963a2fb2012-02-09 14:36:14 -0800409 drawOnce |= rsc->mIO.playCoreCommands(rsc, -1);
Jason Sams83c451a2011-04-21 11:46:50 -0700410 }
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700411
Jason Sams5f27d6f2012-02-07 15:32:08 -0800412 if ((rsc->mRootScript.get() != NULL) && rsc->mHasSurface &&
413 (targetRate || drawOnce) && !rsc->mPaused) {
414
415 drawOnce = false;
416 targetRate = ((rsc->runRootScript() + 15) / 16);
417
418 if (rsc->props.mLogVisual) {
419 rsc->displayDebugStats();
420 }
421
422 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
423 rsc->mHal.funcs.swap(rsc);
424 rsc->timerFrame();
425 rsc->timerSet(RS_TIMER_INTERNAL);
426 rsc->timerPrint();
427 rsc->timerReset();
428 }
Jason Sams83c451a2011-04-21 11:46:50 -0700429 }
Jason Sams93eacc72012-12-18 14:26:57 -0800430#endif
Jason Sams83c451a2011-04-21 11:46:50 -0700431 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700432
Steve Block65982012011-10-20 11:56:00 +0100433 ALOGV("%p RS Thread exiting", rsc);
Jason Samse514b452009-09-25 14:51:22 -0700434
Jason Sams93eacc72012-12-18 14:26:57 -0800435#ifndef RS_COMPATIBILITY_LIB
Jason Sams83c451a2011-04-21 11:46:50 -0700436 if (rsc->mIsGraphicsContext) {
437 pthread_mutex_lock(&gInitMutex);
438 rsc->deinitEGL();
439 pthread_mutex_unlock(&gInitMutex);
440 }
Jason Sams93eacc72012-12-18 14:26:57 -0800441#endif
Jason Sams33b6e3b2009-10-27 14:44:31 -0700442
Steve Block65982012011-10-20 11:56:00 +0100443 ALOGV("%p RS Thread exited", rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700444 return NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700445}
446
Jason Sams741aac92010-12-24 14:38:39 -0800447void Context::destroyWorkerThreadResources() {
Steve Block65982012011-10-20 11:56:00 +0100448 //ALOGV("destroyWorkerThreadResources 1");
Jason Sams2e8665d2011-01-27 00:14:13 -0800449 ObjectBase::zeroAllUserRef(this);
Jason Sams93eacc72012-12-18 14:26:57 -0800450#ifndef RS_COMPATIBILITY_LIB
Jason Sams741aac92010-12-24 14:38:39 -0800451 if (mIsGraphicsContext) {
452 mRaster.clear();
453 mFragment.clear();
454 mVertex.clear();
455 mFragmentStore.clear();
456 mFont.clear();
457 mRootScript.clear();
458 mStateRaster.deinit(this);
459 mStateVertex.deinit(this);
460 mStateFragment.deinit(this);
461 mStateFragmentStore.deinit(this);
462 mStateFont.deinit(this);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700463 mStateSampler.deinit(this);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700464 mFBOCache.deinit(this);
Jason Sams741aac92010-12-24 14:38:39 -0800465 }
Jason Sams93eacc72012-12-18 14:26:57 -0800466#endif
Jason Samsc7cec1e2011-08-18 18:01:33 -0700467 ObjectBase::freeAllChildren(this);
Jason Samscf912de2011-01-09 16:09:51 -0800468 mExit = true;
Jason Sams5f27d6f2012-02-07 15:32:08 -0800469 //ALOGV("destroyWorkerThreadResources 2");
Jason Sams741aac92010-12-24 14:38:39 -0800470}
471
Jason Sams2382aba2011-09-13 15:41:01 -0700472void Context::printWatchdogInfo(void *ctx) {
473 Context *rsc = (Context *)ctx;
Jason Samsee803442011-10-13 16:05:27 -0700474 if (rsc->watchdog.command && rsc->watchdog.file) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000475 ALOGE("RS watchdog timeout: %i %s line %i %s", rsc->watchdog.inRoot,
Jason Samsee803442011-10-13 16:05:27 -0700476 rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file);
477 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000478 ALOGE("RS watchdog timeout: %i", rsc->watchdog.inRoot);
Jason Samsee803442011-10-13 16:05:27 -0700479 }
Jason Sams2382aba2011-09-13 15:41:01 -0700480}
481
482
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800483void Context::setPriority(int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800484 // Note: If we put this in the proper "background" policy
485 // the wallpapers can become completly unresponsive at times.
486 // This is probably not what we want for something the user is actively
487 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800488 mThreadPriority = p;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700489 setpriority(PRIO_PROCESS, mNativeThreadId, p);
Jason Sams9719bd42012-01-12 14:22:21 -0800490 mHal.funcs.setPriority(this, mThreadPriority);
Jason Sams15832442009-11-15 12:14:26 -0800491}
492
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800493Context::Context() {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700494 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700495 mRunning = false;
496 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700497 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700498 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800499 mError = RS_ERROR_NONE;
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700500 mTargetSdkVersion = 14;
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700501 mDPI = 96;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700502 mIsContextLite = false;
Stephen Hines86c6b5f2011-10-31 14:07:54 -0700503 memset(&watchdog, 0, sizeof(watchdog));
Tim Murray0e92fa32012-11-06 14:36:38 -0800504 mForceCpu = false;
Tim Murray4d252d62012-11-29 14:37:59 -0800505 mSynchronous = false;
Tim Murray0e92fa32012-11-06 14:36:38 -0800506}
507
508Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc,
Tim Murray4d252d62012-11-29 14:37:59 -0800509 bool forceCpu, bool synchronous) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700510 Context * rsc = new Context();
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700511
Tim Murray0e92fa32012-11-06 14:36:38 -0800512 rsc->mForceCpu = forceCpu;
Tim Murray4d252d62012-11-29 14:37:59 -0800513 rsc->mSynchronous = synchronous;
Tim Murray0e92fa32012-11-06 14:36:38 -0800514
Jason Sams5c1c79a2010-11-03 14:27:11 -0700515 if (!rsc->initContext(dev, sc)) {
516 delete rsc;
517 return NULL;
518 }
519 return rsc;
520}
521
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700522Context * Context::createContextLite() {
523 Context * rsc = new Context();
524 rsc->mIsContextLite = true;
525 return rsc;
526}
527
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800528bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700529 pthread_mutex_lock(&gInitMutex);
530
Jason Sams1a4efa32011-05-17 15:01:29 -0700531 mIO.init();
Jason Sams5f27d6f2012-02-07 15:32:08 -0800532 mIO.setTimeoutCallback(printWatchdogInfo, this, 2e9);
Jason Sams1a4efa32011-05-17 15:01:29 -0700533
Jason Sams5c1c79a2010-11-03 14:27:11 -0700534 dev->addContext(this);
535 mDev = dev;
Jason Sams6b8552a2010-10-13 15:31:10 -0700536 if (sc) {
537 mUserSurfaceConfig = *sc;
538 } else {
539 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
540 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800541
Jason Sams6b8552a2010-10-13 15:31:10 -0700542 mIsGraphicsContext = sc != NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700543
Jason Samsa658e902009-06-04 14:35:01 -0700544 int status;
545 pthread_attr_t threadAttr;
546
Jason Samsfb03a222009-10-15 16:47:31 -0700547 pthread_mutex_unlock(&gInitMutex);
548
549 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700550
Jason Samsa658e902009-06-04 14:35:01 -0700551 status = pthread_attr_init(&threadAttr);
552 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000553 ALOGE("Failed to init thread attribute.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700554 return false;
Jason Samsa658e902009-06-04 14:35:01 -0700555 }
556
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700557 mHasSurface = false;
Jason Sams992a0b72009-06-23 12:22:47 -0700558
Jason Sams24371d92009-08-19 12:17:14 -0700559 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700560 timerSet(RS_TIMER_INTERNAL);
Tim Murray4d252d62012-11-29 14:37:59 -0800561 if (mSynchronous) {
562 threadProc(this);
563 } else {
564 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
565 if (status) {
566 ALOGE("Failed to start rs context thread.");
567 return false;
568 }
569 while (!mRunning && (mError == RS_ERROR_NONE)) {
570 usleep(100);
571 }
Jason Sams50869382009-08-18 17:07:09 -0700572
Tim Murray4d252d62012-11-29 14:37:59 -0800573 if (mError != RS_ERROR_NONE) {
574 ALOGE("Errors during thread init");
575 return false;
576 }
Jason Sams18133402010-07-20 15:09:00 -0700577
Tim Murray4d252d62012-11-29 14:37:59 -0800578 pthread_attr_destroy(&threadAttr);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700579 }
Jason Sams5c1c79a2010-11-03 14:27:11 -0700580 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700581}
582
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800583Context::~Context() {
Steve Block65982012011-10-20 11:56:00 +0100584 ALOGV("%p Context::~Context", this);
Jason Samscf912de2011-01-09 16:09:51 -0800585
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700586 if (!mIsContextLite) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700587 mPaused = false;
588 void *res;
Jason Sams326e0dd2009-05-22 14:03:28 -0700589
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700590 mIO.shutdown();
591 int status = pthread_join(mThreadId, &res);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800592 rsAssert(mExit);
Jason Sams326e0dd2009-05-22 14:03:28 -0700593
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700594 if (mHal.funcs.shutdownDriver) {
595 mHal.funcs.shutdownDriver(this);
596 }
597
598 // Global structure cleanup.
599 pthread_mutex_lock(&gInitMutex);
600 if (mDev) {
601 mDev->removeContext(this);
602 mDev = NULL;
603 }
604 pthread_mutex_unlock(&gInitMutex);
Jason Sams51462c52011-01-25 00:26:25 -0800605 }
Steve Block65982012011-10-20 11:56:00 +0100606 ALOGV("%p Context::~Context done", this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700607}
608
Jason Sams93eacc72012-12-18 14:26:57 -0800609#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700610void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800611 rsAssert(mIsGraphicsContext);
Jason Sams4b3de472011-04-06 17:52:23 -0700612 mHal.funcs.setSurface(this, w, h, sur);
Jason Sams458f2dc2009-11-03 13:58:36 -0800613
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700614 mHasSurface = sur != NULL;
Jason Sams4b3de472011-04-06 17:52:23 -0700615 mWidth = w;
616 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800617
Jason Sams4b3de472011-04-06 17:52:23 -0700618 if (mWidth && mHeight) {
Jason Sams771565f2010-05-14 15:30:29 -0700619 mStateVertex.updateSize(this);
Alex Sakhartchouka544b632011-07-19 17:50:29 -0700620 mFBOCache.updateSize();
Jason Sams458f2dc2009-11-03 13:58:36 -0800621 }
622}
623
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800624uint32_t Context::getCurrentSurfaceWidth() const {
625 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
626 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
627 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimX();
628 }
629 }
630 if (mFBOCache.mHal.state.depthTarget != NULL) {
631 return mFBOCache.mHal.state.depthTarget->getType()->getDimX();
632 }
633 return mWidth;
634}
635
636uint32_t Context::getCurrentSurfaceHeight() const {
637 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
638 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
639 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimY();
640 }
641 }
642 if (mFBOCache.mHal.state.depthTarget != NULL) {
643 return mFBOCache.mHal.state.depthTarget->getType()->getDimY();
644 }
645 return mHeight;
646}
647
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800648void Context::pause() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800649 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700650 mPaused = true;
651}
652
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800653void Context::resume() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800654 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700655 mPaused = false;
656}
657
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800658void Context::setRootScript(Script *s) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800659 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700660 mRootScript.set(s);
661}
662
Jason Sams60709252010-11-17 15:29:32 -0800663void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800664 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700665 if (pfs == NULL) {
666 mFragmentStore.set(mStateFragmentStore.mDefault);
667 } else {
668 mFragmentStore.set(pfs);
669 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700670}
671
Jason Sams60709252010-11-17 15:29:32 -0800672void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800673 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700674 if (pf == NULL) {
675 mFragment.set(mStateFragment.mDefault);
676 } else {
677 mFragment.set(pf);
678 }
Jason Samscfb1d112009-08-05 13:57:03 -0700679}
680
Jason Sams60709252010-11-17 15:29:32 -0800681void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800682 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700683 if (pr == NULL) {
684 mRaster.set(mStateRaster.mDefault);
685 } else {
686 mRaster.set(pr);
687 }
688}
689
Jason Sams60709252010-11-17 15:29:32 -0800690void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800691 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700692 if (pv == NULL) {
693 mVertex.set(mStateVertex.mDefault);
694 } else {
695 mVertex.set(pv);
696 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700697}
698
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800699void Context::setFont(Font *f) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700700 rsAssert(mIsGraphicsContext);
701 if (f == NULL) {
702 mFont.set(mStateFont.mDefault);
703 } else {
704 mFont.set(f);
705 }
706}
Jason Sams93eacc72012-12-18 14:26:57 -0800707#endif
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700708
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800709void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700710 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700711 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700712 mNames.add(obj);
713}
714
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800715void Context::removeName(ObjectBase *obj) {
716 for (size_t ct=0; ct < mNames.size(); ct++) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700717 if (obj == mNames[ct]) {
718 mNames.removeAt(ct);
719 return;
720 }
721 }
722}
723
Jason Sams1a4efa32011-05-17 15:01:29 -0700724RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) {
725 return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800726}
727
Jason Sams1a4efa32011-05-17 15:01:29 -0700728RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) {
729 return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen);
Jason Sams8c401ef2009-10-06 13:58:47 -0700730}
731
Jason Sams87319de2010-11-22 16:20:16 -0800732bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
733 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Sams1a4efa32011-05-17 15:01:29 -0700734
735 return mIO.sendToClient(cmdID, subID, data, len, waitForSpace);
Jason Sams8c401ef2009-10-06 13:58:47 -0700736}
737
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800738void Context::initToClient() {
739 while (!mRunning) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700740 usleep(100);
741 }
742}
743
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800744void Context::deinitToClient() {
Jason Sams1a4efa32011-05-17 15:01:29 -0700745 mIO.clientShutdown();
Jason Sams8c401ef2009-10-06 13:58:47 -0700746}
Jason Sams50869382009-08-18 17:07:09 -0700747
Jason Sams87319de2010-11-22 16:20:16 -0800748void Context::setError(RsError e, const char *msg) const {
Jason Samsa2cf7552010-03-03 13:03:18 -0800749 mError = e;
Jason Samsaad4bc52010-11-08 17:06:46 -0800750 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Samsa2cf7552010-03-03 13:03:18 -0800751}
752
753
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800754void Context::dumpDebug() const {
Steve Blockaf12ac62012-01-06 19:20:56 +0000755 ALOGE("RS Context debug %p", this);
756 ALOGE("RS Context debug");
Jason Sams13e26342009-11-24 12:26:35 -0800757
Steve Blockaf12ac62012-01-06 19:20:56 +0000758 ALOGE(" RS width %i, height %i", mWidth, mHeight);
759 ALOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
760 ALOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId);
Jason Sams13e26342009-11-24 12:26:35 -0800761}
Jason Samsa4a54e42009-06-10 18:39:40 -0700762
Jason Sams326e0dd2009-05-22 14:03:28 -0700763///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700764//
Jason Sams326e0dd2009-05-22 14:03:28 -0700765
766namespace android {
767namespace renderscript {
768
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800769void rsi_ContextFinish(Context *rsc) {
Jason Sams8c880902010-06-15 12:15:57 -0700770}
Jason Sams326e0dd2009-05-22 14:03:28 -0700771
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800772void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Sams93eacc72012-12-18 14:26:57 -0800773#ifndef RS_COMPATIBILITY_LIB
Jason Sams326e0dd2009-05-22 14:03:28 -0700774 Script *s = static_cast<Script *>(vs);
775 rsc->setRootScript(s);
Jason Sams93eacc72012-12-18 14:26:57 -0800776#endif
Jason Sams326e0dd2009-05-22 14:03:28 -0700777}
778
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800779void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700780 Sampler *s = static_cast<Sampler *>(vs);
781
782 if (slot > RS_MAX_SAMPLER_SLOT) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000783 ALOGE("Invalid sampler slot");
Jason Sams326e0dd2009-05-22 14:03:28 -0700784 return;
785 }
786
787 s->bindToContext(&rsc->mStateSampler, slot);
788}
789
Jason Sams93eacc72012-12-18 14:26:57 -0800790#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800791void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Samsccc010b2010-05-13 18:30:11 -0700792 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams60709252010-11-17 15:29:32 -0800793 rsc->setProgramStore(pfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700794}
795
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800796void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700797 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Sams60709252010-11-17 15:29:32 -0800798 rsc->setProgramFragment(pf);
Jason Sams326e0dd2009-05-22 14:03:28 -0700799}
800
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800801void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Sams5fd09d82009-09-23 13:57:02 -0700802 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Sams60709252010-11-17 15:29:32 -0800803 rsc->setProgramRaster(pr);
Jason Sams5fd09d82009-09-23 13:57:02 -0700804}
805
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800806void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700807 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Sams60709252010-11-17 15:29:32 -0800808 rsc->setProgramVertex(pv);
Jason Sams326e0dd2009-05-22 14:03:28 -0700809}
810
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800811void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700812 Font *font = static_cast<Font *>(vfont);
813 rsc->setFont(font);
814}
Jason Sams93eacc72012-12-18 14:26:57 -0800815#endif
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700816
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700817void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700818 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700819 rsc->assignName(ob, name, name_length);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700820}
Jason Sams326e0dd2009-05-22 14:03:28 -0700821
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800822void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams2353ae32010-10-14 17:48:46 -0700823 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -0700824 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700825 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700826}
827
Jason Sams93eacc72012-12-18 14:26:57 -0800828#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800829void rsi_ContextPause(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700830 rsc->pause();
831}
832
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800833void rsi_ContextResume(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700834 rsc->resume();
835}
836
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700837void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
Mathias Agopianfa402862010-02-12 14:04:35 -0800838 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800839}
Jason Sams93eacc72012-12-18 14:26:57 -0800840#endif
Jason Sams613cad12009-11-12 15:10:25 -0800841
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800842void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800843 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800844}
845
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800846void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Samsc21cf402009-11-17 17:26:46 -0800847 ObjectBase::dumpAll(rsc);
848}
849
Jason Sams741aac92010-12-24 14:38:39 -0800850void rsi_ContextDestroyWorker(Context *rsc) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700851 rsc->destroyWorkerThreadResources();
Jason Sams741aac92010-12-24 14:38:39 -0800852}
853
Jason Samsc975cf42011-04-28 18:26:48 -0700854void rsi_ContextDestroy(Context *rsc) {
Steve Block65982012011-10-20 11:56:00 +0100855 ALOGV("%p rsContextDestroy", rsc);
Jason Sams741aac92010-12-24 14:38:39 -0800856 rsContextDestroyWorker(rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800857 delete rsc;
Steve Block65982012011-10-20 11:56:00 +0100858 ALOGV("%p rsContextDestroy done", rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800859}
860
Jason Sams326e0dd2009-05-22 14:03:28 -0700861
Jason Samsc975cf42011-04-28 18:26:48 -0700862RsMessageToClientType rsi_ContextPeekMessage(Context *rsc,
Jason Sams186e5912011-04-26 14:50:00 -0700863 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700864 uint32_t * subID, size_t subID_length) {
865 return rsc->peekMessageToClient(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800866}
867
Jason Samsc975cf42011-04-28 18:26:48 -0700868RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length,
Jason Sams186e5912011-04-26 14:50:00 -0700869 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700870 uint32_t * subID, size_t subID_length) {
Jason Sams186e5912011-04-26 14:50:00 -0700871 rsAssert(subID_length == sizeof(uint32_t));
872 rsAssert(receiveLen_length == sizeof(size_t));
Jason Sams1a4efa32011-05-17 15:01:29 -0700873 return rsc->getMessageToClient(data, receiveLen, subID, data_length);
Jason Sams8c401ef2009-10-06 13:58:47 -0700874}
875
Jason Samsc975cf42011-04-28 18:26:48 -0700876void rsi_ContextInitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700877 rsc->initToClient();
878}
879
Jason Samsc975cf42011-04-28 18:26:48 -0700880void rsi_ContextDeinitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700881 rsc->deinitToClient();
882}
883
Jason Sams70265202013-02-05 19:20:47 -0800884void rsi_ContextSendMessage(Context *rsc, uint32_t id, const uint8_t *data, size_t len) {
885 rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, id, len, true);
886}
887
Jason Samsc975cf42011-04-28 18:26:48 -0700888}
889}
890
Jason Sams14982c82013-02-22 18:17:05 -0800891RsContext rsContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion,
892 RsContextType ct, bool forceCpu, bool synchronous) {
Steve Block65982012011-10-20 11:56:00 +0100893 ALOGV("rsContextCreate dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700894 Device * dev = static_cast<Device *>(vdev);
Tim Murray4d252d62012-11-29 14:37:59 -0800895 Context *rsc = Context::createContext(dev, NULL, forceCpu, synchronous);
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700896 if (rsc) {
897 rsc->setTargetSdkVersion(sdkVersion);
898 }
Jason Sams789ca832011-05-18 17:36:02 -0700899 return rsc;
900}
901
Jason Sams93eacc72012-12-18 14:26:57 -0800902#ifndef RS_COMPATIBILITY_LIB
Jason Sams789ca832011-05-18 17:36:02 -0700903RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700904 uint32_t sdkVersion, RsSurfaceConfig sc,
905 uint32_t dpi) {
Steve Block65982012011-10-20 11:56:00 +0100906 ALOGV("rsContextCreateGL dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700907 Device * dev = static_cast<Device *>(vdev);
908 Context *rsc = Context::createContext(dev, &sc);
Jason Sams9544f762011-07-13 16:09:42 -0700909 if (rsc) {
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700910 rsc->setTargetSdkVersion(sdkVersion);
Jason Sams9544f762011-07-13 16:09:42 -0700911 rsc->setDPI(dpi);
912 }
Steve Block65982012011-10-20 11:56:00 +0100913 ALOGV("%p rsContextCreateGL ret", rsc);
Jason Sams789ca832011-05-18 17:36:02 -0700914 return rsc;
915}
Jason Sams93eacc72012-12-18 14:26:57 -0800916#endif
Jason Sams789ca832011-05-18 17:36:02 -0700917
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700918// Only to be called at a3d load time, before object is visible to user
919// not thread safe
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800920void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700921 ObjectBase *ob = static_cast<ObjectBase *>(obj);
922 (*name) = ob->getName();
923}