Matvii Zorin | ef3c797 | 2020-08-11 15:15:44 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | |
Roman Stratiienko | 13cc366 | 2020-08-29 21:35:39 +0300 | [diff] [blame] | 17 | #include "Backend.h" |
| 18 | |
Roman Stratiienko | d21071f | 2021-03-09 21:56:50 +0200 | [diff] [blame] | 19 | #include <climits> |
| 20 | |
Roman Stratiienko | 13cc366 | 2020-08-29 21:35:39 +0300 | [diff] [blame] | 21 | #include "BackendManager.h" |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 22 | #include "bufferinfo/BufferInfoGetter.h" |
Matvii Zorin | ef3c797 | 2020-08-11 15:15:44 +0300 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | HWC2::Error Backend::ValidateDisplay(DrmHwcTwo::HwcDisplay *display, |
| 27 | uint32_t *num_types, |
| 28 | uint32_t *num_requests) { |
| 29 | *num_types = 0; |
| 30 | *num_requests = 0; |
| 31 | size_t avail_planes = display->primary_planes().size() + |
| 32 | display->overlay_planes().size(); |
| 33 | |
| 34 | /* |
| 35 | * If more layers then planes, save one plane |
| 36 | * for client composited layers |
| 37 | */ |
| 38 | if (avail_planes < display->layers().size()) |
| 39 | avail_planes--; |
| 40 | |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 41 | std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map; |
| 42 | std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map_tmp; |
Matvii Zorin | ef3c797 | 2020-08-11 15:15:44 +0300 | [diff] [blame] | 43 | uint32_t z_index = 0; |
| 44 | // First create a map of layers and z_order values |
| 45 | for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : |
| 46 | display->layers()) |
| 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)); |
| 51 | |
| 52 | uint32_t total_pixops = display->CalcPixOps(z_map, 0, z_map.size()); |
| 53 | uint32_t gpu_pixops = 0; |
| 54 | |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 55 | int client_start = -1; |
| 56 | size_t client_size = 0; |
Matvii Zorin | ef3c797 | 2020-08-11 15:15:44 +0300 | [diff] [blame] | 57 | |
| 58 | if (display->compositor().ShouldFlattenOnClient()) { |
| 59 | client_start = 0; |
| 60 | client_size = z_map.size(); |
| 61 | display->MarkValidated(z_map, client_start, client_size); |
| 62 | } else { |
| 63 | std::tie(client_start, client_size) = GetClientLayers(display, z_map); |
| 64 | |
Roman Stratiienko | 3f89182 | 2021-04-01 15:52:54 +0300 | [diff] [blame] | 65 | int extra_client = int(z_map.size() - client_size) - int(avail_planes); |
Matvii Zorin | ef3c797 | 2020-08-11 15:15:44 +0300 | [diff] [blame] | 66 | if (extra_client > 0) { |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 67 | int start = 0; |
Roman Stratiienko | b3b5c1e | 2021-02-15 13:44:19 +0200 | [diff] [blame] | 68 | size_t steps = 0; |
Matvii Zorin | ef3c797 | 2020-08-11 15:15:44 +0300 | [diff] [blame] | 69 | if (client_size != 0) { |
Roman Stratiienko | 3f89182 | 2021-04-01 15:52:54 +0300 | [diff] [blame] | 70 | int prepend = std::min(client_start, extra_client); |
| 71 | int append = std::min(int(z_map.size()) - |
| 72 | int(client_start + client_size), |
| 73 | extra_client); |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 74 | start = client_start - (int)prepend; |
Matvii Zorin | ef3c797 | 2020-08-11 15:15:44 +0300 | [diff] [blame] | 75 | client_size += extra_client; |
| 76 | steps = 1 + std::min(std::min(append, prepend), |
Roman Stratiienko | 3f89182 | 2021-04-01 15:52:54 +0300 | [diff] [blame] | 77 | int(z_map.size()) - int(start + client_size)); |
Matvii Zorin | ef3c797 | 2020-08-11 15:15:44 +0300 | [diff] [blame] | 78 | } else { |
| 79 | client_size = extra_client; |
| 80 | steps = 1 + z_map.size() - extra_client; |
| 81 | } |
| 82 | |
| 83 | gpu_pixops = INT_MAX; |
| 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) { |
| 87 | gpu_pixops = po; |
| 88 | client_start = start + i; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | display->MarkValidated(z_map, client_start, client_size); |
| 94 | |
| 95 | bool testing_needed = !(client_start == 0 && client_size == z_map.size()); |
| 96 | |
| 97 | if (testing_needed && |
| 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); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | *num_types = client_size; |
| 107 | |
| 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; |
| 112 | |
| 113 | return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None; |
| 114 | } |
| 115 | |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 116 | std::tuple<int, size_t> Backend::GetClientLayers( |
Matvii Zorin | ef3c797 | 2020-08-11 15:15:44 +0300 | [diff] [blame] | 117 | DrmHwcTwo::HwcDisplay *display, |
| 118 | const std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map) { |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 119 | int client_start = -1; |
| 120 | size_t client_size = 0; |
Matvii Zorin | ef3c797 | 2020-08-11 15:15:44 +0300 | [diff] [blame] | 121 | |
Roman Stratiienko | d21071f | 2021-03-09 21:56:50 +0200 | [diff] [blame] | 122 | for (const auto &[z_order, layer] : z_map) { |
Matvii Zorin | ef3c797 | 2020-08-11 15:15:44 +0300 | [diff] [blame] | 123 | if (IsClientLayer(display, layer)) { |
| 124 | if (client_start < 0) |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 125 | client_start = (int)z_order; |
Matvii Zorin | ef3c797 | 2020-08-11 15:15:44 +0300 | [diff] [blame] | 126 | client_size = (z_order - client_start) + 1; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return std::make_tuple(client_start, client_size); |
| 131 | } |
| 132 | |
| 133 | bool Backend::IsClientLayer(DrmHwcTwo::HwcDisplay *display, |
| 134 | DrmHwcTwo::HwcLayer *layer) { |
| 135 | return !display->HardwareSupportsLayerType(layer->sf_type()) || |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 136 | !BufferInfoGetter::GetInstance()->IsHandleUsable(layer->buffer()) || |
Matvii Zorin | ef3c797 | 2020-08-11 15:15:44 +0300 | [diff] [blame] | 137 | display->color_transform_hint() != HAL_COLOR_TRANSFORM_IDENTITY || |
| 138 | (layer->RequireScalingOrPhasing() && |
| 139 | display->resource_manager()->ForcedScalingWithGpu()); |
| 140 | } |
| 141 | |
Roman Stratiienko | d21071f | 2021-03-09 21:56:50 +0200 | [diff] [blame] | 142 | // clang-format off |
| 143 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables, cert-err58-cpp) |
Matvii Zorin | ef3c797 | 2020-08-11 15:15:44 +0300 | [diff] [blame] | 144 | REGISTER_BACKEND("generic", Backend); |
Roman Stratiienko | d21071f | 2021-03-09 21:56:50 +0200 | [diff] [blame] | 145 | // clang-format on |
Matvii Zorin | ef3c797 | 2020-08-11 15:15:44 +0300 | [diff] [blame] | 146 | |
| 147 | } // namespace android |