blob: 5e21763d4b72d47de040557331fa38be30a3e08e [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -07002 * Copyright (C) 2011 The Android Open Source Project
Jason Sams326e0dd2009-05-22 14:03:28 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Alex Sakhartchouke23d2392012-03-09 09:24:39 -080017#include "rs.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070018#include "rsDevice.h"
19#include "rsContext.h"
20#include "rsThreadIO.h"
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -080021#include "rsMesh.h"
Mathias Agopian5ae678f2009-06-22 18:01:09 -070022#include <ui/FramebufferNativeWindow.h>
Jason Sams5f27d6f2012-02-07 15:32:08 -080023#include <gui/DisplayEventReceiver.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070024
Jason Sams15832442009-11-15 12:14:26 -080025#include <sys/types.h>
26#include <sys/resource.h>
Jason Sams7bf29dd2010-07-19 15:38:19 -070027#include <sched.h>
Jason Sams15832442009-11-15 12:14:26 -080028
Joe Onorato76371ff2009-09-23 16:37:36 -070029#include <cutils/properties.h>
30
Jason Sams8d957fa2010-09-28 14:41:22 -070031#include <sys/syscall.h>
32#include <string.h>
Jason Sams15832442009-11-15 12:14:26 -080033
Jason Sams326e0dd2009-05-22 14:03:28 -070034using namespace android;
35using namespace android::renderscript;
36
Jason Samsfb03a222009-10-15 16:47:31 -070037pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Stephen Hinesca3f09c2011-01-07 15:11:30 -080038pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070039
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080040bool Context::initGLThread() {
Jason Sams6b8552a2010-10-13 15:31:10 -070041 pthread_mutex_lock(&gInitMutex);
Jason Sams6b8552a2010-10-13 15:31:10 -070042
Jason Sams4b3de472011-04-06 17:52:23 -070043 if (!mHal.funcs.initGraphics(this)) {
Jason Sams5c1c79a2010-11-03 14:27:11 -070044 pthread_mutex_unlock(&gInitMutex);
Steve Blockaf12ac62012-01-06 19:20:56 +000045 ALOGE("%p initGraphics failed", this);
Jason Sams5c1c79a2010-11-03 14:27:11 -070046 return false;
Jason Sams6b8552a2010-10-13 15:31:10 -070047 }
48
Jason Sams6b8552a2010-10-13 15:31:10 -070049 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -070050 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -070051}
52
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080053void Context::deinitEGL() {
Jason Sams4b3de472011-04-06 17:52:23 -070054 mHal.funcs.shutdownGraphics(this);
Jason Sams33b6e3b2009-10-27 14:44:31 -070055}
56
Jason Sams60709252010-11-17 15:29:32 -080057Context::PushState::PushState(Context *con) {
58 mRsc = con;
Jason Samsc946b612011-02-23 14:47:17 -080059 if (con->mIsGraphicsContext) {
60 mFragment.set(con->getProgramFragment());
61 mVertex.set(con->getProgramVertex());
62 mStore.set(con->getProgramStore());
63 mRaster.set(con->getProgramRaster());
64 mFont.set(con->getFont());
65 }
Jason Sams60709252010-11-17 15:29:32 -080066}
67
68Context::PushState::~PushState() {
Jason Samsc946b612011-02-23 14:47:17 -080069 if (mRsc->mIsGraphicsContext) {
70 mRsc->setProgramFragment(mFragment.get());
71 mRsc->setProgramVertex(mVertex.get());
72 mRsc->setProgramStore(mStore.get());
73 mRsc->setProgramRaster(mRaster.get());
74 mRsc->setFont(mFont.get());
75 }
Jason Sams60709252010-11-17 15:29:32 -080076}
77
Jason Sams33b6e3b2009-10-27 14:44:31 -070078
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080079uint32_t Context::runScript(Script *s) {
Jason Sams60709252010-11-17 15:29:32 -080080 PushState(this);
Jason Sams10308932009-06-09 12:15:30 -070081
Jason Samsc61346b2010-05-28 18:23:22 -070082 uint32_t ret = s->run(this);
Jason Samsc9d43db2009-07-28 12:02:16 -070083 return ret;
Jason Sams10308932009-06-09 12:15:30 -070084}
85
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080086uint32_t Context::runRootScript() {
Jason Sams2dca84d2009-12-09 11:05:45 -080087 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -070088 mStateFragmentStore.mLast.clear();
Jason Sams2382aba2011-09-13 15:41:01 -070089 watchdog.inRoot = true;
Jason Samsc61346b2010-05-28 18:23:22 -070090 uint32_t ret = runScript(mRootScript.get());
Jason Sams2382aba2011-09-13 15:41:01 -070091 watchdog.inRoot = false;
Jason Sams8cfdd242009-10-14 15:43:53 -070092
Jason Samscfb1d112009-08-05 13:57:03 -070093 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -070094}
95
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080096uint64_t Context::getTime() const {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -070097#ifndef ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -070098 struct timespec t;
99 clock_gettime(CLOCK_MONOTONIC, &t);
100 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700101#else
102 return 0;
103#endif //ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -0700104}
105
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800106void Context::timerReset() {
Jason Sams24371d92009-08-19 12:17:14 -0700107 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
108 mTimers[ct] = 0;
109 }
110}
111
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800112void Context::timerInit() {
Jason Sams24371d92009-08-19 12:17:14 -0700113 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700114 mTimeFrame = mTimeLast;
115 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700116 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700117 mAverageFPSFrameCount = 0;
118 mAverageFPSStartTime = mTimeLast;
119 mAverageFPS = 0;
Jason Sams24371d92009-08-19 12:17:14 -0700120 timerReset();
121}
122
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800123void Context::timerFrame() {
Jason Sams1d54f102009-09-03 15:43:13 -0700124 mTimeLastFrame = mTimeFrame;
125 mTimeFrame = getTime();
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700126 // Update average fps
127 const uint64_t averageFramerateInterval = 1000 * 1000000;
128 mAverageFPSFrameCount ++;
129 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800130 if (inverval >= averageFramerateInterval) {
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700131 inverval = inverval / 1000000;
132 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
133 mAverageFPSFrameCount = 0;
134 mAverageFPSStartTime = mTimeFrame;
135 }
Jason Sams1d54f102009-09-03 15:43:13 -0700136}
137
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800138void Context::timerSet(Timers tm) {
Jason Sams24371d92009-08-19 12:17:14 -0700139 uint64_t last = mTimeLast;
140 mTimeLast = getTime();
141 mTimers[mTimerActive] += mTimeLast - last;
142 mTimerActive = tm;
143}
144
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800145void Context::timerPrint() {
Jason Sams24371d92009-08-19 12:17:14 -0700146 double total = 0;
147 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
148 total += mTimers[ct];
149 }
Jason Sams1d54f102009-09-03 15:43:13 -0700150 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800151 mTimeMSLastFrame = frame / 1000000;
152 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
153 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700154
Jason Sams2dca84d2009-12-09 11:05:45 -0800155
156 if (props.mLogTimes) {
Steve Block65982012011-10-20 11:56:00 +0100157 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 -0800158 mTimeMSLastFrame,
159 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
160 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
161 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700162 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
163 mAverageFPS);
Jason Sams2dca84d2009-12-09 11:05:45 -0800164 }
Jason Sams24371d92009-08-19 12:17:14 -0700165}
166
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800167bool Context::setupCheck() {
Jason Sams900f1612010-09-16 18:18:29 -0700168
Jason Sams721acc42011-04-06 11:23:54 -0700169 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700170 mFragment->setup(this, &mStateFragment);
Jason Sams721acc42011-04-06 11:23:54 -0700171 mRaster->setup(this, &mStateRaster);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700172 mVertex->setup(this, &mStateVertex);
173 mFBOCache.setup(this);
Jason Samsa2cf7552010-03-03 13:03:18 -0800174 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700175}
176
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700177void Context::setupProgramStore() {
Jason Sams721acc42011-04-06 11:23:54 -0700178 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700179}
180
Jason Samsd1f7da62012-03-15 19:18:03 -0700181static uint32_t getProp(const char *str) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700182 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700183 property_get(str, buf, "0");
Jason Samsd1f7da62012-03-15 19:18:03 -0700184 return atoi(buf);
Joe Onorato76371ff2009-09-23 16:37:36 -0700185}
Jason Sams326e0dd2009-05-22 14:03:28 -0700186
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800187void Context::displayDebugStats() {
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700188 char buffer[128];
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700189 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700190 float oldR, oldG, oldB, oldA;
191 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700192 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700193
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700194 ObjectBaseRef<Font> lastFont(getFont());
195 setFont(NULL);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700196 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700197 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700198 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700199
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700200 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700201 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700202
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700203 setFont(lastFont.get());
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700204 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700205}
206
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800207void * Context::threadProc(void *vrsc) {
Jason Sams83c451a2011-04-21 11:46:50 -0700208 Context *rsc = static_cast<Context *>(vrsc);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700209#ifndef ANDROID_RS_SERIALIZE
Jason Sams83c451a2011-04-21 11:46:50 -0700210 rsc->mNativeThreadId = gettid();
Jason Sams83c451a2011-04-21 11:46:50 -0700211 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
212 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700213#endif //ANDROID_RS_SERIALIZE
Jason Samsd1f7da62012-03-15 19:18:03 -0700214 rsc->props.mLogTimes = getProp("debug.rs.profile") != 0;
215 rsc->props.mLogScripts = getProp("debug.rs.script") != 0;
216 rsc->props.mLogObjects = getProp("debug.rs.object") != 0;
217 rsc->props.mLogShaders = getProp("debug.rs.shader") != 0;
218 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes") != 0;
219 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms") != 0;
220 rsc->props.mLogVisual = getProp("debug.rs.visual") != 0;
221 rsc->props.mDebugMaxThreads = getProp("debug.rs.max-threads");
Joe Onorato76371ff2009-09-23 16:37:36 -0700222
Jason Sams83c451a2011-04-21 11:46:50 -0700223 if (!rsdHalInit(rsc, 0, 0)) {
Jason Samsd3e71072011-05-03 15:01:58 -0700224 rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed initializing GL");
Steve Blockaf12ac62012-01-06 19:20:56 +0000225 ALOGE("Hal init failed");
Jason Sams83c451a2011-04-21 11:46:50 -0700226 return NULL;
227 }
228 rsc->mHal.funcs.setPriority(rsc, rsc->mThreadPriority);
Jason Samse5769102009-06-19 16:03:18 -0700229
Jason Sams83c451a2011-04-21 11:46:50 -0700230 if (rsc->mIsGraphicsContext) {
Jason Samsd3e71072011-05-03 15:01:58 -0700231 if (!rsc->initGLThread()) {
232 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
233 return NULL;
234 }
235
Jason Sams83c451a2011-04-21 11:46:50 -0700236 rsc->mStateRaster.init(rsc);
237 rsc->setProgramRaster(NULL);
238 rsc->mStateVertex.init(rsc);
239 rsc->setProgramVertex(NULL);
240 rsc->mStateFragment.init(rsc);
241 rsc->setProgramFragment(NULL);
242 rsc->mStateFragmentStore.init(rsc);
243 rsc->setProgramStore(NULL);
244 rsc->mStateFont.init(rsc);
245 rsc->setFont(NULL);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700246 rsc->mStateSampler.init(rsc);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700247 rsc->mFBOCache.init(rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700248 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700249
Jason Sams83c451a2011-04-21 11:46:50 -0700250 rsc->mRunning = true;
Jason Sams5f27d6f2012-02-07 15:32:08 -0800251 if (!rsc->mIsGraphicsContext) {
252 while (!rsc->mExit) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800253 rsc->mIO.playCoreCommands(rsc, -1);
Jason Samse0aab4a2011-08-12 15:05:15 -0700254 }
Jason Sams5f27d6f2012-02-07 15:32:08 -0800255 } else {
256#ifndef ANDROID_RS_SERIALIZE
257 DisplayEventReceiver displayEvent;
258 DisplayEventReceiver::Event eventBuffer[1];
259#endif
260 int vsyncRate = 0;
261 int targetRate = 0;
Jason Samse0aab4a2011-08-12 15:05:15 -0700262
Jason Sams5f27d6f2012-02-07 15:32:08 -0800263 bool drawOnce = false;
264 while (!rsc->mExit) {
265 rsc->timerSet(RS_TIMER_IDLE);
Jason Sams326e0dd2009-05-22 14:03:28 -0700266
Jason Sams5f27d6f2012-02-07 15:32:08 -0800267#ifndef ANDROID_RS_SERIALIZE
Jason Sams9afd9a52012-02-17 16:59:50 -0800268 if (!rsc->mRootScript.get() || !rsc->mHasSurface || rsc->mPaused) {
269 targetRate = 0;
270 }
271
Jason Sams5f27d6f2012-02-07 15:32:08 -0800272 if (vsyncRate != targetRate) {
273 displayEvent.setVsyncRate(targetRate);
274 vsyncRate = targetRate;
275 }
276 if (targetRate) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800277 drawOnce |= rsc->mIO.playCoreCommands(rsc, displayEvent.getFd());
Jason Sams5f27d6f2012-02-07 15:32:08 -0800278 while (displayEvent.getEvents(eventBuffer, 1) != 0) {
279 //ALOGE("vs2 time past %lld", (rsc->getTime() - eventBuffer[0].header.timestamp) / 1000000);
280 }
281 } else
282#endif
283 {
Jason Sams963a2fb2012-02-09 14:36:14 -0800284 drawOnce |= rsc->mIO.playCoreCommands(rsc, -1);
Jason Sams83c451a2011-04-21 11:46:50 -0700285 }
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700286
Jason Sams5f27d6f2012-02-07 15:32:08 -0800287 if ((rsc->mRootScript.get() != NULL) && rsc->mHasSurface &&
288 (targetRate || drawOnce) && !rsc->mPaused) {
289
290 drawOnce = false;
291 targetRate = ((rsc->runRootScript() + 15) / 16);
292
293 if (rsc->props.mLogVisual) {
294 rsc->displayDebugStats();
295 }
296
297 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
298 rsc->mHal.funcs.swap(rsc);
299 rsc->timerFrame();
300 rsc->timerSet(RS_TIMER_INTERNAL);
301 rsc->timerPrint();
302 rsc->timerReset();
303 }
Jason Sams83c451a2011-04-21 11:46:50 -0700304 }
Jason Sams83c451a2011-04-21 11:46:50 -0700305 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700306
Steve Block65982012011-10-20 11:56:00 +0100307 ALOGV("%p RS Thread exiting", rsc);
Jason Samse514b452009-09-25 14:51:22 -0700308
Jason Sams83c451a2011-04-21 11:46:50 -0700309 if (rsc->mIsGraphicsContext) {
310 pthread_mutex_lock(&gInitMutex);
311 rsc->deinitEGL();
312 pthread_mutex_unlock(&gInitMutex);
313 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700314
Steve Block65982012011-10-20 11:56:00 +0100315 ALOGV("%p RS Thread exited", rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700316 return NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700317}
318
Jason Sams741aac92010-12-24 14:38:39 -0800319void Context::destroyWorkerThreadResources() {
Steve Block65982012011-10-20 11:56:00 +0100320 //ALOGV("destroyWorkerThreadResources 1");
Jason Sams2e8665d2011-01-27 00:14:13 -0800321 ObjectBase::zeroAllUserRef(this);
Jason Sams741aac92010-12-24 14:38:39 -0800322 if (mIsGraphicsContext) {
323 mRaster.clear();
324 mFragment.clear();
325 mVertex.clear();
326 mFragmentStore.clear();
327 mFont.clear();
328 mRootScript.clear();
329 mStateRaster.deinit(this);
330 mStateVertex.deinit(this);
331 mStateFragment.deinit(this);
332 mStateFragmentStore.deinit(this);
333 mStateFont.deinit(this);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700334 mStateSampler.deinit(this);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700335 mFBOCache.deinit(this);
Jason Sams741aac92010-12-24 14:38:39 -0800336 }
Jason Samsc7cec1e2011-08-18 18:01:33 -0700337 ObjectBase::freeAllChildren(this);
Jason Samscf912de2011-01-09 16:09:51 -0800338 mExit = true;
Jason Sams5f27d6f2012-02-07 15:32:08 -0800339 //ALOGV("destroyWorkerThreadResources 2");
Jason Sams741aac92010-12-24 14:38:39 -0800340}
341
Jason Sams2382aba2011-09-13 15:41:01 -0700342void Context::printWatchdogInfo(void *ctx) {
343 Context *rsc = (Context *)ctx;
Jason Samsee803442011-10-13 16:05:27 -0700344 if (rsc->watchdog.command && rsc->watchdog.file) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000345 ALOGE("RS watchdog timeout: %i %s line %i %s", rsc->watchdog.inRoot,
Jason Samsee803442011-10-13 16:05:27 -0700346 rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file);
347 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000348 ALOGE("RS watchdog timeout: %i", rsc->watchdog.inRoot);
Jason Samsee803442011-10-13 16:05:27 -0700349 }
Jason Sams2382aba2011-09-13 15:41:01 -0700350}
351
352
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800353void Context::setPriority(int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800354 // Note: If we put this in the proper "background" policy
355 // the wallpapers can become completly unresponsive at times.
356 // This is probably not what we want for something the user is actively
357 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800358 mThreadPriority = p;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700359 setpriority(PRIO_PROCESS, mNativeThreadId, p);
Jason Sams9719bd42012-01-12 14:22:21 -0800360 mHal.funcs.setPriority(this, mThreadPriority);
Jason Sams15832442009-11-15 12:14:26 -0800361}
362
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800363Context::Context() {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700364 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700365 mRunning = false;
366 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700367 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700368 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800369 mError = RS_ERROR_NONE;
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700370 mTargetSdkVersion = 14;
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700371 mDPI = 96;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700372 mIsContextLite = false;
Stephen Hines86c6b5f2011-10-31 14:07:54 -0700373 memset(&watchdog, 0, sizeof(watchdog));
Jason Sams5c1c79a2010-11-03 14:27:11 -0700374}
375
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800376Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700377 Context * rsc = new Context();
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700378
Jason Sams5c1c79a2010-11-03 14:27:11 -0700379 if (!rsc->initContext(dev, sc)) {
380 delete rsc;
381 return NULL;
382 }
383 return rsc;
384}
385
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700386Context * Context::createContextLite() {
387 Context * rsc = new Context();
388 rsc->mIsContextLite = true;
389 return rsc;
390}
391
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800392bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700393 pthread_mutex_lock(&gInitMutex);
394
Jason Sams1a4efa32011-05-17 15:01:29 -0700395 mIO.init();
Jason Sams5f27d6f2012-02-07 15:32:08 -0800396 mIO.setTimeoutCallback(printWatchdogInfo, this, 2e9);
Jason Sams1a4efa32011-05-17 15:01:29 -0700397
Jason Sams5c1c79a2010-11-03 14:27:11 -0700398 dev->addContext(this);
399 mDev = dev;
Jason Sams6b8552a2010-10-13 15:31:10 -0700400 if (sc) {
401 mUserSurfaceConfig = *sc;
402 } else {
403 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
404 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800405
Jason Sams6b8552a2010-10-13 15:31:10 -0700406 mIsGraphicsContext = sc != NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700407
Jason Samsa658e902009-06-04 14:35:01 -0700408 int status;
409 pthread_attr_t threadAttr;
410
Jason Samsfb03a222009-10-15 16:47:31 -0700411 pthread_mutex_unlock(&gInitMutex);
412
413 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700414
Jason Samsa658e902009-06-04 14:35:01 -0700415 status = pthread_attr_init(&threadAttr);
416 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000417 ALOGE("Failed to init thread attribute.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700418 return false;
Jason Samsa658e902009-06-04 14:35:01 -0700419 }
420
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700421 mHasSurface = false;
Jason Sams992a0b72009-06-23 12:22:47 -0700422
Jason Sams24371d92009-08-19 12:17:14 -0700423 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700424 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700425
Jason Samsa658e902009-06-04 14:35:01 -0700426 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700427 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000428 ALOGE("Failed to start rs context thread.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700429 return false;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700430 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800431 while (!mRunning && (mError == RS_ERROR_NONE)) {
Jason Sams18133402010-07-20 15:09:00 -0700432 usleep(100);
433 }
434
Jason Sams5c1c79a2010-11-03 14:27:11 -0700435 if (mError != RS_ERROR_NONE) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000436 ALOGE("Errors during thread init");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700437 return false;
438 }
439
Jason Samsa658e902009-06-04 14:35:01 -0700440 pthread_attr_destroy(&threadAttr);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700441 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700442}
443
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800444Context::~Context() {
Steve Block65982012011-10-20 11:56:00 +0100445 ALOGV("%p Context::~Context", this);
Jason Samscf912de2011-01-09 16:09:51 -0800446
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700447 if (!mIsContextLite) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700448 mPaused = false;
449 void *res;
Jason Sams326e0dd2009-05-22 14:03:28 -0700450
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700451 mIO.shutdown();
452 int status = pthread_join(mThreadId, &res);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800453 rsAssert(mExit);
Jason Sams326e0dd2009-05-22 14:03:28 -0700454
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700455 if (mHal.funcs.shutdownDriver) {
456 mHal.funcs.shutdownDriver(this);
457 }
458
459 // Global structure cleanup.
460 pthread_mutex_lock(&gInitMutex);
461 if (mDev) {
462 mDev->removeContext(this);
463 mDev = NULL;
464 }
465 pthread_mutex_unlock(&gInitMutex);
Jason Sams51462c52011-01-25 00:26:25 -0800466 }
Steve Block65982012011-10-20 11:56:00 +0100467 ALOGV("%p Context::~Context done", this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700468}
469
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700470void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800471 rsAssert(mIsGraphicsContext);
Jason Sams4b3de472011-04-06 17:52:23 -0700472 mHal.funcs.setSurface(this, w, h, sur);
Jason Sams458f2dc2009-11-03 13:58:36 -0800473
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700474 mHasSurface = sur != NULL;
Jason Sams4b3de472011-04-06 17:52:23 -0700475 mWidth = w;
476 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800477
Jason Sams4b3de472011-04-06 17:52:23 -0700478 if (mWidth && mHeight) {
Jason Sams771565f2010-05-14 15:30:29 -0700479 mStateVertex.updateSize(this);
Alex Sakhartchouka544b632011-07-19 17:50:29 -0700480 mFBOCache.updateSize();
Jason Sams458f2dc2009-11-03 13:58:36 -0800481 }
482}
483
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800484uint32_t Context::getCurrentSurfaceWidth() const {
485 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
486 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
487 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimX();
488 }
489 }
490 if (mFBOCache.mHal.state.depthTarget != NULL) {
491 return mFBOCache.mHal.state.depthTarget->getType()->getDimX();
492 }
493 return mWidth;
494}
495
496uint32_t Context::getCurrentSurfaceHeight() const {
497 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
498 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
499 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimY();
500 }
501 }
502 if (mFBOCache.mHal.state.depthTarget != NULL) {
503 return mFBOCache.mHal.state.depthTarget->getType()->getDimY();
504 }
505 return mHeight;
506}
507
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800508void Context::pause() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800509 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700510 mPaused = true;
511}
512
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800513void Context::resume() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800514 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700515 mPaused = false;
516}
517
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800518void Context::setRootScript(Script *s) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800519 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700520 mRootScript.set(s);
521}
522
Jason Sams60709252010-11-17 15:29:32 -0800523void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800524 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700525 if (pfs == NULL) {
526 mFragmentStore.set(mStateFragmentStore.mDefault);
527 } else {
528 mFragmentStore.set(pfs);
529 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700530}
531
Jason Sams60709252010-11-17 15:29:32 -0800532void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800533 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700534 if (pf == NULL) {
535 mFragment.set(mStateFragment.mDefault);
536 } else {
537 mFragment.set(pf);
538 }
Jason Samscfb1d112009-08-05 13:57:03 -0700539}
540
Jason Sams60709252010-11-17 15:29:32 -0800541void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800542 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700543 if (pr == NULL) {
544 mRaster.set(mStateRaster.mDefault);
545 } else {
546 mRaster.set(pr);
547 }
548}
549
Jason Sams60709252010-11-17 15:29:32 -0800550void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800551 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700552 if (pv == NULL) {
553 mVertex.set(mStateVertex.mDefault);
554 } else {
555 mVertex.set(pv);
556 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700557}
558
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800559void Context::setFont(Font *f) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700560 rsAssert(mIsGraphicsContext);
561 if (f == NULL) {
562 mFont.set(mStateFont.mDefault);
563 } else {
564 mFont.set(f);
565 }
566}
567
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800568void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700569 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700570 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700571 mNames.add(obj);
572}
573
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800574void Context::removeName(ObjectBase *obj) {
575 for (size_t ct=0; ct < mNames.size(); ct++) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700576 if (obj == mNames[ct]) {
577 mNames.removeAt(ct);
578 return;
579 }
580 }
581}
582
Jason Sams1a4efa32011-05-17 15:01:29 -0700583RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) {
584 return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800585}
586
Jason Sams1a4efa32011-05-17 15:01:29 -0700587RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) {
588 return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen);
Jason Sams8c401ef2009-10-06 13:58:47 -0700589}
590
Jason Sams87319de2010-11-22 16:20:16 -0800591bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
592 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Sams1a4efa32011-05-17 15:01:29 -0700593
594 return mIO.sendToClient(cmdID, subID, data, len, waitForSpace);
Jason Sams8c401ef2009-10-06 13:58:47 -0700595}
596
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800597void Context::initToClient() {
598 while (!mRunning) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700599 usleep(100);
600 }
601}
602
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800603void Context::deinitToClient() {
Jason Sams1a4efa32011-05-17 15:01:29 -0700604 mIO.clientShutdown();
Jason Sams8c401ef2009-10-06 13:58:47 -0700605}
Jason Sams50869382009-08-18 17:07:09 -0700606
Jason Sams87319de2010-11-22 16:20:16 -0800607void Context::setError(RsError e, const char *msg) const {
Jason Samsa2cf7552010-03-03 13:03:18 -0800608 mError = e;
Jason Samsaad4bc52010-11-08 17:06:46 -0800609 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Samsa2cf7552010-03-03 13:03:18 -0800610}
611
612
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800613void Context::dumpDebug() const {
Steve Blockaf12ac62012-01-06 19:20:56 +0000614 ALOGE("RS Context debug %p", this);
615 ALOGE("RS Context debug");
Jason Sams13e26342009-11-24 12:26:35 -0800616
Steve Blockaf12ac62012-01-06 19:20:56 +0000617 ALOGE(" RS width %i, height %i", mWidth, mHeight);
618 ALOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
619 ALOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId);
Jason Sams13e26342009-11-24 12:26:35 -0800620}
Jason Samsa4a54e42009-06-10 18:39:40 -0700621
Jason Sams326e0dd2009-05-22 14:03:28 -0700622///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700623//
Jason Sams326e0dd2009-05-22 14:03:28 -0700624
625namespace android {
626namespace renderscript {
627
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800628void rsi_ContextFinish(Context *rsc) {
Jason Sams8c880902010-06-15 12:15:57 -0700629}
Jason Sams326e0dd2009-05-22 14:03:28 -0700630
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800631void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700632 Script *s = static_cast<Script *>(vs);
633 rsc->setRootScript(s);
634}
635
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800636void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700637 Sampler *s = static_cast<Sampler *>(vs);
638
639 if (slot > RS_MAX_SAMPLER_SLOT) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000640 ALOGE("Invalid sampler slot");
Jason Sams326e0dd2009-05-22 14:03:28 -0700641 return;
642 }
643
644 s->bindToContext(&rsc->mStateSampler, slot);
645}
646
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800647void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Samsccc010b2010-05-13 18:30:11 -0700648 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams60709252010-11-17 15:29:32 -0800649 rsc->setProgramStore(pfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700650}
651
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800652void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700653 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Sams60709252010-11-17 15:29:32 -0800654 rsc->setProgramFragment(pf);
Jason Sams326e0dd2009-05-22 14:03:28 -0700655}
656
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800657void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Sams5fd09d82009-09-23 13:57:02 -0700658 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Sams60709252010-11-17 15:29:32 -0800659 rsc->setProgramRaster(pr);
Jason Sams5fd09d82009-09-23 13:57:02 -0700660}
661
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800662void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700663 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Sams60709252010-11-17 15:29:32 -0800664 rsc->setProgramVertex(pv);
Jason Sams326e0dd2009-05-22 14:03:28 -0700665}
666
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800667void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700668 Font *font = static_cast<Font *>(vfont);
669 rsc->setFont(font);
670}
671
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700672void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700673 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700674 rsc->assignName(ob, name, name_length);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700675}
Jason Sams326e0dd2009-05-22 14:03:28 -0700676
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800677void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams2353ae32010-10-14 17:48:46 -0700678 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -0700679 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700680 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700681}
682
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800683void rsi_ContextPause(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700684 rsc->pause();
685}
686
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800687void rsi_ContextResume(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700688 rsc->resume();
689}
690
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700691void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
Mathias Agopianfa402862010-02-12 14:04:35 -0800692 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800693}
694
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800695void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800696 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800697}
698
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800699void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Samsc21cf402009-11-17 17:26:46 -0800700 ObjectBase::dumpAll(rsc);
701}
702
Jason Sams741aac92010-12-24 14:38:39 -0800703void rsi_ContextDestroyWorker(Context *rsc) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700704 rsc->destroyWorkerThreadResources();
Jason Sams741aac92010-12-24 14:38:39 -0800705}
706
Jason Samsc975cf42011-04-28 18:26:48 -0700707void rsi_ContextDestroy(Context *rsc) {
Steve Block65982012011-10-20 11:56:00 +0100708 ALOGV("%p rsContextDestroy", rsc);
Jason Sams741aac92010-12-24 14:38:39 -0800709 rsContextDestroyWorker(rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800710 delete rsc;
Steve Block65982012011-10-20 11:56:00 +0100711 ALOGV("%p rsContextDestroy done", rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800712}
713
Jason Sams326e0dd2009-05-22 14:03:28 -0700714
Jason Samsc975cf42011-04-28 18:26:48 -0700715RsMessageToClientType rsi_ContextPeekMessage(Context *rsc,
Jason Sams186e5912011-04-26 14:50:00 -0700716 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700717 uint32_t * subID, size_t subID_length) {
718 return rsc->peekMessageToClient(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800719}
720
Jason Samsc975cf42011-04-28 18:26:48 -0700721RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length,
Jason Sams186e5912011-04-26 14:50:00 -0700722 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700723 uint32_t * subID, size_t subID_length) {
Jason Sams186e5912011-04-26 14:50:00 -0700724 rsAssert(subID_length == sizeof(uint32_t));
725 rsAssert(receiveLen_length == sizeof(size_t));
Jason Sams1a4efa32011-05-17 15:01:29 -0700726 return rsc->getMessageToClient(data, receiveLen, subID, data_length);
Jason Sams8c401ef2009-10-06 13:58:47 -0700727}
728
Jason Samsc975cf42011-04-28 18:26:48 -0700729void rsi_ContextInitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700730 rsc->initToClient();
731}
732
Jason Samsc975cf42011-04-28 18:26:48 -0700733void rsi_ContextDeinitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700734 rsc->deinitToClient();
735}
736
Jason Samsc975cf42011-04-28 18:26:48 -0700737}
738}
739
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700740RsContext rsContextCreate(RsDevice vdev, uint32_t version,
741 uint32_t sdkVersion) {
Steve Block65982012011-10-20 11:56:00 +0100742 ALOGV("rsContextCreate dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700743 Device * dev = static_cast<Device *>(vdev);
744 Context *rsc = Context::createContext(dev, NULL);
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700745 if (rsc) {
746 rsc->setTargetSdkVersion(sdkVersion);
747 }
Jason Sams789ca832011-05-18 17:36:02 -0700748 return rsc;
749}
750
751RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700752 uint32_t sdkVersion, RsSurfaceConfig sc,
753 uint32_t dpi) {
Steve Block65982012011-10-20 11:56:00 +0100754 ALOGV("rsContextCreateGL dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700755 Device * dev = static_cast<Device *>(vdev);
756 Context *rsc = Context::createContext(dev, &sc);
Jason Sams9544f762011-07-13 16:09:42 -0700757 if (rsc) {
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700758 rsc->setTargetSdkVersion(sdkVersion);
Jason Sams9544f762011-07-13 16:09:42 -0700759 rsc->setDPI(dpi);
760 }
Steve Block65982012011-10-20 11:56:00 +0100761 ALOGV("%p rsContextCreateGL ret", rsc);
Jason Sams789ca832011-05-18 17:36:02 -0700762 return rsc;
763}
764
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700765// Only to be called at a3d load time, before object is visible to user
766// not thread safe
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800767void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700768 ObjectBase *ob = static_cast<ObjectBase *>(obj);
769 (*name) = ob->getName();
770}