rsdCore.cpp revision 45e753a46e587c69b3b0d0c5138e88715a24a29a
1/*
2 * Copyright (C) 2011-2012 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 "../cpu_ref/rsd_cpu.h"
18
19#include "rsdCore.h"
20#include "rsdAllocation.h"
21#include "rsdBcc.h"
22#include "rsdElement.h"
23#include "rsdType.h"
24#ifndef RS_COMPATIBILITY_LIB
25    #include "rsdGL.h"
26    #include "rsdPath.h"
27    #include "rsdProgramStore.h"
28    #include "rsdProgramRaster.h"
29    #include "rsdProgramVertex.h"
30    #include "rsdProgramFragment.h"
31    #include "rsdMesh.h"
32    #include "rsdFrameBuffer.h"
33#else
34    #include <dlfcn.h>
35#endif
36#include "rsdSampler.h"
37#include "rsdScriptGroup.h"
38
39#include <malloc.h>
40#include "rsContext.h"
41
42#include <sys/types.h>
43#include <sys/resource.h>
44#include <sched.h>
45#include <sys/syscall.h>
46#include <string.h>
47
48using namespace android;
49using namespace android::renderscript;
50
51static void Shutdown(Context *rsc);
52static void SetPriority(const Context *rsc, int32_t priority);
53
54#ifndef RS_COMPATIBILITY_LIB
55    #define NATIVE_FUNC(a) a
56#else
57    #define NATIVE_FUNC(a) nullptr
58#endif
59
60static RsdHalFunctions FunctionTable = {
61    NATIVE_FUNC(rsdGLInit),
62    NATIVE_FUNC(rsdGLShutdown),
63    NATIVE_FUNC(rsdGLSetSurface),
64    NATIVE_FUNC(rsdGLSwap),
65
66    Shutdown,
67    nullptr,
68    SetPriority,
69    rsdAllocRuntimeMem,
70    rsdFreeRuntimeMem,
71    {
72        rsdScriptInit,
73        rsdInitIntrinsic,
74        rsdScriptInvokeFunction,
75        rsdScriptInvokeRoot,
76        rsdScriptInvokeForEach,
77        rsdScriptInvokeInit,
78        rsdScriptInvokeFreeChildren,
79        rsdScriptSetGlobalVar,
80        rsdScriptGetGlobalVar,
81        rsdScriptSetGlobalVarWithElemDims,
82        rsdScriptSetGlobalBind,
83        rsdScriptSetGlobalObj,
84        rsdScriptDestroy,
85        rsdScriptInvokeForEachMulti,
86        rsdScriptUpdateCachedObject
87    },
88
89    {
90        rsdAllocationInit,
91        rsdAllocationDestroy,
92        rsdAllocationGrallocBits,
93        rsdAllocationResize,
94        rsdAllocationSyncAll,
95        rsdAllocationMarkDirty,
96        NATIVE_FUNC(rsdAllocationSetSurface),
97        NATIVE_FUNC(rsdAllocationIoSend),
98        NATIVE_FUNC(rsdAllocationIoReceive),
99        rsdAllocationData1D,
100        rsdAllocationData2D,
101        rsdAllocationData3D,
102        rsdAllocationRead1D,
103        rsdAllocationRead2D,
104        rsdAllocationRead3D,
105        rsdAllocationLock1D,
106        rsdAllocationUnlock1D,
107        rsdAllocationData1D_alloc,
108        rsdAllocationData2D_alloc,
109        rsdAllocationData3D_alloc,
110        rsdAllocationElementData1D,
111        rsdAllocationElementData2D,
112        rsdAllocationGenerateMipmaps,
113        rsdAllocationUpdateCachedObject
114    },
115
116
117    {
118        NATIVE_FUNC(rsdProgramStoreInit),
119        NATIVE_FUNC(rsdProgramStoreSetActive),
120        NATIVE_FUNC(rsdProgramStoreDestroy)
121    },
122
123    {
124        NATIVE_FUNC(rsdProgramRasterInit),
125        NATIVE_FUNC(rsdProgramRasterSetActive),
126        NATIVE_FUNC(rsdProgramRasterDestroy)
127    },
128
129    {
130        NATIVE_FUNC(rsdProgramVertexInit),
131        NATIVE_FUNC(rsdProgramVertexSetActive),
132        NATIVE_FUNC(rsdProgramVertexDestroy)
133    },
134
135    {
136        NATIVE_FUNC(rsdProgramFragmentInit),
137        NATIVE_FUNC(rsdProgramFragmentSetActive),
138        NATIVE_FUNC(rsdProgramFragmentDestroy)
139    },
140
141    {
142        NATIVE_FUNC(rsdMeshInit),
143        NATIVE_FUNC(rsdMeshDraw),
144        NATIVE_FUNC(rsdMeshDestroy)
145    },
146
147    {
148        NATIVE_FUNC(rsdPathInitStatic),
149        NATIVE_FUNC(rsdPathInitDynamic),
150        NATIVE_FUNC(rsdPathDraw),
151        NATIVE_FUNC(rsdPathDestroy)
152    },
153
154    {
155        rsdSamplerInit,
156        rsdSamplerDestroy,
157        rsdSamplerUpdateCachedObject
158    },
159
160    {
161        NATIVE_FUNC(rsdFrameBufferInit),
162        NATIVE_FUNC(rsdFrameBufferSetActive),
163        NATIVE_FUNC(rsdFrameBufferDestroy)
164    },
165
166    {
167        rsdScriptGroupInit,
168        rsdScriptGroupSetInput,
169        rsdScriptGroupSetOutput,
170        rsdScriptGroupExecute,
171        rsdScriptGroupDestroy,
172        nullptr
173    },
174
175    {
176        rsdTypeInit,
177        rsdTypeDestroy,
178        rsdTypeUpdateCachedObject
179    },
180
181    {
182        rsdElementInit,
183        rsdElementDestroy,
184        rsdElementUpdateCachedObject
185    },
186
187    nullptr // finish
188};
189
190extern const RsdCpuReference::CpuSymbol * rsdLookupRuntimeStub(Context * pContext, char const* name);
191
192static RsdCpuReference::CpuScript * LookupScript(Context *, const Script *s) {
193    return (RsdCpuReference::CpuScript *)s->mHal.drv;
194}
195
196#ifdef RS_COMPATIBILITY_LIB
197typedef void (*sAllocationDestroyFnPtr) (const Context *rsc, Allocation *alloc);
198typedef void (*sAllocationIoSendFnPtr) (const Context *rsc, Allocation *alloc);
199typedef void (*sAllocationSetSurfaceFnPtr) (const Context *rsc, Allocation *alloc, ANativeWindow *nw);
200static sAllocationDestroyFnPtr sAllocationDestroy;
201static sAllocationIoSendFnPtr sAllocationIoSend;
202static sAllocationSetSurfaceFnPtr sAllocationSetSurface;
203
204static bool loadIOSuppLibSyms() {
205    void* handleIO = nullptr;
206    handleIO = dlopen("libRSSupportIO.so", RTLD_LAZY | RTLD_LOCAL);
207    if (handleIO == nullptr) {
208        ALOGE("Couldn't load libRSSupportIO.so");
209        return false;
210    }
211    sAllocationDestroy = (sAllocationDestroyFnPtr)dlsym(handleIO, "rscAllocationDestroy");
212    if (sAllocationDestroy==nullptr) {
213        ALOGE("Failed to initialize sAllocationDestroy");
214        return false;
215    }
216    sAllocationIoSend = (sAllocationIoSendFnPtr)dlsym(handleIO, "rscAllocationIoSend");
217    if (sAllocationIoSend==nullptr) {
218        ALOGE("Failed to initialize sAllocationIoSend");
219        return false;
220    }
221    sAllocationSetSurface = (sAllocationSetSurfaceFnPtr)dlsym(handleIO, "rscAllocationSetSurface");
222    if (sAllocationSetSurface==nullptr) {
223        ALOGE("Failed to initialize sAllocationIoSend");
224        return false;
225    }
226    return true;
227}
228#endif
229
230extern "C" bool rsdHalInit(RsContext c, uint32_t version_major,
231                           uint32_t version_minor) {
232    Context *rsc = (Context*) c;
233#ifdef RS_COMPATIBILITY_LIB
234    if (loadIOSuppLibSyms()) {
235        FunctionTable.allocation.destroy = sAllocationDestroy;
236        FunctionTable.allocation.ioSend = sAllocationIoSend;
237        FunctionTable.allocation.setSurface = sAllocationSetSurface;
238    }
239#endif
240    rsc->mHal.funcs = FunctionTable;
241    RsdHal *dc = (RsdHal *)calloc(1, sizeof(RsdHal));
242    if (!dc) {
243        ALOGE("Calloc for driver hal failed.");
244        return false;
245    }
246    rsc->mHal.drv = dc;
247
248    dc->mCpuRef = RsdCpuReference::create(rsc, version_major, version_minor,
249                                          &rsdLookupRuntimeStub, &LookupScript);
250    if (!dc->mCpuRef) {
251        ALOGE("RsdCpuReference::create for driver hal failed.");
252        rsc->mHal.drv = nullptr;
253        free(dc);
254        return false;
255    }
256
257#ifndef RS_COMPATIBILITY_LIB
258    // Set a callback for compiler setup here.
259    if (false) {
260        dc->mCpuRef->setSetupCompilerCallback(nullptr);
261    }
262#endif
263
264    return true;
265}
266
267
268void SetPriority(const Context *rsc, int32_t priority) {
269    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
270
271    dc->mCpuRef->setPriority(priority);
272
273#ifndef RS_COMPATIBILITY_LIB
274    if (dc->mHasGraphics) {
275        rsdGLSetPriority(rsc, priority);
276    }
277#endif
278}
279
280void Shutdown(Context *rsc) {
281    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
282    delete dc->mCpuRef;
283    free(dc);
284    rsc->mHal.drv = nullptr;
285}
286
287void* rsdAllocRuntimeMem(size_t size, uint32_t flags) {
288    void* buffer = calloc(size, sizeof(char));
289    return buffer;
290}
291
292void rsdFreeRuntimeMem(void* ptr) {
293    free(ptr);
294}
295