blob: 8d972a2e82426a8c7970cd878c24276d2a365663 [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
Joe Onorato76371ff2009-09-23 16:37:36 -070032#include <cutils/properties.h>
33
Jason Sams8d957fa2010-09-28 14:41:22 -070034#include <sys/syscall.h>
Jason Sams93eacc72012-12-18 14:26:57 -080035#include <string.h>
Stephen Hines414a4612012-09-05 18:05:08 -070036#include <dlfcn.h>
37
Jason Sams326e0dd2009-05-22 14:03:28 -070038using namespace android;
39using namespace android::renderscript;
40
Jason Samsfb03a222009-10-15 16:47:31 -070041pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Stephen Hinesca3f09c2011-01-07 15:11:30 -080042pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070043
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080044bool Context::initGLThread() {
Jason Sams6b8552a2010-10-13 15:31:10 -070045 pthread_mutex_lock(&gInitMutex);
Jason Sams6b8552a2010-10-13 15:31:10 -070046
Jason Sams4b3de472011-04-06 17:52:23 -070047 if (!mHal.funcs.initGraphics(this)) {
Jason Sams5c1c79a2010-11-03 14:27:11 -070048 pthread_mutex_unlock(&gInitMutex);
Steve Blockaf12ac62012-01-06 19:20:56 +000049 ALOGE("%p initGraphics failed", this);
Jason Sams5c1c79a2010-11-03 14:27:11 -070050 return false;
Jason Sams6b8552a2010-10-13 15:31:10 -070051 }
52
Jason Sams6b8552a2010-10-13 15:31:10 -070053 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -070054 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -070055}
56
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080057void Context::deinitEGL() {
Jason Sams93eacc72012-12-18 14:26:57 -080058#ifndef RS_COMPATIBILITY_LIB
Jason Sams4b3de472011-04-06 17:52:23 -070059 mHal.funcs.shutdownGraphics(this);
Jason Sams93eacc72012-12-18 14:26:57 -080060#endif
Jason Sams33b6e3b2009-10-27 14:44:31 -070061}
62
Jason Sams60709252010-11-17 15:29:32 -080063Context::PushState::PushState(Context *con) {
64 mRsc = con;
Jason Sams93eacc72012-12-18 14:26:57 -080065#ifndef RS_COMPATIBILITY_LIB
Jason Samsc946b612011-02-23 14:47:17 -080066 if (con->mIsGraphicsContext) {
67 mFragment.set(con->getProgramFragment());
68 mVertex.set(con->getProgramVertex());
69 mStore.set(con->getProgramStore());
70 mRaster.set(con->getProgramRaster());
71 mFont.set(con->getFont());
72 }
Jason Sams93eacc72012-12-18 14:26:57 -080073#endif
Jason Sams60709252010-11-17 15:29:32 -080074}
75
76Context::PushState::~PushState() {
Jason Sams93eacc72012-12-18 14:26:57 -080077#ifndef RS_COMPATIBILITY_LIB
Jason Samsc946b612011-02-23 14:47:17 -080078 if (mRsc->mIsGraphicsContext) {
79 mRsc->setProgramFragment(mFragment.get());
80 mRsc->setProgramVertex(mVertex.get());
81 mRsc->setProgramStore(mStore.get());
82 mRsc->setProgramRaster(mRaster.get());
83 mRsc->setFont(mFont.get());
84 }
Jason Sams93eacc72012-12-18 14:26:57 -080085#endif
Jason Sams60709252010-11-17 15:29:32 -080086}
87
Jason Sams33b6e3b2009-10-27 14:44:31 -070088
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080089uint32_t Context::runScript(Script *s) {
Shih-wei Liaoda3b58d2012-08-03 04:24:33 -070090 PushState ps(this);
Jason Sams10308932009-06-09 12:15:30 -070091
Jason Samsc61346b2010-05-28 18:23:22 -070092 uint32_t ret = s->run(this);
Jason Samsc9d43db2009-07-28 12:02:16 -070093 return ret;
Jason Sams10308932009-06-09 12:15:30 -070094}
95
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080096uint32_t Context::runRootScript() {
Jason Sams2dca84d2009-12-09 11:05:45 -080097 timerSet(RS_TIMER_SCRIPT);
Jason Sams93eacc72012-12-18 14:26:57 -080098#ifndef RS_COMPATIBILITY_LIB
Jason Sams8c401ef2009-10-06 13:58:47 -070099 mStateFragmentStore.mLast.clear();
Jason Sams93eacc72012-12-18 14:26:57 -0800100#endif
Jason Sams2382aba2011-09-13 15:41:01 -0700101 watchdog.inRoot = true;
Jason Samsc61346b2010-05-28 18:23:22 -0700102 uint32_t ret = runScript(mRootScript.get());
Jason Sams2382aba2011-09-13 15:41:01 -0700103 watchdog.inRoot = false;
Jason Sams8cfdd242009-10-14 15:43:53 -0700104
Jason Samscfb1d112009-08-05 13:57:03 -0700105 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700106}
107
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800108uint64_t Context::getTime() const {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700109#ifndef ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -0700110 struct timespec t;
111 clock_gettime(CLOCK_MONOTONIC, &t);
112 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700113#else
114 return 0;
115#endif //ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -0700116}
117
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800118void Context::timerReset() {
Jason Sams24371d92009-08-19 12:17:14 -0700119 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
120 mTimers[ct] = 0;
121 }
122}
123
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800124void Context::timerInit() {
Jason Sams24371d92009-08-19 12:17:14 -0700125 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700126 mTimeFrame = mTimeLast;
127 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700128 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700129 mAverageFPSFrameCount = 0;
130 mAverageFPSStartTime = mTimeLast;
131 mAverageFPS = 0;
Jason Sams24371d92009-08-19 12:17:14 -0700132 timerReset();
133}
134
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800135void Context::timerFrame() {
Jason Sams1d54f102009-09-03 15:43:13 -0700136 mTimeLastFrame = mTimeFrame;
137 mTimeFrame = getTime();
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700138 // Update average fps
139 const uint64_t averageFramerateInterval = 1000 * 1000000;
140 mAverageFPSFrameCount ++;
141 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800142 if (inverval >= averageFramerateInterval) {
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700143 inverval = inverval / 1000000;
144 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
145 mAverageFPSFrameCount = 0;
146 mAverageFPSStartTime = mTimeFrame;
147 }
Jason Sams1d54f102009-09-03 15:43:13 -0700148}
149
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800150void Context::timerSet(Timers tm) {
Jason Sams24371d92009-08-19 12:17:14 -0700151 uint64_t last = mTimeLast;
152 mTimeLast = getTime();
153 mTimers[mTimerActive] += mTimeLast - last;
154 mTimerActive = tm;
155}
156
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800157void Context::timerPrint() {
Jason Sams24371d92009-08-19 12:17:14 -0700158 double total = 0;
159 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
160 total += mTimers[ct];
161 }
Jason Sams1d54f102009-09-03 15:43:13 -0700162 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800163 mTimeMSLastFrame = frame / 1000000;
164 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
165 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700166
Jason Sams2dca84d2009-12-09 11:05:45 -0800167
168 if (props.mLogTimes) {
Steve Block65982012011-10-20 11:56:00 +0100169 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 -0800170 mTimeMSLastFrame,
171 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
172 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
173 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700174 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
175 mAverageFPS);
Jason Sams2dca84d2009-12-09 11:05:45 -0800176 }
Jason Sams24371d92009-08-19 12:17:14 -0700177}
178
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800179bool Context::setupCheck() {
Jason Sams93eacc72012-12-18 14:26:57 -0800180#ifndef RS_COMPATIBILITY_LIB
Jason Sams721acc42011-04-06 11:23:54 -0700181 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700182 mFragment->setup(this, &mStateFragment);
Jason Sams721acc42011-04-06 11:23:54 -0700183 mRaster->setup(this, &mStateRaster);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700184 mVertex->setup(this, &mStateVertex);
185 mFBOCache.setup(this);
Jason Sams93eacc72012-12-18 14:26:57 -0800186#endif
Jason Samsa2cf7552010-03-03 13:03:18 -0800187 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700188}
189
Jason Sams93eacc72012-12-18 14:26:57 -0800190#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700191void Context::setupProgramStore() {
Jason Sams721acc42011-04-06 11:23:54 -0700192 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700193}
Jason Sams93eacc72012-12-18 14:26:57 -0800194#endif
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700195
Jason Samsd1f7da62012-03-15 19:18:03 -0700196static uint32_t getProp(const char *str) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700197 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700198 property_get(str, buf, "0");
Jason Samsd1f7da62012-03-15 19:18:03 -0700199 return atoi(buf);
Joe Onorato76371ff2009-09-23 16:37:36 -0700200}
Jason Sams326e0dd2009-05-22 14:03:28 -0700201
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800202void Context::displayDebugStats() {
Jason Sams93eacc72012-12-18 14:26:57 -0800203#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700204 char buffer[128];
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700205 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700206 float oldR, oldG, oldB, oldA;
207 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700208 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700209
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700210 ObjectBaseRef<Font> lastFont(getFont());
211 setFont(NULL);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700212 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700213 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700214 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700215
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700216 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700217 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700218
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700219 setFont(lastFont.get());
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700220 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Jason Sams93eacc72012-12-18 14:26:57 -0800221#endif
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700222}
223
Tim Murray0c66f072012-10-23 14:05:02 -0700224bool Context::loadRuntime(const char* filename, Context* rsc) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700225
Tim Murray0c66f072012-10-23 14:05:02 -0700226 // TODO: store the driverSO somewhere so we can dlclose later
Stephen Hines91dfcdb2012-09-07 18:23:35 -0700227 void *driverSO = NULL;
228
Tim Murray0c66f072012-10-23 14:05:02 -0700229 driverSO = dlopen(filename, RTLD_LAZY);
Stephen Hines91dfcdb2012-09-07 18:23:35 -0700230 if (driverSO == NULL) {
Tim Murray0c66f072012-10-23 14:05:02 -0700231 ALOGE("Failed loading RS driver: %s", dlerror());
232 return false;
Stephen Hines414a4612012-09-05 18:05:08 -0700233 }
234
235 // Need to call dlerror() to clear buffer before using it for dlsym().
236 (void) dlerror();
237 typedef bool (*HalSig)(Context*, uint32_t, uint32_t);
238 HalSig halInit = (HalSig) dlsym(driverSO, "rsdHalInit");
239
240 // If we can't find the C variant, we go looking for the C++ version.
241 if (halInit == NULL) {
242 ALOGW("Falling back to find C++ rsdHalInit: %s", dlerror());
243 halInit = (HalSig) dlsym(driverSO,
244 "_Z10rsdHalInitPN7android12renderscript7ContextEjj");
245 }
246
247 if (halInit == NULL) {
Stephen Hines414a4612012-09-05 18:05:08 -0700248 dlclose(driverSO);
249 ALOGE("Failed to find rsdHalInit: %s", dlerror());
Tim Murray0c66f072012-10-23 14:05:02 -0700250 return false;
Stephen Hines414a4612012-09-05 18:05:08 -0700251 }
252
253 if (!(*halInit)(rsc, 0, 0)) {
Stephen Hines414a4612012-09-05 18:05:08 -0700254 dlclose(driverSO);
Steve Blockaf12ac62012-01-06 19:20:56 +0000255 ALOGE("Hal init failed");
Tim Murray0c66f072012-10-23 14:05:02 -0700256 return false;
Jason Sams83c451a2011-04-21 11:46:50 -0700257 }
Tim Murray0c66f072012-10-23 14:05:02 -0700258
259 //validate HAL struct
260
261
262 return true;
263}
264
Jason Sams110f1812013-03-14 16:02:18 -0700265extern "C" bool rsdHalInit(RsContext c, uint32_t version_major, uint32_t version_minor);
266
Tim Murray0c66f072012-10-23 14:05:02 -0700267void * Context::threadProc(void *vrsc) {
268 Context *rsc = static_cast<Context *>(vrsc);
269#ifndef ANDROID_RS_SERIALIZE
270 rsc->mNativeThreadId = gettid();
Jason Sams93eacc72012-12-18 14:26:57 -0800271#ifndef RS_COMPATIBILITY_LIB
Tim Murray4d252d62012-11-29 14:37:59 -0800272 if (!rsc->isSynchronous()) {
273 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
274 }
Tim Murray0c66f072012-10-23 14:05:02 -0700275 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams93eacc72012-12-18 14:26:57 -0800276#else
277 if (!rsc->isSynchronous()) {
278 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, -4);
279 }
280 rsc->mThreadPriority = -4;
281#endif
Tim Murray0c66f072012-10-23 14:05:02 -0700282#endif //ANDROID_RS_SERIALIZE
283 rsc->props.mLogTimes = getProp("debug.rs.profile") != 0;
284 rsc->props.mLogScripts = getProp("debug.rs.script") != 0;
285 rsc->props.mLogObjects = getProp("debug.rs.object") != 0;
286 rsc->props.mLogShaders = getProp("debug.rs.shader") != 0;
287 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes") != 0;
288 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms") != 0;
289 rsc->props.mLogVisual = getProp("debug.rs.visual") != 0;
290 rsc->props.mDebugMaxThreads = getProp("debug.rs.max-threads");
291
292 bool loadDefault = true;
293
294 // Provide a mechanism for dropping in a different RS driver.
Jason Sams110f1812013-03-14 16:02:18 -0700295#ifndef RS_COMPATIBILITY_LIB
Tim Murray0c66f072012-10-23 14:05:02 -0700296#ifdef OVERRIDE_RS_DRIVER
297#define XSTR(S) #S
298#define STR(S) XSTR(S)
299#define OVERRIDE_RS_DRIVER_STRING STR(OVERRIDE_RS_DRIVER)
300
301 if (getProp("debug.rs.default-CPU-driver") != 0) {
302 ALOGE("Skipping override driver and loading default CPU driver");
Tim Murray0e92fa32012-11-06 14:36:38 -0800303 } else if (rsc->mForceCpu) {
304 ALOGV("Application requested CPU execution");
Tim Murray0c66f072012-10-23 14:05:02 -0700305 } else {
306 if (loadRuntime(OVERRIDE_RS_DRIVER_STRING, rsc)) {
307 ALOGE("Successfully loaded runtime: %s", OVERRIDE_RS_DRIVER_STRING);
308 loadDefault = false;
309 } else {
310 ALOGE("Failed to load runtime %s, loading default", OVERRIDE_RS_DRIVER_STRING);
311 }
312 }
313
314#undef XSTR
315#undef STR
316#endif // OVERRIDE_RS_DRIVER
317
318 if (loadDefault) {
319 if (!loadRuntime("libRSDriver.so", rsc)) {
320 ALOGE("Failed to load default runtime!");
Stephen Hines6f01bcf2012-11-19 15:18:16 -0800321 rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed loading RS driver");
Tim Murray0c66f072012-10-23 14:05:02 -0700322 return NULL;
323 }
324 }
Jason Sams110f1812013-03-14 16:02:18 -0700325#else // RS_COMPATIBILITY_LIB
326 if (rsdHalInit(rsc, 0, 0) != true) {
327 return NULL;
328 }
329#endif
330
Tim Murray0c66f072012-10-23 14:05:02 -0700331
Jason Sams83c451a2011-04-21 11:46:50 -0700332 rsc->mHal.funcs.setPriority(rsc, rsc->mThreadPriority);
Jason Samse5769102009-06-19 16:03:18 -0700333
Jason Sams93eacc72012-12-18 14:26:57 -0800334#ifndef RS_COMPATIBILITY_LIB
Jason Sams83c451a2011-04-21 11:46:50 -0700335 if (rsc->mIsGraphicsContext) {
Jason Samsd3e71072011-05-03 15:01:58 -0700336 if (!rsc->initGLThread()) {
337 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
338 return NULL;
339 }
340
Jason Sams83c451a2011-04-21 11:46:50 -0700341 rsc->mStateRaster.init(rsc);
342 rsc->setProgramRaster(NULL);
343 rsc->mStateVertex.init(rsc);
344 rsc->setProgramVertex(NULL);
345 rsc->mStateFragment.init(rsc);
346 rsc->setProgramFragment(NULL);
347 rsc->mStateFragmentStore.init(rsc);
348 rsc->setProgramStore(NULL);
349 rsc->mStateFont.init(rsc);
350 rsc->setFont(NULL);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700351 rsc->mStateSampler.init(rsc);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700352 rsc->mFBOCache.init(rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700353 }
Jason Sams93eacc72012-12-18 14:26:57 -0800354#endif
Jason Sams8ce125b2009-06-17 16:52:59 -0700355
Jason Sams83c451a2011-04-21 11:46:50 -0700356 rsc->mRunning = true;
Tim Murray4d252d62012-11-29 14:37:59 -0800357
358 if (rsc->isSynchronous()) {
359 return NULL;
360 }
361
Jason Sams5f27d6f2012-02-07 15:32:08 -0800362 if (!rsc->mIsGraphicsContext) {
363 while (!rsc->mExit) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800364 rsc->mIO.playCoreCommands(rsc, -1);
Jason Samse0aab4a2011-08-12 15:05:15 -0700365 }
Jason Sams93eacc72012-12-18 14:26:57 -0800366#ifndef RS_COMPATIBILITY_LIB
Jason Sams5f27d6f2012-02-07 15:32:08 -0800367 } else {
368#ifndef ANDROID_RS_SERIALIZE
369 DisplayEventReceiver displayEvent;
370 DisplayEventReceiver::Event eventBuffer[1];
371#endif
372 int vsyncRate = 0;
373 int targetRate = 0;
Jason Samse0aab4a2011-08-12 15:05:15 -0700374
Jason Sams5f27d6f2012-02-07 15:32:08 -0800375 bool drawOnce = false;
376 while (!rsc->mExit) {
377 rsc->timerSet(RS_TIMER_IDLE);
Jason Sams326e0dd2009-05-22 14:03:28 -0700378
Jason Sams5f27d6f2012-02-07 15:32:08 -0800379#ifndef ANDROID_RS_SERIALIZE
Jason Sams9afd9a52012-02-17 16:59:50 -0800380 if (!rsc->mRootScript.get() || !rsc->mHasSurface || rsc->mPaused) {
381 targetRate = 0;
382 }
383
Jason Sams5f27d6f2012-02-07 15:32:08 -0800384 if (vsyncRate != targetRate) {
385 displayEvent.setVsyncRate(targetRate);
386 vsyncRate = targetRate;
387 }
388 if (targetRate) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800389 drawOnce |= rsc->mIO.playCoreCommands(rsc, displayEvent.getFd());
Jason Sams5f27d6f2012-02-07 15:32:08 -0800390 while (displayEvent.getEvents(eventBuffer, 1) != 0) {
391 //ALOGE("vs2 time past %lld", (rsc->getTime() - eventBuffer[0].header.timestamp) / 1000000);
392 }
393 } else
394#endif
395 {
Jason Sams963a2fb2012-02-09 14:36:14 -0800396 drawOnce |= rsc->mIO.playCoreCommands(rsc, -1);
Jason Sams83c451a2011-04-21 11:46:50 -0700397 }
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700398
Jason Sams5f27d6f2012-02-07 15:32:08 -0800399 if ((rsc->mRootScript.get() != NULL) && rsc->mHasSurface &&
400 (targetRate || drawOnce) && !rsc->mPaused) {
401
402 drawOnce = false;
403 targetRate = ((rsc->runRootScript() + 15) / 16);
404
405 if (rsc->props.mLogVisual) {
406 rsc->displayDebugStats();
407 }
408
409 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
410 rsc->mHal.funcs.swap(rsc);
411 rsc->timerFrame();
412 rsc->timerSet(RS_TIMER_INTERNAL);
413 rsc->timerPrint();
414 rsc->timerReset();
415 }
Jason Sams83c451a2011-04-21 11:46:50 -0700416 }
Jason Sams93eacc72012-12-18 14:26:57 -0800417#endif
Jason Sams83c451a2011-04-21 11:46:50 -0700418 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700419
Steve Block65982012011-10-20 11:56:00 +0100420 ALOGV("%p RS Thread exiting", rsc);
Jason Samse514b452009-09-25 14:51:22 -0700421
Jason Sams93eacc72012-12-18 14:26:57 -0800422#ifndef RS_COMPATIBILITY_LIB
Jason Sams83c451a2011-04-21 11:46:50 -0700423 if (rsc->mIsGraphicsContext) {
424 pthread_mutex_lock(&gInitMutex);
425 rsc->deinitEGL();
426 pthread_mutex_unlock(&gInitMutex);
427 }
Jason Sams93eacc72012-12-18 14:26:57 -0800428#endif
Jason Sams33b6e3b2009-10-27 14:44:31 -0700429
Steve Block65982012011-10-20 11:56:00 +0100430 ALOGV("%p RS Thread exited", rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700431 return NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700432}
433
Jason Sams741aac92010-12-24 14:38:39 -0800434void Context::destroyWorkerThreadResources() {
Steve Block65982012011-10-20 11:56:00 +0100435 //ALOGV("destroyWorkerThreadResources 1");
Jason Sams2e8665d2011-01-27 00:14:13 -0800436 ObjectBase::zeroAllUserRef(this);
Jason Sams93eacc72012-12-18 14:26:57 -0800437#ifndef RS_COMPATIBILITY_LIB
Jason Sams741aac92010-12-24 14:38:39 -0800438 if (mIsGraphicsContext) {
439 mRaster.clear();
440 mFragment.clear();
441 mVertex.clear();
442 mFragmentStore.clear();
443 mFont.clear();
444 mRootScript.clear();
445 mStateRaster.deinit(this);
446 mStateVertex.deinit(this);
447 mStateFragment.deinit(this);
448 mStateFragmentStore.deinit(this);
449 mStateFont.deinit(this);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700450 mStateSampler.deinit(this);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700451 mFBOCache.deinit(this);
Jason Sams741aac92010-12-24 14:38:39 -0800452 }
Jason Sams93eacc72012-12-18 14:26:57 -0800453#endif
Jason Samsc7cec1e2011-08-18 18:01:33 -0700454 ObjectBase::freeAllChildren(this);
Jason Samscf912de2011-01-09 16:09:51 -0800455 mExit = true;
Jason Sams5f27d6f2012-02-07 15:32:08 -0800456 //ALOGV("destroyWorkerThreadResources 2");
Jason Sams741aac92010-12-24 14:38:39 -0800457}
458
Jason Sams2382aba2011-09-13 15:41:01 -0700459void Context::printWatchdogInfo(void *ctx) {
460 Context *rsc = (Context *)ctx;
Jason Samsee803442011-10-13 16:05:27 -0700461 if (rsc->watchdog.command && rsc->watchdog.file) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000462 ALOGE("RS watchdog timeout: %i %s line %i %s", rsc->watchdog.inRoot,
Jason Samsee803442011-10-13 16:05:27 -0700463 rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file);
464 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000465 ALOGE("RS watchdog timeout: %i", rsc->watchdog.inRoot);
Jason Samsee803442011-10-13 16:05:27 -0700466 }
Jason Sams2382aba2011-09-13 15:41:01 -0700467}
468
469
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800470void Context::setPriority(int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800471 // Note: If we put this in the proper "background" policy
472 // the wallpapers can become completly unresponsive at times.
473 // This is probably not what we want for something the user is actively
474 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800475 mThreadPriority = p;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700476 setpriority(PRIO_PROCESS, mNativeThreadId, p);
Jason Sams9719bd42012-01-12 14:22:21 -0800477 mHal.funcs.setPriority(this, mThreadPriority);
Jason Sams15832442009-11-15 12:14:26 -0800478}
479
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800480Context::Context() {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700481 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700482 mRunning = false;
483 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700484 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700485 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800486 mError = RS_ERROR_NONE;
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700487 mTargetSdkVersion = 14;
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700488 mDPI = 96;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700489 mIsContextLite = false;
Stephen Hines86c6b5f2011-10-31 14:07:54 -0700490 memset(&watchdog, 0, sizeof(watchdog));
Tim Murray0e92fa32012-11-06 14:36:38 -0800491 mForceCpu = false;
Tim Murray4d252d62012-11-29 14:37:59 -0800492 mSynchronous = false;
Tim Murray0e92fa32012-11-06 14:36:38 -0800493}
494
495Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc,
Tim Murray4d252d62012-11-29 14:37:59 -0800496 bool forceCpu, bool synchronous) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700497 Context * rsc = new Context();
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700498
Tim Murray0e92fa32012-11-06 14:36:38 -0800499 rsc->mForceCpu = forceCpu;
Tim Murray4d252d62012-11-29 14:37:59 -0800500 rsc->mSynchronous = synchronous;
Tim Murray0e92fa32012-11-06 14:36:38 -0800501
Jason Sams5c1c79a2010-11-03 14:27:11 -0700502 if (!rsc->initContext(dev, sc)) {
503 delete rsc;
504 return NULL;
505 }
506 return rsc;
507}
508
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700509Context * Context::createContextLite() {
510 Context * rsc = new Context();
511 rsc->mIsContextLite = true;
512 return rsc;
513}
514
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800515bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700516 pthread_mutex_lock(&gInitMutex);
517
Jason Sams1a4efa32011-05-17 15:01:29 -0700518 mIO.init();
Jason Sams5f27d6f2012-02-07 15:32:08 -0800519 mIO.setTimeoutCallback(printWatchdogInfo, this, 2e9);
Jason Sams1a4efa32011-05-17 15:01:29 -0700520
Jason Sams5c1c79a2010-11-03 14:27:11 -0700521 dev->addContext(this);
522 mDev = dev;
Jason Sams6b8552a2010-10-13 15:31:10 -0700523 if (sc) {
524 mUserSurfaceConfig = *sc;
525 } else {
526 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
527 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800528
Jason Sams6b8552a2010-10-13 15:31:10 -0700529 mIsGraphicsContext = sc != NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700530
Jason Samsa658e902009-06-04 14:35:01 -0700531 int status;
532 pthread_attr_t threadAttr;
533
Jason Samsfb03a222009-10-15 16:47:31 -0700534 pthread_mutex_unlock(&gInitMutex);
535
536 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700537
Jason Samsa658e902009-06-04 14:35:01 -0700538 status = pthread_attr_init(&threadAttr);
539 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000540 ALOGE("Failed to init thread attribute.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700541 return false;
Jason Samsa658e902009-06-04 14:35:01 -0700542 }
543
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700544 mHasSurface = false;
Jason Sams992a0b72009-06-23 12:22:47 -0700545
Jason Sams24371d92009-08-19 12:17:14 -0700546 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700547 timerSet(RS_TIMER_INTERNAL);
Tim Murray4d252d62012-11-29 14:37:59 -0800548 if (mSynchronous) {
549 threadProc(this);
550 } else {
551 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
552 if (status) {
553 ALOGE("Failed to start rs context thread.");
554 return false;
555 }
556 while (!mRunning && (mError == RS_ERROR_NONE)) {
557 usleep(100);
558 }
Jason Sams50869382009-08-18 17:07:09 -0700559
Tim Murray4d252d62012-11-29 14:37:59 -0800560 if (mError != RS_ERROR_NONE) {
561 ALOGE("Errors during thread init");
562 return false;
563 }
Jason Sams18133402010-07-20 15:09:00 -0700564
Tim Murray4d252d62012-11-29 14:37:59 -0800565 pthread_attr_destroy(&threadAttr);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700566 }
Jason Sams5c1c79a2010-11-03 14:27:11 -0700567 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700568}
569
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800570Context::~Context() {
Steve Block65982012011-10-20 11:56:00 +0100571 ALOGV("%p Context::~Context", this);
Jason Samscf912de2011-01-09 16:09:51 -0800572
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700573 if (!mIsContextLite) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700574 mPaused = false;
575 void *res;
Jason Sams326e0dd2009-05-22 14:03:28 -0700576
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700577 mIO.shutdown();
578 int status = pthread_join(mThreadId, &res);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800579 rsAssert(mExit);
Jason Sams326e0dd2009-05-22 14:03:28 -0700580
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700581 if (mHal.funcs.shutdownDriver) {
582 mHal.funcs.shutdownDriver(this);
583 }
584
585 // Global structure cleanup.
586 pthread_mutex_lock(&gInitMutex);
587 if (mDev) {
588 mDev->removeContext(this);
589 mDev = NULL;
590 }
591 pthread_mutex_unlock(&gInitMutex);
Jason Sams51462c52011-01-25 00:26:25 -0800592 }
Steve Block65982012011-10-20 11:56:00 +0100593 ALOGV("%p Context::~Context done", this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700594}
595
Jason Sams93eacc72012-12-18 14:26:57 -0800596#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700597void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800598 rsAssert(mIsGraphicsContext);
Jason Sams4b3de472011-04-06 17:52:23 -0700599 mHal.funcs.setSurface(this, w, h, sur);
Jason Sams458f2dc2009-11-03 13:58:36 -0800600
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700601 mHasSurface = sur != NULL;
Jason Sams4b3de472011-04-06 17:52:23 -0700602 mWidth = w;
603 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800604
Jason Sams4b3de472011-04-06 17:52:23 -0700605 if (mWidth && mHeight) {
Jason Sams771565f2010-05-14 15:30:29 -0700606 mStateVertex.updateSize(this);
Alex Sakhartchouka544b632011-07-19 17:50:29 -0700607 mFBOCache.updateSize();
Jason Sams458f2dc2009-11-03 13:58:36 -0800608 }
609}
610
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800611uint32_t Context::getCurrentSurfaceWidth() const {
612 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
613 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
614 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimX();
615 }
616 }
617 if (mFBOCache.mHal.state.depthTarget != NULL) {
618 return mFBOCache.mHal.state.depthTarget->getType()->getDimX();
619 }
620 return mWidth;
621}
622
623uint32_t Context::getCurrentSurfaceHeight() const {
624 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
625 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
626 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimY();
627 }
628 }
629 if (mFBOCache.mHal.state.depthTarget != NULL) {
630 return mFBOCache.mHal.state.depthTarget->getType()->getDimY();
631 }
632 return mHeight;
633}
634
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800635void Context::pause() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800636 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700637 mPaused = true;
638}
639
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800640void Context::resume() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800641 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700642 mPaused = false;
643}
644
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800645void Context::setRootScript(Script *s) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800646 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700647 mRootScript.set(s);
648}
649
Jason Sams60709252010-11-17 15:29:32 -0800650void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800651 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700652 if (pfs == NULL) {
653 mFragmentStore.set(mStateFragmentStore.mDefault);
654 } else {
655 mFragmentStore.set(pfs);
656 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700657}
658
Jason Sams60709252010-11-17 15:29:32 -0800659void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800660 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700661 if (pf == NULL) {
662 mFragment.set(mStateFragment.mDefault);
663 } else {
664 mFragment.set(pf);
665 }
Jason Samscfb1d112009-08-05 13:57:03 -0700666}
667
Jason Sams60709252010-11-17 15:29:32 -0800668void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800669 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700670 if (pr == NULL) {
671 mRaster.set(mStateRaster.mDefault);
672 } else {
673 mRaster.set(pr);
674 }
675}
676
Jason Sams60709252010-11-17 15:29:32 -0800677void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800678 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700679 if (pv == NULL) {
680 mVertex.set(mStateVertex.mDefault);
681 } else {
682 mVertex.set(pv);
683 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700684}
685
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800686void Context::setFont(Font *f) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700687 rsAssert(mIsGraphicsContext);
688 if (f == NULL) {
689 mFont.set(mStateFont.mDefault);
690 } else {
691 mFont.set(f);
692 }
693}
Jason Sams93eacc72012-12-18 14:26:57 -0800694#endif
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700695
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800696void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700697 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700698 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700699 mNames.add(obj);
700}
701
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800702void Context::removeName(ObjectBase *obj) {
703 for (size_t ct=0; ct < mNames.size(); ct++) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700704 if (obj == mNames[ct]) {
705 mNames.removeAt(ct);
706 return;
707 }
708 }
709}
710
Jason Sams1a4efa32011-05-17 15:01:29 -0700711RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) {
712 return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800713}
714
Jason Sams1a4efa32011-05-17 15:01:29 -0700715RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) {
716 return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen);
Jason Sams8c401ef2009-10-06 13:58:47 -0700717}
718
Jason Sams87319de2010-11-22 16:20:16 -0800719bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
720 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Sams1a4efa32011-05-17 15:01:29 -0700721
722 return mIO.sendToClient(cmdID, subID, data, len, waitForSpace);
Jason Sams8c401ef2009-10-06 13:58:47 -0700723}
724
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800725void Context::initToClient() {
726 while (!mRunning) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700727 usleep(100);
728 }
729}
730
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800731void Context::deinitToClient() {
Jason Sams1a4efa32011-05-17 15:01:29 -0700732 mIO.clientShutdown();
Jason Sams8c401ef2009-10-06 13:58:47 -0700733}
Jason Sams50869382009-08-18 17:07:09 -0700734
Jason Sams87319de2010-11-22 16:20:16 -0800735void Context::setError(RsError e, const char *msg) const {
Jason Samsa2cf7552010-03-03 13:03:18 -0800736 mError = e;
Jason Samsaad4bc52010-11-08 17:06:46 -0800737 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Samsa2cf7552010-03-03 13:03:18 -0800738}
739
740
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800741void Context::dumpDebug() const {
Steve Blockaf12ac62012-01-06 19:20:56 +0000742 ALOGE("RS Context debug %p", this);
743 ALOGE("RS Context debug");
Jason Sams13e26342009-11-24 12:26:35 -0800744
Steve Blockaf12ac62012-01-06 19:20:56 +0000745 ALOGE(" RS width %i, height %i", mWidth, mHeight);
746 ALOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
747 ALOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId);
Jason Sams13e26342009-11-24 12:26:35 -0800748}
Jason Samsa4a54e42009-06-10 18:39:40 -0700749
Jason Sams326e0dd2009-05-22 14:03:28 -0700750///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700751//
Jason Sams326e0dd2009-05-22 14:03:28 -0700752
753namespace android {
754namespace renderscript {
755
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800756void rsi_ContextFinish(Context *rsc) {
Jason Sams8c880902010-06-15 12:15:57 -0700757}
Jason Sams326e0dd2009-05-22 14:03:28 -0700758
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800759void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Sams93eacc72012-12-18 14:26:57 -0800760#ifndef RS_COMPATIBILITY_LIB
Jason Sams326e0dd2009-05-22 14:03:28 -0700761 Script *s = static_cast<Script *>(vs);
762 rsc->setRootScript(s);
Jason Sams93eacc72012-12-18 14:26:57 -0800763#endif
Jason Sams326e0dd2009-05-22 14:03:28 -0700764}
765
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800766void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700767 Sampler *s = static_cast<Sampler *>(vs);
768
769 if (slot > RS_MAX_SAMPLER_SLOT) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000770 ALOGE("Invalid sampler slot");
Jason Sams326e0dd2009-05-22 14:03:28 -0700771 return;
772 }
773
774 s->bindToContext(&rsc->mStateSampler, slot);
775}
776
Jason Sams93eacc72012-12-18 14:26:57 -0800777#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800778void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Samsccc010b2010-05-13 18:30:11 -0700779 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams60709252010-11-17 15:29:32 -0800780 rsc->setProgramStore(pfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700781}
782
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800783void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700784 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Sams60709252010-11-17 15:29:32 -0800785 rsc->setProgramFragment(pf);
Jason Sams326e0dd2009-05-22 14:03:28 -0700786}
787
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800788void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Sams5fd09d82009-09-23 13:57:02 -0700789 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Sams60709252010-11-17 15:29:32 -0800790 rsc->setProgramRaster(pr);
Jason Sams5fd09d82009-09-23 13:57:02 -0700791}
792
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800793void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700794 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Sams60709252010-11-17 15:29:32 -0800795 rsc->setProgramVertex(pv);
Jason Sams326e0dd2009-05-22 14:03:28 -0700796}
797
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800798void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700799 Font *font = static_cast<Font *>(vfont);
800 rsc->setFont(font);
801}
Jason Sams93eacc72012-12-18 14:26:57 -0800802#endif
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700803
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700804void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700805 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700806 rsc->assignName(ob, name, name_length);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700807}
Jason Sams326e0dd2009-05-22 14:03:28 -0700808
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800809void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams2353ae32010-10-14 17:48:46 -0700810 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -0700811 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700812 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700813}
814
Jason Sams93eacc72012-12-18 14:26:57 -0800815#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800816void rsi_ContextPause(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700817 rsc->pause();
818}
819
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800820void rsi_ContextResume(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700821 rsc->resume();
822}
823
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700824void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
Mathias Agopianfa402862010-02-12 14:04:35 -0800825 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800826}
Jason Sams93eacc72012-12-18 14:26:57 -0800827#endif
Jason Sams613cad12009-11-12 15:10:25 -0800828
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800829void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800830 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800831}
832
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800833void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Samsc21cf402009-11-17 17:26:46 -0800834 ObjectBase::dumpAll(rsc);
835}
836
Jason Sams741aac92010-12-24 14:38:39 -0800837void rsi_ContextDestroyWorker(Context *rsc) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700838 rsc->destroyWorkerThreadResources();
Jason Sams741aac92010-12-24 14:38:39 -0800839}
840
Jason Samsc975cf42011-04-28 18:26:48 -0700841void rsi_ContextDestroy(Context *rsc) {
Steve Block65982012011-10-20 11:56:00 +0100842 ALOGV("%p rsContextDestroy", rsc);
Jason Sams741aac92010-12-24 14:38:39 -0800843 rsContextDestroyWorker(rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800844 delete rsc;
Steve Block65982012011-10-20 11:56:00 +0100845 ALOGV("%p rsContextDestroy done", rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800846}
847
Jason Sams326e0dd2009-05-22 14:03:28 -0700848
Jason Samsc975cf42011-04-28 18:26:48 -0700849RsMessageToClientType rsi_ContextPeekMessage(Context *rsc,
Jason Sams186e5912011-04-26 14:50:00 -0700850 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700851 uint32_t * subID, size_t subID_length) {
852 return rsc->peekMessageToClient(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800853}
854
Jason Samsc975cf42011-04-28 18:26:48 -0700855RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length,
Jason Sams186e5912011-04-26 14:50:00 -0700856 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700857 uint32_t * subID, size_t subID_length) {
Jason Sams186e5912011-04-26 14:50:00 -0700858 rsAssert(subID_length == sizeof(uint32_t));
859 rsAssert(receiveLen_length == sizeof(size_t));
Jason Sams1a4efa32011-05-17 15:01:29 -0700860 return rsc->getMessageToClient(data, receiveLen, subID, data_length);
Jason Sams8c401ef2009-10-06 13:58:47 -0700861}
862
Jason Samsc975cf42011-04-28 18:26:48 -0700863void rsi_ContextInitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700864 rsc->initToClient();
865}
866
Jason Samsc975cf42011-04-28 18:26:48 -0700867void rsi_ContextDeinitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700868 rsc->deinitToClient();
869}
870
Jason Sams70265202013-02-05 19:20:47 -0800871void rsi_ContextSendMessage(Context *rsc, uint32_t id, const uint8_t *data, size_t len) {
872 rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, id, len, true);
873}
874
Jason Samsc975cf42011-04-28 18:26:48 -0700875}
876}
877
Jason Sams14982c82013-02-22 18:17:05 -0800878RsContext rsContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion,
879 RsContextType ct, bool forceCpu, bool synchronous) {
Steve Block65982012011-10-20 11:56:00 +0100880 ALOGV("rsContextCreate dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700881 Device * dev = static_cast<Device *>(vdev);
Tim Murray4d252d62012-11-29 14:37:59 -0800882 Context *rsc = Context::createContext(dev, NULL, forceCpu, synchronous);
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700883 if (rsc) {
884 rsc->setTargetSdkVersion(sdkVersion);
885 }
Jason Sams789ca832011-05-18 17:36:02 -0700886 return rsc;
887}
888
Jason Sams93eacc72012-12-18 14:26:57 -0800889#ifndef RS_COMPATIBILITY_LIB
Jason Sams789ca832011-05-18 17:36:02 -0700890RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700891 uint32_t sdkVersion, RsSurfaceConfig sc,
892 uint32_t dpi) {
Steve Block65982012011-10-20 11:56:00 +0100893 ALOGV("rsContextCreateGL dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700894 Device * dev = static_cast<Device *>(vdev);
895 Context *rsc = Context::createContext(dev, &sc);
Jason Sams9544f762011-07-13 16:09:42 -0700896 if (rsc) {
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700897 rsc->setTargetSdkVersion(sdkVersion);
Jason Sams9544f762011-07-13 16:09:42 -0700898 rsc->setDPI(dpi);
899 }
Steve Block65982012011-10-20 11:56:00 +0100900 ALOGV("%p rsContextCreateGL ret", rsc);
Jason Sams789ca832011-05-18 17:36:02 -0700901 return rsc;
902}
Jason Sams93eacc72012-12-18 14:26:57 -0800903#endif
Jason Sams789ca832011-05-18 17:36:02 -0700904
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700905// Only to be called at a3d load time, before object is visible to user
906// not thread safe
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800907void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700908 ObjectBase *ob = static_cast<ObjectBase *>(obj);
909 (*name) = ob->getName();
910}