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