1 /*
2 * Copyright (C) 2016 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 #define LOG_TAG "hwc-platform"
18
19 #include "Planner.h"
20
21 #include <log/log.h>
22
23 #include "drm/DrmDevice.h"
24
25 namespace android {
26
CreateInstance(DrmDevice *)27 std::unique_ptr<Planner> Planner::CreateInstance(DrmDevice *) {
28 std::unique_ptr<Planner> planner(new Planner);
29 planner->AddStage<PlanStageGreedy>();
30 return planner;
31 }
32
GetUsablePlanes(DrmCrtc * crtc,std::vector<DrmPlane * > * primary_planes,std::vector<DrmPlane * > * overlay_planes)33 std::vector<DrmPlane *> Planner::GetUsablePlanes(
34 DrmCrtc *crtc, std::vector<DrmPlane *> *primary_planes,
35 std::vector<DrmPlane *> *overlay_planes) {
36 std::vector<DrmPlane *> usable_planes;
37 std::copy_if(primary_planes->begin(), primary_planes->end(),
38 std::back_inserter(usable_planes),
39 [=](DrmPlane *plane) { return plane->GetCrtcSupported(*crtc); });
40 std::copy_if(overlay_planes->begin(), overlay_planes->end(),
41 std::back_inserter(usable_planes),
42 [=](DrmPlane *plane) { return plane->GetCrtcSupported(*crtc); });
43 return usable_planes;
44 }
45
ValidatePlane(DrmPlane * plane,DrmHwcLayer * layer)46 int Planner::PlanStage::ValidatePlane(DrmPlane *plane, DrmHwcLayer *layer) {
47 int ret = 0;
48 uint64_t blend;
49
50 if ((plane->rotation_property().id() == 0) &&
51 layer->transform != DrmHwcTransform::kIdentity) {
52 ALOGE("Rotation is not supported on plane %d", plane->id());
53 return -EINVAL;
54 }
55
56 if (plane->alpha_property().id() == 0 && layer->alpha != 0xffff) {
57 ALOGE("Alpha is not supported on plane %d", plane->id());
58 return -EINVAL;
59 }
60
61 if (plane->blend_property().id() == 0) {
62 if ((layer->blending != DrmHwcBlending::kNone) &&
63 (layer->blending != DrmHwcBlending::kPreMult)) {
64 ALOGE("Blending is not supported on plane %d", plane->id());
65 return -EINVAL;
66 }
67 } else {
68 switch (layer->blending) {
69 case DrmHwcBlending::kPreMult:
70 std::tie(blend, ret) = plane->blend_property().GetEnumValueWithName(
71 "Pre-multiplied");
72 break;
73 case DrmHwcBlending::kCoverage:
74 std::tie(blend, ret) = plane->blend_property().GetEnumValueWithName(
75 "Coverage");
76 break;
77 case DrmHwcBlending::kNone:
78 default:
79 std::tie(blend,
80 ret) = plane->blend_property().GetEnumValueWithName("None");
81 break;
82 }
83 if (ret)
84 ALOGE("Expected a valid blend mode on plane %d", plane->id());
85 }
86
87 uint32_t format = layer->buffer->format;
88 if (!plane->IsFormatSupported(format)) {
89 ALOGE("Plane %d does not supports %c%c%c%c format", plane->id(), format,
90 format >> 8, format >> 16, format >> 24);
91 return -EINVAL;
92 }
93
94 return ret;
95 }
96
ProvisionPlanes(std::map<size_t,DrmHwcLayer * > & layers,DrmCrtc * crtc,std::vector<DrmPlane * > * primary_planes,std::vector<DrmPlane * > * overlay_planes)97 std::tuple<int, std::vector<DrmCompositionPlane>> Planner::ProvisionPlanes(
98 std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc,
99 std::vector<DrmPlane *> *primary_planes,
100 std::vector<DrmPlane *> *overlay_planes) {
101 std::vector<DrmCompositionPlane> composition;
102 std::vector<DrmPlane *> planes = GetUsablePlanes(crtc, primary_planes,
103 overlay_planes);
104 if (planes.empty()) {
105 ALOGE("Display %d has no usable planes", crtc->display());
106 return std::make_tuple(-ENODEV, std::vector<DrmCompositionPlane>());
107 }
108
109 // Go through the provisioning stages and provision planes
110 for (auto &i : stages_) {
111 int ret = i->ProvisionPlanes(&composition, layers, crtc, &planes);
112 if (ret) {
113 ALOGE("Failed provision stage with ret %d", ret);
114 return std::make_tuple(ret, std::vector<DrmCompositionPlane>());
115 }
116 }
117
118 return std::make_tuple(0, std::move(composition));
119 }
120
ProvisionPlanes(std::vector<DrmCompositionPlane> * composition,std::map<size_t,DrmHwcLayer * > & layers,DrmCrtc * crtc,std::vector<DrmPlane * > * planes)121 int PlanStageProtected::ProvisionPlanes(
122 std::vector<DrmCompositionPlane> *composition,
123 std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc,
124 std::vector<DrmPlane *> *planes) {
125 int ret;
126 int protected_zorder = -1;
127 for (auto i = layers.begin(); i != layers.end();) {
128 if (!i->second->protected_usage()) {
129 ++i;
130 continue;
131 }
132
133 ret = Emplace(composition, planes, DrmCompositionPlane::Type::kLayer, crtc,
134 std::make_pair(i->first, i->second));
135 if (ret) {
136 ALOGE("Failed to dedicate protected layer! Dropping it.");
137 return ret;
138 }
139
140 protected_zorder = i->first;
141 i = layers.erase(i);
142 }
143
144 return 0;
145 }
146
ProvisionPlanes(std::vector<DrmCompositionPlane> * composition,std::map<size_t,DrmHwcLayer * > & layers,DrmCrtc * crtc,std::vector<DrmPlane * > * planes)147 int PlanStageGreedy::ProvisionPlanes(
148 std::vector<DrmCompositionPlane> *composition,
149 std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc,
150 std::vector<DrmPlane *> *planes) {
151 // Fill up the remaining planes
152 for (auto i = layers.begin(); i != layers.end(); i = layers.erase(i)) {
153 int ret = Emplace(composition, planes, DrmCompositionPlane::Type::kLayer,
154 crtc, std::make_pair(i->first, i->second));
155 // We don't have any planes left
156 if (ret == -ENOENT)
157 break;
158 else if (ret) {
159 ALOGE("Failed to emplace layer %zu, dropping it", i->first);
160 return ret;
161 }
162 }
163
164 return 0;
165 }
166 } // namespace android
167