SurfaceFlinger

[096]图解HWC的合成策略

2024-04-23  本文已影响0人  王小二的技术栈

背景

最近好几个网友都来问我HWC是根据什么条件来决定client合成还是device,作为之前一直"吹牛逼",说自己如何好学,如何积极回答网友问题的博主,这波必须安排,看完这篇文章,绝对让你明白。

本文参考实现来源于external/drm_hwcomposer,我相信别的厂家的思路也大差不差。

一、获取参与SF合成的所有layers

假设我们的所有可见的layers按照z轴,图中有7个layer这样子排列。


二、根据该屏幕支持的Usable Planes数量算出client合成的layer数量

假设该屏幕支持4个Planes,但是由于layer的数量大于4,因为需要保留一个Planes,用于client合成,也就是Framebuffersurface。
HWC只支持4-1=3个layer为device合成,需要找到连续7-3=4个layers作为client合成(代码分析2_1)

2.1 选择像素点之和最少的连续4个layers

如图,我们有了四个备选方案,我们要选择像素点个数之和最少的连续4个layers,我觉得这样子的策略,应该是减少GPU合成的工作量(代码分析2_2

2.2 最后的结果

假如第二个方案中,像素点个数之和最少,HWC最后返回的策略如下图。


小结

这个就是最简单的策略,但是接下来要在这个基础,引申出更加复杂的策略。

三、假如有layer指定要client合成

layer是可以指定client合成的,例如layer有圆角,当然还有很多情况,后面讲代码的时候会讲(代码分析3_0)。我们先假设指定client合成的layer为2,4。


第一个指定的client layer和最后一个指定的client layer之间的 layer 必须是变成client了(代码分析3_1),如下图:3就很惨的也被设置成client合成了

四、再次考虑Usable Planes数量算出client合成的layer的数量

按照中说的一样,我们还是需要找到连续的4个layer作为client合成,但是我们还的还得加个条件,包含中提到的2~4的三个layer

4.1 得到备选方案

这次我们只有两个备选方案了,因为2.1中提到的第1个和第4个方案都没有同时包含2~4的三个layer

4.2 选择像素点之和最少的连续4个layer的方案。

还是和中的策略一样,假如这次是2,3,4,5的像素点之和最少,最后得到的结果如下。

小结

到这里应该看明白了,其实图解策略就很简单了,再去结合代码看就比较容易了。

代码分析

代码量不多,我就全部贴上来了external/drm_hwcomposer/backend/Backend.cpp,文章中代码分析X_X对应下面的部分注释,关键是看懂这个函数的实现

//关键代码,返回client layer的start index和size。
std::tie(client_start, client_size) = GetClientLayers(display, layers);

带着我前面讲的策略去跟这个函数。

/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "Backend.h"

#include <climits>

#include "BackendManager.h"
#include "bufferinfo/BufferInfoGetter.h"

namespace android {

HWC2::Error Backend::ValidateDisplay(HwcDisplay *display, uint32_t *num_types,
                                     uint32_t *num_requests) {
  *num_types = 0;
  *num_requests = 0;

  auto layers = display->GetOrderLayersByZPos();

  int client_start = -1;
  size_t client_size = 0;

  if (display->ProcessClientFlatteningState(layers.size() <= 1)) {
    display->total_stats().frames_flattened_++;
    client_start = 0;
    client_size = layers.size();
    MarkValidated(layers, client_start, client_size);
  } else {
    std::tie(client_start, client_size) = GetClientLayers(display, layers);//关键代码,返回client layer的start id和size。

    MarkValidated(layers, client_start, client_size);

    bool testing_needed = !(client_start == 0 && client_size == layers.size());

    AtomicCommitArgs a_args = {.test_only = true};

    if (testing_needed &&
        display->CreateComposition(a_args) != HWC2::Error::None) {
      ++display->total_stats().failed_kms_validate_;
      client_start = 0;
      client_size = layers.size();
      MarkValidated(layers, 0, client_size);
    }
  }

  *num_types = client_size;

  display->total_stats().gpu_pixops_ += CalcPixOps(layers, client_start,
                                                   client_size);
  display->total_stats().total_pixops_ += CalcPixOps(layers, 0, layers.size());

  return *num_types != 0 ? HWC2::Error::HasChanges : HWC2::Error::None;
}

std::tuple<int, size_t> Backend::GetClientLayers(
    HwcDisplay *display, const std::vector<HwcLayer *> &layers) {
  int client_start = -1;
  size_t client_size = 0;

  for (size_t z_order = 0; z_order < layers.size(); ++z_order) {
    if (IsClientLayer(display, layers[z_order])) {
      if (client_start < 0)
        client_start = (int)z_order;
      client_size = (z_order - client_start) + 1;
    }
  }//代码分析3_1

  return GetExtraClientRange(display, layers, client_start, client_size);//扩大额外的需要为client的layers
}

//代码分析3_0
//判断layer是不是必须为client合成或者已经指定了client合成
bool Backend::IsClientLayer(HwcDisplay *display, HwcLayer *layer) {
  return !HardwareSupportsLayerType(layer->GetSfType()) ||
         !BufferInfoGetter::GetInstance()->IsHandleUsable(layer->GetBuffer()) ||
         display->color_transform_hint() != HAL_COLOR_TRANSFORM_IDENTITY ||
         (layer->RequireScalingOrPhasing() &&
          display->GetHwc2()->GetResMan().ForcedScalingWithGpu());
}

bool Backend::HardwareSupportsLayerType(HWC2::Composition comp_type) {
  return comp_type == HWC2::Composition::Device ||
         comp_type == HWC2::Composition::Cursor;
}

//计算连续几个layer的像素点之和
uint32_t Backend::CalcPixOps(const std::vector<HwcLayer *> &layers,
                             size_t first_z, size_t size) {
  uint32_t pixops = 0;
  for (size_t z_order = 0; z_order < layers.size(); ++z_order) {
    if (z_order >= first_z && z_order < first_z + size) {
      hwc_rect_t df = layers[z_order]->GetDisplayFrame();
      pixops += (df.right - df.left) * (df.bottom - df.top);
    }
  }
  return pixops;
}

//标记每个layer的合成策略,最后sf拿到的就是这个标记
void Backend::MarkValidated(std::vector<HwcLayer *> &layers,
                            size_t client_first_z, size_t client_size) {
  for (size_t z_order = 0; z_order < layers.size(); ++z_order) {
    if (z_order >= client_first_z && z_order < client_first_z + client_size)
      layers[z_order]->SetValidatedType(HWC2::Composition::Client);
    else
      layers[z_order]->SetValidatedType(HWC2::Composition::Device);
  }
}

std::tuple<int, int> Backend::GetExtraClientRange(
    HwcDisplay *display, const std::vector<HwcLayer *> &layers,
    int client_start, size_t client_size) {
  auto planes = display->GetPipe().GetUsablePlanes();
  size_t avail_planes = planes.size();//获取整个drm支持的planes的数量

  /*
   * If more layers then planes, save one plane
   * for client composited layers
   */
  if (avail_planes < display->layers().size())
    avail_planes--;//保留一个用于存放所有client合成的layer

  int extra_client = int(layers.size() - client_size) - int(avail_planes);//计算剩余需要成为client的数量
  //上面是代码分析2_1

  if (extra_client > 0) {
    int start = 0;
    size_t steps = 0;
    if (client_size != 0) {//有layer指定了client合成
      int prepend = std::min(client_start, extra_client);
      int append = std::min(int(layers.size()) -
                                int(client_start + client_size),
                            extra_client);
      start = client_start - (int)prepend;
      client_size += extra_client;
      steps = 1 + std::min(std::min(append, prepend),
                           int(layers.size()) - int(start + client_size));
    } else {//没有layer指定了client合成。代码分析
      client_size = extra_client;//剩余的都是client
      steps = 1 + layers.size() - extra_client;//计算硬件合成步伐
    }

    //选择像素之和最少的连续layers
    uint32_t gpu_pixops = UINT32_MAX;
    for (size_t i = 0; i < steps; i++) {
      uint32_t po = CalcPixOps(layers, start + i, client_size);
      if (po < gpu_pixops) {
        gpu_pixops = po;
        client_start = start + int(i);
      }
    } //代码分析2_2
  }

  return std::make_tuple(client_start, client_size);
}

// clang-format off
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables, cert-err58-cpp)
REGISTER_BACKEND("generic", Backend);
// clang-format on

}  // namespace android

上一篇下一篇

猜你喜欢

热点阅读