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