blob: 309fe9532bbac3dc6a600434003eaf1b60bd5bd8 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -07002 * Copyright (C) 2011 The Android Open Source Project
Jason Sams326e0dd2009-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 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
Jason Samsbad80742011-03-16 16:29:28 -070025#include "rs_hal.h"
26
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080027#include "rsMutex.h"
28#include "rsThreadIO.h"
Jason Sams87fe59a2011-04-20 15:09:01 -070029#include "rsMatrix4x4.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070030#include "rsDevice.h"
31#include "rsScriptC.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070032#include "rsAdapter.h"
33#include "rsSampler.h"
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -070034#include "rsFont.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070035#include "rsProgramFragment.h"
Jason Samsccc010b2010-05-13 18:30:11 -070036#include "rsProgramStore.h"
Jason Sams5fd09d82009-09-23 13:57:02 -070037#include "rsProgramRaster.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070038#include "rsProgramVertex.h"
Alex Sakhartchouk7d9c5ff2011-04-01 14:19:01 -070039#include "rsFBOCache.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070040
41#include "rsgApiStructs.h"
42#include "rsLocklessFifo.h"
43
Jason Sams326e0dd2009-05-22 14:03:28 -070044// ---------------------------------------------------------------------------
45namespace android {
Mathias Agopiand4c25e32010-02-09 17:46:37 -080046
Jason Sams326e0dd2009-05-22 14:03:28 -070047namespace renderscript {
48
Jason Sams605048a2010-09-30 18:15:52 -070049#if 0
50#define CHECK_OBJ(o) { \
51 GET_TLS(); \
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080052 if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
Jason Sams605048a2010-09-30 18:15:52 -070053 LOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \
54 } \
55}
56#define CHECK_OBJ_OR_NULL(o) { \
57 GET_TLS(); \
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080058 if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
Jason Sams605048a2010-09-30 18:15:52 -070059 LOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \
60 } \
61}
62#else
63#define CHECK_OBJ(o)
64#define CHECK_OBJ_OR_NULL(o)
65#endif
66
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080067class Context {
Jason Sams326e0dd2009-05-22 14:03:28 -070068public:
Jason Samsbad80742011-03-16 16:29:28 -070069 struct Hal {
70 void * drv;
71
72 RsdHalFunctions funcs;
73 };
74 Hal mHal;
75
Jason Sams5c1c79a2010-11-03 14:27:11 -070076 static Context * createContext(Device *, const RsSurfaceConfig *sc);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -070077 static Context * createContextLite();
Jason Sams326e0dd2009-05-22 14:03:28 -070078 ~Context();
79
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 Sams60709252010-11-17 15:29:32 -080084 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 Sams6b8552a2010-10-13 15:31:10 -070098 RsSurfaceConfig mUserSurfaceConfig;
Jason Samse5769102009-06-19 16:03:18 -070099
Jason Sams326e0dd2009-05-22 14:03:28 -0700100 ElementState mStateElement;
101 TypeState mStateType;
102 SamplerState mStateSampler;
103 ProgramFragmentState mStateFragment;
Jason Samsccc010b2010-05-13 18:30:11 -0700104 ProgramStoreState mStateFragmentStore;
Jason Sams5fd09d82009-09-23 13:57:02 -0700105 ProgramRasterState mStateRaster;
Jason Sams326e0dd2009-05-22 14:03:28 -0700106 ProgramVertexState mStateVertex;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700107 FontState mStateFont;
Jason Sams326e0dd2009-05-22 14:03:28 -0700108
Jason Sams326e0dd2009-05-22 14:03:28 -0700109 ScriptCState mScriptC;
Alex Sakhartchouk7d9c5ff2011-04-01 14:19:01 -0700110 FBOCache mFBOCache;
Jason Sams326e0dd2009-05-22 14:03:28 -0700111
Jason Sams326e0dd2009-05-22 14:03:28 -0700112 void swapBuffers();
113 void setRootScript(Script *);
Jason Sams60709252010-11-17 15:29:32 -0800114 void setProgramRaster(ProgramRaster *);
115 void setProgramVertex(ProgramVertex *);
116 void setProgramFragment(ProgramFragment *);
117 void setProgramStore(ProgramStore *);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700118 void setFont(Font *);
Jason Sams326e0dd2009-05-22 14:03:28 -0700119
120 void updateSurface(void *sur);
121
Jason Sams60709252010-11-17 15:29:32 -0800122 ProgramFragment * getProgramFragment() {return mFragment.get();}
123 ProgramStore * getProgramStore() {return mFragmentStore.get();}
124 ProgramRaster * getProgramRaster() {return mRaster.get();}
125 ProgramVertex * getProgramVertex() {return mVertex.get();}
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700126 Font * getFont() {return mFont.get();}
Jason Sams326e0dd2009-05-22 14:03:28 -0700127
Jason Samsa2cf7552010-03-03 13:03:18 -0800128 bool setupCheck();
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700129 void setupProgramStore();
Jason Sams326e0dd2009-05-22 14:03:28 -0700130
Jason Sams86f1b232009-09-24 17:38:20 -0700131 void pause();
132 void resume();
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700133 void setSurface(uint32_t w, uint32_t h, RsNativeWindow sur);
Jason Sams15832442009-11-15 12:14:26 -0800134 void setPriority(int32_t p);
Jason Sams741aac92010-12-24 14:38:39 -0800135 void destroyWorkerThreadResources();
Jason Sams86f1b232009-09-24 17:38:20 -0700136
Jason Samsa4a54e42009-06-10 18:39:40 -0700137 void assignName(ObjectBase *obj, const char *name, uint32_t len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700138 void removeName(ObjectBase *obj);
Jason Sams8ce125b2009-06-17 16:52:59 -0700139
Jason Sams1a4efa32011-05-17 15:01:29 -0700140 RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID);
141 RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen);
Jason Sams87319de2010-11-22 16:20:16 -0800142 bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const;
Jason Samsc61346b2010-05-28 18:23:22 -0700143 uint32_t runScript(Script *s);
Jason Sams8c401ef2009-10-06 13:58:47 -0700144
145 void initToClient();
146 void deinitToClient();
147
Jason Sams8ce125b2009-06-17 16:52:59 -0700148 ProgramFragment * getDefaultProgramFragment() const {
149 return mStateFragment.mDefault.get();
150 }
151 ProgramVertex * getDefaultProgramVertex() const {
152 return mStateVertex.mDefault.get();
153 }
Jason Samsccc010b2010-05-13 18:30:11 -0700154 ProgramStore * getDefaultProgramStore() const {
Jason Sams8ce125b2009-06-17 16:52:59 -0700155 return mStateFragmentStore.mDefault.get();
156 }
Jason Sams5fd09d82009-09-23 13:57:02 -0700157 ProgramRaster * getDefaultProgramRaster() const {
158 return mStateRaster.mDefault.get();
159 }
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700160 Font* getDefaultFont() const {
161 return mStateFont.mDefault.get();
162 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700163
Jason Sams771565f2010-05-14 15:30:29 -0700164 uint32_t getWidth() const {return mWidth;}
165 uint32_t getHeight() const {return mHeight;}
Jason Samse579df42009-08-10 14:55:26 -0700166
Jason Sams87319de2010-11-22 16:20:16 -0800167 mutable ThreadIO mIO;
Jason Samsfcd31922009-08-17 18:35:48 -0700168
Jason Sams24371d92009-08-19 12:17:14 -0700169 // Timers
170 enum Timers {
171 RS_TIMER_IDLE,
172 RS_TIMER_INTERNAL,
173 RS_TIMER_SCRIPT,
174 RS_TIMER_CLEAR_SWAP,
175 _RS_TIMER_TOTAL
176 };
177 uint64_t getTime() const;
178 void timerInit();
179 void timerReset();
180 void timerSet(Timers);
181 void timerPrint();
Jason Sams1d54f102009-09-03 15:43:13 -0700182 void timerFrame();
Jason Sams24371d92009-08-19 12:17:14 -0700183
Jason Sams1fddd902009-09-25 15:25:00 -0700184 struct {
185 bool mLogTimes;
186 bool mLogScripts;
187 bool mLogObjects;
Jason Samscd506532009-12-15 19:10:11 -0800188 bool mLogShaders;
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700189 bool mLogShadersAttr;
190 bool mLogShadersUniforms;
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700191 bool mLogVisual;
Jason Sams1fddd902009-09-25 15:25:00 -0700192 } props;
Joe Onorato76371ff2009-09-23 16:37:36 -0700193
Jason Sams13e26342009-11-24 12:26:35 -0800194 void dumpDebug() const;
Jason Sams87319de2010-11-22 16:20:16 -0800195 void setError(RsError e, const char *msg = NULL) const;
Jason Sams13e26342009-11-24 12:26:35 -0800196
Jason Samse514b452009-09-25 14:51:22 -0700197 mutable const ObjectBase * mObjHead;
198
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700199 uint32_t getDPI() const {return mDPI;}
200 void setDPI(uint32_t dpi) {mDPI = dpi;}
Jason Sams7bf29dd2010-07-19 15:38:19 -0700201
Jason Sams326e0dd2009-05-22 14:03:28 -0700202 Device *mDev;
Jason Sams4b3de472011-04-06 17:52:23 -0700203protected:
Jason Sams326e0dd2009-05-22 14:03:28 -0700204
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700205 uint32_t mDPI;
Jason Sams613cad12009-11-12 15:10:25 -0800206 uint32_t mWidth;
207 uint32_t mHeight;
Jason Sams2dca84d2009-12-09 11:05:45 -0800208 int32_t mThreadPriority;
Jason Sams4820e8b2010-02-09 16:05:07 -0800209 bool mIsGraphicsContext;
Jason Sams613cad12009-11-12 15:10:25 -0800210
Jason Sams326e0dd2009-05-22 14:03:28 -0700211 bool mRunning;
212 bool mExit;
Jason Sams86f1b232009-09-24 17:38:20 -0700213 bool mPaused;
Jason Sams87319de2010-11-22 16:20:16 -0800214 mutable RsError mError;
Jason Sams326e0dd2009-05-22 14:03:28 -0700215
Jason Sams326e0dd2009-05-22 14:03:28 -0700216 pthread_t mThreadId;
Jason Sams15832442009-11-15 12:14:26 -0800217 pid_t mNativeThreadId;
Jason Sams326e0dd2009-05-22 14:03:28 -0700218
219 ObjectBaseRef<Script> mRootScript;
220 ObjectBaseRef<ProgramFragment> mFragment;
221 ObjectBaseRef<ProgramVertex> mVertex;
Jason Samsccc010b2010-05-13 18:30:11 -0700222 ObjectBaseRef<ProgramStore> mFragmentStore;
Jason Sams5fd09d82009-09-23 13:57:02 -0700223 ObjectBaseRef<ProgramRaster> mRaster;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700224 ObjectBaseRef<Font> mFont;
Jason Sams50869382009-08-18 17:07:09 -0700225
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700226 void displayDebugStats();
227
Jason Sams326e0dd2009-05-22 14:03:28 -0700228private:
229 Context();
Jason Sams5c1c79a2010-11-03 14:27:11 -0700230 bool initContext(Device *, const RsSurfaceConfig *sc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700231
Jason Sams5c1c79a2010-11-03 14:27:11 -0700232
233 bool initGLThread();
Jason Sams33b6e3b2009-10-27 14:44:31 -0700234 void deinitEGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700235
Jason Sams2dca84d2009-12-09 11:05:45 -0800236 uint32_t runRootScript();
Jason Sams326e0dd2009-05-22 14:03:28 -0700237
238 static void * threadProc(void *);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700239 static void * helperThreadProc(void *);
Jason Sams326e0dd2009-05-22 14:03:28 -0700240
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700241 bool mHasSurface;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700242 bool mIsContextLite;
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700243
244 Vector<ObjectBase *> mNames;
Jason Sams24371d92009-08-19 12:17:14 -0700245
246 uint64_t mTimers[_RS_TIMER_TOTAL];
247 Timers mTimerActive;
248 uint64_t mTimeLast;
Jason Sams1d54f102009-09-03 15:43:13 -0700249 uint64_t mTimeFrame;
250 uint64_t mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800251 uint32_t mTimeMSLastFrame;
252 uint32_t mTimeMSLastScript;
253 uint32_t mTimeMSLastSwap;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700254 uint32_t mAverageFPSFrameCount;
255 uint64_t mAverageFPSStartTime;
256 uint32_t mAverageFPS;
Jason Sams326e0dd2009-05-22 14:03:28 -0700257};
258
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800259} // renderscript
260} // android
Jason Sams326e0dd2009-05-22 14:03:28 -0700261#endif