tcuAndroidPlatform.cpp revision 3c827367444ee418f129b2c238299f49d3264554
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 Android EGL platform.
22 *//*--------------------------------------------------------------------*/
23
24#include "tcuAndroidPlatform.hpp"
25#include "gluRenderContext.hpp"
26#include "egluNativeDisplay.hpp"
27#include "egluNativeWindow.hpp"
28#include "egluGLContextFactory.hpp"
29#include "egluUtil.hpp"
30
31namespace tcu
32{
33namespace Android
34{
35
36static const eglu::NativeDisplay::Capability	DISPLAY_CAPABILITIES	= eglu::NativeDisplay::CAPABILITY_GET_DISPLAY_LEGACY;
37static const eglu::NativeWindow::Capability		WINDOW_CAPABILITIES		= (eglu::NativeWindow::Capability)(eglu::NativeWindow::CAPABILITY_CREATE_SURFACE_LEGACY |
38																										   eglu::NativeWindow::CAPABILITY_SET_SURFACE_SIZE |
39																										   eglu::NativeWindow::CAPABILITY_GET_SCREEN_SIZE);
40
41class NativeDisplay : public eglu::NativeDisplay
42{
43public:
44									NativeDisplay			(void) : eglu::NativeDisplay(DISPLAY_CAPABILITIES) {}
45	virtual							~NativeDisplay			(void) {}
46
47	virtual EGLNativeDisplayType	getLegacyNative			(void) { return EGL_DEFAULT_DISPLAY; }
48};
49
50class NativeDisplayFactory : public eglu::NativeDisplayFactory
51{
52public:
53									NativeDisplayFactory	(Window& window);
54									~NativeDisplayFactory	(void) {}
55
56	virtual eglu::NativeDisplay*	createDisplay			(const EGLAttrib* attribList) const;
57};
58
59class NativeWindow : public eglu::NativeWindow
60{
61public:
62									NativeWindow			(Window& window, int width, int height, int32_t format);
63	virtual							~NativeWindow			(void);
64
65	virtual EGLNativeWindowType		getLegacyNative			(void)			{ return m_window.getNativeWindow();	}
66	IVec2							getScreenSize			(void) const	{ return m_window.getSize();			}
67
68	void							setSurfaceSize			(IVec2 size);
69
70	virtual void					processEvents			(void);
71
72private:
73	Window&							m_window;
74	int32_t							m_format;
75};
76
77class NativeWindowFactory : public eglu::NativeWindowFactory
78{
79public:
80									NativeWindowFactory		(Window& window);
81									~NativeWindowFactory	(void);
82
83	virtual eglu::NativeWindow*		createWindow			(eglu::NativeDisplay* nativeDisplay, const eglu::WindowParams& params) const;
84	virtual eglu::NativeWindow*		createWindow			(eglu::NativeDisplay* nativeDisplay, EGLDisplay display, EGLConfig config, const EGLAttrib* attribList, const eglu::WindowParams& params) const;
85
86private:
87	Window&							m_window;
88};
89
90// NativeWindow
91
92NativeWindow::NativeWindow (Window& window, int width, int height, int32_t format)
93	: eglu::NativeWindow	(WINDOW_CAPABILITIES)
94	, m_window				(window)
95	, m_format				(format)
96{
97	// Try to acquire window.
98	if (!m_window.tryAcquire())
99		throw ResourceError("Native window is not available", "", __FILE__, __LINE__);
100
101	// Set up buffers.
102	setSurfaceSize(IVec2(width, height));
103}
104
105NativeWindow::~NativeWindow (void)
106{
107	m_window.release();
108}
109
110void NativeWindow::processEvents (void)
111{
112}
113
114void NativeWindow::setSurfaceSize (tcu::IVec2 size)
115{
116	m_window.setBuffersGeometry(size.x() != eglu::WindowParams::SIZE_DONT_CARE ? size.x() : 0,
117								size.y() != eglu::WindowParams::SIZE_DONT_CARE ? size.y() : 0,
118								m_format);
119}
120
121// NativeWindowFactory
122
123NativeWindowFactory::NativeWindowFactory (Window& window)
124	: eglu::NativeWindowFactory	("default", "Default display", WINDOW_CAPABILITIES)
125	, m_window					(window)
126{
127}
128
129NativeWindowFactory::~NativeWindowFactory (void)
130{
131}
132
133eglu::NativeWindow* NativeWindowFactory::createWindow (eglu::NativeDisplay* nativeDisplay, const eglu::WindowParams& params) const
134{
135	DE_UNREF(nativeDisplay);
136	return new NativeWindow(m_window, params.width, params.height, WINDOW_FORMAT_RGBA_8888);
137}
138
139eglu::NativeWindow* NativeWindowFactory::createWindow (eglu::NativeDisplay* nativeDisplay, EGLDisplay display, EGLConfig config, const EGLAttrib* attribList, const eglu::WindowParams& params) const
140{
141	const int32_t format = (int32_t)eglu::getConfigAttribInt(display, config, EGL_NATIVE_VISUAL_ID);
142	DE_UNREF(nativeDisplay && attribList);
143	return new NativeWindow(m_window, params.width, params.height, format);
144}
145
146// NativeDisplayFactory
147
148NativeDisplayFactory::NativeDisplayFactory (Window& window)
149	: eglu::NativeDisplayFactory("default", "Default display", DISPLAY_CAPABILITIES)
150{
151	m_nativeWindowRegistry.registerFactory(new NativeWindowFactory(window));
152}
153
154eglu::NativeDisplay* NativeDisplayFactory::createDisplay (const EGLAttrib* attribList) const
155{
156	DE_UNREF(attribList);
157	return new NativeDisplay();
158}
159
160// Platform
161
162Platform::Platform (ANativeWindow* window)
163	: m_window(window)
164{
165	m_nativeDisplayFactoryRegistry.registerFactory(new NativeDisplayFactory(m_window));
166	m_contextFactoryRegistry.registerFactory(new eglu::GLContextFactory(m_nativeDisplayFactoryRegistry));
167}
168
169Platform::~Platform (void)
170{
171}
172
173} // Android
174} // tcu
175