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)
76 // Reset buffer size and format back to initial state
77 ANativeWindow_setBuffersGeometry(m_window, 0, 0, 0);
79 m_state = STATE_AVAILABLE;
81 else if (m_state == STATE_PENDING_DESTROY)
82 m_state = STATE_READY_FOR_DESTROY;
84 DE_FATAL("Invalid window state");
87 void Window::markForDestroy (void)
89 de::ScopedLock lock(m_stateLock);
91 if (m_state == STATE_AVAILABLE)
92 m_state = STATE_READY_FOR_DESTROY;
93 else if (m_state == STATE_IN_USE)
94 m_state = STATE_PENDING_DESTROY;
96 DE_FATAL("Invalid window state");
99 bool Window::isPendingDestroy (void) const
101 de::ScopedLock lock(m_stateLock);
102 return m_state == STATE_PENDING_DESTROY;
105 bool Window::tryAcquireForDestroy (bool onlyMarked)
107 de::ScopedLock lock(m_stateLock);
109 if (m_state == STATE_READY_FOR_DESTROY ||
110 (!onlyMarked && m_state == STATE_AVAILABLE))
112 m_state = STATE_ACQUIRED_FOR_DESTROY;
121 WindowRegistry::WindowRegistry (void)
125 WindowRegistry::~WindowRegistry (void)
127 for (vector<Window*>::const_iterator winIter = m_windows.begin(); winIter != m_windows.end(); winIter++)
129 Window* const window = *winIter;
131 if (window->tryAcquireForDestroy(false))
135 print("ERROR: Window was not available for deletion, leaked tcu::Android::Window!\n");
136 DE_FATAL("Window leaked");
141 void WindowRegistry::addWindow (ANativeWindow* window)
143 m_windows.reserve(m_windows.size()+1);
144 m_windows.push_back(new Window(window));
147 void WindowRegistry::destroyWindow (ANativeWindow* rawHandle)
149 for (int ndx = 0; ndx < (int)m_windows.size(); ++ndx)
151 Window* const window = m_windows[ndx];
153 if (window->getNativeWindow() == rawHandle)
155 if (window->tryAcquireForDestroy(false))
158 m_windows[ndx] = m_windows.back();
159 m_windows.pop_back();
162 window->markForDestroy();
168 throw tcu::InternalError("Window not registered", DE_NULL, __FILE__, __LINE__);
171 Window* WindowRegistry::tryAcquireWindow (void)
173 for (int ndx = 0; ndx < (int)m_windows.size(); ++ndx)
175 Window* const window = m_windows[ndx];
177 if (window->tryAcquire())
184 void WindowRegistry::garbageCollect (void)
186 for (int ndx = 0; ndx < (int)m_windows.size(); ++ndx)
188 Window* const window = m_windows[ndx];
190 if (window->tryAcquireForDestroy(true))
193 m_windows[ndx] = m_windows.back();
194 m_windows.pop_back();