• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-------------------------------------------------------------------------
2  * drawElements Quality Program Tester Core
3  * ----------------------------------------
4  *
5  * Copyright 2014 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief WGL Utilities.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "tcuWGL.hpp"
25 #include "tcuWin32Window.hpp"
26 #include "deDynamicLibrary.hpp"
27 #include "deMemory.h"
28 #include "deStringUtil.hpp"
29 #include "tcuFormatUtil.hpp"
30 #include "gluRenderConfig.hpp"
31 #include "glwEnums.hpp"
32 
33 #include <map>
34 #include <sstream>
35 #include <iterator>
36 #include <set>
37 
38 #include <WinGDI.h>
39 
40 // WGL_ARB_pixel_format
41 #define WGL_NUMBER_PIXEL_FORMATS_ARB				0x2000
42 #define WGL_DRAW_TO_WINDOW_ARB						0x2001
43 #define WGL_DRAW_TO_BITMAP_ARB						0x2002
44 #define WGL_ACCELERATION_ARB						0x2003
45 #define WGL_NEED_PALETTE_ARB						0x2004
46 #define WGL_NEED_SYSTEM_PALETTE_ARB					0x2005
47 #define WGL_SWAP_LAYER_BUFFERS_ARB					0x2006
48 #define WGL_SWAP_METHOD_ARB							0x2007
49 #define WGL_NUMBER_OVERLAYS_ARB						0x2008
50 #define WGL_NUMBER_UNDERLAYS_ARB					0x2009
51 #define WGL_TRANSPARENT_ARB							0x200A
52 #define WGL_TRANSPARENT_RED_VALUE_ARB				0x2037
53 #define WGL_TRANSPARENT_GREEN_VALUE_ARB				0x2038
54 #define WGL_TRANSPARENT_BLUE_VALUE_ARB				0x2039
55 #define WGL_TRANSPARENT_ALPHA_VALUE_ARB				0x203A
56 #define WGL_TRANSPARENT_INDEX_VALUE_ARB				0x203B
57 #define WGL_SHARE_DEPTH_ARB							0x200C
58 #define WGL_SHARE_STENCIL_ARB						0x200D
59 #define WGL_SHARE_ACCUM_ARB							0x200E
60 #define WGL_SUPPORT_GDI_ARB							0x200F
61 #define WGL_SUPPORT_OPENGL_ARB						0x2010
62 #define WGL_DOUBLE_BUFFER_ARB						0x2011
63 #define WGL_STEREO_ARB								0x2012
64 #define WGL_PIXEL_TYPE_ARB							0x2013
65 #define WGL_COLOR_BITS_ARB							0x2014
66 #define WGL_RED_BITS_ARB							0x2015
67 #define WGL_RED_SHIFT_ARB							0x2016
68 #define WGL_GREEN_BITS_ARB							0x2017
69 #define WGL_GREEN_SHIFT_ARB							0x2018
70 #define WGL_BLUE_BITS_ARB							0x2019
71 #define WGL_BLUE_SHIFT_ARB							0x201A
72 #define WGL_ALPHA_BITS_ARB							0x201B
73 #define WGL_ALPHA_SHIFT_ARB							0x201C
74 #define WGL_ACCUM_BITS_ARB							0x201D
75 #define WGL_ACCUM_RED_BITS_ARB						0x201E
76 #define WGL_ACCUM_GREEN_BITS_ARB					0x201F
77 #define WGL_ACCUM_BLUE_BITS_ARB						0x2020
78 #define WGL_ACCUM_ALPHA_BITS_ARB					0x2021
79 #define WGL_DEPTH_BITS_ARB							0x2022
80 #define WGL_STENCIL_BITS_ARB						0x2023
81 #define WGL_AUX_BUFFERS_ARB							0x2024
82 
83 #define WGL_NO_ACCELERATION_ARB						0x2025
84 #define WGL_GENERIC_ACCELERATION_ARB				0x2026
85 #define WGL_FULL_ACCELERATION_ARB					0x2027
86 
87 #define WGL_TYPE_RGBA_ARB							0x202B
88 #define WGL_TYPE_COLORINDEX_ARB						0x202C
89 
90 // WGL_ARB_color_buffer_float
91 #define WGL_TYPE_RGBA_FLOAT_ARB						0x21A0
92 
93 // WGL_EXT_pixel_type_packed_float
94 #define WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT			0x20A8
95 
96 // WGL_ARB_multisample
97 #define WGL_SAMPLE_BUFFERS_ARB						0x2041
98 #define WGL_SAMPLES_ARB								0x2042
99 
100 // WGL_ARB_create_context
101 #define WGL_CONTEXT_MAJOR_VERSION_ARB				0x2091
102 #define WGL_CONTEXT_MINOR_VERSION_ARB				0x2092
103 #define WGL_CONTEXT_LAYER_PLANE_ARB					0x2093
104 #define WGL_CONTEXT_FLAGS_ARB						0x2094
105 #define WGL_CONTEXT_PROFILE_MASK_ARB				0x9126
106 #define WGL_CONTEXT_DEBUG_BIT_ARB					0x0001
107 #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB		0x0002
108 #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB			0x00000001
109 #define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB	0x00000002
110 #define WGL_CONTEXT_ES_PROFILE_BIT_EXT				0x00000004
111 
112 // WGL_ARB_create_context_robustness
113 #define WGL_CONTEXT_ROBUST_ACCESS_BIT_ARB			0x0004
114 #define WGL_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB	0x8256
115 #define WGL_NO_RESET_NOTIFICATION_ARB				0x8261
116 #define WGL_LOSE_CONTEXT_ON_RESET_ARB				0x8252
117 
118 // WGL ARB_create_context_no_error
119 #define WGL_CONTEXT_OPENGL_NO_ERROR_ARB				0x31B3
120 
121 DE_BEGIN_EXTERN_C
122 
123 // WGL core
124 typedef HGLRC		(WINAPI* wglCreateContextFunc)				(HDC hdc);
125 typedef BOOL		(WINAPI* wglDeleteContextFunc)				(HGLRC hglrc);
126 typedef BOOL		(WINAPI* wglMakeCurrentFunc)				(HDC hdc, HGLRC hglrc);
127 typedef PROC		(WINAPI* wglGetProcAddressFunc)				(LPCSTR lpszProc);
128 typedef BOOL		(WINAPI* wglSwapLayerBuffersFunc)			(HDC dhc, UINT fuPlanes);
129 
130 // WGL_ARB_pixel_format
131 typedef BOOL		(WINAPI* wglGetPixelFormatAttribivARBFunc)	(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues);
132 typedef BOOL		(WINAPI* wglGetPixelFormatAttribfvARBFunc)	(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, FLOAT *pfValues);
133 typedef BOOL		(WINAPI* wglChoosePixelFormatARBFunc)		(HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
134 
135 // WGL_ARB_create_context
136 typedef HGLRC		(WINAPI* wglCreateContextAttribsARBFunc)	(HDC hdc, HGLRC hshareContext, const int* attribList);
137 typedef const char*	(WINAPI* wglGetExtensionsStringARBFunc)		(HDC hdc);
138 
139 // WGL_EXT_swap_control
140 typedef BOOL		(WINAPI* wglSwapIntervalEXTFunc)			(int interval);
141 
142 DE_END_EXTERN_C
143 
144 namespace tcu
145 {
146 namespace wgl
147 {
148 
149 // Functions
150 
151 struct Functions
152 {
153 	// Core
154 	wglCreateContextFunc				createContext;
155 	wglDeleteContextFunc				deleteContext;
156 	wglMakeCurrentFunc					makeCurrent;
157 	wglGetProcAddressFunc				getProcAddress;
158 	wglSwapLayerBuffersFunc				swapLayerBuffers;
159 
160 	// WGL_ARB_pixel_format
161 	wglGetPixelFormatAttribivARBFunc	getPixelFormatAttribivARB;
162 	wglGetPixelFormatAttribfvARBFunc	getPixelFormatAttribfvARB;
163 	wglChoosePixelFormatARBFunc			choosePixelFormatARB;
164 
165 	// WGL_ARB_create_context
166 	wglCreateContextAttribsARBFunc		createContextAttribsARB;
167 	wglGetExtensionsStringARBFunc		getExtensionsStringARB;
168 
169 	// WGL_EXT_swap_control
170 	wglSwapIntervalEXTFunc				swapIntervalEXT;
171 
172 
Functionstcu::wgl::Functions173 	Functions (void)
174 		: createContext				(DE_NULL)
175 		, deleteContext				(DE_NULL)
176 		, makeCurrent				(DE_NULL)
177 		, getProcAddress			(DE_NULL)
178 		, swapLayerBuffers			(DE_NULL)
179 		, getPixelFormatAttribivARB	(DE_NULL)
180 		, getPixelFormatAttribfvARB	(DE_NULL)
181 		, choosePixelFormatARB		(DE_NULL)
182 		, createContextAttribsARB	(DE_NULL)
183 		, getExtensionsStringARB	(DE_NULL)
184 	{
185 	}
186 };
187 
188 // Library
189 
190 class Library
191 {
192 public:
193 								Library			(HINSTANCE instance);
194 								~Library		(void);
195 
getFunctions(void) const196 	const Functions&			getFunctions	(void) const	{ return m_functions;	}
getGLLibrary(void) const197 	const de::DynamicLibrary&	getGLLibrary	(void) const	{ return m_library;		}
198 	bool						isWglExtensionSupported (const char* extName) const;
199 
200 private:
201 	de::DynamicLibrary			m_library;
202 	Functions					m_functions;
203 	std::set<std::string>		m_extensions;
204 };
205 
Library(HINSTANCE instance)206 Library::Library (HINSTANCE instance)
207 	: m_library("opengl32.dll")
208 {
209 	// Temporary 1x1 window for creating context
210 	win32::Window tmpWindow(instance, 1, 1);
211 
212 	// Load WGL core.
213 	m_functions.createContext		= (wglCreateContextFunc)		m_library.getFunction("wglCreateContext");
214 	m_functions.deleteContext		= (wglDeleteContextFunc)		m_library.getFunction("wglDeleteContext");
215 	m_functions.getProcAddress		= (wglGetProcAddressFunc)		m_library.getFunction("wglGetProcAddress");
216 	m_functions.makeCurrent			= (wglMakeCurrentFunc)			m_library.getFunction("wglMakeCurrent");
217 	m_functions.swapLayerBuffers	= (wglSwapLayerBuffersFunc)		m_library.getFunction("wglSwapLayerBuffers");
218 
219 	if (!m_functions.createContext		||
220 		!m_functions.deleteContext		||
221 		!m_functions.getProcAddress		||
222 		!m_functions.makeCurrent		||
223 		!m_functions.swapLayerBuffers)
224 		throw ResourceError("Failed to load core WGL functions");
225 
226 	{
227 		PIXELFORMATDESCRIPTOR pixelFormatDesc;
228 		deMemset(&pixelFormatDesc, 0, sizeof(pixelFormatDesc));
229 
230 		pixelFormatDesc.nSize			= sizeof(pixelFormatDesc);
231 		pixelFormatDesc.nVersion		= 1;
232 		pixelFormatDesc.dwFlags			= PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
233 		pixelFormatDesc.iPixelType		= PFD_TYPE_RGBA;
234 		pixelFormatDesc.iLayerType		= PFD_MAIN_PLANE;
235 
236 		int pixelFormat = ChoosePixelFormat(tmpWindow.getDeviceContext(), &pixelFormatDesc);
237 		if (!SetPixelFormat(tmpWindow.getDeviceContext(), pixelFormat, &pixelFormatDesc))
238 			throw ResourceError("Failed to set pixel format for temporary context creation");
239 	}
240 
241 	// Create temporary context for loading extension functions.
242 	HGLRC tmpCtx = m_functions.createContext(tmpWindow.getDeviceContext());
243 	if (!tmpCtx || !m_functions.makeCurrent(tmpWindow.getDeviceContext(), tmpCtx))
244 	{
245 		if (tmpCtx)
246 			m_functions.deleteContext(tmpCtx);
247 		throw ResourceError("Failed to create temporary WGL context");
248 	}
249 
250 	// WGL_ARB_pixel_format
251 	m_functions.getPixelFormatAttribivARB	= (wglGetPixelFormatAttribivARBFunc)m_functions.getProcAddress("wglGetPixelFormatAttribivARB");
252 	m_functions.getPixelFormatAttribfvARB	= (wglGetPixelFormatAttribfvARBFunc)m_functions.getProcAddress("wglGetPixelFormatAttribfvARB");
253 	m_functions.choosePixelFormatARB		= (wglChoosePixelFormatARBFunc)m_functions.getProcAddress("wglChoosePixelFormatARB");
254 
255 	// WGL_ARB_create_context
256 	m_functions.createContextAttribsARB		= (wglCreateContextAttribsARBFunc)m_functions.getProcAddress("wglCreateContextAttribsARB");
257 	m_functions.getExtensionsStringARB		= (wglGetExtensionsStringARBFunc)m_functions.getProcAddress("wglGetExtensionsStringARB");
258 
259 	// WGL_EXT_swap_control
260 	m_functions.swapIntervalEXT				= (wglSwapIntervalEXTFunc)m_functions.getProcAddress("wglSwapIntervalEXT");
261 
262 	m_functions.makeCurrent(tmpWindow.getDeviceContext(), NULL);
263 	m_functions.deleteContext(tmpCtx);
264 
265 	if (!m_functions.getPixelFormatAttribivARB	||
266 		!m_functions.getPixelFormatAttribfvARB	||
267 		!m_functions.choosePixelFormatARB		||
268 		!m_functions.createContextAttribsARB	||
269 		!m_functions.getExtensionsStringARB)
270 		throw ResourceError("Failed to load WGL extension functions");
271 
272 	const char* extensions = m_functions.getExtensionsStringARB(tmpWindow.getDeviceContext());
273 	std::istringstream extStream(extensions);
274 	m_extensions = std::set<std::string>(std::istream_iterator<std::string>(extStream),
275 										 std::istream_iterator<std::string>());
276 }
277 
~Library(void)278 Library::~Library (void)
279 {
280 }
281 
isWglExtensionSupported(const char * extName) const282 bool Library::isWglExtensionSupported (const char* extName) const
283 {
284 	return m_extensions.find(extName) != m_extensions.end();
285 }
286 
287 // Core
288 
Core(HINSTANCE instance)289 Core::Core (HINSTANCE instance)
290 	: m_library(new Library(instance))
291 {
292 }
293 
~Core(void)294 Core::~Core (void)
295 {
296 	delete m_library;
297 }
298 
getPixelFormats(HDC deviceCtx) const299 std::vector<int> Core::getPixelFormats (HDC deviceCtx) const
300 {
301 	const Functions& wgl = m_library->getFunctions();
302 
303 	int attribs[] = { WGL_NUMBER_PIXEL_FORMATS_ARB };
304 	int values[DE_LENGTH_OF_ARRAY(attribs)];
305 
306 	if (!wgl.getPixelFormatAttribivARB(deviceCtx, 0, 0, DE_LENGTH_OF_ARRAY(attribs), &attribs[0], &values[0]))
307 		TCU_THROW(ResourceError, "Failed to query number of WGL pixel formats");
308 
309 	std::vector<int> pixelFormats(values[0]);
310 	for (int i = 0; i < values[0]; i++)
311 		pixelFormats[i] = i+1;
312 
313 	return pixelFormats;
314 }
315 
translateAcceleration(int accel)316 static PixelFormatInfo::Acceleration translateAcceleration (int accel)
317 {
318 	switch (accel)
319 	{
320 		case WGL_NO_ACCELERATION_ARB:		return PixelFormatInfo::ACCELERATION_NONE;
321 		case WGL_GENERIC_ACCELERATION_ARB:	return PixelFormatInfo::ACCELERATION_GENERIC;
322 		case WGL_FULL_ACCELERATION_ARB:		return PixelFormatInfo::ACCELERATION_FULL;
323 		default:							return PixelFormatInfo::ACCELERATION_UNKNOWN;
324 	}
325 }
326 
translatePixelType(int type)327 static PixelFormatInfo::PixelType translatePixelType (int type)
328 {
329 	switch (type)
330 	{
331 		case WGL_TYPE_RGBA_ARB:					return PixelFormatInfo::PIXELTYPE_RGBA;
332 		case WGL_TYPE_RGBA_FLOAT_ARB:			return PixelFormatInfo::PIXELTYPE_RGBA_FLOAT;
333 		case WGL_TYPE_RGBA_UNSIGNED_FLOAT_EXT:	return PixelFormatInfo::PIXELTYPE_RGBA_UNSIGNED_FLOAT;
334 		case WGL_TYPE_COLORINDEX_ARB:			return PixelFormatInfo::PIXELTYPE_COLOR_INDEX;
335 		default:								return PixelFormatInfo::PIXELTYPE_UNKNOWN;
336 	}
337 }
338 
getPixelFormatAttribs(const Functions & wgl,HDC deviceCtx,int pixelFormat,int numAttribs,const int * attribs,std::map<int,int> * dst)339 static void getPixelFormatAttribs (const Functions& wgl, HDC deviceCtx, int pixelFormat, int numAttribs, const int* attribs, std::map<int, int>* dst)
340 {
341 	std::vector<int>	values	(numAttribs);
342 
343 	if (!wgl.getPixelFormatAttribivARB(deviceCtx, pixelFormat, 0, numAttribs, &attribs[0], &values[0]))
344 		TCU_THROW(ResourceError, "Pixel format query failed");
345 
346 	for (int ndx = 0; ndx < numAttribs; ++ndx)
347 		(*dst)[attribs[ndx]] = values[ndx];
348 }
349 
getPixelFormatInfo(HDC deviceCtx,int pixelFormat) const350 PixelFormatInfo Core::getPixelFormatInfo (HDC deviceCtx, int pixelFormat) const
351 {
352 	static const int	s_attribsToQuery[] =
353 	{
354 		WGL_DRAW_TO_WINDOW_ARB,
355 		WGL_DRAW_TO_BITMAP_ARB,
356 		WGL_ACCELERATION_ARB,
357 		WGL_NEED_PALETTE_ARB,
358 		WGL_NEED_SYSTEM_PALETTE_ARB,
359 		WGL_NUMBER_OVERLAYS_ARB,
360 		WGL_NUMBER_UNDERLAYS_ARB,
361 		WGL_SUPPORT_OPENGL_ARB,
362 		WGL_DOUBLE_BUFFER_ARB,
363 		WGL_STEREO_ARB,
364 		WGL_PIXEL_TYPE_ARB,
365 		WGL_RED_BITS_ARB,
366 		WGL_GREEN_BITS_ARB,
367 		WGL_BLUE_BITS_ARB,
368 		WGL_ALPHA_BITS_ARB,
369 		WGL_ACCUM_BITS_ARB,
370 		WGL_DEPTH_BITS_ARB,
371 		WGL_STENCIL_BITS_ARB,
372 		WGL_AUX_BUFFERS_ARB,
373 		WGL_SAMPLE_BUFFERS_ARB,
374 		WGL_SAMPLES_ARB,
375 	};
376 	const Functions&	wgl			= m_library->getFunctions();
377 	std::map<int, int>	values;
378 
379 	getPixelFormatAttribs(wgl, deviceCtx, pixelFormat, DE_LENGTH_OF_ARRAY(s_attribsToQuery), &s_attribsToQuery[0], &values);
380 
381 	// Translate values.
382 	PixelFormatInfo info;
383 
384 	info.pixelFormat		= pixelFormat;
385 	info.surfaceTypes		|= (values[WGL_DRAW_TO_WINDOW_ARB] ? PixelFormatInfo::SURFACE_WINDOW : 0);
386 	info.surfaceTypes		|= (values[WGL_DRAW_TO_BITMAP_ARB] ? PixelFormatInfo::SURFACE_PIXMAP : 0);
387 	info.acceleration		= translateAcceleration(values[WGL_ACCELERATION_ARB]);
388 	info.needPalette		= values[WGL_NEED_PALETTE_ARB] != 0;
389 	info.needSystemPalette	= values[WGL_NEED_SYSTEM_PALETTE_ARB] != 0;
390 	info.numOverlays		= values[WGL_NUMBER_OVERLAYS_ARB] != 0;
391 	info.numUnderlays		= values[WGL_NUMBER_UNDERLAYS_ARB] != 0;
392 	info.supportOpenGL		= values[WGL_SUPPORT_OPENGL_ARB] != 0;
393 	info.doubleBuffer		= values[WGL_DOUBLE_BUFFER_ARB] != 0;
394 	info.stereo				= values[WGL_STEREO_ARB] != 0;
395 	info.pixelType			= translatePixelType(values[WGL_PIXEL_TYPE_ARB]);
396 	info.redBits			= values[WGL_RED_BITS_ARB];
397 	info.greenBits			= values[WGL_GREEN_BITS_ARB];
398 	info.blueBits			= values[WGL_BLUE_BITS_ARB];
399 	info.alphaBits			= values[WGL_ALPHA_BITS_ARB];
400 	info.accumBits			= values[WGL_ACCUM_BITS_ARB];
401 	info.depthBits			= values[WGL_DEPTH_BITS_ARB];
402 	info.stencilBits		= values[WGL_STENCIL_BITS_ARB];
403 	info.numAuxBuffers		= values[WGL_AUX_BUFFERS_ARB];
404 	info.sampleBuffers		= values[WGL_SAMPLE_BUFFERS_ARB];
405 	info.samples			= values[WGL_SAMPLES_ARB];
406 
407 	return info;
408 }
409 
410 // Context
411 
Context(const Core * core,HDC deviceCtx,const Context * sharedContext,glu::ContextType ctxType,int pixelFormat,glu::ResetNotificationStrategy resetNotificationStrategy)412 Context::Context (const Core*						core,
413 				  HDC								deviceCtx,
414 				  const Context*					sharedContext,
415 				  glu::ContextType					ctxType,
416 				  int								pixelFormat,
417 				  glu::ResetNotificationStrategy	resetNotificationStrategy)
418 	: m_core		(core)
419 	, m_deviceCtx	(deviceCtx)
420 	, m_context		(0)
421 {
422 	const Functions&		wgl				= core->getLibrary()->getFunctions();
423 	std::vector<int>		attribList;
424 
425 	// Context version and profile
426 	{
427 		int	profileBit	= 0;
428 		HGLRC sharedCtx	= DE_NULL;
429 		int minor		= ctxType.getMinorVersion();
430 		int major		= ctxType.getMajorVersion();
431 
432 		switch (ctxType.getProfile())
433 		{
434 			case glu::PROFILE_CORE:
435 				profileBit = WGL_CONTEXT_CORE_PROFILE_BIT_ARB;
436 				if (major == 3 && minor < 3)
437 					minor = 3;
438 				break;
439 
440 			case glu::PROFILE_ES:
441 				profileBit = WGL_CONTEXT_ES_PROFILE_BIT_EXT;
442 				break;
443 
444 			case glu::PROFILE_COMPATIBILITY:
445 				profileBit = WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB;
446 				break;
447 
448 			default:
449 				TCU_THROW(NotSupportedError, "Unsupported context type for WGL");
450 		}
451 
452 		attribList.push_back(WGL_CONTEXT_MAJOR_VERSION_ARB);
453 		attribList.push_back(major);
454 		attribList.push_back(WGL_CONTEXT_MINOR_VERSION_ARB);
455 		attribList.push_back(minor);
456 		attribList.push_back(WGL_CONTEXT_PROFILE_MASK_ARB);
457 		attribList.push_back(profileBit);
458 	}
459 
460 	// Context flags
461 	{
462 		int		flags	= 0;
463 
464 		if ((ctxType.getFlags() & glu::CONTEXT_FORWARD_COMPATIBLE) != 0)
465 		{
466 			if (glu::isContextTypeES(ctxType))
467 				TCU_THROW(InternalError, "Only OpenGL core contexts can be forward-compatible");
468 
469 			flags |= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
470 		}
471 
472 		if ((ctxType.getFlags() & glu::CONTEXT_DEBUG) != 0)
473 			flags |= WGL_CONTEXT_DEBUG_BIT_ARB;
474 
475 		if ((ctxType.getFlags() & glu::CONTEXT_ROBUST) != 0)
476 			flags |= WGL_CONTEXT_ROBUST_ACCESS_BIT_ARB;
477 
478 		if ((ctxType.getFlags() & glu::CONTEXT_NO_ERROR) != 0)
479 		{
480 			if (core->getLibrary()->isWglExtensionSupported("WGL_ARB_create_context_no_error"))
481 			{
482 				attribList.push_back(WGL_CONTEXT_OPENGL_NO_ERROR_ARB);
483 				attribList.push_back(1);
484 			}
485 			else
486 				TCU_THROW(NotSupportedError, "WGL_ARB_create_context_no_error is required for creating no-error contexts");
487 		}
488 
489 		if (flags != 0)
490 		{
491 			attribList.push_back(WGL_CONTEXT_FLAGS_ARB);
492 			attribList.push_back(flags);
493 		}
494 	}
495 
496 	if (resetNotificationStrategy != glu::RESET_NOTIFICATION_STRATEGY_NOT_SPECIFIED)
497 	{
498 		attribList.push_back(WGL_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB);
499 
500 		if (resetNotificationStrategy == glu::RESET_NOTIFICATION_STRATEGY_NO_RESET_NOTIFICATION)
501 			attribList.push_back(WGL_NO_RESET_NOTIFICATION_ARB);
502 		else if (resetNotificationStrategy == glu::RESET_NOTIFICATION_STRATEGY_LOSE_CONTEXT_ON_RESET)
503 			attribList.push_back(WGL_LOSE_CONTEXT_ON_RESET_ARB);
504 		else
505 			TCU_THROW(InternalError, "Unknown reset notification strategy");
506 	}
507 
508 	// Set pixel format
509 	{
510 		PIXELFORMATDESCRIPTOR pixelFormatDesc;
511 		deMemset(&pixelFormatDesc, 0, sizeof(pixelFormatDesc));
512 
513 		if (!DescribePixelFormat(deviceCtx, pixelFormat, sizeof(pixelFormatDesc), &pixelFormatDesc))
514 			throw ResourceError("DescribePixelFormat() failed");
515 
516 		if (!SetPixelFormat(deviceCtx, pixelFormat, &pixelFormatDesc))
517 			throw ResourceError("Failed to set pixel format");
518 	}
519 
520 	HGLRC sharedCtx = DE_NULL;
521 	if (DE_NULL != sharedContext)
522 		sharedCtx = sharedContext->m_context;
523 
524 	// Terminate attribList
525 	attribList.push_back(0);
526 
527 	// Create context
528 	m_context = wgl.createContextAttribsARB(deviceCtx, sharedCtx, &attribList[0]);
529 
530 	if (!m_context)
531 		TCU_THROW(ResourceError, "Failed to create WGL context");
532 
533 	if (!wgl.makeCurrent(deviceCtx, m_context))
534 	{
535 		wgl.deleteContext(m_context);
536 		TCU_THROW(ResourceError, "wglMakeCurrent() failed");
537 	}
538 
539 	if (core->getLibrary()->isWglExtensionSupported("WGL_EXT_swap_control"))
540 		core->getLibrary()->getFunctions().swapIntervalEXT(0);
541 }
542 
~Context(void)543 Context::~Context (void)
544 {
545 	const Functions& wgl = m_core->getLibrary()->getFunctions();
546 
547 	wgl.makeCurrent(m_deviceCtx, NULL);
548 	wgl.deleteContext(m_context);
549 }
550 
getGLFunction(const char * name) const551 FunctionPtr Context::getGLFunction (const char* name) const
552 {
553 	FunctionPtr ptr = DE_NULL;
554 
555 	// Try first with wglGeProcAddress()
556 	ptr = (FunctionPtr)m_core->getLibrary()->getFunctions().getProcAddress(name);
557 
558 	// Fall-back to dynlib
559 	if (!ptr)
560 		ptr = (FunctionPtr)m_core->getLibrary()->getGLLibrary().getFunction(name);
561 
562 	return ptr;
563 }
564 
makeCurrent(void)565 void Context::makeCurrent (void)
566 {
567 	const Functions& wgl = m_core->getLibrary()->getFunctions();
568 	if (!wgl.makeCurrent(m_deviceCtx, m_context))
569 		TCU_THROW(ResourceError, "wglMakeCurrent() failed");
570 }
571 
swapBuffers(void) const572 void Context::swapBuffers (void) const
573 {
574 	const Functions& wgl = m_core->getLibrary()->getFunctions();
575 	if (!wgl.swapLayerBuffers(m_deviceCtx, WGL_SWAP_MAIN_PLANE))
576 		TCU_THROW(ResourceError, "wglSwapBuffers() failed");
577 }
578 
isSupportedByTests(const PixelFormatInfo & info)579 bool isSupportedByTests (const PixelFormatInfo& info)
580 {
581 	if (!info.supportOpenGL)
582 		return false;
583 
584 	if (info.acceleration != wgl::PixelFormatInfo::ACCELERATION_FULL)
585 		return false;
586 
587 	if (info.pixelType != wgl::PixelFormatInfo::PIXELTYPE_RGBA)
588 		return false;
589 
590 	if ((info.surfaceTypes & wgl::PixelFormatInfo::SURFACE_WINDOW) == 0)
591 		return false;
592 
593 	if (info.needPalette || info.needSystemPalette)
594 		return false;
595 
596 	if (info.numOverlays != 0 || info.numUnderlays != 0)
597 		return false;
598 
599 	if (info.stereo)
600 		return false;
601 
602 	return true;
603 }
604 
choosePixelFormat(const Core & wgl,HDC deviceCtx,const glu::RenderConfig & config)605 int choosePixelFormat (const Core& wgl, HDC deviceCtx, const glu::RenderConfig& config)
606 {
607 	std::vector<int> pixelFormats = wgl.getPixelFormats(deviceCtx);
608 
609 	for (std::vector<int>::const_iterator fmtIter = pixelFormats.begin(); fmtIter != pixelFormats.end(); ++fmtIter)
610 	{
611 		const PixelFormatInfo info = wgl.getPixelFormatInfo(deviceCtx, *fmtIter);
612 
613 		// Skip formats that are fundamentally not compatible with current tests
614 		if (!isSupportedByTests(info))
615 			continue;
616 
617 		if (config.redBits != glu::RenderConfig::DONT_CARE &&
618 			config.redBits != info.redBits)
619 			continue;
620 
621 		if (config.greenBits != glu::RenderConfig::DONT_CARE &&
622 			config.greenBits != info.greenBits)
623 			continue;
624 
625 		if (config.blueBits != glu::RenderConfig::DONT_CARE &&
626 			config.blueBits != info.blueBits)
627 			continue;
628 
629 		if (config.alphaBits != glu::RenderConfig::DONT_CARE &&
630 			config.alphaBits != info.alphaBits)
631 			continue;
632 
633 		if (config.depthBits != glu::RenderConfig::DONT_CARE &&
634 			config.depthBits != info.depthBits)
635 			continue;
636 
637 		if (config.stencilBits != glu::RenderConfig::DONT_CARE &&
638 			config.stencilBits != info.stencilBits)
639 			continue;
640 
641 		if (config.numSamples != glu::RenderConfig::DONT_CARE &&
642 			config.numSamples != info.samples)
643 			continue;
644 
645 		// Passed all tests - select this.
646 		return info.pixelFormat;
647 	}
648 
649 	return -1;
650 }
651 
652 } // wgl
653 } // tcu
654