blob: b38235865794c0cd54e4fc6ba4fed8c1e2b5fb86 [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"
Jason Sams93eacc72012-12-18 14:26:57 -080022#include <string.h>
Jason Samsbad80742011-03-16 16:29:28 -070023
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -080024#include "rsThreadIO.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070025#include "rsScriptC.h"
Jason Samsdbe66d62012-09-17 13:54:41 -070026#include "rsScriptGroup.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070027#include "rsSampler.h"
Jason Sams93eacc72012-12-18 14:26:57 -080028
Stephen Hines574854b2013-07-10 08:29:51 -070029#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
Tim Murrayfa85e912013-05-23 13:19:36 -070030#define ATRACE_TAG ATRACE_TAG_RS
31#include "utils/Trace.h"
Stephen Hines574854b2013-07-10 08:29:51 -070032#else
33#define ATRACE_ENABLED(...) false
34#define ATRACE_NAME(...)
35#define ATRACE_CALL(...)
Tim Murrayfa85e912013-05-23 13:19:36 -070036#endif
37
Jason Sams93eacc72012-12-18 14:26:57 -080038#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -070039#include "rsFont.h"
Jason Sams9e0afb52011-10-31 13:23:43 -070040#include "rsPath.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070041#include "rsProgramFragment.h"
Jason Samsccc010b2010-05-13 18:30:11 -070042#include "rsProgramStore.h"
Jason Sams5fd09d82009-09-23 13:57:02 -070043#include "rsProgramRaster.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070044#include "rsProgramVertex.h"
Alex Sakhartchouk7d9c5ff2011-04-01 14:19:01 -070045#include "rsFBOCache.h"
Tim Murrayfa85e912013-05-23 13:19:36 -070046
Jason Sams93eacc72012-12-18 14:26:57 -080047#endif
48
Jason Sams326e0dd2009-05-22 14:03:28 -070049
Jason Sams326e0dd2009-05-22 14:03:28 -070050// ---------------------------------------------------------------------------
51namespace android {
Mathias Agopiand4c25e32010-02-09 17:46:37 -080052
Jason Sams326e0dd2009-05-22 14:03:28 -070053namespace renderscript {
54
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -080055class Device;
56
Jason Sams605048a2010-09-30 18:15:52 -070057#if 0
58#define CHECK_OBJ(o) { \
59 GET_TLS(); \
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080060 if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
Steve Blockaf12ac62012-01-06 19:20:56 +000061 ALOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \
Jason Sams605048a2010-09-30 18:15:52 -070062 } \
63}
64#define CHECK_OBJ_OR_NULL(o) { \
65 GET_TLS(); \
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080066 if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) { \
Steve Blockaf12ac62012-01-06 19:20:56 +000067 ALOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__); \
Jason Sams605048a2010-09-30 18:15:52 -070068 } \
69}
70#else
71#define CHECK_OBJ(o)
72#define CHECK_OBJ_OR_NULL(o)
73#endif
74
Tim Murrayfa85e912013-05-23 13:19:36 -070075
76
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080077class Context {
Jason Sams326e0dd2009-05-22 14:03:28 -070078public:
Jason Samsbad80742011-03-16 16:29:28 -070079 struct Hal {
80 void * drv;
81
82 RsdHalFunctions funcs;
Jason Sams416f4b62014-06-25 15:45:36 -070083 uint32_t flags;
Jason Samsbad80742011-03-16 16:29:28 -070084 };
85 Hal mHal;
86
Stephen Hinescca3d6c2013-04-15 01:06:39 -070087 static Context * createContext(Device *, const RsSurfaceConfig *sc,
88 RsContextType ct = RS_CONTEXT_TYPE_NORMAL,
Tim Murray84e3dea2013-09-09 16:12:51 -070089 uint32_t flags = 0);
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -070090 static Context * createContextLite();
Jason Sams326e0dd2009-05-22 14:03:28 -070091 ~Context();
92
Jason Sams4961cce2013-04-11 16:11:46 -070093 static pthread_mutex_t gMessageMutex;
Jason Samsfb03a222009-10-15 16:47:31 -070094 static pthread_mutex_t gInitMutex;
Stephen Hinesca3f09c2011-01-07 15:11:30 -080095 // Library mutex (for providing thread-safe calls from the runtime)
96 static pthread_mutex_t gLibMutex;
Jason Samsfb03a222009-10-15 16:47:31 -070097
Jason Sams60709252010-11-17 15:29:32 -080098 class PushState {
99 public:
100 PushState(Context *);
101 ~PushState();
102
103 private:
Jason Sams93eacc72012-12-18 14:26:57 -0800104#ifndef RS_COMPATIBILITY_LIB
Jason Sams60709252010-11-17 15:29:32 -0800105 ObjectBaseRef<ProgramFragment> mFragment;
106 ObjectBaseRef<ProgramVertex> mVertex;
107 ObjectBaseRef<ProgramStore> mStore;
108 ObjectBaseRef<ProgramRaster> mRaster;
109 ObjectBaseRef<Font> mFont;
Jason Sams93eacc72012-12-18 14:26:57 -0800110#endif
Jason Sams60709252010-11-17 15:29:32 -0800111 Context *mRsc;
112 };
113
Jason Sams6b8552a2010-10-13 15:31:10 -0700114 RsSurfaceConfig mUserSurfaceConfig;
Jason Samse5769102009-06-19 16:03:18 -0700115
Jason Sams326e0dd2009-05-22 14:03:28 -0700116 ElementState mStateElement;
117 TypeState mStateType;
118 SamplerState mStateSampler;
Jason Sams93eacc72012-12-18 14:26:57 -0800119
Jason Sams93eacc72012-12-18 14:26:57 -0800120 bool isSynchronous() {return mSynchronous;}
121 bool setupCheck();
122
123#ifndef RS_COMPATIBILITY_LIB
124 FBOCache mFBOCache;
Jason Sams326e0dd2009-05-22 14:03:28 -0700125 ProgramFragmentState mStateFragment;
Jason Samsccc010b2010-05-13 18:30:11 -0700126 ProgramStoreState mStateFragmentStore;
Jason Sams5fd09d82009-09-23 13:57:02 -0700127 ProgramRasterState mStateRaster;
Jason Sams326e0dd2009-05-22 14:03:28 -0700128 ProgramVertexState mStateVertex;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700129 FontState mStateFont;
Jason Sams326e0dd2009-05-22 14:03:28 -0700130
Tim Murray4d252d62012-11-29 14:37:59 -0800131
Jason Sams326e0dd2009-05-22 14:03:28 -0700132 void swapBuffers();
133 void setRootScript(Script *);
Jason Sams60709252010-11-17 15:29:32 -0800134 void setProgramRaster(ProgramRaster *);
135 void setProgramVertex(ProgramVertex *);
136 void setProgramFragment(ProgramFragment *);
137 void setProgramStore(ProgramStore *);
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700138 void setFont(Font *);
Jason Sams326e0dd2009-05-22 14:03:28 -0700139
140 void updateSurface(void *sur);
141
Jason Sams60709252010-11-17 15:29:32 -0800142 ProgramFragment * getProgramFragment() {return mFragment.get();}
143 ProgramStore * getProgramStore() {return mFragmentStore.get();}
144 ProgramRaster * getProgramRaster() {return mRaster.get();}
145 ProgramVertex * getProgramVertex() {return mVertex.get();}
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700146 Font * getFont() {return mFont.get();}
Jason Sams326e0dd2009-05-22 14:03:28 -0700147
Alex Sakhartchouk889fe502010-10-01 10:54:06 -0700148 void setupProgramStore();
Jason Sams326e0dd2009-05-22 14:03:28 -0700149
Jason Sams86f1b232009-09-24 17:38:20 -0700150 void pause();
151 void resume();
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700152 void setSurface(uint32_t w, uint32_t h, RsNativeWindow sur);
Jason Sams93eacc72012-12-18 14:26:57 -0800153#endif
Jason Samsf2748272013-11-26 18:10:59 -0800154 void finish();
Jason Sams93eacc72012-12-18 14:26:57 -0800155
Jason Sams15832442009-11-15 12:14:26 -0800156 void setPriority(int32_t p);
Jason Sams741aac92010-12-24 14:38:39 -0800157 void destroyWorkerThreadResources();
Jason Sams86f1b232009-09-24 17:38:20 -0700158
Jason Samsa4a54e42009-06-10 18:39:40 -0700159 void assignName(ObjectBase *obj, const char *name, uint32_t len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700160 void removeName(ObjectBase *obj);
Jason Sams8ce125b2009-06-17 16:52:59 -0700161
Jason Sams1a4efa32011-05-17 15:01:29 -0700162 RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID);
163 RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen);
Jason Sams87319de2010-11-22 16:20:16 -0800164 bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const;
Jason Samsc61346b2010-05-28 18:23:22 -0700165 uint32_t runScript(Script *s);
Jason Sams8c401ef2009-10-06 13:58:47 -0700166
167 void initToClient();
168 void deinitToClient();
169
Jason Sams93eacc72012-12-18 14:26:57 -0800170#ifndef RS_COMPATIBILITY_LIB
Jason Sams8ce125b2009-06-17 16:52:59 -0700171 ProgramFragment * getDefaultProgramFragment() const {
172 return mStateFragment.mDefault.get();
173 }
174 ProgramVertex * getDefaultProgramVertex() const {
175 return mStateVertex.mDefault.get();
176 }
Jason Samsccc010b2010-05-13 18:30:11 -0700177 ProgramStore * getDefaultProgramStore() const {
Jason Sams8ce125b2009-06-17 16:52:59 -0700178 return mStateFragmentStore.mDefault.get();
179 }
Jason Sams5fd09d82009-09-23 13:57:02 -0700180 ProgramRaster * getDefaultProgramRaster() const {
181 return mStateRaster.mDefault.get();
182 }
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700183 Font* getDefaultFont() const {
184 return mStateFont.mDefault.get();
185 }
Jason Sams8ce125b2009-06-17 16:52:59 -0700186
Jason Sams771565f2010-05-14 15:30:29 -0700187 uint32_t getWidth() const {return mWidth;}
188 uint32_t getHeight() const {return mHeight;}
Jason Samse579df42009-08-10 14:55:26 -0700189
Alex Sakhartchouka74a8f62011-11-16 12:22:10 -0800190 uint32_t getCurrentSurfaceWidth() const;
191 uint32_t getCurrentSurfaceHeight() const;
192
Jason Sams93eacc72012-12-18 14:26:57 -0800193 void setWatchdogGL(const char *cmd, uint32_t line, const char *file) const {
194 watchdog.command = cmd;
195 watchdog.file = file;
196 watchdog.line = line;
197 }
198#endif
199
Jason Sams87319de2010-11-22 16:20:16 -0800200 mutable ThreadIO mIO;
Jason Samsfcd31922009-08-17 18:35:48 -0700201
Jason Sams24371d92009-08-19 12:17:14 -0700202 // Timers
203 enum Timers {
204 RS_TIMER_IDLE,
205 RS_TIMER_INTERNAL,
206 RS_TIMER_SCRIPT,
207 RS_TIMER_CLEAR_SWAP,
208 _RS_TIMER_TOTAL
209 };
210 uint64_t getTime() const;
211 void timerInit();
212 void timerReset();
213 void timerSet(Timers);
214 void timerPrint();
Jason Sams1d54f102009-09-03 15:43:13 -0700215 void timerFrame();
Jason Sams24371d92009-08-19 12:17:14 -0700216
Jason Sams1fddd902009-09-25 15:25:00 -0700217 struct {
218 bool mLogTimes;
219 bool mLogScripts;
220 bool mLogObjects;
Jason Samscd506532009-12-15 19:10:11 -0800221 bool mLogShaders;
Alex Sakhartchouk886f11a2010-09-29 09:49:13 -0700222 bool mLogShadersAttr;
223 bool mLogShadersUniforms;
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700224 bool mLogVisual;
Jason Samsd1f7da62012-03-15 19:18:03 -0700225 uint32_t mDebugMaxThreads;
Jason Sams1fddd902009-09-25 15:25:00 -0700226 } props;
Joe Onorato76371ff2009-09-23 16:37:36 -0700227
Jason Sams2382aba2011-09-13 15:41:01 -0700228 mutable struct {
229 bool inRoot;
230 const char *command;
231 const char *file;
232 uint32_t line;
233 } watchdog;
234 static void printWatchdogInfo(void *ctx);
Jason Sams2382aba2011-09-13 15:41:01 -0700235
Jason Sams13e26342009-11-24 12:26:35 -0800236 void dumpDebug() const;
Jason Sams87319de2010-11-22 16:20:16 -0800237 void setError(RsError e, const char *msg = NULL) const;
Jason Sams13e26342009-11-24 12:26:35 -0800238
Jason Samse514b452009-09-25 14:51:22 -0700239 mutable const ObjectBase * mObjHead;
240
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700241 uint32_t getDPI() const {return mDPI;}
242 void setDPI(uint32_t dpi) {mDPI = dpi;}
Jason Sams7bf29dd2010-07-19 15:38:19 -0700243
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700244 uint32_t getTargetSdkVersion() const {return mTargetSdkVersion;}
245 void setTargetSdkVersion(uint32_t sdkVer) {mTargetSdkVersion = sdkVer;}
246
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700247 RsContextType getContextType() const { return mContextType; }
248 void setContextType(RsContextType ct) { mContextType = ct; }
249
Jason Sams326e0dd2009-05-22 14:03:28 -0700250 Device *mDev;
Jason Sams4b3de472011-04-06 17:52:23 -0700251protected:
Jason Sams326e0dd2009-05-22 14:03:28 -0700252
Stephen Hinescbb0b8a2011-08-01 15:02:34 -0700253 uint32_t mTargetSdkVersion;
Alex Sakhartchouk7b3e9bd2011-03-16 19:28:25 -0700254 uint32_t mDPI;
Jason Sams613cad12009-11-12 15:10:25 -0800255 uint32_t mWidth;
256 uint32_t mHeight;
Jason Sams2dca84d2009-12-09 11:05:45 -0800257 int32_t mThreadPriority;
Jason Sams4820e8b2010-02-09 16:05:07 -0800258 bool mIsGraphicsContext;
Jason Sams613cad12009-11-12 15:10:25 -0800259
Tim Murray0e92fa32012-11-06 14:36:38 -0800260 bool mForceCpu;
261
Stephen Hinescca3d6c2013-04-15 01:06:39 -0700262 RsContextType mContextType;
263
Jason Sams326e0dd2009-05-22 14:03:28 -0700264 bool mRunning;
265 bool mExit;
Jason Sams86f1b232009-09-24 17:38:20 -0700266 bool mPaused;
Jason Sams87319de2010-11-22 16:20:16 -0800267 mutable RsError mError;
Jason Sams326e0dd2009-05-22 14:03:28 -0700268
Jason Sams326e0dd2009-05-22 14:03:28 -0700269 pthread_t mThreadId;
Jason Sams15832442009-11-15 12:14:26 -0800270 pid_t mNativeThreadId;
Jason Sams326e0dd2009-05-22 14:03:28 -0700271
272 ObjectBaseRef<Script> mRootScript;
Jason Sams93eacc72012-12-18 14:26:57 -0800273#ifndef RS_COMPATIBILITY_LIB
Jason Sams326e0dd2009-05-22 14:03:28 -0700274 ObjectBaseRef<ProgramFragment> mFragment;
275 ObjectBaseRef<ProgramVertex> mVertex;
Jason Samsccc010b2010-05-13 18:30:11 -0700276 ObjectBaseRef<ProgramStore> mFragmentStore;
Jason Sams5fd09d82009-09-23 13:57:02 -0700277 ObjectBaseRef<ProgramRaster> mRaster;
Alex Sakhartchoukd3e0ad42010-06-24 17:15:34 -0700278 ObjectBaseRef<Font> mFont;
Jason Sams93eacc72012-12-18 14:26:57 -0800279#endif
Jason Sams50869382009-08-18 17:07:09 -0700280
Alex Sakhartchouk0cae59f2010-08-03 12:03:16 -0700281 void displayDebugStats();
282
Jason Sams326e0dd2009-05-22 14:03:28 -0700283private:
284 Context();
Jason Sams5c1c79a2010-11-03 14:27:11 -0700285 bool initContext(Device *, const RsSurfaceConfig *sc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700286
Tim Murray4d252d62012-11-29 14:37:59 -0800287 bool mSynchronous;
Jason Sams5c1c79a2010-11-03 14:27:11 -0700288 bool initGLThread();
Jason Sams33b6e3b2009-10-27 14:44:31 -0700289 void deinitEGL();
Jason Sams326e0dd2009-05-22 14:03:28 -0700290
Jason Sams2dca84d2009-12-09 11:05:45 -0800291 uint32_t runRootScript();
Jason Sams326e0dd2009-05-22 14:03:28 -0700292
Tim Murray0c66f072012-10-23 14:05:02 -0700293 static bool loadRuntime(const char* filename, Context* rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700294 static void * threadProc(void *);
Jason Sams7bf29dd2010-07-19 15:38:19 -0700295 static void * helperThreadProc(void *);
Jason Sams326e0dd2009-05-22 14:03:28 -0700296
Alex Sakhartchouk7257c7e2011-05-17 12:32:47 -0700297 bool mHasSurface;
Alex Sakhartchoukb81a0eb2011-06-03 10:18:01 -0700298 bool mIsContextLite;
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700299
300 Vector<ObjectBase *> mNames;
Jason Sams24371d92009-08-19 12:17:14 -0700301
302 uint64_t mTimers[_RS_TIMER_TOTAL];
303 Timers mTimerActive;
304 uint64_t mTimeLast;
Jason Sams1d54f102009-09-03 15:43:13 -0700305 uint64_t mTimeFrame;
306 uint64_t mTimeLastFrame;
Jason Sams2dca84d2009-12-09 11:05:45 -0800307 uint32_t mTimeMSLastFrame;
308 uint32_t mTimeMSLastScript;
309 uint32_t mTimeMSLastSwap;
Alex Sakhartchoukc8fb69e2010-10-05 13:23:55 -0700310 uint32_t mAverageFPSFrameCount;
311 uint64_t mAverageFPSStartTime;
312 uint32_t mAverageFPS;
Jason Sams326e0dd2009-05-22 14:03:28 -0700313};
314
Tim Murray397b0b12014-01-07 15:35:08 -0800315void LF_ObjDestroy_handcode(const Context *rsc, RsAsyncVoidPtr objPtr);
316
Alex Sakhartchouk77d9f4b2011-01-31 14:53:24 -0800317} // renderscript
318} // android
Jason Sams326e0dd2009-05-22 14:03:28 -0700319#endif