summaryrefslogtreecommitdiff
path: root/driver/rsdCore.cpp
blob: f70b79b6811630e9608ad6779bdeea10c16d8f9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 * Copyright (C) 2011-2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "../cpu_ref/rsd_cpu.h"

#include "rsdCore.h"
#include "rsdAllocation.h"
#include "rsdBcc.h"
#include "rsdElement.h"
#include "rsdType.h"
#ifndef RS_COMPATIBILITY_LIB
    #include "rsdGL.h"
    #include "rsdProgramStore.h"
    #include "rsdProgramRaster.h"
    #include "rsdProgramVertex.h"
    #include "rsdProgramFragment.h"
    #include "rsdMesh.h"
    #include "rsdFrameBuffer.h"
#else
    #include <dlfcn.h>
#endif
#include "rsdSampler.h"
#include "rsdScriptGroup.h"

#include <malloc.h>
#include "rsContext.h"

#include <sys/types.h>
#include <sys/resource.h>
#include <sched.h>
#include <sys/syscall.h>
#include <string.h>

using namespace android;
using namespace android::renderscript;

static void Shutdown(Context *rsc);
static void SetPriority(const Context *rsc, int32_t priority);

#ifndef RS_COMPATIBILITY_LIB
    #define NATIVE_FUNC(a) a
#else
    #define NATIVE_FUNC(a) nullptr
#endif

extern "C" bool rsdHalQueryHal(RsHalInitEnums entry, void **fnPtr) {
    switch(entry) {
    case RS_HAL_CORE_SHUTDOWN:
        fnPtr[0] = (void *)Shutdown; break;
    case RS_HAL_CORE_SET_PRIORITY:
        fnPtr[0] = (void *)SetPriority; break;
    case RS_HAL_CORE_ALLOC_RUNTIME_MEM:
        fnPtr[0] = (void *)rsdAllocRuntimeMem; break;
    case RS_HAL_CORE_FREE_RUNTIME_MEM:
        fnPtr[0] = (void *)rsdFreeRuntimeMem; break;
    case RS_HAL_CORE_FINISH:
        fnPtr[0] = (void *)nullptr; break;

    case RS_HAL_SCRIPT_INIT:
        fnPtr[0] = (void *)rsdScriptInit; break;
    case RS_HAL_SCRIPT_INIT_INTRINSIC:
        fnPtr[0] = (void *)rsdInitIntrinsic; break;
    case RS_HAL_SCRIPT_INVOKE_FUNCTION:
        fnPtr[0] = (void *)rsdScriptInvokeFunction; break;
    case RS_HAL_SCRIPT_INVOKE_ROOT:
        fnPtr[0] = (void *)rsdScriptInvokeRoot; break;
    case RS_HAL_SCRIPT_INVOKE_FOR_EACH:
        fnPtr[0] = (void *)rsdScriptInvokeForEach; break;
    case RS_HAL_SCRIPT_INVOKE_INIT:
        fnPtr[0] = (void *)rsdScriptInvokeInit; break;
    case RS_HAL_SCRIPT_INVOKE_FREE_CHILDREN:
        fnPtr[0] = (void *)rsdScriptInvokeFreeChildren; break;
    case RS_HAL_SCRIPT_SET_GLOBAL_VAR:
        fnPtr[0] = (void *)rsdScriptSetGlobalVar; break;
    case RS_HAL_SCRIPT_GET_GLOBAL_VAR:
        fnPtr[0] = (void *)rsdScriptGetGlobalVar; break;
    case RS_HAL_SCRIPT_SET_GLOBAL_VAR_WITH_ELEMENT_DIM:
        fnPtr[0] = (void *)rsdScriptSetGlobalVarWithElemDims; break;
    case RS_HAL_SCRIPT_SET_GLOBAL_BIND:
        fnPtr[0] = (void *)rsdScriptSetGlobalBind; break;
    case RS_HAL_SCRIPT_SET_GLOBAL_OBJECT:
        fnPtr[0] = (void *)rsdScriptSetGlobalObj; break;
    case RS_HAL_SCRIPT_DESTROY:
        fnPtr[0] = (void *)rsdScriptDestroy; break;
    case RS_HAL_SCRIPT_INVOKE_FOR_EACH_MULTI:
        fnPtr[0] = (void *)rsdScriptInvokeForEachMulti; break;
    case RS_HAL_SCRIPT_UPDATE_CACHED_OBJECT:
        fnPtr[0] = (void *)rsdScriptUpdateCachedObject; break;
    case RS_HAL_SCRIPT_INVOKE_REDUCE:
        fnPtr[0] = (void *)rsdScriptInvokeReduce; break;

    case RS_HAL_ALLOCATION_INIT:
        fnPtr[0] = (void *)rsdAllocationInit; break;
    case RS_HAL_ALLOCATION_INIT_OEM:
        fnPtr[0] = (void *)nullptr; break;
    case RS_HAL_ALLOCATION_INIT_ADAPTER:
        fnPtr[0] = (void *)rsdAllocationAdapterInit; break;
    case RS_HAL_ALLOCATION_DESTROY:
        fnPtr[0] = (void *)rsdAllocationDestroy; break;
    case RS_HAL_ALLOCATION_GET_GRALLOC_BITS:
        fnPtr[0] = (void *)rsdAllocationGrallocBits; break;
    case RS_HAL_ALLOCATION_RESIZE:
        fnPtr[0] = (void *)rsdAllocationResize; break;
    case RS_HAL_ALLOCATION_SYNC_ALL:
        fnPtr[0] = (void *)rsdAllocationSyncAll; break;
    case RS_HAL_ALLOCATION_MARK_DIRTY:
        fnPtr[0] = (void *)rsdAllocationMarkDirty; break;
    case RS_HAL_ALLOCATION_SET_SURFACE:
        fnPtr[0] = (void *)NATIVE_FUNC(rsdAllocationSetSurface); break;
    case RS_HAL_ALLOCATION_IO_SEND:
        fnPtr[0] = (void *)NATIVE_FUNC(rsdAllocationIoSend); break;
    case RS_HAL_ALLOCATION_IO_RECEIVE:
        fnPtr[0] = (void *)NATIVE_FUNC(rsdAllocationIoReceive); break;
    case RS_HAL_ALLOCATION_DATA_1D:
        fnPtr[0] = (void *)rsdAllocationData1D; break;
    case RS_HAL_ALLOCATION_DATA_2D:
        fnPtr[0] = (void *)rsdAllocationData2D; break;
    case RS_HAL_ALLOCATION_DATA_3D:
        fnPtr[0] = (void *)rsdAllocationData3D; break;
    case RS_HAL_ALLOCATION_READ_1D:
        fnPtr[0] = (void *)rsdAllocationRead1D; break;
    case RS_HAL_ALLOCATION_READ_2D:
        fnPtr[0] = (void *)rsdAllocationRead2D; break;
    case RS_HAL_ALLOCATION_READ_3D:
        fnPtr[0] = (void *)rsdAllocationRead3D; break;
    case RS_HAL_ALLOCATION_LOCK_1D:
        fnPtr[0] = (void *)rsdAllocationLock1D; break;
    case RS_HAL_ALLOCATION_UNLOCK_1D:
        fnPtr[0] = (void *)rsdAllocationUnlock1D; break;
    case RS_HAL_ALLOCATION_COPY_1D:
        fnPtr[0] = (void *)rsdAllocationData1D_alloc; break;
    case RS_HAL_ALLOCATION_COPY_2D:
        fnPtr[0] = (void *)rsdAllocationData2D_alloc; break;
    case RS_HAL_ALLOCATION_COPY_3D:
        fnPtr[0] = (void *)rsdAllocationData3D_alloc; break;
    case RS_HAL_ALLOCATION_ELEMENT_DATA:
        fnPtr[0] = (void *)rsdAllocationElementData; break;
    case RS_HAL_ALLOCATION_ELEMENT_READ:
        fnPtr[0] = (void *)rsdAllocationElementRead; break;
    case RS_HAL_ALLOCATION_GENERATE_MIPMAPS:
        fnPtr[0] = (void *)rsdAllocationGenerateMipmaps; break;
    case RS_HAL_ALLOCATION_UPDATE_CACHED_OBJECT:
        fnPtr[0] = (void *)rsdAllocationUpdateCachedObject; break;
    case RS_HAL_ALLOCATION_ADAPTER_OFFSET:
        fnPtr[0] = (void *)rsdAllocationAdapterOffset; break;
    case RS_HAL_ALLOCATION_GET_POINTER:
        fnPtr[0] = (void *)nullptr; break;
#ifdef RS_COMPATIBILITY_LIB
    case RS_HAL_ALLOCATION_INIT_STRIDED:
        fnPtr[0] = (void *)rsdAllocationInitStrided; break;
#endif

    case RS_HAL_SAMPLER_INIT:
        fnPtr[0] = (void *)rsdSamplerInit; break;
    case RS_HAL_SAMPLER_DESTROY:
        fnPtr[0] = (void *)rsdSamplerDestroy; break;
    case RS_HAL_SAMPLER_UPDATE_CACHED_OBJECT:
        fnPtr[0] = (void *)rsdSamplerUpdateCachedObject; break;

    case RS_HAL_TYPE_INIT:
        fnPtr[0] = (void *)rsdTypeInit; break;
    case RS_HAL_TYPE_DESTROY:
        fnPtr[0] = (void *)rsdTypeDestroy; break;
    case RS_HAL_TYPE_UPDATE_CACHED_OBJECT:
        fnPtr[0] = (void *)rsdTypeUpdateCachedObject; break;

    case RS_HAL_ELEMENT_INIT:
        fnPtr[0] = (void *)rsdElementInit; break;
    case RS_HAL_ELEMENT_DESTROY:
        fnPtr[0] = (void *)rsdElementDestroy; break;
    case RS_HAL_ELEMENT_UPDATE_CACHED_OBJECT:
        fnPtr[0] = (void *)rsdElementUpdateCachedObject; break;

    case RS_HAL_SCRIPT_GROUP_INIT:
        fnPtr[0] = (void *)rsdScriptGroupInit; break;
    case RS_HAL_SCRIPT_GROUP_DESTROY:
        fnPtr[0] = (void *)rsdScriptGroupDestroy; break;
    case RS_HAL_SCRIPT_GROUP_UPDATE_CACHED_OBJECT:
        fnPtr[0] = (void *)nullptr; break;
    case RS_HAL_SCRIPT_GROUP_SET_INPUT:
        fnPtr[0] = (void *)rsdScriptGroupSetInput; break;
    case RS_HAL_SCRIPT_GROUP_SET_OUTPUT:
        fnPtr[0] = (void *)rsdScriptGroupSetOutput; break;
    case RS_HAL_SCRIPT_GROUP_EXECUTE:
        fnPtr[0] = (void *)rsdScriptGroupExecute; break;



    // Functions below this point are for the legacy graphics api,
    // vendor drivers are NOT expected to implement these.  They will never be called
    // for an external driver.
#ifndef RS_COMPATIBILITY_LIB
    case RS_HAL_GRAPHICS_INIT:
        fnPtr[0] = (void *)rsdGLInit; break;
    case RS_HAL_GRAPHICS_SHUTDOWN:
        fnPtr[0] = (void *)rsdGLShutdown; break;
    case RS_HAL_GRAPHICS_SWAP:
        fnPtr[0] = (void *)rsdGLSwap; break;
    case RS_HAL_GRAPHICS_SET_SURFACE:
        fnPtr[0] = (void *)rsdGLSetSurface; break;
    case RS_HAL_GRAPHICS_RASTER_INIT:
        fnPtr[0] = (void *)rsdProgramRasterInit; break;
    case RS_HAL_GRAPHICS_RASTER_SET_ACTIVE:
        fnPtr[0] = (void *)rsdProgramRasterSetActive; break;
    case RS_HAL_GRAPHICS_RASTER_DESTROY:
        fnPtr[0] = (void *)rsdProgramRasterDestroy; break;
    case RS_HAL_GRAPHICS_VERTEX_INIT:
        fnPtr[0] = (void *)rsdProgramVertexInit; break;
    case RS_HAL_GRAPHICS_VERTEX_SET_ACTIVE:
        fnPtr[0] = (void *)rsdProgramVertexSetActive; break;
    case RS_HAL_GRAPHICS_VERTEX_DESTROY:
        fnPtr[0] = (void *)rsdProgramVertexDestroy; break;
    case RS_HAL_GRAPHICS_FRAGMENT_INIT:
        fnPtr[0] = (void *)rsdProgramFragmentInit; break;
    case RS_HAL_GRAPHICS_FRAGMENT_SET_ACTIVE:
        fnPtr[0] = (void *)rsdProgramFragmentSetActive; break;
    case RS_HAL_GRAPHICS_FRAGMENT_DESTROY:
        fnPtr[0] = (void *)rsdProgramFragmentDestroy; break;
    case RS_HAL_GRAPHICS_MESH_INIT:
        fnPtr[0] = (void *)rsdMeshInit; break;
    case RS_HAL_GRAPHICS_MESH_DRAW:
        fnPtr[0] = (void *)rsdMeshDraw; break;
    case RS_HAL_GRAPHICS_MESH_DESTROY:
        fnPtr[0] = (void *)rsdMeshDestroy; break;
    case RS_HAL_GRAPHICS_FB_INIT:
        fnPtr[0] = (void *)rsdFrameBufferInit; break;
    case RS_HAL_GRAPHICS_FB_SET_ACTIVE:
        fnPtr[0] = (void *)rsdFrameBufferSetActive; break;
    case RS_HAL_GRAPHICS_FB_DESTROY:
        fnPtr[0] = (void *)rsdFrameBufferDestroy; break;
    case RS_HAL_GRAPHICS_STORE_INIT:
        fnPtr[0] = (void *)rsdProgramStoreInit; break;
    case RS_HAL_GRAPHICS_STORE_SET_ACTIVE:
        fnPtr[0] = (void *)rsdProgramStoreSetActive; break;
    case RS_HAL_GRAPHICS_STORE_DESTROY:
        fnPtr[0] = (void *)rsdProgramStoreDestroy; break;
#endif

    default:
        ALOGE("ERROR: unknown RenderScript HAL API query, %i", entry);
        return false;
    }

    return true;
}

extern "C" void rsdHalAbort(RsContext) {

}


extern "C" bool rsdHalQueryVersion(uint32_t *major, uint32_t *minor) {
    *major = 23;
    *minor = 0;
    return true;
}



extern const RsdCpuReference::CpuSymbol * rsdLookupRuntimeStub(Context * pContext, char const* name);

static RsdCpuReference::CpuScript * LookupScript(Context *, const Script *s) {
    return (RsdCpuReference::CpuScript *)s->mHal.drv;
}

#ifdef RS_COMPATIBILITY_LIB
typedef void (*sAllocationDestroyFnPtr) (const Context *rsc, Allocation *alloc);
typedef void (*sAllocationIoSendFnPtr) (const Context *rsc, Allocation *alloc);
typedef void (*sAllocationSetSurfaceFnPtr) (const Context *rsc, Allocation *alloc, ANativeWindow *nw);
static sAllocationDestroyFnPtr sAllocationDestroy;
static sAllocationIoSendFnPtr sAllocationIoSend;
static sAllocationSetSurfaceFnPtr sAllocationSetSurface;

static bool loadIOSuppLibSyms() {
    void* handleIO = nullptr;
    handleIO = dlopen("libRSSupportIO.so", RTLD_LAZY | RTLD_LOCAL);
    if (handleIO == nullptr) {
        ALOGE("Couldn't load libRSSupportIO.so");
        return false;
    }
    sAllocationDestroy = (sAllocationDestroyFnPtr)dlsym(handleIO, "rscAllocationDestroy");
    if (sAllocationDestroy==nullptr) {
        ALOGE("Failed to initialize sAllocationDestroy");
        return false;
    }
    sAllocationIoSend = (sAllocationIoSendFnPtr)dlsym(handleIO, "rscAllocationIoSend");
    if (sAllocationIoSend==nullptr) {
        ALOGE("Failed to initialize sAllocationIoSend");
        return false;
    }
    sAllocationSetSurface = (sAllocationSetSurfaceFnPtr)dlsym(handleIO, "rscAllocationSetSurface");
    if (sAllocationSetSurface==nullptr) {
        ALOGE("Failed to initialize sAllocationIoSend");
        return false;
    }
    return true;
}
#endif

extern "C" bool rsdHalInit(RsContext c, uint32_t version_major,
                           uint32_t version_minor) {
    Context *rsc = (Context*) c;
#ifdef RS_COMPATIBILITY_LIB
    if (loadIOSuppLibSyms()) {
        rsc->mHal.funcs.allocation.destroy = sAllocationDestroy;
        rsc->mHal.funcs.allocation.ioSend = sAllocationIoSend;
        rsc->mHal.funcs.allocation.setSurface = sAllocationSetSurface;
    }
#endif

    RsdHal *dc = (RsdHal *)calloc(1, sizeof(RsdHal));
    if (!dc) {
        ALOGE("Calloc for driver hal failed.");
        return false;
    }
    rsc->mHal.drv = dc;

    dc->mCpuRef = RsdCpuReference::create(rsc, version_major, version_minor,
                                          &rsdLookupRuntimeStub, &LookupScript);
    if (!dc->mCpuRef) {
        ALOGE("RsdCpuReference::create for driver hal failed.");
        rsc->mHal.drv = nullptr;
        free(dc);
        return false;
    }

    return true;
}


void SetPriority(const Context *rsc, int32_t priority) {
    RsdHal *dc = (RsdHal *)rsc->mHal.drv;

    dc->mCpuRef->setPriority(priority);

#ifndef RS_COMPATIBILITY_LIB
    if (dc->mHasGraphics) {
        rsdGLSetPriority(rsc, priority);
    }
#endif
}

void Shutdown(Context *rsc) {
    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
    delete dc->mCpuRef;
    free(dc);
    rsc->mHal.drv = nullptr;
}

void* rsdAllocRuntimeMem(size_t size, uint32_t flags) {
    void* buffer = calloc(size, sizeof(char));
    return buffer;
}

void rsdFreeRuntimeMem(void* ptr) {
    free(ptr);
}