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
23#include "rsThreadIO.h"
24#include "rsScriptC.h"
25#include "rsScriptGroup.h"
26#include "rsSampler.h"
27#include <string.h>
28
29// ---------------------------------------------------------------------------
30namespace android {
31
32namespace renderscript {
33
34class Device;
35
36#if 0
37#define CHECK_OBJ(o) { \
38    GET_TLS(); \
39    if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) {  \
40        ALOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__);  \
41    } \
42}
43#define CHECK_OBJ_OR_NULL(o) { \
44    GET_TLS(); \
45    if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) {  \
46        ALOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__);  \
47    } \
48}
49#else
50#define CHECK_OBJ(o)
51#define CHECK_OBJ_OR_NULL(o)
52#endif
53
54class Context {
55public:
56    struct Hal {
57        void * drv;
58
59        RsdHalFunctions funcs;
60    };
61    Hal mHal;
62
63    static Context * createContext(Device *, const RsSurfaceConfig *sc);
64    static Context * createContextLite();
65    ~Context();
66
67    static pthread_mutex_t gInitMutex;
68    // Library mutex (for providing thread-safe calls from the runtime)
69    static pthread_mutex_t gLibMutex;
70
71    class PushState {
72    public:
73        PushState(Context *);
74        ~PushState();
75
76    private:
77        Context *mRsc;
78    };
79
80    RsSurfaceConfig mUserSurfaceConfig;
81
82    ElementState mStateElement;
83    TypeState mStateType;
84    SamplerState mStateSampler;
85
86    ScriptCState mScriptC;
87
88    bool setupCheck();
89
90    void setPriority(int32_t p);
91    void destroyWorkerThreadResources();
92
93    void assignName(ObjectBase *obj, const char *name, uint32_t len);
94    void removeName(ObjectBase *obj);
95
96    RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID);
97    RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen);
98    bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const;
99    uint32_t runScript(Script *s);
100
101    void initToClient();
102    void deinitToClient();
103
104    mutable ThreadIO mIO;
105
106    // Timers
107    enum Timers {
108        RS_TIMER_IDLE,
109        RS_TIMER_INTERNAL,
110        RS_TIMER_SCRIPT,
111        RS_TIMER_CLEAR_SWAP,
112        _RS_TIMER_TOTAL
113    };
114    uint64_t getTime() const;
115    void timerInit();
116    void timerReset();
117    void timerSet(Timers);
118    void timerPrint();
119    void timerFrame();
120
121    struct {
122        bool mLogTimes;
123        bool mLogScripts;
124        bool mLogObjects;
125        bool mLogShaders;
126        bool mLogShadersAttr;
127        bool mLogShadersUniforms;
128        bool mLogVisual;
129        uint32_t mDebugMaxThreads;
130    } props;
131
132    mutable struct {
133        bool inRoot;
134        const char *command;
135        const char *file;
136        uint32_t line;
137    } watchdog;
138    static void printWatchdogInfo(void *ctx);
139
140    void dumpDebug() const;
141    void setError(RsError e, const char *msg = NULL) const;
142
143    mutable const ObjectBase * mObjHead;
144
145    uint32_t getDPI() const {return mDPI;}
146    void setDPI(uint32_t dpi) {mDPI = dpi;}
147
148    uint32_t getTargetSdkVersion() const {return mTargetSdkVersion;}
149    void setTargetSdkVersion(uint32_t sdkVer) {mTargetSdkVersion = sdkVer;}
150
151    Device *mDev;
152protected:
153
154    uint32_t mTargetSdkVersion;
155    uint32_t mDPI;
156    uint32_t mWidth;
157    uint32_t mHeight;
158    int32_t mThreadPriority;
159    bool mIsGraphicsContext;
160
161    bool mRunning;
162    bool mExit;
163    bool mPaused;
164    mutable RsError mError;
165
166    pthread_t mThreadId;
167    pid_t mNativeThreadId;
168
169    ObjectBaseRef<Script> mRootScript;
170
171    void displayDebugStats();
172
173private:
174    Context();
175    bool initContext(Device *, const RsSurfaceConfig *sc);
176
177
178    bool initGLThread();
179    void deinitEGL();
180
181    uint32_t runRootScript();
182
183    static void * threadProc(void *);
184    static void * helperThreadProc(void *);
185
186    bool mHasSurface;
187    bool mIsContextLite;
188
189    Vector<ObjectBase *> mNames;
190
191    uint64_t mTimers[_RS_TIMER_TOTAL];
192    Timers mTimerActive;
193    uint64_t mTimeLast;
194    uint64_t mTimeFrame;
195    uint64_t mTimeLastFrame;
196    uint32_t mTimeMSLastFrame;
197    uint32_t mTimeMSLastScript;
198    uint32_t mTimeMSLastSwap;
199    uint32_t mAverageFPSFrameCount;
200    uint64_t mAverageFPSStartTime;
201    uint32_t mAverageFPS;
202};
203
204} // renderscript
205} // android
206#endif
207