rsCpuCore.h revision f5ef8df639ba6363aa5d546e57ce872d04144cb6
1/*
2 * Copyright (C) 2012 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 RSD_CPU_CORE_H
18#define RSD_CPU_CORE_H
19
20#include "rsd_cpu.h"
21#include "rsSignal.h"
22#include "rsContext.h"
23#include "rsElement.h"
24#include "rsScriptC.h"
25
26namespace bcc {
27    class BCCContext;
28    class RSCompilerDriver;
29    class RSExecutable;
30}
31
32namespace android {
33namespace renderscript {
34
35extern bool gArchUseSIMD;
36
37typedef void (* InvokeFunc_t)(void);
38typedef void (* ForEachFunc_t)(void);
39typedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
40
41class RsdCpuScriptImpl;
42class RsdCpuReferenceImpl;
43
44typedef struct ScriptTLSStructRec {
45    android::renderscript::Context * mContext;
46    const android::renderscript::Script * mScript;
47    RsdCpuScriptImpl *mImpl;
48} ScriptTLSStruct;
49
50typedef struct {
51    RsForEachStubParamStruct fep;
52
53    RsdCpuReferenceImpl *rsc;
54    RsdCpuScriptImpl *script;
55
56    ForEachFunc_t kernel;
57    uint32_t sig;
58    const Allocation * ain;
59    Allocation * aout;
60
61    uint32_t mSliceSize;
62    volatile int mSliceNum;
63    bool isThreadable;
64
65    uint32_t xStart;
66    uint32_t xEnd;
67    uint32_t yStart;
68    uint32_t yEnd;
69    uint32_t zStart;
70    uint32_t zEnd;
71    uint32_t arrayStart;
72    uint32_t arrayEnd;
73} MTLaunchStruct;
74
75
76
77
78class RsdCpuReferenceImpl : public RsdCpuReference {
79public:
80    virtual ~RsdCpuReferenceImpl();
81    RsdCpuReferenceImpl(Context *);
82
83    void lockMutex();
84    void unlockMutex();
85
86    bool init(uint32_t version_major, uint32_t version_minor, sym_lookup_t, script_lookup_t);
87    virtual void setPriority(int32_t priority);
88    virtual void launchThreads(WorkerCallback_t cbk, void *data);
89    static void * helperThreadProc(void *vrsc);
90    RsdCpuScriptImpl * setTLS(RsdCpuScriptImpl *sc);
91
92    Context * getContext() {return mRSC;}
93    uint32_t getThreadCount() const {
94        return mWorkers.mCount + 1;
95    }
96
97    void launchThreads(const Allocation * ain, Allocation * aout,
98                       const RsScriptCall *sc, MTLaunchStruct *mtls);
99
100    virtual CpuScript * createScript(const ScriptC *s,
101                                     char const *resName, char const *cacheDir,
102                                     uint8_t const *bitcode, size_t bitcodeSize,
103                                     uint32_t flags);
104    virtual CpuScript * createIntrinsic(const Script *s,
105                                        RsScriptIntrinsicID iid, Element *e);
106    virtual CpuScriptGroup * createScriptGroup(const ScriptGroup *sg);
107
108    const RsdCpuReference::CpuSymbol *symLookup(const char *);
109
110    RsdCpuReference::CpuScript * lookupScript(const Script *s) {
111        return mScriptLookupFn(mRSC, s);
112    }
113
114#ifndef RS_COMPATIBILITY_LIB
115    void setLinkRuntimeCallback(
116            bcc::RSLinkRuntimeCallback pLinkRuntimeCallback) {
117        mLinkRuntimeCallback = pLinkRuntimeCallback;
118    }
119    bcc::RSLinkRuntimeCallback getLinkRuntimeCallback() {
120        return mLinkRuntimeCallback;
121    }
122
123    void setSelectRTCallback(RSSelectRTCallback pSelectRTCallback) {
124        mSelectRTCallback = pSelectRTCallback;
125    }
126    RSSelectRTCallback getSelectRTCallback() {
127        return mSelectRTCallback;
128    }
129#endif
130    virtual bool getInForEach() { return mInForEach; }
131
132protected:
133    Context *mRSC;
134    uint32_t version_major;
135    uint32_t version_minor;
136    //bool mHasGraphics;
137    bool mInForEach;
138
139    struct Workers {
140        volatile int mRunningCount;
141        volatile int mLaunchCount;
142        uint32_t mCount;
143        pthread_t *mThreadId;
144        pid_t *mNativeThreadId;
145        Signal mCompleteSignal;
146        Signal *mLaunchSignals;
147        WorkerCallback_t mLaunchCallback;
148        void *mLaunchData;
149    };
150    Workers mWorkers;
151    bool mExit;
152    sym_lookup_t mSymLookupFn;
153    script_lookup_t mScriptLookupFn;
154
155    ScriptTLSStruct mTlsStruct;
156
157#ifndef RS_COMPATIBILITY_LIB
158    bcc::RSLinkRuntimeCallback mLinkRuntimeCallback;
159    RSSelectRTCallback mSelectRTCallback;
160#endif
161};
162
163
164}
165}
166
167#endif
168