blob: df85a6bc92830973c8f4adcdb0362eb83724a8f6 [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 Samsd19f10d2009-05-22 14:03:28 -070021#include "rsType.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070022#include "rsAllocation.h"
Jason Sams7c878f32009-06-30 14:13:04 -070023#include "rsMesh.h"
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -080024
Jason Samse4a06c52011-03-16 16:29:28 -070025#include "rs_hal.h"
26
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -080027#ifndef ANDROID_RS_SERIALIZE
28#include "rsMutex.h"
29#include "rsThreadIO.h"
Jason Samsfcf72312011-04-20 15:09:01 -070030#include "rsMatrix4x4.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070031#include "rsDevice.h"
32#include "rsScriptC.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070033#include "rsAdapter.h"
34#include "rsSampler.h"
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070035#include "rsFont.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070036#include "rsProgramFragment.h"
Jason Sams54db59c2010-05-13 18:30:11 -070037#include "rsProgramStore.h"
Jason Samsebfb4362009-09-23 13:57:02 -070038#include "rsProgramRaster.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070039#include "rsProgramVertex.h"
Jason Samsbb51c402009-11-25 13:22:07 -080040#include "rsShaderCache.h"
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -070041#include "rsFBOCache.h"
Jason Samsbb51c402009-11-25 13:22:07 -080042#include "rsVertexArray.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070043
44#include "rsgApiStructs.h"
45#include "rsLocklessFifo.h"
46
Mathias Agopian5cbb9402010-02-12 12:00:38 -080047#include <ui/egl/android_natives.h>
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -080048#endif // ANDROID_RS_SERIALIZE
Jason Samsd19f10d2009-05-22 14:03:28 -070049
50// ---------------------------------------------------------------------------
51namespace android {
Mathias Agopian000479f2010-02-09 17:46:37 -080052
Jason Samsd19f10d2009-05-22 14:03:28 -070053namespace renderscript {
54
Jason Samsf166d9b2010-09-30 18:15:52 -070055#if 0
56#define CHECK_OBJ(o) { \
57 GET_TLS(); \
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080058 if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
Jason Samsf166d9b2010-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 Sakhartchouked9f2102010-11-09 17:00:54 -080064 if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
Jason Samsf166d9b2010-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 Sakhartchoukd0f5bd12011-01-31 14:53:24 -080073#ifndef ANDROID_RS_SERIALIZE
74
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080075class Context {
Jason Samsd19f10d2009-05-22 14:03:28 -070076public:
Jason Samse4a06c52011-03-16 16:29:28 -070077 struct Hal {
78 void * drv;
79
80 RsdHalFunctions funcs;
81 };
82 Hal mHal;
83
Jason Samsd5f06302010-11-03 14:27:11 -070084 static Context * createContext(Device *, const RsSurfaceConfig *sc);
Jason Samsd19f10d2009-05-22 14:03:28 -070085 ~Context();
86
Jason Sams41c19db92009-10-15 16:47:31 -070087 static pthread_mutex_t gInitMutex;
Stephen Hines1ac9da62011-01-07 15:11:30 -080088 // Library mutex (for providing thread-safe calls from the runtime)
89 static pthread_mutex_t gLibMutex;
Jason Sams41c19db92009-10-15 16:47:31 -070090
Jason Samsa17af042010-11-17 15:29:32 -080091 class PushState {
92 public:
93 PushState(Context *);
94 ~PushState();
95
96 private:
97 ObjectBaseRef<ProgramFragment> mFragment;
98 ObjectBaseRef<ProgramVertex> mVertex;
99 ObjectBaseRef<ProgramStore> mStore;
100 ObjectBaseRef<ProgramRaster> mRaster;
101 ObjectBaseRef<Font> mFont;
102 Context *mRsc;
103 };
104
Jason Sams11c8af92010-10-13 15:31:10 -0700105 RsSurfaceConfig mUserSurfaceConfig;
Jason Sams462d11b2009-06-19 16:03:18 -0700106
Jason Samsd19f10d2009-05-22 14:03:28 -0700107 ElementState mStateElement;
108 TypeState mStateType;
109 SamplerState mStateSampler;
110 ProgramFragmentState mStateFragment;
Jason Sams54db59c2010-05-13 18:30:11 -0700111 ProgramStoreState mStateFragmentStore;
Jason Samsebfb4362009-09-23 13:57:02 -0700112 ProgramRasterState mStateRaster;
Jason Samsd19f10d2009-05-22 14:03:28 -0700113 ProgramVertexState mStateVertex;
Jason Samsbb51c402009-11-25 13:22:07 -0800114 VertexArrayState mStateVertexArray;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700115 FontState mStateFont;
Jason Samsd19f10d2009-05-22 14:03:28 -0700116
Jason Samsd19f10d2009-05-22 14:03:28 -0700117 ScriptCState mScriptC;
Jason Samsbb51c402009-11-25 13:22:07 -0800118 ShaderCache mShaderCache;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700119 FBOCache mFBOCache;
Jason Samsd19f10d2009-05-22 14:03:28 -0700120
Jason Samsd19f10d2009-05-22 14:03:28 -0700121 void swapBuffers();
122 void setRootScript(Script *);
Jason Samsa17af042010-11-17 15:29:32 -0800123 void setProgramRaster(ProgramRaster *);
124 void setProgramVertex(ProgramVertex *);
125 void setProgramFragment(ProgramFragment *);
126 void setProgramStore(ProgramStore *);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700127 void setFont(Font *);
Jason Samsd19f10d2009-05-22 14:03:28 -0700128
129 void updateSurface(void *sur);
130
Jason Samsa17af042010-11-17 15:29:32 -0800131 ProgramFragment * getProgramFragment() {return mFragment.get();}
132 ProgramStore * getProgramStore() {return mFragmentStore.get();}
133 ProgramRaster * getProgramRaster() {return mRaster.get();}
134 ProgramVertex * getProgramVertex() {return mVertex.get();}
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700135 Font * getFont() {return mFont.get();}
Jason Samsd19f10d2009-05-22 14:03:28 -0700136
Jason Sams156cce62010-03-03 13:03:18 -0800137 bool setupCheck();
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700138 void setupProgramStore();
Jason Samsd19f10d2009-05-22 14:03:28 -0700139
Jason Sams65e7aa52009-09-24 17:38:20 -0700140 void pause();
141 void resume();
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700142 void setSurface(uint32_t w, uint32_t h, ANativeWindow *sur);
Jason Sams7d787b42009-11-15 12:14:26 -0800143 void setPriority(int32_t p);
Jason Sams5c68a712010-12-24 14:38:39 -0800144 void destroyWorkerThreadResources();
Jason Sams65e7aa52009-09-24 17:38:20 -0700145
Jason Samsd5680f92009-06-10 18:39:40 -0700146 void assignName(ObjectBase *obj, const char *name, uint32_t len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700147 void removeName(ObjectBase *obj);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700148
Jason Sams1c415172010-11-08 17:06:46 -0800149 RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID, bool wait);
150 RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait);
Jason Samsadd9d962010-11-22 16:20:16 -0800151 bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const;
Jason Samsf17bccc2010-05-28 18:23:22 -0700152 uint32_t runScript(Script *s);
Jason Sams516c3192009-10-06 13:58:47 -0700153
154 void initToClient();
155 void deinitToClient();
156
Jason Sams9c54bdb2009-06-17 16:52:59 -0700157 ProgramFragment * getDefaultProgramFragment() const {
158 return mStateFragment.mDefault.get();
159 }
160 ProgramVertex * getDefaultProgramVertex() const {
161 return mStateVertex.mDefault.get();
162 }
Jason Sams54db59c2010-05-13 18:30:11 -0700163 ProgramStore * getDefaultProgramStore() const {
Jason Sams9c54bdb2009-06-17 16:52:59 -0700164 return mStateFragmentStore.mDefault.get();
165 }
Jason Samsebfb4362009-09-23 13:57:02 -0700166 ProgramRaster * getDefaultProgramRaster() const {
167 return mStateRaster.mDefault.get();
168 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700169 Font* getDefaultFont() const {
170 return mStateFont.mDefault.get();
171 }
Jason Sams9c54bdb2009-06-17 16:52:59 -0700172
Jason Samsf603d212010-05-14 15:30:29 -0700173 uint32_t getWidth() const {return mWidth;}
174 uint32_t getHeight() const {return mHeight;}
Jason Sams40a29e82009-08-10 14:55:26 -0700175
Jason Samsadd9d962010-11-22 16:20:16 -0800176 mutable ThreadIO mIO;
Jason Samsbc948de2009-08-17 18:35:48 -0700177
Jason Samsf4d16062009-08-19 12:17:14 -0700178 // Timers
179 enum Timers {
180 RS_TIMER_IDLE,
181 RS_TIMER_INTERNAL,
182 RS_TIMER_SCRIPT,
183 RS_TIMER_CLEAR_SWAP,
184 _RS_TIMER_TOTAL
185 };
186 uint64_t getTime() const;
187 void timerInit();
188 void timerReset();
189 void timerSet(Timers);
190 void timerPrint();
Jason Sams2525a812009-09-03 15:43:13 -0700191 void timerFrame();
Jason Samsf4d16062009-08-19 12:17:14 -0700192
Jason Sams66b27712009-09-25 15:25:00 -0700193 struct {
194 bool mLogTimes;
195 bool mLogScripts;
196 bool mLogObjects;
Jason Sams5dad8b42009-12-15 19:10:11 -0800197 bool mLogShaders;
Alex Sakhartchouk4378f112010-09-29 09:49:13 -0700198 bool mLogShadersAttr;
199 bool mLogShadersUniforms;
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700200 bool mLogVisual;
Jason Sams66b27712009-09-25 15:25:00 -0700201 } props;
Joe Onorato9ac2c662009-09-23 16:37:36 -0700202
Jason Sams9dab6672009-11-24 12:26:35 -0800203 void dumpDebug() const;
Jason Samsadd9d962010-11-22 16:20:16 -0800204 void checkError(const char *, bool isFatal = false) const;
205 void setError(RsError e, const char *msg = NULL) const;
Jason Sams9dab6672009-11-24 12:26:35 -0800206
Jason Samsa9e7a052009-09-25 14:51:22 -0700207 mutable const ObjectBase * mObjHead;
208
Jason Sams2978bfc2010-02-22 15:37:51 -0800209 bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;}
Jason Sams9333e642011-01-13 17:02:35 -0800210 bool ext_GL_IMG_texture_npot() const {return mGL.GL_IMG_texture_npot;}
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700211 bool ext_GL_NV_texture_npot_2D_mipmap() const {return mGL.GL_NV_texture_npot_2D_mipmap;}
Alex Sakhartchouk4378f112010-09-29 09:49:13 -0700212 float ext_texture_max_aniso() const {return mGL.EXT_texture_max_aniso; }
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700213 uint32_t getMaxFragmentTextures() const {return mGL.mMaxFragmentTextureImageUnits;}
214 uint32_t getMaxFragmentUniformVectors() const {return mGL.mMaxFragmentUniformVectors;}
215 uint32_t getMaxVertexUniformVectors() const {return mGL.mMaxVertexUniformVectors;}
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800216 uint32_t getMaxVertexAttributes() const {return mGL.mMaxVertexAttribs;}
Jason Sams2978bfc2010-02-22 15:37:51 -0800217
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700218 uint32_t getDPI() const {return mDPI;}
219 void setDPI(uint32_t dpi) {mDPI = dpi;}
Jason Sams8e6c17f2010-07-19 15:38:19 -0700220
Jason Samsd19f10d2009-05-22 14:03:28 -0700221 Device *mDev;
Jason Sams803626f2011-04-06 17:52:23 -0700222protected:
Jason Samsd19f10d2009-05-22 14:03:28 -0700223
Jason Samsb13ada52009-08-25 11:34:49 -0700224 struct {
Jason Sams0011bcf2009-12-15 12:58:36 -0800225 int32_t mMaxVaryingVectors;
226 int32_t mMaxTextureImageUnits;
227
228 int32_t mMaxFragmentTextureImageUnits;
229 int32_t mMaxFragmentUniformVectors;
230
231 int32_t mMaxVertexAttribs;
232 int32_t mMaxVertexUniformVectors;
233 int32_t mMaxVertexTextureUnits;
Jason Sams2978bfc2010-02-22 15:37:51 -0800234
235 bool OES_texture_npot;
Jason Sams9333e642011-01-13 17:02:35 -0800236 bool GL_IMG_texture_npot;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700237 bool GL_NV_texture_npot_2D_mipmap;
Alex Sakhartchouk4378f112010-09-29 09:49:13 -0700238 float EXT_texture_max_aniso;
Jason Samsb13ada52009-08-25 11:34:49 -0700239 } mGL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700240
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700241 uint32_t mDPI;
Jason Sams3bc47d42009-11-12 15:10:25 -0800242 uint32_t mWidth;
243 uint32_t mHeight;
Jason Samsb9d5c572009-12-09 11:05:45 -0800244 int32_t mThreadPriority;
Jason Sams704ff642010-02-09 16:05:07 -0800245 bool mIsGraphicsContext;
Jason Sams3bc47d42009-11-12 15:10:25 -0800246
Jason Samsd19f10d2009-05-22 14:03:28 -0700247 bool mRunning;
248 bool mExit;
Jason Sams65e7aa52009-09-24 17:38:20 -0700249 bool mPaused;
Jason Samsadd9d962010-11-22 16:20:16 -0800250 mutable RsError mError;
Jason Samsd19f10d2009-05-22 14:03:28 -0700251
Jason Samsd19f10d2009-05-22 14:03:28 -0700252 pthread_t mThreadId;
Jason Sams7d787b42009-11-15 12:14:26 -0800253 pid_t mNativeThreadId;
Jason Samsd19f10d2009-05-22 14:03:28 -0700254
255 ObjectBaseRef<Script> mRootScript;
256 ObjectBaseRef<ProgramFragment> mFragment;
257 ObjectBaseRef<ProgramVertex> mVertex;
Jason Sams54db59c2010-05-13 18:30:11 -0700258 ObjectBaseRef<ProgramStore> mFragmentStore;
Jason Samsebfb4362009-09-23 13:57:02 -0700259 ObjectBaseRef<ProgramRaster> mRaster;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700260 ObjectBaseRef<Font> mFont;
Jason Sams730ee652009-08-18 17:07:09 -0700261
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700262 void displayDebugStats();
263
Jason Samsd19f10d2009-05-22 14:03:28 -0700264private:
265 Context();
Jason Samsd5f06302010-11-03 14:27:11 -0700266 bool initContext(Device *, const RsSurfaceConfig *sc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700267
Jason Samsd5f06302010-11-03 14:27:11 -0700268
269 bool initGLThread();
Jason Sams71362202009-10-27 14:44:31 -0700270 void deinitEGL();
Jason Samsd19f10d2009-05-22 14:03:28 -0700271
Jason Samsb9d5c572009-12-09 11:05:45 -0800272 uint32_t runRootScript();
Jason Samsd19f10d2009-05-22 14:03:28 -0700273
274 static void * threadProc(void *);
Jason Sams8e6c17f2010-07-19 15:38:19 -0700275 static void * helperThreadProc(void *);
Jason Samsd19f10d2009-05-22 14:03:28 -0700276
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700277 ANativeWindow *mWndSurface;
Jason Sams3eaa3382009-06-10 15:04:38 -0700278
279 Vector<ObjectBase *> mNames;
Jason Samsf4d16062009-08-19 12:17:14 -0700280
281 uint64_t mTimers[_RS_TIMER_TOTAL];
282 Timers mTimerActive;
283 uint64_t mTimeLast;
Jason Sams2525a812009-09-03 15:43:13 -0700284 uint64_t mTimeFrame;
285 uint64_t mTimeLastFrame;
Jason Samsb9d5c572009-12-09 11:05:45 -0800286 uint32_t mTimeMSLastFrame;
287 uint32_t mTimeMSLastScript;
288 uint32_t mTimeMSLastSwap;
Alex Sakhartchouk76322af2010-10-05 13:23:55 -0700289 uint32_t mAverageFPSFrameCount;
290 uint64_t mAverageFPSStartTime;
291 uint32_t mAverageFPS;
Jason Samsd19f10d2009-05-22 14:03:28 -0700292};
293
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800294#else
295
296class Context {
297public:
298 Context() {
299 mObjHead = NULL;
300 }
301 ~Context() {
302 ObjectBase::zeroAllUserRef(this);
303 }
304
305 ElementState mStateElement;
306 TypeState mStateType;
307
308 struct {
309 bool mLogTimes;
310 bool mLogScripts;
311 bool mLogObjects;
312 bool mLogShaders;
313 bool mLogShadersAttr;
314 bool mLogShadersUniforms;
315 bool mLogVisual;
316 } props;
317
318 void setError(RsError e, const char *msg = NULL) { }
319
320 mutable const ObjectBase * mObjHead;
321
322protected:
323
324};
325#endif //ANDROID_RS_SERIALIZE
326
327} // renderscript
328} // android
Jason Samsd19f10d2009-05-22 14:03:28 -0700329#endif