rsContext.h revision f4d160653fe405eba9d6f55448ac4599c6cadd77
1d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams/*
2d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams * Copyright (C) 2009 The Android Open Source Project
3d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams *
4d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams * Licensed under the Apache License, Version 2.0 (the "License");
5d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams * you may not use this file except in compliance with the License.
6d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams * You may obtain a copy of the License at
7d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams *
8d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams *      http://www.apache.org/licenses/LICENSE-2.0
9d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams *
10d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams * Unless required by applicable law or agreed to in writing, software
11d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams * distributed under the License is distributed on an "AS IS" BASIS,
12d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams * See the License for the specific language governing permissions and
14d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams * limitations under the License.
15d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams */
16d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
17d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#ifndef ANDROID_RS_CONTEXT_H
18d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#define ANDROID_RS_CONTEXT_H
19d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
20399bfce299210ef9c2fc1e932202223253e2384aJason Sams#include "rsUtils.h"
21399bfce299210ef9c2fc1e932202223253e2384aJason Sams
22d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include <ui/Surface.h>
23d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
24bc948dedcee57a66fe2cb38d4c79d04a10c7efb3Jason Sams#include "rsThreadIO.h"
25d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include "rsType.h"
26d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include "rsMatrix.h"
27d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include "rsAllocation.h"
28d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include "rsTriangleMesh.h"
291bada8cd6e4f340de93cff4a2439835fc3b1456cJason Sams#include "rsSimpleMesh.h"
307c878f3a8379daf21dca6de7aa722ff75328afbeJason Sams#include "rsMesh.h"
31d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include "rsDevice.h"
32d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include "rsScriptC.h"
33d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include "rsAllocation.h"
34d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include "rsAdapter.h"
35d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include "rsSampler.h"
36ee41112e1539de95596600fd2c6dada5d275217fJason Sams#include "rsLight.h"
37d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include "rsProgramFragment.h"
38d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include "rsProgramFragmentStore.h"
39d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include "rsProgramVertex.h"
40d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
41d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include "rsgApiStructs.h"
42d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#include "rsLocklessFifo.h"
43d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
44d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
45d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams// ---------------------------------------------------------------------------
46d19f10d43aa400e1183aa21a97099d02074131a2Jason Samsnamespace android {
47d19f10d43aa400e1183aa21a97099d02074131a2Jason Samsnamespace renderscript {
48d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
499bee51c42eb8c3daffe7d6fa483edbb1689b94d2Jason Samsclass Context
50d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams{
51d19f10d43aa400e1183aa21a97099d02074131a2Jason Samspublic:
52d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    Context(Device *, Surface *);
53d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    ~Context();
54d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
55462d11b880cba72584c135397c0a82618ab63217Jason Sams    static pthread_key_t gThreadTLSKey;
56462d11b880cba72584c135397c0a82618ab63217Jason Sams    struct ScriptTLSStruct {
57462d11b880cba72584c135397c0a82618ab63217Jason Sams        Context * mContext;
58462d11b880cba72584c135397c0a82618ab63217Jason Sams        Script * mScript;
59462d11b880cba72584c135397c0a82618ab63217Jason Sams    };
60462d11b880cba72584c135397c0a82618ab63217Jason Sams
61d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
62d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    //StructuredAllocationContext mStateAllocation;
63d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    ElementState mStateElement;
64d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    TypeState mStateType;
65d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    SamplerState mStateSampler;
66d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    ProgramFragmentState mStateFragment;
67d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    ProgramFragmentStoreState mStateFragmentStore;
68d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    ProgramVertexState mStateVertex;
69bba134c8a1dcfe0c8473307a95899a02c9553504Jason Sams    LightState mStateLight;
70d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
71d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    TriangleMeshContext mStateTriangleMesh;
72d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
73d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    ScriptCState mScriptC;
74d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
75d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    void swapBuffers();
76d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    void setRootScript(Script *);
77d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    void setVertex(ProgramVertex *);
78d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    void setFragment(ProgramFragment *);
79d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    void setFragmentStore(ProgramFragmentStore *);
80d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
81d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    void updateSurface(void *sur);
82d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
83d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    const ProgramFragment * getFragment() {return mFragment.get();}
84d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    const ProgramFragmentStore * getFragmentStore() {return mFragmentStore.get();}
85b0ec1b46d6f5b5612e33fe43a828abea79b87a00Jason Sams    const ProgramVertex * getVertex() {return mVertex.get();}
86d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
87d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    void setupCheck();
889bee51c42eb8c3daffe7d6fa483edbb1689b94d2Jason Sams    void allocationCheck(const Allocation *);
89d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
90d5680f9ba95ec7ce212e8025774914e79982a1eeJason Sams    void assignName(ObjectBase *obj, const char *name, uint32_t len);
913eaa338e11a3b0d6b87d705e5bb95625e82347bdJason Sams    void removeName(ObjectBase *obj);
923eaa338e11a3b0d6b87d705e5bb95625e82347bdJason Sams    ObjectBase * lookupName(const char *name) const;
93d5680f9ba95ec7ce212e8025774914e79982a1eeJason Sams    void appendNameDefines(String8 *str) const;
94d7b3774da62d3c70cc7e8cf549967a1c823501e6Joe Onorato    void appendVarDefines(String8 *str) const;
959c54bdbf458e3c9433d237ae71cf47c4ec47d852Jason Sams
969c54bdbf458e3c9433d237ae71cf47c4ec47d852Jason Sams    ProgramFragment * getDefaultProgramFragment() const {
979c54bdbf458e3c9433d237ae71cf47c4ec47d852Jason Sams        return mStateFragment.mDefault.get();
989c54bdbf458e3c9433d237ae71cf47c4ec47d852Jason Sams    }
999c54bdbf458e3c9433d237ae71cf47c4ec47d852Jason Sams    ProgramVertex * getDefaultProgramVertex() const {
1009c54bdbf458e3c9433d237ae71cf47c4ec47d852Jason Sams        return mStateVertex.mDefault.get();
1019c54bdbf458e3c9433d237ae71cf47c4ec47d852Jason Sams    }
1029c54bdbf458e3c9433d237ae71cf47c4ec47d852Jason Sams    ProgramFragmentStore * getDefaultProgramFragmentStore() const {
1039c54bdbf458e3c9433d237ae71cf47c4ec47d852Jason Sams        return mStateFragmentStore.mDefault.get();
1049c54bdbf458e3c9433d237ae71cf47c4ec47d852Jason Sams    }
1059c54bdbf458e3c9433d237ae71cf47c4ec47d852Jason Sams
106d7b3774da62d3c70cc7e8cf549967a1c823501e6Joe Onorato    void addInt32Define(const char* name, int32_t value) {
107d7b3774da62d3c70cc7e8cf549967a1c823501e6Joe Onorato        mInt32Defines.add(String8(name), value);
108d7b3774da62d3c70cc7e8cf549967a1c823501e6Joe Onorato    }
109d7b3774da62d3c70cc7e8cf549967a1c823501e6Joe Onorato
110d7b3774da62d3c70cc7e8cf549967a1c823501e6Joe Onorato    void addFloatDefine(const char* name, float value) {
111d7b3774da62d3c70cc7e8cf549967a1c823501e6Joe Onorato        mFloatDefines.add(String8(name), value);
112d7b3774da62d3c70cc7e8cf549967a1c823501e6Joe Onorato    }
113d7b3774da62d3c70cc7e8cf549967a1c823501e6Joe Onorato
11440a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    uint32_t getWidth() const {return mWidth;}
11540a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    uint32_t getHeight() const {return mHeight;}
11640a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
117bc948dedcee57a66fe2cb38d4c79d04a10c7efb3Jason Sams
118bc948dedcee57a66fe2cb38d4c79d04a10c7efb3Jason Sams    ThreadIO mIO;
119730ee65d4ddb307898053b623120bad1655fadadJason Sams    void objDestroyAdd(ObjectBase *);
120bc948dedcee57a66fe2cb38d4c79d04a10c7efb3Jason Sams
121f4d160653fe405eba9d6f55448ac4599c6cadd77Jason Sams    // Timers
122f4d160653fe405eba9d6f55448ac4599c6cadd77Jason Sams    enum Timers {
123f4d160653fe405eba9d6f55448ac4599c6cadd77Jason Sams        RS_TIMER_IDLE,
124f4d160653fe405eba9d6f55448ac4599c6cadd77Jason Sams        RS_TIMER_INTERNAL,
125f4d160653fe405eba9d6f55448ac4599c6cadd77Jason Sams        RS_TIMER_SCRIPT,
126f4d160653fe405eba9d6f55448ac4599c6cadd77Jason Sams        RS_TIMER_CLEAR_SWAP,
127f4d160653fe405eba9d6f55448ac4599c6cadd77Jason Sams        _RS_TIMER_TOTAL
128f4d160653fe405eba9d6f55448ac4599c6cadd77Jason Sams    };
129f4d160653fe405eba9d6f55448ac4599c6cadd77Jason Sams    uint64_t getTime() const;
130f4d160653fe405eba9d6f55448ac4599c6cadd77Jason Sams    void timerInit();
131f4d160653fe405eba9d6f55448ac4599c6cadd77Jason Sams    void timerReset();
132f4d160653fe405eba9d6f55448ac4599c6cadd77Jason Sams    void timerSet(Timers);
133f4d160653fe405eba9d6f55448ac4599c6cadd77Jason Sams    void timerPrint();
134f4d160653fe405eba9d6f55448ac4599c6cadd77Jason Sams
135d19f10d43aa400e1183aa21a97099d02074131a2Jason Samsprotected:
136d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    Device *mDev;
137d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
138d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    EGLint mNumConfigs;
139d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    EGLint mMajorVersion;
140d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    EGLint mMinorVersion;
141d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    EGLConfig mConfig;
142d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    EGLContext mContext;
143d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    EGLSurface mSurface;
144d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    EGLint mWidth;
145d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    EGLint mHeight;
146d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    EGLDisplay mDisplay;
147d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
148d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    bool mRunning;
149d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    bool mExit;
150d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
151d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    pthread_t mThreadId;
152d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
153d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    ObjectBaseRef<Script> mRootScript;
154d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    ObjectBaseRef<ProgramFragment> mFragment;
155d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    ObjectBaseRef<ProgramVertex> mVertex;
156d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    ObjectBaseRef<ProgramFragmentStore> mFragmentStore;
157d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
158730ee65d4ddb307898053b623120bad1655fadadJason Sams
159730ee65d4ddb307898053b623120bad1655fadadJason Sams    struct ObjDestroyOOB {
160730ee65d4ddb307898053b623120bad1655fadadJason Sams        pthread_mutex_t mMutex;
161730ee65d4ddb307898053b623120bad1655fadadJason Sams        Vector<ObjectBase *> mDestroyList;
162730ee65d4ddb307898053b623120bad1655fadadJason Sams        bool mNeedToEmpty;
163730ee65d4ddb307898053b623120bad1655fadadJason Sams    };
164730ee65d4ddb307898053b623120bad1655fadadJason Sams    ObjDestroyOOB mObjDestroy;
165730ee65d4ddb307898053b623120bad1655fadadJason Sams    bool objDestroyOOBInit();
166730ee65d4ddb307898053b623120bad1655fadadJason Sams    void objDestroyOOBRun();
167730ee65d4ddb307898053b623120bad1655fadadJason Sams    void objDestroyOOBDestroy();
168730ee65d4ddb307898053b623120bad1655fadadJason Sams
169d19f10d43aa400e1183aa21a97099d02074131a2Jason Samsprivate:
170d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    Context();
171d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
172d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    void initEGL();
173d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
1743eaa338e11a3b0d6b87d705e5bb95625e82347bdJason Sams    bool runScript(Script *s, uint32_t launchID);
175a09f11d6c641726b61f80c15230a18d31c146fecJason Sams    bool runRootScript();
176d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
177d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    static void * threadProc(void *);
178d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
179d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams    Surface *mWndSurface;
1803eaa338e11a3b0d6b87d705e5bb95625e82347bdJason Sams
1813eaa338e11a3b0d6b87d705e5bb95625e82347bdJason Sams    Vector<ObjectBase *> mNames;
182d7b3774da62d3c70cc7e8cf549967a1c823501e6Joe Onorato    KeyedVector<String8,int> mInt32Defines;
183d7b3774da62d3c70cc7e8cf549967a1c823501e6Joe Onorato    KeyedVector<String8,float> mFloatDefines;
184f4d160653fe405eba9d6f55448ac4599c6cadd77Jason Sams
185f4d160653fe405eba9d6f55448ac4599c6cadd77Jason Sams    uint64_t mTimers[_RS_TIMER_TOTAL];
186f4d160653fe405eba9d6f55448ac4599c6cadd77Jason Sams    Timers mTimerActive;
187f4d160653fe405eba9d6f55448ac4599c6cadd77Jason Sams    uint64_t mTimeLast;
188d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams};
189d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
190d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams
191d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams}
192d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams}
193d19f10d43aa400e1183aa21a97099d02074131a2Jason Sams#endif
194