早先曾与ZL仔细的讨论过这个问题,他当时在WinCE6.0上移植TCPMP,发现播放视频不太流畅,于是想通过直接写显存进行加速。目的很明确,在应用中申请一段虚拟空间,通过某种方法将其映射到显存上,视频解码过程中直接往映射过的虚拟空间上写。这种方法与使用GAPI有一点类似。
实现这个需求,需要用到函数VirtualCopyEx()。看看帮助中关于它的说明,This function dynamically maps a virtual address to a physical address by creating a new page-table entry.This function is callable in kernel mode and in user mode, when the source and destination process handles are the active process.This function is similar to VirtualCopy, except VirtualCopyEx requires handles to the source and destination process.
据此基本可以确定,我们的确可以在应用中申请一段虚拟空间,然后通过这个函数将其映射到某段物理空间上。其中目标进程是我们的应用,而源进程是NK.exe。为了实现在NK.exe中执行VirtualCopyEx(),可以加载一个内核态的驱动。更为方便的方法是移植一个OALIOCTL,并在IOControl()中添加一个case。这样,应用程序在做内存映射时就无需打开某个流驱动,直接调用KernelIoControl()即可。
OALIOCTL中添加的关键代码如下。
1 typedef struct { 2 void* pvDestMem; 3 DWORD dwPhysAddr; 4 DWORD dwSize; 5 } VIRTUAL_COPY_EX_DATA; 6 7 #define IOCTL_VIRTUAL_COPY_EX CTL_CODE (FILE_DEVICE_UNKNOWN,3333,METHOD_BUFFERED,FILE_ANY_ACCESS) 8 9 10 case IOCTL_VIRTUAL_COPY_EX: 11 { 12 VIRTUAL_COPY_EX_DATA *p = (VIRTUAL_COPY_EX_DATA*)pInBuf; 13 HANDLE hDst = (HANDLE)GetDirectCallerProcessId(); 14 HANDLE hSrc = (HANDLE)GetCurrentProcessId(); 15 fRet = VirtualCopyEx(hDst,p->pvDestMem,hSrc,(LPVOID)p->dwPhysAddr,p->dwSize, 16 PAGE_READWRITE|PAGE_PHYSICAL|PAGE_NOCACHE); 17 }break;
应用程序中进行内存映射的关键代码如下。
1 volatile LPVOID GetVirtual(DWORD dwPhyBaseAddress, DWORD dwSize) 2 { 3 volatile LPVOID pVirtual; 4 VIRTUAL_COPY_EX_DATA vced; 5 6 if(dwPhyBaseAddress&0xFFF) 7 { 8 return NULL; 9 } 10 vced.dwPhysAddr = dwPhyBaseAddress>>8; 11 pVirtual = VirtualAlloc(0,dwSize,MEM_RESERVE,PAGE_NOACCESS); 12 vced.pvDestMem = pVirtual; 13 vced.dwSize = dwSize; 14 KernelIoControl(IOCTL_VIRTUAL_COPY_EX,&vced, sizeof(vced), NULL, NULL, NULL); 15 return pVirtual; 16 } 17 18 // WinCE6.0模拟器中应用程序直接写屏 19 PBYTE pLCDBuf = (PBYTE)GetVirtual(0x33f00000,0x100000); 20 memset(pLCDBuf,0,0x100000); 21
这种方法在WinCE6.0的模拟器中测试了一下,能达到预期的效果。