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 GL context factory.
22 *//*--------------------------------------------------------------------*/
23
24 #include "tcuWGLContextFactory.hpp"
25
26 #include "gluRenderConfig.hpp"
27 #include "tcuRenderTarget.hpp"
28 #include "tcuWin32Window.hpp"
29 #include "glwFunctions.hpp"
30 #include "glwInitFunctions.hpp"
31 #include "deString.h"
32
33 using std::vector;
34
35 namespace tcu
36 {
37 namespace wgl
38 {
39 namespace
40 {
41
42 enum
43 {
44 DEFAULT_WINDOW_WIDTH = 400,
45 DEFAULT_WINDOW_HEIGHT = 300
46 };
47
48 class WGLFunctionLoader : public glw::FunctionLoader
49 {
50 public:
WGLFunctionLoader(const wgl::Context & context)51 WGLFunctionLoader (const wgl::Context& context)
52 : m_context(context)
53 {
54 }
55
get(const char * name) const56 glw::GenericFuncType get (const char* name) const
57 {
58 return (glw::GenericFuncType)m_context.getGLFunction(name);
59 }
60
61 private:
62 const wgl::Context& m_context;
63 };
64
65 class WGLContext : public glu::RenderContext
66 {
67 public:
68 WGLContext (HINSTANCE instance, const wgl::Core& wglCore, const glu::RenderConfig& config);
69 ~WGLContext (void);
70
getType(void) const71 glu::ContextType getType (void) const { return m_contextType; }
getRenderTarget(void) const72 const RenderTarget& getRenderTarget (void) const { return m_renderTarget; }
73 void postIterate (void);
getFunctions(void) const74 const glw::Functions& getFunctions (void) const { return m_functions; }
75
76 private:
77 WGLContext (const WGLContext& other);
78 WGLContext& operator= (const WGLContext& other);
79
80 glu::ContextType m_contextType;
81
82 win32::Window m_window;
83 wgl::Context* m_context;
84
85 tcu::RenderTarget m_renderTarget;
86 glw::Functions m_functions;
87 };
88
WGLContext(HINSTANCE instance,const wgl::Core & wglCore,const glu::RenderConfig & config)89 WGLContext::WGLContext (HINSTANCE instance, const wgl::Core& wglCore, const glu::RenderConfig& config)
90 : m_contextType (config.type)
91 , m_window (instance,
92 config.width != glu::RenderConfig::DONT_CARE ? config.width : DEFAULT_WINDOW_WIDTH,
93 config.height != glu::RenderConfig::DONT_CARE ? config.height : DEFAULT_WINDOW_HEIGHT)
94 , m_context (DE_NULL)
95 {
96 if (config.surfaceType != glu::RenderConfig::SURFACETYPE_WINDOW &&
97 config.surfaceType != glu::RenderConfig::SURFACETYPE_DONT_CARE)
98 throw NotSupportedError("Unsupported surface type");
99
100 HDC deviceCtx = m_window.getDeviceContext();
101 int pixelFormat = 0;
102
103 if (config.id != glu::RenderConfig::DONT_CARE)
104 pixelFormat = config.id;
105 else
106 pixelFormat = wgl::choosePixelFormat(wglCore, deviceCtx, config);
107
108 if (pixelFormat < 0)
109 throw NotSupportedError("Compatible WGL pixel format not found");
110
111 m_context = new wgl::Context(&wglCore, deviceCtx, config.type, pixelFormat);
112
113 try
114 {
115 // Describe selected config & get render target props.
116 const wgl::PixelFormatInfo info = wglCore.getPixelFormatInfo(deviceCtx, pixelFormat);
117 const IVec2 size = m_window.getSize();
118
119 m_renderTarget = tcu::RenderTarget(size.x(), size.y(),
120 tcu::PixelFormat(info.redBits, info.greenBits, info.blueBits, info.alphaBits),
121 info.depthBits, info.stencilBits,
122 info.sampleBuffers ? info.samples : 0);
123
124 // Load functions
125 {
126 WGLFunctionLoader funcLoader(*m_context);
127 glu::initFunctions(&m_functions, &funcLoader, config.type.getAPI());
128 }
129
130 if (config.windowVisibility != glu::RenderConfig::VISIBILITY_VISIBLE &&
131 config.windowVisibility != glu::RenderConfig::VISIBILITY_HIDDEN)
132 throw NotSupportedError("Unsupported window visibility mode");
133
134 m_window.setVisible(config.windowVisibility != glu::RenderConfig::VISIBILITY_HIDDEN);
135 }
136 catch (...)
137 {
138 delete m_context;
139 throw;
140 }
141 }
142
~WGLContext(void)143 WGLContext::~WGLContext (void)
144 {
145 delete m_context;
146 }
147
postIterate(void)148 void WGLContext::postIterate (void)
149 {
150 m_context->swapBuffers();
151 m_window.processEvents();
152 }
153
154 } // anonymous
155
ContextFactory(HINSTANCE instance)156 ContextFactory::ContextFactory (HINSTANCE instance)
157 : glu::ContextFactory ("wgl", "Windows WGL OpenGL context")
158 , m_instance (instance)
159 , m_wglCore (instance)
160 {
161 }
162
createContext(const glu::RenderConfig & config,const tcu::CommandLine &) const163 glu::RenderContext* ContextFactory::createContext (const glu::RenderConfig& config, const tcu::CommandLine&) const
164 {
165 return new WGLContext(m_instance, m_wglCore, config);
166 }
167
168 } // wgl
169 } // tcu
170