blob: 3f0458526e9bad0fdb76111a1c1aca4b5322ac55 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "rsDevice.h"
18#include "rsContext.h"
19#include "rsThreadIO.h"
Mathias Agopian5ae678f2009-06-22 18:01:09 -070020#include <ui/FramebufferNativeWindow.h>
Jason Sams6b8552a2010-10-13 15:31:10 -070021#include <ui/PixelFormat.h>
Jason Samsafcb25c2009-08-25 11:34:49 -070022#include <ui/EGLUtils.h>
Mathias Agopian9b97c292010-02-12 12:00:38 -080023#include <ui/egl/android_natives.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 Sams1aa5a4e2009-06-22 17:15:15 -070031#include <GLES/gl.h>
32#include <GLES/glext.h>
Jason Sams4815c0d2009-12-15 12:58:36 -080033#include <GLES2/gl2.h>
34#include <GLES2/gl2ext.h>
Jason Sams1aa5a4e2009-06-22 17:15:15 -070035
Jason Sams15832442009-11-15 12:14:26 -080036#include <cutils/sched_policy.h>
Jason Sams8d957fa2010-09-28 14:41:22 -070037#include <sys/syscall.h>
38#include <string.h>
Jason Sams15832442009-11-15 12:14:26 -080039
Jason Sams326e0dd2009-05-22 14:03:28 -070040using namespace android;
41using namespace android::renderscript;
42
Jason Samse5769102009-06-19 16:03:18 -070043pthread_key_t Context::gThreadTLSKey = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070044uint32_t Context::gThreadTLSKeyCount = 0;
Jason Sams33b6e3b2009-10-27 14:44:31 -070045uint32_t Context::gGLContextCount = 0;
Jason Samsfb03a222009-10-15 16:47:31 -070046pthread_mutex_t Context::gInitMutex = PTHREAD_MUTEX_INITIALIZER;
Jason Sams326e0dd2009-05-22 14:03:28 -070047
Jason Sams33b6e3b2009-10-27 14:44:31 -070048static void checkEglError(const char* op, EGLBoolean returnVal = EGL_TRUE) {
49 if (returnVal != EGL_TRUE) {
50 fprintf(stderr, "%s() returned %d\n", op, returnVal);
51 }
52
53 for (EGLint error = eglGetError(); error != EGL_SUCCESS; error
54 = eglGetError()) {
55 fprintf(stderr, "after %s() eglError %s (0x%x)\n", op, EGLUtils::strerror(error),
56 error);
57 }
58}
59
Jason Sams6b8552a2010-10-13 15:31:10 -070060void printEGLConfiguration(EGLDisplay dpy, EGLConfig config) {
61
62#define X(VAL) {VAL, #VAL}
63 struct {EGLint attribute; const char* name;} names[] = {
64 X(EGL_BUFFER_SIZE),
65 X(EGL_ALPHA_SIZE),
66 X(EGL_BLUE_SIZE),
67 X(EGL_GREEN_SIZE),
68 X(EGL_RED_SIZE),
69 X(EGL_DEPTH_SIZE),
70 X(EGL_STENCIL_SIZE),
71 X(EGL_CONFIG_CAVEAT),
72 X(EGL_CONFIG_ID),
73 X(EGL_LEVEL),
74 X(EGL_MAX_PBUFFER_HEIGHT),
75 X(EGL_MAX_PBUFFER_PIXELS),
76 X(EGL_MAX_PBUFFER_WIDTH),
77 X(EGL_NATIVE_RENDERABLE),
78 X(EGL_NATIVE_VISUAL_ID),
79 X(EGL_NATIVE_VISUAL_TYPE),
80 X(EGL_SAMPLES),
81 X(EGL_SAMPLE_BUFFERS),
82 X(EGL_SURFACE_TYPE),
83 X(EGL_TRANSPARENT_TYPE),
84 X(EGL_TRANSPARENT_RED_VALUE),
85 X(EGL_TRANSPARENT_GREEN_VALUE),
86 X(EGL_TRANSPARENT_BLUE_VALUE),
87 X(EGL_BIND_TO_TEXTURE_RGB),
88 X(EGL_BIND_TO_TEXTURE_RGBA),
89 X(EGL_MIN_SWAP_INTERVAL),
90 X(EGL_MAX_SWAP_INTERVAL),
91 X(EGL_LUMINANCE_SIZE),
92 X(EGL_ALPHA_MASK_SIZE),
93 X(EGL_COLOR_BUFFER_TYPE),
94 X(EGL_RENDERABLE_TYPE),
95 X(EGL_CONFORMANT),
96 };
97#undef X
98
99 for (size_t j = 0; j < sizeof(names) / sizeof(names[0]); j++) {
100 EGLint value = -1;
101 EGLint returnVal = eglGetConfigAttrib(dpy, config, names[j].attribute, &value);
102 EGLint error = eglGetError();
103 if (returnVal && error == EGL_SUCCESS) {
104 LOGV(" %s: %d (0x%x)", names[j].name, value, value);
105 }
106 }
107}
108
109
110void Context::initGLThread()
Jason Sams326e0dd2009-05-22 14:03:28 -0700111{
Jason Sams6b8552a2010-10-13 15:31:10 -0700112 pthread_mutex_lock(&gInitMutex);
113 LOGV("initGLThread start %p", this);
114
Jason Samsafcb25c2009-08-25 11:34:49 -0700115 mEGL.mNumConfigs = -1;
116 EGLint configAttribs[128];
117 EGLint *configAttribsPtr = configAttribs;
Jason Samsc460e552009-11-25 13:22:07 -0800118 EGLint context_attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
Jason Sams326e0dd2009-05-22 14:03:28 -0700119
Jason Samsafcb25c2009-08-25 11:34:49 -0700120 memset(configAttribs, 0, sizeof(configAttribs));
Jason Sams326e0dd2009-05-22 14:03:28 -0700121
Jason Samsafcb25c2009-08-25 11:34:49 -0700122 configAttribsPtr[0] = EGL_SURFACE_TYPE;
123 configAttribsPtr[1] = EGL_WINDOW_BIT;
124 configAttribsPtr += 2;
Jason Sams326e0dd2009-05-22 14:03:28 -0700125
Jason Sams6b8552a2010-10-13 15:31:10 -0700126 configAttribsPtr[0] = EGL_RENDERABLE_TYPE;
127 configAttribsPtr[1] = EGL_OPENGL_ES2_BIT;
128 configAttribsPtr += 2;
Jason Samsc460e552009-11-25 13:22:07 -0800129
Jason Sams6b8552a2010-10-13 15:31:10 -0700130 if (mUserSurfaceConfig.depthMin > 0) {
Jason Samsafcb25c2009-08-25 11:34:49 -0700131 configAttribsPtr[0] = EGL_DEPTH_SIZE;
Jason Sams6b8552a2010-10-13 15:31:10 -0700132 configAttribsPtr[1] = mUserSurfaceConfig.depthMin;
Jason Samsafcb25c2009-08-25 11:34:49 -0700133 configAttribsPtr += 2;
134 }
Jason Sams9397e302009-08-27 20:23:34 -0700135
Jason Sams5fd09d82009-09-23 13:57:02 -0700136 if (mDev->mForceSW) {
137 configAttribsPtr[0] = EGL_CONFIG_CAVEAT;
138 configAttribsPtr[1] = EGL_SLOW_CONFIG;
139 configAttribsPtr += 2;
140 }
141
Jason Samsafcb25c2009-08-25 11:34:49 -0700142 configAttribsPtr[0] = EGL_NONE;
143 rsAssert(configAttribsPtr < (configAttribs + (sizeof(configAttribs) / sizeof(EGLint))));
Jason Sams326e0dd2009-05-22 14:03:28 -0700144
Jason Sams4c5f99e2010-09-14 14:59:03 -0700145 LOGV("%p initEGL start", this);
Jason Samsafcb25c2009-08-25 11:34:49 -0700146 mEGL.mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700147 checkEglError("eglGetDisplay");
148
Jason Samsafcb25c2009-08-25 11:34:49 -0700149 eglInitialize(mEGL.mDisplay, &mEGL.mMajorVersion, &mEGL.mMinorVersion);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700150 checkEglError("eglInitialize");
Jason Samsafcb25c2009-08-25 11:34:49 -0700151
Jason Sams6b8552a2010-10-13 15:31:10 -0700152#if 1
153 PixelFormat pf = PIXEL_FORMAT_RGBA_8888;
154 if (mUserSurfaceConfig.alphaMin == 0) {
155 pf = PIXEL_FORMAT_RGBX_8888;
156 }
157
158 status_t err = EGLUtils::selectConfigForPixelFormat(mEGL.mDisplay, configAttribs, pf, &mEGL.mConfig);
Jason Samsafcb25c2009-08-25 11:34:49 -0700159 if (err) {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700160 LOGE("%p, couldn't find an EGLConfig matching the screen format\n", this);
Jason Samsafcb25c2009-08-25 11:34:49 -0700161 }
Jason Sams6b8552a2010-10-13 15:31:10 -0700162 if (props.mLogVisual) {
163 printEGLConfiguration(mEGL.mDisplay, mEGL.mConfig);
Jason Samsc460e552009-11-25 13:22:07 -0800164 }
Jason Sams6b8552a2010-10-13 15:31:10 -0700165#else
166 eglChooseConfig(mEGL.mDisplay, configAttribs, &mEGL.mConfig, 1, &mEGL.mNumConfigs);
167#endif
168
169 mEGL.mContext = eglCreateContext(mEGL.mDisplay, mEGL.mConfig, EGL_NO_CONTEXT, context_attribs2);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700170 checkEglError("eglCreateContext");
171 if (mEGL.mContext == EGL_NO_CONTEXT) {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700172 LOGE("%p, eglCreateContext returned EGL_NO_CONTEXT", this);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700173 }
174 gGLContextCount++;
Jason Sams6b8552a2010-10-13 15:31:10 -0700175
176
177 EGLint pbuffer_attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
178 mEGL.mSurfaceDefault = eglCreatePbufferSurface(mEGL.mDisplay, mEGL.mConfig, pbuffer_attribs);
179 checkEglError("eglCreatePbufferSurface");
180 if (mEGL.mSurfaceDefault == EGL_NO_SURFACE) {
181 LOGE("eglCreatePbufferSurface returned EGL_NO_SURFACE");
182 }
183
184 EGLBoolean ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurfaceDefault, mEGL.mSurfaceDefault, mEGL.mContext);
185 checkEglError("eglMakeCurrent", ret);
186
187 mGL.mVersion = glGetString(GL_VERSION);
188 mGL.mVendor = glGetString(GL_VENDOR);
189 mGL.mRenderer = glGetString(GL_RENDERER);
190 mGL.mExtensions = glGetString(GL_EXTENSIONS);
191
192 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
193 LOGV("GL Version %s", mGL.mVersion);
194 //LOGV("GL Vendor %s", mGL.mVendor);
195 LOGV("GL Renderer %s", mGL.mRenderer);
196 //LOGV("GL Extensions %s", mGL.mExtensions);
197
198 const char *verptr = NULL;
199 if (strlen((const char *)mGL.mVersion) > 9) {
200 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
201 verptr = (const char *)mGL.mVersion + 12;
202 }
203 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
204 verptr = (const char *)mGL.mVersion + 9;
205 }
206 }
207
208 if (!verptr) {
209 LOGE("Error, OpenGL ES Lite not supported");
210 } else {
211 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
212 }
213
214 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
215 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
216 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
217
218 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
219 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
220
221 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
222 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
223
224 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700225 mGL.GL_NV_texture_npot_2D_mipmap = NULL != strstr((const char *)mGL.mExtensions, "GL_NV_texture_npot_2D_mipmap");
Jason Sams6b8552a2010-10-13 15:31:10 -0700226 mGL.EXT_texture_max_aniso = 1.0f;
227 bool hasAniso = NULL != strstr((const char *)mGL.mExtensions, "GL_EXT_texture_filter_anisotropic");
228 if(hasAniso) {
229 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &mGL.EXT_texture_max_aniso);
230 }
231
232 LOGV("initGLThread end %p", this);
233 pthread_mutex_unlock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700234}
235
Jason Sams33b6e3b2009-10-27 14:44:31 -0700236void Context::deinitEGL()
237{
Jason Sams4c5f99e2010-09-14 14:59:03 -0700238 LOGV("%p, deinitEGL", this);
Jason Sams6b8552a2010-10-13 15:31:10 -0700239
240 eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, mEGL.mContext);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700241 eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
242 checkEglError("eglDestroyContext");
243
244 gGLContextCount--;
245 if (!gGLContextCount) {
246 eglTerminate(mEGL.mDisplay);
247 }
248}
249
250
Jason Samsc61346b2010-05-28 18:23:22 -0700251uint32_t Context::runScript(Script *s)
Jason Sams10308932009-06-09 12:15:30 -0700252{
253 ObjectBaseRef<ProgramFragment> frag(mFragment);
254 ObjectBaseRef<ProgramVertex> vtx(mVertex);
Jason Samsccc010b2010-05-13 18:30:11 -0700255 ObjectBaseRef<ProgramStore> store(mFragmentStore);
Jason Samsb681c8a2009-09-28 18:12:56 -0700256 ObjectBaseRef<ProgramRaster> raster(mRaster);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700257 ObjectBaseRef<Font> font(mFont);
Jason Sams10308932009-06-09 12:15:30 -0700258
Jason Samsc61346b2010-05-28 18:23:22 -0700259 uint32_t ret = s->run(this);
Jason Sams10308932009-06-09 12:15:30 -0700260
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700261 mFragment.set(frag);
262 mVertex.set(vtx);
263 mFragmentStore.set(store);
Jason Samsb681c8a2009-09-28 18:12:56 -0700264 mRaster.set(raster);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700265 mFont.set(font);
Jason Samsc9d43db2009-07-28 12:02:16 -0700266 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700267}
268
Jason Samsd01d9702009-12-23 14:35:29 -0800269void Context::checkError(const char *msg) const
270{
271 GLenum err = glGetError();
272 if (err != GL_NO_ERROR) {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700273 LOGE("%p, GL Error, 0x%x, from %s", this, err, msg);
Jason Samsd01d9702009-12-23 14:35:29 -0800274 }
275}
Jason Sams10308932009-06-09 12:15:30 -0700276
Jason Sams2dca84d2009-12-09 11:05:45 -0800277uint32_t Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700278{
Jason Sams771565f2010-05-14 15:30:29 -0700279 glViewport(0, 0, mWidth, mHeight);
Jason Sams306fb232009-08-25 17:09:59 -0700280
Jason Sams2dca84d2009-12-09 11:05:45 -0800281 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -0700282 mStateFragmentStore.mLast.clear();
Jason Samsc61346b2010-05-28 18:23:22 -0700283 uint32_t ret = runScript(mRootScript.get());
Jason Sams8cfdd242009-10-14 15:43:53 -0700284
Jason Samsd01d9702009-12-23 14:35:29 -0800285 checkError("runRootScript");
Jason Samscfb1d112009-08-05 13:57:03 -0700286 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700287}
288
Jason Sams24371d92009-08-19 12:17:14 -0700289uint64_t Context::getTime() const
290{
291 struct timespec t;
292 clock_gettime(CLOCK_MONOTONIC, &t);
293 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
294}
295
296void Context::timerReset()
297{
298 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
299 mTimers[ct] = 0;
300 }
301}
302
303void Context::timerInit()
304{
305 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700306 mTimeFrame = mTimeLast;
307 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700308 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700309 mAverageFPSFrameCount = 0;
310 mAverageFPSStartTime = mTimeLast;
311 mAverageFPS = 0;
Jason Sams24371d92009-08-19 12:17:14 -0700312 timerReset();
313}
314
Jason Sams1d54f102009-09-03 15:43:13 -0700315void Context::timerFrame()
316{
317 mTimeLastFrame = mTimeFrame;
318 mTimeFrame = getTime();
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700319 // Update average fps
320 const uint64_t averageFramerateInterval = 1000 * 1000000;
321 mAverageFPSFrameCount ++;
322 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
323 if(inverval >= averageFramerateInterval) {
324 inverval = inverval / 1000000;
325 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
326 mAverageFPSFrameCount = 0;
327 mAverageFPSStartTime = mTimeFrame;
328 }
Jason Sams1d54f102009-09-03 15:43:13 -0700329}
330
Jason Sams24371d92009-08-19 12:17:14 -0700331void Context::timerSet(Timers tm)
332{
333 uint64_t last = mTimeLast;
334 mTimeLast = getTime();
335 mTimers[mTimerActive] += mTimeLast - last;
336 mTimerActive = tm;
337}
338
339void Context::timerPrint()
340{
341 double total = 0;
342 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
343 total += mTimers[ct];
344 }
Jason Sams1d54f102009-09-03 15:43:13 -0700345 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800346 mTimeMSLastFrame = frame / 1000000;
347 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
348 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700349
Jason Sams2dca84d2009-12-09 11:05:45 -0800350
351 if (props.mLogTimes) {
Alex Sakhartchouk64cd98e2010-10-18 17:18:50 -0700352 LOGV("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 -0800353 mTimeMSLastFrame,
354 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
355 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
356 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700357 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
358 mAverageFPS);
Jason Sams2dca84d2009-12-09 11:05:45 -0800359 }
Jason Sams24371d92009-08-19 12:17:14 -0700360}
361
Jason Samsa2cf7552010-03-03 13:03:18 -0800362bool Context::setupCheck()
Jason Sams326e0dd2009-05-22 14:03:28 -0700363{
Jason Sams900f1612010-09-16 18:18:29 -0700364 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
365 LOGE("Context::setupCheck() 1 fail");
366 return false;
Jason Samsc460e552009-11-25 13:22:07 -0800367 }
Jason Sams900f1612010-09-16 18:18:29 -0700368
369 mFragmentStore->setupGL2(this, &mStateFragmentStore);
370 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
371 mRaster->setupGL2(this, &mStateRaster);
372 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
Jason Samsa2cf7552010-03-03 13:03:18 -0800373 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700374}
375
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700376void Context::setupProgramStore() {
377 mFragmentStore->setupGL2(this, &mStateFragmentStore);
378}
379
Jason Sams1fddd902009-09-25 15:25:00 -0700380static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700381{
382 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700383 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700384 return 0 != strcmp(buf, "0");
385}
Jason Sams326e0dd2009-05-22 14:03:28 -0700386
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700387void Context::displayDebugStats()
388{
389 char buffer[128];
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700390 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700391 float oldR, oldG, oldB, oldA;
392 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700393 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700394
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700395 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700396 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700397 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700398
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700399 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700400 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700401
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700402 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700403}
404
Jason Sams326e0dd2009-05-22 14:03:28 -0700405void * Context::threadProc(void *vrsc)
406{
407 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800408 rsc->mNativeThreadId = gettid();
409
410 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800411 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700412
Jason Sams1fddd902009-09-25 15:25:00 -0700413 rsc->props.mLogTimes = getProp("debug.rs.profile");
414 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800415 rsc->props.mLogObjects = getProp("debug.rs.object");
416 rsc->props.mLogShaders = getProp("debug.rs.shader");
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700417 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes");
418 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms");
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700419 rsc->props.mLogVisual = getProp("debug.rs.visual");
Joe Onorato76371ff2009-09-23 16:37:36 -0700420
Jason Sams605048a2010-09-30 18:15:52 -0700421 rsc->mTlsStruct = new ScriptTLSStruct;
422 if (!rsc->mTlsStruct) {
Jason Samse5769102009-06-19 16:03:18 -0700423 LOGE("Error allocating tls storage");
424 return NULL;
425 }
Jason Sams605048a2010-09-30 18:15:52 -0700426 rsc->mTlsStruct->mContext = rsc;
427 rsc->mTlsStruct->mScript = NULL;
428 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
Jason Samse5769102009-06-19 16:03:18 -0700429 if (status) {
430 LOGE("pthread_setspecific %i", status);
431 }
432
Jason Sams6b8552a2010-10-13 15:31:10 -0700433 rsc->initGLThread();
434
Stephen Hines01b7d292010-09-28 15:45:45 -0700435 rsc->mScriptC.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800436 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700437 rsc->mStateRaster.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800438 rsc->setRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700439 rsc->mStateVertex.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800440 rsc->setVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700441 rsc->mStateFragment.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800442 rsc->setFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700443 rsc->mStateFragmentStore.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800444 rsc->setFragmentStore(NULL);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700445 rsc->mStateFont.init(rsc);
446 rsc->setFont(NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -0800447 rsc->mStateVertexArray.init(rsc);
448 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700449
Jason Sams326e0dd2009-05-22 14:03:28 -0700450 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700451 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700452 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700453 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700454 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800455 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700456
Jason Sams2dca84d2009-12-09 11:05:45 -0800457 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800458 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800459 targetTime = rsc->runRootScript();
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700460
461 if(rsc->props.mLogVisual) {
462 rsc->displayDebugStats();
463 }
464
Jason Sams2dca84d2009-12-09 11:05:45 -0800465 mDraw = targetTime && !rsc->mPaused;
466 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700467 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800468 rsc->timerFrame();
469 rsc->timerSet(RS_TIMER_INTERNAL);
470 rsc->timerPrint();
471 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700472 }
Jason Sams2dca84d2009-12-09 11:05:45 -0800473 if (rsc->mThreadPriority > 0 && targetTime) {
474 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
475 if (t > 0) {
476 usleep(t);
477 }
478 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700479 }
480
Jason Sams4c5f99e2010-09-14 14:59:03 -0700481 LOGV("%p, RS Thread exiting", rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800482 if (rsc->mIsGraphicsContext) {
483 rsc->mRaster.clear();
484 rsc->mFragment.clear();
485 rsc->mVertex.clear();
486 rsc->mFragmentStore.clear();
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700487 rsc->mFont.clear();
Jason Sams4820e8b2010-02-09 16:05:07 -0800488 rsc->mRootScript.clear();
489 rsc->mStateRaster.deinit(rsc);
490 rsc->mStateVertex.deinit(rsc);
491 rsc->mStateFragment.deinit(rsc);
492 rsc->mStateFragmentStore.deinit(rsc);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700493 rsc->mStateFont.deinit(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800494 }
Jason Samse514b452009-09-25 14:51:22 -0700495 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700496
Jason Sams4820e8b2010-02-09 16:05:07 -0800497 if (rsc->mIsGraphicsContext) {
498 pthread_mutex_lock(&gInitMutex);
499 rsc->deinitEGL();
500 pthread_mutex_unlock(&gInitMutex);
501 }
Jason Sams605048a2010-09-30 18:15:52 -0700502 delete rsc->mTlsStruct;
Jason Sams33b6e3b2009-10-27 14:44:31 -0700503
Jason Sams4c5f99e2010-09-14 14:59:03 -0700504 LOGV("%p, RS Thread exited", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700505 return NULL;
506}
507
Jason Sams7bf29dd2010-07-19 15:38:19 -0700508void * Context::helperThreadProc(void *vrsc)
509{
510 Context *rsc = static_cast<Context *>(vrsc);
511 uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount);
512
Jason Sams18133402010-07-20 15:09:00 -0700513 LOGV("RS helperThread starting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700514
515 rsc->mWorkers.mLaunchSignals[idx].init();
516 rsc->mWorkers.mNativeThreadId[idx] = gettid();
517
Jason Sams8d957fa2010-09-28 14:41:22 -0700518#if 0
519 typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
520 cpu_set_t cpuset;
521 memset(&cpuset, 0, sizeof(cpuset));
522 cpuset.bits[idx / 64] |= 1ULL << (idx % 64);
523 int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx],
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700524 sizeof(cpuset), &cpuset);
Jason Sams8d957fa2010-09-28 14:41:22 -0700525 LOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
526#endif
Jason Sams7bf29dd2010-07-19 15:38:19 -0700527
528 setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority);
Jason Sams605048a2010-09-30 18:15:52 -0700529 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
530 if (status) {
531 LOGE("pthread_setspecific %i", status);
532 }
533
Jason Sams7bf29dd2010-07-19 15:38:19 -0700534 while(rsc->mRunning) {
535 rsc->mWorkers.mLaunchSignals[idx].wait();
536 if (rsc->mWorkers.mLaunchCallback) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700537 rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx);
538 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700539 android_atomic_dec(&rsc->mWorkers.mRunningCount);
540 rsc->mWorkers.mCompleteSignal.set();
541 }
Jason Sams18133402010-07-20 15:09:00 -0700542
543 LOGV("RS helperThread exiting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700544 return NULL;
545}
546
547void Context::launchThreads(WorkerCallback_t cbk, void *data)
548{
549 mWorkers.mLaunchData = data;
550 mWorkers.mLaunchCallback = cbk;
551 mWorkers.mRunningCount = (int)mWorkers.mCount;
552 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
553 mWorkers.mLaunchSignals[ct].set();
554 }
555 while(mWorkers.mRunningCount) {
556 mWorkers.mCompleteSignal.wait();
557 }
558}
559
Jason Sams15832442009-11-15 12:14:26 -0800560void Context::setPriority(int32_t p)
561{
562 // Note: If we put this in the proper "background" policy
563 // the wallpapers can become completly unresponsive at times.
564 // This is probably not what we want for something the user is actively
565 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800566 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800567#if 0
568 SchedPolicy pol = SP_FOREGROUND;
569 if (p > 0) {
570 pol = SP_BACKGROUND;
571 }
572 if (!set_sched_policy(mNativeThreadId, pol)) {
573 // success; reset the priority as well
574 }
575#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700576 setpriority(PRIO_PROCESS, mNativeThreadId, p);
577 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
578 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p);
579 }
Jason Sams15832442009-11-15 12:14:26 -0800580#endif
581}
582
Jason Sams6b8552a2010-10-13 15:31:10 -0700583Context::Context(Device *dev, const RsSurfaceConfig *sc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700584{
Jason Samsfb03a222009-10-15 16:47:31 -0700585 pthread_mutex_lock(&gInitMutex);
586
Jason Sams326e0dd2009-05-22 14:03:28 -0700587 dev->addContext(this);
588 mDev = dev;
589 mRunning = false;
590 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700591 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700592 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800593 mError = RS_ERROR_NONE;
594 mErrorMsg = NULL;
Jason Sams6b8552a2010-10-13 15:31:10 -0700595 if (sc) {
596 mUserSurfaceConfig = *sc;
597 } else {
598 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
599 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800600
Jason Sams613cad12009-11-12 15:10:25 -0800601 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800602 memset(&mGL, 0, sizeof(mGL));
Jason Sams6b8552a2010-10-13 15:31:10 -0700603 mIsGraphicsContext = sc != NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700604
Jason Samsa658e902009-06-04 14:35:01 -0700605 int status;
606 pthread_attr_t threadAttr;
607
Jason Samsfb03a222009-10-15 16:47:31 -0700608 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700609 status = pthread_key_create(&gThreadTLSKey, NULL);
610 if (status) {
611 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700612 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700613 return;
614 }
Jason Samse5769102009-06-19 16:03:18 -0700615 }
Jason Samsfb03a222009-10-15 16:47:31 -0700616 gThreadTLSKeyCount++;
Jason Sams6b8552a2010-10-13 15:31:10 -0700617
Jason Samsfb03a222009-10-15 16:47:31 -0700618 pthread_mutex_unlock(&gInitMutex);
619
620 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700621
Jason Samsa658e902009-06-04 14:35:01 -0700622 status = pthread_attr_init(&threadAttr);
623 if (status) {
624 LOGE("Failed to init thread attribute.");
625 return;
626 }
627
Jason Sams613cad12009-11-12 15:10:25 -0800628 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700629
Jason Sams24371d92009-08-19 12:17:14 -0700630 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700631 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700632
Jason Sams7c7c78a2010-07-26 17:12:55 -0700633 int cpu = sysconf(_SC_NPROCESSORS_ONLN);
634 LOGV("RS Launching thread(s), reported CPU count %i", cpu);
635 if (cpu < 2) cpu = 0;
636
637 mWorkers.mCount = (uint32_t)cpu;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700638 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
639 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
640 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
641 mWorkers.mLaunchCallback = NULL;
Jason Samsa658e902009-06-04 14:35:01 -0700642 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700643 if (status) {
644 LOGE("Failed to start rs context thread.");
Jason Sams7bf29dd2010-07-19 15:38:19 -0700645 return;
646 }
Jason Sams18133402010-07-20 15:09:00 -0700647 while(!mRunning) {
648 usleep(100);
649 }
650
Jason Sams8d957fa2010-09-28 14:41:22 -0700651 mWorkers.mCompleteSignal.init();
Jason Sams7bf29dd2010-07-19 15:38:19 -0700652 mWorkers.mRunningCount = 0;
653 mWorkers.mLaunchCount = 0;
654 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
655 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
656 if (status) {
657 mWorkers.mCount = ct;
658 LOGE("Created fewer than expected number of RS threads.");
659 break;
660 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700661 }
Jason Samsa658e902009-06-04 14:35:01 -0700662 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700663}
664
665Context::~Context()
666{
Jason Sams8c0ee652009-08-25 14:49:07 -0700667 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700668 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700669 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700670 void *res;
671
Jason Sams8c0ee652009-08-25 14:49:07 -0700672 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700673 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700674
Jason Samsfb03a222009-10-15 16:47:31 -0700675 // Global structure cleanup.
676 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700677 if (mDev) {
678 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700679 --gThreadTLSKeyCount;
680 if (!gThreadTLSKeyCount) {
681 pthread_key_delete(gThreadTLSKey);
682 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800683 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700684 }
Jason Samsfb03a222009-10-15 16:47:31 -0700685 pthread_mutex_unlock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700686}
687
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700688void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800689{
Jason Sams4820e8b2010-02-09 16:05:07 -0800690 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800691
Jason Sams458f2dc2009-11-03 13:58:36 -0800692 EGLBoolean ret;
693 if (mEGL.mSurface != NULL) {
Jason Sams6b8552a2010-10-13 15:31:10 -0700694 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurfaceDefault, mEGL.mSurfaceDefault, mEGL.mContext);
Jason Sams458f2dc2009-11-03 13:58:36 -0800695 checkEglError("eglMakeCurrent", ret);
696
697 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
698 checkEglError("eglDestroySurface", ret);
699
700 mEGL.mSurface = NULL;
Jason Sams6b8552a2010-10-13 15:31:10 -0700701 mWidth = 1;
702 mHeight = 1;
Jason Sams458f2dc2009-11-03 13:58:36 -0800703 }
704
705 mWndSurface = sur;
706 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700707 mWidth = w;
708 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800709
Jason Sams458f2dc2009-11-03 13:58:36 -0800710 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
711 checkEglError("eglCreateWindowSurface");
712 if (mEGL.mSurface == EGL_NO_SURFACE) {
713 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
714 }
715
716 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
717 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800718
Jason Sams771565f2010-05-14 15:30:29 -0700719 mStateVertex.updateSize(this);
Jason Sams458f2dc2009-11-03 13:58:36 -0800720 }
721}
722
Jason Sams86f1b232009-09-24 17:38:20 -0700723void Context::pause()
724{
Jason Sams4820e8b2010-02-09 16:05:07 -0800725 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700726 mPaused = true;
727}
728
729void Context::resume()
730{
Jason Sams4820e8b2010-02-09 16:05:07 -0800731 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700732 mPaused = false;
733}
734
Jason Sams326e0dd2009-05-22 14:03:28 -0700735void Context::setRootScript(Script *s)
736{
Jason Sams4820e8b2010-02-09 16:05:07 -0800737 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700738 mRootScript.set(s);
739}
740
Jason Samsccc010b2010-05-13 18:30:11 -0700741void Context::setFragmentStore(ProgramStore *pfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700742{
Jason Sams4820e8b2010-02-09 16:05:07 -0800743 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700744 if (pfs == NULL) {
745 mFragmentStore.set(mStateFragmentStore.mDefault);
746 } else {
747 mFragmentStore.set(pfs);
748 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700749}
750
751void Context::setFragment(ProgramFragment *pf)
752{
Jason Sams4820e8b2010-02-09 16:05:07 -0800753 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700754 if (pf == NULL) {
755 mFragment.set(mStateFragment.mDefault);
756 } else {
757 mFragment.set(pf);
758 }
Jason Samscfb1d112009-08-05 13:57:03 -0700759}
760
Jason Sams5fd09d82009-09-23 13:57:02 -0700761void Context::setRaster(ProgramRaster *pr)
762{
Jason Sams4820e8b2010-02-09 16:05:07 -0800763 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700764 if (pr == NULL) {
765 mRaster.set(mStateRaster.mDefault);
766 } else {
767 mRaster.set(pr);
768 }
769}
770
Jason Sams326e0dd2009-05-22 14:03:28 -0700771void Context::setVertex(ProgramVertex *pv)
772{
Jason Sams4820e8b2010-02-09 16:05:07 -0800773 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700774 if (pv == NULL) {
775 mVertex.set(mStateVertex.mDefault);
776 } else {
777 mVertex.set(pv);
778 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700779}
780
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700781void Context::setFont(Font *f)
782{
783 rsAssert(mIsGraphicsContext);
784 if (f == NULL) {
785 mFont.set(mStateFont.mDefault);
786 } else {
787 mFont.set(f);
788 }
789}
790
Jason Samsa4a54e42009-06-10 18:39:40 -0700791void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700792{
793 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700794 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700795 mNames.add(obj);
796}
797
798void Context::removeName(ObjectBase *obj)
799{
800 for(size_t ct=0; ct < mNames.size(); ct++) {
801 if (obj == mNames[ct]) {
802 mNames.removeAt(ct);
803 return;
804 }
805 }
806}
807
Jason Sams8c401ef2009-10-06 13:58:47 -0700808uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
809{
810 //LOGE("getMessageToClient %i %i", bufferLen, wait);
Jason Sams39cd3172010-08-25 14:31:48 -0700811 *receiveLen = 0;
Jason Sams8c401ef2009-10-06 13:58:47 -0700812 if (!wait) {
813 if (mIO.mToClient.isEmpty()) {
814 // No message to get and not going to wait for one.
Jason Sams8c401ef2009-10-06 13:58:47 -0700815 return 0;
816 }
817 }
818
819 //LOGE("getMessageToClient 2 con=%p", this);
820 uint32_t bytesData = 0;
821 uint32_t commandID = 0;
822 const void *d = mIO.mToClient.get(&commandID, &bytesData);
823 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
824
825 *receiveLen = bytesData;
826 if (bufferLen >= bytesData) {
827 memcpy(data, d, bytesData);
828 mIO.mToClient.next();
829 return commandID;
830 }
831 return 0;
832}
833
834bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
835{
836 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
837 if (cmdID == 0) {
838 LOGE("Attempting to send invalid command 0 to client.");
839 return false;
840 }
841 if (!waitForSpace) {
Jason Sams8c46b102010-08-18 12:38:03 -0700842 if (!mIO.mToClient.makeSpaceNonBlocking(len + 8)) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700843 // Not enough room, and not waiting.
844 return false;
845 }
846 }
847 //LOGE("sendMessageToClient 2");
Jason Samsef5867a2010-07-28 11:17:53 -0700848 if (len > 0) {
849 void *p = mIO.mToClient.reserve(len);
850 memcpy(p, data, len);
851 mIO.mToClient.commit(cmdID, len);
852 } else {
853 mIO.mToClient.commit(cmdID, 0);
854 }
Jason Sams8c401ef2009-10-06 13:58:47 -0700855 //LOGE("sendMessageToClient 3");
856 return true;
857}
858
859void Context::initToClient()
860{
861 while(!mRunning) {
862 usleep(100);
863 }
864}
865
866void Context::deinitToClient()
867{
868 mIO.mToClient.shutdown();
869}
Jason Sams50869382009-08-18 17:07:09 -0700870
Jason Samsa2cf7552010-03-03 13:03:18 -0800871const char * Context::getError(RsError *err)
872{
873 *err = mError;
874 mError = RS_ERROR_NONE;
875 if (*err != RS_ERROR_NONE) {
876 return mErrorMsg;
877 }
878 return NULL;
879}
880
881void Context::setError(RsError e, const char *msg)
882{
883 mError = e;
884 mErrorMsg = msg;
885}
886
887
Jason Sams13e26342009-11-24 12:26:35 -0800888void Context::dumpDebug() const
889{
890 LOGE("RS Context debug %p", this);
891 LOGE("RS Context debug");
892
893 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700894 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800895 LOGE(" GL vendor: %s", mGL.mVendor);
896 LOGE(" GL renderer: %s", mGL.mRenderer);
897 LOGE(" GL Version: %s", mGL.mVersion);
898 LOGE(" GL Extensions: %s", mGL.mExtensions);
899 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
900 LOGE(" RS width %i, height %i", mWidth, mHeight);
Jason Sams6b8552a2010-10-13 15:31:10 -0700901 LOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
Jason Sams13e26342009-11-24 12:26:35 -0800902 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
903
Jason Sams4815c0d2009-12-15 12:58:36 -0800904 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
905 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
906 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
907 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800908}
Jason Samsa4a54e42009-06-10 18:39:40 -0700909
Jason Sams326e0dd2009-05-22 14:03:28 -0700910///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700911//
Jason Sams326e0dd2009-05-22 14:03:28 -0700912
913namespace android {
914namespace renderscript {
915
Jason Sams8c880902010-06-15 12:15:57 -0700916void rsi_ContextFinish(Context *rsc)
917{
918}
Jason Sams326e0dd2009-05-22 14:03:28 -0700919
920void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
921{
922 Script *s = static_cast<Script *>(vs);
923 rsc->setRootScript(s);
924}
925
926void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
927{
928 Sampler *s = static_cast<Sampler *>(vs);
929
930 if (slot > RS_MAX_SAMPLER_SLOT) {
931 LOGE("Invalid sampler slot");
932 return;
933 }
934
935 s->bindToContext(&rsc->mStateSampler, slot);
936}
937
Jason Samsccc010b2010-05-13 18:30:11 -0700938void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700939{
Jason Samsccc010b2010-05-13 18:30:11 -0700940 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700941 rsc->setFragmentStore(pfs);
942}
943
944void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
945{
946 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
947 rsc->setFragment(pf);
948}
949
Jason Sams5fd09d82009-09-23 13:57:02 -0700950void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
951{
952 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
953 rsc->setRaster(pr);
954}
955
Jason Sams326e0dd2009-05-22 14:03:28 -0700956void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
957{
958 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
959 rsc->setVertex(pv);
960}
961
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700962void rsi_ContextBindFont(Context *rsc, RsFont vfont)
963{
964 Font *font = static_cast<Font *>(vfont);
965 rsc->setFont(font);
966}
967
Jason Samsa4a54e42009-06-10 18:39:40 -0700968void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700969{
970 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700971 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700972}
Jason Sams326e0dd2009-05-22 14:03:28 -0700973
Jason Sams2353ae32010-10-14 17:48:46 -0700974void rsi_ObjDestroy(Context *rsc, void *optr)
Jason Sams707aaf32009-08-18 14:14:24 -0700975{
Jason Sams2353ae32010-10-14 17:48:46 -0700976 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -0700977 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700978 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700979}
980
Jason Sams86f1b232009-09-24 17:38:20 -0700981void rsi_ContextPause(Context *rsc)
982{
983 rsc->pause();
984}
985
986void rsi_ContextResume(Context *rsc)
987{
988 rsc->resume();
989}
990
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700991void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800992{
Mathias Agopianfa402862010-02-12 14:04:35 -0800993 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800994}
995
Jason Sams15832442009-11-15 12:14:26 -0800996void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -0800997{
Jason Sams15832442009-11-15 12:14:26 -0800998 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -0800999}
1000
Jason Samsc21cf402009-11-17 17:26:46 -08001001void rsi_ContextDump(Context *rsc, int32_t bits)
1002{
1003 ObjectBase::dumpAll(rsc);
1004}
1005
Jason Samsa2cf7552010-03-03 13:03:18 -08001006const char * rsi_ContextGetError(Context *rsc, RsError *e)
1007{
1008 const char *msg = rsc->getError(e);
1009 if (*e != RS_ERROR_NONE) {
1010 LOGE("RS Error %i %s", *e, msg);
1011 }
1012 return msg;
1013}
1014
Jason Sams326e0dd2009-05-22 14:03:28 -07001015}
1016}
1017
1018
Jason Sams4820e8b2010-02-09 16:05:07 -08001019RsContext rsContextCreate(RsDevice vdev, uint32_t version)
Jason Sams326e0dd2009-05-22 14:03:28 -07001020{
Jason Sams4820e8b2010-02-09 16:05:07 -08001021 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -07001022 Device * dev = static_cast<Device *>(vdev);
Jason Sams6b8552a2010-10-13 15:31:10 -07001023 Context *rsc = new Context(dev, NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -08001024 return rsc;
1025}
1026
Jason Sams6b8552a2010-10-13 15:31:10 -07001027RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, RsSurfaceConfig sc)
Jason Sams4820e8b2010-02-09 16:05:07 -08001028{
Jason Sams6b8552a2010-10-13 15:31:10 -07001029 LOGV("rsContextCreateGL %p", vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -08001030 Device * dev = static_cast<Device *>(vdev);
Jason Sams6b8552a2010-10-13 15:31:10 -07001031 Context *rsc = new Context(dev, &sc);
Jason Sams900f1612010-09-16 18:18:29 -07001032 LOGV("rsContextCreateGL ret %p ", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -07001033 return rsc;
1034}
1035
1036void rsContextDestroy(RsContext vrsc)
1037{
1038 Context * rsc = static_cast<Context *>(vrsc);
1039 delete rsc;
1040}
1041
Jason Sams8c401ef2009-10-06 13:58:47 -07001042uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
1043{
1044 Context * rsc = static_cast<Context *>(vrsc);
1045 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
1046}
1047
1048void rsContextInitToClient(RsContext vrsc)
1049{
1050 Context * rsc = static_cast<Context *>(vrsc);
1051 rsc->initToClient();
1052}
1053
1054void rsContextDeinitToClient(RsContext vrsc)
1055{
1056 Context * rsc = static_cast<Context *>(vrsc);
1057 rsc->deinitToClient();
1058}
1059
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -07001060// Only to be called at a3d load time, before object is visible to user
1061// not thread safe
1062void rsaGetName(RsContext con, void * obj, const char **name)
1063{
1064 ObjectBase *ob = static_cast<ObjectBase *>(obj);
1065 (*name) = ob->getName();
1066}