blob: 8cabf8787ae3674a59182fb74a9c042e0ed82255 [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"
Jason Sams1bada8c2009-08-09 17:01:55 -070028#include "rsSimpleMesh.h"
Jason Sams7c878f32009-06-30 14:13:04 -070029#include "rsMesh.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070030#include "rsDevice.h"
31#include "rsScriptC.h"
32#include "rsAllocation.h"
33#include "rsAdapter.h"
34#include "rsSampler.h"
Jason Samsee411122009-07-21 12:20:54 -070035#include "rsLight.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070036#include "rsProgramFragment.h"
37#include "rsProgramFragmentStore.h"
Jason Samsebfb4362009-09-23 13:57:02 -070038#include "rsProgramRaster.h"
Jason Samsd19f10d2009-05-22 14:03:28 -070039#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;
Jason Samsebfb4362009-09-23 13:57:02 -070068 ProgramRasterState mStateRaster;
Jason Samsd19f10d2009-05-22 14:03:28 -070069 ProgramVertexState mStateVertex;
Jason Samsbba134c2009-06-22 15:49:21 -070070 LightState mStateLight;
Jason Samsd19f10d2009-05-22 14:03:28 -070071
Jason Samsd19f10d2009-05-22 14:03:28 -070072 ScriptCState mScriptC;
73
Jason Samsd19f10d2009-05-22 14:03:28 -070074 void swapBuffers();
75 void setRootScript(Script *);
Jason Samsebfb4362009-09-23 13:57:02 -070076 void setRaster(ProgramRaster *);
Jason Samsd19f10d2009-05-22 14:03:28 -070077 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 Samsebfb4362009-09-23 13:57:02 -070085 const ProgramRaster * getRaster() {return mRaster.get();}
Jason Samsb0ec1b42009-07-28 12:02:16 -070086 const ProgramVertex * getVertex() {return mVertex.get();}
Jason Samsd19f10d2009-05-22 14:03:28 -070087
88 void setupCheck();
Jason Sams9bee51c2009-08-05 13:57:03 -070089 void allocationCheck(const Allocation *);
Jason Samsd19f10d2009-05-22 14:03:28 -070090
Jason Samsd5680f92009-06-10 18:39:40 -070091 void assignName(ObjectBase *obj, const char *name, uint32_t len);
Jason Sams3eaa3382009-06-10 15:04:38 -070092 void removeName(ObjectBase *obj);
93 ObjectBase * lookupName(const char *name) const;
Jason Samsd5680f92009-06-10 18:39:40 -070094 void appendNameDefines(String8 *str) const;
Joe Onoratod7b37742009-08-09 22:57:44 -070095 void appendVarDefines(String8 *str) const;
Jason Sams9c54bdb2009-06-17 16:52:59 -070096
97 ProgramFragment * getDefaultProgramFragment() const {
98 return mStateFragment.mDefault.get();
99 }
100 ProgramVertex * getDefaultProgramVertex() const {
101 return mStateVertex.mDefault.get();
102 }
103 ProgramFragmentStore * getDefaultProgramFragmentStore() const {
104 return mStateFragmentStore.mDefault.get();
105 }
Jason Samsebfb4362009-09-23 13:57:02 -0700106 ProgramRaster * getDefaultProgramRaster() const {
107 return mStateRaster.mDefault.get();
108 }
Jason Sams9c54bdb2009-06-17 16:52:59 -0700109
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 Samsb13ada52009-08-25 11:34:49 -0700118 uint32_t getWidth() const {return mEGL.mWidth;}
119 uint32_t getHeight() const {return mEGL.mHeight;}
Jason Sams40a29e82009-08-10 14:55:26 -0700120
Jason Samsbc948de2009-08-17 18:35:48 -0700121
122 ThreadIO mIO;
Jason Sams730ee652009-08-18 17:07:09 -0700123 void objDestroyAdd(ObjectBase *);
Jason Samsbc948de2009-08-17 18:35:48 -0700124
Jason Samsf4d16062009-08-19 12:17:14 -0700125 // Timers
126 enum Timers {
127 RS_TIMER_IDLE,
128 RS_TIMER_INTERNAL,
129 RS_TIMER_SCRIPT,
130 RS_TIMER_CLEAR_SWAP,
131 _RS_TIMER_TOTAL
132 };
133 uint64_t getTime() const;
134 void timerInit();
135 void timerReset();
136 void timerSet(Timers);
137 void timerPrint();
Jason Sams2525a812009-09-03 15:43:13 -0700138 void timerFrame();
Jason Samsf4d16062009-08-19 12:17:14 -0700139
Jason Samsb13ada52009-08-25 11:34:49 -0700140 bool checkVersion1_1() const {return (mGL.mMajorVersion > 1) || (mGL.mMinorVersion >= 1); }
141 bool checkVersion2_0() const {return mGL.mMajorVersion >= 2; }
142
Joe Onorato9ac2c662009-09-23 16:37:36 -0700143 bool logTimes;
144
Jason Samsd19f10d2009-05-22 14:03:28 -0700145protected:
146 Device *mDev;
147
Jason Samsb13ada52009-08-25 11:34:49 -0700148 struct {
149 EGLint mNumConfigs;
150 EGLint mMajorVersion;
151 EGLint mMinorVersion;
152 EGLConfig mConfig;
153 EGLContext mContext;
154 EGLSurface mSurface;
155 EGLint mWidth;
156 EGLint mHeight;
157 EGLDisplay mDisplay;
158 } mEGL;
159
160 struct {
161 const uint8_t * mVendor;
162 const uint8_t * mRenderer;
163 const uint8_t * mVersion;
164 const uint8_t * mExtensions;
165
166 uint32_t mMajorVersion;
167 uint32_t mMinorVersion;
168
169 } mGL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700170
171 bool mRunning;
172 bool mExit;
Jason Samsb13ada52009-08-25 11:34:49 -0700173 bool mUseDepth;
Jason Samsd19f10d2009-05-22 14:03:28 -0700174
Jason Samsd19f10d2009-05-22 14:03:28 -0700175 pthread_t mThreadId;
176
177 ObjectBaseRef<Script> mRootScript;
178 ObjectBaseRef<ProgramFragment> mFragment;
179 ObjectBaseRef<ProgramVertex> mVertex;
180 ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
Jason Samsebfb4362009-09-23 13:57:02 -0700181 ObjectBaseRef<ProgramRaster> mRaster;
Jason Samsd19f10d2009-05-22 14:03:28 -0700182
Jason Sams730ee652009-08-18 17:07:09 -0700183
184 struct ObjDestroyOOB {
185 pthread_mutex_t mMutex;
186 Vector<ObjectBase *> mDestroyList;
187 bool mNeedToEmpty;
188 };
189 ObjDestroyOOB mObjDestroy;
190 bool objDestroyOOBInit();
191 void objDestroyOOBRun();
192 void objDestroyOOBDestroy();
193
Jason Samsd19f10d2009-05-22 14:03:28 -0700194private:
195 Context();
196
197 void initEGL();
198
Jason Sams3eaa3382009-06-10 15:04:38 -0700199 bool runScript(Script *s, uint32_t launchID);
Jason Samsa09f11d2009-06-04 17:58:03 -0700200 bool runRootScript();
Jason Samsd19f10d2009-05-22 14:03:28 -0700201
202 static void * threadProc(void *);
203
Jason Samsd19f10d2009-05-22 14:03:28 -0700204 Surface *mWndSurface;
Jason Sams3eaa3382009-06-10 15:04:38 -0700205
206 Vector<ObjectBase *> mNames;
Joe Onoratod7b37742009-08-09 22:57:44 -0700207 KeyedVector<String8,int> mInt32Defines;
208 KeyedVector<String8,float> mFloatDefines;
Jason Samsf4d16062009-08-19 12:17:14 -0700209
210 uint64_t mTimers[_RS_TIMER_TOTAL];
211 Timers mTimerActive;
212 uint64_t mTimeLast;
Jason Sams2525a812009-09-03 15:43:13 -0700213 uint64_t mTimeFrame;
214 uint64_t mTimeLastFrame;
Jason Samsd19f10d2009-05-22 14:03:28 -0700215};
216
Jason Samsd19f10d2009-05-22 14:03:28 -0700217}
218}
219#endif