RenderThread.h revision d41c4d8c732095ae99c955b6b82f7306633004b1
1cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck/* 2cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck * Copyright (C) 2013 The Android Open Source Project 3cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck * 4cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck * Licensed under the Apache License, Version 2.0 (the "License"); 5cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck * you may not use this file except in compliance with the License. 6cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck * You may obtain a copy of the License at 7cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck * 8cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck * http://www.apache.org/licenses/LICENSE-2.0 9cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck * 10cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck * Unless required by applicable law or agreed to in writing, software 11cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck * distributed under the License is distributed on an "AS IS" BASIS, 12cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck * See the License for the specific language governing permissions and 14cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck * limitations under the License. 15cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck */ 16cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 17cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck#ifndef RENDERTHREAD_H_ 18cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck#define RENDERTHREAD_H_ 19cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 20cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck#include "RenderTask.h" 21e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck 22e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck#include <memory> 23e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck#include <set> 24e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck 25cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck#include <cutils/compiler.h> 26cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck#include <utils/Looper.h> 27cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck#include <utils/Mutex.h> 28cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck#include <utils/Singleton.h> 29cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck#include <utils/Thread.h> 30cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 3118f16e6fba74eda173e1e7c869e6e2e2acc073ffJohn Reck#include "TimeLord.h" 3218f16e6fba74eda173e1e7c869e6e2e2acc073ffJohn Reck 33cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Recknamespace android { 343b20251a355c88193c439f928a84ae69483fb488John Reck 35e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reckclass DisplayEventReceiver; 36e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck 37cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Recknamespace uirenderer { 383b20251a355c88193c439f928a84ae69483fb488John Reck 393b20251a355c88193c439f928a84ae69483fb488John Reckclass RenderState; 403b20251a355c88193c439f928a84ae69483fb488John Reck 41cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Recknamespace renderthread { 42cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 43443a714fa7c0dd07fee3527cc5bc3d3ca1fb7d44John Reckclass CanvasContext; 44e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reckclass DispatchFrameCallbacks; 453b20251a355c88193c439f928a84ae69483fb488John Reckclass EglManager; 463b20251a355c88193c439f928a84ae69483fb488John Reckclass RenderProxy; 47e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck 484f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reckclass TaskQueue { 494f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reckpublic: 504f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck TaskQueue(); 514f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck 524f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck RenderTask* next(); 534f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck void queue(RenderTask* task); 54a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck void queueAtFront(RenderTask* task); 554f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck RenderTask* peek(); 564f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck void remove(RenderTask* task); 574f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck 584f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reckprivate: 594f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck RenderTask* mHead; 604f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck RenderTask* mTail; 614f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck}; 624f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck 63e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck// Mimics android.view.Choreographer.FrameCallback 64e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reckclass IFrameCallback { 65e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reckpublic: 6618f16e6fba74eda173e1e7c869e6e2e2acc073ffJohn Reck virtual void doFrame() = 0; 67e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck 68e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reckprotected: 69e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck ~IFrameCallback() {} 70e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck}; 71e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck 723b20251a355c88193c439f928a84ae69483fb488John Reckclass ANDROID_API RenderThread : public Thread, protected Singleton<RenderThread> { 73cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reckpublic: 74cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck // RenderThread takes complete ownership of tasks that are queued 75cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck // and will delete them after they are run 76cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck ANDROID_API void queue(RenderTask* task); 77a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck ANDROID_API void queueAtFront(RenderTask* task); 78a733f89c05567c97359169832f41389b939baaadJohn Reck void queueAt(RenderTask* task, nsecs_t runAtNs); 794f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck void remove(RenderTask* task); 80cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 81e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck // Mimics android.view.Choreographer 82e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck void postFrameCallback(IFrameCallback* callback); 8301a5ea35fbba4c5bb1d7790ae1677a2fa752e042John Reck bool removeFrameCallback(IFrameCallback* callback); 84a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck // If the callback is currently registered, it will be pushed back until 85a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck // the next vsync. If it is not currently registered this does nothing. 86a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck void pushBackFrameCallback(IFrameCallback* callback); 87e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck 8818f16e6fba74eda173e1e7c869e6e2e2acc073ffJohn Reck TimeLord& timeLord() { return mTimeLord; } 893b20251a355c88193c439f928a84ae69483fb488John Reck RenderState& renderState() { return *mRenderState; } 903b20251a355c88193c439f928a84ae69483fb488John Reck EglManager& eglManager() { return *mEglManager; } 9118f16e6fba74eda173e1e7c869e6e2e2acc073ffJohn Reck 92cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reckprotected: 93d41c4d8c732095ae99c955b6b82f7306633004b1Chris Craik virtual bool threadLoop() override; 94cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 95cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reckprivate: 96cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck friend class Singleton<RenderThread>; 97e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck friend class DispatchFrameCallbacks; 983b20251a355c88193c439f928a84ae69483fb488John Reck friend class RenderProxy; 99cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 100cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck RenderThread(); 101cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck virtual ~RenderThread(); 102cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 1033b20251a355c88193c439f928a84ae69483fb488John Reck void initThreadLocals(); 104e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck void initializeDisplayEventReceiver(); 105e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck static int displayEventReceiverCallback(int fd, int events, void* data); 106a733f89c05567c97359169832f41389b939baaadJohn Reck void drainDisplayEventQueue(); 107e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck void dispatchFrameCallbacks(); 108a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck void requestVsync(); 109e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck 1104f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck // Returns the next task to be run. If this returns NULL nextWakeup is set 1114f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck // to the time to requery for the nextTask to run. mNextWakeup is also 1124f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck // set to this time 1134f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck RenderTask* nextTask(nsecs_t* nextWakeup); 114cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 115cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck sp<Looper> mLooper; 116cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck Mutex mLock; 117cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 1184f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck nsecs_t mNextWakeup; 1194f02bf4eef6af47f35c70c4dda5b7b9523d89ca0John Reck TaskQueue mQueue; 120e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck 121e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck DisplayEventReceiver* mDisplayEventReceiver; 122e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck bool mVsyncRequested; 123e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck std::set<IFrameCallback*> mFrameCallbacks; 124a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck // We defer the actual registration of these callbacks until 125a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck // both mQueue *and* mDisplayEventReceiver have been drained off all 126a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck // immediate events. This makes sure that we catch the next vsync, not 127a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck // the previous one 128a5dda645da738da7b4ae15e28fa7d93d3b04b94fJohn Reck std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks; 129e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck bool mFrameCallbackTaskPending; 130e45b1fd03b524d2b57cc6c222d89076a31a08beaJohn Reck DispatchFrameCallbacks* mFrameCallbackTask; 13118f16e6fba74eda173e1e7c869e6e2e2acc073ffJohn Reck 13218f16e6fba74eda173e1e7c869e6e2e2acc073ffJohn Reck TimeLord mTimeLord; 1333b20251a355c88193c439f928a84ae69483fb488John Reck RenderState* mRenderState; 1343b20251a355c88193c439f928a84ae69483fb488John Reck EglManager* mEglManager; 135cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck}; 136cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck 137cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck} /* namespace renderthread */ 138cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck} /* namespace uirenderer */ 139cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck} /* namespace android */ 140cec24ae16e9a0a7c3075f1a8d9149bb7fb3813fcJohn Reck#endif /* RENDERTHREAD_H_ */ 141