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