util: fix DXT1 RGBA texture compression if the source color is (0, 0, 0, 0)
[mesa.git] / src / gallium / state_trackers / wgl / stw_ext_pbuffer.c
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <windows.h>
29
30 #define WGL_WGLEXT_PROTOTYPES
31
32 #include <GL/gl.h>
33 #include <GL/wglext.h>
34
35 #include "pipe/p_defines.h"
36 #include "pipe/p_screen.h"
37
38 #include "stw_device.h"
39 #include "stw_pixelformat.h"
40 #include "stw_framebuffer.h"
41
42
43 HPBUFFERARB WINAPI
44 wglCreatePbufferARB(HDC _hDC,
45 int iPixelFormat,
46 int iWidth,
47 int iHeight,
48 const int *piAttribList)
49 {
50 static boolean first = TRUE;
51 const int *piAttrib;
52 int useLargest = 0;
53 const struct stw_pixelformat_info *info;
54 struct stw_framebuffer *fb;
55 HWND hWnd;
56 HDC hDC;
57
58 info = stw_pixelformat_get_info(iPixelFormat);
59 if (!info) {
60 SetLastError(ERROR_INVALID_PIXEL_FORMAT);
61 return 0;
62 }
63
64 if (iWidth <= 0 || iHeight <= 0) {
65 SetLastError(ERROR_INVALID_DATA);
66 return 0;
67 }
68
69 for (piAttrib = piAttribList; *piAttrib; piAttrib++) {
70 switch (*piAttrib) {
71 case WGL_PBUFFER_LARGEST_ARB:
72 piAttrib++;
73 useLargest = *piAttrib;
74 break;
75 default:
76 SetLastError(ERROR_INVALID_DATA);
77 return 0;
78 }
79 }
80
81 if (iWidth > stw_dev->max_2d_length) {
82 if (useLargest) {
83 iWidth = stw_dev->max_2d_length;
84 } else {
85 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
86 return 0;
87 }
88 }
89
90 if (iHeight > stw_dev->max_2d_length) {
91 if (useLargest) {
92 iHeight = stw_dev->max_2d_length;
93 } else {
94 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
95 return 0;
96 }
97 }
98
99 /*
100 * Implement pbuffers through invisible windows
101 */
102
103 if (first) {
104 WNDCLASS wc;
105 memset(&wc, 0, sizeof wc);
106 wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
107 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
108 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
109 wc.lpfnWndProc = DefWindowProc;
110 wc.lpszClassName = "wglpbuffer";
111 wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
112 RegisterClass(&wc);
113 first = FALSE;
114 }
115
116 hWnd = CreateWindowEx(0,
117 "wglpbuffer", /* wc.lpszClassName */
118 "wglpbuffer",
119 #if 0 /* Useful for debugging what the application is drawing */
120 WS_VISIBLE |
121 #endif
122 WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
123 CW_USEDEFAULT, CW_USEDEFAULT, /* x, y */
124 iWidth, iHeight,
125 NULL,
126 NULL,
127 NULL,
128 NULL);
129 if (!hWnd) {
130 return 0;
131 }
132
133 hDC = GetDC(hWnd);
134 if (!hDC) {
135 return 0;
136 }
137
138 SetPixelFormat(hDC, iPixelFormat, &info->pfd);
139
140 fb = stw_framebuffer_create(hDC, iPixelFormat);
141 if (!fb) {
142 SetLastError(ERROR_NO_SYSTEM_RESOURCES);
143 }
144
145 return (HPBUFFERARB)fb;
146 }
147
148
149 HDC WINAPI
150 wglGetPbufferDCARB(HPBUFFERARB hPbuffer)
151 {
152 struct stw_framebuffer *fb;
153 HDC hDC;
154
155 fb = (struct stw_framebuffer *)hPbuffer;
156
157 hDC = GetDC(fb->hWnd);
158 SetPixelFormat(hDC, fb->iPixelFormat, &fb->pfi->pfd);
159
160 return hDC;
161 }
162
163
164 int WINAPI
165 wglReleasePbufferDCARB(HPBUFFERARB hPbuffer,
166 HDC hDC)
167 {
168 struct stw_framebuffer *fb;
169
170 fb = (struct stw_framebuffer *)hPbuffer;
171
172 return ReleaseDC(fb->hWnd, hDC);
173 }
174
175
176 BOOL WINAPI
177 wglDestroyPbufferARB(HPBUFFERARB hPbuffer)
178 {
179 struct stw_framebuffer *fb;
180
181 fb = (struct stw_framebuffer *)hPbuffer;
182
183 /* This will destroy all our data */
184 return DestroyWindow(fb->hWnd);
185 }
186
187
188 BOOL WINAPI
189 wglQueryPbufferARB(HPBUFFERARB hPbuffer,
190 int iAttribute,
191 int *piValue)
192 {
193 struct stw_framebuffer *fb;
194
195 fb = (struct stw_framebuffer *)hPbuffer;
196
197 switch (iAttribute) {
198 case WGL_PBUFFER_WIDTH_ARB:
199 *piValue = fb->width;
200 return TRUE;
201 case WGL_PBUFFER_HEIGHT_ARB:
202 *piValue = fb->width;
203 return TRUE;
204 case WGL_PBUFFER_LOST_ARB:
205 /* We assume that no content is ever lost due to display mode change */
206 *piValue = FALSE;
207 return TRUE;
208 default:
209 SetLastError(ERROR_INVALID_DATA);
210 return FALSE;
211 }
212 }