blob: e432a4f9239e77c18e3f1f0d0c0a7dcd3ff68898 [file] [log] [blame]
Jason Sams0ca7cba2015-03-11 15:22:38 -07001/*
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
Miao Wang59f61422017-03-14 14:23:52 -070024#if !defined(RS_VENDOR_LIB) && !defined(RS_COMPATIBILITY_LIB)
Jason Sams0ca7cba2015-03-11 15:22:38 -070025#include "rsMesh.h"
Jason Sams0ca7cba2015-03-11 15:22:38 -070026#endif
27
28#include <sys/types.h>
29#include <sys/resource.h>
30#include <sched.h>
31
32#include <sys/syscall.h>
33#include <string.h>
34#include <dlfcn.h>
35#include <inttypes.h>
36#include <unistd.h>
37
Jason Sams0ca7cba2015-03-11 15:22:38 -070038#ifdef RS_COMPATIBILITY_LIB
39#include "rsCompatibilityLib.h"
40#endif
41
Chih-Hung Hsieh11496ac2016-11-15 15:14:05 -080042namespace android {
43namespace renderscript {
Jason Sams0ca7cba2015-03-11 15:22:38 -070044
45typedef bool (*HalQueryVersion)(uint32_t *version_major, uint32_t *version_minor);
46typedef bool (*HalQueryHal)(android::renderscript::RsHalInitEnums entry, void **fnPtr);
47typedef bool (*HalInit)(RsContext, uint32_t version_major, uint32_t version_minor);
48typedef void (*HalAbort)(RsContext);
49
50
51static bool LoadHalTable(Context *rsc, HalQueryHal fn, bool loadGraphics) {
52 bool ret = true;
53
54 ret &= fn(RS_HAL_CORE_SHUTDOWN, (void **)&rsc->mHal.funcs.shutdownDriver);
55 ret &= fn(RS_HAL_CORE_SET_PRIORITY, (void **)&rsc->mHal.funcs.setPriority);
56 ret &= fn(RS_HAL_CORE_ALLOC_RUNTIME_MEM, (void **)&rsc->mHal.funcs.allocRuntimeMem);
57 ret &= fn(RS_HAL_CORE_FREE_RUNTIME_MEM, (void **)&rsc->mHal.funcs.freeRuntimeMem);
58 ret &= fn(RS_HAL_CORE_FINISH, (void **)&rsc->mHal.funcs.finish);
59
60 ret &= fn(RS_HAL_SCRIPT_INIT, (void **)&rsc->mHal.funcs.script.init);
61 ret &= fn(RS_HAL_SCRIPT_INIT_INTRINSIC, (void **)&rsc->mHal.funcs.script.initIntrinsic);
62 ret &= fn(RS_HAL_SCRIPT_INVOKE_FUNCTION, (void **)&rsc->mHal.funcs.script.invokeFunction);
63 ret &= fn(RS_HAL_SCRIPT_INVOKE_ROOT, (void **)&rsc->mHal.funcs.script.invokeRoot);
64 ret &= fn(RS_HAL_SCRIPT_INVOKE_FOR_EACH, (void **)&rsc->mHal.funcs.script.invokeForEach);
Matt Wala14ce0072015-07-30 17:30:25 -070065 ret &= fn(RS_HAL_SCRIPT_INVOKE_REDUCE, (void **)&rsc->mHal.funcs.script.invokeReduce);
Jason Sams0ca7cba2015-03-11 15:22:38 -070066 ret &= fn(RS_HAL_SCRIPT_INVOKE_INIT, (void **)&rsc->mHal.funcs.script.invokeInit);
67 ret &= fn(RS_HAL_SCRIPT_INVOKE_FREE_CHILDREN, (void **)&rsc->mHal.funcs.script.invokeFreeChildren);
68 ret &= fn(RS_HAL_SCRIPT_SET_GLOBAL_VAR, (void **)&rsc->mHal.funcs.script.setGlobalVar);
69 ret &= fn(RS_HAL_SCRIPT_GET_GLOBAL_VAR, (void **)&rsc->mHal.funcs.script.getGlobalVar);
70 ret &= fn(RS_HAL_SCRIPT_SET_GLOBAL_VAR_WITH_ELEMENT_DIM, (void **)&rsc->mHal.funcs.script.setGlobalVarWithElemDims);
71 ret &= fn(RS_HAL_SCRIPT_SET_GLOBAL_BIND, (void **)&rsc->mHal.funcs.script.setGlobalBind);
72 ret &= fn(RS_HAL_SCRIPT_SET_GLOBAL_OBJECT, (void **)&rsc->mHal.funcs.script.setGlobalObj);
73 ret &= fn(RS_HAL_SCRIPT_DESTROY, (void **)&rsc->mHal.funcs.script.destroy);
74 ret &= fn(RS_HAL_SCRIPT_INVOKE_FOR_EACH_MULTI, (void **)&rsc->mHal.funcs.script.invokeForEachMulti);
75 ret &= fn(RS_HAL_SCRIPT_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.script.updateCachedObject);
76
77 ret &= fn(RS_HAL_ALLOCATION_INIT, (void **)&rsc->mHal.funcs.allocation.init);
Jason Samsf82b6262015-05-11 15:02:50 -070078 ret &= fn(RS_HAL_ALLOCATION_INIT_OEM, (void **)&rsc->mHal.funcs.allocation.initOem);
Jason Sams0ca7cba2015-03-11 15:22:38 -070079 ret &= fn(RS_HAL_ALLOCATION_INIT_ADAPTER, (void **)&rsc->mHal.funcs.allocation.initAdapter);
80 ret &= fn(RS_HAL_ALLOCATION_DESTROY, (void **)&rsc->mHal.funcs.allocation.destroy);
81 ret &= fn(RS_HAL_ALLOCATION_GET_GRALLOC_BITS, (void **)&rsc->mHal.funcs.allocation.grallocBits);
82 ret &= fn(RS_HAL_ALLOCATION_RESIZE, (void **)&rsc->mHal.funcs.allocation.resize);
83 ret &= fn(RS_HAL_ALLOCATION_SYNC_ALL, (void **)&rsc->mHal.funcs.allocation.syncAll);
84 ret &= fn(RS_HAL_ALLOCATION_MARK_DIRTY, (void **)&rsc->mHal.funcs.allocation.markDirty);
85 ret &= fn(RS_HAL_ALLOCATION_SET_SURFACE, (void **)&rsc->mHal.funcs.allocation.setSurface);
86 ret &= fn(RS_HAL_ALLOCATION_IO_SEND, (void **)&rsc->mHal.funcs.allocation.ioSend);
87 ret &= fn(RS_HAL_ALLOCATION_IO_RECEIVE, (void **)&rsc->mHal.funcs.allocation.ioReceive);
88 ret &= fn(RS_HAL_ALLOCATION_DATA_1D, (void **)&rsc->mHal.funcs.allocation.data1D);
89 ret &= fn(RS_HAL_ALLOCATION_DATA_2D, (void **)&rsc->mHal.funcs.allocation.data2D);
90 ret &= fn(RS_HAL_ALLOCATION_DATA_3D, (void **)&rsc->mHal.funcs.allocation.data3D);
91 ret &= fn(RS_HAL_ALLOCATION_READ_1D, (void **)&rsc->mHal.funcs.allocation.read1D);
92 ret &= fn(RS_HAL_ALLOCATION_READ_2D, (void **)&rsc->mHal.funcs.allocation.read2D);
93 ret &= fn(RS_HAL_ALLOCATION_READ_3D, (void **)&rsc->mHal.funcs.allocation.read3D);
94 ret &= fn(RS_HAL_ALLOCATION_LOCK_1D, (void **)&rsc->mHal.funcs.allocation.lock1D);
95 ret &= fn(RS_HAL_ALLOCATION_UNLOCK_1D, (void **)&rsc->mHal.funcs.allocation.unlock1D);
96 ret &= fn(RS_HAL_ALLOCATION_COPY_1D, (void **)&rsc->mHal.funcs.allocation.allocData1D);
97 ret &= fn(RS_HAL_ALLOCATION_COPY_2D, (void **)&rsc->mHal.funcs.allocation.allocData2D);
98 ret &= fn(RS_HAL_ALLOCATION_COPY_3D, (void **)&rsc->mHal.funcs.allocation.allocData3D);
99 ret &= fn(RS_HAL_ALLOCATION_ELEMENT_DATA, (void **)&rsc->mHal.funcs.allocation.elementData);
100 ret &= fn(RS_HAL_ALLOCATION_ELEMENT_READ, (void **)&rsc->mHal.funcs.allocation.elementRead);
101 ret &= fn(RS_HAL_ALLOCATION_GENERATE_MIPMAPS, (void **)&rsc->mHal.funcs.allocation.generateMipmaps);
102 ret &= fn(RS_HAL_ALLOCATION_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.allocation.updateCachedObject);
103 ret &= fn(RS_HAL_ALLOCATION_ADAPTER_OFFSET, (void **)&rsc->mHal.funcs.allocation.adapterOffset);
Jason Sams8ce12812015-05-18 17:28:59 -0700104 ret &= fn(RS_HAL_ALLOCATION_GET_POINTER, (void **)&rsc->mHal.funcs.allocation.getPointer);
Miao Wang47a58812015-07-23 21:59:16 -0700105#ifdef RS_COMPATIBILITY_LIB
106 ret &= fn(RS_HAL_ALLOCATION_INIT_STRIDED, (void **)&rsc->mHal.funcs.allocation.initStrided);
107#endif
Jason Sams0ca7cba2015-03-11 15:22:38 -0700108
109 ret &= fn(RS_HAL_SAMPLER_INIT, (void **)&rsc->mHal.funcs.sampler.init);
110 ret &= fn(RS_HAL_SAMPLER_DESTROY, (void **)&rsc->mHal.funcs.sampler.destroy);
111 ret &= fn(RS_HAL_SAMPLER_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.sampler.updateCachedObject);
112
113 ret &= fn(RS_HAL_TYPE_INIT, (void **)&rsc->mHal.funcs.type.init);
114 ret &= fn(RS_HAL_TYPE_DESTROY, (void **)&rsc->mHal.funcs.type.destroy);
115 ret &= fn(RS_HAL_TYPE_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.type.updateCachedObject);
116
117 ret &= fn(RS_HAL_ELEMENT_INIT, (void **)&rsc->mHal.funcs.element.init);
118 ret &= fn(RS_HAL_ELEMENT_DESTROY, (void **)&rsc->mHal.funcs.element.destroy);
119 ret &= fn(RS_HAL_ELEMENT_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.element.updateCachedObject);
120
121 ret &= fn(RS_HAL_SCRIPT_GROUP_INIT, (void **)&rsc->mHal.funcs.scriptgroup.init);
122 ret &= fn(RS_HAL_SCRIPT_GROUP_DESTROY, (void **)&rsc->mHal.funcs.scriptgroup.destroy);
123 ret &= fn(RS_HAL_SCRIPT_GROUP_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.scriptgroup.updateCachedObject);
124 ret &= fn(RS_HAL_SCRIPT_GROUP_SET_INPUT, (void **)&rsc->mHal.funcs.scriptgroup.setInput);
125 ret &= fn(RS_HAL_SCRIPT_GROUP_SET_OUTPUT, (void **)&rsc->mHal.funcs.scriptgroup.setOutput);
126 ret &= fn(RS_HAL_SCRIPT_GROUP_EXECUTE, (void **)&rsc->mHal.funcs.scriptgroup.execute);
127
128
129 if (loadGraphics) {
130 ret &= fn(RS_HAL_GRAPHICS_INIT, (void **)&rsc->mHal.funcs.initGraphics);
131 ret &= fn(RS_HAL_GRAPHICS_SHUTDOWN, (void **)&rsc->mHal.funcs.shutdownGraphics);
132 ret &= fn(RS_HAL_GRAPHICS_SWAP, (void **)&rsc->mHal.funcs.swap);
133 ret &= fn(RS_HAL_GRAPHICS_SET_SURFACE, (void **)&rsc->mHal.funcs.setSurface);
134 ret &= fn(RS_HAL_GRAPHICS_RASTER_INIT, (void **)&rsc->mHal.funcs.raster.init);
135 ret &= fn(RS_HAL_GRAPHICS_RASTER_SET_ACTIVE, (void **)&rsc->mHal.funcs.raster.setActive);
136 ret &= fn(RS_HAL_GRAPHICS_RASTER_DESTROY, (void **)&rsc->mHal.funcs.raster.destroy);
137 ret &= fn(RS_HAL_GRAPHICS_VERTEX_INIT, (void **)&rsc->mHal.funcs.vertex.init);
138 ret &= fn(RS_HAL_GRAPHICS_VERTEX_SET_ACTIVE, (void **)&rsc->mHal.funcs.vertex.setActive);
139 ret &= fn(RS_HAL_GRAPHICS_VERTEX_DESTROY, (void **)&rsc->mHal.funcs.vertex.destroy);
140 ret &= fn(RS_HAL_GRAPHICS_FRAGMENT_INIT, (void **)&rsc->mHal.funcs.fragment.init);
141 ret &= fn(RS_HAL_GRAPHICS_FRAGMENT_SET_ACTIVE, (void **)&rsc->mHal.funcs.fragment.setActive);
142 ret &= fn(RS_HAL_GRAPHICS_FRAGMENT_DESTROY, (void **)&rsc->mHal.funcs.fragment.destroy);
143 ret &= fn(RS_HAL_GRAPHICS_MESH_INIT, (void **)&rsc->mHal.funcs.mesh.init);
144 ret &= fn(RS_HAL_GRAPHICS_MESH_DRAW, (void **)&rsc->mHal.funcs.mesh.draw);
Jason Samsb2e33f32015-03-16 12:12:21 -0700145 ret &= fn(RS_HAL_GRAPHICS_MESH_DESTROY, (void **)&rsc->mHal.funcs.mesh.destroy);
Jason Sams0ca7cba2015-03-11 15:22:38 -0700146 ret &= fn(RS_HAL_GRAPHICS_FB_INIT, (void **)&rsc->mHal.funcs.framebuffer.init);
147 ret &= fn(RS_HAL_GRAPHICS_FB_SET_ACTIVE, (void **)&rsc->mHal.funcs.framebuffer.setActive);
148 ret &= fn(RS_HAL_GRAPHICS_FB_DESTROY, (void **)&rsc->mHal.funcs.framebuffer.destroy);
149 ret &= fn(RS_HAL_GRAPHICS_STORE_INIT, (void **)&rsc->mHal.funcs.store.init);
150 ret &= fn(RS_HAL_GRAPHICS_STORE_SET_ACTIVE, (void **)&rsc->mHal.funcs.store.setActive);
151 ret &= fn(RS_HAL_GRAPHICS_STORE_DESTROY, (void **)&rsc->mHal.funcs.store.destroy);
152 }
153
154 return ret;
155}
156
157bool Context::loadRuntime(const char* filename) {
158 HalQueryVersion fnQueryVersion = nullptr;
159 HalQueryHal fnQueryHal = nullptr;
160 HalInit fnInit = nullptr;
161 HalAbort fnAbort = nullptr;
162
163
164 // TODO: store the driverSO somewhere so we can dlclose later
165 void *driverSO = nullptr;
166
167 driverSO = dlopen(filename, RTLD_LAZY);
168 if (driverSO == nullptr) {
169 ALOGE("Failed loading RS driver: %s", dlerror());
170 return false;
171 }
172
173 // Need to call dlerror() to clear buffer before using it for dlsym().
174 (void) dlerror();
175
176 fnQueryVersion = (HalQueryVersion) dlsym(driverSO, "rsdHalQueryVersion");
177 fnQueryHal = (HalQueryHal) dlsym(driverSO, "rsdHalQueryHal");
178 fnInit = (HalInit) dlsym(driverSO, "rsdHalInit");
179 fnAbort = (HalAbort) dlsym(driverSO, "rsdHalAbort");
180 uint32_t version_major = 0;
181 uint32_t version_minor = 0;
182
183 if ((fnQueryVersion == nullptr) || (fnQueryHal == nullptr) ||
184 (fnInit == nullptr) || (fnAbort == nullptr)) {
185
186 ALOGE("Failed to find hal setup entry points: %s", dlerror());
187 goto error;
188 }
189
190 if (!fnQueryVersion(&version_major, &version_minor)) {
191 ALOGE("Error checking RS driver version, %s", filename);
192 goto error;
193 }
194
David Gross8e707912016-06-06 16:15:00 -0700195 if (version_major != RS_HAL_VERSION) {
196 ALOGE("Mismatched RS HAL versions: %s is version %u but version %u is expected",
197 filename, version_major, RS_HAL_VERSION);
198 goto error;
199 }
200
Jason Sams0ca7cba2015-03-11 15:22:38 -0700201 if (!LoadHalTable(this, fnQueryHal, mIsGraphicsContext)) {
202 ALOGE("Error loading RS HAL table, %s", filename);
203 goto error;
204 }
205
206 if (!(*fnInit)(this, 0, 0)) {
207 ALOGE("Hal init failed, %s", filename);
208 goto error;
209 }
210
Stephen Hines4c368af2015-05-06 00:43:02 -0700211 // Only map in the actual driver name if we successfully load the runtime.
Jon Parrb268abd2015-06-11 14:27:51 +0100212 setDriverName(filename);
Stephen Hines4c368af2015-05-06 00:43:02 -0700213
Jason Sams0ca7cba2015-03-11 15:22:38 -0700214 return true;
215
216
217error:
218 if (fnAbort != nullptr) {
219 fnAbort(this);
220 }
221 dlclose(driverSO);
222 return false;
223}
224
225
226
Yang Ni75f0d312016-11-11 12:30:09 -0800227bool Context::loadDriver(bool forceDefault, bool forceRSoV) {
Jason Sams0ca7cba2015-03-11 15:22:38 -0700228 bool loadDefault = true;
229
230 // Provide a mechanism for dropping in a different RS driver.
231#ifndef RS_COMPATIBILITY_LIB
Yang Ni75f0d312016-11-11 12:30:09 -0800232
233 if (forceRSoV) {
Yang Ni257223a2017-01-12 14:18:53 -0800234 // If the property is set to use the RSoV driver, load it and fall back
235 // to the vendor driver or the CPU reference driver if it does not load.
Yang Ni75f0d312016-11-11 12:30:09 -0800236 if (loadRuntime("libRSDriver_RSoV.so")) {
237 ALOGV("Successfully loaded the RSoV driver!");
238 return true;
239 }
240 ALOGE("Failed to load the RSoV driver!");
Yang Ni75f0d312016-11-11 12:30:09 -0800241 }
242
Miao Wang5fccd072017-05-16 15:39:34 -0700243 if (!forceDefault && mVendorDriverName != nullptr) {
244 if (loadRuntime(mVendorDriverName)) {
245 ALOGV("Successfully loaded runtime: %s", mVendorDriverName);
Jason Sams0ca7cba2015-03-11 15:22:38 -0700246 loadDefault = false;
247 } else {
Miao Wang5fccd072017-05-16 15:39:34 -0700248 ALOGE("Failed to load runtime %s, loading default", mVendorDriverName);
Jason Sams0ca7cba2015-03-11 15:22:38 -0700249 }
250 }
Jason Sams0ca7cba2015-03-11 15:22:38 -0700251
252 if (loadDefault) {
253 if (!loadRuntime("libRSDriver.so")) {
254 ALOGE("Failed to load default runtime!");
255 return false;
256 }
257 }
258#else // RS_COMPATIBILITY_LIB
Miao Wang6b1b1732015-03-13 14:37:56 -0700259 if (!LoadHalTable(this, rsdHalQueryHal, false)) {
260 ALOGE("Error loading RS HAL table");
261 return false;
262 }
Jason Sams0ca7cba2015-03-11 15:22:38 -0700263 if (rsdHalInit(this, 0, 0) != true) {
264 return false;
265 }
266#endif
267
268 return true;
269}
Chih-Hung Hsieh11496ac2016-11-15 15:14:05 -0800270
271} // namespace renderscript
272} // namespace android