rsCpuCore.cpp revision 4b2bea3dc20865f3a198797702e19912a6a2171c
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#include "rsCpuCore.h"
18#include "rsCpuScript.h"
19#include "rsCpuScriptGroup.h"
20
21#include <malloc.h>
22#include "rsContext.h"
23
24#include <sys/types.h>
25#include <sys/resource.h>
26#include <sched.h>
27#include <sys/syscall.h>
28#include <string.h>
29#include <unistd.h>
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <fcntl.h>
34
35#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
36#include <cutils/properties.h>
37#include "utils/StopWatch.h"
38#endif
39
40#ifdef RS_SERVER
41// Android exposes gettid(), standard Linux does not
42static pid_t gettid() {
43    return syscall(SYS_gettid);
44}
45#endif
46
47using namespace android;
48using namespace android::renderscript;
49
50typedef void (*outer_foreach_t)(
51    const android::renderscript::RsExpandKernelParams *,
52    uint32_t x1, uint32_t x2,
53    uint32_t instep, uint32_t outstep);
54
55
56static pthread_key_t gThreadTLSKey = 0;
57static uint32_t gThreadTLSKeyCount = 0;
58static pthread_mutex_t gInitMutex = PTHREAD_MUTEX_INITIALIZER;
59
60bool android::renderscript::gArchUseSIMD = false;
61
62RsdCpuReference::~RsdCpuReference() {
63}
64
65RsdCpuReference * RsdCpuReference::create(Context *rsc, uint32_t version_major,
66        uint32_t version_minor, sym_lookup_t lfn, script_lookup_t slfn
67#ifndef RS_COMPATIBILITY_LIB
68        , bcc::RSLinkRuntimeCallback pLinkRuntimeCallback,
69        RSSelectRTCallback pSelectRTCallback,
70        const char *pBccPluginName
71#endif
72        ) {
73
74    RsdCpuReferenceImpl *cpu = new RsdCpuReferenceImpl(rsc);
75    if (!cpu) {
76        return NULL;
77    }
78    if (!cpu->init(version_major, version_minor, lfn, slfn)) {
79        delete cpu;
80        return NULL;
81    }
82
83#ifndef RS_COMPATIBILITY_LIB
84    cpu->setLinkRuntimeCallback(pLinkRuntimeCallback);
85    cpu->setSelectRTCallback(pSelectRTCallback);
86    if (pBccPluginName) {
87        cpu->setBccPluginName(pBccPluginName);
88    }
89#endif
90
91    return cpu;
92}
93
94
95Context * RsdCpuReference::getTlsContext() {
96    ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(gThreadTLSKey);
97    return tls->mContext;
98}
99
100const Script * RsdCpuReference::getTlsScript() {
101    ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(gThreadTLSKey);
102    return tls->mScript;
103}
104
105pthread_key_t RsdCpuReference::getThreadTLSKey(){ return gThreadTLSKey; }
106
107////////////////////////////////////////////////////////////
108///
109
110RsdCpuReferenceImpl::RsdCpuReferenceImpl(Context *rsc) {
111    mRSC = rsc;
112
113    version_major = 0;
114    version_minor = 0;
115    mInForEach = false;
116    memset(&mWorkers, 0, sizeof(mWorkers));
117    memset(&mTlsStruct, 0, sizeof(mTlsStruct));
118    mExit = false;
119#ifndef RS_COMPATIBILITY_LIB
120    mLinkRuntimeCallback = NULL;
121    mSelectRTCallback = NULL;
122    mSetupCompilerCallback = NULL;
123#endif
124}
125
126
127void * RsdCpuReferenceImpl::helperThreadProc(void *vrsc) {
128    RsdCpuReferenceImpl *dc = (RsdCpuReferenceImpl *)vrsc;
129
130    uint32_t idx = __sync_fetch_and_add(&dc->mWorkers.mLaunchCount, 1);
131
132    //ALOGV("RS helperThread starting %p idx=%i", dc, idx);
133
134    dc->mWorkers.mLaunchSignals[idx].init();
135    dc->mWorkers.mNativeThreadId[idx] = gettid();
136
137    memset(&dc->mTlsStruct, 0, sizeof(dc->mTlsStruct));
138    int status = pthread_setspecific(gThreadTLSKey, &dc->mTlsStruct);
139    if (status) {
140        ALOGE("pthread_setspecific %i", status);
141    }
142
143#if 0
144    typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
145    cpu_set_t cpuset;
146    memset(&cpuset, 0, sizeof(cpuset));
147    cpuset.bits[idx / 64] |= 1ULL << (idx % 64);
148    int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx],
149              sizeof(cpuset), &cpuset);
150    ALOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
151#endif
152
153    while (!dc->mExit) {
154        dc->mWorkers.mLaunchSignals[idx].wait();
155        if (dc->mWorkers.mLaunchCallback) {
156           // idx +1 is used because the calling thread is always worker 0.
157           dc->mWorkers.mLaunchCallback(dc->mWorkers.mLaunchData, idx+1);
158        }
159        __sync_fetch_and_sub(&dc->mWorkers.mRunningCount, 1);
160        dc->mWorkers.mCompleteSignal.set();
161    }
162
163    //ALOGV("RS helperThread exited %p idx=%i", dc, idx);
164    return NULL;
165}
166
167void RsdCpuReferenceImpl::launchThreads(WorkerCallback_t cbk, void *data) {
168    mWorkers.mLaunchData = data;
169    mWorkers.mLaunchCallback = cbk;
170
171    // fast path for very small launches
172    MTLaunchStruct *mtls = (MTLaunchStruct *)data;
173    if (mtls && mtls->fep.dimY <= 1 && mtls->xEnd <= mtls->xStart + mtls->mSliceSize) {
174        if (mWorkers.mLaunchCallback) {
175            mWorkers.mLaunchCallback(mWorkers.mLaunchData, 0);
176        }
177        return;
178    }
179
180    mWorkers.mRunningCount = mWorkers.mCount;
181    __sync_synchronize();
182
183    for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
184        mWorkers.mLaunchSignals[ct].set();
185    }
186
187    // We use the calling thread as one of the workers so we can start without
188    // the delay of the thread wakeup.
189    if (mWorkers.mLaunchCallback) {
190        mWorkers.mLaunchCallback(mWorkers.mLaunchData, 0);
191    }
192
193    while (__sync_fetch_and_or(&mWorkers.mRunningCount, 0) != 0) {
194        mWorkers.mCompleteSignal.wait();
195    }
196}
197
198
199void RsdCpuReferenceImpl::lockMutex() {
200    pthread_mutex_lock(&gInitMutex);
201}
202
203void RsdCpuReferenceImpl::unlockMutex() {
204    pthread_mutex_unlock(&gInitMutex);
205}
206
207static int
208read_file(const char*  pathname, char*  buffer, size_t  buffsize)
209{
210    int  fd, len;
211
212    fd = open(pathname, O_RDONLY);
213    if (fd < 0)
214        return -1;
215
216    do {
217        len = read(fd, buffer, buffsize);
218    } while (len < 0 && errno == EINTR);
219
220    close(fd);
221
222    return len;
223}
224
225static void GetCpuInfo() {
226    char cpuinfo[4096];
227    int  cpuinfo_len;
228
229    cpuinfo_len = read_file("/proc/cpuinfo", cpuinfo, sizeof cpuinfo);
230    if (cpuinfo_len < 0)  /* should not happen */ {
231        return;
232    }
233
234#if defined(ARCH_ARM_HAVE_VFP) || defined(ARCH_ARM_USE_INTRINSICS)
235    gArchUseSIMD = (!!strstr(cpuinfo, " neon")) ||
236                   (!!strstr(cpuinfo, " asimd"));
237#elif defined(ARCH_X86_HAVE_SSSE3)
238    gArchUseSIMD = !!strstr(cpuinfo, " ssse3");
239#endif
240}
241
242bool RsdCpuReferenceImpl::init(uint32_t version_major, uint32_t version_minor,
243                               sym_lookup_t lfn, script_lookup_t slfn) {
244
245    mSymLookupFn = lfn;
246    mScriptLookupFn = slfn;
247
248    lockMutex();
249    if (!gThreadTLSKeyCount) {
250        int status = pthread_key_create(&gThreadTLSKey, NULL);
251        if (status) {
252            ALOGE("Failed to init thread tls key.");
253            unlockMutex();
254            return false;
255        }
256    }
257    gThreadTLSKeyCount++;
258    unlockMutex();
259
260    mTlsStruct.mContext = mRSC;
261    mTlsStruct.mScript = NULL;
262    int status = pthread_setspecific(gThreadTLSKey, &mTlsStruct);
263    if (status) {
264        ALOGE("pthread_setspecific %i", status);
265    }
266
267    GetCpuInfo();
268
269    int cpu = sysconf(_SC_NPROCESSORS_ONLN);
270    if(mRSC->props.mDebugMaxThreads) {
271        cpu = mRSC->props.mDebugMaxThreads;
272    }
273    if (cpu < 2) {
274        mWorkers.mCount = 0;
275        return true;
276    }
277
278    // Subtract one from the cpu count because we also use the command thread as a worker.
279    mWorkers.mCount = (uint32_t)(cpu - 1);
280
281    ALOGV("%p Launching thread(s), CPUs %i", mRSC, mWorkers.mCount + 1);
282
283    mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
284    mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
285    mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
286    mWorkers.mLaunchCallback = NULL;
287
288    mWorkers.mCompleteSignal.init();
289
290    mWorkers.mRunningCount = mWorkers.mCount;
291    mWorkers.mLaunchCount = 0;
292    __sync_synchronize();
293
294    pthread_attr_t threadAttr;
295    status = pthread_attr_init(&threadAttr);
296    if (status) {
297        ALOGE("Failed to init thread attribute.");
298        return false;
299    }
300
301    for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
302        status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
303        if (status) {
304            mWorkers.mCount = ct;
305            ALOGE("Created fewer than expected number of RS threads.");
306            break;
307        }
308    }
309    while (__sync_fetch_and_or(&mWorkers.mRunningCount, 0) != 0) {
310        usleep(100);
311    }
312
313    pthread_attr_destroy(&threadAttr);
314    return true;
315}
316
317
318void RsdCpuReferenceImpl::setPriority(int32_t priority) {
319    for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
320        setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], priority);
321    }
322}
323
324RsdCpuReferenceImpl::~RsdCpuReferenceImpl() {
325    mExit = true;
326    mWorkers.mLaunchData = NULL;
327    mWorkers.mLaunchCallback = NULL;
328    mWorkers.mRunningCount = mWorkers.mCount;
329    __sync_synchronize();
330    for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
331        mWorkers.mLaunchSignals[ct].set();
332    }
333    void *res;
334    for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
335        pthread_join(mWorkers.mThreadId[ct], &res);
336    }
337    rsAssert(__sync_fetch_and_or(&mWorkers.mRunningCount, 0) == 0);
338    free(mWorkers.mThreadId);
339    free(mWorkers.mNativeThreadId);
340    delete[] mWorkers.mLaunchSignals;
341
342    // Global structure cleanup.
343    lockMutex();
344    --gThreadTLSKeyCount;
345    if (!gThreadTLSKeyCount) {
346        pthread_key_delete(gThreadTLSKey);
347    }
348    unlockMutex();
349
350}
351
352typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
353
354static void wc_xy(void *usr, uint32_t idx) {
355    MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
356
357    RsExpandKernelParams kparams;
358    kparams.takeFields(mtls->fep);
359
360    // Used by CpuScriptGroup, IntrinsicBlur, and IntrinsicHistogram
361    kparams.lid = idx;
362
363    outer_foreach_t fn = (outer_foreach_t) mtls->kernel;
364    while (1) {
365        uint32_t slice  = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1);
366        uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize;
367        uint32_t yEnd   = yStart + mtls->mSliceSize;
368
369        yEnd = rsMin(yEnd, mtls->yEnd);
370
371        if (yEnd <= yStart) {
372            return;
373        }
374
375        //ALOGE("usr idx %i, x %i,%i  y %i,%i", idx, mtls->xStart, mtls->xEnd, yStart, yEnd);
376        //ALOGE("usr ptr in %p,  out %p", mtls->fep.ptrIn, mtls->fep.ptrOut);
377
378        for (kparams.y = yStart; kparams.y < yEnd; kparams.y++) {
379            kparams.out = mtls->fep.ptrOut +
380                          (mtls->fep.yStrideOut * kparams.y) +
381                          (mtls->fep.eStrideOut * mtls->xStart);
382
383            kparams.in = mtls->fep.ptrIn +
384                         (mtls->fep.yStrideIn * kparams.y) +
385                         (mtls->fep.eStrideIn * mtls->xStart);
386
387
388            fn(&kparams, mtls->xStart, mtls->xEnd, mtls->fep.eStrideIn,
389               mtls->fep.eStrideOut);
390        }
391    }
392}
393
394static void wc_x(void *usr, uint32_t idx) {
395    MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
396
397    RsExpandKernelParams kparams;
398    kparams.takeFields(mtls->fep);
399
400    // Used by CpuScriptGroup, IntrinsicBlur, and IntrisicHistogram
401    kparams.lid = idx;
402
403    outer_foreach_t fn = (outer_foreach_t) mtls->kernel;
404    while (1) {
405        uint32_t slice  = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1);
406        uint32_t xStart = mtls->xStart + slice * mtls->mSliceSize;
407        uint32_t xEnd   = xStart + mtls->mSliceSize;
408
409        xEnd = rsMin(xEnd, mtls->xEnd);
410
411        if (xEnd <= xStart) {
412            return;
413        }
414
415        //ALOGE("usr slice %i idx %i, x %i,%i", slice, idx, xStart, xEnd);
416        //ALOGE("usr ptr in %p,  out %p", mtls->fep.ptrIn, mtls->fep.ptrOut);
417
418        kparams.out = mtls->fep.ptrOut + (mtls->fep.eStrideOut * xStart);
419        kparams.in  = mtls->fep.ptrIn  + (mtls->fep.eStrideIn  * xStart);
420
421        fn(&kparams, xStart, xEnd, mtls->fep.eStrideIn, mtls->fep.eStrideOut);
422    }
423}
424
425void RsdCpuReferenceImpl::launchThreads(const Allocation * ain, Allocation * aout,
426                                        const RsScriptCall *sc, MTLaunchStruct *mtls) {
427
428    //android::StopWatch kernel_time("kernel time");
429
430    if ((mWorkers.mCount >= 1) && mtls->isThreadable && !mInForEach) {
431        const size_t targetByteChunk = 16 * 1024;
432        mInForEach = true;
433        if (mtls->fep.dimY > 1) {
434            uint32_t s1 = mtls->fep.dimY / ((mWorkers.mCount + 1) * 4);
435            uint32_t s2 = 0;
436
437            // This chooses our slice size to rate limit atomic ops to
438            // one per 16k bytes of reads/writes.
439            if (mtls->fep.yStrideOut) {
440                s2 = targetByteChunk / mtls->fep.yStrideOut;
441            } else {
442                s2 = targetByteChunk / mtls->fep.yStrideIn;
443            }
444            mtls->mSliceSize = rsMin(s1, s2);
445
446            if(mtls->mSliceSize < 1) {
447                mtls->mSliceSize = 1;
448            }
449
450         //   mtls->mSliceSize = 2;
451            launchThreads(wc_xy, mtls);
452        } else {
453            uint32_t s1 = mtls->fep.dimX / ((mWorkers.mCount + 1) * 4);
454            uint32_t s2 = 0;
455
456            // This chooses our slice size to rate limit atomic ops to
457            // one per 16k bytes of reads/writes.
458            if (mtls->fep.eStrideOut) {
459                s2 = targetByteChunk / mtls->fep.eStrideOut;
460            } else {
461                s2 = targetByteChunk / mtls->fep.eStrideIn;
462            }
463            mtls->mSliceSize = rsMin(s1, s2);
464
465            if(mtls->mSliceSize < 1) {
466                mtls->mSliceSize = 1;
467            }
468
469            launchThreads(wc_x, mtls);
470        }
471        mInForEach = false;
472
473        //ALOGE("launch 1");
474    } else {
475        RsExpandKernelParams kparams;
476        kparams.takeFields(mtls->fep);
477
478        //ALOGE("launch 3");
479        outer_foreach_t fn = (outer_foreach_t) mtls->kernel;
480        for (uint32_t arrayIndex = mtls->arrayStart;
481             arrayIndex < mtls->arrayEnd; arrayIndex++) {
482
483            for (kparams.z = mtls->zStart; kparams.z < mtls->zEnd;
484                 kparams.z++) {
485
486                for (kparams.y = mtls->yStart; kparams.y < mtls->yEnd;
487                     kparams.y++) {
488
489                    uint32_t offset =
490                      kparams.dimY * kparams.dimZ * arrayIndex +
491                      kparams.dimY * kparams.z + kparams.y;
492
493                    kparams.out = mtls->fep.ptrOut +
494                                  (mtls->fep.yStrideOut * offset) +
495                                  (mtls->fep.eStrideOut * mtls->xStart);
496
497                    kparams.in = mtls->fep.ptrIn +
498                                 (mtls->fep.yStrideIn * offset) +
499                                 (mtls->fep.eStrideIn * mtls->xStart);
500
501                    fn(&kparams, mtls->xStart, mtls->xEnd, mtls->fep.eStrideIn,
502                       mtls->fep.eStrideOut);
503                }
504            }
505        }
506    }
507}
508
509void RsdCpuReferenceImpl::launchThreads(const Allocation** ains, uint32_t inLen, Allocation* aout,
510                                        const RsScriptCall* sc, MTLaunchStruct* mtls) {
511
512    //android::StopWatch kernel_time("kernel time");
513
514    if ((mWorkers.mCount >= 1) && mtls->isThreadable && !mInForEach) {
515        const size_t targetByteChunk = 16 * 1024;
516        mInForEach = true;
517        if (mtls->fep.dimY > 1) {
518            uint32_t s1 = mtls->fep.dimY / ((mWorkers.mCount + 1) * 4);
519            uint32_t s2 = 0;
520
521            // This chooses our slice size to rate limit atomic ops to
522            // one per 16k bytes of reads/writes.
523            if (mtls->fep.yStrideOut) {
524                s2 = targetByteChunk / mtls->fep.yStrideOut;
525            } else {
526                s2 = targetByteChunk / mtls->fep.yStrideIn;
527            }
528            mtls->mSliceSize = rsMin(s1, s2);
529
530            if(mtls->mSliceSize < 1) {
531                mtls->mSliceSize = 1;
532            }
533
534         //   mtls->mSliceSize = 2;
535            launchThreads(wc_xy, mtls);
536        } else {
537            uint32_t s1 = mtls->fep.dimX / ((mWorkers.mCount + 1) * 4);
538            uint32_t s2 = 0;
539
540            // This chooses our slice size to rate limit atomic ops to
541            // one per 16k bytes of reads/writes.
542            if (mtls->fep.eStrideOut) {
543                s2 = targetByteChunk / mtls->fep.eStrideOut;
544            } else {
545                s2 = targetByteChunk / mtls->fep.eStrideIn;
546            }
547            mtls->mSliceSize = rsMin(s1, s2);
548
549            if (mtls->mSliceSize < 1) {
550                mtls->mSliceSize = 1;
551            }
552
553            launchThreads(wc_x, mtls);
554        }
555        mInForEach = false;
556
557        //ALOGE("launch 1");
558    } else {
559        RsExpandKernelParams kparams;
560        kparams.takeFields(mtls->fep);
561
562        // Allocate space for our input base pointers.
563        kparams.ins = new const void*[inLen];
564
565        // Allocate space for our input stride information.
566        kparams.eStrideIns = new uint32_t[inLen];
567
568        // Fill our stride information.
569        for (int inIndex = inLen; --inIndex >= 0;) {
570          kparams.eStrideIns[inIndex] = mtls->fep.inStrides[inIndex].eStride;
571        }
572
573        //ALOGE("launch 3");
574        outer_foreach_t fn = (outer_foreach_t) mtls->kernel;
575        for (uint32_t arrayIndex = mtls->arrayStart;
576             arrayIndex < mtls->arrayEnd; arrayIndex++) {
577
578            for (kparams.z = mtls->zStart; kparams.z < mtls->zEnd;
579                 kparams.z++) {
580
581                for (kparams.y = mtls->yStart; kparams.y < mtls->yEnd;
582                     kparams.y++) {
583
584                    uint32_t offset =
585                      mtls->fep.dimY * mtls->fep.dimZ * arrayIndex +
586                      mtls->fep.dimY * kparams.z + kparams.y;
587
588                    kparams.out = mtls->fep.ptrOut +
589                                  (mtls->fep.yStrideOut * offset) +
590                                  (mtls->fep.eStrideOut * mtls->xStart);
591
592                    for (int inIndex = inLen; --inIndex >= 0;) {
593                        StridePair &strides = mtls->fep.inStrides[inIndex];
594
595                        kparams.ins[inIndex] =
596                          mtls->fep.ptrIns[inIndex] +
597                          (strides.yStride * offset) +
598                          (strides.eStride * mtls->xStart);
599                    }
600
601                    /*
602                     * The fourth argument is zero here because multi-input
603                     * kernels get their stride information from a member of p
604                     * that points to an array.
605                     */
606                    fn(&kparams, mtls->xStart, mtls->xEnd, 0,
607                       mtls->fep.eStrideOut);
608                }
609            }
610        }
611
612        // Free our arrays.
613        delete[] kparams.ins;
614        delete[] kparams.eStrideIns;
615    }
616}
617
618RsdCpuScriptImpl * RsdCpuReferenceImpl::setTLS(RsdCpuScriptImpl *sc) {
619    //ALOGE("setTls %p", sc);
620    ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(gThreadTLSKey);
621    rsAssert(tls);
622    RsdCpuScriptImpl *old = tls->mImpl;
623    tls->mImpl = sc;
624    tls->mContext = mRSC;
625    if (sc) {
626        tls->mScript = sc->getScript();
627    } else {
628        tls->mScript = NULL;
629    }
630    return old;
631}
632
633const RsdCpuReference::CpuSymbol * RsdCpuReferenceImpl::symLookup(const char *name) {
634    return mSymLookupFn(mRSC, name);
635}
636
637
638RsdCpuReference::CpuScript * RsdCpuReferenceImpl::createScript(const ScriptC *s,
639                                    char const *resName, char const *cacheDir,
640                                    uint8_t const *bitcode, size_t bitcodeSize,
641                                    uint32_t flags) {
642
643    RsdCpuScriptImpl *i = new RsdCpuScriptImpl(this, s);
644    if (!i->init(resName, cacheDir, bitcode, bitcodeSize, flags
645#ifndef RS_COMPATIBILITY_LIB
646        , getBccPluginName()
647#endif
648        )) {
649        delete i;
650        return NULL;
651    }
652    return i;
653}
654
655extern RsdCpuScriptImpl * rsdIntrinsic_3DLUT(RsdCpuReferenceImpl *ctx,
656                                             const Script *s, const Element *e);
657extern RsdCpuScriptImpl * rsdIntrinsic_Convolve3x3(RsdCpuReferenceImpl *ctx,
658                                                   const Script *s, const Element *e);
659extern RsdCpuScriptImpl * rsdIntrinsic_ColorMatrix(RsdCpuReferenceImpl *ctx,
660                                                   const Script *s, const Element *e);
661extern RsdCpuScriptImpl * rsdIntrinsic_LUT(RsdCpuReferenceImpl *ctx,
662                                           const Script *s, const Element *e);
663extern RsdCpuScriptImpl * rsdIntrinsic_Convolve5x5(RsdCpuReferenceImpl *ctx,
664                                                   const Script *s, const Element *e);
665extern RsdCpuScriptImpl * rsdIntrinsic_Blur(RsdCpuReferenceImpl *ctx,
666                                            const Script *s, const Element *e);
667extern RsdCpuScriptImpl * rsdIntrinsic_YuvToRGB(RsdCpuReferenceImpl *ctx,
668                                                const Script *s, const Element *e);
669extern RsdCpuScriptImpl * rsdIntrinsic_Blend(RsdCpuReferenceImpl *ctx,
670                                             const Script *s, const Element *e);
671extern RsdCpuScriptImpl * rsdIntrinsic_Histogram(RsdCpuReferenceImpl *ctx,
672                                                 const Script *s, const Element *e);
673extern RsdCpuScriptImpl * rsdIntrinsic_LoopFilter(RsdCpuReferenceImpl *ctx,
674                                                  const Script *s, const Element *e);
675extern RsdCpuScriptImpl * rsdIntrinsic_Resize(RsdCpuReferenceImpl *ctx,
676                                              const Script *s, const Element *e);
677
678RsdCpuReference::CpuScript * RsdCpuReferenceImpl::createIntrinsic(const Script *s,
679                                    RsScriptIntrinsicID iid, Element *e) {
680
681    RsdCpuScriptImpl *i = NULL;
682    switch (iid) {
683    case RS_SCRIPT_INTRINSIC_ID_3DLUT:
684        i = rsdIntrinsic_3DLUT(this, s, e);
685        break;
686    case RS_SCRIPT_INTRINSIC_ID_CONVOLVE_3x3:
687        i = rsdIntrinsic_Convolve3x3(this, s, e);
688        break;
689    case RS_SCRIPT_INTRINSIC_ID_COLOR_MATRIX:
690        i = rsdIntrinsic_ColorMatrix(this, s, e);
691        break;
692    case RS_SCRIPT_INTRINSIC_ID_LUT:
693        i = rsdIntrinsic_LUT(this, s, e);
694        break;
695    case RS_SCRIPT_INTRINSIC_ID_CONVOLVE_5x5:
696        i = rsdIntrinsic_Convolve5x5(this, s, e);
697        break;
698    case RS_SCRIPT_INTRINSIC_ID_BLUR:
699        i = rsdIntrinsic_Blur(this, s, e);
700        break;
701    case RS_SCRIPT_INTRINSIC_ID_YUV_TO_RGB:
702        i = rsdIntrinsic_YuvToRGB(this, s, e);
703        break;
704    case RS_SCRIPT_INTRINSIC_ID_BLEND:
705        i = rsdIntrinsic_Blend(this, s, e);
706        break;
707    case RS_SCRIPT_INTRINSIC_ID_HISTOGRAM:
708        i = rsdIntrinsic_Histogram(this, s, e);
709        break;
710    case RS_SCRIPT_INTRINSIC_ID_RESIZE:
711        i = rsdIntrinsic_Resize(this, s, e);
712        break;
713#ifndef RS_COMPATIBILITY_LIB
714    case RS_SCRIPT_INTRINSIC_ID_LOOP_FILTER:
715        i = rsdIntrinsic_LoopFilter(this, s, e);
716        break;
717#endif
718
719    default:
720        rsAssert(0);
721    }
722
723    return i;
724}
725
726RsdCpuReference::CpuScriptGroup * RsdCpuReferenceImpl::createScriptGroup(const ScriptGroup *sg) {
727    CpuScriptGroupImpl *sgi = new CpuScriptGroupImpl(this, sg);
728    if (!sgi->init()) {
729        delete sgi;
730        return NULL;
731    }
732    return sgi;
733}
734