blob: e38f92ad08d42783f7b55d5c773e06bb34d5b336 [file] [log] [blame]
Jason Samsbad80742011-03-16 16:29:28 -07001/*
Stephen Hinesee7aa2e2012-01-12 18:56:23 -08002 * Copyright (C) 2011-2012 The Android Open Source Project
Jason Samsbad80742011-03-16 16:29:28 -07003 *
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
Jason Sams709a0972012-11-15 18:18:04 -080017#include "../cpu_ref/rsd_cpu.h"
18
Jason Samsbad80742011-03-16 16:29:28 -070019#include "rsdCore.h"
Jason Samseb4fe182011-05-26 16:33:01 -070020#include "rsdAllocation.h"
Jason Samsbad80742011-03-16 16:29:28 -070021#include "rsdBcc.h"
Jason Sams110f1812013-03-14 16:02:18 -070022#ifndef RS_COMPATIBILITY_LIB
Stephen Hinesa221f562013-07-10 17:59:47 -070023 #include "MemChunk.h"
Jason Sams110f1812013-03-14 16:02:18 -070024 #include "rsdGL.h"
25 #include "rsdPath.h"
26 #include "rsdProgramStore.h"
27 #include "rsdProgramRaster.h"
28 #include "rsdProgramVertex.h"
29 #include "rsdProgramFragment.h"
30 #include "rsdMesh.h"
31 #include "rsdFrameBuffer.h"
32#endif
Alex Sakhartchouk7f126c72011-05-05 16:56:27 -070033#include "rsdSampler.h"
Jason Samsdbe66d62012-09-17 13:54:41 -070034#include "rsdScriptGroup.h"
Jason Samsbad80742011-03-16 16:29:28 -070035
36#include <malloc.h>
37#include "rsContext.h"
38
Jason Samscdfdb8f2011-03-17 16:12:47 -070039#include <sys/types.h>
40#include <sys/resource.h>
41#include <sched.h>
Jason Samscdfdb8f2011-03-17 16:12:47 -070042#include <sys/syscall.h>
43#include <string.h>
44
Jason Samsbad80742011-03-16 16:29:28 -070045using namespace android;
46using namespace android::renderscript;
47
Jason Samscdfdb8f2011-03-17 16:12:47 -070048static void Shutdown(Context *rsc);
49static void SetPriority(const Context *rsc, int32_t priority);
50
Jason Sams110f1812013-03-14 16:02:18 -070051#ifndef RS_COMPATIBILITY_LIB
52 #define NATIVE_FUNC(a) a
53#else
54 #define NATIVE_FUNC(a) NULL
55#endif
56
57
Jason Samsbad80742011-03-16 16:29:28 -070058static RsdHalFunctions FunctionTable = {
Jason Sams110f1812013-03-14 16:02:18 -070059 NATIVE_FUNC(rsdGLInit),
60 NATIVE_FUNC(rsdGLShutdown),
61 NATIVE_FUNC(rsdGLSetSurface),
62 NATIVE_FUNC(rsdGLSwap),
Jason Sams4b3de472011-04-06 17:52:23 -070063
Jason Samscdfdb8f2011-03-17 16:12:47 -070064 Shutdown,
Jason Samsbad80742011-03-16 16:29:28 -070065 NULL,
Jason Samscdfdb8f2011-03-17 16:12:47 -070066 SetPriority,
Tim Murray34689382013-03-11 12:12:03 -070067 rsdAllocRuntimeMem,
68 rsdFreeRuntimeMem,
Jason Samsbad80742011-03-16 16:29:28 -070069 {
70 rsdScriptInit,
Jason Sams8eaba4f2012-08-14 14:38:05 -070071 rsdInitIntrinsic,
Jason Samsbad80742011-03-16 16:29:28 -070072 rsdScriptInvokeFunction,
73 rsdScriptInvokeRoot,
Jason Samscdfdb8f2011-03-17 16:12:47 -070074 rsdScriptInvokeForEach,
Jason Samsbad80742011-03-16 16:29:28 -070075 rsdScriptInvokeInit,
Stephen Hines4ee16ff2011-08-31 17:41:39 -070076 rsdScriptInvokeFreeChildren,
Jason Samsbad80742011-03-16 16:29:28 -070077 rsdScriptSetGlobalVar,
Tim Murray9c642392013-04-11 13:29:59 -070078 rsdScriptGetGlobalVar,
Stephen Hines2980f072012-04-09 18:26:29 -070079 rsdScriptSetGlobalVarWithElemDims,
Jason Samsbad80742011-03-16 16:29:28 -070080 rsdScriptSetGlobalBind,
81 rsdScriptSetGlobalObj,
82 rsdScriptDestroy
Jason Sams8feea4e2011-03-18 15:03:25 -070083 },
84
Jason Samseb4fe182011-05-26 16:33:01 -070085 {
86 rsdAllocationInit,
87 rsdAllocationDestroy,
Jason Samsddceab92013-08-07 13:02:32 -070088 rsdAllocationGrallocBits,
Jason Samseb4fe182011-05-26 16:33:01 -070089 rsdAllocationResize,
90 rsdAllocationSyncAll,
91 rsdAllocationMarkDirty,
Jason Sams110f1812013-03-14 16:02:18 -070092 NATIVE_FUNC(rsdAllocationSetSurface),
93 NATIVE_FUNC(rsdAllocationIoSend),
94 NATIVE_FUNC(rsdAllocationIoReceive),
Jason Samseb4fe182011-05-26 16:33:01 -070095 rsdAllocationData1D,
96 rsdAllocationData2D,
97 rsdAllocationData3D,
Jason Sams807fdc42012-07-25 17:55:39 -070098 rsdAllocationRead1D,
99 rsdAllocationRead2D,
100 rsdAllocationRead3D,
101 rsdAllocationLock1D,
102 rsdAllocationUnlock1D,
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700103 rsdAllocationData1D_alloc,
104 rsdAllocationData2D_alloc,
105 rsdAllocationData3D_alloc,
Jason Samseb4fe182011-05-26 16:33:01 -0700106 rsdAllocationElementData1D,
Jason Sams61a4bb72012-07-25 19:33:43 -0700107 rsdAllocationElementData2D,
108 rsdAllocationGenerateMipmaps
Jason Samseb4fe182011-05-26 16:33:01 -0700109 },
110
Jason Sams8feea4e2011-03-18 15:03:25 -0700111
112 {
Jason Sams110f1812013-03-14 16:02:18 -0700113 NATIVE_FUNC(rsdProgramStoreInit),
114 NATIVE_FUNC(rsdProgramStoreSetActive),
115 NATIVE_FUNC(rsdProgramStoreDestroy)
Jason Sams721acc42011-04-06 11:23:54 -0700116 },
117
118 {
Jason Sams110f1812013-03-14 16:02:18 -0700119 NATIVE_FUNC(rsdProgramRasterInit),
120 NATIVE_FUNC(rsdProgramRasterSetActive),
121 NATIVE_FUNC(rsdProgramRasterDestroy)
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700122 },
123
124 {
Jason Sams110f1812013-03-14 16:02:18 -0700125 NATIVE_FUNC(rsdProgramVertexInit),
126 NATIVE_FUNC(rsdProgramVertexSetActive),
127 NATIVE_FUNC(rsdProgramVertexDestroy)
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700128 },
129
130 {
Jason Sams110f1812013-03-14 16:02:18 -0700131 NATIVE_FUNC(rsdProgramFragmentInit),
132 NATIVE_FUNC(rsdProgramFragmentSetActive),
133 NATIVE_FUNC(rsdProgramFragmentDestroy)
Alex Sakhartchouka04e30d2011-04-29 16:49:08 -0700134 },
135
136 {
Jason Sams110f1812013-03-14 16:02:18 -0700137 NATIVE_FUNC(rsdMeshInit),
138 NATIVE_FUNC(rsdMeshDraw),
139 NATIVE_FUNC(rsdMeshDestroy)
Alex Sakhartchouk7f126c72011-05-05 16:56:27 -0700140 },
141
142 {
Jason Sams110f1812013-03-14 16:02:18 -0700143 NATIVE_FUNC(rsdPathInitStatic),
144 NATIVE_FUNC(rsdPathInitDynamic),
145 NATIVE_FUNC(rsdPathDraw),
146 NATIVE_FUNC(rsdPathDestroy)
Jason Sams9e0afb52011-10-31 13:23:43 -0700147 },
148
149 {
Alex Sakhartchouk7f126c72011-05-05 16:56:27 -0700150 rsdSamplerInit,
151 rsdSamplerDestroy
152 },
Jason Sams8feea4e2011-03-18 15:03:25 -0700153
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700154 {
Jason Sams110f1812013-03-14 16:02:18 -0700155 NATIVE_FUNC(rsdFrameBufferInit),
156 NATIVE_FUNC(rsdFrameBufferSetActive),
157 NATIVE_FUNC(rsdFrameBufferDestroy)
Alex Sakhartchoukda6d34a2011-05-13 14:53:34 -0700158 },
159
Jason Samsdbe66d62012-09-17 13:54:41 -0700160 {
161 rsdScriptGroupInit,
162 rsdScriptGroupSetInput,
163 rsdScriptGroupSetOutput,
164 rsdScriptGroupExecute,
165 rsdScriptGroupDestroy
Jason Samsf2748272013-11-26 18:10:59 -0800166 },
Jason Samsdbe66d62012-09-17 13:54:41 -0700167
Jason Samsf2748272013-11-26 18:10:59 -0800168 NULL // finish
Jason Samsbad80742011-03-16 16:29:28 -0700169};
170
Jason Sams709a0972012-11-15 18:18:04 -0800171extern const RsdCpuReference::CpuSymbol * rsdLookupRuntimeStub(Context * pContext, char const* name);
Jason Samsbad80742011-03-16 16:29:28 -0700172
Jason Sams709a0972012-11-15 18:18:04 -0800173static RsdCpuReference::CpuScript * LookupScript(Context *, const Script *s) {
174 return (RsdCpuReference::CpuScript *)s->mHal.drv;
Jason Samscdfdb8f2011-03-17 16:12:47 -0700175}
176
Stephen Hines414a4612012-09-05 18:05:08 -0700177extern "C" bool rsdHalInit(RsContext c, uint32_t version_major,
178 uint32_t version_minor) {
179 Context *rsc = (Context*) c;
Jason Samsbad80742011-03-16 16:29:28 -0700180 rsc->mHal.funcs = FunctionTable;
181
Jason Sams87fe59a2011-04-20 15:09:01 -0700182 RsdHal *dc = (RsdHal *)calloc(1, sizeof(RsdHal));
Jason Sams2cfe51e2011-03-18 17:08:54 -0700183 if (!dc) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000184 ALOGE("Calloc for driver hal failed.");
Jason Samsbad80742011-03-16 16:29:28 -0700185 return false;
186 }
Jason Samscdfdb8f2011-03-17 16:12:47 -0700187 rsc->mHal.drv = dc;
Jason Samsbad80742011-03-16 16:29:28 -0700188
Stephen Hinesa221f562013-07-10 17:59:47 -0700189 dc->mCpuRef = RsdCpuReference::create(rsc, version_major, version_minor,
Jason Sams709a0972012-11-15 18:18:04 -0800190 &rsdLookupRuntimeStub, &LookupScript);
191 if (!dc->mCpuRef) {
192 ALOGE("RsdCpuReference::create for driver hal failed.");
193 free(dc);
Jason Samscdfdb8f2011-03-17 16:12:47 -0700194 return false;
195 }
196
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700197#ifndef RS_COMPATIBILITY_LIB
198 // Set a callback for compiler setup here.
199 if (false) {
200 dc->mCpuRef->setSetupCompilerCallback(NULL);
201 }
Stephen Hinesa221f562013-07-10 17:59:47 -0700202
203 // Set a callback for switching MemChunk's allocator here.
204 // Note that the allocation function must return page-aligned memory, so
205 // that it can be mprotected properly (i.e. code should be written and
206 // later switched to read+execute only).
207 if (false) {
208 MemChunk::registerAllocFreeCallbacks(
209 rsc->mHal.funcs.allocRuntimeMem,
210 rsc->mHal.funcs.freeRuntimeMem);
211 }
Stephen Hinesb7d9c802013-04-29 19:13:09 -0700212#endif
213
Jason Samsbad80742011-03-16 16:29:28 -0700214 return true;
215}
216
Jason Samscdfdb8f2011-03-17 16:12:47 -0700217
218void SetPriority(const Context *rsc, int32_t priority) {
Jason Sams87fe59a2011-04-20 15:09:01 -0700219 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
Jason Sams709a0972012-11-15 18:18:04 -0800220
221 dc->mCpuRef->setPriority(priority);
222
Jason Sams110f1812013-03-14 16:02:18 -0700223#ifndef RS_COMPATIBILITY_LIB
Jason Sams9719bd42012-01-12 14:22:21 -0800224 if (dc->mHasGraphics) {
225 rsdGLSetPriority(rsc, priority);
226 }
Jason Sams110f1812013-03-14 16:02:18 -0700227#endif
Jason Samscdfdb8f2011-03-17 16:12:47 -0700228}
229
230void Shutdown(Context *rsc) {
Jason Sams87fe59a2011-04-20 15:09:01 -0700231 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
Jason Sams709a0972012-11-15 18:18:04 -0800232 delete dc->mCpuRef;
233 rsc->mHal.drv = NULL;
Jason Samscdfdb8f2011-03-17 16:12:47 -0700234}
235
Tim Murray34689382013-03-11 12:12:03 -0700236void* rsdAllocRuntimeMem(size_t size, uint32_t flags) {
237 void* buffer = calloc(size, sizeof(char));
238 return buffer;
239}
240
241void rsdFreeRuntimeMem(void* ptr) {
242 free(ptr);
243}