blob: f40f4114092e7a6813e8b901fc6045d849f93056 [file] [log] [blame]
Jason Sams709a0972012-11-15 18:18:04 -08001/*
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
21#include <malloc.h>
22#include "rsContext.h"
23
24#include <sys/types.h>
25#include <sys/resource.h>
26#include <sched.h>
Jason Sams709a0972012-11-15 18:18:04 -080027#include <sys/syscall.h>
28#include <string.h>
Stephen Hinesb0934b62013-07-03 17:27:38 -070029#include <unistd.h>
Tim Murray0b575de2013-03-15 15:56:43 -070030
Jason Samsf5ef8df2013-08-06 13:49:25 -070031#include <stdio.h>
32#include <stdlib.h>
33#include <fcntl.h>
34
Stephen Hinesb0934b62013-07-03 17:27:38 -070035#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
Tim Murray0b575de2013-03-15 15:56:43 -070036#include <cutils/properties.h>
Jason Sams709a0972012-11-15 18:18:04 -080037#include "utils/StopWatch.h"
Tim Murray0b575de2013-03-15 15:56:43 -070038#endif
39
40#ifdef RS_SERVER
41// Android exposes gettid(), standard Linux does not
42static pid_t gettid() {
43 return syscall(SYS_gettid);
44}
45#endif
Jason Sams709a0972012-11-15 18:18:04 -080046
47using namespace android;
48using namespace android::renderscript;
49
50typedef void (*outer_foreach_t)(
Chris Wailes80ef6932014-07-08 11:22:18 -070051 const android::renderscript::RsExpandKernelParams *,
Chris Wailes9ed79102014-07-25 15:53:28 -070052 uint32_t x1, uint32_t x2, uint32_t outstep);
Jason Sams709a0972012-11-15 18:18:04 -080053
54
55static pthread_key_t gThreadTLSKey = 0;
56static uint32_t gThreadTLSKeyCount = 0;
57static pthread_mutex_t gInitMutex = PTHREAD_MUTEX_INITIALIZER;
58
Jason Samsf5ef8df2013-08-06 13:49:25 -070059bool android::renderscript::gArchUseSIMD = false;
60
Jason Sams709a0972012-11-15 18:18:04 -080061RsdCpuReference::~RsdCpuReference() {
62}
63
64RsdCpuReference * RsdCpuReference::create(Context *rsc, uint32_t version_major,
Jason Samscadfac42013-03-06 18:09:08 -080065 uint32_t version_minor, sym_lookup_t lfn, script_lookup_t slfn
66#ifndef RS_COMPATIBILITY_LIB
Stephen Hines1d476622013-03-29 22:08:49 -070067 , bcc::RSLinkRuntimeCallback pLinkRuntimeCallback,
Stephen Hines00511322014-01-31 11:20:23 -080068 RSSelectRTCallback pSelectRTCallback,
69 const char *pBccPluginName
Jason Samscadfac42013-03-06 18:09:08 -080070#endif
71 ) {
Jason Sams709a0972012-11-15 18:18:04 -080072
73 RsdCpuReferenceImpl *cpu = new RsdCpuReferenceImpl(rsc);
74 if (!cpu) {
Chris Wailes44bef6f2014-08-12 13:51:10 -070075 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -080076 }
77 if (!cpu->init(version_major, version_minor, lfn, slfn)) {
78 delete cpu;
Chris Wailes44bef6f2014-08-12 13:51:10 -070079 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -080080 }
Stephen Hinesf218bf12013-02-12 19:32:38 -080081
Jason Samscadfac42013-03-06 18:09:08 -080082#ifndef RS_COMPATIBILITY_LIB
Stephen Hinesf218bf12013-02-12 19:32:38 -080083 cpu->setLinkRuntimeCallback(pLinkRuntimeCallback);
Stephen Hines1d476622013-03-29 22:08:49 -070084 cpu->setSelectRTCallback(pSelectRTCallback);
Stephen Hines00511322014-01-31 11:20:23 -080085 if (pBccPluginName) {
86 cpu->setBccPluginName(pBccPluginName);
87 }
Jason Samscadfac42013-03-06 18:09:08 -080088#endif
Stephen Hinesf218bf12013-02-12 19:32:38 -080089
Jason Sams709a0972012-11-15 18:18:04 -080090 return cpu;
91}
92
93
94Context * RsdCpuReference::getTlsContext() {
95 ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(gThreadTLSKey);
96 return tls->mContext;
97}
98
99const Script * RsdCpuReference::getTlsScript() {
100 ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(gThreadTLSKey);
101 return tls->mScript;
102}
103
Stephen Hinesf218bf12013-02-12 19:32:38 -0800104pthread_key_t RsdCpuReference::getThreadTLSKey(){ return gThreadTLSKey; }
Jason Sams709a0972012-11-15 18:18:04 -0800105
106////////////////////////////////////////////////////////////
107///
108
109RsdCpuReferenceImpl::RsdCpuReferenceImpl(Context *rsc) {
110 mRSC = rsc;
111
112 version_major = 0;
113 version_minor = 0;
114 mInForEach = false;
115 memset(&mWorkers, 0, sizeof(mWorkers));
116 memset(&mTlsStruct, 0, sizeof(mTlsStruct));
117 mExit = false;
Jason Samscadfac42013-03-06 18:09:08 -0800118#ifndef RS_COMPATIBILITY_LIB
Chris Wailes44bef6f2014-08-12 13:51:10 -0700119 mLinkRuntimeCallback = nullptr;
120 mSelectRTCallback = nullptr;
121 mSetupCompilerCallback = nullptr;
Jason Samscadfac42013-03-06 18:09:08 -0800122#endif
Jason Sams709a0972012-11-15 18:18:04 -0800123}
124
125
126void * RsdCpuReferenceImpl::helperThreadProc(void *vrsc) {
127 RsdCpuReferenceImpl *dc = (RsdCpuReferenceImpl *)vrsc;
128
Tim Murray0b575de2013-03-15 15:56:43 -0700129 uint32_t idx = __sync_fetch_and_add(&dc->mWorkers.mLaunchCount, 1);
Jason Sams709a0972012-11-15 18:18:04 -0800130
131 //ALOGV("RS helperThread starting %p idx=%i", dc, idx);
132
133 dc->mWorkers.mLaunchSignals[idx].init();
134 dc->mWorkers.mNativeThreadId[idx] = gettid();
135
136 memset(&dc->mTlsStruct, 0, sizeof(dc->mTlsStruct));
137 int status = pthread_setspecific(gThreadTLSKey, &dc->mTlsStruct);
138 if (status) {
139 ALOGE("pthread_setspecific %i", status);
140 }
141
142#if 0
143 typedef struct {uint64_t bits[1024 / 64]; } cpu_set_t;
144 cpu_set_t cpuset;
145 memset(&cpuset, 0, sizeof(cpuset));
146 cpuset.bits[idx / 64] |= 1ULL << (idx % 64);
147 int ret = syscall(241, rsc->mWorkers.mNativeThreadId[idx],
148 sizeof(cpuset), &cpuset);
149 ALOGE("SETAFFINITY ret = %i %s", ret, EGLUtils::strerror(ret));
150#endif
151
152 while (!dc->mExit) {
153 dc->mWorkers.mLaunchSignals[idx].wait();
154 if (dc->mWorkers.mLaunchCallback) {
155 // idx +1 is used because the calling thread is always worker 0.
156 dc->mWorkers.mLaunchCallback(dc->mWorkers.mLaunchData, idx+1);
157 }
Tim Murray0b575de2013-03-15 15:56:43 -0700158 __sync_fetch_and_sub(&dc->mWorkers.mRunningCount, 1);
Jason Sams709a0972012-11-15 18:18:04 -0800159 dc->mWorkers.mCompleteSignal.set();
160 }
161
162 //ALOGV("RS helperThread exited %p idx=%i", dc, idx);
Chris Wailes44bef6f2014-08-12 13:51:10 -0700163 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800164}
165
166void RsdCpuReferenceImpl::launchThreads(WorkerCallback_t cbk, void *data) {
167 mWorkers.mLaunchData = data;
168 mWorkers.mLaunchCallback = cbk;
Tim Murray4d252d62012-11-29 14:37:59 -0800169
170 // fast path for very small launches
171 MTLaunchStruct *mtls = (MTLaunchStruct *)data;
172 if (mtls && mtls->fep.dimY <= 1 && mtls->xEnd <= mtls->xStart + mtls->mSliceSize) {
173 if (mWorkers.mLaunchCallback) {
174 mWorkers.mLaunchCallback(mWorkers.mLaunchData, 0);
175 }
176 return;
177 }
178
Tim Murray0b575de2013-03-15 15:56:43 -0700179 mWorkers.mRunningCount = mWorkers.mCount;
180 __sync_synchronize();
181
Jason Sams709a0972012-11-15 18:18:04 -0800182 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
183 mWorkers.mLaunchSignals[ct].set();
184 }
185
186 // We use the calling thread as one of the workers so we can start without
187 // the delay of the thread wakeup.
188 if (mWorkers.mLaunchCallback) {
Tim Murray4d252d62012-11-29 14:37:59 -0800189 mWorkers.mLaunchCallback(mWorkers.mLaunchData, 0);
Jason Sams709a0972012-11-15 18:18:04 -0800190 }
191
Tim Murray0b575de2013-03-15 15:56:43 -0700192 while (__sync_fetch_and_or(&mWorkers.mRunningCount, 0) != 0) {
Jason Sams709a0972012-11-15 18:18:04 -0800193 mWorkers.mCompleteSignal.wait();
194 }
195}
196
197
198void RsdCpuReferenceImpl::lockMutex() {
199 pthread_mutex_lock(&gInitMutex);
200}
201
202void RsdCpuReferenceImpl::unlockMutex() {
203 pthread_mutex_unlock(&gInitMutex);
204}
205
Jason Samsf5ef8df2013-08-06 13:49:25 -0700206static int
207read_file(const char* pathname, char* buffer, size_t buffsize)
208{
209 int fd, len;
210
211 fd = open(pathname, O_RDONLY);
212 if (fd < 0)
213 return -1;
214
215 do {
216 len = read(fd, buffer, buffsize);
217 } while (len < 0 && errno == EINTR);
218
219 close(fd);
220
221 return len;
222}
223
224static void GetCpuInfo() {
225 char cpuinfo[4096];
226 int cpuinfo_len;
227
228 cpuinfo_len = read_file("/proc/cpuinfo", cpuinfo, sizeof cpuinfo);
229 if (cpuinfo_len < 0) /* should not happen */ {
230 return;
231 }
232
Jason Sams074424a2014-05-22 13:30:03 -0700233#if defined(ARCH_ARM_HAVE_VFP) || defined(ARCH_ARM_USE_INTRINSICS)
234 gArchUseSIMD = (!!strstr(cpuinfo, " neon")) ||
235 (!!strstr(cpuinfo, " asimd"));
Rose, James7b7060c2014-04-22 12:08:06 +0800236#elif defined(ARCH_X86_HAVE_SSSE3)
237 gArchUseSIMD = !!strstr(cpuinfo, " ssse3");
238#endif
Jason Samsf5ef8df2013-08-06 13:49:25 -0700239}
Jason Samsf5ef8df2013-08-06 13:49:25 -0700240
Jason Sams709a0972012-11-15 18:18:04 -0800241bool RsdCpuReferenceImpl::init(uint32_t version_major, uint32_t version_minor,
242 sym_lookup_t lfn, script_lookup_t slfn) {
243
244 mSymLookupFn = lfn;
245 mScriptLookupFn = slfn;
246
247 lockMutex();
248 if (!gThreadTLSKeyCount) {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700249 int status = pthread_key_create(&gThreadTLSKey, nullptr);
Jason Sams709a0972012-11-15 18:18:04 -0800250 if (status) {
251 ALOGE("Failed to init thread tls key.");
252 unlockMutex();
253 return false;
254 }
255 }
256 gThreadTLSKeyCount++;
257 unlockMutex();
258
259 mTlsStruct.mContext = mRSC;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700260 mTlsStruct.mScript = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800261 int status = pthread_setspecific(gThreadTLSKey, &mTlsStruct);
262 if (status) {
263 ALOGE("pthread_setspecific %i", status);
264 }
265
Jason Samsf5ef8df2013-08-06 13:49:25 -0700266 GetCpuInfo();
Jason Samsf5ef8df2013-08-06 13:49:25 -0700267
Jason Sams709a0972012-11-15 18:18:04 -0800268 int cpu = sysconf(_SC_NPROCESSORS_ONLN);
269 if(mRSC->props.mDebugMaxThreads) {
270 cpu = mRSC->props.mDebugMaxThreads;
271 }
272 if (cpu < 2) {
273 mWorkers.mCount = 0;
274 return true;
275 }
276
277 // Subtract one from the cpu count because we also use the command thread as a worker.
278 mWorkers.mCount = (uint32_t)(cpu - 1);
279
Jason Sams8ca358a2013-03-19 13:59:40 -0700280 ALOGV("%p Launching thread(s), CPUs %i", mRSC, mWorkers.mCount + 1);
Jason Sams709a0972012-11-15 18:18:04 -0800281
282 mWorkers.mThreadId = (pthread_t *) calloc(mWorkers.mCount, sizeof(pthread_t));
283 mWorkers.mNativeThreadId = (pid_t *) calloc(mWorkers.mCount, sizeof(pid_t));
284 mWorkers.mLaunchSignals = new Signal[mWorkers.mCount];
Chris Wailes44bef6f2014-08-12 13:51:10 -0700285 mWorkers.mLaunchCallback = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800286
287 mWorkers.mCompleteSignal.init();
288
Tim Murray0b575de2013-03-15 15:56:43 -0700289 mWorkers.mRunningCount = mWorkers.mCount;
290 mWorkers.mLaunchCount = 0;
291 __sync_synchronize();
Jason Sams709a0972012-11-15 18:18:04 -0800292
293 pthread_attr_t threadAttr;
294 status = pthread_attr_init(&threadAttr);
295 if (status) {
296 ALOGE("Failed to init thread attribute.");
297 return false;
298 }
299
300 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
301 status = pthread_create(&mWorkers.mThreadId[ct], &threadAttr, helperThreadProc, this);
302 if (status) {
303 mWorkers.mCount = ct;
304 ALOGE("Created fewer than expected number of RS threads.");
305 break;
306 }
307 }
Tim Murray0b575de2013-03-15 15:56:43 -0700308 while (__sync_fetch_and_or(&mWorkers.mRunningCount, 0) != 0) {
Jason Sams709a0972012-11-15 18:18:04 -0800309 usleep(100);
310 }
311
312 pthread_attr_destroy(&threadAttr);
313 return true;
314}
315
316
317void RsdCpuReferenceImpl::setPriority(int32_t priority) {
318 for (uint32_t ct=0; ct < mWorkers.mCount; ct++) {
319 setpriority(PRIO_PROCESS, mWorkers.mNativeThreadId[ct], priority);
320 }
321}
322
323RsdCpuReferenceImpl::~RsdCpuReferenceImpl() {
324 mExit = true;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700325 mWorkers.mLaunchData = nullptr;
326 mWorkers.mLaunchCallback = nullptr;
Tim Murray0b575de2013-03-15 15:56:43 -0700327 mWorkers.mRunningCount = mWorkers.mCount;
328 __sync_synchronize();
Jason Sams709a0972012-11-15 18:18:04 -0800329 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
330 mWorkers.mLaunchSignals[ct].set();
331 }
332 void *res;
333 for (uint32_t ct = 0; ct < mWorkers.mCount; ct++) {
334 pthread_join(mWorkers.mThreadId[ct], &res);
335 }
Tim Murray0b575de2013-03-15 15:56:43 -0700336 rsAssert(__sync_fetch_and_or(&mWorkers.mRunningCount, 0) == 0);
Jens Gulin07ef7042014-02-19 18:16:01 +0100337 free(mWorkers.mThreadId);
338 free(mWorkers.mNativeThreadId);
339 delete[] mWorkers.mLaunchSignals;
Jason Sams709a0972012-11-15 18:18:04 -0800340
341 // Global structure cleanup.
342 lockMutex();
343 --gThreadTLSKeyCount;
344 if (!gThreadTLSKeyCount) {
345 pthread_key_delete(gThreadTLSKey);
346 }
347 unlockMutex();
348
349}
350
351typedef void (*rs_t)(const void *, void *, const void *, uint32_t, uint32_t, uint32_t, uint32_t);
Chris Wailesf3712132014-07-16 15:18:30 -0700352typedef void (*walk_loop_t)(MTLaunchStruct*,
353 RsExpandKernelParams&,
354 outer_foreach_t);
Jason Sams709a0972012-11-15 18:18:04 -0800355
Chris Wailesf3712132014-07-16 15:18:30 -0700356
357static void walk_wrapper(void* usr, uint32_t idx, walk_loop_t walk_loop) {
Jason Sams709a0972012-11-15 18:18:04 -0800358 MTLaunchStruct *mtls = (MTLaunchStruct *)usr;
Chris Wailes80ef6932014-07-08 11:22:18 -0700359
Chris Wailesf3712132014-07-16 15:18:30 -0700360 uint32_t inLen = mtls->fep.inLen;
361
Chris Wailes80ef6932014-07-08 11:22:18 -0700362 RsExpandKernelParams kparams;
363 kparams.takeFields(mtls->fep);
364
365 // Used by CpuScriptGroup, IntrinsicBlur, and IntrinsicHistogram
366 kparams.lid = idx;
Jason Sams709a0972012-11-15 18:18:04 -0800367
Chris Wailesf3712132014-07-16 15:18:30 -0700368 if (inLen > 0) {
369 // Allocate space for our input base pointers.
370 kparams.ins = (const void**)alloca(inLen * sizeof(void*));
Chris Wailes80ef6932014-07-08 11:22:18 -0700371
Chris Wailesf3712132014-07-16 15:18:30 -0700372 // Allocate space for our input stride information.
373 kparams.inEStrides = (uint32_t*)alloca(inLen * sizeof(uint32_t));
Chris Wailes80ef6932014-07-08 11:22:18 -0700374
Chris Wailesf3712132014-07-16 15:18:30 -0700375 // Fill our stride information.
376 for (int inIndex = inLen; --inIndex >= 0;) {
377 kparams.inEStrides[inIndex] = mtls->fep.inStrides[inIndex].eStride;
Jason Sams709a0972012-11-15 18:18:04 -0800378 }
379 }
Jason Sams709a0972012-11-15 18:18:04 -0800380
381 outer_foreach_t fn = (outer_foreach_t) mtls->kernel;
Chris Wailes80ef6932014-07-08 11:22:18 -0700382
Chris Wailesf3712132014-07-16 15:18:30 -0700383 walk_loop(mtls, kparams, fn);
Jason Sams709a0972012-11-15 18:18:04 -0800384}
385
Chris Wailesf3712132014-07-16 15:18:30 -0700386static void walk_2d(void *usr, uint32_t idx) {
387 walk_wrapper(usr, idx, [](MTLaunchStruct *mtls,
388 RsExpandKernelParams &kparams,
389 outer_foreach_t fn) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700390
Chris Wailesf3712132014-07-16 15:18:30 -0700391 while (1) {
392 uint32_t slice = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1);
393 uint32_t yStart = mtls->yStart + slice * mtls->mSliceSize;
394 uint32_t yEnd = yStart + mtls->mSliceSize;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700395
Chris Wailesf3712132014-07-16 15:18:30 -0700396 yEnd = rsMin(yEnd, mtls->yEnd);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700397
Chris Wailesf3712132014-07-16 15:18:30 -0700398 if (yEnd <= yStart) {
399 return;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700400 }
401
Chris Wailesf3712132014-07-16 15:18:30 -0700402 for (kparams.y = yStart; kparams.y < yEnd; kparams.y++) {
403 kparams.out = mtls->fep.outPtr +
404 (mtls->fep.outStride.yStride * kparams.y) +
405 (mtls->fep.outStride.eStride * mtls->xStart);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700406
Chris Wailesf3712132014-07-16 15:18:30 -0700407 for (int inIndex = mtls->fep.inLen; --inIndex >= 0;) {
408 StridePair &strides = mtls->fep.inStrides[inIndex];
Stephen Hines4b2bea32014-08-13 17:32:10 +0000409
Chris Wailesf3712132014-07-16 15:18:30 -0700410 kparams.ins[inIndex] =
411 mtls->fep.inPtrs[inIndex] +
412 (strides.yStride * kparams.y) +
413 (strides.eStride * mtls->xStart);
Stephen Hines4b2bea32014-08-13 17:32:10 +0000414 }
Chris Wailesf3712132014-07-16 15:18:30 -0700415
Chris Wailes9ed79102014-07-25 15:53:28 -0700416 fn(&kparams, mtls->xStart, mtls->xEnd,
Chris Wailesf3712132014-07-16 15:18:30 -0700417 mtls->fep.outStride.eStride);
Stephen Hines4b2bea32014-08-13 17:32:10 +0000418 }
419 }
Chris Wailesf3712132014-07-16 15:18:30 -0700420 });
Stephen Hines4b2bea32014-08-13 17:32:10 +0000421}
422
Chris Wailesf3712132014-07-16 15:18:30 -0700423static void walk_1d(void *usr, uint32_t idx) {
424 walk_wrapper(usr, idx, [](MTLaunchStruct *mtls,
425 RsExpandKernelParams &kparams,
426 outer_foreach_t fn) {
427
428 while (1) {
429 uint32_t slice = (uint32_t)__sync_fetch_and_add(&mtls->mSliceNum, 1);
430 uint32_t xStart = mtls->xStart + slice * mtls->mSliceSize;
431 uint32_t xEnd = xStart + mtls->mSliceSize;
432
433 xEnd = rsMin(xEnd, mtls->xEnd);
434
435 if (xEnd <= xStart) {
436 return;
437 }
438
439 kparams.out = mtls->fep.outPtr +
440 (mtls->fep.outStride.eStride * xStart);
441
442 for (int inIndex = mtls->fep.inLen; --inIndex >= 0;) {
443 StridePair &strides = mtls->fep.inStrides[inIndex];
444
445 kparams.ins[inIndex] =
446 mtls->fep.inPtrs[inIndex] + (strides.eStride * xStart);
447 }
448
Chris Wailes9ed79102014-07-25 15:53:28 -0700449 fn(&kparams, xStart, xEnd, mtls->fep.outStride.eStride);
Chris Wailesf3712132014-07-16 15:18:30 -0700450 }
451 });
452}
453
454
455void RsdCpuReferenceImpl::launchThreads(const Allocation ** ains,
456 uint32_t inLen,
457 Allocation* aout,
458 const RsScriptCall* sc,
459 MTLaunchStruct* mtls) {
Stephen Hines4b2bea32014-08-13 17:32:10 +0000460
461 //android::StopWatch kernel_time("kernel time");
462
463 if ((mWorkers.mCount >= 1) && mtls->isThreadable && !mInForEach) {
464 const size_t targetByteChunk = 16 * 1024;
465 mInForEach = true;
Chris Wailesf3712132014-07-16 15:18:30 -0700466
Stephen Hines4b2bea32014-08-13 17:32:10 +0000467 if (mtls->fep.dimY > 1) {
468 uint32_t s1 = mtls->fep.dimY / ((mWorkers.mCount + 1) * 4);
469 uint32_t s2 = 0;
470
471 // This chooses our slice size to rate limit atomic ops to
472 // one per 16k bytes of reads/writes.
Chris Wailesf3712132014-07-16 15:18:30 -0700473 if (mtls->fep.outStride.yStride) {
474 s2 = targetByteChunk / mtls->fep.outStride.yStride;
Stephen Hines4b2bea32014-08-13 17:32:10 +0000475 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700476 // We know that there is either an output or an input.
477 s2 = targetByteChunk / mtls->fep.inStrides[0].yStride;
Stephen Hines4b2bea32014-08-13 17:32:10 +0000478 }
479 mtls->mSliceSize = rsMin(s1, s2);
480
481 if(mtls->mSliceSize < 1) {
482 mtls->mSliceSize = 1;
483 }
484
Chris Wailesf3712132014-07-16 15:18:30 -0700485 launchThreads(walk_2d, mtls);
Stephen Hines4b2bea32014-08-13 17:32:10 +0000486 } else {
487 uint32_t s1 = mtls->fep.dimX / ((mWorkers.mCount + 1) * 4);
488 uint32_t s2 = 0;
489
490 // This chooses our slice size to rate limit atomic ops to
491 // one per 16k bytes of reads/writes.
Chris Wailesf3712132014-07-16 15:18:30 -0700492 if (mtls->fep.outStride.eStride) {
493 s2 = targetByteChunk / mtls->fep.outStride.eStride;
Stephen Hines4b2bea32014-08-13 17:32:10 +0000494 } else {
Chris Wailesf3712132014-07-16 15:18:30 -0700495 // We know that there is either an output or an input.
496 s2 = targetByteChunk / mtls->fep.inStrides[0].eStride;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700497 }
498 mtls->mSliceSize = rsMin(s1, s2);
499
500 if (mtls->mSliceSize < 1) {
501 mtls->mSliceSize = 1;
502 }
503
Chris Wailesf3712132014-07-16 15:18:30 -0700504 launchThreads(walk_1d, mtls);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700505 }
506 mInForEach = false;
507
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700508 } else {
Chris Wailes80ef6932014-07-08 11:22:18 -0700509 RsExpandKernelParams kparams;
510 kparams.takeFields(mtls->fep);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700511
Chris Wailesf3712132014-07-16 15:18:30 -0700512 if (inLen > 0) {
513 // Allocate space for our input base pointers.
514 kparams.ins = (const void**)alloca(inLen * sizeof(void*));
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700515
Chris Wailesf3712132014-07-16 15:18:30 -0700516 // Allocate space for our input stride information.
517 kparams.inEStrides = (uint32_t*)alloca(inLen * sizeof(uint32_t));
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700518
Chris Wailesf3712132014-07-16 15:18:30 -0700519 // Fill our stride information.
520 for (int inIndex = inLen; --inIndex >= 0;) {
521 kparams.inEStrides[inIndex] =
522 mtls->fep.inStrides[inIndex].eStride;
523 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700524 }
525
526 //ALOGE("launch 3");
527 outer_foreach_t fn = (outer_foreach_t) mtls->kernel;
Chris Wailes80ef6932014-07-08 11:22:18 -0700528 for (uint32_t arrayIndex = mtls->arrayStart;
529 arrayIndex < mtls->arrayEnd; arrayIndex++) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700530
Chris Wailes80ef6932014-07-08 11:22:18 -0700531 for (kparams.z = mtls->zStart; kparams.z < mtls->zEnd;
532 kparams.z++) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700533
Chris Wailes80ef6932014-07-08 11:22:18 -0700534 for (kparams.y = mtls->yStart; kparams.y < mtls->yEnd;
535 kparams.y++) {
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700536
Chris Wailes80ef6932014-07-08 11:22:18 -0700537 uint32_t offset =
538 mtls->fep.dimY * mtls->fep.dimZ * arrayIndex +
539 mtls->fep.dimY * kparams.z + kparams.y;
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700540
Chris Wailesf3712132014-07-16 15:18:30 -0700541 kparams.out = mtls->fep.outPtr +
542 (mtls->fep.outStride.yStride * offset) +
543 (mtls->fep.outStride.eStride * mtls->xStart);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700544
Chris Wailes80ef6932014-07-08 11:22:18 -0700545 for (int inIndex = inLen; --inIndex >= 0;) {
546 StridePair &strides = mtls->fep.inStrides[inIndex];
547
548 kparams.ins[inIndex] =
Chris Wailesf3712132014-07-16 15:18:30 -0700549 mtls->fep.inPtrs[inIndex] +
Chris Wailes80ef6932014-07-08 11:22:18 -0700550 (strides.yStride * offset) +
551 (strides.eStride * mtls->xStart);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700552 }
553
Chris Wailes9ed79102014-07-25 15:53:28 -0700554 fn(&kparams, mtls->xStart, mtls->xEnd,
Chris Wailesf3712132014-07-16 15:18:30 -0700555 mtls->fep.outStride.eStride);
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700556 }
557 }
558 }
Chris Wailes4b3c34e2014-06-11 12:00:29 -0700559 }
560}
561
Jason Sams709a0972012-11-15 18:18:04 -0800562RsdCpuScriptImpl * RsdCpuReferenceImpl::setTLS(RsdCpuScriptImpl *sc) {
563 //ALOGE("setTls %p", sc);
564 ScriptTLSStruct * tls = (ScriptTLSStruct *)pthread_getspecific(gThreadTLSKey);
565 rsAssert(tls);
566 RsdCpuScriptImpl *old = tls->mImpl;
567 tls->mImpl = sc;
568 tls->mContext = mRSC;
569 if (sc) {
570 tls->mScript = sc->getScript();
571 } else {
Chris Wailes44bef6f2014-08-12 13:51:10 -0700572 tls->mScript = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800573 }
574 return old;
575}
576
577const RsdCpuReference::CpuSymbol * RsdCpuReferenceImpl::symLookup(const char *name) {
578 return mSymLookupFn(mRSC, name);
579}
580
581
582RsdCpuReference::CpuScript * RsdCpuReferenceImpl::createScript(const ScriptC *s,
583 char const *resName, char const *cacheDir,
584 uint8_t const *bitcode, size_t bitcodeSize,
585 uint32_t flags) {
586
587 RsdCpuScriptImpl *i = new RsdCpuScriptImpl(this, s);
Stephen Hines00511322014-01-31 11:20:23 -0800588 if (!i->init(resName, cacheDir, bitcode, bitcodeSize, flags
589#ifndef RS_COMPATIBILITY_LIB
590 , getBccPluginName()
591#endif
592 )) {
Jason Sams709a0972012-11-15 18:18:04 -0800593 delete i;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700594 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800595 }
596 return i;
597}
598
Jason Sams7c4b8882013-01-04 10:50:05 -0800599extern RsdCpuScriptImpl * rsdIntrinsic_3DLUT(RsdCpuReferenceImpl *ctx,
600 const Script *s, const Element *e);
Jason Samsc905efd2012-11-26 15:20:18 -0800601extern RsdCpuScriptImpl * rsdIntrinsic_Convolve3x3(RsdCpuReferenceImpl *ctx,
602 const Script *s, const Element *e);
603extern RsdCpuScriptImpl * rsdIntrinsic_ColorMatrix(RsdCpuReferenceImpl *ctx,
604 const Script *s, const Element *e);
605extern RsdCpuScriptImpl * rsdIntrinsic_LUT(RsdCpuReferenceImpl *ctx,
606 const Script *s, const Element *e);
607extern RsdCpuScriptImpl * rsdIntrinsic_Convolve5x5(RsdCpuReferenceImpl *ctx,
608 const Script *s, const Element *e);
609extern RsdCpuScriptImpl * rsdIntrinsic_Blur(RsdCpuReferenceImpl *ctx,
610 const Script *s, const Element *e);
611extern RsdCpuScriptImpl * rsdIntrinsic_YuvToRGB(RsdCpuReferenceImpl *ctx,
612 const Script *s, const Element *e);
613extern RsdCpuScriptImpl * rsdIntrinsic_Blend(RsdCpuReferenceImpl *ctx,
614 const Script *s, const Element *e);
Jason Sams2282e282013-06-17 16:52:01 -0700615extern RsdCpuScriptImpl * rsdIntrinsic_Histogram(RsdCpuReferenceImpl *ctx,
616 const Script *s, const Element *e);
Matthieu Delahaye6fc3e122014-03-04 11:05:49 -0600617extern RsdCpuScriptImpl * rsdIntrinsic_LoopFilter(RsdCpuReferenceImpl *ctx,
618 const Script *s, const Element *e);
Jason Sams39ab94a2014-04-16 17:14:05 -0700619extern RsdCpuScriptImpl * rsdIntrinsic_Resize(RsdCpuReferenceImpl *ctx,
620 const Script *s, const Element *e);
Jason Sams709a0972012-11-15 18:18:04 -0800621
622RsdCpuReference::CpuScript * RsdCpuReferenceImpl::createIntrinsic(const Script *s,
623 RsScriptIntrinsicID iid, Element *e) {
624
Chris Wailes44bef6f2014-08-12 13:51:10 -0700625 RsdCpuScriptImpl *i = nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800626 switch (iid) {
Jason Sams7c4b8882013-01-04 10:50:05 -0800627 case RS_SCRIPT_INTRINSIC_ID_3DLUT:
628 i = rsdIntrinsic_3DLUT(this, s, e);
629 break;
Jason Sams709a0972012-11-15 18:18:04 -0800630 case RS_SCRIPT_INTRINSIC_ID_CONVOLVE_3x3:
Jason Samsc905efd2012-11-26 15:20:18 -0800631 i = rsdIntrinsic_Convolve3x3(this, s, e);
Jason Sams709a0972012-11-15 18:18:04 -0800632 break;
633 case RS_SCRIPT_INTRINSIC_ID_COLOR_MATRIX:
Jason Samsc905efd2012-11-26 15:20:18 -0800634 i = rsdIntrinsic_ColorMatrix(this, s, e);
Jason Sams709a0972012-11-15 18:18:04 -0800635 break;
636 case RS_SCRIPT_INTRINSIC_ID_LUT:
Jason Samsc905efd2012-11-26 15:20:18 -0800637 i = rsdIntrinsic_LUT(this, s, e);
Jason Sams709a0972012-11-15 18:18:04 -0800638 break;
639 case RS_SCRIPT_INTRINSIC_ID_CONVOLVE_5x5:
Jason Samsc905efd2012-11-26 15:20:18 -0800640 i = rsdIntrinsic_Convolve5x5(this, s, e);
Jason Sams709a0972012-11-15 18:18:04 -0800641 break;
642 case RS_SCRIPT_INTRINSIC_ID_BLUR:
Jason Samsc905efd2012-11-26 15:20:18 -0800643 i = rsdIntrinsic_Blur(this, s, e);
Jason Sams709a0972012-11-15 18:18:04 -0800644 break;
645 case RS_SCRIPT_INTRINSIC_ID_YUV_TO_RGB:
Jason Samsc905efd2012-11-26 15:20:18 -0800646 i = rsdIntrinsic_YuvToRGB(this, s, e);
Jason Sams709a0972012-11-15 18:18:04 -0800647 break;
648 case RS_SCRIPT_INTRINSIC_ID_BLEND:
Jason Samsc905efd2012-11-26 15:20:18 -0800649 i = rsdIntrinsic_Blend(this, s, e);
Jason Sams709a0972012-11-15 18:18:04 -0800650 break;
Jason Sams2282e282013-06-17 16:52:01 -0700651 case RS_SCRIPT_INTRINSIC_ID_HISTOGRAM:
652 i = rsdIntrinsic_Histogram(this, s, e);
653 break;
Jason Sams39ab94a2014-04-16 17:14:05 -0700654 case RS_SCRIPT_INTRINSIC_ID_RESIZE:
655 i = rsdIntrinsic_Resize(this, s, e);
656 break;
Matthieu Delahaye6fc3e122014-03-04 11:05:49 -0600657#ifndef RS_COMPATIBILITY_LIB
658 case RS_SCRIPT_INTRINSIC_ID_LOOP_FILTER:
659 i = rsdIntrinsic_LoopFilter(this, s, e);
660 break;
661#endif
Jason Sams709a0972012-11-15 18:18:04 -0800662
663 default:
664 rsAssert(0);
665 }
666
667 return i;
668}
669
670RsdCpuReference::CpuScriptGroup * RsdCpuReferenceImpl::createScriptGroup(const ScriptGroup *sg) {
671 CpuScriptGroupImpl *sgi = new CpuScriptGroupImpl(this, sg);
672 if (!sgi->init()) {
673 delete sgi;
Chris Wailes44bef6f2014-08-12 13:51:10 -0700674 return nullptr;
Jason Sams709a0972012-11-15 18:18:04 -0800675 }
676 return sgi;
677}