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 window.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "tcuAndroidWindow.hpp"
25 
26 namespace tcu
27 {
28 namespace Android
29 {
30 
31 using std::vector;
32 
33 // Window
34 
Window(ANativeWindow * window)35 Window::Window (ANativeWindow* window)
36 	: m_window	(window)
37 	, m_state	(STATE_AVAILABLE)
38 {
39 }
40 
~Window(void)41 Window::~Window (void)
42 {
43 }
44 
setBuffersGeometry(int width,int height,int32_t format)45 void Window::setBuffersGeometry (int width, int height, int32_t format)
46 {
47 	ANativeWindow_setBuffersGeometry(m_window, width, height, format);
48 }
49 
getSize(void) const50 IVec2 Window::getSize (void) const
51 {
52 	const int32_t	width	= ANativeWindow_getWidth(m_window);
53 	const int32_t	height	= ANativeWindow_getHeight(m_window);
54 	return IVec2(width, height);
55 }
56 
tryAcquire(void)57 bool Window::tryAcquire (void)
58 {
59 	de::ScopedLock lock(m_stateLock);
60 
61 	if (m_state == STATE_AVAILABLE)
62 	{
63 		m_state = STATE_IN_USE;
64 		return true;
65 	}
66 	else
67 		return false;
68 }
69 
release(void)70 void Window::release (void)
71 {
72 	de::ScopedLock lock(m_stateLock);
73 
74 	if (m_state == STATE_IN_USE)
75 		m_state = STATE_AVAILABLE;
76 	else if (m_state == STATE_PENDING_DESTROY)
77 		m_state = STATE_READY_FOR_DESTROY;
78 	else
79 		DE_ASSERT(!"Invalid window state");
80 }
81 
markForDestroy(void)82 void Window::markForDestroy (void)
83 {
84 	de::ScopedLock lock(m_stateLock);
85 
86 	if (m_state == STATE_AVAILABLE)
87 		m_state = STATE_READY_FOR_DESTROY;
88 	else if (m_state == STATE_IN_USE)
89 		m_state = STATE_PENDING_DESTROY;
90 	else
91 		DE_ASSERT(!"Invalid window state");
92 }
93 
isPendingDestroy(void) const94 bool Window::isPendingDestroy (void) const
95 {
96 	de::ScopedLock lock(m_stateLock);
97 	return m_state == STATE_PENDING_DESTROY;
98 }
99 
tryAcquireForDestroy(bool onlyMarked)100 bool Window::tryAcquireForDestroy (bool onlyMarked)
101 {
102 	de::ScopedLock lock(m_stateLock);
103 
104 	if (m_state == STATE_READY_FOR_DESTROY ||
105 		(!onlyMarked && m_state == STATE_AVAILABLE))
106 	{
107 		m_state = STATE_ACQUIRED_FOR_DESTROY;
108 		return true;
109 	}
110 	else
111 		return false;
112 }
113 
114 // WindowRegistry
115 
WindowRegistry(void)116 WindowRegistry::WindowRegistry (void)
117 {
118 }
119 
~WindowRegistry(void)120 WindowRegistry::~WindowRegistry (void)
121 {
122 	for (vector<Window*>::const_iterator winIter = m_windows.begin(); winIter != m_windows.end(); winIter++)
123 	{
124 		Window* const window = *winIter;
125 
126 		if (window->tryAcquireForDestroy(false))
127 			delete window;
128 		else
129 		{
130 			print("ERROR: Window was not available for deletion, leaked tcu::Android::Window!\n");
131 			DE_ASSERT(!"Window leaked");
132 		}
133 	}
134 }
135 
addWindow(ANativeWindow * window)136 void WindowRegistry::addWindow (ANativeWindow* window)
137 {
138 	m_windows.reserve(m_windows.size()+1);
139 	m_windows.push_back(new Window(window));
140 }
141 
destroyWindow(ANativeWindow * rawHandle)142 void WindowRegistry::destroyWindow (ANativeWindow* rawHandle)
143 {
144 	for (int ndx = 0; ndx < (int)m_windows.size(); ++ndx)
145 	{
146 		Window* const window = m_windows[ndx];
147 
148 		if (window->getNativeWindow() == rawHandle)
149 		{
150 			if (window->tryAcquireForDestroy(false))
151 			{
152 				delete window;
153 				m_windows[ndx] = m_windows.back();
154 				m_windows.pop_back();
155 			}
156 			else
157 				window->markForDestroy();
158 
159 			return;
160 		}
161 	}
162 
163 	throw tcu::InternalError("Window not registered", DE_NULL, __FILE__, __LINE__);
164 }
165 
tryAcquireWindow(void)166 Window* WindowRegistry::tryAcquireWindow (void)
167 {
168 	for (int ndx = 0; ndx < (int)m_windows.size(); ++ndx)
169 	{
170 		Window* const window = m_windows[ndx];
171 
172 		if (window->tryAcquire())
173 			return window;
174 	}
175 
176 	return DE_NULL;
177 }
178 
garbageCollect(void)179 void WindowRegistry::garbageCollect (void)
180 {
181 	for (int ndx = 0; ndx < (int)m_windows.size(); ++ndx)
182 	{
183 		Window* const window = m_windows[ndx];
184 
185 		if (window->tryAcquireForDestroy(true))
186 		{
187 			delete window;
188 			m_windows[ndx] = m_windows.back();
189 			m_windows.pop_back();
190 			ndx -= 1;
191 		}
192 	}
193 }
194 
195 } // Android
196 } // tcu
197