blob: 634416b2f35bc981bea6f05175ad75ade5fd4277 [file] [log] [blame]
Jason Sams326e0dd2009-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 Samsa5577802009-07-13 12:20:31 -070020#include "rsUtils.h"
21
Jason Sams326e0dd2009-05-22 14:03:28 -070022#include <ui/Surface.h>
23
Jason Samsfcd31922009-08-17 18:35:48 -070024#include "rsThreadIO.h"
Jason Sams326e0dd2009-05-22 14:03:28 -070025#include "rsType.h"
26#include "rsMatrix.h"
27#include "rsAllocation.h"
28#include "rsTriangleMesh.h"
Jason Samse5ffb872009-08-09 17:01:55 -070029#include "rsSimpleMesh.h"
Jason Samsa89371c2009-06-30 14:13:04 -070030#include "rsMesh.h"
Jason Sams326e0dd2009-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 Samsb5909ce2009-07-21 12:20:54 -070036#include "rsLight.h"
Jason Sams326e0dd2009-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 Samscfb1d112009-08-05 13:57:03 -070049class Context
Jason Sams326e0dd2009-05-22 14:03:28 -070050{
51public:
Jason Samsafcb25c2009-08-25 11:34:49 -070052 Context(Device *, Surface *, bool useDepth);
Jason Sams326e0dd2009-05-22 14:03:28 -070053 ~Context();
54
Jason Samse5769102009-06-19 16:03:18 -070055 static pthread_key_t gThreadTLSKey;
56 struct ScriptTLSStruct {
57 Context * mContext;
58 Script * mScript;
59 };
60
Jason Sams326e0dd2009-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 Sams62bc1db2009-06-22 15:49:21 -070069 LightState mStateLight;
Jason Sams326e0dd2009-05-22 14:03:28 -070070
71 TriangleMeshContext mStateTriangleMesh;
72
73 ScriptCState mScriptC;
74
Jason Sams326e0dd2009-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 Samsc9d43db2009-07-28 12:02:16 -070085 const ProgramVertex * getVertex() {return mVertex.get();}
Jason Sams326e0dd2009-05-22 14:03:28 -070086
87 void setupCheck();
Jason Samscfb1d112009-08-05 13:57:03 -070088 void allocationCheck(const Allocation *);
Jason Sams326e0dd2009-05-22 14:03:28 -070089
Jason Samsa4a54e42009-06-10 18:39:40 -070090 void assignName(ObjectBase *obj, const char *name, uint32_t len);
Jason Samsa0a1b6f2009-06-10 15:04:38 -070091 void removeName(ObjectBase *obj);
92 ObjectBase * lookupName(const char *name) const;
Jason Samsa4a54e42009-06-10 18:39:40 -070093 void appendNameDefines(String8 *str) const;
Joe Onorato57b79ce2009-08-09 22:57:44 -070094 void appendVarDefines(String8 *str) const;
Jason Sams8ce125b2009-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 Onorato57b79ce2009-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 Samsafcb25c2009-08-25 11:34:49 -0700114 uint32_t getWidth() const {return mEGL.mWidth;}
115 uint32_t getHeight() const {return mEGL.mHeight;}
Jason Samse579df42009-08-10 14:55:26 -0700116
Jason Samsfcd31922009-08-17 18:35:48 -0700117
118 ThreadIO mIO;
Jason Sams50869382009-08-18 17:07:09 -0700119 void objDestroyAdd(ObjectBase *);
Jason Samsfcd31922009-08-17 18:35:48 -0700120
Jason Sams24371d92009-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();
Jason Sams1d54f102009-09-03 15:43:13 -0700134 void timerFrame();
Jason Sams24371d92009-08-19 12:17:14 -0700135
Jason Samsafcb25c2009-08-25 11:34:49 -0700136 bool checkVersion1_1() const {return (mGL.mMajorVersion > 1) || (mGL.mMinorVersion >= 1); }
137 bool checkVersion2_0() const {return mGL.mMajorVersion >= 2; }
138
Jason Sams326e0dd2009-05-22 14:03:28 -0700139protected:
140 Device *mDev;
141
Jason Samsafcb25c2009-08-25 11:34:49 -0700142 struct {
143 EGLint mNumConfigs;
144 EGLint mMajorVersion;
145 EGLint mMinorVersion;
146 EGLConfig mConfig;
147 EGLContext mContext;
148 EGLSurface mSurface;
149 EGLint mWidth;
150 EGLint mHeight;
151 EGLDisplay mDisplay;
152 } mEGL;
153
154 struct {
155 const uint8_t * mVendor;
156 const uint8_t * mRenderer;
157 const uint8_t * mVersion;
158 const uint8_t * mExtensions;
159
160 uint32_t mMajorVersion;
161 uint32_t mMinorVersion;
162
163 } mGL;
Jason Sams326e0dd2009-05-22 14:03:28 -0700164
165 bool mRunning;
166 bool mExit;
Jason Samsafcb25c2009-08-25 11:34:49 -0700167 bool mUseDepth;
Jason Sams326e0dd2009-05-22 14:03:28 -0700168
Jason Sams326e0dd2009-05-22 14:03:28 -0700169 pthread_t mThreadId;
170
171 ObjectBaseRef<Script> mRootScript;
172 ObjectBaseRef<ProgramFragment> mFragment;
173 ObjectBaseRef<ProgramVertex> mVertex;
174 ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
175
Jason Sams50869382009-08-18 17:07:09 -0700176
177 struct ObjDestroyOOB {
178 pthread_mutex_t mMutex;
179 Vector<ObjectBase *> mDestroyList;
180 bool mNeedToEmpty;
181 };
182 ObjDestroyOOB mObjDestroy;
183 bool objDestroyOOBInit();
184 void objDestroyOOBRun();
185 void objDestroyOOBDestroy();
186
Jason Sams326e0dd2009-05-22 14:03:28 -0700187private:
188 Context();
189
190 void initEGL();
191
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700192 bool runScript(Script *s, uint32_t launchID);
Jason Samsa44cb292009-06-04 17:58:03 -0700193 bool runRootScript();
Jason Sams326e0dd2009-05-22 14:03:28 -0700194
195 static void * threadProc(void *);
196
Jason Sams326e0dd2009-05-22 14:03:28 -0700197 Surface *mWndSurface;
Jason Samsa0a1b6f2009-06-10 15:04:38 -0700198
199 Vector<ObjectBase *> mNames;
Joe Onorato57b79ce2009-08-09 22:57:44 -0700200 KeyedVector<String8,int> mInt32Defines;
201 KeyedVector<String8,float> mFloatDefines;
Jason Sams24371d92009-08-19 12:17:14 -0700202
203 uint64_t mTimers[_RS_TIMER_TOTAL];
204 Timers mTimerActive;
205 uint64_t mTimeLast;
Jason Sams1d54f102009-09-03 15:43:13 -0700206 uint64_t mTimeFrame;
207 uint64_t mTimeLastFrame;
Jason Sams326e0dd2009-05-22 14:03:28 -0700208};
209
210
211}
212}
213#endif