blob: 62b3c5ceb9cfedb356ee2d13305c5a2525c3f416 [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 Sams326e0dd2009-05-22 14:03:28 -070021
Jason Sams15832442009-11-15 12:14:26 -080022#include <sys/types.h>
23#include <sys/resource.h>
Jason Sams7bf29dd2010-07-19 15:38:19 -070024#include <sched.h>
Jason Sams15832442009-11-15 12:14:26 -080025
Joe Onorato76371ff2009-09-23 16:37:36 -070026#include <cutils/properties.h>
27
Jason Sams15832442009-11-15 12:14:26 -080028#include <cutils/sched_policy.h>
Jason Sams8d957fa2010-09-28 14:41:22 -070029#include <sys/syscall.h>
30#include <string.h>
Jason Sams15832442009-11-15 12:14:26 -080031
Jason Sams326e0dd2009-05-22 14:03:28 -070032using namespace android;
33using namespace android::renderscript;
34
Jason Samsfb03a222009-10-15 16:47:31 -070035pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Stephen Hinesca3f09c2011-01-07 15:11:30 -080036pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070037
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080038bool Context::initGLThread() {
Jason Sams6b8552a2010-10-13 15:31:10 -070039 pthread_mutex_lock(&gInitMutex);
Jason Sams6b8552a2010-10-13 15:31:10 -070040
Jason Sams4b3de472011-04-06 17:52:23 -070041 if (!mHal.funcs.initGraphics(this)) {
Jason Sams5c1c79a2010-11-03 14:27:11 -070042 pthread_mutex_unlock(&gInitMutex);
Steve Blockaf12ac62012-01-06 19:20:56 +000043 ALOGE("%p initGraphics failed", this);
Jason Sams5c1c79a2010-11-03 14:27:11 -070044 return false;
Jason Sams6b8552a2010-10-13 15:31:10 -070045 }
46
Jason Sams6b8552a2010-10-13 15:31:10 -070047 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -070048 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -070049}
50
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080051void Context::deinitEGL() {
Jason Sams4b3de472011-04-06 17:52:23 -070052 mHal.funcs.shutdownGraphics(this);
Jason Sams33b6e3b2009-10-27 14:44:31 -070053}
54
Jason Sams60709252010-11-17 15:29:32 -080055Context::PushState::PushState(Context *con) {
56 mRsc = con;
Jason Samsc946b612011-02-23 14:47:17 -080057 if (con->mIsGraphicsContext) {
58 mFragment.set(con->getProgramFragment());
59 mVertex.set(con->getProgramVertex());
60 mStore.set(con->getProgramStore());
61 mRaster.set(con->getProgramRaster());
62 mFont.set(con->getFont());
63 }
Jason Sams60709252010-11-17 15:29:32 -080064}
65
66Context::PushState::~PushState() {
Jason Samsc946b612011-02-23 14:47:17 -080067 if (mRsc->mIsGraphicsContext) {
68 mRsc->setProgramFragment(mFragment.get());
69 mRsc->setProgramVertex(mVertex.get());
70 mRsc->setProgramStore(mStore.get());
71 mRsc->setProgramRaster(mRaster.get());
72 mRsc->setFont(mFont.get());
73 }
Jason Sams60709252010-11-17 15:29:32 -080074}
75
Jason Sams33b6e3b2009-10-27 14:44:31 -070076
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080077uint32_t Context::runScript(Script *s) {
Jason Sams60709252010-11-17 15:29:32 -080078 PushState(this);
Jason Sams10308932009-06-09 12:15:30 -070079
Jason Samsc61346b2010-05-28 18:23:22 -070080 uint32_t ret = s->run(this);
Jason Samsc9d43db2009-07-28 12:02:16 -070081 return ret;
Jason Sams10308932009-06-09 12:15:30 -070082}
83
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080084uint32_t Context::runRootScript() {
Jason Sams2dca84d2009-12-09 11:05:45 -080085 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -070086 mStateFragmentStore.mLast.clear();
Jason Sams2382aba2011-09-13 15:41:01 -070087 watchdog.inRoot = true;
Jason Samsc61346b2010-05-28 18:23:22 -070088 uint32_t ret = runScript(mRootScript.get());
Jason Sams2382aba2011-09-13 15:41:01 -070089 watchdog.inRoot = false;
Jason Sams8cfdd242009-10-14 15:43:53 -070090
Jason Samscfb1d112009-08-05 13:57:03 -070091 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -070092}
93
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080094uint64_t Context::getTime() const {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -070095#ifndef ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -070096 struct timespec t;
97 clock_gettime(CLOCK_MONOTONIC, &t);
98 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -070099#else
100 return 0;
101#endif //ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -0700102}
103
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800104void Context::timerReset() {
Jason Sams24371d92009-08-19 12:17:14 -0700105 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
106 mTimers[ct] = 0;
107 }
108}
109
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800110void Context::timerInit() {
Jason Sams24371d92009-08-19 12:17:14 -0700111 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700112 mTimeFrame = mTimeLast;
113 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700114 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700115 mAverageFPSFrameCount = 0;
116 mAverageFPSStartTime = mTimeLast;
117 mAverageFPS = 0;
Jason Sams24371d92009-08-19 12:17:14 -0700118 timerReset();
119}
120
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800121void Context::timerFrame() {
Jason Sams1d54f102009-09-03 15:43:13 -0700122 mTimeLastFrame = mTimeFrame;
123 mTimeFrame = getTime();
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700124 // Update average fps
125 const uint64_t averageFramerateInterval = 1000 * 1000000;
126 mAverageFPSFrameCount ++;
127 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800128 if (inverval >= averageFramerateInterval) {
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700129 inverval = inverval / 1000000;
130 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
131 mAverageFPSFrameCount = 0;
132 mAverageFPSStartTime = mTimeFrame;
133 }
Jason Sams1d54f102009-09-03 15:43:13 -0700134}
135
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800136void Context::timerSet(Timers tm) {
Jason Sams24371d92009-08-19 12:17:14 -0700137 uint64_t last = mTimeLast;
138 mTimeLast = getTime();
139 mTimers[mTimerActive] += mTimeLast - last;
140 mTimerActive = tm;
141}
142
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800143void Context::timerPrint() {
Jason Sams24371d92009-08-19 12:17:14 -0700144 double total = 0;
145 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
146 total += mTimers[ct];
147 }
Jason Sams1d54f102009-09-03 15:43:13 -0700148 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800149 mTimeMSLastFrame = frame / 1000000;
150 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
151 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700152
Jason Sams2dca84d2009-12-09 11:05:45 -0800153
154 if (props.mLogTimes) {
Steve Block65982012011-10-20 11:56:00 +0100155 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 -0800156 mTimeMSLastFrame,
157 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
158 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
159 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700160 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
161 mAverageFPS);
Jason Sams2dca84d2009-12-09 11:05:45 -0800162 }
Jason Sams24371d92009-08-19 12:17:14 -0700163}
164
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800165bool Context::setupCheck() {
Jason Sams900f1612010-09-16 18:18:29 -0700166
Jason Sams721acc42011-04-06 11:23:54 -0700167 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700168 mFragment->setup(this, &mStateFragment);
Jason Sams721acc42011-04-06 11:23:54 -0700169 mRaster->setup(this, &mStateRaster);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700170 mVertex->setup(this, &mStateVertex);
171 mFBOCache.setup(this);
Jason Samsa2cf7552010-03-03 13:03:18 -0800172 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700173}
174
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700175void Context::setupProgramStore() {
Jason Sams721acc42011-04-06 11:23:54 -0700176 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700177}
178
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800179static bool getProp(const char *str) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700180 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700181 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700182 return 0 != strcmp(buf, "0");
183}
Jason Sams326e0dd2009-05-22 14:03:28 -0700184
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800185void Context::displayDebugStats() {
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700186 char buffer[128];
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700187 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700188 float oldR, oldG, oldB, oldA;
189 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700190 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700191
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700192 ObjectBaseRef<Font> lastFont(getFont());
193 setFont(NULL);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700194 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700195 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700196 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700197
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700198 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700199 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700200
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700201 setFont(lastFont.get());
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700202 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700203}
204
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800205void * Context::threadProc(void *vrsc) {
Jason Sams83c451a2011-04-21 11:46:50 -0700206 Context *rsc = static_cast<Context *>(vrsc);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700207#ifndef ANDROID_RS_SERIALIZE
Jason Sams83c451a2011-04-21 11:46:50 -0700208 rsc->mNativeThreadId = gettid();
Jason Sams83c451a2011-04-21 11:46:50 -0700209 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
210 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700211#endif //ANDROID_RS_SERIALIZE
Jason Sams83c451a2011-04-21 11:46:50 -0700212 rsc->props.mLogTimes = getProp("debug.rs.profile");
213 rsc->props.mLogScripts = getProp("debug.rs.script");
214 rsc->props.mLogObjects = getProp("debug.rs.object");
215 rsc->props.mLogShaders = getProp("debug.rs.shader");
216 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes");
217 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms");
218 rsc->props.mLogVisual = getProp("debug.rs.visual");
Joe Onorato76371ff2009-09-23 16:37:36 -0700219
Jason Sams83c451a2011-04-21 11:46:50 -0700220 if (!rsdHalInit(rsc, 0, 0)) {
Jason Samsd3e71072011-05-03 15:01:58 -0700221 rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed initializing GL");
Steve Blockaf12ac62012-01-06 19:20:56 +0000222 ALOGE("Hal init failed");
Jason Sams83c451a2011-04-21 11:46:50 -0700223 return NULL;
224 }
225 rsc->mHal.funcs.setPriority(rsc, rsc->mThreadPriority);
Jason Samse5769102009-06-19 16:03:18 -0700226
Jason Sams83c451a2011-04-21 11:46:50 -0700227 if (rsc->mIsGraphicsContext) {
Jason Samsd3e71072011-05-03 15:01:58 -0700228 if (!rsc->initGLThread()) {
229 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
230 return NULL;
231 }
232
Jason Sams83c451a2011-04-21 11:46:50 -0700233 rsc->mStateRaster.init(rsc);
234 rsc->setProgramRaster(NULL);
235 rsc->mStateVertex.init(rsc);
236 rsc->setProgramVertex(NULL);
237 rsc->mStateFragment.init(rsc);
238 rsc->setProgramFragment(NULL);
239 rsc->mStateFragmentStore.init(rsc);
240 rsc->setProgramStore(NULL);
241 rsc->mStateFont.init(rsc);
242 rsc->setFont(NULL);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700243 rsc->mStateSampler.init(rsc);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700244 rsc->mFBOCache.init(rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700245 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700246
Jason Sams83c451a2011-04-21 11:46:50 -0700247 rsc->mRunning = true;
248 bool mDraw = true;
Jason Samse0aab4a2011-08-12 15:05:15 -0700249 bool doWait = true;
250
251 uint64_t targetTime = rsc->getTime();
Jason Sams83c451a2011-04-21 11:46:50 -0700252 while (!rsc->mExit) {
Jason Samse0aab4a2011-08-12 15:05:15 -0700253 uint64_t waitTime = 0;
254 uint64_t now = rsc->getTime();
Jason Samsdc314a12011-08-17 17:15:23 -0700255 if (!doWait) {
256 if (now < targetTime) {
257 waitTime = targetTime - now;
258 doWait = true;
259 }
Jason Samse0aab4a2011-08-12 15:05:15 -0700260 }
261
262 mDraw |= rsc->mIO.playCoreCommands(rsc, doWait, waitTime);
Jason Sams83c451a2011-04-21 11:46:50 -0700263 mDraw &= (rsc->mRootScript.get() != NULL);
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700264 mDraw &= rsc->mHasSurface;
Jason Sams326e0dd2009-05-22 14:03:28 -0700265
Jason Sams83c451a2011-04-21 11:46:50 -0700266 if (mDraw && rsc->mIsGraphicsContext) {
Jason Samse0aab4a2011-08-12 15:05:15 -0700267 uint64_t delay = rsc->runRootScript() * 1000000;
268 targetTime = rsc->getTime() + delay;
Jason Samsdc314a12011-08-17 17:15:23 -0700269 doWait = (delay == 0);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700270
Jason Sams83c451a2011-04-21 11:46:50 -0700271 if (rsc->props.mLogVisual) {
272 rsc->displayDebugStats();
273 }
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700274
Jason Samse0aab4a2011-08-12 15:05:15 -0700275 mDraw = !rsc->mPaused;
Jason Sams83c451a2011-04-21 11:46:50 -0700276 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
277 rsc->mHal.funcs.swap(rsc);
278 rsc->timerFrame();
279 rsc->timerSet(RS_TIMER_INTERNAL);
280 rsc->timerPrint();
281 rsc->timerReset();
Jason Samsca7c2022011-10-26 17:56:00 -0700282 } else {
283 doWait = true;
Jason Sams83c451a2011-04-21 11:46:50 -0700284 }
Jason Sams83c451a2011-04-21 11:46:50 -0700285 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700286
Steve Block65982012011-10-20 11:56:00 +0100287 ALOGV("%p RS Thread exiting", rsc);
Jason Samse514b452009-09-25 14:51:22 -0700288
Jason Sams83c451a2011-04-21 11:46:50 -0700289 if (rsc->mIsGraphicsContext) {
290 pthread_mutex_lock(&gInitMutex);
291 rsc->deinitEGL();
292 pthread_mutex_unlock(&gInitMutex);
293 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700294
Steve Block65982012011-10-20 11:56:00 +0100295 ALOGV("%p RS Thread exited", rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700296 return NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700297}
298
Jason Sams741aac92010-12-24 14:38:39 -0800299void Context::destroyWorkerThreadResources() {
Steve Block65982012011-10-20 11:56:00 +0100300 //ALOGV("destroyWorkerThreadResources 1");
Jason Sams2e8665d2011-01-27 00:14:13 -0800301 ObjectBase::zeroAllUserRef(this);
Jason Sams741aac92010-12-24 14:38:39 -0800302 if (mIsGraphicsContext) {
303 mRaster.clear();
304 mFragment.clear();
305 mVertex.clear();
306 mFragmentStore.clear();
307 mFont.clear();
308 mRootScript.clear();
309 mStateRaster.deinit(this);
310 mStateVertex.deinit(this);
311 mStateFragment.deinit(this);
312 mStateFragmentStore.deinit(this);
313 mStateFont.deinit(this);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700314 mStateSampler.deinit(this);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700315 mFBOCache.deinit(this);
Jason Sams741aac92010-12-24 14:38:39 -0800316 }
Jason Samsc7cec1e2011-08-18 18:01:33 -0700317 ObjectBase::freeAllChildren(this);
Steve Block65982012011-10-20 11:56:00 +0100318 //ALOGV("destroyWorkerThreadResources 2");
Jason Samscf912de2011-01-09 16:09:51 -0800319 mExit = true;
Jason Sams741aac92010-12-24 14:38:39 -0800320}
321
Jason Sams2382aba2011-09-13 15:41:01 -0700322void Context::printWatchdogInfo(void *ctx) {
323 Context *rsc = (Context *)ctx;
Jason Samsee803442011-10-13 16:05:27 -0700324 if (rsc->watchdog.command && rsc->watchdog.file) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000325 ALOGE("RS watchdog timeout: %i %s line %i %s", rsc->watchdog.inRoot,
Jason Samsee803442011-10-13 16:05:27 -0700326 rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file);
327 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000328 ALOGE("RS watchdog timeout: %i", rsc->watchdog.inRoot);
Jason Samsee803442011-10-13 16:05:27 -0700329 }
Jason Sams2382aba2011-09-13 15:41:01 -0700330}
331
332
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800333void Context::setPriority(int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800334 // Note: If we put this in the proper "background" policy
335 // the wallpapers can become completly unresponsive at times.
336 // This is probably not what we want for something the user is actively
337 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800338 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800339#if 0
340 SchedPolicy pol = SP_FOREGROUND;
341 if (p > 0) {
342 pol = SP_BACKGROUND;
343 }
344 if (!set_sched_policy(mNativeThreadId, pol)) {
345 // success; reset the priority as well
346 }
347#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700348 setpriority(PRIO_PROCESS, mNativeThreadId, p);
Jason Sams15832442009-11-15 12:14:26 -0800349#endif
Jason Sams9719bd42012-01-12 14:22:21 -0800350 mHal.funcs.setPriority(this, mThreadPriority);
Jason Sams15832442009-11-15 12:14:26 -0800351}
352
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800353Context::Context() {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700354 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700355 mRunning = false;
356 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700357 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700358 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800359 mError = RS_ERROR_NONE;
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700360 mTargetSdkVersion = 14;
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700361 mDPI = 96;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700362 mIsContextLite = false;
Stephen Hines86c6b5f2011-10-31 14:07:54 -0700363 memset(&watchdog, 0, sizeof(watchdog));
Jason Sams5c1c79a2010-11-03 14:27:11 -0700364}
365
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800366Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700367 Context * rsc = new Context();
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700368
Jason Sams5c1c79a2010-11-03 14:27:11 -0700369 if (!rsc->initContext(dev, sc)) {
370 delete rsc;
371 return NULL;
372 }
373 return rsc;
374}
375
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700376Context * Context::createContextLite() {
377 Context * rsc = new Context();
378 rsc->mIsContextLite = true;
379 return rsc;
380}
381
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800382bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700383 pthread_mutex_lock(&gInitMutex);
384
Jason Sams1a4efa32011-05-17 15:01:29 -0700385 mIO.init();
Jason Sams2382aba2011-09-13 15:41:01 -0700386 mIO.setTimoutCallback(printWatchdogInfo, this, 2e9);
Jason Sams1a4efa32011-05-17 15:01:29 -0700387
Jason Sams5c1c79a2010-11-03 14:27:11 -0700388 dev->addContext(this);
389 mDev = dev;
Jason Sams6b8552a2010-10-13 15:31:10 -0700390 if (sc) {
391 mUserSurfaceConfig = *sc;
392 } else {
393 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
394 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800395
Jason Sams6b8552a2010-10-13 15:31:10 -0700396 mIsGraphicsContext = sc != NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700397
Jason Samsa658e902009-06-04 14:35:01 -0700398 int status;
399 pthread_attr_t threadAttr;
400
Jason Samsfb03a222009-10-15 16:47:31 -0700401 pthread_mutex_unlock(&gInitMutex);
402
403 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700404
Jason Samsa658e902009-06-04 14:35:01 -0700405 status = pthread_attr_init(&threadAttr);
406 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000407 ALOGE("Failed to init thread attribute.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700408 return false;
Jason Samsa658e902009-06-04 14:35:01 -0700409 }
410
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700411 mHasSurface = false;
Jason Sams992a0b72009-06-23 12:22:47 -0700412
Jason Sams24371d92009-08-19 12:17:14 -0700413 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700414 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700415
Jason Samsa658e902009-06-04 14:35:01 -0700416 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700417 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000418 ALOGE("Failed to start rs context thread.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700419 return false;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700420 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800421 while (!mRunning && (mError == RS_ERROR_NONE)) {
Jason Sams18133402010-07-20 15:09:00 -0700422 usleep(100);
423 }
424
Jason Sams5c1c79a2010-11-03 14:27:11 -0700425 if (mError != RS_ERROR_NONE) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000426 ALOGE("Errors during thread init");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700427 return false;
428 }
429
Jason Samsa658e902009-06-04 14:35:01 -0700430 pthread_attr_destroy(&threadAttr);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700431 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700432}
433
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800434Context::~Context() {
Steve Block65982012011-10-20 11:56:00 +0100435 ALOGV("%p Context::~Context", this);
Jason Samscf912de2011-01-09 16:09:51 -0800436
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700437 if (!mIsContextLite) {
438 mIO.coreFlush();
439 rsAssert(mExit);
440 mExit = true;
441 mPaused = false;
442 void *res;
Jason Sams326e0dd2009-05-22 14:03:28 -0700443
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700444 mIO.shutdown();
445 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700446
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700447 if (mHal.funcs.shutdownDriver) {
448 mHal.funcs.shutdownDriver(this);
449 }
450
451 // Global structure cleanup.
452 pthread_mutex_lock(&gInitMutex);
453 if (mDev) {
454 mDev->removeContext(this);
455 mDev = NULL;
456 }
457 pthread_mutex_unlock(&gInitMutex);
Jason Sams51462c52011-01-25 00:26:25 -0800458 }
Steve Block65982012011-10-20 11:56:00 +0100459 ALOGV("%p Context::~Context done", this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700460}
461
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700462void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800463 rsAssert(mIsGraphicsContext);
Jason Sams4b3de472011-04-06 17:52:23 -0700464 mHal.funcs.setSurface(this, w, h, sur);
Jason Sams458f2dc2009-11-03 13:58:36 -0800465
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700466 mHasSurface = sur != NULL;
Jason Sams4b3de472011-04-06 17:52:23 -0700467 mWidth = w;
468 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800469
Jason Sams4b3de472011-04-06 17:52:23 -0700470 if (mWidth && mHeight) {
Jason Sams771565f2010-05-14 15:30:29 -0700471 mStateVertex.updateSize(this);
Alex Sakhartchouka544b632011-07-19 17:50:29 -0700472 mFBOCache.updateSize();
Jason Sams458f2dc2009-11-03 13:58:36 -0800473 }
474}
475
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800476uint32_t Context::getCurrentSurfaceWidth() const {
477 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
478 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
479 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimX();
480 }
481 }
482 if (mFBOCache.mHal.state.depthTarget != NULL) {
483 return mFBOCache.mHal.state.depthTarget->getType()->getDimX();
484 }
485 return mWidth;
486}
487
488uint32_t Context::getCurrentSurfaceHeight() 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()->getDimY();
492 }
493 }
494 if (mFBOCache.mHal.state.depthTarget != NULL) {
495 return mFBOCache.mHal.state.depthTarget->getType()->getDimY();
496 }
497 return mHeight;
498}
499
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800500void Context::pause() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800501 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700502 mPaused = true;
503}
504
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800505void Context::resume() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800506 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700507 mPaused = false;
508}
509
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800510void Context::setRootScript(Script *s) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800511 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700512 mRootScript.set(s);
513}
514
Jason Sams60709252010-11-17 15:29:32 -0800515void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800516 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700517 if (pfs == NULL) {
518 mFragmentStore.set(mStateFragmentStore.mDefault);
519 } else {
520 mFragmentStore.set(pfs);
521 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700522}
523
Jason Sams60709252010-11-17 15:29:32 -0800524void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800525 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700526 if (pf == NULL) {
527 mFragment.set(mStateFragment.mDefault);
528 } else {
529 mFragment.set(pf);
530 }
Jason Samscfb1d112009-08-05 13:57:03 -0700531}
532
Jason Sams60709252010-11-17 15:29:32 -0800533void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800534 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700535 if (pr == NULL) {
536 mRaster.set(mStateRaster.mDefault);
537 } else {
538 mRaster.set(pr);
539 }
540}
541
Jason Sams60709252010-11-17 15:29:32 -0800542void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800543 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700544 if (pv == NULL) {
545 mVertex.set(mStateVertex.mDefault);
546 } else {
547 mVertex.set(pv);
548 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700549}
550
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800551void Context::setFont(Font *f) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700552 rsAssert(mIsGraphicsContext);
553 if (f == NULL) {
554 mFont.set(mStateFont.mDefault);
555 } else {
556 mFont.set(f);
557 }
558}
559
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800560void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700561 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700562 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700563 mNames.add(obj);
564}
565
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800566void Context::removeName(ObjectBase *obj) {
567 for (size_t ct=0; ct < mNames.size(); ct++) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700568 if (obj == mNames[ct]) {
569 mNames.removeAt(ct);
570 return;
571 }
572 }
573}
574
Jason Sams1a4efa32011-05-17 15:01:29 -0700575RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) {
576 return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800577}
578
Jason Sams1a4efa32011-05-17 15:01:29 -0700579RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) {
580 return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen);
Jason Sams8c401ef2009-10-06 13:58:47 -0700581}
582
Jason Sams87319de2010-11-22 16:20:16 -0800583bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
584 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Sams1a4efa32011-05-17 15:01:29 -0700585
586 return mIO.sendToClient(cmdID, subID, data, len, waitForSpace);
Jason Sams8c401ef2009-10-06 13:58:47 -0700587}
588
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800589void Context::initToClient() {
590 while (!mRunning) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700591 usleep(100);
592 }
593}
594
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800595void Context::deinitToClient() {
Jason Sams1a4efa32011-05-17 15:01:29 -0700596 mIO.clientShutdown();
Jason Sams8c401ef2009-10-06 13:58:47 -0700597}
Jason Sams50869382009-08-18 17:07:09 -0700598
Jason Sams87319de2010-11-22 16:20:16 -0800599void Context::setError(RsError e, const char *msg) const {
Jason Samsa2cf7552010-03-03 13:03:18 -0800600 mError = e;
Jason Samsaad4bc52010-11-08 17:06:46 -0800601 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Samsa2cf7552010-03-03 13:03:18 -0800602}
603
604
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800605void Context::dumpDebug() const {
Steve Blockaf12ac62012-01-06 19:20:56 +0000606 ALOGE("RS Context debug %p", this);
607 ALOGE("RS Context debug");
Jason Sams13e26342009-11-24 12:26:35 -0800608
Steve Blockaf12ac62012-01-06 19:20:56 +0000609 ALOGE(" RS width %i, height %i", mWidth, mHeight);
610 ALOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
611 ALOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId);
Jason Sams13e26342009-11-24 12:26:35 -0800612}
Jason Samsa4a54e42009-06-10 18:39:40 -0700613
Jason Sams326e0dd2009-05-22 14:03:28 -0700614///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700615//
Jason Sams326e0dd2009-05-22 14:03:28 -0700616
617namespace android {
618namespace renderscript {
619
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800620void rsi_ContextFinish(Context *rsc) {
Jason Sams8c880902010-06-15 12:15:57 -0700621}
Jason Sams326e0dd2009-05-22 14:03:28 -0700622
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800623void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700624 Script *s = static_cast<Script *>(vs);
625 rsc->setRootScript(s);
626}
627
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800628void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700629 Sampler *s = static_cast<Sampler *>(vs);
630
631 if (slot > RS_MAX_SAMPLER_SLOT) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000632 ALOGE("Invalid sampler slot");
Jason Sams326e0dd2009-05-22 14:03:28 -0700633 return;
634 }
635
636 s->bindToContext(&rsc->mStateSampler, slot);
637}
638
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800639void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Samsccc010b2010-05-13 18:30:11 -0700640 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams60709252010-11-17 15:29:32 -0800641 rsc->setProgramStore(pfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700642}
643
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800644void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700645 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Sams60709252010-11-17 15:29:32 -0800646 rsc->setProgramFragment(pf);
Jason Sams326e0dd2009-05-22 14:03:28 -0700647}
648
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800649void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Sams5fd09d82009-09-23 13:57:02 -0700650 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Sams60709252010-11-17 15:29:32 -0800651 rsc->setProgramRaster(pr);
Jason Sams5fd09d82009-09-23 13:57:02 -0700652}
653
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800654void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700655 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Sams60709252010-11-17 15:29:32 -0800656 rsc->setProgramVertex(pv);
Jason Sams326e0dd2009-05-22 14:03:28 -0700657}
658
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800659void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700660 Font *font = static_cast<Font *>(vfont);
661 rsc->setFont(font);
662}
663
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700664void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700665 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700666 rsc->assignName(ob, name, name_length);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700667}
Jason Sams326e0dd2009-05-22 14:03:28 -0700668
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800669void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams2353ae32010-10-14 17:48:46 -0700670 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -0700671 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700672 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700673}
674
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800675void rsi_ContextPause(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700676 rsc->pause();
677}
678
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800679void rsi_ContextResume(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700680 rsc->resume();
681}
682
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700683void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
Mathias Agopianfa402862010-02-12 14:04:35 -0800684 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800685}
686
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800687void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800688 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800689}
690
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800691void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Samsc21cf402009-11-17 17:26:46 -0800692 ObjectBase::dumpAll(rsc);
693}
694
Jason Sams741aac92010-12-24 14:38:39 -0800695void rsi_ContextDestroyWorker(Context *rsc) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700696 rsc->destroyWorkerThreadResources();
Jason Sams741aac92010-12-24 14:38:39 -0800697}
698
Jason Samsc975cf42011-04-28 18:26:48 -0700699void rsi_ContextDestroy(Context *rsc) {
Steve Block65982012011-10-20 11:56:00 +0100700 ALOGV("%p rsContextDestroy", rsc);
Jason Sams741aac92010-12-24 14:38:39 -0800701 rsContextDestroyWorker(rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800702 delete rsc;
Steve Block65982012011-10-20 11:56:00 +0100703 ALOGV("%p rsContextDestroy done", rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800704}
705
Jason Sams326e0dd2009-05-22 14:03:28 -0700706
Jason Samsc975cf42011-04-28 18:26:48 -0700707RsMessageToClientType rsi_ContextPeekMessage(Context *rsc,
Jason Sams186e5912011-04-26 14:50:00 -0700708 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700709 uint32_t * subID, size_t subID_length) {
710 return rsc->peekMessageToClient(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800711}
712
Jason Samsc975cf42011-04-28 18:26:48 -0700713RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length,
Jason Sams186e5912011-04-26 14:50:00 -0700714 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700715 uint32_t * subID, size_t subID_length) {
Jason Sams186e5912011-04-26 14:50:00 -0700716 rsAssert(subID_length == sizeof(uint32_t));
717 rsAssert(receiveLen_length == sizeof(size_t));
Jason Sams1a4efa32011-05-17 15:01:29 -0700718 return rsc->getMessageToClient(data, receiveLen, subID, data_length);
Jason Sams8c401ef2009-10-06 13:58:47 -0700719}
720
Jason Samsc975cf42011-04-28 18:26:48 -0700721void rsi_ContextInitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700722 rsc->initToClient();
723}
724
Jason Samsc975cf42011-04-28 18:26:48 -0700725void rsi_ContextDeinitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700726 rsc->deinitToClient();
727}
728
Jason Samsc975cf42011-04-28 18:26:48 -0700729}
730}
731
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700732RsContext rsContextCreate(RsDevice vdev, uint32_t version,
733 uint32_t sdkVersion) {
Steve Block65982012011-10-20 11:56:00 +0100734 ALOGV("rsContextCreate dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700735 Device * dev = static_cast<Device *>(vdev);
736 Context *rsc = Context::createContext(dev, NULL);
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700737 if (rsc) {
738 rsc->setTargetSdkVersion(sdkVersion);
739 }
Jason Sams789ca832011-05-18 17:36:02 -0700740 return rsc;
741}
742
743RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700744 uint32_t sdkVersion, RsSurfaceConfig sc,
745 uint32_t dpi) {
Steve Block65982012011-10-20 11:56:00 +0100746 ALOGV("rsContextCreateGL dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700747 Device * dev = static_cast<Device *>(vdev);
748 Context *rsc = Context::createContext(dev, &sc);
Jason Sams9544f762011-07-13 16:09:42 -0700749 if (rsc) {
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700750 rsc->setTargetSdkVersion(sdkVersion);
Jason Sams9544f762011-07-13 16:09:42 -0700751 rsc->setDPI(dpi);
752 }
Steve Block65982012011-10-20 11:56:00 +0100753 ALOGV("%p rsContextCreateGL ret", rsc);
Jason Sams789ca832011-05-18 17:36:02 -0700754 return rsc;
755}
756
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700757// Only to be called at a3d load time, before object is visible to user
758// not thread safe
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800759void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700760 ObjectBase *ob = static_cast<ObjectBase *>(obj);
761 (*name) = ob->getName();
762}