blob: 9e9e2b9af6889e57aa1653cd105b89caffe32374 [file] [log] [blame]
Matvii Zorinef3c7972020-08-11 15:15:44 +03001/*
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 Stratiienko13cc3662020-08-29 21:35:39 +030017#include "Backend.h"
18
Roman Stratiienkod21071f2021-03-09 21:56:50 +020019#include <climits>
20
Roman Stratiienko13cc3662020-08-29 21:35:39 +030021#include "BackendManager.h"
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030022#include "bufferinfo/BufferInfoGetter.h"
Matvii Zorinef3c7972020-08-11 15:15:44 +030023
24namespace android {
25
26HWC2::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 Stratiienkoe2f2c922021-02-13 10:57:47 +020041 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
42 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map_tmp;
Matvii Zorinef3c7972020-08-11 15:15:44 +030043 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 Stratiienkoe2f2c922021-02-13 10:57:47 +020055 int client_start = -1;
56 size_t client_size = 0;
Matvii Zorinef3c7972020-08-11 15:15:44 +030057
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 Stratiienko3f891822021-04-01 15:52:54 +030065 int extra_client = int(z_map.size() - client_size) - int(avail_planes);
Matvii Zorinef3c7972020-08-11 15:15:44 +030066 if (extra_client > 0) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020067 int start = 0;
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020068 size_t steps = 0;
Matvii Zorinef3c7972020-08-11 15:15:44 +030069 if (client_size != 0) {
Roman Stratiienko3f891822021-04-01 15:52:54 +030070 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 Stratiienkoe2f2c922021-02-13 10:57:47 +020074 start = client_start - (int)prepend;
Matvii Zorinef3c7972020-08-11 15:15:44 +030075 client_size += extra_client;
76 steps = 1 + std::min(std::min(append, prepend),
Roman Stratiienko3f891822021-04-01 15:52:54 +030077 int(z_map.size()) - int(start + client_size));
Matvii Zorinef3c7972020-08-11 15:15:44 +030078 } 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 Stratiienkoe2f2c922021-02-13 10:57:47 +0200116std::tuple<int, size_t> Backend::GetClientLayers(
Matvii Zorinef3c7972020-08-11 15:15:44 +0300117 DrmHwcTwo::HwcDisplay *display,
118 const std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200119 int client_start = -1;
120 size_t client_size = 0;
Matvii Zorinef3c7972020-08-11 15:15:44 +0300121
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200122 for (const auto &[z_order, layer] : z_map) {
Matvii Zorinef3c7972020-08-11 15:15:44 +0300123 if (IsClientLayer(display, layer)) {
124 if (client_start < 0)
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200125 client_start = (int)z_order;
Matvii Zorinef3c7972020-08-11 15:15:44 +0300126 client_size = (z_order - client_start) + 1;
127 }
128 }
129
130 return std::make_tuple(client_start, client_size);
131}
132
133bool Backend::IsClientLayer(DrmHwcTwo::HwcDisplay *display,
134 DrmHwcTwo::HwcLayer *layer) {
135 return !display->HardwareSupportsLayerType(layer->sf_type()) ||
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +0300136 !BufferInfoGetter::GetInstance()->IsHandleUsable(layer->buffer()) ||
Matvii Zorinef3c7972020-08-11 15:15:44 +0300137 display->color_transform_hint() != HAL_COLOR_TRANSFORM_IDENTITY ||
138 (layer->RequireScalingOrPhasing() &&
139 display->resource_manager()->ForcedScalingWithGpu());
140}
141
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200142// clang-format off
143// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables, cert-err58-cpp)
Matvii Zorinef3c7972020-08-11 15:15:44 +0300144REGISTER_BACKEND("generic", Backend);
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200145// clang-format on
Matvii Zorinef3c7972020-08-11 15:15:44 +0300146
147} // namespace android