?? libmain.cpp
字號(hào):
//// Copyright (c) Samsung Electronics. Co. LTD. All rights reserved.///*++THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OFANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TOTHE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR APARTICULAR PURPOSE.*/#include <stdlib.h>#include <malloc.h>#include <string.h>#include <stdio.h>#include "register.h"#include "macros.h"#include "fgl.h"#include "platform.h"#include <windows.h>#include <bsp.h>#include "DrvLib.h"#include "s3c6410_base_regs.h"#include "bsp_cfg.h"#include "fimgdrv.h"#include <pmplatform.h>extern int g_InitDone;extern HANDLE g_hPwrControl;static HANDLE g_hMapping;#define FIMG_PHY_BASE S3C6410_BASE_REG_PA_FIMG_3DSE#define FIMG_PHY_SIZE 0x90000#define ALLOCATESIZE 0x200000#define ALLOCATENUM 8/* Type, Structure & Class Definitions */struct s3c_3d_mem_alloc{ int size; unsigned int vir_addr; unsigned int phy_addr;};typedef struct ALLOCMEM_POOL { void* phyAddr; void* virAddr; HANDLE handle; BOOL used; void* openHandle; int size; struct ALLOCMEM_POOL *next;} ALLOCMEM_POOL;ALLOCMEM_POOL *pHeadOfMemoryPool = NULL;ALLOCMEM_POOL *pDepthBuffer = NULL;int g_blkSize = 0;int g_blkNum = 0;int g_depthBufferSize = 0;// Pool memoryAddressBase gPoolMem = { (void*) 0, NULL};// FIMG registersAddressBase gFimgBase = { (void*)FIMG_PHY_BASE, NULL};static int fimg_initcount = 0;#if 0static volatile S5PC100_DMAC_REG *g_pPDMAC0Reg = NULL;static volatile S5PC100_DMAC_REG *g_pPDMAC1Reg = NULL;static volatile S5PC100_DMAC_REG *g_pMDMACReg = NULL;static volatile S5PC100_SYSCON_CLK_REG *g_pSysConReg = NULL;#endifBOOL ReadMemoryPoolFromRegistry(int *pBlkNum, int *pBlkSize, int *pDepthBufferSize);void FimgPowerOn(BOOL isOn);BOOL AllocateMemoryList();//void FreeMemoryList();/*********************************************************************************** Function Name : InitFimg Inputs : None Outputs : None Returns : None Description : This function initializes the graphics hardware.************************************************************************************/extern "C" void InitFimg (){ unsigned int GPU_version = 0; fglGetVersion(&GPU_version); Plat::printf("\n\n\n *** FIMG VERSION : 0x%x *** \n\n\n",GPU_version); fglSoftReset(); WRITEREG(FGPF_STENCIL_DEPTH_MASK, 0); WRITEREG(FGRA_PIXEL_SAMPOS ,FGL_SAMPLE_CENTER);}/*********************************************************************************** Function Name : InitDevice_wince Inputs : primary and secondary surface pointers Outputs : None Returns : opengl es 2.0 contexts Description : This is the starting function of OpenGL ES 2.0 which maps the FIMG registers, set the framebuffer structure parameters, allocates a pool of memory and creates a gl context.************************************************************************************/extern "C" BOOL GLES2Initdriver (){ DWORD dwPhys = 0;#if (_WIN32_WCE >= 600) PHYSICAL_ADDRESS ioPhysicalBase = {0,0};#endif Plat::printf("[FIMGDRV] InitDriver\n"); //------------------------------------------------------------------------- // Map FIMG SFRs //------------------------------------------------------------------------- g_hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, FIMG_PHY_SIZE, NULL); if(g_hMapping == NULL) { RETAILMSG (1,(TEXT("CreateFileMapping error\r\n"))); return false; } gFimgBase.vaddr = (void *)MapViewOfFile(g_hMapping, FILE_MAP_WRITE, 0, 0, 0); if (gFimgBase.vaddr < 0) { RETAILMSG (1,(TEXT("gFimgBase.vaddr not mapped\r\n"))); return false; } //if(!VirtualCopy (pVaShared, virtaddr, szBlock, PAGE_NOCACHE|PAGE_READWRITE)) if(!VirtualCopy (gFimgBase.vaddr, (LPVOID)((UINT32)FIMG_PHY_BASE>>8), FIMG_PHY_SIZE, PAGE_NOCACHE|PAGE_READWRITE|PAGE_PHYSICAL)) { RETAILMSG (1,(TEXT("VirtualCopy error\r\n"))); return false; } if(!ReadMemoryPoolFromRegistry(&g_blkNum, &g_blkSize, &g_depthBufferSize)) { g_blkSize = ALLOCATESIZE; g_blkNum = ALLOCATENUM; } AllocateMemoryList(); return true;}extern "C" BOOL GLES2Opendriver (){ // RETAILMSG(1,(TEXT("WinCE Version = %d\n"), _WIN32_WCE)); EnterCriticalSection(&gles20_open_mutex); if(fimg_initcount == 0) { //------------------------------------------------------------------------- // Graphics hardware (FIMG) initialization //------------------------------------------------------------------------- FimgPowerOn(TRUE); InitFimg(); g_InitDone = TRUE; } fimg_initcount++; LeaveCriticalSection(&gles20_open_mutex); return TRUE;}/*********************************************************************************** Function Name : CloseDevice Inputs : None Outputs : None Returns : None Description : This fucntion unmap the FIMG registers, pool memory.************************************************************************************/extern "C" BOOL GLES2DeInitdriver(void){ // Unmap any memory areas that we may have mapped. if (gFimgBase.vaddr) { CloseHandle(g_hMapping); gFimgBase.vaddr = NULL; } return true;}extern "C" BOOL GLES2Closedriver(void){ EnterCriticalSection(&gles20_open_mutex); fimg_initcount--; if(fimg_initcount == 0) { g_InitDone = FALSE; FimgPowerOn(FALSE); } LeaveCriticalSection(&gles20_open_mutex); return TRUE;}extern "C" void GetPhysicalAddress(BufferAddress* bufAddr){ DWORD dwPhys = 0; //BufferAddress *bufAddr = (BufferAddress *) MapCallerPtr( (LPVOID)unMappedbufAddr, sizeof(BufferAddress)); if(TRUE == LockPages((LPVOID)bufAddr->vaddrCP, 1, &dwPhys, LOCKFLAG_QUERY_ONLY)) { //APR 06Mar07 //LockPages was observed to return physical addresses aligned at 4KB. //This hack was put here to capture the offset from 4KB aligned physical address if any dwPhys |= (DWORD)(((DWORD)bufAddr->vaddrCP) & (0xFFF)); } else { Plat::printf("LockPage failed..\r\n"); } bufAddr->paddr = (void*)dwPhys; bufAddr->vaddr = bufAddr->vaddrCP; }void FimgPowerOn(BOOL isOn){ S3C6410_SYSCON_REG *pSysConReg;#if (_WIN32_WCE>=600) PHYSICAL_ADDRESS ioPhysicalBase = {0,0}; #endif // Alloc and Map System Controller SFR#if (_WIN32_WCE>=600) ioPhysicalBase.LowPart = S3C6410_BASE_REG_PA_SYSCON; pSysConReg = (S3C6410_SYSCON_REG *)MmMapIoSpace(ioPhysicalBase, sizeof(S3C6410_SYSCON_REG), FALSE);#else pSysConReg = (S3C6410_SYSCON_REG *)DrvLib_MapIoSpace(S3C6410_BASE_REG_PA_SYSCON, sizeof(S3C6410_SYSCON_REG), FALSE);#endif if (pSysConReg == NULL) { RETAILMSG(1,(_T("[FIMG:ERR] FimgPowerOn() : pSysConReg DrvLib_MapIoSpace() Failed\n\r"))); return; } if(isOn) { pSysConReg->NORMAL_CFG |= (1<<10); // DOMAIN_G on pSysConReg->HCLK_GATE |= (1<<31); // Clock On } else { pSysConReg->NORMAL_CFG &= ~(1<<10); // DOMAIN_G off pSysConReg->HCLK_GATE &= ~(1<<31); // Clock Off } #if (_WIN32_WCE>=600) MmUnmapIoSpace((PVOID)pSysConReg, sizeof(S3C6410_SYSCON_REG)); #else DrvLib_UnmapIoSpace((PVOID)pSysConReg);#endif }extern "C" void GetSFRAddress(int* sfrAddr){#if (_WIN32_WCE >= 600) *sfrAddr = (int)VirtualAllocCopyEx( (HANDLE)GetCurrentProcessId(), (HANDLE)GetDirectCallerProcessId(), gFimgBase.vaddr, FIMG_PHY_SIZE, PAGE_READWRITE | PAGE_NOCACHE); Plat::printf("sfr phy address = 0x%x\n", gFimgBase.vaddr); Plat::printf("sfr address = 0x%x\n", *sfrAddr);#else *sfrAddr = (int)gFimgBase.paddr;//(int)MapPtrToProcess(gFimgBase.vaddr, (HANDLE) GetCallerProcess()); // Plat::printf("sfr address = 0x%x\n", *sfrAddr); #endif }extern "C" void FreeSFRAddress(int sfrAddr){#if (_WIN32_WCE >= 600) if(!VirtualFreeEx((HANDLE)GetDirectCallerProcessId(), (LPVOID)sfrAddr, 0, MEM_RELEASE)) { RETAILMSG(1,(TEXT("%s: VirtualFreeEx is failed\r\n"),_T(__FUNCTION__))); }#else //UnMapPtr((LPVOID)sfrAddr); #endif }extern "C" void GetDMASFRAddress(int** sfrAddr){#if 0#if (_WIN32_WCE >= 600) sfrAddr[0] = (int *)VirtualAllocCopyEx( (HANDLE)GetCurrentProcessId(), (HANDLE)GetDirectCallerProcessId(), (LPVOID)g_pPDMAC0Reg, sizeof(S5PC100_DMAC_REG), PAGE_READWRITE | PAGE_NOCACHE); sfrAddr[1] = (int *)VirtualAllocCopyEx( (HANDLE)GetCurrentProcessId(), (HANDLE)GetDirectCallerProcessId(), (LPVOID)g_pPDMAC1Reg, sizeof(S5PC100_DMAC_REG), PAGE_READWRITE | PAGE_NOCACHE); sfrAddr[2] = (int *)VirtualAllocCopyEx( (HANDLE)GetCurrentProcessId(), (HANDLE)GetDirectCallerProcessId(), (LPVOID)g_pMDMACReg, sizeof(S5PC100_DMAC_REG), PAGE_READWRITE | PAGE_NOCACHE); sfrAddr[3] = (int *)VirtualAllocCopyEx( (HANDLE)GetCurrentProcessId(), (HANDLE)GetDirectCallerProcessId(), (LPVOID)g_pSysConReg, sizeof(S5PC100_SYSCON_CLK_REG), PAGE_READWRITE | PAGE_NOCACHE); #else#if 0 sfrAddr[0] = (int*)S5PC100_BASE_REG_PA_PDMA0; sfrAddr[1] = (int*)S5PC100_BASE_REG_PA_PDMA1; sfrAddr[2] = (int*)S5PC100_BASE_REG_PA_MDMA; sfrAddr[3] = (int*)S5PC100_BASE_REG_PA_SYSCON_CLK;#endif#endif #endif}extern "C" void FreeDMASFRAddress(int** sfrAddr){#if 0 #if (_WIN32_WCE >= 600) VirtualFreeEx((HANDLE)GetDirectCallerProcessId(), (LPVOID)sfrAddr[0], 0, MEM_RELEASE); VirtualFreeEx((HANDLE)GetDirectCallerProcessId(), (LPVOID)sfrAddr[1], 0, MEM_RELEASE); VirtualFreeEx((HANDLE)GetDirectCallerProcessId(), (LPVOID)sfrAddr[2], 0, MEM_RELEASE); VirtualFreeEx((HANDLE)GetDirectCallerProcessId(), (LPVOID)sfrAddr[3], 0, MEM_RELEASE);#else //UnMapPtr((LPVOID)sfrAddr); #endif #endif}void* GetEmptyMemBlock(){ ALLOCMEM_POOL *pTemp = pHeadOfMemoryPool; EnterCriticalSection(&gles20_chunkalloc_mutex); while(pTemp != NULL) { if(pTemp->used == FALSE) { pTemp->used = TRUE; break; } pTemp = pTemp->next; } LeaveCriticalSection(&gles20_chunkalloc_mutex); return pTemp; }extern "C" void DoAllocPhysMem(DWORD hOpenContext, void* bufAddr, int size){ FIMG_CONTEXT* openHandle = (FIMG_CONTEXT*)hOpenContext; ALLOCMEM_ITEM *pHead = openHandle->allocated_list; ALLOCMEM_ITEM *pItem, *pPrev = NULL; s3c_3d_mem_alloc* pOutputBuf = (s3c_3d_mem_alloc*)bufAddr; ALLOCMEM_POOL *pTemp = NULL; int i; for(i=0;i<10;i++) { Plat::printf("[FIMGDRV:0x%08x] DoAllocPhysMem %dth try \n", hOpenContext, (i+1)); pTemp = (ALLOCMEM_POOL *)GetEmptyMemBlock(); if(pTemp != NULL) break; Sleep(100); } if(pTemp == NULL) { Plat::printf("[FIMGDRV:0x%08x] Buffer Allocator unable to allocate %d bytes\n", hOpenContext, size); pTemp = pHeadOfMemoryPool; while(pTemp != NULL) { Plat::printf("[FIMGDRV:0x%08x] status 0x%x 0x%x 0x%x by 0x%08x\n", hOpenContext, pTemp->virAddr, pTemp->phyAddr, pTemp->used, pTemp->openHandle); pTemp = pTemp->next; } } else { pOutputBuf->size = pTemp->size; pOutputBuf->phy_addr = (unsigned int)pTemp->phyAddr; #if (_WIN32_WCE >= 600) void* pCPAddr; pCPAddr = VirtualAllocCopyEx((HANDLE)GetCurrentProcessId(), (HANDLE)GetDirectCallerProcessId(), (LPVOID)pTemp->virAddr, pTemp->size, PAGE_READWRITE | PAGE_NOCACHE ); pOutputBuf->vir_addr = (unsigned int)pCPAddr; #else pOutputBuf->vir_addr = (unsigned int)pTemp->virAddr;#endif for(pItem=pHead;pItem != NULL; pItem=pItem->next) { pPrev = pItem; } pItem = (ALLOCMEM_ITEM*)malloc(sizeof(ALLOCMEM_ITEM)); pItem->phyAddr = pTemp->phyAddr; pItem->virAddr = pTemp->virAddr; pItem->virAddrCP = (void*)pOutputBuf->vir_addr; pItem->memPool = (void*)pTemp; pTemp->used = TRUE; pTemp->openHandle = (void*)hOpenContext; pItem->next = 0; if(pPrev != NULL) pPrev->next = pItem; else openHandle->allocated_list = pItem; Plat::printf("[FIMGDRV:0x%08x] DoAllocPhysMem 0x%x 0x%x 0x%x %d\n", hOpenContext, pOutputBuf->phy_addr, pOutputBuf->vir_addr, pTemp->virAddr, pOutputBuf->size); Plat::printf("[FIMGDRV:0x%08x] After Alloc, Block list for this context\n", hOpenContext); Plat::printf("[FIMGDRV:0x%08x] ========================================\n", hOpenContext); for(pItem=openHandle->allocated_list;pItem != NULL; pItem=pItem->next) { Plat::printf("[FIMGDRV:0x%08x] 0x%x 0x%x 0x%x is Allocated\n", hOpenContext, pItem->phyAddr, pItem->virAddrCP, pItem->virAddr); }
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -