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 RenderActivity base class.
22 *//*--------------------------------------------------------------------*/
24 #include "tcuAndroidRenderActivity.hpp"
25 #include "deSemaphore.hpp"
27 #include <android/window.h>
35 # define DBG_PRINT(X) print X
47 MESSAGE_QUEUE_SIZE = 8 //!< Length of RenderThread message queue.
51 static const char* getMessageTypeName (MessageType type)
53 static const char* s_names[] =
61 "INPUT_QUEUE_CREATED",
62 "INPUT_QUEUE_DESTROYED",
65 DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(s_names) == MESSAGETYPE_LAST);
72 RenderThread::RenderThread (NativeActivity& activity)
73 : m_activity (activity)
74 , m_msgQueue (MESSAGE_QUEUE_SIZE)
75 , m_threadRunning (false)
76 , m_inputQueue (DE_NULL)
77 , m_windowState (WINDOWSTATE_NOT_CREATED)
84 RenderThread::~RenderThread (void)
88 void RenderThread::start (void)
90 m_threadRunning = true;
94 void RenderThread::stop (void)
96 // Queue finish command
97 enqueue(Message(MESSAGE_FINISH));
99 // Wait for thread to terminate
102 m_threadRunning = false;
105 void RenderThread::enqueue (const Message& message)
107 // \note Thread must be running or otherwise nobody is going to drain the queue.
108 DE_ASSERT(m_threadRunning);
109 m_msgQueue.pushFront(message);
112 void RenderThread::pause (void)
114 enqueue(Message(MESSAGE_PAUSE));
117 void RenderThread::resume (void)
119 enqueue(Message(MESSAGE_RESUME));
122 void RenderThread::sync (void)
124 de::Semaphore waitSem(0);
125 enqueue(Message(MESSAGE_SYNC, &waitSem));
129 void RenderThread::processMessage (const Message& message)
131 DBG_PRINT(("RenderThread::processMessage(): message = { %s, %p }\n", getMessageTypeName(message.type), message.payload.window));
133 switch (message.type)
135 case MESSAGE_RESUME: m_paused = false; break;
136 case MESSAGE_PAUSE: m_paused = true; break;
137 case MESSAGE_FINISH: m_finish = true; break;
139 // \note While Platform / WindowRegistry are currently multi-window -capable,
140 // the fact that platform gives us windows too late / at unexpected times
141 // forces us to do some sanity checking and limit system to one window here.
142 case MESSAGE_WINDOW_CREATED:
143 if (m_windowState != WINDOWSTATE_NOT_CREATED && m_windowState != WINDOWSTATE_DESTROYED)
144 throw InternalError("Got unexpected onNativeWindowCreated() event from system");
146 m_windowState = WINDOWSTATE_NOT_INITIALIZED;
147 m_window = message.payload.window;
150 case MESSAGE_WINDOW_RESIZED:
151 if (m_window != message.payload.window)
152 throw InternalError("Got onNativeWindowResized() event targeting different window");
154 if (m_windowState == WINDOWSTATE_NOT_INITIALIZED)
156 // Got first resize event, window is ready for use.
157 m_windowState = WINDOWSTATE_READY;
158 onWindowCreated(message.payload.window);
160 else if (m_windowState == WINDOWSTATE_READY)
161 onWindowResized(message.payload.window);
163 throw InternalError("Got unexpected onNativeWindowResized() event from system");
167 case MESSAGE_WINDOW_DESTROYED:
168 if (m_window != message.payload.window)
169 throw InternalError("Got onNativeWindowDestroyed() event targeting different window");
171 if (m_windowState != WINDOWSTATE_NOT_INITIALIZED && m_windowState != WINDOWSTATE_READY)
172 throw InternalError("Got unexpected onNativeWindowDestroyed() event from system");
174 if (m_windowState == WINDOWSTATE_READY)
175 onWindowDestroyed(message.payload.window);
177 m_windowState = WINDOWSTATE_DESTROYED;
181 case MESSAGE_INPUT_QUEUE_CREATED:
182 m_inputQueue = message.payload.inputQueue;
185 case MESSAGE_INPUT_QUEUE_DESTROYED:
186 m_inputQueue = message.payload.inputQueue;
190 message.payload.semaphore->increment();
194 throw std::runtime_error("Unknown message type");
199 void RenderThread::run (void)
202 m_windowState = WINDOWSTATE_NOT_CREATED;
210 if (m_paused || m_windowState != WINDOWSTATE_READY)
212 // Block until we are not paused and window is ready.
213 Message msg = m_msgQueue.popBack();
218 // Process available commands
221 if (m_msgQueue.tryPopBack(msg))
228 DE_ASSERT(m_windowState == WINDOWSTATE_READY);
230 // Process input events.
231 // \todo [2013-05-08 pyry] What if system fills up the input queue before we have window ready?
232 while (m_inputQueue &&
233 AInputQueue_hasEvents(m_inputQueue) > 0)
236 TCU_CHECK(AInputQueue_getEvent(m_inputQueue, &event) >= 0);
238 AInputQueue_finishEvent(m_inputQueue, event, 1);
241 // Everything set up - safe to render.
244 DBG_PRINT(("RenderThread::run(): render\n"));
249 catch (const std::exception& e)
251 print("RenderThread: %s\n", e.what());
254 // Tell activity to finish.
255 DBG_PRINT(("RenderThread::run(): done, waiting for FINISH\n"));
258 // Thread must keep draining message queue until FINISH message is encountered.
263 Message msg = m_msgQueue.popBack();
265 // Ignore all but SYNC and FINISH messages.
266 if (msg.type == MESSAGE_SYNC || msg.type == MESSAGE_FINISH)
270 catch (const std::exception& e)
272 die("RenderThread: %s\n", e.what());
275 DBG_PRINT(("RenderThread::run(): exiting...\n"));
280 RenderActivity::RenderActivity (ANativeActivity* activity)
281 : NativeActivity(activity)
284 DBG_PRINT(("RenderActivity::RenderActivity()"));
287 RenderActivity::~RenderActivity (void)
289 DBG_PRINT(("RenderActivity::~RenderActivity()"));
292 void RenderActivity::setThread (RenderThread* thread)
294 DE_ASSERT(!m_thread && thread);
298 void RenderActivity::onStart (void)
300 DBG_PRINT(("RenderActivity::onStart()"));
303 void RenderActivity::onResume (void)
305 DBG_PRINT(("RenderActivity::onResume()"));
307 // Resume (or start) test execution
311 void RenderActivity::onPause (void)
313 DBG_PRINT(("RenderActivity::onPause()"));
315 // Pause test execution
319 void RenderActivity::onStop (void)
321 DBG_PRINT(("RenderActivity::onStop()"));
324 void RenderActivity::onDestroy (void)
326 DBG_PRINT(("RenderActivity::onDestroy()"));
329 void RenderActivity::onNativeWindowCreated (ANativeWindow* window)
331 DBG_PRINT(("RenderActivity::onNativeWindowCreated()"));
332 m_thread->enqueue(Message(MESSAGE_WINDOW_CREATED, window));
335 void RenderActivity::onNativeWindowResized (ANativeWindow* window)
337 DBG_PRINT(("RenderActivity::onNativeWindowResized()"));
338 m_thread->enqueue(Message(MESSAGE_WINDOW_RESIZED, window));
341 void RenderActivity::onNativeWindowRedrawNeeded (ANativeWindow* window)
346 void RenderActivity::onNativeWindowDestroyed (ANativeWindow* window)
348 DBG_PRINT(("RenderActivity::onNativeWindowDestroyed()"));
349 m_thread->enqueue(Message(MESSAGE_WINDOW_DESTROYED, window));
350 m_thread->sync(); // Block until thread has processed all messages.
353 void RenderActivity::onInputQueueCreated (AInputQueue* queue)
355 DBG_PRINT(("RenderActivity::onInputQueueCreated()"));
356 m_thread->enqueue(Message(MESSAGE_INPUT_QUEUE_CREATED, queue));
359 void RenderActivity::onInputQueueDestroyed (AInputQueue* queue)
361 DBG_PRINT(("RenderActivity::onInputQueueDestroyed()"));
362 m_thread->enqueue(Message(MESSAGE_INPUT_QUEUE_DESTROYED, queue));