blob: 916c6a0ada53e90434bbfba8972a0344d8a65289 [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
Tim Murray397b0b12014-01-07 15:35:08 -080022#include "rsgApiStructs.h"
23
Jason Sams93eacc72012-12-18 14:26:57 -080024#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -080025#include "rsMesh.h"
Mathias Agopian5ae678f2009-06-22 18:01:09 -070026#include <ui/FramebufferNativeWindow.h>
Jason Sams5f27d6f2012-02-07 15:32:08 -080027#include <gui/DisplayEventReceiver.h>
Jason Sams93eacc72012-12-18 14:26:57 -080028#endif
Jason Sams326e0dd2009-05-22 14:03:28 -070029
Jason Sams15832442009-11-15 12:14:26 -080030#include <sys/types.h>
31#include <sys/resource.h>
Jason Sams7bf29dd2010-07-19 15:38:19 -070032#include <sched.h>
Jason Sams15832442009-11-15 12:14:26 -080033
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>
Ian Rogersf8852d02014-01-29 15:35:17 -080037#include <inttypes.h>
Stephen Hinesb0934b62013-07-03 17:27:38 -070038#include <unistd.h>
Stephen Hines414a4612012-09-05 18:05:08 -070039
Stephen Hines16647802013-08-15 16:28:01 -070040#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB) && \
41 defined(HAVE_ANDROID_OS)
Tim Murray0b575de2013-03-15 15:56:43 -070042#include <cutils/properties.h>
43#endif
44
Stephen Hines6dfe6842013-08-14 17:56:38 -070045#ifdef RS_COMPATIBILITY_LIB
46#include "rsCompatibilityLib.h"
47#endif
48
Tim Murray0b575de2013-03-15 15:56:43 -070049#ifdef RS_SERVER
50// Android exposes gettid(), standard Linux does not
51static pid_t gettid() {
52 return syscall(SYS_gettid);
53}
54#endif
55
Jason Sams326e0dd2009-05-22 14:03:28 -070056using namespace android;
57using namespace android::renderscript;
58
Jason Samsfb03a222009-10-15 16:47:31 -070059pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams4961cce2013-04-11 16:11:46 -070060pthread_mutex_t Context::gMessageMutex = PTHREAD_MUTEX_INITIALIZER;
Stephen Hinesca3f09c2011-01-07 15:11:30 -080061pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070062
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080063bool Context::initGLThread() {
Jason Sams6b8552a2010-10-13 15:31:10 -070064 pthread_mutex_lock(&gInitMutex);
Jason Sams6b8552a2010-10-13 15:31:10 -070065
Jason Sams4b3de472011-04-06 17:52:23 -070066 if (!mHal.funcs.initGraphics(this)) {
Jason Sams5c1c79a2010-11-03 14:27:11 -070067 pthread_mutex_unlock(&gInitMutex);
Steve Blockaf12ac62012-01-06 19:20:56 +000068 ALOGE("%p initGraphics failed", this);
Jason Sams5c1c79a2010-11-03 14:27:11 -070069 return false;
Jason Sams6b8552a2010-10-13 15:31:10 -070070 }
71
Jason Sams6b8552a2010-10-13 15:31:10 -070072 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -070073 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -070074}
75
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080076void Context::deinitEGL() {
Jason Sams93eacc72012-12-18 14:26:57 -080077#ifndef RS_COMPATIBILITY_LIB
Jason Sams4b3de472011-04-06 17:52:23 -070078 mHal.funcs.shutdownGraphics(this);
Jason Sams93eacc72012-12-18 14:26:57 -080079#endif
Jason Sams33b6e3b2009-10-27 14:44:31 -070080}
81
Jason Sams60709252010-11-17 15:29:32 -080082Context::PushState::PushState(Context *con) {
83 mRsc = con;
Jason Sams93eacc72012-12-18 14:26:57 -080084#ifndef RS_COMPATIBILITY_LIB
Jason Samsc946b612011-02-23 14:47:17 -080085 if (con->mIsGraphicsContext) {
86 mFragment.set(con->getProgramFragment());
87 mVertex.set(con->getProgramVertex());
88 mStore.set(con->getProgramStore());
89 mRaster.set(con->getProgramRaster());
90 mFont.set(con->getFont());
91 }
Jason Sams93eacc72012-12-18 14:26:57 -080092#endif
Jason Sams60709252010-11-17 15:29:32 -080093}
94
95Context::PushState::~PushState() {
Jason Sams93eacc72012-12-18 14:26:57 -080096#ifndef RS_COMPATIBILITY_LIB
Jason Samsc946b612011-02-23 14:47:17 -080097 if (mRsc->mIsGraphicsContext) {
98 mRsc->setProgramFragment(mFragment.get());
99 mRsc->setProgramVertex(mVertex.get());
100 mRsc->setProgramStore(mStore.get());
101 mRsc->setProgramRaster(mRaster.get());
102 mRsc->setFont(mFont.get());
103 }
Jason Sams93eacc72012-12-18 14:26:57 -0800104#endif
Jason Sams60709252010-11-17 15:29:32 -0800105}
106
Jason Sams33b6e3b2009-10-27 14:44:31 -0700107
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800108uint32_t Context::runScript(Script *s) {
Shih-wei Liaoda3b58d2012-08-03 04:24:33 -0700109 PushState ps(this);
Jason Sams10308932009-06-09 12:15:30 -0700110
Jason Samsc61346b2010-05-28 18:23:22 -0700111 uint32_t ret = s->run(this);
Jason Samsc9d43db2009-07-28 12:02:16 -0700112 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700113}
114
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800115uint32_t Context::runRootScript() {
Jason Sams2dca84d2009-12-09 11:05:45 -0800116 timerSet(RS_TIMER_SCRIPT);
Jason Sams93eacc72012-12-18 14:26:57 -0800117#ifndef RS_COMPATIBILITY_LIB
Jason Sams8c401ef2009-10-06 13:58:47 -0700118 mStateFragmentStore.mLast.clear();
Jason Sams93eacc72012-12-18 14:26:57 -0800119#endif
Jason Sams2382aba2011-09-13 15:41:01 -0700120 watchdog.inRoot = true;
Jason Samsc61346b2010-05-28 18:23:22 -0700121 uint32_t ret = runScript(mRootScript.get());
Jason Sams2382aba2011-09-13 15:41:01 -0700122 watchdog.inRoot = false;
Jason Sams8cfdd242009-10-14 15:43:53 -0700123
Jason Samscfb1d112009-08-05 13:57:03 -0700124 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700125}
126
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800127uint64_t Context::getTime() const {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700128#ifndef ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -0700129 struct timespec t;
130 clock_gettime(CLOCK_MONOTONIC, &t);
131 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700132#else
133 return 0;
134#endif //ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -0700135}
136
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800137void Context::timerReset() {
Jason Sams24371d92009-08-19 12:17:14 -0700138 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
139 mTimers[ct] = 0;
140 }
141}
142
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800143void Context::timerInit() {
Jason Sams24371d92009-08-19 12:17:14 -0700144 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700145 mTimeFrame = mTimeLast;
146 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700147 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700148 mAverageFPSFrameCount = 0;
149 mAverageFPSStartTime = mTimeLast;
150 mAverageFPS = 0;
Jason Sams24371d92009-08-19 12:17:14 -0700151 timerReset();
152}
153
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800154void Context::timerFrame() {
Jason Sams1d54f102009-09-03 15:43:13 -0700155 mTimeLastFrame = mTimeFrame;
156 mTimeFrame = getTime();
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700157 // Update average fps
158 const uint64_t averageFramerateInterval = 1000 * 1000000;
159 mAverageFPSFrameCount ++;
160 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800161 if (inverval >= averageFramerateInterval) {
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700162 inverval = inverval / 1000000;
163 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
164 mAverageFPSFrameCount = 0;
165 mAverageFPSStartTime = mTimeFrame;
166 }
Jason Sams1d54f102009-09-03 15:43:13 -0700167}
168
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800169void Context::timerSet(Timers tm) {
Jason Sams24371d92009-08-19 12:17:14 -0700170 uint64_t last = mTimeLast;
171 mTimeLast = getTime();
172 mTimers[mTimerActive] += mTimeLast - last;
173 mTimerActive = tm;
174}
175
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800176void Context::timerPrint() {
Jason Sams24371d92009-08-19 12:17:14 -0700177 double total = 0;
178 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
179 total += mTimers[ct];
180 }
Jason Sams1d54f102009-09-03 15:43:13 -0700181 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800182 mTimeMSLastFrame = frame / 1000000;
183 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
184 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700185
Jason Sams2dca84d2009-12-09 11:05:45 -0800186
187 if (props.mLogTimes) {
Ian Rogersf8852d02014-01-29 15:35:17 -0800188 ALOGV("RS: Frame (%i), Script %2.1f%% (%i), Swap %2.1f%% (%i), Idle %2.1f%% (%" PRIi64 "), "
189 "Internal %2.1f%% (%" PRIi64 "), Avg fps: %u",
Jason Sams2dca84d2009-12-09 11:05:45 -0800190 mTimeMSLastFrame,
191 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
192 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
193 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700194 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
195 mAverageFPS);
Jason Sams2dca84d2009-12-09 11:05:45 -0800196 }
Jason Sams24371d92009-08-19 12:17:14 -0700197}
198
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800199bool Context::setupCheck() {
Jason Sams93eacc72012-12-18 14:26:57 -0800200#ifndef RS_COMPATIBILITY_LIB
Jason Sams721acc42011-04-06 11:23:54 -0700201 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700202 mFragment->setup(this, &mStateFragment);
Jason Sams721acc42011-04-06 11:23:54 -0700203 mRaster->setup(this, &mStateRaster);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700204 mVertex->setup(this, &mStateVertex);
205 mFBOCache.setup(this);
Jason Sams93eacc72012-12-18 14:26:57 -0800206#endif
Jason Samsa2cf7552010-03-03 13:03:18 -0800207 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700208}
209
Jason Sams93eacc72012-12-18 14:26:57 -0800210#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700211void Context::setupProgramStore() {
Jason Sams721acc42011-04-06 11:23:54 -0700212 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700213}
Jason Sams93eacc72012-12-18 14:26:57 -0800214#endif
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700215
Jason Samsd1f7da62012-03-15 19:18:03 -0700216static uint32_t getProp(const char *str) {
Nick Kralevich7e85ca22013-05-22 15:04:39 -0700217#if !defined(RS_SERVER) && defined(HAVE_ANDROID_OS)
Joe Onorato76371ff2009-09-23 16:37:36 -0700218 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700219 property_get(str, buf, "0");
Jason Samsd1f7da62012-03-15 19:18:03 -0700220 return atoi(buf);
Tim Murray0b575de2013-03-15 15:56:43 -0700221#else
222 return 0;
223#endif
Joe Onorato76371ff2009-09-23 16:37:36 -0700224}
Jason Sams326e0dd2009-05-22 14:03:28 -0700225
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800226void Context::displayDebugStats() {
Jason Sams93eacc72012-12-18 14:26:57 -0800227#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700228 char buffer[128];
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700229 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700230 float oldR, oldG, oldB, oldA;
231 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700232 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700233
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700234 ObjectBaseRef<Font> lastFont(getFont());
235 setFont(NULL);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700236 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700237 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700238 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700239
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700240 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700241 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700242
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700243 setFont(lastFont.get());
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700244 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Jason Sams93eacc72012-12-18 14:26:57 -0800245#endif
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700246}
247
Tim Murray0c66f072012-10-23 14:05:02 -0700248bool Context::loadRuntime(const char* filename, Context* rsc) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700249
Tim Murray0c66f072012-10-23 14:05:02 -0700250 // TODO: store the driverSO somewhere so we can dlclose later
Stephen Hines91dfcdb2012-09-07 18:23:35 -0700251 void *driverSO = NULL;
252
Tim Murray0c66f072012-10-23 14:05:02 -0700253 driverSO = dlopen(filename, RTLD_LAZY);
Stephen Hines91dfcdb2012-09-07 18:23:35 -0700254 if (driverSO == NULL) {
Tim Murray0c66f072012-10-23 14:05:02 -0700255 ALOGE("Failed loading RS driver: %s", dlerror());
256 return false;
Stephen Hines414a4612012-09-05 18:05:08 -0700257 }
258
259 // Need to call dlerror() to clear buffer before using it for dlsym().
260 (void) dlerror();
261 typedef bool (*HalSig)(Context*, uint32_t, uint32_t);
262 HalSig halInit = (HalSig) dlsym(driverSO, "rsdHalInit");
263
264 // If we can't find the C variant, we go looking for the C++ version.
265 if (halInit == NULL) {
266 ALOGW("Falling back to find C++ rsdHalInit: %s", dlerror());
267 halInit = (HalSig) dlsym(driverSO,
268 "_Z10rsdHalInitPN7android12renderscript7ContextEjj");
269 }
270
271 if (halInit == NULL) {
Stephen Hines414a4612012-09-05 18:05:08 -0700272 dlclose(driverSO);
273 ALOGE("Failed to find rsdHalInit: %s", dlerror());
Tim Murray0c66f072012-10-23 14:05:02 -0700274 return false;
Stephen Hines414a4612012-09-05 18:05:08 -0700275 }
276
277 if (!(*halInit)(rsc, 0, 0)) {
Stephen Hines414a4612012-09-05 18:05:08 -0700278 dlclose(driverSO);
Steve Blockaf12ac62012-01-06 19:20:56 +0000279 ALOGE("Hal init failed");
Tim Murray0c66f072012-10-23 14:05:02 -0700280 return false;
Jason Sams83c451a2011-04-21 11:46:50 -0700281 }
Tim Murray0c66f072012-10-23 14:05:02 -0700282
283 //validate HAL struct
284
285
286 return true;
287}
288
Jason Sams110f1812013-03-14 16:02:18 -0700289extern "C" bool rsdHalInit(RsContext c, uint32_t version_major, uint32_t version_minor);
290
Tim Murray0c66f072012-10-23 14:05:02 -0700291void * Context::threadProc(void *vrsc) {
292 Context *rsc = static_cast<Context *>(vrsc);
293#ifndef ANDROID_RS_SERIALIZE
294 rsc->mNativeThreadId = gettid();
Jason Sams93eacc72012-12-18 14:26:57 -0800295#ifndef RS_COMPATIBILITY_LIB
Tim Murray4d252d62012-11-29 14:37:59 -0800296 if (!rsc->isSynchronous()) {
297 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
298 }
Tim Murray0c66f072012-10-23 14:05:02 -0700299 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams93eacc72012-12-18 14:26:57 -0800300#else
301 if (!rsc->isSynchronous()) {
302 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, -4);
303 }
304 rsc->mThreadPriority = -4;
305#endif
Tim Murray0c66f072012-10-23 14:05:02 -0700306#endif //ANDROID_RS_SERIALIZE
307 rsc->props.mLogTimes = getProp("debug.rs.profile") != 0;
308 rsc->props.mLogScripts = getProp("debug.rs.script") != 0;
309 rsc->props.mLogObjects = getProp("debug.rs.object") != 0;
310 rsc->props.mLogShaders = getProp("debug.rs.shader") != 0;
311 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes") != 0;
312 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms") != 0;
313 rsc->props.mLogVisual = getProp("debug.rs.visual") != 0;
314 rsc->props.mDebugMaxThreads = getProp("debug.rs.max-threads");
315
316 bool loadDefault = true;
317
318 // Provide a mechanism for dropping in a different RS driver.
Jason Sams110f1812013-03-14 16:02:18 -0700319#ifndef RS_COMPATIBILITY_LIB
Tim Murray0c66f072012-10-23 14:05:02 -0700320#ifdef OVERRIDE_RS_DRIVER
321#define XSTR(S) #S
322#define STR(S) XSTR(S)
323#define OVERRIDE_RS_DRIVER_STRING STR(OVERRIDE_RS_DRIVER)
324
325 if (getProp("debug.rs.default-CPU-driver") != 0) {
Stephen Hines43463922013-11-14 13:57:50 -0800326 ALOGD("Skipping override driver and loading default CPU driver");
Jason Samscd5f1ce2014-03-31 14:44:53 -0700327 } else if (rsc->mForceCpu || rsc->mIsGraphicsContext) {
Tim Murray0e92fa32012-11-06 14:36:38 -0800328 ALOGV("Application requested CPU execution");
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700329 } else if (rsc->getContextType() == RS_CONTEXT_TYPE_DEBUG) {
330 ALOGV("Application requested debug context");
Tim Murray0c66f072012-10-23 14:05:02 -0700331 } else {
332 if (loadRuntime(OVERRIDE_RS_DRIVER_STRING, rsc)) {
Stephen Hines43463922013-11-14 13:57:50 -0800333 ALOGV("Successfully loaded runtime: %s", OVERRIDE_RS_DRIVER_STRING);
Tim Murray0c66f072012-10-23 14:05:02 -0700334 loadDefault = false;
335 } else {
336 ALOGE("Failed to load runtime %s, loading default", OVERRIDE_RS_DRIVER_STRING);
337 }
338 }
339
340#undef XSTR
341#undef STR
342#endif // OVERRIDE_RS_DRIVER
343
344 if (loadDefault) {
345 if (!loadRuntime("libRSDriver.so", rsc)) {
346 ALOGE("Failed to load default runtime!");
Stephen Hines6f01bcf2012-11-19 15:18:16 -0800347 rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed loading RS driver");
Tim Murray0c66f072012-10-23 14:05:02 -0700348 return NULL;
349 }
350 }
Jason Sams110f1812013-03-14 16:02:18 -0700351#else // RS_COMPATIBILITY_LIB
352 if (rsdHalInit(rsc, 0, 0) != true) {
353 return NULL;
354 }
355#endif
356
Tim Murray0c66f072012-10-23 14:05:02 -0700357
Jason Sams83c451a2011-04-21 11:46:50 -0700358 rsc->mHal.funcs.setPriority(rsc, rsc->mThreadPriority);
Jason Samse5769102009-06-19 16:03:18 -0700359
Jason Sams93eacc72012-12-18 14:26:57 -0800360#ifndef RS_COMPATIBILITY_LIB
Jason Sams83c451a2011-04-21 11:46:50 -0700361 if (rsc->mIsGraphicsContext) {
Jason Samsd3e71072011-05-03 15:01:58 -0700362 if (!rsc->initGLThread()) {
363 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
364 return NULL;
365 }
366
Jason Sams83c451a2011-04-21 11:46:50 -0700367 rsc->mStateRaster.init(rsc);
368 rsc->setProgramRaster(NULL);
369 rsc->mStateVertex.init(rsc);
370 rsc->setProgramVertex(NULL);
371 rsc->mStateFragment.init(rsc);
372 rsc->setProgramFragment(NULL);
373 rsc->mStateFragmentStore.init(rsc);
374 rsc->setProgramStore(NULL);
375 rsc->mStateFont.init(rsc);
376 rsc->setFont(NULL);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700377 rsc->mStateSampler.init(rsc);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700378 rsc->mFBOCache.init(rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700379 }
Jason Sams93eacc72012-12-18 14:26:57 -0800380#endif
Jason Sams8ce125b2009-06-17 16:52:59 -0700381
Jason Sams83c451a2011-04-21 11:46:50 -0700382 rsc->mRunning = true;
Tim Murray4d252d62012-11-29 14:37:59 -0800383
384 if (rsc->isSynchronous()) {
385 return NULL;
386 }
387
Jason Sams5f27d6f2012-02-07 15:32:08 -0800388 if (!rsc->mIsGraphicsContext) {
389 while (!rsc->mExit) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800390 rsc->mIO.playCoreCommands(rsc, -1);
Jason Samse0aab4a2011-08-12 15:05:15 -0700391 }
Jason Sams93eacc72012-12-18 14:26:57 -0800392#ifndef RS_COMPATIBILITY_LIB
Jason Sams5f27d6f2012-02-07 15:32:08 -0800393 } else {
394#ifndef ANDROID_RS_SERIALIZE
395 DisplayEventReceiver displayEvent;
396 DisplayEventReceiver::Event eventBuffer[1];
397#endif
398 int vsyncRate = 0;
399 int targetRate = 0;
Jason Samse0aab4a2011-08-12 15:05:15 -0700400
Jason Sams5f27d6f2012-02-07 15:32:08 -0800401 bool drawOnce = false;
402 while (!rsc->mExit) {
403 rsc->timerSet(RS_TIMER_IDLE);
Jason Sams326e0dd2009-05-22 14:03:28 -0700404
Jason Sams5f27d6f2012-02-07 15:32:08 -0800405#ifndef ANDROID_RS_SERIALIZE
Jason Sams9afd9a52012-02-17 16:59:50 -0800406 if (!rsc->mRootScript.get() || !rsc->mHasSurface || rsc->mPaused) {
407 targetRate = 0;
408 }
409
Jason Sams5f27d6f2012-02-07 15:32:08 -0800410 if (vsyncRate != targetRate) {
411 displayEvent.setVsyncRate(targetRate);
412 vsyncRate = targetRate;
413 }
414 if (targetRate) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800415 drawOnce |= rsc->mIO.playCoreCommands(rsc, displayEvent.getFd());
Jason Sams5f27d6f2012-02-07 15:32:08 -0800416 while (displayEvent.getEvents(eventBuffer, 1) != 0) {
417 //ALOGE("vs2 time past %lld", (rsc->getTime() - eventBuffer[0].header.timestamp) / 1000000);
418 }
419 } else
420#endif
421 {
Jason Sams963a2fb2012-02-09 14:36:14 -0800422 drawOnce |= rsc->mIO.playCoreCommands(rsc, -1);
Jason Sams83c451a2011-04-21 11:46:50 -0700423 }
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700424
Jason Sams5f27d6f2012-02-07 15:32:08 -0800425 if ((rsc->mRootScript.get() != NULL) && rsc->mHasSurface &&
426 (targetRate || drawOnce) && !rsc->mPaused) {
427
428 drawOnce = false;
429 targetRate = ((rsc->runRootScript() + 15) / 16);
430
431 if (rsc->props.mLogVisual) {
432 rsc->displayDebugStats();
433 }
434
435 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
436 rsc->mHal.funcs.swap(rsc);
437 rsc->timerFrame();
438 rsc->timerSet(RS_TIMER_INTERNAL);
439 rsc->timerPrint();
440 rsc->timerReset();
441 }
Jason Sams83c451a2011-04-21 11:46:50 -0700442 }
Jason Sams93eacc72012-12-18 14:26:57 -0800443#endif
Jason Sams83c451a2011-04-21 11:46:50 -0700444 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700445
Tim Murray20f62ca2013-05-09 11:57:35 -0700446 //ALOGV("%p RS Thread exiting", rsc);
Jason Samse514b452009-09-25 14:51:22 -0700447
Jason Sams93eacc72012-12-18 14:26:57 -0800448#ifndef RS_COMPATIBILITY_LIB
Jason Sams83c451a2011-04-21 11:46:50 -0700449 if (rsc->mIsGraphicsContext) {
450 pthread_mutex_lock(&gInitMutex);
451 rsc->deinitEGL();
452 pthread_mutex_unlock(&gInitMutex);
453 }
Jason Sams93eacc72012-12-18 14:26:57 -0800454#endif
Jason Sams33b6e3b2009-10-27 14:44:31 -0700455
Tim Murray20f62ca2013-05-09 11:57:35 -0700456 //ALOGV("%p RS Thread exited", rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700457 return NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700458}
459
Jason Sams741aac92010-12-24 14:38:39 -0800460void Context::destroyWorkerThreadResources() {
Steve Block65982012011-10-20 11:56:00 +0100461 //ALOGV("destroyWorkerThreadResources 1");
Jason Sams2e8665d2011-01-27 00:14:13 -0800462 ObjectBase::zeroAllUserRef(this);
Jason Sams93eacc72012-12-18 14:26:57 -0800463#ifndef RS_COMPATIBILITY_LIB
Jason Sams741aac92010-12-24 14:38:39 -0800464 if (mIsGraphicsContext) {
465 mRaster.clear();
466 mFragment.clear();
467 mVertex.clear();
468 mFragmentStore.clear();
469 mFont.clear();
470 mRootScript.clear();
471 mStateRaster.deinit(this);
472 mStateVertex.deinit(this);
473 mStateFragment.deinit(this);
474 mStateFragmentStore.deinit(this);
475 mStateFont.deinit(this);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700476 mStateSampler.deinit(this);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700477 mFBOCache.deinit(this);
Jason Sams741aac92010-12-24 14:38:39 -0800478 }
Jason Sams93eacc72012-12-18 14:26:57 -0800479#endif
Jason Samsc7cec1e2011-08-18 18:01:33 -0700480 ObjectBase::freeAllChildren(this);
Jason Samscf912de2011-01-09 16:09:51 -0800481 mExit = true;
Jason Sams5f27d6f2012-02-07 15:32:08 -0800482 //ALOGV("destroyWorkerThreadResources 2");
Jason Sams741aac92010-12-24 14:38:39 -0800483}
484
Jason Sams2382aba2011-09-13 15:41:01 -0700485void Context::printWatchdogInfo(void *ctx) {
486 Context *rsc = (Context *)ctx;
Jason Samsee803442011-10-13 16:05:27 -0700487 if (rsc->watchdog.command && rsc->watchdog.file) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000488 ALOGE("RS watchdog timeout: %i %s line %i %s", rsc->watchdog.inRoot,
Jason Samsee803442011-10-13 16:05:27 -0700489 rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file);
490 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000491 ALOGE("RS watchdog timeout: %i", rsc->watchdog.inRoot);
Jason Samsee803442011-10-13 16:05:27 -0700492 }
Jason Sams2382aba2011-09-13 15:41:01 -0700493}
494
495
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800496void Context::setPriority(int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800497 // Note: If we put this in the proper "background" policy
498 // the wallpapers can become completly unresponsive at times.
499 // This is probably not what we want for something the user is actively
500 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800501 mThreadPriority = p;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700502 setpriority(PRIO_PROCESS, mNativeThreadId, p);
Jason Sams9719bd42012-01-12 14:22:21 -0800503 mHal.funcs.setPriority(this, mThreadPriority);
Jason Sams15832442009-11-15 12:14:26 -0800504}
505
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800506Context::Context() {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700507 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700508 mRunning = false;
509 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700510 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700511 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800512 mError = RS_ERROR_NONE;
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700513 mTargetSdkVersion = 14;
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700514 mDPI = 96;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700515 mIsContextLite = false;
Stephen Hines86c6b5f2011-10-31 14:07:54 -0700516 memset(&watchdog, 0, sizeof(watchdog));
Jason Samsf2748272013-11-26 18:10:59 -0800517 memset(&mHal, 0, sizeof(mHal));
Tim Murray0e92fa32012-11-06 14:36:38 -0800518 mForceCpu = false;
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700519 mContextType = RS_CONTEXT_TYPE_NORMAL;
Tim Murray4d252d62012-11-29 14:37:59 -0800520 mSynchronous = false;
Tim Murray0e92fa32012-11-06 14:36:38 -0800521}
522
523Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc,
Tim Murray84e3dea2013-09-09 16:12:51 -0700524 RsContextType ct, uint32_t flags) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700525 Context * rsc = new Context();
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700526
Tim Murray84e3dea2013-09-09 16:12:51 -0700527 if (flags & RS_CONTEXT_LOW_LATENCY) {
528 rsc->mForceCpu = true;
529 }
530 if (flags & RS_CONTEXT_SYNCHRONOUS) {
531 rsc->mSynchronous = true;
532 }
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700533 rsc->mContextType = ct;
Tim Murray0e92fa32012-11-06 14:36:38 -0800534
Jason Sams5c1c79a2010-11-03 14:27:11 -0700535 if (!rsc->initContext(dev, sc)) {
536 delete rsc;
537 return NULL;
538 }
539 return rsc;
540}
541
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700542Context * Context::createContextLite() {
543 Context * rsc = new Context();
544 rsc->mIsContextLite = true;
545 return rsc;
546}
547
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800548bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700549 pthread_mutex_lock(&gInitMutex);
550
Jason Sams1a4efa32011-05-17 15:01:29 -0700551 mIO.init();
Jason Sams5f27d6f2012-02-07 15:32:08 -0800552 mIO.setTimeoutCallback(printWatchdogInfo, this, 2e9);
Jason Sams1a4efa32011-05-17 15:01:29 -0700553
Jason Sams5c1c79a2010-11-03 14:27:11 -0700554 dev->addContext(this);
555 mDev = dev;
Jason Sams6b8552a2010-10-13 15:31:10 -0700556 if (sc) {
557 mUserSurfaceConfig = *sc;
558 } else {
559 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
560 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800561
Jason Sams6b8552a2010-10-13 15:31:10 -0700562 mIsGraphicsContext = sc != NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700563
Jason Samsa658e902009-06-04 14:35:01 -0700564 int status;
565 pthread_attr_t threadAttr;
566
Jason Samsfb03a222009-10-15 16:47:31 -0700567 pthread_mutex_unlock(&gInitMutex);
568
569 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700570
Jason Samsa658e902009-06-04 14:35:01 -0700571 status = pthread_attr_init(&threadAttr);
572 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000573 ALOGE("Failed to init thread attribute.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700574 return false;
Jason Samsa658e902009-06-04 14:35:01 -0700575 }
576
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700577 mHasSurface = false;
Jason Sams992a0b72009-06-23 12:22:47 -0700578
Jason Sams24371d92009-08-19 12:17:14 -0700579 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700580 timerSet(RS_TIMER_INTERNAL);
Tim Murray4d252d62012-11-29 14:37:59 -0800581 if (mSynchronous) {
582 threadProc(this);
583 } else {
584 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
585 if (status) {
586 ALOGE("Failed to start rs context thread.");
587 return false;
588 }
589 while (!mRunning && (mError == RS_ERROR_NONE)) {
590 usleep(100);
591 }
Jason Sams50869382009-08-18 17:07:09 -0700592
Tim Murray4d252d62012-11-29 14:37:59 -0800593 if (mError != RS_ERROR_NONE) {
594 ALOGE("Errors during thread init");
595 return false;
596 }
Jason Sams18133402010-07-20 15:09:00 -0700597
Tim Murray4d252d62012-11-29 14:37:59 -0800598 pthread_attr_destroy(&threadAttr);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700599 }
Jason Sams5c1c79a2010-11-03 14:27:11 -0700600 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700601}
602
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800603Context::~Context() {
Tim Murray20f62ca2013-05-09 11:57:35 -0700604 //ALOGV("%p Context::~Context", this);
Jason Samscf912de2011-01-09 16:09:51 -0800605
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700606 if (!mIsContextLite) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700607 mPaused = false;
608 void *res;
Jason Sams326e0dd2009-05-22 14:03:28 -0700609
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700610 mIO.shutdown();
611 int status = pthread_join(mThreadId, &res);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800612 rsAssert(mExit);
Jason Sams326e0dd2009-05-22 14:03:28 -0700613
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700614 if (mHal.funcs.shutdownDriver) {
615 mHal.funcs.shutdownDriver(this);
616 }
617
618 // Global structure cleanup.
619 pthread_mutex_lock(&gInitMutex);
620 if (mDev) {
621 mDev->removeContext(this);
622 mDev = NULL;
623 }
624 pthread_mutex_unlock(&gInitMutex);
Jason Sams51462c52011-01-25 00:26:25 -0800625 }
Tim Murray20f62ca2013-05-09 11:57:35 -0700626 //ALOGV("%p Context::~Context done", this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700627}
628
Jason Sams93eacc72012-12-18 14:26:57 -0800629#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700630void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800631 rsAssert(mIsGraphicsContext);
Jason Sams4b3de472011-04-06 17:52:23 -0700632 mHal.funcs.setSurface(this, w, h, sur);
Jason Sams458f2dc2009-11-03 13:58:36 -0800633
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700634 mHasSurface = sur != NULL;
Jason Sams4b3de472011-04-06 17:52:23 -0700635 mWidth = w;
636 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800637
Jason Sams4b3de472011-04-06 17:52:23 -0700638 if (mWidth && mHeight) {
Jason Sams771565f2010-05-14 15:30:29 -0700639 mStateVertex.updateSize(this);
Alex Sakhartchouka544b632011-07-19 17:50:29 -0700640 mFBOCache.updateSize();
Jason Sams458f2dc2009-11-03 13:58:36 -0800641 }
642}
643
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800644uint32_t Context::getCurrentSurfaceWidth() const {
645 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
646 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
647 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimX();
648 }
649 }
650 if (mFBOCache.mHal.state.depthTarget != NULL) {
651 return mFBOCache.mHal.state.depthTarget->getType()->getDimX();
652 }
653 return mWidth;
654}
655
656uint32_t Context::getCurrentSurfaceHeight() const {
657 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
658 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
659 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimY();
660 }
661 }
662 if (mFBOCache.mHal.state.depthTarget != NULL) {
663 return mFBOCache.mHal.state.depthTarget->getType()->getDimY();
664 }
665 return mHeight;
666}
667
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800668void Context::pause() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800669 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700670 mPaused = true;
671}
672
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800673void Context::resume() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800674 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700675 mPaused = false;
676}
677
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800678void Context::setRootScript(Script *s) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800679 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700680 mRootScript.set(s);
681}
682
Jason Sams60709252010-11-17 15:29:32 -0800683void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800684 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700685 if (pfs == NULL) {
686 mFragmentStore.set(mStateFragmentStore.mDefault);
687 } else {
688 mFragmentStore.set(pfs);
689 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700690}
691
Jason Sams60709252010-11-17 15:29:32 -0800692void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800693 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700694 if (pf == NULL) {
695 mFragment.set(mStateFragment.mDefault);
696 } else {
697 mFragment.set(pf);
698 }
Jason Samscfb1d112009-08-05 13:57:03 -0700699}
700
Jason Sams60709252010-11-17 15:29:32 -0800701void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800702 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700703 if (pr == NULL) {
704 mRaster.set(mStateRaster.mDefault);
705 } else {
706 mRaster.set(pr);
707 }
708}
709
Jason Sams60709252010-11-17 15:29:32 -0800710void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800711 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700712 if (pv == NULL) {
713 mVertex.set(mStateVertex.mDefault);
714 } else {
715 mVertex.set(pv);
716 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700717}
718
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800719void Context::setFont(Font *f) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700720 rsAssert(mIsGraphicsContext);
721 if (f == NULL) {
722 mFont.set(mStateFont.mDefault);
723 } else {
724 mFont.set(f);
725 }
726}
Jason Sams93eacc72012-12-18 14:26:57 -0800727#endif
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700728
Jason Samsf2748272013-11-26 18:10:59 -0800729void Context::finish() {
730 if (mHal.funcs.finish) {
731 mHal.funcs.finish(this);
732 }
733}
734
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800735void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700736 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700737 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700738 mNames.add(obj);
739}
740
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800741void Context::removeName(ObjectBase *obj) {
742 for (size_t ct=0; ct < mNames.size(); ct++) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700743 if (obj == mNames[ct]) {
744 mNames.removeAt(ct);
745 return;
746 }
747 }
748}
749
Jason Sams1a4efa32011-05-17 15:01:29 -0700750RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) {
751 return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800752}
753
Jason Sams1a4efa32011-05-17 15:01:29 -0700754RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) {
755 return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen);
Jason Sams8c401ef2009-10-06 13:58:47 -0700756}
757
Jason Sams87319de2010-11-22 16:20:16 -0800758bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
759 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Sams1a4efa32011-05-17 15:01:29 -0700760
Jason Sams4961cce2013-04-11 16:11:46 -0700761 pthread_mutex_lock(&gMessageMutex);
762 bool ret = mIO.sendToClient(cmdID, subID, data, len, waitForSpace);
763 pthread_mutex_unlock(&gMessageMutex);
764 return ret;
Jason Sams8c401ef2009-10-06 13:58:47 -0700765}
766
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800767void Context::initToClient() {
768 while (!mRunning) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700769 usleep(100);
770 }
771}
772
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800773void Context::deinitToClient() {
Jason Sams1a4efa32011-05-17 15:01:29 -0700774 mIO.clientShutdown();
Jason Sams8c401ef2009-10-06 13:58:47 -0700775}
Jason Sams50869382009-08-18 17:07:09 -0700776
Jason Sams87319de2010-11-22 16:20:16 -0800777void Context::setError(RsError e, const char *msg) const {
Jason Samsa2cf7552010-03-03 13:03:18 -0800778 mError = e;
Jason Samsaad4bc52010-11-08 17:06:46 -0800779 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Samsa2cf7552010-03-03 13:03:18 -0800780}
781
782
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800783void Context::dumpDebug() const {
Steve Blockaf12ac62012-01-06 19:20:56 +0000784 ALOGE("RS Context debug %p", this);
785 ALOGE("RS Context debug");
Jason Sams13e26342009-11-24 12:26:35 -0800786
Steve Blockaf12ac62012-01-06 19:20:56 +0000787 ALOGE(" RS width %i, height %i", mWidth, mHeight);
788 ALOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
789 ALOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId);
Jason Sams13e26342009-11-24 12:26:35 -0800790}
Jason Samsa4a54e42009-06-10 18:39:40 -0700791
Jason Sams326e0dd2009-05-22 14:03:28 -0700792///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700793//
Jason Sams326e0dd2009-05-22 14:03:28 -0700794
795namespace android {
796namespace renderscript {
797
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800798void rsi_ContextFinish(Context *rsc) {
Jason Samsf2748272013-11-26 18:10:59 -0800799 rsc->finish();
Jason Sams8c880902010-06-15 12:15:57 -0700800}
Jason Sams326e0dd2009-05-22 14:03:28 -0700801
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800802void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Sams93eacc72012-12-18 14:26:57 -0800803#ifndef RS_COMPATIBILITY_LIB
Jason Sams326e0dd2009-05-22 14:03:28 -0700804 Script *s = static_cast<Script *>(vs);
805 rsc->setRootScript(s);
Jason Sams93eacc72012-12-18 14:26:57 -0800806#endif
Jason Sams326e0dd2009-05-22 14:03:28 -0700807}
808
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800809void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700810 Sampler *s = static_cast<Sampler *>(vs);
811
812 if (slot > RS_MAX_SAMPLER_SLOT) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000813 ALOGE("Invalid sampler slot");
Jason Sams326e0dd2009-05-22 14:03:28 -0700814 return;
815 }
816
817 s->bindToContext(&rsc->mStateSampler, slot);
818}
819
Jason Sams93eacc72012-12-18 14:26:57 -0800820#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800821void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Samsccc010b2010-05-13 18:30:11 -0700822 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams60709252010-11-17 15:29:32 -0800823 rsc->setProgramStore(pfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700824}
825
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800826void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700827 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Sams60709252010-11-17 15:29:32 -0800828 rsc->setProgramFragment(pf);
Jason Sams326e0dd2009-05-22 14:03:28 -0700829}
830
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800831void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Sams5fd09d82009-09-23 13:57:02 -0700832 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Sams60709252010-11-17 15:29:32 -0800833 rsc->setProgramRaster(pr);
Jason Sams5fd09d82009-09-23 13:57:02 -0700834}
835
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800836void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700837 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Sams60709252010-11-17 15:29:32 -0800838 rsc->setProgramVertex(pv);
Jason Sams326e0dd2009-05-22 14:03:28 -0700839}
840
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800841void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700842 Font *font = static_cast<Font *>(vfont);
843 rsc->setFont(font);
844}
Jason Sams93eacc72012-12-18 14:26:57 -0800845#endif
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700846
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700847void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700848 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700849 rsc->assignName(ob, name, name_length);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700850}
Jason Sams326e0dd2009-05-22 14:03:28 -0700851
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800852void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams2353ae32010-10-14 17:48:46 -0700853 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -0700854 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700855 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700856}
857
Jason Sams93eacc72012-12-18 14:26:57 -0800858#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800859void rsi_ContextPause(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700860 rsc->pause();
861}
862
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800863void rsi_ContextResume(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700864 rsc->resume();
865}
866
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700867void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
Mathias Agopianfa402862010-02-12 14:04:35 -0800868 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800869}
Jason Sams93eacc72012-12-18 14:26:57 -0800870#endif
Jason Sams613cad12009-11-12 15:10:25 -0800871
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800872void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800873 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800874}
875
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800876void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Samsc21cf402009-11-17 17:26:46 -0800877 ObjectBase::dumpAll(rsc);
878}
879
Jason Sams741aac92010-12-24 14:38:39 -0800880void rsi_ContextDestroyWorker(Context *rsc) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700881 rsc->destroyWorkerThreadResources();
Jason Sams741aac92010-12-24 14:38:39 -0800882}
883
Jason Samsc975cf42011-04-28 18:26:48 -0700884void rsi_ContextDestroy(Context *rsc) {
Tim Murray20f62ca2013-05-09 11:57:35 -0700885 //ALOGV("%p rsContextDestroy", rsc);
Jason Sams741aac92010-12-24 14:38:39 -0800886 rsContextDestroyWorker(rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800887 delete rsc;
Tim Murray20f62ca2013-05-09 11:57:35 -0700888 //ALOGV("%p rsContextDestroy done", rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800889}
890
Jason Samsc975cf42011-04-28 18:26:48 -0700891RsMessageToClientType rsi_ContextPeekMessage(Context *rsc,
Jason Sams186e5912011-04-26 14:50:00 -0700892 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700893 uint32_t * subID, size_t subID_length) {
894 return rsc->peekMessageToClient(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800895}
896
Jason Samsc975cf42011-04-28 18:26:48 -0700897RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length,
Jason Sams186e5912011-04-26 14:50:00 -0700898 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700899 uint32_t * subID, size_t subID_length) {
Jason Sams186e5912011-04-26 14:50:00 -0700900 rsAssert(subID_length == sizeof(uint32_t));
901 rsAssert(receiveLen_length == sizeof(size_t));
Jason Sams1a4efa32011-05-17 15:01:29 -0700902 return rsc->getMessageToClient(data, receiveLen, subID, data_length);
Jason Sams8c401ef2009-10-06 13:58:47 -0700903}
904
Jason Samsc975cf42011-04-28 18:26:48 -0700905void rsi_ContextInitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700906 rsc->initToClient();
907}
908
Jason Samsc975cf42011-04-28 18:26:48 -0700909void rsi_ContextDeinitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700910 rsc->deinitToClient();
911}
912
Jason Sams70265202013-02-05 19:20:47 -0800913void rsi_ContextSendMessage(Context *rsc, uint32_t id, const uint8_t *data, size_t len) {
914 rsc->sendMessageToClient(data, RS_MESSAGE_TO_CLIENT_USER, id, len, true);
915}
916
Tim Murray397b0b12014-01-07 15:35:08 -0800917// implementation of handcode LF_ObjDestroy
918// required so nObjDestroy can be run from finalizer without blocking
919void LF_ObjDestroy_handcode(const Context *rsc, RsAsyncVoidPtr objPtr) {
920 if (((Context *)rsc)->isSynchronous()) {
921 rsi_ObjDestroy((Context *)rsc, objPtr);
922 return;
923 }
924
925 // struct has two parts:
926 // RsPlaybackRemoteHeader (cmdID and bytes)
927 // RS_CMD_ObjDestroy (ptr)
928 struct destroyCmd {
929 uint32_t cmdID;
930 uint32_t bytes;
931 RsAsyncVoidPtr ptr;
932 };
933
934 destroyCmd cmd;
935 cmd.cmdID = RS_CMD_ID_ObjDestroy;
936 cmd.bytes = sizeof(RsAsyncVoidPtr);
937 cmd.ptr = objPtr;
938 ThreadIO *io = &((Context *)rsc)->mIO;
939 io->coreWrite((void*)&cmd, sizeof(destroyCmd));
940
941}
942
Jason Samsc975cf42011-04-28 18:26:48 -0700943}
944}
945
Tim Murrayc2ce7072013-07-17 18:38:53 -0700946extern "C" RsContext rsContextCreate(RsDevice vdev, uint32_t version, uint32_t sdkVersion,
Tim Murray84e3dea2013-09-09 16:12:51 -0700947 RsContextType ct, uint32_t flags) {
Tim Murray20f62ca2013-05-09 11:57:35 -0700948 //ALOGV("rsContextCreate dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700949 Device * dev = static_cast<Device *>(vdev);
Tim Murray84e3dea2013-09-09 16:12:51 -0700950 Context *rsc = Context::createContext(dev, NULL, ct, flags);
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700951 if (rsc) {
952 rsc->setTargetSdkVersion(sdkVersion);
953 }
Jason Sams789ca832011-05-18 17:36:02 -0700954 return rsc;
955}
956
Jason Sams93eacc72012-12-18 14:26:57 -0800957#ifndef RS_COMPATIBILITY_LIB
Jason Sams789ca832011-05-18 17:36:02 -0700958RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700959 uint32_t sdkVersion, RsSurfaceConfig sc,
960 uint32_t dpi) {
Tim Murray20f62ca2013-05-09 11:57:35 -0700961 //ALOGV("rsContextCreateGL dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700962 Device * dev = static_cast<Device *>(vdev);
963 Context *rsc = Context::createContext(dev, &sc);
Jason Sams9544f762011-07-13 16:09:42 -0700964 if (rsc) {
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700965 rsc->setTargetSdkVersion(sdkVersion);
Jason Sams9544f762011-07-13 16:09:42 -0700966 rsc->setDPI(dpi);
967 }
Tim Murray20f62ca2013-05-09 11:57:35 -0700968 //ALOGV("%p rsContextCreateGL ret", rsc);
Jason Sams789ca832011-05-18 17:36:02 -0700969 return rsc;
970}
Jason Sams93eacc72012-12-18 14:26:57 -0800971#endif
Jason Sams789ca832011-05-18 17:36:02 -0700972
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700973// Only to be called at a3d load time, before object is visible to user
974// not thread safe
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800975void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700976 ObjectBase *ob = static_cast<ObjectBase *>(obj);
977 (*name) = ob->getName();
978}
Tim Murray397b0b12014-01-07 15:35:08 -0800979