Path: blob/21.2-virgl/src/gallium/frontends/wgl/stw_wgl.c
4561 views
/**************************************************************************1*2* Copyright 2008 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627/**28* @file29*30* Fake WGL gallium frontend.31*32* These functions implement the WGL API, on top of the ICD DDI, so that the33* resulting DLL can be used as a drop-in replacement for the system's34* opengl32.dll.35*36* These functions never get called for ICD drivers, which use exclusively the37* ICD DDI, i.e., the Drv* entrypoints.38*/3940#include <windows.h>41#include <GL/gl.h>4243#include "util/u_debug.h"44#include "gldrv.h"45#include "stw_context.h"46#include "stw_pixelformat.h"47#include "stw_wgl.h"48#include "stw_ext_context.h"495051static void52overrideOpenGL32EntryPoints(void);5354WINGDIAPI BOOL APIENTRY55wglCopyContext(56HGLRC hglrcSrc,57HGLRC hglrcDst,58UINT mask )59{60return DrvCopyContext( (DHGLRC)(UINT_PTR)hglrcSrc,61(DHGLRC)(UINT_PTR)hglrcDst,62mask );63}6465WINGDIAPI HGLRC APIENTRY66wglCreateContext(67HDC hdc )68{69overrideOpenGL32EntryPoints();70return (HGLRC)(UINT_PTR)DrvCreateContext(hdc);71}7273WINGDIAPI HGLRC APIENTRY74wglCreateLayerContext(75HDC hdc,76int iLayerPlane )77{78overrideOpenGL32EntryPoints();79return (HGLRC)(UINT_PTR)DrvCreateLayerContext( hdc, iLayerPlane );80}8182WINGDIAPI BOOL APIENTRY83wglDeleteContext(84HGLRC hglrc )85{86return DrvDeleteContext((DHGLRC)(UINT_PTR)hglrc );87}888990WINGDIAPI HGLRC APIENTRY91wglGetCurrentContext( VOID )92{93return (HGLRC)(UINT_PTR)stw_get_current_context();94}9596WINGDIAPI HDC APIENTRY97wglGetCurrentDC( VOID )98{99return stw_get_current_dc();100}101102WINGDIAPI HDC APIENTRY103wglGetCurrentReadDCARB( VOID )104{105return stw_get_current_read_dc();106}107108109WINGDIAPI BOOL APIENTRY110wglMakeCurrent(111HDC hdc,112HGLRC hglrc )113{114return DrvSetContext( hdc, (DHGLRC)(UINT_PTR)hglrc, NULL ) ? TRUE : FALSE;115}116117118WINGDIAPI BOOL APIENTRY119wglSwapBuffers(120HDC hdc )121{122return DrvSwapBuffers( hdc );123}124125126WINGDIAPI DWORD WINAPI127wglSwapMultipleBuffers(UINT n,128CONST WGLSWAP *ps)129{130UINT i;131132for (i =0; i < n; ++i)133wglSwapBuffers(ps->hdc);134135return 0;136}137138139WINGDIAPI BOOL APIENTRY140wglSwapLayerBuffers(141HDC hdc,142UINT fuPlanes )143{144return DrvSwapLayerBuffers( hdc, fuPlanes );145}146147WINGDIAPI PROC APIENTRY148wglGetProcAddress(149LPCSTR lpszProc )150{151return DrvGetProcAddress( lpszProc );152}153154155WINGDIAPI int APIENTRY156wglChoosePixelFormat(157HDC hdc,158CONST PIXELFORMATDESCRIPTOR *ppfd )159{160if (ppfd->nSize != sizeof( PIXELFORMATDESCRIPTOR ) || ppfd->nVersion != 1)161return 0;162if (ppfd->iPixelType != PFD_TYPE_RGBA)163return 0;164if (!(ppfd->dwFlags & PFD_DRAW_TO_WINDOW))165return 0;166if (!(ppfd->dwFlags & PFD_SUPPORT_OPENGL))167return 0;168if (ppfd->dwFlags & PFD_DRAW_TO_BITMAP)169return 0;170if (!(ppfd->dwFlags & PFD_STEREO_DONTCARE) && (ppfd->dwFlags & PFD_STEREO))171return 0;172173return stw_pixelformat_choose( hdc, ppfd );174}175176WINGDIAPI int APIENTRY177wglDescribePixelFormat(178HDC hdc,179int iPixelFormat,180UINT nBytes,181LPPIXELFORMATDESCRIPTOR ppfd )182{183return DrvDescribePixelFormat( hdc, iPixelFormat, nBytes, ppfd );184}185186WINGDIAPI int APIENTRY187wglGetPixelFormat(188HDC hdc )189{190return stw_pixelformat_get( hdc );191}192193WINGDIAPI BOOL APIENTRY194wglSetPixelFormat(195HDC hdc,196int iPixelFormat,197const PIXELFORMATDESCRIPTOR *ppfd )198{199/* SetPixelFormat (hence wglSetPixelFormat) must not touch ppfd, per200* http://msdn.microsoft.com/en-us/library/dd369049(v=vs.85).aspx201*/202(void) ppfd;203204return DrvSetPixelFormat( hdc, iPixelFormat );205}206207208WINGDIAPI BOOL APIENTRY209wglUseFontBitmapsA(210HDC hdc,211DWORD first,212DWORD count,213DWORD listBase )214{215return wglUseFontBitmapsW(hdc, first, count, listBase);216}217218WINGDIAPI BOOL APIENTRY219wglShareLists(220HGLRC hglrc1,221HGLRC hglrc2 )222{223return DrvShareLists((DHGLRC)(UINT_PTR)hglrc1,224(DHGLRC)(UINT_PTR)hglrc2);225}226227WINGDIAPI BOOL APIENTRY228wglUseFontBitmapsW(229HDC hdc,230DWORD first,231DWORD count,232DWORD listBase )233{234GLYPHMETRICS gm;235MAT2 tra;236FIXED one, minus_one, zero;237void *buffer = NULL;238BOOL result = TRUE;239240one.value = 1;241one.fract = 0;242minus_one.value = -1;243minus_one.fract = 0;244zero.value = 0;245zero.fract = 0;246247tra.eM11 = one;248tra.eM22 = minus_one;249tra.eM12 = tra.eM21 = zero;250251for (int i = 0; i < count; i++) {252DWORD size = GetGlyphOutline(hdc, first + i, GGO_BITMAP, &gm, 0,253NULL, &tra);254255glNewList(listBase + i, GL_COMPILE);256257if (size != GDI_ERROR) {258if (size == 0) {259glBitmap(0, 0, (GLfloat)-gm.gmptGlyphOrigin.x,260(GLfloat)gm.gmptGlyphOrigin.y,261(GLfloat)gm.gmCellIncX,262(GLfloat)gm.gmCellIncY, NULL);263}264else {265buffer = realloc(buffer, size);266size = GetGlyphOutline(hdc, first + i, GGO_BITMAP, &gm,267size, buffer, &tra);268269glBitmap(gm.gmBlackBoxX, gm.gmBlackBoxY,270-gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y,271gm.gmCellIncX, gm.gmCellIncY, buffer);272}273}274else {275result = FALSE;276}277278glEndList();279}280281free(buffer);282283return result;284}285286WINGDIAPI BOOL APIENTRY287wglUseFontOutlinesA(288HDC hdc,289DWORD first,290DWORD count,291DWORD listBase,292FLOAT deviation,293FLOAT extrusion,294int format,295LPGLYPHMETRICSFLOAT lpgmf )296{297(void) hdc;298(void) first;299(void) count;300(void) listBase;301(void) deviation;302(void) extrusion;303(void) format;304(void) lpgmf;305306assert( 0 );307308return FALSE;309}310311WINGDIAPI BOOL APIENTRY312wglUseFontOutlinesW(313HDC hdc,314DWORD first,315DWORD count,316DWORD listBase,317FLOAT deviation,318FLOAT extrusion,319int format,320LPGLYPHMETRICSFLOAT lpgmf )321{322(void) hdc;323(void) first;324(void) count;325(void) listBase;326(void) deviation;327(void) extrusion;328(void) format;329(void) lpgmf;330331assert( 0 );332333return FALSE;334}335336WINGDIAPI BOOL APIENTRY337wglDescribeLayerPlane(338HDC hdc,339int iPixelFormat,340int iLayerPlane,341UINT nBytes,342LPLAYERPLANEDESCRIPTOR plpd )343{344return DrvDescribeLayerPlane(hdc, iPixelFormat, iLayerPlane, nBytes, plpd);345}346347WINGDIAPI int APIENTRY348wglSetLayerPaletteEntries(349HDC hdc,350int iLayerPlane,351int iStart,352int cEntries,353CONST COLORREF *pcr )354{355return DrvSetLayerPaletteEntries(hdc, iLayerPlane, iStart, cEntries, pcr);356}357358WINGDIAPI int APIENTRY359wglGetLayerPaletteEntries(360HDC hdc,361int iLayerPlane,362int iStart,363int cEntries,364COLORREF *pcr )365{366return DrvGetLayerPaletteEntries(hdc, iLayerPlane, iStart, cEntries, pcr);367}368369WINGDIAPI BOOL APIENTRY370wglRealizeLayerPalette(371HDC hdc,372int iLayerPlane,373BOOL bRealize )374{375(void) hdc;376(void) iLayerPlane;377(void) bRealize;378379assert( 0 );380381return FALSE;382}383384385/* When this library is used as a opengl32.dll drop-in replacement, ensure we386* use the wglCreate/Destroy entrypoints above, and not the true opengl32.dll,387* which could happen if this library's name is not opengl32.dll exactly.388*389* For example, Qt 5.4 bundles this as opengl32sw.dll:390* https://blog.qt.io/blog/2014/11/27/qt-weekly-21-dynamic-opengl-implementation-loading-in-qt-5-4/391*/392static void393overrideOpenGL32EntryPoints(void)394{395wglCreateContext_func = &wglCreateContext;396wglDeleteContext_func = &wglDeleteContext;397}398399400