blob: adaefc6017bf3ec0e1d4d969c2fc28a47cddac3e [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
266 if (vsyncRate != targetRate) {
267 displayEvent.setVsyncRate(targetRate);
268 vsyncRate = targetRate;
269 }
270 if (targetRate) {
Jason Sams87e27212012-02-09 14:36:14 -0800271 drawOnce |= rsc->mIO.playCoreCommands(rsc, displayEvent.getFd());
Jason Sams4c2e4c82012-02-07 15:32:08 -0800272 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 {
Jason Sams87e27212012-02-09 14:36:14 -0800278 drawOnce |= rsc->mIO.playCoreCommands(rsc, -1);
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700279 }
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700280
Jason Sams4c2e4c82012-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 Samsbe8ac6a2011-04-21 11:46:50 -0700298 }
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700299 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700300
Steve Block71f2cf12011-10-20 11:56:00 +0100301 ALOGV("%p RS Thread exiting", rsc);
Jason Samsa9e7a052009-09-25 14:51:22 -0700302
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700303 if (rsc->mIsGraphicsContext) {
304 pthread_mutex_lock(&gInitMutex);
305 rsc->deinitEGL();
306 pthread_mutex_unlock(&gInitMutex);
307 }
Jason Sams71362202009-10-27 14:44:31 -0700308
Steve Block71f2cf12011-10-20 11:56:00 +0100309 ALOGV("%p RS Thread exited", rsc);
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700310 return NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700311}
312
Jason Sams5c68a712010-12-24 14:38:39 -0800313void Context::destroyWorkerThreadResources() {
Steve Block71f2cf12011-10-20 11:56:00 +0100314 //ALOGV("destroyWorkerThreadResources 1");
Jason Sams38f8d9d2011-01-27 00:14:13 -0800315 ObjectBase::zeroAllUserRef(this);
Jason Sams5c68a712010-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 Sakhartchouk117abdb2011-08-16 13:09:46 -0700328 mStateSampler.deinit(this);
Alex Sakhartchouk2f6964f2011-05-13 14:53:34 -0700329 mFBOCache.deinit(this);
Jason Sams5c68a712010-12-24 14:38:39 -0800330 }
Jason Sams777ec262011-08-18 18:01:33 -0700331 ObjectBase::freeAllChildren(this);
Jason Sams84035ff2011-01-09 16:09:51 -0800332 mExit = true;
Jason Sams4c2e4c82012-02-07 15:32:08 -0800333 //ALOGV("destroyWorkerThreadResources 2");
Jason Sams5c68a712010-12-24 14:38:39 -0800334}
335
Jason Sams5316b9e2011-09-13 15:41:01 -0700336void Context::printWatchdogInfo(void *ctx) {
337 Context *rsc = (Context *)ctx;
Jason Sams43b57ec2011-10-13 16:05:27 -0700338 if (rsc->watchdog.command && rsc->watchdog.file) {
Steve Block3762c312012-01-06 19:20:56 +0000339 ALOGE("RS watchdog timeout: %i %s line %i %s", rsc->watchdog.inRoot,
Jason Sams43b57ec2011-10-13 16:05:27 -0700340 rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file);
341 } else {
Steve Block3762c312012-01-06 19:20:56 +0000342 ALOGE("RS watchdog timeout: %i", rsc->watchdog.inRoot);
Jason Sams43b57ec2011-10-13 16:05:27 -0700343 }
Jason Sams5316b9e2011-09-13 15:41:01 -0700344}
345
346
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800347void Context::setPriority(int32_t p) {
Jason Sams7d787b42009-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 Samsb9d5c572009-12-09 11:05:45 -0800352 mThreadPriority = p;
Jason Sams7d787b42009-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 Sams8e6c17f2010-07-19 15:38:19 -0700362 setpriority(PRIO_PROCESS, mNativeThreadId, p);
Jason Sams7d787b42009-11-15 12:14:26 -0800363#endif
Jason Sams17801f12012-01-12 14:22:21 -0800364 mHal.funcs.setPriority(this, mThreadPriority);
Jason Sams7d787b42009-11-15 12:14:26 -0800365}
366
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800367Context::Context() {
Jason Samsd5f06302010-11-03 14:27:11 -0700368 mDev = NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700369 mRunning = false;
370 mExit = false;
Jason Sams65e7aa52009-09-24 17:38:20 -0700371 mPaused = false;
Jason Samsa9e7a052009-09-25 14:51:22 -0700372 mObjHead = NULL;
Jason Sams156cce62010-03-03 13:03:18 -0800373 mError = RS_ERROR_NONE;
Stephen Hines4382467a2011-08-01 15:02:34 -0700374 mTargetSdkVersion = 14;
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700375 mDPI = 96;
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700376 mIsContextLite = false;
Stephen Hinesa0491842011-10-31 14:07:54 -0700377 memset(&watchdog, 0, sizeof(watchdog));
Jason Samsd5f06302010-11-03 14:27:11 -0700378}
379
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800380Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Samsd5f06302010-11-03 14:27:11 -0700381 Context * rsc = new Context();
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700382
Jason Samsd5f06302010-11-03 14:27:11 -0700383 if (!rsc->initContext(dev, sc)) {
384 delete rsc;
385 return NULL;
386 }
387 return rsc;
388}
389
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700390Context * Context::createContextLite() {
391 Context * rsc = new Context();
392 rsc->mIsContextLite = true;
393 return rsc;
394}
395
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800396bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Samsd5f06302010-11-03 14:27:11 -0700397 pthread_mutex_lock(&gInitMutex);
398
Jason Samsedbfabd2011-05-17 15:01:29 -0700399 mIO.init();
Jason Sams4c2e4c82012-02-07 15:32:08 -0800400 mIO.setTimeoutCallback(printWatchdogInfo, this, 2e9);
Jason Samsedbfabd2011-05-17 15:01:29 -0700401
Jason Samsd5f06302010-11-03 14:27:11 -0700402 dev->addContext(this);
403 mDev = dev;
Jason Sams11c8af92010-10-13 15:31:10 -0700404 if (sc) {
405 mUserSurfaceConfig = *sc;
406 } else {
407 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
408 }
Jason Sams156cce62010-03-03 13:03:18 -0800409
Jason Sams11c8af92010-10-13 15:31:10 -0700410 mIsGraphicsContext = sc != NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700411
Jason Sams8ad00102009-06-04 14:35:01 -0700412 int status;
413 pthread_attr_t threadAttr;
414
Jason Sams41c19db92009-10-15 16:47:31 -0700415 pthread_mutex_unlock(&gInitMutex);
416
417 // Global init done at this point.
Jason Sams462d11b2009-06-19 16:03:18 -0700418
Jason Sams8ad00102009-06-04 14:35:01 -0700419 status = pthread_attr_init(&threadAttr);
420 if (status) {
Steve Block3762c312012-01-06 19:20:56 +0000421 ALOGE("Failed to init thread attribute.");
Jason Samsd5f06302010-11-03 14:27:11 -0700422 return false;
Jason Sams8ad00102009-06-04 14:35:01 -0700423 }
424
Alex Sakhartchouk6c72eec2011-05-17 12:32:47 -0700425 mHasSurface = false;
Jason Samsf29ca502009-06-23 12:22:47 -0700426
Jason Samsf4d16062009-08-19 12:17:14 -0700427 timerInit();
Jason Samsd3f2eaf2009-09-24 15:42:52 -0700428 timerSet(RS_TIMER_INTERNAL);
Jason Sams730ee652009-08-18 17:07:09 -0700429
Jason Sams8ad00102009-06-04 14:35:01 -0700430 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Samsd19f10d2009-05-22 14:03:28 -0700431 if (status) {
Steve Block3762c312012-01-06 19:20:56 +0000432 ALOGE("Failed to start rs context thread.");
Jason Samsd5f06302010-11-03 14:27:11 -0700433 return false;
Jason Sams8e6c17f2010-07-19 15:38:19 -0700434 }
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800435 while (!mRunning && (mError == RS_ERROR_NONE)) {
Jason Samsc7f4e412010-07-20 15:09:00 -0700436 usleep(100);
437 }
438
Jason Samsd5f06302010-11-03 14:27:11 -0700439 if (mError != RS_ERROR_NONE) {
Steve Block3762c312012-01-06 19:20:56 +0000440 ALOGE("Errors during thread init");
Jason Samsd5f06302010-11-03 14:27:11 -0700441 return false;
442 }
443
Jason Sams8ad00102009-06-04 14:35:01 -0700444 pthread_attr_destroy(&threadAttr);
Jason Samsd5f06302010-11-03 14:27:11 -0700445 return true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700446}
447
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800448Context::~Context() {
Steve Block71f2cf12011-10-20 11:56:00 +0100449 ALOGV("%p Context::~Context", this);
Jason Sams84035ff2011-01-09 16:09:51 -0800450
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700451 if (!mIsContextLite) {
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700452 mPaused = false;
453 void *res;
Jason Samsd19f10d2009-05-22 14:03:28 -0700454
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700455 mIO.shutdown();
456 int status = pthread_join(mThreadId, &res);
Jason Sams4c2e4c82012-02-07 15:32:08 -0800457 rsAssert(mExit);
Jason Samsd19f10d2009-05-22 14:03:28 -0700458
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700459 if (mHal.funcs.shutdownDriver) {
460 mHal.funcs.shutdownDriver(this);
461 }
462
463 // Global structure cleanup.
464 pthread_mutex_lock(&gInitMutex);
465 if (mDev) {
466 mDev->removeContext(this);
467 mDev = NULL;
468 }
469 pthread_mutex_unlock(&gInitMutex);
Jason Sams03855bb2011-01-25 00:26:25 -0800470 }
Steve Block71f2cf12011-10-20 11:56:00 +0100471 ALOGV("%p Context::~Context done", this);
Jason Samsd19f10d2009-05-22 14:03:28 -0700472}
473
Alex Sakhartchouk6c72eec2011-05-17 12:32:47 -0700474void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) {
Jason Sams704ff642010-02-09 16:05:07 -0800475 rsAssert(mIsGraphicsContext);
Jason Sams803626f2011-04-06 17:52:23 -0700476 mHal.funcs.setSurface(this, w, h, sur);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800477
Alex Sakhartchouk6c72eec2011-05-17 12:32:47 -0700478 mHasSurface = sur != NULL;
Jason Sams803626f2011-04-06 17:52:23 -0700479 mWidth = w;
480 mHeight = h;
Jason Sams3bc47d42009-11-12 15:10:25 -0800481
Jason Sams803626f2011-04-06 17:52:23 -0700482 if (mWidth && mHeight) {
Jason Samsf603d212010-05-14 15:30:29 -0700483 mStateVertex.updateSize(this);
Alex Sakhartchouk10ed049352011-07-19 17:50:29 -0700484 mFBOCache.updateSize();
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800485 }
486}
487
Alex Sakhartchouk2d8ef492011-11-16 12:22:10 -0800488uint32_t Context::getCurrentSurfaceWidth() const {
489 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
490 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
491 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimX();
492 }
493 }
494 if (mFBOCache.mHal.state.depthTarget != NULL) {
495 return mFBOCache.mHal.state.depthTarget->getType()->getDimX();
496 }
497 return mWidth;
498}
499
500uint32_t Context::getCurrentSurfaceHeight() const {
501 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
502 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
503 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimY();
504 }
505 }
506 if (mFBOCache.mHal.state.depthTarget != NULL) {
507 return mFBOCache.mHal.state.depthTarget->getType()->getDimY();
508 }
509 return mHeight;
510}
511
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800512void Context::pause() {
Jason Sams704ff642010-02-09 16:05:07 -0800513 rsAssert(mIsGraphicsContext);
Jason Sams65e7aa52009-09-24 17:38:20 -0700514 mPaused = true;
515}
516
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800517void Context::resume() {
Jason Sams704ff642010-02-09 16:05:07 -0800518 rsAssert(mIsGraphicsContext);
Jason Sams65e7aa52009-09-24 17:38:20 -0700519 mPaused = false;
520}
521
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800522void Context::setRootScript(Script *s) {
Jason Sams704ff642010-02-09 16:05:07 -0800523 rsAssert(mIsGraphicsContext);
Jason Samsd19f10d2009-05-22 14:03:28 -0700524 mRootScript.set(s);
525}
526
Jason Samsa17af042010-11-17 15:29:32 -0800527void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams704ff642010-02-09 16:05:07 -0800528 rsAssert(mIsGraphicsContext);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700529 if (pfs == NULL) {
530 mFragmentStore.set(mStateFragmentStore.mDefault);
531 } else {
532 mFragmentStore.set(pfs);
533 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700534}
535
Jason Samsa17af042010-11-17 15:29:32 -0800536void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams704ff642010-02-09 16:05:07 -0800537 rsAssert(mIsGraphicsContext);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700538 if (pf == NULL) {
539 mFragment.set(mStateFragment.mDefault);
540 } else {
541 mFragment.set(pf);
542 }
Jason Sams9bee51c2009-08-05 13:57:03 -0700543}
544
Jason Samsa17af042010-11-17 15:29:32 -0800545void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams704ff642010-02-09 16:05:07 -0800546 rsAssert(mIsGraphicsContext);
Jason Samsebfb4362009-09-23 13:57:02 -0700547 if (pr == NULL) {
548 mRaster.set(mStateRaster.mDefault);
549 } else {
550 mRaster.set(pr);
551 }
552}
553
Jason Samsa17af042010-11-17 15:29:32 -0800554void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams704ff642010-02-09 16:05:07 -0800555 rsAssert(mIsGraphicsContext);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700556 if (pv == NULL) {
557 mVertex.set(mStateVertex.mDefault);
558 } else {
559 mVertex.set(pv);
560 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700561}
562
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800563void Context::setFont(Font *f) {
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700564 rsAssert(mIsGraphicsContext);
565 if (f == NULL) {
566 mFont.set(mStateFont.mDefault);
567 } else {
568 mFont.set(f);
569 }
570}
571
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800572void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Sams3eaa3382009-06-10 15:04:38 -0700573 rsAssert(!obj->getName());
Jason Samsd5680f92009-06-10 18:39:40 -0700574 obj->setName(name, len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700575 mNames.add(obj);
576}
577
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800578void Context::removeName(ObjectBase *obj) {
579 for (size_t ct=0; ct < mNames.size(); ct++) {
Jason Sams3eaa3382009-06-10 15:04:38 -0700580 if (obj == mNames[ct]) {
581 mNames.removeAt(ct);
582 return;
583 }
584 }
585}
586
Jason Samsedbfabd2011-05-17 15:01:29 -0700587RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) {
588 return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID);
Jason Sams1c415172010-11-08 17:06:46 -0800589}
590
Jason Samsedbfabd2011-05-17 15:01:29 -0700591RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) {
592 return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen);
Jason Sams516c3192009-10-06 13:58:47 -0700593}
594
Jason Samsadd9d962010-11-22 16:20:16 -0800595bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
596 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Samsedbfabd2011-05-17 15:01:29 -0700597
598 return mIO.sendToClient(cmdID, subID, data, len, waitForSpace);
Jason Sams516c3192009-10-06 13:58:47 -0700599}
600
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800601void Context::initToClient() {
602 while (!mRunning) {
Jason Sams516c3192009-10-06 13:58:47 -0700603 usleep(100);
604 }
605}
606
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800607void Context::deinitToClient() {
Jason Samsedbfabd2011-05-17 15:01:29 -0700608 mIO.clientShutdown();
Jason Sams516c3192009-10-06 13:58:47 -0700609}
Jason Sams730ee652009-08-18 17:07:09 -0700610
Jason Samsadd9d962010-11-22 16:20:16 -0800611void Context::setError(RsError e, const char *msg) const {
Jason Sams156cce62010-03-03 13:03:18 -0800612 mError = e;
Jason Sams1c415172010-11-08 17:06:46 -0800613 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Sams156cce62010-03-03 13:03:18 -0800614}
615
616
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800617void Context::dumpDebug() const {
Steve Block3762c312012-01-06 19:20:56 +0000618 ALOGE("RS Context debug %p", this);
619 ALOGE("RS Context debug");
Jason Sams9dab6672009-11-24 12:26:35 -0800620
Steve Block3762c312012-01-06 19:20:56 +0000621 ALOGE(" RS width %i, height %i", mWidth, mHeight);
622 ALOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
623 ALOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId);
Jason Sams9dab6672009-11-24 12:26:35 -0800624}
Jason Samsd5680f92009-06-10 18:39:40 -0700625
Jason Samsd19f10d2009-05-22 14:03:28 -0700626///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa09f11d2009-06-04 17:58:03 -0700627//
Jason Samsd19f10d2009-05-22 14:03:28 -0700628
629namespace android {
630namespace renderscript {
631
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800632void rsi_ContextFinish(Context *rsc) {
Jason Sams96ed4cf2010-06-15 12:15:57 -0700633}
Jason Samsd19f10d2009-05-22 14:03:28 -0700634
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800635void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700636 Script *s = static_cast<Script *>(vs);
637 rsc->setRootScript(s);
638}
639
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800640void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700641 Sampler *s = static_cast<Sampler *>(vs);
642
643 if (slot > RS_MAX_SAMPLER_SLOT) {
Steve Block3762c312012-01-06 19:20:56 +0000644 ALOGE("Invalid sampler slot");
Jason Samsd19f10d2009-05-22 14:03:28 -0700645 return;
646 }
647
648 s->bindToContext(&rsc->mStateSampler, slot);
649}
650
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800651void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Sams54db59c2010-05-13 18:30:11 -0700652 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Samsa17af042010-11-17 15:29:32 -0800653 rsc->setProgramStore(pfs);
Jason Samsd19f10d2009-05-22 14:03:28 -0700654}
655
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800656void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700657 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Samsa17af042010-11-17 15:29:32 -0800658 rsc->setProgramFragment(pf);
Jason Samsd19f10d2009-05-22 14:03:28 -0700659}
660
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800661void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Samsebfb4362009-09-23 13:57:02 -0700662 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Samsa17af042010-11-17 15:29:32 -0800663 rsc->setProgramRaster(pr);
Jason Samsebfb4362009-09-23 13:57:02 -0700664}
665
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800666void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700667 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Samsa17af042010-11-17 15:29:32 -0800668 rsc->setProgramVertex(pv);
Jason Samsd19f10d2009-05-22 14:03:28 -0700669}
670
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800671void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700672 Font *font = static_cast<Font *>(vfont);
673 rsc->setFont(font);
674}
675
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700676void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) {
Jason Sams3eaa3382009-06-10 15:04:38 -0700677 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -0700678 rsc->assignName(ob, name, name_length);
Jason Sams3eaa3382009-06-10 15:04:38 -0700679}
Jason Samsd19f10d2009-05-22 14:03:28 -0700680
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800681void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams3b9c52a2010-10-14 17:48:46 -0700682 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams7ce033d2009-08-18 14:14:24 -0700683 rsc->removeName(ob);
Jason Sams07ae4062009-08-27 20:23:34 -0700684 ob->decUserRef();
Jason Sams7ce033d2009-08-18 14:14:24 -0700685}
686
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800687void rsi_ContextPause(Context *rsc) {
Jason Sams65e7aa52009-09-24 17:38:20 -0700688 rsc->pause();
689}
690
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800691void rsi_ContextResume(Context *rsc) {
Jason Sams65e7aa52009-09-24 17:38:20 -0700692 rsc->resume();
693}
694
Alex Sakhartchouk6c72eec2011-05-17 12:32:47 -0700695void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
Mathias Agopian128ce4b2010-02-12 14:04:35 -0800696 rsc->setSurface(w, h, sur);
Jason Sams3bc47d42009-11-12 15:10:25 -0800697}
698
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800699void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams7d787b42009-11-15 12:14:26 -0800700 rsc->setPriority(p);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800701}
702
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800703void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Sams715333b2009-11-17 17:26:46 -0800704 ObjectBase::dumpAll(rsc);
705}
706
Jason Sams5c68a712010-12-24 14:38:39 -0800707void rsi_ContextDestroyWorker(Context *rsc) {
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700708 rsc->destroyWorkerThreadResources();
Jason Sams5c68a712010-12-24 14:38:39 -0800709}
710
Jason Samsc5765372011-04-28 18:26:48 -0700711void rsi_ContextDestroy(Context *rsc) {
Steve Block71f2cf12011-10-20 11:56:00 +0100712 ALOGV("%p rsContextDestroy", rsc);
Jason Sams5c68a712010-12-24 14:38:39 -0800713 rsContextDestroyWorker(rsc);
Jason Sams546f01b2010-12-09 12:19:46 -0800714 delete rsc;
Steve Block71f2cf12011-10-20 11:56:00 +0100715 ALOGV("%p rsContextDestroy done", rsc);
Jason Sams546f01b2010-12-09 12:19:46 -0800716}
717
Jason Samsd19f10d2009-05-22 14:03:28 -0700718
Jason Samsc5765372011-04-28 18:26:48 -0700719RsMessageToClientType rsi_ContextPeekMessage(Context *rsc,
Jason Sams65bdaf12011-04-26 14:50:00 -0700720 size_t * receiveLen, size_t receiveLen_length,
Jason Samsedbfabd2011-05-17 15:01:29 -0700721 uint32_t * subID, size_t subID_length) {
722 return rsc->peekMessageToClient(receiveLen, subID);
Jason Sams1c415172010-11-08 17:06:46 -0800723}
724
Jason Samsc5765372011-04-28 18:26:48 -0700725RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length,
Jason Sams65bdaf12011-04-26 14:50:00 -0700726 size_t * receiveLen, size_t receiveLen_length,
Jason Samsedbfabd2011-05-17 15:01:29 -0700727 uint32_t * subID, size_t subID_length) {
Jason Sams65bdaf12011-04-26 14:50:00 -0700728 rsAssert(subID_length == sizeof(uint32_t));
729 rsAssert(receiveLen_length == sizeof(size_t));
Jason Samsedbfabd2011-05-17 15:01:29 -0700730 return rsc->getMessageToClient(data, receiveLen, subID, data_length);
Jason Sams516c3192009-10-06 13:58:47 -0700731}
732
Jason Samsc5765372011-04-28 18:26:48 -0700733void rsi_ContextInitToClient(Context *rsc) {
Jason Sams516c3192009-10-06 13:58:47 -0700734 rsc->initToClient();
735}
736
Jason Samsc5765372011-04-28 18:26:48 -0700737void rsi_ContextDeinitToClient(Context *rsc) {
Jason Sams516c3192009-10-06 13:58:47 -0700738 rsc->deinitToClient();
739}
740
Jason Samsc5765372011-04-28 18:26:48 -0700741}
742}
743
Stephen Hines4382467a2011-08-01 15:02:34 -0700744RsContext rsContextCreate(RsDevice vdev, uint32_t version,
745 uint32_t sdkVersion) {
Steve Block71f2cf12011-10-20 11:56:00 +0100746 ALOGV("rsContextCreate dev=%p", vdev);
Jason Samsd9d37cc2011-05-18 17:36:02 -0700747 Device * dev = static_cast<Device *>(vdev);
748 Context *rsc = Context::createContext(dev, NULL);
Stephen Hines4382467a2011-08-01 15:02:34 -0700749 if (rsc) {
750 rsc->setTargetSdkVersion(sdkVersion);
751 }
Jason Samsd9d37cc2011-05-18 17:36:02 -0700752 return rsc;
753}
754
755RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
Stephen Hines4382467a2011-08-01 15:02:34 -0700756 uint32_t sdkVersion, RsSurfaceConfig sc,
757 uint32_t dpi) {
Steve Block71f2cf12011-10-20 11:56:00 +0100758 ALOGV("rsContextCreateGL dev=%p", vdev);
Jason Samsd9d37cc2011-05-18 17:36:02 -0700759 Device * dev = static_cast<Device *>(vdev);
760 Context *rsc = Context::createContext(dev, &sc);
Jason Samsfb06b7a2011-07-13 16:09:42 -0700761 if (rsc) {
Stephen Hines4382467a2011-08-01 15:02:34 -0700762 rsc->setTargetSdkVersion(sdkVersion);
Jason Samsfb06b7a2011-07-13 16:09:42 -0700763 rsc->setDPI(dpi);
764 }
Steve Block71f2cf12011-10-20 11:56:00 +0100765 ALOGV("%p rsContextCreateGL ret", rsc);
Jason Samsd9d37cc2011-05-18 17:36:02 -0700766 return rsc;
767}
768
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700769// Only to be called at a3d load time, before object is visible to user
770// not thread safe
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800771void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700772 ObjectBase *ob = static_cast<ObjectBase *>(obj);
773 (*name) = ob->getName();
774}