blob: 749973aae44ff82069c8e6ac949dc2554ec5504f [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;
Jason Sams4961cce2013-04-11 16:11:46 -070051pthread_mutex_t Context::gMessageMutex = PTHREAD_MUTEX_INITIALIZER;
Stephen Hinesca3f09c2011-01-07 15:11:30 -080052pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070053
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080054bool Context::initGLThread() {
Jason Sams6b8552a2010-10-13 15:31:10 -070055 pthread_mutex_lock(&gInitMutex);
Jason Sams6b8552a2010-10-13 15:31:10 -070056
Jason Sams4b3de472011-04-06 17:52:23 -070057 if (!mHal.funcs.initGraphics(this)) {
Jason Sams5c1c79a2010-11-03 14:27:11 -070058 pthread_mutex_unlock(&gInitMutex);
Steve Blockaf12ac62012-01-06 19:20:56 +000059 ALOGE("%p initGraphics failed", this);
Jason Sams5c1c79a2010-11-03 14:27:11 -070060 return false;
Jason Sams6b8552a2010-10-13 15:31:10 -070061 }
62
Jason Sams6b8552a2010-10-13 15:31:10 -070063 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -070064 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -070065}
66
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080067void Context::deinitEGL() {
Jason Sams93eacc72012-12-18 14:26:57 -080068#ifndef RS_COMPATIBILITY_LIB
Jason Sams4b3de472011-04-06 17:52:23 -070069 mHal.funcs.shutdownGraphics(this);
Jason Sams93eacc72012-12-18 14:26:57 -080070#endif
Jason Sams33b6e3b2009-10-27 14:44:31 -070071}
72
Jason Sams60709252010-11-17 15:29:32 -080073Context::PushState::PushState(Context *con) {
74 mRsc = con;
Jason Sams93eacc72012-12-18 14:26:57 -080075#ifndef RS_COMPATIBILITY_LIB
Jason Samsc946b612011-02-23 14:47:17 -080076 if (con->mIsGraphicsContext) {
77 mFragment.set(con->getProgramFragment());
78 mVertex.set(con->getProgramVertex());
79 mStore.set(con->getProgramStore());
80 mRaster.set(con->getProgramRaster());
81 mFont.set(con->getFont());
82 }
Jason Sams93eacc72012-12-18 14:26:57 -080083#endif
Jason Sams60709252010-11-17 15:29:32 -080084}
85
86Context::PushState::~PushState() {
Jason Sams93eacc72012-12-18 14:26:57 -080087#ifndef RS_COMPATIBILITY_LIB
Jason Samsc946b612011-02-23 14:47:17 -080088 if (mRsc->mIsGraphicsContext) {
89 mRsc->setProgramFragment(mFragment.get());
90 mRsc->setProgramVertex(mVertex.get());
91 mRsc->setProgramStore(mStore.get());
92 mRsc->setProgramRaster(mRaster.get());
93 mRsc->setFont(mFont.get());
94 }
Jason Sams93eacc72012-12-18 14:26:57 -080095#endif
Jason Sams60709252010-11-17 15:29:32 -080096}
97
Jason Sams33b6e3b2009-10-27 14:44:31 -070098
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080099uint32_t Context::runScript(Script *s) {
Shih-wei Liaoda3b58d2012-08-03 04:24:33 -0700100 PushState ps(this);
Jason Sams10308932009-06-09 12:15:30 -0700101
Jason Samsc61346b2010-05-28 18:23:22 -0700102 uint32_t ret = s->run(this);
Jason Samsc9d43db2009-07-28 12:02:16 -0700103 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700104}
105
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800106uint32_t Context::runRootScript() {
Jason Sams2dca84d2009-12-09 11:05:45 -0800107 timerSet(RS_TIMER_SCRIPT);
Jason Sams93eacc72012-12-18 14:26:57 -0800108#ifndef RS_COMPATIBILITY_LIB
Jason Sams8c401ef2009-10-06 13:58:47 -0700109 mStateFragmentStore.mLast.clear();
Jason Sams93eacc72012-12-18 14:26:57 -0800110#endif
Jason Sams2382aba2011-09-13 15:41:01 -0700111 watchdog.inRoot = true;
Jason Samsc61346b2010-05-28 18:23:22 -0700112 uint32_t ret = runScript(mRootScript.get());
Jason Sams2382aba2011-09-13 15:41:01 -0700113 watchdog.inRoot = false;
Jason Sams8cfdd242009-10-14 15:43:53 -0700114
Jason Samscfb1d112009-08-05 13:57:03 -0700115 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700116}
117
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800118uint64_t Context::getTime() const {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700119#ifndef ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -0700120 struct timespec t;
121 clock_gettime(CLOCK_MONOTONIC, &t);
122 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700123#else
124 return 0;
125#endif //ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -0700126}
127
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800128void Context::timerReset() {
Jason Sams24371d92009-08-19 12:17:14 -0700129 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
130 mTimers[ct] = 0;
131 }
132}
133
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800134void Context::timerInit() {
Jason Sams24371d92009-08-19 12:17:14 -0700135 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700136 mTimeFrame = mTimeLast;
137 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700138 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700139 mAverageFPSFrameCount = 0;
140 mAverageFPSStartTime = mTimeLast;
141 mAverageFPS = 0;
Jason Sams24371d92009-08-19 12:17:14 -0700142 timerReset();
143}
144
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800145void Context::timerFrame() {
Jason Sams1d54f102009-09-03 15:43:13 -0700146 mTimeLastFrame = mTimeFrame;
147 mTimeFrame = getTime();
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700148 // Update average fps
149 const uint64_t averageFramerateInterval = 1000 * 1000000;
150 mAverageFPSFrameCount ++;
151 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800152 if (inverval >= averageFramerateInterval) {
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700153 inverval = inverval / 1000000;
154 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
155 mAverageFPSFrameCount = 0;
156 mAverageFPSStartTime = mTimeFrame;
157 }
Jason Sams1d54f102009-09-03 15:43:13 -0700158}
159
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800160void Context::timerSet(Timers tm) {
Jason Sams24371d92009-08-19 12:17:14 -0700161 uint64_t last = mTimeLast;
162 mTimeLast = getTime();
163 mTimers[mTimerActive] += mTimeLast - last;
164 mTimerActive = tm;
165}
166
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800167void Context::timerPrint() {
Jason Sams24371d92009-08-19 12:17:14 -0700168 double total = 0;
169 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
170 total += mTimers[ct];
171 }
Jason Sams1d54f102009-09-03 15:43:13 -0700172 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800173 mTimeMSLastFrame = frame / 1000000;
174 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
175 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700176
Jason Sams2dca84d2009-12-09 11:05:45 -0800177
178 if (props.mLogTimes) {
Steve Block65982012011-10-20 11:56:00 +0100179 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 -0800180 mTimeMSLastFrame,
181 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
182 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
183 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700184 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
185 mAverageFPS);
Jason Sams2dca84d2009-12-09 11:05:45 -0800186 }
Jason Sams24371d92009-08-19 12:17:14 -0700187}
188
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800189bool Context::setupCheck() {
Jason Sams93eacc72012-12-18 14:26:57 -0800190#ifndef RS_COMPATIBILITY_LIB
Jason Sams721acc42011-04-06 11:23:54 -0700191 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700192 mFragment->setup(this, &mStateFragment);
Jason Sams721acc42011-04-06 11:23:54 -0700193 mRaster->setup(this, &mStateRaster);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700194 mVertex->setup(this, &mStateVertex);
195 mFBOCache.setup(this);
Jason Sams93eacc72012-12-18 14:26:57 -0800196#endif
Jason Samsa2cf7552010-03-03 13:03:18 -0800197 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700198}
199
Jason Sams93eacc72012-12-18 14:26:57 -0800200#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700201void Context::setupProgramStore() {
Jason Sams721acc42011-04-06 11:23:54 -0700202 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700203}
Jason Sams93eacc72012-12-18 14:26:57 -0800204#endif
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700205
Jason Samsd1f7da62012-03-15 19:18:03 -0700206static uint32_t getProp(const char *str) {
Tim Murray0b575de2013-03-15 15:56:43 -0700207#ifndef RS_SERVER
Joe Onorato76371ff2009-09-23 16:37:36 -0700208 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700209 property_get(str, buf, "0");
Jason Samsd1f7da62012-03-15 19:18:03 -0700210 return atoi(buf);
Tim Murray0b575de2013-03-15 15:56:43 -0700211#else
212 return 0;
213#endif
Joe Onorato76371ff2009-09-23 16:37:36 -0700214}
Jason Sams326e0dd2009-05-22 14:03:28 -0700215
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800216void Context::displayDebugStats() {
Jason Sams93eacc72012-12-18 14:26:57 -0800217#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700218 char buffer[128];
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700219 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700220 float oldR, oldG, oldB, oldA;
221 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700222 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700223
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700224 ObjectBaseRef<Font> lastFont(getFont());
225 setFont(NULL);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700226 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700227 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700228 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700229
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700230 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700231 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700232
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700233 setFont(lastFont.get());
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700234 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Jason Sams93eacc72012-12-18 14:26:57 -0800235#endif
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700236}
237
Tim Murray0c66f072012-10-23 14:05:02 -0700238bool Context::loadRuntime(const char* filename, Context* rsc) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700239
Tim Murray0c66f072012-10-23 14:05:02 -0700240 // TODO: store the driverSO somewhere so we can dlclose later
Stephen Hines91dfcdb2012-09-07 18:23:35 -0700241 void *driverSO = NULL;
242
Tim Murray0c66f072012-10-23 14:05:02 -0700243 driverSO = dlopen(filename, RTLD_LAZY);
Stephen Hines91dfcdb2012-09-07 18:23:35 -0700244 if (driverSO == NULL) {
Tim Murray0c66f072012-10-23 14:05:02 -0700245 ALOGE("Failed loading RS driver: %s", dlerror());
246 return false;
Stephen Hines414a4612012-09-05 18:05:08 -0700247 }
248
249 // Need to call dlerror() to clear buffer before using it for dlsym().
250 (void) dlerror();
251 typedef bool (*HalSig)(Context*, uint32_t, uint32_t);
252 HalSig halInit = (HalSig) dlsym(driverSO, "rsdHalInit");
253
254 // If we can't find the C variant, we go looking for the C++ version.
255 if (halInit == NULL) {
256 ALOGW("Falling back to find C++ rsdHalInit: %s", dlerror());
257 halInit = (HalSig) dlsym(driverSO,
258 "_Z10rsdHalInitPN7android12renderscript7ContextEjj");
259 }
260
261 if (halInit == NULL) {
Stephen Hines414a4612012-09-05 18:05:08 -0700262 dlclose(driverSO);
263 ALOGE("Failed to find rsdHalInit: %s", dlerror());
Tim Murray0c66f072012-10-23 14:05:02 -0700264 return false;
Stephen Hines414a4612012-09-05 18:05:08 -0700265 }
266
267 if (!(*halInit)(rsc, 0, 0)) {
Stephen Hines414a4612012-09-05 18:05:08 -0700268 dlclose(driverSO);
Steve Blockaf12ac62012-01-06 19:20:56 +0000269 ALOGE("Hal init failed");
Tim Murray0c66f072012-10-23 14:05:02 -0700270 return false;
Jason Sams83c451a2011-04-21 11:46:50 -0700271 }
Tim Murray0c66f072012-10-23 14:05:02 -0700272
273 //validate HAL struct
274
275
276 return true;
277}
278
Jason Sams110f1812013-03-14 16:02:18 -0700279extern "C" bool rsdHalInit(RsContext c, uint32_t version_major, uint32_t version_minor);
280
Tim Murray0c66f072012-10-23 14:05:02 -0700281void * Context::threadProc(void *vrsc) {
282 Context *rsc = static_cast<Context *>(vrsc);
283#ifndef ANDROID_RS_SERIALIZE
284 rsc->mNativeThreadId = gettid();
Jason Sams93eacc72012-12-18 14:26:57 -0800285#ifndef RS_COMPATIBILITY_LIB
Tim Murray4d252d62012-11-29 14:37:59 -0800286 if (!rsc->isSynchronous()) {
287 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
288 }
Tim Murray0c66f072012-10-23 14:05:02 -0700289 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams93eacc72012-12-18 14:26:57 -0800290#else
291 if (!rsc->isSynchronous()) {
292 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, -4);
293 }
294 rsc->mThreadPriority = -4;
295#endif
Tim Murray0c66f072012-10-23 14:05:02 -0700296#endif //ANDROID_RS_SERIALIZE
297 rsc->props.mLogTimes = getProp("debug.rs.profile") != 0;
298 rsc->props.mLogScripts = getProp("debug.rs.script") != 0;
299 rsc->props.mLogObjects = getProp("debug.rs.object") != 0;
300 rsc->props.mLogShaders = getProp("debug.rs.shader") != 0;
301 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes") != 0;
302 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms") != 0;
303 rsc->props.mLogVisual = getProp("debug.rs.visual") != 0;
304 rsc->props.mDebugMaxThreads = getProp("debug.rs.max-threads");
305
306 bool loadDefault = true;
307
308 // Provide a mechanism for dropping in a different RS driver.
Jason Sams110f1812013-03-14 16:02:18 -0700309#ifndef RS_COMPATIBILITY_LIB
Tim Murray0c66f072012-10-23 14:05:02 -0700310#ifdef OVERRIDE_RS_DRIVER
311#define XSTR(S) #S
312#define STR(S) XSTR(S)
313#define OVERRIDE_RS_DRIVER_STRING STR(OVERRIDE_RS_DRIVER)
314
315 if (getProp("debug.rs.default-CPU-driver") != 0) {
316 ALOGE("Skipping override driver and loading default CPU driver");
Tim Murray0e92fa32012-11-06 14:36:38 -0800317 } else if (rsc->mForceCpu) {
318 ALOGV("Application requested CPU execution");
Tim Murray0c66f072012-10-23 14:05:02 -0700319 } else {
320 if (loadRuntime(OVERRIDE_RS_DRIVER_STRING, rsc)) {
321 ALOGE("Successfully loaded runtime: %s", OVERRIDE_RS_DRIVER_STRING);
322 loadDefault = false;
323 } else {
324 ALOGE("Failed to load runtime %s, loading default", OVERRIDE_RS_DRIVER_STRING);
325 }
326 }
327
328#undef XSTR
329#undef STR
330#endif // OVERRIDE_RS_DRIVER
331
332 if (loadDefault) {
333 if (!loadRuntime("libRSDriver.so", rsc)) {
334 ALOGE("Failed to load default runtime!");
Stephen Hines6f01bcf2012-11-19 15:18:16 -0800335 rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed loading RS driver");
Tim Murray0c66f072012-10-23 14:05:02 -0700336 return NULL;
337 }
338 }
Jason Sams110f1812013-03-14 16:02:18 -0700339#else // RS_COMPATIBILITY_LIB
340 if (rsdHalInit(rsc, 0, 0) != true) {
341 return NULL;
342 }
343#endif
344
Tim Murray0c66f072012-10-23 14:05:02 -0700345
Jason Sams83c451a2011-04-21 11:46:50 -0700346 rsc->mHal.funcs.setPriority(rsc, rsc->mThreadPriority);
Jason Samse5769102009-06-19 16:03:18 -0700347
Jason Sams93eacc72012-12-18 14:26:57 -0800348#ifndef RS_COMPATIBILITY_LIB
Jason Sams83c451a2011-04-21 11:46:50 -0700349 if (rsc->mIsGraphicsContext) {
Jason Samsd3e71072011-05-03 15:01:58 -0700350 if (!rsc->initGLThread()) {
351 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
352 return NULL;
353 }
354
Jason Sams83c451a2011-04-21 11:46:50 -0700355 rsc->mStateRaster.init(rsc);
356 rsc->setProgramRaster(NULL);
357 rsc->mStateVertex.init(rsc);
358 rsc->setProgramVertex(NULL);
359 rsc->mStateFragment.init(rsc);
360 rsc->setProgramFragment(NULL);
361 rsc->mStateFragmentStore.init(rsc);
362 rsc->setProgramStore(NULL);
363 rsc->mStateFont.init(rsc);
364 rsc->setFont(NULL);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700365 rsc->mStateSampler.init(rsc);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700366 rsc->mFBOCache.init(rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700367 }
Jason Sams93eacc72012-12-18 14:26:57 -0800368#endif
Jason Sams8ce125b2009-06-17 16:52:59 -0700369
Jason Sams83c451a2011-04-21 11:46:50 -0700370 rsc->mRunning = true;
Tim Murray4d252d62012-11-29 14:37:59 -0800371
372 if (rsc->isSynchronous()) {
373 return NULL;
374 }
375
Jason Sams5f27d6f2012-02-07 15:32:08 -0800376 if (!rsc->mIsGraphicsContext) {
377 while (!rsc->mExit) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800378 rsc->mIO.playCoreCommands(rsc, -1);
Jason Samse0aab4a2011-08-12 15:05:15 -0700379 }
Jason Sams93eacc72012-12-18 14:26:57 -0800380#ifndef RS_COMPATIBILITY_LIB
Jason Sams5f27d6f2012-02-07 15:32:08 -0800381 } else {
382#ifndef ANDROID_RS_SERIALIZE
383 DisplayEventReceiver displayEvent;
384 DisplayEventReceiver::Event eventBuffer[1];
385#endif
386 int vsyncRate = 0;
387 int targetRate = 0;
Jason Samse0aab4a2011-08-12 15:05:15 -0700388
Jason Sams5f27d6f2012-02-07 15:32:08 -0800389 bool drawOnce = false;
390 while (!rsc->mExit) {
391 rsc->timerSet(RS_TIMER_IDLE);
Jason Sams326e0dd2009-05-22 14:03:28 -0700392
Jason Sams5f27d6f2012-02-07 15:32:08 -0800393#ifndef ANDROID_RS_SERIALIZE
Jason Sams9afd9a52012-02-17 16:59:50 -0800394 if (!rsc->mRootScript.get() || !rsc->mHasSurface || rsc->mPaused) {
395 targetRate = 0;
396 }
397
Jason Sams5f27d6f2012-02-07 15:32:08 -0800398 if (vsyncRate != targetRate) {
399 displayEvent.setVsyncRate(targetRate);
400 vsyncRate = targetRate;
401 }
402 if (targetRate) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800403 drawOnce |= rsc->mIO.playCoreCommands(rsc, displayEvent.getFd());
Jason Sams5f27d6f2012-02-07 15:32:08 -0800404 while (displayEvent.getEvents(eventBuffer, 1) != 0) {
405 //ALOGE("vs2 time past %lld", (rsc->getTime() - eventBuffer[0].header.timestamp) / 1000000);
406 }
407 } else
408#endif
409 {
Jason Sams963a2fb2012-02-09 14:36:14 -0800410 drawOnce |= rsc->mIO.playCoreCommands(rsc, -1);
Jason Sams83c451a2011-04-21 11:46:50 -0700411 }
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700412
Jason Sams5f27d6f2012-02-07 15:32:08 -0800413 if ((rsc->mRootScript.get() != NULL) && rsc->mHasSurface &&
414 (targetRate || drawOnce) && !rsc->mPaused) {
415
416 drawOnce = false;
417 targetRate = ((rsc->runRootScript() + 15) / 16);
418
419 if (rsc->props.mLogVisual) {
420 rsc->displayDebugStats();
421 }
422
423 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
424 rsc->mHal.funcs.swap(rsc);
425 rsc->timerFrame();
426 rsc->timerSet(RS_TIMER_INTERNAL);
427 rsc->timerPrint();
428 rsc->timerReset();
429 }
Jason Sams83c451a2011-04-21 11:46:50 -0700430 }
Jason Sams93eacc72012-12-18 14:26:57 -0800431#endif
Jason Sams83c451a2011-04-21 11:46:50 -0700432 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700433
Steve Block65982012011-10-20 11:56:00 +0100434 ALOGV("%p RS Thread exiting", rsc);
Jason Samse514b452009-09-25 14:51:22 -0700435
Jason Sams93eacc72012-12-18 14:26:57 -0800436#ifndef RS_COMPATIBILITY_LIB
Jason Sams83c451a2011-04-21 11:46:50 -0700437 if (rsc->mIsGraphicsContext) {
438 pthread_mutex_lock(&gInitMutex);
439 rsc->deinitEGL();
440 pthread_mutex_unlock(&gInitMutex);
441 }
Jason Sams93eacc72012-12-18 14:26:57 -0800442#endif
Jason Sams33b6e3b2009-10-27 14:44:31 -0700443
Steve Block65982012011-10-20 11:56:00 +0100444 ALOGV("%p RS Thread exited", rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700445 return NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700446}
447
Jason Sams741aac92010-12-24 14:38:39 -0800448void Context::destroyWorkerThreadResources() {
Steve Block65982012011-10-20 11:56:00 +0100449 //ALOGV("destroyWorkerThreadResources 1");
Jason Sams2e8665d2011-01-27 00:14:13 -0800450 ObjectBase::zeroAllUserRef(this);
Jason Sams93eacc72012-12-18 14:26:57 -0800451#ifndef RS_COMPATIBILITY_LIB
Jason Sams741aac92010-12-24 14:38:39 -0800452 if (mIsGraphicsContext) {
453 mRaster.clear();
454 mFragment.clear();
455 mVertex.clear();
456 mFragmentStore.clear();
457 mFont.clear();
458 mRootScript.clear();
459 mStateRaster.deinit(this);
460 mStateVertex.deinit(this);
461 mStateFragment.deinit(this);
462 mStateFragmentStore.deinit(this);
463 mStateFont.deinit(this);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700464 mStateSampler.deinit(this);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700465 mFBOCache.deinit(this);
Jason Sams741aac92010-12-24 14:38:39 -0800466 }
Jason Sams93eacc72012-12-18 14:26:57 -0800467#endif
Jason Samsc7cec1e2011-08-18 18:01:33 -0700468 ObjectBase::freeAllChildren(this);
Jason Samscf912de2011-01-09 16:09:51 -0800469 mExit = true;
Jason Sams5f27d6f2012-02-07 15:32:08 -0800470 //ALOGV("destroyWorkerThreadResources 2");
Jason Sams741aac92010-12-24 14:38:39 -0800471}
472
Jason Sams2382aba2011-09-13 15:41:01 -0700473void Context::printWatchdogInfo(void *ctx) {
474 Context *rsc = (Context *)ctx;
Jason Samsee803442011-10-13 16:05:27 -0700475 if (rsc->watchdog.command && rsc->watchdog.file) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000476 ALOGE("RS watchdog timeout: %i %s line %i %s", rsc->watchdog.inRoot,
Jason Samsee803442011-10-13 16:05:27 -0700477 rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file);
478 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000479 ALOGE("RS watchdog timeout: %i", rsc->watchdog.inRoot);
Jason Samsee803442011-10-13 16:05:27 -0700480 }
Jason Sams2382aba2011-09-13 15:41:01 -0700481}
482
483
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800484void Context::setPriority(int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800485 // Note: If we put this in the proper "background" policy
486 // the wallpapers can become completly unresponsive at times.
487 // This is probably not what we want for something the user is actively
488 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800489 mThreadPriority = p;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700490 setpriority(PRIO_PROCESS, mNativeThreadId, p);
Jason Sams9719bd42012-01-12 14:22:21 -0800491 mHal.funcs.setPriority(this, mThreadPriority);
Jason Sams15832442009-11-15 12:14:26 -0800492}
493
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800494Context::Context() {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700495 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700496 mRunning = false;
497 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700498 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700499 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800500 mError = RS_ERROR_NONE;
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700501 mTargetSdkVersion = 14;
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700502 mDPI = 96;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700503 mIsContextLite = false;
Stephen Hines86c6b5f2011-10-31 14:07:54 -0700504 memset(&watchdog, 0, sizeof(watchdog));
Tim Murray0e92fa32012-11-06 14:36:38 -0800505 mForceCpu = false;
Tim Murray4d252d62012-11-29 14:37:59 -0800506 mSynchronous = false;
Tim Murray0e92fa32012-11-06 14:36:38 -0800507}
508
509Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc,
Tim Murray4d252d62012-11-29 14:37:59 -0800510 bool forceCpu, bool synchronous) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700511 Context * rsc = new Context();
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700512
Tim Murray0e92fa32012-11-06 14:36:38 -0800513 rsc->mForceCpu = forceCpu;
Tim Murray4d252d62012-11-29 14:37:59 -0800514 rsc->mSynchronous = synchronous;
Tim Murray0e92fa32012-11-06 14:36:38 -0800515
Jason Sams5c1c79a2010-11-03 14:27:11 -0700516 if (!rsc->initContext(dev, sc)) {
517 delete rsc;
518 return NULL;
519 }
520 return rsc;
521}
522
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700523Context * Context::createContextLite() {
524 Context * rsc = new Context();
525 rsc->mIsContextLite = true;
526 return rsc;
527}
528
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800529bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700530 pthread_mutex_lock(&gInitMutex);
531
Jason Sams1a4efa32011-05-17 15:01:29 -0700532 mIO.init();
Jason Sams5f27d6f2012-02-07 15:32:08 -0800533 mIO.setTimeoutCallback(printWatchdogInfo, this, 2e9);
Jason Sams1a4efa32011-05-17 15:01:29 -0700534
Jason Sams5c1c79a2010-11-03 14:27:11 -0700535 dev->addContext(this);
536 mDev = dev;
Jason Sams6b8552a2010-10-13 15:31:10 -0700537 if (sc) {
538 mUserSurfaceConfig = *sc;
539 } else {
540 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
541 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800542
Jason Sams6b8552a2010-10-13 15:31:10 -0700543 mIsGraphicsContext = sc != NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700544
Jason Samsa658e902009-06-04 14:35:01 -0700545 int status;
546 pthread_attr_t threadAttr;
547
Jason Samsfb03a222009-10-15 16:47:31 -0700548 pthread_mutex_unlock(&gInitMutex);
549
550 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700551
Jason Samsa658e902009-06-04 14:35:01 -0700552 status = pthread_attr_init(&threadAttr);
553 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000554 ALOGE("Failed to init thread attribute.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700555 return false;
Jason Samsa658e902009-06-04 14:35:01 -0700556 }
557
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700558 mHasSurface = false;
Jason Sams992a0b72009-06-23 12:22:47 -0700559
Jason Sams24371d92009-08-19 12:17:14 -0700560 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700561 timerSet(RS_TIMER_INTERNAL);
Tim Murray4d252d62012-11-29 14:37:59 -0800562 if (mSynchronous) {
563 threadProc(this);
564 } else {
565 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
566 if (status) {
567 ALOGE("Failed to start rs context thread.");
568 return false;
569 }
570 while (!mRunning && (mError == RS_ERROR_NONE)) {
571 usleep(100);
572 }
Jason Sams50869382009-08-18 17:07:09 -0700573
Tim Murray4d252d62012-11-29 14:37:59 -0800574 if (mError != RS_ERROR_NONE) {
575 ALOGE("Errors during thread init");
576 return false;
577 }
Jason Sams18133402010-07-20 15:09:00 -0700578
Tim Murray4d252d62012-11-29 14:37:59 -0800579 pthread_attr_destroy(&threadAttr);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700580 }
Jason Sams5c1c79a2010-11-03 14:27:11 -0700581 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700582}
583
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800584Context::~Context() {
Steve Block65982012011-10-20 11:56:00 +0100585 ALOGV("%p Context::~Context", this);
Jason Samscf912de2011-01-09 16:09:51 -0800586
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700587 if (!mIsContextLite) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700588 mPaused = false;
589 void *res;
Jason Sams326e0dd2009-05-22 14:03:28 -0700590
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700591 mIO.shutdown();
592 int status = pthread_join(mThreadId, &res);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800593 rsAssert(mExit);
Jason Sams326e0dd2009-05-22 14:03:28 -0700594
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700595 if (mHal.funcs.shutdownDriver) {
596 mHal.funcs.shutdownDriver(this);
597 }
598
599 // Global structure cleanup.
600 pthread_mutex_lock(&gInitMutex);
601 if (mDev) {
602 mDev->removeContext(this);
603 mDev = NULL;
604 }
605 pthread_mutex_unlock(&gInitMutex);
Jason Sams51462c52011-01-25 00:26:25 -0800606 }
Steve Block65982012011-10-20 11:56:00 +0100607 ALOGV("%p Context::~Context done", this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700608}
609
Jason Sams93eacc72012-12-18 14:26:57 -0800610#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700611void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800612 rsAssert(mIsGraphicsContext);
Jason Sams4b3de472011-04-06 17:52:23 -0700613 mHal.funcs.setSurface(this, w, h, sur);
Jason Sams458f2dc2009-11-03 13:58:36 -0800614
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700615 mHasSurface = sur != NULL;
Jason Sams4b3de472011-04-06 17:52:23 -0700616 mWidth = w;
617 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800618
Jason Sams4b3de472011-04-06 17:52:23 -0700619 if (mWidth && mHeight) {
Jason Sams771565f2010-05-14 15:30:29 -0700620 mStateVertex.updateSize(this);
Alex Sakhartchouka544b632011-07-19 17:50:29 -0700621 mFBOCache.updateSize();
Jason Sams458f2dc2009-11-03 13:58:36 -0800622 }
623}
624
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800625uint32_t Context::getCurrentSurfaceWidth() const {
626 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
627 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
628 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimX();
629 }
630 }
631 if (mFBOCache.mHal.state.depthTarget != NULL) {
632 return mFBOCache.mHal.state.depthTarget->getType()->getDimX();
633 }
634 return mWidth;
635}
636
637uint32_t Context::getCurrentSurfaceHeight() const {
638 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
639 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
640 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimY();
641 }
642 }
643 if (mFBOCache.mHal.state.depthTarget != NULL) {
644 return mFBOCache.mHal.state.depthTarget->getType()->getDimY();
645 }
646 return mHeight;
647}
648
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800649void Context::pause() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800650 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700651 mPaused = true;
652}
653
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800654void Context::resume() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800655 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700656 mPaused = false;
657}
658
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800659void Context::setRootScript(Script *s) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800660 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700661 mRootScript.set(s);
662}
663
Jason Sams60709252010-11-17 15:29:32 -0800664void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800665 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700666 if (pfs == NULL) {
667 mFragmentStore.set(mStateFragmentStore.mDefault);
668 } else {
669 mFragmentStore.set(pfs);
670 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700671}
672
Jason Sams60709252010-11-17 15:29:32 -0800673void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800674 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700675 if (pf == NULL) {
676 mFragment.set(mStateFragment.mDefault);
677 } else {
678 mFragment.set(pf);
679 }
Jason Samscfb1d112009-08-05 13:57:03 -0700680}
681
Jason Sams60709252010-11-17 15:29:32 -0800682void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800683 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700684 if (pr == NULL) {
685 mRaster.set(mStateRaster.mDefault);
686 } else {
687 mRaster.set(pr);
688 }
689}
690
Jason Sams60709252010-11-17 15:29:32 -0800691void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800692 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700693 if (pv == NULL) {
694 mVertex.set(mStateVertex.mDefault);
695 } else {
696 mVertex.set(pv);
697 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700698}
699
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800700void Context::setFont(Font *f) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700701 rsAssert(mIsGraphicsContext);
702 if (f == NULL) {
703 mFont.set(mStateFont.mDefault);
704 } else {
705 mFont.set(f);
706 }
707}
Jason Sams93eacc72012-12-18 14:26:57 -0800708#endif
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700709
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800710void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700711 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700712 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700713 mNames.add(obj);
714}
715
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800716void Context::removeName(ObjectBase *obj) {
717 for (size_t ct=0; ct < mNames.size(); ct++) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700718 if (obj == mNames[ct]) {
719 mNames.removeAt(ct);
720 return;
721 }
722 }
723}
724
Jason Sams1a4efa32011-05-17 15:01:29 -0700725RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) {
726 return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800727}
728
Jason Sams1a4efa32011-05-17 15:01:29 -0700729RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) {
730 return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen);
Jason Sams8c401ef2009-10-06 13:58:47 -0700731}
732
Jason Sams87319de2010-11-22 16:20:16 -0800733bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
734 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Sams1a4efa32011-05-17 15:01:29 -0700735
Jason Sams4961cce2013-04-11 16:11:46 -0700736 pthread_mutex_lock(&gMessageMutex);
737 bool ret = mIO.sendToClient(cmdID, subID, data, len, waitForSpace);
738 pthread_mutex_unlock(&gMessageMutex);
739 return ret;
Jason Sams8c401ef2009-10-06 13:58:47 -0700740}
741
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800742void Context::initToClient() {
743 while (!mRunning) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700744 usleep(100);
745 }
746}
747
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800748void Context::deinitToClient() {
Jason Sams1a4efa32011-05-17 15:01:29 -0700749 mIO.clientShutdown();
Jason Sams8c401ef2009-10-06 13:58:47 -0700750}
Jason Sams50869382009-08-18 17:07:09 -0700751
Jason Sams87319de2010-11-22 16:20:16 -0800752void Context::setError(RsError e, const char *msg) const {
Jason Samsa2cf7552010-03-03 13:03:18 -0800753 mError = e;
Jason Samsaad4bc52010-11-08 17:06:46 -0800754 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Samsa2cf7552010-03-03 13:03:18 -0800755}
756
757
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800758void Context::dumpDebug() const {
Steve Blockaf12ac62012-01-06 19:20:56 +0000759 ALOGE("RS Context debug %p", this);
760 ALOGE("RS Context debug");
Jason Sams13e26342009-11-24 12:26:35 -0800761
Steve Blockaf12ac62012-01-06 19:20:56 +0000762 ALOGE(" RS width %i, height %i", mWidth, mHeight);
763 ALOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
764 ALOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId);
Jason Sams13e26342009-11-24 12:26:35 -0800765}
Jason Samsa4a54e42009-06-10 18:39:40 -0700766
Jason Sams326e0dd2009-05-22 14:03:28 -0700767///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700768//
Jason Sams326e0dd2009-05-22 14:03:28 -0700769
770namespace android {
771namespace renderscript {
772
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800773void rsi_ContextFinish(Context *rsc) {
Jason Sams8c880902010-06-15 12:15:57 -0700774}
Jason Sams326e0dd2009-05-22 14:03:28 -0700775
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800776void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Sams93eacc72012-12-18 14:26:57 -0800777#ifndef RS_COMPATIBILITY_LIB
Jason Sams326e0dd2009-05-22 14:03:28 -0700778 Script *s = static_cast<Script *>(vs);
779 rsc->setRootScript(s);
Jason Sams93eacc72012-12-18 14:26:57 -0800780#endif
Jason Sams326e0dd2009-05-22 14:03:28 -0700781}
782
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800783void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700784 Sampler *s = static_cast<Sampler *>(vs);
785
786 if (slot > RS_MAX_SAMPLER_SLOT) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000787 ALOGE("Invalid sampler slot");
Jason Sams326e0dd2009-05-22 14:03:28 -0700788 return;
789 }
790
791 s->bindToContext(&rsc->mStateSampler, slot);
792}
793
Jason Sams93eacc72012-12-18 14:26:57 -0800794#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800795void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Samsccc010b2010-05-13 18:30:11 -0700796 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams60709252010-11-17 15:29:32 -0800797 rsc->setProgramStore(pfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700798}
799
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800800void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700801 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Sams60709252010-11-17 15:29:32 -0800802 rsc->setProgramFragment(pf);
Jason Sams326e0dd2009-05-22 14:03:28 -0700803}
804
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800805void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Sams5fd09d82009-09-23 13:57:02 -0700806 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Sams60709252010-11-17 15:29:32 -0800807 rsc->setProgramRaster(pr);
Jason Sams5fd09d82009-09-23 13:57:02 -0700808}
809
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800810void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700811 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Sams60709252010-11-17 15:29:32 -0800812 rsc->setProgramVertex(pv);
Jason Sams326e0dd2009-05-22 14:03:28 -0700813}
814
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800815void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700816 Font *font = static_cast<Font *>(vfont);
817 rsc->setFont(font);
818}
Jason Sams93eacc72012-12-18 14:26:57 -0800819#endif
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700820
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700821void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700822 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700823 rsc->assignName(ob, name, name_length);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700824}
Jason Sams326e0dd2009-05-22 14:03:28 -0700825
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800826void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams2353ae32010-10-14 17:48:46 -0700827 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -0700828 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700829 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700830}
831
Jason Sams93eacc72012-12-18 14:26:57 -0800832#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800833void rsi_ContextPause(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700834 rsc->pause();
835}
836
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800837void rsi_ContextResume(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700838 rsc->resume();
839}
840
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700841void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
Mathias Agopianfa402862010-02-12 14:04:35 -0800842 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800843}
Jason Sams93eacc72012-12-18 14:26:57 -0800844#endif
Jason Sams613cad12009-11-12 15:10:25 -0800845
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800846void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800847 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800848}
849
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800850void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Samsc21cf402009-11-17 17:26:46 -0800851 ObjectBase::dumpAll(rsc);
852}
853
Jason Sams741aac92010-12-24 14:38:39 -0800854void rsi_ContextDestroyWorker(Context *rsc) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700855 rsc->destroyWorkerThreadResources();
Jason Sams741aac92010-12-24 14:38:39 -0800856}
857
Jason Samsc975cf42011-04-28 18:26:48 -0700858void rsi_ContextDestroy(Context *rsc) {
Steve Block65982012011-10-20 11:56:00 +0100859 ALOGV("%p rsContextDestroy", rsc);
Jason Sams741aac92010-12-24 14:38:39 -0800860 rsContextDestroyWorker(rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800861 delete rsc;
Steve Block65982012011-10-20 11:56:00 +0100862 ALOGV("%p rsContextDestroy done", rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800863}
864
Jason Sams326e0dd2009-05-22 14:03:28 -0700865
Jason Samsc975cf42011-04-28 18:26:48 -0700866RsMessageToClientType rsi_ContextPeekMessage(Context *rsc,
Jason Sams186e5912011-04-26 14:50:00 -0700867 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700868 uint32_t * subID, size_t subID_length) {
869 return rsc->peekMessageToClient(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800870}
871
Jason Samsc975cf42011-04-28 18:26:48 -0700872RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length,
Jason Sams186e5912011-04-26 14:50:00 -0700873 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700874 uint32_t * subID, size_t subID_length) {
Jason Sams186e5912011-04-26 14:50:00 -0700875 rsAssert(subID_length == sizeof(uint32_t));
876 rsAssert(receiveLen_length == sizeof(size_t));
Jason Sams1a4efa32011-05-17 15:01:29 -0700877 return rsc->getMessageToClient(data, receiveLen, subID, data_length);
Jason Sams8c401ef2009-10-06 13:58:47 -0700878}
879
Jason Samsc975cf42011-04-28 18:26:48 -0700880void rsi_ContextInitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700881 rsc->initToClient();
882}
883
Jason Samsc975cf42011-04-28 18:26:48 -0700884void rsi_ContextDeinitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700885 rsc->deinitToClient();
886}
887
Jason Sams70265202013-02-05 19:20:47 -0800888void rsi_ContextSendMessage(Context *rsc, uint32_t id, const uint8_t *data, size_t len) {
889 rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, id, len, true);
890}
891
Jason Samsc975cf42011-04-28 18:26:48 -0700892}
893}
894
Jason Sams14982c82013-02-22 18:17:05 -0800895RsContext rsContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion,
896 RsContextType ct, bool forceCpu, bool synchronous) {
Steve Block65982012011-10-20 11:56:00 +0100897 ALOGV("rsContextCreate dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700898 Device * dev = static_cast<Device *>(vdev);
Tim Murray4d252d62012-11-29 14:37:59 -0800899 Context *rsc = Context::createContext(dev, NULL, forceCpu, synchronous);
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700900 if (rsc) {
901 rsc->setTargetSdkVersion(sdkVersion);
902 }
Jason Sams789ca832011-05-18 17:36:02 -0700903 return rsc;
904}
905
Jason Sams93eacc72012-12-18 14:26:57 -0800906#ifndef RS_COMPATIBILITY_LIB
Jason Sams789ca832011-05-18 17:36:02 -0700907RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700908 uint32_t sdkVersion, RsSurfaceConfig sc,
909 uint32_t dpi) {
Steve Block65982012011-10-20 11:56:00 +0100910 ALOGV("rsContextCreateGL dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700911 Device * dev = static_cast<Device *>(vdev);
912 Context *rsc = Context::createContext(dev, &sc);
Jason Sams9544f762011-07-13 16:09:42 -0700913 if (rsc) {
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700914 rsc->setTargetSdkVersion(sdkVersion);
Jason Sams9544f762011-07-13 16:09:42 -0700915 rsc->setDPI(dpi);
916 }
Steve Block65982012011-10-20 11:56:00 +0100917 ALOGV("%p rsContextCreateGL ret", rsc);
Jason Sams789ca832011-05-18 17:36:02 -0700918 return rsc;
919}
Jason Sams93eacc72012-12-18 14:26:57 -0800920#endif
Jason Sams789ca832011-05-18 17:36:02 -0700921
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700922// Only to be called at a3d load time, before object is visible to user
923// not thread safe
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800924void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700925 ObjectBase *ob = static_cast<ObjectBase *>(obj);
926 (*name) = ob->getName();
927}