blob: 3570e102d96e842b41ba7d4d5ea0e3e9f2d1da18 [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 Sams65e7aa52009-09-24 17:38:20 -070091 void pause();
92 void resume();
93
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
Jason Sams516c3192009-10-06 13:58:47 -0700100 uint32_t getMessageToClient(void *data, size_t *receiveLen, size_t bufferLen, bool wait);
101 bool sendMessageToClient(void *data, uint32_t cmdID, size_t len, bool waitForSpace);
Jason Samsbd2197f2009-10-07 18:14:01 -0700102 bool runScript(Script *s, uint32_t launchID);
Jason Sams516c3192009-10-06 13:58:47 -0700103
104 void initToClient();
105 void deinitToClient();
106
Jason Sams9c54bdb2009-06-17 16:52:59 -0700107 ProgramFragment * getDefaultProgramFragment() const {
108 return mStateFragment.mDefault.get();
109 }
110 ProgramVertex * getDefaultProgramVertex() const {
111 return mStateVertex.mDefault.get();
112 }
113 ProgramFragmentStore * getDefaultProgramFragmentStore() const {
114 return mStateFragmentStore.mDefault.get();
115 }
Jason Samsebfb4362009-09-23 13:57:02 -0700116 ProgramRaster * getDefaultProgramRaster() const {
117 return mStateRaster.mDefault.get();
118 }
Jason Sams9c54bdb2009-06-17 16:52:59 -0700119
Joe Onoratod7b37742009-08-09 22:57:44 -0700120 void addInt32Define(const char* name, int32_t value) {
121 mInt32Defines.add(String8(name), value);
122 }
123
124 void addFloatDefine(const char* name, float value) {
125 mFloatDefines.add(String8(name), value);
126 }
127
Jason Samsb13ada52009-08-25 11:34:49 -0700128 uint32_t getWidth() const {return mEGL.mWidth;}
129 uint32_t getHeight() const {return mEGL.mHeight;}
Jason Sams40a29e82009-08-10 14:55:26 -0700130
Jason Samsbc948de2009-08-17 18:35:48 -0700131
132 ThreadIO mIO;
Jason Sams730ee652009-08-18 17:07:09 -0700133 void objDestroyAdd(ObjectBase *);
Jason Samsbc948de2009-08-17 18:35:48 -0700134
Jason Samsf4d16062009-08-19 12:17:14 -0700135 // Timers
136 enum Timers {
137 RS_TIMER_IDLE,
138 RS_TIMER_INTERNAL,
139 RS_TIMER_SCRIPT,
140 RS_TIMER_CLEAR_SWAP,
141 _RS_TIMER_TOTAL
142 };
143 uint64_t getTime() const;
144 void timerInit();
145 void timerReset();
146 void timerSet(Timers);
147 void timerPrint();
Jason Sams2525a812009-09-03 15:43:13 -0700148 void timerFrame();
Jason Samsf4d16062009-08-19 12:17:14 -0700149
Jason Samsb13ada52009-08-25 11:34:49 -0700150 bool checkVersion1_1() const {return (mGL.mMajorVersion > 1) || (mGL.mMinorVersion >= 1); }
151 bool checkVersion2_0() const {return mGL.mMajorVersion >= 2; }
152
Jason Sams66b27712009-09-25 15:25:00 -0700153 struct {
154 bool mLogTimes;
155 bool mLogScripts;
156 bool mLogObjects;
157 } props;
Joe Onorato9ac2c662009-09-23 16:37:36 -0700158
Jason Samsa9e7a052009-09-25 14:51:22 -0700159 mutable const ObjectBase * mObjHead;
160
Jason Samsd19f10d2009-05-22 14:03:28 -0700161protected:
162 Device *mDev;
163
Jason Samsb13ada52009-08-25 11:34:49 -0700164 struct {
165 EGLint mNumConfigs;
166 EGLint mMajorVersion;
167 EGLint mMinorVersion;
168 EGLConfig mConfig;
169 EGLContext mContext;
170 EGLSurface mSurface;
171 EGLint mWidth;
172 EGLint mHeight;
173 EGLDisplay mDisplay;
174 } mEGL;
175
176 struct {
177 const uint8_t * mVendor;
178 const uint8_t * mRenderer;
179 const uint8_t * mVersion;
180 const uint8_t * mExtensions;
181
182 uint32_t mMajorVersion;
183 uint32_t mMinorVersion;
184
185 } mGL;
Jason Samsd19f10d2009-05-22 14:03:28 -0700186
187 bool mRunning;
188 bool mExit;
Jason Samsb13ada52009-08-25 11:34:49 -0700189 bool mUseDepth;
Jason Sams65e7aa52009-09-24 17:38:20 -0700190 bool mPaused;
Jason Samsd19f10d2009-05-22 14:03:28 -0700191
Jason Samsd19f10d2009-05-22 14:03:28 -0700192 pthread_t mThreadId;
193
194 ObjectBaseRef<Script> mRootScript;
195 ObjectBaseRef<ProgramFragment> mFragment;
196 ObjectBaseRef<ProgramVertex> mVertex;
197 ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
Jason Samsebfb4362009-09-23 13:57:02 -0700198 ObjectBaseRef<ProgramRaster> mRaster;
Jason Samsd19f10d2009-05-22 14:03:28 -0700199
Jason Sams730ee652009-08-18 17:07:09 -0700200
201 struct ObjDestroyOOB {
202 pthread_mutex_t mMutex;
203 Vector<ObjectBase *> mDestroyList;
204 bool mNeedToEmpty;
205 };
206 ObjDestroyOOB mObjDestroy;
207 bool objDestroyOOBInit();
208 void objDestroyOOBRun();
209 void objDestroyOOBDestroy();
210
Jason Samsd19f10d2009-05-22 14:03:28 -0700211private:
212 Context();
213
214 void initEGL();
215
Jason Samsa09f11d2009-06-04 17:58:03 -0700216 bool runRootScript();
Jason Samsd19f10d2009-05-22 14:03:28 -0700217
218 static void * threadProc(void *);
219
Jason Samsd19f10d2009-05-22 14:03:28 -0700220 Surface *mWndSurface;
Jason Sams3eaa3382009-06-10 15:04:38 -0700221
222 Vector<ObjectBase *> mNames;
Joe Onoratod7b37742009-08-09 22:57:44 -0700223 KeyedVector<String8,int> mInt32Defines;
224 KeyedVector<String8,float> mFloatDefines;
Jason Samsf4d16062009-08-19 12:17:14 -0700225
226 uint64_t mTimers[_RS_TIMER_TOTAL];
227 Timers mTimerActive;
228 uint64_t mTimeLast;
Jason Sams2525a812009-09-03 15:43:13 -0700229 uint64_t mTimeFrame;
230 uint64_t mTimeLastFrame;
Jason Samsd19f10d2009-05-22 14:03:28 -0700231};
232
Jason Samsd19f10d2009-05-22 14:03:28 -0700233}
234}
235#endif