blob: 61218dab5d5ca50d893af551bc2bd404edc8af49 [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 Samsbad80742011-03-16 16:29:28 -070021#include "rs_hal.h"
22
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080023#include "rsThreadIO.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070024#include "rsScriptC.h"
Jason Samsdbe66d62012-09-17 13:54:41 -070025#include "rsScriptGroup.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070026#include "rsSampler.h"
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -070027#include "rsFont.h"
Jason Sams9e0afb52011-10-31 13:23:43 -070028#include "rsPath.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070029#include "rsProgramFragment.h"
Jason Samsccc010b2010-05-13 18:30:11 -070030#include "rsProgramStore.h"
Jason Sams5fd09d82009-09-23 13:57:02 -070031#include "rsProgramRaster.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070032#include "rsProgramVertex.h"
Alex Sakhartchouk7d9c5ff2011-04-01 14:19:01 -070033#include "rsFBOCache.h"
Shih-wei Liaoda3b58d2012-08-03 04:24:33 -070034#include <string.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070035
Jason Sams326e0dd2009-05-22 14:03:28 -070036// ---------------------------------------------------------------------------
37namespace android {
Mathias Agopiand4c25e32010-02-09 17:46:37 -080038
Jason Sams326e0dd2009-05-22 14:03:28 -070039namespace renderscript {
40
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -080041class Device;
42
Jason Sams605048a2010-09-30 18:15:52 -070043#if 0
44#define CHECK_OBJ(o) { \
45 GET_TLS(); \
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080046 if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
Steve Blockaf12ac62012-01-06 19:20:56 +000047 ALOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \
Jason Sams605048a2010-09-30 18:15:52 -070048 } \
49}
50#define CHECK_OBJ_OR_NULL(o) { \
51 GET_TLS(); \
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080052 if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
Steve Blockaf12ac62012-01-06 19:20:56 +000053 ALOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \
Jason Sams605048a2010-09-30 18:15:52 -070054 } \
55}
56#else
57#define CHECK_OBJ(o)
58#define CHECK_OBJ_OR_NULL(o)
59#endif
60
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080061class Context {
Jason Sams326e0dd2009-05-22 14:03:28 -070062public:
Jason Samsbad80742011-03-16 16:29:28 -070063 struct Hal {
64 void * drv;
65
66 RsdHalFunctions funcs;
67 };
68 Hal mHal;
69
Jason Sams5c1c79a2010-11-03 14:27:11 -070070 static Context * createContext(Device *, const RsSurfaceConfig *sc);
Tim Murray0e92fa32012-11-06 14:36:38 -080071 static Context * createContext(Device *, const RsSurfaceConfig *sc, bool forceCpu);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -070072 static Context * createContextLite();
Jason Sams326e0dd2009-05-22 14:03:28 -070073 ~Context();
74
Jason Samsfb03a222009-10-15 16:47:31 -070075 static pthread_mutex_t gInitMutex;
Stephen Hinesca3f09c2011-01-07 15:11:30 -080076 // Library mutex (for providing thread-safe calls from the runtime)
77 static pthread_mutex_t gLibMutex;
Jason Samsfb03a222009-10-15 16:47:31 -070078
Jason Sams60709252010-11-17 15:29:32 -080079 class PushState {
80 public:
81 PushState(Context *);
82 ~PushState();
83
84 private:
85 ObjectBaseRef<ProgramFragment> mFragment;
86 ObjectBaseRef<ProgramVertex> mVertex;
87 ObjectBaseRef<ProgramStore> mStore;
88 ObjectBaseRef<ProgramRaster> mRaster;
89 ObjectBaseRef<Font> mFont;
90 Context *mRsc;
91 };
92
Jason Sams6b8552a2010-10-13 15:31:10 -070093 RsSurfaceConfig mUserSurfaceConfig;
Jason Samse5769102009-06-19 16:03:18 -070094
Jason Sams326e0dd2009-05-22 14:03:28 -070095 ElementState mStateElement;
96 TypeState mStateType;
97 SamplerState mStateSampler;
98 ProgramFragmentState mStateFragment;
Jason Samsccc010b2010-05-13 18:30:11 -070099 ProgramStoreState mStateFragmentStore;
Jason Sams5fd09d82009-09-23 13:57:02 -0700100 ProgramRasterState mStateRaster;
Jason Sams326e0dd2009-05-22 14:03:28 -0700101 ProgramVertexState mStateVertex;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700102 FontState mStateFont;
Jason Sams326e0dd2009-05-22 14:03:28 -0700103
Jason Sams326e0dd2009-05-22 14:03:28 -0700104 ScriptCState mScriptC;
Alex Sakhartchouk7d9c5ff2011-04-01 14:19:01 -0700105 FBOCache mFBOCache;
Jason Sams326e0dd2009-05-22 14:03:28 -0700106
Jason Sams326e0dd2009-05-22 14:03:28 -0700107 void swapBuffers();
108 void setRootScript(Script *);
Jason Sams60709252010-11-17 15:29:32 -0800109 void setProgramRaster(ProgramRaster *);
110 void setProgramVertex(ProgramVertex *);
111 void setProgramFragment(ProgramFragment *);
112 void setProgramStore(ProgramStore *);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700113 void setFont(Font *);
Jason Sams326e0dd2009-05-22 14:03:28 -0700114
115 void updateSurface(void *sur);
116
Jason Sams60709252010-11-17 15:29:32 -0800117 ProgramFragment * getProgramFragment() {return mFragment.get();}
118 ProgramStore * getProgramStore() {return mFragmentStore.get();}
119 ProgramRaster * getProgramRaster() {return mRaster.get();}
120 ProgramVertex * getProgramVertex() {return mVertex.get();}
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700121 Font * getFont() {return mFont.get();}
Jason Sams326e0dd2009-05-22 14:03:28 -0700122
Jason Samsa2cf7552010-03-03 13:03:18 -0800123 bool setupCheck();
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700124 void setupProgramStore();
Jason Sams326e0dd2009-05-22 14:03:28 -0700125
Jason Sams86f1b232009-09-24 17:38:20 -0700126 void pause();
127 void resume();
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700128 void setSurface(uint32_t w, uint32_t h, RsNativeWindow sur);
Jason Sams15832442009-11-15 12:14:26 -0800129 void setPriority(int32_t p);
Jason Sams741aac92010-12-24 14:38:39 -0800130 void destroyWorkerThreadResources();
Jason Sams86f1b232009-09-24 17:38:20 -0700131
Jason Samsa4a54e42009-06-10 18:39:40 -0700132 void assignName(ObjectBase *obj, const char *name, uint32_t len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700133 void removeName(ObjectBase *obj);
Jason Sams8ce125b2009-06-17 16:52:59 -0700134
Jason Sams1a4efa32011-05-17 15:01:29 -0700135 RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID);
136 RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen);
Jason Sams87319de2010-11-22 16:20:16 -0800137 bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const;
Jason Samsc61346b2010-05-28 18:23:22 -0700138 uint32_t runScript(Script *s);
Jason Sams8c401ef2009-10-06 13:58:47 -0700139
140 void initToClient();
141 void deinitToClient();
142
Jason Sams8ce125b2009-06-17 16:52:59 -0700143 ProgramFragment * getDefaultProgramFragment() const {
144 return mStateFragment.mDefault.get();
145 }
146 ProgramVertex * getDefaultProgramVertex() const {
147 return mStateVertex.mDefault.get();
148 }
Jason Samsccc010b2010-05-13 18:30:11 -0700149 ProgramStore * getDefaultProgramStore() const {
Jason Sams8ce125b2009-06-17 16:52:59 -0700150 return mStateFragmentStore.mDefault.get();
151 }
Jason Sams5fd09d82009-09-23 13:57:02 -0700152 ProgramRaster * getDefaultProgramRaster() const {
153 return mStateRaster.mDefault.get();
154 }
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700155 Font* getDefaultFont() const {
156 return mStateFont.mDefault.get();
157 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700158
Jason Sams771565f2010-05-14 15:30:29 -0700159 uint32_t getWidth() const {return mWidth;}
160 uint32_t getHeight() const {return mHeight;}
Jason Samse579df42009-08-10 14:55:26 -0700161
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800162 uint32_t getCurrentSurfaceWidth() const;
163 uint32_t getCurrentSurfaceHeight() const;
164
Jason Sams87319de2010-11-22 16:20:16 -0800165 mutable ThreadIO mIO;
Jason Samsfcd31922009-08-17 18:35:48 -0700166
Jason Sams24371d92009-08-19 12:17:14 -0700167 // Timers
168 enum Timers {
169 RS_TIMER_IDLE,
170 RS_TIMER_INTERNAL,
171 RS_TIMER_SCRIPT,
172 RS_TIMER_CLEAR_SWAP,
173 _RS_TIMER_TOTAL
174 };
175 uint64_t getTime() const;
176 void timerInit();
177 void timerReset();
178 void timerSet(Timers);
179 void timerPrint();
Jason Sams1d54f102009-09-03 15:43:13 -0700180 void timerFrame();
Jason Sams24371d92009-08-19 12:17:14 -0700181
Jason Sams1fddd902009-09-25 15:25:00 -0700182 struct {
183 bool mLogTimes;
184 bool mLogScripts;
185 bool mLogObjects;
Jason Samscd506532009-12-15 19:10:11 -0800186 bool mLogShaders;
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700187 bool mLogShadersAttr;
188 bool mLogShadersUniforms;
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700189 bool mLogVisual;
Jason Samsd1f7da62012-03-15 19:18:03 -0700190 uint32_t mDebugMaxThreads;
Jason Sams1fddd902009-09-25 15:25:00 -0700191 } props;
Joe Onorato76371ff2009-09-23 16:37:36 -0700192
Jason Sams2382aba2011-09-13 15:41:01 -0700193 mutable struct {
194 bool inRoot;
195 const char *command;
196 const char *file;
197 uint32_t line;
198 } watchdog;
199 static void printWatchdogInfo(void *ctx);
200 void setWatchdogGL(const char *cmd, uint32_t line, const char *file) const {
201 watchdog.command = cmd;
202 watchdog.file = file;
203 watchdog.line = line;
204 }
205
Jason Sams13e26342009-11-24 12:26:35 -0800206 void dumpDebug() const;
Jason Sams87319de2010-11-22 16:20:16 -0800207 void setError(RsError e, const char *msg = NULL) const;
Jason Sams13e26342009-11-24 12:26:35 -0800208
Jason Samse514b452009-09-25 14:51:22 -0700209 mutable const ObjectBase * mObjHead;
210
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700211 uint32_t getDPI() const {return mDPI;}
212 void setDPI(uint32_t dpi) {mDPI = dpi;}
Jason Sams7bf29dd2010-07-19 15:38:19 -0700213
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700214 uint32_t getTargetSdkVersion() const {return mTargetSdkVersion;}
215 void setTargetSdkVersion(uint32_t sdkVer) {mTargetSdkVersion = sdkVer;}
216
Jason Sams326e0dd2009-05-22 14:03:28 -0700217 Device *mDev;
Jason Sams4b3de472011-04-06 17:52:23 -0700218protected:
Jason Sams326e0dd2009-05-22 14:03:28 -0700219
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700220 uint32_t mTargetSdkVersion;
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700221 uint32_t mDPI;
Jason Sams613cad12009-11-12 15:10:25 -0800222 uint32_t mWidth;
223 uint32_t mHeight;
Jason Sams2dca84d2009-12-09 11:05:45 -0800224 int32_t mThreadPriority;
Jason Sams4820e8b2010-02-09 16:05:07 -0800225 bool mIsGraphicsContext;
Jason Sams613cad12009-11-12 15:10:25 -0800226
Tim Murray0e92fa32012-11-06 14:36:38 -0800227 bool mForceCpu;
228
Jason Sams326e0dd2009-05-22 14:03:28 -0700229 bool mRunning;
230 bool mExit;
Jason Sams86f1b232009-09-24 17:38:20 -0700231 bool mPaused;
Jason Sams87319de2010-11-22 16:20:16 -0800232 mutable RsError mError;
Jason Sams326e0dd2009-05-22 14:03:28 -0700233
Jason Sams326e0dd2009-05-22 14:03:28 -0700234 pthread_t mThreadId;
Jason Sams15832442009-11-15 12:14:26 -0800235 pid_t mNativeThreadId;
Jason Sams326e0dd2009-05-22 14:03:28 -0700236
237 ObjectBaseRef<Script> mRootScript;
238 ObjectBaseRef<ProgramFragment> mFragment;
239 ObjectBaseRef<ProgramVertex> mVertex;
Jason Samsccc010b2010-05-13 18:30:11 -0700240 ObjectBaseRef<ProgramStore> mFragmentStore;
Jason Sams5fd09d82009-09-23 13:57:02 -0700241 ObjectBaseRef<ProgramRaster> mRaster;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700242 ObjectBaseRef<Font> mFont;
Jason Sams50869382009-08-18 17:07:09 -0700243
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700244 void displayDebugStats();
245
Jason Sams326e0dd2009-05-22 14:03:28 -0700246private:
247 Context();
Jason Sams5c1c79a2010-11-03 14:27:11 -0700248 bool initContext(Device *, const RsSurfaceConfig *sc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700249
Jason Sams5c1c79a2010-11-03 14:27:11 -0700250 bool initGLThread();
Jason Sams33b6e3b2009-10-27 14:44:31 -0700251 void deinitEGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700252
Jason Sams2dca84d2009-12-09 11:05:45 -0800253 uint32_t runRootScript();
Jason Sams326e0dd2009-05-22 14:03:28 -0700254
Tim Murray0c66f072012-10-23 14:05:02 -0700255 static bool loadRuntime(const char* filename, Context* rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700256 static void * threadProc(void *);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700257 static void * helperThreadProc(void *);
Jason Sams326e0dd2009-05-22 14:03:28 -0700258
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700259 bool mHasSurface;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700260 bool mIsContextLite;
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700261
262 Vector<ObjectBase *> mNames;
Jason Sams24371d92009-08-19 12:17:14 -0700263
264 uint64_t mTimers[_RS_TIMER_TOTAL];
265 Timers mTimerActive;
266 uint64_t mTimeLast;
Jason Sams1d54f102009-09-03 15:43:13 -0700267 uint64_t mTimeFrame;
268 uint64_t mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800269 uint32_t mTimeMSLastFrame;
270 uint32_t mTimeMSLastScript;
271 uint32_t mTimeMSLastSwap;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700272 uint32_t mAverageFPSFrameCount;
273 uint64_t mAverageFPSStartTime;
274 uint32_t mAverageFPS;
Jason Sams326e0dd2009-05-22 14:03:28 -0700275};
276
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800277} // renderscript
278} // android
Jason Sams326e0dd2009-05-22 14:03:28 -0700279#endif