blob: 54c555ff6fe16b23e187de8556d7fde116df28c2 [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
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
Jason Sams399bfce2009-07-13 12:20:31 -070020#include "rsUtils.h"
21
Joe Onoratod7b37742009-08-09 22:57:44 -070022#include <utils/KeyedVector.h>
23#include <utils/String8.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070024#include <utils/Vector.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070025#include <ui/Surface.h>
26
27#include "rsType.h"
28#include "rsMatrix.h"
29#include "rsAllocation.h"
30#include "rsTriangleMesh.h"
Jason Sams1bada8c2009-08-09 17:01:55 -070031#include "rsSimpleMesh.h"
Jason Sams7c878f32009-06-30 14:13:04 -070032#include "rsMesh.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070033#include "rsDevice.h"
34#include "rsScriptC.h"
35#include "rsAllocation.h"
36#include "rsAdapter.h"
37#include "rsSampler.h"
Jason Samsee411122009-07-21 12:20:54 -070038#include "rsLight.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070039#include "rsProgramFragment.h"
40#include "rsProgramFragmentStore.h"
41#include "rsProgramVertex.h"
42
43#include "rsgApiStructs.h"
44#include "rsLocklessFifo.h"
45
46
47// ---------------------------------------------------------------------------
48namespace android {
49namespace renderscript {
50
Jason Sams9bee51c2009-08-05 13:57:03 -070051class Context
Jason Samsd19f10d2009-05-22 14:03:28 -070052{
53public:
54 Context(Device *, Surface *);
55 ~Context();
56
Jason Sams462d11b2009-06-19 16:03:18 -070057 static pthread_key_t gThreadTLSKey;
58 struct ScriptTLSStruct {
59 Context * mContext;
60 Script * mScript;
61 };
62
Jason Samsd19f10d2009-05-22 14:03:28 -070063
64 //StructuredAllocationContext mStateAllocation;
65 ElementState mStateElement;
66 TypeState mStateType;
67 SamplerState mStateSampler;
68 ProgramFragmentState mStateFragment;
69 ProgramFragmentStoreState mStateFragmentStore;
70 ProgramVertexState mStateVertex;
Jason Samsbba134c2009-06-22 15:49:21 -070071 LightState mStateLight;
Jason Samsd19f10d2009-05-22 14:03:28 -070072
73 TriangleMeshContext mStateTriangleMesh;
74
75 ScriptCState mScriptC;
76
77 static Context * getContext() {return gCon;}
78
79 void swapBuffers();
80 void setRootScript(Script *);
81 void setVertex(ProgramVertex *);
82 void setFragment(ProgramFragment *);
83 void setFragmentStore(ProgramFragmentStore *);
84
85 void updateSurface(void *sur);
86
87 const ProgramFragment * getFragment() {return mFragment.get();}
88 const ProgramFragmentStore * getFragmentStore() {return mFragmentStore.get();}
Jason Samsb0ec1b42009-07-28 12:02:16 -070089 const ProgramVertex * getVertex() {return mVertex.get();}
Jason Samsd19f10d2009-05-22 14:03:28 -070090
91 void setupCheck();
Jason Sams9bee51c2009-08-05 13:57:03 -070092 void allocationCheck(const Allocation *);
Jason Samsd19f10d2009-05-22 14:03:28 -070093
Jason Samsd5680f92009-06-10 18:39:40 -070094 void assignName(ObjectBase *obj, const char *name, uint32_t len);
Jason Sams3eaa3382009-06-10 15:04:38 -070095 void removeName(ObjectBase *obj);
96 ObjectBase * lookupName(const char *name) const;
Jason Samsd5680f92009-06-10 18:39:40 -070097 void appendNameDefines(String8 *str) const;
Joe Onoratod7b37742009-08-09 22:57:44 -070098 void appendVarDefines(String8 *str) const;
Jason Sams9c54bdb2009-06-17 16:52:59 -070099
100 ProgramFragment * getDefaultProgramFragment() const {
101 return mStateFragment.mDefault.get();
102 }
103 ProgramVertex * getDefaultProgramVertex() const {
104 return mStateVertex.mDefault.get();
105 }
106 ProgramFragmentStore * getDefaultProgramFragmentStore() const {
107 return mStateFragmentStore.mDefault.get();
108 }
109
Joe Onoratod7b37742009-08-09 22:57:44 -0700110 void addInt32Define(const char* name, int32_t value) {
111 mInt32Defines.add(String8(name), value);
112 }
113
114 void addFloatDefine(const char* name, float value) {
115 mFloatDefines.add(String8(name), value);
116 }
117
Jason Sams40a29e82009-08-10 14:55:26 -0700118 uint32_t getWidth() const {return mWidth;}
119 uint32_t getHeight() const {return mHeight;}
120
Jason Samsd19f10d2009-05-22 14:03:28 -0700121protected:
122 Device *mDev;
123
124 EGLint mNumConfigs;
125 EGLint mMajorVersion;
126 EGLint mMinorVersion;
127 EGLConfig mConfig;
128 EGLContext mContext;
129 EGLSurface mSurface;
130 EGLint mWidth;
131 EGLint mHeight;
132 EGLDisplay mDisplay;
133
134 bool mRunning;
135 bool mExit;
136
Jason Samsd19f10d2009-05-22 14:03:28 -0700137 pthread_t mThreadId;
138
139 ObjectBaseRef<Script> mRootScript;
140 ObjectBaseRef<ProgramFragment> mFragment;
141 ObjectBaseRef<ProgramVertex> mVertex;
142 ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
143
144private:
145 Context();
146
147 void initEGL();
148
Jason Sams3eaa3382009-06-10 15:04:38 -0700149 bool runScript(Script *s, uint32_t launchID);
Jason Samsa09f11d2009-06-04 17:58:03 -0700150 bool runRootScript();
Jason Samsd19f10d2009-05-22 14:03:28 -0700151
152 static void * threadProc(void *);
153
154 // todo: put in TLS
155 static Context *gCon;
156 Surface *mWndSurface;
Jason Sams3eaa3382009-06-10 15:04:38 -0700157
158 Vector<ObjectBase *> mNames;
Joe Onoratod7b37742009-08-09 22:57:44 -0700159 KeyedVector<String8,int> mInt32Defines;
160 KeyedVector<String8,float> mFloatDefines;
Jason Samsd19f10d2009-05-22 14:03:28 -0700161};
162
163
164}
165}
166#endif