rsContext.h revision 5ae678f2e68e25bd74cdc408df354c3b6fe481bf
1/*
2 * Copyright (C) 2009 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 <utils/Vector.h>
21#include <ui/Surface.h>
22
23#include "rsType.h"
24#include "rsMatrix.h"
25#include "rsAllocation.h"
26#include "rsTriangleMesh.h"
27#include "rsDevice.h"
28#include "rsScriptC.h"
29#include "rsAllocation.h"
30#include "rsAdapter.h"
31#include "rsSampler.h"
32#include "rsProgramFragment.h"
33#include "rsProgramFragmentStore.h"
34#include "rsProgramVertex.h"
35
36#include "rsgApiStructs.h"
37#include "rsLocklessFifo.h"
38
39
40// ---------------------------------------------------------------------------
41namespace android {
42namespace renderscript {
43
44class Context
45{
46public:
47    Context(Device *, Surface *);
48    ~Context();
49
50    static pthread_key_t gThreadTLSKey;
51    struct ScriptTLSStruct {
52        Context * mContext;
53        Script * mScript;
54    };
55
56
57    //StructuredAllocationContext mStateAllocation;
58    ElementState mStateElement;
59    TypeState mStateType;
60    SamplerState mStateSampler;
61    ProgramFragmentState mStateFragment;
62    ProgramFragmentStoreState mStateFragmentStore;
63    ProgramVertexState mStateVertex;
64
65    TriangleMeshContext mStateTriangleMesh;
66
67    ScriptCState mScriptC;
68
69    static Context * getContext() {return gCon;}
70
71    void swapBuffers();
72    void setRootScript(Script *);
73    void setVertex(ProgramVertex *);
74    void setFragment(ProgramFragment *);
75    void setFragmentStore(ProgramFragmentStore *);
76
77    void updateSurface(void *sur);
78
79    const ProgramFragment * getFragment() {return mFragment.get();}
80    const ProgramFragmentStore * getFragmentStore() {return mFragmentStore.get();}
81
82    void setupCheck();
83
84    void assignName(ObjectBase *obj, const char *name, uint32_t len);
85    void removeName(ObjectBase *obj);
86    ObjectBase * lookupName(const char *name) const;
87    void appendNameDefines(String8 *str) const;
88
89
90    ProgramFragment * getDefaultProgramFragment() const {
91        return mStateFragment.mDefault.get();
92    }
93    ProgramVertex * getDefaultProgramVertex() const {
94        return mStateVertex.mDefault.get();
95    }
96    ProgramFragmentStore * getDefaultProgramFragmentStore() const {
97        return mStateFragmentStore.mDefault.get();
98    }
99
100protected:
101    Device *mDev;
102
103    EGLint mNumConfigs;
104    EGLint mMajorVersion;
105    EGLint mMinorVersion;
106    EGLConfig mConfig;
107    EGLContext mContext;
108    EGLSurface mSurface;
109    EGLint mWidth;
110    EGLint mHeight;
111    EGLDisplay mDisplay;
112
113    bool mRunning;
114    bool mExit;
115
116    LocklessCommandFifo mServerCommands;
117    LocklessCommandFifo mServerReturns;
118
119    pthread_t mThreadId;
120
121    ObjectBaseRef<Script> mRootScript;
122    ObjectBaseRef<ProgramFragment> mFragment;
123    ObjectBaseRef<ProgramVertex> mVertex;
124    ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
125
126private:
127    Context();
128
129    void initEGL();
130
131    bool runScript(Script *s, uint32_t launchID);
132    bool runRootScript();
133
134    static void * threadProc(void *);
135
136    // todo: put in TLS
137    static Context *gCon;
138    Surface *mWndSurface;
139
140    Vector<ObjectBase *> mNames;
141};
142
143
144}
145}
146#endif
147