blob: eacfdf795c54c308fdbf6fef02635b107a94423b [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#ifndef ANDROID_RS_CONTEXT_H
18#define ANDROID_RS_CONTEXT_H
19
Jason Samsa5577802009-07-13 12:20:31 -070020#include "rsUtils.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070021#include "rsType.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070022#include "rsAllocation.h"
Jason Samsa89371c2009-06-30 14:13:04 -070023#include "rsMesh.h"
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080024
Jason Samsbad80742011-03-16 16:29:28 -070025#include "rs_hal.h"
26
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080027#ifndef ANDROID_RS_SERIALIZE
28#include "rsMutex.h"
29#include "rsThreadIO.h"
30#include "rsMatrix.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070031#include "rsDevice.h"
32#include "rsScriptC.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070033#include "rsAdapter.h"
34#include "rsSampler.h"
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -070035#include "rsFont.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070036#include "rsProgramFragment.h"
Jason Samsccc010b2010-05-13 18:30:11 -070037#include "rsProgramStore.h"
Jason Sams5fd09d82009-09-23 13:57:02 -070038#include "rsProgramRaster.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070039#include "rsProgramVertex.h"
Jason Samsc460e552009-11-25 13:22:07 -080040#include "rsShaderCache.h"
Alex Sakhartchouk7d9c5ff2011-04-01 14:19:01 -070041#include "rsFBOCache.h"
Jason Samsc460e552009-11-25 13:22:07 -080042#include "rsVertexArray.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070043
44#include "rsgApiStructs.h"
45#include "rsLocklessFifo.h"
46
Mathias Agopian9b97c292010-02-12 12:00:38 -080047#include <ui/egl/android_natives.h>
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080048#endif // ANDROID_RS_SERIALIZE
Jason Sams326e0dd2009-05-22 14:03:28 -070049
50// ---------------------------------------------------------------------------
51namespace android {
Mathias Agopiand4c25e32010-02-09 17:46:37 -080052
Jason Sams326e0dd2009-05-22 14:03:28 -070053namespace renderscript {
54
Jason Sams605048a2010-09-30 18:15:52 -070055#if 0
56#define CHECK_OBJ(o) { \
57 GET_TLS(); \
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080058 if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
Jason Sams605048a2010-09-30 18:15:52 -070059 LOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \
60 } \
61}
62#define CHECK_OBJ_OR_NULL(o) { \
63 GET_TLS(); \
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080064 if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
Jason Sams605048a2010-09-30 18:15:52 -070065 LOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \
66 } \
67}
68#else
69#define CHECK_OBJ(o)
70#define CHECK_OBJ_OR_NULL(o)
71#endif
72
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080073#ifndef ANDROID_RS_SERIALIZE
74
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080075class Context {
Jason Sams326e0dd2009-05-22 14:03:28 -070076public:
Jason Samsbad80742011-03-16 16:29:28 -070077 struct Hal {
78 void * drv;
79
80 RsdHalFunctions funcs;
81 };
82 Hal mHal;
83
Jason Sams5c1c79a2010-11-03 14:27:11 -070084 static Context * createContext(Device *, const RsSurfaceConfig *sc);
Jason Sams326e0dd2009-05-22 14:03:28 -070085 ~Context();
86
Jason Samse5769102009-06-19 16:03:18 -070087 static pthread_key_t gThreadTLSKey;
Jason Samsfb03a222009-10-15 16:47:31 -070088 static uint32_t gThreadTLSKeyCount;
Jason Sams33b6e3b2009-10-27 14:44:31 -070089 static uint32_t gGLContextCount;
Jason Samsfb03a222009-10-15 16:47:31 -070090 static pthread_mutex_t gInitMutex;
Stephen Hinesca3f09c2011-01-07 15:11:30 -080091 // Library mutex (for providing thread-safe calls from the runtime)
92 static pthread_mutex_t gLibMutex;
Jason Samsfb03a222009-10-15 16:47:31 -070093
Jason Sams60709252010-11-17 15:29:32 -080094 class PushState {
95 public:
96 PushState(Context *);
97 ~PushState();
98
99 private:
100 ObjectBaseRef<ProgramFragment> mFragment;
101 ObjectBaseRef<ProgramVertex> mVertex;
102 ObjectBaseRef<ProgramStore> mStore;
103 ObjectBaseRef<ProgramRaster> mRaster;
104 ObjectBaseRef<Font> mFont;
105 Context *mRsc;
106 };
107
Jason Sams605048a2010-09-30 18:15:52 -0700108 ScriptTLSStruct *mTlsStruct;
Jason Sams6b8552a2010-10-13 15:31:10 -0700109 RsSurfaceConfig mUserSurfaceConfig;
Jason Samse5769102009-06-19 16:03:18 -0700110
Jason Sams326e0dd2009-05-22 14:03:28 -0700111 ElementState mStateElement;
112 TypeState mStateType;
113 SamplerState mStateSampler;
114 ProgramFragmentState mStateFragment;
Jason Samsccc010b2010-05-13 18:30:11 -0700115 ProgramStoreState mStateFragmentStore;
Jason Sams5fd09d82009-09-23 13:57:02 -0700116 ProgramRasterState mStateRaster;
Jason Sams326e0dd2009-05-22 14:03:28 -0700117 ProgramVertexState mStateVertex;
Jason Samsc460e552009-11-25 13:22:07 -0800118 VertexArrayState mStateVertexArray;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700119 FontState mStateFont;
Jason Sams326e0dd2009-05-22 14:03:28 -0700120
Jason Sams326e0dd2009-05-22 14:03:28 -0700121 ScriptCState mScriptC;
Jason Samsc460e552009-11-25 13:22:07 -0800122 ShaderCache mShaderCache;
Alex Sakhartchouk7d9c5ff2011-04-01 14:19:01 -0700123 FBOCache mFBOCache;
Jason Sams326e0dd2009-05-22 14:03:28 -0700124
Jason Sams326e0dd2009-05-22 14:03:28 -0700125 void swapBuffers();
126 void setRootScript(Script *);
Jason Sams60709252010-11-17 15:29:32 -0800127 void setProgramRaster(ProgramRaster *);
128 void setProgramVertex(ProgramVertex *);
129 void setProgramFragment(ProgramFragment *);
130 void setProgramStore(ProgramStore *);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700131 void setFont(Font *);
Jason Sams326e0dd2009-05-22 14:03:28 -0700132
133 void updateSurface(void *sur);
134
Jason Sams60709252010-11-17 15:29:32 -0800135 ProgramFragment * getProgramFragment() {return mFragment.get();}
136 ProgramStore * getProgramStore() {return mFragmentStore.get();}
137 ProgramRaster * getProgramRaster() {return mRaster.get();}
138 ProgramVertex * getProgramVertex() {return mVertex.get();}
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700139 Font * getFont() {return mFont.get();}
Jason Sams326e0dd2009-05-22 14:03:28 -0700140
Jason Samsa2cf7552010-03-03 13:03:18 -0800141 bool setupCheck();
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700142 void setupProgramStore();
Jason Sams326e0dd2009-05-22 14:03:28 -0700143
Jason Sams86f1b232009-09-24 17:38:20 -0700144 void pause();
145 void resume();
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700146 void setSurface(uint32_t w, uint32_t h, ANativeWindow *sur);
Jason Sams15832442009-11-15 12:14:26 -0800147 void setPriority(int32_t p);
Jason Sams741aac92010-12-24 14:38:39 -0800148 void destroyWorkerThreadResources();
Jason Sams86f1b232009-09-24 17:38:20 -0700149
Jason Samsa4a54e42009-06-10 18:39:40 -0700150 void assignName(ObjectBase *obj, const char *name, uint32_t len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700151 void removeName(ObjectBase *obj);
Jason Sams8ce125b2009-06-17 16:52:59 -0700152
Jason Samsaad4bc52010-11-08 17:06:46 -0800153 RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID, bool wait);
154 RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait);
Jason Sams87319de2010-11-22 16:20:16 -0800155 bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const;
Jason Samsc61346b2010-05-28 18:23:22 -0700156 uint32_t runScript(Script *s);
Jason Sams8c401ef2009-10-06 13:58:47 -0700157
158 void initToClient();
159 void deinitToClient();
160
Jason Sams8ce125b2009-06-17 16:52:59 -0700161 ProgramFragment * getDefaultProgramFragment() const {
162 return mStateFragment.mDefault.get();
163 }
164 ProgramVertex * getDefaultProgramVertex() const {
165 return mStateVertex.mDefault.get();
166 }
Jason Samsccc010b2010-05-13 18:30:11 -0700167 ProgramStore * getDefaultProgramStore() const {
Jason Sams8ce125b2009-06-17 16:52:59 -0700168 return mStateFragmentStore.mDefault.get();
169 }
Jason Sams5fd09d82009-09-23 13:57:02 -0700170 ProgramRaster * getDefaultProgramRaster() const {
171 return mStateRaster.mDefault.get();
172 }
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700173 Font* getDefaultFont() const {
174 return mStateFont.mDefault.get();
175 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700176
Jason Sams771565f2010-05-14 15:30:29 -0700177 uint32_t getWidth() const {return mWidth;}
178 uint32_t getHeight() const {return mHeight;}
Jason Samse579df42009-08-10 14:55:26 -0700179
Jason Sams87319de2010-11-22 16:20:16 -0800180 mutable ThreadIO mIO;
Jason Samsfcd31922009-08-17 18:35:48 -0700181
Jason Sams24371d92009-08-19 12:17:14 -0700182 // Timers
183 enum Timers {
184 RS_TIMER_IDLE,
185 RS_TIMER_INTERNAL,
186 RS_TIMER_SCRIPT,
187 RS_TIMER_CLEAR_SWAP,
188 _RS_TIMER_TOTAL
189 };
190 uint64_t getTime() const;
191 void timerInit();
192 void timerReset();
193 void timerSet(Timers);
194 void timerPrint();
Jason Sams1d54f102009-09-03 15:43:13 -0700195 void timerFrame();
Jason Sams24371d92009-08-19 12:17:14 -0700196
Jason Sams1fddd902009-09-25 15:25:00 -0700197 struct {
198 bool mLogTimes;
199 bool mLogScripts;
200 bool mLogObjects;
Jason Samscd506532009-12-15 19:10:11 -0800201 bool mLogShaders;
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700202 bool mLogShadersAttr;
203 bool mLogShadersUniforms;
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700204 bool mLogVisual;
Jason Sams1fddd902009-09-25 15:25:00 -0700205 } props;
Joe Onorato76371ff2009-09-23 16:37:36 -0700206
Jason Sams13e26342009-11-24 12:26:35 -0800207 void dumpDebug() const;
Jason Sams87319de2010-11-22 16:20:16 -0800208 void checkError(const char *, bool isFatal = false) const;
209 void setError(RsError e, const char *msg = NULL) const;
Jason Sams13e26342009-11-24 12:26:35 -0800210
Jason Samse514b452009-09-25 14:51:22 -0700211 mutable const ObjectBase * mObjHead;
212
Jason Samsef21edc2010-02-22 15:37:51 -0800213 bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;}
Jason Sams0f7785c2011-01-13 17:02:35 -0800214 bool ext_GL_IMG_texture_npot() const {return mGL.GL_IMG_texture_npot;}
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700215 bool ext_GL_NV_texture_npot_2D_mipmap() const {return mGL.GL_NV_texture_npot_2D_mipmap;}
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700216 float ext_texture_max_aniso() const {return mGL.EXT_texture_max_aniso; }
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700217 uint32_t getMaxFragmentTextures() const {return mGL.mMaxFragmentTextureImageUnits;}
218 uint32_t getMaxFragmentUniformVectors() const {return mGL.mMaxFragmentUniformVectors;}
219 uint32_t getMaxVertexUniformVectors() const {return mGL.mMaxVertexUniformVectors;}
Alex Sakhartchouk2d791972010-12-13 14:48:21 -0800220 uint32_t getMaxVertexAttributes() const {return mGL.mMaxVertexAttribs;}
Jason Samsef21edc2010-02-22 15:37:51 -0800221
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700222 uint32_t getDPI() const {return mDPI;}
223 void setDPI(uint32_t dpi) {mDPI = dpi;}
Jason Sams7bf29dd2010-07-19 15:38:19 -0700224
Jason Sams326e0dd2009-05-22 14:03:28 -0700225protected:
226 Device *mDev;
227
Jason Samsafcb25c2009-08-25 11:34:49 -0700228 struct {
229 EGLint mNumConfigs;
230 EGLint mMajorVersion;
231 EGLint mMinorVersion;
232 EGLConfig mConfig;
233 EGLContext mContext;
234 EGLSurface mSurface;
Jason Sams6b8552a2010-10-13 15:31:10 -0700235 EGLSurface mSurfaceDefault;
Jason Samsafcb25c2009-08-25 11:34:49 -0700236 EGLDisplay mDisplay;
237 } mEGL;
238
239 struct {
240 const uint8_t * mVendor;
241 const uint8_t * mRenderer;
242 const uint8_t * mVersion;
243 const uint8_t * mExtensions;
244
245 uint32_t mMajorVersion;
246 uint32_t mMinorVersion;
247
Jason Sams4815c0d2009-12-15 12:58:36 -0800248 int32_t mMaxVaryingVectors;
249 int32_t mMaxTextureImageUnits;
250
251 int32_t mMaxFragmentTextureImageUnits;
252 int32_t mMaxFragmentUniformVectors;
253
254 int32_t mMaxVertexAttribs;
255 int32_t mMaxVertexUniformVectors;
256 int32_t mMaxVertexTextureUnits;
Jason Samsef21edc2010-02-22 15:37:51 -0800257
258 bool OES_texture_npot;
Jason Sams0f7785c2011-01-13 17:02:35 -0800259 bool GL_IMG_texture_npot;
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700260 bool GL_NV_texture_npot_2D_mipmap;
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700261 float EXT_texture_max_aniso;
Jason Samsafcb25c2009-08-25 11:34:49 -0700262 } mGL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700263
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700264 uint32_t mDPI;
Jason Sams613cad12009-11-12 15:10:25 -0800265 uint32_t mWidth;
266 uint32_t mHeight;
Jason Sams2dca84d2009-12-09 11:05:45 -0800267 int32_t mThreadPriority;
Jason Sams4820e8b2010-02-09 16:05:07 -0800268 bool mIsGraphicsContext;
Jason Sams613cad12009-11-12 15:10:25 -0800269
Jason Sams326e0dd2009-05-22 14:03:28 -0700270 bool mRunning;
271 bool mExit;
Jason Sams86f1b232009-09-24 17:38:20 -0700272 bool mPaused;
Jason Sams87319de2010-11-22 16:20:16 -0800273 mutable RsError mError;
Jason Sams326e0dd2009-05-22 14:03:28 -0700274
Jason Sams326e0dd2009-05-22 14:03:28 -0700275 pthread_t mThreadId;
Jason Sams15832442009-11-15 12:14:26 -0800276 pid_t mNativeThreadId;
Jason Sams326e0dd2009-05-22 14:03:28 -0700277
278 ObjectBaseRef<Script> mRootScript;
279 ObjectBaseRef<ProgramFragment> mFragment;
280 ObjectBaseRef<ProgramVertex> mVertex;
Jason Samsccc010b2010-05-13 18:30:11 -0700281 ObjectBaseRef<ProgramStore> mFragmentStore;
Jason Sams5fd09d82009-09-23 13:57:02 -0700282 ObjectBaseRef<ProgramRaster> mRaster;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700283 ObjectBaseRef<Font> mFont;
Jason Sams50869382009-08-18 17:07:09 -0700284
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700285 void displayDebugStats();
286
Jason Sams326e0dd2009-05-22 14:03:28 -0700287private:
288 Context();
Jason Sams5c1c79a2010-11-03 14:27:11 -0700289 bool initContext(Device *, const RsSurfaceConfig *sc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700290
Jason Sams5c1c79a2010-11-03 14:27:11 -0700291
292 bool initGLThread();
Jason Sams33b6e3b2009-10-27 14:44:31 -0700293 void deinitEGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700294
Jason Sams2dca84d2009-12-09 11:05:45 -0800295 uint32_t runRootScript();
Jason Sams326e0dd2009-05-22 14:03:28 -0700296
297 static void * threadProc(void *);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700298 static void * helperThreadProc(void *);
Jason Sams326e0dd2009-05-22 14:03:28 -0700299
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700300 ANativeWindow *mWndSurface;
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700301
302 Vector<ObjectBase *> mNames;
Jason Sams24371d92009-08-19 12:17:14 -0700303
304 uint64_t mTimers[_RS_TIMER_TOTAL];
305 Timers mTimerActive;
306 uint64_t mTimeLast;
Jason Sams1d54f102009-09-03 15:43:13 -0700307 uint64_t mTimeFrame;
308 uint64_t mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800309 uint32_t mTimeMSLastFrame;
310 uint32_t mTimeMSLastScript;
311 uint32_t mTimeMSLastSwap;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700312 uint32_t mAverageFPSFrameCount;
313 uint64_t mAverageFPSStartTime;
314 uint32_t mAverageFPS;
Jason Sams326e0dd2009-05-22 14:03:28 -0700315};
316
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800317#else
318
319class Context {
320public:
321 Context() {
322 mObjHead = NULL;
323 }
324 ~Context() {
325 ObjectBase::zeroAllUserRef(this);
326 }
327
328 ElementState mStateElement;
329 TypeState mStateType;
330
331 struct {
332 bool mLogTimes;
333 bool mLogScripts;
334 bool mLogObjects;
335 bool mLogShaders;
336 bool mLogShadersAttr;
337 bool mLogShadersUniforms;
338 bool mLogVisual;
339 } props;
340
341 void setError(RsError e, const char *msg = NULL) { }
342
343 mutable const ObjectBase * mObjHead;
344
345protected:
346
347};
348#endif //ANDROID_RS_SERIALIZE
349
350} // renderscript
351} // android
Jason Sams326e0dd2009-05-22 14:03:28 -0700352#endif