blob: a07ded374cdfc4e560ff829a1b92cda69df170d7 [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -07002 * Copyright (C) 2011 The Android Open Source Project
Jason Samsd19f10d2009-05-22 14:03:28 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "rsDevice.h"
18#include "rsContext.h"
19#include "rsThreadIO.h"
Mathias Agopian3142f4f2009-06-22 18:01:09 -070020#include <ui/FramebufferNativeWindow.h>
Jason Sams11c8af92010-10-13 15:31:10 -070021#include <ui/PixelFormat.h>
Mathias Agopian5cbb9402010-02-12 12:00:38 -080022#include <ui/egl/android_natives.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070023
Jason Sams7d787b42009-11-15 12:14:26 -080024#include <sys/types.h>
25#include <sys/resource.h>
Jason Sams8e6c17f2010-07-19 15:38:19 -070026#include <sched.h>
Jason Sams7d787b42009-11-15 12:14:26 -080027
Joe Onorato9ac2c662009-09-23 16:37:36 -070028#include <cutils/properties.h>
29
Jason Sams4b962e52009-06-22 17:15:15 -070030#include <GLES/gl.h>
31#include <GLES/glext.h>
Jason Sams0011bcf2009-12-15 12:58:36 -080032#include <GLES2/gl2.h>
33#include <GLES2/gl2ext.h>
Jason Sams4b962e52009-06-22 17:15:15 -070034
Jason Sams7d787b42009-11-15 12:14:26 -080035#include <cutils/sched_policy.h>
Jason Samsf3470ed2010-09-28 14:41:22 -070036#include <sys/syscall.h>
37#include <string.h>
Jason Sams7d787b42009-11-15 12:14:26 -080038
Jason Samsd19f10d2009-05-22 14:03:28 -070039using namespace android;
40using namespace android::renderscript;
41
Jason Sams41c19db92009-10-15 16:47:31 -070042pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Stephen Hines1ac9da62011-01-07 15:11:30 -080043pthread_mutex_t Context::gLibMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Samsd19f10d2009-05-22 14:03:28 -070044
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080045bool Context::initGLThread() {
Jason Sams11c8af92010-10-13 15:31:10 -070046 pthread_mutex_lock(&gInitMutex);
47 LOGV("initGLThread start %p", this);
48
Jason Sams803626f2011-04-06 17:52:23 -070049 if (!mHal.funcs.initGraphics(this)) {
Jason Samsd5f06302010-11-03 14:27:11 -070050 pthread_mutex_unlock(&gInitMutex);
Jason Sams803626f2011-04-06 17:52:23 -070051 LOGE("%p, initGraphics failed", this);
Jason Samsd5f06302010-11-03 14:27:11 -070052 return false;
Jason Sams11c8af92010-10-13 15:31:10 -070053 }
54
Jason Sams803626f2011-04-06 17:52:23 -070055 const char * ext = (const char *)glGetString(GL_EXTENSIONS);
Jason Sams11c8af92010-10-13 15:31:10 -070056
57 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
58 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
59 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
60
61 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
62 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
63
64 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
65 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
66
Jason Sams803626f2011-04-06 17:52:23 -070067 mGL.OES_texture_npot = NULL != strstr(ext, "GL_OES_texture_npot");
68 mGL.GL_IMG_texture_npot = NULL != strstr(ext, "GL_IMG_texture_npot");
69 mGL.GL_NV_texture_npot_2D_mipmap = NULL != strstr(ext, "GL_NV_texture_npot_2D_mipmap");
Jason Sams11c8af92010-10-13 15:31:10 -070070 mGL.EXT_texture_max_aniso = 1.0f;
Jason Sams803626f2011-04-06 17:52:23 -070071 bool hasAniso = NULL != strstr(ext, "GL_EXT_texture_filter_anisotropic");
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080072 if (hasAniso) {
Jason Sams11c8af92010-10-13 15:31:10 -070073 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &mGL.EXT_texture_max_aniso);
74 }
75
76 LOGV("initGLThread end %p", this);
77 pthread_mutex_unlock(&gInitMutex);
Jason Samsd5f06302010-11-03 14:27:11 -070078 return true;
Jason Samsd19f10d2009-05-22 14:03:28 -070079}
80
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080081void Context::deinitEGL() {
Jason Samscfc04362010-09-14 14:59:03 -070082 LOGV("%p, deinitEGL", this);
Jason Sams803626f2011-04-06 17:52:23 -070083 mHal.funcs.shutdownGraphics(this);
Jason Sams71362202009-10-27 14:44:31 -070084}
85
Jason Samsa17af042010-11-17 15:29:32 -080086Context::PushState::PushState(Context *con) {
87 mRsc = con;
Jason Sams07078e32011-02-23 14:47:17 -080088 if (con->mIsGraphicsContext) {
89 mFragment.set(con->getProgramFragment());
90 mVertex.set(con->getProgramVertex());
91 mStore.set(con->getProgramStore());
92 mRaster.set(con->getProgramRaster());
93 mFont.set(con->getFont());
94 }
Jason Samsa17af042010-11-17 15:29:32 -080095}
96
97Context::PushState::~PushState() {
Jason Sams07078e32011-02-23 14:47:17 -080098 if (mRsc->mIsGraphicsContext) {
99 mRsc->setProgramFragment(mFragment.get());
100 mRsc->setProgramVertex(mVertex.get());
101 mRsc->setProgramStore(mStore.get());
102 mRsc->setProgramRaster(mRaster.get());
103 mRsc->setFont(mFont.get());
104 }
Jason Samsa17af042010-11-17 15:29:32 -0800105}
106
Jason Sams71362202009-10-27 14:44:31 -0700107
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800108uint32_t Context::runScript(Script *s) {
Jason Samsa17af042010-11-17 15:29:32 -0800109 PushState(this);
Jason Samsda423d82009-06-09 12:15:30 -0700110
Jason Samsf17bccc2010-05-28 18:23:22 -0700111 uint32_t ret = s->run(this);
Jason Samsb0ec1b42009-07-28 12:02:16 -0700112 return ret;
Jason Samsda423d82009-06-09 12:15:30 -0700113}
114
Jason Samsadd9d962010-11-22 16:20:16 -0800115void Context::checkError(const char *msg, bool isFatal) const {
116
Jason Sams718cd1f2009-12-23 14:35:29 -0800117 GLenum err = glGetError();
118 if (err != GL_NO_ERROR) {
Jason Samsadd9d962010-11-22 16:20:16 -0800119 char buf[1024];
120 snprintf(buf, sizeof(buf), "GL Error = 0x%08x, from: %s", err, msg);
121
122 if (isFatal) {
123 setError(RS_ERROR_FATAL_DRIVER, buf);
124 } else {
125 switch (err) {
126 case GL_OUT_OF_MEMORY:
127 setError(RS_ERROR_OUT_OF_MEMORY, buf);
128 break;
129 default:
130 setError(RS_ERROR_DRIVER, buf);
131 break;
132 }
133 }
134
135 LOGE("%p, %s", this, buf);
Jason Sams718cd1f2009-12-23 14:35:29 -0800136 }
137}
Jason Samsda423d82009-06-09 12:15:30 -0700138
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800139uint32_t Context::runRootScript() {
Jason Samsf603d212010-05-14 15:30:29 -0700140 glViewport(0, 0, mWidth, mHeight);
Jason Sams67c68442009-08-25 17:09:59 -0700141
Jason Samsb9d5c572009-12-09 11:05:45 -0800142 timerSet(RS_TIMER_SCRIPT);
Jason Sams516c3192009-10-06 13:58:47 -0700143 mStateFragmentStore.mLast.clear();
Jason Samsf17bccc2010-05-28 18:23:22 -0700144 uint32_t ret = runScript(mRootScript.get());
Jason Samsc7412b32009-10-14 15:43:53 -0700145
Jason Sams718cd1f2009-12-23 14:35:29 -0800146 checkError("runRootScript");
Jason Sams9bee51c2009-08-05 13:57:03 -0700147 return ret;
Jason Samsd19f10d2009-05-22 14:03:28 -0700148}
149
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800150uint64_t Context::getTime() const {
Jason Samsf4d16062009-08-19 12:17:14 -0700151 struct timespec t;
152 clock_gettime(CLOCK_MONOTONIC, &t);
153 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
154}
155
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800156void Context::timerReset() {
Jason Samsf4d16062009-08-19 12:17:14 -0700157 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
158 mTimers[ct] = 0;
159 }
160}
161
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800162void Context::timerInit() {
Jason Samsf4d16062009-08-19 12:17:14 -0700163 mTimeLast = getTime();
Jason Sams2525a812009-09-03 15:43:13 -0700164 mTimeFrame = mTimeLast;
165 mTimeLastFrame = mTimeLast;
Jason Samsf4d16062009-08-19 12:17:14 -0700166 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchouk76322af2010-10-05 13:23:55 -0700167 mAverageFPSFrameCount = 0;
168 mAverageFPSStartTime = mTimeLast;
169 mAverageFPS = 0;
Jason Samsf4d16062009-08-19 12:17:14 -0700170 timerReset();
171}
172
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800173void Context::timerFrame() {
Jason Sams2525a812009-09-03 15:43:13 -0700174 mTimeLastFrame = mTimeFrame;
175 mTimeFrame = getTime();
Alex Sakhartchouk76322af2010-10-05 13:23:55 -0700176 // Update average fps
177 const uint64_t averageFramerateInterval = 1000 * 1000000;
178 mAverageFPSFrameCount ++;
179 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800180 if (inverval >= averageFramerateInterval) {
Alex Sakhartchouk76322af2010-10-05 13:23:55 -0700181 inverval = inverval / 1000000;
182 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
183 mAverageFPSFrameCount = 0;
184 mAverageFPSStartTime = mTimeFrame;
185 }
Jason Sams2525a812009-09-03 15:43:13 -0700186}
187
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800188void Context::timerSet(Timers tm) {
Jason Samsf4d16062009-08-19 12:17:14 -0700189 uint64_t last = mTimeLast;
190 mTimeLast = getTime();
191 mTimers[mTimerActive] += mTimeLast - last;
192 mTimerActive = tm;
193}
194
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800195void Context::timerPrint() {
Jason Samsf4d16062009-08-19 12:17:14 -0700196 double total = 0;
197 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
198 total += mTimers[ct];
199 }
Jason Sams2525a812009-09-03 15:43:13 -0700200 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Samsb9d5c572009-12-09 11:05:45 -0800201 mTimeMSLastFrame = frame / 1000000;
202 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
203 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Samsf4d16062009-08-19 12:17:14 -0700204
Jason Samsb9d5c572009-12-09 11:05:45 -0800205
206 if (props.mLogTimes) {
Alex Sakhartchouk98bfe5d2010-10-18 17:18:50 -0700207 LOGV("RS: Frame (%i), Script %2.1f%% (%i), Swap %2.1f%% (%i), Idle %2.1f%% (%lli), Internal %2.1f%% (%lli), Avg fps: %u",
Jason Samsb9d5c572009-12-09 11:05:45 -0800208 mTimeMSLastFrame,
209 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
210 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
211 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
Alex Sakhartchouk76322af2010-10-05 13:23:55 -0700212 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
213 mAverageFPS);
Jason Samsb9d5c572009-12-09 11:05:45 -0800214 }
Jason Samsf4d16062009-08-19 12:17:14 -0700215}
216
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800217bool Context::setupCheck() {
Jason Samsd081fff2010-09-16 18:18:29 -0700218
Jason Sams331bf9b2011-04-06 11:23:54 -0700219 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700220 mFragment->setupGL2(this, &mStateFragment);
Jason Sams331bf9b2011-04-06 11:23:54 -0700221 mRaster->setup(this, &mStateRaster);
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -0700222 mVertex->setupGL2(this, &mStateVertex);
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700223 mFBOCache.setupGL2(this);
Jason Sams156cce62010-03-03 13:03:18 -0800224 return true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700225}
226
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700227void Context::setupProgramStore() {
Jason Sams331bf9b2011-04-06 11:23:54 -0700228 mFragmentStore->setup(this, &mStateFragmentStore);
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700229}
230
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800231static bool getProp(const char *str) {
Joe Onorato9ac2c662009-09-23 16:37:36 -0700232 char buf[PROPERTY_VALUE_MAX];
Jason Sams66b27712009-09-25 15:25:00 -0700233 property_get(str, buf, "0");
Joe Onorato9ac2c662009-09-23 16:37:36 -0700234 return 0 != strcmp(buf, "0");
235}
Jason Samsd19f10d2009-05-22 14:03:28 -0700236
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800237void Context::displayDebugStats() {
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700238 char buffer[128];
Alex Sakhartchouk76322af2010-10-05 13:23:55 -0700239 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchouk55e81982010-08-05 11:24:14 -0700240 float oldR, oldG, oldB, oldA;
241 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk10825a02010-10-05 11:33:27 -0700242 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700243
Alex Sakhartchouk20a93542011-03-17 13:49:38 -0700244 ObjectBaseRef<Font> lastFont(getFont());
245 setFont(NULL);
Alex Sakhartchouk10825a02010-10-05 11:33:27 -0700246 float shadowCol = 0.1f;
Alex Sakhartchouk55e81982010-08-05 11:24:14 -0700247 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchouk76322af2010-10-05 13:23:55 -0700248 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700249
Alex Sakhartchouk10825a02010-10-05 11:33:27 -0700250 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchouk76322af2010-10-05 13:23:55 -0700251 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700252
Alex Sakhartchouk20a93542011-03-17 13:49:38 -0700253 setFont(lastFont.get());
Alex Sakhartchouk55e81982010-08-05 11:24:14 -0700254 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700255}
256
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800257void * Context::threadProc(void *vrsc) {
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700258 Context *rsc = static_cast<Context *>(vrsc);
259 rsc->mNativeThreadId = gettid();
Jason Sams7d787b42009-11-15 12:14:26 -0800260
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700261 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
262 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Samsd19f10d2009-05-22 14:03:28 -0700263
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700264 rsc->props.mLogTimes = getProp("debug.rs.profile");
265 rsc->props.mLogScripts = getProp("debug.rs.script");
266 rsc->props.mLogObjects = getProp("debug.rs.object");
267 rsc->props.mLogShaders = getProp("debug.rs.shader");
268 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes");
269 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms");
270 rsc->props.mLogVisual = getProp("debug.rs.visual");
Joe Onorato9ac2c662009-09-23 16:37:36 -0700271
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700272 if (!rsdHalInit(rsc, 0, 0)) {
273 LOGE("Hal init failed");
274 return NULL;
275 }
276 rsc->mHal.funcs.setPriority(rsc, rsc->mThreadPriority);
Jason Sams462d11b2009-06-19 16:03:18 -0700277
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700278 if (!rsc->initGLThread()) {
279 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
280 return NULL;
281 }
Jason Sams11c8af92010-10-13 15:31:10 -0700282
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700283 if (rsc->mIsGraphicsContext) {
284 rsc->mStateRaster.init(rsc);
285 rsc->setProgramRaster(NULL);
286 rsc->mStateVertex.init(rsc);
287 rsc->setProgramVertex(NULL);
288 rsc->mStateFragment.init(rsc);
289 rsc->setProgramFragment(NULL);
290 rsc->mStateFragmentStore.init(rsc);
291 rsc->setProgramStore(NULL);
292 rsc->mStateFont.init(rsc);
293 rsc->setFont(NULL);
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700294 }
Jason Sams9c54bdb2009-06-17 16:52:59 -0700295
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700296 rsc->mRunning = true;
297 bool mDraw = true;
298 while (!rsc->mExit) {
299 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
300 mDraw &= (rsc->mRootScript.get() != NULL);
301 mDraw &= (rsc->mWndSurface != NULL);
Jason Samsd19f10d2009-05-22 14:03:28 -0700302
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700303 uint32_t targetTime = 0;
304 if (mDraw && rsc->mIsGraphicsContext) {
305 targetTime = rsc->runRootScript();
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700306
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700307 if (rsc->props.mLogVisual) {
308 rsc->displayDebugStats();
309 }
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700310
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700311 mDraw = targetTime && !rsc->mPaused;
312 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
313 rsc->mHal.funcs.swap(rsc);
314 rsc->timerFrame();
315 rsc->timerSet(RS_TIMER_INTERNAL);
316 rsc->timerPrint();
317 rsc->timerReset();
318 }
319 if (targetTime > 1) {
320 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
321 if (t > 0) {
322 usleep(t);
323 }
324 }
325 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700326
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700327 LOGV("%p, RS Thread exiting", rsc);
Jason Samsa9e7a052009-09-25 14:51:22 -0700328
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700329 if (rsc->mIsGraphicsContext) {
330 pthread_mutex_lock(&gInitMutex);
331 rsc->deinitEGL();
332 pthread_mutex_unlock(&gInitMutex);
333 }
Jason Sams71362202009-10-27 14:44:31 -0700334
Jason Samsbe8ac6a2011-04-21 11:46:50 -0700335 LOGV("%p, RS Thread exited", rsc);
336 return NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700337}
338
Jason Sams5c68a712010-12-24 14:38:39 -0800339void Context::destroyWorkerThreadResources() {
Jason Samsc55de662011-01-23 17:48:45 -0800340 //LOGV("destroyWorkerThreadResources 1");
Jason Sams38f8d9d2011-01-27 00:14:13 -0800341 ObjectBase::zeroAllUserRef(this);
Jason Sams5c68a712010-12-24 14:38:39 -0800342 if (mIsGraphicsContext) {
343 mRaster.clear();
344 mFragment.clear();
345 mVertex.clear();
346 mFragmentStore.clear();
347 mFont.clear();
348 mRootScript.clear();
349 mStateRaster.deinit(this);
350 mStateVertex.deinit(this);
351 mStateFragment.deinit(this);
352 mStateFragmentStore.deinit(this);
353 mStateFont.deinit(this);
354 }
Jason Samsc55de662011-01-23 17:48:45 -0800355 //LOGV("destroyWorkerThreadResources 2");
Jason Sams84035ff2011-01-09 16:09:51 -0800356 mExit = true;
Jason Sams5c68a712010-12-24 14:38:39 -0800357}
358
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800359void Context::setPriority(int32_t p) {
Jason Sams7d787b42009-11-15 12:14:26 -0800360 // Note: If we put this in the proper "background" policy
361 // the wallpapers can become completly unresponsive at times.
362 // This is probably not what we want for something the user is actively
363 // looking at.
Jason Samsb9d5c572009-12-09 11:05:45 -0800364 mThreadPriority = p;
Jason Sams7d787b42009-11-15 12:14:26 -0800365#if 0
366 SchedPolicy pol = SP_FOREGROUND;
367 if (p > 0) {
368 pol = SP_BACKGROUND;
369 }
370 if (!set_sched_policy(mNativeThreadId, pol)) {
371 // success; reset the priority as well
372 }
373#else
Jason Sams8e6c17f2010-07-19 15:38:19 -0700374 setpriority(PRIO_PROCESS, mNativeThreadId, p);
Jason Sams7d787b42009-11-15 12:14:26 -0800375#endif
376}
377
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800378Context::Context() {
Jason Samsd5f06302010-11-03 14:27:11 -0700379 mDev = NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700380 mRunning = false;
381 mExit = false;
Jason Sams65e7aa52009-09-24 17:38:20 -0700382 mPaused = false;
Jason Samsa9e7a052009-09-25 14:51:22 -0700383 mObjHead = NULL;
Jason Sams156cce62010-03-03 13:03:18 -0800384 mError = RS_ERROR_NONE;
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700385 mDPI = 96;
Jason Samsd5f06302010-11-03 14:27:11 -0700386}
387
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800388Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Samsd5f06302010-11-03 14:27:11 -0700389 Context * rsc = new Context();
390 if (!rsc->initContext(dev, sc)) {
391 delete rsc;
392 return NULL;
393 }
394 return rsc;
395}
396
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800397bool Context::initContext(Device *dev, const RsSurfaceConfig *sc) {
Jason Samsd5f06302010-11-03 14:27:11 -0700398 pthread_mutex_lock(&gInitMutex);
399
400 dev->addContext(this);
401 mDev = dev;
Jason Sams11c8af92010-10-13 15:31:10 -0700402 if (sc) {
403 mUserSurfaceConfig = *sc;
404 } else {
405 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
406 }
Jason Sams156cce62010-03-03 13:03:18 -0800407
Jason Sams704ff642010-02-09 16:05:07 -0800408 memset(&mGL, 0, sizeof(mGL));
Jason Sams11c8af92010-10-13 15:31:10 -0700409 mIsGraphicsContext = sc != NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700410
Jason Sams8ad00102009-06-04 14:35:01 -0700411 int status;
412 pthread_attr_t threadAttr;
413
Jason Sams41c19db92009-10-15 16:47:31 -0700414 pthread_mutex_unlock(&gInitMutex);
415
416 // Global init done at this point.
Jason Sams462d11b2009-06-19 16:03:18 -0700417
Jason Sams8ad00102009-06-04 14:35:01 -0700418 status = pthread_attr_init(&threadAttr);
419 if (status) {
420 LOGE("Failed to init thread attribute.");
Jason Samsd5f06302010-11-03 14:27:11 -0700421 return false;
Jason Sams8ad00102009-06-04 14:35:01 -0700422 }
423
Jason Sams3bc47d42009-11-12 15:10:25 -0800424 mWndSurface = NULL;
Jason Samsf29ca502009-06-23 12:22:47 -0700425
Jason Samsf4d16062009-08-19 12:17:14 -0700426 timerInit();
Jason Samsd3f2eaf2009-09-24 15:42:52 -0700427 timerSet(RS_TIMER_INTERNAL);
Jason Sams730ee652009-08-18 17:07:09 -0700428
Jason Sams8ad00102009-06-04 14:35:01 -0700429 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Samsd19f10d2009-05-22 14:03:28 -0700430 if (status) {
431 LOGE("Failed to start rs context thread.");
Jason Samsd5f06302010-11-03 14:27:11 -0700432 return false;
Jason Sams8e6c17f2010-07-19 15:38:19 -0700433 }
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800434 while (!mRunning && (mError == RS_ERROR_NONE)) {
Jason Samsc7f4e412010-07-20 15:09:00 -0700435 usleep(100);
436 }
437
Jason Samsd5f06302010-11-03 14:27:11 -0700438 if (mError != RS_ERROR_NONE) {
Jason Sams80e29cf2011-03-18 17:08:54 -0700439 LOGE("Errors during thread init");
Jason Samsd5f06302010-11-03 14:27:11 -0700440 return false;
441 }
442
Jason Sams8ad00102009-06-04 14:35:01 -0700443 pthread_attr_destroy(&threadAttr);
Jason Samsd5f06302010-11-03 14:27:11 -0700444 return true;
Jason Samsd19f10d2009-05-22 14:03:28 -0700445}
446
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800447Context::~Context() {
Jason Samsf5b45962009-08-25 14:49:07 -0700448 LOGV("Context::~Context");
Jason Sams84035ff2011-01-09 16:09:51 -0800449
450 mIO.mToCore.flush();
451 rsAssert(mExit);
Jason Samsd19f10d2009-05-22 14:03:28 -0700452 mExit = true;
Jason Sams65e7aa52009-09-24 17:38:20 -0700453 mPaused = false;
Jason Samsd19f10d2009-05-22 14:03:28 -0700454 void *res;
455
Jason Samsf5b45962009-08-25 14:49:07 -0700456 mIO.shutdown();
Jason Samsd19f10d2009-05-22 14:03:28 -0700457 int status = pthread_join(mThreadId, &res);
Jason Samsd19f10d2009-05-22 14:03:28 -0700458
Jason Sams55d2a252011-03-17 16:12:47 -0700459
460 if (mHal.funcs.shutdownDriver) {
461 mHal.funcs.shutdownDriver(this);
Jason Sams03855bb2011-01-25 00:26:25 -0800462 }
Jason Samsc55de662011-01-23 17:48:45 -0800463
Jason Sams41c19db92009-10-15 16:47:31 -0700464 // Global structure cleanup.
465 pthread_mutex_lock(&gInitMutex);
Jason Samsd19f10d2009-05-22 14:03:28 -0700466 if (mDev) {
467 mDev->removeContext(this);
Jason Samsb7a6c432009-11-02 14:25:10 -0800468 mDev = NULL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700469 }
Jason Sams41c19db92009-10-15 16:47:31 -0700470 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c68a712010-12-24 14:38:39 -0800471 LOGV("Context::~Context done");
Jason Samsd19f10d2009-05-22 14:03:28 -0700472}
473
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800474void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur) {
Jason Sams704ff642010-02-09 16:05:07 -0800475 rsAssert(mIsGraphicsContext);
Jason Sams803626f2011-04-06 17:52:23 -0700476 mHal.funcs.setSurface(this, w, h, sur);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800477
478 mWndSurface = sur;
Jason Sams803626f2011-04-06 17:52:23 -0700479 mWidth = w;
480 mHeight = h;
Jason Sams3bc47d42009-11-12 15:10:25 -0800481
Jason Sams803626f2011-04-06 17:52:23 -0700482 if (mWidth && mHeight) {
Jason Samsf603d212010-05-14 15:30:29 -0700483 mStateVertex.updateSize(this);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800484 }
485}
486
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800487void Context::pause() {
Jason Sams704ff642010-02-09 16:05:07 -0800488 rsAssert(mIsGraphicsContext);
Jason Sams65e7aa52009-09-24 17:38:20 -0700489 mPaused = true;
490}
491
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800492void Context::resume() {
Jason Sams704ff642010-02-09 16:05:07 -0800493 rsAssert(mIsGraphicsContext);
Jason Sams65e7aa52009-09-24 17:38:20 -0700494 mPaused = false;
495}
496
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800497void Context::setRootScript(Script *s) {
Jason Sams704ff642010-02-09 16:05:07 -0800498 rsAssert(mIsGraphicsContext);
Jason Samsd19f10d2009-05-22 14:03:28 -0700499 mRootScript.set(s);
500}
501
Jason Samsa17af042010-11-17 15:29:32 -0800502void Context::setProgramStore(ProgramStore *pfs) {
Jason Sams704ff642010-02-09 16:05:07 -0800503 rsAssert(mIsGraphicsContext);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700504 if (pfs == NULL) {
505 mFragmentStore.set(mStateFragmentStore.mDefault);
506 } else {
507 mFragmentStore.set(pfs);
508 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700509}
510
Jason Samsa17af042010-11-17 15:29:32 -0800511void Context::setProgramFragment(ProgramFragment *pf) {
Jason Sams704ff642010-02-09 16:05:07 -0800512 rsAssert(mIsGraphicsContext);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700513 if (pf == NULL) {
514 mFragment.set(mStateFragment.mDefault);
515 } else {
516 mFragment.set(pf);
517 }
Jason Sams9bee51c2009-08-05 13:57:03 -0700518}
519
Jason Samsa17af042010-11-17 15:29:32 -0800520void Context::setProgramRaster(ProgramRaster *pr) {
Jason Sams704ff642010-02-09 16:05:07 -0800521 rsAssert(mIsGraphicsContext);
Jason Samsebfb4362009-09-23 13:57:02 -0700522 if (pr == NULL) {
523 mRaster.set(mStateRaster.mDefault);
524 } else {
525 mRaster.set(pr);
526 }
527}
528
Jason Samsa17af042010-11-17 15:29:32 -0800529void Context::setProgramVertex(ProgramVertex *pv) {
Jason Sams704ff642010-02-09 16:05:07 -0800530 rsAssert(mIsGraphicsContext);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700531 if (pv == NULL) {
532 mVertex.set(mStateVertex.mDefault);
533 } else {
534 mVertex.set(pv);
535 }
Jason Samsd19f10d2009-05-22 14:03:28 -0700536}
537
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800538void Context::setFont(Font *f) {
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700539 rsAssert(mIsGraphicsContext);
540 if (f == NULL) {
541 mFont.set(mStateFont.mDefault);
542 } else {
543 mFont.set(f);
544 }
545}
546
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800547void Context::assignName(ObjectBase *obj, const char *name, uint32_t len) {
Jason Sams3eaa3382009-06-10 15:04:38 -0700548 rsAssert(!obj->getName());
Jason Samsd5680f92009-06-10 18:39:40 -0700549 obj->setName(name, len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700550 mNames.add(obj);
551}
552
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800553void Context::removeName(ObjectBase *obj) {
554 for (size_t ct=0; ct < mNames.size(); ct++) {
Jason Sams3eaa3382009-06-10 15:04:38 -0700555 if (obj == mNames[ct]) {
556 mNames.removeAt(ct);
557 return;
558 }
559 }
560}
561
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800562RsMessageToClientType Context::peekMessageToClient(size_t *receiveLen, uint32_t *subID, bool wait) {
Jason Sams1c415172010-11-08 17:06:46 -0800563 *receiveLen = 0;
564 if (!wait && mIO.mToClient.isEmpty()) {
565 return RS_MESSAGE_TO_CLIENT_NONE;
566 }
567
568 uint32_t bytesData = 0;
569 uint32_t commandID = 0;
570 const uint32_t *d = (const uint32_t *)mIO.mToClient.get(&commandID, &bytesData);
571 *receiveLen = bytesData - sizeof(uint32_t);
572 if (bytesData) {
573 *subID = d[0];
574 }
575 return (RsMessageToClientType)commandID;
576}
577
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800578RsMessageToClientType Context::getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait) {
Jason Sams516c3192009-10-06 13:58:47 -0700579 //LOGE("getMessageToClient %i %i", bufferLen, wait);
Jason Sams1d45c472010-08-25 14:31:48 -0700580 *receiveLen = 0;
Jason Sams1c415172010-11-08 17:06:46 -0800581 if (!wait && mIO.mToClient.isEmpty()) {
582 return RS_MESSAGE_TO_CLIENT_NONE;
Jason Sams516c3192009-10-06 13:58:47 -0700583 }
584
585 //LOGE("getMessageToClient 2 con=%p", this);
586 uint32_t bytesData = 0;
587 uint32_t commandID = 0;
Jason Sams1c415172010-11-08 17:06:46 -0800588 const uint32_t *d = (const uint32_t *)mIO.mToClient.get(&commandID, &bytesData);
Jason Sams516c3192009-10-06 13:58:47 -0700589 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
590
Jason Sams1c415172010-11-08 17:06:46 -0800591 *receiveLen = bytesData - sizeof(uint32_t);
592 *subID = d[0];
593
594 //LOGE("getMessageToClient %i %i", commandID, *subID);
Jason Sams516c3192009-10-06 13:58:47 -0700595 if (bufferLen >= bytesData) {
Jason Sams1c415172010-11-08 17:06:46 -0800596 memcpy(data, d+1, *receiveLen);
Jason Sams516c3192009-10-06 13:58:47 -0700597 mIO.mToClient.next();
Jason Sams1c415172010-11-08 17:06:46 -0800598 return (RsMessageToClientType)commandID;
Jason Sams516c3192009-10-06 13:58:47 -0700599 }
Jason Sams1c415172010-11-08 17:06:46 -0800600 return RS_MESSAGE_TO_CLIENT_RESIZE;
Jason Sams516c3192009-10-06 13:58:47 -0700601}
602
Jason Samsadd9d962010-11-22 16:20:16 -0800603bool Context::sendMessageToClient(const void *data, RsMessageToClientType cmdID,
604 uint32_t subID, size_t len, bool waitForSpace) const {
Jason Sams1c415172010-11-08 17:06:46 -0800605 //LOGE("sendMessageToClient %i %i %i %i", cmdID, subID, len, waitForSpace);
Jason Sams516c3192009-10-06 13:58:47 -0700606 if (cmdID == 0) {
607 LOGE("Attempting to send invalid command 0 to client.");
608 return false;
609 }
610 if (!waitForSpace) {
Jason Sams1c415172010-11-08 17:06:46 -0800611 if (!mIO.mToClient.makeSpaceNonBlocking(len + 12)) {
Jason Sams516c3192009-10-06 13:58:47 -0700612 // Not enough room, and not waiting.
613 return false;
614 }
615 }
616 //LOGE("sendMessageToClient 2");
Jason Sams1c415172010-11-08 17:06:46 -0800617 uint32_t *p = (uint32_t *)mIO.mToClient.reserve(len + sizeof(subID));
618 p[0] = subID;
Jason Sams17966512010-07-28 11:17:53 -0700619 if (len > 0) {
Jason Sams1c415172010-11-08 17:06:46 -0800620 memcpy(p+1, data, len);
Jason Sams17966512010-07-28 11:17:53 -0700621 }
Jason Sams1c415172010-11-08 17:06:46 -0800622 mIO.mToClient.commit(cmdID, len + sizeof(subID));
Jason Sams516c3192009-10-06 13:58:47 -0700623 //LOGE("sendMessageToClient 3");
624 return true;
625}
626
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800627void Context::initToClient() {
628 while (!mRunning) {
Jason Sams516c3192009-10-06 13:58:47 -0700629 usleep(100);
630 }
631}
632
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800633void Context::deinitToClient() {
Jason Sams516c3192009-10-06 13:58:47 -0700634 mIO.mToClient.shutdown();
635}
Jason Sams730ee652009-08-18 17:07:09 -0700636
Jason Samsadd9d962010-11-22 16:20:16 -0800637void Context::setError(RsError e, const char *msg) const {
Jason Sams156cce62010-03-03 13:03:18 -0800638 mError = e;
Jason Sams1c415172010-11-08 17:06:46 -0800639 sendMessageToClient(msg, RS_MESSAGE_TO_CLIENT_ERROR, e, strlen(msg) + 1, true);
Jason Sams156cce62010-03-03 13:03:18 -0800640}
641
642
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800643void Context::dumpDebug() const {
Jason Sams9dab6672009-11-24 12:26:35 -0800644 LOGE("RS Context debug %p", this);
645 LOGE("RS Context debug");
646
Jason Sams9dab6672009-11-24 12:26:35 -0800647 LOGE(" RS width %i, height %i", mWidth, mHeight);
Jason Sams11c8af92010-10-13 15:31:10 -0700648 LOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
Jason Sams9dab6672009-11-24 12:26:35 -0800649 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
Jason Sams9dab6672009-11-24 12:26:35 -0800650}
Jason Samsd5680f92009-06-10 18:39:40 -0700651
Jason Samsd19f10d2009-05-22 14:03:28 -0700652///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa09f11d2009-06-04 17:58:03 -0700653//
Jason Samsd19f10d2009-05-22 14:03:28 -0700654
655namespace android {
656namespace renderscript {
657
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800658void rsi_ContextFinish(Context *rsc) {
Jason Sams96ed4cf2010-06-15 12:15:57 -0700659}
Jason Samsd19f10d2009-05-22 14:03:28 -0700660
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800661void rsi_ContextBindRootScript(Context *rsc, RsScript vs) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700662 Script *s = static_cast<Script *>(vs);
663 rsc->setRootScript(s);
664}
665
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800666void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700667 Sampler *s = static_cast<Sampler *>(vs);
668
669 if (slot > RS_MAX_SAMPLER_SLOT) {
670 LOGE("Invalid sampler slot");
671 return;
672 }
673
674 s->bindToContext(&rsc->mStateSampler, slot);
675}
676
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800677void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs) {
Jason Sams54db59c2010-05-13 18:30:11 -0700678 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Samsa17af042010-11-17 15:29:32 -0800679 rsc->setProgramStore(pfs);
Jason Samsd19f10d2009-05-22 14:03:28 -0700680}
681
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800682void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700683 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
Jason Samsa17af042010-11-17 15:29:32 -0800684 rsc->setProgramFragment(pf);
Jason Samsd19f10d2009-05-22 14:03:28 -0700685}
686
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800687void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr) {
Jason Samsebfb4362009-09-23 13:57:02 -0700688 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
Jason Samsa17af042010-11-17 15:29:32 -0800689 rsc->setProgramRaster(pr);
Jason Samsebfb4362009-09-23 13:57:02 -0700690}
691
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800692void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700693 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
Jason Samsa17af042010-11-17 15:29:32 -0800694 rsc->setProgramVertex(pv);
Jason Samsd19f10d2009-05-22 14:03:28 -0700695}
696
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800697void rsi_ContextBindFont(Context *rsc, RsFont vfont) {
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700698 Font *font = static_cast<Font *>(vfont);
699 rsc->setFont(font);
700}
701
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -0700702void rsi_AssignName(Context *rsc, RsObjectBase obj, const char *name, uint32_t name_length) {
Jason Sams3eaa3382009-06-10 15:04:38 -0700703 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -0700704 rsc->assignName(ob, name, name_length);
Jason Sams3eaa3382009-06-10 15:04:38 -0700705}
Jason Samsd19f10d2009-05-22 14:03:28 -0700706
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800707void rsi_ObjDestroy(Context *rsc, void *optr) {
Jason Sams3b9c52a2010-10-14 17:48:46 -0700708 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams7ce033d2009-08-18 14:14:24 -0700709 rsc->removeName(ob);
Jason Sams07ae4062009-08-27 20:23:34 -0700710 ob->decUserRef();
Jason Sams7ce033d2009-08-18 14:14:24 -0700711}
712
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800713void rsi_ContextPause(Context *rsc) {
Jason Sams65e7aa52009-09-24 17:38:20 -0700714 rsc->pause();
715}
716
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800717void rsi_ContextResume(Context *rsc) {
Jason Sams65e7aa52009-09-24 17:38:20 -0700718 rsc->resume();
719}
720
Alex Sakhartchouke7c4a752011-04-06 10:57:51 -0700721void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur, size_t sur_length) {
Mathias Agopian128ce4b2010-02-12 14:04:35 -0800722 rsc->setSurface(w, h, sur);
Jason Sams3bc47d42009-11-12 15:10:25 -0800723}
724
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800725void rsi_ContextSetPriority(Context *rsc, int32_t p) {
Jason Sams7d787b42009-11-15 12:14:26 -0800726 rsc->setPriority(p);
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800727}
728
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800729void rsi_ContextDump(Context *rsc, int32_t bits) {
Jason Sams715333b2009-11-17 17:26:46 -0800730 ObjectBase::dumpAll(rsc);
731}
732
Jason Sams5c68a712010-12-24 14:38:39 -0800733void rsi_ContextDestroyWorker(Context *rsc) {
Jason Sams5c68a712010-12-24 14:38:39 -0800734 rsc->destroyWorkerThreadResources();;
Jason Sams5c68a712010-12-24 14:38:39 -0800735}
736
737}
738}
739
740void rsContextDestroy(RsContext vcon) {
741 LOGV("rsContextDestroy %p", vcon);
742 Context *rsc = static_cast<Context *>(vcon);
743 rsContextDestroyWorker(rsc);
Jason Sams546f01b2010-12-09 12:19:46 -0800744 delete rsc;
Jason Sams5c68a712010-12-24 14:38:39 -0800745 LOGV("rsContextDestroy 2 %p", vcon);
Jason Sams546f01b2010-12-09 12:19:46 -0800746}
747
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800748RsContext rsContextCreate(RsDevice vdev, uint32_t version) {
Jason Sams704ff642010-02-09 16:05:07 -0800749 LOGV("rsContextCreate %p", vdev);
Jason Samsd19f10d2009-05-22 14:03:28 -0700750 Device * dev = static_cast<Device *>(vdev);
Jason Samsd5f06302010-11-03 14:27:11 -0700751 Context *rsc = Context::createContext(dev, NULL);
Jason Sams704ff642010-02-09 16:05:07 -0800752 return rsc;
753}
754
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700755RsContext rsContextCreateGL(RsDevice vdev, uint32_t version,
756 RsSurfaceConfig sc, uint32_t dpi) {
Jason Sams11c8af92010-10-13 15:31:10 -0700757 LOGV("rsContextCreateGL %p", vdev);
Jason Sams704ff642010-02-09 16:05:07 -0800758 Device * dev = static_cast<Device *>(vdev);
Jason Samsd5f06302010-11-03 14:27:11 -0700759 Context *rsc = Context::createContext(dev, &sc);
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700760 rsc->setDPI(dpi);
Jason Samsd081fff2010-09-16 18:18:29 -0700761 LOGV("rsContextCreateGL ret %p ", rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700762 return rsc;
763}
764
Jason Sams65bdaf12011-04-26 14:50:00 -0700765RsMessageToClientType rsContextPeekMessage(RsContext vrsc,
766 size_t * receiveLen, size_t receiveLen_length,
767 uint32_t * subID, size_t subID_length, bool wait) {
Jason Sams516c3192009-10-06 13:58:47 -0700768 Context * rsc = static_cast<Context *>(vrsc);
Jason Sams1c415172010-11-08 17:06:46 -0800769 return rsc->peekMessageToClient(receiveLen, subID, wait);
770}
771
Jason Sams65bdaf12011-04-26 14:50:00 -0700772RsMessageToClientType rsContextGetMessage(RsContext vrsc, void * data, size_t data_length,
773 size_t * receiveLen, size_t receiveLen_length,
774 uint32_t * subID, size_t subID_length, bool wait) {
Jason Sams1c415172010-11-08 17:06:46 -0800775 Context * rsc = static_cast<Context *>(vrsc);
Jason Sams65bdaf12011-04-26 14:50:00 -0700776 rsAssert(subID_length == sizeof(uint32_t));
777 rsAssert(receiveLen_length == sizeof(size_t));
778 return rsc->getMessageToClient(data, receiveLen, subID, data_length, wait);
Jason Sams516c3192009-10-06 13:58:47 -0700779}
780
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800781void rsContextInitToClient(RsContext vrsc) {
Jason Sams516c3192009-10-06 13:58:47 -0700782 Context * rsc = static_cast<Context *>(vrsc);
783 rsc->initToClient();
784}
785
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800786void rsContextDeinitToClient(RsContext vrsc) {
Jason Sams516c3192009-10-06 13:58:47 -0700787 Context * rsc = static_cast<Context *>(vrsc);
788 rsc->deinitToClient();
789}
790
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700791// Only to be called at a3d load time, before object is visible to user
792// not thread safe
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800793void rsaGetName(RsContext con, void * obj, const char **name) {
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700794 ObjectBase *ob = static_cast<ObjectBase *>(obj);
795 (*name) = ob->getName();
796}