1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Tester Core
3 * ----------------------------------------
5 * Copyright 2014 The Android Open Source Project
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * \brief Android window.
22 *//*--------------------------------------------------------------------*/
24 #include "tcuAndroidWindow.hpp"
35 Window::Window (ANativeWindow* window)
37 , m_state (STATE_AVAILABLE)
41 Window::~Window (void)
45 void Window::setBuffersGeometry (int width, int height, int32_t format)
47 ANativeWindow_setBuffersGeometry(m_window, width, height, format);
50 IVec2 Window::getSize (void) const
52 const int32_t width = ANativeWindow_getWidth(m_window);
53 const int32_t height = ANativeWindow_getHeight(m_window);
54 return IVec2(width, height);
57 bool Window::tryAcquire (void)
59 de::ScopedLock lock(m_stateLock);
61 if (m_state == STATE_AVAILABLE)
63 m_state = STATE_IN_USE;
70 void Window::release (void)
72 de::ScopedLock lock(m_stateLock);
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;
79 DE_ASSERT(!"Invalid window state");
82 void Window::markForDestroy (void)
84 de::ScopedLock lock(m_stateLock);
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;
91 DE_ASSERT(!"Invalid window state");
94 bool Window::isPendingDestroy (void) const
96 de::ScopedLock lock(m_stateLock);
97 return m_state == STATE_PENDING_DESTROY;
100 bool Window::tryAcquireForDestroy (bool onlyMarked)
102 de::ScopedLock lock(m_stateLock);
104 if (m_state == STATE_READY_FOR_DESTROY ||
105 (!onlyMarked && m_state == STATE_AVAILABLE))
107 m_state = STATE_ACQUIRED_FOR_DESTROY;
116 WindowRegistry::WindowRegistry (void)
120 WindowRegistry::~WindowRegistry (void)
122 for (vector<Window*>::const_iterator winIter = m_windows.begin(); winIter != m_windows.end(); winIter++)
124 Window* const window = *winIter;
126 if (window->tryAcquireForDestroy(false))
130 print("ERROR: Window was not available for deletion, leaked tcu::Android::Window!\n");
131 DE_ASSERT(!"Window leaked");
136 void WindowRegistry::addWindow (ANativeWindow* window)
138 m_windows.reserve(m_windows.size()+1);
139 m_windows.push_back(new Window(window));
142 void WindowRegistry::destroyWindow (ANativeWindow* rawHandle)
144 for (int ndx = 0; ndx < (int)m_windows.size(); ++ndx)
146 Window* const window = m_windows[ndx];
148 if (window->getNativeWindow() == rawHandle)
150 if (window->tryAcquireForDestroy(false))
153 m_windows[ndx] = m_windows.back();
154 m_windows.pop_back();
157 window->markForDestroy();
163 throw tcu::InternalError("Window not registered", DE_NULL, __FILE__, __LINE__);
166 Window* WindowRegistry::tryAcquireWindow (void)
168 for (int ndx = 0; ndx < (int)m_windows.size(); ++ndx)
170 Window* const window = m_windows[ndx];
172 if (window->tryAcquire())
179 void WindowRegistry::garbageCollect (void)
181 for (int ndx = 0; ndx < (int)m_windows.size(); ++ndx)
183 Window* const window = m_windows[ndx];
185 if (window->tryAcquireForDestroy(true))
188 m_windows[ndx] = m_windows.back();
189 m_windows.pop_back();