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