rsCpuCore.h revision 005113297b19ed256b6db9d6bc293ed9266899fc
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
26#include <string>
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
46typedef struct ScriptTLSStructRec {
47    android::renderscript::Context * mContext;
48    const android::renderscript::Script * mScript;
49    RsdCpuScriptImpl *mImpl;
50} ScriptTLSStruct;
51
52typedef struct {
53    RsForEachStubParamStruct fep;
54
55    RsdCpuReferenceImpl *rsc;
56    RsdCpuScriptImpl *script;
57
58    ForEachFunc_t kernel;
59    uint32_t sig;
60    const Allocation * ain;
61    Allocation * aout;
62
63    uint32_t mSliceSize;
64    volatile int mSliceNum;
65    bool isThreadable;
66
67    uint32_t xStart;
68    uint32_t xEnd;
69    uint32_t yStart;
70    uint32_t yEnd;
71    uint32_t zStart;
72    uint32_t zEnd;
73    uint32_t arrayStart;
74    uint32_t arrayEnd;
75} MTLaunchStruct;
76
77
78
79
80class RsdCpuReferenceImpl : public RsdCpuReference {
81public:
82    virtual ~RsdCpuReferenceImpl();
83    RsdCpuReferenceImpl(Context *);
84
85    void lockMutex();
86    void unlockMutex();
87
88    bool init(uint32_t version_major, uint32_t version_minor, sym_lookup_t, script_lookup_t);
89    virtual void setPriority(int32_t priority);
90    virtual void launchThreads(WorkerCallback_t cbk, void *data);
91    static void * helperThreadProc(void *vrsc);
92    RsdCpuScriptImpl * setTLS(RsdCpuScriptImpl *sc);
93
94    Context * getContext() {return mRSC;}
95    uint32_t getThreadCount() const {
96        return mWorkers.mCount + 1;
97    }
98
99    void launchThreads(const Allocation * ain, Allocation * aout,
100                       const RsScriptCall *sc, MTLaunchStruct *mtls);
101
102    virtual CpuScript * createScript(const ScriptC *s,
103                                     char const *resName, char const *cacheDir,
104                                     uint8_t const *bitcode, size_t bitcodeSize,
105                                     uint32_t flags);
106    virtual CpuScript * createIntrinsic(const Script *s,
107                                        RsScriptIntrinsicID iid, Element *e);
108    virtual CpuScriptGroup * createScriptGroup(const ScriptGroup *sg);
109
110    const RsdCpuReference::CpuSymbol *symLookup(const char *);
111
112    RsdCpuReference::CpuScript * lookupScript(const Script *s) {
113        return mScriptLookupFn(mRSC, s);
114    }
115
116#ifndef RS_COMPATIBILITY_LIB
117    void setLinkRuntimeCallback(
118            bcc::RSLinkRuntimeCallback pLinkRuntimeCallback) {
119        mLinkRuntimeCallback = pLinkRuntimeCallback;
120    }
121    bcc::RSLinkRuntimeCallback getLinkRuntimeCallback() {
122        return mLinkRuntimeCallback;
123    }
124
125    void setSelectRTCallback(RSSelectRTCallback pSelectRTCallback) {
126        mSelectRTCallback = pSelectRTCallback;
127    }
128    RSSelectRTCallback getSelectRTCallback() {
129        return mSelectRTCallback;
130    }
131
132    virtual void setSetupCompilerCallback(
133            RSSetupCompilerCallback pSetupCompilerCallback) {
134        mSetupCompilerCallback = pSetupCompilerCallback;
135    }
136    virtual RSSetupCompilerCallback getSetupCompilerCallback() const {
137        return mSetupCompilerCallback;
138    }
139
140    virtual void setBccPluginName(const char *name) {
141        mBccPluginName.assign(name);
142    }
143    virtual const char *getBccPluginName() const {
144        return mBccPluginName.c_str();
145    }
146#endif
147    virtual bool getInForEach() { return mInForEach; }
148
149protected:
150    Context *mRSC;
151    uint32_t version_major;
152    uint32_t version_minor;
153    //bool mHasGraphics;
154    bool mInForEach;
155
156    struct Workers {
157        volatile int mRunningCount;
158        volatile int mLaunchCount;
159        uint32_t mCount;
160        pthread_t *mThreadId;
161        pid_t *mNativeThreadId;
162        Signal mCompleteSignal;
163        Signal *mLaunchSignals;
164        WorkerCallback_t mLaunchCallback;
165        void *mLaunchData;
166    };
167    Workers mWorkers;
168    bool mExit;
169    sym_lookup_t mSymLookupFn;
170    script_lookup_t mScriptLookupFn;
171
172    ScriptTLSStruct mTlsStruct;
173
174#ifndef RS_COMPATIBILITY_LIB
175    bcc::RSLinkRuntimeCallback mLinkRuntimeCallback;
176    RSSelectRTCallback mSelectRTCallback;
177    RSSetupCompilerCallback mSetupCompilerCallback;
178    std::string mBccPluginName;
179#endif
180};
181
182
183}
184}
185
186#endif
187