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> |
| 24 | #include <vector> |
| 25 | |
Roman Stratiienko | aa3cd54 | 2020-08-29 11:26:16 +0300 | [diff] [blame^] | 26 | #include "compositor/drmdisplaycomposition.h" |
| 27 | #include "drmhwcomposer.h" |
| 28 | |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 29 | namespace android { |
| 30 | |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 31 | class DrmDevice; |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 32 | |
| 33 | class Importer { |
| 34 | public: |
| 35 | virtual ~Importer() { |
| 36 | } |
| 37 | |
| 38 | // Creates a platform-specific importer instance |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 39 | static Importer *CreateInstance(DrmDevice *drm); |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 40 | |
| 41 | // Imports the buffer referred to by handle into bo. |
| 42 | // |
| 43 | // Note: This can be called from a different thread than ReleaseBuffer. The |
| 44 | // implementation is responsible for ensuring thread safety. |
| 45 | virtual int ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) = 0; |
| 46 | |
| 47 | // Releases the buffer object (ie: does the inverse of ImportBuffer) |
| 48 | // |
| 49 | // Note: This can be called from a different thread than ImportBuffer. The |
| 50 | // implementation is responsible for ensuring thread safety. |
| 51 | virtual int ReleaseBuffer(hwc_drm_bo_t *bo) = 0; |
Alexey Firago | 18ec688 | 2018-11-21 23:47:05 +0300 | [diff] [blame] | 52 | |
| 53 | // Checks if importer can import the buffer. |
| 54 | virtual bool CanImportBuffer(buffer_handle_t handle) = 0; |
Roman Stratiienko | 4163efc | 2019-12-06 12:30:28 +0200 | [diff] [blame] | 55 | |
| 56 | // Convert platform-dependent buffer format to drm_hwc internal format. |
| 57 | virtual int ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) = 0; |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 58 | }; |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 59 | |
| 60 | class Planner { |
| 61 | public: |
| 62 | class PlanStage { |
| 63 | public: |
| 64 | virtual ~PlanStage() { |
| 65 | } |
| 66 | |
| 67 | virtual int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition, |
| 68 | std::map<size_t, DrmHwcLayer *> &layers, |
| 69 | DrmCrtc *crtc, |
| 70 | std::vector<DrmPlane *> *planes) = 0; |
| 71 | |
| 72 | protected: |
| 73 | // Removes and returns the next available plane from planes |
| 74 | static DrmPlane *PopPlane(std::vector<DrmPlane *> *planes) { |
| 75 | if (planes->empty()) |
| 76 | return NULL; |
| 77 | DrmPlane *plane = planes->front(); |
| 78 | planes->erase(planes->begin()); |
| 79 | return plane; |
| 80 | } |
| 81 | |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame] | 82 | static int ValidatePlane(DrmPlane *plane, DrmHwcLayer *layer); |
| 83 | |
Rob Herring | af0d975 | 2018-05-04 16:34:19 -0500 | [diff] [blame] | 84 | // Inserts the given layer:plane in the composition at the back |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 85 | static int Emplace(std::vector<DrmCompositionPlane> *composition, |
| 86 | std::vector<DrmPlane *> *planes, |
| 87 | DrmCompositionPlane::Type type, DrmCrtc *crtc, |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame] | 88 | std::pair<size_t, DrmHwcLayer *> layer) { |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 89 | DrmPlane *plane = PopPlane(planes); |
Alexandru Gheorghe | ea1c5e5 | 2018-09-17 10:48:54 +0100 | [diff] [blame] | 90 | std::vector<DrmPlane *> unused_planes; |
| 91 | int ret = -ENOENT; |
| 92 | while (plane) { |
| 93 | ret = ValidatePlane(plane, layer.second); |
| 94 | if (!ret) |
| 95 | break; |
Sean Paul | 2689aeb | 2019-03-13 14:36:52 -0400 | [diff] [blame] | 96 | if (!plane->zpos_property().is_immutable()) |
Alexandru Gheorghe | ea1c5e5 | 2018-09-17 10:48:54 +0100 | [diff] [blame] | 97 | unused_planes.push_back(plane); |
| 98 | plane = PopPlane(planes); |
| 99 | } |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 100 | |
Alexandru Gheorghe | ea1c5e5 | 2018-09-17 10:48:54 +0100 | [diff] [blame] | 101 | if (!ret) { |
| 102 | composition->emplace_back(type, plane, crtc, layer.first); |
| 103 | planes->insert(planes->begin(), unused_planes.begin(), |
| 104 | unused_planes.end()); |
| 105 | } |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame] | 106 | |
Alexandru Gheorghe | ea1c5e5 | 2018-09-17 10:48:54 +0100 | [diff] [blame] | 107 | return ret; |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 108 | } |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | // Creates a planner instance with platform-specific planning stages |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 112 | static std::unique_ptr<Planner> CreateInstance(DrmDevice *drm); |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 113 | |
| 114 | // 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] | 115 | // entire stack can't fit in hardware, FIXME |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 116 | // |
| 117 | // @layers: a map of index:layer of layers to composite |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 118 | // @primary_planes: a vector of primary planes available for this frame |
| 119 | // @overlay_planes: a vector of overlay planes available for this frame |
| 120 | // |
| 121 | // Returns: A tuple with the status of the operation (0 for success) and |
| 122 | // a vector of the resulting plan (ie: layer->plane mapping). |
| 123 | std::tuple<int, std::vector<DrmCompositionPlane>> ProvisionPlanes( |
Rob Herring | af0d975 | 2018-05-04 16:34:19 -0500 | [diff] [blame] | 124 | std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc, |
| 125 | std::vector<DrmPlane *> *primary_planes, |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 126 | std::vector<DrmPlane *> *overlay_planes); |
| 127 | |
| 128 | template <typename T, typename... A> |
| 129 | void AddStage(A &&... args) { |
| 130 | stages_.emplace_back( |
| 131 | std::unique_ptr<PlanStage>(new T(std::forward(args)...))); |
| 132 | } |
| 133 | |
| 134 | private: |
| 135 | std::vector<DrmPlane *> GetUsablePlanes( |
| 136 | DrmCrtc *crtc, std::vector<DrmPlane *> *primary_planes, |
| 137 | std::vector<DrmPlane *> *overlay_planes); |
| 138 | |
| 139 | std::vector<std::unique_ptr<PlanStage>> stages_; |
| 140 | }; |
| 141 | |
| 142 | // This plan stage extracts all protected layers and places them on dedicated |
| 143 | // planes. |
| 144 | class PlanStageProtected : public Planner::PlanStage { |
| 145 | public: |
| 146 | int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition, |
| 147 | std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc, |
| 148 | std::vector<DrmPlane *> *planes); |
| 149 | }; |
| 150 | |
| 151 | // This plan stage places as many layers on dedicated planes as possible (first |
| 152 | // come first serve), and then sticks the rest in a precomposition plane (if |
| 153 | // needed). |
| 154 | class PlanStageGreedy : public Planner::PlanStage { |
| 155 | public: |
| 156 | int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition, |
| 157 | std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc, |
| 158 | std::vector<DrmPlane *> *planes); |
| 159 | }; |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 160 | } // namespace android |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 161 | #endif |