1/*
2 * Copyright (C) 2011-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#include "../cpu_ref/rsd_cpu.h"
18
19#include "rsdCore.h"
20
21#include "rsdBcc.h"
22#include "rsdAllocation.h"
23
24#include "rsContext.h"
25#include "rsElement.h"
26#include "rsScriptC.h"
27
28#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
29#include "utils/Vector.h"
30#include "utils/Timers.h"
31#include "utils/StopWatch.h"
32#endif
33
34using namespace android;
35using namespace android::renderscript;
36
37
38bool rsdScriptInit(const Context *rsc,
39                     ScriptC *script,
40                     char const *resName,
41                     char const *cacheDir,
42                     uint8_t const *bitcode,
43                     size_t bitcodeSize,
44                     uint32_t flags) {
45    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
46    RsdCpuReference::CpuScript * cs =
47        dc->mCpuRef->createScript(script, resName, cacheDir, bitcode,
48                                  bitcodeSize, flags);
49    if (cs == nullptr) {
50        return false;
51    }
52    script->mHal.drv = cs;
53    cs->populateScript(script);
54    return true;
55}
56
57bool rsdInitIntrinsic(const Context *rsc, Script *s, RsScriptIntrinsicID iid,
58                      Element *e) {
59    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
60    RsdCpuReference::CpuScript * cs = dc->mCpuRef->createIntrinsic(s, iid, e);
61    if (cs == nullptr) {
62        return false;
63    }
64    s->mHal.drv = cs;
65    cs->populateScript(s);
66    return true;
67}
68
69void rsdScriptInvokeForEach(const Context *rsc,
70                            Script *s,
71                            uint32_t slot,
72                            const Allocation * ain,
73                            Allocation * aout,
74                            const void * usr,
75                            size_t usrLen,
76                            const RsScriptCall *sc) {
77
78    if (ain == nullptr) {
79        rsdScriptInvokeForEachMulti(rsc, s, slot, nullptr, 0, aout, usr, usrLen,
80                                    sc);
81    } else {
82        const Allocation *ains[1] = {ain};
83
84        rsdScriptInvokeForEachMulti(rsc, s, slot, ains, 1, aout, usr, usrLen,
85                                    sc);
86    }
87}
88
89void rsdScriptInvokeForEachMulti(const Context *rsc,
90                                 Script *s,
91                                 uint32_t slot,
92                                 const Allocation ** ains,
93                                 size_t inLen,
94                                 Allocation * aout,
95                                 const void * usr,
96                                 size_t usrLen,
97                                 const RsScriptCall *sc) {
98
99    RsdCpuReference::CpuScript *cs = (RsdCpuReference::CpuScript *)s->mHal.drv;
100    cs->invokeForEach(slot, ains, inLen, aout, usr, usrLen, sc);
101}
102
103
104int rsdScriptInvokeRoot(const Context *dc, Script *s) {
105    RsdCpuReference::CpuScript *cs = (RsdCpuReference::CpuScript *)s->mHal.drv;
106    return cs->invokeRoot();
107}
108
109void rsdScriptInvokeInit(const Context *dc, Script *s) {
110    RsdCpuReference::CpuScript *cs = (RsdCpuReference::CpuScript *)s->mHal.drv;
111    cs->invokeInit();
112}
113
114void rsdScriptInvokeFreeChildren(const Context *dc, Script *s) {
115    RsdCpuReference::CpuScript *cs = (RsdCpuReference::CpuScript *)s->mHal.drv;
116    cs->invokeFreeChildren();
117}
118
119void rsdScriptInvokeFunction(const Context *dc, Script *s,
120                            uint32_t slot,
121                            const void *params,
122                            size_t paramLength) {
123    RsdCpuReference::CpuScript *cs = (RsdCpuReference::CpuScript *)s->mHal.drv;
124    cs->invokeFunction(slot, params, paramLength);
125}
126
127void rsdScriptInvokeReduce(const Context *dc, Script *s,
128                           uint32_t slot,
129                           const Allocation ** ains, size_t inLen,
130                           Allocation *aout,
131                           const RsScriptCall *sc) {
132    RsdCpuReference::CpuScript *cs = (RsdCpuReference::CpuScript *)s->mHal.drv;
133    cs->invokeReduce(slot, ains, inLen, aout, sc);
134}
135
136void rsdScriptSetGlobalVar(const Context *dc, const Script *s,
137                           uint32_t slot, void *data, size_t dataLength) {
138    RsdCpuReference::CpuScript *cs = (RsdCpuReference::CpuScript *)s->mHal.drv;
139    cs->setGlobalVar(slot, data, dataLength);
140}
141
142void rsdScriptGetGlobalVar(const Context *dc, const Script *s,
143                           uint32_t slot, void *data, size_t dataLength) {
144    RsdCpuReference::CpuScript *cs = (RsdCpuReference::CpuScript *)s->mHal.drv;
145    cs->getGlobalVar(slot, data, dataLength);
146}
147
148
149void rsdScriptSetGlobalVarWithElemDims(const Context *dc, const Script *s,
150                                       uint32_t slot, void *data, size_t dataLength,
151                                       const android::renderscript::Element *elem,
152                                       const uint32_t *dims, size_t dimLength) {
153    RsdCpuReference::CpuScript *cs = (RsdCpuReference::CpuScript *)s->mHal.drv;
154    cs->setGlobalVarWithElemDims(slot, data, dataLength, elem, dims, dimLength);
155}
156
157void rsdScriptSetGlobalBind(const Context *dc, const Script *s, uint32_t slot, Allocation *data) {
158    RsdCpuReference::CpuScript *cs = (RsdCpuReference::CpuScript *)s->mHal.drv;
159    cs->setGlobalBind(slot, data);
160}
161
162void rsdScriptSetGlobalObj(const Context *dc, const Script *s, uint32_t slot, ObjectBase *data) {
163    RsdCpuReference::CpuScript *cs = (RsdCpuReference::CpuScript *)s->mHal.drv;
164    cs->setGlobalObj(slot, data);
165}
166
167void rsdScriptDestroy(const Context *dc, Script *s) {
168    RsdCpuReference::CpuScript *cs = (RsdCpuReference::CpuScript *)s->mHal.drv;
169    delete cs;
170    s->mHal.drv = nullptr;
171}
172
173
174Allocation * rsdScriptGetAllocationForPointer(const android::renderscript::Context *dc,
175                                              const android::renderscript::Script *sc,
176                                              const void *ptr) {
177    RsdCpuReference::CpuScript *cs = (RsdCpuReference::CpuScript *)sc->mHal.drv;
178    return cs->getAllocationForPointer(ptr);
179}
180
181void rsdScriptUpdateCachedObject(const Context *rsc,
182                                 const Script *script,
183                                 rs_script *obj)
184{
185    obj->p = script;
186#ifdef __LP64__
187    obj->r = nullptr;
188    if (script != nullptr) {
189        obj->v1 = script->mHal.drv;
190    } else {
191        obj->v1 = nullptr;
192    }
193    obj->v2 = nullptr;
194#endif
195}
196