rsdCore.cpp revision ddceab9a001f07a3395226c5e06e3b420720af0f
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#ifndef RS_COMPATIBILITY_LIB
23    #include "MemChunk.h"
24    #include "rsdGL.h"
25    #include "rsdPath.h"
26    #include "rsdProgramStore.h"
27    #include "rsdProgramRaster.h"
28    #include "rsdProgramVertex.h"
29    #include "rsdProgramFragment.h"
30    #include "rsdMesh.h"
31    #include "rsdFrameBuffer.h"
32#endif
33#include "rsdSampler.h"
34#include "rsdScriptGroup.h"
35
36#include <malloc.h>
37#include "rsContext.h"
38
39#include <sys/types.h>
40#include <sys/resource.h>
41#include <sched.h>
42#include <sys/syscall.h>
43#include <string.h>
44
45#ifndef RS_SERVER
46#include <cutils/properties.h>
47#endif
48
49using namespace android;
50using namespace android::renderscript;
51
52static void Shutdown(Context *rsc);
53static void SetPriority(const Context *rsc, int32_t priority);
54
55#ifndef RS_COMPATIBILITY_LIB
56    #define NATIVE_FUNC(a) a
57#else
58    #define NATIVE_FUNC(a) NULL
59#endif
60
61
62static RsdHalFunctions FunctionTable = {
63    NATIVE_FUNC(rsdGLInit),
64    NATIVE_FUNC(rsdGLShutdown),
65    NATIVE_FUNC(rsdGLSetSurface),
66    NATIVE_FUNC(rsdGLSwap),
67
68    Shutdown,
69    NULL,
70    SetPriority,
71    rsdAllocRuntimeMem,
72    rsdFreeRuntimeMem,
73    {
74        rsdScriptInit,
75        rsdInitIntrinsic,
76        rsdScriptInvokeFunction,
77        rsdScriptInvokeRoot,
78        rsdScriptInvokeForEach,
79        rsdScriptInvokeInit,
80        rsdScriptInvokeFreeChildren,
81        rsdScriptSetGlobalVar,
82        rsdScriptGetGlobalVar,
83        rsdScriptSetGlobalVarWithElemDims,
84        rsdScriptSetGlobalBind,
85        rsdScriptSetGlobalObj,
86        rsdScriptDestroy
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    },
114
115
116    {
117        NATIVE_FUNC(rsdProgramStoreInit),
118        NATIVE_FUNC(rsdProgramStoreSetActive),
119        NATIVE_FUNC(rsdProgramStoreDestroy)
120    },
121
122    {
123        NATIVE_FUNC(rsdProgramRasterInit),
124        NATIVE_FUNC(rsdProgramRasterSetActive),
125        NATIVE_FUNC(rsdProgramRasterDestroy)
126    },
127
128    {
129        NATIVE_FUNC(rsdProgramVertexInit),
130        NATIVE_FUNC(rsdProgramVertexSetActive),
131        NATIVE_FUNC(rsdProgramVertexDestroy)
132    },
133
134    {
135        NATIVE_FUNC(rsdProgramFragmentInit),
136        NATIVE_FUNC(rsdProgramFragmentSetActive),
137        NATIVE_FUNC(rsdProgramFragmentDestroy)
138    },
139
140    {
141        NATIVE_FUNC(rsdMeshInit),
142        NATIVE_FUNC(rsdMeshDraw),
143        NATIVE_FUNC(rsdMeshDestroy)
144    },
145
146    {
147        NATIVE_FUNC(rsdPathInitStatic),
148        NATIVE_FUNC(rsdPathInitDynamic),
149        NATIVE_FUNC(rsdPathDraw),
150        NATIVE_FUNC(rsdPathDestroy)
151    },
152
153    {
154        rsdSamplerInit,
155        rsdSamplerDestroy
156    },
157
158    {
159        NATIVE_FUNC(rsdFrameBufferInit),
160        NATIVE_FUNC(rsdFrameBufferSetActive),
161        NATIVE_FUNC(rsdFrameBufferDestroy)
162    },
163
164    {
165        rsdScriptGroupInit,
166        rsdScriptGroupSetInput,
167        rsdScriptGroupSetOutput,
168        rsdScriptGroupExecute,
169        rsdScriptGroupDestroy
170    }
171
172
173};
174
175extern const RsdCpuReference::CpuSymbol * rsdLookupRuntimeStub(Context * pContext, char const* name);
176
177static RsdCpuReference::CpuScript * LookupScript(Context *, const Script *s) {
178    return (RsdCpuReference::CpuScript *)s->mHal.drv;
179}
180
181extern "C" bool rsdHalInit(RsContext c, uint32_t version_major,
182                           uint32_t version_minor) {
183    Context *rsc = (Context*) c;
184    rsc->mHal.funcs = FunctionTable;
185
186    RsdHal *dc = (RsdHal *)calloc(1, sizeof(RsdHal));
187    if (!dc) {
188        ALOGE("Calloc for driver hal failed.");
189        return false;
190    }
191    rsc->mHal.drv = dc;
192
193    dc->mCpuRef = RsdCpuReference::create(rsc, version_major, version_minor,
194                                          &rsdLookupRuntimeStub, &LookupScript);
195    if (!dc->mCpuRef) {
196        ALOGE("RsdCpuReference::create for driver hal failed.");
197        free(dc);
198        return false;
199    }
200
201#ifndef RS_COMPATIBILITY_LIB
202    // Set a callback for compiler setup here.
203    if (false) {
204        dc->mCpuRef->setSetupCompilerCallback(NULL);
205    }
206
207    // Set a callback for switching MemChunk's allocator here.
208    // Note that the allocation function must return page-aligned memory, so
209    // that it can be mprotected properly (i.e. code should be written and
210    // later switched to read+execute only).
211    if (false) {
212        MemChunk::registerAllocFreeCallbacks(
213                rsc->mHal.funcs.allocRuntimeMem,
214                rsc->mHal.funcs.freeRuntimeMem);
215    }
216#endif
217
218    return true;
219}
220
221
222void SetPriority(const Context *rsc, int32_t priority) {
223    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
224
225    dc->mCpuRef->setPriority(priority);
226
227#ifndef RS_COMPATIBILITY_LIB
228    if (dc->mHasGraphics) {
229        rsdGLSetPriority(rsc, priority);
230    }
231#endif
232}
233
234void Shutdown(Context *rsc) {
235    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
236    delete dc->mCpuRef;
237    rsc->mHal.drv = NULL;
238}
239
240void* rsdAllocRuntimeMem(size_t size, uint32_t flags) {
241    void* buffer = calloc(size, sizeof(char));
242    return buffer;
243}
244
245void rsdFreeRuntimeMem(void* ptr) {
246    free(ptr);
247}
248