blob: 944cd86c3d4e3fab75a146e89593bcbae3c3dbb9 [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");
225 mGL.EXT_texture_max_aniso = 1.0f;
226 bool hasAniso = NULL != strstr((const char *)mGL.mExtensions, "GL_EXT_texture_filter_anisotropic");
227 if(hasAniso) {
228 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &mGL.EXT_texture_max_aniso);
229 }
230
231 LOGV("initGLThread end %p", this);
232 pthread_mutex_unlock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700233}
234
Jason Sams33b6e3b2009-10-27 14:44:31 -0700235void Context::deinitEGL()
236{
Jason Sams4c5f99e2010-09-14 14:59:03 -0700237 LOGV("%p, deinitEGL", this);
Jason Sams6b8552a2010-10-13 15:31:10 -0700238
239 eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, mEGL.mContext);
Jason Sams33b6e3b2009-10-27 14:44:31 -0700240 eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
241 checkEglError("eglDestroyContext");
242
243 gGLContextCount--;
244 if (!gGLContextCount) {
245 eglTerminate(mEGL.mDisplay);
246 }
247}
248
249
Jason Samsc61346b2010-05-28 18:23:22 -0700250uint32_t Context::runScript(Script *s)
Jason Sams10308932009-06-09 12:15:30 -0700251{
252 ObjectBaseRef<ProgramFragment> frag(mFragment);
253 ObjectBaseRef<ProgramVertex> vtx(mVertex);
Jason Samsccc010b2010-05-13 18:30:11 -0700254 ObjectBaseRef<ProgramStore> store(mFragmentStore);
Jason Samsb681c8a2009-09-28 18:12:56 -0700255 ObjectBaseRef<ProgramRaster> raster(mRaster);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700256 ObjectBaseRef<Font> font(mFont);
Jason Sams10308932009-06-09 12:15:30 -0700257
Jason Samsc61346b2010-05-28 18:23:22 -0700258 uint32_t ret = s->run(this);
Jason Sams10308932009-06-09 12:15:30 -0700259
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700260 mFragment.set(frag);
261 mVertex.set(vtx);
262 mFragmentStore.set(store);
Jason Samsb681c8a2009-09-28 18:12:56 -0700263 mRaster.set(raster);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700264 mFont.set(font);
Jason Samsc9d43db2009-07-28 12:02:16 -0700265 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700266}
267
Jason Samsd01d9702009-12-23 14:35:29 -0800268void Context::checkError(const char *msg) const
269{
270 GLenum err = glGetError();
271 if (err != GL_NO_ERROR) {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700272 LOGE("%p, GL Error, 0x%x, from %s", this, err, msg);
Jason Samsd01d9702009-12-23 14:35:29 -0800273 }
274}
Jason Sams10308932009-06-09 12:15:30 -0700275
Jason Sams2dca84d2009-12-09 11:05:45 -0800276uint32_t Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700277{
Jason Sams771565f2010-05-14 15:30:29 -0700278 glViewport(0, 0, mWidth, mHeight);
Jason Sams306fb232009-08-25 17:09:59 -0700279
Jason Sams2dca84d2009-12-09 11:05:45 -0800280 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -0700281 mStateFragmentStore.mLast.clear();
Jason Samsc61346b2010-05-28 18:23:22 -0700282 uint32_t ret = runScript(mRootScript.get());
Jason Sams8cfdd242009-10-14 15:43:53 -0700283
Jason Samsd01d9702009-12-23 14:35:29 -0800284 checkError("runRootScript");
Jason Samscfb1d112009-08-05 13:57:03 -0700285 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700286}
287
Jason Sams24371d92009-08-19 12:17:14 -0700288uint64_t Context::getTime() const
289{
290 struct timespec t;
291 clock_gettime(CLOCK_MONOTONIC, &t);
292 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
293}
294
295void Context::timerReset()
296{
297 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
298 mTimers[ct] = 0;
299 }
300}
301
302void Context::timerInit()
303{
304 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700305 mTimeFrame = mTimeLast;
306 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700307 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700308 mAverageFPSFrameCount = 0;
309 mAverageFPSStartTime = mTimeLast;
310 mAverageFPS = 0;
Jason Sams24371d92009-08-19 12:17:14 -0700311 timerReset();
312}
313
Jason Sams1d54f102009-09-03 15:43:13 -0700314void Context::timerFrame()
315{
316 mTimeLastFrame = mTimeFrame;
317 mTimeFrame = getTime();
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700318 // Update average fps
319 const uint64_t averageFramerateInterval = 1000 * 1000000;
320 mAverageFPSFrameCount ++;
321 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
322 if(inverval >= averageFramerateInterval) {
323 inverval = inverval / 1000000;
324 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
325 mAverageFPSFrameCount = 0;
326 mAverageFPSStartTime = mTimeFrame;
327 }
Jason Sams1d54f102009-09-03 15:43:13 -0700328}
329
Jason Sams24371d92009-08-19 12:17:14 -0700330void Context::timerSet(Timers tm)
331{
332 uint64_t last = mTimeLast;
333 mTimeLast = getTime();
334 mTimers[mTimerActive] += mTimeLast - last;
335 mTimerActive = tm;
336}
337
338void Context::timerPrint()
339{
340 double total = 0;
341 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
342 total += mTimers[ct];
343 }
Jason Sams1d54f102009-09-03 15:43:13 -0700344 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800345 mTimeMSLastFrame = frame / 1000000;
346 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
347 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700348
Jason Sams2dca84d2009-12-09 11:05:45 -0800349
350 if (props.mLogTimes) {
Alex Sakhartchouk64cd98e2010-10-18 17:18:50 -0700351 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 -0800352 mTimeMSLastFrame,
353 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
354 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
355 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700356 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
357 mAverageFPS);
Jason Sams2dca84d2009-12-09 11:05:45 -0800358 }
Jason Sams24371d92009-08-19 12:17:14 -0700359}
360
Jason Samsa2cf7552010-03-03 13:03:18 -0800361bool Context::setupCheck()
Jason Sams326e0dd2009-05-22 14:03:28 -0700362{
Jason Sams900f1612010-09-16 18:18:29 -0700363 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
364 LOGE("Context::setupCheck() 1 fail");
365 return false;
Jason Samsc460e552009-11-25 13:22:07 -0800366 }
Jason Sams900f1612010-09-16 18:18:29 -0700367
368 mFragmentStore->setupGL2(this, &mStateFragmentStore);
369 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
370 mRaster->setupGL2(this, &mStateRaster);
371 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
Jason Samsa2cf7552010-03-03 13:03:18 -0800372 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700373}
374
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700375void Context::setupProgramStore() {
376 mFragmentStore->setupGL2(this, &mStateFragmentStore);
377}
378
Jason Sams1fddd902009-09-25 15:25:00 -0700379static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700380{
381 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700382 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700383 return 0 != strcmp(buf, "0");
384}
Jason Sams326e0dd2009-05-22 14:03:28 -0700385
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700386void Context::displayDebugStats()
387{
388 char buffer[128];
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700389 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700390 float oldR, oldG, oldB, oldA;
391 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700392 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700393
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700394 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700395 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700396 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700397
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700398 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700399 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700400
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700401 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700402}
403
Jason Sams326e0dd2009-05-22 14:03:28 -0700404void * Context::threadProc(void *vrsc)
405{
406 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800407 rsc->mNativeThreadId = gettid();
408
409 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800410 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700411
Jason Sams1fddd902009-09-25 15:25:00 -0700412 rsc->props.mLogTimes = getProp("debug.rs.profile");
413 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800414 rsc->props.mLogObjects = getProp("debug.rs.object");
415 rsc->props.mLogShaders = getProp("debug.rs.shader");
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700416 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes");
417 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms");
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700418 rsc->props.mLogVisual = getProp("debug.rs.visual");
Joe Onorato76371ff2009-09-23 16:37:36 -0700419
Jason Sams605048a2010-09-30 18:15:52 -0700420 rsc->mTlsStruct = new ScriptTLSStruct;
421 if (!rsc->mTlsStruct) {
Jason Samse5769102009-06-19 16:03:18 -0700422 LOGE("Error allocating tls storage");
423 return NULL;
424 }
Jason Sams605048a2010-09-30 18:15:52 -0700425 rsc->mTlsStruct->mContext = rsc;
426 rsc->mTlsStruct->mScript = NULL;
427 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
Jason Samse5769102009-06-19 16:03:18 -0700428 if (status) {
429 LOGE("pthread_setspecific %i", status);
430 }
431
Jason Sams6b8552a2010-10-13 15:31:10 -0700432 rsc->initGLThread();
433
Stephen Hines01b7d292010-09-28 15:45:45 -0700434 rsc->mScriptC.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800435 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700436 rsc->mStateRaster.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800437 rsc->setRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700438 rsc->mStateVertex.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800439 rsc->setVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700440 rsc->mStateFragment.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800441 rsc->setFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700442 rsc->mStateFragmentStore.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800443 rsc->setFragmentStore(NULL);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700444 rsc->mStateFont.init(rsc);
445 rsc->setFont(NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -0800446 rsc->mStateVertexArray.init(rsc);
447 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700448
Jason Sams326e0dd2009-05-22 14:03:28 -0700449 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700450 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700451 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700452 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700453 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800454 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700455
Jason Sams2dca84d2009-12-09 11:05:45 -0800456 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800457 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800458 targetTime = rsc->runRootScript();
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700459
460 if(rsc->props.mLogVisual) {
461 rsc->displayDebugStats();
462 }
463
Jason Sams2dca84d2009-12-09 11:05:45 -0800464 mDraw = targetTime && !rsc->mPaused;
465 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700466 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800467 rsc->timerFrame();
468 rsc->timerSet(RS_TIMER_INTERNAL);
469 rsc->timerPrint();
470 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700471 }
Jason Sams2dca84d2009-12-09 11:05:45 -0800472 if (rsc->mThreadPriority > 0 && targetTime) {
473 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
474 if (t > 0) {
475 usleep(t);
476 }
477 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700478 }
479
Jason Sams4c5f99e2010-09-14 14:59:03 -0700480 LOGV("%p, RS Thread exiting", rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800481 if (rsc->mIsGraphicsContext) {
482 rsc->mRaster.clear();
483 rsc->mFragment.clear();
484 rsc->mVertex.clear();
485 rsc->mFragmentStore.clear();
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700486 rsc->mFont.clear();
Jason Sams4820e8b2010-02-09 16:05:07 -0800487 rsc->mRootScript.clear();
488 rsc->mStateRaster.deinit(rsc);
489 rsc->mStateVertex.deinit(rsc);
490 rsc->mStateFragment.deinit(rsc);
491 rsc->mStateFragmentStore.deinit(rsc);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700492 rsc->mStateFont.deinit(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800493 }
Jason Samse514b452009-09-25 14:51:22 -0700494 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700495
Jason Sams4820e8b2010-02-09 16:05:07 -0800496 if (rsc->mIsGraphicsContext) {
497 pthread_mutex_lock(&gInitMutex);
498 rsc->deinitEGL();
499 pthread_mutex_unlock(&gInitMutex);
500 }
Jason Sams605048a2010-09-30 18:15:52 -0700501 delete rsc->mTlsStruct;
Jason Sams33b6e3b2009-10-27 14:44:31 -0700502
Jason Sams4c5f99e2010-09-14 14:59:03 -0700503 LOGV("%p, RS Thread exited", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700504 return NULL;
505}
506
Jason Sams7bf29dd2010-07-19 15:38:19 -0700507void * Context::helperThreadProc(void *vrsc)
508{
509 Context *rsc = static_cast<Context *>(vrsc);
510 uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount);
511
Jason Sams18133402010-07-20 15:09:00 -0700512 LOGV("RS helperThread starting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700513
514 rsc->mWorkers.mLaunchSignals[idx].init();
515 rsc->mWorkers.mNativeThreadId[idx] = gettid();
516
Jason Sams8d957fa2010-09-28 14:41:22 -0700517#if 0
518 typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
519 cpu_set_t cpuset;
520 memset(&cpuset, 0, sizeof(cpuset));
521 cpuset.bits[idx / 64] |= 1ULL << (idx % 64);
522 int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx],
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700523 sizeof(cpuset), &cpuset);
Jason Sams8d957fa2010-09-28 14:41:22 -0700524 LOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
525#endif
Jason Sams7bf29dd2010-07-19 15:38:19 -0700526
527 setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority);
Jason Sams605048a2010-09-30 18:15:52 -0700528 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
529 if (status) {
530 LOGE("pthread_setspecific %i", status);
531 }
532
Jason Sams7bf29dd2010-07-19 15:38:19 -0700533 while(rsc->mRunning) {
534 rsc->mWorkers.mLaunchSignals[idx].wait();
535 if (rsc->mWorkers.mLaunchCallback) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700536 rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx);
537 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700538 android_atomic_dec(&rsc->mWorkers.mRunningCount);
539 rsc->mWorkers.mCompleteSignal.set();
540 }
Jason Sams18133402010-07-20 15:09:00 -0700541
542 LOGV("RS helperThread exiting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700543 return NULL;
544}
545
546void Context::launchThreads(WorkerCallback_t cbk, void *data)
547{
548 mWorkers.mLaunchData = data;
549 mWorkers.mLaunchCallback = cbk;
550 mWorkers.mRunningCount = (int)mWorkers.mCount;
551 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
552 mWorkers.mLaunchSignals[ct].set();
553 }
554 while(mWorkers.mRunningCount) {
555 mWorkers.mCompleteSignal.wait();
556 }
557}
558
Jason Sams15832442009-11-15 12:14:26 -0800559void Context::setPriority(int32_t p)
560{
561 // Note: If we put this in the proper "background" policy
562 // the wallpapers can become completly unresponsive at times.
563 // This is probably not what we want for something the user is actively
564 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800565 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800566#if 0
567 SchedPolicy pol = SP_FOREGROUND;
568 if (p > 0) {
569 pol = SP_BACKGROUND;
570 }
571 if (!set_sched_policy(mNativeThreadId, pol)) {
572 // success; reset the priority as well
573 }
574#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700575 setpriority(PRIO_PROCESS, mNativeThreadId, p);
576 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
577 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p);
578 }
Jason Sams15832442009-11-15 12:14:26 -0800579#endif
580}
581
Jason Sams6b8552a2010-10-13 15:31:10 -0700582Context::Context(Device *dev, const RsSurfaceConfig *sc)
Jason Sams326e0dd2009-05-22 14:03:28 -0700583{
Jason Samsfb03a222009-10-15 16:47:31 -0700584 pthread_mutex_lock(&gInitMutex);
585
Jason Sams326e0dd2009-05-22 14:03:28 -0700586 dev->addContext(this);
587 mDev = dev;
588 mRunning = false;
589 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700590 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700591 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800592 mError = RS_ERROR_NONE;
593 mErrorMsg = NULL;
Jason Sams6b8552a2010-10-13 15:31:10 -0700594 if (sc) {
595 mUserSurfaceConfig = *sc;
596 } else {
597 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
598 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800599
Jason Sams613cad12009-11-12 15:10:25 -0800600 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800601 memset(&mGL, 0, sizeof(mGL));
Jason Sams6b8552a2010-10-13 15:31:10 -0700602 mIsGraphicsContext = sc != NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700603
Jason Samsa658e902009-06-04 14:35:01 -0700604 int status;
605 pthread_attr_t threadAttr;
606
Jason Samsfb03a222009-10-15 16:47:31 -0700607 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700608 status = pthread_key_create(&gThreadTLSKey, NULL);
609 if (status) {
610 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700611 pthread_mutex_unlock(&gInitMutex);
Jason Sams9e4e13d2009-10-06 17:16:55 -0700612 return;
613 }
Jason Samse5769102009-06-19 16:03:18 -0700614 }
Jason Samsfb03a222009-10-15 16:47:31 -0700615 gThreadTLSKeyCount++;
Jason Sams6b8552a2010-10-13 15:31:10 -0700616
Jason Samsfb03a222009-10-15 16:47:31 -0700617 pthread_mutex_unlock(&gInitMutex);
618
619 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700620
Jason Samsa658e902009-06-04 14:35:01 -0700621 status = pthread_attr_init(&threadAttr);
622 if (status) {
623 LOGE("Failed to init thread attribute.");
624 return;
625 }
626
Jason Sams613cad12009-11-12 15:10:25 -0800627 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700628
Jason Sams24371d92009-08-19 12:17:14 -0700629 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700630 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700631
Jason Sams7c7c78a2010-07-26 17:12:55 -0700632 int cpu = sysconf(_SC_NPROCESSORS_ONLN);
633 LOGV("RS Launching thread(s), reported CPU count %i", cpu);
634 if (cpu < 2) cpu = 0;
635
636 mWorkers.mCount = (uint32_t)cpu;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700637 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
638 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
639 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
640 mWorkers.mLaunchCallback = NULL;
Jason Samsa658e902009-06-04 14:35:01 -0700641 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700642 if (status) {
643 LOGE("Failed to start rs context thread.");
Jason Sams7bf29dd2010-07-19 15:38:19 -0700644 return;
645 }
Jason Sams18133402010-07-20 15:09:00 -0700646 while(!mRunning) {
647 usleep(100);
648 }
649
Jason Sams8d957fa2010-09-28 14:41:22 -0700650 mWorkers.mCompleteSignal.init();
Jason Sams7bf29dd2010-07-19 15:38:19 -0700651 mWorkers.mRunningCount = 0;
652 mWorkers.mLaunchCount = 0;
653 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
654 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
655 if (status) {
656 mWorkers.mCount = ct;
657 LOGE("Created fewer than expected number of RS threads.");
658 break;
659 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700660 }
Jason Samsa658e902009-06-04 14:35:01 -0700661 pthread_attr_destroy(&threadAttr);
Jason Sams326e0dd2009-05-22 14:03:28 -0700662}
663
664Context::~Context()
665{
Jason Sams8c0ee652009-08-25 14:49:07 -0700666 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700667 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700668 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700669 void *res;
670
Jason Sams8c0ee652009-08-25 14:49:07 -0700671 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700672 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700673
Jason Samsfb03a222009-10-15 16:47:31 -0700674 // Global structure cleanup.
675 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700676 if (mDev) {
677 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700678 --gThreadTLSKeyCount;
679 if (!gThreadTLSKeyCount) {
680 pthread_key_delete(gThreadTLSKey);
681 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800682 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700683 }
Jason Samsfb03a222009-10-15 16:47:31 -0700684 pthread_mutex_unlock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700685}
686
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700687void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800688{
Jason Sams4820e8b2010-02-09 16:05:07 -0800689 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800690
Jason Sams458f2dc2009-11-03 13:58:36 -0800691 EGLBoolean ret;
692 if (mEGL.mSurface != NULL) {
Jason Sams6b8552a2010-10-13 15:31:10 -0700693 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurfaceDefault, mEGL.mSurfaceDefault, mEGL.mContext);
Jason Sams458f2dc2009-11-03 13:58:36 -0800694 checkEglError("eglMakeCurrent", ret);
695
696 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
697 checkEglError("eglDestroySurface", ret);
698
699 mEGL.mSurface = NULL;
Jason Sams6b8552a2010-10-13 15:31:10 -0700700 mWidth = 1;
701 mHeight = 1;
Jason Sams458f2dc2009-11-03 13:58:36 -0800702 }
703
704 mWndSurface = sur;
705 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700706 mWidth = w;
707 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800708
Jason Sams458f2dc2009-11-03 13:58:36 -0800709 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
710 checkEglError("eglCreateWindowSurface");
711 if (mEGL.mSurface == EGL_NO_SURFACE) {
712 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
713 }
714
715 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
716 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800717
Jason Sams771565f2010-05-14 15:30:29 -0700718 mStateVertex.updateSize(this);
Jason Sams458f2dc2009-11-03 13:58:36 -0800719 }
720}
721
Jason Sams86f1b232009-09-24 17:38:20 -0700722void Context::pause()
723{
Jason Sams4820e8b2010-02-09 16:05:07 -0800724 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700725 mPaused = true;
726}
727
728void Context::resume()
729{
Jason Sams4820e8b2010-02-09 16:05:07 -0800730 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700731 mPaused = false;
732}
733
Jason Sams326e0dd2009-05-22 14:03:28 -0700734void Context::setRootScript(Script *s)
735{
Jason Sams4820e8b2010-02-09 16:05:07 -0800736 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700737 mRootScript.set(s);
738}
739
Jason Samsccc010b2010-05-13 18:30:11 -0700740void Context::setFragmentStore(ProgramStore *pfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700741{
Jason Sams4820e8b2010-02-09 16:05:07 -0800742 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700743 if (pfs == NULL) {
744 mFragmentStore.set(mStateFragmentStore.mDefault);
745 } else {
746 mFragmentStore.set(pfs);
747 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700748}
749
750void Context::setFragment(ProgramFragment *pf)
751{
Jason Sams4820e8b2010-02-09 16:05:07 -0800752 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700753 if (pf == NULL) {
754 mFragment.set(mStateFragment.mDefault);
755 } else {
756 mFragment.set(pf);
757 }
Jason Samscfb1d112009-08-05 13:57:03 -0700758}
759
Jason Sams5fd09d82009-09-23 13:57:02 -0700760void Context::setRaster(ProgramRaster *pr)
761{
Jason Sams4820e8b2010-02-09 16:05:07 -0800762 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700763 if (pr == NULL) {
764 mRaster.set(mStateRaster.mDefault);
765 } else {
766 mRaster.set(pr);
767 }
768}
769
Jason Sams326e0dd2009-05-22 14:03:28 -0700770void Context::setVertex(ProgramVertex *pv)
771{
Jason Sams4820e8b2010-02-09 16:05:07 -0800772 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700773 if (pv == NULL) {
774 mVertex.set(mStateVertex.mDefault);
775 } else {
776 mVertex.set(pv);
777 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700778}
779
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700780void Context::setFont(Font *f)
781{
782 rsAssert(mIsGraphicsContext);
783 if (f == NULL) {
784 mFont.set(mStateFont.mDefault);
785 } else {
786 mFont.set(f);
787 }
788}
789
Jason Samsa4a54e42009-06-10 18:39:40 -0700790void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700791{
792 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700793 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700794 mNames.add(obj);
795}
796
797void Context::removeName(ObjectBase *obj)
798{
799 for(size_t ct=0; ct < mNames.size(); ct++) {
800 if (obj == mNames[ct]) {
801 mNames.removeAt(ct);
802 return;
803 }
804 }
805}
806
Jason Sams8c401ef2009-10-06 13:58:47 -0700807uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
808{
809 //LOGE("getMessageToClient %i %i", bufferLen, wait);
Jason Sams39cd3172010-08-25 14:31:48 -0700810 *receiveLen = 0;
Jason Sams8c401ef2009-10-06 13:58:47 -0700811 if (!wait) {
812 if (mIO.mToClient.isEmpty()) {
813 // No message to get and not going to wait for one.
Jason Sams8c401ef2009-10-06 13:58:47 -0700814 return 0;
815 }
816 }
817
818 //LOGE("getMessageToClient 2 con=%p", this);
819 uint32_t bytesData = 0;
820 uint32_t commandID = 0;
821 const void *d = mIO.mToClient.get(&commandID, &bytesData);
822 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
823
824 *receiveLen = bytesData;
825 if (bufferLen >= bytesData) {
826 memcpy(data, d, bytesData);
827 mIO.mToClient.next();
828 return commandID;
829 }
830 return 0;
831}
832
833bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
834{
835 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
836 if (cmdID == 0) {
837 LOGE("Attempting to send invalid command 0 to client.");
838 return false;
839 }
840 if (!waitForSpace) {
Jason Sams8c46b102010-08-18 12:38:03 -0700841 if (!mIO.mToClient.makeSpaceNonBlocking(len + 8)) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700842 // Not enough room, and not waiting.
843 return false;
844 }
845 }
846 //LOGE("sendMessageToClient 2");
Jason Samsef5867a2010-07-28 11:17:53 -0700847 if (len > 0) {
848 void *p = mIO.mToClient.reserve(len);
849 memcpy(p, data, len);
850 mIO.mToClient.commit(cmdID, len);
851 } else {
852 mIO.mToClient.commit(cmdID, 0);
853 }
Jason Sams8c401ef2009-10-06 13:58:47 -0700854 //LOGE("sendMessageToClient 3");
855 return true;
856}
857
858void Context::initToClient()
859{
860 while(!mRunning) {
861 usleep(100);
862 }
863}
864
865void Context::deinitToClient()
866{
867 mIO.mToClient.shutdown();
868}
Jason Sams50869382009-08-18 17:07:09 -0700869
Jason Samsa2cf7552010-03-03 13:03:18 -0800870const char * Context::getError(RsError *err)
871{
872 *err = mError;
873 mError = RS_ERROR_NONE;
874 if (*err != RS_ERROR_NONE) {
875 return mErrorMsg;
876 }
877 return NULL;
878}
879
880void Context::setError(RsError e, const char *msg)
881{
882 mError = e;
883 mErrorMsg = msg;
884}
885
886
Jason Sams13e26342009-11-24 12:26:35 -0800887void Context::dumpDebug() const
888{
889 LOGE("RS Context debug %p", this);
890 LOGE("RS Context debug");
891
892 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700893 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800894 LOGE(" GL vendor: %s", mGL.mVendor);
895 LOGE(" GL renderer: %s", mGL.mRenderer);
896 LOGE(" GL Version: %s", mGL.mVersion);
897 LOGE(" GL Extensions: %s", mGL.mExtensions);
898 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
899 LOGE(" RS width %i, height %i", mWidth, mHeight);
Jason Sams6b8552a2010-10-13 15:31:10 -0700900 LOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
Jason Sams13e26342009-11-24 12:26:35 -0800901 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
902
Jason Sams4815c0d2009-12-15 12:58:36 -0800903 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
904 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
905 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
906 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800907}
Jason Samsa4a54e42009-06-10 18:39:40 -0700908
Jason Sams326e0dd2009-05-22 14:03:28 -0700909///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700910//
Jason Sams326e0dd2009-05-22 14:03:28 -0700911
912namespace android {
913namespace renderscript {
914
Jason Sams8c880902010-06-15 12:15:57 -0700915void rsi_ContextFinish(Context *rsc)
916{
917}
Jason Sams326e0dd2009-05-22 14:03:28 -0700918
919void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
920{
921 Script *s = static_cast<Script *>(vs);
922 rsc->setRootScript(s);
923}
924
925void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
926{
927 Sampler *s = static_cast<Sampler *>(vs);
928
929 if (slot > RS_MAX_SAMPLER_SLOT) {
930 LOGE("Invalid sampler slot");
931 return;
932 }
933
934 s->bindToContext(&rsc->mStateSampler, slot);
935}
936
Jason Samsccc010b2010-05-13 18:30:11 -0700937void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700938{
Jason Samsccc010b2010-05-13 18:30:11 -0700939 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700940 rsc->setFragmentStore(pfs);
941}
942
943void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
944{
945 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
946 rsc->setFragment(pf);
947}
948
Jason Sams5fd09d82009-09-23 13:57:02 -0700949void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
950{
951 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
952 rsc->setRaster(pr);
953}
954
Jason Sams326e0dd2009-05-22 14:03:28 -0700955void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
956{
957 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
958 rsc->setVertex(pv);
959}
960
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700961void rsi_ContextBindFont(Context *rsc, RsFont vfont)
962{
963 Font *font = static_cast<Font *>(vfont);
964 rsc->setFont(font);
965}
966
Jason Samsa4a54e42009-06-10 18:39:40 -0700967void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700968{
969 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -0700970 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700971}
Jason Sams326e0dd2009-05-22 14:03:28 -0700972
Alex Sakhartchouk9fc9f032010-08-04 14:45:48 -0700973void rsi_GetName(Context *rsc, void * obj, const char **name)
974{
975 ObjectBase *ob = static_cast<ObjectBase *>(obj);
976 (*name) = ob->getName();
977}
978
Jason Sams2353ae32010-10-14 17:48:46 -0700979void rsi_ObjDestroy(Context *rsc, void *optr)
Jason Sams707aaf32009-08-18 14:14:24 -0700980{
Jason Sams2353ae32010-10-14 17:48:46 -0700981 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -0700982 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -0700983 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -0700984}
985
Jason Sams86f1b232009-09-24 17:38:20 -0700986void rsi_ContextPause(Context *rsc)
987{
988 rsc->pause();
989}
990
991void rsi_ContextResume(Context *rsc)
992{
993 rsc->resume();
994}
995
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700996void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800997{
Mathias Agopianfa402862010-02-12 14:04:35 -0800998 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -0800999}
1000
Jason Sams15832442009-11-15 12:14:26 -08001001void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -08001002{
Jason Sams15832442009-11-15 12:14:26 -08001003 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -08001004}
1005
Jason Samsc21cf402009-11-17 17:26:46 -08001006void rsi_ContextDump(Context *rsc, int32_t bits)
1007{
1008 ObjectBase::dumpAll(rsc);
1009}
1010
Jason Samsa2cf7552010-03-03 13:03:18 -08001011const char * rsi_ContextGetError(Context *rsc, RsError *e)
1012{
1013 const char *msg = rsc->getError(e);
1014 if (*e != RS_ERROR_NONE) {
1015 LOGE("RS Error %i %s", *e, msg);
1016 }
1017 return msg;
1018}
1019
Jason Sams326e0dd2009-05-22 14:03:28 -07001020}
1021}
1022
1023
Jason Sams4820e8b2010-02-09 16:05:07 -08001024RsContext rsContextCreate(RsDevice vdev, uint32_t version)
Jason Sams326e0dd2009-05-22 14:03:28 -07001025{
Jason Sams4820e8b2010-02-09 16:05:07 -08001026 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -07001027 Device * dev = static_cast<Device *>(vdev);
Jason Sams6b8552a2010-10-13 15:31:10 -07001028 Context *rsc = new Context(dev, NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -08001029 return rsc;
1030}
1031
Jason Sams6b8552a2010-10-13 15:31:10 -07001032RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, RsSurfaceConfig sc)
Jason Sams4820e8b2010-02-09 16:05:07 -08001033{
Jason Sams6b8552a2010-10-13 15:31:10 -07001034 LOGV("rsContextCreateGL %p", vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -08001035 Device * dev = static_cast<Device *>(vdev);
Jason Sams6b8552a2010-10-13 15:31:10 -07001036 Context *rsc = new Context(dev, &sc);
Jason Sams900f1612010-09-16 18:18:29 -07001037 LOGV("rsContextCreateGL ret %p ", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -07001038 return rsc;
1039}
1040
1041void rsContextDestroy(RsContext vrsc)
1042{
1043 Context * rsc = static_cast<Context *>(vrsc);
1044 delete rsc;
1045}
1046
Jason Sams8c401ef2009-10-06 13:58:47 -07001047uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
1048{
1049 Context * rsc = static_cast<Context *>(vrsc);
1050 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
1051}
1052
1053void rsContextInitToClient(RsContext vrsc)
1054{
1055 Context * rsc = static_cast<Context *>(vrsc);
1056 rsc->initToClient();
1057}
1058
1059void rsContextDeinitToClient(RsContext vrsc)
1060{
1061 Context * rsc = static_cast<Context *>(vrsc);
1062 rsc->deinitToClient();
1063}
1064