Home | History | Annotate | Download | only in renderthread
      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 #ifndef RENDERTHREAD_H_
     18 #define RENDERTHREAD_H_
     19 
     20 #include "RenderTask.h"
     21 
     22 #include "../JankTracker.h"
     23 #include "TimeLord.h"
     24 
     25 #include <GrContext.h>
     26 #include <cutils/compiler.h>
     27 #include <ui/DisplayInfo.h>
     28 #include <utils/Looper.h>
     29 #include <utils/Thread.h>
     30 
     31 #include <memory>
     32 #include <set>
     33 
     34 namespace android {
     35 
     36 class DisplayEventReceiver;
     37 
     38 namespace uirenderer {
     39 
     40 class Readback;
     41 class RenderState;
     42 class TestUtils;
     43 
     44 namespace renderthread {
     45 
     46 class CanvasContext;
     47 class DispatchFrameCallbacks;
     48 class EglManager;
     49 class RenderProxy;
     50 class VulkanManager;
     51 
     52 class TaskQueue {
     53 public:
     54     TaskQueue();
     55 
     56     RenderTask* next();
     57     void queue(RenderTask* task);
     58     void queueAtFront(RenderTask* task);
     59     RenderTask* peek();
     60     void remove(RenderTask* task);
     61 
     62 private:
     63     RenderTask* mHead;
     64     RenderTask* mTail;
     65 };
     66 
     67 // Mimics android.view.Choreographer.FrameCallback
     68 class IFrameCallback {
     69 public:
     70     virtual void doFrame() = 0;
     71 
     72 protected:
     73     ~IFrameCallback() {}
     74 };
     75 
     76 class ANDROID_API RenderThread : public Thread {
     77     PREVENT_COPY_AND_ASSIGN(RenderThread);
     78 public:
     79     // RenderThread takes complete ownership of tasks that are queued
     80     // and will delete them after they are run
     81     ANDROID_API void queue(RenderTask* task);
     82     ANDROID_API void queueAndWait(RenderTask* task);
     83     ANDROID_API void queueAtFront(RenderTask* task);
     84     void queueAt(RenderTask* task, nsecs_t runAtNs);
     85     void remove(RenderTask* task);
     86 
     87     // Mimics android.view.Choreographer
     88     void postFrameCallback(IFrameCallback* callback);
     89     bool removeFrameCallback(IFrameCallback* callback);
     90     // If the callback is currently registered, it will be pushed back until
     91     // the next vsync. If it is not currently registered this does nothing.
     92     void pushBackFrameCallback(IFrameCallback* callback);
     93 
     94     TimeLord& timeLord() { return mTimeLord; }
     95     RenderState& renderState() const { return *mRenderState; }
     96     EglManager& eglManager() const { return *mEglManager; }
     97     JankTracker& jankTracker() { return *mJankTracker; }
     98     Readback& readback();
     99 
    100     const DisplayInfo& mainDisplayInfo() { return mDisplayInfo; }
    101 
    102     GrContext* getGrContext() const { return mGrContext.get(); }
    103     void setGrContext(GrContext* cxt) { mGrContext.reset(cxt); }
    104 
    105     VulkanManager& vulkanManager() { return *mVkManager; }
    106 
    107 protected:
    108     virtual bool threadLoop() override;
    109 
    110 private:
    111     friend class DispatchFrameCallbacks;
    112     friend class RenderProxy;
    113     friend class android::uirenderer::TestUtils;
    114 
    115     RenderThread();
    116     virtual ~RenderThread();
    117 
    118     static bool hasInstance();
    119     static RenderThread& getInstance();
    120 
    121     void initThreadLocals();
    122     void initializeDisplayEventReceiver();
    123     static int displayEventReceiverCallback(int fd, int events, void* data);
    124     void drainDisplayEventQueue();
    125     void dispatchFrameCallbacks();
    126     void requestVsync();
    127 
    128     // Returns the next task to be run. If this returns NULL nextWakeup is set
    129     // to the time to requery for the nextTask to run. mNextWakeup is also
    130     // set to this time
    131     RenderTask* nextTask(nsecs_t* nextWakeup);
    132 
    133     sp<Looper> mLooper;
    134     Mutex mLock;
    135 
    136     nsecs_t mNextWakeup;
    137     TaskQueue mQueue;
    138 
    139     DisplayInfo mDisplayInfo;
    140 
    141     DisplayEventReceiver* mDisplayEventReceiver;
    142     bool mVsyncRequested;
    143     std::set<IFrameCallback*> mFrameCallbacks;
    144     // We defer the actual registration of these callbacks until
    145     // both mQueue *and* mDisplayEventReceiver have been drained off all
    146     // immediate events. This makes sure that we catch the next vsync, not
    147     // the previous one
    148     std::set<IFrameCallback*> mPendingRegistrationFrameCallbacks;
    149     bool mFrameCallbackTaskPending;
    150     DispatchFrameCallbacks* mFrameCallbackTask;
    151 
    152     TimeLord mTimeLord;
    153     RenderState* mRenderState;
    154     EglManager* mEglManager;
    155 
    156     JankTracker* mJankTracker = nullptr;
    157     Readback* mReadback = nullptr;
    158 
    159     sk_sp<GrContext> mGrContext;
    160     VulkanManager* mVkManager;
    161 };
    162 
    163 } /* namespace renderthread */
    164 } /* namespace uirenderer */
    165 } /* namespace android */
    166 #endif /* RENDERTHREAD_H_ */
    167