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