Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 1 | /* |
Stephen Hines | ee7aa2e | 2012-01-12 18:56:23 -0800 | [diff] [blame] | 2 | * Copyright (C) 2011-2012 The Android Open Source Project |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 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 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 17 | #include "../cpu_ref/rsd_cpu.h" |
| 18 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 19 | #include "rsdCore.h" |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 20 | #include "rsdAllocation.h" |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 21 | #include "rsdBcc.h" |
Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 22 | #ifndef RS_COMPATIBILITY_LIB |
Stephen Hines | a221f56 | 2013-07-10 17:59:47 -0700 | [diff] [blame] | 23 | #include "MemChunk.h" |
Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 24 | #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 Sakhartchouk | 7f126c7 | 2011-05-05 16:56:27 -0700 | [diff] [blame] | 33 | #include "rsdSampler.h" |
Jason Sams | dbe66d6 | 2012-09-17 13:54:41 -0700 | [diff] [blame] | 34 | #include "rsdScriptGroup.h" |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 35 | |
| 36 | #include <malloc.h> |
| 37 | #include "rsContext.h" |
| 38 | |
Jason Sams | cdfdb8f | 2011-03-17 16:12:47 -0700 | [diff] [blame] | 39 | #include <sys/types.h> |
| 40 | #include <sys/resource.h> |
| 41 | #include <sched.h> |
Jason Sams | cdfdb8f | 2011-03-17 16:12:47 -0700 | [diff] [blame] | 42 | #include <sys/syscall.h> |
| 43 | #include <string.h> |
| 44 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 45 | using namespace android; |
| 46 | using namespace android::renderscript; |
| 47 | |
Jason Sams | cdfdb8f | 2011-03-17 16:12:47 -0700 | [diff] [blame] | 48 | static void Shutdown(Context *rsc); |
| 49 | static void SetPriority(const Context *rsc, int32_t priority); |
| 50 | |
Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 51 | #ifndef RS_COMPATIBILITY_LIB |
| 52 | #define NATIVE_FUNC(a) a |
| 53 | #else |
| 54 | #define NATIVE_FUNC(a) NULL |
| 55 | #endif |
| 56 | |
| 57 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 58 | static RsdHalFunctions FunctionTable = { |
Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 59 | NATIVE_FUNC(rsdGLInit), |
| 60 | NATIVE_FUNC(rsdGLShutdown), |
| 61 | NATIVE_FUNC(rsdGLSetSurface), |
| 62 | NATIVE_FUNC(rsdGLSwap), |
Jason Sams | 4b3de47 | 2011-04-06 17:52:23 -0700 | [diff] [blame] | 63 | |
Jason Sams | cdfdb8f | 2011-03-17 16:12:47 -0700 | [diff] [blame] | 64 | Shutdown, |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 65 | NULL, |
Jason Sams | cdfdb8f | 2011-03-17 16:12:47 -0700 | [diff] [blame] | 66 | SetPriority, |
Tim Murray | 3468938 | 2013-03-11 12:12:03 -0700 | [diff] [blame] | 67 | rsdAllocRuntimeMem, |
| 68 | rsdFreeRuntimeMem, |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 69 | { |
| 70 | rsdScriptInit, |
Jason Sams | 8eaba4f | 2012-08-14 14:38:05 -0700 | [diff] [blame] | 71 | rsdInitIntrinsic, |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 72 | rsdScriptInvokeFunction, |
| 73 | rsdScriptInvokeRoot, |
Jason Sams | cdfdb8f | 2011-03-17 16:12:47 -0700 | [diff] [blame] | 74 | rsdScriptInvokeForEach, |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 75 | rsdScriptInvokeInit, |
Stephen Hines | 4ee16ff | 2011-08-31 17:41:39 -0700 | [diff] [blame] | 76 | rsdScriptInvokeFreeChildren, |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 77 | rsdScriptSetGlobalVar, |
Tim Murray | 9c64239 | 2013-04-11 13:29:59 -0700 | [diff] [blame] | 78 | rsdScriptGetGlobalVar, |
Stephen Hines | 2980f07 | 2012-04-09 18:26:29 -0700 | [diff] [blame] | 79 | rsdScriptSetGlobalVarWithElemDims, |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 80 | rsdScriptSetGlobalBind, |
| 81 | rsdScriptSetGlobalObj, |
| 82 | rsdScriptDestroy |
Jason Sams | 8feea4e | 2011-03-18 15:03:25 -0700 | [diff] [blame] | 83 | }, |
| 84 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 85 | { |
| 86 | rsdAllocationInit, |
| 87 | rsdAllocationDestroy, |
Jason Sams | ddceab9 | 2013-08-07 13:02:32 -0700 | [diff] [blame] | 88 | rsdAllocationGrallocBits, |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 89 | rsdAllocationResize, |
| 90 | rsdAllocationSyncAll, |
| 91 | rsdAllocationMarkDirty, |
Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 92 | NATIVE_FUNC(rsdAllocationSetSurface), |
| 93 | NATIVE_FUNC(rsdAllocationIoSend), |
| 94 | NATIVE_FUNC(rsdAllocationIoReceive), |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 95 | rsdAllocationData1D, |
| 96 | rsdAllocationData2D, |
| 97 | rsdAllocationData3D, |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 98 | rsdAllocationRead1D, |
| 99 | rsdAllocationRead2D, |
| 100 | rsdAllocationRead3D, |
| 101 | rsdAllocationLock1D, |
| 102 | rsdAllocationUnlock1D, |
Alex Sakhartchouk | 74a8279 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 103 | rsdAllocationData1D_alloc, |
| 104 | rsdAllocationData2D_alloc, |
| 105 | rsdAllocationData3D_alloc, |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 106 | rsdAllocationElementData1D, |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 107 | rsdAllocationElementData2D, |
| 108 | rsdAllocationGenerateMipmaps |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 109 | }, |
| 110 | |
Jason Sams | 8feea4e | 2011-03-18 15:03:25 -0700 | [diff] [blame] | 111 | |
| 112 | { |
Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 113 | NATIVE_FUNC(rsdProgramStoreInit), |
| 114 | NATIVE_FUNC(rsdProgramStoreSetActive), |
| 115 | NATIVE_FUNC(rsdProgramStoreDestroy) |
Jason Sams | 721acc4 | 2011-04-06 11:23:54 -0700 | [diff] [blame] | 116 | }, |
| 117 | |
| 118 | { |
Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 119 | NATIVE_FUNC(rsdProgramRasterInit), |
| 120 | NATIVE_FUNC(rsdProgramRasterSetActive), |
| 121 | NATIVE_FUNC(rsdProgramRasterDestroy) |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 122 | }, |
| 123 | |
| 124 | { |
Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 125 | NATIVE_FUNC(rsdProgramVertexInit), |
| 126 | NATIVE_FUNC(rsdProgramVertexSetActive), |
| 127 | NATIVE_FUNC(rsdProgramVertexDestroy) |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 128 | }, |
| 129 | |
| 130 | { |
Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 131 | NATIVE_FUNC(rsdProgramFragmentInit), |
| 132 | NATIVE_FUNC(rsdProgramFragmentSetActive), |
| 133 | NATIVE_FUNC(rsdProgramFragmentDestroy) |
Alex Sakhartchouk | a04e30d | 2011-04-29 16:49:08 -0700 | [diff] [blame] | 134 | }, |
| 135 | |
| 136 | { |
Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 137 | NATIVE_FUNC(rsdMeshInit), |
| 138 | NATIVE_FUNC(rsdMeshDraw), |
| 139 | NATIVE_FUNC(rsdMeshDestroy) |
Alex Sakhartchouk | 7f126c7 | 2011-05-05 16:56:27 -0700 | [diff] [blame] | 140 | }, |
| 141 | |
| 142 | { |
Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 143 | NATIVE_FUNC(rsdPathInitStatic), |
| 144 | NATIVE_FUNC(rsdPathInitDynamic), |
| 145 | NATIVE_FUNC(rsdPathDraw), |
| 146 | NATIVE_FUNC(rsdPathDestroy) |
Jason Sams | 9e0afb5 | 2011-10-31 13:23:43 -0700 | [diff] [blame] | 147 | }, |
| 148 | |
| 149 | { |
Alex Sakhartchouk | 7f126c7 | 2011-05-05 16:56:27 -0700 | [diff] [blame] | 150 | rsdSamplerInit, |
| 151 | rsdSamplerDestroy |
| 152 | }, |
Jason Sams | 8feea4e | 2011-03-18 15:03:25 -0700 | [diff] [blame] | 153 | |
Alex Sakhartchouk | da6d34a | 2011-05-13 14:53:34 -0700 | [diff] [blame] | 154 | { |
Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 155 | NATIVE_FUNC(rsdFrameBufferInit), |
| 156 | NATIVE_FUNC(rsdFrameBufferSetActive), |
| 157 | NATIVE_FUNC(rsdFrameBufferDestroy) |
Alex Sakhartchouk | da6d34a | 2011-05-13 14:53:34 -0700 | [diff] [blame] | 158 | }, |
| 159 | |
Jason Sams | dbe66d6 | 2012-09-17 13:54:41 -0700 | [diff] [blame] | 160 | { |
| 161 | rsdScriptGroupInit, |
| 162 | rsdScriptGroupSetInput, |
| 163 | rsdScriptGroupSetOutput, |
| 164 | rsdScriptGroupExecute, |
| 165 | rsdScriptGroupDestroy |
Jason Sams | f274827 | 2013-11-26 18:10:59 -0800 | [diff] [blame^] | 166 | }, |
Jason Sams | dbe66d6 | 2012-09-17 13:54:41 -0700 | [diff] [blame] | 167 | |
Jason Sams | f274827 | 2013-11-26 18:10:59 -0800 | [diff] [blame^] | 168 | NULL // finish |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 171 | extern const RsdCpuReference::CpuSymbol * rsdLookupRuntimeStub(Context * pContext, char const* name); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 172 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 173 | static RsdCpuReference::CpuScript * LookupScript(Context *, const Script *s) { |
| 174 | return (RsdCpuReference::CpuScript *)s->mHal.drv; |
Jason Sams | cdfdb8f | 2011-03-17 16:12:47 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Stephen Hines | 414a461 | 2012-09-05 18:05:08 -0700 | [diff] [blame] | 177 | extern "C" bool rsdHalInit(RsContext c, uint32_t version_major, |
| 178 | uint32_t version_minor) { |
| 179 | Context *rsc = (Context*) c; |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 180 | rsc->mHal.funcs = FunctionTable; |
| 181 | |
Jason Sams | 87fe59a | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 182 | RsdHal *dc = (RsdHal *)calloc(1, sizeof(RsdHal)); |
Jason Sams | 2cfe51e | 2011-03-18 17:08:54 -0700 | [diff] [blame] | 183 | if (!dc) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 184 | ALOGE("Calloc for driver hal failed."); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 185 | return false; |
| 186 | } |
Jason Sams | cdfdb8f | 2011-03-17 16:12:47 -0700 | [diff] [blame] | 187 | rsc->mHal.drv = dc; |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 188 | |
Stephen Hines | a221f56 | 2013-07-10 17:59:47 -0700 | [diff] [blame] | 189 | dc->mCpuRef = RsdCpuReference::create(rsc, version_major, version_minor, |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 190 | &rsdLookupRuntimeStub, &LookupScript); |
| 191 | if (!dc->mCpuRef) { |
| 192 | ALOGE("RsdCpuReference::create for driver hal failed."); |
| 193 | free(dc); |
Jason Sams | cdfdb8f | 2011-03-17 16:12:47 -0700 | [diff] [blame] | 194 | return false; |
| 195 | } |
| 196 | |
Stephen Hines | b7d9c80 | 2013-04-29 19:13:09 -0700 | [diff] [blame] | 197 | #ifndef RS_COMPATIBILITY_LIB |
| 198 | // Set a callback for compiler setup here. |
| 199 | if (false) { |
| 200 | dc->mCpuRef->setSetupCompilerCallback(NULL); |
| 201 | } |
Stephen Hines | a221f56 | 2013-07-10 17:59:47 -0700 | [diff] [blame] | 202 | |
| 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 Hines | b7d9c80 | 2013-04-29 19:13:09 -0700 | [diff] [blame] | 212 | #endif |
| 213 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 214 | return true; |
| 215 | } |
| 216 | |
Jason Sams | cdfdb8f | 2011-03-17 16:12:47 -0700 | [diff] [blame] | 217 | |
| 218 | void SetPriority(const Context *rsc, int32_t priority) { |
Jason Sams | 87fe59a | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 219 | RsdHal *dc = (RsdHal *)rsc->mHal.drv; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 220 | |
| 221 | dc->mCpuRef->setPriority(priority); |
| 222 | |
Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 223 | #ifndef RS_COMPATIBILITY_LIB |
Jason Sams | 9719bd4 | 2012-01-12 14:22:21 -0800 | [diff] [blame] | 224 | if (dc->mHasGraphics) { |
| 225 | rsdGLSetPriority(rsc, priority); |
| 226 | } |
Jason Sams | 110f181 | 2013-03-14 16:02:18 -0700 | [diff] [blame] | 227 | #endif |
Jason Sams | cdfdb8f | 2011-03-17 16:12:47 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void Shutdown(Context *rsc) { |
Jason Sams | 87fe59a | 2011-04-20 15:09:01 -0700 | [diff] [blame] | 231 | RsdHal *dc = (RsdHal *)rsc->mHal.drv; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 232 | delete dc->mCpuRef; |
| 233 | rsc->mHal.drv = NULL; |
Jason Sams | cdfdb8f | 2011-03-17 16:12:47 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Tim Murray | 3468938 | 2013-03-11 12:12:03 -0700 | [diff] [blame] | 236 | void* rsdAllocRuntimeMem(size_t size, uint32_t flags) { |
| 237 | void* buffer = calloc(size, sizeof(char)); |
| 238 | return buffer; |
| 239 | } |
| 240 | |
| 241 | void rsdFreeRuntimeMem(void* ptr) { |
| 242 | free(ptr); |
| 243 | } |