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 | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 20 | #include "drmdisplaycomposition.h" |
Zach Reizner | 7642c92 | 2015-10-29 10:11:16 -0700 | [diff] [blame] | 21 | #include "drmhwcomposer.h" |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 22 | |
| 23 | #include <hardware/hardware.h> |
| 24 | #include <hardware/hwcomposer.h> |
| 25 | |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 26 | #include <map> |
| 27 | #include <vector> |
| 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; |
| 52 | }; |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 53 | |
| 54 | class Planner { |
| 55 | public: |
| 56 | class PlanStage { |
| 57 | public: |
| 58 | virtual ~PlanStage() { |
| 59 | } |
| 60 | |
| 61 | virtual int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition, |
| 62 | std::map<size_t, DrmHwcLayer *> &layers, |
| 63 | DrmCrtc *crtc, |
| 64 | std::vector<DrmPlane *> *planes) = 0; |
| 65 | |
| 66 | protected: |
| 67 | // Removes and returns the next available plane from planes |
| 68 | static DrmPlane *PopPlane(std::vector<DrmPlane *> *planes) { |
| 69 | if (planes->empty()) |
| 70 | return NULL; |
| 71 | DrmPlane *plane = planes->front(); |
| 72 | planes->erase(planes->begin()); |
| 73 | return plane; |
| 74 | } |
| 75 | |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame^] | 76 | static int ValidatePlane(DrmPlane *plane, DrmHwcLayer *layer); |
| 77 | |
Rob Herring | af0d975 | 2018-05-04 16:34:19 -0500 | [diff] [blame] | 78 | // Inserts the given layer:plane in the composition at the back |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 79 | static int Emplace(std::vector<DrmCompositionPlane> *composition, |
| 80 | std::vector<DrmPlane *> *planes, |
| 81 | DrmCompositionPlane::Type type, DrmCrtc *crtc, |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame^] | 82 | std::pair<size_t, DrmHwcLayer *> layer) { |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 83 | DrmPlane *plane = PopPlane(planes); |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame^] | 84 | int ret; |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 85 | if (!plane) |
| 86 | return -ENOENT; |
| 87 | |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame^] | 88 | ret = ValidatePlane(plane, layer.second); |
| 89 | if (ret) |
| 90 | return -EINVAL; |
| 91 | |
| 92 | composition->emplace_back(type, plane, crtc, layer.first); |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 93 | return 0; |
| 94 | } |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | // Creates a planner instance with platform-specific planning stages |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 98 | static std::unique_ptr<Planner> CreateInstance(DrmDevice *drm); |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 99 | |
| 100 | // 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] | 101 | // entire stack can't fit in hardware, FIXME |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 102 | // |
| 103 | // @layers: a map of index:layer of layers to composite |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 104 | // @primary_planes: a vector of primary planes available for this frame |
| 105 | // @overlay_planes: a vector of overlay planes available for this frame |
| 106 | // |
| 107 | // Returns: A tuple with the status of the operation (0 for success) and |
| 108 | // a vector of the resulting plan (ie: layer->plane mapping). |
| 109 | std::tuple<int, std::vector<DrmCompositionPlane>> ProvisionPlanes( |
Rob Herring | af0d975 | 2018-05-04 16:34:19 -0500 | [diff] [blame] | 110 | std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc, |
| 111 | std::vector<DrmPlane *> *primary_planes, |
Sean Paul | 4c4646e | 2016-05-10 04:19:24 -0400 | [diff] [blame] | 112 | std::vector<DrmPlane *> *overlay_planes); |
| 113 | |
| 114 | template <typename T, typename... A> |
| 115 | void AddStage(A &&... args) { |
| 116 | stages_.emplace_back( |
| 117 | std::unique_ptr<PlanStage>(new T(std::forward(args)...))); |
| 118 | } |
| 119 | |
| 120 | private: |
| 121 | std::vector<DrmPlane *> GetUsablePlanes( |
| 122 | DrmCrtc *crtc, std::vector<DrmPlane *> *primary_planes, |
| 123 | std::vector<DrmPlane *> *overlay_planes); |
| 124 | |
| 125 | std::vector<std::unique_ptr<PlanStage>> stages_; |
| 126 | }; |
| 127 | |
| 128 | // This plan stage extracts all protected layers and places them on dedicated |
| 129 | // planes. |
| 130 | class PlanStageProtected : public Planner::PlanStage { |
| 131 | public: |
| 132 | int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition, |
| 133 | std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc, |
| 134 | std::vector<DrmPlane *> *planes); |
| 135 | }; |
| 136 | |
| 137 | // This plan stage places as many layers on dedicated planes as possible (first |
| 138 | // come first serve), and then sticks the rest in a precomposition plane (if |
| 139 | // needed). |
| 140 | class PlanStageGreedy : public Planner::PlanStage { |
| 141 | public: |
| 142 | int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition, |
| 143 | std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc, |
| 144 | std::vector<DrmPlane *> *planes); |
| 145 | }; |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 146 | } // namespace android |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 147 | #endif |