Home | History | Annotate | Download | only in driver
      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 "rsdProgramStore.h"
     27     #include "rsdProgramRaster.h"
     28     #include "rsdProgramVertex.h"
     29     #include "rsdProgramFragment.h"
     30     #include "rsdMesh.h"
     31     #include "rsdFrameBuffer.h"
     32 #else
     33     #include <dlfcn.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 
     47 using namespace android;
     48 using namespace android::renderscript;
     49 
     50 static void Shutdown(Context *rsc);
     51 static 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) nullptr
     57 #endif
     58 
     59 extern "C" bool rsdHalQueryHal(RsHalInitEnums entry, void **fnPtr) {
     60     switch(entry) {
     61     case RS_HAL_CORE_SHUTDOWN:
     62         fnPtr[0] = (void *)Shutdown; break;
     63     case RS_HAL_CORE_SET_PRIORITY:
     64         fnPtr[0] = (void *)SetPriority; break;
     65     case RS_HAL_CORE_ALLOC_RUNTIME_MEM:
     66         fnPtr[0] = (void *)rsdAllocRuntimeMem; break;
     67     case RS_HAL_CORE_FREE_RUNTIME_MEM:
     68         fnPtr[0] = (void *)rsdFreeRuntimeMem; break;
     69     case RS_HAL_CORE_FINISH:
     70         fnPtr[0] = (void *)nullptr; break;
     71 
     72     case RS_HAL_SCRIPT_INIT:
     73         fnPtr[0] = (void *)rsdScriptInit; break;
     74     case RS_HAL_SCRIPT_INIT_INTRINSIC:
     75         fnPtr[0] = (void *)rsdInitIntrinsic; break;
     76     case RS_HAL_SCRIPT_INVOKE_FUNCTION:
     77         fnPtr[0] = (void *)rsdScriptInvokeFunction; break;
     78     case RS_HAL_SCRIPT_INVOKE_ROOT:
     79         fnPtr[0] = (void *)rsdScriptInvokeRoot; break;
     80     case RS_HAL_SCRIPT_INVOKE_FOR_EACH:
     81         fnPtr[0] = (void *)rsdScriptInvokeForEach; break;
     82     case RS_HAL_SCRIPT_INVOKE_INIT:
     83         fnPtr[0] = (void *)rsdScriptInvokeInit; break;
     84     case RS_HAL_SCRIPT_INVOKE_FREE_CHILDREN:
     85         fnPtr[0] = (void *)rsdScriptInvokeFreeChildren; break;
     86     case RS_HAL_SCRIPT_SET_GLOBAL_VAR:
     87         fnPtr[0] = (void *)rsdScriptSetGlobalVar; break;
     88     case RS_HAL_SCRIPT_GET_GLOBAL_VAR:
     89         fnPtr[0] = (void *)rsdScriptGetGlobalVar; break;
     90     case RS_HAL_SCRIPT_SET_GLOBAL_VAR_WITH_ELEMENT_DIM:
     91         fnPtr[0] = (void *)rsdScriptSetGlobalVarWithElemDims; break;
     92     case RS_HAL_SCRIPT_SET_GLOBAL_BIND:
     93         fnPtr[0] = (void *)rsdScriptSetGlobalBind; break;
     94     case RS_HAL_SCRIPT_SET_GLOBAL_OBJECT:
     95         fnPtr[0] = (void *)rsdScriptSetGlobalObj; break;
     96     case RS_HAL_SCRIPT_DESTROY:
     97         fnPtr[0] = (void *)rsdScriptDestroy; break;
     98     case RS_HAL_SCRIPT_INVOKE_FOR_EACH_MULTI:
     99         fnPtr[0] = (void *)rsdScriptInvokeForEachMulti; break;
    100     case RS_HAL_SCRIPT_UPDATE_CACHED_OBJECT:
    101         fnPtr[0] = (void *)rsdScriptUpdateCachedObject; break;
    102     case RS_HAL_SCRIPT_INVOKE_REDUCE:
    103         fnPtr[0] = (void *)rsdScriptInvokeReduce; break;
    104 
    105     case RS_HAL_ALLOCATION_INIT:
    106         fnPtr[0] = (void *)rsdAllocationInit; break;
    107     case RS_HAL_ALLOCATION_INIT_OEM:
    108         fnPtr[0] = (void *)nullptr; break;
    109     case RS_HAL_ALLOCATION_INIT_ADAPTER:
    110         fnPtr[0] = (void *)rsdAllocationAdapterInit; break;
    111     case RS_HAL_ALLOCATION_DESTROY:
    112         fnPtr[0] = (void *)rsdAllocationDestroy; break;
    113     case RS_HAL_ALLOCATION_GET_GRALLOC_BITS:
    114         fnPtr[0] = (void *)rsdAllocationGrallocBits; break;
    115     case RS_HAL_ALLOCATION_RESIZE:
    116         fnPtr[0] = (void *)rsdAllocationResize; break;
    117     case RS_HAL_ALLOCATION_SYNC_ALL:
    118         fnPtr[0] = (void *)rsdAllocationSyncAll; break;
    119     case RS_HAL_ALLOCATION_MARK_DIRTY:
    120         fnPtr[0] = (void *)rsdAllocationMarkDirty; break;
    121     case RS_HAL_ALLOCATION_SET_SURFACE:
    122         fnPtr[0] = (void *)NATIVE_FUNC(rsdAllocationSetSurface); break;
    123     case RS_HAL_ALLOCATION_IO_SEND:
    124         fnPtr[0] = (void *)NATIVE_FUNC(rsdAllocationIoSend); break;
    125     case RS_HAL_ALLOCATION_IO_RECEIVE:
    126         fnPtr[0] = (void *)NATIVE_FUNC(rsdAllocationIoReceive); break;
    127     case RS_HAL_ALLOCATION_DATA_1D:
    128         fnPtr[0] = (void *)rsdAllocationData1D; break;
    129     case RS_HAL_ALLOCATION_DATA_2D:
    130         fnPtr[0] = (void *)rsdAllocationData2D; break;
    131     case RS_HAL_ALLOCATION_DATA_3D:
    132         fnPtr[0] = (void *)rsdAllocationData3D; break;
    133     case RS_HAL_ALLOCATION_READ_1D:
    134         fnPtr[0] = (void *)rsdAllocationRead1D; break;
    135     case RS_HAL_ALLOCATION_READ_2D:
    136         fnPtr[0] = (void *)rsdAllocationRead2D; break;
    137     case RS_HAL_ALLOCATION_READ_3D:
    138         fnPtr[0] = (void *)rsdAllocationRead3D; break;
    139     case RS_HAL_ALLOCATION_LOCK_1D:
    140         fnPtr[0] = (void *)rsdAllocationLock1D; break;
    141     case RS_HAL_ALLOCATION_UNLOCK_1D:
    142         fnPtr[0] = (void *)rsdAllocationUnlock1D; break;
    143     case RS_HAL_ALLOCATION_COPY_1D:
    144         fnPtr[0] = (void *)rsdAllocationData1D_alloc; break;
    145     case RS_HAL_ALLOCATION_COPY_2D:
    146         fnPtr[0] = (void *)rsdAllocationData2D_alloc; break;
    147     case RS_HAL_ALLOCATION_COPY_3D:
    148         fnPtr[0] = (void *)rsdAllocationData3D_alloc; break;
    149     case RS_HAL_ALLOCATION_ELEMENT_DATA:
    150         fnPtr[0] = (void *)rsdAllocationElementData; break;
    151     case RS_HAL_ALLOCATION_ELEMENT_READ:
    152         fnPtr[0] = (void *)rsdAllocationElementRead; break;
    153     case RS_HAL_ALLOCATION_GENERATE_MIPMAPS:
    154         fnPtr[0] = (void *)rsdAllocationGenerateMipmaps; break;
    155     case RS_HAL_ALLOCATION_UPDATE_CACHED_OBJECT:
    156         fnPtr[0] = (void *)rsdAllocationUpdateCachedObject; break;
    157     case RS_HAL_ALLOCATION_ADAPTER_OFFSET:
    158         fnPtr[0] = (void *)rsdAllocationAdapterOffset; break;
    159     case RS_HAL_ALLOCATION_GET_POINTER:
    160         fnPtr[0] = (void *)nullptr; break;
    161 #ifdef RS_COMPATIBILITY_LIB
    162     case RS_HAL_ALLOCATION_INIT_STRIDED:
    163         fnPtr[0] = (void *)rsdAllocationInitStrided; break;
    164 #endif
    165 
    166     case RS_HAL_SAMPLER_INIT:
    167         fnPtr[0] = (void *)rsdSamplerInit; break;
    168     case RS_HAL_SAMPLER_DESTROY:
    169         fnPtr[0] = (void *)rsdSamplerDestroy; break;
    170     case RS_HAL_SAMPLER_UPDATE_CACHED_OBJECT:
    171         fnPtr[0] = (void *)rsdSamplerUpdateCachedObject; break;
    172 
    173     case RS_HAL_TYPE_INIT:
    174         fnPtr[0] = (void *)rsdTypeInit; break;
    175     case RS_HAL_TYPE_DESTROY:
    176         fnPtr[0] = (void *)rsdTypeDestroy; break;
    177     case RS_HAL_TYPE_UPDATE_CACHED_OBJECT:
    178         fnPtr[0] = (void *)rsdTypeUpdateCachedObject; break;
    179 
    180     case RS_HAL_ELEMENT_INIT:
    181         fnPtr[0] = (void *)rsdElementInit; break;
    182     case RS_HAL_ELEMENT_DESTROY:
    183         fnPtr[0] = (void *)rsdElementDestroy; break;
    184     case RS_HAL_ELEMENT_UPDATE_CACHED_OBJECT:
    185         fnPtr[0] = (void *)rsdElementUpdateCachedObject; break;
    186 
    187     case RS_HAL_SCRIPT_GROUP_INIT:
    188         fnPtr[0] = (void *)rsdScriptGroupInit; break;
    189     case RS_HAL_SCRIPT_GROUP_DESTROY:
    190         fnPtr[0] = (void *)rsdScriptGroupDestroy; break;
    191     case RS_HAL_SCRIPT_GROUP_UPDATE_CACHED_OBJECT:
    192         fnPtr[0] = (void *)nullptr; break;
    193     case RS_HAL_SCRIPT_GROUP_SET_INPUT:
    194         fnPtr[0] = (void *)rsdScriptGroupSetInput; break;
    195     case RS_HAL_SCRIPT_GROUP_SET_OUTPUT:
    196         fnPtr[0] = (void *)rsdScriptGroupSetOutput; break;
    197     case RS_HAL_SCRIPT_GROUP_EXECUTE:
    198         fnPtr[0] = (void *)rsdScriptGroupExecute; break;
    199 
    200 
    201 
    202     // Functions below this point are for the legacy graphics api,
    203     // vendor drivers are NOT expected to implement these.  They will never be called
    204     // for an external driver.
    205 #ifndef RS_COMPATIBILITY_LIB
    206     case RS_HAL_GRAPHICS_INIT:
    207         fnPtr[0] = (void *)rsdGLInit; break;
    208     case RS_HAL_GRAPHICS_SHUTDOWN:
    209         fnPtr[0] = (void *)rsdGLShutdown; break;
    210     case RS_HAL_GRAPHICS_SWAP:
    211         fnPtr[0] = (void *)rsdGLSwap; break;
    212     case RS_HAL_GRAPHICS_SET_SURFACE:
    213         fnPtr[0] = (void *)rsdGLSetSurface; break;
    214     case RS_HAL_GRAPHICS_RASTER_INIT:
    215         fnPtr[0] = (void *)rsdProgramRasterInit; break;
    216     case RS_HAL_GRAPHICS_RASTER_SET_ACTIVE:
    217         fnPtr[0] = (void *)rsdProgramRasterSetActive; break;
    218     case RS_HAL_GRAPHICS_RASTER_DESTROY:
    219         fnPtr[0] = (void *)rsdProgramRasterDestroy; break;
    220     case RS_HAL_GRAPHICS_VERTEX_INIT:
    221         fnPtr[0] = (void *)rsdProgramVertexInit; break;
    222     case RS_HAL_GRAPHICS_VERTEX_SET_ACTIVE:
    223         fnPtr[0] = (void *)rsdProgramVertexSetActive; break;
    224     case RS_HAL_GRAPHICS_VERTEX_DESTROY:
    225         fnPtr[0] = (void *)rsdProgramVertexDestroy; break;
    226     case RS_HAL_GRAPHICS_FRAGMENT_INIT:
    227         fnPtr[0] = (void *)rsdProgramFragmentInit; break;
    228     case RS_HAL_GRAPHICS_FRAGMENT_SET_ACTIVE:
    229         fnPtr[0] = (void *)rsdProgramFragmentSetActive; break;
    230     case RS_HAL_GRAPHICS_FRAGMENT_DESTROY:
    231         fnPtr[0] = (void *)rsdProgramFragmentDestroy; break;
    232     case RS_HAL_GRAPHICS_MESH_INIT:
    233         fnPtr[0] = (void *)rsdMeshInit; break;
    234     case RS_HAL_GRAPHICS_MESH_DRAW:
    235         fnPtr[0] = (void *)rsdMeshDraw; break;
    236     case RS_HAL_GRAPHICS_MESH_DESTROY:
    237         fnPtr[0] = (void *)rsdMeshDestroy; break;
    238     case RS_HAL_GRAPHICS_FB_INIT:
    239         fnPtr[0] = (void *)rsdFrameBufferInit; break;
    240     case RS_HAL_GRAPHICS_FB_SET_ACTIVE:
    241         fnPtr[0] = (void *)rsdFrameBufferSetActive; break;
    242     case RS_HAL_GRAPHICS_FB_DESTROY:
    243         fnPtr[0] = (void *)rsdFrameBufferDestroy; break;
    244     case RS_HAL_GRAPHICS_STORE_INIT:
    245         fnPtr[0] = (void *)rsdProgramStoreInit; break;
    246     case RS_HAL_GRAPHICS_STORE_SET_ACTIVE:
    247         fnPtr[0] = (void *)rsdProgramStoreSetActive; break;
    248     case RS_HAL_GRAPHICS_STORE_DESTROY:
    249         fnPtr[0] = (void *)rsdProgramStoreDestroy; break;
    250 #endif
    251 
    252     default:
    253         ALOGE("ERROR: unknown RenderScript HAL API query, %i", entry);
    254         return false;
    255     }
    256 
    257     return true;
    258 }
    259 
    260 extern "C" void rsdHalAbort(RsContext) {
    261 
    262 }
    263 
    264 
    265 extern "C" bool rsdHalQueryVersion(uint32_t *major, uint32_t *minor) {
    266     *major = RS_HAL_VERSION;
    267     *minor = 0;
    268     return true;
    269 }
    270 
    271 
    272 
    273 extern const RsdCpuReference::CpuSymbol * rsdLookupRuntimeStub(Context * pContext, char const* name);
    274 
    275 static RsdCpuReference::CpuScript * LookupScript(Context *, const Script *s) {
    276     return (RsdCpuReference::CpuScript *)s->mHal.drv;
    277 }
    278 
    279 #ifdef RS_COMPATIBILITY_LIB
    280 typedef void (*sAllocationDestroyFnPtr) (const Context *rsc, Allocation *alloc);
    281 typedef void (*sAllocationIoSendFnPtr) (const Context *rsc, Allocation *alloc);
    282 typedef void (*sAllocationSetSurfaceFnPtr) (const Context *rsc, Allocation *alloc, ANativeWindow *nw);
    283 static sAllocationDestroyFnPtr sAllocationDestroy;
    284 static sAllocationIoSendFnPtr sAllocationIoSend;
    285 static sAllocationSetSurfaceFnPtr sAllocationSetSurface;
    286 
    287 static bool loadIOSuppLibSyms() {
    288     void* handleIO = nullptr;
    289     handleIO = dlopen("libRSSupportIO.so", RTLD_LAZY | RTLD_LOCAL);
    290     if (handleIO == nullptr) {
    291         ALOGE("Couldn't load libRSSupportIO.so");
    292         return false;
    293     }
    294     sAllocationDestroy = (sAllocationDestroyFnPtr)dlsym(handleIO, "rscAllocationDestroy");
    295     if (sAllocationDestroy==nullptr) {
    296         ALOGE("Failed to initialize sAllocationDestroy");
    297         return false;
    298     }
    299     sAllocationIoSend = (sAllocationIoSendFnPtr)dlsym(handleIO, "rscAllocationIoSend");
    300     if (sAllocationIoSend==nullptr) {
    301         ALOGE("Failed to initialize sAllocationIoSend");
    302         return false;
    303     }
    304     sAllocationSetSurface = (sAllocationSetSurfaceFnPtr)dlsym(handleIO, "rscAllocationSetSurface");
    305     if (sAllocationSetSurface==nullptr) {
    306         ALOGE("Failed to initialize sAllocationIoSend");
    307         return false;
    308     }
    309     return true;
    310 }
    311 #endif
    312 
    313 extern "C" bool rsdHalInit(RsContext c, uint32_t version_major,
    314                            uint32_t version_minor) {
    315     Context *rsc = (Context*) c;
    316 #ifdef RS_COMPATIBILITY_LIB
    317     if (loadIOSuppLibSyms()) {
    318         rsc->mHal.funcs.allocation.destroy = sAllocationDestroy;
    319         rsc->mHal.funcs.allocation.ioSend = sAllocationIoSend;
    320         rsc->mHal.funcs.allocation.setSurface = sAllocationSetSurface;
    321     }
    322 #endif
    323 
    324     RsdHal *dc = (RsdHal *)calloc(1, sizeof(RsdHal));
    325     if (!dc) {
    326         ALOGE("Calloc for driver hal failed.");
    327         return false;
    328     }
    329     rsc->mHal.drv = dc;
    330 
    331     dc->mCpuRef = RsdCpuReference::create(rsc, version_major, version_minor,
    332                                           &rsdLookupRuntimeStub, &LookupScript);
    333     if (!dc->mCpuRef) {
    334         ALOGE("RsdCpuReference::create for driver hal failed.");
    335         rsc->mHal.drv = nullptr;
    336         free(dc);
    337         return false;
    338     }
    339 
    340     return true;
    341 }
    342 
    343 
    344 void SetPriority(const Context *rsc, int32_t priority) {
    345     RsdHal *dc = (RsdHal *)rsc->mHal.drv;
    346 
    347     dc->mCpuRef->setPriority(priority);
    348 
    349 #ifndef RS_COMPATIBILITY_LIB
    350     if (dc->mHasGraphics) {
    351         rsdGLSetPriority(rsc, priority);
    352     }
    353 #endif
    354 }
    355 
    356 void Shutdown(Context *rsc) {
    357     RsdHal *dc = (RsdHal *)rsc->mHal.drv;
    358     delete dc->mCpuRef;
    359     free(dc);
    360     rsc->mHal.drv = nullptr;
    361 }
    362 
    363 void* rsdAllocRuntimeMem(size_t size, uint32_t flags) {
    364     void* buffer = calloc(size, sizeof(char));
    365     return buffer;
    366 }
    367 
    368 void rsdFreeRuntimeMem(void* ptr) {
    369     free(ptr);
    370 }
    371