10ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams/*
20ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams * Copyright (C) 2015 The Android Open Source Project
30ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams *
40ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams * Licensed under the Apache License, Version 2.0 (the "License");
50ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams * you may not use this file except in compliance with the License.
60ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams * You may obtain a copy of the License at
70ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams *
80ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams *      http://www.apache.org/licenses/LICENSE-2.0
90ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams *
100ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams * Unless required by applicable law or agreed to in writing, software
110ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams * distributed under the License is distributed on an "AS IS" BASIS,
120ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams * See the License for the specific language governing permissions and
140ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams * limitations under the License.
150ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams */
160ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
170ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#include "rs.h"
180ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#include "rsDevice.h"
190ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#include "rsContext.h"
200ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#include "rsThreadIO.h"
210ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
220ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#include "rsgApiStructs.h"
230ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
240ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#ifndef RS_COMPATIBILITY_LIB
250ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#include "rsMesh.h"
260ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#include <gui/DisplayEventReceiver.h>
270ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#endif
280ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
290ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#include <sys/types.h>
300ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#include <sys/resource.h>
310ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#include <sched.h>
320ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
330ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#include <sys/syscall.h>
340ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#include <string.h>
350ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#include <dlfcn.h>
360ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#include <inttypes.h>
370ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#include <unistd.h>
380ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
390ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB) && \
40e5e18cca3eefccf3a6aabd254ceb2c59f120a7d2Elliott Hughes        defined(__ANDROID__)
410ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#include <cutils/properties.h>
420ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#endif
430ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
440ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#ifdef RS_COMPATIBILITY_LIB
450ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#include "rsCompatibilityLib.h"
460ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#endif
470ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
480ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Samsusing namespace android;
490ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Samsusing namespace android::renderscript;
500ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
510ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
520ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
530ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Samstypedef bool (*HalQueryVersion)(uint32_t *version_major, uint32_t *version_minor);
540ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Samstypedef bool (*HalQueryHal)(android::renderscript::RsHalInitEnums entry, void **fnPtr);
550ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Samstypedef bool (*HalInit)(RsContext, uint32_t version_major, uint32_t version_minor);
560ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Samstypedef void (*HalAbort)(RsContext);
570ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
580ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
590ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Samsstatic bool LoadHalTable(Context *rsc, HalQueryHal fn, bool loadGraphics) {
600ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    bool ret = true;
610ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
620ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_CORE_SHUTDOWN, (void **)&rsc->mHal.funcs.shutdownDriver);
630ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_CORE_SET_PRIORITY, (void **)&rsc->mHal.funcs.setPriority);
640ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_CORE_ALLOC_RUNTIME_MEM, (void **)&rsc->mHal.funcs.allocRuntimeMem);
650ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_CORE_FREE_RUNTIME_MEM, (void **)&rsc->mHal.funcs.freeRuntimeMem);
660ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_CORE_FINISH, (void **)&rsc->mHal.funcs.finish);
670ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
680ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_INIT, (void **)&rsc->mHal.funcs.script.init);
690ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_INIT_INTRINSIC, (void **)&rsc->mHal.funcs.script.initIntrinsic);
700ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_INVOKE_FUNCTION, (void **)&rsc->mHal.funcs.script.invokeFunction);
710ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_INVOKE_ROOT, (void **)&rsc->mHal.funcs.script.invokeRoot);
720ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_INVOKE_FOR_EACH, (void **)&rsc->mHal.funcs.script.invokeForEach);
7314ce007a633b10e3b9a3fae29d8f53a7e8c9b59fMatt Wala    ret &= fn(RS_HAL_SCRIPT_INVOKE_REDUCE, (void **)&rsc->mHal.funcs.script.invokeReduce);
740ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_INVOKE_INIT, (void **)&rsc->mHal.funcs.script.invokeInit);
750ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_INVOKE_FREE_CHILDREN, (void **)&rsc->mHal.funcs.script.invokeFreeChildren);
760ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_SET_GLOBAL_VAR, (void **)&rsc->mHal.funcs.script.setGlobalVar);
770ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_GET_GLOBAL_VAR, (void **)&rsc->mHal.funcs.script.getGlobalVar);
780ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_SET_GLOBAL_VAR_WITH_ELEMENT_DIM, (void **)&rsc->mHal.funcs.script.setGlobalVarWithElemDims);
790ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_SET_GLOBAL_BIND, (void **)&rsc->mHal.funcs.script.setGlobalBind);
800ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_SET_GLOBAL_OBJECT, (void **)&rsc->mHal.funcs.script.setGlobalObj);
810ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_DESTROY, (void **)&rsc->mHal.funcs.script.destroy);
820ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_INVOKE_FOR_EACH_MULTI, (void **)&rsc->mHal.funcs.script.invokeForEachMulti);
830ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.script.updateCachedObject);
840ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
850ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_INIT, (void **)&rsc->mHal.funcs.allocation.init);
86f82b626e0479ce4a23ebff1fc088e073dcabaa30Jason Sams    ret &= fn(RS_HAL_ALLOCATION_INIT_OEM, (void **)&rsc->mHal.funcs.allocation.initOem);
870ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_INIT_ADAPTER, (void **)&rsc->mHal.funcs.allocation.initAdapter);
880ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_DESTROY, (void **)&rsc->mHal.funcs.allocation.destroy);
890ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_GET_GRALLOC_BITS, (void **)&rsc->mHal.funcs.allocation.grallocBits);
900ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_RESIZE, (void **)&rsc->mHal.funcs.allocation.resize);
910ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_SYNC_ALL, (void **)&rsc->mHal.funcs.allocation.syncAll);
920ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_MARK_DIRTY, (void **)&rsc->mHal.funcs.allocation.markDirty);
930ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_SET_SURFACE, (void **)&rsc->mHal.funcs.allocation.setSurface);
940ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_IO_SEND, (void **)&rsc->mHal.funcs.allocation.ioSend);
950ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_IO_RECEIVE, (void **)&rsc->mHal.funcs.allocation.ioReceive);
960ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_DATA_1D, (void **)&rsc->mHal.funcs.allocation.data1D);
970ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_DATA_2D, (void **)&rsc->mHal.funcs.allocation.data2D);
980ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_DATA_3D, (void **)&rsc->mHal.funcs.allocation.data3D);
990ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_READ_1D, (void **)&rsc->mHal.funcs.allocation.read1D);
1000ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_READ_2D, (void **)&rsc->mHal.funcs.allocation.read2D);
1010ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_READ_3D, (void **)&rsc->mHal.funcs.allocation.read3D);
1020ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_LOCK_1D, (void **)&rsc->mHal.funcs.allocation.lock1D);
1030ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_UNLOCK_1D, (void **)&rsc->mHal.funcs.allocation.unlock1D);
1040ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_COPY_1D, (void **)&rsc->mHal.funcs.allocation.allocData1D);
1050ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_COPY_2D, (void **)&rsc->mHal.funcs.allocation.allocData2D);
1060ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_COPY_3D, (void **)&rsc->mHal.funcs.allocation.allocData3D);
1070ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_ELEMENT_DATA, (void **)&rsc->mHal.funcs.allocation.elementData);
1080ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_ELEMENT_READ, (void **)&rsc->mHal.funcs.allocation.elementRead);
1090ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_GENERATE_MIPMAPS, (void **)&rsc->mHal.funcs.allocation.generateMipmaps);
1100ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.allocation.updateCachedObject);
1110ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ALLOCATION_ADAPTER_OFFSET, (void **)&rsc->mHal.funcs.allocation.adapterOffset);
1128ce12815675bfaeb2768959b092d6db293ba36c5Jason Sams    ret &= fn(RS_HAL_ALLOCATION_GET_POINTER, (void **)&rsc->mHal.funcs.allocation.getPointer);
11347a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang#ifdef RS_COMPATIBILITY_LIB
11447a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang    ret &= fn(RS_HAL_ALLOCATION_INIT_STRIDED, (void **)&rsc->mHal.funcs.allocation.initStrided);
11547a5881b8f85d65c74f2471fe6261d4cdb3dce5eMiao Wang#endif
1160ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
1170ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SAMPLER_INIT, (void **)&rsc->mHal.funcs.sampler.init);
1180ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SAMPLER_DESTROY, (void **)&rsc->mHal.funcs.sampler.destroy);
1190ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SAMPLER_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.sampler.updateCachedObject);
1200ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
1210ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_TYPE_INIT, (void **)&rsc->mHal.funcs.type.init);
1220ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_TYPE_DESTROY, (void **)&rsc->mHal.funcs.type.destroy);
1230ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_TYPE_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.type.updateCachedObject);
1240ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
1250ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ELEMENT_INIT, (void **)&rsc->mHal.funcs.element.init);
1260ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ELEMENT_DESTROY, (void **)&rsc->mHal.funcs.element.destroy);
1270ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_ELEMENT_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.element.updateCachedObject);
1280ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
1290ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_GROUP_INIT, (void **)&rsc->mHal.funcs.scriptgroup.init);
1300ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_GROUP_DESTROY, (void **)&rsc->mHal.funcs.scriptgroup.destroy);
1310ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_GROUP_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.scriptgroup.updateCachedObject);
1320ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_GROUP_SET_INPUT, (void **)&rsc->mHal.funcs.scriptgroup.setInput);
1330ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_GROUP_SET_OUTPUT, (void **)&rsc->mHal.funcs.scriptgroup.setOutput);
1340ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    ret &= fn(RS_HAL_SCRIPT_GROUP_EXECUTE, (void **)&rsc->mHal.funcs.scriptgroup.execute);
1350ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
1360ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
1370ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    if (loadGraphics) {
1380ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_INIT, (void **)&rsc->mHal.funcs.initGraphics);
1390ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_SHUTDOWN, (void **)&rsc->mHal.funcs.shutdownGraphics);
1400ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_SWAP, (void **)&rsc->mHal.funcs.swap);
1410ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_SET_SURFACE, (void **)&rsc->mHal.funcs.setSurface);
1420ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_RASTER_INIT, (void **)&rsc->mHal.funcs.raster.init);
1430ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_RASTER_SET_ACTIVE, (void **)&rsc->mHal.funcs.raster.setActive);
1440ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_RASTER_DESTROY, (void **)&rsc->mHal.funcs.raster.destroy);
1450ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_VERTEX_INIT, (void **)&rsc->mHal.funcs.vertex.init);
1460ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_VERTEX_SET_ACTIVE, (void **)&rsc->mHal.funcs.vertex.setActive);
1470ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_VERTEX_DESTROY, (void **)&rsc->mHal.funcs.vertex.destroy);
1480ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_FRAGMENT_INIT, (void **)&rsc->mHal.funcs.fragment.init);
1490ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_FRAGMENT_SET_ACTIVE, (void **)&rsc->mHal.funcs.fragment.setActive);
1500ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_FRAGMENT_DESTROY, (void **)&rsc->mHal.funcs.fragment.destroy);
1510ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_MESH_INIT, (void **)&rsc->mHal.funcs.mesh.init);
1520ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_MESH_DRAW, (void **)&rsc->mHal.funcs.mesh.draw);
153b2e33f32b66b820b1973232f0841c5de8616372bJason Sams        ret &= fn(RS_HAL_GRAPHICS_MESH_DESTROY, (void **)&rsc->mHal.funcs.mesh.destroy);
1540ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_FB_INIT, (void **)&rsc->mHal.funcs.framebuffer.init);
1550ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_FB_SET_ACTIVE, (void **)&rsc->mHal.funcs.framebuffer.setActive);
1560ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_FB_DESTROY, (void **)&rsc->mHal.funcs.framebuffer.destroy);
1570ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_STORE_INIT, (void **)&rsc->mHal.funcs.store.init);
1580ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_STORE_SET_ACTIVE, (void **)&rsc->mHal.funcs.store.setActive);
1590ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ret &= fn(RS_HAL_GRAPHICS_STORE_DESTROY, (void **)&rsc->mHal.funcs.store.destroy);
1600ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    }
1610ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
1620ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    return ret;
1630ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams}
1640ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
1650ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Samsbool Context::loadRuntime(const char* filename) {
1660ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    HalQueryVersion fnQueryVersion = nullptr;
1670ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    HalQueryHal fnQueryHal = nullptr;
1680ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    HalInit fnInit = nullptr;
1690ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    HalAbort fnAbort = nullptr;
1700ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
1710ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
1720ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    // TODO: store the driverSO somewhere so we can dlclose later
1730ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    void *driverSO = nullptr;
1740ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
1750ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    driverSO = dlopen(filename, RTLD_LAZY);
1760ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    if (driverSO == nullptr) {
1770ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ALOGE("Failed loading RS driver: %s", dlerror());
1780ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        return false;
1790ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    }
1800ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
1810ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    // Need to call dlerror() to clear buffer before using it for dlsym().
1820ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    (void) dlerror();
1830ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
1840ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    fnQueryVersion = (HalQueryVersion) dlsym(driverSO, "rsdHalQueryVersion");
1850ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    fnQueryHal = (HalQueryHal) dlsym(driverSO, "rsdHalQueryHal");
1860ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    fnInit = (HalInit) dlsym(driverSO, "rsdHalInit");
1870ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    fnAbort = (HalAbort) dlsym(driverSO, "rsdHalAbort");
1880ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    uint32_t version_major = 0;
1890ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    uint32_t version_minor = 0;
1900ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
1910ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    if ((fnQueryVersion == nullptr) || (fnQueryHal == nullptr) ||
1920ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        (fnInit == nullptr) || (fnAbort == nullptr)) {
1930ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
1940ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ALOGE("Failed to find hal setup entry points: %s", dlerror());
1950ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        goto error;
1960ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    }
1970ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
1980ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    if (!fnQueryVersion(&version_major, &version_minor)) {
1990ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ALOGE("Error checking RS driver version, %s", filename);
2000ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        goto error;
2010ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    }
2020ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
2038e70791ff732ce244077310bdfdaf75dc19baabcDavid Gross    if (version_major != RS_HAL_VERSION) {
2048e70791ff732ce244077310bdfdaf75dc19baabcDavid Gross        ALOGE("Mismatched RS HAL versions: %s is version %u but version %u is expected",
2058e70791ff732ce244077310bdfdaf75dc19baabcDavid Gross              filename, version_major, RS_HAL_VERSION);
2068e70791ff732ce244077310bdfdaf75dc19baabcDavid Gross        goto error;
2078e70791ff732ce244077310bdfdaf75dc19baabcDavid Gross    }
2088e70791ff732ce244077310bdfdaf75dc19baabcDavid Gross
2090ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    if (!LoadHalTable(this, fnQueryHal, mIsGraphicsContext)) {
2100ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ALOGE("Error loading RS HAL table, %s", filename);
2110ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        goto error;
2120ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    }
2130ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
2140ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    if (!(*fnInit)(this, 0, 0)) {
2150ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        ALOGE("Hal init failed,  %s", filename);
2160ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        goto error;
2170ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    }
2180ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
2194c368af7e705f0bcb77fa99495b2e33ef20d2699Stephen Hines    // Only map in the actual driver name if we successfully load the runtime.
220b268abd5528936d13a85b03f44458fe3a55cafadJon Parr    setDriverName(filename);
2214c368af7e705f0bcb77fa99495b2e33ef20d2699Stephen Hines
2220ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    return true;
2230ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
2240ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
2250ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Samserror:
2260ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    if (fnAbort != nullptr) {
2270ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        fnAbort(this);
2280ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    }
2290ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    dlclose(driverSO);
2300ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    return false;
2310ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams}
2320ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
2330ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
2340ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
2350ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Samsbool Context::loadDriver(bool forceDefault) {
2360ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    bool loadDefault = true;
2370ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
2380ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    // Provide a mechanism for dropping in a different RS driver.
2390ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#ifndef RS_COMPATIBILITY_LIB
2400ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#ifdef OVERRIDE_RS_DRIVER
2410ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#define XSTR(S) #S
2420ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#define STR(S) XSTR(S)
2430ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#define OVERRIDE_RS_DRIVER_STRING STR(OVERRIDE_RS_DRIVER)
2440ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
2450ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    if (!forceDefault) {
2460ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        if (loadRuntime(OVERRIDE_RS_DRIVER_STRING)) {
2470ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams            ALOGV("Successfully loaded runtime: %s", OVERRIDE_RS_DRIVER_STRING);
2480ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams            loadDefault = false;
2490ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        } else {
2500ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams            ALOGE("Failed to load runtime %s, loading default", OVERRIDE_RS_DRIVER_STRING);
2510ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        }
2520ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    }
2530ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#undef XSTR
2540ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#undef STR
2550ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#endif  // OVERRIDE_RS_DRIVER
2560ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
2570ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    if (loadDefault) {
2580ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        if (!loadRuntime("libRSDriver.so")) {
2590ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams            ALOGE("Failed to load default runtime!");
2600ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams            return false;
2610ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        }
2620ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    }
2630ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#else // RS_COMPATIBILITY_LIB
2646b1b173adc9e97f797d3324006f850cf840137acMiao Wang    if (!LoadHalTable(this, rsdHalQueryHal, false)) {
2656b1b173adc9e97f797d3324006f850cf840137acMiao Wang        ALOGE("Error loading RS HAL table");
2666b1b173adc9e97f797d3324006f850cf840137acMiao Wang        return false;
2676b1b173adc9e97f797d3324006f850cf840137acMiao Wang    }
2680ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    if (rsdHalInit(this, 0, 0) != true) {
2690ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams        return false;
2700ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    }
2710ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams#endif
2720ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams
2730ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams    return true;
2740ca7cbaea8d5cf18f25d1148be75fbb6e2d86c62Jason Sams}
275