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 
17 #include "Backend.h"
18 
19 #include "BackendManager.h"
20 #include "bufferinfo/BufferInfoGetter.h"
21 
22 namespace android {
23 
ValidateDisplay(DrmHwcTwo::HwcDisplay * display,uint32_t * num_types,uint32_t * num_requests)24 HWC2::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 
39   std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map, z_map_tmp;
40   uint32_t z_index = 0;
41   // First create a map of layers and z_order values
42   for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l :
43        display->layers())
44     z_map_tmp.emplace(std::make_pair(l.second.z_order(), &l.second));
45   // normalise the map so that the lowest z_order layer has key 0
46   for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map_tmp)
47     z_map.emplace(std::make_pair(z_index++, l.second));
48 
49   uint32_t total_pixops = display->CalcPixOps(z_map, 0, z_map.size());
50   uint32_t gpu_pixops = 0;
51 
52   int client_start = -1, client_size = 0;
53 
54   if (display->compositor().ShouldFlattenOnClient()) {
55     client_start = 0;
56     client_size = z_map.size();
57     display->MarkValidated(z_map, client_start, client_size);
58   } else {
59     std::tie(client_start, client_size) = GetClientLayers(display, z_map);
60 
61     int extra_client = (z_map.size() - client_size) - avail_planes;
62     if (extra_client > 0) {
63       int start = 0, steps;
64       if (client_size != 0) {
65         int prepend = std::min(client_start, extra_client);
66         int append = std::min(int(z_map.size() - (client_start + client_size)),
67                               extra_client);
68         start = client_start - prepend;
69         client_size += extra_client;
70         steps = 1 + std::min(std::min(append, prepend),
71                              int(z_map.size()) - (start + client_size));
72       } else {
73         client_size = extra_client;
74         steps = 1 + z_map.size() - extra_client;
75       }
76 
77       gpu_pixops = INT_MAX;
78       for (int i = 0; i < steps; i++) {
79         uint32_t po = display->CalcPixOps(z_map, start + i, client_size);
80         if (po < gpu_pixops) {
81           gpu_pixops = po;
82           client_start = start + i;
83         }
84       }
85     }
86 
87     display->MarkValidated(z_map, client_start, client_size);
88 
89     bool testing_needed = !(client_start == 0 && client_size == z_map.size());
90 
91     if (testing_needed &&
92         display->CreateComposition(true) != HWC2::Error::None) {
93       ++display->total_stats().failed_kms_validate_;
94       gpu_pixops = total_pixops;
95       client_size = z_map.size();
96       display->MarkValidated(z_map, 0, client_size);
97     }
98   }
99 
100   *num_types = client_size;
101 
102   display->total_stats().frames_flattened_ = display->compositor()
103                                                  .GetFlattenedFramesCount();
104   display->total_stats().gpu_pixops_ += gpu_pixops;
105   display->total_stats().total_pixops_ += total_pixops;
106 
107   return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None;
108 }
109 
GetClientLayers(DrmHwcTwo::HwcDisplay * display,const std::map<uint32_t,DrmHwcTwo::HwcLayer * > & z_map)110 std::tuple<int, int> Backend::GetClientLayers(
111     DrmHwcTwo::HwcDisplay *display,
112     const std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map) {
113   int client_start = -1, client_size = 0;
114 
115   for (auto & [ z_order, layer ] : z_map) {
116     if (IsClientLayer(display, layer)) {
117       if (client_start < 0)
118         client_start = z_order;
119       client_size = (z_order - client_start) + 1;
120     }
121   }
122 
123   return std::make_tuple(client_start, client_size);
124 }
125 
IsClientLayer(DrmHwcTwo::HwcDisplay * display,DrmHwcTwo::HwcLayer * layer)126 bool Backend::IsClientLayer(DrmHwcTwo::HwcDisplay *display,
127                             DrmHwcTwo::HwcLayer *layer) {
128   return !display->HardwareSupportsLayerType(layer->sf_type()) ||
129          !BufferInfoGetter::GetInstance()->IsHandleUsable(layer->buffer()) ||
130          display->color_transform_hint() != HAL_COLOR_TRANSFORM_IDENTITY ||
131          (layer->RequireScalingOrPhasing() &&
132           display->resource_manager()->ForcedScalingWithGpu());
133 }
134 
135 REGISTER_BACKEND("generic", Backend);
136 
137 }  // namespace android
138