blob: c58a88c733187fcec76e2f6513863b9e5f38b9bf [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
Jason Samsd19f10d2009-05-22 14:03:28 -070022#include <ui/Surface.h>
23
Jason Samsbc948de2009-08-17 18:35:48 -070024#include "rsThreadIO.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070025#include "rsType.h"
26#include "rsMatrix.h"
27#include "rsAllocation.h"
28#include "rsTriangleMesh.h"
Jason Sams1bada8c2009-08-09 17:01:55 -070029#include "rsSimpleMesh.h"
Jason Sams7c878f32009-06-30 14:13:04 -070030#include "rsMesh.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070031#include "rsDevice.h"
32#include "rsScriptC.h"
33#include "rsAllocation.h"
34#include "rsAdapter.h"
35#include "rsSampler.h"
Jason Samsee411122009-07-21 12:20:54 -070036#include "rsLight.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070037#include "rsProgramFragment.h"
38#include "rsProgramFragmentStore.h"
39#include "rsProgramVertex.h"
40
41#include "rsgApiStructs.h"
42#include "rsLocklessFifo.h"
43
44
45// ---------------------------------------------------------------------------
46namespace android {
47namespace renderscript {
48
Jason Sams9bee51c2009-08-05 13:57:03 -070049class Context
Jason Samsd19f10d2009-05-22 14:03:28 -070050{
51public:
Jason Samsb13ada52009-08-25 11:34:49 -070052 Context(Device *, Surface *, bool useDepth);
Jason Samsd19f10d2009-05-22 14:03:28 -070053 ~Context();
54
Jason Sams462d11b2009-06-19 16:03:18 -070055 static pthread_key_t gThreadTLSKey;
56 struct ScriptTLSStruct {
57 Context * mContext;
58 Script * mScript;
59 };
60
Jason Samsd19f10d2009-05-22 14:03:28 -070061
62 //StructuredAllocationContext mStateAllocation;
63 ElementState mStateElement;
64 TypeState mStateType;
65 SamplerState mStateSampler;
66 ProgramFragmentState mStateFragment;
67 ProgramFragmentStoreState mStateFragmentStore;
68 ProgramVertexState mStateVertex;
Jason Samsbba134c2009-06-22 15:49:21 -070069 LightState mStateLight;
Jason Samsd19f10d2009-05-22 14:03:28 -070070
71 TriangleMeshContext mStateTriangleMesh;
72
73 ScriptCState mScriptC;
74
Jason Samsd19f10d2009-05-22 14:03:28 -070075 void swapBuffers();
76 void setRootScript(Script *);
77 void setVertex(ProgramVertex *);
78 void setFragment(ProgramFragment *);
79 void setFragmentStore(ProgramFragmentStore *);
80
81 void updateSurface(void *sur);
82
83 const ProgramFragment * getFragment() {return mFragment.get();}
84 const ProgramFragmentStore * getFragmentStore() {return mFragmentStore.get();}
Jason Samsb0ec1b42009-07-28 12:02:16 -070085 const ProgramVertex * getVertex() {return mVertex.get();}
Jason Samsd19f10d2009-05-22 14:03:28 -070086
87 void setupCheck();
Jason Sams9bee51c2009-08-05 13:57:03 -070088 void allocationCheck(const Allocation *);
Jason Samsd19f10d2009-05-22 14:03:28 -070089
Jason Samsd5680f92009-06-10 18:39:40 -070090 void assignName(ObjectBase *obj, const char *name, uint32_t len);
Jason Sams3eaa3382009-06-10 15:04:38 -070091 void removeName(ObjectBase *obj);
92 ObjectBase * lookupName(const char *name) const;
Jason Samsd5680f92009-06-10 18:39:40 -070093 void appendNameDefines(String8 *str) const;
Joe Onoratod7b37742009-08-09 22:57:44 -070094 void appendVarDefines(String8 *str) const;
Jason Sams9c54bdb2009-06-17 16:52:59 -070095
96 ProgramFragment * getDefaultProgramFragment() const {
97 return mStateFragment.mDefault.get();
98 }
99 ProgramVertex * getDefaultProgramVertex() const {
100 return mStateVertex.mDefault.get();
101 }
102 ProgramFragmentStore * getDefaultProgramFragmentStore() const {
103 return mStateFragmentStore.mDefault.get();
104 }
105
Joe Onoratod7b37742009-08-09 22:57:44 -0700106 void addInt32Define(const char* name, int32_t value) {
107 mInt32Defines.add(String8(name), value);
108 }
109
110 void addFloatDefine(const char* name, float value) {
111 mFloatDefines.add(String8(name), value);
112 }
113
Jason Samsb13ada52009-08-25 11:34:49 -0700114 uint32_t getWidth() const {return mEGL.mWidth;}
115 uint32_t getHeight() const {return mEGL.mHeight;}
Jason Sams40a29e82009-08-10 14:55:26 -0700116
Jason Samsbc948de2009-08-17 18:35:48 -0700117
118 ThreadIO mIO;
Jason Sams730ee652009-08-18 17:07:09 -0700119 void objDestroyAdd(ObjectBase *);
Jason Samsbc948de2009-08-17 18:35:48 -0700120
Jason Samsf4d16062009-08-19 12:17:14 -0700121 // Timers
122 enum Timers {
123 RS_TIMER_IDLE,
124 RS_TIMER_INTERNAL,
125 RS_TIMER_SCRIPT,
126 RS_TIMER_CLEAR_SWAP,
127 _RS_TIMER_TOTAL
128 };
129 uint64_t getTime() const;
130 void timerInit();
131 void timerReset();
132 void timerSet(Timers);
133 void timerPrint();
134
Jason Samsb13ada52009-08-25 11:34:49 -0700135 bool checkVersion1_1() const {return (mGL.mMajorVersion > 1) || (mGL.mMinorVersion >= 1); }
136 bool checkVersion2_0() const {return mGL.mMajorVersion >= 2; }
137
Jason Samsd19f10d2009-05-22 14:03:28 -0700138protected:
139 Device *mDev;
140
Jason Samsb13ada52009-08-25 11:34:49 -0700141 struct {
142 EGLint mNumConfigs;
143 EGLint mMajorVersion;
144 EGLint mMinorVersion;
145 EGLConfig mConfig;
146 EGLContext mContext;
147 EGLSurface mSurface;
148 EGLint mWidth;
149 EGLint mHeight;
150 EGLDisplay mDisplay;
151 } mEGL;
152
153 struct {
154 const uint8_t * mVendor;
155 const uint8_t * mRenderer;
156 const uint8_t * mVersion;
157 const uint8_t * mExtensions;
158
159 uint32_t mMajorVersion;
160 uint32_t mMinorVersion;
161
162 } mGL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700163
164 bool mRunning;
165 bool mExit;
Jason Samsb13ada52009-08-25 11:34:49 -0700166 bool mUseDepth;
Jason Samsd19f10d2009-05-22 14:03:28 -0700167
Jason Samsd19f10d2009-05-22 14:03:28 -0700168 pthread_t mThreadId;
169
170 ObjectBaseRef<Script> mRootScript;
171 ObjectBaseRef<ProgramFragment> mFragment;
172 ObjectBaseRef<ProgramVertex> mVertex;
173 ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
174
Jason Sams730ee652009-08-18 17:07:09 -0700175
176 struct ObjDestroyOOB {
177 pthread_mutex_t mMutex;
178 Vector<ObjectBase *> mDestroyList;
179 bool mNeedToEmpty;
180 };
181 ObjDestroyOOB mObjDestroy;
182 bool objDestroyOOBInit();
183 void objDestroyOOBRun();
184 void objDestroyOOBDestroy();
185
Jason Samsd19f10d2009-05-22 14:03:28 -0700186private:
187 Context();
188
189 void initEGL();
190
Jason Sams3eaa3382009-06-10 15:04:38 -0700191 bool runScript(Script *s, uint32_t launchID);
Jason Samsa09f11d2009-06-04 17:58:03 -0700192 bool runRootScript();
Jason Samsd19f10d2009-05-22 14:03:28 -0700193
194 static void * threadProc(void *);
195
Jason Samsd19f10d2009-05-22 14:03:28 -0700196 Surface *mWndSurface;
Jason Sams3eaa3382009-06-10 15:04:38 -0700197
198 Vector<ObjectBase *> mNames;
Joe Onoratod7b37742009-08-09 22:57:44 -0700199 KeyedVector<String8,int> mInt32Defines;
200 KeyedVector<String8,float> mFloatDefines;
Jason Samsf4d16062009-08-19 12:17:14 -0700201
202 uint64_t mTimers[_RS_TIMER_TOTAL];
203 Timers mTimerActive;
204 uint64_t mTimeLast;
Jason Samsd19f10d2009-05-22 14:03:28 -0700205};
206
207
208}
209}
210#endif