blob: ea9c4065e82f965afdef2bb0a0ccabfd296d020f [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>
Jason Sams15832442009-11-15 12:14:26 -080032
Stephen Hines414a4612012-09-05 18:05:08 -070033#include <dlfcn.h>
34
Jason Sams326e0dd2009-05-22 14:03:28 -070035using namespace android;
36using namespace android::renderscript;
37
Jason Samsfb03a222009-10-15 16:47:31 -070038pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Stephen Hinesca3f09c2011-01-07 15:11:30 -080039pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070040
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080041bool Context::initGLThread() {
Jason Sams6b8552a2010-10-13 15:31:10 -070042 pthread_mutex_lock(&gInitMutex);
Jason Sams6b8552a2010-10-13 15:31:10 -070043
Jason Sams4b3de472011-04-06 17:52:23 -070044 if (!mHal.funcs.initGraphics(this)) {
Jason Sams5c1c79a2010-11-03 14:27:11 -070045 pthread_mutex_unlock(&gInitMutex);
Steve Blockaf12ac62012-01-06 19:20:56 +000046 ALOGE("%p initGraphics failed", this);
Jason Sams5c1c79a2010-11-03 14:27:11 -070047 return false;
Jason Sams6b8552a2010-10-13 15:31:10 -070048 }
49
Jason Sams6b8552a2010-10-13 15:31:10 -070050 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -070051 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -070052}
53
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080054void Context::deinitEGL() {
Jason Sams4b3de472011-04-06 17:52:23 -070055 mHal.funcs.shutdownGraphics(this);
Jason Sams33b6e3b2009-10-27 14:44:31 -070056}
57
Jason Sams60709252010-11-17 15:29:32 -080058Context::PushState::PushState(Context *con) {
59 mRsc = con;
Jason Samsc946b612011-02-23 14:47:17 -080060 if (con->mIsGraphicsContext) {
61 mFragment.set(con->getProgramFragment());
62 mVertex.set(con->getProgramVertex());
63 mStore.set(con->getProgramStore());
64 mRaster.set(con->getProgramRaster());
65 mFont.set(con->getFont());
66 }
Jason Sams60709252010-11-17 15:29:32 -080067}
68
69Context::PushState::~PushState() {
Jason Samsc946b612011-02-23 14:47:17 -080070 if (mRsc->mIsGraphicsContext) {
71 mRsc->setProgramFragment(mFragment.get());
72 mRsc->setProgramVertex(mVertex.get());
73 mRsc->setProgramStore(mStore.get());
74 mRsc->setProgramRaster(mRaster.get());
75 mRsc->setFont(mFont.get());
76 }
Jason Sams60709252010-11-17 15:29:32 -080077}
78
Jason Sams33b6e3b2009-10-27 14:44:31 -070079
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080080uint32_t Context::runScript(Script *s) {
Shih-wei Liaoda3b58d2012-08-03 04:24:33 -070081 PushState ps(this);
Jason Sams10308932009-06-09 12:15:30 -070082
Jason Samsc61346b2010-05-28 18:23:22 -070083 uint32_t ret = s->run(this);
Jason Samsc9d43db2009-07-28 12:02:16 -070084 return ret;
Jason Sams10308932009-06-09 12:15:30 -070085}
86
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080087uint32_t Context::runRootScript() {
Jason Sams2dca84d2009-12-09 11:05:45 -080088 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -070089 mStateFragmentStore.mLast.clear();
Jason Sams2382aba2011-09-13 15:41:01 -070090 watchdog.inRoot = true;
Jason Samsc61346b2010-05-28 18:23:22 -070091 uint32_t ret = runScript(mRootScript.get());
Jason Sams2382aba2011-09-13 15:41:01 -070092 watchdog.inRoot = false;
Jason Sams8cfdd242009-10-14 15:43:53 -070093
Jason Samscfb1d112009-08-05 13:57:03 -070094 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -070095}
96
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080097uint64_t Context::getTime() const {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -070098#ifndef ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -070099 struct timespec t;
100 clock_gettime(CLOCK_MONOTONIC, &t);
101 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700102#else
103 return 0;
104#endif //ANDROID_RS_SERIALIZE
Jason Sams24371d92009-08-19 12:17:14 -0700105}
106
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800107void Context::timerReset() {
Jason Sams24371d92009-08-19 12:17:14 -0700108 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
109 mTimers[ct] = 0;
110 }
111}
112
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800113void Context::timerInit() {
Jason Sams24371d92009-08-19 12:17:14 -0700114 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700115 mTimeFrame = mTimeLast;
116 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700117 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700118 mAverageFPSFrameCount = 0;
119 mAverageFPSStartTime = mTimeLast;
120 mAverageFPS = 0;
Jason Sams24371d92009-08-19 12:17:14 -0700121 timerReset();
122}
123
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800124void Context::timerFrame() {
Jason Sams1d54f102009-09-03 15:43:13 -0700125 mTimeLastFrame = mTimeFrame;
126 mTimeFrame = getTime();
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700127 // Update average fps
128 const uint64_t averageFramerateInterval = 1000 * 1000000;
129 mAverageFPSFrameCount ++;
130 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800131 if (inverval >= averageFramerateInterval) {
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700132 inverval = inverval / 1000000;
133 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
134 mAverageFPSFrameCount = 0;
135 mAverageFPSStartTime = mTimeFrame;
136 }
Jason Sams1d54f102009-09-03 15:43:13 -0700137}
138
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800139void Context::timerSet(Timers tm) {
Jason Sams24371d92009-08-19 12:17:14 -0700140 uint64_t last = mTimeLast;
141 mTimeLast = getTime();
142 mTimers[mTimerActive] += mTimeLast - last;
143 mTimerActive = tm;
144}
145
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800146void Context::timerPrint() {
Jason Sams24371d92009-08-19 12:17:14 -0700147 double total = 0;
148 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
149 total += mTimers[ct];
150 }
Jason Sams1d54f102009-09-03 15:43:13 -0700151 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800152 mTimeMSLastFrame = frame / 1000000;
153 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
154 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700155
Jason Sams2dca84d2009-12-09 11:05:45 -0800156
157 if (props.mLogTimes) {
Steve Block65982012011-10-20 11:56:00 +0100158 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 -0800159 mTimeMSLastFrame,
160 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
161 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
162 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700163 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
164 mAverageFPS);
Jason Sams2dca84d2009-12-09 11:05:45 -0800165 }
Jason Sams24371d92009-08-19 12:17:14 -0700166}
167
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800168bool Context::setupCheck() {
Jason Sams900f1612010-09-16 18:18:29 -0700169
Jason Sams721acc42011-04-06 11:23:54 -0700170 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700171 mFragment->setup(this, &mStateFragment);
Jason Sams721acc42011-04-06 11:23:54 -0700172 mRaster->setup(this, &mStateRaster);
Alex Sakhartchoukc19ff012011-05-06 14:59:45 -0700173 mVertex->setup(this, &mStateVertex);
174 mFBOCache.setup(this);
Jason Samsa2cf7552010-03-03 13:03:18 -0800175 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700176}
177
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700178void Context::setupProgramStore() {
Jason Sams721acc42011-04-06 11:23:54 -0700179 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700180}
181
Jason Samsd1f7da62012-03-15 19:18:03 -0700182static uint32_t getProp(const char *str) {
Joe Onorato76371ff2009-09-23 16:37:36 -0700183 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700184 property_get(str, buf, "0");
Jason Samsd1f7da62012-03-15 19:18:03 -0700185 return atoi(buf);
Joe Onorato76371ff2009-09-23 16:37:36 -0700186}
Jason Sams326e0dd2009-05-22 14:03:28 -0700187
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800188void Context::displayDebugStats() {
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700189 char buffer[128];
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700190 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700191 float oldR, oldG, oldB, oldA;
192 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700193 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700194
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700195 ObjectBaseRef<Font> lastFont(getFont());
196 setFont(NULL);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700197 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700198 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700199 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700200
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700201 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700202 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700203
Alex Sakhartchouk1809bde2011-03-17 13:49:38 -0700204 setFont(lastFont.get());
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700205 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700206}
207
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800208void * Context::threadProc(void *vrsc) {
Jason Sams83c451a2011-04-21 11:46:50 -0700209 Context *rsc = static_cast<Context *>(vrsc);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700210#ifndef ANDROID_RS_SERIALIZE
Jason Sams83c451a2011-04-21 11:46:50 -0700211 rsc->mNativeThreadId = gettid();
Jason Sams83c451a2011-04-21 11:46:50 -0700212 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
213 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700214#endif //ANDROID_RS_SERIALIZE
Jason Samsd1f7da62012-03-15 19:18:03 -0700215 rsc->props.mLogTimes = getProp("debug.rs.profile") != 0;
216 rsc->props.mLogScripts = getProp("debug.rs.script") != 0;
217 rsc->props.mLogObjects = getProp("debug.rs.object") != 0;
218 rsc->props.mLogShaders = getProp("debug.rs.shader") != 0;
219 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes") != 0;
220 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms") != 0;
221 rsc->props.mLogVisual = getProp("debug.rs.visual") != 0;
222 rsc->props.mDebugMaxThreads = getProp("debug.rs.max-threads");
Joe Onorato76371ff2009-09-23 16:37:36 -0700223
Stephen Hines91dfcdb2012-09-07 18:23:35 -0700224 void *driverSO = NULL;
225
226 // Provide a mechanism for dropping in a different RS driver.
227#ifdef OVERRIDE_RS_DRIVER
228#define XSTR(S) #S
229#define STR(S) XSTR(S)
230#define OVERRIDE_RS_DRIVER_STRING STR(OVERRIDE_RS_DRIVER)
231 driverSO = dlopen(OVERRIDE_RS_DRIVER_STRING, RTLD_LAZY);
Stephen Hines414a4612012-09-05 18:05:08 -0700232 if (driverSO == NULL) {
Stephen Hines91dfcdb2012-09-07 18:23:35 -0700233 ALOGE("Failed loading %s: %s", OVERRIDE_RS_DRIVER_STRING, dlerror());
234 // Continue to attempt loading fallback driver
235 }
236#undef XSTR
237#undef STR
238#endif // OVERRIDE_RS_DRIVER
239
240 // Attempt to load the reference RS driver (if necessary).
241 if (driverSO == NULL) {
242 driverSO = dlopen("libRSDriver.so", RTLD_LAZY);
243 if (driverSO == NULL) {
244 rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed loading RS driver");
245 ALOGE("Failed loading RS driver: %s", dlerror());
246 return NULL;
247 }
Stephen Hines414a4612012-09-05 18:05:08 -0700248 }
249
250 // Need to call dlerror() to clear buffer before using it for dlsym().
251 (void) dlerror();
252 typedef bool (*HalSig)(Context*, uint32_t, uint32_t);
253 HalSig halInit = (HalSig) dlsym(driverSO, "rsdHalInit");
254
255 // If we can't find the C variant, we go looking for the C++ version.
256 if (halInit == NULL) {
257 ALOGW("Falling back to find C++ rsdHalInit: %s", dlerror());
258 halInit = (HalSig) dlsym(driverSO,
259 "_Z10rsdHalInitPN7android12renderscript7ContextEjj");
260 }
261
262 if (halInit == NULL) {
263 rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed to find rsdHalInit");
264 dlclose(driverSO);
265 ALOGE("Failed to find rsdHalInit: %s", dlerror());
266 return NULL;
267 }
268
269 if (!(*halInit)(rsc, 0, 0)) {
270 rsc->setError(RS_ERROR_FATAL_DRIVER, "Failed initializing RS Driver");
271 dlclose(driverSO);
Steve Blockaf12ac62012-01-06 19:20:56 +0000272 ALOGE("Hal init failed");
Jason Sams83c451a2011-04-21 11:46:50 -0700273 return NULL;
274 }
275 rsc->mHal.funcs.setPriority(rsc, rsc->mThreadPriority);
Jason Samse5769102009-06-19 16:03:18 -0700276
Jason Sams83c451a2011-04-21 11:46:50 -0700277 if (rsc->mIsGraphicsContext) {
Jason Samsd3e71072011-05-03 15:01:58 -0700278 if (!rsc->initGLThread()) {
279 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
280 return NULL;
281 }
282
Jason Sams83c451a2011-04-21 11:46:50 -0700283 rsc->mStateRaster.init(rsc);
284 rsc->setProgramRaster(NULL);
285 rsc->mStateVertex.init(rsc);
286 rsc->setProgramVertex(NULL);
287 rsc->mStateFragment.init(rsc);
288 rsc->setProgramFragment(NULL);
289 rsc->mStateFragmentStore.init(rsc);
290 rsc->setProgramStore(NULL);
291 rsc->mStateFont.init(rsc);
292 rsc->setFont(NULL);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700293 rsc->mStateSampler.init(rsc);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700294 rsc->mFBOCache.init(rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700295 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700296
Jason Sams83c451a2011-04-21 11:46:50 -0700297 rsc->mRunning = true;
Jason Sams5f27d6f2012-02-07 15:32:08 -0800298 if (!rsc->mIsGraphicsContext) {
299 while (!rsc->mExit) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800300 rsc->mIO.playCoreCommands(rsc, -1);
Jason Samse0aab4a2011-08-12 15:05:15 -0700301 }
Jason Sams5f27d6f2012-02-07 15:32:08 -0800302 } else {
303#ifndef ANDROID_RS_SERIALIZE
304 DisplayEventReceiver displayEvent;
305 DisplayEventReceiver::Event eventBuffer[1];
306#endif
307 int vsyncRate = 0;
308 int targetRate = 0;
Jason Samse0aab4a2011-08-12 15:05:15 -0700309
Jason Sams5f27d6f2012-02-07 15:32:08 -0800310 bool drawOnce = false;
311 while (!rsc->mExit) {
312 rsc->timerSet(RS_TIMER_IDLE);
Jason Sams326e0dd2009-05-22 14:03:28 -0700313
Jason Sams5f27d6f2012-02-07 15:32:08 -0800314#ifndef ANDROID_RS_SERIALIZE
Jason Sams9afd9a52012-02-17 16:59:50 -0800315 if (!rsc->mRootScript.get() || !rsc->mHasSurface || rsc->mPaused) {
316 targetRate = 0;
317 }
318
Jason Sams5f27d6f2012-02-07 15:32:08 -0800319 if (vsyncRate != targetRate) {
320 displayEvent.setVsyncRate(targetRate);
321 vsyncRate = targetRate;
322 }
323 if (targetRate) {
Jason Sams963a2fb2012-02-09 14:36:14 -0800324 drawOnce |= rsc->mIO.playCoreCommands(rsc, displayEvent.getFd());
Jason Sams5f27d6f2012-02-07 15:32:08 -0800325 while (displayEvent.getEvents(eventBuffer, 1) != 0) {
326 //ALOGE("vs2 time past %lld", (rsc->getTime() - eventBuffer[0].header.timestamp) / 1000000);
327 }
328 } else
329#endif
330 {
Jason Sams963a2fb2012-02-09 14:36:14 -0800331 drawOnce |= rsc->mIO.playCoreCommands(rsc, -1);
Jason Sams83c451a2011-04-21 11:46:50 -0700332 }
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700333
Jason Sams5f27d6f2012-02-07 15:32:08 -0800334 if ((rsc->mRootScript.get() != NULL) && rsc->mHasSurface &&
335 (targetRate || drawOnce) && !rsc->mPaused) {
336
337 drawOnce = false;
338 targetRate = ((rsc->runRootScript() + 15) / 16);
339
340 if (rsc->props.mLogVisual) {
341 rsc->displayDebugStats();
342 }
343
344 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
345 rsc->mHal.funcs.swap(rsc);
346 rsc->timerFrame();
347 rsc->timerSet(RS_TIMER_INTERNAL);
348 rsc->timerPrint();
349 rsc->timerReset();
350 }
Jason Sams83c451a2011-04-21 11:46:50 -0700351 }
Jason Sams83c451a2011-04-21 11:46:50 -0700352 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700353
Steve Block65982012011-10-20 11:56:00 +0100354 ALOGV("%p RS Thread exiting", rsc);
Jason Samse514b452009-09-25 14:51:22 -0700355
Jason Sams83c451a2011-04-21 11:46:50 -0700356 if (rsc->mIsGraphicsContext) {
357 pthread_mutex_lock(&gInitMutex);
358 rsc->deinitEGL();
359 pthread_mutex_unlock(&gInitMutex);
360 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700361
Steve Block65982012011-10-20 11:56:00 +0100362 ALOGV("%p RS Thread exited", rsc);
Jason Sams83c451a2011-04-21 11:46:50 -0700363 return NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700364}
365
Jason Sams741aac92010-12-24 14:38:39 -0800366void Context::destroyWorkerThreadResources() {
Steve Block65982012011-10-20 11:56:00 +0100367 //ALOGV("destroyWorkerThreadResources 1");
Jason Sams2e8665d2011-01-27 00:14:13 -0800368 ObjectBase::zeroAllUserRef(this);
Jason Sams741aac92010-12-24 14:38:39 -0800369 if (mIsGraphicsContext) {
370 mRaster.clear();
371 mFragment.clear();
372 mVertex.clear();
373 mFragmentStore.clear();
374 mFont.clear();
375 mRootScript.clear();
376 mStateRaster.deinit(this);
377 mStateVertex.deinit(this);
378 mStateFragment.deinit(this);
379 mStateFragmentStore.deinit(this);
380 mStateFont.deinit(this);
Alex Sakhartchoukc700e642011-08-16 13:09:46 -0700381 mStateSampler.deinit(this);
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700382 mFBOCache.deinit(this);
Jason Sams741aac92010-12-24 14:38:39 -0800383 }
Jason Samsc7cec1e2011-08-18 18:01:33 -0700384 ObjectBase::freeAllChildren(this);
Jason Samscf912de2011-01-09 16:09:51 -0800385 mExit = true;
Jason Sams5f27d6f2012-02-07 15:32:08 -0800386 //ALOGV("destroyWorkerThreadResources 2");
Jason Sams741aac92010-12-24 14:38:39 -0800387}
388
Jason Sams2382aba2011-09-13 15:41:01 -0700389void Context::printWatchdogInfo(void *ctx) {
390 Context *rsc = (Context *)ctx;
Jason Samsee803442011-10-13 16:05:27 -0700391 if (rsc->watchdog.command && rsc->watchdog.file) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000392 ALOGE("RS watchdog timeout: %i %s line %i %s", rsc->watchdog.inRoot,
Jason Samsee803442011-10-13 16:05:27 -0700393 rsc->watchdog.command, rsc->watchdog.line, rsc->watchdog.file);
394 } else {
Steve Blockaf12ac62012-01-06 19:20:56 +0000395 ALOGE("RS watchdog timeout: %i", rsc->watchdog.inRoot);
Jason Samsee803442011-10-13 16:05:27 -0700396 }
Jason Sams2382aba2011-09-13 15:41:01 -0700397}
398
399
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800400void Context::setPriority(int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800401 // Note: If we put this in the proper "background" policy
402 // the wallpapers can become completly unresponsive at times.
403 // This is probably not what we want for something the user is actively
404 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800405 mThreadPriority = p;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700406 setpriority(PRIO_PROCESS, mNativeThreadId, p);
Jason Sams9719bd42012-01-12 14:22:21 -0800407 mHal.funcs.setPriority(this, mThreadPriority);
Jason Sams15832442009-11-15 12:14:26 -0800408}
409
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800410Context::Context() {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700411 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700412 mRunning = false;
413 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700414 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700415 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800416 mError = RS_ERROR_NONE;
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700417 mTargetSdkVersion = 14;
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700418 mDPI = 96;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700419 mIsContextLite = false;
Stephen Hines86c6b5f2011-10-31 14:07:54 -0700420 memset(&watchdog, 0, sizeof(watchdog));
Jason Sams5c1c79a2010-11-03 14:27:11 -0700421}
422
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800423Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700424 Context * rsc = new Context();
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700425
Jason Sams5c1c79a2010-11-03 14:27:11 -0700426 if (!rsc->initContext(dev, sc)) {
427 delete rsc;
428 return NULL;
429 }
430 return rsc;
431}
432
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700433Context * Context::createContextLite() {
434 Context * rsc = new Context();
435 rsc->mIsContextLite = true;
436 return rsc;
437}
438
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800439bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Sams5c1c79a2010-11-03 14:27:11 -0700440 pthread_mutex_lock(&gInitMutex);
441
Jason Sams1a4efa32011-05-17 15:01:29 -0700442 mIO.init();
Jason Sams5f27d6f2012-02-07 15:32:08 -0800443 mIO.setTimeoutCallback(printWatchdogInfo, this, 2e9);
Jason Sams1a4efa32011-05-17 15:01:29 -0700444
Jason Sams5c1c79a2010-11-03 14:27:11 -0700445 dev->addContext(this);
446 mDev = dev;
Jason Sams6b8552a2010-10-13 15:31:10 -0700447 if (sc) {
448 mUserSurfaceConfig = *sc;
449 } else {
450 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
451 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800452
Jason Sams6b8552a2010-10-13 15:31:10 -0700453 mIsGraphicsContext = sc != NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700454
Jason Samsa658e902009-06-04 14:35:01 -0700455 int status;
456 pthread_attr_t threadAttr;
457
Jason Samsfb03a222009-10-15 16:47:31 -0700458 pthread_mutex_unlock(&gInitMutex);
459
460 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700461
Jason Samsa658e902009-06-04 14:35:01 -0700462 status = pthread_attr_init(&threadAttr);
463 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000464 ALOGE("Failed to init thread attribute.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700465 return false;
Jason Samsa658e902009-06-04 14:35:01 -0700466 }
467
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700468 mHasSurface = false;
Jason Sams992a0b72009-06-23 12:22:47 -0700469
Jason Sams24371d92009-08-19 12:17:14 -0700470 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700471 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700472
Jason Samsa658e902009-06-04 14:35:01 -0700473 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700474 if (status) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000475 ALOGE("Failed to start rs context thread.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700476 return false;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700477 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800478 while (!mRunning && (mError == RS_ERROR_NONE)) {
Jason Sams18133402010-07-20 15:09:00 -0700479 usleep(100);
480 }
481
Jason Sams5c1c79a2010-11-03 14:27:11 -0700482 if (mError != RS_ERROR_NONE) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000483 ALOGE("Errors during thread init");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700484 return false;
485 }
486
Jason Samsa658e902009-06-04 14:35:01 -0700487 pthread_attr_destroy(&threadAttr);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700488 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700489}
490
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800491Context::~Context() {
Steve Block65982012011-10-20 11:56:00 +0100492 ALOGV("%p Context::~Context", this);
Jason Samscf912de2011-01-09 16:09:51 -0800493
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700494 if (!mIsContextLite) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700495 mPaused = false;
496 void *res;
Jason Sams326e0dd2009-05-22 14:03:28 -0700497
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700498 mIO.shutdown();
499 int status = pthread_join(mThreadId, &res);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800500 rsAssert(mExit);
Jason Sams326e0dd2009-05-22 14:03:28 -0700501
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700502 if (mHal.funcs.shutdownDriver) {
503 mHal.funcs.shutdownDriver(this);
504 }
505
506 // Global structure cleanup.
507 pthread_mutex_lock(&gInitMutex);
508 if (mDev) {
509 mDev->removeContext(this);
510 mDev = NULL;
511 }
512 pthread_mutex_unlock(&gInitMutex);
Jason Sams51462c52011-01-25 00:26:25 -0800513 }
Steve Block65982012011-10-20 11:56:00 +0100514 ALOGV("%p Context::~Context done", this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700515}
516
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700517void Context::setSurface(uint32_t w, uint32_t h, RsNativeWindow sur) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800518 rsAssert(mIsGraphicsContext);
Jason Sams4b3de472011-04-06 17:52:23 -0700519 mHal.funcs.setSurface(this, w, h, sur);
Jason Sams458f2dc2009-11-03 13:58:36 -0800520
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700521 mHasSurface = sur != NULL;
Jason Sams4b3de472011-04-06 17:52:23 -0700522 mWidth = w;
523 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800524
Jason Sams4b3de472011-04-06 17:52:23 -0700525 if (mWidth && mHeight) {
Jason Sams771565f2010-05-14 15:30:29 -0700526 mStateVertex.updateSize(this);
Alex Sakhartchouka544b632011-07-19 17:50:29 -0700527 mFBOCache.updateSize();
Jason Sams458f2dc2009-11-03 13:58:36 -0800528 }
529}
530
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800531uint32_t Context::getCurrentSurfaceWidth() const {
532 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
533 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
534 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimX();
535 }
536 }
537 if (mFBOCache.mHal.state.depthTarget != NULL) {
538 return mFBOCache.mHal.state.depthTarget->getType()->getDimX();
539 }
540 return mWidth;
541}
542
543uint32_t Context::getCurrentSurfaceHeight() const {
544 for (uint32_t i = 0; i < mFBOCache.mHal.state.colorTargetsCount; i ++) {
545 if (mFBOCache.mHal.state.colorTargets[i] != NULL) {
546 return mFBOCache.mHal.state.colorTargets[i]->getType()->getDimY();
547 }
548 }
549 if (mFBOCache.mHal.state.depthTarget != NULL) {
550 return mFBOCache.mHal.state.depthTarget->getType()->getDimY();
551 }
552 return mHeight;
553}
554
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800555void Context::pause() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800556 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700557 mPaused = true;
558}
559
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800560void Context::resume() {
Jason Sams4820e8b2010-02-09 16:05:07 -0800561 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700562 mPaused = false;
563}
564
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800565void Context::setRootScript(Script *s) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800566 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700567 mRootScript.set(s);
568}
569
Jason Sams60709252010-11-17 15:29:32 -0800570void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800571 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700572 if (pfs == NULL) {
573 mFragmentStore.set(mStateFragmentStore.mDefault);
574 } else {
575 mFragmentStore.set(pfs);
576 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700577}
578
Jason Sams60709252010-11-17 15:29:32 -0800579void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800580 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700581 if (pf == NULL) {
582 mFragment.set(mStateFragment.mDefault);
583 } else {
584 mFragment.set(pf);
585 }
Jason Samscfb1d112009-08-05 13:57:03 -0700586}
587
Jason Sams60709252010-11-17 15:29:32 -0800588void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800589 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700590 if (pr == NULL) {
591 mRaster.set(mStateRaster.mDefault);
592 } else {
593 mRaster.set(pr);
594 }
595}
596
Jason Sams60709252010-11-17 15:29:32 -0800597void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams4820e8b2010-02-09 16:05:07 -0800598 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700599 if (pv == NULL) {
600 mVertex.set(mStateVertex.mDefault);
601 } else {
602 mVertex.set(pv);
603 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700604}
605
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800606void Context::setFont(Font *f) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700607 rsAssert(mIsGraphicsContext);
608 if (f == NULL) {
609 mFont.set(mStateFont.mDefault);
610 } else {
611 mFont.set(f);
612 }
613}
614
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800615void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700616 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700617 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700618 mNames.add(obj);
619}
620
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800621void Context::removeName(ObjectBase *obj) {
622 for (size_t ct=0; ct < mNames.size(); ct++) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700623 if (obj == mNames[ct]) {
624 mNames.removeAt(ct);
625 return;
626 }
627 }
628}
629
Jason Sams1a4efa32011-05-17 15:01:29 -0700630RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID) {
631 return (RsMessageToClientType)mIO.getClientHeader(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800632}
633
Jason Sams1a4efa32011-05-17 15:01:29 -0700634RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen) {
635 return (RsMessageToClientType)mIO.getClientPayload(data, receiveLen, subID, bufferLen);
Jason Sams8c401ef2009-10-06 13:58:47 -0700636}
637
Jason Sams87319de2010-11-22 16:20:16 -0800638bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
639 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Sams1a4efa32011-05-17 15:01:29 -0700640
641 return mIO.sendToClient(cmdID, subID, data, len, waitForSpace);
Jason Sams8c401ef2009-10-06 13:58:47 -0700642}
643
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800644void Context::initToClient() {
645 while (!mRunning) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700646 usleep(100);
647 }
648}
649
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800650void Context::deinitToClient() {
Jason Sams1a4efa32011-05-17 15:01:29 -0700651 mIO.clientShutdown();
Jason Sams8c401ef2009-10-06 13:58:47 -0700652}
Jason Sams50869382009-08-18 17:07:09 -0700653
Jason Sams87319de2010-11-22 16:20:16 -0800654void Context::setError(RsError e, const char *msg) const {
Jason Samsa2cf7552010-03-03 13:03:18 -0800655 mError = e;
Jason Samsaad4bc52010-11-08 17:06:46 -0800656 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Samsa2cf7552010-03-03 13:03:18 -0800657}
658
659
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800660void Context::dumpDebug() const {
Steve Blockaf12ac62012-01-06 19:20:56 +0000661 ALOGE("RS Context debug %p", this);
662 ALOGE("RS Context debug");
Jason Sams13e26342009-11-24 12:26:35 -0800663
Steve Blockaf12ac62012-01-06 19:20:56 +0000664 ALOGE(" RS width %i, height %i", mWidth, mHeight);
665 ALOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
666 ALOGE(" RS pThreadID %li, nativeThreadID %i", (long int)mThreadId, mNativeThreadId);
Jason Sams13e26342009-11-24 12:26:35 -0800667}
Jason Samsa4a54e42009-06-10 18:39:40 -0700668
Jason Sams326e0dd2009-05-22 14:03:28 -0700669///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700670//
Jason Sams326e0dd2009-05-22 14:03:28 -0700671
672namespace android {
673namespace renderscript {
674
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800675void rsi_ContextFinish(Context *rsc) {
Jason Sams8c880902010-06-15 12:15:57 -0700676}
Jason Sams326e0dd2009-05-22 14:03:28 -0700677
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800678void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700679 Script *s = static_cast<Script *>(vs);
680 rsc->setRootScript(s);
681}
682
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800683void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700684 Sampler *s = static_cast<Sampler *>(vs);
685
686 if (slot > RS_MAX_SAMPLER_SLOT) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000687 ALOGE("Invalid sampler slot");
Jason Sams326e0dd2009-05-22 14:03:28 -0700688 return;
689 }
690
691 s->bindToContext(&rsc->mStateSampler, slot);
692}
693
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800694void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Samsccc010b2010-05-13 18:30:11 -0700695 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams60709252010-11-17 15:29:32 -0800696 rsc->setProgramStore(pfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700697}
698
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800699void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700700 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Sams60709252010-11-17 15:29:32 -0800701 rsc->setProgramFragment(pf);
Jason Sams326e0dd2009-05-22 14:03:28 -0700702}
703
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800704void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Sams5fd09d82009-09-23 13:57:02 -0700705 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Sams60709252010-11-17 15:29:32 -0800706 rsc->setProgramRaster(pr);
Jason Sams5fd09d82009-09-23 13:57:02 -0700707}
708
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800709void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700710 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Sams60709252010-11-17 15:29:32 -0800711 rsc->setProgramVertex(pv);
Jason Sams326e0dd2009-05-22 14:03:28 -0700712}
713
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800714void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700715 Font *font = static_cast<Font *>(vfont);
716 rsc->setFont(font);
717}
718
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700719void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, size_t name_length) {
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700720 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Alex Sakhartchouk70b83c12011-04-06 10:57:51 -0700721 rsc->assignName(ob, name, name_length);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700722}
Jason Sams326e0dd2009-05-22 14:03:28 -0700723
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800724void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams2353ae32010-10-14 17:48:46 -0700725 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -0700726 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700727 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700728}
729
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800730void rsi_ContextPause(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700731 rsc->pause();
732}
733
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800734void rsi_ContextResume(Context *rsc) {
Jason Sams86f1b232009-09-24 17:38:20 -0700735 rsc->resume();
736}
737
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700738void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, RsNativeWindow sur) {
Mathias Agopianfa402862010-02-12 14:04:35 -0800739 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800740}
741
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800742void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams15832442009-11-15 12:14:26 -0800743 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800744}
745
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800746void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Samsc21cf402009-11-17 17:26:46 -0800747 ObjectBase::dumpAll(rsc);
748}
749
Jason Sams741aac92010-12-24 14:38:39 -0800750void rsi_ContextDestroyWorker(Context *rsc) {
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700751 rsc->destroyWorkerThreadResources();
Jason Sams741aac92010-12-24 14:38:39 -0800752}
753
Jason Samsc975cf42011-04-28 18:26:48 -0700754void rsi_ContextDestroy(Context *rsc) {
Steve Block65982012011-10-20 11:56:00 +0100755 ALOGV("%p rsContextDestroy", rsc);
Jason Sams741aac92010-12-24 14:38:39 -0800756 rsContextDestroyWorker(rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800757 delete rsc;
Steve Block65982012011-10-20 11:56:00 +0100758 ALOGV("%p rsContextDestroy done", rsc);
Jason Sams1dcefab2010-12-09 12:19:46 -0800759}
760
Jason Sams326e0dd2009-05-22 14:03:28 -0700761
Jason Samsc975cf42011-04-28 18:26:48 -0700762RsMessageToClientType rsi_ContextPeekMessage(Context *rsc,
Jason Sams186e5912011-04-26 14:50:00 -0700763 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700764 uint32_t * subID, size_t subID_length) {
765 return rsc->peekMessageToClient(receiveLen, subID);
Jason Samsaad4bc52010-11-08 17:06:46 -0800766}
767
Jason Samsc975cf42011-04-28 18:26:48 -0700768RsMessageToClientType rsi_ContextGetMessage(Context *rsc, void * data, size_t data_length,
Jason Sams186e5912011-04-26 14:50:00 -0700769 size_t * receiveLen, size_t receiveLen_length,
Jason Sams1a4efa32011-05-17 15:01:29 -0700770 uint32_t * subID, size_t subID_length) {
Jason Sams186e5912011-04-26 14:50:00 -0700771 rsAssert(subID_length == sizeof(uint32_t));
772 rsAssert(receiveLen_length == sizeof(size_t));
Jason Sams1a4efa32011-05-17 15:01:29 -0700773 return rsc->getMessageToClient(data, receiveLen, subID, data_length);
Jason Sams8c401ef2009-10-06 13:58:47 -0700774}
775
Jason Samsc975cf42011-04-28 18:26:48 -0700776void rsi_ContextInitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700777 rsc->initToClient();
778}
779
Jason Samsc975cf42011-04-28 18:26:48 -0700780void rsi_ContextDeinitToClient(Context *rsc) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700781 rsc->deinitToClient();
782}
783
Jason Samsc975cf42011-04-28 18:26:48 -0700784}
785}
786
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700787RsContext rsContextCreate(RsDevice vdev, uint32_t version,
788 uint32_t sdkVersion) {
Steve Block65982012011-10-20 11:56:00 +0100789 ALOGV("rsContextCreate dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700790 Device * dev = static_cast<Device *>(vdev);
791 Context *rsc = Context::createContext(dev, NULL);
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700792 if (rsc) {
793 rsc->setTargetSdkVersion(sdkVersion);
794 }
Jason Sams789ca832011-05-18 17:36:02 -0700795 return rsc;
796}
797
798RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700799 uint32_t sdkVersion, RsSurfaceConfig sc,
800 uint32_t dpi) {
Steve Block65982012011-10-20 11:56:00 +0100801 ALOGV("rsContextCreateGL dev=%p", vdev);
Jason Sams789ca832011-05-18 17:36:02 -0700802 Device * dev = static_cast<Device *>(vdev);
803 Context *rsc = Context::createContext(dev, &sc);
Jason Sams9544f762011-07-13 16:09:42 -0700804 if (rsc) {
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700805 rsc->setTargetSdkVersion(sdkVersion);
Jason Sams9544f762011-07-13 16:09:42 -0700806 rsc->setDPI(dpi);
807 }
Steve Block65982012011-10-20 11:56:00 +0100808 ALOGV("%p rsContextCreateGL ret", rsc);
Jason Sams789ca832011-05-18 17:36:02 -0700809 return rsc;
810}
811
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700812// Only to be called at a3d load time, before object is visible to user
813// not thread safe
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800814void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700815 ObjectBase *ob = static_cast<ObjectBase *>(obj);
816 (*name) = ob->getName();
817}