2 * Copyright (C) 2020 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.
21 #include "BackendManager.h"
22 #include "bufferinfo/BufferInfoGetter.h"
26 HWC2::Error Backend::ValidateDisplay(DrmHwcTwo::HwcDisplay *display,
28 uint32_t *num_requests) {
31 size_t avail_planes = display->primary_planes().size() +
32 display->overlay_planes().size();
35 * If more layers then planes, save one plane
36 * for client composited layers
38 if (avail_planes < display->layers().size())
41 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
42 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map_tmp;
44 // First create a map of layers and z_order values
45 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l :
47 z_map_tmp.emplace(std::make_pair(l.second.z_order(), &l.second));
48 // normalise the map so that the lowest z_order layer has key 0
49 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map_tmp)
50 z_map.emplace(std::make_pair(z_index++, l.second));
52 uint32_t total_pixops = display->CalcPixOps(z_map, 0, z_map.size());
53 uint32_t gpu_pixops = 0;
55 int client_start = -1;
56 size_t client_size = 0;
58 if (display->compositor().ShouldFlattenOnClient()) {
60 client_size = z_map.size();
61 display->MarkValidated(z_map, client_start, client_size);
63 std::tie(client_start, client_size) = GetClientLayers(display, z_map);
65 int extra_client = int(z_map.size() - client_size) - int(avail_planes);
66 if (extra_client > 0) {
69 if (client_size != 0) {
70 int prepend = std::min(client_start, extra_client);
71 int append = std::min(int(z_map.size()) -
72 int(client_start + client_size),
74 start = client_start - (int)prepend;
75 client_size += extra_client;
76 steps = 1 + std::min(std::min(append, prepend),
77 int(z_map.size()) - int(start + client_size));
79 client_size = extra_client;
80 steps = 1 + z_map.size() - extra_client;
84 for (int i = 0; i < steps; i++) {
85 uint32_t po = display->CalcPixOps(z_map, start + i, client_size);
86 if (po < gpu_pixops) {
88 client_start = start + i;
93 display->MarkValidated(z_map, client_start, client_size);
95 bool testing_needed = !(client_start == 0 && client_size == z_map.size());
98 display->CreateComposition(true) != HWC2::Error::None) {
99 ++display->total_stats().failed_kms_validate_;
100 gpu_pixops = total_pixops;
101 client_size = z_map.size();
102 display->MarkValidated(z_map, 0, client_size);
106 *num_types = client_size;
108 display->total_stats().frames_flattened_ = display->compositor()
109 .GetFlattenedFramesCount();
110 display->total_stats().gpu_pixops_ += gpu_pixops;
111 display->total_stats().total_pixops_ += total_pixops;
113 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
116 std::tuple<int, size_t> Backend::GetClientLayers(
117 DrmHwcTwo::HwcDisplay *display,
118 const std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map) {
119 int client_start = -1;
120 size_t client_size = 0;
122 for (const auto &[z_order, layer] : z_map) {
123 if (IsClientLayer(display, layer)) {
124 if (client_start < 0)
125 client_start = (int)z_order;
126 client_size = (z_order - client_start) + 1;
130 return std::make_tuple(client_start, client_size);
133 bool Backend::IsClientLayer(DrmHwcTwo::HwcDisplay *display,
134 DrmHwcTwo::HwcLayer *layer) {
135 return !display->HardwareSupportsLayerType(layer->sf_type()) ||
136 !BufferInfoGetter::GetInstance()->IsHandleUsable(layer->buffer()) ||
137 display->color_transform_hint() != HAL_COLOR_TRANSFORM_IDENTITY ||
138 (layer->RequireScalingOrPhasing() &&
139 display->resource_manager()->ForcedScalingWithGpu());
143 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables, cert-err58-cpp)
144 REGISTER_BACKEND("generic", Backend);
147 } // namespace android