Skip to content

Commit 438d131

Browse files
committed
Merge pull request #280 from dblock/kc7bfi-merged
#270 + #271: Added OpenGL32 support.
2 parents b2e16d4 + e465dca commit 438d131

File tree

10 files changed

+633
-2
lines changed

10 files changed

+633
-2
lines changed

CHANGES.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
NOTE: as of JNA 4.0, JNA is now dual-licensed under LGPL and ASL (see LICENSE).
1+
NOTE: as of JNA 4.0, JNA is now dual-licensed under LGPL and ASL (see LICENSE).
22

33
NOTE: JNI native support is typically incompatible between minor versions, and almost always incompatible between major versions.
44

@@ -13,6 +13,8 @@ Features
1313
* [#226](https://github.com/twall/jna/issues/226): Added OSGI information to jna-platform.jar - [@brettwooldridge](https://github.com/brettwooldridge).
1414
* [#267](https://github.com/twall/jna/pull/267): Added support for Windows RAS32 API, `com.sun.jna.platform.win32.Rasapi32` and `Rasapi32Util` - [@kc7bfi](https://github.com/kc7bfi).
1515
* [#101](https://github.com/twall/jna/issues/101): Modify `com.sun.jna.platform.win32.Advapi32Util.registryGet*` API to support `KEY_WOW64` option - [@falldog] (https://github.com/falldog).
16+
* [#271](https://github.com/twall/jna/pull/271): Added `com.sun.jna.platform.win32.Gdi32.ChoosePixelFormat` and `SetPixelFormat` - [@kc7bfi](https://github.com/kc7bfi).
17+
* [#271](https://github.com/twall/jna/pull/271): Added `com.sun.jna.platform.win32.OpenGL32`, `OpenGL32Util` and `WinOpenGL` - [@kc7bfi](https://github.com/kc7bfi).
1618

1719
Bug Fixes
1820
---------
@@ -693,3 +695,4 @@ Bug Fixes
693695
* Properly handle NULL when the return value is a Structure
694696
* Proper conversion to wchar_t on linux
695697
* Copy full length of Java strings to C strings instead of stopping when a NUL character is encountered
698+

contrib/platform/src/com/sun/jna/platform/win32/GDI32.java

+30
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.sun.jna.platform.win32.WinDef.HRGN;
2020
import com.sun.jna.platform.win32.WinGDI.BITMAPINFO;
2121
import com.sun.jna.platform.win32.WinGDI.BITMAPINFOHEADER;
22+
import com.sun.jna.platform.win32.WinGDI.PIXELFORMATDESCRIPTOR;
2223
import com.sun.jna.platform.win32.WinGDI.RGNDATA;
2324
import com.sun.jna.platform.win32.WinNT.HANDLE;
2425
import com.sun.jna.ptr.PointerByReference;
@@ -313,4 +314,33 @@ HBITMAP CreateDIBSection(HDC hDC, BITMAPINFO pbmi, int iUsage,
313314
* BITMAPINFO} structure.
314315
*/
315316
int GetDIBits(HDC hdc, HBITMAP hbmp, int uStartScan, int cScanLines, Pointer lpvBits, BITMAPINFO lpbi, int uUsage);
317+
318+
/**
319+
* The ChoosePixelFormat function attempts to match an appropriate pixel format supported
320+
* by a device context to a given pixel format specification.
321+
*
322+
* @param hdc
323+
* Specifies the device context that the function examines to determine the best
324+
* match for the pixel format descriptor pointed to by ppfd.
325+
* @param ppfd
326+
* Pointer to a PIXELFORMATDESCRIPTOR structure that specifies the requested pixel format.
327+
* @return If the function succeeds, the return value is a pixel format index (one-based) that
328+
* is the closest match to the given pixel format descriptor.
329+
*/
330+
public int ChoosePixelFormat(HDC hdc, PIXELFORMATDESCRIPTOR.ByReference ppfd);
331+
332+
/**
333+
* The SetPixelFormat function sets the pixel format of the specified device context to the
334+
* format specified by the iPixelFormat index.
335+
*
336+
* @param hdc
337+
* Specifies the device context whose pixel format the function attempts to set.
338+
* @param iPixelFormat
339+
* Index that identifies the pixel format to set. The various pixel formats supported
340+
* by a device context are identified by one-based indexes.
341+
* @param ppfd
342+
* Pointer to a PIXELFORMATDESCRIPTOR structure that contains the logical pixel format specification.
343+
* @return true if successful
344+
*/
345+
public boolean SetPixelFormat(HDC hdc, int iPixelFormat, PIXELFORMATDESCRIPTOR.ByReference ppfd);
316346
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* Copyright (c) 2011 Timothy Wall, All Rights Reserved
2+
*
3+
* This library is free software; you can redistribute it and/or
4+
* modify it under the terms of the GNU Lesser General Public
5+
* License as published by the Free Software Foundation; either
6+
* version 2.1 of the License, or (at your option) any later version.
7+
*
8+
* This library is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
* Lesser General Public License for more details.
12+
*/
13+
package com.sun.jna.platform.win32;
14+
15+
import com.sun.jna.win32.StdCallLibrary;
16+
17+
/**
18+
* Definitions for WinOpenGL
19+
*/
20+
public interface GL extends StdCallLibrary {
21+
public final int GL_VENDOR = 0x1F00;
22+
public final int GL_RENDERER = 0x1F01;
23+
public final int GL_VERSION = 0x1F02;
24+
public final int GL_EXTENSIONS = 0x1F03;
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* Copyright (c) 2011 Timothy Wall, All Rights Reserved
2+
*
3+
* This library is free software; you can redistribute it and/or
4+
* modify it under the terms of the GNU Lesser General Public
5+
* License as published by the Free Software Foundation; either
6+
* version 2.1 of the License, or (at your option) any later version.
7+
*
8+
* This library is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
* Lesser General Public License for more details.
12+
*/
13+
package com.sun.jna.platform.win32;
14+
15+
import com.sun.jna.Native;
16+
import com.sun.jna.Pointer;
17+
import com.sun.jna.platform.win32.WinDef.HDC;
18+
import com.sun.jna.platform.win32.WinDef.HGLRC;
19+
import com.sun.jna.win32.StdCallLibrary;
20+
21+
/**
22+
* opengl32.dll Interface.
23+
*/
24+
public interface OpenGL32 extends StdCallLibrary {
25+
OpenGL32 INSTANCE = (OpenGL32) Native.loadLibrary("opengl32", OpenGL32.class);
26+
27+
/**
28+
* The glGetString function returns a string describing the current OpenGL connection.
29+
*
30+
* @param name
31+
* One of the following symbolic constants.
32+
* @return The glGetString function returns a pointer to a static string describing some aspect of the current OpenGL connection.
33+
*/
34+
public String glGetString(int name);
35+
36+
/**
37+
* The wglCreateContext function creates a new OpenGL rendering context, which is suitable for drawing on the device
38+
* referenced by hdc. The rendering context has the same pixel format as the device context.
39+
*
40+
* @param hdc
41+
* Handle to a device context for which the function creates a suitable OpenGL rendering context.
42+
* @return handle to an OpenGL rendering context
43+
*/
44+
public WinDef.HGLRC wglCreateContext(HDC windowDC);
45+
46+
/**
47+
* The wglGetCurrentContext function obtains a handle to the current OpenGL rendering context of the calling thread.
48+
*
49+
* @return If the calling thread has a current OpenGL rendering context, wglGetCurrentContext returns a
50+
* handle to that rendering context. Otherwise, the return value is NULL.
51+
*/
52+
public WinDef.HGLRC wglGetCurrentContext();
53+
54+
/**
55+
* The wglMakeCurrent function makes a specified OpenGL rendering context the calling thread's current rendering
56+
* context. All subsequent OpenGL calls made by the thread are drawn on the device identified by hdc.
57+
*
58+
* @param hdc
59+
* Handle to a device context. Subsequent OpenGL calls made by the calling thread are drawn on the
60+
* device identified by hdc.
61+
* @param hglrc
62+
* Handle to an OpenGL rendering context that the function sets as the calling thread's rendering context.
63+
* @return true if successful
64+
*/
65+
public boolean wglMakeCurrent(HDC windowDC, WinDef.HGLRC hglrc);
66+
67+
/**
68+
* The wglDeleteContext function deletes a specified OpenGL rendering context.
69+
*
70+
* @param hglrc
71+
* Handle to an OpenGL rendering context that the function will delete.
72+
* @return true if successful
73+
*/
74+
public boolean wglDeleteContext(WinDef.HGLRC hglrc);
75+
76+
/**
77+
* The wglGetProcAddress function returns the address of an OpenGL extension function for use with the
78+
* current OpenGL rendering context.
79+
*
80+
* @param lpszProc
81+
* Points to a null-terminated string that is the name of the extension function.
82+
* The name of the extension function must be identical to a corresponding function implemented by OpenGL.
83+
* @return When the function succeeds, the return value is the address of the extension function.
84+
*/
85+
public Pointer wglGetProcAddress(String lpszProc);
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* Copyright (c) 2011 Timothy Wall, All Rights Reserved
2+
*
3+
* This library is free software; you can redistribute it and/or
4+
* modify it under the terms of the GNU Lesser General Public
5+
* License as published by the Free Software Foundation; either
6+
* version 2.1 of the License, or (at your option) any later version.
7+
*
8+
* This library is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
* Lesser General Public License for more details.
12+
*/
13+
package com.sun.jna.platform.win32;
14+
15+
import com.sun.jna.Function;
16+
import com.sun.jna.Pointer;
17+
import com.sun.jna.platform.win32.WinDef.HDC;
18+
import com.sun.jna.platform.win32.WinDef.HGLRCByReference;
19+
import com.sun.jna.platform.win32.WinDef.HWND;
20+
import com.sun.jna.platform.win32.WinGDI.PIXELFORMATDESCRIPTOR;
21+
22+
/**
23+
* opengl32 utility API.
24+
*/
25+
public abstract class OpenGL32Util {
26+
27+
/**
28+
* Return a procedure function pointer
29+
* @param procName the procedure name
30+
* @return the function
31+
*/
32+
public static Function wglGetProcAddress(String procName) {
33+
Pointer funcPointer = OpenGL32.INSTANCE.wglGetProcAddress("wglEnumGpusNV");
34+
return (funcPointer == null) ? null : Function.getFunction(funcPointer);
35+
}
36+
37+
/**
38+
* Return a RAS connection by name
39+
* @param connName the connection name
40+
* @return the RAS connection structure
41+
*/
42+
public static int countGpusNV() {
43+
// create a dummy window
44+
HWND hWnd = User32Util.createWindow("Message", null, 0, 0, 0, 0, 0, null, null, null, null);
45+
HDC hdc = User32.INSTANCE.GetDC(hWnd);
46+
47+
// set a compatible pixel format
48+
PIXELFORMATDESCRIPTOR.ByReference pfd = new PIXELFORMATDESCRIPTOR.ByReference();
49+
pfd.nVersion = 1;
50+
pfd.dwFlags = WinGDI.PFD_DRAW_TO_WINDOW | WinGDI.PFD_SUPPORT_OPENGL | WinGDI.PFD_DOUBLEBUFFER;
51+
pfd.iPixelType = WinGDI.PFD_TYPE_RGBA;
52+
pfd.cColorBits = 24;
53+
pfd.cDepthBits = 16;
54+
pfd.iLayerType = WinGDI.PFD_MAIN_PLANE;
55+
GDI32.INSTANCE.SetPixelFormat(hdc, GDI32.INSTANCE.ChoosePixelFormat(hdc, pfd), pfd);
56+
57+
// create the OpenGL context to get function address
58+
WinDef.HGLRC hGLRC = OpenGL32.INSTANCE.wglCreateContext(hdc);
59+
OpenGL32.INSTANCE.wglMakeCurrent(hdc, hGLRC);
60+
Pointer funcPointer = OpenGL32.INSTANCE.wglGetProcAddress("wglEnumGpusNV");
61+
Function fncEnumGpusNV = (funcPointer == null) ? null : Function.getFunction(funcPointer);
62+
OpenGL32.INSTANCE.wglDeleteContext(hGLRC);
63+
64+
// destroy the window
65+
User32.INSTANCE.ReleaseDC(hWnd, hdc);
66+
User32Util.destroyWindow(hWnd);
67+
68+
// abort if the nVidia extensions are not present
69+
if (fncEnumGpusNV == null) return 0;
70+
71+
// enumerate nVidia adapters
72+
HGLRCByReference hGPU = new HGLRCByReference();
73+
for (int i = 0; i < 16; i++) {
74+
Boolean ok = (Boolean) fncEnumGpusNV.invoke(Boolean.class, new Object[] { Integer.valueOf(i), hGPU, });
75+
if (!ok.booleanValue()) return i;
76+
}
77+
78+
return 0;
79+
}
80+
}

contrib/platform/src/com/sun/jna/platform/win32/WinDef.java

+50
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.sun.jna.Structure;
2424
import com.sun.jna.platform.win32.BaseTSD.LONG_PTR;
2525
import com.sun.jna.platform.win32.WinNT.HANDLE;
26+
import com.sun.jna.platform.win32.WinNT.HANDLEByReference;
2627
import com.sun.jna.ptr.ByReference;
2728
import com.sun.jna.win32.StdCallLibrary;
2829

@@ -1509,4 +1510,53 @@ public CHAR getValue() {
15091510
return new CHAR(getPointer().getChar(0));
15101511
}
15111512
}
1513+
1514+
/**
1515+
* handle to an OpenGL rendering context
1516+
*/
1517+
public static class HGLRC extends HANDLE {
1518+
1519+
/**
1520+
* Instantiates a new HGLRC .
1521+
*/
1522+
public HGLRC() {
1523+
1524+
}
1525+
1526+
/**
1527+
* Instantiates a new HGLRC .
1528+
*
1529+
* @param p
1530+
* the p
1531+
*/
1532+
public HGLRC(Pointer p) {
1533+
super(p);
1534+
}
1535+
}
1536+
1537+
/**
1538+
* handle to an OpenGL rendering context
1539+
*/
1540+
public static class HGLRCByReference extends HANDLEByReference {
1541+
1542+
/**
1543+
* Instantiates a new HGLRC .
1544+
*/
1545+
public HGLRCByReference() {
1546+
1547+
}
1548+
1549+
/**
1550+
* Instantiates a new HGLRC .
1551+
*
1552+
* @param p
1553+
* the p
1554+
*/
1555+
public HGLRCByReference(HGLRC h) {
1556+
super(h);
1557+
}
1558+
}
1559+
1560+
1561+
15121562
}

0 commit comments

Comments
 (0)