13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements Quality Program Tester Core
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * ----------------------------------------
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License");
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License.
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *      http://www.apache.org/licenses/LICENSE-2.0
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS,
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License.
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*!
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief WGL GL context factory.
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuWGLContextFactory.hpp"
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "gluRenderConfig.hpp"
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuRenderTarget.hpp"
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuWin32Window.hpp"
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "glwFunctions.hpp"
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "glwInitFunctions.hpp"
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deString.h"
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing std::vector;
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace tcu
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyryenum
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DEFAULT_WINDOW_WIDTH	= 400,
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DEFAULT_WINDOW_HEIGHT	= 300
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass WGLFunctionLoader : public glw::FunctionLoader
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	WGLFunctionLoader (const wgl::Context& context)
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		: m_context(context)
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	glw::GenericFuncType get (const char* name) const
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return (glw::GenericFuncType)m_context.getGLFunction(name);
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const wgl::Context& m_context;
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass WGLContext : public glu::RenderContext
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry									WGLContext			(HINSTANCE instance, const wgl::Core& wglCore, const glu::RenderConfig& config);
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry									~WGLContext			(void);
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	glu::ContextType				getType				(void) const	{ return m_contextType;			}
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const RenderTarget&				getRenderTarget		(void) const	{ return m_renderTarget;		}
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void							postIterate			(void);
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const glw::Functions&			getFunctions		(void) const	{ return m_functions;			}
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry									WGLContext			(const WGLContext& other);
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	WGLContext&						operator=			(const WGLContext& other);
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	glu::ContextType				m_contextType;
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Win32Window						m_window;
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	wgl::Context*					m_context;
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	tcu::RenderTarget				m_renderTarget;
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	glw::Functions					m_functions;
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
873c827367444ee418f129b2c238299f49d3264554Jarkko PoyryWGLContext::WGLContext (HINSTANCE instance, const wgl::Core& wglCore, const glu::RenderConfig& config)
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	: m_contextType	(config.type)
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_window		(instance,
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					 config.width	!= glu::RenderConfig::DONT_CARE	? config.width	: DEFAULT_WINDOW_WIDTH,
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					 config.height	!= glu::RenderConfig::DONT_CARE	? config.height	: DEFAULT_WINDOW_HEIGHT)
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_context		(DE_NULL)
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (config.surfaceType != glu::RenderConfig::SURFACETYPE_WINDOW &&
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		config.surfaceType != glu::RenderConfig::SURFACETYPE_DONT_CARE)
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		throw NotSupportedError("Unsupported surface type");
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	HDC		deviceCtx	= m_window.getDeviceContext();
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int		pixelFormat	= 0;
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (config.id != glu::RenderConfig::DONT_CARE)
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		pixelFormat = config.id;
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		pixelFormat = wgl::choosePixelFormat(wglCore, deviceCtx, config);
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (pixelFormat < 0)
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		throw NotSupportedError("Compatible WGL pixel format not found");
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_context = new wgl::Context(&wglCore, deviceCtx, config.type, pixelFormat);
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	try
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Describe selected config & get render target props.
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const wgl::PixelFormatInfo	info	= wglCore.getPixelFormatInfo(deviceCtx, pixelFormat);
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const IVec2					size	= m_window.getSize();
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_renderTarget = tcu::RenderTarget(size.x(), size.y(),
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry										   tcu::PixelFormat(info.redBits, info.greenBits, info.blueBits, info.alphaBits),
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry										   info.depthBits, info.stencilBits,
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry										   info.sampleBuffers ? info.samples : 0);
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Load functions
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			WGLFunctionLoader funcLoader(*m_context);
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			glu::initFunctions(&m_functions, &funcLoader, config.type.getAPI());
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (config.windowVisibility != glu::RenderConfig::VISIBILITY_VISIBLE &&
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			config.windowVisibility != glu::RenderConfig::VISIBILITY_HIDDEN)
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			throw NotSupportedError("Unsupported window visibility mode");
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_window.setVisible(config.windowVisibility != glu::RenderConfig::VISIBILITY_HIDDEN);
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	catch (...)
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		delete m_context;
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		throw;
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1413c827367444ee418f129b2c238299f49d3264554Jarkko PoyryWGLContext::~WGLContext (void)
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	delete m_context;
1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid WGLContext::postIterate (void)
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_context->swapBuffers();
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_window.processEvents();
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // anonymous
1533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1543c827367444ee418f129b2c238299f49d3264554Jarkko PoyryWGLContextFactory::WGLContextFactory (HINSTANCE instance)
1553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	: glu::ContextFactory	("wgl", "Windows WGL OpenGL context")
1563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_instance			(instance)
1573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_wglCore				(instance)
1583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1613c827367444ee418f129b2c238299f49d3264554Jarkko Poyryglu::RenderContext* WGLContextFactory::createContext (const glu::RenderConfig& config, const tcu::CommandLine&) const
1623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return new WGLContext(m_instance, m_wglCore, config);
1643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // tcu
167