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