android_external_minigbm/gbm_mesa_driver/UniqueFd.h
Roman Stratiienko e297f9bacd minigbm: Add gbm_mesa backend
This is adaptation of the Rob Herring's gbm_gralloc HAL [1] to work
as minigbm backend, optimised to work in modern Android conditions.

gbm_gralloc has a huge potential and can provide more-or-less optimal
allocation by using mesa3d allocation APIs. It should work out of the
box for the all hardware mesa3d is supported.

Limitations:
This backend doesn't care of any other SOC-specific graphical components
such as camera, hardware video codecs, etc.

Differences between gbm_gralloc:
1. Designed to distinguish between Allocator and Mapper-sphal users.
   For Allocator gbm driver is initialized using standalone KMS card
   node (but only if 'lima', 'panfrost' or 'v3d' GPU was detected).
   For Mapper requests driver is initialized only using GPU render node.
   (which require less permissions for regular apps but suffitient
    for mapping).
2. GBM driver is initialized and bo is imported into gbm_mesa only
   if Mapper imports bo with software usage flags.
   (no time wasted in case SW access isn't required)
3. gbm_gralloc supported only HAL_PIXEL_FORMAT_YV12 video format. This
   driver aims to support more formats (using linear modifier only).

* NOTE:
This may also require:
1. Adding secomp rule into the mediaswcodec.policy file
"sched_getaffinity: 1"
2. Adding records to selinux vendor/file_contexts file
"/vendor/lib{64}?/libgbm_mesa_wrapper.so
 u:object_r:same_process_hal_file:s0"
"/vendor/bin/hw/android\.hardware\.graphics\.allocator@4\.0-service\.minigbm_gbm_mesa
 u:object_r:hal_graphics_allocator_default_exec:s0"
3. Kernel v5.3+ for using fstat(dma-buf)->inode as unique buffer id

[1]: https://github.com/robherring/gbm_gralloc

v2:
- Fixed incorrect size calculations for NV12 buffers
- Add etnaviv, freedreno and vc4 to gpus list which require kmsro entry
- Rebased

v3:
Squashed local fix commits:
- RPI4: Add alignment for CSI camera
- gbm_mesa: Handle DRM_FORMAT_R8 format to handle BLOBS
- gbm_mesa: Always use DRM_FORMAT_R8 for buffers that unsupported by GBM
- gbm_mesa: Add RGB888 format support for CPU access
- gbm_mesa: Remove incorrect negation
- gbm_mesa: Fallback COMPOSER buffers into VRAM
- gbm_mesa: Add combinations to support external camera

v4:
- Use dlopen/dlsym to access gbm_wrapper
- Add fallback allocation for unsupported by mesa3d formats

v5:
- Obtain map-time stride and report it to Android as pixel-stride.
  Map-time strides are different after gbm_create and gbm_import.
  Use map stride after gbm_import.
  This fixes artifacts on Intel and Nouveau.
- GBM wrapper has been converted from cpp to c
- Code refactor and cleanup
- Licence headers added

Signed-off-by: Roman Stratiienko <r.stratiienko@gmail.com>
Change-Id: If0f3dac42bea74f97a87e5a682380c33f4ff6837
2026-07-15 22:36:25 -04:00

115 lines
2.6 KiB
C++

/*
* Copyright (C) 2021 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.
*/
#ifndef UNIQUEFD_H_
#define UNIQUEFD_H_
#include <unistd.h>
#include <memory>
/*
* Using UniqueFd:
* 1. Create UniqueFd object:
* auto fd_obj = UniqueFd(open("SomeFile", xxx));
*
* 2. Check whether the fd_obj is empty:
* if (!fd_obj) { return -errno; }
*
* 3. Accessing the file descriptor:
* int ret = read(fd_obj.Get(), buf, buf_size);
*
* 4. Closing the file:
* FD will be closed once execution leaves fd_obj scope (on any return,
* exception, destruction of class/struct where object is member, etc.).
* User can also force closing the fd_obj by calling:
* fd_obj = UniqueFd();
* // fd is closed and fd_obj is empty now.
*
* 5. File descriptor may be transferred to the code, which will close it after
* using. This can be done in 2 ways:
* a. Duplicate the fd, in this case both fds should be closed separately:
* int out_fd = dup(fd_obj.Get();
* ...
* close(out_fd);
* b. Transfer ownership, use this method if you do not need the fd anymore.
* int out_fd = fd_obj.Release();
* // fd_obj is empty now.
* ...
* close(out_fd);
*
* 6. Transferring fd into another UniqueFD object:
* UniqueFd fd_obj_2 = std::move(fd_obj);
* // fd_obj empty now
*/
constexpr int kEmptyFd = -1;
class UniqueFd
{
public:
UniqueFd() = default;
explicit UniqueFd(int fd) : fd_(fd){};
auto Release [[nodiscard]] () -> int
{
return std::exchange(fd_, kEmptyFd);
}
auto Get [[nodiscard]] () const -> int
{
return fd_;
}
explicit operator bool() const
{
return fd_ != kEmptyFd;
}
~UniqueFd()
{
Set(kEmptyFd);
}
/* Allow move semantics */
UniqueFd(UniqueFd &&rhs) noexcept
{
Set(rhs.Release());
}
auto operator=(UniqueFd &&rhs) noexcept -> UniqueFd &
{
Set(rhs.Release());
return *this;
}
/* Disable copy semantics */
UniqueFd(const UniqueFd &) = delete;
auto operator=(const UniqueFd &) = delete;
private:
void Set(int new_fd)
{
if (fd_ != kEmptyFd) {
close(fd_);
}
fd_ = new_fd;
}
int fd_ = kEmptyFd;
};
#endif