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