rsCpuCore.cpp revision 1ffd86b448d78366190c540f98f8b6d641cdb6cf
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#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 nullptr;
77    }
78    if (!cpu->init(version_major, version_minor, lfn, slfn)) {
79        delete cpu;
80        return nullptr;
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 = nullptr;
121    mSelectRTCallback = nullptr;
122    mSetupCompilerCallback = nullptr;
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 nullptr;
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, nullptr);
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 = nullptr;
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_CONF);
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 = nullptr;
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 = nullptr;
327    mWorkers.mLaunchCallback = nullptr;
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);
353typedef void (*walk_loop_t)(MTLaunchStruct*,
354                            RsExpandKernelParams&,
355                            outer_foreach_t);
356
357
358static void walk_wrapper(void* usr, uint32_t idx, walk_loop_t walk_loop) {
359    MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
360
361    uint32_t inLen = mtls->fep.inLen;
362
363    RsExpandKernelParams kparams;
364    kparams.takeFields(mtls->fep);
365
366    // Used by CpuScriptGroup, IntrinsicBlur, and IntrinsicHistogram
367    kparams.lid = idx;
368
369    if (inLen > 0) {
370        // Allocate space for our input base pointers.
371        kparams.ins = (const void**)alloca(inLen * sizeof(void*));
372
373        // Allocate space for our input stride information.
374        kparams.inEStrides = (uint32_t*)alloca(inLen * sizeof(uint32_t));
375
376        // Fill our stride information.
377        for (int inIndex = inLen; --inIndex >= 0;) {
378          kparams.inEStrides[inIndex] = mtls->fep.inStrides[inIndex].eStride;
379        }
380    }
381
382    outer_foreach_t fn = (outer_foreach_t) mtls->kernel;
383
384    walk_loop(mtls, kparams, fn);
385}
386
387static void walk_2d(void *usr, uint32_t idx) {
388    walk_wrapper(usr, idx, [](MTLaunchStruct *mtls,
389                              RsExpandKernelParams &kparams,
390                              outer_foreach_t fn) {
391
392        while (1) {
393            uint32_t slice  = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1);
394            uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize;
395            uint32_t yEnd   = yStart + mtls->mSliceSize;
396
397            yEnd = rsMin(yEnd, mtls->yEnd);
398
399            if (yEnd <= yStart) {
400                return;
401            }
402
403            for (kparams.y = yStart; kparams.y < yEnd; kparams.y++) {
404                kparams.out = mtls->fep.outPtr +
405                              (mtls->fep.outStride.yStride * kparams.y) +
406                              (mtls->fep.outStride.eStride * mtls->xStart);
407
408                for (int inIndex = mtls->fep.inLen; --inIndex >= 0;) {
409                    StridePair &strides = mtls->fep.inStrides[inIndex];
410
411                    kparams.ins[inIndex] =
412                      mtls->fep.inPtrs[inIndex] +
413                      (strides.yStride * kparams.y) +
414                      (strides.eStride * mtls->xStart);
415                }
416
417                fn(&kparams, mtls->xStart, mtls->xEnd,
418                   mtls->fep.outStride.eStride);
419            }
420        }
421    });
422}
423
424static void walk_1d(void *usr, uint32_t idx) {
425    walk_wrapper(usr, idx, [](MTLaunchStruct *mtls,
426                              RsExpandKernelParams &kparams,
427                              outer_foreach_t fn) {
428
429        while (1) {
430            uint32_t slice  = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1);
431            uint32_t xStart = mtls->xStart + slice * mtls->mSliceSize;
432            uint32_t xEnd   = xStart + mtls->mSliceSize;
433
434            xEnd = rsMin(xEnd, mtls->xEnd);
435
436            if (xEnd <= xStart) {
437                return;
438            }
439
440            kparams.out = mtls->fep.outPtr +
441                          (mtls->fep.outStride.eStride * xStart);
442
443            for (int inIndex = mtls->fep.inLen; --inIndex >= 0;) {
444                StridePair &strides = mtls->fep.inStrides[inIndex];
445
446                kparams.ins[inIndex] =
447                  mtls->fep.inPtrs[inIndex] + (strides.eStride * xStart);
448            }
449
450            fn(&kparams, xStart, xEnd, mtls->fep.outStride.eStride);
451        }
452    });
453}
454
455
456void RsdCpuReferenceImpl::launchThreads(const Allocation ** ains,
457                                        uint32_t inLen,
458                                        Allocation* aout,
459                                        const RsScriptCall* sc,
460                                        MTLaunchStruct* mtls) {
461
462    //android::StopWatch kernel_time("kernel time");
463
464    if ((mWorkers.mCount >= 1) && mtls->isThreadable && !mInForEach) {
465        const size_t targetByteChunk = 16 * 1024;
466        mInForEach = true;
467
468        if (mtls->fep.dimY > 1) {
469            uint32_t s1 = mtls->fep.dimY / ((mWorkers.mCount + 1) * 4);
470            uint32_t s2 = 0;
471
472            // This chooses our slice size to rate limit atomic ops to
473            // one per 16k bytes of reads/writes.
474            if (mtls->fep.outStride.yStride) {
475                s2 = targetByteChunk / mtls->fep.outStride.yStride;
476            } else {
477                // We know that there is either an output or an input.
478                s2 = targetByteChunk / mtls->fep.inStrides[0].yStride;
479            }
480            mtls->mSliceSize = rsMin(s1, s2);
481
482            if(mtls->mSliceSize < 1) {
483                mtls->mSliceSize = 1;
484            }
485
486            launchThreads(walk_2d, mtls);
487        } else {
488            uint32_t s1 = mtls->fep.dimX / ((mWorkers.mCount + 1) * 4);
489            uint32_t s2 = 0;
490
491            // This chooses our slice size to rate limit atomic ops to
492            // one per 16k bytes of reads/writes.
493            if (mtls->fep.outStride.eStride) {
494                s2 = targetByteChunk / mtls->fep.outStride.eStride;
495            } else {
496                // We know that there is either an output or an input.
497                s2 = targetByteChunk / mtls->fep.inStrides[0].eStride;
498            }
499            mtls->mSliceSize = rsMin(s1, s2);
500
501            if (mtls->mSliceSize < 1) {
502                mtls->mSliceSize = 1;
503            }
504
505            launchThreads(walk_1d, mtls);
506        }
507        mInForEach = false;
508
509    } else {
510        RsExpandKernelParams kparams;
511        kparams.takeFields(mtls->fep);
512
513        if (inLen > 0) {
514            // Allocate space for our input base pointers.
515            kparams.ins = (const void**)alloca(inLen * sizeof(void*));
516
517            // Allocate space for our input stride information.
518            kparams.inEStrides = (uint32_t*)alloca(inLen * sizeof(uint32_t));
519
520            // Fill our stride information.
521            for (int inIndex = inLen; --inIndex >= 0;) {
522                kparams.inEStrides[inIndex] =
523                    mtls->fep.inStrides[inIndex].eStride;
524            }
525        }
526
527        //ALOGE("launch 3");
528        outer_foreach_t fn = (outer_foreach_t) mtls->kernel;
529        for (uint32_t arrayIndex = mtls->arrayStart;
530             arrayIndex < mtls->arrayEnd; arrayIndex++) {
531
532            for (kparams.z = mtls->zStart; kparams.z < mtls->zEnd;
533                 kparams.z++) {
534
535                for (kparams.y = mtls->yStart; kparams.y < mtls->yEnd;
536                     kparams.y++) {
537
538                    uint32_t offset =
539                      mtls->fep.dimY * mtls->fep.dimZ * arrayIndex +
540                      mtls->fep.dimY * kparams.z + kparams.y;
541
542                    kparams.out = mtls->fep.outPtr +
543                                  (mtls->fep.outStride.yStride * offset) +
544                                  (mtls->fep.outStride.eStride * mtls->xStart);
545
546                    for (int inIndex = inLen; --inIndex >= 0;) {
547                        StridePair &strides = mtls->fep.inStrides[inIndex];
548
549                        kparams.ins[inIndex] =
550                          mtls->fep.inPtrs[inIndex] +
551                          (strides.yStride * offset) +
552                          (strides.eStride * mtls->xStart);
553                    }
554
555                    fn(&kparams, mtls->xStart, mtls->xEnd,
556                       mtls->fep.outStride.eStride);
557                }
558            }
559        }
560    }
561}
562
563RsdCpuScriptImpl * RsdCpuReferenceImpl::setTLS(RsdCpuScriptImpl *sc) {
564    //ALOGE("setTls %p", sc);
565    ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(gThreadTLSKey);
566    rsAssert(tls);
567    RsdCpuScriptImpl *old = tls->mImpl;
568    tls->mImpl = sc;
569    tls->mContext = mRSC;
570    if (sc) {
571        tls->mScript = sc->getScript();
572    } else {
573        tls->mScript = nullptr;
574    }
575    return old;
576}
577
578const RsdCpuReference::CpuSymbol * RsdCpuReferenceImpl::symLookup(const char *name) {
579    return mSymLookupFn(mRSC, name);
580}
581
582
583RsdCpuReference::CpuScript * RsdCpuReferenceImpl::createScript(const ScriptC *s,
584                                    char const *resName, char const *cacheDir,
585                                    uint8_t const *bitcode, size_t bitcodeSize,
586                                    uint32_t flags) {
587
588    RsdCpuScriptImpl *i = new RsdCpuScriptImpl(this, s);
589    if (!i->init(resName, cacheDir, bitcode, bitcodeSize, flags
590#ifndef RS_COMPATIBILITY_LIB
591        , getBccPluginName()
592#endif
593        )) {
594        delete i;
595        return nullptr;
596    }
597    return i;
598}
599
600extern RsdCpuScriptImpl * rsdIntrinsic_3DLUT(RsdCpuReferenceImpl *ctx,
601                                             const Script *s, const Element *e);
602extern RsdCpuScriptImpl * rsdIntrinsic_Convolve3x3(RsdCpuReferenceImpl *ctx,
603                                                   const Script *s, const Element *e);
604extern RsdCpuScriptImpl * rsdIntrinsic_ColorMatrix(RsdCpuReferenceImpl *ctx,
605                                                   const Script *s, const Element *e);
606extern RsdCpuScriptImpl * rsdIntrinsic_LUT(RsdCpuReferenceImpl *ctx,
607                                           const Script *s, const Element *e);
608extern RsdCpuScriptImpl * rsdIntrinsic_Convolve5x5(RsdCpuReferenceImpl *ctx,
609                                                   const Script *s, const Element *e);
610extern RsdCpuScriptImpl * rsdIntrinsic_Blur(RsdCpuReferenceImpl *ctx,
611                                            const Script *s, const Element *e);
612extern RsdCpuScriptImpl * rsdIntrinsic_YuvToRGB(RsdCpuReferenceImpl *ctx,
613                                                const Script *s, const Element *e);
614extern RsdCpuScriptImpl * rsdIntrinsic_Blend(RsdCpuReferenceImpl *ctx,
615                                             const Script *s, const Element *e);
616extern RsdCpuScriptImpl * rsdIntrinsic_Histogram(RsdCpuReferenceImpl *ctx,
617                                                 const Script *s, const Element *e);
618extern RsdCpuScriptImpl * rsdIntrinsic_Resize(RsdCpuReferenceImpl *ctx,
619                                              const Script *s, const Element *e);
620
621RsdCpuReference::CpuScript * RsdCpuReferenceImpl::createIntrinsic(const Script *s,
622                                    RsScriptIntrinsicID iid, Element *e) {
623
624    RsdCpuScriptImpl *i = nullptr;
625    switch (iid) {
626    case RS_SCRIPT_INTRINSIC_ID_3DLUT:
627        i = rsdIntrinsic_3DLUT(this, s, e);
628        break;
629    case RS_SCRIPT_INTRINSIC_ID_CONVOLVE_3x3:
630        i = rsdIntrinsic_Convolve3x3(this, s, e);
631        break;
632    case RS_SCRIPT_INTRINSIC_ID_COLOR_MATRIX:
633        i = rsdIntrinsic_ColorMatrix(this, s, e);
634        break;
635    case RS_SCRIPT_INTRINSIC_ID_LUT:
636        i = rsdIntrinsic_LUT(this, s, e);
637        break;
638    case RS_SCRIPT_INTRINSIC_ID_CONVOLVE_5x5:
639        i = rsdIntrinsic_Convolve5x5(this, s, e);
640        break;
641    case RS_SCRIPT_INTRINSIC_ID_BLUR:
642        i = rsdIntrinsic_Blur(this, s, e);
643        break;
644    case RS_SCRIPT_INTRINSIC_ID_YUV_TO_RGB:
645        i = rsdIntrinsic_YuvToRGB(this, s, e);
646        break;
647    case RS_SCRIPT_INTRINSIC_ID_BLEND:
648        i = rsdIntrinsic_Blend(this, s, e);
649        break;
650    case RS_SCRIPT_INTRINSIC_ID_HISTOGRAM:
651        i = rsdIntrinsic_Histogram(this, s, e);
652        break;
653    case RS_SCRIPT_INTRINSIC_ID_RESIZE:
654        i = rsdIntrinsic_Resize(this, s, e);
655        break;
656
657    default:
658        rsAssert(0);
659    }
660
661    return i;
662}
663
664void* RsdCpuReferenceImpl::createScriptGroup(const ScriptGroupBase *sg) {
665  switch (sg->getApiVersion()) {
666    case ScriptGroupBase::SG_V1: {
667      CpuScriptGroupImpl *sgi = new CpuScriptGroupImpl(this, sg);
668      if (!sgi->init()) {
669        delete sgi;
670        return nullptr;
671      }
672      return sgi;
673    }
674    case ScriptGroupBase::SG_V2: {
675      return new CpuScriptGroup2Impl(this, sg);
676    }
677  }
678  return nullptr;
679}
680