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