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