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