blob: de110c6abb510d47b0523f6affe0f5d4a2039fa3 [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 Samse4a06c52011-03-16 16:29:28 -070021#include "rs_hal.h"
22
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -080023#include "rsThreadIO.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070024#include "rsScriptC.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070025#include "rsSampler.h"
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070026#include "rsFont.h"
Jason Samsf15ed012011-10-31 13:23:43 -070027#include "rsPath.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070028#include "rsProgramFragment.h"
Jason Sams54db59c2010-05-13 18:30:11 -070029#include "rsProgramStore.h"
Jason Samsebfb4362009-09-23 13:57:02 -070030#include "rsProgramRaster.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070031#include "rsProgramVertex.h"
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -070032#include "rsFBOCache.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070033
Jason Samsd19f10d2009-05-22 14:03:28 -070034// ---------------------------------------------------------------------------
35namespace android {
Mathias Agopian000479f2010-02-09 17:46:37 -080036
Jason Samsd19f10d2009-05-22 14:03:28 -070037namespace renderscript {
38
Alex Sakhartchoukbedc0232012-03-09 10:47:27 -080039class Device;
40
Jason Samsf166d9b2010-09-30 18:15:52 -070041#if 0
42#define CHECK_OBJ(o) { \
43 GET_TLS(); \
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080044 if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
Steve Block3762c312012-01-06 19:20:56 +000045 ALOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \
Jason Samsf166d9b2010-09-30 18:15:52 -070046 } \
47}
48#define CHECK_OBJ_OR_NULL(o) { \
49 GET_TLS(); \
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080050 if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
Steve Block3762c312012-01-06 19:20:56 +000051 ALOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \
Jason Samsf166d9b2010-09-30 18:15:52 -070052 } \
53}
54#else
55#define CHECK_OBJ(o)
56#define CHECK_OBJ_OR_NULL(o)
57#endif
58
Alex Sakhartchouked9f2102010-11-09 17:00:54 -080059class Context {
Jason Samsd19f10d2009-05-22 14:03:28 -070060public:
Jason Samse4a06c52011-03-16 16:29:28 -070061 struct Hal {
62 void * drv;
63
64 RsdHalFunctions funcs;
65 };
66 Hal mHal;
67
Jason Samsd5f06302010-11-03 14:27:11 -070068 static Context * createContext(Device *, const RsSurfaceConfig *sc);
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -070069 static Context * createContextLite();
Jason Samsd19f10d2009-05-22 14:03:28 -070070 ~Context();
71
Jason Sams41c19db92009-10-15 16:47:31 -070072 static pthread_mutex_t gInitMutex;
Stephen Hines1ac9da62011-01-07 15:11:30 -080073 // Library mutex (for providing thread-safe calls from the runtime)
74 static pthread_mutex_t gLibMutex;
Jason Sams41c19db92009-10-15 16:47:31 -070075
Jason Samsa17af042010-11-17 15:29:32 -080076 class PushState {
77 public:
78 PushState(Context *);
79 ~PushState();
80
81 private:
82 ObjectBaseRef<ProgramFragment> mFragment;
83 ObjectBaseRef<ProgramVertex> mVertex;
84 ObjectBaseRef<ProgramStore> mStore;
85 ObjectBaseRef<ProgramRaster> mRaster;
86 ObjectBaseRef<Font> mFont;
87 Context *mRsc;
88 };
89
Jason Sams11c8af92010-10-13 15:31:10 -070090 RsSurfaceConfig mUserSurfaceConfig;
Jason Sams462d11b2009-06-19 16:03:18 -070091
Jason Samsd19f10d2009-05-22 14:03:28 -070092 ElementState mStateElement;
93 TypeState mStateType;
94 SamplerState mStateSampler;
95 ProgramFragmentState mStateFragment;
Jason Sams54db59c2010-05-13 18:30:11 -070096 ProgramStoreState mStateFragmentStore;
Jason Samsebfb4362009-09-23 13:57:02 -070097 ProgramRasterState mStateRaster;
Jason Samsd19f10d2009-05-22 14:03:28 -070098 ProgramVertexState mStateVertex;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070099 FontState mStateFont;
Jason Samsd19f10d2009-05-22 14:03:28 -0700100
Jason Samsd19f10d2009-05-22 14:03:28 -0700101 ScriptCState mScriptC;
Alex Sakhartchouk8e90f2b2011-04-01 14:19:01 -0700102 FBOCache mFBOCache;
Jason Samsd19f10d2009-05-22 14:03:28 -0700103
Jason Samsd19f10d2009-05-22 14:03:28 -0700104 void swapBuffers();
105 void setRootScript(Script *);
Jason Samsa17af042010-11-17 15:29:32 -0800106 void setProgramRaster(ProgramRaster *);
107 void setProgramVertex(ProgramVertex *);
108 void setProgramFragment(ProgramFragment *);
109 void setProgramStore(ProgramStore *);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700110 void setFont(Font *);
Jason Samsd19f10d2009-05-22 14:03:28 -0700111
112 void updateSurface(void *sur);
113
Jason Samsa17af042010-11-17 15:29:32 -0800114 ProgramFragment * getProgramFragment() {return mFragment.get();}
115 ProgramStore * getProgramStore() {return mFragmentStore.get();}
116 ProgramRaster * getProgramRaster() {return mRaster.get();}
117 ProgramVertex * getProgramVertex() {return mVertex.get();}
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700118 Font * getFont() {return mFont.get();}
Jason Samsd19f10d2009-05-22 14:03:28 -0700119
Jason Sams156cce62010-03-03 13:03:18 -0800120 bool setupCheck();
Alex Sakhartchoukfeede2a2010-10-01 10:54:06 -0700121 void setupProgramStore();
Jason Samsd19f10d2009-05-22 14:03:28 -0700122
Jason Sams65e7aa52009-09-24 17:38:20 -0700123 void pause();
124 void resume();
Alex Sakhartchouk6c72eec2011-05-17 12:32:47 -0700125 void setSurface(uint32_t w, uint32_t h, RsNativeWindow sur);
Jason Sams7d787b42009-11-15 12:14:26 -0800126 void setPriority(int32_t p);
Jason Sams5c68a712010-12-24 14:38:39 -0800127 void destroyWorkerThreadResources();
Jason Sams65e7aa52009-09-24 17:38:20 -0700128
Jason Samsd5680f92009-06-10 18:39:40 -0700129 void assignName(ObjectBase *obj, const char *name, uint32_t len);
Jason Sams3eaa3382009-06-10 15:04:38 -0700130 void removeName(ObjectBase *obj);
Jason Sams9c54bdb2009-06-17 16:52:59 -0700131
Jason Samsedbfabd2011-05-17 15:01:29 -0700132 RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID);
133 RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen);
Jason Samsadd9d962010-11-22 16:20:16 -0800134 bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const;
Jason Samsf17bccc2010-05-28 18:23:22 -0700135 uint32_t runScript(Script *s);
Jason Sams516c3192009-10-06 13:58:47 -0700136
137 void initToClient();
138 void deinitToClient();
139
Jason Sams9c54bdb2009-06-17 16:52:59 -0700140 ProgramFragment * getDefaultProgramFragment() const {
141 return mStateFragment.mDefault.get();
142 }
143 ProgramVertex * getDefaultProgramVertex() const {
144 return mStateVertex.mDefault.get();
145 }
Jason Sams54db59c2010-05-13 18:30:11 -0700146 ProgramStore * getDefaultProgramStore() const {
Jason Sams9c54bdb2009-06-17 16:52:59 -0700147 return mStateFragmentStore.mDefault.get();
148 }
Jason Samsebfb4362009-09-23 13:57:02 -0700149 ProgramRaster * getDefaultProgramRaster() const {
150 return mStateRaster.mDefault.get();
151 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700152 Font* getDefaultFont() const {
153 return mStateFont.mDefault.get();
154 }
Jason Sams9c54bdb2009-06-17 16:52:59 -0700155
Jason Samsf603d212010-05-14 15:30:29 -0700156 uint32_t getWidth() const {return mWidth;}
157 uint32_t getHeight() const {return mHeight;}
Jason Sams40a29e82009-08-10 14:55:26 -0700158
Alex Sakhartchouk2d8ef492011-11-16 12:22:10 -0800159 uint32_t getCurrentSurfaceWidth() const;
160 uint32_t getCurrentSurfaceHeight() const;
161
Jason Samsadd9d962010-11-22 16:20:16 -0800162 mutable ThreadIO mIO;
Jason Samsbc948de2009-08-17 18:35:48 -0700163
Jason Samsf4d16062009-08-19 12:17:14 -0700164 // Timers
165 enum Timers {
166 RS_TIMER_IDLE,
167 RS_TIMER_INTERNAL,
168 RS_TIMER_SCRIPT,
169 RS_TIMER_CLEAR_SWAP,
170 _RS_TIMER_TOTAL
171 };
172 uint64_t getTime() const;
173 void timerInit();
174 void timerReset();
175 void timerSet(Timers);
176 void timerPrint();
Jason Sams2525a812009-09-03 15:43:13 -0700177 void timerFrame();
Jason Samsf4d16062009-08-19 12:17:14 -0700178
Jason Sams66b27712009-09-25 15:25:00 -0700179 struct {
180 bool mLogTimes;
181 bool mLogScripts;
182 bool mLogObjects;
Jason Sams5dad8b42009-12-15 19:10:11 -0800183 bool mLogShaders;
Alex Sakhartchouk4378f112010-09-29 09:49:13 -0700184 bool mLogShadersAttr;
185 bool mLogShadersUniforms;
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700186 bool mLogVisual;
Jason Sams27914292012-03-15 19:18:03 -0700187 uint32_t mDebugMaxThreads;
Jason Sams66b27712009-09-25 15:25:00 -0700188 } props;
Joe Onorato9ac2c662009-09-23 16:37:36 -0700189
Jason Sams5316b9e2011-09-13 15:41:01 -0700190 mutable struct {
191 bool inRoot;
192 const char *command;
193 const char *file;
194 uint32_t line;
195 } watchdog;
196 static void printWatchdogInfo(void *ctx);
197 void setWatchdogGL(const char *cmd, uint32_t line, const char *file) const {
198 watchdog.command = cmd;
199 watchdog.file = file;
200 watchdog.line = line;
201 }
202
Jason Sams9dab6672009-11-24 12:26:35 -0800203 void dumpDebug() const;
Jason Samsadd9d962010-11-22 16:20:16 -0800204 void setError(RsError e, const char *msg = NULL) const;
Jason Sams9dab6672009-11-24 12:26:35 -0800205
Jason Samsa9e7a052009-09-25 14:51:22 -0700206 mutable const ObjectBase * mObjHead;
207
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700208 uint32_t getDPI() const {return mDPI;}
209 void setDPI(uint32_t dpi) {mDPI = dpi;}
Jason Sams8e6c17f2010-07-19 15:38:19 -0700210
Stephen Hines4382467a2011-08-01 15:02:34 -0700211 uint32_t getTargetSdkVersion() const {return mTargetSdkVersion;}
212 void setTargetSdkVersion(uint32_t sdkVer) {mTargetSdkVersion = sdkVer;}
213
Jason Samsd19f10d2009-05-22 14:03:28 -0700214 Device *mDev;
Jason Sams803626f2011-04-06 17:52:23 -0700215protected:
Jason Samsd19f10d2009-05-22 14:03:28 -0700216
Stephen Hines4382467a2011-08-01 15:02:34 -0700217 uint32_t mTargetSdkVersion;
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700218 uint32_t mDPI;
Jason Sams3bc47d42009-11-12 15:10:25 -0800219 uint32_t mWidth;
220 uint32_t mHeight;
Jason Samsb9d5c572009-12-09 11:05:45 -0800221 int32_t mThreadPriority;
Jason Sams704ff642010-02-09 16:05:07 -0800222 bool mIsGraphicsContext;
Jason Sams3bc47d42009-11-12 15:10:25 -0800223
Jason Samsd19f10d2009-05-22 14:03:28 -0700224 bool mRunning;
225 bool mExit;
Jason Sams65e7aa52009-09-24 17:38:20 -0700226 bool mPaused;
Jason Samsadd9d962010-11-22 16:20:16 -0800227 mutable RsError mError;
Jason Samsd19f10d2009-05-22 14:03:28 -0700228
Jason Samsd19f10d2009-05-22 14:03:28 -0700229 pthread_t mThreadId;
Jason Sams7d787b42009-11-15 12:14:26 -0800230 pid_t mNativeThreadId;
Jason Samsd19f10d2009-05-22 14:03:28 -0700231
232 ObjectBaseRef<Script> mRootScript;
233 ObjectBaseRef<ProgramFragment> mFragment;
234 ObjectBaseRef<ProgramVertex> mVertex;
Jason Sams54db59c2010-05-13 18:30:11 -0700235 ObjectBaseRef<ProgramStore> mFragmentStore;
Jason Samsebfb4362009-09-23 13:57:02 -0700236 ObjectBaseRef<ProgramRaster> mRaster;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700237 ObjectBaseRef<Font> mFont;
Jason Sams730ee652009-08-18 17:07:09 -0700238
Alex Sakhartchouk6de55502010-08-03 12:03:16 -0700239 void displayDebugStats();
240
Jason Samsd19f10d2009-05-22 14:03:28 -0700241private:
242 Context();
Jason Samsd5f06302010-11-03 14:27:11 -0700243 bool initContext(Device *, const RsSurfaceConfig *sc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700244
Jason Samsd5f06302010-11-03 14:27:11 -0700245
246 bool initGLThread();
Jason Sams71362202009-10-27 14:44:31 -0700247 void deinitEGL();
Jason Samsd19f10d2009-05-22 14:03:28 -0700248
Jason Samsb9d5c572009-12-09 11:05:45 -0800249 uint32_t runRootScript();
Jason Samsd19f10d2009-05-22 14:03:28 -0700250
251 static void * threadProc(void *);
Jason Sams8e6c17f2010-07-19 15:38:19 -0700252 static void * helperThreadProc(void *);
Jason Samsd19f10d2009-05-22 14:03:28 -0700253
Alex Sakhartchouk6c72eec2011-05-17 12:32:47 -0700254 bool mHasSurface;
Alex Sakhartchouk17a8a192011-06-03 10:18:01 -0700255 bool mIsContextLite;
Jason Sams3eaa3382009-06-10 15:04:38 -0700256
257 Vector<ObjectBase *> mNames;
Jason Samsf4d16062009-08-19 12:17:14 -0700258
259 uint64_t mTimers[_RS_TIMER_TOTAL];
260 Timers mTimerActive;
261 uint64_t mTimeLast;
Jason Sams2525a812009-09-03 15:43:13 -0700262 uint64_t mTimeFrame;
263 uint64_t mTimeLastFrame;
Jason Samsb9d5c572009-12-09 11:05:45 -0800264 uint32_t mTimeMSLastFrame;
265 uint32_t mTimeMSLastScript;
266 uint32_t mTimeMSLastSwap;
Alex Sakhartchouk76322af2010-10-05 13:23:55 -0700267 uint32_t mAverageFPSFrameCount;
268 uint64_t mAverageFPSStartTime;
269 uint32_t mAverageFPS;
Jason Samsd19f10d2009-05-22 14:03:28 -0700270};
271
Alex Sakhartchoukd0f5bd12011-01-31 14:53:24 -0800272} // renderscript
273} // android
Jason Samsd19f10d2009-05-22 14:03:28 -0700274#endif