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 "MemChunk.h"
26    #include "rsdGL.h"
27    #include "rsdPath.h"
28    #include "rsdProgramStore.h"
29    #include "rsdProgramRaster.h"
30    #include "rsdProgramVertex.h"
31    #include "rsdProgramFragment.h"
32    #include "rsdMesh.h"
33    #include "rsdFrameBuffer.h"
34#endif
35#include "rsdSampler.h"
36#include "rsdScriptGroup.h"
37
38#include <malloc.h>
39#include "rsContext.h"
40
41#include <sys/types.h>
42#include <sys/resource.h>
43#include <sched.h>
44#include <sys/syscall.h>
45#include <string.h>
46
47using namespace android;
48using namespace android::renderscript;
49
50static void Shutdown(Context *rsc);
51static void SetPriority(const Context *rsc, int32_t priority);
52
53#ifndef RS_COMPATIBILITY_LIB
54    #define NATIVE_FUNC(a) a
55#else
56    #define NATIVE_FUNC(a) NULL
57#endif
58
59
60static RsdHalFunctions FunctionTable = {
61    NATIVE_FUNC(rsdGLInit),
62    NATIVE_FUNC(rsdGLShutdown),
63    NATIVE_FUNC(rsdGLSetSurface),
64    NATIVE_FUNC(rsdGLSwap),
65
66    Shutdown,
67    NULL,
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        NULL
173    },
174
175    {
176        rsdTypeInit,
177        rsdTypeDestroy,
178        rsdTypeUpdateCachedObject
179    },
180
181    {
182        rsdElementInit,
183        rsdElementDestroy,
184        rsdElementUpdateCachedObject
185    },
186
187    NULL // 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
196extern "C" bool rsdHalInit(RsContext c, uint32_t version_major,
197                           uint32_t version_minor) {
198    Context *rsc = (Context*) c;
199    rsc->mHal.funcs = FunctionTable;
200
201    RsdHal *dc = (RsdHal *)calloc(1, sizeof(RsdHal));
202    if (!dc) {
203        ALOGE("Calloc for driver hal failed.");
204        return false;
205    }
206    rsc->mHal.drv = dc;
207
208    dc->mCpuRef = RsdCpuReference::create(rsc, version_major, version_minor,
209                                          &rsdLookupRuntimeStub, &LookupScript);
210    if (!dc->mCpuRef) {
211        ALOGE("RsdCpuReference::create for driver hal failed.");
212        rsc->mHal.drv = NULL;
213        free(dc);
214        return false;
215    }
216
217#ifndef RS_COMPATIBILITY_LIB
218    // Set a callback for compiler setup here.
219    if (false) {
220        dc->mCpuRef->setSetupCompilerCallback(NULL);
221    }
222
223    // Set a callback for switching MemChunk's allocator here.
224    // Note that the allocation function must return page-aligned memory, so
225    // that it can be mprotected properly (i.e. code should be written and
226    // later switched to read+execute only).
227    if (false) {
228        MemChunk::registerAllocFreeCallbacks(
229                rsc->mHal.funcs.allocRuntimeMem,
230                rsc->mHal.funcs.freeRuntimeMem);
231    }
232#endif
233
234    return true;
235}
236
237
238void SetPriority(const Context *rsc, int32_t priority) {
239    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
240
241    dc->mCpuRef->setPriority(priority);
242
243#ifndef RS_COMPATIBILITY_LIB
244    if (dc->mHasGraphics) {
245        rsdGLSetPriority(rsc, priority);
246    }
247#endif
248}
249
250void Shutdown(Context *rsc) {
251    RsdHal *dc = (RsdHal *)rsc->mHal.drv;
252    delete dc->mCpuRef;
253    free(dc);
254    rsc->mHal.drv = NULL;
255}
256
257void* rsdAllocRuntimeMem(size_t size, uint32_t flags) {
258    void* buffer = calloc(size, sizeof(char));
259    return buffer;
260}
261
262void rsdFreeRuntimeMem(void* ptr) {
263    free(ptr);
264}
265