blob: 72574a60a37d1268851c7506fd3d26479cec6b52 [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"
30#include "rsMatrix.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"
41#include "rsVertexArray.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070042
43#include "rsgApiStructs.h"
44#include "rsLocklessFifo.h"
45
Mathias Agopian5cbb9402010-02-12 12:00:38 -080046#include <ui/egl/android_natives.h>
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -080047#endif // ANDROID_RS_SERIALIZE
Jason Samsd19f10d2009-05-22 14:03:28 -070048
49// ---------------------------------------------------------------------------
50namespace android {
Mathias Agopian000479f2010-02-09 17:46:37 -080051
Jason Samsd19f10d2009-05-22 14:03:28 -070052namespace renderscript {
53
Jason Samsf166d9b2010-09-30 18:15:52 -070054#if 0
55#define CHECK_OBJ(o) { \
56 GET_TLS(); \
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080057 if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
Jason Samsf166d9b2010-09-30 18:15:52 -070058 LOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \
59 } \
60}
61#define CHECK_OBJ_OR_NULL(o) { \
62 GET_TLS(); \
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080063 if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
Jason Samsf166d9b2010-09-30 18:15:52 -070064 LOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \
65 } \
66}
67#else
68#define CHECK_OBJ(o)
69#define CHECK_OBJ_OR_NULL(o)
70#endif
71
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -080072#ifndef ANDROID_RS_SERIALIZE
73
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080074class Context {
Jason Samsd19f10d2009-05-22 14:03:28 -070075public:
Jason Samse4a06c52011-03-16 16:29:28 -070076 struct Hal {
77 void * drv;
78
79 RsdHalFunctions funcs;
80 };
81 Hal mHal;
82
Jason Samsd5f06302010-11-03 14:27:11 -070083 static Context * createContext(Device *, const RsSurfaceConfig *sc);
Jason Samsd19f10d2009-05-22 14:03:28 -070084 ~Context();
85
Jason Sams462d11b2009-06-19 16:03:18 -070086 static pthread_key_t gThreadTLSKey;
Jason Sams41c19db92009-10-15 16:47:31 -070087 static uint32_t gThreadTLSKeyCount;
Jason Sams71362202009-10-27 14:44:31 -070088 static uint32_t gGLContextCount;
Jason Sams41c19db92009-10-15 16:47:31 -070089 static pthread_mutex_t gInitMutex;
Stephen Hines1ac9da62011-01-07 15:11:30 -080090 // Library mutex (for providing thread-safe calls from the runtime)
91 static pthread_mutex_t gLibMutex;
Jason Sams41c19db92009-10-15 16:47:31 -070092
Jason Samsa17af042010-11-17 15:29:32 -080093 class PushState {
94 public:
95 PushState(Context *);
96 ~PushState();
97
98 private:
99 ObjectBaseRef<ProgramFragment> mFragment;
100 ObjectBaseRef<ProgramVertex> mVertex;
101 ObjectBaseRef<ProgramStore> mStore;
102 ObjectBaseRef<ProgramRaster> mRaster;
103 ObjectBaseRef<Font> mFont;
104 Context *mRsc;
105 };
106
Jason Samsf166d9b2010-09-30 18:15:52 -0700107 ScriptTLSStruct *mTlsStruct;
Jason Sams11c8af92010-10-13 15:31:10 -0700108 RsSurfaceConfig mUserSurfaceConfig;
Jason Sams462d11b2009-06-19 16:03:18 -0700109
Jason Samsd19f10d2009-05-22 14:03:28 -0700110 ElementState mStateElement;
111 TypeState mStateType;
112 SamplerState mStateSampler;
113 ProgramFragmentState mStateFragment;
Jason Sams54db59c2010-05-13 18:30:11 -0700114 ProgramStoreState mStateFragmentStore;
Jason Samsebfb4362009-09-23 13:57:02 -0700115 ProgramRasterState mStateRaster;
Jason Samsd19f10d2009-05-22 14:03:28 -0700116 ProgramVertexState mStateVertex;
Jason Samsbb51c402009-11-25 13:22:07 -0800117 VertexArrayState mStateVertexArray;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700118 FontState mStateFont;
Jason Samsd19f10d2009-05-22 14:03:28 -0700119
Jason Samsd19f10d2009-05-22 14:03:28 -0700120 ScriptCState mScriptC;
Jason Samsbb51c402009-11-25 13:22:07 -0800121 ShaderCache mShaderCache;
Jason Samsd19f10d2009-05-22 14:03:28 -0700122
Jason Samsd19f10d2009-05-22 14:03:28 -0700123 void swapBuffers();
124 void setRootScript(Script *);
Jason Samsa17af042010-11-17 15:29:32 -0800125 void setProgramRaster(ProgramRaster *);
126 void setProgramVertex(ProgramVertex *);
127 void setProgramFragment(ProgramFragment *);
128 void setProgramStore(ProgramStore *);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700129 void setFont(Font *);
Jason Samsd19f10d2009-05-22 14:03:28 -0700130
131 void updateSurface(void *sur);
132
Jason Samsa17af042010-11-17 15:29:32 -0800133 ProgramFragment * getProgramFragment() {return mFragment.get();}
134 ProgramStore * getProgramStore() {return mFragmentStore.get();}
135 ProgramRaster * getProgramRaster() {return mRaster.get();}
136 ProgramVertex * getProgramVertex() {return mVertex.get();}
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700137 Font * getFont() {return mFont.get();}
Jason Samsd19f10d2009-05-22 14:03:28 -0700138
Jason Sams156cce62010-03-03 13:03:18 -0800139 bool setupCheck();
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700140 void setupProgramStore();
Jason Samsd19f10d2009-05-22 14:03:28 -0700141
Jason Sams65e7aa52009-09-24 17:38:20 -0700142 void pause();
143 void resume();
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700144 void setSurface(uint32_t w, uint32_t h, ANativeWindow *sur);
Jason Sams7d787b42009-11-15 12:14:26 -0800145 void setPriority(int32_t p);
Jason Sams5c68a712010-12-24 14:38:39 -0800146 void destroyWorkerThreadResources();
Jason Sams65e7aa52009-09-24 17:38:20 -0700147
Jason Samsd5680f92009-06-10 18:39:40 -0700148 void assignName(ObjectBase *obj, const char *name, uint32_t len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700149 void removeName(ObjectBase *obj);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700150
Jason Sams1c415172010-11-08 17:06:46 -0800151 RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID, bool wait);
152 RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait);
Jason Samsadd9d962010-11-22 16:20:16 -0800153 bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const;
Jason Samsf17bccc2010-05-28 18:23:22 -0700154 uint32_t runScript(Script *s);
Jason Sams516c3192009-10-06 13:58:47 -0700155
156 void initToClient();
157 void deinitToClient();
158
Jason Sams9c54bdb2009-06-17 16:52:59 -0700159 ProgramFragment * getDefaultProgramFragment() const {
160 return mStateFragment.mDefault.get();
161 }
162 ProgramVertex * getDefaultProgramVertex() const {
163 return mStateVertex.mDefault.get();
164 }
Jason Sams54db59c2010-05-13 18:30:11 -0700165 ProgramStore * getDefaultProgramStore() const {
Jason Sams9c54bdb2009-06-17 16:52:59 -0700166 return mStateFragmentStore.mDefault.get();
167 }
Jason Samsebfb4362009-09-23 13:57:02 -0700168 ProgramRaster * getDefaultProgramRaster() const {
169 return mStateRaster.mDefault.get();
170 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700171 Font* getDefaultFont() const {
172 return mStateFont.mDefault.get();
173 }
Jason Sams9c54bdb2009-06-17 16:52:59 -0700174
Jason Samsf603d212010-05-14 15:30:29 -0700175 uint32_t getWidth() const {return mWidth;}
176 uint32_t getHeight() const {return mHeight;}
Jason Sams40a29e82009-08-10 14:55:26 -0700177
Jason Samsadd9d962010-11-22 16:20:16 -0800178 mutable ThreadIO mIO;
Jason Samsbc948de2009-08-17 18:35:48 -0700179
Jason Samsf4d16062009-08-19 12:17:14 -0700180 // Timers
181 enum Timers {
182 RS_TIMER_IDLE,
183 RS_TIMER_INTERNAL,
184 RS_TIMER_SCRIPT,
185 RS_TIMER_CLEAR_SWAP,
186 _RS_TIMER_TOTAL
187 };
188 uint64_t getTime() const;
189 void timerInit();
190 void timerReset();
191 void timerSet(Timers);
192 void timerPrint();
Jason Sams2525a812009-09-03 15:43:13 -0700193 void timerFrame();
Jason Samsf4d16062009-08-19 12:17:14 -0700194
Jason Sams66b27712009-09-25 15:25:00 -0700195 struct {
196 bool mLogTimes;
197 bool mLogScripts;
198 bool mLogObjects;
Jason Sams5dad8b42009-12-15 19:10:11 -0800199 bool mLogShaders;
Alex Sakhartchouk4378f112010-09-29 09:49:13 -0700200 bool mLogShadersAttr;
201 bool mLogShadersUniforms;
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700202 bool mLogVisual;
Jason Sams66b27712009-09-25 15:25:00 -0700203 } props;
Joe Onorato9ac2c662009-09-23 16:37:36 -0700204
Jason Sams9dab6672009-11-24 12:26:35 -0800205 void dumpDebug() const;
Jason Samsadd9d962010-11-22 16:20:16 -0800206 void checkError(const char *, bool isFatal = false) const;
207 void setError(RsError e, const char *msg = NULL) const;
Jason Sams9dab6672009-11-24 12:26:35 -0800208
Jason Samsa9e7a052009-09-25 14:51:22 -0700209 mutable const ObjectBase * mObjHead;
210
Jason Sams2978bfc2010-02-22 15:37:51 -0800211 bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;}
Jason Sams9333e642011-01-13 17:02:35 -0800212 bool ext_GL_IMG_texture_npot() const {return mGL.GL_IMG_texture_npot;}
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700213 bool ext_GL_NV_texture_npot_2D_mipmap() const {return mGL.GL_NV_texture_npot_2D_mipmap;}
Alex Sakhartchouk4378f112010-09-29 09:49:13 -0700214 float ext_texture_max_aniso() const {return mGL.EXT_texture_max_aniso; }
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700215 uint32_t getMaxFragmentTextures() const {return mGL.mMaxFragmentTextureImageUnits;}
216 uint32_t getMaxFragmentUniformVectors() const {return mGL.mMaxFragmentUniformVectors;}
217 uint32_t getMaxVertexUniformVectors() const {return mGL.mMaxVertexUniformVectors;}
Alex Sakhartchouk6b5222d2010-12-13 14:48:21 -0800218 uint32_t getMaxVertexAttributes() const {return mGL.mMaxVertexAttribs;}
Jason Sams2978bfc2010-02-22 15:37:51 -0800219
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700220 uint32_t getDPI() const {return mDPI;}
221 void setDPI(uint32_t dpi) {mDPI = dpi;}
Jason Sams8e6c17f2010-07-19 15:38:19 -0700222
Jason Samsd19f10d2009-05-22 14:03:28 -0700223protected:
224 Device *mDev;
225
Jason Samsb13ada52009-08-25 11:34:49 -0700226 struct {
227 EGLint mNumConfigs;
228 EGLint mMajorVersion;
229 EGLint mMinorVersion;
230 EGLConfig mConfig;
231 EGLContext mContext;
232 EGLSurface mSurface;
Jason Sams11c8af92010-10-13 15:31:10 -0700233 EGLSurface mSurfaceDefault;
Jason Samsb13ada52009-08-25 11:34:49 -0700234 EGLDisplay mDisplay;
235 } mEGL;
236
237 struct {
238 const uint8_t * mVendor;
239 const uint8_t * mRenderer;
240 const uint8_t * mVersion;
241 const uint8_t * mExtensions;
242
243 uint32_t mMajorVersion;
244 uint32_t mMinorVersion;
245
Jason Sams0011bcf2009-12-15 12:58:36 -0800246 int32_t mMaxVaryingVectors;
247 int32_t mMaxTextureImageUnits;
248
249 int32_t mMaxFragmentTextureImageUnits;
250 int32_t mMaxFragmentUniformVectors;
251
252 int32_t mMaxVertexAttribs;
253 int32_t mMaxVertexUniformVectors;
254 int32_t mMaxVertexTextureUnits;
Jason Sams2978bfc2010-02-22 15:37:51 -0800255
256 bool OES_texture_npot;
Jason Sams9333e642011-01-13 17:02:35 -0800257 bool GL_IMG_texture_npot;
Alex Sakhartchouk581cc642010-10-27 14:10:07 -0700258 bool GL_NV_texture_npot_2D_mipmap;
Alex Sakhartchouk4378f112010-09-29 09:49:13 -0700259 float EXT_texture_max_aniso;
Jason Samsb13ada52009-08-25 11:34:49 -0700260 } mGL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700261
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700262 uint32_t mDPI;
Jason Sams3bc47d42009-11-12 15:10:25 -0800263 uint32_t mWidth;
264 uint32_t mHeight;
Jason Samsb9d5c572009-12-09 11:05:45 -0800265 int32_t mThreadPriority;
Jason Sams704ff642010-02-09 16:05:07 -0800266 bool mIsGraphicsContext;
Jason Sams3bc47d42009-11-12 15:10:25 -0800267
Jason Samsd19f10d2009-05-22 14:03:28 -0700268 bool mRunning;
269 bool mExit;
Jason Sams65e7aa52009-09-24 17:38:20 -0700270 bool mPaused;
Jason Samsadd9d962010-11-22 16:20:16 -0800271 mutable RsError mError;
Jason Samsd19f10d2009-05-22 14:03:28 -0700272
Jason Samsd19f10d2009-05-22 14:03:28 -0700273 pthread_t mThreadId;
Jason Sams7d787b42009-11-15 12:14:26 -0800274 pid_t mNativeThreadId;
Jason Samsd19f10d2009-05-22 14:03:28 -0700275
276 ObjectBaseRef<Script> mRootScript;
277 ObjectBaseRef<ProgramFragment> mFragment;
278 ObjectBaseRef<ProgramVertex> mVertex;
Jason Sams54db59c2010-05-13 18:30:11 -0700279 ObjectBaseRef<ProgramStore> mFragmentStore;
Jason Samsebfb4362009-09-23 13:57:02 -0700280 ObjectBaseRef<ProgramRaster> mRaster;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700281 ObjectBaseRef<Font> mFont;
Jason Sams730ee652009-08-18 17:07:09 -0700282
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700283 void displayDebugStats();
284
Jason Samsd19f10d2009-05-22 14:03:28 -0700285private:
286 Context();
Jason Samsd5f06302010-11-03 14:27:11 -0700287 bool initContext(Device *, const RsSurfaceConfig *sc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700288
Jason Samsd5f06302010-11-03 14:27:11 -0700289
290 bool initGLThread();
Jason Sams71362202009-10-27 14:44:31 -0700291 void deinitEGL();
Jason Samsd19f10d2009-05-22 14:03:28 -0700292
Jason Samsb9d5c572009-12-09 11:05:45 -0800293 uint32_t runRootScript();
Jason Samsd19f10d2009-05-22 14:03:28 -0700294
295 static void * threadProc(void *);
Jason Sams8e6c17f2010-07-19 15:38:19 -0700296 static void * helperThreadProc(void *);
Jason Samsd19f10d2009-05-22 14:03:28 -0700297
Dianne Hackborn8b49bd12010-06-30 13:56:17 -0700298 ANativeWindow *mWndSurface;
Jason Sams3eaa3382009-06-10 15:04:38 -0700299
300 Vector<ObjectBase *> mNames;
Jason Samsf4d16062009-08-19 12:17:14 -0700301
302 uint64_t mTimers[_RS_TIMER_TOTAL];
303 Timers mTimerActive;
304 uint64_t mTimeLast;
Jason Sams2525a812009-09-03 15:43:13 -0700305 uint64_t mTimeFrame;
306 uint64_t mTimeLastFrame;
Jason Samsb9d5c572009-12-09 11:05:45 -0800307 uint32_t mTimeMSLastFrame;
308 uint32_t mTimeMSLastScript;
309 uint32_t mTimeMSLastSwap;
Alex Sakhartchouk76322af2010-10-05 13:23:55 -0700310 uint32_t mAverageFPSFrameCount;
311 uint64_t mAverageFPSStartTime;
312 uint32_t mAverageFPS;
Jason Samsd19f10d2009-05-22 14:03:28 -0700313};
314
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800315#else
316
317class Context {
318public:
319 Context() {
320 mObjHead = NULL;
321 }
322 ~Context() {
323 ObjectBase::zeroAllUserRef(this);
324 }
325
326 ElementState mStateElement;
327 TypeState mStateType;
328
329 struct {
330 bool mLogTimes;
331 bool mLogScripts;
332 bool mLogObjects;
333 bool mLogShaders;
334 bool mLogShadersAttr;
335 bool mLogShadersUniforms;
336 bool mLogVisual;
337 } props;
338
339 void setError(RsError e, const char *msg = NULL) { }
340
341 mutable const ObjectBase * mObjHead;
342
343protected:
344
345};
346#endif //ANDROID_RS_SERIALIZE
347
348} // renderscript
349} // android
Jason Samsd19f10d2009-05-22 14:03:28 -0700350#endif