blob: 95ac76e84fd93d7f653920db0075dfdf9f78af3d [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -07002 * Copyright (C) 2011 The Android Open Source Project
Jason Samsd19f10d2009-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 Agopian3142f4f2009-06-22 18:01:09 -070020#include <ui/FramebufferNativeWindow.h>
Jason Sams4c2e4c82012-02-07 15:32:08 -080021#include <gui/DisplayEventReceiver.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070022
Jason Sams7d787b42009-11-15 12:14:26 -080023#include <sys/types.h>
24#include <sys/resource.h>
Jason Sams8e6c17f2010-07-19 15:38:19 -070025#include <sched.h>
Jason Sams7d787b42009-11-15 12:14:26 -080026
Joe Onorato9ac2c662009-09-23 16:37:36 -070027#include <cutils/properties.h>
28
Jason Sams7d787b42009-11-15 12:14:26 -080029#include <cutils/sched_policy.h>
Jason Samsf3470ed2010-09-28 14:41:22 -070030#include <sys/syscall.h>
31#include <string.h>
Jason Sams7d787b42009-11-15 12:14:26 -080032
Jason Samsd19f10d2009-05-22 14:03:28 -070033using namespace android;
34using namespace android::renderscript;
35
Jason Sams41c19db92009-10-15 16:47:31 -070036pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Stephen Hines1ac9da62011-01-07 15:11:30 -080037pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Samsd19f10d2009-05-22 14:03:28 -070038
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080039bool Context::initGLThread() {
Jason Sams11c8af92010-10-13 15:31:10 -070040 pthread_mutex_lock(&gInitMutex);
Jason Sams11c8af92010-10-13 15:31:10 -070041
Jason Sams803626f2011-04-06 17:52:23 -070042 if (!mHal.funcs.initGraphics(this)) {
Jason Samsd5f06302010-11-03 14:27:11 -070043 pthread_mutex_unlock(&gInitMutex);
Steve Block3762c312012-01-06 19:20:56 +000044 ALOGE("%p initGraphics failed", this);
Jason Samsd5f06302010-11-03 14:27:11 -070045 return false;
Jason Sams11c8af92010-10-13 15:31:10 -070046 }
47
Jason Sams11c8af92010-10-13 15:31:10 -070048 pthread_mutex_unlock(&gInitMutex);
Jason Samsd5f06302010-11-03 14:27:11 -070049 return true;
Jason Samsd19f10d2009-05-22 14:03:28 -070050}
51
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080052void Context::deinitEGL() {
Jason Sams803626f2011-04-06 17:52:23 -070053 mHal.funcs.shutdownGraphics(this);
Jason Sams71362202009-10-27 14:44:31 -070054}
55
Jason Samsa17af042010-11-17 15:29:32 -080056Context::PushState::PushState(Context *con) {
57 mRsc = con;
Jason Sams07078e32011-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 Samsa17af042010-11-17 15:29:32 -080065}
66
67Context::PushState::~PushState() {
Jason Sams07078e32011-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 Samsa17af042010-11-17 15:29:32 -080075}
76
Jason Sams71362202009-10-27 14:44:31 -070077
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080078uint32_t Context::runScript(Script *s) {
Jason Samsa17af042010-11-17 15:29:32 -080079 PushState(this);
Jason Samsda423d82009-06-09 12:15:30 -070080
Jason Samsf17bccc2010-05-28 18:23:22 -070081 uint32_t ret = s->run(this);
Jason Samsb0ec1b42009-07-28 12:02:16 -070082 return ret;
Jason Samsda423d82009-06-09 12:15:30 -070083}
84
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080085uint32_t Context::runRootScript() {
Jason Samsb9d5c572009-12-09 11:05:45 -080086 timerSet(RS_TIMER_SCRIPT);
Jason Sams516c3192009-10-06 13:58:47 -070087 mStateFragmentStore.mLast.clear();
Jason Sams5316b9e2011-09-13 15:41:01 -070088 watchdog.inRoot = true;
Jason Samsf17bccc2010-05-28 18:23:22 -070089 uint32_t ret = runScript(mRootScript.get());
Jason Sams5316b9e2011-09-13 15:41:01 -070090 watchdog.inRoot = false;
Jason Samsc7412b32009-10-14 15:43:53 -070091
Jason Sams9bee51c2009-08-05 13:57:03 -070092 return ret;
Jason Samsd19f10d2009-05-22 14:03:28 -070093}
94
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080095uint64_t Context::getTime() const {
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -070096#ifndef ANDROID_RS_SERIALIZE
Jason Samsf4d16062009-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 Sakhartchouk17a8a192011-06-03 10:18:01 -0700100#else
101 return 0;
102#endif //ANDROID_RS_SERIALIZE
Jason Samsf4d16062009-08-19 12:17:14 -0700103}
104
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800105void Context::timerReset() {
Jason Samsf4d16062009-08-19 12:17:14 -0700106 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
107 mTimers[ct] = 0;
108 }
109}
110
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800111void Context::timerInit() {
Jason Samsf4d16062009-08-19 12:17:14 -0700112 mTimeLast = getTime();
Jason Sams2525a812009-09-03 15:43:13 -0700113 mTimeFrame = mTimeLast;
114 mTimeLastFrame = mTimeLast;
Jason Samsf4d16062009-08-19 12:17:14 -0700115 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchouk76322af2010-10-05 13:23:55 -0700116 mAverageFPSFrameCount = 0;
117 mAverageFPSStartTime = mTimeLast;
118 mAverageFPS = 0;
Jason Samsf4d16062009-08-19 12:17:14 -0700119 timerReset();
120}
121
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800122void Context::timerFrame() {
Jason Sams2525a812009-09-03 15:43:13 -0700123 mTimeLastFrame = mTimeFrame;
124 mTimeFrame = getTime();
Alex Sakhartchouk76322af2010-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 Sakhartchouked9f2102010-11-09 17:00:54 -0800129 if (inverval >= averageFramerateInterval) {
Alex Sakhartchouk76322af2010-10-05 13:23:55 -0700130 inverval = inverval / 1000000;
131 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
132 mAverageFPSFrameCount = 0;
133 mAverageFPSStartTime = mTimeFrame;
134 }
Jason Sams2525a812009-09-03 15:43:13 -0700135}
136
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800137void Context::timerSet(Timers tm) {
Jason Samsf4d16062009-08-19 12:17:14 -0700138 uint64_t last = mTimeLast;
139 mTimeLast = getTime();
140 mTimers[mTimerActive] += mTimeLast - last;
141 mTimerActive = tm;
142}
143
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800144void Context::timerPrint() {
Jason Samsf4d16062009-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 Sams2525a812009-09-03 15:43:13 -0700149 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Samsb9d5c572009-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 Samsf4d16062009-08-19 12:17:14 -0700153
Jason Samsb9d5c572009-12-09 11:05:45 -0800154
155 if (props.mLogTimes) {
Steve Block71f2cf12011-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 Samsb9d5c572009-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 Sakhartchouk76322af2010-10-05 13:23:55 -0700161 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
162 mAverageFPS);
Jason Samsb9d5c572009-12-09 11:05:45 -0800163 }
Jason Samsf4d16062009-08-19 12:17:14 -0700164}
165
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800166bool Context::setupCheck() {
Jason Samsd081fff2010-09-16 18:18:29 -0700167
Jason Sams331bf9b2011-04-06 11:23:54 -0700168 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchouk407cae92011-05-06 14:59:45 -0700169 mFragment->setup(this, &mStateFragment);
Jason Sams331bf9b2011-04-06 11:23:54 -0700170 mRaster->setup(this, &mStateRaster);
Alex Sakhartchouk407cae92011-05-06 14:59:45 -0700171 mVertex->setup(this, &mStateVertex);
172 mFBOCache.setup(this);
Jason Sams156cce62010-03-03 13:03:18 -0800173 return true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700174}
175
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700176void Context::setupProgramStore() {
Jason Sams331bf9b2011-04-06 11:23:54 -0700177 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700178}
179
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800180static bool getProp(const char *str) {
Joe Onorato9ac2c662009-09-23 16:37:36 -0700181 char buf[PROPERTY_VALUE_MAX];
Jason Sams66b27712009-09-25 15:25:00 -0700182 property_get(str, buf, "0");
Joe Onorato9ac2c662009-09-23 16:37:36 -0700183 return 0 != strcmp(buf, "0");
184}
Jason Samsd19f10d2009-05-22 14:03:28 -0700185
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800186void Context::displayDebugStats() {
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700187 char buffer[128];
Alex Sakhartchouk76322af2010-10-05 13:23:55 -0700188 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchouk55e81982010-08-05 11:24:14 -0700189 float oldR, oldG, oldB, oldA;
190 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk10825a02010-10-05 11:33:27 -0700191 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700192
Alex Sakhartchouk20a93542011-03-17 13:49:38 -0700193 ObjectBaseRef<Font> lastFont(getFont());
194 setFont(NULL);
Alex Sakhartchouk10825a02010-10-05 11:33:27 -0700195 float shadowCol = 0.1f;
Alex Sakhartchouk55e81982010-08-05 11:24:14 -0700196 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchouk76322af2010-10-05 13:23:55 -0700197 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700198
Alex Sakhartchouk10825a02010-10-05 11:33:27 -0700199 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchouk76322af2010-10-05 13:23:55 -0700200 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700201
Alex Sakhartchouk20a93542011-03-17 13:49:38 -0700202 setFont(lastFont.get());
Alex Sakhartchouk55e81982010-08-05 11:24:14 -0700203 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700204}
205
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800206void * Context::threadProc(void *vrsc) {
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700207 Context *rsc = static_cast<Context *>(vrsc);
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700208#ifndef ANDROID_RS_SERIALIZE
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700209 rsc->mNativeThreadId = gettid();
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700210 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
211 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700212#endif //ANDROID_RS_SERIALIZE
Jason Samsbe8ac6a2011-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 Onorato9ac2c662009-09-23 16:37:36 -0700220
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700221 if (!rsdHalInit(rsc, 0, 0)) {
Jason Sams26985362011-05-03 15:01:58 -0700222 rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed initializing GL");
Steve Block3762c312012-01-06 19:20:56 +0000223 ALOGE("Hal init failed");
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700224 return NULL;
225 }
226 rsc->mHal.funcs.setPriority(rsc, rsc->mThreadPriority);
Jason Sams462d11b2009-06-19 16:03:18 -0700227
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700228 if (rsc->mIsGraphicsContext) {
Jason Sams26985362011-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 Samsbe8ac6a2011-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 Sakhartchouk117abdb2011-08-16 13:09:46 -0700244 rsc->mStateSampler.init(rsc);
Alex Sakhartchouk2f6964f2011-05-13 14:53:34 -0700245 rsc->mFBOCache.init(rsc);
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700246 }
Jason Sams9c54bdb2009-06-17 16:52:59 -0700247
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700248 rsc->mRunning = true;
Jason Sams4c2e4c82012-02-07 15:32:08 -0800249 if (!rsc->mIsGraphicsContext) {
250 while (!rsc->mExit) {
Jason Sams87e27212012-02-09 14:36:14 -0800251 rsc->mIO.playCoreCommands(rsc, -1);
Jason Samsbfc78912011-08-12 15:05:15 -0700252 }
Jason Sams4c2e4c82012-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 Samsbfc78912011-08-12 15:05:15 -0700260
Jason Sams4c2e4c82012-02-07 15:32:08 -0800261 bool drawOnce = false;
262 while (!rsc->mExit) {
263 rsc->timerSet(RS_TIMER_IDLE);
Jason Samsd19f10d2009-05-22 14:03:28 -0700264
Jason Sams4c2e4c82012-02-07 15:32:08 -0800265#ifndef ANDROID_RS_SERIALIZE
Jason Samsf7795e02012-02-17 16:59:50 -0800266 if (!rsc->mRootScript.get() || !rsc->mHasSurface || rsc->mPaused) {
267 targetRate = 0;
268 }
269
Jason Sams4c2e4c82012-02-07 15:32:08 -0800270 if (vsyncRate != targetRate) {
271 displayEvent.setVsyncRate(targetRate);
272 vsyncRate = targetRate;
273 }
274 if (targetRate) {
Jason Sams87e27212012-02-09 14:36:14 -0800275 drawOnce |= rsc->mIO.playCoreCommands(rsc, displayEvent.getFd());
Jason Sams4c2e4c82012-02-07 15:32:08 -0800276 while (displayEvent.getEvents(eventBuffer, 1) != 0) {
277 //ALOGE("vs2 time past %lld", (rsc->getTime() - eventBuffer[0].header.timestamp) / 1000000);
278 }
279 } else
280#endif
281 {
Jason Sams87e27212012-02-09 14:36:14 -0800282 drawOnce |= rsc->mIO.playCoreCommands(rsc, -1);
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700283 }
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700284
Jason Sams4c2e4c82012-02-07 15:32:08 -0800285 if ((rsc->mRootScript.get() != NULL) && rsc->mHasSurface &&
286 (targetRate || drawOnce) && !rsc->mPaused) {
287
288 drawOnce = false;
289 targetRate = ((rsc->runRootScript() + 15) / 16);
290
291 if (rsc->props.mLogVisual) {
292 rsc->displayDebugStats();
293 }
294
295 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
296 rsc->mHal.funcs.swap(rsc);
297 rsc->timerFrame();
298 rsc->timerSet(RS_TIMER_INTERNAL);
299 rsc->timerPrint();
300 rsc->timerReset();
301 }
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700302 }
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700303 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700304
Steve Block71f2cf12011-10-20 11:56:00 +0100305 ALOGV("%p RS Thread exiting", rsc);
Jason Samsa9e7a052009-09-25 14:51:22 -0700306
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700307 if (rsc->mIsGraphicsContext) {
308 pthread_mutex_lock(&gInitMutex);
309 rsc->deinitEGL();
310 pthread_mutex_unlock(&gInitMutex);
311 }
Jason Sams71362202009-10-27 14:44:31 -0700312
Steve Block71f2cf12011-10-20 11:56:00 +0100313 ALOGV("%p RS Thread exited", rsc);
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700314 return NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700315}
316
Jason Sams5c68a712010-12-24 14:38:39 -0800317void Context::destroyWorkerThreadResources() {
Steve Block71f2cf12011-10-20 11:56:00 +0100318 //ALOGV("destroyWorkerThreadResources 1");
Jason Sams38f8d9d2011-01-27 00:14:13 -0800319 ObjectBase::zeroAllUserRef(this);
Jason Sams5c68a712010-12-24 14:38:39 -0800320 if (mIsGraphicsContext) {
321 mRaster.clear();
322 mFragment.clear();
323 mVertex.clear();
324 mFragmentStore.clear();
325 mFont.clear();
326 mRootScript.clear();
327 mStateRaster.deinit(this);
328 mStateVertex.deinit(this);
329 mStateFragment.deinit(this);
330 mStateFragmentStore.deinit(this);
331 mStateFont.deinit(this);
Alex Sakhartchouk117abdb2011-08-16 13:09:46 -0700332 mStateSampler.deinit(this);
Alex Sakhartchouk2f6964f2011-05-13 14:53:34 -0700333 mFBOCache.deinit(this);
Jason Sams5c68a712010-12-24 14:38:39 -0800334 }
Jason Sams777ec262011-08-18 18:01:33 -0700335 ObjectBase::freeAllChildren(this);
Jason Sams84035ff2011-01-09 16:09:51 -0800336 mExit = true;
Jason Sams4c2e4c82012-02-07 15:32:08 -0800337 //ALOGV("destroyWorkerThreadResources 2");
Jason Sams5c68a712010-12-24 14:38:39 -0800338}
339
Jason Sams5316b9e2011-09-13 15:41:01 -0700340void Context::printWatchdogInfo(void *ctx) {
341 Context *rsc = (Context *)ctx;
Jason Sams43b57ec2011-10-13 16:05:27 -0700342 if (rsc->watchdog.command && rsc->watchdog.file) {
Steve Block3762c312012-01-06 19:20:56 +0000343 ALOGE("RS watchdog timeout: %i %s line %i %s", rsc->watchdog.inRoot,
Jason Sams43b57ec2011-10-13 16:05:27 -0700344 rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file);
345 } else {
Steve Block3762c312012-01-06 19:20:56 +0000346 ALOGE("RS watchdog timeout: %i", rsc->watchdog.inRoot);
Jason Sams43b57ec2011-10-13 16:05:27 -0700347 }
Jason Sams5316b9e2011-09-13 15:41:01 -0700348}
349
350
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800351void Context::setPriority(int32_t p) {
Jason Sams7d787b42009-11-15 12:14:26 -0800352 // Note: If we put this in the proper "background" policy
353 // the wallpapers can become completly unresponsive at times.
354 // This is probably not what we want for something the user is actively
355 // looking at.
Jason Samsb9d5c572009-12-09 11:05:45 -0800356 mThreadPriority = p;
Jason Sams7d787b42009-11-15 12:14:26 -0800357#if 0
358 SchedPolicy pol = SP_FOREGROUND;
359 if (p > 0) {
360 pol = SP_BACKGROUND;
361 }
362 if (!set_sched_policy(mNativeThreadId, pol)) {
363 // success; reset the priority as well
364 }
365#else
Jason Sams8e6c17f2010-07-19 15:38:19 -0700366 setpriority(PRIO_PROCESS, mNativeThreadId, p);
Jason Sams7d787b42009-11-15 12:14:26 -0800367#endif
Jason Sams17801f12012-01-12 14:22:21 -0800368 mHal.funcs.setPriority(this, mThreadPriority);
Jason Sams7d787b42009-11-15 12:14:26 -0800369}
370
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800371Context::Context() {
Jason Samsd5f06302010-11-03 14:27:11 -0700372 mDev = NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700373 mRunning = false;
374 mExit = false;
Jason Sams65e7aa52009-09-24 17:38:20 -0700375 mPaused = false;
Jason Samsa9e7a052009-09-25 14:51:22 -0700376 mObjHead = NULL;
Jason Sams156cce62010-03-03 13:03:18 -0800377 mError = RS_ERROR_NONE;
Stephen Hines4382467a2011-08-01 15:02:34 -0700378 mTargetSdkVersion = 14;
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700379 mDPI = 96;
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700380 mIsContextLite = false;
Stephen Hinesa0491842011-10-31 14:07:54 -0700381 memset(&watchdog, 0, sizeof(watchdog));
Jason Samsd5f06302010-11-03 14:27:11 -0700382}
383
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800384Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Samsd5f06302010-11-03 14:27:11 -0700385 Context * rsc = new Context();
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700386
Jason Samsd5f06302010-11-03 14:27:11 -0700387 if (!rsc->initContext(dev, sc)) {
388 delete rsc;
389 return NULL;
390 }
391 return rsc;
392}
393
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700394Context * Context::createContextLite() {
395 Context * rsc = new Context();
396 rsc->mIsContextLite = true;
397 return rsc;
398}
399
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800400bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Samsd5f06302010-11-03 14:27:11 -0700401 pthread_mutex_lock(&gInitMutex);
402
Jason Samsedbfabd2011-05-17 15:01:29 -0700403 mIO.init();
Jason Sams4c2e4c82012-02-07 15:32:08 -0800404 mIO.setTimeoutCallback(printWatchdogInfo, this, 2e9);
Jason Samsedbfabd2011-05-17 15:01:29 -0700405
Jason Samsd5f06302010-11-03 14:27:11 -0700406 dev->addContext(this);
407 mDev = dev;
Jason Sams11c8af92010-10-13 15:31:10 -0700408 if (sc) {
409 mUserSurfaceConfig = *sc;
410 } else {
411 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
412 }
Jason Sams156cce62010-03-03 13:03:18 -0800413
Jason Sams11c8af92010-10-13 15:31:10 -0700414 mIsGraphicsContext = sc != NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700415
Jason Sams8ad00102009-06-04 14:35:01 -0700416 int status;
417 pthread_attr_t threadAttr;
418
Jason Sams41c19db92009-10-15 16:47:31 -0700419 pthread_mutex_unlock(&gInitMutex);
420
421 // Global init done at this point.
Jason Sams462d11b2009-06-19 16:03:18 -0700422
Jason Sams8ad00102009-06-04 14:35:01 -0700423 status = pthread_attr_init(&threadAttr);
424 if (status) {
Steve Block3762c312012-01-06 19:20:56 +0000425 ALOGE("Failed to init thread attribute.");
Jason Samsd5f06302010-11-03 14:27:11 -0700426 return false;
Jason Sams8ad00102009-06-04 14:35:01 -0700427 }
428
Alex Sakhartchouk6c72eec2011-05-17 12:32:47 -0700429 mHasSurface = false;
Jason Samsf29ca502009-06-23 12:22:47 -0700430
Jason Samsf4d16062009-08-19 12:17:14 -0700431 timerInit();
Jason Samsd3f2eaf2009-09-24 15:42:52 -0700432 timerSet(RS_TIMER_INTERNAL);
Jason Sams730ee652009-08-18 17:07:09 -0700433
Jason Sams8ad00102009-06-04 14:35:01 -0700434 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Samsd19f10d2009-05-22 14:03:28 -0700435 if (status) {
Steve Block3762c312012-01-06 19:20:56 +0000436 ALOGE("Failed to start rs context thread.");
Jason Samsd5f06302010-11-03 14:27:11 -0700437 return false;
Jason Sams8e6c17f2010-07-19 15:38:19 -0700438 }
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800439 while (!mRunning && (mError == RS_ERROR_NONE)) {
Jason Samsc7f4e412010-07-20 15:09:00 -0700440 usleep(100);
441 }
442
Jason Samsd5f06302010-11-03 14:27:11 -0700443 if (mError != RS_ERROR_NONE) {
Steve Block3762c312012-01-06 19:20:56 +0000444 ALOGE("Errors during thread init");
Jason Samsd5f06302010-11-03 14:27:11 -0700445 return false;
446 }
447
Jason Sams8ad00102009-06-04 14:35:01 -0700448 pthread_attr_destroy(&threadAttr);
Jason Samsd5f06302010-11-03 14:27:11 -0700449 return true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700450}
451
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800452Context::~Context() {
Steve Block71f2cf12011-10-20 11:56:00 +0100453 ALOGV("%p Context::~Context", this);
Jason Sams84035ff2011-01-09 16:09:51 -0800454
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700455 if (!mIsContextLite) {
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700456 mPaused = false;
457 void *res;
Jason Samsd19f10d2009-05-22 14:03:28 -0700458
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700459 mIO.shutdown();
460 int status = pthread_join(mThreadId, &res);
Jason Sams4c2e4c82012-02-07 15:32:08 -0800461 rsAssert(mExit);
Jason Samsd19f10d2009-05-22 14:03:28 -0700462
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700463 if (mHal.funcs.shutdownDriver) {
464 mHal.funcs.shutdownDriver(this);
465 }
466
467 // Global structure cleanup.
468 pthread_mutex_lock(&gInitMutex);
469 if (mDev) {
470 mDev->removeContext(this);
471 mDev = NULL;
472 }
473 pthread_mutex_unlock(&gInitMutex);
Jason Sams03855bb2011-01-25 00:26:25 -0800474 }
Steve Block71f2cf12011-10-20 11:56:00 +0100475 ALOGV("%p Context::~Context done", this);
Jason Samsd19f10d2009-05-22 14:03:28 -0700476}
477
Alex Sakhartchouk6c72eec2011-05-17 12:32:47 -0700478void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) {
Jason Sams704ff642010-02-09 16:05:07 -0800479 rsAssert(mIsGraphicsContext);
Jason Sams803626f2011-04-06 17:52:23 -0700480 mHal.funcs.setSurface(this, w, h, sur);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800481
Alex Sakhartchouk6c72eec2011-05-17 12:32:47 -0700482 mHasSurface = sur != NULL;
Jason Sams803626f2011-04-06 17:52:23 -0700483 mWidth = w;
484 mHeight = h;
Jason Sams3bc47d42009-11-12 15:10:25 -0800485
Jason Sams803626f2011-04-06 17:52:23 -0700486 if (mWidth && mHeight) {
Jason Samsf603d212010-05-14 15:30:29 -0700487 mStateVertex.updateSize(this);
Alex Sakhartchouk10ed049352011-07-19 17:50:29 -0700488 mFBOCache.updateSize();
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800489 }
490}
491
Alex Sakhartchouk2d8ef492011-11-16 12:22:10 -0800492uint32_t Context::getCurrentSurfaceWidth() const {
493 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
494 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
495 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimX();
496 }
497 }
498 if (mFBOCache.mHal.state.depthTarget != NULL) {
499 return mFBOCache.mHal.state.depthTarget->getType()->getDimX();
500 }
501 return mWidth;
502}
503
504uint32_t Context::getCurrentSurfaceHeight() const {
505 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
506 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
507 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimY();
508 }
509 }
510 if (mFBOCache.mHal.state.depthTarget != NULL) {
511 return mFBOCache.mHal.state.depthTarget->getType()->getDimY();
512 }
513 return mHeight;
514}
515
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800516void Context::pause() {
Jason Sams704ff642010-02-09 16:05:07 -0800517 rsAssert(mIsGraphicsContext);
Jason Sams65e7aa52009-09-24 17:38:20 -0700518 mPaused = true;
519}
520
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800521void Context::resume() {
Jason Sams704ff642010-02-09 16:05:07 -0800522 rsAssert(mIsGraphicsContext);
Jason Sams65e7aa52009-09-24 17:38:20 -0700523 mPaused = false;
524}
525
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800526void Context::setRootScript(Script *s) {
Jason Sams704ff642010-02-09 16:05:07 -0800527 rsAssert(mIsGraphicsContext);
Jason Samsd19f10d2009-05-22 14:03:28 -0700528 mRootScript.set(s);
529}
530
Jason Samsa17af042010-11-17 15:29:32 -0800531void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams704ff642010-02-09 16:05:07 -0800532 rsAssert(mIsGraphicsContext);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700533 if (pfs == NULL) {
534 mFragmentStore.set(mStateFragmentStore.mDefault);
535 } else {
536 mFragmentStore.set(pfs);
537 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700538}
539
Jason Samsa17af042010-11-17 15:29:32 -0800540void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams704ff642010-02-09 16:05:07 -0800541 rsAssert(mIsGraphicsContext);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700542 if (pf == NULL) {
543 mFragment.set(mStateFragment.mDefault);
544 } else {
545 mFragment.set(pf);
546 }
Jason Sams9bee51c2009-08-05 13:57:03 -0700547}
548
Jason Samsa17af042010-11-17 15:29:32 -0800549void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams704ff642010-02-09 16:05:07 -0800550 rsAssert(mIsGraphicsContext);
Jason Samsebfb4362009-09-23 13:57:02 -0700551 if (pr == NULL) {
552 mRaster.set(mStateRaster.mDefault);
553 } else {
554 mRaster.set(pr);
555 }
556}
557
Jason Samsa17af042010-11-17 15:29:32 -0800558void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams704ff642010-02-09 16:05:07 -0800559 rsAssert(mIsGraphicsContext);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700560 if (pv == NULL) {
561 mVertex.set(mStateVertex.mDefault);
562 } else {
563 mVertex.set(pv);
564 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700565}
566
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800567void Context::setFont(Font *f) {
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700568 rsAssert(mIsGraphicsContext);
569 if (f == NULL) {
570 mFont.set(mStateFont.mDefault);
571 } else {
572 mFont.set(f);
573 }
574}
575
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800576void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Sams3eaa3382009-06-10 15:04:38 -0700577 rsAssert(!obj->getName());
Jason Samsd5680f92009-06-10 18:39:40 -0700578 obj->setName(name, len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700579 mNames.add(obj);
580}
581
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800582void Context::removeName(ObjectBase *obj) {
583 for (size_t ct=0; ct < mNames.size(); ct++) {
Jason Sams3eaa3382009-06-10 15:04:38 -0700584 if (obj == mNames[ct]) {
585 mNames.removeAt(ct);
586 return;
587 }
588 }
589}
590
Jason Samsedbfabd2011-05-17 15:01:29 -0700591RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) {
592 return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID);
Jason Sams1c415172010-11-08 17:06:46 -0800593}
594
Jason Samsedbfabd2011-05-17 15:01:29 -0700595RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) {
596 return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen);
Jason Sams516c3192009-10-06 13:58:47 -0700597}
598
Jason Samsadd9d962010-11-22 16:20:16 -0800599bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
600 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Samsedbfabd2011-05-17 15:01:29 -0700601
602 return mIO.sendToClient(cmdID, subID, data, len, waitForSpace);
Jason Sams516c3192009-10-06 13:58:47 -0700603}
604
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800605void Context::initToClient() {
606 while (!mRunning) {
Jason Sams516c3192009-10-06 13:58:47 -0700607 usleep(100);
608 }
609}
610
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800611void Context::deinitToClient() {
Jason Samsedbfabd2011-05-17 15:01:29 -0700612 mIO.clientShutdown();
Jason Sams516c3192009-10-06 13:58:47 -0700613}
Jason Sams730ee652009-08-18 17:07:09 -0700614
Jason Samsadd9d962010-11-22 16:20:16 -0800615void Context::setError(RsError e, const char *msg) const {
Jason Sams156cce62010-03-03 13:03:18 -0800616 mError = e;
Jason Sams1c415172010-11-08 17:06:46 -0800617 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Sams156cce62010-03-03 13:03:18 -0800618}
619
620
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800621void Context::dumpDebug() const {
Steve Block3762c312012-01-06 19:20:56 +0000622 ALOGE("RS Context debug %p", this);
623 ALOGE("RS Context debug");
Jason Sams9dab6672009-11-24 12:26:35 -0800624
Steve Block3762c312012-01-06 19:20:56 +0000625 ALOGE(" RS width %i, height %i", mWidth, mHeight);
626 ALOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
627 ALOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId);
Jason Sams9dab6672009-11-24 12:26:35 -0800628}
Jason Samsd5680f92009-06-10 18:39:40 -0700629
Jason Samsd19f10d2009-05-22 14:03:28 -0700630///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa09f11d2009-06-04 17:58:03 -0700631//
Jason Samsd19f10d2009-05-22 14:03:28 -0700632
633namespace android {
634namespace renderscript {
635
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800636void rsi_ContextFinish(Context *rsc) {
Jason Sams96ed4cf2010-06-15 12:15:57 -0700637}
Jason Samsd19f10d2009-05-22 14:03:28 -0700638
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800639void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700640 Script *s = static_cast<Script *>(vs);
641 rsc->setRootScript(s);
642}
643
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800644void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700645 Sampler *s = static_cast<Sampler *>(vs);
646
647 if (slot > RS_MAX_SAMPLER_SLOT) {
Steve Block3762c312012-01-06 19:20:56 +0000648 ALOGE("Invalid sampler slot");
Jason Samsd19f10d2009-05-22 14:03:28 -0700649 return;
650 }
651
652 s->bindToContext(&rsc->mStateSampler, slot);
653}
654
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800655void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Sams54db59c2010-05-13 18:30:11 -0700656 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Samsa17af042010-11-17 15:29:32 -0800657 rsc->setProgramStore(pfs);
Jason Samsd19f10d2009-05-22 14:03:28 -0700658}
659
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800660void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700661 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Samsa17af042010-11-17 15:29:32 -0800662 rsc->setProgramFragment(pf);
Jason Samsd19f10d2009-05-22 14:03:28 -0700663}
664
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800665void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Samsebfb4362009-09-23 13:57:02 -0700666 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Samsa17af042010-11-17 15:29:32 -0800667 rsc->setProgramRaster(pr);
Jason Samsebfb4362009-09-23 13:57:02 -0700668}
669
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800670void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700671 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Samsa17af042010-11-17 15:29:32 -0800672 rsc->setProgramVertex(pv);
Jason Samsd19f10d2009-05-22 14:03:28 -0700673}
674
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800675void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700676 Font *font = static_cast<Font *>(vfont);
677 rsc->setFont(font);
678}
679
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700680void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) {
Jason Sams3eaa3382009-06-10 15:04:38 -0700681 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -0700682 rsc->assignName(ob, name, name_length);
Jason Sams3eaa3382009-06-10 15:04:38 -0700683}
Jason Samsd19f10d2009-05-22 14:03:28 -0700684
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800685void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams3b9c52a2010-10-14 17:48:46 -0700686 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams7ce033d2009-08-18 14:14:24 -0700687 rsc->removeName(ob);
Jason Sams07ae4062009-08-27 20:23:34 -0700688 ob->decUserRef();
Jason Sams7ce033d2009-08-18 14:14:24 -0700689}
690
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800691void rsi_ContextPause(Context *rsc) {
Jason Sams65e7aa52009-09-24 17:38:20 -0700692 rsc->pause();
693}
694
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800695void rsi_ContextResume(Context *rsc) {
Jason Sams65e7aa52009-09-24 17:38:20 -0700696 rsc->resume();
697}
698
Alex Sakhartchouk6c72eec2011-05-17 12:32:47 -0700699void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
Mathias Agopian128ce4b2010-02-12 14:04:35 -0800700 rsc->setSurface(w, h, sur);
Jason Sams3bc47d42009-11-12 15:10:25 -0800701}
702
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800703void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams7d787b42009-11-15 12:14:26 -0800704 rsc->setPriority(p);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800705}
706
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800707void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Sams715333b2009-11-17 17:26:46 -0800708 ObjectBase::dumpAll(rsc);
709}
710
Jason Sams5c68a712010-12-24 14:38:39 -0800711void rsi_ContextDestroyWorker(Context *rsc) {
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700712 rsc->destroyWorkerThreadResources();
Jason Sams5c68a712010-12-24 14:38:39 -0800713}
714
Jason Samsc5765372011-04-28 18:26:48 -0700715void rsi_ContextDestroy(Context *rsc) {
Steve Block71f2cf12011-10-20 11:56:00 +0100716 ALOGV("%p rsContextDestroy", rsc);
Jason Sams5c68a712010-12-24 14:38:39 -0800717 rsContextDestroyWorker(rsc);
Jason Sams546f01b2010-12-09 12:19:46 -0800718 delete rsc;
Steve Block71f2cf12011-10-20 11:56:00 +0100719 ALOGV("%p rsContextDestroy done", rsc);
Jason Sams546f01b2010-12-09 12:19:46 -0800720}
721
Jason Samsd19f10d2009-05-22 14:03:28 -0700722
Jason Samsc5765372011-04-28 18:26:48 -0700723RsMessageToClientType rsi_ContextPeekMessage(Context *rsc,
Jason Sams65bdaf12011-04-26 14:50:00 -0700724 size_t * receiveLen, size_t receiveLen_length,
Jason Samsedbfabd2011-05-17 15:01:29 -0700725 uint32_t * subID, size_t subID_length) {
726 return rsc->peekMessageToClient(receiveLen, subID);
Jason Sams1c415172010-11-08 17:06:46 -0800727}
728
Jason Samsc5765372011-04-28 18:26:48 -0700729RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length,
Jason Sams65bdaf12011-04-26 14:50:00 -0700730 size_t * receiveLen, size_t receiveLen_length,
Jason Samsedbfabd2011-05-17 15:01:29 -0700731 uint32_t * subID, size_t subID_length) {
Jason Sams65bdaf12011-04-26 14:50:00 -0700732 rsAssert(subID_length == sizeof(uint32_t));
733 rsAssert(receiveLen_length == sizeof(size_t));
Jason Samsedbfabd2011-05-17 15:01:29 -0700734 return rsc->getMessageToClient(data, receiveLen, subID, data_length);
Jason Sams516c3192009-10-06 13:58:47 -0700735}
736
Jason Samsc5765372011-04-28 18:26:48 -0700737void rsi_ContextInitToClient(Context *rsc) {
Jason Sams516c3192009-10-06 13:58:47 -0700738 rsc->initToClient();
739}
740
Jason Samsc5765372011-04-28 18:26:48 -0700741void rsi_ContextDeinitToClient(Context *rsc) {
Jason Sams516c3192009-10-06 13:58:47 -0700742 rsc->deinitToClient();
743}
744
Jason Samsc5765372011-04-28 18:26:48 -0700745}
746}
747
Stephen Hines4382467a2011-08-01 15:02:34 -0700748RsContext rsContextCreate(RsDevice vdev, uint32_t version,
749 uint32_t sdkVersion) {
Steve Block71f2cf12011-10-20 11:56:00 +0100750 ALOGV("rsContextCreate dev=%p", vdev);
Jason Samsd9d37cc2011-05-18 17:36:02 -0700751 Device * dev = static_cast<Device *>(vdev);
752 Context *rsc = Context::createContext(dev, NULL);
Stephen Hines4382467a2011-08-01 15:02:34 -0700753 if (rsc) {
754 rsc->setTargetSdkVersion(sdkVersion);
755 }
Jason Samsd9d37cc2011-05-18 17:36:02 -0700756 return rsc;
757}
758
759RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
Stephen Hines4382467a2011-08-01 15:02:34 -0700760 uint32_t sdkVersion, RsSurfaceConfig sc,
761 uint32_t dpi) {
Steve Block71f2cf12011-10-20 11:56:00 +0100762 ALOGV("rsContextCreateGL dev=%p", vdev);
Jason Samsd9d37cc2011-05-18 17:36:02 -0700763 Device * dev = static_cast<Device *>(vdev);
764 Context *rsc = Context::createContext(dev, &sc);
Jason Samsfb06b7a2011-07-13 16:09:42 -0700765 if (rsc) {
Stephen Hines4382467a2011-08-01 15:02:34 -0700766 rsc->setTargetSdkVersion(sdkVersion);
Jason Samsfb06b7a2011-07-13 16:09:42 -0700767 rsc->setDPI(dpi);
768 }
Steve Block71f2cf12011-10-20 11:56:00 +0100769 ALOGV("%p rsContextCreateGL ret", rsc);
Jason Samsd9d37cc2011-05-18 17:36:02 -0700770 return rsc;
771}
772
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700773// Only to be called at a3d load time, before object is visible to user
774// not thread safe
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800775void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700776 ObjectBase *ob = static_cast<ObjectBase *>(obj);
777 (*name) = ob->getName();
778}