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 X11Egl Platform.
22 *//*--------------------------------------------------------------------*/
23
24#include "tcuX11EglPlatform.hpp"
25
26#include "egluGLContextFactory.hpp"
27
28namespace tcu
29{
30namespace x11
31{
32namespace egl
33{
34
35using std::string;
36
37using de::MovePtr;
38using de::UniquePtr;
39using glu::ContextFactory;
40using eglu::GLContextFactory;
41using eglu::NativeDisplay;
42using eglu::NativeDisplayFactory;
43using eglu::NativeWindow;
44using eglu::NativeWindowFactory;
45using eglu::NativePixmap;
46using eglu::NativePixmapFactory;
47using eglu::WindowParams;
48using tcu::TextureLevel;
49
50#ifndef EGL_PLATFORM_X11_EXT
51#	define EGL_PLATFORM_X11_EXT			0x31D5
52#endif
53#ifndef EGL_PLATFORM_X11_SCREEN_EXT
54#	define EGL_PLATFORM_X11_SCREEN_EXT	0x31D6
55#endif
56
57class Display : public NativeDisplay
58{
59public:
60	static const Capability CAPABILITIES 		= Capability(CAPABILITY_GET_DISPLAY_LEGACY |
61															 CAPABILITY_GET_DISPLAY_PLATFORM);
62
63							Display				(MovePtr<x11::Display> x11Display)
64								: NativeDisplay	(CAPABILITIES,
65												 EGL_PLATFORM_X11_EXT,
66												 "EGL_EXT_platform_x11")
67								, m_display		(x11Display) {}
68
69	void*					getPlatformNative	(void) 	{ return m_display->getXDisplay(); }
70	EGLNativeDisplayType	getLegacyNative		(void)	{ return m_display->getXDisplay(); }
71
72	x11::Display&			getX11Display		(void)	{ return *m_display; }
73
74private:
75	UniquePtr<x11::Display>	m_display;
76};
77
78
79class Window : public NativeWindow
80{
81public:
82	static const Capability	CAPABILITIES		= Capability(CAPABILITY_CREATE_SURFACE_LEGACY |
83															 CAPABILITY_CREATE_SURFACE_PLATFORM |
84															 CAPABILITY_GET_SURFACE_SIZE |
85															 CAPABILITY_SET_SURFACE_SIZE |
86															 CAPABILITY_GET_SCREEN_SIZE);
87
88							Window				(Display&				display,
89												 const WindowParams&	params,
90												 Visual*				visual);
91
92	EGLNativeWindowType		getLegacyNative		(void) { return m_window.getXID(); }
93	void*					getPlatformNative	(void) { return &m_window.getXID(); }
94	IVec2					getSurfaceSize		(void) const;
95	void					setSurfaceSize		(IVec2 size);
96	IVec2					getScreenSize		(void) const { return getSurfaceSize(); }
97
98private:
99	x11::Window				m_window;
100};
101
102Window::Window(Display& display, const WindowParams& params, Visual* visual)
103	: NativeWindow	(CAPABILITIES)
104	, m_window		(display.getX11Display(), params.width, params.height, visual)
105{
106	m_window.setVisibility((params.visibility != WindowParams::VISIBILITY_HIDDEN));
107}
108
109IVec2 Window::getSurfaceSize (void) const
110{
111	IVec2 ret;
112	m_window.getDimensions(&ret.x(), &ret.y());
113	return ret;
114}
115
116void Window::setSurfaceSize (IVec2 size)
117{
118	m_window.setDimensions(size.x(), size.y());
119}
120
121class WindowFactory : public NativeWindowFactory
122{
123public:
124						WindowFactory		(void);
125
126	NativeWindow*		createWindow		(NativeDisplay*			nativeDisplay,
127											 const WindowParams&	params) const;
128
129	NativeWindow*		createWindow		(NativeDisplay*			nativeDisplay,
130											 EGLDisplay				display,
131											 EGLConfig				config,
132											 const EGLAttrib*		attribList,
133											 const WindowParams&	params) const;
134};
135
136WindowFactory::WindowFactory (void)
137	: NativeWindowFactory ("window", "X11 Window", Window::CAPABILITIES)
138{
139}
140
141NativeWindow* WindowFactory::createWindow (NativeDisplay*		nativeDisplay,
142										   const WindowParams&	params) const
143{
144	Display&	display	= *dynamic_cast<Display*>(nativeDisplay);
145
146	return new Window(display, params, DE_NULL);
147}
148
149NativeWindow* WindowFactory::createWindow (NativeDisplay*			nativeDisplay,
150										   EGLDisplay				eglDisplay,
151										   EGLConfig				config,
152										   const EGLAttrib*			attribList,
153										   const WindowParams&		params) const
154{
155	DE_UNREF(attribList);
156
157	Display&	display		= *dynamic_cast<Display*>(nativeDisplay);
158	EGLint		visualID	= 0;
159	::Visual*	visual		= DE_NULL;
160	eglGetConfigAttrib(eglDisplay, config, EGL_NATIVE_VISUAL_ID, &visualID);
161
162	if (visualID != 0)
163		visual = display.getX11Display().getVisual(visualID);
164
165	return new Window(display, params, visual);
166}
167
168#if 0
169class Pixmap : public NativePixmap
170{
171public:
172	enum {
173		CAPABILITIES = (CAPABILITY_CREATE_SURFACE_LEGACY |
174						CAPABILITY_CREATE_SURFACE_PLATFORM |
175						CAPABILITY_READ_PIXELS)
176	};
177
178							Pixmap				(MovePtr<x11::Pixmap> x11Pixmap)
179								: NativePixmap	(CAPABILITIES)
180								, m_pixmap		(x11Pixmap) {}
181
182	void*					getPlatformNative	(void) { return &m_pixmap.getXID(); }
183	void					readPixels			(TextureLevel* dst);
184
185private:
186	UniquePtr<x11::Pixmap>	m_pixmap;
187};
188
189class PixmapFactory : public NativePixmapFactory
190{
191public:
192					PixmapFactory	(void)
193						: NativePixmapFactory ("pixmap", "X11 Pixmap", Pixmap::CAPABILITIES) {}
194
195	NativePixmap*	createPixmap	(NativeDisplay* nativeDisplay,
196									 int			width,
197									 int			height) const;
198};
199
200NativePixmap* PixmapFactory::createPixmap (NativeDisplay* nativeDisplay,
201										   int			width,
202										   int			height) const
203
204{
205	Display*				display		= dynamic_cast<Display*>(nativeDisplay);
206	MovePtr<x11::Pixmap>	x11Pixmap	(new x11::Pixmap(display->getX11Display(),
207														 width, height));
208	return new Pixmap(x11Pixmap);
209}
210#endif
211
212class DisplayFactory : public NativeDisplayFactory
213{
214public:
215						DisplayFactory		(EventState& eventState);
216
217	NativeDisplay*		createDisplay		(const EGLAttrib* attribList) const;
218
219private:
220	EventState&			m_eventState;
221};
222
223DisplayFactory::DisplayFactory (EventState& eventState)
224	: NativeDisplayFactory	("x11", "Native X11 Display",
225							 Display::CAPABILITIES,
226							 EGL_PLATFORM_X11_SCREEN_EXT,
227							 "EGL_EXT_platform_x11")
228	, m_eventState			(eventState)
229{
230	m_nativeWindowRegistry.registerFactory(new WindowFactory());
231	// m_nativePixmapRegistry.registerFactory(new PixmapFactory());
232}
233
234NativeDisplay* DisplayFactory::createDisplay (const EGLAttrib* attribList) const
235{
236	DE_UNREF(attribList);
237
238	//! \todo [2014-03-18 lauri] Somehow make the display configurable from command line
239	MovePtr<x11::Display>	x11Display	(new x11::Display(m_eventState, DE_NULL));
240
241	return new Display(x11Display);
242}
243
244Platform::Platform (EventState& eventState)
245{
246	m_nativeDisplayFactoryRegistry.registerFactory(new DisplayFactory(eventState));
247}
248
249MovePtr<ContextFactory> Platform::createContextFactory (void)
250{
251	return MovePtr<ContextFactory>(new GLContextFactory(m_nativeDisplayFactoryRegistry));
252}
253
254
255} // egl
256} // x11
257} // tcu
258