1 /* 2 * Copyright (C) 2011-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 "../cpu_ref/rsd_cpu.h" 18 19 #include "rsdCore.h" 20 #include "rsdAllocation.h" 21 #include "rsdBcc.h" 22 #ifndef RS_COMPATIBILITY_LIB 23 #include "MemChunk.h" 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 33 #include "rsdSampler.h" 34 #include "rsdScriptGroup.h" 35 36 #include <malloc.h> 37 #include "rsContext.h" 38 39 #include <sys/types.h> 40 #include <sys/resource.h> 41 #include <sched.h> 42 #include <sys/syscall.h> 43 #include <string.h> 44 45 using namespace android; 46 using namespace android::renderscript; 47 48 static void Shutdown(Context *rsc); 49 static void SetPriority(const Context *rsc, int32_t priority); 50 51 #ifndef RS_COMPATIBILITY_LIB 52 #define NATIVE_FUNC(a) a 53 #else 54 #define NATIVE_FUNC(a) NULL 55 #endif 56 57 58 static RsdHalFunctions FunctionTable = { 59 NATIVE_FUNC(rsdGLInit), 60 NATIVE_FUNC(rsdGLShutdown), 61 NATIVE_FUNC(rsdGLSetSurface), 62 NATIVE_FUNC(rsdGLSwap), 63 64 Shutdown, 65 NULL, 66 SetPriority, 67 rsdAllocRuntimeMem, 68 rsdFreeRuntimeMem, 69 { 70 rsdScriptInit, 71 rsdInitIntrinsic, 72 rsdScriptInvokeFunction, 73 rsdScriptInvokeRoot, 74 rsdScriptInvokeForEach, 75 rsdScriptInvokeInit, 76 rsdScriptInvokeFreeChildren, 77 rsdScriptSetGlobalVar, 78 rsdScriptGetGlobalVar, 79 rsdScriptSetGlobalVarWithElemDims, 80 rsdScriptSetGlobalBind, 81 rsdScriptSetGlobalObj, 82 rsdScriptDestroy 83 }, 84 85 { 86 rsdAllocationInit, 87 rsdAllocationDestroy, 88 rsdAllocationGrallocBits, 89 rsdAllocationResize, 90 rsdAllocationSyncAll, 91 rsdAllocationMarkDirty, 92 NATIVE_FUNC(rsdAllocationSetSurface), 93 NATIVE_FUNC(rsdAllocationIoSend), 94 NATIVE_FUNC(rsdAllocationIoReceive), 95 rsdAllocationData1D, 96 rsdAllocationData2D, 97 rsdAllocationData3D, 98 rsdAllocationRead1D, 99 rsdAllocationRead2D, 100 rsdAllocationRead3D, 101 rsdAllocationLock1D, 102 rsdAllocationUnlock1D, 103 rsdAllocationData1D_alloc, 104 rsdAllocationData2D_alloc, 105 rsdAllocationData3D_alloc, 106 rsdAllocationElementData1D, 107 rsdAllocationElementData2D, 108 rsdAllocationGenerateMipmaps 109 }, 110 111 112 { 113 NATIVE_FUNC(rsdProgramStoreInit), 114 NATIVE_FUNC(rsdProgramStoreSetActive), 115 NATIVE_FUNC(rsdProgramStoreDestroy) 116 }, 117 118 { 119 NATIVE_FUNC(rsdProgramRasterInit), 120 NATIVE_FUNC(rsdProgramRasterSetActive), 121 NATIVE_FUNC(rsdProgramRasterDestroy) 122 }, 123 124 { 125 NATIVE_FUNC(rsdProgramVertexInit), 126 NATIVE_FUNC(rsdProgramVertexSetActive), 127 NATIVE_FUNC(rsdProgramVertexDestroy) 128 }, 129 130 { 131 NATIVE_FUNC(rsdProgramFragmentInit), 132 NATIVE_FUNC(rsdProgramFragmentSetActive), 133 NATIVE_FUNC(rsdProgramFragmentDestroy) 134 }, 135 136 { 137 NATIVE_FUNC(rsdMeshInit), 138 NATIVE_FUNC(rsdMeshDraw), 139 NATIVE_FUNC(rsdMeshDestroy) 140 }, 141 142 { 143 NATIVE_FUNC(rsdPathInitStatic), 144 NATIVE_FUNC(rsdPathInitDynamic), 145 NATIVE_FUNC(rsdPathDraw), 146 NATIVE_FUNC(rsdPathDestroy) 147 }, 148 149 { 150 rsdSamplerInit, 151 rsdSamplerDestroy 152 }, 153 154 { 155 NATIVE_FUNC(rsdFrameBufferInit), 156 NATIVE_FUNC(rsdFrameBufferSetActive), 157 NATIVE_FUNC(rsdFrameBufferDestroy) 158 }, 159 160 { 161 rsdScriptGroupInit, 162 rsdScriptGroupSetInput, 163 rsdScriptGroupSetOutput, 164 rsdScriptGroupExecute, 165 rsdScriptGroupDestroy 166 } 167 168 169 }; 170 171 extern const RsdCpuReference::CpuSymbol * rsdLookupRuntimeStub(Context * pContext, char const* name); 172 173 static RsdCpuReference::CpuScript * LookupScript(Context *, const Script *s) { 174 return (RsdCpuReference::CpuScript *)s->mHal.drv; 175 } 176 177 extern "C" bool rsdHalInit(RsContext c, uint32_t version_major, 178 uint32_t version_minor) { 179 Context *rsc = (Context*) c; 180 rsc->mHal.funcs = FunctionTable; 181 182 RsdHal *dc = (RsdHal *)calloc(1, sizeof(RsdHal)); 183 if (!dc) { 184 ALOGE("Calloc for driver hal failed."); 185 return false; 186 } 187 rsc->mHal.drv = dc; 188 189 dc->mCpuRef = RsdCpuReference::create(rsc, version_major, version_minor, 190 &rsdLookupRuntimeStub, &LookupScript); 191 if (!dc->mCpuRef) { 192 ALOGE("RsdCpuReference::create for driver hal failed."); 193 free(dc); 194 return false; 195 } 196 197 #ifndef RS_COMPATIBILITY_LIB 198 // Set a callback for compiler setup here. 199 if (false) { 200 dc->mCpuRef->setSetupCompilerCallback(NULL); 201 } 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 } 212 #endif 213 214 return true; 215 } 216 217 218 void SetPriority(const Context *rsc, int32_t priority) { 219 RsdHal *dc = (RsdHal *)rsc->mHal.drv; 220 221 dc->mCpuRef->setPriority(priority); 222 223 #ifndef RS_COMPATIBILITY_LIB 224 if (dc->mHasGraphics) { 225 rsdGLSetPriority(rsc, priority); 226 } 227 #endif 228 } 229 230 void Shutdown(Context *rsc) { 231 RsdHal *dc = (RsdHal *)rsc->mHal.drv; 232 delete dc->mCpuRef; 233 rsc->mHal.drv = NULL; 234 } 235 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 } 244