RenderThread.cpp revision cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fc
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#define LOG_TAG "RenderThread" 18 19#include "RenderThread.h" 20 21#include <utils/Log.h> 22 23namespace android { 24using namespace uirenderer::renderthread; 25ANDROID_SINGLETON_STATIC_INSTANCE(RenderThread); 26 27namespace uirenderer { 28namespace renderthread { 29 30RenderThread::RenderThread() : Thread(true), Singleton<RenderThread>() 31 , mQueueHead(0), mQueueTail(0) { 32 mLooper = new Looper(false); 33 run("RenderThread"); 34} 35 36RenderThread::~RenderThread() { 37} 38 39bool RenderThread::threadLoop() { 40 for (;;) { 41 int result = mLooper->pollAll(-1); 42 if (result == ALOOPER_POLL_ERROR) { 43 // TODO Something? 44 break; 45 } 46 // Process our queue, if we have anything 47 while (RenderTask* task = nextTask()) { 48 task->run(); 49 delete task; 50 } 51 } 52 53 return false; 54} 55 56void RenderThread::queue(RenderTask* task) { 57 AutoMutex _lock(mLock); 58 if (mQueueTail) { 59 mQueueTail->mNext = task; 60 } else { 61 mQueueHead = task; 62 } 63 mQueueTail = task; 64 if (mQueueHead == task) { 65 // Only wake if this is the first task 66 mLooper->wake(); 67 } 68} 69 70RenderTask* RenderThread::nextTask() { 71 AutoMutex _lock(mLock); 72 RenderTask* ret = mQueueHead; 73 if (ret) { 74 if (mQueueTail == mQueueHead) { 75 mQueueTail = mQueueHead = 0; 76 } else { 77 mQueueHead = ret->mNext; 78 } 79 ret->mNext = 0; 80 } 81 return ret; 82} 83 84} /* namespace renderthread */ 85} /* namespace uirenderer */ 86} /* namespace android */ 87