2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "hwc-platform"
19 #include "drmresources.h"
22 #include <cutils/log.h>
26 std::vector<DrmPlane *> Planner::GetUsablePlanes(
27 DrmCrtc *crtc, std::vector<DrmPlane *> *primary_planes,
28 std::vector<DrmPlane *> *overlay_planes) {
29 std::vector<DrmPlane *> usable_planes;
30 std::copy_if(primary_planes->begin(), primary_planes->end(),
31 std::back_inserter(usable_planes),
32 [=](DrmPlane *plane) { return plane->GetCrtcSupported(*crtc); });
33 std::copy_if(overlay_planes->begin(), overlay_planes->end(),
34 std::back_inserter(usable_planes),
35 [=](DrmPlane *plane) { return plane->GetCrtcSupported(*crtc); });
39 std::tuple<int, std::vector<DrmCompositionPlane>> Planner::ProvisionPlanes(
40 std::map<size_t, DrmHwcLayer *> &layers, bool use_squash_fb, DrmCrtc *crtc,
41 std::vector<DrmPlane *> *primary_planes,
42 std::vector<DrmPlane *> *overlay_planes) {
43 std::vector<DrmCompositionPlane> composition;
44 std::vector<DrmPlane *> planes =
45 GetUsablePlanes(crtc, primary_planes, overlay_planes);
47 ALOGE("Display %d has no usable planes", crtc->display());
48 return std::make_tuple(-ENODEV, std::vector<DrmCompositionPlane>());
51 // If needed, reserve the squash plane at the highest z-order
52 DrmPlane *squash_plane = NULL;
54 if (!planes.empty()) {
55 squash_plane = planes.back();
58 ALOGI("Not enough planes to reserve for squash fb");
62 // If needed, reserve the precomp plane at the next highest z-order
63 DrmPlane *precomp_plane = NULL;
64 if (layers.size() > planes.size()) {
65 if (!planes.empty()) {
66 precomp_plane = planes.back();
68 composition.emplace_back(DrmCompositionPlane::Type::kPrecomp,
71 ALOGE("Not enough planes to reserve for precomp fb");
75 // Go through the provisioning stages and provision planes
76 for (auto &i : stages_) {
77 int ret = i->ProvisionPlanes(&composition, layers, crtc, &planes);
79 ALOGE("Failed provision stage with ret %d", ret);
80 return std::make_tuple(ret, std::vector<DrmCompositionPlane>());
85 composition.emplace_back(DrmCompositionPlane::Type::kSquash, squash_plane,
88 return std::make_tuple(0, std::move(composition));
91 int PlanStageProtected::ProvisionPlanes(
92 std::vector<DrmCompositionPlane> *composition,
93 std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc,
94 std::vector<DrmPlane *> *planes) {
96 int protected_zorder = -1;
97 for (auto i = layers.begin(); i != layers.end();) {
98 if (!i->second->protected_usage()) {
103 ret = Emplace(composition, planes, DrmCompositionPlane::Type::kLayer, crtc,
106 ALOGE("Failed to dedicate protected layer! Dropping it.");
108 protected_zorder = i->first;
112 if (protected_zorder == -1)
115 // Add any layers below the protected content to the precomposition since we
116 // need to punch a hole through them.
117 for (auto i = layers.begin(); i != layers.end();) {
118 // Skip layers above the z-order of the protected content
119 if (i->first > static_cast<size_t>(protected_zorder)) {
124 // If there's no precomp layer already queued, queue one now.
125 DrmCompositionPlane *precomp = GetPrecomp(composition);
127 precomp->source_layers().emplace_back(i->first);
129 if (!planes->empty()) {
130 DrmPlane *precomp_plane = planes->back();
132 composition->emplace_back(DrmCompositionPlane::Type::kPrecomp,
133 precomp_plane, crtc, i->first);
135 ALOGE("Not enough planes to reserve for precomp fb");
143 int PlanStagePrecomp::ProvisionPlanes(
144 std::vector<DrmCompositionPlane> *composition,
145 std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc,
146 std::vector<DrmPlane *> *planes) {
147 DrmCompositionPlane *precomp = GetPrecomp(composition);
148 if (!precomp || precomp->source_layers().empty())
151 // Find lowest zorder out of precomp layers
152 size_t precomp_zorder = *std::min_element(
153 precomp->source_layers().begin(), precomp->source_layers().end(),
154 [](size_t a, size_t b) { return a < b; });
156 // if there are any remaining layers on top of any of the precomp layers,
157 // add them to precomp to avoid blending issues since precomp is always at
159 for (auto i = layers.begin(); i != layers.end();) {
160 if (i->first < precomp_zorder) {
164 precomp->source_layers().emplace_back(i->first);
171 int PlanStageGreedy::ProvisionPlanes(
172 std::vector<DrmCompositionPlane> *composition,
173 std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc,
174 std::vector<DrmPlane *> *planes) {
175 // Fill up the remaining planes
176 for (auto i = layers.begin(); i != layers.end(); i = layers.erase(i)) {
177 int ret = Emplace(composition, planes, DrmCompositionPlane::Type::kLayer,
179 // We don't have any planes left
183 ALOGE("Failed to emplace layer %zu, dropping it", i->first);
186 // Put the rest of the layers in the precomp plane
187 DrmCompositionPlane *precomp = GetPrecomp(composition);
189 for (auto i = layers.begin(); i != layers.end(); i = layers.erase(i))
190 precomp->source_layers().emplace_back(i->first);