blob: 678d327dc05da94e145af9bbbefd21c38a039f87 [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
Jason Sams5c1c79a2010-11-03 14:27:11 -0700110bool 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 Sams5c1c79a2010-11-03 14:27:11 -0700172 pthread_mutex_unlock(&gInitMutex);
Jason Sams4c5f99e2010-09-14 14:59:03 -0700173 LOGE("%p, eglCreateContext returned EGL_NO_CONTEXT", this);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700174 return false;
Jason Sams33b6e3b2009-10-27 14:44:31 -0700175 }
176 gGLContextCount++;
Jason Sams6b8552a2010-10-13 15:31:10 -0700177
178
179 EGLint pbuffer_attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
180 mEGL.mSurfaceDefault = eglCreatePbufferSurface(mEGL.mDisplay, mEGL.mConfig, pbuffer_attribs);
181 checkEglError("eglCreatePbufferSurface");
182 if (mEGL.mSurfaceDefault == EGL_NO_SURFACE) {
183 LOGE("eglCreatePbufferSurface returned EGL_NO_SURFACE");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700184 pthread_mutex_unlock(&gInitMutex);
185 deinitEGL();
186 return false;
Jason Sams6b8552a2010-10-13 15:31:10 -0700187 }
188
189 EGLBoolean ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurfaceDefault, mEGL.mSurfaceDefault, mEGL.mContext);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700190 if (ret == EGL_FALSE) {
191 LOGE("eglMakeCurrent returned EGL_FALSE");
192 checkEglError("eglMakeCurrent", ret);
193 pthread_mutex_unlock(&gInitMutex);
194 deinitEGL();
195 return false;
196 }
Jason Sams6b8552a2010-10-13 15:31:10 -0700197
198 mGL.mVersion = glGetString(GL_VERSION);
199 mGL.mVendor = glGetString(GL_VENDOR);
200 mGL.mRenderer = glGetString(GL_RENDERER);
201 mGL.mExtensions = glGetString(GL_EXTENSIONS);
202
203 //LOGV("EGL Version %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
204 LOGV("GL Version %s", mGL.mVersion);
205 //LOGV("GL Vendor %s", mGL.mVendor);
206 LOGV("GL Renderer %s", mGL.mRenderer);
207 //LOGV("GL Extensions %s", mGL.mExtensions);
208
209 const char *verptr = NULL;
210 if (strlen((const char *)mGL.mVersion) > 9) {
211 if (!memcmp(mGL.mVersion, "OpenGL ES-CM", 12)) {
212 verptr = (const char *)mGL.mVersion + 12;
213 }
214 if (!memcmp(mGL.mVersion, "OpenGL ES ", 10)) {
215 verptr = (const char *)mGL.mVersion + 9;
216 }
217 }
218
219 if (!verptr) {
220 LOGE("Error, OpenGL ES Lite not supported");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700221 pthread_mutex_unlock(&gInitMutex);
222 deinitEGL();
223 return false;
Jason Sams6b8552a2010-10-13 15:31:10 -0700224 } else {
225 sscanf(verptr, " %i.%i", &mGL.mMajorVersion, &mGL.mMinorVersion);
226 }
227
228 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &mGL.mMaxVertexAttribs);
229 glGetIntegerv(GL_MAX_VERTEX_UNIFORM_VECTORS, &mGL.mMaxVertexUniformVectors);
230 glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &mGL.mMaxVertexTextureUnits);
231
232 glGetIntegerv(GL_MAX_VARYING_VECTORS, &mGL.mMaxVaryingVectors);
233 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &mGL.mMaxTextureImageUnits);
234
235 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &mGL.mMaxFragmentTextureImageUnits);
236 glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_VECTORS, &mGL.mMaxFragmentUniformVectors);
237
238 mGL.OES_texture_npot = NULL != strstr((const char *)mGL.mExtensions, "GL_OES_texture_npot");
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700239 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 -0700240 mGL.EXT_texture_max_aniso = 1.0f;
241 bool hasAniso = NULL != strstr((const char *)mGL.mExtensions, "GL_EXT_texture_filter_anisotropic");
242 if(hasAniso) {
243 glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &mGL.EXT_texture_max_aniso);
244 }
245
246 LOGV("initGLThread end %p", this);
247 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700248 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700249}
250
Jason Sams33b6e3b2009-10-27 14:44:31 -0700251void Context::deinitEGL()
252{
Jason Sams4c5f99e2010-09-14 14:59:03 -0700253 LOGV("%p, deinitEGL", this);
Jason Sams6b8552a2010-10-13 15:31:10 -0700254
Jason Sams5c1c79a2010-11-03 14:27:11 -0700255 if (mEGL.mContext != EGL_NO_CONTEXT) {
256 eglMakeCurrent(mEGL.mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, mEGL.mContext);
257 eglDestroyContext(mEGL.mDisplay, mEGL.mContext);
258 checkEglError("eglDestroyContext");
259 }
Jason Sams33b6e3b2009-10-27 14:44:31 -0700260
261 gGLContextCount--;
262 if (!gGLContextCount) {
263 eglTerminate(mEGL.mDisplay);
264 }
265}
266
267
Jason Samsc61346b2010-05-28 18:23:22 -0700268uint32_t Context::runScript(Script *s)
Jason Sams10308932009-06-09 12:15:30 -0700269{
270 ObjectBaseRef<ProgramFragment> frag(mFragment);
271 ObjectBaseRef<ProgramVertex> vtx(mVertex);
Jason Samsccc010b2010-05-13 18:30:11 -0700272 ObjectBaseRef<ProgramStore> store(mFragmentStore);
Jason Samsb681c8a2009-09-28 18:12:56 -0700273 ObjectBaseRef<ProgramRaster> raster(mRaster);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700274 ObjectBaseRef<Font> font(mFont);
Jason Sams10308932009-06-09 12:15:30 -0700275
Jason Samsc61346b2010-05-28 18:23:22 -0700276 uint32_t ret = s->run(this);
Jason Sams10308932009-06-09 12:15:30 -0700277
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700278 mFragment.set(frag);
279 mVertex.set(vtx);
280 mFragmentStore.set(store);
Jason Samsb681c8a2009-09-28 18:12:56 -0700281 mRaster.set(raster);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700282 mFont.set(font);
Jason Samsc9d43db2009-07-28 12:02:16 -0700283 return ret;
Jason Sams10308932009-06-09 12:15:30 -0700284}
285
Jason Samsd01d9702009-12-23 14:35:29 -0800286void Context::checkError(const char *msg) const
287{
288 GLenum err = glGetError();
289 if (err != GL_NO_ERROR) {
Jason Sams4c5f99e2010-09-14 14:59:03 -0700290 LOGE("%p, GL Error, 0x%x, from %s", this, err, msg);
Jason Samsd01d9702009-12-23 14:35:29 -0800291 }
292}
Jason Sams10308932009-06-09 12:15:30 -0700293
Jason Sams2dca84d2009-12-09 11:05:45 -0800294uint32_t Context::runRootScript()
Jason Sams326e0dd2009-05-22 14:03:28 -0700295{
Jason Sams771565f2010-05-14 15:30:29 -0700296 glViewport(0, 0, mWidth, mHeight);
Jason Sams306fb232009-08-25 17:09:59 -0700297
Jason Sams2dca84d2009-12-09 11:05:45 -0800298 timerSet(RS_TIMER_SCRIPT);
Jason Sams8c401ef2009-10-06 13:58:47 -0700299 mStateFragmentStore.mLast.clear();
Jason Samsc61346b2010-05-28 18:23:22 -0700300 uint32_t ret = runScript(mRootScript.get());
Jason Sams8cfdd242009-10-14 15:43:53 -0700301
Jason Samsd01d9702009-12-23 14:35:29 -0800302 checkError("runRootScript");
Jason Samscfb1d112009-08-05 13:57:03 -0700303 return ret;
Jason Sams326e0dd2009-05-22 14:03:28 -0700304}
305
Jason Sams24371d92009-08-19 12:17:14 -0700306uint64_t Context::getTime() const
307{
308 struct timespec t;
309 clock_gettime(CLOCK_MONOTONIC, &t);
310 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
311}
312
313void Context::timerReset()
314{
315 for (int ct=0; ct < _RS_TIMER_TOTAL; ct++) {
316 mTimers[ct] = 0;
317 }
318}
319
320void Context::timerInit()
321{
322 mTimeLast = getTime();
Jason Sams1d54f102009-09-03 15:43:13 -0700323 mTimeFrame = mTimeLast;
324 mTimeLastFrame = mTimeLast;
Jason Sams24371d92009-08-19 12:17:14 -0700325 mTimerActive = RS_TIMER_INTERNAL;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700326 mAverageFPSFrameCount = 0;
327 mAverageFPSStartTime = mTimeLast;
328 mAverageFPS = 0;
Jason Sams24371d92009-08-19 12:17:14 -0700329 timerReset();
330}
331
Jason Sams1d54f102009-09-03 15:43:13 -0700332void Context::timerFrame()
333{
334 mTimeLastFrame = mTimeFrame;
335 mTimeFrame = getTime();
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700336 // Update average fps
337 const uint64_t averageFramerateInterval = 1000 * 1000000;
338 mAverageFPSFrameCount ++;
339 uint64_t inverval = mTimeFrame - mAverageFPSStartTime;
340 if(inverval >= averageFramerateInterval) {
341 inverval = inverval / 1000000;
342 mAverageFPS = (mAverageFPSFrameCount * 1000) / inverval;
343 mAverageFPSFrameCount = 0;
344 mAverageFPSStartTime = mTimeFrame;
345 }
Jason Sams1d54f102009-09-03 15:43:13 -0700346}
347
Jason Sams24371d92009-08-19 12:17:14 -0700348void Context::timerSet(Timers tm)
349{
350 uint64_t last = mTimeLast;
351 mTimeLast = getTime();
352 mTimers[mTimerActive] += mTimeLast - last;
353 mTimerActive = tm;
354}
355
356void Context::timerPrint()
357{
358 double total = 0;
359 for (int ct = 0; ct < _RS_TIMER_TOTAL; ct++) {
360 total += mTimers[ct];
361 }
Jason Sams1d54f102009-09-03 15:43:13 -0700362 uint64_t frame = mTimeFrame - mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800363 mTimeMSLastFrame = frame / 1000000;
364 mTimeMSLastScript = mTimers[RS_TIMER_SCRIPT] / 1000000;
365 mTimeMSLastSwap = mTimers[RS_TIMER_CLEAR_SWAP] / 1000000;
Jason Sams24371d92009-08-19 12:17:14 -0700366
Jason Sams2dca84d2009-12-09 11:05:45 -0800367
368 if (props.mLogTimes) {
Alex Sakhartchouk64cd98e2010-10-18 17:18:50 -0700369 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 -0800370 mTimeMSLastFrame,
371 100.0 * mTimers[RS_TIMER_SCRIPT] / total, mTimeMSLastScript,
372 100.0 * mTimers[RS_TIMER_CLEAR_SWAP] / total, mTimeMSLastSwap,
373 100.0 * mTimers[RS_TIMER_IDLE] / total, mTimers[RS_TIMER_IDLE] / 1000000,
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700374 100.0 * mTimers[RS_TIMER_INTERNAL] / total, mTimers[RS_TIMER_INTERNAL] / 1000000,
375 mAverageFPS);
Jason Sams2dca84d2009-12-09 11:05:45 -0800376 }
Jason Sams24371d92009-08-19 12:17:14 -0700377}
378
Jason Samsa2cf7552010-03-03 13:03:18 -0800379bool Context::setupCheck()
Jason Sams326e0dd2009-05-22 14:03:28 -0700380{
Jason Sams900f1612010-09-16 18:18:29 -0700381 if (!mShaderCache.lookup(this, mVertex.get(), mFragment.get())) {
382 LOGE("Context::setupCheck() 1 fail");
383 return false;
Jason Samsc460e552009-11-25 13:22:07 -0800384 }
Jason Sams900f1612010-09-16 18:18:29 -0700385
386 mFragmentStore->setupGL2(this, &mStateFragmentStore);
387 mFragment->setupGL2(this, &mStateFragment, &mShaderCache);
388 mRaster->setupGL2(this, &mStateRaster);
389 mVertex->setupGL2(this, &mStateVertex, &mShaderCache);
Jason Samsa2cf7552010-03-03 13:03:18 -0800390 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700391}
392
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700393void Context::setupProgramStore() {
394 mFragmentStore->setupGL2(this, &mStateFragmentStore);
395}
396
Jason Sams1fddd902009-09-25 15:25:00 -0700397static bool getProp(const char *str)
Joe Onorato76371ff2009-09-23 16:37:36 -0700398{
399 char buf[PROPERTY_VALUE_MAX];
Jason Sams1fddd902009-09-25 15:25:00 -0700400 property_get(str, buf, "0");
Joe Onorato76371ff2009-09-23 16:37:36 -0700401 return 0 != strcmp(buf, "0");
402}
Jason Sams326e0dd2009-05-22 14:03:28 -0700403
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700404void Context::displayDebugStats()
405{
406 char buffer[128];
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700407 sprintf(buffer, "Avg fps %u, Frame %i ms, Script %i ms", mAverageFPS, mTimeMSLastFrame, mTimeMSLastScript);
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700408 float oldR, oldG, oldB, oldA;
409 mStateFont.getFontColor(&oldR, &oldG, &oldB, &oldA);
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700410 uint32_t bufferLen = strlen(buffer);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700411
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700412 float shadowCol = 0.1f;
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700413 mStateFont.setFontColor(shadowCol, shadowCol, shadowCol, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700414 mStateFont.renderText(buffer, bufferLen, 5, getHeight() - 6);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700415
Alex Sakhartchouk09c67352010-10-05 11:33:27 -0700416 mStateFont.setFontColor(1.0f, 0.7f, 0.0f, 1.0f);
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700417 mStateFont.renderText(buffer, bufferLen, 4, getHeight() - 7);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700418
Alex Sakhartchoukca5a4542010-08-05 11:24:14 -0700419 mStateFont.setFontColor(oldR, oldG, oldB, oldA);
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700420}
421
Jason Sams326e0dd2009-05-22 14:03:28 -0700422void * Context::threadProc(void *vrsc)
423{
424 Context *rsc = static_cast<Context *>(vrsc);
Jason Sams15832442009-11-15 12:14:26 -0800425 rsc->mNativeThreadId = gettid();
426
427 setpriority(PRIO_PROCESS, rsc->mNativeThreadId, ANDROID_PRIORITY_DISPLAY);
Jason Sams2dca84d2009-12-09 11:05:45 -0800428 rsc->mThreadPriority = ANDROID_PRIORITY_DISPLAY;
Jason Sams326e0dd2009-05-22 14:03:28 -0700429
Jason Sams1fddd902009-09-25 15:25:00 -0700430 rsc->props.mLogTimes = getProp("debug.rs.profile");
431 rsc->props.mLogScripts = getProp("debug.rs.script");
Jason Sams433eca32010-01-06 11:57:52 -0800432 rsc->props.mLogObjects = getProp("debug.rs.object");
433 rsc->props.mLogShaders = getProp("debug.rs.shader");
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700434 rsc->props.mLogShadersAttr = getProp("debug.rs.shader.attributes");
435 rsc->props.mLogShadersUniforms = getProp("debug.rs.shader.uniforms");
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700436 rsc->props.mLogVisual = getProp("debug.rs.visual");
Joe Onorato76371ff2009-09-23 16:37:36 -0700437
Jason Sams605048a2010-09-30 18:15:52 -0700438 rsc->mTlsStruct = new ScriptTLSStruct;
439 if (!rsc->mTlsStruct) {
Jason Samse5769102009-06-19 16:03:18 -0700440 LOGE("Error allocating tls storage");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700441 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed allocation for TLS");
Jason Samse5769102009-06-19 16:03:18 -0700442 return NULL;
443 }
Jason Sams605048a2010-09-30 18:15:52 -0700444 rsc->mTlsStruct->mContext = rsc;
445 rsc->mTlsStruct->mScript = NULL;
446 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
Jason Samse5769102009-06-19 16:03:18 -0700447 if (status) {
448 LOGE("pthread_setspecific %i", status);
449 }
450
Jason Sams5c1c79a2010-11-03 14:27:11 -0700451 if (!rsc->initGLThread()) {
452 rsc->setError(RS_ERROR_OUT_OF_MEMORY, "Failed initializing GL");
453 return NULL;
454 }
Jason Sams6b8552a2010-10-13 15:31:10 -0700455
Stephen Hines01b7d292010-09-28 15:45:45 -0700456 rsc->mScriptC.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800457 if (rsc->mIsGraphicsContext) {
Jason Sams771565f2010-05-14 15:30:29 -0700458 rsc->mStateRaster.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800459 rsc->setRaster(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700460 rsc->mStateVertex.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800461 rsc->setVertex(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700462 rsc->mStateFragment.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800463 rsc->setFragment(NULL);
Jason Sams771565f2010-05-14 15:30:29 -0700464 rsc->mStateFragmentStore.init(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800465 rsc->setFragmentStore(NULL);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700466 rsc->mStateFont.init(rsc);
467 rsc->setFont(NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -0800468 rsc->mStateVertexArray.init(rsc);
469 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700470
Jason Sams326e0dd2009-05-22 14:03:28 -0700471 rsc->mRunning = true;
Jason Samsa44cb292009-06-04 17:58:03 -0700472 bool mDraw = true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700473 while (!rsc->mExit) {
Jason Samsfcd31922009-08-17 18:35:48 -0700474 mDraw |= rsc->mIO.playCoreCommands(rsc, !mDraw);
Jason Sams732f1c02009-06-18 16:58:42 -0700475 mDraw &= (rsc->mRootScript.get() != NULL);
Jason Sams458f2dc2009-11-03 13:58:36 -0800476 mDraw &= (rsc->mWndSurface != NULL);
Jason Sams326e0dd2009-05-22 14:03:28 -0700477
Jason Sams2dca84d2009-12-09 11:05:45 -0800478 uint32_t targetTime = 0;
Jason Sams4820e8b2010-02-09 16:05:07 -0800479 if (mDraw && rsc->mIsGraphicsContext) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800480 targetTime = rsc->runRootScript();
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700481
482 if(rsc->props.mLogVisual) {
483 rsc->displayDebugStats();
484 }
485
Jason Sams2dca84d2009-12-09 11:05:45 -0800486 mDraw = targetTime && !rsc->mPaused;
487 rsc->timerSet(RS_TIMER_CLEAR_SWAP);
Jason Samsafcb25c2009-08-25 11:34:49 -0700488 eglSwapBuffers(rsc->mEGL.mDisplay, rsc->mEGL.mSurface);
Jason Sams2dca84d2009-12-09 11:05:45 -0800489 rsc->timerFrame();
490 rsc->timerSet(RS_TIMER_INTERNAL);
491 rsc->timerPrint();
492 rsc->timerReset();
Jason Sams326e0dd2009-05-22 14:03:28 -0700493 }
Jason Sams177f8442010-10-29 10:19:21 -0700494 if (targetTime > 1) {
Jason Sams2dca84d2009-12-09 11:05:45 -0800495 int32_t t = (targetTime - (int32_t)(rsc->mTimeMSLastScript + rsc->mTimeMSLastSwap)) * 1000;
496 if (t > 0) {
497 usleep(t);
498 }
499 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700500 }
501
Jason Sams4c5f99e2010-09-14 14:59:03 -0700502 LOGV("%p, RS Thread exiting", rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800503 if (rsc->mIsGraphicsContext) {
504 rsc->mRaster.clear();
505 rsc->mFragment.clear();
506 rsc->mVertex.clear();
507 rsc->mFragmentStore.clear();
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700508 rsc->mFont.clear();
Jason Sams4820e8b2010-02-09 16:05:07 -0800509 rsc->mRootScript.clear();
510 rsc->mStateRaster.deinit(rsc);
511 rsc->mStateVertex.deinit(rsc);
512 rsc->mStateFragment.deinit(rsc);
513 rsc->mStateFragmentStore.deinit(rsc);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700514 rsc->mStateFont.deinit(rsc);
Jason Sams4820e8b2010-02-09 16:05:07 -0800515 }
Jason Samse514b452009-09-25 14:51:22 -0700516 ObjectBase::zeroAllUserRef(rsc);
Jason Samse514b452009-09-25 14:51:22 -0700517
Jason Sams4820e8b2010-02-09 16:05:07 -0800518 if (rsc->mIsGraphicsContext) {
519 pthread_mutex_lock(&gInitMutex);
520 rsc->deinitEGL();
521 pthread_mutex_unlock(&gInitMutex);
522 }
Jason Sams605048a2010-09-30 18:15:52 -0700523 delete rsc->mTlsStruct;
Jason Sams33b6e3b2009-10-27 14:44:31 -0700524
Jason Sams4c5f99e2010-09-14 14:59:03 -0700525 LOGV("%p, RS Thread exited", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700526 return NULL;
527}
528
Jason Sams7bf29dd2010-07-19 15:38:19 -0700529void * Context::helperThreadProc(void *vrsc)
530{
531 Context *rsc = static_cast<Context *>(vrsc);
532 uint32_t idx = (uint32_t)android_atomic_inc(&rsc->mWorkers.mLaunchCount);
533
Jason Sams18133402010-07-20 15:09:00 -0700534 LOGV("RS helperThread starting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700535
536 rsc->mWorkers.mLaunchSignals[idx].init();
537 rsc->mWorkers.mNativeThreadId[idx] = gettid();
538
Jason Sams8d957fa2010-09-28 14:41:22 -0700539#if 0
540 typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
541 cpu_set_t cpuset;
542 memset(&cpuset, 0, sizeof(cpuset));
543 cpuset.bits[idx / 64] |= 1ULL << (idx % 64);
544 int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx],
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700545 sizeof(cpuset), &cpuset);
Jason Sams8d957fa2010-09-28 14:41:22 -0700546 LOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
547#endif
Jason Sams7bf29dd2010-07-19 15:38:19 -0700548
549 setpriority(PRIO_PROCESS, rsc->mWorkers.mNativeThreadId[idx], rsc->mThreadPriority);
Jason Sams605048a2010-09-30 18:15:52 -0700550 int status = pthread_setspecific(rsc->gThreadTLSKey, rsc->mTlsStruct);
551 if (status) {
552 LOGE("pthread_setspecific %i", status);
553 }
554
Jason Sams7bf29dd2010-07-19 15:38:19 -0700555 while(rsc->mRunning) {
556 rsc->mWorkers.mLaunchSignals[idx].wait();
557 if (rsc->mWorkers.mLaunchCallback) {
Jason Sams7bf29dd2010-07-19 15:38:19 -0700558 rsc->mWorkers.mLaunchCallback(rsc->mWorkers.mLaunchData, idx);
559 }
Jason Sams7bf29dd2010-07-19 15:38:19 -0700560 android_atomic_dec(&rsc->mWorkers.mRunningCount);
561 rsc->mWorkers.mCompleteSignal.set();
562 }
Jason Sams18133402010-07-20 15:09:00 -0700563
564 LOGV("RS helperThread exiting %p idx=%i", rsc, idx);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700565 return NULL;
566}
567
568void Context::launchThreads(WorkerCallback_t cbk, void *data)
569{
570 mWorkers.mLaunchData = data;
571 mWorkers.mLaunchCallback = cbk;
572 mWorkers.mRunningCount = (int)mWorkers.mCount;
573 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
574 mWorkers.mLaunchSignals[ct].set();
575 }
576 while(mWorkers.mRunningCount) {
577 mWorkers.mCompleteSignal.wait();
578 }
579}
580
Jason Sams15832442009-11-15 12:14:26 -0800581void Context::setPriority(int32_t p)
582{
583 // Note: If we put this in the proper "background" policy
584 // the wallpapers can become completly unresponsive at times.
585 // This is probably not what we want for something the user is actively
586 // looking at.
Jason Sams2dca84d2009-12-09 11:05:45 -0800587 mThreadPriority = p;
Jason Sams15832442009-11-15 12:14:26 -0800588#if 0
589 SchedPolicy pol = SP_FOREGROUND;
590 if (p > 0) {
591 pol = SP_BACKGROUND;
592 }
593 if (!set_sched_policy(mNativeThreadId, pol)) {
594 // success; reset the priority as well
595 }
596#else
Jason Sams7bf29dd2010-07-19 15:38:19 -0700597 setpriority(PRIO_PROCESS, mNativeThreadId, p);
598 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
599 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], p);
600 }
Jason Sams15832442009-11-15 12:14:26 -0800601#endif
602}
603
Jason Sams5c1c79a2010-11-03 14:27:11 -0700604Context::Context()
Jason Sams326e0dd2009-05-22 14:03:28 -0700605{
Jason Sams5c1c79a2010-11-03 14:27:11 -0700606 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700607 mRunning = false;
608 mExit = false;
Jason Sams86f1b232009-09-24 17:38:20 -0700609 mPaused = false;
Jason Samse514b452009-09-25 14:51:22 -0700610 mObjHead = NULL;
Jason Samsa2cf7552010-03-03 13:03:18 -0800611 mError = RS_ERROR_NONE;
612 mErrorMsg = NULL;
Jason Sams5c1c79a2010-11-03 14:27:11 -0700613}
614
615Context * Context::createContext(Device *dev, const RsSurfaceConfig *sc)
616{
617 Context * rsc = new Context();
618 if (!rsc->initContext(dev, sc)) {
619 delete rsc;
620 return NULL;
621 }
622 return rsc;
623}
624
625bool Context::initContext(Device *dev, const RsSurfaceConfig *sc)
626{
627 pthread_mutex_lock(&gInitMutex);
628
629 dev->addContext(this);
630 mDev = dev;
Jason Sams6b8552a2010-10-13 15:31:10 -0700631 if (sc) {
632 mUserSurfaceConfig = *sc;
633 } else {
634 memset(&mUserSurfaceConfig, 0, sizeof(mUserSurfaceConfig));
635 }
Jason Samsa2cf7552010-03-03 13:03:18 -0800636
Jason Sams613cad12009-11-12 15:10:25 -0800637 memset(&mEGL, 0, sizeof(mEGL));
Jason Sams4820e8b2010-02-09 16:05:07 -0800638 memset(&mGL, 0, sizeof(mGL));
Jason Sams6b8552a2010-10-13 15:31:10 -0700639 mIsGraphicsContext = sc != NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700640
Jason Samsa658e902009-06-04 14:35:01 -0700641 int status;
642 pthread_attr_t threadAttr;
643
Jason Samsfb03a222009-10-15 16:47:31 -0700644 if (!gThreadTLSKeyCount) {
Jason Sams9e4e13d2009-10-06 17:16:55 -0700645 status = pthread_key_create(&gThreadTLSKey, NULL);
646 if (status) {
647 LOGE("Failed to init thread tls key.");
Jason Samsfb03a222009-10-15 16:47:31 -0700648 pthread_mutex_unlock(&gInitMutex);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700649 return false;
Jason Sams9e4e13d2009-10-06 17:16:55 -0700650 }
Jason Samse5769102009-06-19 16:03:18 -0700651 }
Jason Samsfb03a222009-10-15 16:47:31 -0700652 gThreadTLSKeyCount++;
Jason Sams6b8552a2010-10-13 15:31:10 -0700653
Jason Samsfb03a222009-10-15 16:47:31 -0700654 pthread_mutex_unlock(&gInitMutex);
655
656 // Global init done at this point.
Jason Samse5769102009-06-19 16:03:18 -0700657
Jason Samsa658e902009-06-04 14:35:01 -0700658 status = pthread_attr_init(&threadAttr);
659 if (status) {
660 LOGE("Failed to init thread attribute.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700661 return false;
Jason Samsa658e902009-06-04 14:35:01 -0700662 }
663
Jason Sams613cad12009-11-12 15:10:25 -0800664 mWndSurface = NULL;
Jason Sams992a0b72009-06-23 12:22:47 -0700665
Jason Sams24371d92009-08-19 12:17:14 -0700666 timerInit();
Jason Samsa8919332009-09-24 15:42:52 -0700667 timerSet(RS_TIMER_INTERNAL);
Jason Sams50869382009-08-18 17:07:09 -0700668
Jason Sams7c7c78a2010-07-26 17:12:55 -0700669 int cpu = sysconf(_SC_NPROCESSORS_ONLN);
670 LOGV("RS Launching thread(s), reported CPU count %i", cpu);
671 if (cpu < 2) cpu = 0;
672
673 mWorkers.mCount = (uint32_t)cpu;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700674 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
675 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
676 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
677 mWorkers.mLaunchCallback = NULL;
Jason Samsa658e902009-06-04 14:35:01 -0700678 status = pthread_create(&mThreadId, &threadAttr, threadProc, this);
Jason Sams326e0dd2009-05-22 14:03:28 -0700679 if (status) {
680 LOGE("Failed to start rs context thread.");
Jason Sams5c1c79a2010-11-03 14:27:11 -0700681 return false;
Jason Sams7bf29dd2010-07-19 15:38:19 -0700682 }
Jason Sams5c1c79a2010-11-03 14:27:11 -0700683 while(!mRunning && (mError == RS_ERROR_NONE)) {
Jason Sams18133402010-07-20 15:09:00 -0700684 usleep(100);
685 }
686
Jason Sams5c1c79a2010-11-03 14:27:11 -0700687 if (mError != RS_ERROR_NONE) {
688 return false;
689 }
690
Jason Sams8d957fa2010-09-28 14:41:22 -0700691 mWorkers.mCompleteSignal.init();
Jason Sams7bf29dd2010-07-19 15:38:19 -0700692 mWorkers.mRunningCount = 0;
693 mWorkers.mLaunchCount = 0;
694 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
695 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
696 if (status) {
697 mWorkers.mCount = ct;
698 LOGE("Created fewer than expected number of RS threads.");
699 break;
700 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700701 }
Jason Samsa658e902009-06-04 14:35:01 -0700702 pthread_attr_destroy(&threadAttr);
Jason Sams5c1c79a2010-11-03 14:27:11 -0700703 return true;
Jason Sams326e0dd2009-05-22 14:03:28 -0700704}
705
706Context::~Context()
707{
Jason Sams8c0ee652009-08-25 14:49:07 -0700708 LOGV("Context::~Context");
Jason Sams326e0dd2009-05-22 14:03:28 -0700709 mExit = true;
Jason Sams86f1b232009-09-24 17:38:20 -0700710 mPaused = false;
Jason Sams326e0dd2009-05-22 14:03:28 -0700711 void *res;
712
Jason Sams8c0ee652009-08-25 14:49:07 -0700713 mIO.shutdown();
Jason Sams326e0dd2009-05-22 14:03:28 -0700714 int status = pthread_join(mThreadId, &res);
Jason Sams326e0dd2009-05-22 14:03:28 -0700715
Jason Samsfb03a222009-10-15 16:47:31 -0700716 // Global structure cleanup.
717 pthread_mutex_lock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700718 if (mDev) {
719 mDev->removeContext(this);
Jason Samsfb03a222009-10-15 16:47:31 -0700720 --gThreadTLSKeyCount;
721 if (!gThreadTLSKeyCount) {
722 pthread_key_delete(gThreadTLSKey);
723 }
Jason Samsbf3c14e2009-11-02 14:25:10 -0800724 mDev = NULL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700725 }
Jason Samsfb03a222009-10-15 16:47:31 -0700726 pthread_mutex_unlock(&gInitMutex);
Jason Sams326e0dd2009-05-22 14:03:28 -0700727}
728
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700729void Context::setSurface(uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -0800730{
Jason Sams4820e8b2010-02-09 16:05:07 -0800731 rsAssert(mIsGraphicsContext);
Jason Sams613cad12009-11-12 15:10:25 -0800732
Jason Sams458f2dc2009-11-03 13:58:36 -0800733 EGLBoolean ret;
734 if (mEGL.mSurface != NULL) {
Jason Sams6b8552a2010-10-13 15:31:10 -0700735 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurfaceDefault, mEGL.mSurfaceDefault, mEGL.mContext);
Jason Sams458f2dc2009-11-03 13:58:36 -0800736 checkEglError("eglMakeCurrent", ret);
737
738 ret = eglDestroySurface(mEGL.mDisplay, mEGL.mSurface);
739 checkEglError("eglDestroySurface", ret);
740
741 mEGL.mSurface = NULL;
Jason Sams6b8552a2010-10-13 15:31:10 -0700742 mWidth = 1;
743 mHeight = 1;
Jason Sams458f2dc2009-11-03 13:58:36 -0800744 }
745
746 mWndSurface = sur;
747 if (mWndSurface != NULL) {
Jason Sams771565f2010-05-14 15:30:29 -0700748 mWidth = w;
749 mHeight = h;
Jason Sams613cad12009-11-12 15:10:25 -0800750
Jason Sams458f2dc2009-11-03 13:58:36 -0800751 mEGL.mSurface = eglCreateWindowSurface(mEGL.mDisplay, mEGL.mConfig, mWndSurface, NULL);
752 checkEglError("eglCreateWindowSurface");
753 if (mEGL.mSurface == EGL_NO_SURFACE) {
754 LOGE("eglCreateWindowSurface returned EGL_NO_SURFACE");
755 }
756
757 ret = eglMakeCurrent(mEGL.mDisplay, mEGL.mSurface, mEGL.mSurface, mEGL.mContext);
758 checkEglError("eglMakeCurrent", ret);
Jason Sams613cad12009-11-12 15:10:25 -0800759
Jason Sams771565f2010-05-14 15:30:29 -0700760 mStateVertex.updateSize(this);
Jason Sams458f2dc2009-11-03 13:58:36 -0800761 }
762}
763
Jason Sams86f1b232009-09-24 17:38:20 -0700764void Context::pause()
765{
Jason Sams4820e8b2010-02-09 16:05:07 -0800766 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700767 mPaused = true;
768}
769
770void Context::resume()
771{
Jason Sams4820e8b2010-02-09 16:05:07 -0800772 rsAssert(mIsGraphicsContext);
Jason Sams86f1b232009-09-24 17:38:20 -0700773 mPaused = false;
774}
775
Jason Sams326e0dd2009-05-22 14:03:28 -0700776void Context::setRootScript(Script *s)
777{
Jason Sams4820e8b2010-02-09 16:05:07 -0800778 rsAssert(mIsGraphicsContext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700779 mRootScript.set(s);
780}
781
Jason Samsccc010b2010-05-13 18:30:11 -0700782void Context::setFragmentStore(ProgramStore *pfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700783{
Jason Sams4820e8b2010-02-09 16:05:07 -0800784 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700785 if (pfs == NULL) {
786 mFragmentStore.set(mStateFragmentStore.mDefault);
787 } else {
788 mFragmentStore.set(pfs);
789 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700790}
791
792void Context::setFragment(ProgramFragment *pf)
793{
Jason Sams4820e8b2010-02-09 16:05:07 -0800794 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700795 if (pf == NULL) {
796 mFragment.set(mStateFragment.mDefault);
797 } else {
798 mFragment.set(pf);
799 }
Jason Samscfb1d112009-08-05 13:57:03 -0700800}
801
Jason Sams5fd09d82009-09-23 13:57:02 -0700802void Context::setRaster(ProgramRaster *pr)
803{
Jason Sams4820e8b2010-02-09 16:05:07 -0800804 rsAssert(mIsGraphicsContext);
Jason Sams5fd09d82009-09-23 13:57:02 -0700805 if (pr == NULL) {
806 mRaster.set(mStateRaster.mDefault);
807 } else {
808 mRaster.set(pr);
809 }
810}
811
Jason Sams326e0dd2009-05-22 14:03:28 -0700812void Context::setVertex(ProgramVertex *pv)
813{
Jason Sams4820e8b2010-02-09 16:05:07 -0800814 rsAssert(mIsGraphicsContext);
Jason Sams8ce125b2009-06-17 16:52:59 -0700815 if (pv == NULL) {
816 mVertex.set(mStateVertex.mDefault);
817 } else {
818 mVertex.set(pv);
819 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700820}
821
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700822void Context::setFont(Font *f)
823{
824 rsAssert(mIsGraphicsContext);
825 if (f == NULL) {
826 mFont.set(mStateFont.mDefault);
827 } else {
828 mFont.set(f);
829 }
830}
831
Jason Samsa4a54e42009-06-10 18:39:40 -0700832void Context::assignName(ObjectBase *obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700833{
834 rsAssert(!obj->getName());
Jason Samsa4a54e42009-06-10 18:39:40 -0700835 obj->setName(name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700836 mNames.add(obj);
837}
838
839void Context::removeName(ObjectBase *obj)
840{
841 for(size_t ct=0; ct < mNames.size(); ct++) {
842 if (obj == mNames[ct]) {
843 mNames.removeAt(ct);
844 return;
845 }
846 }
847}
848
Jason Sams8c401ef2009-10-06 13:58:47 -0700849uint32_t Context::getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait)
850{
851 //LOGE("getMessageToClient %i %i", bufferLen, wait);
Jason Sams39cd3172010-08-25 14:31:48 -0700852 *receiveLen = 0;
Jason Sams8c401ef2009-10-06 13:58:47 -0700853 if (!wait) {
854 if (mIO.mToClient.isEmpty()) {
855 // No message to get and not going to wait for one.
Jason Sams8c401ef2009-10-06 13:58:47 -0700856 return 0;
857 }
858 }
859
860 //LOGE("getMessageToClient 2 con=%p", this);
861 uint32_t bytesData = 0;
862 uint32_t commandID = 0;
863 const void *d = mIO.mToClient.get(&commandID, &bytesData);
864 //LOGE("getMessageToClient 3 %i %i", commandID, bytesData);
865
866 *receiveLen = bytesData;
867 if (bufferLen >= bytesData) {
868 memcpy(data, d, bytesData);
869 mIO.mToClient.next();
870 return commandID;
871 }
872 return 0;
873}
874
875bool Context::sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace)
876{
877 //LOGE("sendMessageToClient %i %i %i", cmdID, len, waitForSpace);
878 if (cmdID == 0) {
879 LOGE("Attempting to send invalid command 0 to client.");
880 return false;
881 }
882 if (!waitForSpace) {
Jason Sams8c46b102010-08-18 12:38:03 -0700883 if (!mIO.mToClient.makeSpaceNonBlocking(len + 8)) {
Jason Sams8c401ef2009-10-06 13:58:47 -0700884 // Not enough room, and not waiting.
885 return false;
886 }
887 }
888 //LOGE("sendMessageToClient 2");
Jason Samsef5867a2010-07-28 11:17:53 -0700889 if (len > 0) {
890 void *p = mIO.mToClient.reserve(len);
891 memcpy(p, data, len);
892 mIO.mToClient.commit(cmdID, len);
893 } else {
894 mIO.mToClient.commit(cmdID, 0);
895 }
Jason Sams8c401ef2009-10-06 13:58:47 -0700896 //LOGE("sendMessageToClient 3");
897 return true;
898}
899
900void Context::initToClient()
901{
902 while(!mRunning) {
903 usleep(100);
904 }
905}
906
907void Context::deinitToClient()
908{
909 mIO.mToClient.shutdown();
910}
Jason Sams50869382009-08-18 17:07:09 -0700911
Jason Samsa2cf7552010-03-03 13:03:18 -0800912const char * Context::getError(RsError *err)
913{
914 *err = mError;
915 mError = RS_ERROR_NONE;
916 if (*err != RS_ERROR_NONE) {
917 return mErrorMsg;
918 }
919 return NULL;
920}
921
922void Context::setError(RsError e, const char *msg)
923{
924 mError = e;
925 mErrorMsg = msg;
926}
927
928
Jason Sams13e26342009-11-24 12:26:35 -0800929void Context::dumpDebug() const
930{
931 LOGE("RS Context debug %p", this);
932 LOGE("RS Context debug");
933
934 LOGE(" EGL ver %i %i", mEGL.mMajorVersion, mEGL.mMinorVersion);
Jason Sams22fa3712010-05-19 17:22:57 -0700935 LOGE(" EGL context %p surface %p, Display=%p", mEGL.mContext, mEGL.mSurface, mEGL.mDisplay);
Jason Sams13e26342009-11-24 12:26:35 -0800936 LOGE(" GL vendor: %s", mGL.mVendor);
937 LOGE(" GL renderer: %s", mGL.mRenderer);
938 LOGE(" GL Version: %s", mGL.mVersion);
939 LOGE(" GL Extensions: %s", mGL.mExtensions);
940 LOGE(" GL int Versions %i %i", mGL.mMajorVersion, mGL.mMinorVersion);
941 LOGE(" RS width %i, height %i", mWidth, mHeight);
Jason Sams6b8552a2010-10-13 15:31:10 -0700942 LOGE(" RS running %i, exit %i, paused %i", mRunning, mExit, mPaused);
Jason Sams13e26342009-11-24 12:26:35 -0800943 LOGE(" RS pThreadID %li, nativeThreadID %i", mThreadId, mNativeThreadId);
944
Jason Sams4815c0d2009-12-15 12:58:36 -0800945 LOGV("MAX Textures %i, %i %i", mGL.mMaxVertexTextureUnits, mGL.mMaxFragmentTextureImageUnits, mGL.mMaxTextureImageUnits);
946 LOGV("MAX Attribs %i", mGL.mMaxVertexAttribs);
947 LOGV("MAX Uniforms %i, %i", mGL.mMaxVertexUniformVectors, mGL.mMaxFragmentUniformVectors);
948 LOGV("MAX Varyings %i", mGL.mMaxVaryingVectors);
Jason Sams13e26342009-11-24 12:26:35 -0800949}
Jason Samsa4a54e42009-06-10 18:39:40 -0700950
Jason Sams326e0dd2009-05-22 14:03:28 -0700951///////////////////////////////////////////////////////////////////////////////////////////
Jason Samsa44cb292009-06-04 17:58:03 -0700952//
Jason Sams326e0dd2009-05-22 14:03:28 -0700953
954namespace android {
955namespace renderscript {
956
Jason Sams8c880902010-06-15 12:15:57 -0700957void rsi_ContextFinish(Context *rsc)
958{
959}
Jason Sams326e0dd2009-05-22 14:03:28 -0700960
961void rsi_ContextBindRootScript(Context *rsc, RsScript vs)
962{
963 Script *s = static_cast<Script *>(vs);
964 rsc->setRootScript(s);
965}
966
967void rsi_ContextBindSampler(Context *rsc, uint32_t slot, RsSampler vs)
968{
969 Sampler *s = static_cast<Sampler *>(vs);
970
971 if (slot > RS_MAX_SAMPLER_SLOT) {
972 LOGE("Invalid sampler slot");
973 return;
974 }
975
976 s->bindToContext(&rsc->mStateSampler, slot);
977}
978
Jason Samsccc010b2010-05-13 18:30:11 -0700979void rsi_ContextBindProgramStore(Context *rsc, RsProgramStore vpfs)
Jason Sams326e0dd2009-05-22 14:03:28 -0700980{
Jason Samsccc010b2010-05-13 18:30:11 -0700981 ProgramStore *pfs = static_cast<ProgramStore *>(vpfs);
Jason Sams326e0dd2009-05-22 14:03:28 -0700982 rsc->setFragmentStore(pfs);
983}
984
985void rsi_ContextBindProgramFragment(Context *rsc, RsProgramFragment vpf)
986{
987 ProgramFragment *pf = static_cast<ProgramFragment *>(vpf);
988 rsc->setFragment(pf);
989}
990
Jason Sams5fd09d82009-09-23 13:57:02 -0700991void rsi_ContextBindProgramRaster(Context *rsc, RsProgramRaster vpr)
992{
993 ProgramRaster *pr = static_cast<ProgramRaster *>(vpr);
994 rsc->setRaster(pr);
995}
996
Jason Sams326e0dd2009-05-22 14:03:28 -0700997void rsi_ContextBindProgramVertex(Context *rsc, RsProgramVertex vpv)
998{
999 ProgramVertex *pv = static_cast<ProgramVertex *>(vpv);
1000 rsc->setVertex(pv);
1001}
1002
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -07001003void rsi_ContextBindFont(Context *rsc, RsFont vfont)
1004{
1005 Font *font = static_cast<Font *>(vfont);
1006 rsc->setFont(font);
1007}
1008
Jason Samsa4a54e42009-06-10 18:39:40 -07001009void rsi_AssignName(Context *rsc, void * obj, const char *name, uint32_t len)
Jason Samsa0a1b6f2009-06-10 15:04:38 -07001010{
1011 ObjectBase *ob = static_cast<ObjectBase *>(obj);
Jason Samsa4a54e42009-06-10 18:39:40 -07001012 rsc->assignName(ob, name, len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -07001013}
Jason Sams326e0dd2009-05-22 14:03:28 -07001014
Jason Sams2353ae32010-10-14 17:48:46 -07001015void rsi_ObjDestroy(Context *rsc, void *optr)
Jason Sams707aaf32009-08-18 14:14:24 -07001016{
Jason Sams2353ae32010-10-14 17:48:46 -07001017 ObjectBase *ob = static_cast<ObjectBase *>(optr);
Jason Sams707aaf32009-08-18 14:14:24 -07001018 rsc->removeName(ob);
Jason Sams9397e302009-08-27 20:23:34 -07001019 ob->decUserRef();
Jason Sams707aaf32009-08-18 14:14:24 -07001020}
1021
Jason Sams86f1b232009-09-24 17:38:20 -07001022void rsi_ContextPause(Context *rsc)
1023{
1024 rsc->pause();
1025}
1026
1027void rsi_ContextResume(Context *rsc)
1028{
1029 rsc->resume();
1030}
1031
Dianne Hackborn1c769c32010-06-30 13:56:17 -07001032void rsi_ContextSetSurface(Context *rsc, uint32_t w, uint32_t h, ANativeWindow *sur)
Jason Sams458f2dc2009-11-03 13:58:36 -08001033{
Mathias Agopianfa402862010-02-12 14:04:35 -08001034 rsc->setSurface(w, h, sur);
Jason Sams613cad12009-11-12 15:10:25 -08001035}
1036
Jason Sams15832442009-11-15 12:14:26 -08001037void rsi_ContextSetPriority(Context *rsc, int32_t p)
Jason Sams613cad12009-11-12 15:10:25 -08001038{
Jason Sams15832442009-11-15 12:14:26 -08001039 rsc->setPriority(p);
Jason Sams458f2dc2009-11-03 13:58:36 -08001040}
1041
Jason Samsc21cf402009-11-17 17:26:46 -08001042void rsi_ContextDump(Context *rsc, int32_t bits)
1043{
1044 ObjectBase::dumpAll(rsc);
1045}
1046
Jason Samsa2cf7552010-03-03 13:03:18 -08001047const char * rsi_ContextGetError(Context *rsc, RsError *e)
1048{
1049 const char *msg = rsc->getError(e);
1050 if (*e != RS_ERROR_NONE) {
1051 LOGE("RS Error %i %s", *e, msg);
1052 }
1053 return msg;
1054}
1055
Jason Sams326e0dd2009-05-22 14:03:28 -07001056}
1057}
1058
1059
Jason Sams4820e8b2010-02-09 16:05:07 -08001060RsContext rsContextCreate(RsDevice vdev, uint32_t version)
Jason Sams326e0dd2009-05-22 14:03:28 -07001061{
Jason Sams4820e8b2010-02-09 16:05:07 -08001062 LOGV("rsContextCreate %p", vdev);
Jason Sams326e0dd2009-05-22 14:03:28 -07001063 Device * dev = static_cast<Device *>(vdev);
Jason Sams5c1c79a2010-11-03 14:27:11 -07001064 Context *rsc = Context::createContext(dev, NULL);
Jason Sams4820e8b2010-02-09 16:05:07 -08001065 return rsc;
1066}
1067
Jason Sams6b8552a2010-10-13 15:31:10 -07001068RsContext rsContextCreateGL(RsDevice vdev, uint32_t version, RsSurfaceConfig sc)
Jason Sams4820e8b2010-02-09 16:05:07 -08001069{
Jason Sams6b8552a2010-10-13 15:31:10 -07001070 LOGV("rsContextCreateGL %p", vdev);
Jason Sams4820e8b2010-02-09 16:05:07 -08001071 Device * dev = static_cast<Device *>(vdev);
Jason Sams5c1c79a2010-11-03 14:27:11 -07001072 Context *rsc = Context::createContext(dev, &sc);
Jason Sams900f1612010-09-16 18:18:29 -07001073 LOGV("rsContextCreateGL ret %p ", rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -07001074 return rsc;
1075}
1076
1077void rsContextDestroy(RsContext vrsc)
1078{
1079 Context * rsc = static_cast<Context *>(vrsc);
1080 delete rsc;
1081}
1082
Jason Sams8c401ef2009-10-06 13:58:47 -07001083uint32_t rsContextGetMessage(RsContext vrsc, void *data, size_t *receiveLen, size_t bufferLen, bool wait)
1084{
1085 Context * rsc = static_cast<Context *>(vrsc);
1086 return rsc->getMessageToClient(data, receiveLen, bufferLen, wait);
1087}
1088
1089void rsContextInitToClient(RsContext vrsc)
1090{
1091 Context * rsc = static_cast<Context *>(vrsc);
1092 rsc->initToClient();
1093}
1094
1095void rsContextDeinitToClient(RsContext vrsc)
1096{
1097 Context * rsc = static_cast<Context *>(vrsc);
1098 rsc->deinitToClient();
1099}
1100
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -07001101// Only to be called at a3d load time, before object is visible to user
1102// not thread safe
1103void rsaGetName(RsContext con, void * obj, const char **name)
1104{
1105 ObjectBase *ob = static_cast<ObjectBase *>(obj);
1106 (*name) = ob->getName();
1107}