blob: 82c368747cc5e6c0206a199d3fcd8837abcc20a4 [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"
21
Jason Samsbc948de2009-08-17 18:35:48 -070022#include "rsThreadIO.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070023#include "rsType.h"
24#include "rsMatrix.h"
25#include "rsAllocation.h"
Jason Sams1bada8c2009-08-09 17:01:55 -070026#include "rsSimpleMesh.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"
Jason Samsee411122009-07-21 12:20:54 -070033#include "rsLight.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070034#include "rsProgramFragment.h"
35#include "rsProgramFragmentStore.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 Sams9bee51c2009-08-05 13:57:03 -070051class Context
Jason Samsd19f10d2009-05-22 14:03:28 -070052{
53public:
Jason Sams704ff642010-02-09 16:05:07 -080054 Context(Device *, bool isGraphics, bool useDepth);
Jason Samsd19f10d2009-05-22 14:03:28 -070055 ~Context();
56
Jason Sams462d11b2009-06-19 16:03:18 -070057 static pthread_key_t gThreadTLSKey;
Jason Sams41c19db92009-10-15 16:47:31 -070058 static uint32_t gThreadTLSKeyCount;
Jason Sams71362202009-10-27 14:44:31 -070059 static uint32_t gGLContextCount;
Jason Sams41c19db92009-10-15 16:47:31 -070060 static pthread_mutex_t gInitMutex;
61
Jason Sams462d11b2009-06-19 16:03:18 -070062 struct ScriptTLSStruct {
63 Context * mContext;
64 Script * mScript;
65 };
66
Jason Samsd19f10d2009-05-22 14:03:28 -070067
68 //StructuredAllocationContext mStateAllocation;
69 ElementState mStateElement;
70 TypeState mStateType;
71 SamplerState mStateSampler;
72 ProgramFragmentState mStateFragment;
73 ProgramFragmentStoreState mStateFragmentStore;
Jason Samsebfb4362009-09-23 13:57:02 -070074 ProgramRasterState mStateRaster;
Jason Samsd19f10d2009-05-22 14:03:28 -070075 ProgramVertexState mStateVertex;
Jason Samsbba134c2009-06-22 15:49:21 -070076 LightState mStateLight;
Jason Samsbb51c402009-11-25 13:22:07 -080077 VertexArrayState mStateVertexArray;
Jason Samsd19f10d2009-05-22 14:03:28 -070078
Jason Samsd19f10d2009-05-22 14:03:28 -070079 ScriptCState mScriptC;
Jason Samsbb51c402009-11-25 13:22:07 -080080 ShaderCache mShaderCache;
Jason Samsd19f10d2009-05-22 14:03:28 -070081
Jason Samsd19f10d2009-05-22 14:03:28 -070082 void swapBuffers();
83 void setRootScript(Script *);
Jason Samsebfb4362009-09-23 13:57:02 -070084 void setRaster(ProgramRaster *);
Jason Samsd19f10d2009-05-22 14:03:28 -070085 void setVertex(ProgramVertex *);
86 void setFragment(ProgramFragment *);
87 void setFragmentStore(ProgramFragmentStore *);
88
89 void updateSurface(void *sur);
90
91 const ProgramFragment * getFragment() {return mFragment.get();}
92 const ProgramFragmentStore * getFragmentStore() {return mFragmentStore.get();}
Jason Samsebfb4362009-09-23 13:57:02 -070093 const ProgramRaster * getRaster() {return mRaster.get();}
Jason Samsb0ec1b42009-07-28 12:02:16 -070094 const ProgramVertex * getVertex() {return mVertex.get();}
Jason Samsd19f10d2009-05-22 14:03:28 -070095
Jason Sams156cce62010-03-03 13:03:18 -080096 bool setupCheck();
Jason Sams3b7d39b2009-12-14 12:57:40 -080097 bool checkDriver() const {return mEGL.mSurface != 0;}
Jason Samsd19f10d2009-05-22 14:03:28 -070098
Jason Sams65e7aa52009-09-24 17:38:20 -070099 void pause();
100 void resume();
Mathias Agopian5cbb9402010-02-12 12:00:38 -0800101 void setSurface(uint32_t w, uint32_t h, android_native_window_t *sur);
Jason Sams7d787b42009-11-15 12:14:26 -0800102 void setPriority(int32_t p);
Jason Sams65e7aa52009-09-24 17:38:20 -0700103
Jason Samsd5680f92009-06-10 18:39:40 -0700104 void assignName(ObjectBase *obj, const char *name, uint32_t len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700105 void removeName(ObjectBase *obj);
106 ObjectBase * lookupName(const char *name) const;
Jason Samsd5680f92009-06-10 18:39:40 -0700107 void appendNameDefines(String8 *str) const;
Jason Sams9c54bdb2009-06-17 16:52:59 -0700108
Jason Sams516c3192009-10-06 13:58:47 -0700109 uint32_t getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait);
110 bool sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace);
Jason Samsb9d5c572009-12-09 11:05:45 -0800111 uint32_t runScript(Script *s, uint32_t launchID);
Jason Sams516c3192009-10-06 13:58:47 -0700112
113 void initToClient();
114 void deinitToClient();
115
Jason Sams9c54bdb2009-06-17 16:52:59 -0700116 ProgramFragment * getDefaultProgramFragment() const {
117 return mStateFragment.mDefault.get();
118 }
119 ProgramVertex * getDefaultProgramVertex() const {
120 return mStateVertex.mDefault.get();
121 }
122 ProgramFragmentStore * getDefaultProgramFragmentStore() const {
123 return mStateFragmentStore.mDefault.get();
124 }
Jason Samsebfb4362009-09-23 13:57:02 -0700125 ProgramRaster * getDefaultProgramRaster() const {
126 return mStateRaster.mDefault.get();
127 }
Jason Sams9c54bdb2009-06-17 16:52:59 -0700128
Jason Samsb13ada52009-08-25 11:34:49 -0700129 uint32_t getWidth() const {return mEGL.mWidth;}
130 uint32_t getHeight() const {return mEGL.mHeight;}
Jason Sams40a29e82009-08-10 14:55:26 -0700131
Jason Samsbc948de2009-08-17 18:35:48 -0700132
133 ThreadIO mIO;
Jason Sams730ee652009-08-18 17:07:09 -0700134 void objDestroyAdd(ObjectBase *);
Jason Samsbc948de2009-08-17 18:35:48 -0700135
Jason Samsf4d16062009-08-19 12:17:14 -0700136 // Timers
137 enum Timers {
138 RS_TIMER_IDLE,
139 RS_TIMER_INTERNAL,
140 RS_TIMER_SCRIPT,
141 RS_TIMER_CLEAR_SWAP,
142 _RS_TIMER_TOTAL
143 };
144 uint64_t getTime() const;
145 void timerInit();
146 void timerReset();
147 void timerSet(Timers);
148 void timerPrint();
Jason Sams2525a812009-09-03 15:43:13 -0700149 void timerFrame();
Jason Samsf4d16062009-08-19 12:17:14 -0700150
Jason Samsb13ada52009-08-25 11:34:49 -0700151 bool checkVersion1_1() const {return (mGL.mMajorVersion > 1) || (mGL.mMinorVersion >= 1); }
152 bool checkVersion2_0() const {return mGL.mMajorVersion >= 2; }
153
Jason Sams66b27712009-09-25 15:25:00 -0700154 struct {
155 bool mLogTimes;
156 bool mLogScripts;
157 bool mLogObjects;
Jason Sams5dad8b42009-12-15 19:10:11 -0800158 bool mLogShaders;
Jason Sams66b27712009-09-25 15:25:00 -0700159 } props;
Joe Onorato9ac2c662009-09-23 16:37:36 -0700160
Jason Sams9dab6672009-11-24 12:26:35 -0800161 void dumpDebug() const;
Jason Sams718cd1f2009-12-23 14:35:29 -0800162 void checkError(const char *) const;
Jason Sams156cce62010-03-03 13:03:18 -0800163 const char * getError(RsError *);
164 void setError(RsError e, const char *msg);
Jason Sams9dab6672009-11-24 12:26:35 -0800165
Jason Samsa9e7a052009-09-25 14:51:22 -0700166 mutable const ObjectBase * mObjHead;
167
Jason Sams2978bfc2010-02-22 15:37:51 -0800168 bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;}
169
Jason Samsd19f10d2009-05-22 14:03:28 -0700170protected:
171 Device *mDev;
172
Jason Samsb13ada52009-08-25 11:34:49 -0700173 struct {
174 EGLint mNumConfigs;
175 EGLint mMajorVersion;
176 EGLint mMinorVersion;
177 EGLConfig mConfig;
178 EGLContext mContext;
179 EGLSurface mSurface;
180 EGLint mWidth;
181 EGLint mHeight;
182 EGLDisplay mDisplay;
183 } mEGL;
184
185 struct {
186 const uint8_t * mVendor;
187 const uint8_t * mRenderer;
188 const uint8_t * mVersion;
189 const uint8_t * mExtensions;
190
191 uint32_t mMajorVersion;
192 uint32_t mMinorVersion;
193
Jason Sams0011bcf2009-12-15 12:58:36 -0800194 int32_t mMaxVaryingVectors;
195 int32_t mMaxTextureImageUnits;
196
197 int32_t mMaxFragmentTextureImageUnits;
198 int32_t mMaxFragmentUniformVectors;
199
200 int32_t mMaxVertexAttribs;
201 int32_t mMaxVertexUniformVectors;
202 int32_t mMaxVertexTextureUnits;
Jason Sams2978bfc2010-02-22 15:37:51 -0800203
204 bool OES_texture_npot;
Jason Samsb13ada52009-08-25 11:34:49 -0700205 } mGL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700206
Jason Sams3bc47d42009-11-12 15:10:25 -0800207 uint32_t mWidth;
208 uint32_t mHeight;
Jason Samsb9d5c572009-12-09 11:05:45 -0800209 int32_t mThreadPriority;
Jason Sams704ff642010-02-09 16:05:07 -0800210 bool mIsGraphicsContext;
Jason Sams3bc47d42009-11-12 15:10:25 -0800211
Jason Samsd19f10d2009-05-22 14:03:28 -0700212 bool mRunning;
213 bool mExit;
Jason Samsb13ada52009-08-25 11:34:49 -0700214 bool mUseDepth;
Jason Sams65e7aa52009-09-24 17:38:20 -0700215 bool mPaused;
Jason Sams156cce62010-03-03 13:03:18 -0800216 RsError mError;
217 const char *mErrorMsg;
Jason Samsd19f10d2009-05-22 14:03:28 -0700218
Jason Samsd19f10d2009-05-22 14:03:28 -0700219 pthread_t mThreadId;
Jason Sams7d787b42009-11-15 12:14:26 -0800220 pid_t mNativeThreadId;
Jason Samsd19f10d2009-05-22 14:03:28 -0700221
222 ObjectBaseRef<Script> mRootScript;
223 ObjectBaseRef<ProgramFragment> mFragment;
224 ObjectBaseRef<ProgramVertex> mVertex;
225 ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
Jason Samsebfb4362009-09-23 13:57:02 -0700226 ObjectBaseRef<ProgramRaster> mRaster;
Jason Samsd19f10d2009-05-22 14:03:28 -0700227
Jason Sams730ee652009-08-18 17:07:09 -0700228
229 struct ObjDestroyOOB {
230 pthread_mutex_t mMutex;
231 Vector<ObjectBase *> mDestroyList;
232 bool mNeedToEmpty;
233 };
234 ObjDestroyOOB mObjDestroy;
235 bool objDestroyOOBInit();
236 void objDestroyOOBRun();
237 void objDestroyOOBDestroy();
238
Jason Samsd19f10d2009-05-22 14:03:28 -0700239private:
240 Context();
241
Jason Samsbb51c402009-11-25 13:22:07 -0800242 void initEGL(bool useGL2);
Jason Sams71362202009-10-27 14:44:31 -0700243 void deinitEGL();
Jason Samsd19f10d2009-05-22 14:03:28 -0700244
Jason Samsb9d5c572009-12-09 11:05:45 -0800245 uint32_t runRootScript();
Jason Samsd19f10d2009-05-22 14:03:28 -0700246
247 static void * threadProc(void *);
248
Mathias Agopian5cbb9402010-02-12 12:00:38 -0800249 android_native_window_t *mWndSurface;
Jason Sams3eaa3382009-06-10 15:04:38 -0700250
251 Vector<ObjectBase *> mNames;
Jason Samsf4d16062009-08-19 12:17:14 -0700252
253 uint64_t mTimers[_RS_TIMER_TOTAL];
254 Timers mTimerActive;
255 uint64_t mTimeLast;
Jason Sams2525a812009-09-03 15:43:13 -0700256 uint64_t mTimeFrame;
257 uint64_t mTimeLastFrame;
Jason Samsb9d5c572009-12-09 11:05:45 -0800258 uint32_t mTimeMSLastFrame;
259 uint32_t mTimeMSLastScript;
260 uint32_t mTimeMSLastSwap;
Jason Samsd19f10d2009-05-22 14:03:28 -0700261};
262
Jason Samsd19f10d2009-05-22 14:03:28 -0700263}
264}
265#endif