Home | History | Annotate | Download | only in surfaceflinger
      1 /*
      2  * Copyright 2016 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 ANDROID_FENCETRACKER_H
     18 #define ANDROID_FENCETRACKER_H
     19 
     20 #include <ui/Fence.h>
     21 #include <utils/Mutex.h>
     22 #include <utils/RefBase.h>
     23 #include <utils/String8.h>
     24 #include <utils/Timers.h>
     25 #include <utils/Vector.h>
     26 
     27 #include <unordered_map>
     28 
     29 namespace android {
     30 
     31 class Layer;
     32 
     33 /*
     34  * Keeps a circular buffer of fence/timestamp data for the last N frames in
     35  * SurfaceFlinger. Gets timestamps for fences after they have signaled.
     36  */
     37 class FenceTracker {
     38 public:
     39      FenceTracker();
     40      void dump(String8* outString);
     41      void addFrame(nsecs_t refreshStartTime, sp<Fence> retireFence,
     42              const Vector<sp<Layer>>& layers, sp<Fence> glDoneFence);
     43 
     44 protected:
     45      static constexpr size_t MAX_FRAME_HISTORY = 128;
     46 
     47      struct LayerRecord {
     48          String8 name; // layer name
     49          uint64_t frameNumber; // frame number for this layer
     50          bool isGlesComposition; // was GLES composition used for this layer?
     51          nsecs_t postedTime; // time when buffer was queued
     52          nsecs_t acquireTime; // timestamp from the acquire fence
     53          nsecs_t releaseTime; // timestamp from the release fence
     54          sp<Fence> acquireFence; // acquire fence
     55          sp<Fence> releaseFence; // release fence
     56 
     57          LayerRecord(const String8& name, uint64_t frameNumber,
     58                  bool isGlesComposition, nsecs_t postedTime,
     59                  nsecs_t acquireTime, nsecs_t releaseTime,
     60                  sp<Fence> acquireFence, sp<Fence> releaseFence) :
     61                  name(name), frameNumber(frameNumber),
     62                  isGlesComposition(isGlesComposition), postedTime(postedTime),
     63                  acquireTime(acquireTime), releaseTime(releaseTime),
     64                  acquireFence(acquireFence), releaseFence(releaseFence) {};
     65          LayerRecord() : name("uninitialized"), frameNumber(0),
     66                  isGlesComposition(false), postedTime(0), acquireTime(0),
     67                  releaseTime(0), acquireFence(Fence::NO_FENCE),
     68                  releaseFence(Fence::NO_FENCE) {};
     69      };
     70 
     71      struct FrameRecord {
     72          // global SurfaceFlinger frame counter
     73          uint64_t frameId;
     74          // layer data for this frame
     75          std::unordered_map<int32_t, LayerRecord> layers;
     76          // timestamp for when SurfaceFlinger::handleMessageRefresh() was called
     77          nsecs_t refreshStartTime;
     78          // timestamp from the retire fence
     79          nsecs_t retireTime;
     80          // timestamp from the GLES composition completion fence
     81          nsecs_t glesCompositionDoneTime;
     82          // primary display retire fence for this frame
     83          sp<Fence> retireFence;
     84          // if GLES composition was done, the fence for its completion
     85          sp<Fence> glesCompositionDoneFence;
     86 
     87          FrameRecord() : frameId(0), layers(), refreshStartTime(0),
     88                  retireTime(0), glesCompositionDoneTime(0),
     89                  retireFence(Fence::NO_FENCE),
     90                  glesCompositionDoneFence(Fence::NO_FENCE) {}
     91      };
     92 
     93      uint64_t mFrameCounter;
     94      uint32_t mOffset;
     95      FrameRecord mFrames[MAX_FRAME_HISTORY];
     96      Mutex mMutex;
     97 
     98      void checkFencesForCompletion();
     99 };
    100 
    101 }
    102 
    103 #endif // ANDROID_FRAMETRACKER_H
    104