blob: 16e2859951134d97b1bcb598be5f95331a3bc1db [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 Stratiienkoe2f2c922021-02-13 10:57:47 +020063 size_t extra_client = (z_map.size() - client_size) - 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 Stratiienkoe2f2c922021-02-13 10:57:47 +020068 size_t prepend = std::min((size_t)client_start, extra_client);
69 size_t append = std::min(z_map.size() - (client_start + client_size),
70 extra_client);
71 start = client_start - (int)prepend;
Matvii Zorinef3c7972020-08-11 15:15:44 +030072 client_size += extra_client;
73 steps = 1 + std::min(std::min(append, prepend),
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020074 z_map.size() - (start + client_size));
Matvii Zorinef3c7972020-08-11 15:15:44 +030075 } else {
76 client_size = extra_client;
77 steps = 1 + z_map.size() - extra_client;
78 }
79
80 gpu_pixops = INT_MAX;
81 for (int i = 0; i < steps; i++) {
82 uint32_t po = display->CalcPixOps(z_map, start + i, client_size);
83 if (po < gpu_pixops) {
84 gpu_pixops = po;
85 client_start = start + i;
86 }
87 }
88 }
89
90 display->MarkValidated(z_map, client_start, client_size);
91
92 bool testing_needed = !(client_start == 0 && client_size == z_map.size());
93
94 if (testing_needed &&
95 display->CreateComposition(true) != HWC2::Error::None) {
96 ++display->total_stats().failed_kms_validate_;
97 gpu_pixops = total_pixops;
98 client_size = z_map.size();
99 display->MarkValidated(z_map, 0, client_size);
100 }
101 }
102
103 *num_types = client_size;
104
105 display->total_stats().frames_flattened_ = display->compositor()
106 .GetFlattenedFramesCount();
107 display->total_stats().gpu_pixops_ += gpu_pixops;
108 display->total_stats().total_pixops_ += total_pixops;
109
110 return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
111}
112
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200113std::tuple<int, size_t> Backend::GetClientLayers(
Matvii Zorinef3c7972020-08-11 15:15:44 +0300114 DrmHwcTwo::HwcDisplay *display,
115 const std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200116 int client_start = -1;
117 size_t client_size = 0;
Matvii Zorinef3c7972020-08-11 15:15:44 +0300118
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200119 for (auto &[z_order, layer] : z_map) {
Matvii Zorinef3c7972020-08-11 15:15:44 +0300120 if (IsClientLayer(display, layer)) {
121 if (client_start < 0)
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200122 client_start = (int)z_order;
Matvii Zorinef3c7972020-08-11 15:15:44 +0300123 client_size = (z_order - client_start) + 1;
124 }
125 }
126
127 return std::make_tuple(client_start, client_size);
128}
129
130bool Backend::IsClientLayer(DrmHwcTwo::HwcDisplay *display,
131 DrmHwcTwo::HwcLayer *layer) {
132 return !display->HardwareSupportsLayerType(layer->sf_type()) ||
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +0300133 !BufferInfoGetter::GetInstance()->IsHandleUsable(layer->buffer()) ||
Matvii Zorinef3c7972020-08-11 15:15:44 +0300134 display->color_transform_hint() != HAL_COLOR_TRANSFORM_IDENTITY ||
135 (layer->RequireScalingOrPhasing() &&
136 display->resource_manager()->ForcedScalingWithGpu());
137}
138
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200139// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Matvii Zorinef3c7972020-08-11 15:15:44 +0300140REGISTER_BACKEND("generic", Backend);
141
142} // namespace android