blob: 2bc53b6a136ccb2e956bc5bcff62eedaa79e191e [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
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_SERVER) && !defined(RS_COMPATIBILITY_LIB) && \
40 defined(HAVE_ANDROID_OS)
41#include <cutils/properties.h>
42#endif
43
44#ifdef RS_COMPATIBILITY_LIB
45#include "rsCompatibilityLib.h"
46#endif
47
48using namespace android;
49using namespace android::renderscript;
50
51
52
53typedef bool (*HalQueryVersion)(uint32_t *version_major, uint32_t *version_minor);
54typedef bool (*HalQueryHal)(android::renderscript::RsHalInitEnums entry, void **fnPtr);
55typedef bool (*HalInit)(RsContext, uint32_t version_major, uint32_t version_minor);
56typedef void (*HalAbort)(RsContext);
57
58
59static bool LoadHalTable(Context *rsc, HalQueryHal fn, bool loadGraphics) {
60 bool ret = true;
61
62 ret &= fn(RS_HAL_CORE_SHUTDOWN, (void **)&rsc->mHal.funcs.shutdownDriver);
63 ret &= fn(RS_HAL_CORE_SET_PRIORITY, (void **)&rsc->mHal.funcs.setPriority);
64 ret &= fn(RS_HAL_CORE_ALLOC_RUNTIME_MEM, (void **)&rsc->mHal.funcs.allocRuntimeMem);
65 ret &= fn(RS_HAL_CORE_FREE_RUNTIME_MEM, (void **)&rsc->mHal.funcs.freeRuntimeMem);
66 ret &= fn(RS_HAL_CORE_FINISH, (void **)&rsc->mHal.funcs.finish);
67
68 ret &= fn(RS_HAL_SCRIPT_INIT, (void **)&rsc->mHal.funcs.script.init);
69 ret &= fn(RS_HAL_SCRIPT_INIT_INTRINSIC, (void **)&rsc->mHal.funcs.script.initIntrinsic);
70 ret &= fn(RS_HAL_SCRIPT_INVOKE_FUNCTION, (void **)&rsc->mHal.funcs.script.invokeFunction);
71 ret &= fn(RS_HAL_SCRIPT_INVOKE_ROOT, (void **)&rsc->mHal.funcs.script.invokeRoot);
72 ret &= fn(RS_HAL_SCRIPT_INVOKE_FOR_EACH, (void **)&rsc->mHal.funcs.script.invokeForEach);
73 ret &= fn(RS_HAL_SCRIPT_INVOKE_INIT, (void **)&rsc->mHal.funcs.script.invokeInit);
74 ret &= fn(RS_HAL_SCRIPT_INVOKE_FREE_CHILDREN, (void **)&rsc->mHal.funcs.script.invokeFreeChildren);
75 ret &= fn(RS_HAL_SCRIPT_SET_GLOBAL_VAR, (void **)&rsc->mHal.funcs.script.setGlobalVar);
76 ret &= fn(RS_HAL_SCRIPT_GET_GLOBAL_VAR, (void **)&rsc->mHal.funcs.script.getGlobalVar);
77 ret &= fn(RS_HAL_SCRIPT_SET_GLOBAL_VAR_WITH_ELEMENT_DIM, (void **)&rsc->mHal.funcs.script.setGlobalVarWithElemDims);
78 ret &= fn(RS_HAL_SCRIPT_SET_GLOBAL_BIND, (void **)&rsc->mHal.funcs.script.setGlobalBind);
79 ret &= fn(RS_HAL_SCRIPT_SET_GLOBAL_OBJECT, (void **)&rsc->mHal.funcs.script.setGlobalObj);
80 ret &= fn(RS_HAL_SCRIPT_DESTROY, (void **)&rsc->mHal.funcs.script.destroy);
81 ret &= fn(RS_HAL_SCRIPT_INVOKE_FOR_EACH_MULTI, (void **)&rsc->mHal.funcs.script.invokeForEachMulti);
82 ret &= fn(RS_HAL_SCRIPT_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.script.updateCachedObject);
83
84 ret &= fn(RS_HAL_ALLOCATION_INIT, (void **)&rsc->mHal.funcs.allocation.init);
Jason Samsf82b6262015-05-11 15:02:50 -070085 ret &= fn(RS_HAL_ALLOCATION_INIT_OEM, (void **)&rsc->mHal.funcs.allocation.initOem);
Jason Sams0ca7cba2015-03-11 15:22:38 -070086 ret &= fn(RS_HAL_ALLOCATION_INIT_ADAPTER, (void **)&rsc->mHal.funcs.allocation.initAdapter);
87 ret &= fn(RS_HAL_ALLOCATION_DESTROY, (void **)&rsc->mHal.funcs.allocation.destroy);
88 ret &= fn(RS_HAL_ALLOCATION_GET_GRALLOC_BITS, (void **)&rsc->mHal.funcs.allocation.grallocBits);
89 ret &= fn(RS_HAL_ALLOCATION_RESIZE, (void **)&rsc->mHal.funcs.allocation.resize);
90 ret &= fn(RS_HAL_ALLOCATION_SYNC_ALL, (void **)&rsc->mHal.funcs.allocation.syncAll);
91 ret &= fn(RS_HAL_ALLOCATION_MARK_DIRTY, (void **)&rsc->mHal.funcs.allocation.markDirty);
92 ret &= fn(RS_HAL_ALLOCATION_SET_SURFACE, (void **)&rsc->mHal.funcs.allocation.setSurface);
93 ret &= fn(RS_HAL_ALLOCATION_IO_SEND, (void **)&rsc->mHal.funcs.allocation.ioSend);
94 ret &= fn(RS_HAL_ALLOCATION_IO_RECEIVE, (void **)&rsc->mHal.funcs.allocation.ioReceive);
95 ret &= fn(RS_HAL_ALLOCATION_DATA_1D, (void **)&rsc->mHal.funcs.allocation.data1D);
96 ret &= fn(RS_HAL_ALLOCATION_DATA_2D, (void **)&rsc->mHal.funcs.allocation.data2D);
97 ret &= fn(RS_HAL_ALLOCATION_DATA_3D, (void **)&rsc->mHal.funcs.allocation.data3D);
98 ret &= fn(RS_HAL_ALLOCATION_READ_1D, (void **)&rsc->mHal.funcs.allocation.read1D);
99 ret &= fn(RS_HAL_ALLOCATION_READ_2D, (void **)&rsc->mHal.funcs.allocation.read2D);
100 ret &= fn(RS_HAL_ALLOCATION_READ_3D, (void **)&rsc->mHal.funcs.allocation.read3D);
101 ret &= fn(RS_HAL_ALLOCATION_LOCK_1D, (void **)&rsc->mHal.funcs.allocation.lock1D);
102 ret &= fn(RS_HAL_ALLOCATION_UNLOCK_1D, (void **)&rsc->mHal.funcs.allocation.unlock1D);
103 ret &= fn(RS_HAL_ALLOCATION_COPY_1D, (void **)&rsc->mHal.funcs.allocation.allocData1D);
104 ret &= fn(RS_HAL_ALLOCATION_COPY_2D, (void **)&rsc->mHal.funcs.allocation.allocData2D);
105 ret &= fn(RS_HAL_ALLOCATION_COPY_3D, (void **)&rsc->mHal.funcs.allocation.allocData3D);
106 ret &= fn(RS_HAL_ALLOCATION_ELEMENT_DATA, (void **)&rsc->mHal.funcs.allocation.elementData);
107 ret &= fn(RS_HAL_ALLOCATION_ELEMENT_READ, (void **)&rsc->mHal.funcs.allocation.elementRead);
108 ret &= fn(RS_HAL_ALLOCATION_GENERATE_MIPMAPS, (void **)&rsc->mHal.funcs.allocation.generateMipmaps);
109 ret &= fn(RS_HAL_ALLOCATION_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.allocation.updateCachedObject);
110 ret &= fn(RS_HAL_ALLOCATION_ADAPTER_OFFSET, (void **)&rsc->mHal.funcs.allocation.adapterOffset);
111
112 ret &= fn(RS_HAL_SAMPLER_INIT, (void **)&rsc->mHal.funcs.sampler.init);
113 ret &= fn(RS_HAL_SAMPLER_DESTROY, (void **)&rsc->mHal.funcs.sampler.destroy);
114 ret &= fn(RS_HAL_SAMPLER_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.sampler.updateCachedObject);
115
116 ret &= fn(RS_HAL_TYPE_INIT, (void **)&rsc->mHal.funcs.type.init);
117 ret &= fn(RS_HAL_TYPE_DESTROY, (void **)&rsc->mHal.funcs.type.destroy);
118 ret &= fn(RS_HAL_TYPE_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.type.updateCachedObject);
119
120 ret &= fn(RS_HAL_ELEMENT_INIT, (void **)&rsc->mHal.funcs.element.init);
121 ret &= fn(RS_HAL_ELEMENT_DESTROY, (void **)&rsc->mHal.funcs.element.destroy);
122 ret &= fn(RS_HAL_ELEMENT_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.element.updateCachedObject);
123
124 ret &= fn(RS_HAL_SCRIPT_GROUP_INIT, (void **)&rsc->mHal.funcs.scriptgroup.init);
125 ret &= fn(RS_HAL_SCRIPT_GROUP_DESTROY, (void **)&rsc->mHal.funcs.scriptgroup.destroy);
126 ret &= fn(RS_HAL_SCRIPT_GROUP_UPDATE_CACHED_OBJECT, (void **)&rsc->mHal.funcs.scriptgroup.updateCachedObject);
127 ret &= fn(RS_HAL_SCRIPT_GROUP_SET_INPUT, (void **)&rsc->mHal.funcs.scriptgroup.setInput);
128 ret &= fn(RS_HAL_SCRIPT_GROUP_SET_OUTPUT, (void **)&rsc->mHal.funcs.scriptgroup.setOutput);
129 ret &= fn(RS_HAL_SCRIPT_GROUP_EXECUTE, (void **)&rsc->mHal.funcs.scriptgroup.execute);
130
131
132 if (loadGraphics) {
133 ret &= fn(RS_HAL_GRAPHICS_INIT, (void **)&rsc->mHal.funcs.initGraphics);
134 ret &= fn(RS_HAL_GRAPHICS_SHUTDOWN, (void **)&rsc->mHal.funcs.shutdownGraphics);
135 ret &= fn(RS_HAL_GRAPHICS_SWAP, (void **)&rsc->mHal.funcs.swap);
136 ret &= fn(RS_HAL_GRAPHICS_SET_SURFACE, (void **)&rsc->mHal.funcs.setSurface);
137 ret &= fn(RS_HAL_GRAPHICS_RASTER_INIT, (void **)&rsc->mHal.funcs.raster.init);
138 ret &= fn(RS_HAL_GRAPHICS_RASTER_SET_ACTIVE, (void **)&rsc->mHal.funcs.raster.setActive);
139 ret &= fn(RS_HAL_GRAPHICS_RASTER_DESTROY, (void **)&rsc->mHal.funcs.raster.destroy);
140 ret &= fn(RS_HAL_GRAPHICS_VERTEX_INIT, (void **)&rsc->mHal.funcs.vertex.init);
141 ret &= fn(RS_HAL_GRAPHICS_VERTEX_SET_ACTIVE, (void **)&rsc->mHal.funcs.vertex.setActive);
142 ret &= fn(RS_HAL_GRAPHICS_VERTEX_DESTROY, (void **)&rsc->mHal.funcs.vertex.destroy);
143 ret &= fn(RS_HAL_GRAPHICS_FRAGMENT_INIT, (void **)&rsc->mHal.funcs.fragment.init);
144 ret &= fn(RS_HAL_GRAPHICS_FRAGMENT_SET_ACTIVE, (void **)&rsc->mHal.funcs.fragment.setActive);
145 ret &= fn(RS_HAL_GRAPHICS_FRAGMENT_DESTROY, (void **)&rsc->mHal.funcs.fragment.destroy);
146 ret &= fn(RS_HAL_GRAPHICS_MESH_INIT, (void **)&rsc->mHal.funcs.mesh.init);
147 ret &= fn(RS_HAL_GRAPHICS_MESH_DRAW, (void **)&rsc->mHal.funcs.mesh.draw);
Jason Samsb2e33f32015-03-16 12:12:21 -0700148 ret &= fn(RS_HAL_GRAPHICS_MESH_DESTROY, (void **)&rsc->mHal.funcs.mesh.destroy);
Jason Sams0ca7cba2015-03-11 15:22:38 -0700149 ret &= fn(RS_HAL_GRAPHICS_FB_INIT, (void **)&rsc->mHal.funcs.framebuffer.init);
150 ret &= fn(RS_HAL_GRAPHICS_FB_SET_ACTIVE, (void **)&rsc->mHal.funcs.framebuffer.setActive);
151 ret &= fn(RS_HAL_GRAPHICS_FB_DESTROY, (void **)&rsc->mHal.funcs.framebuffer.destroy);
152 ret &= fn(RS_HAL_GRAPHICS_STORE_INIT, (void **)&rsc->mHal.funcs.store.init);
153 ret &= fn(RS_HAL_GRAPHICS_STORE_SET_ACTIVE, (void **)&rsc->mHal.funcs.store.setActive);
154 ret &= fn(RS_HAL_GRAPHICS_STORE_DESTROY, (void **)&rsc->mHal.funcs.store.destroy);
155 }
156
157 return ret;
158}
159
160bool Context::loadRuntime(const char* filename) {
161 HalQueryVersion fnQueryVersion = nullptr;
162 HalQueryHal fnQueryHal = nullptr;
163 HalInit fnInit = nullptr;
164 HalAbort fnAbort = nullptr;
165
166
167 // TODO: store the driverSO somewhere so we can dlclose later
168 void *driverSO = nullptr;
169
170 driverSO = dlopen(filename, RTLD_LAZY);
171 if (driverSO == nullptr) {
172 ALOGE("Failed loading RS driver: %s", dlerror());
173 return false;
174 }
175
176 // Need to call dlerror() to clear buffer before using it for dlsym().
177 (void) dlerror();
178
179 fnQueryVersion = (HalQueryVersion) dlsym(driverSO, "rsdHalQueryVersion");
180 fnQueryHal = (HalQueryHal) dlsym(driverSO, "rsdHalQueryHal");
181 fnInit = (HalInit) dlsym(driverSO, "rsdHalInit");
182 fnAbort = (HalAbort) dlsym(driverSO, "rsdHalAbort");
183 uint32_t version_major = 0;
184 uint32_t version_minor = 0;
185
186 if ((fnQueryVersion == nullptr) || (fnQueryHal == nullptr) ||
187 (fnInit == nullptr) || (fnAbort == nullptr)) {
188
189 ALOGE("Failed to find hal setup entry points: %s", dlerror());
190 goto error;
191 }
192
193 if (!fnQueryVersion(&version_major, &version_minor)) {
194 ALOGE("Error checking RS driver version, %s", filename);
195 goto error;
196 }
197
198 if (!LoadHalTable(this, fnQueryHal, mIsGraphicsContext)) {
199 ALOGE("Error loading RS HAL table, %s", filename);
200 goto error;
201 }
202
203 if (!(*fnInit)(this, 0, 0)) {
204 ALOGE("Hal init failed, %s", filename);
205 goto error;
206 }
207
Stephen Hines4c368af2015-05-06 00:43:02 -0700208 // Only map in the actual driver name if we successfully load the runtime.
209 mDriverName = filename;
210
Jason Sams0ca7cba2015-03-11 15:22:38 -0700211 return true;
212
213
214error:
215 if (fnAbort != nullptr) {
216 fnAbort(this);
217 }
218 dlclose(driverSO);
219 return false;
220}
221
222
223
224bool Context::loadDriver(bool forceDefault) {
225 bool loadDefault = true;
226
227 // Provide a mechanism for dropping in a different RS driver.
228#ifndef RS_COMPATIBILITY_LIB
229#ifdef OVERRIDE_RS_DRIVER
230#define XSTR(S) #S
231#define STR(S) XSTR(S)
232#define OVERRIDE_RS_DRIVER_STRING STR(OVERRIDE_RS_DRIVER)
233
234 if (!forceDefault) {
235 if (loadRuntime(OVERRIDE_RS_DRIVER_STRING)) {
236 ALOGV("Successfully loaded runtime: %s", OVERRIDE_RS_DRIVER_STRING);
237 loadDefault = false;
238 } else {
239 ALOGE("Failed to load runtime %s, loading default", OVERRIDE_RS_DRIVER_STRING);
240 }
241 }
242#undef XSTR
243#undef STR
244#endif // OVERRIDE_RS_DRIVER
245
246 if (loadDefault) {
247 if (!loadRuntime("libRSDriver.so")) {
248 ALOGE("Failed to load default runtime!");
249 return false;
250 }
251 }
252#else // RS_COMPATIBILITY_LIB
Miao Wang6b1b1732015-03-13 14:37:56 -0700253 if (!LoadHalTable(this, rsdHalQueryHal, false)) {
254 ALOGE("Error loading RS HAL table");
255 return false;
256 }
Jason Sams0ca7cba2015-03-11 15:22:38 -0700257 if (rsdHalInit(this, 0, 0) != true) {
258 return false;
259 }
260#endif
261
262 return true;
263}
264
265
266