blob: dd795a606ddf1ece2f7221e76f10d23524f4aaa9 [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 Hines6dfe6842013-08-14 17:56:38 -070037#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
Tim Murray0b575de2013-03-15 15:56:43 -070038#include <cutils/properties.h>
39#endif
40
Stephen Hines6dfe6842013-08-14 17:56:38 -070041#ifdef RS_COMPATIBILITY_LIB
42#include "rsCompatibilityLib.h"
43#endif
44
Tim Murray0b575de2013-03-15 15:56:43 -070045#ifdef RS_SERVER
46// Android exposes gettid(), standard Linux does not
47static pid_t gettid() {
48 return syscall(SYS_gettid);
49}
50#endif
51
Jason Sams326e0dd2009-05-22 14:03:28 -070052using namespace android;
53using namespace android::renderscript;
54
Jason Samsfb03a222009-10-15 16:47:31 -070055pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams4961cce2013-04-11 16:11:46 -070056pthread_mutex_t Context::gMessageMutex = PTHREAD_MUTEX_INITIALIZER;
Stephen Hinesca3f09c2011-01-07 15:11:30 -080057pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070058
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080059bool Context::initGLThread() {
Jason Sams6b8552a2010-10-13 15:31:10 -070060 pthread_mutex_lock(&gInitMutex);
Jason Sams6b8552a2010-10-13 15:31:10 -070061
Jason Sams4b3de472011-04-06 17:52:23 -070062 if (!mHal.funcs.initGraphics(this)) {
Jason Sams5c1c79a2010-11-03 14:27:11 -070063 pthread_mutex_unlock(&gInitMutex);
Steve Blockaf12ac62012-01-06 19:20:56 +000064 ALOGE("%p initGraphics failed", this);
Jason Sams5c1c79a2010-11-03 14:27:11 -070065 return false;
Jason Sams6b8552a2010-10-13 15:31:10 -070066 }
67
Jason Sams6b8552a2010-10-13 15:31:10 -070068 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -070069 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -070070}
71
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080072void Context::deinitEGL() {
Jason Sams93eacc72012-12-18 14:26:57 -080073#ifndef RS_COMPATIBILITY_LIB
Jason Sams4b3de472011-04-06 17:52:23 -070074 mHal.funcs.shutdownGraphics(this);
Jason Sams93eacc72012-12-18 14:26:57 -080075#endif
Jason Sams33b6e3b2009-10-27 14:44:31 -070076}
77
Jason Sams60709252010-11-17 15:29:32 -080078Context::PushState::PushState(Context *con) {
79 mRsc = con;
Jason Sams93eacc72012-12-18 14:26:57 -080080#ifndef RS_COMPATIBILITY_LIB
Jason Samsc946b612011-02-23 14:47:17 -080081 if (con->mIsGraphicsContext) {
82 mFragment.set(con->getProgramFragment());
83 mVertex.set(con->getProgramVertex());
84 mStore.set(con->getProgramStore());
85 mRaster.set(con->getProgramRaster());
86 mFont.set(con->getFont());
87 }
Jason Sams93eacc72012-12-18 14:26:57 -080088#endif
Jason Sams60709252010-11-17 15:29:32 -080089}
90
91Context::PushState::~PushState() {
Jason Sams93eacc72012-12-18 14:26:57 -080092#ifndef RS_COMPATIBILITY_LIB
Jason Samsc946b612011-02-23 14:47:17 -080093 if (mRsc->mIsGraphicsContext) {
94 mRsc->setProgramFragment(mFragment.get());
95 mRsc->setProgramVertex(mVertex.get());
96 mRsc->setProgramStore(mStore.get());
97 mRsc->setProgramRaster(mRaster.get());
98 mRsc->setFont(mFont.get());
99 }
Jason Sams93eacc72012-12-18 14:26:57 -0800100#endif
Jason Sams60709252010-11-17 15:29:32 -0800101}
102
Jason Sams33b6e3b2009-10-27 14:44:31 -0700103
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800104uint32_t Context::runScript(Script *s) {
Shih-wei Liaoda3b58d2012-08-03 04:24:33 -0700105 PushState ps(this);
Jason Sams10308932009-06-09 12:15:30 -0700106
Jason Samsc61346b2010-05-28 18:23:22 -0700107 uint32_t ret = s->run(this);
Jason Samsc9d43db2009-07-28 12:02:16 -0700108 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700109}
110
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800111uint32_t Context::runRootScript() {
Jason Sams2dca84d2009-12-09 11:05:45 -0800112 timerSet(RS_TIMER_SCRIPT);
Jason Sams93eacc72012-12-18 14:26:57 -0800113#ifndef RS_COMPATIBILITY_LIB
Jason Sams8c401ef2009-10-06 13:58:47 -0700114 mStateFragmentStore.mLast.clear();
Jason Sams93eacc72012-12-18 14:26:57 -0800115#endif
Jason Sams2382aba2011-09-13 15:41:01 -0700116 watchdog.inRoot = true;
Jason Samsc61346b2010-05-28 18:23:22 -0700117 uint32_t ret = runScript(mRootScript.get());
Jason Sams2382aba2011-09-13 15:41:01 -0700118 watchdog.inRoot = false;
Jason Sams8cfdd242009-10-14 15:43:53 -0700119
Jason Samscfb1d112009-08-05 13:57:03 -0700120 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700121}
122
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800123uint64_t Context::getTime() const {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700124#ifndef ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -0700125 struct timespec t;
126 clock_gettime(CLOCK_MONOTONIC, &t);
127 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700128#else
129 return 0;
130#endif //ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -0700131}
132
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800133void Context::timerReset() {
Jason Sams24371d92009-08-19 12:17:14 -0700134 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
135 mTimers[ct] = 0;
136 }
137}
138
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800139void Context::timerInit() {
Jason Sams24371d92009-08-19 12:17:14 -0700140 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700141 mTimeFrame = mTimeLast;
142 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700143 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700144 mAverageFPSFrameCount = 0;
145 mAverageFPSStartTime = mTimeLast;
146 mAverageFPS = 0;
Jason Sams24371d92009-08-19 12:17:14 -0700147 timerReset();
148}
149
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800150void Context::timerFrame() {
Jason Sams1d54f102009-09-03 15:43:13 -0700151 mTimeLastFrame = mTimeFrame;
152 mTimeFrame = getTime();
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700153 // Update average fps
154 const uint64_t averageFramerateInterval = 1000 * 1000000;
155 mAverageFPSFrameCount ++;
156 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800157 if (inverval >= averageFramerateInterval) {
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700158 inverval = inverval / 1000000;
159 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
160 mAverageFPSFrameCount = 0;
161 mAverageFPSStartTime = mTimeFrame;
162 }
Jason Sams1d54f102009-09-03 15:43:13 -0700163}
164
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800165void Context::timerSet(Timers tm) {
Jason Sams24371d92009-08-19 12:17:14 -0700166 uint64_t last = mTimeLast;
167 mTimeLast = getTime();
168 mTimers[mTimerActive] += mTimeLast - last;
169 mTimerActive = tm;
170}
171
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800172void Context::timerPrint() {
Jason Sams24371d92009-08-19 12:17:14 -0700173 double total = 0;
174 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
175 total += mTimers[ct];
176 }
Jason Sams1d54f102009-09-03 15:43:13 -0700177 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800178 mTimeMSLastFrame = frame / 1000000;
179 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
180 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700181
Jason Sams2dca84d2009-12-09 11:05:45 -0800182
183 if (props.mLogTimes) {
Steve Block65982012011-10-20 11:56:00 +0100184 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 -0800185 mTimeMSLastFrame,
186 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
187 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
188 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700189 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
190 mAverageFPS);
Jason Sams2dca84d2009-12-09 11:05:45 -0800191 }
Jason Sams24371d92009-08-19 12:17:14 -0700192}
193
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800194bool Context::setupCheck() {
Jason Sams93eacc72012-12-18 14:26:57 -0800195#ifndef RS_COMPATIBILITY_LIB
Jason Sams721acc42011-04-06 11:23:54 -0700196 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700197 mFragment->setup(this, &mStateFragment);
Jason Sams721acc42011-04-06 11:23:54 -0700198 mRaster->setup(this, &mStateRaster);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700199 mVertex->setup(this, &mStateVertex);
200 mFBOCache.setup(this);
Jason Sams93eacc72012-12-18 14:26:57 -0800201#endif
Jason Samsa2cf7552010-03-03 13:03:18 -0800202 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700203}
204
Jason Sams93eacc72012-12-18 14:26:57 -0800205#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700206void Context::setupProgramStore() {
Jason Sams721acc42011-04-06 11:23:54 -0700207 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700208}
Jason Sams93eacc72012-12-18 14:26:57 -0800209#endif
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700210
Jason Samsd1f7da62012-03-15 19:18:03 -0700211static uint32_t getProp(const char *str) {
Tim Murray0b575de2013-03-15 15:56:43 -0700212#ifndef RS_SERVER
Joe Onorato76371ff2009-09-23 16:37:36 -0700213 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700214 property_get(str, buf, "0");
Jason Samsd1f7da62012-03-15 19:18:03 -0700215 return atoi(buf);
Tim Murray0b575de2013-03-15 15:56:43 -0700216#else
217 return 0;
218#endif
Joe Onorato76371ff2009-09-23 16:37:36 -0700219}
Jason Sams326e0dd2009-05-22 14:03:28 -0700220
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800221void Context::displayDebugStats() {
Jason Sams93eacc72012-12-18 14:26:57 -0800222#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700223 char buffer[128];
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700224 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700225 float oldR, oldG, oldB, oldA;
226 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700227 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700228
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700229 ObjectBaseRef<Font> lastFont(getFont());
230 setFont(NULL);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700231 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700232 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700233 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700234
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700235 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700236 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700237
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700238 setFont(lastFont.get());
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700239 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Jason Sams93eacc72012-12-18 14:26:57 -0800240#endif
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700241}
242
Tim Murray0c66f072012-10-23 14:05:02 -0700243bool Context::loadRuntime(const char* filename, Context* rsc) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700244
Tim Murray0c66f072012-10-23 14:05:02 -0700245 // TODO: store the driverSO somewhere so we can dlclose later
Stephen Hines91dfcdb2012-09-07 18:23:35 -0700246 void *driverSO = NULL;
247
Tim Murray0c66f072012-10-23 14:05:02 -0700248 driverSO = dlopen(filename, RTLD_LAZY);
Stephen Hines91dfcdb2012-09-07 18:23:35 -0700249 if (driverSO == NULL) {
Tim Murray0c66f072012-10-23 14:05:02 -0700250 ALOGE("Failed loading RS driver: %s", dlerror());
251 return false;
Stephen Hines414a4612012-09-05 18:05:08 -0700252 }
253
254 // Need to call dlerror() to clear buffer before using it for dlsym().
255 (void) dlerror();
256 typedef bool (*HalSig)(Context*, uint32_t, uint32_t);
257 HalSig halInit = (HalSig) dlsym(driverSO, "rsdHalInit");
258
259 // If we can't find the C variant, we go looking for the C++ version.
260 if (halInit == NULL) {
261 ALOGW("Falling back to find C++ rsdHalInit: %s", dlerror());
262 halInit = (HalSig) dlsym(driverSO,
263 "_Z10rsdHalInitPN7android12renderscript7ContextEjj");
264 }
265
266 if (halInit == NULL) {
Stephen Hines414a4612012-09-05 18:05:08 -0700267 dlclose(driverSO);
268 ALOGE("Failed to find rsdHalInit: %s", dlerror());
Tim Murray0c66f072012-10-23 14:05:02 -0700269 return false;
Stephen Hines414a4612012-09-05 18:05:08 -0700270 }
271
272 if (!(*halInit)(rsc, 0, 0)) {
Stephen Hines414a4612012-09-05 18:05:08 -0700273 dlclose(driverSO);
Steve Blockaf12ac62012-01-06 19:20:56 +0000274 ALOGE("Hal init failed");
Tim Murray0c66f072012-10-23 14:05:02 -0700275 return false;
Jason Sams83c451a2011-04-21 11:46:50 -0700276 }
Tim Murray0c66f072012-10-23 14:05:02 -0700277
278 //validate HAL struct
279
280
281 return true;
282}
283
Jason Sams110f1812013-03-14 16:02:18 -0700284extern "C" bool rsdHalInit(RsContext c, uint32_t version_major, uint32_t version_minor);
285
Tim Murray0c66f072012-10-23 14:05:02 -0700286void * Context::threadProc(void *vrsc) {
287 Context *rsc = static_cast<Context *>(vrsc);
288#ifndef ANDROID_RS_SERIALIZE
289 rsc->mNativeThreadId = gettid();
Jason Sams93eacc72012-12-18 14:26:57 -0800290#ifndef RS_COMPATIBILITY_LIB
Tim Murray4d252d62012-11-29 14:37:59 -0800291 if (!rsc->isSynchronous()) {
292 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
293 }
Tim Murray0c66f072012-10-23 14:05:02 -0700294 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams93eacc72012-12-18 14:26:57 -0800295#else
296 if (!rsc->isSynchronous()) {
297 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, -4);
298 }
299 rsc->mThreadPriority = -4;
300#endif
Tim Murray0c66f072012-10-23 14:05:02 -0700301#endif //ANDROID_RS_SERIALIZE
302 rsc->props.mLogTimes = getProp("debug.rs.profile") != 0;
303 rsc->props.mLogScripts = getProp("debug.rs.script") != 0;
304 rsc->props.mLogObjects = getProp("debug.rs.object") != 0;
305 rsc->props.mLogShaders = getProp("debug.rs.shader") != 0;
306 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes") != 0;
307 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms") != 0;
308 rsc->props.mLogVisual = getProp("debug.rs.visual") != 0;
309 rsc->props.mDebugMaxThreads = getProp("debug.rs.max-threads");
310
311 bool loadDefault = true;
312
313 // Provide a mechanism for dropping in a different RS driver.
Jason Sams110f1812013-03-14 16:02:18 -0700314#ifndef RS_COMPATIBILITY_LIB
Tim Murray0c66f072012-10-23 14:05:02 -0700315#ifdef OVERRIDE_RS_DRIVER
316#define XSTR(S) #S
317#define STR(S) XSTR(S)
318#define OVERRIDE_RS_DRIVER_STRING STR(OVERRIDE_RS_DRIVER)
319
320 if (getProp("debug.rs.default-CPU-driver") != 0) {
321 ALOGE("Skipping override driver and loading default CPU driver");
Tim Murray0e92fa32012-11-06 14:36:38 -0800322 } else if (rsc->mForceCpu) {
323 ALOGV("Application requested CPU execution");
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700324 } else if (rsc->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
325 ALOGV("Application requested debug context");
Tim Murray0c66f072012-10-23 14:05:02 -0700326 } else {
327 if (loadRuntime(OVERRIDE_RS_DRIVER_STRING, rsc)) {
328 ALOGE("Successfully loaded runtime: %s", OVERRIDE_RS_DRIVER_STRING);
329 loadDefault = false;
330 } else {
331 ALOGE("Failed to load runtime %s, loading default", OVERRIDE_RS_DRIVER_STRING);
332 }
333 }
334
335#undef XSTR
336#undef STR
337#endif // OVERRIDE_RS_DRIVER
338
339 if (loadDefault) {
340 if (!loadRuntime("libRSDriver.so", rsc)) {
341 ALOGE("Failed to load default runtime!");
Stephen Hines6f01bcf2012-11-19 15:18:16 -0800342 rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed loading RS driver");
Tim Murray0c66f072012-10-23 14:05:02 -0700343 return NULL;
344 }
345 }
Jason Sams110f1812013-03-14 16:02:18 -0700346#else // RS_COMPATIBILITY_LIB
347 if (rsdHalInit(rsc, 0, 0) != true) {
348 return NULL;
349 }
350#endif
351
Tim Murray0c66f072012-10-23 14:05:02 -0700352
Jason Sams83c451a2011-04-21 11:46:50 -0700353 rsc->mHal.funcs.setPriority(rsc, rsc->mThreadPriority);
Jason Samse5769102009-06-19 16:03:18 -0700354
Jason Sams93eacc72012-12-18 14:26:57 -0800355#ifndef RS_COMPATIBILITY_LIB
Jason Sams83c451a2011-04-21 11:46:50 -0700356 if (rsc->mIsGraphicsContext) {
Jason Samsd3e71072011-05-03 15:01:58 -0700357 if (!rsc->initGLThread()) {
358 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
359 return NULL;
360 }
361
Jason Sams83c451a2011-04-21 11:46:50 -0700362 rsc->mStateRaster.init(rsc);
363 rsc->setProgramRaster(NULL);
364 rsc->mStateVertex.init(rsc);
365 rsc->setProgramVertex(NULL);
366 rsc->mStateFragment.init(rsc);
367 rsc->setProgramFragment(NULL);
368 rsc->mStateFragmentStore.init(rsc);
369 rsc->setProgramStore(NULL);
370 rsc->mStateFont.init(rsc);
371 rsc->setFont(NULL);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700372 rsc->mStateSampler.init(rsc);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700373 rsc->mFBOCache.init(rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700374 }
Jason Sams93eacc72012-12-18 14:26:57 -0800375#endif
Jason Sams8ce125b2009-06-17 16:52:59 -0700376
Jason Sams83c451a2011-04-21 11:46:50 -0700377 rsc->mRunning = true;
Tim Murray4d252d62012-11-29 14:37:59 -0800378
379 if (rsc->isSynchronous()) {
380 return NULL;
381 }
382
Jason Sams5f27d6f2012-02-07 15:32:08 -0800383 if (!rsc->mIsGraphicsContext) {
384 while (!rsc->mExit) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800385 rsc->mIO.playCoreCommands(rsc, -1);
Jason Samse0aab4a2011-08-12 15:05:15 -0700386 }
Jason Sams93eacc72012-12-18 14:26:57 -0800387#ifndef RS_COMPATIBILITY_LIB
Jason Sams5f27d6f2012-02-07 15:32:08 -0800388 } else {
389#ifndef ANDROID_RS_SERIALIZE
390 DisplayEventReceiver displayEvent;
391 DisplayEventReceiver::Event eventBuffer[1];
392#endif
393 int vsyncRate = 0;
394 int targetRate = 0;
Jason Samse0aab4a2011-08-12 15:05:15 -0700395
Jason Sams5f27d6f2012-02-07 15:32:08 -0800396 bool drawOnce = false;
397 while (!rsc->mExit) {
398 rsc->timerSet(RS_TIMER_IDLE);
Jason Sams326e0dd2009-05-22 14:03:28 -0700399
Jason Sams5f27d6f2012-02-07 15:32:08 -0800400#ifndef ANDROID_RS_SERIALIZE
Jason Sams9afd9a52012-02-17 16:59:50 -0800401 if (!rsc->mRootScript.get() || !rsc->mHasSurface || rsc->mPaused) {
402 targetRate = 0;
403 }
404
Jason Sams5f27d6f2012-02-07 15:32:08 -0800405 if (vsyncRate != targetRate) {
406 displayEvent.setVsyncRate(targetRate);
407 vsyncRate = targetRate;
408 }
409 if (targetRate) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800410 drawOnce |= rsc->mIO.playCoreCommands(rsc, displayEvent.getFd());
Jason Sams5f27d6f2012-02-07 15:32:08 -0800411 while (displayEvent.getEvents(eventBuffer, 1) != 0) {
412 //ALOGE("vs2 time past %lld", (rsc->getTime() - eventBuffer[0].header.timestamp) / 1000000);
413 }
414 } else
415#endif
416 {
Jason Sams963a2fb2012-02-09 14:36:14 -0800417 drawOnce |= rsc->mIO.playCoreCommands(rsc, -1);
Jason Sams83c451a2011-04-21 11:46:50 -0700418 }
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700419
Jason Sams5f27d6f2012-02-07 15:32:08 -0800420 if ((rsc->mRootScript.get() != NULL) && rsc->mHasSurface &&
421 (targetRate || drawOnce) && !rsc->mPaused) {
422
423 drawOnce = false;
424 targetRate = ((rsc->runRootScript() + 15) / 16);
425
426 if (rsc->props.mLogVisual) {
427 rsc->displayDebugStats();
428 }
429
430 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
431 rsc->mHal.funcs.swap(rsc);
432 rsc->timerFrame();
433 rsc->timerSet(RS_TIMER_INTERNAL);
434 rsc->timerPrint();
435 rsc->timerReset();
436 }
Jason Sams83c451a2011-04-21 11:46:50 -0700437 }
Jason Sams93eacc72012-12-18 14:26:57 -0800438#endif
Jason Sams83c451a2011-04-21 11:46:50 -0700439 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700440
Tim Murray20f62ca2013-05-09 11:57:35 -0700441 //ALOGV("%p RS Thread exiting", rsc);
Jason Samse514b452009-09-25 14:51:22 -0700442
Jason Sams93eacc72012-12-18 14:26:57 -0800443#ifndef RS_COMPATIBILITY_LIB
Jason Sams83c451a2011-04-21 11:46:50 -0700444 if (rsc->mIsGraphicsContext) {
445 pthread_mutex_lock(&gInitMutex);
446 rsc->deinitEGL();
447 pthread_mutex_unlock(&gInitMutex);
448 }
Jason Sams93eacc72012-12-18 14:26:57 -0800449#endif
Jason Sams33b6e3b2009-10-27 14:44:31 -0700450
Tim Murray20f62ca2013-05-09 11:57:35 -0700451 //ALOGV("%p RS Thread exited", rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700452 return NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700453}
454
Jason Sams741aac92010-12-24 14:38:39 -0800455void Context::destroyWorkerThreadResources() {
Steve Block65982012011-10-20 11:56:00 +0100456 //ALOGV("destroyWorkerThreadResources 1");
Jason Sams2e8665d2011-01-27 00:14:13 -0800457 ObjectBase::zeroAllUserRef(this);
Jason Sams93eacc72012-12-18 14:26:57 -0800458#ifndef RS_COMPATIBILITY_LIB
Jason Sams741aac92010-12-24 14:38:39 -0800459 if (mIsGraphicsContext) {
460 mRaster.clear();
461 mFragment.clear();
462 mVertex.clear();
463 mFragmentStore.clear();
464 mFont.clear();
465 mRootScript.clear();
466 mStateRaster.deinit(this);
467 mStateVertex.deinit(this);
468 mStateFragment.deinit(this);
469 mStateFragmentStore.deinit(this);
470 mStateFont.deinit(this);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700471 mStateSampler.deinit(this);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700472 mFBOCache.deinit(this);
Jason Sams741aac92010-12-24 14:38:39 -0800473 }
Jason Sams93eacc72012-12-18 14:26:57 -0800474#endif
Jason Samsc7cec1e2011-08-18 18:01:33 -0700475 ObjectBase::freeAllChildren(this);
Jason Samscf912de2011-01-09 16:09:51 -0800476 mExit = true;
Jason Sams5f27d6f2012-02-07 15:32:08 -0800477 //ALOGV("destroyWorkerThreadResources 2");
Jason Sams741aac92010-12-24 14:38:39 -0800478}
479
Jason Sams2382aba2011-09-13 15:41:01 -0700480void Context::printWatchdogInfo(void *ctx) {
481 Context *rsc = (Context *)ctx;
Jason Samsee803442011-10-13 16:05:27 -0700482 if (rsc->watchdog.command && rsc->watchdog.file) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000483 ALOGE("RS watchdog timeout: %i %s line %i %s", rsc->watchdog.inRoot,
Jason Samsee803442011-10-13 16:05:27 -0700484 rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file);
485 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000486 ALOGE("RS watchdog timeout: %i", rsc->watchdog.inRoot);
Jason Samsee803442011-10-13 16:05:27 -0700487 }
Jason Sams2382aba2011-09-13 15:41:01 -0700488}
489
490
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800491void Context::setPriority(int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800492 // Note: If we put this in the proper "background" policy
493 // the wallpapers can become completly unresponsive at times.
494 // This is probably not what we want for something the user is actively
495 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800496 mThreadPriority = p;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700497 setpriority(PRIO_PROCESS, mNativeThreadId, p);
Jason Sams9719bd42012-01-12 14:22:21 -0800498 mHal.funcs.setPriority(this, mThreadPriority);
Jason Sams15832442009-11-15 12:14:26 -0800499}
500
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800501Context::Context() {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700502 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700503 mRunning = false;
504 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700505 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700506 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800507 mError = RS_ERROR_NONE;
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700508 mTargetSdkVersion = 14;
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700509 mDPI = 96;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700510 mIsContextLite = false;
Stephen Hines86c6b5f2011-10-31 14:07:54 -0700511 memset(&watchdog, 0, sizeof(watchdog));
Tim Murray0e92fa32012-11-06 14:36:38 -0800512 mForceCpu = false;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700513 mContextType = RS_CONTEXT_TYPE_NORMAL;
Tim Murray4d252d62012-11-29 14:37:59 -0800514 mSynchronous = false;
Tim Murray0e92fa32012-11-06 14:36:38 -0800515}
516
517Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc,
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700518 RsContextType ct, bool forceCpu,
519 bool synchronous) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700520 Context * rsc = new Context();
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700521
Tim Murray0e92fa32012-11-06 14:36:38 -0800522 rsc->mForceCpu = forceCpu;
Tim Murray4d252d62012-11-29 14:37:59 -0800523 rsc->mSynchronous = synchronous;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700524 rsc->mContextType = ct;
Tim Murray0e92fa32012-11-06 14:36:38 -0800525
Jason Sams5c1c79a2010-11-03 14:27:11 -0700526 if (!rsc->initContext(dev, sc)) {
527 delete rsc;
528 return NULL;
529 }
530 return rsc;
531}
532
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700533Context * Context::createContextLite() {
534 Context * rsc = new Context();
535 rsc->mIsContextLite = true;
536 return rsc;
537}
538
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800539bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700540 pthread_mutex_lock(&gInitMutex);
541
Jason Sams1a4efa32011-05-17 15:01:29 -0700542 mIO.init();
Jason Sams5f27d6f2012-02-07 15:32:08 -0800543 mIO.setTimeoutCallback(printWatchdogInfo, this, 2e9);
Jason Sams1a4efa32011-05-17 15:01:29 -0700544
Jason Sams5c1c79a2010-11-03 14:27:11 -0700545 dev->addContext(this);
546 mDev = dev;
Jason Sams6b8552a2010-10-13 15:31:10 -0700547 if (sc) {
548 mUserSurfaceConfig = *sc;
549 } else {
550 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
551 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800552
Jason Sams6b8552a2010-10-13 15:31:10 -0700553 mIsGraphicsContext = sc != NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700554
Jason Samsa658e902009-06-04 14:35:01 -0700555 int status;
556 pthread_attr_t threadAttr;
557
Jason Samsfb03a222009-10-15 16:47:31 -0700558 pthread_mutex_unlock(&gInitMutex);
559
560 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700561
Jason Samsa658e902009-06-04 14:35:01 -0700562 status = pthread_attr_init(&threadAttr);
563 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000564 ALOGE("Failed to init thread attribute.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700565 return false;
Jason Samsa658e902009-06-04 14:35:01 -0700566 }
567
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700568 mHasSurface = false;
Jason Sams992a0b72009-06-23 12:22:47 -0700569
Jason Sams24371d92009-08-19 12:17:14 -0700570 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700571 timerSet(RS_TIMER_INTERNAL);
Tim Murray4d252d62012-11-29 14:37:59 -0800572 if (mSynchronous) {
573 threadProc(this);
574 } else {
575 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
576 if (status) {
577 ALOGE("Failed to start rs context thread.");
578 return false;
579 }
580 while (!mRunning && (mError == RS_ERROR_NONE)) {
581 usleep(100);
582 }
Jason Sams50869382009-08-18 17:07:09 -0700583
Tim Murray4d252d62012-11-29 14:37:59 -0800584 if (mError != RS_ERROR_NONE) {
585 ALOGE("Errors during thread init");
586 return false;
587 }
Jason Sams18133402010-07-20 15:09:00 -0700588
Tim Murray4d252d62012-11-29 14:37:59 -0800589 pthread_attr_destroy(&threadAttr);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700590 }
Jason Sams5c1c79a2010-11-03 14:27:11 -0700591 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700592}
593
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800594Context::~Context() {
Tim Murray20f62ca2013-05-09 11:57:35 -0700595 //ALOGV("%p Context::~Context", this);
Jason Samscf912de2011-01-09 16:09:51 -0800596
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700597 if (!mIsContextLite) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700598 mPaused = false;
599 void *res;
Jason Sams326e0dd2009-05-22 14:03:28 -0700600
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700601 mIO.shutdown();
602 int status = pthread_join(mThreadId, &res);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800603 rsAssert(mExit);
Jason Sams326e0dd2009-05-22 14:03:28 -0700604
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700605 if (mHal.funcs.shutdownDriver) {
606 mHal.funcs.shutdownDriver(this);
607 }
608
609 // Global structure cleanup.
610 pthread_mutex_lock(&gInitMutex);
611 if (mDev) {
612 mDev->removeContext(this);
613 mDev = NULL;
614 }
615 pthread_mutex_unlock(&gInitMutex);
Jason Sams51462c52011-01-25 00:26:25 -0800616 }
Tim Murray20f62ca2013-05-09 11:57:35 -0700617 //ALOGV("%p Context::~Context done", this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700618}
619
Jason Sams93eacc72012-12-18 14:26:57 -0800620#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700621void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800622 rsAssert(mIsGraphicsContext);
Jason Sams4b3de472011-04-06 17:52:23 -0700623 mHal.funcs.setSurface(this, w, h, sur);
Jason Sams458f2dc2009-11-03 13:58:36 -0800624
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700625 mHasSurface = sur != NULL;
Jason Sams4b3de472011-04-06 17:52:23 -0700626 mWidth = w;
627 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800628
Jason Sams4b3de472011-04-06 17:52:23 -0700629 if (mWidth && mHeight) {
Jason Sams771565f2010-05-14 15:30:29 -0700630 mStateVertex.updateSize(this);
Alex Sakhartchouka544b632011-07-19 17:50:29 -0700631 mFBOCache.updateSize();
Jason Sams458f2dc2009-11-03 13:58:36 -0800632 }
633}
634
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800635uint32_t Context::getCurrentSurfaceWidth() const {
636 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
637 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
638 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimX();
639 }
640 }
641 if (mFBOCache.mHal.state.depthTarget != NULL) {
642 return mFBOCache.mHal.state.depthTarget->getType()->getDimX();
643 }
644 return mWidth;
645}
646
647uint32_t Context::getCurrentSurfaceHeight() const {
648 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
649 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
650 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimY();
651 }
652 }
653 if (mFBOCache.mHal.state.depthTarget != NULL) {
654 return mFBOCache.mHal.state.depthTarget->getType()->getDimY();
655 }
656 return mHeight;
657}
658
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800659void Context::pause() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800660 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700661 mPaused = true;
662}
663
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800664void Context::resume() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800665 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700666 mPaused = false;
667}
668
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800669void Context::setRootScript(Script *s) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800670 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700671 mRootScript.set(s);
672}
673
Jason Sams60709252010-11-17 15:29:32 -0800674void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800675 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700676 if (pfs == NULL) {
677 mFragmentStore.set(mStateFragmentStore.mDefault);
678 } else {
679 mFragmentStore.set(pfs);
680 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700681}
682
Jason Sams60709252010-11-17 15:29:32 -0800683void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800684 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700685 if (pf == NULL) {
686 mFragment.set(mStateFragment.mDefault);
687 } else {
688 mFragment.set(pf);
689 }
Jason Samscfb1d112009-08-05 13:57:03 -0700690}
691
Jason Sams60709252010-11-17 15:29:32 -0800692void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800693 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700694 if (pr == NULL) {
695 mRaster.set(mStateRaster.mDefault);
696 } else {
697 mRaster.set(pr);
698 }
699}
700
Jason Sams60709252010-11-17 15:29:32 -0800701void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800702 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700703 if (pv == NULL) {
704 mVertex.set(mStateVertex.mDefault);
705 } else {
706 mVertex.set(pv);
707 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700708}
709
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800710void Context::setFont(Font *f) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700711 rsAssert(mIsGraphicsContext);
712 if (f == NULL) {
713 mFont.set(mStateFont.mDefault);
714 } else {
715 mFont.set(f);
716 }
717}
Jason Sams93eacc72012-12-18 14:26:57 -0800718#endif
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700719
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800720void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700721 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700722 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700723 mNames.add(obj);
724}
725
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800726void Context::removeName(ObjectBase *obj) {
727 for (size_t ct=0; ct < mNames.size(); ct++) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700728 if (obj == mNames[ct]) {
729 mNames.removeAt(ct);
730 return;
731 }
732 }
733}
734
Jason Sams1a4efa32011-05-17 15:01:29 -0700735RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) {
736 return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800737}
738
Jason Sams1a4efa32011-05-17 15:01:29 -0700739RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) {
740 return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen);
Jason Sams8c401ef2009-10-06 13:58:47 -0700741}
742
Jason Sams87319de2010-11-22 16:20:16 -0800743bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
744 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Sams1a4efa32011-05-17 15:01:29 -0700745
Jason Sams4961cce2013-04-11 16:11:46 -0700746 pthread_mutex_lock(&gMessageMutex);
747 bool ret = mIO.sendToClient(cmdID, subID, data, len, waitForSpace);
748 pthread_mutex_unlock(&gMessageMutex);
749 return ret;
Jason Sams8c401ef2009-10-06 13:58:47 -0700750}
751
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800752void Context::initToClient() {
753 while (!mRunning) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700754 usleep(100);
755 }
756}
757
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800758void Context::deinitToClient() {
Jason Sams1a4efa32011-05-17 15:01:29 -0700759 mIO.clientShutdown();
Jason Sams8c401ef2009-10-06 13:58:47 -0700760}
Jason Sams50869382009-08-18 17:07:09 -0700761
Jason Sams87319de2010-11-22 16:20:16 -0800762void Context::setError(RsError e, const char *msg) const {
Jason Samsa2cf7552010-03-03 13:03:18 -0800763 mError = e;
Jason Samsaad4bc52010-11-08 17:06:46 -0800764 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Samsa2cf7552010-03-03 13:03:18 -0800765}
766
767
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800768void Context::dumpDebug() const {
Steve Blockaf12ac62012-01-06 19:20:56 +0000769 ALOGE("RS Context debug %p", this);
770 ALOGE("RS Context debug");
Jason Sams13e26342009-11-24 12:26:35 -0800771
Steve Blockaf12ac62012-01-06 19:20:56 +0000772 ALOGE(" RS width %i, height %i", mWidth, mHeight);
773 ALOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
774 ALOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId);
Jason Sams13e26342009-11-24 12:26:35 -0800775}
Jason Samsa4a54e42009-06-10 18:39:40 -0700776
Jason Sams326e0dd2009-05-22 14:03:28 -0700777///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700778//
Jason Sams326e0dd2009-05-22 14:03:28 -0700779
780namespace android {
781namespace renderscript {
782
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800783void rsi_ContextFinish(Context *rsc) {
Jason Sams8c880902010-06-15 12:15:57 -0700784}
Jason Sams326e0dd2009-05-22 14:03:28 -0700785
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800786void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Sams93eacc72012-12-18 14:26:57 -0800787#ifndef RS_COMPATIBILITY_LIB
Jason Sams326e0dd2009-05-22 14:03:28 -0700788 Script *s = static_cast<Script *>(vs);
789 rsc->setRootScript(s);
Jason Sams93eacc72012-12-18 14:26:57 -0800790#endif
Jason Sams326e0dd2009-05-22 14:03:28 -0700791}
792
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800793void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700794 Sampler *s = static_cast<Sampler *>(vs);
795
796 if (slot > RS_MAX_SAMPLER_SLOT) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000797 ALOGE("Invalid sampler slot");
Jason Sams326e0dd2009-05-22 14:03:28 -0700798 return;
799 }
800
801 s->bindToContext(&rsc->mStateSampler, slot);
802}
803
Jason Sams93eacc72012-12-18 14:26:57 -0800804#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800805void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Samsccc010b2010-05-13 18:30:11 -0700806 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams60709252010-11-17 15:29:32 -0800807 rsc->setProgramStore(pfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700808}
809
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800810void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700811 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Sams60709252010-11-17 15:29:32 -0800812 rsc->setProgramFragment(pf);
Jason Sams326e0dd2009-05-22 14:03:28 -0700813}
814
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800815void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Sams5fd09d82009-09-23 13:57:02 -0700816 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Sams60709252010-11-17 15:29:32 -0800817 rsc->setProgramRaster(pr);
Jason Sams5fd09d82009-09-23 13:57:02 -0700818}
819
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800820void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700821 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Sams60709252010-11-17 15:29:32 -0800822 rsc->setProgramVertex(pv);
Jason Sams326e0dd2009-05-22 14:03:28 -0700823}
824
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800825void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700826 Font *font = static_cast<Font *>(vfont);
827 rsc->setFont(font);
828}
Jason Sams93eacc72012-12-18 14:26:57 -0800829#endif
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700830
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700831void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700832 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700833 rsc->assignName(ob, name, name_length);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700834}
Jason Sams326e0dd2009-05-22 14:03:28 -0700835
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800836void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams2353ae32010-10-14 17:48:46 -0700837 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -0700838 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700839 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700840}
841
Jason Sams93eacc72012-12-18 14:26:57 -0800842#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800843void rsi_ContextPause(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700844 rsc->pause();
845}
846
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800847void rsi_ContextResume(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700848 rsc->resume();
849}
850
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700851void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
Mathias Agopianfa402862010-02-12 14:04:35 -0800852 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800853}
Jason Sams93eacc72012-12-18 14:26:57 -0800854#endif
Jason Sams613cad12009-11-12 15:10:25 -0800855
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800856void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800857 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800858}
859
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800860void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Samsc21cf402009-11-17 17:26:46 -0800861 ObjectBase::dumpAll(rsc);
862}
863
Jason Sams741aac92010-12-24 14:38:39 -0800864void rsi_ContextDestroyWorker(Context *rsc) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700865 rsc->destroyWorkerThreadResources();
Jason Sams741aac92010-12-24 14:38:39 -0800866}
867
Jason Samsc975cf42011-04-28 18:26:48 -0700868void rsi_ContextDestroy(Context *rsc) {
Tim Murray20f62ca2013-05-09 11:57:35 -0700869 //ALOGV("%p rsContextDestroy", rsc);
Jason Sams741aac92010-12-24 14:38:39 -0800870 rsContextDestroyWorker(rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800871 delete rsc;
Tim Murray20f62ca2013-05-09 11:57:35 -0700872 //ALOGV("%p rsContextDestroy done", rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800873}
874
Jason Sams326e0dd2009-05-22 14:03:28 -0700875
Jason Samsc975cf42011-04-28 18:26:48 -0700876RsMessageToClientType rsi_ContextPeekMessage(Context *rsc,
Jason Sams186e5912011-04-26 14:50:00 -0700877 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700878 uint32_t * subID, size_t subID_length) {
879 return rsc->peekMessageToClient(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800880}
881
Jason Samsc975cf42011-04-28 18:26:48 -0700882RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length,
Jason Sams186e5912011-04-26 14:50:00 -0700883 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700884 uint32_t * subID, size_t subID_length) {
Jason Sams186e5912011-04-26 14:50:00 -0700885 rsAssert(subID_length == sizeof(uint32_t));
886 rsAssert(receiveLen_length == sizeof(size_t));
Jason Sams1a4efa32011-05-17 15:01:29 -0700887 return rsc->getMessageToClient(data, receiveLen, subID, data_length);
Jason Sams8c401ef2009-10-06 13:58:47 -0700888}
889
Jason Samsc975cf42011-04-28 18:26:48 -0700890void rsi_ContextInitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700891 rsc->initToClient();
892}
893
Jason Samsc975cf42011-04-28 18:26:48 -0700894void rsi_ContextDeinitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700895 rsc->deinitToClient();
896}
897
Jason Sams70265202013-02-05 19:20:47 -0800898void rsi_ContextSendMessage(Context *rsc, uint32_t id, const uint8_t *data, size_t len) {
899 rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, id, len, true);
900}
901
Jason Samsc975cf42011-04-28 18:26:48 -0700902}
903}
904
Jason Sams14982c82013-02-22 18:17:05 -0800905RsContext rsContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion,
906 RsContextType ct, bool forceCpu, bool synchronous) {
Tim Murray20f62ca2013-05-09 11:57:35 -0700907 //ALOGV("rsContextCreate dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700908 Device * dev = static_cast<Device *>(vdev);
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700909 Context *rsc = Context::createContext(dev, NULL, ct, forceCpu, synchronous);
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700910 if (rsc) {
911 rsc->setTargetSdkVersion(sdkVersion);
912 }
Jason Sams789ca832011-05-18 17:36:02 -0700913 return rsc;
914}
915
Jason Sams93eacc72012-12-18 14:26:57 -0800916#ifndef RS_COMPATIBILITY_LIB
Jason Sams789ca832011-05-18 17:36:02 -0700917RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700918 uint32_t sdkVersion, RsSurfaceConfig sc,
919 uint32_t dpi) {
Tim Murray20f62ca2013-05-09 11:57:35 -0700920 //ALOGV("rsContextCreateGL dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700921 Device * dev = static_cast<Device *>(vdev);
922 Context *rsc = Context::createContext(dev, &sc);
Jason Sams9544f762011-07-13 16:09:42 -0700923 if (rsc) {
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700924 rsc->setTargetSdkVersion(sdkVersion);
Jason Sams9544f762011-07-13 16:09:42 -0700925 rsc->setDPI(dpi);
926 }
Tim Murray20f62ca2013-05-09 11:57:35 -0700927 //ALOGV("%p rsContextCreateGL ret", rsc);
Jason Sams789ca832011-05-18 17:36:02 -0700928 return rsc;
929}
Jason Sams93eacc72012-12-18 14:26:57 -0800930#endif
Jason Sams789ca832011-05-18 17:36:02 -0700931
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700932// Only to be called at a3d load time, before object is visible to user
933// not thread safe
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800934void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700935 ObjectBase *ob = static_cast<ObjectBase *>(obj);
936 (*name) = ob->getName();
937}