blob: 3505385e16a6e7f643c503f6109eced4c45a5002 [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
19#include "BackendManager.h"
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030020#include "bufferinfo/BufferInfoGetter.h"
Matvii Zorinef3c7972020-08-11 15:15:44 +030021
22namespace android {
23
24HWC2::Error Backend::ValidateDisplay(DrmHwcTwo::HwcDisplay *display,
25 uint32_t *num_types,
26 uint32_t *num_requests) {
27 *num_types = 0;
28 *num_requests = 0;
29 size_t avail_planes = display->primary_planes().size() +
30 display->overlay_planes().size();
31
32 /*
33 * If more layers then planes, save one plane
34 * for client composited layers
35 */
36 if (avail_planes < display->layers().size())
37 avail_planes--;
38
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020039 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
40 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map_tmp;
Matvii Zorinef3c7972020-08-11 15:15:44 +030041 uint32_t z_index = 0;
42 // First create a map of layers and z_order values
43 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l :
44 display->layers())
45 z_map_tmp.emplace(std::make_pair(l.second.z_order(), &l.second));
46 // normalise the map so that the lowest z_order layer has key 0
47 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map_tmp)
48 z_map.emplace(std::make_pair(z_index++, l.second));
49
50 uint32_t total_pixops = display->CalcPixOps(z_map, 0, z_map.size());
51 uint32_t gpu_pixops = 0;
52
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020053 int client_start = -1;
54 size_t client_size = 0;
Matvii Zorinef3c7972020-08-11 15:15:44 +030055
56 if (display->compositor().ShouldFlattenOnClient()) {
57 client_start = 0;
58 client_size = z_map.size();
59 display->MarkValidated(z_map, client_start, client_size);
60 } else {
61 std::tie(client_start, client_size) = GetClientLayers(display, z_map);
62
Roman Stratiienko3f891822021-04-01 15:52:54 +030063 int extra_client = int(z_map.size() - client_size) - int(avail_planes);
Matvii Zorinef3c7972020-08-11 15:15:44 +030064 if (extra_client > 0) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020065 int start = 0;
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020066 size_t steps = 0;
Matvii Zorinef3c7972020-08-11 15:15:44 +030067 if (client_size != 0) {
Roman Stratiienko3f891822021-04-01 15:52:54 +030068 int prepend = std::min(client_start, extra_client);
69 int append = std::min(int(z_map.size()) -
70 int(client_start + client_size),
71 extra_client);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020072 start = client_start - (int)prepend;
Matvii Zorinef3c7972020-08-11 15:15:44 +030073 client_size += extra_client;
74 steps = 1 + std::min(std::min(append, prepend),
Roman Stratiienko3f891822021-04-01 15:52:54 +030075 int(z_map.size()) - int(start + client_size));
Matvii Zorinef3c7972020-08-11 15:15:44 +030076 } else {
77 client_size = extra_client;
78 steps = 1 + z_map.size() - extra_client;
79 }
80
81 gpu_pixops = INT_MAX;
82 for (int i = 0; i < steps; i++) {
83 uint32_t po = display->CalcPixOps(z_map, start + i, client_size);
84 if (po < gpu_pixops) {
85 gpu_pixops = po;
86 client_start = start + i;
87 }
88 }
89 }
90
91 display->MarkValidated(z_map, client_start, client_size);
92
93 bool testing_needed = !(client_start == 0 && client_size == z_map.size());
94
95 if (testing_needed &&
96 display->CreateComposition(true) != HWC2::Error::None) {
97 ++display->total_stats().failed_kms_validate_;
98 gpu_pixops = total_pixops;
99 client_size = z_map.size();
100 display->MarkValidated(z_map, 0, client_size);
101 }
102 }
103
104 *num_types = client_size;
105
106 display->total_stats().frames_flattened_ = display->compositor()
107 .GetFlattenedFramesCount();
108 display->total_stats().gpu_pixops_ += gpu_pixops;
109 display->total_stats().total_pixops_ += total_pixops;
110
111 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
112}
113
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200114std::tuple<int, size_t> Backend::GetClientLayers(
Matvii Zorinef3c7972020-08-11 15:15:44 +0300115 DrmHwcTwo::HwcDisplay *display,
116 const std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200117 int client_start = -1;
118 size_t client_size = 0;
Matvii Zorinef3c7972020-08-11 15:15:44 +0300119
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200120 for (auto &[z_order, layer] : z_map) {
Matvii Zorinef3c7972020-08-11 15:15:44 +0300121 if (IsClientLayer(display, layer)) {
122 if (client_start < 0)
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200123 client_start = (int)z_order;
Matvii Zorinef3c7972020-08-11 15:15:44 +0300124 client_size = (z_order - client_start) + 1;
125 }
126 }
127
128 return std::make_tuple(client_start, client_size);
129}
130
131bool Backend::IsClientLayer(DrmHwcTwo::HwcDisplay *display,
132 DrmHwcTwo::HwcLayer *layer) {
133 return !display->HardwareSupportsLayerType(layer->sf_type()) ||
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +0300134 !BufferInfoGetter::GetInstance()->IsHandleUsable(layer->buffer()) ||
Matvii Zorinef3c7972020-08-11 15:15:44 +0300135 display->color_transform_hint() != HAL_COLOR_TRANSFORM_IDENTITY ||
136 (layer->RequireScalingOrPhasing() &&
137 display->resource_manager()->ForcedScalingWithGpu());
138}
139
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200140// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Matvii Zorinef3c7972020-08-11 15:15:44 +0300141REGISTER_BACKEND("generic", Backend);
142
143} // namespace android