blob: be615a3f273e199332987295fb9d9dfac00cff12 [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -07002 * Copyright (C) 2011 The Android Open Source Project
Jason Samsd19f10d2009-05-22 14:03:28 -07003 *
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"
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -070040#include "rsFBOCache.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070041
42#include "rsgApiStructs.h"
43#include "rsLocklessFifo.h"
44
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -080045#endif // ANDROID_RS_SERIALIZE
Jason Samsd19f10d2009-05-22 14:03:28 -070046
47// ---------------------------------------------------------------------------
48namespace android {
Mathias Agopian000479f2010-02-09 17:46:37 -080049
Jason Samsd19f10d2009-05-22 14:03:28 -070050namespace renderscript {
51
Jason Samsf166d9b2010-09-30 18:15:52 -070052#if 0
53#define CHECK_OBJ(o) { \
54 GET_TLS(); \
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080055 if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
Jason Samsf166d9b2010-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 Sakhartchouked9f2102010-11-09 17:00:54 -080061 if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
Jason Samsf166d9b2010-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 Sakhartchoukd0f5bd12011-01-31 14:53:24 -080070#ifndef ANDROID_RS_SERIALIZE
71
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080072class Context {
Jason Samsd19f10d2009-05-22 14:03:28 -070073public:
Jason Samse4a06c52011-03-16 16:29:28 -070074 struct Hal {
75 void * drv;
76
77 RsdHalFunctions funcs;
78 };
79 Hal mHal;
80
Jason Samsd5f06302010-11-03 14:27:11 -070081 static Context * createContext(Device *, const RsSurfaceConfig *sc);
Jason Samsd19f10d2009-05-22 14:03:28 -070082 ~Context();
83
Jason Sams41c19db92009-10-15 16:47:31 -070084 static pthread_mutex_t gInitMutex;
Stephen Hines1ac9da62011-01-07 15:11:30 -080085 // Library mutex (for providing thread-safe calls from the runtime)
86 static pthread_mutex_t gLibMutex;
Jason Sams41c19db92009-10-15 16:47:31 -070087
Jason Samsa17af042010-11-17 15:29:32 -080088 class PushState {
89 public:
90 PushState(Context *);
91 ~PushState();
92
93 private:
94 ObjectBaseRef<ProgramFragment> mFragment;
95 ObjectBaseRef<ProgramVertex> mVertex;
96 ObjectBaseRef<ProgramStore> mStore;
97 ObjectBaseRef<ProgramRaster> mRaster;
98 ObjectBaseRef<Font> mFont;
99 Context *mRsc;
100 };
101
Jason Sams11c8af92010-10-13 15:31:10 -0700102 RsSurfaceConfig mUserSurfaceConfig;
Jason Sams462d11b2009-06-19 16:03:18 -0700103
Jason Samsd19f10d2009-05-22 14:03:28 -0700104 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;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700111 FontState mStateFont;
Jason Samsd19f10d2009-05-22 14:03:28 -0700112
Jason Samsd19f10d2009-05-22 14:03:28 -0700113 ScriptCState mScriptC;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700114 FBOCache mFBOCache;
Jason Samsd19f10d2009-05-22 14:03:28 -0700115
Jason Samsd19f10d2009-05-22 14:03:28 -0700116 void swapBuffers();
117 void setRootScript(Script *);
Jason Samsa17af042010-11-17 15:29:32 -0800118 void setProgramRaster(ProgramRaster *);
119 void setProgramVertex(ProgramVertex *);
120 void setProgramFragment(ProgramFragment *);
121 void setProgramStore(ProgramStore *);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700122 void setFont(Font *);
Jason Samsd19f10d2009-05-22 14:03:28 -0700123
124 void updateSurface(void *sur);
125
Jason Samsa17af042010-11-17 15:29:32 -0800126 ProgramFragment * getProgramFragment() {return mFragment.get();}
127 ProgramStore * getProgramStore() {return mFragmentStore.get();}
128 ProgramRaster * getProgramRaster() {return mRaster.get();}
129 ProgramVertex * getProgramVertex() {return mVertex.get();}
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700130 Font * getFont() {return mFont.get();}
Jason Samsd19f10d2009-05-22 14:03:28 -0700131
Jason Sams156cce62010-03-03 13:03:18 -0800132 bool setupCheck();
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700133 void setupProgramStore();
Jason Samsd19f10d2009-05-22 14:03:28 -0700134
Jason Sams65e7aa52009-09-24 17:38:20 -0700135 void pause();
136 void resume();
Alex Sakhartchouk6c72eec2011-05-17 12:32:47 -0700137 void setSurface(uint32_t w, uint32_t h, RsNativeWindow sur);
Jason Sams7d787b42009-11-15 12:14:26 -0800138 void setPriority(int32_t p);
Jason Sams5c68a712010-12-24 14:38:39 -0800139 void destroyWorkerThreadResources();
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 Samsedbfabd2011-05-17 15:01:29 -0700144 RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID);
145 RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen);
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 setError(RsError e, const char *msg = NULL) const;
Jason Sams9dab6672009-11-24 12:26:35 -0800200
Jason Samsa9e7a052009-09-25 14:51:22 -0700201 mutable const ObjectBase * mObjHead;
202
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700203 uint32_t getDPI() const {return mDPI;}
204 void setDPI(uint32_t dpi) {mDPI = dpi;}
Jason Sams8e6c17f2010-07-19 15:38:19 -0700205
Jason Samsd19f10d2009-05-22 14:03:28 -0700206 Device *mDev;
Jason Sams803626f2011-04-06 17:52:23 -0700207protected:
Jason Samsd19f10d2009-05-22 14:03:28 -0700208
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700209 uint32_t mDPI;
Jason Sams3bc47d42009-11-12 15:10:25 -0800210 uint32_t mWidth;
211 uint32_t mHeight;
Jason Samsb9d5c572009-12-09 11:05:45 -0800212 int32_t mThreadPriority;
Jason Sams704ff642010-02-09 16:05:07 -0800213 bool mIsGraphicsContext;
Jason Sams3bc47d42009-11-12 15:10:25 -0800214
Jason Samsd19f10d2009-05-22 14:03:28 -0700215 bool mRunning;
216 bool mExit;
Jason Sams65e7aa52009-09-24 17:38:20 -0700217 bool mPaused;
Jason Samsadd9d962010-11-22 16:20:16 -0800218 mutable RsError mError;
Jason Samsd19f10d2009-05-22 14:03:28 -0700219
Jason Samsd19f10d2009-05-22 14:03:28 -0700220 pthread_t mThreadId;
Jason Sams7d787b42009-11-15 12:14:26 -0800221 pid_t mNativeThreadId;
Jason Samsd19f10d2009-05-22 14:03:28 -0700222
223 ObjectBaseRef<Script> mRootScript;
224 ObjectBaseRef<ProgramFragment> mFragment;
225 ObjectBaseRef<ProgramVertex> mVertex;
Jason Sams54db59c2010-05-13 18:30:11 -0700226 ObjectBaseRef<ProgramStore> mFragmentStore;
Jason Samsebfb4362009-09-23 13:57:02 -0700227 ObjectBaseRef<ProgramRaster> mRaster;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700228 ObjectBaseRef<Font> mFont;
Jason Sams730ee652009-08-18 17:07:09 -0700229
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700230 void displayDebugStats();
231
Jason Samsd19f10d2009-05-22 14:03:28 -0700232private:
233 Context();
Jason Samsd5f06302010-11-03 14:27:11 -0700234 bool initContext(Device *, const RsSurfaceConfig *sc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700235
Jason Samsd5f06302010-11-03 14:27:11 -0700236
237 bool initGLThread();
Jason Sams71362202009-10-27 14:44:31 -0700238 void deinitEGL();
Jason Samsd19f10d2009-05-22 14:03:28 -0700239
Jason Samsb9d5c572009-12-09 11:05:45 -0800240 uint32_t runRootScript();
Jason Samsd19f10d2009-05-22 14:03:28 -0700241
242 static void * threadProc(void *);
Jason Sams8e6c17f2010-07-19 15:38:19 -0700243 static void * helperThreadProc(void *);
Jason Samsd19f10d2009-05-22 14:03:28 -0700244
Alex Sakhartchouk6c72eec2011-05-17 12:32:47 -0700245 bool mHasSurface;
Jason Sams3eaa3382009-06-10 15:04:38 -0700246
247 Vector<ObjectBase *> mNames;
Jason Samsf4d16062009-08-19 12:17:14 -0700248
249 uint64_t mTimers[_RS_TIMER_TOTAL];
250 Timers mTimerActive;
251 uint64_t mTimeLast;
Jason Sams2525a812009-09-03 15:43:13 -0700252 uint64_t mTimeFrame;
253 uint64_t mTimeLastFrame;
Jason Samsb9d5c572009-12-09 11:05:45 -0800254 uint32_t mTimeMSLastFrame;
255 uint32_t mTimeMSLastScript;
256 uint32_t mTimeMSLastSwap;
Alex Sakhartchouk76322af2010-10-05 13:23:55 -0700257 uint32_t mAverageFPSFrameCount;
258 uint64_t mAverageFPSStartTime;
259 uint32_t mAverageFPS;
Jason Samsd19f10d2009-05-22 14:03:28 -0700260};
261
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800262#else
263
264class Context {
265public:
266 Context() {
267 mObjHead = NULL;
268 }
269 ~Context() {
270 ObjectBase::zeroAllUserRef(this);
271 }
272
273 ElementState mStateElement;
274 TypeState mStateType;
275
276 struct {
277 bool mLogTimes;
278 bool mLogScripts;
279 bool mLogObjects;
280 bool mLogShaders;
281 bool mLogShadersAttr;
282 bool mLogShadersUniforms;
283 bool mLogVisual;
284 } props;
285
286 void setError(RsError e, const char *msg = NULL) { }
287
288 mutable const ObjectBase * mObjHead;
289
290protected:
291
292};
293#endif //ANDROID_RS_SERIALIZE
294
295} // renderscript
296} // android
Jason Samsd19f10d2009-05-22 14:03:28 -0700297#endif