Home | History | Annotate | Download | only in Scheduler
      1 /*
      2  * Copyright 2018 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 #pragma once
     18 
     19 #include <array>
     20 #include <cinttypes>
     21 #include <cstdint>
     22 #include <numeric>
     23 #include <string>
     24 #include <unordered_map>
     25 
     26 #include <utils/Timers.h>
     27 
     28 #include "LayerInfo.h"
     29 #include "SchedulerUtils.h"
     30 
     31 namespace android {
     32 namespace scheduler {
     33 
     34 /*
     35  * This class represents information about layers that are considered current. We keep an
     36  * unordered map between layer name and LayerInfo.
     37  */
     38 class LayerHistory {
     39 public:
     40     // Handle for each layer we keep track of.
     41     class LayerHandle {
     42     public:
     43         LayerHandle(LayerHistory& lh, int64_t id) : mId(id), mLayerHistory(lh) {}
     44         ~LayerHandle() { mLayerHistory.destroyLayer(mId); }
     45 
     46         const int64_t mId;
     47 
     48     private:
     49         LayerHistory& mLayerHistory;
     50     };
     51 
     52     LayerHistory();
     53     ~LayerHistory();
     54 
     55     // When the layer is first created, register it.
     56     std::unique_ptr<LayerHandle> createLayer(const std::string name, float maxRefreshRate);
     57 
     58     // Method for inserting layers and their requested present time into the unordered map.
     59     void insert(const std::unique_ptr<LayerHandle>& layerHandle, nsecs_t presentTime, bool isHdr);
     60     // Method for setting layer visibility
     61     void setVisibility(const std::unique_ptr<LayerHandle>& layerHandle, bool visible);
     62 
     63     // Returns the desired refresh rate, which is a max refresh rate of all the current
     64     // layers. See go/content-fps-detection-in-scheduler for more information.
     65     std::pair<float, bool> getDesiredRefreshRateAndHDR();
     66 
     67     // Removes the handle and the object from the map.
     68     void destroyLayer(const int64_t id);
     69 
     70 private:
     71     // Removes the layers that have been idle for a given amount of time from mLayerInfos.
     72     void removeIrrelevantLayers() REQUIRES(mLock);
     73 
     74     // Information about currently active layers.
     75     std::mutex mLock;
     76     std::unordered_map<int64_t, std::shared_ptr<LayerInfo>> mActiveLayerInfos GUARDED_BY(mLock);
     77     std::unordered_map<int64_t, std::shared_ptr<LayerInfo>> mInactiveLayerInfos GUARDED_BY(mLock);
     78 
     79     // Each layer has it's own ID. This variable keeps track of the count.
     80     static std::atomic<int64_t> sNextId;
     81 
     82     // Flag whether to log layer FPS in systrace
     83     bool mTraceEnabled = false;
     84 };
     85 
     86 } // namespace scheduler
     87 } // namespace android