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 #include "rsdElement.h"
23 #include "rsdType.h"
24 #ifndef RS_COMPATIBILITY_LIB
25     #include "rsdGL.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 #else
33     #include <dlfcn.h>
34 #endif
35 #include "rsdSampler.h"
36 #include "rsdScriptGroup.h"
37 
38 #include <malloc.h>
39 #include "rsContext.h"
40 
41 #include <sys/types.h>
42 #include <sys/resource.h>
43 #include <sched.h>
44 #include <sys/syscall.h>
45 #include <string.h>
46 
47 using namespace android;
48 using namespace android::renderscript;
49 
50 static void Shutdown(Context *rsc);
51 static void SetPriority(const Context *rsc, int32_t priority);
52 
53 #ifndef RS_COMPATIBILITY_LIB
54     #define NATIVE_FUNC(a) a
55 #else
56     #define NATIVE_FUNC(a) nullptr
57 #endif
58 
rsdHalQueryHal(RsHalInitEnums entry,void ** fnPtr)59 extern "C" bool rsdHalQueryHal(RsHalInitEnums entry, void **fnPtr) {
60     switch(entry) {
61     case RS_HAL_CORE_SHUTDOWN:
62         fnPtr[0] = (void *)Shutdown; break;
63     case RS_HAL_CORE_SET_PRIORITY:
64         fnPtr[0] = (void *)SetPriority; break;
65     case RS_HAL_CORE_ALLOC_RUNTIME_MEM:
66         fnPtr[0] = (void *)rsdAllocRuntimeMem; break;
67     case RS_HAL_CORE_FREE_RUNTIME_MEM:
68         fnPtr[0] = (void *)rsdFreeRuntimeMem; break;
69     case RS_HAL_CORE_FINISH:
70         fnPtr[0] = (void *)nullptr; break;
71 
72     case RS_HAL_SCRIPT_INIT:
73         fnPtr[0] = (void *)rsdScriptInit; break;
74     case RS_HAL_SCRIPT_INIT_INTRINSIC:
75         fnPtr[0] = (void *)rsdInitIntrinsic; break;
76     case RS_HAL_SCRIPT_INVOKE_FUNCTION:
77         fnPtr[0] = (void *)rsdScriptInvokeFunction; break;
78     case RS_HAL_SCRIPT_INVOKE_ROOT:
79         fnPtr[0] = (void *)rsdScriptInvokeRoot; break;
80     case RS_HAL_SCRIPT_INVOKE_FOR_EACH:
81         fnPtr[0] = (void *)rsdScriptInvokeForEach; break;
82     case RS_HAL_SCRIPT_INVOKE_INIT:
83         fnPtr[0] = (void *)rsdScriptInvokeInit; break;
84     case RS_HAL_SCRIPT_INVOKE_FREE_CHILDREN:
85         fnPtr[0] = (void *)rsdScriptInvokeFreeChildren; break;
86     case RS_HAL_SCRIPT_SET_GLOBAL_VAR:
87         fnPtr[0] = (void *)rsdScriptSetGlobalVar; break;
88     case RS_HAL_SCRIPT_GET_GLOBAL_VAR:
89         fnPtr[0] = (void *)rsdScriptGetGlobalVar; break;
90     case RS_HAL_SCRIPT_SET_GLOBAL_VAR_WITH_ELEMENT_DIM:
91         fnPtr[0] = (void *)rsdScriptSetGlobalVarWithElemDims; break;
92     case RS_HAL_SCRIPT_SET_GLOBAL_BIND:
93         fnPtr[0] = (void *)rsdScriptSetGlobalBind; break;
94     case RS_HAL_SCRIPT_SET_GLOBAL_OBJECT:
95         fnPtr[0] = (void *)rsdScriptSetGlobalObj; break;
96     case RS_HAL_SCRIPT_DESTROY:
97         fnPtr[0] = (void *)rsdScriptDestroy; break;
98     case RS_HAL_SCRIPT_INVOKE_FOR_EACH_MULTI:
99         fnPtr[0] = (void *)rsdScriptInvokeForEachMulti; break;
100     case RS_HAL_SCRIPT_UPDATE_CACHED_OBJECT:
101         fnPtr[0] = (void *)rsdScriptUpdateCachedObject; break;
102 
103     case RS_HAL_ALLOCATION_INIT:
104         fnPtr[0] = (void *)rsdAllocationInit; break;
105     case RS_HAL_ALLOCATION_INIT_OEM:
106         fnPtr[0] = (void *)nullptr; break;
107     case RS_HAL_ALLOCATION_INIT_ADAPTER:
108         fnPtr[0] = (void *)rsdAllocationAdapterInit; break;
109     case RS_HAL_ALLOCATION_DESTROY:
110         fnPtr[0] = (void *)rsdAllocationDestroy; break;
111     case RS_HAL_ALLOCATION_GET_GRALLOC_BITS:
112         fnPtr[0] = (void *)rsdAllocationGrallocBits; break;
113     case RS_HAL_ALLOCATION_RESIZE:
114         fnPtr[0] = (void *)rsdAllocationResize; break;
115     case RS_HAL_ALLOCATION_SYNC_ALL:
116         fnPtr[0] = (void *)rsdAllocationSyncAll; break;
117     case RS_HAL_ALLOCATION_MARK_DIRTY:
118         fnPtr[0] = (void *)rsdAllocationMarkDirty; break;
119     case RS_HAL_ALLOCATION_SET_SURFACE:
120         fnPtr[0] = (void *)NATIVE_FUNC(rsdAllocationSetSurface); break;
121     case RS_HAL_ALLOCATION_IO_SEND:
122         fnPtr[0] = (void *)NATIVE_FUNC(rsdAllocationIoSend); break;
123     case RS_HAL_ALLOCATION_IO_RECEIVE:
124         fnPtr[0] = (void *)NATIVE_FUNC(rsdAllocationIoReceive); break;
125     case RS_HAL_ALLOCATION_DATA_1D:
126         fnPtr[0] = (void *)rsdAllocationData1D; break;
127     case RS_HAL_ALLOCATION_DATA_2D:
128         fnPtr[0] = (void *)rsdAllocationData2D; break;
129     case RS_HAL_ALLOCATION_DATA_3D:
130         fnPtr[0] = (void *)rsdAllocationData3D; break;
131     case RS_HAL_ALLOCATION_READ_1D:
132         fnPtr[0] = (void *)rsdAllocationRead1D; break;
133     case RS_HAL_ALLOCATION_READ_2D:
134         fnPtr[0] = (void *)rsdAllocationRead2D; break;
135     case RS_HAL_ALLOCATION_READ_3D:
136         fnPtr[0] = (void *)rsdAllocationRead3D; break;
137     case RS_HAL_ALLOCATION_LOCK_1D:
138         fnPtr[0] = (void *)rsdAllocationLock1D; break;
139     case RS_HAL_ALLOCATION_UNLOCK_1D:
140         fnPtr[0] = (void *)rsdAllocationUnlock1D; break;
141     case RS_HAL_ALLOCATION_COPY_1D:
142         fnPtr[0] = (void *)rsdAllocationData1D_alloc; break;
143     case RS_HAL_ALLOCATION_COPY_2D:
144         fnPtr[0] = (void *)rsdAllocationData2D_alloc; break;
145     case RS_HAL_ALLOCATION_COPY_3D:
146         fnPtr[0] = (void *)rsdAllocationData3D_alloc; break;
147     case RS_HAL_ALLOCATION_ELEMENT_DATA:
148         fnPtr[0] = (void *)rsdAllocationElementData; break;
149     case RS_HAL_ALLOCATION_ELEMENT_READ:
150         fnPtr[0] = (void *)rsdAllocationElementRead; break;
151     case RS_HAL_ALLOCATION_GENERATE_MIPMAPS:
152         fnPtr[0] = (void *)rsdAllocationGenerateMipmaps; break;
153     case RS_HAL_ALLOCATION_UPDATE_CACHED_OBJECT:
154         fnPtr[0] = (void *)rsdAllocationUpdateCachedObject; break;
155     case RS_HAL_ALLOCATION_ADAPTER_OFFSET:
156         fnPtr[0] = (void *)rsdAllocationAdapterOffset; break;
157     case RS_HAL_ALLOCATION_GET_POINTER:
158         fnPtr[0] = (void *)nullptr; break;
159 
160     case RS_HAL_SAMPLER_INIT:
161         fnPtr[0] = (void *)rsdSamplerInit; break;
162     case RS_HAL_SAMPLER_DESTROY:
163         fnPtr[0] = (void *)rsdSamplerDestroy; break;
164     case RS_HAL_SAMPLER_UPDATE_CACHED_OBJECT:
165         fnPtr[0] = (void *)rsdSamplerUpdateCachedObject; break;
166 
167     case RS_HAL_TYPE_INIT:
168         fnPtr[0] = (void *)rsdTypeInit; break;
169     case RS_HAL_TYPE_DESTROY:
170         fnPtr[0] = (void *)rsdTypeDestroy; break;
171     case RS_HAL_TYPE_UPDATE_CACHED_OBJECT:
172         fnPtr[0] = (void *)rsdTypeUpdateCachedObject; break;
173 
174     case RS_HAL_ELEMENT_INIT:
175         fnPtr[0] = (void *)rsdElementInit; break;
176     case RS_HAL_ELEMENT_DESTROY:
177         fnPtr[0] = (void *)rsdElementDestroy; break;
178     case RS_HAL_ELEMENT_UPDATE_CACHED_OBJECT:
179         fnPtr[0] = (void *)rsdElementUpdateCachedObject; break;
180 
181     case RS_HAL_SCRIPT_GROUP_INIT:
182         fnPtr[0] = (void *)rsdScriptGroupInit; break;
183     case RS_HAL_SCRIPT_GROUP_DESTROY:
184         fnPtr[0] = (void *)rsdScriptGroupDestroy; break;
185     case RS_HAL_SCRIPT_GROUP_UPDATE_CACHED_OBJECT:
186         fnPtr[0] = (void *)nullptr; break;
187     case RS_HAL_SCRIPT_GROUP_SET_INPUT:
188         fnPtr[0] = (void *)rsdScriptGroupSetInput; break;
189     case RS_HAL_SCRIPT_GROUP_SET_OUTPUT:
190         fnPtr[0] = (void *)rsdScriptGroupSetOutput; break;
191     case RS_HAL_SCRIPT_GROUP_EXECUTE:
192         fnPtr[0] = (void *)rsdScriptGroupExecute; break;
193 
194 
195 
196     // Functions below this point are for the legacy graphics api,
197     // vendor drivers are NOT expected to implement these.  They will never be called
198     // for an external driver.
199 #ifndef RS_COMPATIBILITY_LIB
200     case RS_HAL_GRAPHICS_INIT:
201         fnPtr[0] = (void *)rsdGLInit; break;
202     case RS_HAL_GRAPHICS_SHUTDOWN:
203         fnPtr[0] = (void *)rsdGLShutdown; break;
204     case RS_HAL_GRAPHICS_SWAP:
205         fnPtr[0] = (void *)rsdGLSwap; break;
206     case RS_HAL_GRAPHICS_SET_SURFACE:
207         fnPtr[0] = (void *)rsdGLSetSurface; break;
208     case RS_HAL_GRAPHICS_RASTER_INIT:
209         fnPtr[0] = (void *)rsdProgramRasterInit; break;
210     case RS_HAL_GRAPHICS_RASTER_SET_ACTIVE:
211         fnPtr[0] = (void *)rsdProgramRasterSetActive; break;
212     case RS_HAL_GRAPHICS_RASTER_DESTROY:
213         fnPtr[0] = (void *)rsdProgramRasterDestroy; break;
214     case RS_HAL_GRAPHICS_VERTEX_INIT:
215         fnPtr[0] = (void *)rsdProgramVertexInit; break;
216     case RS_HAL_GRAPHICS_VERTEX_SET_ACTIVE:
217         fnPtr[0] = (void *)rsdProgramVertexSetActive; break;
218     case RS_HAL_GRAPHICS_VERTEX_DESTROY:
219         fnPtr[0] = (void *)rsdProgramVertexDestroy; break;
220     case RS_HAL_GRAPHICS_FRAGMENT_INIT:
221         fnPtr[0] = (void *)rsdProgramFragmentInit; break;
222     case RS_HAL_GRAPHICS_FRAGMENT_SET_ACTIVE:
223         fnPtr[0] = (void *)rsdProgramFragmentSetActive; break;
224     case RS_HAL_GRAPHICS_FRAGMENT_DESTROY:
225         fnPtr[0] = (void *)rsdProgramFragmentDestroy; break;
226     case RS_HAL_GRAPHICS_MESH_INIT:
227         fnPtr[0] = (void *)rsdMeshInit; break;
228     case RS_HAL_GRAPHICS_MESH_DRAW:
229         fnPtr[0] = (void *)rsdMeshDraw; break;
230     case RS_HAL_GRAPHICS_MESH_DESTROY:
231         fnPtr[0] = (void *)rsdMeshDestroy; break;
232     case RS_HAL_GRAPHICS_FB_INIT:
233         fnPtr[0] = (void *)rsdFrameBufferInit; break;
234     case RS_HAL_GRAPHICS_FB_SET_ACTIVE:
235         fnPtr[0] = (void *)rsdFrameBufferSetActive; break;
236     case RS_HAL_GRAPHICS_FB_DESTROY:
237         fnPtr[0] = (void *)rsdFrameBufferDestroy; break;
238     case RS_HAL_GRAPHICS_STORE_INIT:
239         fnPtr[0] = (void *)rsdProgramStoreInit; break;
240     case RS_HAL_GRAPHICS_STORE_SET_ACTIVE:
241         fnPtr[0] = (void *)rsdProgramStoreSetActive; break;
242     case RS_HAL_GRAPHICS_STORE_DESTROY:
243         fnPtr[0] = (void *)rsdProgramStoreDestroy; break;
244 #endif
245 
246     default:
247         ALOGE("ERROR: unknown RenderScript HAL API query, %i", entry);
248         return false;
249     }
250 
251     return true;
252 }
253 
rsdHalAbort(RsContext)254 extern "C" void rsdHalAbort(RsContext) {
255 
256 }
257 
258 
rsdHalQueryVersion(uint32_t * major,uint32_t * minor)259 extern "C" bool rsdHalQueryVersion(uint32_t *major, uint32_t *minor) {
260     *major = 23;
261     *minor = 0;
262     return true;
263 }
264 
265 
266 
267 extern const RsdCpuReference::CpuSymbol * rsdLookupRuntimeStub(Context * pContext, char const* name);
268 
LookupScript(Context *,const Script * s)269 static RsdCpuReference::CpuScript * LookupScript(Context *, const Script *s) {
270     return (RsdCpuReference::CpuScript *)s->mHal.drv;
271 }
272 
273 #ifdef RS_COMPATIBILITY_LIB
274 typedef void (*sAllocationDestroyFnPtr) (const Context *rsc, Allocation *alloc);
275 typedef void (*sAllocationIoSendFnPtr) (const Context *rsc, Allocation *alloc);
276 typedef void (*sAllocationSetSurfaceFnPtr) (const Context *rsc, Allocation *alloc, ANativeWindow *nw);
277 static sAllocationDestroyFnPtr sAllocationDestroy;
278 static sAllocationIoSendFnPtr sAllocationIoSend;
279 static sAllocationSetSurfaceFnPtr sAllocationSetSurface;
280 
loadIOSuppLibSyms()281 static bool loadIOSuppLibSyms() {
282     void* handleIO = nullptr;
283     handleIO = dlopen("libRSSupportIO.so", RTLD_LAZY | RTLD_LOCAL);
284     if (handleIO == nullptr) {
285         ALOGE("Couldn't load libRSSupportIO.so");
286         return false;
287     }
288     sAllocationDestroy = (sAllocationDestroyFnPtr)dlsym(handleIO, "rscAllocationDestroy");
289     if (sAllocationDestroy==nullptr) {
290         ALOGE("Failed to initialize sAllocationDestroy");
291         return false;
292     }
293     sAllocationIoSend = (sAllocationIoSendFnPtr)dlsym(handleIO, "rscAllocationIoSend");
294     if (sAllocationIoSend==nullptr) {
295         ALOGE("Failed to initialize sAllocationIoSend");
296         return false;
297     }
298     sAllocationSetSurface = (sAllocationSetSurfaceFnPtr)dlsym(handleIO, "rscAllocationSetSurface");
299     if (sAllocationSetSurface==nullptr) {
300         ALOGE("Failed to initialize sAllocationIoSend");
301         return false;
302     }
303     return true;
304 }
305 #endif
306 
rsdHalInit(RsContext c,uint32_t version_major,uint32_t version_minor)307 extern "C" bool rsdHalInit(RsContext c, uint32_t version_major,
308                            uint32_t version_minor) {
309     Context *rsc = (Context*) c;
310 #ifdef RS_COMPATIBILITY_LIB
311     if (loadIOSuppLibSyms()) {
312         rsc->mHal.funcs.allocation.destroy = sAllocationDestroy;
313         rsc->mHal.funcs.allocation.ioSend = sAllocationIoSend;
314         rsc->mHal.funcs.allocation.setSurface = sAllocationSetSurface;
315     }
316 #endif
317 
318     RsdHal *dc = (RsdHal *)calloc(1, sizeof(RsdHal));
319     if (!dc) {
320         ALOGE("Calloc for driver hal failed.");
321         return false;
322     }
323     rsc->mHal.drv = dc;
324 
325     dc->mCpuRef = RsdCpuReference::create(rsc, version_major, version_minor,
326                                           &rsdLookupRuntimeStub, &LookupScript);
327     if (!dc->mCpuRef) {
328         ALOGE("RsdCpuReference::create for driver hal failed.");
329         rsc->mHal.drv = nullptr;
330         free(dc);
331         return false;
332     }
333 
334 #ifndef RS_COMPATIBILITY_LIB
335     // Set a callback for compiler setup here.
336     if (false) {
337         dc->mCpuRef->setSetupCompilerCallback(nullptr);
338     }
339 #endif
340 
341     return true;
342 }
343 
344 
SetPriority(const Context * rsc,int32_t priority)345 void SetPriority(const Context *rsc, int32_t priority) {
346     RsdHal *dc = (RsdHal *)rsc->mHal.drv;
347 
348     dc->mCpuRef->setPriority(priority);
349 
350 #ifndef RS_COMPATIBILITY_LIB
351     if (dc->mHasGraphics) {
352         rsdGLSetPriority(rsc, priority);
353     }
354 #endif
355 }
356 
Shutdown(Context * rsc)357 void Shutdown(Context *rsc) {
358     RsdHal *dc = (RsdHal *)rsc->mHal.drv;
359     delete dc->mCpuRef;
360     free(dc);
361     rsc->mHal.drv = nullptr;
362 }
363 
rsdAllocRuntimeMem(size_t size,uint32_t flags)364 void* rsdAllocRuntimeMem(size_t size, uint32_t flags) {
365     void* buffer = calloc(size, sizeof(char));
366     return buffer;
367 }
368 
rsdFreeRuntimeMem(void * ptr)369 void rsdFreeRuntimeMem(void* ptr) {
370     free(ptr);
371 }
372