| Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 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 |  | 
| Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 28 | #include "LayerInfo.h" | 
| Ana Krulec | 434c22d | 2018-11-28 13:48:36 +0100 | [diff] [blame] | 29 | #include "SchedulerUtils.h" | 
|  | 30 |  | 
| Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 31 | namespace android { | 
| Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 32 | namespace scheduler { | 
| Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 33 |  | 
|  | 34 | /* | 
| Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 35 | * This class represents information about layers that are considered current. We keep an | 
|  | 36 | * unordered map between layer name and LayerInfo. | 
| Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 37 | */ | 
|  | 38 | class LayerHistory { | 
|  | 39 | public: | 
| Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 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 |  | 
| Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 52 | LayerHistory(); | 
|  | 53 | ~LayerHistory(); | 
|  | 54 |  | 
| Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 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); | 
|  | 60 | // Returns the desired refresh rate, which is a max refresh rate of all the current | 
|  | 61 | // layers. See go/content-fps-detection-in-scheduler for more information. | 
|  | 62 | float getDesiredRefreshRate(); | 
|  | 63 |  | 
|  | 64 | // Removes the handle and the object from the map. | 
|  | 65 | void destroyLayer(const int64_t id); | 
| Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 66 |  | 
|  | 67 | private: | 
| Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 68 | // Removes the layers that have been idle for a given amount of time from mLayerInfos. | 
|  | 69 | void removeIrrelevantLayers() REQUIRES(mLock); | 
|  | 70 |  | 
|  | 71 | // Information about currently active layers. | 
|  | 72 | std::mutex mLock; | 
|  | 73 | std::unordered_map<int64_t, std::shared_ptr<LayerInfo>> mActiveLayerInfos GUARDED_BY(mLock); | 
|  | 74 | std::unordered_map<int64_t, std::shared_ptr<LayerInfo>> mInactiveLayerInfos GUARDED_BY(mLock); | 
|  | 75 |  | 
|  | 76 | // Each layer has it's own ID. This variable keeps track of the count. | 
|  | 77 | static std::atomic<int64_t> sNextId; | 
|  | 78 |  | 
|  | 79 | // Flag whether to log layer FPS in systrace | 
|  | 80 | bool mTraceEnabled = false; | 
| Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 81 | }; | 
|  | 82 |  | 
| Ady Abraham | 09bd392 | 2019-04-08 10:44:56 -0700 | [diff] [blame] | 83 | } // namespace scheduler | 
| Ana Krulec | 61f86db | 2018-11-19 14:16:35 +0100 | [diff] [blame] | 84 | } // namespace android |