blob: c5e32a67dd1311eefa23dfd298d5e98cb96b30c8 [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
25#ifndef ANDROID_RS_SERIALIZE
26#include "rsMutex.h"
27#include "rsThreadIO.h"
28#include "rsMatrix.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070029#include "rsDevice.h"
30#include "rsScriptC.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070031#include "rsAdapter.h"
32#include "rsSampler.h"
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -070033#include "rsFont.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070034#include "rsProgramFragment.h"
Jason Samsccc010b2010-05-13 18:30:11 -070035#include "rsProgramStore.h"
Jason Sams5fd09d82009-09-23 13:57:02 -070036#include "rsProgramRaster.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070037#include "rsProgramVertex.h"
Jason Samsc460e552009-11-25 13:22:07 -080038#include "rsShaderCache.h"
39#include "rsVertexArray.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070040
41#include "rsgApiStructs.h"
42#include "rsLocklessFifo.h"
43
Mathias Agopian9b97c292010-02-12 12:00:38 -080044#include <ui/egl/android_natives.h>
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080045#endif // ANDROID_RS_SERIALIZE
Jason Sams326e0dd2009-05-22 14:03:28 -070046
47// ---------------------------------------------------------------------------
48namespace android {
Mathias Agopiand4c25e32010-02-09 17:46:37 -080049
Jason Sams326e0dd2009-05-22 14:03:28 -070050namespace renderscript {
51
Jason Sams605048a2010-09-30 18:15:52 -070052#if 0
53#define CHECK_OBJ(o) { \
54 GET_TLS(); \
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080055 if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
Jason Sams605048a2010-09-30 18:15:52 -070056 LOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \
57 } \
58}
59#define CHECK_OBJ_OR_NULL(o) { \
60 GET_TLS(); \
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080061 if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
Jason Sams605048a2010-09-30 18:15:52 -070062 LOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \
63 } \
64}
65#else
66#define CHECK_OBJ(o)
67#define CHECK_OBJ_OR_NULL(o)
68#endif
69
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080070#ifndef ANDROID_RS_SERIALIZE
71
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080072class Context {
Jason Sams326e0dd2009-05-22 14:03:28 -070073public:
Jason Sams5c1c79a2010-11-03 14:27:11 -070074 static Context * createContext(Device *, const RsSurfaceConfig *sc);
Jason Sams326e0dd2009-05-22 14:03:28 -070075 ~Context();
76
Jason Samse5769102009-06-19 16:03:18 -070077 static pthread_key_t gThreadTLSKey;
Jason Samsfb03a222009-10-15 16:47:31 -070078 static uint32_t gThreadTLSKeyCount;
Jason Sams33b6e3b2009-10-27 14:44:31 -070079 static uint32_t gGLContextCount;
Jason Samsfb03a222009-10-15 16:47:31 -070080 static pthread_mutex_t gInitMutex;
Stephen Hinesca3f09c2011-01-07 15:11:30 -080081 // Library mutex (for providing thread-safe calls from the runtime)
82 static pthread_mutex_t gLibMutex;
Jason Samsfb03a222009-10-15 16:47:31 -070083
Jason Samse5769102009-06-19 16:03:18 -070084 struct ScriptTLSStruct {
85 Context * mContext;
86 Script * mScript;
87 };
Jason Sams60709252010-11-17 15:29:32 -080088
89 class PushState {
90 public:
91 PushState(Context *);
92 ~PushState();
93
94 private:
95 ObjectBaseRef<ProgramFragment> mFragment;
96 ObjectBaseRef<ProgramVertex> mVertex;
97 ObjectBaseRef<ProgramStore> mStore;
98 ObjectBaseRef<ProgramRaster> mRaster;
99 ObjectBaseRef<Font> mFont;
100 Context *mRsc;
101 };
102
Jason Sams605048a2010-09-30 18:15:52 -0700103 ScriptTLSStruct *mTlsStruct;
Jason Sams6b8552a2010-10-13 15:31:10 -0700104 RsSurfaceConfig mUserSurfaceConfig;
Jason Samse5769102009-06-19 16:03:18 -0700105
Jason Sams7bf29dd2010-07-19 15:38:19 -0700106 typedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
Jason Sams326e0dd2009-05-22 14:03:28 -0700107
108 //StructuredAllocationContext mStateAllocation;
109 ElementState mStateElement;
110 TypeState mStateType;
111 SamplerState mStateSampler;
112 ProgramFragmentState mStateFragment;
Jason Samsccc010b2010-05-13 18:30:11 -0700113 ProgramStoreState mStateFragmentStore;
Jason Sams5fd09d82009-09-23 13:57:02 -0700114 ProgramRasterState mStateRaster;
Jason Sams326e0dd2009-05-22 14:03:28 -0700115 ProgramVertexState mStateVertex;
Jason Samsc460e552009-11-25 13:22:07 -0800116 VertexArrayState mStateVertexArray;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700117 FontState mStateFont;
Jason Sams326e0dd2009-05-22 14:03:28 -0700118
Jason Sams326e0dd2009-05-22 14:03:28 -0700119 ScriptCState mScriptC;
Jason Samsc460e552009-11-25 13:22:07 -0800120 ShaderCache mShaderCache;
Jason Sams326e0dd2009-05-22 14:03:28 -0700121
Jason Sams326e0dd2009-05-22 14:03:28 -0700122 void swapBuffers();
123 void setRootScript(Script *);
Jason Sams60709252010-11-17 15:29:32 -0800124 void setProgramRaster(ProgramRaster *);
125 void setProgramVertex(ProgramVertex *);
126 void setProgramFragment(ProgramFragment *);
127 void setProgramStore(ProgramStore *);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700128 void setFont(Font *);
Jason Sams326e0dd2009-05-22 14:03:28 -0700129
130 void updateSurface(void *sur);
131
Jason Sams60709252010-11-17 15:29:32 -0800132 ProgramFragment * getProgramFragment() {return mFragment.get();}
133 ProgramStore * getProgramStore() {return mFragmentStore.get();}
134 ProgramRaster * getProgramRaster() {return mRaster.get();}
135 ProgramVertex * getProgramVertex() {return mVertex.get();}
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700136 Font * getFont() {return mFont.get();}
Jason Sams326e0dd2009-05-22 14:03:28 -0700137
Jason Samsa2cf7552010-03-03 13:03:18 -0800138 bool setupCheck();
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700139 void setupProgramStore();
Jason Sams326e0dd2009-05-22 14:03:28 -0700140
Jason Sams86f1b232009-09-24 17:38:20 -0700141 void pause();
142 void resume();
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700143 void setSurface(uint32_t w, uint32_t h, ANativeWindow *sur);
Jason Sams15832442009-11-15 12:14:26 -0800144 void setPriority(int32_t p);
Jason Sams741aac92010-12-24 14:38:39 -0800145 void destroyWorkerThreadResources();
Jason Sams86f1b232009-09-24 17:38:20 -0700146
Jason Samsa4a54e42009-06-10 18:39:40 -0700147 void assignName(ObjectBase *obj, const char *name, uint32_t len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700148 void removeName(ObjectBase *obj);
Jason Sams8ce125b2009-06-17 16:52:59 -0700149
Jason Samsaad4bc52010-11-08 17:06:46 -0800150 RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID, bool wait);
151 RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen, bool wait);
Jason Sams87319de2010-11-22 16:20:16 -0800152 bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const;
Jason Samsc61346b2010-05-28 18:23:22 -0700153 uint32_t runScript(Script *s);
Jason Sams8c401ef2009-10-06 13:58:47 -0700154
155 void initToClient();
156 void deinitToClient();
157
Jason Sams8ce125b2009-06-17 16:52:59 -0700158 ProgramFragment * getDefaultProgramFragment() const {
159 return mStateFragment.mDefault.get();
160 }
161 ProgramVertex * getDefaultProgramVertex() const {
162 return mStateVertex.mDefault.get();
163 }
Jason Samsccc010b2010-05-13 18:30:11 -0700164 ProgramStore * getDefaultProgramStore() const {
Jason Sams8ce125b2009-06-17 16:52:59 -0700165 return mStateFragmentStore.mDefault.get();
166 }
Jason Sams5fd09d82009-09-23 13:57:02 -0700167 ProgramRaster * getDefaultProgramRaster() const {
168 return mStateRaster.mDefault.get();
169 }
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700170 Font* getDefaultFont() const {
171 return mStateFont.mDefault.get();
172 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700173
Jason Sams771565f2010-05-14 15:30:29 -0700174 uint32_t getWidth() const {return mWidth;}
175 uint32_t getHeight() const {return mHeight;}
Jason Samse579df42009-08-10 14:55:26 -0700176
Jason Sams87319de2010-11-22 16:20:16 -0800177 mutable ThreadIO mIO;
Jason Samsfcd31922009-08-17 18:35:48 -0700178
Jason Sams24371d92009-08-19 12:17:14 -0700179 // Timers
180 enum Timers {
181 RS_TIMER_IDLE,
182 RS_TIMER_INTERNAL,
183 RS_TIMER_SCRIPT,
184 RS_TIMER_CLEAR_SWAP,
185 _RS_TIMER_TOTAL
186 };
187 uint64_t getTime() const;
188 void timerInit();
189 void timerReset();
190 void timerSet(Timers);
191 void timerPrint();
Jason Sams1d54f102009-09-03 15:43:13 -0700192 void timerFrame();
Jason Sams24371d92009-08-19 12:17:14 -0700193
Jason Sams1fddd902009-09-25 15:25:00 -0700194 struct {
195 bool mLogTimes;
196 bool mLogScripts;
197 bool mLogObjects;
Jason Samscd506532009-12-15 19:10:11 -0800198 bool mLogShaders;
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700199 bool mLogShadersAttr;
200 bool mLogShadersUniforms;
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700201 bool mLogVisual;
Jason Sams1fddd902009-09-25 15:25:00 -0700202 } props;
Joe Onorato76371ff2009-09-23 16:37:36 -0700203
Jason Sams13e26342009-11-24 12:26:35 -0800204 void dumpDebug() const;
Jason Sams87319de2010-11-22 16:20:16 -0800205 void checkError(const char *, bool isFatal = false) const;
206 void setError(RsError e, const char *msg = NULL) const;
Jason Sams13e26342009-11-24 12:26:35 -0800207
Jason Samse514b452009-09-25 14:51:22 -0700208 mutable const ObjectBase * mObjHead;
209
Jason Samsef21edc2010-02-22 15:37:51 -0800210 bool ext_OES_texture_npot() const {return mGL.OES_texture_npot;}
Jason Sams0f7785c2011-01-13 17:02:35 -0800211 bool ext_GL_IMG_texture_npot() const {return mGL.GL_IMG_texture_npot;}
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700212 bool ext_GL_NV_texture_npot_2D_mipmap() const {return mGL.GL_NV_texture_npot_2D_mipmap;}
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700213 float ext_texture_max_aniso() const {return mGL.EXT_texture_max_aniso; }
Alex Sakhartchouk383e5b12010-09-23 16:16:33 -0700214 uint32_t getMaxFragmentTextures() const {return mGL.mMaxFragmentTextureImageUnits;}
215 uint32_t getMaxFragmentUniformVectors() const {return mGL.mMaxFragmentUniformVectors;}
216 uint32_t getMaxVertexUniformVectors() const {return mGL.mMaxVertexUniformVectors;}
Alex Sakhartchouk2d791972010-12-13 14:48:21 -0800217 uint32_t getMaxVertexAttributes() const {return mGL.mMaxVertexAttribs;}
Jason Samsef21edc2010-02-22 15:37:51 -0800218
Jason Sams7bf29dd2010-07-19 15:38:19 -0700219 void launchThreads(WorkerCallback_t cbk, void *data);
Jason Sams8d957fa2010-09-28 14:41:22 -0700220 uint32_t getWorkerPoolSize() const {return (uint32_t)mWorkers.mCount;}
Jason Sams7bf29dd2010-07-19 15:38:19 -0700221
Jason Sams326e0dd2009-05-22 14:03:28 -0700222protected:
223 Device *mDev;
224
Jason Samsafcb25c2009-08-25 11:34:49 -0700225 struct {
226 EGLint mNumConfigs;
227 EGLint mMajorVersion;
228 EGLint mMinorVersion;
229 EGLConfig mConfig;
230 EGLContext mContext;
231 EGLSurface mSurface;
Jason Sams6b8552a2010-10-13 15:31:10 -0700232 EGLSurface mSurfaceDefault;
Jason Samsafcb25c2009-08-25 11:34:49 -0700233 EGLDisplay mDisplay;
234 } mEGL;
235
236 struct {
237 const uint8_t * mVendor;
238 const uint8_t * mRenderer;
239 const uint8_t * mVersion;
240 const uint8_t * mExtensions;
241
242 uint32_t mMajorVersion;
243 uint32_t mMinorVersion;
244
Jason Sams4815c0d2009-12-15 12:58:36 -0800245 int32_t mMaxVaryingVectors;
246 int32_t mMaxTextureImageUnits;
247
248 int32_t mMaxFragmentTextureImageUnits;
249 int32_t mMaxFragmentUniformVectors;
250
251 int32_t mMaxVertexAttribs;
252 int32_t mMaxVertexUniformVectors;
253 int32_t mMaxVertexTextureUnits;
Jason Samsef21edc2010-02-22 15:37:51 -0800254
255 bool OES_texture_npot;
Jason Sams0f7785c2011-01-13 17:02:35 -0800256 bool GL_IMG_texture_npot;
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -0700257 bool GL_NV_texture_npot_2D_mipmap;
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700258 float EXT_texture_max_aniso;
Jason Samsafcb25c2009-08-25 11:34:49 -0700259 } mGL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700260
Jason Sams613cad12009-11-12 15:10:25 -0800261 uint32_t mWidth;
262 uint32_t mHeight;
Jason Sams2dca84d2009-12-09 11:05:45 -0800263 int32_t mThreadPriority;
Jason Sams4820e8b2010-02-09 16:05:07 -0800264 bool mIsGraphicsContext;
Jason Sams613cad12009-11-12 15:10:25 -0800265
Jason Sams326e0dd2009-05-22 14:03:28 -0700266 bool mRunning;
267 bool mExit;
Jason Sams86f1b232009-09-24 17:38:20 -0700268 bool mPaused;
Jason Sams87319de2010-11-22 16:20:16 -0800269 mutable RsError mError;
Jason Sams326e0dd2009-05-22 14:03:28 -0700270
Jason Sams326e0dd2009-05-22 14:03:28 -0700271 pthread_t mThreadId;
Jason Sams15832442009-11-15 12:14:26 -0800272 pid_t mNativeThreadId;
Jason Sams326e0dd2009-05-22 14:03:28 -0700273
Jason Sams7bf29dd2010-07-19 15:38:19 -0700274 struct Workers {
275 volatile int mRunningCount;
276 volatile int mLaunchCount;
277 uint32_t mCount;
278 pthread_t *mThreadId;
279 pid_t *mNativeThreadId;
280 Signal mCompleteSignal;
281
282 Signal *mLaunchSignals;
283 WorkerCallback_t mLaunchCallback;
284 void *mLaunchData;
285 };
286 Workers mWorkers;
287
Jason Sams326e0dd2009-05-22 14:03:28 -0700288 ObjectBaseRef<Script> mRootScript;
289 ObjectBaseRef<ProgramFragment> mFragment;
290 ObjectBaseRef<ProgramVertex> mVertex;
Jason Samsccc010b2010-05-13 18:30:11 -0700291 ObjectBaseRef<ProgramStore> mFragmentStore;
Jason Sams5fd09d82009-09-23 13:57:02 -0700292 ObjectBaseRef<ProgramRaster> mRaster;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700293 ObjectBaseRef<Font> mFont;
Jason Sams50869382009-08-18 17:07:09 -0700294
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700295 void displayDebugStats();
296
Jason Sams326e0dd2009-05-22 14:03:28 -0700297private:
298 Context();
Jason Sams5c1c79a2010-11-03 14:27:11 -0700299 bool initContext(Device *, const RsSurfaceConfig *sc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700300
Jason Sams5c1c79a2010-11-03 14:27:11 -0700301
302 bool initGLThread();
Jason Sams33b6e3b2009-10-27 14:44:31 -0700303 void deinitEGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700304
Jason Sams2dca84d2009-12-09 11:05:45 -0800305 uint32_t runRootScript();
Jason Sams326e0dd2009-05-22 14:03:28 -0700306
307 static void * threadProc(void *);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700308 static void * helperThreadProc(void *);
Jason Sams326e0dd2009-05-22 14:03:28 -0700309
Dianne Hackborn1c769c32010-06-30 13:56:17 -0700310 ANativeWindow *mWndSurface;
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700311
312 Vector<ObjectBase *> mNames;
Jason Sams24371d92009-08-19 12:17:14 -0700313
314 uint64_t mTimers[_RS_TIMER_TOTAL];
315 Timers mTimerActive;
316 uint64_t mTimeLast;
Jason Sams1d54f102009-09-03 15:43:13 -0700317 uint64_t mTimeFrame;
318 uint64_t mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800319 uint32_t mTimeMSLastFrame;
320 uint32_t mTimeMSLastScript;
321 uint32_t mTimeMSLastSwap;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700322 uint32_t mAverageFPSFrameCount;
323 uint64_t mAverageFPSStartTime;
324 uint32_t mAverageFPS;
Jason Sams326e0dd2009-05-22 14:03:28 -0700325};
326
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800327#else
328
329class Context {
330public:
331 Context() {
332 mObjHead = NULL;
333 }
334 ~Context() {
335 ObjectBase::zeroAllUserRef(this);
336 }
337
338 ElementState mStateElement;
339 TypeState mStateType;
340
341 struct {
342 bool mLogTimes;
343 bool mLogScripts;
344 bool mLogObjects;
345 bool mLogShaders;
346 bool mLogShadersAttr;
347 bool mLogShadersUniforms;
348 bool mLogVisual;
349 } props;
350
351 void setError(RsError e, const char *msg = NULL) { }
352
353 mutable const ObjectBase * mObjHead;
354
355protected:
356
357};
358#endif //ANDROID_RS_SERIALIZE
359
360} // renderscript
361} // android
Jason Sams326e0dd2009-05-22 14:03:28 -0700362#endif