1326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams/*
2a04e30dbb5ab11592b03666bb3d102070759c58eAlex Sakhartchouk * Copyright (C) 2011 The Android Open Source Project
3326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
4326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * Licensed under the Apache License, Version 2.0 (the "License");
5326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * you may not use this file except in compliance with the License.
6326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * You may obtain a copy of the License at
7326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
8326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *      http://www.apache.org/licenses/LICENSE-2.0
9326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
10326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * Unless required by applicable law or agreed to in writing, software
11326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * distributed under the License is distributed on an "AS IS" BASIS,
12326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * See the License for the specific language governing permissions and
14326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * limitations under the License.
15326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams */
16326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
17326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#ifndef ANDROID_RS_CONTEXT_H
18326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#define ANDROID_RS_CONTEXT_H
19326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
20a5577808737a394bdb156fbeb80018d0a4d8438dJason Sams#include "rsUtils.h"
21bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams#include "rs_hal.h"
2293eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#include <string.h>
23bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams
2477d9f4bd05b2d2a161f30c12a2248f9c97eaac42Alex Sakhartchouk#include "rsThreadIO.h"
25326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#include "rsScriptC.h"
26dbe66d6783c1e53cd1572de0ef6ef6fdf6f76f48Jason Sams#include "rsScriptGroup.h"
27326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#include "rsSampler.h"
2893eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams
29574854bcb2eb25a85b9b52faf2fb3e743fa7aa14Stephen Hines#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
30fa85e9143c6f6a227c67bab5f08ff86194abdf2aTim Murray#define ATRACE_TAG ATRACE_TAG_RS
31fa85e9143c6f6a227c67bab5f08ff86194abdf2aTim Murray#include "utils/Trace.h"
32574854bcb2eb25a85b9b52faf2fb3e743fa7aa14Stephen Hines#else
33574854bcb2eb25a85b9b52faf2fb3e743fa7aa14Stephen Hines#define ATRACE_ENABLED(...) false
34574854bcb2eb25a85b9b52faf2fb3e743fa7aa14Stephen Hines#define ATRACE_NAME(...)
35574854bcb2eb25a85b9b52faf2fb3e743fa7aa14Stephen Hines#define ATRACE_CALL(...)
36fa85e9143c6f6a227c67bab5f08ff86194abdf2aTim Murray#endif
37fa85e9143c6f6a227c67bab5f08ff86194abdf2aTim Murray
3893eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
39d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk#include "rsFont.h"
40326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#include "rsProgramFragment.h"
41ccc010bb7c0f89e162bf60033968a20be90a903aJason Sams#include "rsProgramStore.h"
425fd09d847586f9680b4f495413b6ca5fbb69af6eJason Sams#include "rsProgramRaster.h"
43326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#include "rsProgramVertex.h"
447d9c5ffccb7a5e682860f752403e5a03aed587beAlex Sakhartchouk#include "rsFBOCache.h"
45fa85e9143c6f6a227c67bab5f08ff86194abdf2aTim Murray
4693eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
4793eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams
4893fbc61ad282cbbf4a6a93396fdd8747b1fe3451Stephen McGroarty/*
4993fbc61ad282cbbf4a6a93396fdd8747b1fe3451Stephen McGroarty * This global will be found by the debugger and will have its value flipped.
5093fbc61ad282cbbf4a6a93396fdd8747b1fe3451Stephen McGroarty * It's independent of the Context class to allow the debugger to do the above
5193fbc61ad282cbbf4a6a93396fdd8747b1fe3451Stephen McGroarty * without knowing the type makeup. This allows the debugger to be attached at
5293fbc61ad282cbbf4a6a93396fdd8747b1fe3451Stephen McGroarty * an earlier stage.
5393fbc61ad282cbbf4a6a93396fdd8747b1fe3451Stephen McGroarty*/
5493fbc61ad282cbbf4a6a93396fdd8747b1fe3451Stephen McGroartyextern "C" int gDebuggerPresent;
55326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
56326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams// ---------------------------------------------------------------------------
57326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsnamespace android {
58d4c25e3c197bc79c8508b76b20611b67ef883138Mathias Agopian
59326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsnamespace renderscript {
60326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
614edf030cbb7c6ac08dc563335c2af73c20f6e2e5Alex Sakhartchoukclass Device;
624edf030cbb7c6ac08dc563335c2af73c20f6e2e5Alex Sakhartchouk
63605048a9f9af925782f6c90bc1cdc5af59e337d8Jason Sams#if 0
64605048a9f9af925782f6c90bc1cdc5af59e337d8Jason Sams#define CHECK_OBJ(o) { \
65605048a9f9af925782f6c90bc1cdc5af59e337d8Jason Sams    GET_TLS(); \
66afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (!ObjectBase::isValid(rsc, (const ObjectBase *)o)) {  \
67af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__);  \
68605048a9f9af925782f6c90bc1cdc5af59e337d8Jason Sams    } \
69605048a9f9af925782f6c90bc1cdc5af59e337d8Jason Sams}
70605048a9f9af925782f6c90bc1cdc5af59e337d8Jason Sams#define CHECK_OBJ_OR_NULL(o) { \
71605048a9f9af925782f6c90bc1cdc5af59e337d8Jason Sams    GET_TLS(); \
72afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchouk    if (o && !ObjectBase::isValid(rsc, (const ObjectBase *)o)) {  \
73af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("Bad object %p at %s, %i", o, __FILE__, __LINE__);  \
74605048a9f9af925782f6c90bc1cdc5af59e337d8Jason Sams    } \
75605048a9f9af925782f6c90bc1cdc5af59e337d8Jason Sams}
76605048a9f9af925782f6c90bc1cdc5af59e337d8Jason Sams#else
77605048a9f9af925782f6c90bc1cdc5af59e337d8Jason Sams#define CHECK_OBJ(o)
78605048a9f9af925782f6c90bc1cdc5af59e337d8Jason Sams#define CHECK_OBJ_OR_NULL(o)
79605048a9f9af925782f6c90bc1cdc5af59e337d8Jason Sams#endif
80605048a9f9af925782f6c90bc1cdc5af59e337d8Jason Sams
81fa85e9143c6f6a227c67bab5f08ff86194abdf2aTim Murray
82fa85e9143c6f6a227c67bab5f08ff86194abdf2aTim Murray
83afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukclass Context {
84326e0ddf89e8df2837752fbfd7a014814b32082cJason Samspublic:
85bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams    struct Hal {
86bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams        void * drv;
87bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams
88bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams        RsdHalFunctions funcs;
89416f4b6d3ee6f30b747e7cd556b28d84b0226cf3Jason Sams        uint32_t flags;
90bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams    };
91bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams    Hal mHal;
92bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams
93cca3d6ca444bef3b6d75431ec19bd07bfe40a733Stephen Hines    static Context * createContext(Device *, const RsSurfaceConfig *sc,
94cca3d6ca444bef3b6d75431ec19bd07bfe40a733Stephen Hines            RsContextType ct = RS_CONTEXT_TYPE_NORMAL,
9584e3dea053bea25c1cec44ffb298f8b5b9b9141aTim Murray            uint32_t flags = 0);
96b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk    static Context * createContextLite();
97326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    ~Context();
98326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
994961cceab2b71bf0ab59e1b66a7559f67ed28781Jason Sams    static pthread_mutex_t gMessageMutex;
100fb03a22ad2adadc1ff50a8b50d43ad7fcc3fa6edJason Sams    static pthread_mutex_t gInitMutex;
101ca3f09c0924e9515901dfd47fa5f95385d53cf80Stephen Hines    // Library mutex (for providing thread-safe calls from the runtime)
102ca3f09c0924e9515901dfd47fa5f95385d53cf80Stephen Hines    static pthread_mutex_t gLibMutex;
103fb03a22ad2adadc1ff50a8b50d43ad7fcc3fa6edJason Sams
10460709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams    class PushState {
10560709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams    public:
10660709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams        PushState(Context *);
10760709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams        ~PushState();
10860709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams
10960709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams    private:
11093eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
11160709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams        ObjectBaseRef<ProgramFragment> mFragment;
11260709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams        ObjectBaseRef<ProgramVertex> mVertex;
11360709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams        ObjectBaseRef<ProgramStore> mStore;
11460709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams        ObjectBaseRef<ProgramRaster> mRaster;
11560709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams        ObjectBaseRef<Font> mFont;
11693eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
11760709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams        Context *mRsc;
11860709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams    };
11960709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams
1206b8552a4f6a44848255d77222e66aa92dd21b1b5Jason Sams    RsSurfaceConfig mUserSurfaceConfig;
121e57691037aea219562ac686429b4b98202aab7bcJason Sams
122326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    ElementState mStateElement;
123326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    TypeState mStateType;
124326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    SamplerState mStateSampler;
12593eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams
12693eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams    bool isSynchronous() {return mSynchronous;}
12793eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams    bool setupCheck();
12893eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams
12993eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
13093eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams    FBOCache mFBOCache;
131326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    ProgramFragmentState mStateFragment;
132ccc010bb7c0f89e162bf60033968a20be90a903aJason Sams    ProgramStoreState mStateFragmentStore;
1335fd09d847586f9680b4f495413b6ca5fbb69af6eJason Sams    ProgramRasterState mStateRaster;
134326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    ProgramVertexState mStateVertex;
135d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk    FontState mStateFont;
136326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
1374d252d6e807b89764dad123ac845df298c52ca97Tim Murray
138326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    void swapBuffers();
139326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    void setRootScript(Script *);
14060709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams    void setProgramRaster(ProgramRaster *);
14160709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams    void setProgramVertex(ProgramVertex *);
14260709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams    void setProgramFragment(ProgramFragment *);
14360709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams    void setProgramStore(ProgramStore *);
144d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk    void setFont(Font *);
145326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
146326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    void updateSurface(void *sur);
147326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
14860709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams    ProgramFragment * getProgramFragment() {return mFragment.get();}
14960709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams    ProgramStore * getProgramStore() {return mFragmentStore.get();}
15060709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams    ProgramRaster * getProgramRaster() {return mRaster.get();}
15160709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams    ProgramVertex * getProgramVertex() {return mVertex.get();}
152d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk    Font * getFont() {return mFont.get();}
153326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
154889fe50e7aaebed8cb8284b16a0e51e64e8a3a9cAlex Sakhartchouk    void setupProgramStore();
155326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
15686f1b23aaaf9b8822a009d8c3e585e46768abb6aJason Sams    void pause();
15786f1b23aaaf9b8822a009d8c3e585e46768abb6aJason Sams    void resume();
1587257c7ee4b66f00c43d9235f3ac600061ae79968Alex Sakhartchouk    void setSurface(uint32_t w, uint32_t h, RsNativeWindow sur);
15993eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
160f2748276ea84bed83728e38a6661946b5fb3bc5cJason Sams    void finish();
16193eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams
162158324456b8dbf40f684dd0a4af3493b9549f3f5Jason Sams    void setPriority(int32_t p);
163741aac95b777b2e6cb90f484a05e489a79a6ef05Jason Sams    void destroyWorkerThreadResources();
16486f1b23aaaf9b8822a009d8c3e585e46768abb6aJason Sams
165a4a54e42fc710a62b47cbcb9d64c34a190429d9eJason Sams    void assignName(ObjectBase *obj, const char *name, uint32_t len);
166a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216Jason Sams    void removeName(ObjectBase *obj);
1678ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams
1681a4efa363916977ef9aeab756725b3bdc880a15bJason Sams    RsMessageToClientType peekMessageToClient(size_t *receiveLen, uint32_t *subID);
1691a4efa363916977ef9aeab756725b3bdc880a15bJason Sams    RsMessageToClientType getMessageToClient(void *data, size_t *receiveLen, uint32_t *subID, size_t bufferLen);
17087319de2b16a185cf360827c96a42cf1fcaae744Jason Sams    bool sendMessageToClient(const void *data, RsMessageToClientType cmdID, uint32_t subID, size_t len, bool waitForSpace) const;
171c61346b91434307c5003029017b54ce9c49112beJason Sams    uint32_t runScript(Script *s);
1728c401effb0837155fc39ca0364f57a882d127d38Jason Sams
1738c401effb0837155fc39ca0364f57a882d127d38Jason Sams    void initToClient();
1748c401effb0837155fc39ca0364f57a882d127d38Jason Sams    void deinitToClient();
1758c401effb0837155fc39ca0364f57a882d127d38Jason Sams
17693eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
1778ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    ProgramFragment * getDefaultProgramFragment() const {
1788ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams        return mStateFragment.mDefault.get();
1798ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    }
1808ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    ProgramVertex * getDefaultProgramVertex() const {
1818ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams        return mStateVertex.mDefault.get();
1828ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    }
183ccc010bb7c0f89e162bf60033968a20be90a903aJason Sams    ProgramStore * getDefaultProgramStore() const {
1848ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams        return mStateFragmentStore.mDefault.get();
1858ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    }
1865fd09d847586f9680b4f495413b6ca5fbb69af6eJason Sams    ProgramRaster * getDefaultProgramRaster() const {
1875fd09d847586f9680b4f495413b6ca5fbb69af6eJason Sams        return mStateRaster.mDefault.get();
1885fd09d847586f9680b4f495413b6ca5fbb69af6eJason Sams    }
189d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk    Font* getDefaultFont() const {
190d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk        return mStateFont.mDefault.get();
191d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk    }
1928ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams
193771565f47fc44608444c00aa8fa3bda769ceaeceJason Sams    uint32_t getWidth() const {return mWidth;}
194771565f47fc44608444c00aa8fa3bda769ceaeceJason Sams    uint32_t getHeight() const {return mHeight;}
195e579df42e85d9e00f53c42ef1b78dbd209dba989Jason Sams
196a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk    uint32_t getCurrentSurfaceWidth() const;
197a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk    uint32_t getCurrentSurfaceHeight() const;
198a74a8f635ce4fae0a9d4b9c79e9fa412787bf6a2Alex Sakhartchouk
19993eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams    void setWatchdogGL(const char *cmd, uint32_t line, const char *file) const {
20093eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams        watchdog.command = cmd;
20193eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams        watchdog.file = file;
20293eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams        watchdog.line = line;
20393eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams    }
20493eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
20593eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams
20687319de2b16a185cf360827c96a42cf1fcaae744Jason Sams    mutable ThreadIO mIO;
207fcd3192ebff8ab58d841836b7e94361d0998338cJason Sams
20824371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    // Timers
20924371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    enum Timers {
21024371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams        RS_TIMER_IDLE,
21124371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams        RS_TIMER_INTERNAL,
21224371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams        RS_TIMER_SCRIPT,
21324371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams        RS_TIMER_CLEAR_SWAP,
21424371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams        _RS_TIMER_TOTAL
21524371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    };
21624371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    uint64_t getTime() const;
21724371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    void timerInit();
21824371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    void timerReset();
21924371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    void timerSet(Timers);
22024371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    void timerPrint();
2211d54f10f3c23e0d7ec57e52ec3b0701a2a5ed24eJason Sams    void timerFrame();
22224371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams
2231fddd90849deaae89b546ff492c345d485bbce42Jason Sams    struct {
2241fddd90849deaae89b546ff492c345d485bbce42Jason Sams        bool mLogTimes;
2251fddd90849deaae89b546ff492c345d485bbce42Jason Sams        bool mLogScripts;
226cd50653f99c960e1a47c2c30e53b369b8805344aJason Sams        bool mLogShaders;
227886f11ade9dde05485cb11c0d67d87f76a428f6cAlex Sakhartchouk        bool mLogShadersAttr;
228886f11ade9dde05485cb11c0d67d87f76a428f6cAlex Sakhartchouk        bool mLogShadersUniforms;
2290cae59f7d8dd63d1bf0ca4abedecb4cfa3ab1921Alex Sakhartchouk        bool mLogVisual;
230d1f7da6803a1bfc0bf8129a66316cfb8994e7110Jason Sams        uint32_t mDebugMaxThreads;
2311fddd90849deaae89b546ff492c345d485bbce42Jason Sams    } props;
23276371fff76412fd020e24ddb8bf1ddb5c75f0ed1Joe Onorato
2332382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    mutable struct {
2342382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        bool inRoot;
2352382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        const char *command;
2362382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        const char *file;
2372382aba4a55c6ae74789c478eead8fbd96593321Jason Sams        uint32_t line;
2382382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    } watchdog;
2392382aba4a55c6ae74789c478eead8fbd96593321Jason Sams    static void printWatchdogInfo(void *ctx);
2402382aba4a55c6ae74789c478eead8fbd96593321Jason Sams
24113e2634a71a30d289ed8d821aef61c7d1687460eJason Sams    void dumpDebug() const;
24244bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    void setError(RsError e, const char *msg = nullptr) const;
24313e2634a71a30d289ed8d821aef61c7d1687460eJason Sams
244e514b45de8561fbc6ef6770845102ca10b0a69d7Jason Sams    mutable const ObjectBase * mObjHead;
245e514b45de8561fbc6ef6770845102ca10b0a69d7Jason Sams
2467b3e9bd825901e33661e3c385e3e7c6f40ca6000Alex Sakhartchouk    uint32_t getDPI() const {return mDPI;}
2477b3e9bd825901e33661e3c385e3e7c6f40ca6000Alex Sakhartchouk    void setDPI(uint32_t dpi) {mDPI = dpi;}
2487bf29ddc35450d8064541c42c99a1f48be6cf0ddJason Sams
249cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines    uint32_t getTargetSdkVersion() const {return mTargetSdkVersion;}
250cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines    void setTargetSdkVersion(uint32_t sdkVer) {mTargetSdkVersion = sdkVer;}
251cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines
252cca3d6ca444bef3b6d75431ec19bd07bfe40a733Stephen Hines    RsContextType getContextType() const { return mContextType; }
253cca3d6ca444bef3b6d75431ec19bd07bfe40a733Stephen Hines    void setContextType(RsContextType ct) { mContextType = ct; }
254cca3d6ca444bef3b6d75431ec19bd07bfe40a733Stephen Hines
2554efe3d38ee1d7b76c93a47ce6218b1f708fd4d9aJason Sams    // Check for Fatal errors
2564efe3d38ee1d7b76c93a47ce6218b1f708fd4d9aJason Sams    // Should be used to prevent work from being launched
2574efe3d38ee1d7b76c93a47ce6218b1f708fd4d9aJason Sams    // which could take the process down.  Maximizes the chance
2584efe3d38ee1d7b76c93a47ce6218b1f708fd4d9aJason Sams    // the process lives long enough to get the error to the developer
2594efe3d38ee1d7b76c93a47ce6218b1f708fd4d9aJason Sams    bool hadFatalError() {return mFatalErrorOccured;}
2604efe3d38ee1d7b76c93a47ce6218b1f708fd4d9aJason Sams
261326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    Device *mDev;
262f3213d7fd648da98bb3b03204eaf90f03c31926bMiao Wang
263f3213d7fd648da98bb3b03204eaf90f03c31926bMiao Wang#ifdef RS_COMPATIBILITY_LIB
264f3213d7fd648da98bb3b03204eaf90f03c31926bMiao Wang    void setNativeLibDir(const char * libDir, uint32_t length) {
265ea6e0621b4716ed77635f9d81787ee7057752af1Tim Murray        if (!hasSetNativeLibDir) {
266ea6e0621b4716ed77635f9d81787ee7057752af1Tim Murray            if (length <= PATH_MAX) {
267ea6e0621b4716ed77635f9d81787ee7057752af1Tim Murray                memcpy(nativeLibDir, libDir, length);
268867ce7f7985175e77f679141b698c474bd52b4c1Jason Sams                nativeLibDir[length] = 0;
269ea6e0621b4716ed77635f9d81787ee7057752af1Tim Murray                hasSetNativeLibDir = true;
270ea6e0621b4716ed77635f9d81787ee7057752af1Tim Murray            } else {
271ea6e0621b4716ed77635f9d81787ee7057752af1Tim Murray                setError(RS_ERROR_BAD_VALUE, "Invalid path");
272ea6e0621b4716ed77635f9d81787ee7057752af1Tim Murray            }
273ea6e0621b4716ed77635f9d81787ee7057752af1Tim Murray        }
274f3213d7fd648da98bb3b03204eaf90f03c31926bMiao Wang    }
275f3213d7fd648da98bb3b03204eaf90f03c31926bMiao Wang    const char * getNativeLibDir() {
276f3213d7fd648da98bb3b03204eaf90f03c31926bMiao Wang        return nativeLibDir;
277f3213d7fd648da98bb3b03204eaf90f03c31926bMiao Wang    }
278f3213d7fd648da98bb3b03204eaf90f03c31926bMiao Wang#endif
279f3213d7fd648da98bb3b03204eaf90f03c31926bMiao Wang
280867ce7f7985175e77f679141b698c474bd52b4c1Jason Sams    void setCacheDir(const char * cacheDir_arg, uint32_t length);
281ea6e0621b4716ed77635f9d81787ee7057752af1Tim Murray    const char * getCacheDir() {
282867ce7f7985175e77f679141b698c474bd52b4c1Jason Sams        if (hasSetCacheDir) {
283867ce7f7985175e77f679141b698c474bd52b4c1Jason Sams            return mCacheDir;
284867ce7f7985175e77f679141b698c474bd52b4c1Jason Sams        }
285867ce7f7985175e77f679141b698c474bd52b4c1Jason Sams        return nullptr;
286ea6e0621b4716ed77635f9d81787ee7057752af1Tim Murray    }
287ea6e0621b4716ed77635f9d81787ee7057752af1Tim Murray
288d97617a0a736b4dadcf978bfc1fa0aef0d4d50d3Jason Sams    // Returns the actual loaded driver's name (like "libRSDriver.so").
289d97617a0a736b4dadcf978bfc1fa0aef0d4d50d3Jason Sams    const char * getDriverName() {
290d97617a0a736b4dadcf978bfc1fa0aef0d4d50d3Jason Sams        return mDriverName;
291d97617a0a736b4dadcf978bfc1fa0aef0d4d50d3Jason Sams    }
292d97617a0a736b4dadcf978bfc1fa0aef0d4d50d3Jason Sams
293ea2e4d37418c6ada5d3c8cd08b8e60348dd46790Jon Parr    // Set a new driver name, should be called from within
294ea2e4d37418c6ada5d3c8cd08b8e60348dd46790Jon Parr    // rsdHalInit in order to alter default behaviour.
295ea2e4d37418c6ada5d3c8cd08b8e60348dd46790Jon Parr    void setDriverName(const char * name) {
296ea2e4d37418c6ada5d3c8cd08b8e60348dd46790Jon Parr        if (!mDriverName) {
297ea2e4d37418c6ada5d3c8cd08b8e60348dd46790Jon Parr            mDriverName = name;
298ea2e4d37418c6ada5d3c8cd08b8e60348dd46790Jon Parr        }
299ea2e4d37418c6ada5d3c8cd08b8e60348dd46790Jon Parr    }
300ea2e4d37418c6ada5d3c8cd08b8e60348dd46790Jon Parr
301ea6e0621b4716ed77635f9d81787ee7057752af1Tim Murray
3024b3de47071d875faaa7d419d050a464b09538797Jason Samsprotected:
303326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
304cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines    uint32_t mTargetSdkVersion;
3057b3e9bd825901e33661e3c385e3e7c6f40ca6000Alex Sakhartchouk    uint32_t mDPI;
306613cad1702dbb76eb2a6ba0cfcb43b9fe207cebcJason Sams    uint32_t mWidth;
307613cad1702dbb76eb2a6ba0cfcb43b9fe207cebcJason Sams    uint32_t mHeight;
3082dca84dd6c07992f78ad050177975f16486dd77eJason Sams    int32_t mThreadPriority;
3094820e8bb83b1f78e6232ebe853221f737da2a1eaJason Sams    bool mIsGraphicsContext;
310613cad1702dbb76eb2a6ba0cfcb43b9fe207cebcJason Sams
3110e92fa3775b3537c29a7a0ca603d892548936038Tim Murray    bool mForceCpu;
3120e92fa3775b3537c29a7a0ca603d892548936038Tim Murray
313cca3d6ca444bef3b6d75431ec19bd07bfe40a733Stephen Hines    RsContextType mContextType;
314cca3d6ca444bef3b6d75431ec19bd07bfe40a733Stephen Hines
315326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    bool mRunning;
316326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    bool mExit;
31786f1b23aaaf9b8822a009d8c3e585e46768abb6aJason Sams    bool mPaused;
3184efe3d38ee1d7b76c93a47ce6218b1f708fd4d9aJason Sams    mutable bool mFatalErrorOccured;
31987319de2b16a185cf360827c96a42cf1fcaae744Jason Sams    mutable RsError mError;
320326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
3214efe3d38ee1d7b76c93a47ce6218b1f708fd4d9aJason Sams
322326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    pthread_t mThreadId;
323158324456b8dbf40f684dd0a4af3493b9549f3f5Jason Sams    pid_t mNativeThreadId;
324326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
325326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    ObjectBaseRef<Script> mRootScript;
32693eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
327326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    ObjectBaseRef<ProgramFragment> mFragment;
328326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    ObjectBaseRef<ProgramVertex> mVertex;
329ccc010bb7c0f89e162bf60033968a20be90a903aJason Sams    ObjectBaseRef<ProgramStore> mFragmentStore;
3305fd09d847586f9680b4f495413b6ca5fbb69af6eJason Sams    ObjectBaseRef<ProgramRaster> mRaster;
331d3e0ad43dc758c409fc23d1893dab67b18520c24Alex Sakhartchouk    ObjectBaseRef<Font> mFont;
33293eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
3335086938044e0a9b6b1138f915d0d252fe046e102Jason Sams
3340cae59f7d8dd63d1bf0ca4abedecb4cfa3ab1921Alex Sakhartchouk    void displayDebugStats();
3350cae59f7d8dd63d1bf0ca4abedecb4cfa3ab1921Alex Sakhartchouk
336326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsprivate:
337326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    Context();
3385c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams    bool initContext(Device *, const RsSurfaceConfig *sc);
33993fbc61ad282cbbf4a6a93396fdd8747b1fe3451Stephen McGroarty    void waitForDebugger();
3404d252d6e807b89764dad123ac845df298c52ca97Tim Murray    bool mSynchronous;
3415c1c79a54c63b9de8c391f7ed890c02f280ec17fJason Sams    bool initGLThread();
34233b6e3b91329080e5cdd0b8fdbcd3e6a906032aeJason Sams    void deinitEGL();
343326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
3442dca84dd6c07992f78ad050177975f16486dd77eJason Sams    uint32_t runRootScript();
345326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
3460ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    bool loadRuntime(const char* filename);
3470ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    bool loadDriver(bool forceDefault);
348326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    static void * threadProc(void *);
3497bf29ddc35450d8064541c42c99a1f48be6cf0ddJason Sams    static void * helperThreadProc(void *);
350326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
3517257c7ee4b66f00c43d9235f3ac600061ae79968Alex Sakhartchouk    bool mHasSurface;
352b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk    bool mIsContextLite;
353a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216Jason Sams
354d97617a0a736b4dadcf978bfc1fa0aef0d4d50d3Jason Sams    // This holds the name of the driver (like "libRSDriver.so").
355d97617a0a736b4dadcf978bfc1fa0aef0d4d50d3Jason Sams    // Since this is always just a static string, we don't have to
356d97617a0a736b4dadcf978bfc1fa0aef0d4d50d3Jason Sams    // allocate, copy, or free any memory here.
357d97617a0a736b4dadcf978bfc1fa0aef0d4d50d3Jason Sams    const char* mDriverName;
358d97617a0a736b4dadcf978bfc1fa0aef0d4d50d3Jason Sams
359b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni    Vector<ObjectBase *> mNames;
36024371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams
36124371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    uint64_t mTimers[_RS_TIMER_TOTAL];
36224371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    Timers mTimerActive;
36324371d93cdb6999971c4058f78974da3c3d5fc64Jason Sams    uint64_t mTimeLast;
3641d54f10f3c23e0d7ec57e52ec3b0701a2a5ed24eJason Sams    uint64_t mTimeFrame;
3651d54f10f3c23e0d7ec57e52ec3b0701a2a5ed24eJason Sams    uint64_t mTimeLastFrame;
3662dca84dd6c07992f78ad050177975f16486dd77eJason Sams    uint32_t mTimeMSLastFrame;
3672dca84dd6c07992f78ad050177975f16486dd77eJason Sams    uint32_t mTimeMSLastScript;
3682dca84dd6c07992f78ad050177975f16486dd77eJason Sams    uint32_t mTimeMSLastSwap;
369c8fb69e4a3e01501a3d38a6d3ea185e583d3f493Alex Sakhartchouk    uint32_t mAverageFPSFrameCount;
370c8fb69e4a3e01501a3d38a6d3ea185e583d3f493Alex Sakhartchouk    uint64_t mAverageFPSStartTime;
371c8fb69e4a3e01501a3d38a6d3ea185e583d3f493Alex Sakhartchouk    uint32_t mAverageFPS;
372f3213d7fd648da98bb3b03204eaf90f03c31926bMiao Wang#ifdef RS_COMPATIBILITY_LIB
373ea6e0621b4716ed77635f9d81787ee7057752af1Tim Murray    bool hasSetNativeLibDir = false;
374f3213d7fd648da98bb3b03204eaf90f03c31926bMiao Wang    char nativeLibDir[PATH_MAX+1];
375f3213d7fd648da98bb3b03204eaf90f03c31926bMiao Wang#endif
376ea6e0621b4716ed77635f9d81787ee7057752af1Tim Murray    bool hasSetCacheDir = false;
377ea6e0621b4716ed77635f9d81787ee7057752af1Tim Murray    char mCacheDir[PATH_MAX+1];
378326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams};
379326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
380397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murrayvoid LF_ObjDestroy_handcode(const Context *rsc, RsAsyncVoidPtr objPtr);
381397b0b17fd26439eb82a76fea903952b88e5cc96Tim Murray
38277d9f4bd05b2d2a161f30c12a2248f9c97eaac42Alex Sakhartchouk} // renderscript
38377d9f4bd05b2d2a161f30c12a2248f9c97eaac42Alex Sakhartchouk} // android
384326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#endif
385