1326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams/*
24419977d78018a9933c7f455fe001f644f2d638bStephen Hines * Copyright (C) 2009-2012 The Android Open Source Project
3326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
4326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * Licensed under the Apache License, Version 2.0 (the "License");
5326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * you may not use this file except in compliance with the License.
6326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * You may obtain a copy of the License at
7326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
8326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *      http://www.apache.org/licenses/LICENSE-2.0
9326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
10326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * Unless required by applicable law or agreed to in writing, software
11326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * distributed under the License is distributed on an "AS IS" BASIS,
12326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * See the License for the specific language governing permissions and
14326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * limitations under the License.
15326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams */
16326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
17326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#include "rsContext.h"
18326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#include "rsScriptC.h"
191ef8b80bc49f415b93912fe7335f852953bde4a4Jack Palevich
20f4d216ee0f0ef18edbde208a61dc4e2fe4c8f050Jean-Luc Brouillet#if !defined(RS_COMPATIBILITY_LIB) && !defined(ANDROID_RS_SERIALIZE)
21cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines#include <bcinfo/BitcodeTranslator.h>
22f8d4469e2f51740bd7c0e48f6da024a75c4ef372Stephen Hines#include <bcinfo/BitcodeWrapper.h>
23cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines#endif
24cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines
25b0934b67b95cc27e2358c2aa4db5f7c1067c8f9bStephen Hines#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
260b575de8ed0b628d84d256f5846500b0385979bdTim Murray#include "utils/Timers.h"
2747935ac8e3dd619f758abd576605a564e33bb59cTobias Grosser#include "cutils/trace.h"
28574854bcb2eb25a85b9b52faf2fb3e743fa7aa14Stephen Hines#endif
2947935ac8e3dd619f758abd576605a564e33bb59cTobias Grosser
30e78c14bd277e60d5619516e4088a04572a5b250fTim Murray#include <sys/stat.h>
31e78c14bd277e60d5619516e4088a04572a5b250fTim Murray
32b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni#include <string>
33b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni
346847e73314e13aa02231268cca245a81eb0539caChris Wailes#ifdef USE_MINGW
356847e73314e13aa02231268cca245a81eb0539caChris Wailes/* Define the default path separator for the platform. */
366847e73314e13aa02231268cca245a81eb0539caChris Wailes#define OS_PATH_SEPARATOR     '\\'
376847e73314e13aa02231268cca245a81eb0539caChris Wailes#define OS_PATH_SEPARATOR_STR "\\"
386847e73314e13aa02231268cca245a81eb0539caChris Wailes
396847e73314e13aa02231268cca245a81eb0539caChris Wailes#else /* not USE_MINGW */
406847e73314e13aa02231268cca245a81eb0539caChris Wailes
416847e73314e13aa02231268cca245a81eb0539caChris Wailes/* Define the default path separator for the platform. */
426847e73314e13aa02231268cca245a81eb0539caChris Wailes#define OS_PATH_SEPARATOR     '/'
436847e73314e13aa02231268cca245a81eb0539caChris Wailes#define OS_PATH_SEPARATOR_STR "/"
446847e73314e13aa02231268cca245a81eb0539caChris Wailes
456847e73314e13aa02231268cca245a81eb0539caChris Wailes#endif
466847e73314e13aa02231268cca245a81eb0539caChris Wailes
47326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsusing namespace android;
48326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsusing namespace android::renderscript;
49326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
50e57691037aea219562ac686429b4b98202aab7bcJason Sams#define GET_TLS()  Context::ScriptTLSStruct * tls = \
51e57691037aea219562ac686429b4b98202aab7bcJason Sams    (Context::ScriptTLSStruct *)pthread_getspecific(Context::gThreadTLSKey); \
52e57691037aea219562ac686429b4b98202aab7bcJason Sams    Context * rsc = tls->mContext; \
53e57691037aea219562ac686429b4b98202aab7bcJason Sams    ScriptC * sc = (ScriptC *) tls->mScript
54e57691037aea219562ac686429b4b98202aab7bcJason Sams
55afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex SakhartchoukScriptC::ScriptC(Context *rsc) : Script(rsc) {
56f4d216ee0f0ef18edbde208a61dc4e2fe4c8f050Jean-Luc Brouillet#if !defined(RS_COMPATIBILITY_LIB) && !defined(ANDROID_RS_SERIALIZE)
5744bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    BT = nullptr;
58cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines#endif
59326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
60326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
61afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex SakhartchoukScriptC::~ScriptC() {
62f4d216ee0f0ef18edbde208a61dc4e2fe4c8f050Jean-Luc Brouillet#if !defined(RS_COMPATIBILITY_LIB) && !defined(ANDROID_RS_SERIALIZE)
63cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines    if (BT) {
64cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines        delete BT;
6544bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        BT = nullptr;
66cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines    }
67cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines#endif
6877020c5248ca0921d7d24fb60dec5105ba39026cJason Sams    if (mInitialized) {
6977020c5248ca0921d7d24fb60dec5105ba39026cJason Sams        mRSC->mHal.funcs.script.invokeFreeChildren(mRSC, this);
7077020c5248ca0921d7d24fb60dec5105ba39026cJason Sams        mRSC->mHal.funcs.script.destroy(mRSC, this);
7177020c5248ca0921d7d24fb60dec5105ba39026cJason Sams    }
72326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
73326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
7493eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
75e78c14bd277e60d5619516e4088a04572a5b250fTim Murraybool ScriptC::createCacheDir(const char *cacheDir) {
766847e73314e13aa02231268cca245a81eb0539caChris Wailes    std::string currentDir;
776847e73314e13aa02231268cca245a81eb0539caChris Wailes    const std::string cacheDirString(cacheDir);
786847e73314e13aa02231268cca245a81eb0539caChris Wailes
79e78c14bd277e60d5619516e4088a04572a5b250fTim Murray    struct stat statBuf;
80e78c14bd277e60d5619516e4088a04572a5b250fTim Murray    int statReturn = stat(cacheDir, &statBuf);
81e78c14bd277e60d5619516e4088a04572a5b250fTim Murray    if (!statReturn) {
82e78c14bd277e60d5619516e4088a04572a5b250fTim Murray        return true;
83e78c14bd277e60d5619516e4088a04572a5b250fTim Murray    }
84e78c14bd277e60d5619516e4088a04572a5b250fTim Murray
856847e73314e13aa02231268cca245a81eb0539caChris Wailes    // Start from the beginning of the cacheDirString.
866847e73314e13aa02231268cca245a81eb0539caChris Wailes    int currPos = 0;
876847e73314e13aa02231268cca245a81eb0539caChris Wailes
886847e73314e13aa02231268cca245a81eb0539caChris Wailes    // Reserve space in currentDir for the entire cacheDir path.
896847e73314e13aa02231268cca245a81eb0539caChris Wailes    currentDir.reserve(cacheDirString.length());
906847e73314e13aa02231268cca245a81eb0539caChris Wailes
916847e73314e13aa02231268cca245a81eb0539caChris Wailes    while (currPos >= 0) {
926847e73314e13aa02231268cca245a81eb0539caChris Wailes        /*
936847e73314e13aa02231268cca245a81eb0539caChris Wailes         * The character at currPos should be a path separator.  We need to look
946847e73314e13aa02231268cca245a81eb0539caChris Wailes         * for the next one.
956847e73314e13aa02231268cca245a81eb0539caChris Wailes         */
966847e73314e13aa02231268cca245a81eb0539caChris Wailes        int nextPos = cacheDirString.find(OS_PATH_SEPARATOR_STR, currPos + 1);
976847e73314e13aa02231268cca245a81eb0539caChris Wailes
986847e73314e13aa02231268cca245a81eb0539caChris Wailes        if (nextPos > 0) {
996847e73314e13aa02231268cca245a81eb0539caChris Wailes            // A new path separator has been found.
1006847e73314e13aa02231268cca245a81eb0539caChris Wailes            currentDir += cacheDirString.substr(currPos, nextPos - currPos);
1016847e73314e13aa02231268cca245a81eb0539caChris Wailes        } else {
1026847e73314e13aa02231268cca245a81eb0539caChris Wailes            // There are no more path separators.
1036847e73314e13aa02231268cca245a81eb0539caChris Wailes            currentDir += cacheDirString.substr(currPos);
1046847e73314e13aa02231268cca245a81eb0539caChris Wailes        }
1056847e73314e13aa02231268cca245a81eb0539caChris Wailes
1066847e73314e13aa02231268cca245a81eb0539caChris Wailes        currPos = nextPos;
107e78c14bd277e60d5619516e4088a04572a5b250fTim Murray
1086847e73314e13aa02231268cca245a81eb0539caChris Wailes        statReturn = stat(currentDir.c_str(), &statBuf);
109e78c14bd277e60d5619516e4088a04572a5b250fTim Murray
110e78c14bd277e60d5619516e4088a04572a5b250fTim Murray        if (statReturn) {
111e78c14bd277e60d5619516e4088a04572a5b250fTim Murray            if (errno == ENOENT) {
1126847e73314e13aa02231268cca245a81eb0539caChris Wailes                if (mkdir(currentDir.c_str(), S_IRUSR | S_IWUSR | S_IXUSR)) {
113e78c14bd277e60d5619516e4088a04572a5b250fTim Murray                    ALOGE("Couldn't create cache directory: %s",
1146847e73314e13aa02231268cca245a81eb0539caChris Wailes                          currentDir.c_str());
115e78c14bd277e60d5619516e4088a04572a5b250fTim Murray                    ALOGE("Error: %s", strerror(errno));
116e78c14bd277e60d5619516e4088a04572a5b250fTim Murray                    return false;
117e78c14bd277e60d5619516e4088a04572a5b250fTim Murray                }
118e78c14bd277e60d5619516e4088a04572a5b250fTim Murray            } else {
119e78c14bd277e60d5619516e4088a04572a5b250fTim Murray                ALOGE("Stat error: %s", strerror(errno));
120e78c14bd277e60d5619516e4088a04572a5b250fTim Murray                return false;
121e78c14bd277e60d5619516e4088a04572a5b250fTim Murray            }
122e78c14bd277e60d5619516e4088a04572a5b250fTim Murray        }
123e78c14bd277e60d5619516e4088a04572a5b250fTim Murray    }
124e78c14bd277e60d5619516e4088a04572a5b250fTim Murray    return true;
125e78c14bd277e60d5619516e4088a04572a5b250fTim Murray}
12693eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
127e78c14bd277e60d5619516e4088a04572a5b250fTim Murray
128afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ScriptC::setupScript(Context *rsc) {
1290b575de8ed0b628d84d256f5846500b0385979bdTim Murray#ifndef RS_SERVER
130c61346b91434307c5003029017b54ce9c49112beJason Sams    mEnviroment.mStartTimeMillis
131c61346b91434307c5003029017b54ce9c49112beJason Sams                = nanoseconds_to_milliseconds(systemTime(SYSTEM_TIME_MONOTONIC));
1320b575de8ed0b628d84d256f5846500b0385979bdTim Murray#endif
133c61346b91434307c5003029017b54ce9c49112beJason Sams
134bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams    for (uint32_t ct=0; ct < mHal.info.exportedVariableCount; ct++) {
135900f1616bf33c7ba13cf2a737832a95bcd176388Jason Sams        if (mSlots[ct].get() && !mTypes[ct].get()) {
136900f1616bf33c7ba13cf2a737832a95bcd176388Jason Sams            mTypes[ct].set(mSlots[ct]->getType());
137900f1616bf33c7ba13cf2a737832a95bcd176388Jason Sams        }
138900f1616bf33c7ba13cf2a737832a95bcd176388Jason Sams
139900f1616bf33c7ba13cf2a737832a95bcd176388Jason Sams        if (!mTypes[ct].get())
140be36bf3a76481737a7fa606a04144ceef80eb4f2Jason Sams            continue;
141807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams        rsc->mHal.funcs.script.setGlobalBind(rsc, this, ct, mSlots[ct].get());
142ce92d4baf7a5bce097228fdd4498601764cd4014Jason Sams    }
143ce92d4baf7a5bce097228fdd4498601764cd4014Jason Sams}
144ce92d4baf7a5bce097228fdd4498601764cd4014Jason Sams
145afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukvoid ScriptC::setupGLState(Context *rsc) {
14693eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
147a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216Jason Sams    if (mEnviroment.mFragmentStore.get()) {
14860709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams        rsc->setProgramStore(mEnviroment.mFragmentStore.get());
149a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216Jason Sams    }
150a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216Jason Sams    if (mEnviroment.mFragment.get()) {
15160709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams        rsc->setProgramFragment(mEnviroment.mFragment.get());
152a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216Jason Sams    }
1538ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    if (mEnviroment.mVertex.get()) {
15460709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams        rsc->setProgramVertex(mEnviroment.mVertex.get());
1558ce125be69531dbf3a7e856d5e59d1b8e2789db0Jason Sams    }
156b681c8af69ef6938c80dd5cc5fc920db94ee9f43Jason Sams    if (mEnviroment.mRaster.get()) {
15760709257bbdeb0c50f39b9c8969dc76264d6e142Jason Sams        rsc->setProgramRaster(mEnviroment.mRaster.get());
158b681c8af69ef6938c80dd5cc5fc920db94ee9f43Jason Sams    }
15993eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
160c61346b91434307c5003029017b54ce9c49112beJason Sams}
161a0a1b6fbece2eb8d72d788422ab3e5f58d5a9216Jason Sams
162afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukuint32_t ScriptC::run(Context *rsc) {
16344bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    if (mHal.info.root == nullptr) {
164c61346b91434307c5003029017b54ce9c49112beJason Sams        rsc->setError(RS_ERROR_BAD_SCRIPT, "Attempted to run bad script");
165c61346b91434307c5003029017b54ce9c49112beJason Sams        return 0;
1669c4e4ca9ff75a7fe18544c83fcf782e46c9b6ac2Joe Onorato    }
167c61346b91434307c5003029017b54ce9c49112beJason Sams
1681f24db404b2cf8768bf2bd53a3caeb5812b8ce9fJason Sams    setupGLState(rsc);
169c61346b91434307c5003029017b54ce9c49112beJason Sams    setupScript(rsc);
1701d54f10f3c23e0d7ec57e52ec3b0701a2a5ed24eJason Sams
1712dca84dd6c07992f78ad050177975f16486dd77eJason Sams    uint32_t ret = 0;
172b9077f48e2c5aff1838bbbeccf56c2b5f5490ab6Jason Sams
173b9077f48e2c5aff1838bbbeccf56c2b5f5490ab6Jason Sams    if (rsc->props.mLogScripts) {
1746598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block        ALOGV("%p ScriptC::run invoking root,  ptr %p", rsc, mHal.info.root);
175b9077f48e2c5aff1838bbbeccf56c2b5f5490ab6Jason Sams    }
176b9077f48e2c5aff1838bbbeccf56c2b5f5490ab6Jason Sams
177cdfdb8f2cdf4668c476cac842212892b2505ff3fJason Sams    ret = rsc->mHal.funcs.script.invokeRoot(rsc, this);
178b9077f48e2c5aff1838bbbeccf56c2b5f5490ab6Jason Sams
179b9077f48e2c5aff1838bbbeccf56c2b5f5490ab6Jason Sams    if (rsc->props.mLogScripts) {
1806598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block        ALOGV("%p ScriptC::run invoking complete, ret=%i", rsc, ret);
181b9077f48e2c5aff1838bbbeccf56c2b5f5490ab6Jason Sams    }
182b9077f48e2c5aff1838bbbeccf56c2b5f5490ab6Jason Sams
183e45ac6e91864f2a6617b6a2c0aa87cdb62cf58fdJason Sams    return ret;
184326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
185326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
186177f8446d58e5b1a4258935371a9450dbe34dca6Jason Sams
187ace3e01f07252ee0fa47fcf1ac48864d8220b90eJason Samsvoid ScriptC::runForEach(Context *rsc,
1884419977d78018a9933c7f455fe001f644f2d638bStephen Hines                         uint32_t slot,
1894b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes                         const Allocation ** ains,
1904b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes                         size_t inLen,
1914b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes                         Allocation * aout,
1924b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes                         const void * usr,
1934b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes                         size_t usrBytes,
1944b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes                         const RsScriptCall *sc) {
195441104d6b802ab14f67af8e8e55570de1fcc9de6Pirama Arumuga Nainar    // Make a copy of RsScriptCall and zero out extra fields that are absent
196441104d6b802ab14f67af8e8e55570de1fcc9de6Pirama Arumuga Nainar    // in API levels below 23.
197441104d6b802ab14f67af8e8e55570de1fcc9de6Pirama Arumuga Nainar    RsScriptCall sc_copy;
198441104d6b802ab14f67af8e8e55570de1fcc9de6Pirama Arumuga Nainar    if (sc != nullptr && getApiLevel() < 23) {
199441104d6b802ab14f67af8e8e55570de1fcc9de6Pirama Arumuga Nainar        memset(&sc_copy, 0, sizeof(sc_copy));
200441104d6b802ab14f67af8e8e55570de1fcc9de6Pirama Arumuga Nainar        memcpy(&sc_copy, sc, 7*4);
201441104d6b802ab14f67af8e8e55570de1fcc9de6Pirama Arumuga Nainar        sc = &sc_copy;
202441104d6b802ab14f67af8e8e55570de1fcc9de6Pirama Arumuga Nainar    }
203441104d6b802ab14f67af8e8e55570de1fcc9de6Pirama Arumuga Nainar
2044b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes    // Trace this function call.
2054b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes    // To avoid overhead we only build the string if tracing is actually
2064b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes    // enabled.
207b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni    String8 *AString = NULL;
208b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni    const char *String = "";
2094b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes    if (ATRACE_ENABLED()) {
210b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        AString = new String8("runForEach_");
211b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        AString->append(mHal.info.exportedForeachFuncList[slot].first);
212b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        String = AString->string();
2134b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes    }
214b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni    ATRACE_NAME(String);
215b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni    (void)String;
2164efe3d38ee1d7b76c93a47ce6218b1f708fd4d9aJason Sams    if (mRSC->hadFatalError()) return;
2174b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes
2184b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes    Context::PushState ps(rsc);
2194b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes
2204b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes    setupGLState(rsc);
2214b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes    setupScript(rsc);
2224b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes
22344bef6fba6244292b751387f3d6c31cca96c28adChris Wailes    if (rsc->mHal.funcs.script.invokeForEachMulti != nullptr) {
224f37121300217d3b39ab66dd9c8881bcbcad932dfChris Wailes        rsc->mHal.funcs.script.invokeForEachMulti(rsc, this, slot, ains, inLen,
225f37121300217d3b39ab66dd9c8881bcbcad932dfChris Wailes                                                  aout, usr, usrBytes, sc);
226f37121300217d3b39ab66dd9c8881bcbcad932dfChris Wailes
227f37121300217d3b39ab66dd9c8881bcbcad932dfChris Wailes    } else if (inLen == 1) {
228f37121300217d3b39ab66dd9c8881bcbcad932dfChris Wailes        rsc->mHal.funcs.script.invokeForEach(rsc, this, slot, ains[0], aout,
229f37121300217d3b39ab66dd9c8881bcbcad932dfChris Wailes                                             usr, usrBytes, sc);
2304b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes
231f37121300217d3b39ab66dd9c8881bcbcad932dfChris Wailes    } else {
232f37121300217d3b39ab66dd9c8881bcbcad932dfChris Wailes        rsc->setError(RS_ERROR_FATAL_DRIVER,
233f37121300217d3b39ab66dd9c8881bcbcad932dfChris Wailes                      "Driver support for multi-input not present");
234f37121300217d3b39ab66dd9c8881bcbcad932dfChris Wailes    }
235f37121300217d3b39ab66dd9c8881bcbcad932dfChris Wailes
236b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni    if (AString) {
237b8353c5943f4038fd7f08db3d958390ce9418798Yang Ni        delete AString;
238f37121300217d3b39ab66dd9c8881bcbcad932dfChris Wailes    }
2394b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes}
2404b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes
241b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchoukvoid ScriptC::Invoke(Context *rsc, uint32_t slot, const void *data, size_t len) {
242fa85e9143c6f6a227c67bab5f08ff86194abdf2aTim Murray    ATRACE_CALL();
243fa85e9143c6f6a227c67bab5f08ff86194abdf2aTim Murray
244bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams    if (slot >= mHal.info.exportedFunctionCount) {
24522fa371bf64b8aae786acfe5d711af21afb13a93Jason Sams        rsc->setError(RS_ERROR_BAD_SCRIPT, "Calling invoke on bad script");
24622fa371bf64b8aae786acfe5d711af21afb13a93Jason Sams        return;
24722fa371bf64b8aae786acfe5d711af21afb13a93Jason Sams    }
2484efe3d38ee1d7b76c93a47ce6218b1f708fd4d9aJason Sams    if (mRSC->hadFatalError()) return;
2494efe3d38ee1d7b76c93a47ce6218b1f708fd4d9aJason Sams
250c61346b91434307c5003029017b54ce9c49112beJason Sams    setupScript(rsc);
25122fa371bf64b8aae786acfe5d711af21afb13a93Jason Sams
252b9077f48e2c5aff1838bbbeccf56c2b5f5490ab6Jason Sams    if (rsc->props.mLogScripts) {
2536598201f1c4f409defac9a5af789fb53a7cc00f8Steve Block        ALOGV("%p ScriptC::Invoke invoking slot %i,  ptr %p", rsc, slot, this);
254b9077f48e2c5aff1838bbbeccf56c2b5f5490ab6Jason Sams    }
255bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams    rsc->mHal.funcs.script.invokeFunction(rsc, this, slot, data, len);
25622fa371bf64b8aae786acfe5d711af21afb13a93Jason Sams}
25722fa371bf64b8aae786acfe5d711af21afb13a93Jason Sams
258f6af3bd80479b98890dbd389001df9096f6fceafStephen Hinesstatic const bool kDebugBitcode = false;
259f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines
260f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines#ifndef RS_COMPATIBILITY_LIB
261f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines#ifndef ANDROID_RS_SERIALIZE
262f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines
263f6af3bd80479b98890dbd389001df9096f6fceafStephen Hinesstatic bool dumpBitcodeFile(const char *cacheDir, const char *resName,
264f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines                            const char *suffix, const uint8_t *bitcode,
265f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines                            size_t bitcodeLen) {
266f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines    std::string f(cacheDir);
267f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines    f.append("/");
268f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines    f.append(resName);
269f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines    f.append("#");
270f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines    f.append(suffix);
271f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines    f.append(".bc");
272f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines
273f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines    if (!ScriptC::createCacheDir(cacheDir)) {
274f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines        return false;
275f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines    }
276f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines
277f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines    FILE *fp = fopen(f.c_str(), "w");
278f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines    if (!fp) {
279f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines        ALOGE("Could not open %s", f.c_str());
280f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines        return false;
281f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines    }
282f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines
283f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines    size_t nWritten = fwrite(bitcode, 1, bitcodeLen, fp);
284f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines    fclose(fp);
285f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines    if (nWritten != bitcodeLen) {
286f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines        ALOGE("Could not write %s", f.c_str());
287f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines        return false;
288f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines    }
289f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines    return true;
290f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines}
291f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines
292f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines#endif  // !ANDROID_RS_SERIALIZE
293f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines#endif  // !RS_COMPATIBILITY_LIB
294f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines
295f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines
296bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Samsbool ScriptC::runCompiler(Context *rsc,
297bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams                          const char *resName,
298bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams                          const char *cacheDir,
299bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams                          const uint8_t *bitcode,
300bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams                          size_t bitcodeLen) {
301fa85e9143c6f6a227c67bab5f08ff86194abdf2aTim Murray    ATRACE_CALL();
302af12ac6a08651464f8d823add667c706f993b587Steve Block    //ALOGE("runCompiler %p %p %p %p %p %i", rsc, this, resName, cacheDir, bitcode, bitcodeLen);
30393eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
304cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines#ifndef ANDROID_RS_SERIALIZE
305f8d4469e2f51740bd7c0e48f6da024a75c4ef372Stephen Hines    uint32_t sdkVersion = 0;
306f8d4469e2f51740bd7c0e48f6da024a75c4ef372Stephen Hines    bcinfo::BitcodeWrapper bcWrapper((const char *)bitcode, bitcodeLen);
307f8d4469e2f51740bd7c0e48f6da024a75c4ef372Stephen Hines    if (!bcWrapper.unwrap()) {
308af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("Bitcode is not in proper container format (raw or wrapper)");
309f8d4469e2f51740bd7c0e48f6da024a75c4ef372Stephen Hines        return false;
310f8d4469e2f51740bd7c0e48f6da024a75c4ef372Stephen Hines    }
311f8d4469e2f51740bd7c0e48f6da024a75c4ef372Stephen Hines
312f8d4469e2f51740bd7c0e48f6da024a75c4ef372Stephen Hines    if (bcWrapper.getBCFileType() == bcinfo::BC_WRAPPER) {
313f8d4469e2f51740bd7c0e48f6da024a75c4ef372Stephen Hines        sdkVersion = bcWrapper.getTargetAPI();
314f8d4469e2f51740bd7c0e48f6da024a75c4ef372Stephen Hines    }
315f8d4469e2f51740bd7c0e48f6da024a75c4ef372Stephen Hines
316f8d4469e2f51740bd7c0e48f6da024a75c4ef372Stephen Hines    if (sdkVersion == 0) {
317f8d4469e2f51740bd7c0e48f6da024a75c4ef372Stephen Hines        // This signals that we didn't have a wrapper containing information
318f8d4469e2f51740bd7c0e48f6da024a75c4ef372Stephen Hines        // about the bitcode.
319f8d4469e2f51740bd7c0e48f6da024a75c4ef372Stephen Hines        sdkVersion = rsc->getTargetSdkVersion();
320f8d4469e2f51740bd7c0e48f6da024a75c4ef372Stephen Hines    }
321f8d4469e2f51740bd7c0e48f6da024a75c4ef372Stephen Hines
3225d95a78ab5758c2625949d78bb67eb3c5d9701ccStephen Hines    // Save off the sdkVersion, so that we can handle broken cases later.
3235d95a78ab5758c2625949d78bb67eb3c5d9701ccStephen Hines    // Bug 19734267
3245d95a78ab5758c2625949d78bb67eb3c5d9701ccStephen Hines    mApiLevel = sdkVersion;
3255d95a78ab5758c2625949d78bb67eb3c5d9701ccStephen Hines
326cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines    if (BT) {
327cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines        delete BT;
328cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines    }
329cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines    BT = new bcinfo::BitcodeTranslator((const char *)bitcode, bitcodeLen,
330cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines                                       sdkVersion);
331cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines    if (!BT->translate()) {
332af12ac6a08651464f8d823add667c706f993b587Steve Block        ALOGE("Failed to translate bitcode from version: %u", sdkVersion);
333cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines        delete BT;
33444bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        BT = nullptr;
335cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines        return false;
336cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines    }
337cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines    bitcode = (const uint8_t *) BT->getTranslatedBitcode();
338cbb0b8aceedb9146ba901cba1fbd6d1e9ca88f51Stephen Hines    bitcodeLen = BT->getTranslatedBitcodeSize();
3391d54f10f3c23e0d7ec57e52ec3b0701a2a5ed24eJason Sams
340f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines    if (kDebugBitcode) {
341f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines        if (!dumpBitcodeFile(cacheDir, resName, "after", bitcode, bitcodeLen)) {
342f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines            return false;
343f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines        }
344f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines    }
345f6af3bd80479b98890dbd389001df9096f6fceafStephen Hines
346e195a3f57ace3b66d313a6ee88c6e93d5c9d87f4Tim Murray#endif
34784bf2b877024aaa154b66e0f2338d54bdabd855aTim Murray    if (!cacheDir) {
34884bf2b877024aaa154b66e0f2338d54bdabd855aTim Murray        // MUST BE FIXED BEFORE ANYTHING USING C++ API IS RELEASED
34984bf2b877024aaa154b66e0f2338d54bdabd855aTim Murray        cacheDir = getenv("EXTERNAL_STORAGE");
35084bf2b877024aaa154b66e0f2338d54bdabd855aTim Murray        ALOGV("Cache dir changed to %s", cacheDir);
35184bf2b877024aaa154b66e0f2338d54bdabd855aTim Murray    }
35284bf2b877024aaa154b66e0f2338d54bdabd855aTim Murray
353e78c14bd277e60d5619516e4088a04572a5b250fTim Murray    // ensure that cache dir exists
35484bf2b877024aaa154b66e0f2338d54bdabd855aTim Murray    if (cacheDir && !createCacheDir(cacheDir)) {
355e78c14bd277e60d5619516e4088a04572a5b250fTim Murray      return false;
356e78c14bd277e60d5619516e4088a04572a5b250fTim Murray    }
35793eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
358e78c14bd277e60d5619516e4088a04572a5b250fTim Murray
35977020c5248ca0921d7d24fb60dec5105ba39026cJason Sams    if (!rsc->mHal.funcs.script.init(rsc, this, resName, cacheDir, bitcode, bitcodeLen, 0)) {
36077020c5248ca0921d7d24fb60dec5105ba39026cJason Sams        return false;
36177020c5248ca0921d7d24fb60dec5105ba39026cJason Sams    }
3628c88090e8cccab103eebe2ff569e116e9f5fb208Jason Sams
36377020c5248ca0921d7d24fb60dec5105ba39026cJason Sams    mInitialized = true;
36493eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
365bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams    mEnviroment.mFragment.set(rsc->getDefaultProgramFragment());
366bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams    mEnviroment.mVertex.set(rsc->getDefaultProgramVertex());
367bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams    mEnviroment.mFragmentStore.set(rsc->getDefaultProgramStore());
368bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams    mEnviroment.mRaster.set(rsc->getDefaultProgramRaster());
36993eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
3701d54f10f3c23e0d7ec57e52ec3b0701a2a5ed24eJason Sams
371bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams    rsc->mHal.funcs.script.invokeInit(rsc, this);
372a4a54e42fc710a62b47cbcb9d64c34a190429d9eJason Sams
373bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams    for (size_t i=0; i < mHal.info.exportedPragmaCount; ++i) {
374bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams        const char * key = mHal.info.exportedPragmaKeyList[i];
375bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams        const char * value = mHal.info.exportedPragmaValueList[i];
376af12ac6a08651464f8d823add667c706f993b587Steve Block        //ALOGE("pragma %s %s", keys[i], values[i]);
377bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams        if (!strcmp(key, "version")) {
378bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams            if (!strcmp(value, "1")) {
379b5dc6af59c717fd871874930ac8e7b2b0f26dae2Stephen Hines                continue;
380b5dc6af59c717fd871874930ac8e7b2b0f26dae2Stephen Hines            }
381af12ac6a08651464f8d823add667c706f993b587Steve Block            ALOGE("Invalid version pragma value: %s\n", value);
38226b2c9f5b0d77b69fb5edf96dab7d57f1de1d594Jason Sams            return false;
3837b337b1ce7e8861a254d7d06c2d0a26bced9b52bStephen Hines        }
3847b337b1ce7e8861a254d7d06c2d0a26bced9b52bStephen Hines
38593eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#ifndef RS_COMPATIBILITY_LIB
386bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams        if (!strcmp(key, "stateVertex")) {
387bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams            if (!strcmp(value, "default")) {
3881030893d9b99b72468034da13df025bda479bb97Jason Sams                continue;
3891030893d9b99b72468034da13df025bda479bb97Jason Sams            }
390bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams            if (!strcmp(value, "parent")) {
391bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams                mEnviroment.mVertex.clear();
3927b337b1ce7e8861a254d7d06c2d0a26bced9b52bStephen Hines                continue;
3931030893d9b99b72468034da13df025bda479bb97Jason Sams            }
394af12ac6a08651464f8d823add667c706f993b587Steve Block            ALOGE("Unrecognized value %s passed to stateVertex", value);
39526b2c9f5b0d77b69fb5edf96dab7d57f1de1d594Jason Sams            return false;
3967b337b1ce7e8861a254d7d06c2d0a26bced9b52bStephen Hines        }
3971030893d9b99b72468034da13df025bda479bb97Jason Sams
398bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams        if (!strcmp(key, "stateRaster")) {
399bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams            if (!strcmp(value, "default")) {
4007b337b1ce7e8861a254d7d06c2d0a26bced9b52bStephen Hines                continue;
4011030893d9b99b72468034da13df025bda479bb97Jason Sams            }
402bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams            if (!strcmp(value, "parent")) {
403bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams                mEnviroment.mRaster.clear();
4047b337b1ce7e8861a254d7d06c2d0a26bced9b52bStephen Hines                continue;
4057b337b1ce7e8861a254d7d06c2d0a26bced9b52bStephen Hines            }
406af12ac6a08651464f8d823add667c706f993b587Steve Block            ALOGE("Unrecognized value %s passed to stateRaster", value);
40726b2c9f5b0d77b69fb5edf96dab7d57f1de1d594Jason Sams            return false;
4087b337b1ce7e8861a254d7d06c2d0a26bced9b52bStephen Hines        }
4091030893d9b99b72468034da13df025bda479bb97Jason Sams
410bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams        if (!strcmp(key, "stateFragment")) {
411bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams            if (!strcmp(value, "default")) {
4127b337b1ce7e8861a254d7d06c2d0a26bced9b52bStephen Hines                continue;
4137b337b1ce7e8861a254d7d06c2d0a26bced9b52bStephen Hines            }
414bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams            if (!strcmp(value, "parent")) {
415bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams                mEnviroment.mFragment.clear();
4167b337b1ce7e8861a254d7d06c2d0a26bced9b52bStephen Hines                continue;
4171030893d9b99b72468034da13df025bda479bb97Jason Sams            }
418af12ac6a08651464f8d823add667c706f993b587Steve Block            ALOGE("Unrecognized value %s passed to stateFragment", value);
41926b2c9f5b0d77b69fb5edf96dab7d57f1de1d594Jason Sams            return false;
4207b337b1ce7e8861a254d7d06c2d0a26bced9b52bStephen Hines        }
4211030893d9b99b72468034da13df025bda479bb97Jason Sams
422bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams        if (!strcmp(key, "stateStore")) {
423bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams            if (!strcmp(value, "default")) {
4247b337b1ce7e8861a254d7d06c2d0a26bced9b52bStephen Hines                continue;
4251030893d9b99b72468034da13df025bda479bb97Jason Sams            }
426bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams            if (!strcmp(value, "parent")) {
427bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams                mEnviroment.mFragmentStore.clear();
4287b337b1ce7e8861a254d7d06c2d0a26bced9b52bStephen Hines                continue;
4297b337b1ce7e8861a254d7d06c2d0a26bced9b52bStephen Hines            }
430af12ac6a08651464f8d823add667c706f993b587Steve Block            ALOGE("Unrecognized value %s passed to stateStore", value);
43126b2c9f5b0d77b69fb5edf96dab7d57f1de1d594Jason Sams            return false;
4321030893d9b99b72468034da13df025bda479bb97Jason Sams        }
43393eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams#endif
43493eacc7ce0aad4314b4cb41a281f59ce54bb3286Jason Sams
4351030893d9b99b72468034da13df025bda479bb97Jason Sams    }
4362e8665de7c0eb4514c67baf8693d61c892e5303dJason Sams
437bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams    mSlots = new ObjectBaseRef<Allocation>[mHal.info.exportedVariableCount];
438bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams    mTypes = new ObjectBaseRef<const Type>[mHal.info.exportedVariableCount];
4392e8665de7c0eb4514c67baf8693d61c892e5303dJason Sams
44026b2c9f5b0d77b69fb5edf96dab7d57f1de1d594Jason Sams    return true;
44157b79ceb1126e3797fa42367b97dd7bcfcda1ed9Joe Onorato}
4421030893d9b99b72468034da13df025bda479bb97Jason Sams
443326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsnamespace android {
444326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsnamespace renderscript {
445326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
446ce8a079bd4d296f9f1a24d7a5808d57f71dfc2e4Shih-wei LiaoRsScript rsi_ScriptCCreate(Context *rsc,
44770b83c111beceaf8fbb700580833e7fec99272cfAlex Sakhartchouk                           const char *resName, size_t resName_length,
44870b83c111beceaf8fbb700580833e7fec99272cfAlex Sakhartchouk                           const char *cacheDir, size_t cacheDir_length,
449b81a0eb8180791e4eaab1253b59fa8bd562b046bAlex Sakhartchouk                           const char *text, size_t text_length)
4509503b66e5329703535f7cc71a0755e6f3b1e39a6Shih-wei Liao{
451249d453869cca82dcb57123f6d1c8df3ca9f4372Jason Sams    ScriptC *s = new ScriptC(rsc);
4521f52633fefd1862451e6a30209d590680d02beecJason Sams
45370b83c111beceaf8fbb700580833e7fec99272cfAlex Sakhartchouk    if (!s->runCompiler(rsc, resName, cacheDir, (uint8_t *)text, text_length)) {
45426b2c9f5b0d77b69fb5edf96dab7d57f1de1d594Jason Sams        // Error during compile, destroy s and return null.
4554769d68bba1f2af113197ecd6d92ecdae090a6d7Stephen Hines        ObjectBase::checkDelete(s);
45644bef6fba6244292b751387f3d6c31cca96c28adChris Wailes        return nullptr;
45726b2c9f5b0d77b69fb5edf96dab7d57f1de1d594Jason Sams    }
458bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams
459bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams    s->incUserRef();
460249d453869cca82dcb57123f6d1c8df3ca9f4372Jason Sams    return s;
461326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
462326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
463326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
464326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
465