blob: 04284dd964b0bb9993577627ccd59fb44c91fafb [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
17#include "rsDevice.h"
18#include "rsContext.h"
19#include "rsThreadIO.h"
Mathias Agopian5ae678f2009-06-22 18:01:09 -070020#include <ui/FramebufferNativeWindow.h>
Jason Sams5f27d6f2012-02-07 15:32:08 -080021#include <gui/DisplayEventReceiver.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070022
Jason Sams15832442009-11-15 12:14:26 -080023#include <sys/types.h>
24#include <sys/resource.h>
Jason Sams7bf29dd2010-07-19 15:38:19 -070025#include <sched.h>
Jason Sams15832442009-11-15 12:14:26 -080026
Joe Onorato76371ff2009-09-23 16:37:36 -070027#include <cutils/properties.h>
28
Jason Sams15832442009-11-15 12:14:26 -080029#include <cutils/sched_policy.h>
Jason Sams8d957fa2010-09-28 14:41:22 -070030#include <sys/syscall.h>
31#include <string.h>
Jason Sams15832442009-11-15 12:14:26 -080032
Jason Sams326e0dd2009-05-22 14:03:28 -070033using namespace android;
34using namespace android::renderscript;
35
Jason Samsfb03a222009-10-15 16:47:31 -070036pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Stephen Hinesca3f09c2011-01-07 15:11:30 -080037pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070038
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080039bool Context::initGLThread() {
Jason Sams6b8552a2010-10-13 15:31:10 -070040 pthread_mutex_lock(&gInitMutex);
Jason Sams6b8552a2010-10-13 15:31:10 -070041
Jason Sams4b3de472011-04-06 17:52:23 -070042 if (!mHal.funcs.initGraphics(this)) {
Jason Sams5c1c79a2010-11-03 14:27:11 -070043 pthread_mutex_unlock(&gInitMutex);
Steve Blockaf12ac62012-01-06 19:20:56 +000044 ALOGE("%p initGraphics failed", this);
Jason Sams5c1c79a2010-11-03 14:27:11 -070045 return false;
Jason Sams6b8552a2010-10-13 15:31:10 -070046 }
47
Jason Sams6b8552a2010-10-13 15:31:10 -070048 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -070049 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -070050}
51
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080052void Context::deinitEGL() {
Jason Sams4b3de472011-04-06 17:52:23 -070053 mHal.funcs.shutdownGraphics(this);
Jason Sams33b6e3b2009-10-27 14:44:31 -070054}
55
Jason Sams60709252010-11-17 15:29:32 -080056Context::PushState::PushState(Context *con) {
57 mRsc = con;
Jason Samsc946b612011-02-23 14:47:17 -080058 if (con->mIsGraphicsContext) {
59 mFragment.set(con->getProgramFragment());
60 mVertex.set(con->getProgramVertex());
61 mStore.set(con->getProgramStore());
62 mRaster.set(con->getProgramRaster());
63 mFont.set(con->getFont());
64 }
Jason Sams60709252010-11-17 15:29:32 -080065}
66
67Context::PushState::~PushState() {
Jason Samsc946b612011-02-23 14:47:17 -080068 if (mRsc->mIsGraphicsContext) {
69 mRsc->setProgramFragment(mFragment.get());
70 mRsc->setProgramVertex(mVertex.get());
71 mRsc->setProgramStore(mStore.get());
72 mRsc->setProgramRaster(mRaster.get());
73 mRsc->setFont(mFont.get());
74 }
Jason Sams60709252010-11-17 15:29:32 -080075}
76
Jason Sams33b6e3b2009-10-27 14:44:31 -070077
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080078uint32_t Context::runScript(Script *s) {
Jason Sams60709252010-11-17 15:29:32 -080079 PushState(this);
Jason Sams10308932009-06-09 12:15:30 -070080
Jason Samsc61346b2010-05-28 18:23:22 -070081 uint32_t ret = s->run(this);
Jason Samsc9d43db2009-07-28 12:02:16 -070082 return ret;
Jason Sams10308932009-06-09 12:15:30 -070083}
84
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080085uint32_t Context::runRootScript() {
Jason Sams2dca84d2009-12-09 11:05:45 -080086 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -070087 mStateFragmentStore.mLast.clear();
Jason Sams2382aba2011-09-13 15:41:01 -070088 watchdog.inRoot = true;
Jason Samsc61346b2010-05-28 18:23:22 -070089 uint32_t ret = runScript(mRootScript.get());
Jason Sams2382aba2011-09-13 15:41:01 -070090 watchdog.inRoot = false;
Jason Sams8cfdd242009-10-14 15:43:53 -070091
Jason Samscfb1d112009-08-05 13:57:03 -070092 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -070093}
94
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080095uint64_t Context::getTime() const {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -070096#ifndef ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -070097 struct timespec t;
98 clock_gettime(CLOCK_MONOTONIC, &t);
99 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700100#else
101 return 0;
102#endif //ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -0700103}
104
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800105void Context::timerReset() {
Jason Sams24371d92009-08-19 12:17:14 -0700106 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
107 mTimers[ct] = 0;
108 }
109}
110
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800111void Context::timerInit() {
Jason Sams24371d92009-08-19 12:17:14 -0700112 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700113 mTimeFrame = mTimeLast;
114 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700115 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700116 mAverageFPSFrameCount = 0;
117 mAverageFPSStartTime = mTimeLast;
118 mAverageFPS = 0;
Jason Sams24371d92009-08-19 12:17:14 -0700119 timerReset();
120}
121
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800122void Context::timerFrame() {
Jason Sams1d54f102009-09-03 15:43:13 -0700123 mTimeLastFrame = mTimeFrame;
124 mTimeFrame = getTime();
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700125 // Update average fps
126 const uint64_t averageFramerateInterval = 1000 * 1000000;
127 mAverageFPSFrameCount ++;
128 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800129 if (inverval >= averageFramerateInterval) {
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700130 inverval = inverval / 1000000;
131 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
132 mAverageFPSFrameCount = 0;
133 mAverageFPSStartTime = mTimeFrame;
134 }
Jason Sams1d54f102009-09-03 15:43:13 -0700135}
136
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800137void Context::timerSet(Timers tm) {
Jason Sams24371d92009-08-19 12:17:14 -0700138 uint64_t last = mTimeLast;
139 mTimeLast = getTime();
140 mTimers[mTimerActive] += mTimeLast - last;
141 mTimerActive = tm;
142}
143
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800144void Context::timerPrint() {
Jason Sams24371d92009-08-19 12:17:14 -0700145 double total = 0;
146 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
147 total += mTimers[ct];
148 }
Jason Sams1d54f102009-09-03 15:43:13 -0700149 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800150 mTimeMSLastFrame = frame / 1000000;
151 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
152 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700153
Jason Sams2dca84d2009-12-09 11:05:45 -0800154
155 if (props.mLogTimes) {
Steve Block65982012011-10-20 11:56:00 +0100156 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 -0800157 mTimeMSLastFrame,
158 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
159 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
160 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700161 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
162 mAverageFPS);
Jason Sams2dca84d2009-12-09 11:05:45 -0800163 }
Jason Sams24371d92009-08-19 12:17:14 -0700164}
165
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800166bool Context::setupCheck() {
Jason Sams900f1612010-09-16 18:18:29 -0700167
Jason Sams721acc42011-04-06 11:23:54 -0700168 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700169 mFragment->setup(this, &mStateFragment);
Jason Sams721acc42011-04-06 11:23:54 -0700170 mRaster->setup(this, &mStateRaster);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700171 mVertex->setup(this, &mStateVertex);
172 mFBOCache.setup(this);
Jason Samsa2cf7552010-03-03 13:03:18 -0800173 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700174}
175
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700176void Context::setupProgramStore() {
Jason Sams721acc42011-04-06 11:23:54 -0700177 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700178}
179
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800180static bool getProp(const char *str) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700181 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700182 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700183 return 0 != strcmp(buf, "0");
184}
Jason Sams326e0dd2009-05-22 14:03:28 -0700185
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800186void Context::displayDebugStats() {
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700187 char buffer[128];
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700188 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700189 float oldR, oldG, oldB, oldA;
190 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700191 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700192
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700193 ObjectBaseRef<Font> lastFont(getFont());
194 setFont(NULL);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700195 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700196 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700197 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700198
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700199 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700200 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700201
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700202 setFont(lastFont.get());
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700203 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700204}
205
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800206void * Context::threadProc(void *vrsc) {
Jason Sams83c451a2011-04-21 11:46:50 -0700207 Context *rsc = static_cast<Context *>(vrsc);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700208#ifndef ANDROID_RS_SERIALIZE
Jason Sams83c451a2011-04-21 11:46:50 -0700209 rsc->mNativeThreadId = gettid();
Jason Sams83c451a2011-04-21 11:46:50 -0700210 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
211 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700212#endif //ANDROID_RS_SERIALIZE
Jason Sams83c451a2011-04-21 11:46:50 -0700213 rsc->props.mLogTimes = getProp("debug.rs.profile");
214 rsc->props.mLogScripts = getProp("debug.rs.script");
215 rsc->props.mLogObjects = getProp("debug.rs.object");
216 rsc->props.mLogShaders = getProp("debug.rs.shader");
217 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes");
218 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms");
219 rsc->props.mLogVisual = getProp("debug.rs.visual");
Joe Onorato76371ff2009-09-23 16:37:36 -0700220
Jason Sams83c451a2011-04-21 11:46:50 -0700221 if (!rsdHalInit(rsc, 0, 0)) {
Jason Samsd3e71072011-05-03 15:01:58 -0700222 rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed initializing GL");
Steve Blockaf12ac62012-01-06 19:20:56 +0000223 ALOGE("Hal init failed");
Jason Sams83c451a2011-04-21 11:46:50 -0700224 return NULL;
225 }
226 rsc->mHal.funcs.setPriority(rsc, rsc->mThreadPriority);
Jason Samse5769102009-06-19 16:03:18 -0700227
Jason Sams83c451a2011-04-21 11:46:50 -0700228 if (rsc->mIsGraphicsContext) {
Jason Samsd3e71072011-05-03 15:01:58 -0700229 if (!rsc->initGLThread()) {
230 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
231 return NULL;
232 }
233
Jason Sams83c451a2011-04-21 11:46:50 -0700234 rsc->mStateRaster.init(rsc);
235 rsc->setProgramRaster(NULL);
236 rsc->mStateVertex.init(rsc);
237 rsc->setProgramVertex(NULL);
238 rsc->mStateFragment.init(rsc);
239 rsc->setProgramFragment(NULL);
240 rsc->mStateFragmentStore.init(rsc);
241 rsc->setProgramStore(NULL);
242 rsc->mStateFont.init(rsc);
243 rsc->setFont(NULL);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700244 rsc->mStateSampler.init(rsc);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700245 rsc->mFBOCache.init(rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700246 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700247
Jason Sams83c451a2011-04-21 11:46:50 -0700248 rsc->mRunning = true;
Jason Sams5f27d6f2012-02-07 15:32:08 -0800249 if (!rsc->mIsGraphicsContext) {
250 while (!rsc->mExit) {
251 rsc->mIO.playCoreCommands(rsc, true, -1);
Jason Samse0aab4a2011-08-12 15:05:15 -0700252 }
Jason Sams5f27d6f2012-02-07 15:32:08 -0800253 } else {
254#ifndef ANDROID_RS_SERIALIZE
255 DisplayEventReceiver displayEvent;
256 DisplayEventReceiver::Event eventBuffer[1];
257#endif
258 int vsyncRate = 0;
259 int targetRate = 0;
Jason Samse0aab4a2011-08-12 15:05:15 -0700260
Jason Sams5f27d6f2012-02-07 15:32:08 -0800261 bool drawOnce = false;
262 while (!rsc->mExit) {
263 rsc->timerSet(RS_TIMER_IDLE);
Jason Sams326e0dd2009-05-22 14:03:28 -0700264
Jason Sams5f27d6f2012-02-07 15:32:08 -0800265#ifndef ANDROID_RS_SERIALIZE
266 if (vsyncRate != targetRate) {
267 displayEvent.setVsyncRate(targetRate);
268 vsyncRate = targetRate;
269 }
270 if (targetRate) {
271 drawOnce |= rsc->mIO.playCoreCommands(rsc, true, displayEvent.getFd());
272 while (displayEvent.getEvents(eventBuffer, 1) != 0) {
273 //ALOGE("vs2 time past %lld", (rsc->getTime() - eventBuffer[0].header.timestamp) / 1000000);
274 }
275 } else
276#endif
277 {
278 drawOnce |= rsc->mIO.playCoreCommands(rsc, true, -1);
Jason Sams83c451a2011-04-21 11:46:50 -0700279 }
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700280
Jason Sams5f27d6f2012-02-07 15:32:08 -0800281 if ((rsc->mRootScript.get() != NULL) && rsc->mHasSurface &&
282 (targetRate || drawOnce) && !rsc->mPaused) {
283
284 drawOnce = false;
285 targetRate = ((rsc->runRootScript() + 15) / 16);
286
287 if (rsc->props.mLogVisual) {
288 rsc->displayDebugStats();
289 }
290
291 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
292 rsc->mHal.funcs.swap(rsc);
293 rsc->timerFrame();
294 rsc->timerSet(RS_TIMER_INTERNAL);
295 rsc->timerPrint();
296 rsc->timerReset();
297 }
Jason Sams83c451a2011-04-21 11:46:50 -0700298 }
Jason Sams83c451a2011-04-21 11:46:50 -0700299 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700300
Steve Block65982012011-10-20 11:56:00 +0100301 ALOGV("%p RS Thread exiting", rsc);
Jason Samse514b452009-09-25 14:51:22 -0700302
Jason Sams83c451a2011-04-21 11:46:50 -0700303 if (rsc->mIsGraphicsContext) {
304 pthread_mutex_lock(&gInitMutex);
305 rsc->deinitEGL();
306 pthread_mutex_unlock(&gInitMutex);
307 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700308
Steve Block65982012011-10-20 11:56:00 +0100309 ALOGV("%p RS Thread exited", rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700310 return NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700311}
312
Jason Sams741aac92010-12-24 14:38:39 -0800313void Context::destroyWorkerThreadResources() {
Steve Block65982012011-10-20 11:56:00 +0100314 //ALOGV("destroyWorkerThreadResources 1");
Jason Sams2e8665d2011-01-27 00:14:13 -0800315 ObjectBase::zeroAllUserRef(this);
Jason Sams741aac92010-12-24 14:38:39 -0800316 if (mIsGraphicsContext) {
317 mRaster.clear();
318 mFragment.clear();
319 mVertex.clear();
320 mFragmentStore.clear();
321 mFont.clear();
322 mRootScript.clear();
323 mStateRaster.deinit(this);
324 mStateVertex.deinit(this);
325 mStateFragment.deinit(this);
326 mStateFragmentStore.deinit(this);
327 mStateFont.deinit(this);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700328 mStateSampler.deinit(this);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700329 mFBOCache.deinit(this);
Jason Sams741aac92010-12-24 14:38:39 -0800330 }
Jason Samsc7cec1e2011-08-18 18:01:33 -0700331 ObjectBase::freeAllChildren(this);
Jason Samscf912de2011-01-09 16:09:51 -0800332 mExit = true;
Jason Sams5f27d6f2012-02-07 15:32:08 -0800333 //ALOGV("destroyWorkerThreadResources 2");
Jason Sams741aac92010-12-24 14:38:39 -0800334}
335
Jason Sams2382aba2011-09-13 15:41:01 -0700336void Context::printWatchdogInfo(void *ctx) {
337 Context *rsc = (Context *)ctx;
Jason Samsee803442011-10-13 16:05:27 -0700338 if (rsc->watchdog.command && rsc->watchdog.file) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000339 ALOGE("RS watchdog timeout: %i %s line %i %s", rsc->watchdog.inRoot,
Jason Samsee803442011-10-13 16:05:27 -0700340 rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file);
341 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000342 ALOGE("RS watchdog timeout: %i", rsc->watchdog.inRoot);
Jason Samsee803442011-10-13 16:05:27 -0700343 }
Jason Sams2382aba2011-09-13 15:41:01 -0700344}
345
346
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800347void Context::setPriority(int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800348 // Note: If we put this in the proper "background" policy
349 // the wallpapers can become completly unresponsive at times.
350 // This is probably not what we want for something the user is actively
351 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800352 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800353#if 0
354 SchedPolicy pol = SP_FOREGROUND;
355 if (p > 0) {
356 pol = SP_BACKGROUND;
357 }
358 if (!set_sched_policy(mNativeThreadId, pol)) {
359 // success; reset the priority as well
360 }
361#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700362 setpriority(PRIO_PROCESS, mNativeThreadId, p);
Jason Sams15832442009-11-15 12:14:26 -0800363#endif
364}
365
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800366Context::Context() {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700367 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700368 mRunning = false;
369 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700370 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700371 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800372 mError = RS_ERROR_NONE;
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700373 mTargetSdkVersion = 14;
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700374 mDPI = 96;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700375 mIsContextLite = false;
Stephen Hines86c6b5f2011-10-31 14:07:54 -0700376 memset(&watchdog, 0, sizeof(watchdog));
Jason Sams5c1c79a2010-11-03 14:27:11 -0700377}
378
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800379Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700380 Context * rsc = new Context();
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700381
Jason Sams5c1c79a2010-11-03 14:27:11 -0700382 if (!rsc->initContext(dev, sc)) {
383 delete rsc;
384 return NULL;
385 }
386 return rsc;
387}
388
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700389Context * Context::createContextLite() {
390 Context * rsc = new Context();
391 rsc->mIsContextLite = true;
392 return rsc;
393}
394
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800395bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700396 pthread_mutex_lock(&gInitMutex);
397
Jason Sams1a4efa32011-05-17 15:01:29 -0700398 mIO.init();
Jason Sams5f27d6f2012-02-07 15:32:08 -0800399 mIO.setTimeoutCallback(printWatchdogInfo, this, 2e9);
Jason Sams1a4efa32011-05-17 15:01:29 -0700400
Jason Sams5c1c79a2010-11-03 14:27:11 -0700401 dev->addContext(this);
402 mDev = dev;
Jason Sams6b8552a2010-10-13 15:31:10 -0700403 if (sc) {
404 mUserSurfaceConfig = *sc;
405 } else {
406 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
407 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800408
Jason Sams6b8552a2010-10-13 15:31:10 -0700409 mIsGraphicsContext = sc != NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700410
Jason Samsa658e902009-06-04 14:35:01 -0700411 int status;
412 pthread_attr_t threadAttr;
413
Jason Samsfb03a222009-10-15 16:47:31 -0700414 pthread_mutex_unlock(&gInitMutex);
415
416 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700417
Jason Samsa658e902009-06-04 14:35:01 -0700418 status = pthread_attr_init(&threadAttr);
419 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000420 ALOGE("Failed to init thread attribute.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700421 return false;
Jason Samsa658e902009-06-04 14:35:01 -0700422 }
423
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700424 mHasSurface = false;
Jason Sams992a0b72009-06-23 12:22:47 -0700425
Jason Sams24371d92009-08-19 12:17:14 -0700426 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700427 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700428
Jason Samsa658e902009-06-04 14:35:01 -0700429 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700430 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000431 ALOGE("Failed to start rs context thread.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700432 return false;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700433 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800434 while (!mRunning && (mError == RS_ERROR_NONE)) {
Jason Sams18133402010-07-20 15:09:00 -0700435 usleep(100);
436 }
437
Jason Sams5c1c79a2010-11-03 14:27:11 -0700438 if (mError != RS_ERROR_NONE) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000439 ALOGE("Errors during thread init");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700440 return false;
441 }
442
Jason Samsa658e902009-06-04 14:35:01 -0700443 pthread_attr_destroy(&threadAttr);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700444 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700445}
446
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800447Context::~Context() {
Steve Block65982012011-10-20 11:56:00 +0100448 ALOGV("%p Context::~Context", this);
Jason Samscf912de2011-01-09 16:09:51 -0800449
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700450 if (!mIsContextLite) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700451 mPaused = false;
452 void *res;
Jason Sams326e0dd2009-05-22 14:03:28 -0700453
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700454 mIO.shutdown();
455 int status = pthread_join(mThreadId, &res);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800456 rsAssert(mExit);
Jason Sams326e0dd2009-05-22 14:03:28 -0700457
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700458 if (mHal.funcs.shutdownDriver) {
459 mHal.funcs.shutdownDriver(this);
460 }
461
462 // Global structure cleanup.
463 pthread_mutex_lock(&gInitMutex);
464 if (mDev) {
465 mDev->removeContext(this);
466 mDev = NULL;
467 }
468 pthread_mutex_unlock(&gInitMutex);
Jason Sams51462c52011-01-25 00:26:25 -0800469 }
Steve Block65982012011-10-20 11:56:00 +0100470 ALOGV("%p Context::~Context done", this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700471}
472
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700473void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800474 rsAssert(mIsGraphicsContext);
Jason Sams4b3de472011-04-06 17:52:23 -0700475 mHal.funcs.setSurface(this, w, h, sur);
Jason Sams458f2dc2009-11-03 13:58:36 -0800476
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700477 mHasSurface = sur != NULL;
Jason Sams4b3de472011-04-06 17:52:23 -0700478 mWidth = w;
479 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800480
Jason Sams4b3de472011-04-06 17:52:23 -0700481 if (mWidth && mHeight) {
Jason Sams771565f2010-05-14 15:30:29 -0700482 mStateVertex.updateSize(this);
Alex Sakhartchouka544b632011-07-19 17:50:29 -0700483 mFBOCache.updateSize();
Jason Sams458f2dc2009-11-03 13:58:36 -0800484 }
485}
486
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800487uint32_t Context::getCurrentSurfaceWidth() const {
488 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
489 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
490 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimX();
491 }
492 }
493 if (mFBOCache.mHal.state.depthTarget != NULL) {
494 return mFBOCache.mHal.state.depthTarget->getType()->getDimX();
495 }
496 return mWidth;
497}
498
499uint32_t Context::getCurrentSurfaceHeight() const {
500 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
501 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
502 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimY();
503 }
504 }
505 if (mFBOCache.mHal.state.depthTarget != NULL) {
506 return mFBOCache.mHal.state.depthTarget->getType()->getDimY();
507 }
508 return mHeight;
509}
510
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800511void Context::pause() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800512 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700513 mPaused = true;
514}
515
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800516void Context::resume() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800517 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700518 mPaused = false;
519}
520
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800521void Context::setRootScript(Script *s) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800522 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700523 mRootScript.set(s);
524}
525
Jason Sams60709252010-11-17 15:29:32 -0800526void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800527 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700528 if (pfs == NULL) {
529 mFragmentStore.set(mStateFragmentStore.mDefault);
530 } else {
531 mFragmentStore.set(pfs);
532 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700533}
534
Jason Sams60709252010-11-17 15:29:32 -0800535void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800536 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700537 if (pf == NULL) {
538 mFragment.set(mStateFragment.mDefault);
539 } else {
540 mFragment.set(pf);
541 }
Jason Samscfb1d112009-08-05 13:57:03 -0700542}
543
Jason Sams60709252010-11-17 15:29:32 -0800544void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800545 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700546 if (pr == NULL) {
547 mRaster.set(mStateRaster.mDefault);
548 } else {
549 mRaster.set(pr);
550 }
551}
552
Jason Sams60709252010-11-17 15:29:32 -0800553void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800554 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700555 if (pv == NULL) {
556 mVertex.set(mStateVertex.mDefault);
557 } else {
558 mVertex.set(pv);
559 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700560}
561
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800562void Context::setFont(Font *f) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700563 rsAssert(mIsGraphicsContext);
564 if (f == NULL) {
565 mFont.set(mStateFont.mDefault);
566 } else {
567 mFont.set(f);
568 }
569}
570
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800571void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700572 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700573 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700574 mNames.add(obj);
575}
576
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800577void Context::removeName(ObjectBase *obj) {
578 for (size_t ct=0; ct < mNames.size(); ct++) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700579 if (obj == mNames[ct]) {
580 mNames.removeAt(ct);
581 return;
582 }
583 }
584}
585
Jason Sams1a4efa32011-05-17 15:01:29 -0700586RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) {
587 return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800588}
589
Jason Sams1a4efa32011-05-17 15:01:29 -0700590RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) {
591 return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen);
Jason Sams8c401ef2009-10-06 13:58:47 -0700592}
593
Jason Sams87319de2010-11-22 16:20:16 -0800594bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
595 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Sams1a4efa32011-05-17 15:01:29 -0700596
597 return mIO.sendToClient(cmdID, subID, data, len, waitForSpace);
Jason Sams8c401ef2009-10-06 13:58:47 -0700598}
599
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800600void Context::initToClient() {
601 while (!mRunning) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700602 usleep(100);
603 }
604}
605
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800606void Context::deinitToClient() {
Jason Sams1a4efa32011-05-17 15:01:29 -0700607 mIO.clientShutdown();
Jason Sams8c401ef2009-10-06 13:58:47 -0700608}
Jason Sams50869382009-08-18 17:07:09 -0700609
Jason Sams87319de2010-11-22 16:20:16 -0800610void Context::setError(RsError e, const char *msg) const {
Jason Samsa2cf7552010-03-03 13:03:18 -0800611 mError = e;
Jason Samsaad4bc52010-11-08 17:06:46 -0800612 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Samsa2cf7552010-03-03 13:03:18 -0800613}
614
615
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800616void Context::dumpDebug() const {
Steve Blockaf12ac62012-01-06 19:20:56 +0000617 ALOGE("RS Context debug %p", this);
618 ALOGE("RS Context debug");
Jason Sams13e26342009-11-24 12:26:35 -0800619
Steve Blockaf12ac62012-01-06 19:20:56 +0000620 ALOGE(" RS width %i, height %i", mWidth, mHeight);
621 ALOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
622 ALOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId);
Jason Sams13e26342009-11-24 12:26:35 -0800623}
Jason Samsa4a54e42009-06-10 18:39:40 -0700624
Jason Sams326e0dd2009-05-22 14:03:28 -0700625///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700626//
Jason Sams326e0dd2009-05-22 14:03:28 -0700627
628namespace android {
629namespace renderscript {
630
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800631void rsi_ContextFinish(Context *rsc) {
Jason Sams8c880902010-06-15 12:15:57 -0700632}
Jason Sams326e0dd2009-05-22 14:03:28 -0700633
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800634void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700635 Script *s = static_cast<Script *>(vs);
636 rsc->setRootScript(s);
637}
638
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800639void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700640 Sampler *s = static_cast<Sampler *>(vs);
641
642 if (slot > RS_MAX_SAMPLER_SLOT) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000643 ALOGE("Invalid sampler slot");
Jason Sams326e0dd2009-05-22 14:03:28 -0700644 return;
645 }
646
647 s->bindToContext(&rsc->mStateSampler, slot);
648}
649
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800650void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Samsccc010b2010-05-13 18:30:11 -0700651 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams60709252010-11-17 15:29:32 -0800652 rsc->setProgramStore(pfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700653}
654
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800655void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700656 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Sams60709252010-11-17 15:29:32 -0800657 rsc->setProgramFragment(pf);
Jason Sams326e0dd2009-05-22 14:03:28 -0700658}
659
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800660void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Sams5fd09d82009-09-23 13:57:02 -0700661 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Sams60709252010-11-17 15:29:32 -0800662 rsc->setProgramRaster(pr);
Jason Sams5fd09d82009-09-23 13:57:02 -0700663}
664
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800665void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700666 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Sams60709252010-11-17 15:29:32 -0800667 rsc->setProgramVertex(pv);
Jason Sams326e0dd2009-05-22 14:03:28 -0700668}
669
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800670void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700671 Font *font = static_cast<Font *>(vfont);
672 rsc->setFont(font);
673}
674
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700675void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700676 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700677 rsc->assignName(ob, name, name_length);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700678}
Jason Sams326e0dd2009-05-22 14:03:28 -0700679
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800680void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams2353ae32010-10-14 17:48:46 -0700681 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -0700682 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700683 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700684}
685
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800686void rsi_ContextPause(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700687 rsc->pause();
688}
689
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800690void rsi_ContextResume(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700691 rsc->resume();
692}
693
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700694void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
Mathias Agopianfa402862010-02-12 14:04:35 -0800695 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800696}
697
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800698void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800699 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800700}
701
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800702void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Samsc21cf402009-11-17 17:26:46 -0800703 ObjectBase::dumpAll(rsc);
704}
705
Jason Sams741aac92010-12-24 14:38:39 -0800706void rsi_ContextDestroyWorker(Context *rsc) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700707 rsc->destroyWorkerThreadResources();
Jason Sams741aac92010-12-24 14:38:39 -0800708}
709
Jason Samsc975cf42011-04-28 18:26:48 -0700710void rsi_ContextDestroy(Context *rsc) {
Steve Block65982012011-10-20 11:56:00 +0100711 ALOGV("%p rsContextDestroy", rsc);
Jason Sams741aac92010-12-24 14:38:39 -0800712 rsContextDestroyWorker(rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800713 delete rsc;
Steve Block65982012011-10-20 11:56:00 +0100714 ALOGV("%p rsContextDestroy done", rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800715}
716
Jason Sams326e0dd2009-05-22 14:03:28 -0700717
Jason Samsc975cf42011-04-28 18:26:48 -0700718RsMessageToClientType rsi_ContextPeekMessage(Context *rsc,
Jason Sams186e5912011-04-26 14:50:00 -0700719 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700720 uint32_t * subID, size_t subID_length) {
721 return rsc->peekMessageToClient(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800722}
723
Jason Samsc975cf42011-04-28 18:26:48 -0700724RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length,
Jason Sams186e5912011-04-26 14:50:00 -0700725 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700726 uint32_t * subID, size_t subID_length) {
Jason Sams186e5912011-04-26 14:50:00 -0700727 rsAssert(subID_length == sizeof(uint32_t));
728 rsAssert(receiveLen_length == sizeof(size_t));
Jason Sams1a4efa32011-05-17 15:01:29 -0700729 return rsc->getMessageToClient(data, receiveLen, subID, data_length);
Jason Sams8c401ef2009-10-06 13:58:47 -0700730}
731
Jason Samsc975cf42011-04-28 18:26:48 -0700732void rsi_ContextInitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700733 rsc->initToClient();
734}
735
Jason Samsc975cf42011-04-28 18:26:48 -0700736void rsi_ContextDeinitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700737 rsc->deinitToClient();
738}
739
Jason Samsc975cf42011-04-28 18:26:48 -0700740}
741}
742
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700743RsContext rsContextCreate(RsDevice vdev, uint32_t version,
744 uint32_t sdkVersion) {
Steve Block65982012011-10-20 11:56:00 +0100745 ALOGV("rsContextCreate dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700746 Device * dev = static_cast<Device *>(vdev);
747 Context *rsc = Context::createContext(dev, NULL);
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700748 if (rsc) {
749 rsc->setTargetSdkVersion(sdkVersion);
750 }
Jason Sams789ca832011-05-18 17:36:02 -0700751 return rsc;
752}
753
754RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700755 uint32_t sdkVersion, RsSurfaceConfig sc,
756 uint32_t dpi) {
Steve Block65982012011-10-20 11:56:00 +0100757 ALOGV("rsContextCreateGL dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700758 Device * dev = static_cast<Device *>(vdev);
759 Context *rsc = Context::createContext(dev, &sc);
Jason Sams9544f762011-07-13 16:09:42 -0700760 if (rsc) {
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700761 rsc->setTargetSdkVersion(sdkVersion);
Jason Sams9544f762011-07-13 16:09:42 -0700762 rsc->setDPI(dpi);
763 }
Steve Block65982012011-10-20 11:56:00 +0100764 ALOGV("%p rsContextCreateGL ret", rsc);
Jason Sams789ca832011-05-18 17:36:02 -0700765 return rsc;
766}
767
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700768// Only to be called at a3d load time, before object is visible to user
769// not thread safe
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800770void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700771 ObjectBase *ob = static_cast<ObjectBase *>(obj);
772 (*name) = ob->getName();
773}