blob: 7c1fe800c8f6699a6d9513d374f12e02f3591d3f [file] [log] [blame]
Sean Paulda6270d2015-06-01 14:11:52 -04001/*
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 Paul5d8acfc2016-04-21 16:26:27 -040017#ifndef ANDROID_DRM_PLATFORM_H_
18#define ANDROID_DRM_PLATFORM_H_
Sean Paulda6270d2015-06-01 14:11:52 -040019
Sean Paulda6270d2015-06-01 14:11:52 -040020#include <hardware/hardware.h>
21#include <hardware/hwcomposer.h>
22
Sean Paul4c4646e2016-05-10 04:19:24 -040023#include <map>
Roman Stratiienkod21071f2021-03-09 21:56:50 +020024#include <memory>
Sean Paul4c4646e2016-05-10 04:19:24 -040025#include <vector>
26
Roman Stratiienko13cc3662020-08-29 21:35:39 +030027#include "compositor/DrmDisplayComposition.h"
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030028#include "drmhwcomposer.h"
29
Sean Paulda6270d2015-06-01 14:11:52 -040030namespace android {
31
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010032class DrmDevice;
Sean Paulda6270d2015-06-01 14:11:52 -040033
Sean Paul4c4646e2016-05-10 04:19:24 -040034class 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 Paul4c4646e2016-05-10 04:19:24 -040043 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
Lowry Li9b6cafd2018-08-28 17:58:21 +080055 static int ValidatePlane(DrmPlane *plane, DrmHwcLayer *layer);
56
Rob Herringaf0d9752018-05-04 16:34:19 -050057 // Inserts the given layer:plane in the composition at the back
Sean Paul4c4646e2016-05-10 04:19:24 -040058 static int Emplace(std::vector<DrmCompositionPlane> *composition,
59 std::vector<DrmPlane *> *planes,
Matvii Zorinf0757c22021-01-18 15:54:22 +020060 DrmCompositionPlane::Type type,
Lowry Li9b6cafd2018-08-28 17:58:21 +080061 std::pair<size_t, DrmHwcLayer *> layer) {
Sean Paul4c4646e2016-05-10 04:19:24 -040062 DrmPlane *plane = PopPlane(planes);
Alexandru Gheorgheea1c5e52018-09-17 10:48:54 +010063 std::vector<DrmPlane *> unused_planes;
64 int ret = -ENOENT;
65 while (plane) {
66 ret = ValidatePlane(plane, layer.second);
67 if (!ret)
68 break;
Sean Paul2689aeb2019-03-13 14:36:52 -040069 if (!plane->zpos_property().is_immutable())
Alexandru Gheorgheea1c5e52018-09-17 10:48:54 +010070 unused_planes.push_back(plane);
71 plane = PopPlane(planes);
72 }
Sean Paul4c4646e2016-05-10 04:19:24 -040073
Alexandru Gheorgheea1c5e52018-09-17 10:48:54 +010074 if (!ret) {
Matvii Zorinf0757c22021-01-18 15:54:22 +020075 composition->emplace_back(type, plane, layer.first);
Alexandru Gheorgheea1c5e52018-09-17 10:48:54 +010076 planes->insert(planes->begin(), unused_planes.begin(),
77 unused_planes.end());
78 }
Lowry Li9b6cafd2018-08-28 17:58:21 +080079
Alexandru Gheorgheea1c5e52018-09-17 10:48:54 +010080 return ret;
Sean Paul4c4646e2016-05-10 04:19:24 -040081 }
Sean Paul4c4646e2016-05-10 04:19:24 -040082 };
83
84 // Creates a planner instance with platform-specific planning stages
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010085 static std::unique_ptr<Planner> CreateInstance(DrmDevice *drm);
Sean Paul4c4646e2016-05-10 04:19:24 -040086
87 // Takes a stack of layers and provisions hardware planes for them. If the
Rob Herringaf0d9752018-05-04 16:34:19 -050088 // entire stack can't fit in hardware, FIXME
Sean Paul4c4646e2016-05-10 04:19:24 -040089 //
90 // @layers: a map of index:layer of layers to composite
Sean Paul4c4646e2016-05-10 04:19:24 -040091 // @primary_planes: a vector of primary planes available for this frame
92 // @overlay_planes: a vector of overlay planes available for this frame
93 //
94 // Returns: A tuple with the status of the operation (0 for success) and
95 // a vector of the resulting plan (ie: layer->plane mapping).
96 std::tuple<int, std::vector<DrmCompositionPlane>> ProvisionPlanes(
Rob Herringaf0d9752018-05-04 16:34:19 -050097 std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc,
98 std::vector<DrmPlane *> *primary_planes,
Sean Paul4c4646e2016-05-10 04:19:24 -040099 std::vector<DrmPlane *> *overlay_planes);
100
101 template <typename T, typename... A>
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200102 void AddStage(A &&...args) {
Sean Paul4c4646e2016-05-10 04:19:24 -0400103 stages_.emplace_back(
104 std::unique_ptr<PlanStage>(new T(std::forward(args)...)));
105 }
106
107 private:
108 std::vector<DrmPlane *> GetUsablePlanes(
109 DrmCrtc *crtc, std::vector<DrmPlane *> *primary_planes,
110 std::vector<DrmPlane *> *overlay_planes);
111
112 std::vector<std::unique_ptr<PlanStage>> stages_;
113};
114
115// This plan stage extracts all protected layers and places them on dedicated
116// planes.
117class PlanStageProtected : public Planner::PlanStage {
118 public:
119 int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition,
Matvii Zorinf0757c22021-01-18 15:54:22 +0200120 std::map<size_t, DrmHwcLayer *> &layers,
Sean Paul4c4646e2016-05-10 04:19:24 -0400121 std::vector<DrmPlane *> *planes);
122};
123
124// This plan stage places as many layers on dedicated planes as possible (first
125// come first serve), and then sticks the rest in a precomposition plane (if
126// needed).
127class PlanStageGreedy : public Planner::PlanStage {
128 public:
129 int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition,
Matvii Zorinf0757c22021-01-18 15:54:22 +0200130 std::map<size_t, DrmHwcLayer *> &layers,
Sean Paul4c4646e2016-05-10 04:19:24 -0400131 std::vector<DrmPlane *> *planes);
132};
Sean Paulf72cccd2018-08-27 13:59:08 -0400133} // namespace android
Sean Paulda6270d2015-06-01 14:11:52 -0400134#endif