1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
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 
20 #include "rsUtils.h"
21 #include "rs_hal.h"
22 #include <string.h>
23 
24 #include "rsThreadIO.h"
25 #include "rsScriptC.h"
26 #include "rsScriptGroup.h"
27 #include "rsSampler.h"
28 
29 #define ATRACE_ENABLED(...) false
30 #define ATRACE_NAME(...)
31 #define ATRACE_CALL(...)
32 
33 #ifndef RS_COMPATIBILITY_LIB
34 #include "rsFont.h"
35 #include "rsProgramFragment.h"
36 #include "rsProgramStore.h"
37 #include "rsProgramRaster.h"
38 #include "rsProgramVertex.h"
39 #include "rsFBOCache.h"
40 
41 #endif
42 
43 #include <vector>
44 
45 // ---------------------------------------------------------------------------
46 namespace android {
47 
48 namespace renderscript {
49 
50 class Device;
51 
52 #if 0
53 #define CHECK_OBJ(o) { \
54     GET_TLS(); \
55     if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) {  \
56         ALOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__);  \
57     } \
58 }
59 #define CHECK_OBJ_OR_NULL(o) { \
60     GET_TLS(); \
61     if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) {  \
62         ALOGE("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 
70 
71 
72 class Context {
73 public:
74     struct Hal {
75         void * drv;
76 
77         RsdHalFunctions funcs;
78         uint32_t flags;
79     };
80     Hal mHal;
81 
82     static Context * createContext(Device *, const RsSurfaceConfig *sc,
83             RsContextType ct = RS_CONTEXT_TYPE_NORMAL,
84             uint32_t flags = 0,
85             const char* vendorDriverName = nullptr);
86     static Context * createContextLite();
87     ~Context();
88 
89     static pthread_mutex_t gMessageMutex;
90     static pthread_mutex_t gInitMutex;
91     // Library mutex (for providing thread-safe calls from the runtime)
92     static pthread_mutex_t gLibMutex;
93 
94     class PushState {
95     public:
96         explicit PushState(Context *);
97         ~PushState();
98 
99     private:
100 #if !defined(RS_VENDOR_LIB) && !defined(RS_COMPATIBILITY_LIB)
101         ObjectBaseRef<ProgramFragment> mFragment;
102         ObjectBaseRef<ProgramVertex> mVertex;
103         ObjectBaseRef<ProgramStore> mStore;
104         ObjectBaseRef<ProgramRaster> mRaster;
105         ObjectBaseRef<Font> mFont;
106 #endif
107         Context *mRsc;
108     };
109 
110     RsSurfaceConfig mUserSurfaceConfig;
111 
112     ElementState mStateElement;
113     TypeState mStateType;
114     SamplerState mStateSampler;
115 
isSynchronous()116     bool isSynchronous() {return mSynchronous;}
117     bool setupCheck();
118 
119 #if !defined(RS_VENDOR_LIB) && !defined(RS_COMPATIBILITY_LIB)
120     FBOCache mFBOCache;
121     ProgramFragmentState mStateFragment;
122     ProgramStoreState mStateFragmentStore;
123     ProgramRasterState mStateRaster;
124     ProgramVertexState mStateVertex;
125     FontState mStateFont;
126 
127 
128     void swapBuffers();
129     void setRootScript(Script *);
130     void setProgramRaster(ProgramRaster *);
131     void setProgramVertex(ProgramVertex *);
132     void setProgramFragment(ProgramFragment *);
133     void setProgramStore(ProgramStore *);
134     void setFont(Font *);
135 
136     void updateSurface(void *sur);
137 
getProgramFragment()138     ProgramFragment * getProgramFragment() {return mFragment.get();}
getProgramStore()139     ProgramStore * getProgramStore() {return mFragmentStore.get();}
getProgramRaster()140     ProgramRaster * getProgramRaster() {return mRaster.get();}
getProgramVertex()141     ProgramVertex * getProgramVertex() {return mVertex.get();}
getFont()142     Font * getFont() {return mFont.get();}
143 
144     void setupProgramStore();
145 
146     void pause();
147     void resume();
148     void setSurface(uint32_t w, uint32_t h, RsNativeWindow sur);
149 #endif
150     void finish();
151 
152     void setPriority(int32_t p);
153     void destroyWorkerThreadResources();
154 
155     void assignName(ObjectBase *obj, const char *name, uint32_t len);
156     void removeName(ObjectBase *obj);
157 
158     RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID);
159     RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen);
160     bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const;
161     uint32_t runScript(Script *s);
162 
163     void initToClient();
164     void deinitToClient();
165 
166 #if !defined(RS_VENDOR_LIB) && !defined(RS_COMPATIBILITY_LIB)
getDefaultProgramFragment()167     ProgramFragment * getDefaultProgramFragment() const {
168         return mStateFragment.mDefault.get();
169     }
getDefaultProgramVertex()170     ProgramVertex * getDefaultProgramVertex() const {
171         return mStateVertex.mDefault.get();
172     }
getDefaultProgramStore()173     ProgramStore * getDefaultProgramStore() const {
174         return mStateFragmentStore.mDefault.get();
175     }
getDefaultProgramRaster()176     ProgramRaster * getDefaultProgramRaster() const {
177         return mStateRaster.mDefault.get();
178     }
getDefaultFont()179     Font* getDefaultFont() const {
180         return mStateFont.mDefault.get();
181     }
182 
getWidth()183     uint32_t getWidth() const {return mWidth;}
getHeight()184     uint32_t getHeight() const {return mHeight;}
185 
186     uint32_t getCurrentSurfaceWidth() const;
187     uint32_t getCurrentSurfaceHeight() const;
188 
setWatchdogGL(const char * cmd,uint32_t line,const char * file)189     void setWatchdogGL(const char *cmd, uint32_t line, const char *file) const {
190         watchdog.command = cmd;
191         watchdog.file = file;
192         watchdog.line = line;
193     }
194 #endif
195 
196     mutable ThreadIO mIO;
197 
198     // Timers
199     enum Timers {
200         RS_TIMER_IDLE,
201         RS_TIMER_INTERNAL,
202         RS_TIMER_SCRIPT,
203         RS_TIMER_CLEAR_SWAP,
204         _RS_TIMER_TOTAL
205     };
206     uint64_t getTime() const;
207     void timerInit();
208     void timerReset();
209     void timerSet(Timers);
210     void timerPrint();
211     void timerFrame();
212 
213     struct {
214         bool mLogTimes;
215         bool mLogScripts;
216         bool mLogShaders;
217         bool mLogShadersAttr;
218         bool mLogShadersUniforms;
219         bool mLogVisual;
220         uint32_t mLogReduce;
221         bool mDebugReduceSplitAccum;
222         uint32_t mDebugMaxThreads;
223     } props;
224 
225     mutable struct {
226         bool inRoot;
227         const char *command;
228         const char *file;
229         uint32_t line;
230     } watchdog;
231     static void printWatchdogInfo(void *ctx);
232 
233     void dumpDebug() const;
234     void setError(RsError e, const char *msg = nullptr) const;
235 
236     mutable const ObjectBase * mObjHead;
237 
getDPI()238     uint32_t getDPI() const {return mDPI;}
setDPI(uint32_t dpi)239     void setDPI(uint32_t dpi) {mDPI = dpi;}
240 
getTargetSdkVersion()241     uint32_t getTargetSdkVersion() const {return mTargetSdkVersion;}
setTargetSdkVersion(uint32_t sdkVer)242     void setTargetSdkVersion(uint32_t sdkVer) {mTargetSdkVersion = sdkVer;}
243 
getContextType()244     RsContextType getContextType() const { return mContextType; }
setContextType(RsContextType ct)245     void setContextType(RsContextType ct) { mContextType = ct; }
246 
247     // Check for Fatal errors
248     // Should be used to prevent work from being launched
249     // which could take the process down.  Maximizes the chance
250     // the process lives long enough to get the error to the developer
hadFatalError()251     bool hadFatalError() {return mFatalErrorOccured;}
252 
getOptLevel()253     uint32_t getOptLevel() const { return mOptLevel; }
setOptLevel(uint32_t optLevel)254     void setOptLevel(uint32_t optLevel) { mOptLevel = optLevel; }
255 
256     Device *mDev;
257 
258 #ifdef RS_COMPATIBILITY_LIB
setNativeLibDir(const char * libDir,uint32_t length)259     void setNativeLibDir(const char * libDir, uint32_t length) {
260         if (!hasSetNativeLibDir) {
261             if (length <= PATH_MAX) {
262                 memcpy(nativeLibDir, libDir, length);
263                 nativeLibDir[length] = 0;
264                 hasSetNativeLibDir = true;
265             } else {
266                 setError(RS_ERROR_BAD_VALUE, "Invalid path");
267             }
268         }
269     }
getNativeLibDir()270     const char * getNativeLibDir() {
271         return nativeLibDir;
272     }
273 #endif
274 
275     void setCacheDir(const char * cacheDir_arg, uint32_t length);
getCacheDir()276     const char * getCacheDir() {
277         if (hasSetCacheDir) {
278             return mCacheDir;
279         }
280         return nullptr;
281     }
282 
283     // Returns the actual loaded driver's name (like "libRSDriver.so").
getDriverName()284     const char * getDriverName() {
285         return mDriverName;
286     }
287 
288     // Set a new driver name, should be called from within
289     // rsdHalInit in order to alter default behaviour.
setDriverName(const char * name)290     void setDriverName(const char * name) {
291         if (!mDriverName) {
292             mDriverName = name;
293         }
294     }
295 
296 protected:
297 
298     uint32_t mTargetSdkVersion;
299     uint32_t mDPI;
300     uint32_t mWidth;
301     uint32_t mHeight;
302     int32_t mThreadPriority;
303     bool mIsGraphicsContext;
304 
305     bool mForceRSoV;
306     bool mForceCpu;
307 
308     RsContextType mContextType;
309     uint32_t mOptLevel;
310 
311     bool mRunning;
312     bool mExit;
313     bool mPaused;
314     mutable bool mFatalErrorOccured;
315     mutable RsError mError;
316 
317 
318     pthread_t mThreadId;
319     pid_t mNativeThreadId;
320 
321     ObjectBaseRef<Script> mRootScript;
322 #if !defined(RS_VENDOR_LIB) && !defined(RS_COMPATIBILITY_LIB)
323     ObjectBaseRef<ProgramFragment> mFragment;
324     ObjectBaseRef<ProgramVertex> mVertex;
325     ObjectBaseRef<ProgramStore> mFragmentStore;
326     ObjectBaseRef<ProgramRaster> mRaster;
327     ObjectBaseRef<Font> mFont;
328 #endif
329 
330     void displayDebugStats();
331 
332 private:
333     Context();
334     bool initContext(Device *, const RsSurfaceConfig *sc);
335     bool mSynchronous;
336     bool initGLThread();
337     void deinitEGL();
338 
339     uint32_t runRootScript();
340 
341     bool loadRuntime(const char* filename);
342     // Loads the driver.
343     // forceDefault: If true, loads the default CPU driver.
344     // forceRSoV:  If true, overrides forceDefault and loads the RSoV driver.
345     bool loadDriver(bool forceDefault, bool forceRSoV);
346     static void * threadProc(void *);
347     static void * helperThreadProc(void *);
348 
349     bool mHasSurface;
350     bool mIsContextLite;
351 
352     // This holds the name of the driver (like "libRSDriver.so").
353     // Since this is always just a static string, we don't have to
354     // allocate, copy, or free any memory here.
355     const char* mDriverName;
356     const char* mVendorDriverName;
357 
358     std::vector<ObjectBase *> mNames;
359 
360     // Sync fence id for Graphic API, default value -1.
361     int32_t mSyncFd = -1;
362     uint64_t mTimers[_RS_TIMER_TOTAL];
363     Timers mTimerActive;
364     uint64_t mTimeLast;
365     uint64_t mTimeFrame;
366     uint64_t mTimeLastFrame;
367     uint32_t mTimeMSLastFrame;
368     uint32_t mTimeMSLastScript;
369     uint32_t mTimeMSLastSwap;
370     uint32_t mAverageFPSFrameCount;
371     uint64_t mAverageFPSStartTime;
372     uint32_t mAverageFPS;
373 #ifdef RS_COMPATIBILITY_LIB
374     bool hasSetNativeLibDir = false;
375     char nativeLibDir[PATH_MAX+1];
376 #endif
377     bool hasSetCacheDir = false;
378     char mCacheDir[PATH_MAX+1];
379 };
380 
381 void LF_ObjDestroy_handcode(const Context *rsc, RsAsyncVoidPtr objPtr);
382 
383 } // namespace renderscript
384 } // namespace android
385 #endif
386