Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Sean Paul | 5d8acfc | 2016-04-21 16:26:27 -0400 | [diff] [blame] | 17 | #ifndef ANDROID_DRM_PLATFORM_H_ |
| 18 | #define ANDROID_DRM_PLATFORM_H_ |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 19 | |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 20 | #include <hardware/hardware.h> |
| 21 | #include <hardware/hwcomposer.h> |
| 22 | |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 23 | #include <map> |
Roman Stratiienko | d21071f | 2021-03-09 21:56:50 +0200 | [diff] [blame] | 24 | #include <memory> |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 25 | #include <vector> |
| 26 | |
Roman Stratiienko | 13cc366 | 2020-08-29 21:35:39 +0300 | [diff] [blame] | 27 | #include "compositor/DrmDisplayComposition.h" |
Roman Stratiienko | aa3cd54 | 2020-08-29 11:26:16 +0300 | [diff] [blame] | 28 | #include "drmhwcomposer.h" |
| 29 | |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 30 | namespace android { |
| 31 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 32 | class DrmDevice; |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 33 | |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 34 | class Planner { |
| 35 | public: |
| 36 | class PlanStage { |
| 37 | public: |
| 38 | virtual ~PlanStage() { |
| 39 | } |
| 40 | |
| 41 | virtual int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition, |
| 42 | std::map<size_t, DrmHwcLayer *> &layers, |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 43 | std::vector<DrmPlane *> *planes) = 0; |
| 44 | |
| 45 | protected: |
| 46 | // Removes and returns the next available plane from planes |
| 47 | static DrmPlane *PopPlane(std::vector<DrmPlane *> *planes) { |
| 48 | if (planes->empty()) |
| 49 | return NULL; |
| 50 | DrmPlane *plane = planes->front(); |
| 51 | planes->erase(planes->begin()); |
| 52 | return plane; |
| 53 | } |
| 54 | |
Rob Herring | af0d975 | 2018-05-04 16:34:19 -0500 | [diff] [blame] | 55 | // Inserts the given layer:plane in the composition at the back |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 56 | static int Emplace(std::vector<DrmCompositionPlane> *composition, |
| 57 | std::vector<DrmPlane *> *planes, |
Matvii Zorin | f0757c2 | 2021-01-18 15:54:22 +0200 | [diff] [blame] | 58 | DrmCompositionPlane::Type type, |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame] | 59 | std::pair<size_t, DrmHwcLayer *> layer) { |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 60 | DrmPlane *plane = PopPlane(planes); |
Alexandru Gheorghe | ea1c5e5 | 2018-09-17 10:48:54 +0100 | [diff] [blame] | 61 | std::vector<DrmPlane *> unused_planes; |
| 62 | int ret = -ENOENT; |
| 63 | while (plane) { |
Matvii Zorin | 67a89d3 | 2021-01-31 14:45:05 +0200 | [diff] [blame^] | 64 | ret = plane->IsValidForLayer(layer.second) ? 0 : -EINVAL; |
Alexandru Gheorghe | ea1c5e5 | 2018-09-17 10:48:54 +0100 | [diff] [blame] | 65 | if (!ret) |
| 66 | break; |
Sean Paul | 2689aeb | 2019-03-13 14:36:52 -0400 | [diff] [blame] | 67 | if (!plane->zpos_property().is_immutable()) |
Alexandru Gheorghe | ea1c5e5 | 2018-09-17 10:48:54 +0100 | [diff] [blame] | 68 | unused_planes.push_back(plane); |
| 69 | plane = PopPlane(planes); |
| 70 | } |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 71 | |
Alexandru Gheorghe | ea1c5e5 | 2018-09-17 10:48:54 +0100 | [diff] [blame] | 72 | if (!ret) { |
Matvii Zorin | f0757c2 | 2021-01-18 15:54:22 +0200 | [diff] [blame] | 73 | composition->emplace_back(type, plane, layer.first); |
Alexandru Gheorghe | ea1c5e5 | 2018-09-17 10:48:54 +0100 | [diff] [blame] | 74 | planes->insert(planes->begin(), unused_planes.begin(), |
| 75 | unused_planes.end()); |
| 76 | } |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame] | 77 | |
Alexandru Gheorghe | ea1c5e5 | 2018-09-17 10:48:54 +0100 | [diff] [blame] | 78 | return ret; |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 79 | } |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | // Creates a planner instance with platform-specific planning stages |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 83 | static std::unique_ptr<Planner> CreateInstance(DrmDevice *drm); |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 84 | |
| 85 | // Takes a stack of layers and provisions hardware planes for them. If the |
Rob Herring | af0d975 | 2018-05-04 16:34:19 -0500 | [diff] [blame] | 86 | // entire stack can't fit in hardware, FIXME |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 87 | // |
| 88 | // @layers: a map of index:layer of layers to composite |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 89 | // @primary_planes: a vector of primary planes available for this frame |
| 90 | // @overlay_planes: a vector of overlay planes available for this frame |
| 91 | // |
| 92 | // Returns: A tuple with the status of the operation (0 for success) and |
| 93 | // a vector of the resulting plan (ie: layer->plane mapping). |
| 94 | std::tuple<int, std::vector<DrmCompositionPlane>> ProvisionPlanes( |
Rob Herring | af0d975 | 2018-05-04 16:34:19 -0500 | [diff] [blame] | 95 | std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc, |
| 96 | std::vector<DrmPlane *> *primary_planes, |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 97 | std::vector<DrmPlane *> *overlay_planes); |
| 98 | |
| 99 | template <typename T, typename... A> |
Roman Stratiienko | d21071f | 2021-03-09 21:56:50 +0200 | [diff] [blame] | 100 | void AddStage(A &&...args) { |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 101 | stages_.emplace_back( |
| 102 | std::unique_ptr<PlanStage>(new T(std::forward(args)...))); |
| 103 | } |
| 104 | |
| 105 | private: |
| 106 | std::vector<DrmPlane *> GetUsablePlanes( |
| 107 | DrmCrtc *crtc, std::vector<DrmPlane *> *primary_planes, |
| 108 | std::vector<DrmPlane *> *overlay_planes); |
| 109 | |
| 110 | std::vector<std::unique_ptr<PlanStage>> stages_; |
| 111 | }; |
| 112 | |
| 113 | // This plan stage extracts all protected layers and places them on dedicated |
| 114 | // planes. |
| 115 | class PlanStageProtected : public Planner::PlanStage { |
| 116 | public: |
| 117 | int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition, |
Matvii Zorin | f0757c2 | 2021-01-18 15:54:22 +0200 | [diff] [blame] | 118 | std::map<size_t, DrmHwcLayer *> &layers, |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 119 | std::vector<DrmPlane *> *planes); |
| 120 | }; |
| 121 | |
| 122 | // This plan stage places as many layers on dedicated planes as possible (first |
| 123 | // come first serve), and then sticks the rest in a precomposition plane (if |
| 124 | // needed). |
| 125 | class PlanStageGreedy : public Planner::PlanStage { |
| 126 | public: |
| 127 | int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition, |
Matvii Zorin | f0757c2 | 2021-01-18 15:54:22 +0200 | [diff] [blame] | 128 | std::map<size_t, DrmHwcLayer *> &layers, |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 129 | std::vector<DrmPlane *> *planes); |
| 130 | }; |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 131 | } // namespace android |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 132 | #endif |