blob: df275bcab3f405a91f333bd0df513a6ba5663e5f [file] [log] [blame]
Jason Samsd19f10d2009-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#ifndef ANDROID_RS_CONTEXT_H
18#define ANDROID_RS_CONTEXT_H
19
Jason Sams399bfce2009-07-13 12:20:31 -070020#include "rsUtils.h"
Jason Samsc1d726c2010-03-18 11:39:44 -070021#include "rsMutex.h"
Jason Sams399bfce2009-07-13 12:20:31 -070022
Jason Samsbc948de2009-08-17 18:35:48 -070023#include "rsThreadIO.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070024#include "rsType.h"
25#include "rsMatrix.h"
26#include "rsAllocation.h"
Jason Sams7c878f32009-06-30 14:13:04 -070027#include "rsMesh.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070028#include "rsDevice.h"
29#include "rsScriptC.h"
30#include "rsAllocation.h"
31#include "rsAdapter.h"
32#include "rsSampler.h"
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070033#include "rsFont.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070034#include "rsProgramFragment.h"
Jason Sams54db59c2010-05-13 18:30:11 -070035#include "rsProgramStore.h"
Jason Samsebfb4362009-09-23 13:57:02 -070036#include "rsProgramRaster.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070037#include "rsProgramVertex.h"
Jason Samsbb51c402009-11-25 13:22:07 -080038#include "rsShaderCache.h"
39#include "rsVertexArray.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070040
41#include "rsgApiStructs.h"
42#include "rsLocklessFifo.h"
43
Mathias Agopian5cbb9402010-02-12 12:00:38 -080044#include <ui/egl/android_natives.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070045
46// ---------------------------------------------------------------------------
47namespace android {
Mathias Agopian000479f2010-02-09 17:46:37 -080048
Jason Samsd19f10d2009-05-22 14:03:28 -070049namespace renderscript {
50
Jason Samsf166d9b2010-09-30 18:15:52 -070051#if 0
52#define CHECK_OBJ(o) { \
53 GET_TLS(); \
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080054 if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
Jason Samsf166d9b2010-09-30 18:15:52 -070055 LOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \
56 } \
57}
58#define CHECK_OBJ_OR_NULL(o) { \
59 GET_TLS(); \
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080060 if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
Jason Samsf166d9b2010-09-30 18:15:52 -070061 LOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \
62 } \
63}
64#else
65#define CHECK_OBJ(o)
66#define CHECK_OBJ_OR_NULL(o)
67#endif
68
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080069class Context {
Jason Samsd19f10d2009-05-22 14:03:28 -070070public:
Jason Samsd5f06302010-11-03 14:27:11 -070071 static Context * createContext(Device *, const RsSurfaceConfig *sc);
Jason Samsd19f10d2009-05-22 14:03:28 -070072 ~Context();
73
Jason Sams462d11b2009-06-19 16:03:18 -070074 static pthread_key_t gThreadTLSKey;
Jason Sams41c19db92009-10-15 16:47:31 -070075 static uint32_t gThreadTLSKeyCount;
Jason Sams71362202009-10-27 14:44:31 -070076 static uint32_t gGLContextCount;
Jason Sams41c19db92009-10-15 16:47:31 -070077 static pthread_mutex_t gInitMutex;
Stephen Hines1ac9da62011-01-07 15:11:30 -080078 // Library mutex (for providing thread-safe calls from the runtime)
79 static pthread_mutex_t gLibMutex;
Jason Sams41c19db92009-10-15 16:47:31 -070080
Jason Sams462d11b2009-06-19 16:03:18 -070081 struct ScriptTLSStruct {
82 Context * mContext;
83 Script * mScript;
84 };
Jason Samsa17af042010-11-17 15:29:32 -080085
86 class PushState {
87 public:
88 PushState(Context *);
89 ~PushState();
90
91 private:
92 ObjectBaseRef<ProgramFragment> mFragment;
93 ObjectBaseRef<ProgramVertex> mVertex;
94 ObjectBaseRef<ProgramStore> mStore;
95 ObjectBaseRef<ProgramRaster> mRaster;
96 ObjectBaseRef<Font> mFont;
97 Context *mRsc;
98 };
99
Jason Samsf166d9b2010-09-30 18:15:52 -0700100 ScriptTLSStruct *mTlsStruct;
Jason Sams11c8af92010-10-13 15:31:10 -0700101 RsSurfaceConfig mUserSurfaceConfig;
Jason Sams462d11b2009-06-19 16:03:18 -0700102
Jason Sams8e6c17f2010-07-19 15:38:19 -0700103 typedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
Jason Samsd19f10d2009-05-22 14:03:28 -0700104
105 //StructuredAllocationContext mStateAllocation;
106 ElementState mStateElement;
107 TypeState mStateType;
108 SamplerState mStateSampler;
109 ProgramFragmentState mStateFragment;
Jason Sams54db59c2010-05-13 18:30:11 -0700110 ProgramStoreState mStateFragmentStore;
Jason Samsebfb4362009-09-23 13:57:02 -0700111 ProgramRasterState mStateRaster;
Jason Samsd19f10d2009-05-22 14:03:28 -0700112 ProgramVertexState mStateVertex;
Jason Samsbb51c402009-11-25 13:22:07 -0800113 VertexArrayState mStateVertexArray;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700114 FontState mStateFont;
Jason Samsd19f10d2009-05-22 14:03:28 -0700115
Jason Samsd19f10d2009-05-22 14:03:28 -0700116 ScriptCState mScriptC;
Jason Samsbb51c402009-11-25 13:22:07 -0800117 ShaderCache mShaderCache;
Jason Samsd19f10d2009-05-22 14:03:28 -0700118
Jason Samsd19f10d2009-05-22 14:03:28 -0700119 void swapBuffers();
120 void setRootScript(Script *);
Jason Samsa17af042010-11-17 15:29:32 -0800121 void setProgramRaster(ProgramRaster *);
122 void setProgramVertex(ProgramVertex *);
123 void setProgramFragment(ProgramFragment *);
124 void setProgramStore(ProgramStore *);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700125 void setFont(Font *);
Jason Samsd19f10d2009-05-22 14:03:28 -0700126
127 void updateSurface(void *sur);
128
Jason Samsa17af042010-11-17 15:29:32 -0800129 ProgramFragment * getProgramFragment() {return mFragment.get();}
130 ProgramStore * getProgramStore() {return mFragmentStore.get();}
131 ProgramRaster * getProgramRaster() {return mRaster.get();}
132 ProgramVertex * getProgramVertex() {return mVertex.get();}
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700133 Font * getFont() {return mFont.get();}
Jason Samsd19f10d2009-05-22 14:03:28 -0700134
Jason Sams156cce62010-03-03 13:03:18 -0800135 bool setupCheck();
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700136 void setupProgramStore();
Jason Samsd19f10d2009-05-22 14:03:28 -0700137
Jason Sams65e7aa52009-09-24 17:38:20 -0700138 void pause();
139 void resume();
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700140 void setSurface(uint32_t w, uint32_t h, ANativeWindow *sur);
Jason Sams7d787b42009-11-15 12:14:26 -0800141 void setPriority(int32_t p);
Jason Sams5c68a712010-12-24 14:38:39 -0800142 void destroyWorkerThreadResources();
Jason Sams65e7aa52009-09-24 17:38:20 -0700143
Jason Samsd5680f92009-06-10 18:39:40 -0700144 void assignName(ObjectBase *obj, const char *name, uint32_t len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700145 void removeName(ObjectBase *obj);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700146
Jason Sams1c415172010-11-08 17:06:46 -0800147 RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID, bool wait);
148 RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait);
Jason Samsadd9d962010-11-22 16:20:16 -0800149 bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const;
Jason Samsf17bccc2010-05-28 18:23:22 -0700150 uint32_t runScript(Script *s);
Jason Sams516c3192009-10-06 13:58:47 -0700151
152 void initToClient();
153 void deinitToClient();
154
Jason Sams9c54bdb2009-06-17 16:52:59 -0700155 ProgramFragment * getDefaultProgramFragment() const {
156 return mStateFragment.mDefault.get();
157 }
158 ProgramVertex * getDefaultProgramVertex() const {
159 return mStateVertex.mDefault.get();
160 }
Jason Sams54db59c2010-05-13 18:30:11 -0700161 ProgramStore * getDefaultProgramStore() const {
Jason Sams9c54bdb2009-06-17 16:52:59 -0700162 return mStateFragmentStore.mDefault.get();
163 }
Jason Samsebfb4362009-09-23 13:57:02 -0700164 ProgramRaster * getDefaultProgramRaster() const {
165 return mStateRaster.mDefault.get();
166 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700167 Font* getDefaultFont() const {
168 return mStateFont.mDefault.get();
169 }
Jason Sams9c54bdb2009-06-17 16:52:59 -0700170
Jason Samsf603d212010-05-14 15:30:29 -0700171 uint32_t getWidth() const {return mWidth;}
172 uint32_t getHeight() const {return mHeight;}
Jason Sams40a29e82009-08-10 14:55:26 -0700173
Jason Samsadd9d962010-11-22 16:20:16 -0800174 mutable ThreadIO mIO;
Jason Samsbc948de2009-08-17 18:35:48 -0700175
Jason Samsf4d16062009-08-19 12:17:14 -0700176 // Timers
177 enum Timers {
178 RS_TIMER_IDLE,
179 RS_TIMER_INTERNAL,
180 RS_TIMER_SCRIPT,
181 RS_TIMER_CLEAR_SWAP,
182 _RS_TIMER_TOTAL
183 };
184 uint64_t getTime() const;
185 void timerInit();
186 void timerReset();
187 void timerSet(Timers);
188 void timerPrint();
Jason Sams2525a812009-09-03 15:43:13 -0700189 void timerFrame();
Jason Samsf4d16062009-08-19 12:17:14 -0700190
Jason Sams66b27712009-09-25 15:25:00 -0700191 struct {
192 bool mLogTimes;
193 bool mLogScripts;
194 bool mLogObjects;
Jason Sams5dad8b42009-12-15 19:10:11 -0800195 bool mLogShaders;
Alex Sakhartchouk4378f112010-09-29 09:49:13 -0700196 bool mLogShadersAttr;
197 bool mLogShadersUniforms;
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700198 bool mLogVisual;
Jason Sams66b27712009-09-25 15:25:00 -0700199 } props;
Joe Onorato9ac2c662009-09-23 16:37:36 -0700200
Jason Sams9dab6672009-11-24 12:26:35 -0800201 void dumpDebug() const;
Jason Samsadd9d962010-11-22 16:20:16 -0800202 void checkError(const char *, bool isFatal = false) const;
203 void setError(RsError e, const char *msg = NULL) const;
Jason Sams9dab6672009-11-24 12:26:35 -0800204
Jason Samsa9e7a052009-09-25 14:51:22 -0700205 mutable const ObjectBase * mObjHead;
206
Jason Sams2978bfc2010-02-22 15:37:51 -0800207 bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;}
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700208 bool ext_GL_NV_texture_npot_2D_mipmap() const {return mGL.GL_NV_texture_npot_2D_mipmap;}
Alex Sakhartchouk4378f112010-09-29 09:49:13 -0700209 float ext_texture_max_aniso() const {return mGL.EXT_texture_max_aniso; }
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700210 uint32_t getMaxFragmentTextures() const {return mGL.mMaxFragmentTextureImageUnits;}
211 uint32_t getMaxFragmentUniformVectors() const {return mGL.mMaxFragmentUniformVectors;}
212 uint32_t getMaxVertexUniformVectors() const {return mGL.mMaxVertexUniformVectors;}
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800213 uint32_t getMaxVertexAttributes() const {return mGL.mMaxVertexAttribs;}
Jason Sams2978bfc2010-02-22 15:37:51 -0800214
Jason Sams8e6c17f2010-07-19 15:38:19 -0700215 void launchThreads(WorkerCallback_t cbk, void *data);
Jason Samsf3470ed2010-09-28 14:41:22 -0700216 uint32_t getWorkerPoolSize() const {return (uint32_t)mWorkers.mCount;}
Jason Sams8e6c17f2010-07-19 15:38:19 -0700217
Jason Samsd19f10d2009-05-22 14:03:28 -0700218protected:
219 Device *mDev;
220
Jason Samsb13ada52009-08-25 11:34:49 -0700221 struct {
222 EGLint mNumConfigs;
223 EGLint mMajorVersion;
224 EGLint mMinorVersion;
225 EGLConfig mConfig;
226 EGLContext mContext;
227 EGLSurface mSurface;
Jason Sams11c8af92010-10-13 15:31:10 -0700228 EGLSurface mSurfaceDefault;
Jason Samsb13ada52009-08-25 11:34:49 -0700229 EGLDisplay mDisplay;
230 } mEGL;
231
232 struct {
233 const uint8_t * mVendor;
234 const uint8_t * mRenderer;
235 const uint8_t * mVersion;
236 const uint8_t * mExtensions;
237
238 uint32_t mMajorVersion;
239 uint32_t mMinorVersion;
240
Jason Sams0011bcf2009-12-15 12:58:36 -0800241 int32_t mMaxVaryingVectors;
242 int32_t mMaxTextureImageUnits;
243
244 int32_t mMaxFragmentTextureImageUnits;
245 int32_t mMaxFragmentUniformVectors;
246
247 int32_t mMaxVertexAttribs;
248 int32_t mMaxVertexUniformVectors;
249 int32_t mMaxVertexTextureUnits;
Jason Sams2978bfc2010-02-22 15:37:51 -0800250
251 bool OES_texture_npot;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700252 bool GL_NV_texture_npot_2D_mipmap;
Alex Sakhartchouk4378f112010-09-29 09:49:13 -0700253 float EXT_texture_max_aniso;
Jason Samsb13ada52009-08-25 11:34:49 -0700254 } mGL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700255
Jason Sams3bc47d42009-11-12 15:10:25 -0800256 uint32_t mWidth;
257 uint32_t mHeight;
Jason Samsb9d5c572009-12-09 11:05:45 -0800258 int32_t mThreadPriority;
Jason Sams704ff642010-02-09 16:05:07 -0800259 bool mIsGraphicsContext;
Jason Sams3bc47d42009-11-12 15:10:25 -0800260
Jason Samsd19f10d2009-05-22 14:03:28 -0700261 bool mRunning;
262 bool mExit;
Jason Sams65e7aa52009-09-24 17:38:20 -0700263 bool mPaused;
Jason Samsadd9d962010-11-22 16:20:16 -0800264 mutable RsError mError;
Jason Samsd19f10d2009-05-22 14:03:28 -0700265
Jason Samsd19f10d2009-05-22 14:03:28 -0700266 pthread_t mThreadId;
Jason Sams7d787b42009-11-15 12:14:26 -0800267 pid_t mNativeThreadId;
Jason Samsd19f10d2009-05-22 14:03:28 -0700268
Jason Sams8e6c17f2010-07-19 15:38:19 -0700269 struct Workers {
270 volatile int mRunningCount;
271 volatile int mLaunchCount;
272 uint32_t mCount;
273 pthread_t *mThreadId;
274 pid_t *mNativeThreadId;
275 Signal mCompleteSignal;
276
277 Signal *mLaunchSignals;
278 WorkerCallback_t mLaunchCallback;
279 void *mLaunchData;
280 };
281 Workers mWorkers;
282
Jason Samsd19f10d2009-05-22 14:03:28 -0700283 ObjectBaseRef<Script> mRootScript;
284 ObjectBaseRef<ProgramFragment> mFragment;
285 ObjectBaseRef<ProgramVertex> mVertex;
Jason Sams54db59c2010-05-13 18:30:11 -0700286 ObjectBaseRef<ProgramStore> mFragmentStore;
Jason Samsebfb4362009-09-23 13:57:02 -0700287 ObjectBaseRef<ProgramRaster> mRaster;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700288 ObjectBaseRef<Font> mFont;
Jason Sams730ee652009-08-18 17:07:09 -0700289
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700290 void displayDebugStats();
291
Jason Samsd19f10d2009-05-22 14:03:28 -0700292private:
293 Context();
Jason Samsd5f06302010-11-03 14:27:11 -0700294 bool initContext(Device *, const RsSurfaceConfig *sc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700295
Jason Samsd5f06302010-11-03 14:27:11 -0700296
297 bool initGLThread();
Jason Sams71362202009-10-27 14:44:31 -0700298 void deinitEGL();
Jason Samsd19f10d2009-05-22 14:03:28 -0700299
Jason Samsb9d5c572009-12-09 11:05:45 -0800300 uint32_t runRootScript();
Jason Samsd19f10d2009-05-22 14:03:28 -0700301
302 static void * threadProc(void *);
Jason Sams8e6c17f2010-07-19 15:38:19 -0700303 static void * helperThreadProc(void *);
Jason Samsd19f10d2009-05-22 14:03:28 -0700304
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700305 ANativeWindow *mWndSurface;
Jason Sams3eaa3382009-06-10 15:04:38 -0700306
307 Vector<ObjectBase *> mNames;
Jason Samsf4d16062009-08-19 12:17:14 -0700308
309 uint64_t mTimers[_RS_TIMER_TOTAL];
310 Timers mTimerActive;
311 uint64_t mTimeLast;
Jason Sams2525a812009-09-03 15:43:13 -0700312 uint64_t mTimeFrame;
313 uint64_t mTimeLastFrame;
Jason Samsb9d5c572009-12-09 11:05:45 -0800314 uint32_t mTimeMSLastFrame;
315 uint32_t mTimeMSLastScript;
316 uint32_t mTimeMSLastSwap;
Alex Sakhartchouk76322af2010-10-05 13:23:55 -0700317 uint32_t mAverageFPSFrameCount;
318 uint64_t mAverageFPSStartTime;
319 uint32_t mAverageFPS;
Jason Samsd19f10d2009-05-22 14:03:28 -0700320};
321
Jason Samsd19f10d2009-05-22 14:03:28 -0700322}
323}
324#endif