blob: 49ee676b84429ce0fd4c4af6af496c516c2d1b44 [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;
78
Jason Sams462d11b2009-06-19 16:03:18 -070079 struct ScriptTLSStruct {
80 Context * mContext;
81 Script * mScript;
82 };
Jason Samsa17af042010-11-17 15:29:32 -080083
84 class PushState {
85 public:
86 PushState(Context *);
87 ~PushState();
88
89 private:
90 ObjectBaseRef<ProgramFragment> mFragment;
91 ObjectBaseRef<ProgramVertex> mVertex;
92 ObjectBaseRef<ProgramStore> mStore;
93 ObjectBaseRef<ProgramRaster> mRaster;
94 ObjectBaseRef<Font> mFont;
95 Context *mRsc;
96 };
97
Jason Samsf166d9b2010-09-30 18:15:52 -070098 ScriptTLSStruct *mTlsStruct;
Jason Sams11c8af92010-10-13 15:31:10 -070099 RsSurfaceConfig mUserSurfaceConfig;
Jason Sams462d11b2009-06-19 16:03:18 -0700100
Jason Sams8e6c17f2010-07-19 15:38:19 -0700101 typedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
Jason Samsd19f10d2009-05-22 14:03:28 -0700102
103 //StructuredAllocationContext mStateAllocation;
104 ElementState mStateElement;
105 TypeState mStateType;
106 SamplerState mStateSampler;
107 ProgramFragmentState mStateFragment;
Jason Sams54db59c2010-05-13 18:30:11 -0700108 ProgramStoreState mStateFragmentStore;
Jason Samsebfb4362009-09-23 13:57:02 -0700109 ProgramRasterState mStateRaster;
Jason Samsd19f10d2009-05-22 14:03:28 -0700110 ProgramVertexState mStateVertex;
Jason Samsbb51c402009-11-25 13:22:07 -0800111 VertexArrayState mStateVertexArray;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700112 FontState mStateFont;
Jason Samsd19f10d2009-05-22 14:03:28 -0700113
Jason Samsd19f10d2009-05-22 14:03:28 -0700114 ScriptCState mScriptC;
Jason Samsbb51c402009-11-25 13:22:07 -0800115 ShaderCache mShaderCache;
Jason Samsd19f10d2009-05-22 14:03:28 -0700116
Jason Samsd19f10d2009-05-22 14:03:28 -0700117 void swapBuffers();
118 void setRootScript(Script *);
Jason Samsa17af042010-11-17 15:29:32 -0800119 void setProgramRaster(ProgramRaster *);
120 void setProgramVertex(ProgramVertex *);
121 void setProgramFragment(ProgramFragment *);
122 void setProgramStore(ProgramStore *);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700123 void setFont(Font *);
Jason Samsd19f10d2009-05-22 14:03:28 -0700124
125 void updateSurface(void *sur);
126
Jason Samsa17af042010-11-17 15:29:32 -0800127 ProgramFragment * getProgramFragment() {return mFragment.get();}
128 ProgramStore * getProgramStore() {return mFragmentStore.get();}
129 ProgramRaster * getProgramRaster() {return mRaster.get();}
130 ProgramVertex * getProgramVertex() {return mVertex.get();}
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700131 Font * getFont() {return mFont.get();}
Jason Samsd19f10d2009-05-22 14:03:28 -0700132
Jason Sams156cce62010-03-03 13:03:18 -0800133 bool setupCheck();
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700134 void setupProgramStore();
Jason Samsd19f10d2009-05-22 14:03:28 -0700135
Jason Sams65e7aa52009-09-24 17:38:20 -0700136 void pause();
137 void resume();
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700138 void setSurface(uint32_t w, uint32_t h, ANativeWindow *sur);
Jason Sams7d787b42009-11-15 12:14:26 -0800139 void setPriority(int32_t p);
Jason Sams65e7aa52009-09-24 17:38:20 -0700140
Jason Samsd5680f92009-06-10 18:39:40 -0700141 void assignName(ObjectBase *obj, const char *name, uint32_t len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700142 void removeName(ObjectBase *obj);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700143
Jason Sams1c415172010-11-08 17:06:46 -0800144 RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID, bool wait);
145 RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait);
Jason Samsadd9d962010-11-22 16:20:16 -0800146 bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const;
Jason Samsf17bccc2010-05-28 18:23:22 -0700147 uint32_t runScript(Script *s);
Jason Sams516c3192009-10-06 13:58:47 -0700148
149 void initToClient();
150 void deinitToClient();
151
Jason Sams9c54bdb2009-06-17 16:52:59 -0700152 ProgramFragment * getDefaultProgramFragment() const {
153 return mStateFragment.mDefault.get();
154 }
155 ProgramVertex * getDefaultProgramVertex() const {
156 return mStateVertex.mDefault.get();
157 }
Jason Sams54db59c2010-05-13 18:30:11 -0700158 ProgramStore * getDefaultProgramStore() const {
Jason Sams9c54bdb2009-06-17 16:52:59 -0700159 return mStateFragmentStore.mDefault.get();
160 }
Jason Samsebfb4362009-09-23 13:57:02 -0700161 ProgramRaster * getDefaultProgramRaster() const {
162 return mStateRaster.mDefault.get();
163 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700164 Font* getDefaultFont() const {
165 return mStateFont.mDefault.get();
166 }
Jason Sams9c54bdb2009-06-17 16:52:59 -0700167
Jason Samsf603d212010-05-14 15:30:29 -0700168 uint32_t getWidth() const {return mWidth;}
169 uint32_t getHeight() const {return mHeight;}
Jason Sams40a29e82009-08-10 14:55:26 -0700170
Jason Samsadd9d962010-11-22 16:20:16 -0800171 mutable ThreadIO mIO;
Jason Samsbc948de2009-08-17 18:35:48 -0700172
Jason Samsf4d16062009-08-19 12:17:14 -0700173 // Timers
174 enum Timers {
175 RS_TIMER_IDLE,
176 RS_TIMER_INTERNAL,
177 RS_TIMER_SCRIPT,
178 RS_TIMER_CLEAR_SWAP,
179 _RS_TIMER_TOTAL
180 };
181 uint64_t getTime() const;
182 void timerInit();
183 void timerReset();
184 void timerSet(Timers);
185 void timerPrint();
Jason Sams2525a812009-09-03 15:43:13 -0700186 void timerFrame();
Jason Samsf4d16062009-08-19 12:17:14 -0700187
Jason Sams66b27712009-09-25 15:25:00 -0700188 struct {
189 bool mLogTimes;
190 bool mLogScripts;
191 bool mLogObjects;
Jason Sams5dad8b42009-12-15 19:10:11 -0800192 bool mLogShaders;
Alex Sakhartchouk4378f112010-09-29 09:49:13 -0700193 bool mLogShadersAttr;
194 bool mLogShadersUniforms;
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700195 bool mLogVisual;
Jason Sams66b27712009-09-25 15:25:00 -0700196 } props;
Joe Onorato9ac2c662009-09-23 16:37:36 -0700197
Jason Sams9dab6672009-11-24 12:26:35 -0800198 void dumpDebug() const;
Jason Samsadd9d962010-11-22 16:20:16 -0800199 void checkError(const char *, bool isFatal = false) const;
200 void setError(RsError e, const char *msg = NULL) const;
Jason Sams9dab6672009-11-24 12:26:35 -0800201
Jason Samsa9e7a052009-09-25 14:51:22 -0700202 mutable const ObjectBase * mObjHead;
203
Jason Sams2978bfc2010-02-22 15:37:51 -0800204 bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;}
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700205 bool ext_GL_NV_texture_npot_2D_mipmap() const {return mGL.GL_NV_texture_npot_2D_mipmap;}
Alex Sakhartchouk4378f112010-09-29 09:49:13 -0700206 float ext_texture_max_aniso() const {return mGL.EXT_texture_max_aniso; }
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700207 uint32_t getMaxFragmentTextures() const {return mGL.mMaxFragmentTextureImageUnits;}
208 uint32_t getMaxFragmentUniformVectors() const {return mGL.mMaxFragmentUniformVectors;}
209 uint32_t getMaxVertexUniformVectors() const {return mGL.mMaxVertexUniformVectors;}
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800210 uint32_t getMaxVertexAttributes() const {return mGL.mMaxVertexAttribs;}
Jason Sams2978bfc2010-02-22 15:37:51 -0800211
Jason Sams8e6c17f2010-07-19 15:38:19 -0700212 void launchThreads(WorkerCallback_t cbk, void *data);
Jason Samsf3470ed2010-09-28 14:41:22 -0700213 uint32_t getWorkerPoolSize() const {return (uint32_t)mWorkers.mCount;}
Jason Sams8e6c17f2010-07-19 15:38:19 -0700214
Jason Samsd19f10d2009-05-22 14:03:28 -0700215protected:
216 Device *mDev;
217
Jason Samsb13ada52009-08-25 11:34:49 -0700218 struct {
219 EGLint mNumConfigs;
220 EGLint mMajorVersion;
221 EGLint mMinorVersion;
222 EGLConfig mConfig;
223 EGLContext mContext;
224 EGLSurface mSurface;
Jason Sams11c8af92010-10-13 15:31:10 -0700225 EGLSurface mSurfaceDefault;
Jason Samsb13ada52009-08-25 11:34:49 -0700226 EGLDisplay mDisplay;
227 } mEGL;
228
229 struct {
230 const uint8_t * mVendor;
231 const uint8_t * mRenderer;
232 const uint8_t * mVersion;
233 const uint8_t * mExtensions;
234
235 uint32_t mMajorVersion;
236 uint32_t mMinorVersion;
237
Jason Sams0011bcf2009-12-15 12:58:36 -0800238 int32_t mMaxVaryingVectors;
239 int32_t mMaxTextureImageUnits;
240
241 int32_t mMaxFragmentTextureImageUnits;
242 int32_t mMaxFragmentUniformVectors;
243
244 int32_t mMaxVertexAttribs;
245 int32_t mMaxVertexUniformVectors;
246 int32_t mMaxVertexTextureUnits;
Jason Sams2978bfc2010-02-22 15:37:51 -0800247
248 bool OES_texture_npot;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700249 bool GL_NV_texture_npot_2D_mipmap;
Alex Sakhartchouk4378f112010-09-29 09:49:13 -0700250 float EXT_texture_max_aniso;
Jason Samsb13ada52009-08-25 11:34:49 -0700251 } mGL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700252
Jason Sams3bc47d42009-11-12 15:10:25 -0800253 uint32_t mWidth;
254 uint32_t mHeight;
Jason Samsb9d5c572009-12-09 11:05:45 -0800255 int32_t mThreadPriority;
Jason Sams704ff642010-02-09 16:05:07 -0800256 bool mIsGraphicsContext;
Jason Sams3bc47d42009-11-12 15:10:25 -0800257
Jason Samsd19f10d2009-05-22 14:03:28 -0700258 bool mRunning;
259 bool mExit;
Jason Sams65e7aa52009-09-24 17:38:20 -0700260 bool mPaused;
Jason Samsadd9d962010-11-22 16:20:16 -0800261 mutable RsError mError;
Jason Samsd19f10d2009-05-22 14:03:28 -0700262
Jason Samsd19f10d2009-05-22 14:03:28 -0700263 pthread_t mThreadId;
Jason Sams7d787b42009-11-15 12:14:26 -0800264 pid_t mNativeThreadId;
Jason Samsd19f10d2009-05-22 14:03:28 -0700265
Jason Sams8e6c17f2010-07-19 15:38:19 -0700266 struct Workers {
267 volatile int mRunningCount;
268 volatile int mLaunchCount;
269 uint32_t mCount;
270 pthread_t *mThreadId;
271 pid_t *mNativeThreadId;
272 Signal mCompleteSignal;
273
274 Signal *mLaunchSignals;
275 WorkerCallback_t mLaunchCallback;
276 void *mLaunchData;
277 };
278 Workers mWorkers;
279
Jason Samsd19f10d2009-05-22 14:03:28 -0700280 ObjectBaseRef<Script> mRootScript;
281 ObjectBaseRef<ProgramFragment> mFragment;
282 ObjectBaseRef<ProgramVertex> mVertex;
Jason Sams54db59c2010-05-13 18:30:11 -0700283 ObjectBaseRef<ProgramStore> mFragmentStore;
Jason Samsebfb4362009-09-23 13:57:02 -0700284 ObjectBaseRef<ProgramRaster> mRaster;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700285 ObjectBaseRef<Font> mFont;
Jason Sams730ee652009-08-18 17:07:09 -0700286
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700287 void displayDebugStats();
288
Jason Samsd19f10d2009-05-22 14:03:28 -0700289private:
290 Context();
Jason Samsd5f06302010-11-03 14:27:11 -0700291 bool initContext(Device *, const RsSurfaceConfig *sc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700292
Jason Samsd5f06302010-11-03 14:27:11 -0700293
294 bool initGLThread();
Jason Sams71362202009-10-27 14:44:31 -0700295 void deinitEGL();
Jason Samsd19f10d2009-05-22 14:03:28 -0700296
Jason Samsb9d5c572009-12-09 11:05:45 -0800297 uint32_t runRootScript();
Jason Samsd19f10d2009-05-22 14:03:28 -0700298
299 static void * threadProc(void *);
Jason Sams8e6c17f2010-07-19 15:38:19 -0700300 static void * helperThreadProc(void *);
Jason Samsd19f10d2009-05-22 14:03:28 -0700301
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700302 ANativeWindow *mWndSurface;
Jason Sams3eaa3382009-06-10 15:04:38 -0700303
304 Vector<ObjectBase *> mNames;
Jason Samsf4d16062009-08-19 12:17:14 -0700305
306 uint64_t mTimers[_RS_TIMER_TOTAL];
307 Timers mTimerActive;
308 uint64_t mTimeLast;
Jason Sams2525a812009-09-03 15:43:13 -0700309 uint64_t mTimeFrame;
310 uint64_t mTimeLastFrame;
Jason Samsb9d5c572009-12-09 11:05:45 -0800311 uint32_t mTimeMSLastFrame;
312 uint32_t mTimeMSLastScript;
313 uint32_t mTimeMSLastSwap;
Alex Sakhartchouk76322af2010-10-05 13:23:55 -0700314 uint32_t mAverageFPSFrameCount;
315 uint64_t mAverageFPSStartTime;
316 uint32_t mAverageFPS;
Jason Samsd19f10d2009-05-22 14:03:28 -0700317};
318
Jason Samsd19f10d2009-05-22 14:03:28 -0700319}
320}
321#endif