rsDriverLoader.cpp revision 62237219e567b9f972c86e7ca4e96f9b3d5ad4de
1/*
2 * Copyright (C) 2015 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 "rs.h"
18#include "rsDevice.h"
19#include "rsContext.h"
20#include "rsThreadIO.h"
21
22#include "rsgApiStructs.h"
23
24#ifndef RS_COMPATIBILITY_LIB
25#include "rsMesh.h"
26#include <gui/DisplayEventReceiver.h>
27#endif
28
29#include <sys/types.h>
30#include <sys/resource.h>
31#include <sched.h>
32
33#include <sys/syscall.h>
34#include <string.h>
35#include <dlfcn.h>
36#include <inttypes.h>
37#include <unistd.h>
38
39#if !defined(RS_COMPATIBILITY_LIB) && defined(__ANDROID__)
40#include <cutils/properties.h>
41#endif
42
43#ifdef RS_COMPATIBILITY_LIB
44#include "rsCompatibilityLib.h"
45#endif
46
47namespace android {
48namespace renderscript {
49
50typedef bool (*HalQueryVersion)(uint32_t *version_major, uint32_t *version_minor);
51typedef bool (*HalQueryHal)(android::renderscript::RsHalInitEnums entry, void **fnPtr);
52typedef bool (*HalInit)(RsContext, uint32_t version_major, uint32_t version_minor);
53typedef void (*HalAbort)(RsContext);
54
55
56static bool LoadHalTable(Context *rsc, HalQueryHal fn, bool loadGraphics) {
57    bool ret = true;
58
59    ret &= fn(RS_HAL_CORE_SHUTDOWN, (void **)&rsc->mHal.funcs.shutdownDriver);
60    ret &= fn(RS_HAL_CORE_SET_PRIORITY, (void **)&rsc->mHal.funcs.setPriority);
61    ret &= fn(RS_HAL_CORE_ALLOC_RUNTIME_MEM, (void **)&rsc->mHal.funcs.allocRuntimeMem);
62    ret &= fn(RS_HAL_CORE_FREE_RUNTIME_MEM, (void **)&rsc->mHal.funcs.freeRuntimeMem);
63    ret &= fn(RS_HAL_CORE_FINISH, (void **)&rsc->mHal.funcs.finish);
64
65    ret &= fn(RS_HAL_SCRIPT_INIT, (void **)&rsc->mHal.funcs.script.init);
66    ret &= fn(RS_HAL_SCRIPT_INIT_INTRINSIC, (void **)&rsc->mHal.funcs.script.initIntrinsic);
67    ret &= fn(RS_HAL_SCRIPT_INVOKE_FUNCTION, (void **)&rsc->mHal.funcs.script.invokeFunction);
68    ret &= fn(RS_HAL_SCRIPT_INVOKE_ROOT, (void **)&rsc->mHal.funcs.script.invokeRoot);
69    ret &= fn(RS_HAL_SCRIPT_INVOKE_FOR_EACH, (void **)&rsc->mHal.funcs.script.invokeForEach);
70    ret &= fn(RS_HAL_SCRIPT_INVOKE_REDUCE, (void **)&rsc->mHal.funcs.script.invokeReduce);
71    ret &= fn(RS_HAL_SCRIPT_INVOKE_INIT, (void **)&rsc->mHal.funcs.script.invokeInit);
72    ret &= fn(RS_HAL_SCRIPT_INVOKE_FREE_CHILDREN, (void **)&rsc->mHal.funcs.script.invokeFreeChildren);
73    ret &= fn(RS_HAL_SCRIPT_SET_GLOBAL_VAR, (void **)&rsc->mHal.funcs.script.setGlobalVar);
74    ret &= fn(RS_HAL_SCRIPT_GET_GLOBAL_VAR, (void **)&rsc->mHal.funcs.script.getGlobalVar);
75    ret &= fn(RS_HAL_SCRIPT_SET_GLOBAL_VAR_WITH_ELEMENT_DIM, (void **)&rsc->mHal.funcs.script.setGlobalVarWithElemDims);
76    ret &= fn(RS_HAL_SCRIPT_SET_GLOBAL_BIND, (void **)&rsc->mHal.funcs.script.setGlobalBind);
77    ret &= fn(RS_HAL_SCRIPT_SET_GLOBAL_OBJECT, (void **)&rsc->mHal.funcs.script.setGlobalObj);
78    ret &= fn(RS_HAL_SCRIPT_DESTROY, (void **)&rsc->mHal.funcs.script.destroy);
79    ret &= fn(RS_HAL_SCRIPT_INVOKE_FOR_EACH_MULTI, (void **)&rsc->mHal.funcs.script.invokeForEachMulti);
80    ret &= fn(RS_HAL_SCRIPT_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.script.updateCachedObject);
81
82    ret &= fn(RS_HAL_ALLOCATION_INIT, (void **)&rsc->mHal.funcs.allocation.init);
83    ret &= fn(RS_HAL_ALLOCATION_INIT_OEM, (void **)&rsc->mHal.funcs.allocation.initOem);
84    ret &= fn(RS_HAL_ALLOCATION_INIT_ADAPTER, (void **)&rsc->mHal.funcs.allocation.initAdapter);
85    ret &= fn(RS_HAL_ALLOCATION_DESTROY, (void **)&rsc->mHal.funcs.allocation.destroy);
86    ret &= fn(RS_HAL_ALLOCATION_GET_GRALLOC_BITS, (void **)&rsc->mHal.funcs.allocation.grallocBits);
87    ret &= fn(RS_HAL_ALLOCATION_RESIZE, (void **)&rsc->mHal.funcs.allocation.resize);
88    ret &= fn(RS_HAL_ALLOCATION_SYNC_ALL, (void **)&rsc->mHal.funcs.allocation.syncAll);
89    ret &= fn(RS_HAL_ALLOCATION_MARK_DIRTY, (void **)&rsc->mHal.funcs.allocation.markDirty);
90    ret &= fn(RS_HAL_ALLOCATION_SET_SURFACE, (void **)&rsc->mHal.funcs.allocation.setSurface);
91    ret &= fn(RS_HAL_ALLOCATION_IO_SEND, (void **)&rsc->mHal.funcs.allocation.ioSend);
92    ret &= fn(RS_HAL_ALLOCATION_IO_RECEIVE, (void **)&rsc->mHal.funcs.allocation.ioReceive);
93    ret &= fn(RS_HAL_ALLOCATION_DATA_1D, (void **)&rsc->mHal.funcs.allocation.data1D);
94    ret &= fn(RS_HAL_ALLOCATION_DATA_2D, (void **)&rsc->mHal.funcs.allocation.data2D);
95    ret &= fn(RS_HAL_ALLOCATION_DATA_3D, (void **)&rsc->mHal.funcs.allocation.data3D);
96    ret &= fn(RS_HAL_ALLOCATION_READ_1D, (void **)&rsc->mHal.funcs.allocation.read1D);
97    ret &= fn(RS_HAL_ALLOCATION_READ_2D, (void **)&rsc->mHal.funcs.allocation.read2D);
98    ret &= fn(RS_HAL_ALLOCATION_READ_3D, (void **)&rsc->mHal.funcs.allocation.read3D);
99    ret &= fn(RS_HAL_ALLOCATION_LOCK_1D, (void **)&rsc->mHal.funcs.allocation.lock1D);
100    ret &= fn(RS_HAL_ALLOCATION_UNLOCK_1D, (void **)&rsc->mHal.funcs.allocation.unlock1D);
101    ret &= fn(RS_HAL_ALLOCATION_COPY_1D, (void **)&rsc->mHal.funcs.allocation.allocData1D);
102    ret &= fn(RS_HAL_ALLOCATION_COPY_2D, (void **)&rsc->mHal.funcs.allocation.allocData2D);
103    ret &= fn(RS_HAL_ALLOCATION_COPY_3D, (void **)&rsc->mHal.funcs.allocation.allocData3D);
104    ret &= fn(RS_HAL_ALLOCATION_ELEMENT_DATA, (void **)&rsc->mHal.funcs.allocation.elementData);
105    ret &= fn(RS_HAL_ALLOCATION_ELEMENT_READ, (void **)&rsc->mHal.funcs.allocation.elementRead);
106    ret &= fn(RS_HAL_ALLOCATION_GENERATE_MIPMAPS, (void **)&rsc->mHal.funcs.allocation.generateMipmaps);
107    ret &= fn(RS_HAL_ALLOCATION_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.allocation.updateCachedObject);
108    ret &= fn(RS_HAL_ALLOCATION_ADAPTER_OFFSET, (void **)&rsc->mHal.funcs.allocation.adapterOffset);
109    ret &= fn(RS_HAL_ALLOCATION_GET_POINTER, (void **)&rsc->mHal.funcs.allocation.getPointer);
110#ifdef RS_COMPATIBILITY_LIB
111    ret &= fn(RS_HAL_ALLOCATION_INIT_STRIDED, (void **)&rsc->mHal.funcs.allocation.initStrided);
112#endif
113
114    ret &= fn(RS_HAL_SAMPLER_INIT, (void **)&rsc->mHal.funcs.sampler.init);
115    ret &= fn(RS_HAL_SAMPLER_DESTROY, (void **)&rsc->mHal.funcs.sampler.destroy);
116    ret &= fn(RS_HAL_SAMPLER_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.sampler.updateCachedObject);
117
118    ret &= fn(RS_HAL_TYPE_INIT, (void **)&rsc->mHal.funcs.type.init);
119    ret &= fn(RS_HAL_TYPE_DESTROY, (void **)&rsc->mHal.funcs.type.destroy);
120    ret &= fn(RS_HAL_TYPE_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.type.updateCachedObject);
121
122    ret &= fn(RS_HAL_ELEMENT_INIT, (void **)&rsc->mHal.funcs.element.init);
123    ret &= fn(RS_HAL_ELEMENT_DESTROY, (void **)&rsc->mHal.funcs.element.destroy);
124    ret &= fn(RS_HAL_ELEMENT_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.element.updateCachedObject);
125
126    ret &= fn(RS_HAL_SCRIPT_GROUP_INIT, (void **)&rsc->mHal.funcs.scriptgroup.init);
127    ret &= fn(RS_HAL_SCRIPT_GROUP_DESTROY, (void **)&rsc->mHal.funcs.scriptgroup.destroy);
128    ret &= fn(RS_HAL_SCRIPT_GROUP_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.scriptgroup.updateCachedObject);
129    ret &= fn(RS_HAL_SCRIPT_GROUP_SET_INPUT, (void **)&rsc->mHal.funcs.scriptgroup.setInput);
130    ret &= fn(RS_HAL_SCRIPT_GROUP_SET_OUTPUT, (void **)&rsc->mHal.funcs.scriptgroup.setOutput);
131    ret &= fn(RS_HAL_SCRIPT_GROUP_EXECUTE, (void **)&rsc->mHal.funcs.scriptgroup.execute);
132
133
134    if (loadGraphics) {
135        ret &= fn(RS_HAL_GRAPHICS_INIT, (void **)&rsc->mHal.funcs.initGraphics);
136        ret &= fn(RS_HAL_GRAPHICS_SHUTDOWN, (void **)&rsc->mHal.funcs.shutdownGraphics);
137        ret &= fn(RS_HAL_GRAPHICS_SWAP, (void **)&rsc->mHal.funcs.swap);
138        ret &= fn(RS_HAL_GRAPHICS_SET_SURFACE, (void **)&rsc->mHal.funcs.setSurface);
139        ret &= fn(RS_HAL_GRAPHICS_RASTER_INIT, (void **)&rsc->mHal.funcs.raster.init);
140        ret &= fn(RS_HAL_GRAPHICS_RASTER_SET_ACTIVE, (void **)&rsc->mHal.funcs.raster.setActive);
141        ret &= fn(RS_HAL_GRAPHICS_RASTER_DESTROY, (void **)&rsc->mHal.funcs.raster.destroy);
142        ret &= fn(RS_HAL_GRAPHICS_VERTEX_INIT, (void **)&rsc->mHal.funcs.vertex.init);
143        ret &= fn(RS_HAL_GRAPHICS_VERTEX_SET_ACTIVE, (void **)&rsc->mHal.funcs.vertex.setActive);
144        ret &= fn(RS_HAL_GRAPHICS_VERTEX_DESTROY, (void **)&rsc->mHal.funcs.vertex.destroy);
145        ret &= fn(RS_HAL_GRAPHICS_FRAGMENT_INIT, (void **)&rsc->mHal.funcs.fragment.init);
146        ret &= fn(RS_HAL_GRAPHICS_FRAGMENT_SET_ACTIVE, (void **)&rsc->mHal.funcs.fragment.setActive);
147        ret &= fn(RS_HAL_GRAPHICS_FRAGMENT_DESTROY, (void **)&rsc->mHal.funcs.fragment.destroy);
148        ret &= fn(RS_HAL_GRAPHICS_MESH_INIT, (void **)&rsc->mHal.funcs.mesh.init);
149        ret &= fn(RS_HAL_GRAPHICS_MESH_DRAW, (void **)&rsc->mHal.funcs.mesh.draw);
150        ret &= fn(RS_HAL_GRAPHICS_MESH_DESTROY, (void **)&rsc->mHal.funcs.mesh.destroy);
151        ret &= fn(RS_HAL_GRAPHICS_FB_INIT, (void **)&rsc->mHal.funcs.framebuffer.init);
152        ret &= fn(RS_HAL_GRAPHICS_FB_SET_ACTIVE, (void **)&rsc->mHal.funcs.framebuffer.setActive);
153        ret &= fn(RS_HAL_GRAPHICS_FB_DESTROY, (void **)&rsc->mHal.funcs.framebuffer.destroy);
154        ret &= fn(RS_HAL_GRAPHICS_STORE_INIT, (void **)&rsc->mHal.funcs.store.init);
155        ret &= fn(RS_HAL_GRAPHICS_STORE_SET_ACTIVE, (void **)&rsc->mHal.funcs.store.setActive);
156        ret &= fn(RS_HAL_GRAPHICS_STORE_DESTROY, (void **)&rsc->mHal.funcs.store.destroy);
157    }
158
159    return ret;
160}
161
162bool Context::loadRuntime(const char* filename) {
163    HalQueryVersion fnQueryVersion = nullptr;
164    HalQueryHal fnQueryHal = nullptr;
165    HalInit fnInit = nullptr;
166    HalAbort fnAbort = nullptr;
167
168
169    // TODO: store the driverSO somewhere so we can dlclose later
170    void *driverSO = nullptr;
171
172    driverSO = dlopen(filename, RTLD_LAZY);
173    if (driverSO == nullptr) {
174        ALOGE("Failed loading RS driver: %s", dlerror());
175        return false;
176    }
177
178    // Need to call dlerror() to clear buffer before using it for dlsym().
179    (void) dlerror();
180
181    fnQueryVersion = (HalQueryVersion) dlsym(driverSO, "rsdHalQueryVersion");
182    fnQueryHal = (HalQueryHal) dlsym(driverSO, "rsdHalQueryHal");
183    fnInit = (HalInit) dlsym(driverSO, "rsdHalInit");
184    fnAbort = (HalAbort) dlsym(driverSO, "rsdHalAbort");
185    uint32_t version_major = 0;
186    uint32_t version_minor = 0;
187
188    if ((fnQueryVersion == nullptr) || (fnQueryHal == nullptr) ||
189        (fnInit == nullptr) || (fnAbort == nullptr)) {
190
191        ALOGE("Failed to find hal setup entry points: %s", dlerror());
192        goto error;
193    }
194
195    if (!fnQueryVersion(&version_major, &version_minor)) {
196        ALOGE("Error checking RS driver version, %s", filename);
197        goto error;
198    }
199
200    if (version_major != RS_HAL_VERSION) {
201        ALOGE("Mismatched RS HAL versions: %s is version %u but version %u is expected",
202              filename, version_major, RS_HAL_VERSION);
203        goto error;
204    }
205
206    if (!LoadHalTable(this, fnQueryHal, mIsGraphicsContext)) {
207        ALOGE("Error loading RS HAL table, %s", filename);
208        goto error;
209    }
210
211    if (!(*fnInit)(this, 0, 0)) {
212        ALOGE("Hal init failed,  %s", filename);
213        goto error;
214    }
215
216    // Only map in the actual driver name if we successfully load the runtime.
217    setDriverName(filename);
218
219    return true;
220
221
222error:
223    if (fnAbort != nullptr) {
224        fnAbort(this);
225    }
226    dlclose(driverSO);
227    return false;
228}
229
230
231
232bool Context::loadDriver(bool forceDefault, bool forceRSoV) {
233    bool loadDefault = true;
234
235    // Provide a mechanism for dropping in a different RS driver.
236#ifndef RS_COMPATIBILITY_LIB
237
238    if (forceRSoV) {
239        // If the property is set to use the RSoV driver, load it and fall back
240        // to the vendor driver or the CPU reference driver if it does not load.
241        if (loadRuntime("libRSDriver_RSoV.so")) {
242            ALOGV("Successfully loaded the RSoV driver!");
243            return true;
244        }
245        ALOGE("Failed to load the RSoV driver!");
246    }
247
248#ifdef OVERRIDE_RS_DRIVER
249#define XSTR(S) #S
250#define STR(S) XSTR(S)
251#define OVERRIDE_RS_DRIVER_STRING STR(OVERRIDE_RS_DRIVER)
252    if (!forceDefault) {
253        if (loadRuntime(OVERRIDE_RS_DRIVER_STRING)) {
254            ALOGV("Successfully loaded runtime: %s", OVERRIDE_RS_DRIVER_STRING);
255            loadDefault = false;
256        } else {
257            ALOGE("Failed to load runtime %s, loading default", OVERRIDE_RS_DRIVER_STRING);
258        }
259    }
260#undef XSTR
261#undef STR
262#endif  // OVERRIDE_RS_DRIVER
263
264    if (loadDefault) {
265        if (!loadRuntime("libRSDriver.so")) {
266            ALOGE("Failed to load default runtime!");
267            return false;
268        }
269    }
270#else // RS_COMPATIBILITY_LIB
271    if (!LoadHalTable(this, rsdHalQueryHal, false)) {
272        ALOGE("Error loading RS HAL table");
273        return false;
274    }
275    if (rsdHalInit(this, 0, 0) != true) {
276        return false;
277    }
278#endif
279
280    return true;
281}
282
283} // namespace renderscript
284} // namespace android
285