rsContext.h revision 0ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62
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#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
30#define ATRACE_TAG ATRACE_TAG_RS
31#include "utils/Trace.h"
32#else
33#define ATRACE_ENABLED(...) false
34#define ATRACE_NAME(...)
35#define ATRACE_CALL(...)
36#endif
37
38#ifndef RS_COMPATIBILITY_LIB
39#include "rsFont.h"
40#include "rsProgramFragment.h"
41#include "rsProgramStore.h"
42#include "rsProgramRaster.h"
43#include "rsProgramVertex.h"
44#include "rsFBOCache.h"
45
46#endif
47
48
49// ---------------------------------------------------------------------------
50namespace android {
51
52namespace renderscript {
53
54class Device;
55
56#if 0
57#define CHECK_OBJ(o) { \
58    GET_TLS(); \
59    if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) {  \
60        ALOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__);  \
61    } \
62}
63#define CHECK_OBJ_OR_NULL(o) { \
64    GET_TLS(); \
65    if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) {  \
66        ALOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__);  \
67    } \
68}
69#else
70#define CHECK_OBJ(o)
71#define CHECK_OBJ_OR_NULL(o)
72#endif
73
74
75
76class Context {
77public:
78    struct Hal {
79        void * drv;
80
81        RsdHalFunctions funcs;
82        uint32_t flags;
83    };
84    Hal mHal;
85
86    static Context * createContext(Device *, const RsSurfaceConfig *sc,
87            RsContextType ct = RS_CONTEXT_TYPE_NORMAL,
88            uint32_t flags = 0);
89    static Context * createContextLite();
90    ~Context();
91
92    static pthread_mutex_t gMessageMutex;
93    static pthread_mutex_t gInitMutex;
94    // Library mutex (for providing thread-safe calls from the runtime)
95    static pthread_mutex_t gLibMutex;
96
97    class PushState {
98    public:
99        PushState(Context *);
100        ~PushState();
101
102    private:
103#ifndef RS_COMPATIBILITY_LIB
104        ObjectBaseRef<ProgramFragment> mFragment;
105        ObjectBaseRef<ProgramVertex> mVertex;
106        ObjectBaseRef<ProgramStore> mStore;
107        ObjectBaseRef<ProgramRaster> mRaster;
108        ObjectBaseRef<Font> mFont;
109#endif
110        Context *mRsc;
111    };
112
113    RsSurfaceConfig mUserSurfaceConfig;
114
115    ElementState mStateElement;
116    TypeState mStateType;
117    SamplerState mStateSampler;
118
119    bool isSynchronous() {return mSynchronous;}
120    bool setupCheck();
121
122#ifndef RS_COMPATIBILITY_LIB
123    FBOCache mFBOCache;
124    ProgramFragmentState mStateFragment;
125    ProgramStoreState mStateFragmentStore;
126    ProgramRasterState mStateRaster;
127    ProgramVertexState mStateVertex;
128    FontState mStateFont;
129
130
131    void swapBuffers();
132    void setRootScript(Script *);
133    void setProgramRaster(ProgramRaster *);
134    void setProgramVertex(ProgramVertex *);
135    void setProgramFragment(ProgramFragment *);
136    void setProgramStore(ProgramStore *);
137    void setFont(Font *);
138
139    void updateSurface(void *sur);
140
141    ProgramFragment * getProgramFragment() {return mFragment.get();}
142    ProgramStore * getProgramStore() {return mFragmentStore.get();}
143    ProgramRaster * getProgramRaster() {return mRaster.get();}
144    ProgramVertex * getProgramVertex() {return mVertex.get();}
145    Font * getFont() {return mFont.get();}
146
147    void setupProgramStore();
148
149    void pause();
150    void resume();
151    void setSurface(uint32_t w, uint32_t h, RsNativeWindow sur);
152#endif
153    void finish();
154
155    void setPriority(int32_t p);
156    void destroyWorkerThreadResources();
157
158    void assignName(ObjectBase *obj, const char *name, uint32_t len);
159    void removeName(ObjectBase *obj);
160
161    RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID);
162    RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen);
163    bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const;
164    uint32_t runScript(Script *s);
165
166    void initToClient();
167    void deinitToClient();
168
169#ifndef RS_COMPATIBILITY_LIB
170    ProgramFragment * getDefaultProgramFragment() const {
171        return mStateFragment.mDefault.get();
172    }
173    ProgramVertex * getDefaultProgramVertex() const {
174        return mStateVertex.mDefault.get();
175    }
176    ProgramStore * getDefaultProgramStore() const {
177        return mStateFragmentStore.mDefault.get();
178    }
179    ProgramRaster * getDefaultProgramRaster() const {
180        return mStateRaster.mDefault.get();
181    }
182    Font* getDefaultFont() const {
183        return mStateFont.mDefault.get();
184    }
185
186    uint32_t getWidth() const {return mWidth;}
187    uint32_t getHeight() const {return mHeight;}
188
189    uint32_t getCurrentSurfaceWidth() const;
190    uint32_t getCurrentSurfaceHeight() const;
191
192    void setWatchdogGL(const char *cmd, uint32_t line, const char *file) const {
193        watchdog.command = cmd;
194        watchdog.file = file;
195        watchdog.line = line;
196    }
197#endif
198
199    mutable ThreadIO mIO;
200
201    // Timers
202    enum Timers {
203        RS_TIMER_IDLE,
204        RS_TIMER_INTERNAL,
205        RS_TIMER_SCRIPT,
206        RS_TIMER_CLEAR_SWAP,
207        _RS_TIMER_TOTAL
208    };
209    uint64_t getTime() const;
210    void timerInit();
211    void timerReset();
212    void timerSet(Timers);
213    void timerPrint();
214    void timerFrame();
215
216    struct {
217        bool mLogTimes;
218        bool mLogScripts;
219        bool mLogObjects;
220        bool mLogShaders;
221        bool mLogShadersAttr;
222        bool mLogShadersUniforms;
223        bool mLogVisual;
224        uint32_t mDebugMaxThreads;
225    } props;
226
227    mutable struct {
228        bool inRoot;
229        const char *command;
230        const char *file;
231        uint32_t line;
232    } watchdog;
233    static void printWatchdogInfo(void *ctx);
234
235    void dumpDebug() const;
236    void setError(RsError e, const char *msg = nullptr) const;
237
238    mutable const ObjectBase * mObjHead;
239
240    uint32_t getDPI() const {return mDPI;}
241    void setDPI(uint32_t dpi) {mDPI = dpi;}
242
243    uint32_t getTargetSdkVersion() const {return mTargetSdkVersion;}
244    void setTargetSdkVersion(uint32_t sdkVer) {mTargetSdkVersion = sdkVer;}
245
246    RsContextType getContextType() const { return mContextType; }
247    void setContextType(RsContextType ct) { mContextType = ct; }
248
249    Device *mDev;
250
251#ifdef RS_COMPATIBILITY_LIB
252    void setNativeLibDir(const char * libDir, uint32_t length) {
253        memcpy(nativeLibDir, libDir, length);
254    }
255    const char * getNativeLibDir() {
256        return nativeLibDir;
257    }
258#endif
259
260protected:
261
262    uint32_t mTargetSdkVersion;
263    uint32_t mDPI;
264    uint32_t mWidth;
265    uint32_t mHeight;
266    int32_t mThreadPriority;
267    bool mIsGraphicsContext;
268
269    bool mForceCpu;
270
271    RsContextType mContextType;
272
273    bool mRunning;
274    bool mExit;
275    bool mPaused;
276    mutable RsError mError;
277
278    pthread_t mThreadId;
279    pid_t mNativeThreadId;
280
281    ObjectBaseRef<Script> mRootScript;
282#ifndef RS_COMPATIBILITY_LIB
283    ObjectBaseRef<ProgramFragment> mFragment;
284    ObjectBaseRef<ProgramVertex> mVertex;
285    ObjectBaseRef<ProgramStore> mFragmentStore;
286    ObjectBaseRef<ProgramRaster> mRaster;
287    ObjectBaseRef<Font> mFont;
288#endif
289
290    void displayDebugStats();
291
292private:
293    Context();
294    bool initContext(Device *, const RsSurfaceConfig *sc);
295
296    bool mSynchronous;
297    bool initGLThread();
298    void deinitEGL();
299
300    uint32_t runRootScript();
301
302    bool loadRuntime(const char* filename);
303    bool loadDriver(bool forceDefault);
304    static void * threadProc(void *);
305    static void * helperThreadProc(void *);
306
307    bool mHasSurface;
308    bool mIsContextLite;
309
310    Vector<ObjectBase *> mNames;
311
312    uint64_t mTimers[_RS_TIMER_TOTAL];
313    Timers mTimerActive;
314    uint64_t mTimeLast;
315    uint64_t mTimeFrame;
316    uint64_t mTimeLastFrame;
317    uint32_t mTimeMSLastFrame;
318    uint32_t mTimeMSLastScript;
319    uint32_t mTimeMSLastSwap;
320    uint32_t mAverageFPSFrameCount;
321    uint64_t mAverageFPSStartTime;
322    uint32_t mAverageFPS;
323#ifdef RS_COMPATIBILITY_LIB
324    char nativeLibDir[PATH_MAX+1];
325#endif
326};
327
328void LF_ObjDestroy_handcode(const Context *rsc, RsAsyncVoidPtr objPtr);
329
330} // renderscript
331} // android
332#endif
333