rsCpuCore.h revision 14ce007a633b10e3b9a3fae29d8f53a7e8c9b59f
1709a0978ae141198018ca9769f8d96292a8928e6Jason Sams/*
2709a0978ae141198018ca9769f8d96292a8928e6Jason Sams * Copyright (C) 2012 The Android Open Source Project
3709a0978ae141198018ca9769f8d96292a8928e6Jason Sams *
4709a0978ae141198018ca9769f8d96292a8928e6Jason Sams * Licensed under the Apache License, Version 2.0 (the "License");
5709a0978ae141198018ca9769f8d96292a8928e6Jason Sams * you may not use this file except in compliance with the License.
6709a0978ae141198018ca9769f8d96292a8928e6Jason Sams * You may obtain a copy of the License at
7709a0978ae141198018ca9769f8d96292a8928e6Jason Sams *
8709a0978ae141198018ca9769f8d96292a8928e6Jason Sams *      http://www.apache.org/licenses/LICENSE-2.0
9709a0978ae141198018ca9769f8d96292a8928e6Jason Sams *
10709a0978ae141198018ca9769f8d96292a8928e6Jason Sams * Unless required by applicable law or agreed to in writing, software
11709a0978ae141198018ca9769f8d96292a8928e6Jason Sams * distributed under the License is distributed on an "AS IS" BASIS,
12709a0978ae141198018ca9769f8d96292a8928e6Jason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13709a0978ae141198018ca9769f8d96292a8928e6Jason Sams * See the License for the specific language governing permissions and
14709a0978ae141198018ca9769f8d96292a8928e6Jason Sams * limitations under the License.
15709a0978ae141198018ca9769f8d96292a8928e6Jason Sams */
16709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
17709a0978ae141198018ca9769f8d96292a8928e6Jason Sams#ifndef RSD_CPU_CORE_H
18709a0978ae141198018ca9769f8d96292a8928e6Jason Sams#define RSD_CPU_CORE_H
19709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
20709a0978ae141198018ca9769f8d96292a8928e6Jason Sams#include "rsd_cpu.h"
21709a0978ae141198018ca9769f8d96292a8928e6Jason Sams#include "rsSignal.h"
22709a0978ae141198018ca9769f8d96292a8928e6Jason Sams#include "rsContext.h"
23c31585b8ca865bf2b35abc79c8a8ee42de27bee8Yang Ni#include "rsCppUtils.h"
24709a0978ae141198018ca9769f8d96292a8928e6Jason Sams#include "rsElement.h"
25709a0978ae141198018ca9769f8d96292a8928e6Jason Sams#include "rsScriptC.h"
26dced5c96bc53c45a1aac782ea9bd738b0d50fd09David Gross#include "rsCpuCoreRuntime.h"
27709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
28709a0978ae141198018ca9769f8d96292a8928e6Jason Samsnamespace android {
29709a0978ae141198018ca9769f8d96292a8928e6Jason Samsnamespace renderscript {
30709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
3111fd9ec1ab8dfa7ae45c6edeea48dddc4633efeaMatt Wala// Whether the CPU we're running on supports SIMD instructions
32f5ef8df639ba6363aa5d546e57ce872d04144cb6Jason Samsextern bool gArchUseSIMD;
33709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
3414ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala// Function types found in RenderScript code
3514ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Walatypedef void (*ReduceFunc_t)(const uint8_t *inBuf, uint8_t *outBuf, uint32_t len);
3614ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Walatypedef void (*ForEachFunc_t)(const RsExpandKernelDriverInfo *info, uint32_t x1, uint32_t x2, uint32_t outStride);
3714ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Walatypedef void (*InvokeFunc_t)(void *params);
3814ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Walatypedef void (*InitOrDtorFunc_t)(void);
3914ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Walatypedef int  (*RootFunc_t)(void);
4014ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala
4114ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala// Internal driver callback used to execute a kernel
42709a0978ae141198018ca9769f8d96292a8928e6Jason Samstypedef void (*WorkerCallback_t)(void *usr, uint32_t idx);
43709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
44709a0978ae141198018ca9769f8d96292a8928e6Jason Samsclass RsdCpuScriptImpl;
45709a0978ae141198018ca9769f8d96292a8928e6Jason Samsclass RsdCpuReferenceImpl;
46709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
47f37121300217d3b39ab66dd9c8881bcbcad932dfChris Wailesstruct ScriptTLSStruct {
48709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    android::renderscript::Context * mContext;
49709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    const android::renderscript::Script * mScript;
50709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    RsdCpuScriptImpl *mImpl;
51f37121300217d3b39ab66dd9c8881bcbcad932dfChris Wailes};
52709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
5314ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala// MTLaunchStruct passes information about a multithreaded kernel launch.
5414ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Walastruct MTLaunchStructCommon {
5514ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    RsdCpuReferenceImpl *rs;
56709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    RsdCpuScriptImpl *script;
57709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
58709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    uint32_t mSliceSize;
59709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    volatile int mSliceNum;
60709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    bool isThreadable;
61709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
6214ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    // Boundary information about the launch
63bf2111d3b3de310932099514f06924e48fa1d7b2Jason Sams    RsLaunchDimensions start;
64bf2111d3b3de310932099514f06924e48fa1d7b2Jason Sams    RsLaunchDimensions end;
6514ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    // Points to MTLaunchStructForEach::fep::dim or
6614ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    // MTLaunchStructReduce::inputDim.
6714ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    RsLaunchDimensions *dimPtr;
6814ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala};
6914ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala
7014ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Walastruct MTLaunchStructForEach : public MTLaunchStructCommon {
7114ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    // Driver info structure
7214ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    RsExpandKernelDriverInfo fep;
7314ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala
7414ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    ForEachFunc_t kernel;
7514ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    uint32_t sig;
7614ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    const Allocation *ains[RS_KERNEL_INPUT_LIMIT];
7714ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    Allocation *aout[RS_KERNEL_INPUT_LIMIT];
7814ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala};
7914ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala
8014ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Walastruct MTLaunchStructReduce : public MTLaunchStructCommon {
8114ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    ReduceFunc_t kernel;
8214ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    const uint8_t *inBuf;
8314ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    uint8_t *outBuf;
8414ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    RsLaunchDimensions inputDim;
85f37121300217d3b39ab66dd9c8881bcbcad932dfChris Wailes};
86709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
87709a0978ae141198018ca9769f8d96292a8928e6Jason Samsclass RsdCpuReferenceImpl : public RsdCpuReference {
88709a0978ae141198018ca9769f8d96292a8928e6Jason Samspublic:
89c060f1435e7b9405f3be8974417fa6f410f03753Stephen Hines    ~RsdCpuReferenceImpl() override;
90709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    RsdCpuReferenceImpl(Context *);
91709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
92709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    void lockMutex();
93709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    void unlockMutex();
94709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
95709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    bool init(uint32_t version_major, uint32_t version_minor, sym_lookup_t, script_lookup_t);
96c060f1435e7b9405f3be8974417fa6f410f03753Stephen Hines    void setPriority(int32_t priority) override;
97709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    virtual void launchThreads(WorkerCallback_t cbk, void *data);
98709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    static void * helperThreadProc(void *vrsc);
99709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    RsdCpuScriptImpl * setTLS(RsdCpuScriptImpl *sc);
100709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
101709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    Context * getContext() {return mRSC;}
102c44d6706868749abe37780fc28b2cc627ddcf269Jason Sams    uint32_t getThreadCount() const {
103c44d6706868749abe37780fc28b2cc627ddcf269Jason Sams        return mWorkers.mCount + 1;
104c44d6706868749abe37780fc28b2cc627ddcf269Jason Sams    }
105709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
10614ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    // Launch foreach kernel
10714ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    void launchForEach(const Allocation **ains, uint32_t inLen, Allocation *aout,
10814ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala                       const RsScriptCall *sc, MTLaunchStructForEach *mtls);
10914ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala
11014ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    // Launch a reduce kernel
11114ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    void launchReduce(const Allocation *ain, Allocation *aout,
11214ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala                      MTLaunchStructReduce *mtls);
1134b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes
114c060f1435e7b9405f3be8974417fa6f410f03753Stephen Hines    CpuScript * createScript(const ScriptC *s, char const *resName, char const *cacheDir,
115c060f1435e7b9405f3be8974417fa6f410f03753Stephen Hines                             uint8_t const *bitcode, size_t bitcodeSize, uint32_t flags) override;
116c060f1435e7b9405f3be8974417fa6f410f03753Stephen Hines    CpuScript * createIntrinsic(const Script *s, RsScriptIntrinsicID iid, Element *e) override;
117c060f1435e7b9405f3be8974417fa6f410f03753Stephen Hines    void* createScriptGroup(const ScriptGroupBase *sg) override;
118709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
119709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    const RsdCpuReference::CpuSymbol *symLookup(const char *);
120709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
12114ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    RsdCpuReference::CpuScript *lookupScript(const Script *s) {
122709a0978ae141198018ca9769f8d96292a8928e6Jason Sams        return mScriptLookupFn(mRSC, s);
123709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    }
124709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
1251d476620399d54774e4fd386c1d23cc583d49522Stephen Hines    void setSelectRTCallback(RSSelectRTCallback pSelectRTCallback) {
1261d476620399d54774e4fd386c1d23cc583d49522Stephen Hines        mSelectRTCallback = pSelectRTCallback;
1271d476620399d54774e4fd386c1d23cc583d49522Stephen Hines    }
1281d476620399d54774e4fd386c1d23cc583d49522Stephen Hines    RSSelectRTCallback getSelectRTCallback() {
1291d476620399d54774e4fd386c1d23cc583d49522Stephen Hines        return mSelectRTCallback;
1301d476620399d54774e4fd386c1d23cc583d49522Stephen Hines    }
131b7d9c80c98fc96aa7c638e3124be24f13a6436b2Stephen Hines
132005113297b19ed256b6db9d6bc293ed9266899fcStephen Hines    virtual void setBccPluginName(const char *name) {
133c31585b8ca865bf2b35abc79c8a8ee42de27bee8Yang Ni        mBccPluginName.setTo(name);
134005113297b19ed256b6db9d6bc293ed9266899fcStephen Hines    }
135005113297b19ed256b6db9d6bc293ed9266899fcStephen Hines    virtual const char *getBccPluginName() const {
136c31585b8ca865bf2b35abc79c8a8ee42de27bee8Yang Ni        return mBccPluginName.string();
137005113297b19ed256b6db9d6bc293ed9266899fcStephen Hines    }
138c060f1435e7b9405f3be8974417fa6f410f03753Stephen Hines    bool getInForEach() override { return mInForEach; }
139709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
1408409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines    // Set to true if we should embed global variable information in the code.
141c060f1435e7b9405f3be8974417fa6f410f03753Stephen Hines    void setEmbedGlobalInfo(bool v) override {
1428409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines        mEmbedGlobalInfo = v;
1438409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines    }
1448409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines
1458409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines    // Returns true if we should embed global variable information in the code.
146c060f1435e7b9405f3be8974417fa6f410f03753Stephen Hines    bool getEmbedGlobalInfo() const override {
1478409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines        return mEmbedGlobalInfo;
1488409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines    }
1498409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines
1508409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines    // Set to true if we should skip constant (immutable) global variables when
1518409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines    // potentially embedding information about globals.
152c060f1435e7b9405f3be8974417fa6f410f03753Stephen Hines    void setEmbedGlobalInfoSkipConstant(bool v) override {
1538409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines        mEmbedGlobalInfoSkipConstant = v;
1548409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines    }
1558409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines
1568409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines    // Returns true if we should skip constant (immutable) global variables when
1578409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines    // potentially embedding information about globals.
158c060f1435e7b9405f3be8974417fa6f410f03753Stephen Hines    bool getEmbedGlobalInfoSkipConstant() const override {
1598409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines        return mEmbedGlobalInfoSkipConstant;
1608409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines    }
1618409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines
162709a0978ae141198018ca9769f8d96292a8928e6Jason Samsprotected:
163709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    Context *mRSC;
164709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    uint32_t version_major;
165709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    uint32_t version_minor;
166709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    //bool mHasGraphics;
167709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    bool mInForEach;
168709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
169709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    struct Workers {
170709a0978ae141198018ca9769f8d96292a8928e6Jason Sams        volatile int mRunningCount;
171709a0978ae141198018ca9769f8d96292a8928e6Jason Sams        volatile int mLaunchCount;
172709a0978ae141198018ca9769f8d96292a8928e6Jason Sams        uint32_t mCount;
173709a0978ae141198018ca9769f8d96292a8928e6Jason Sams        pthread_t *mThreadId;
174709a0978ae141198018ca9769f8d96292a8928e6Jason Sams        pid_t *mNativeThreadId;
175709a0978ae141198018ca9769f8d96292a8928e6Jason Sams        Signal mCompleteSignal;
176709a0978ae141198018ca9769f8d96292a8928e6Jason Sams        Signal *mLaunchSignals;
177709a0978ae141198018ca9769f8d96292a8928e6Jason Sams        WorkerCallback_t mLaunchCallback;
178709a0978ae141198018ca9769f8d96292a8928e6Jason Sams        void *mLaunchData;
179709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    };
180709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    Workers mWorkers;
181709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    bool mExit;
182709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    sym_lookup_t mSymLookupFn;
183709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    script_lookup_t mScriptLookupFn;
184709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
185709a0978ae141198018ca9769f8d96292a8928e6Jason Sams    ScriptTLSStruct mTlsStruct;
186f218bf115af4ae4fd79adbb8842608b308a4cf07Stephen Hines
1871d476620399d54774e4fd386c1d23cc583d49522Stephen Hines    RSSelectRTCallback mSelectRTCallback;
188c31585b8ca865bf2b35abc79c8a8ee42de27bee8Yang Ni    String8 mBccPluginName;
1898409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines
1908409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines    // Specifies whether we should embed global variable information in the
1918409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines    // code via special RS variables that can be examined later by the driver.
1928409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines    // Defaults to true.
1938409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines    bool mEmbedGlobalInfo;
1948409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines
1958409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines    // Specifies whether we should skip constant (immutable) global variables
1968409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines    // when potentially embedding information about globals.
1978409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines    // Defaults to true.
1988409d6414dd4a42aa59779fcfe9fce18648cb135Stephen Hines    bool mEmbedGlobalInfoSkipConstant;
199709a0978ae141198018ca9769f8d96292a8928e6Jason Sams};
200709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
201709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
202709a0978ae141198018ca9769f8d96292a8928e6Jason Sams}
203709a0978ae141198018ca9769f8d96292a8928e6Jason Sams}
204709a0978ae141198018ca9769f8d96292a8928e6Jason Sams
205709a0978ae141198018ca9769f8d96292a8928e6Jason Sams#endif
206