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 #ifndef RS_HAL_H
18 #define RS_HAL_H
19 
20 #include <rsInternalDefines.h>
21 
22 /**
23  * The interface for loading RenderScript drivers
24  *
25  * The startup sequence is
26  *
27  * 1: dlopen driver
28  * 2: Query driver version with rsdHalQueryVersion()
29  * 3: Fill in HAL pointer table with calls to rsdHalQueryHAL()
30  * 4: Initialize the context with rsdHalInit()
31  *
32  * If any of these functions return false, the loading of the
33  * driver will abort and the reference driver will be used.
34  * rsdHalAbort() will be called to clean up any partially
35  * allocated state.
36  *
37  * A driver should return FALSE for any conditions that will
38  * prevent the driver from working normally.
39  *
40  *
41  * If these are successful, the driver will be loaded and used
42  * normally.  Teardown will use the normal
43  * context->mHal.funcs.shutdown() path.  There will be no call
44  * to rsdHalAbort().
45  *
46  *
47  */
48 
49 
50 struct ANativeWindow;
51 
52 namespace android {
53 namespace renderscript {
54 
55 class Context;
56 class ObjectBase;
57 class Element;
58 class Type;
59 class Allocation;
60 class Script;
61 class ScriptKernelID;
62 class ScriptFieldID;
63 class ScriptMethodID;
64 class ScriptC;
65 class ScriptGroup;
66 class ScriptGroupBase;
67 class Path;
68 class Program;
69 class ProgramStore;
70 class ProgramRaster;
71 class ProgramVertex;
72 class ProgramFragment;
73 class Mesh;
74 class Sampler;
75 class FBOCache;
76 
77 /**
78  * Define the internal object types.  This ia a mirror of the
79  * definition in rs_types.rsh except with the p value typed
80  * correctly.
81  *
82  * p = pointer to internal object implementation
83  * r = reserved by libRS runtime
84  * v1 = Mirror of p->mHal.drv
85  * v2 = reserved for use by vendor drivers
86  */
87 
88 #ifndef __LP64__
89 #define RS_BASE_OBJ(_t_) typedef struct { const _t_* p; } __attribute__((packed, aligned(4)))
90 #else
91 #define RS_BASE_OBJ(_t_) typedef struct { const _t_* p; const void* r; const void* v1; const void* v2; }
92 #endif
93 
94 RS_BASE_OBJ(ObjectBase) rs_object_base;
95 RS_BASE_OBJ(Element) rs_element;
96 RS_BASE_OBJ(Type) rs_type;
97 RS_BASE_OBJ(Allocation) rs_allocation;
98 RS_BASE_OBJ(Sampler) rs_sampler;
99 RS_BASE_OBJ(Script) rs_script;
100 RS_BASE_OBJ(ScriptGroup) rs_script_group;
101 
102 #ifndef __LP64__
103 typedef struct { const int* p; } __attribute__((packed, aligned(4))) rs_mesh;
104 typedef struct { const int* p; } __attribute__((packed, aligned(4))) rs_program_fragment;
105 typedef struct { const int* p; } __attribute__((packed, aligned(4))) rs_program_vertex;
106 typedef struct { const int* p; } __attribute__((packed, aligned(4))) rs_program_raster;
107 typedef struct { const int* p; } __attribute__((packed, aligned(4))) rs_program_store;
108 typedef struct { const int* p; } __attribute__((packed, aligned(4))) rs_font;
109 #endif // __LP64__
110 
111 
112 typedef void *(*RsHalSymbolLookupFunc)(void *usrptr, char const *symbolName);
113 
114 /**
115  * Script management functions
116  */
117 typedef struct {
118     bool (*initGraphics)(const Context *);
119     void (*shutdownGraphics)(const Context *);
120     bool (*setSurface)(const Context *, uint32_t w, uint32_t h, RsNativeWindow);
121     void (*swap)(const Context *);
122 
123     void (*shutdownDriver)(Context *);
124     void (*setPriority)(const Context *, int32_t priority);
125 
126     void* (*allocRuntimeMem)(size_t size, uint32_t flags);
127     void (*freeRuntimeMem)(void* ptr);
128 
129     struct {
130         bool (*init)(const Context *rsc, ScriptC *s,
131                      char const *resName,
132                      char const *cacheDir,
133                      uint8_t const *bitcode,
134                      size_t bitcodeSize,
135                      uint32_t flags);
136         bool (*initIntrinsic)(const Context *rsc, Script *s,
137                               RsScriptIntrinsicID iid,
138                               Element *e);
139 
140         void (*invokeFunction)(const Context *rsc, Script *s,
141                                uint32_t slot,
142                                const void *params,
143                                size_t paramLength);
144         int (*invokeRoot)(const Context *rsc, Script *s);
145         void (*invokeForEach)(const Context *rsc,
146                               Script *s,
147                               uint32_t slot,
148                               const Allocation * ain,
149                               Allocation * aout,
150                               const void * usr,
151                               size_t usrLen,
152                               const RsScriptCall *sc);
153         void (*invokeInit)(const Context *rsc, Script *s);
154         void (*invokeFreeChildren)(const Context *rsc, Script *s);
155 
156         void (*setGlobalVar)(const Context *rsc, const Script *s,
157                              uint32_t slot,
158                              void *data,
159                              size_t dataLength);
160         void (*getGlobalVar)(const Context *rsc, const Script *s,
161                              uint32_t slot,
162                              void *data,
163                              size_t dataLength);
164         void (*setGlobalVarWithElemDims)(const Context *rsc, const Script *s,
165                                          uint32_t slot,
166                                          void *data,
167                                          size_t dataLength,
168                                          const Element *e,
169                                          const uint32_t *dims,
170                                          size_t dimLength);
171         void (*setGlobalBind)(const Context *rsc, const Script *s,
172                               uint32_t slot,
173                               Allocation *data);
174         void (*setGlobalObj)(const Context *rsc, const Script *s,
175                              uint32_t slot,
176                              ObjectBase *data);
177 
178         void (*destroy)(const Context *rsc, Script *s);
179         void (*invokeForEachMulti)(const Context *rsc,
180                                    Script *s,
181                                    uint32_t slot,
182                                    const Allocation ** ains,
183                                    size_t inLen,
184                                    Allocation * aout,
185                                    const void * usr,
186                                    size_t usrLen,
187                                    const RsScriptCall *sc);
188         void (*updateCachedObject)(const Context *rsc, const Script *, rs_script *obj);
189     } script;
190 
191     struct {
192         bool (*init)(const Context *rsc, Allocation *alloc, bool forceZero);
193         bool (*initOem)(const Context *rsc, Allocation *alloc, bool forceZero, void *usrPtr);
194         bool (*initAdapter)(const Context *rsc, Allocation *alloc);
195         void (*destroy)(const Context *rsc, Allocation *alloc);
196         uint32_t (*grallocBits)(const Context *rsc, Allocation *alloc);
197 
198         void (*resize)(const Context *rsc, const Allocation *alloc, const Type *newType,
199                        bool zeroNew);
200         void (*syncAll)(const Context *rsc, const Allocation *alloc, RsAllocationUsageType src);
201         void (*markDirty)(const Context *rsc, const Allocation *alloc);
202 
203         void (*setSurface)(const Context *rsc, Allocation *alloc, ANativeWindow *sur);
204         void (*ioSend)(const Context *rsc, Allocation *alloc);
205 
206         /**
207          * A new gralloc buffer is in use. The pointers and strides in
208          * mHal.drvState.lod[0-2] will be updated with the new values.
209          *
210          * The new gralloc handle is provided in mHal.state.nativeBuffer
211          *
212          */
213         void (*ioReceive)(const Context *rsc, Allocation *alloc);
214 
215         void (*data1D)(const Context *rsc, const Allocation *alloc,
216                        uint32_t xoff, uint32_t lod, size_t count,
217                        const void *data, size_t sizeBytes);
218         void (*data2D)(const Context *rsc, const Allocation *alloc,
219                        uint32_t xoff, uint32_t yoff, uint32_t lod,
220                        RsAllocationCubemapFace face, uint32_t w, uint32_t h,
221                        const void *data, size_t sizeBytes, size_t stride);
222         void (*data3D)(const Context *rsc, const Allocation *alloc,
223                        uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
224                        uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes,
225                        size_t stride);
226 
227         void (*read1D)(const Context *rsc, const Allocation *alloc,
228                        uint32_t xoff, uint32_t lod, size_t count,
229                        void *data, size_t sizeBytes);
230         void (*read2D)(const Context *rsc, const Allocation *alloc,
231                        uint32_t xoff, uint32_t yoff, uint32_t lod,
232                        RsAllocationCubemapFace face, uint32_t w, uint32_t h,
233                        void *data, size_t sizeBytes, size_t stride);
234         void (*read3D)(const Context *rsc, const Allocation *alloc,
235                        uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
236                        uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes,
237                        size_t stride);
238 
239         // Lock and unlock make a 1D region of memory available to the CPU
240         // for direct access by pointer.  Once unlock is called control is
241         // returned to the SOC driver.
242         void * (*lock1D)(const Context *rsc, const Allocation *alloc);
243         void (*unlock1D)(const Context *rsc, const Allocation *alloc);
244 
245         // Allocation to allocation copies
246         void (*allocData1D)(const Context *rsc,
247                             const Allocation *dstAlloc,
248                             uint32_t dstXoff, uint32_t dstLod, size_t count,
249                             const Allocation *srcAlloc, uint32_t srcXoff, uint32_t srcLod);
250         void (*allocData2D)(const Context *rsc,
251                             const Allocation *dstAlloc,
252                             uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
253                             RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
254                             const Allocation *srcAlloc,
255                             uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
256                             RsAllocationCubemapFace srcFace);
257         void (*allocData3D)(const Context *rsc,
258                             const Allocation *dstAlloc,
259                             uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
260                             uint32_t dstLod,
261                             uint32_t w, uint32_t h, uint32_t d,
262                             const Allocation *srcAlloc,
263                             uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
264                             uint32_t srcLod);
265 
266         void (*elementData)(const Context *rsc, const Allocation *alloc,
267                             uint32_t x, uint32_t y, uint32_t z,
268                             const void *data, uint32_t elementOff, size_t sizeBytes);
269         void (*elementRead)(const Context *rsc, const Allocation *alloc,
270                             uint32_t x, uint32_t y, uint32_t z,
271                             void *data, uint32_t elementOff, size_t sizeBytes);
272 
273         void (*generateMipmaps)(const Context *rsc, const Allocation *alloc);
274 
275         void (*updateCachedObject)(const Context *rsc, const Allocation *alloc, rs_allocation *obj);
276 
277         void (*adapterOffset)(const Context *rsc, const Allocation *alloc);
278 
279         void (*getPointer)(const Context *rsc, const Allocation *alloc,
280                            uint32_t lod, RsAllocationCubemapFace face,
281                            uint32_t z, uint32_t array);
282     } allocation;
283 
284     struct {
285         bool (*init)(const Context *rsc, const ProgramStore *ps);
286         void (*setActive)(const Context *rsc, const ProgramStore *ps);
287         void (*destroy)(const Context *rsc, const ProgramStore *ps);
288     } store;
289 
290     struct {
291         bool (*init)(const Context *rsc, const ProgramRaster *ps);
292         void (*setActive)(const Context *rsc, const ProgramRaster *ps);
293         void (*destroy)(const Context *rsc, const ProgramRaster *ps);
294     } raster;
295 
296     struct {
297         bool (*init)(const Context *rsc, const ProgramVertex *pv,
298                      const char* shader, size_t shaderLen,
299                      const char** textureNames, size_t textureNamesCount,
300                      const size_t *textureNamesLength);
301         void (*setActive)(const Context *rsc, const ProgramVertex *pv);
302         void (*destroy)(const Context *rsc, const ProgramVertex *pv);
303     } vertex;
304 
305     struct {
306         bool (*init)(const Context *rsc, const ProgramFragment *pf,
307                      const char* shader, size_t shaderLen,
308                      const char** textureNames, size_t textureNamesCount,
309                      const size_t *textureNamesLength);
310         void (*setActive)(const Context *rsc, const ProgramFragment *pf);
311         void (*destroy)(const Context *rsc, const ProgramFragment *pf);
312     } fragment;
313 
314     struct {
315         bool (*init)(const Context *rsc, const Mesh *m);
316         void (*draw)(const Context *rsc, const Mesh *m, uint32_t primIndex, uint32_t start, uint32_t len);
317         void (*destroy)(const Context *rsc, const Mesh *m);
318     } mesh;
319 
320     struct {
321         bool (*init)(const Context *rsc, const Sampler *m);
322         void (*destroy)(const Context *rsc, const Sampler *m);
323         void (*updateCachedObject)(const Context *rsc, const Sampler *s, rs_sampler *obj);
324     } sampler;
325 
326     struct {
327         bool (*init)(const Context *rsc, const FBOCache *fb);
328         void (*setActive)(const Context *rsc, const FBOCache *fb);
329         void (*destroy)(const Context *rsc, const FBOCache *fb);
330     } framebuffer;
331 
332     struct {
333         bool (*init)(const Context *rsc, ScriptGroupBase *sg);
334         void (*setInput)(const Context *rsc, const ScriptGroup *sg,
335                          const ScriptKernelID *kid, Allocation *);
336         void (*setOutput)(const Context *rsc, const ScriptGroup *sg,
337                           const ScriptKernelID *kid, Allocation *);
338         void (*execute)(const Context *rsc, const ScriptGroupBase *sg);
339         void (*destroy)(const Context *rsc, const ScriptGroupBase *sg);
340         void (*updateCachedObject)(const Context *rsc, const ScriptGroup *sg, rs_script_group *obj);
341     } scriptgroup;
342 
343     struct {
344         bool (*init)(const Context *rsc, const Type *m);
345         void (*destroy)(const Context *rsc, const Type *m);
346         void (*updateCachedObject)(const Context *rsc, const Type *s, rs_type *obj);
347     } type;
348 
349     struct {
350         bool (*init)(const Context *rsc, const Element *m);
351         void (*destroy)(const Context *rsc, const Element *m);
352         void (*updateCachedObject)(const Context *rsc, const Element *s, rs_element *obj);
353     } element;
354 
355     void (*finish)(const Context *rsc);
356 } RsdHalFunctions;
357 
358 
359 enum RsHalInitEnums {
360     RS_HAL_CORE_SHUTDOWN                                    = 1,
361     RS_HAL_CORE_SET_PRIORITY                                = 2,
362     RS_HAL_CORE_ALLOC_RUNTIME_MEM                           = 3,
363     RS_HAL_CORE_FREE_RUNTIME_MEM                            = 4,
364     RS_HAL_CORE_FINISH                                      = 5,
365 
366     RS_HAL_SCRIPT_INIT                                      = 1000,
367     RS_HAL_SCRIPT_INIT_INTRINSIC                            = 1001,
368     RS_HAL_SCRIPT_INVOKE_FUNCTION                           = 1002,
369     RS_HAL_SCRIPT_INVOKE_ROOT                               = 1003,
370     RS_HAL_SCRIPT_INVOKE_FOR_EACH                           = 1004,
371     RS_HAL_SCRIPT_INVOKE_INIT                               = 1005,
372     RS_HAL_SCRIPT_INVOKE_FREE_CHILDREN                      = 1006,
373     RS_HAL_SCRIPT_SET_GLOBAL_VAR                            = 1007,
374     RS_HAL_SCRIPT_GET_GLOBAL_VAR                            = 1008,
375     RS_HAL_SCRIPT_SET_GLOBAL_VAR_WITH_ELEMENT_DIM           = 1009,
376     RS_HAL_SCRIPT_SET_GLOBAL_BIND                           = 1010,
377     RS_HAL_SCRIPT_SET_GLOBAL_OBJECT                         = 1011,
378     RS_HAL_SCRIPT_DESTROY                                   = 1012,
379     RS_HAL_SCRIPT_INVOKE_FOR_EACH_MULTI                     = 1013,
380     RS_HAL_SCRIPT_UPDATE_CACHED_OBJECT                      = 1014,
381 
382     RS_HAL_ALLOCATION_INIT                                  = 2000,
383     RS_HAL_ALLOCATION_INIT_ADAPTER                          = 2001,
384     RS_HAL_ALLOCATION_DESTROY                               = 2002,
385     RS_HAL_ALLOCATION_GET_GRALLOC_BITS                      = 2003,
386     RS_HAL_ALLOCATION_RESIZE                                = 2004,
387     RS_HAL_ALLOCATION_SYNC_ALL                              = 2005,
388     RS_HAL_ALLOCATION_MARK_DIRTY                            = 2006,
389     RS_HAL_ALLOCATION_SET_SURFACE                           = 2007,
390     RS_HAL_ALLOCATION_IO_SEND                               = 2008,
391     RS_HAL_ALLOCATION_IO_RECEIVE                            = 2009,
392     RS_HAL_ALLOCATION_DATA_1D                               = 2010,
393     RS_HAL_ALLOCATION_DATA_2D                               = 2011,
394     RS_HAL_ALLOCATION_DATA_3D                               = 2012,
395     RS_HAL_ALLOCATION_READ_1D                               = 2013,
396     RS_HAL_ALLOCATION_READ_2D                               = 2014,
397     RS_HAL_ALLOCATION_READ_3D                               = 2015,
398     RS_HAL_ALLOCATION_LOCK_1D                               = 2016,
399     RS_HAL_ALLOCATION_UNLOCK_1D                             = 2017,
400     RS_HAL_ALLOCATION_COPY_1D                               = 2018,
401     RS_HAL_ALLOCATION_COPY_2D                               = 2019,
402     RS_HAL_ALLOCATION_COPY_3D                               = 2020,
403     RS_HAL_ALLOCATION_ELEMENT_DATA                          = 2021,
404     RS_HAL_ALLOCATION_ELEMENT_READ                          = 2022,
405     RS_HAL_ALLOCATION_GENERATE_MIPMAPS                      = 2023,
406     RS_HAL_ALLOCATION_UPDATE_CACHED_OBJECT                  = 2024,
407     RS_HAL_ALLOCATION_ADAPTER_OFFSET                        = 2025,
408     RS_HAL_ALLOCATION_INIT_OEM                              = 2026,
409     RS_HAL_ALLOCATION_GET_POINTER                           = 2027,
410 
411     RS_HAL_SAMPLER_INIT                                     = 3000,
412     RS_HAL_SAMPLER_DESTROY                                  = 3001,
413     RS_HAL_SAMPLER_UPDATE_CACHED_OBJECT                     = 3002,
414 
415     RS_HAL_TYPE_INIT                                        = 4000,
416     RS_HAL_TYPE_DESTROY                                     = 4001,
417     RS_HAL_TYPE_UPDATE_CACHED_OBJECT                        = 4002,
418 
419     RS_HAL_ELEMENT_INIT                                     = 5000,
420     RS_HAL_ELEMENT_DESTROY                                  = 5001,
421     RS_HAL_ELEMENT_UPDATE_CACHED_OBJECT                     = 5002,
422 
423     RS_HAL_SCRIPT_GROUP_INIT                                = 6000,
424     RS_HAL_SCRIPT_GROUP_DESTROY                             = 6001,
425     RS_HAL_SCRIPT_GROUP_UPDATE_CACHED_OBJECT                = 6002,
426     RS_HAL_SCRIPT_GROUP_SET_INPUT                           = 6003,
427     RS_HAL_SCRIPT_GROUP_SET_OUTPUT                          = 6004,
428     RS_HAL_SCRIPT_GROUP_EXECUTE                             = 6005,
429 
430 
431 
432     RS_HAL_GRAPHICS_INIT                                    = 100001,
433     RS_HAL_GRAPHICS_SHUTDOWN                                = 100002,
434     RS_HAL_GRAPHICS_SWAP                                    = 100003,
435     RS_HAL_GRAPHICS_SET_SURFACE                             = 100004,
436     RS_HAL_GRAPHICS_RASTER_INIT                             = 101000,
437     RS_HAL_GRAPHICS_RASTER_SET_ACTIVE                       = 101001,
438     RS_HAL_GRAPHICS_RASTER_DESTROY                          = 101002,
439     RS_HAL_GRAPHICS_VERTEX_INIT                             = 102000,
440     RS_HAL_GRAPHICS_VERTEX_SET_ACTIVE                       = 102001,
441     RS_HAL_GRAPHICS_VERTEX_DESTROY                          = 102002,
442     RS_HAL_GRAPHICS_FRAGMENT_INIT                           = 103000,
443     RS_HAL_GRAPHICS_FRAGMENT_SET_ACTIVE                     = 103001,
444     RS_HAL_GRAPHICS_FRAGMENT_DESTROY                        = 103002,
445     RS_HAL_GRAPHICS_MESH_INIT                               = 104000,
446     RS_HAL_GRAPHICS_MESH_DRAW                               = 104001,
447     RS_HAL_GRAPHICS_MESH_DESTROY                            = 104002,
448     RS_HAL_GRAPHICS_FB_INIT                                 = 105000,
449     RS_HAL_GRAPHICS_FB_SET_ACTIVE                           = 105001,
450     RS_HAL_GRAPHICS_FB_DESTROY                              = 105002,
451     RS_HAL_GRAPHICS_STORE_INIT                              = 106000,
452     RS_HAL_GRAPHICS_STORE_SET_ACTIVE                        = 106001,
453     RS_HAL_GRAPHICS_STORE_DESTROY                           = 106002,
454 };
455 
456 }
457 }
458 
459 #ifdef __cplusplus
460 extern "C" {
461 #endif
462 
463 /**
464  * Get the major version number of the driver.  The major
465  * version should be the API version number
466  *
467  * The Minor version number is vendor specific
468  *
469  * return: False will abort loading the driver, true indicates
470  * success
471  */
472 bool rsdHalQueryVersion(uint32_t *version_major, uint32_t *version_minor);
473 
474 
475 /**
476  * Get an entry point in the driver HAL
477  *
478  * The driver should set the function pointer to its
479  * implementation of the function.  If it does not have an entry
480  * for an enum, its should set the function pointer to NULL
481  *
482  * Returning NULL is expected in cases during development as new
483  * entry points are added that a driver may not understand.  If
484  * the runtime receives a NULL it will decide if the function is
485  * required and will either continue loading or abort as needed.
486  *
487  *
488  * return: False will abort loading the driver, true indicates
489  * success
490  *
491  */
492 bool rsdHalQueryHal(android::renderscript::RsHalInitEnums entry, void **fnPtr);
493 
494 
495 /**
496  * Called to initialize the context for use with a driver.
497  *
498  * return: False will abort loading the driver, true indicates
499  * success
500  */
501 bool rsdHalInit(RsContext, uint32_t version_major, uint32_t version_minor);
502 
503 /**
504  * Called if one of the loading functions above returns false.
505  * This is to clean up any resources allocated during an error
506  * condition. If this path is called it means the normal
507  * context->mHal.funcs.shutdown() will not be called.
508  */
509 void rsdHalAbort(RsContext);
510 
511 #ifdef __cplusplus
512 }
513 #endif
514 
515 #endif
516