1 /*
2 * Copyright (C) 2016 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 "rsovCore.h"
18
19 #include <malloc.h>
20 #include <sched.h>
21 #include <string.h>
22 #include <sys/resource.h>
23 #include <sys/syscall.h>
24 #include <sys/types.h>
25
26 #include "cpu_ref/rsd_cpu.h"
27 #include "rsContext.h"
28 #include "rsovAllocation.h"
29 #include "rsovContext.h"
30 #include "rsovElement.h"
31 #include "rsovSampler.h"
32 #include "rsovScript.h"
33 #include "rsovScriptGroup.h"
34 #include "rsovType.h"
35
36 namespace android {
37 namespace renderscript {
38
39 namespace {
40
SetPriority(const Context * rsc,int32_t priority)41 void SetPriority(const Context *rsc, int32_t priority) {
42 RSoVHal *dc = (RSoVHal *)rsc->mHal.drv;
43
44 dc->mCpuRef->setPriority(priority);
45 }
46
Shutdown(Context * rsc)47 void Shutdown(Context *rsc) {
48 RSoVHal *dc = (RSoVHal *)rsc->mHal.drv;
49 delete dc->mCpuRef;
50 free(dc);
51 rsc->mHal.drv = nullptr;
52 }
53
AllocRuntimeMem(size_t size,uint32_t flags)54 void *AllocRuntimeMem(size_t size, uint32_t flags) {
55 void* buffer = calloc(size, sizeof(char));
56 return buffer;
57 }
58
FreeRuntimeMem(void * ptr)59 void FreeRuntimeMem(void* ptr) {
60 free(ptr);
61 }
62
lookupRuntimeStubs(Context * pContext,char const * name)63 const RsdCpuReference::CpuSymbol *lookupRuntimeStubs(
64 Context *pContext, char const *name) {
65 return nullptr;
66 }
67
68 } // anonymous namespace
69
70 namespace rsov {
71
72 namespace {
73
lookupCpuScript(Context * rsc,const Script * s)74 RsdCpuReference::CpuScript *lookupCpuScript(Context *rsc, const Script *s) {
75 if (RSoVScript::isScriptCpuBacked(s)) {
76 return reinterpret_cast<RsdCpuReference::CpuScript *>(s->mHal.drv);
77 }
78
79 RSoVScript *rsovScript = reinterpret_cast<RSoVScript *>(s->mHal.drv);
80 return rsovScript->getCpuScript();
81 }
82
83 } // anonymous namespace
84
85 } // namespace rsov
86
87
rsdHalQueryHal(RsHalInitEnums entry,void ** fnPtr)88 extern "C" bool rsdHalQueryHal(RsHalInitEnums entry, void **fnPtr) {
89 switch (entry) {
90 case RS_HAL_ALLOCATION_INIT:
91 fnPtr[0] = (void *)rsovAllocationInit;
92 break;
93 case RS_HAL_ALLOCATION_INIT_OEM:
94 fnPtr[0] = (void *)nullptr;
95 break;
96 case RS_HAL_ALLOCATION_INIT_ADAPTER:
97 fnPtr[0] = (void *)rsovAllocationAdapterInit;
98 break;
99 case RS_HAL_ALLOCATION_DESTROY:
100 fnPtr[0] = (void *)rsovAllocationDestroy;
101 break;
102 case RS_HAL_ALLOCATION_GET_GRALLOC_BITS:
103 fnPtr[0] = (void *)rsovAllocationGrallocBits;
104 break;
105 case RS_HAL_ALLOCATION_DATA_1D:
106 fnPtr[0] = (void *)rsovAllocationData1D;
107 break;
108 case RS_HAL_ALLOCATION_DATA_2D:
109 fnPtr[0] = (void *)rsovAllocationData2D;
110 break;
111 case RS_HAL_ALLOCATION_DATA_3D:
112 fnPtr[0] = (void *)rsovAllocationData3D;
113 break;
114 case RS_HAL_ALLOCATION_READ_1D:
115 fnPtr[0] = (void *)rsovAllocationRead1D;
116 break;
117 case RS_HAL_ALLOCATION_READ_2D:
118 fnPtr[0] = (void *)rsovAllocationRead2D;
119 break;
120 case RS_HAL_ALLOCATION_READ_3D:
121 fnPtr[0] = (void *)rsovAllocationRead3D;
122 break;
123 case RS_HAL_ALLOCATION_LOCK_1D:
124 fnPtr[0] = (void *)rsovAllocationLock1D;
125 break;
126 case RS_HAL_ALLOCATION_UNLOCK_1D:
127 fnPtr[0] = (void *)rsovAllocationUnlock1D;
128 break;
129 case RS_HAL_ALLOCATION_COPY_1D:
130 fnPtr[0] = (void *)rsovAllocationData1D_alloc;
131 break;
132 case RS_HAL_ALLOCATION_COPY_2D:
133 fnPtr[0] = (void *)rsovAllocationData2D_alloc;
134 break;
135 case RS_HAL_ALLOCATION_COPY_3D:
136 fnPtr[0] = (void *)rsovAllocationData3D_alloc;
137 break;
138 case RS_HAL_ALLOCATION_ADAPTER_OFFSET:
139 fnPtr[0] = (void *)rsovAllocationAdapterOffset;
140 break;
141 case RS_HAL_ALLOCATION_RESIZE:
142 fnPtr[0] = (void *)rsovAllocationResize;
143 break;
144 case RS_HAL_ALLOCATION_SYNC_ALL:
145 fnPtr[0] = (void *)rsovAllocationSyncAll;
146 break;
147 case RS_HAL_ALLOCATION_MARK_DIRTY:
148 fnPtr[0] = (void *)rsovAllocationMarkDirty;
149 break;
150 case RS_HAL_ALLOCATION_GENERATE_MIPMAPS:
151 fnPtr[0] = (void *)rsovAllocationGenerateMipmaps;
152 break;
153 case RS_HAL_ALLOCATION_UPDATE_CACHED_OBJECT:
154 fnPtr[0] = (void *)rsovAllocationUpdateCachedObject;
155 break;
156 case RS_HAL_ALLOCATION_GET_POINTER:
157 fnPtr[0] = (void *)nullptr;
158 break;
159 case RS_HAL_ALLOCATION_SET_SURFACE:
160 fnPtr[0] = (void *)rsovAllocationSetSurface;
161 break;
162 case RS_HAL_ALLOCATION_IO_SEND:
163 fnPtr[0] = (void *)rsovAllocationIoSend;
164 break;
165 case RS_HAL_ALLOCATION_IO_RECEIVE:
166 fnPtr[0] = (void *)rsovAllocationIoReceive;
167 break;
168 case RS_HAL_ALLOCATION_ELEMENT_DATA:
169 fnPtr[0] = (void *)rsovAllocationElementData;
170 break;
171 case RS_HAL_ALLOCATION_ELEMENT_READ:
172 fnPtr[0] = (void *)rsovAllocationElementRead;
173 break;
174
175 case RS_HAL_CORE_SHUTDOWN:
176 fnPtr[0] = (void *)Shutdown;
177 break;
178 case RS_HAL_CORE_SET_PRIORITY:
179 fnPtr[0] = (void *)SetPriority;
180 break;
181 case RS_HAL_CORE_ALLOC_RUNTIME_MEM:
182 fnPtr[0] = (void *)AllocRuntimeMem;
183 break;
184 case RS_HAL_CORE_FREE_RUNTIME_MEM:
185 fnPtr[0] = (void *)FreeRuntimeMem;
186 break;
187 case RS_HAL_CORE_FINISH:
188 fnPtr[0] = (void *)nullptr;
189 break;
190
191 case RS_HAL_SCRIPT_INIT:
192 fnPtr[0] = (void *)rsovScriptInit;
193 break;
194 case RS_HAL_SCRIPT_INIT_INTRINSIC:
195 fnPtr[0] = (void *)rsovInitIntrinsic;
196 break;
197 case RS_HAL_SCRIPT_INVOKE_FUNCTION:
198 fnPtr[0] = (void *)rsovScriptInvokeFunction;
199 break;
200 case RS_HAL_SCRIPT_INVOKE_ROOT:
201 fnPtr[0] = (void *)rsovScriptInvokeRoot;
202 break;
203 case RS_HAL_SCRIPT_INVOKE_FOR_EACH:
204 fnPtr[0] = (void *)rsovScriptInvokeForEach;
205 break;
206 case RS_HAL_SCRIPT_INVOKE_INIT:
207 fnPtr[0] = (void *)rsovScriptInvokeInit;
208 break;
209 case RS_HAL_SCRIPT_INVOKE_FREE_CHILDREN:
210 fnPtr[0] = (void *)rsovScriptInvokeFreeChildren;
211 break;
212 case RS_HAL_SCRIPT_DESTROY:
213 fnPtr[0] = (void *)rsovScriptDestroy;
214 break;
215 case RS_HAL_SCRIPT_SET_GLOBAL_VAR:
216 fnPtr[0] = (void *)rsovScriptSetGlobalVar;
217 break;
218 case RS_HAL_SCRIPT_GET_GLOBAL_VAR:
219 fnPtr[0] = (void *)rsovScriptGetGlobalVar;
220 break;
221 case RS_HAL_SCRIPT_SET_GLOBAL_VAR_WITH_ELEMENT_DIM:
222 fnPtr[0] = (void *)rsovScriptSetGlobalVarWithElemDims;
223 break;
224 case RS_HAL_SCRIPT_SET_GLOBAL_BIND:
225 fnPtr[0] = (void *)rsovScriptSetGlobalBind;
226 break;
227 case RS_HAL_SCRIPT_SET_GLOBAL_OBJECT:
228 fnPtr[0] = (void *)rsovScriptSetGlobalObj;
229 break;
230 case RS_HAL_SCRIPT_INVOKE_FOR_EACH_MULTI:
231 fnPtr[0] = (void *)rsovScriptInvokeForEachMulti;
232 break;
233 case RS_HAL_SCRIPT_UPDATE_CACHED_OBJECT:
234 fnPtr[0] = (void *)rsovScriptUpdateCachedObject;
235 break;
236 case RS_HAL_SCRIPT_INVOKE_REDUCE:
237 fnPtr[0] = (void *)rsovScriptInvokeReduce;
238 break;
239
240 case RS_HAL_SAMPLER_INIT:
241 fnPtr[0] = (void *)rsovSamplerInit;
242 break;
243 case RS_HAL_SAMPLER_DESTROY:
244 fnPtr[0] = (void *)rsovSamplerDestroy;
245 break;
246 case RS_HAL_SAMPLER_UPDATE_CACHED_OBJECT:
247 fnPtr[0] = (void *)rsovSamplerUpdateCachedObject;
248 break;
249
250 case RS_HAL_TYPE_INIT:
251 fnPtr[0] = (void *)rsovTypeInit;
252 break;
253 case RS_HAL_TYPE_DESTROY:
254 fnPtr[0] = (void *)rsovTypeDestroy;
255 break;
256 case RS_HAL_TYPE_UPDATE_CACHED_OBJECT:
257 fnPtr[0] = (void *)rsovTypeUpdateCachedObject;
258 break;
259
260 case RS_HAL_ELEMENT_INIT:
261 fnPtr[0] = (void *)rsovElementInit;
262 break;
263 case RS_HAL_ELEMENT_DESTROY:
264 fnPtr[0] = (void *)rsovElementDestroy;
265 break;
266 case RS_HAL_ELEMENT_UPDATE_CACHED_OBJECT:
267 fnPtr[0] = (void *)rsovElementUpdateCachedObject;
268 break;
269
270 case RS_HAL_SCRIPT_GROUP_INIT:
271 fnPtr[0] = (void *)rsovScriptGroupInit;
272 break;
273 case RS_HAL_SCRIPT_GROUP_DESTROY:
274 fnPtr[0] = (void *)rsovScriptGroupDestroy;
275 break;
276 case RS_HAL_SCRIPT_GROUP_UPDATE_CACHED_OBJECT:
277 fnPtr[0] = (void *)nullptr;
278 break;
279 case RS_HAL_SCRIPT_GROUP_SET_INPUT:
280 fnPtr[0] = (void *)rsovScriptGroupSetInput;
281 break;
282 case RS_HAL_SCRIPT_GROUP_SET_OUTPUT:
283 fnPtr[0] = (void *)rsovScriptGroupSetOutput;
284 break;
285 case RS_HAL_SCRIPT_GROUP_EXECUTE:
286 fnPtr[0] = (void *)rsovScriptGroupExecute;
287 break;
288
289 // Ignore entries for the legacy graphics api,
290
291 default:
292 ALOGE("ERROR: unknown RenderScript HAL API query, %i", entry);
293 return false;
294 }
295
296 return true;
297 }
298
rsdHalAbort(RsContext)299 extern "C" void rsdHalAbort(RsContext) {}
300
rsdHalQueryVersion(uint32_t * major,uint32_t * minor)301 extern "C" bool rsdHalQueryVersion(uint32_t *major, uint32_t *minor) {
302 *major = RS_HAL_VERSION;
303 *minor = 0;
304 return true;
305 }
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
311 std::unique_ptr<RSoVHal> hal(new RSoVHal());
312 if (!hal) {
313 ALOGE("Failed creating RSoV driver hal.");
314 return false;
315 }
316
317 std::unique_ptr<rsov::RSoVContext> rsov(rsov::RSoVContext::create());
318 if (!rsov) {
319 ALOGE("RSoVContext::create for driver hal failed.");
320 return false;
321 }
322
323 std::unique_ptr<RsdCpuReference> cpuref(RsdCpuReference::create(rsc, version_major, version_minor,
324 &lookupRuntimeStubs,
325 &rsov::lookupCpuScript));
326 if (!cpuref) {
327 ALOGE("RsdCpuReference::create for driver hal failed.");
328 return false;
329 }
330
331 hal->mRSoV = rsov.release();
332 hal->mCpuRef = cpuref.release();
333 rsc->mHal.drv = hal.release();
334
335 return true;
336 }
337
338 } // namespace renderscript
339 } // namespace android
340