Flutter Impeller
impeller::android::HardwareBuffer Class Reference

A wrapper for AHardwareBuffer https://developer.android.com/ndk/reference/group/a-hardware-buffer. More...

#include <hardware_buffer.h>

Public Types

enum class  CPUAccessType {
  kRead ,
  kWrite
}
 

Public Member Functions

 HardwareBuffer (HardwareBufferDescriptor descriptor)
 
 ~HardwareBuffer ()
 
 HardwareBuffer (const HardwareBuffer &)=delete
 
HardwareBufferoperator= (const HardwareBuffer &)=delete
 
bool IsValid () const
 
AHardwareBuffer * GetHandle () const
 
const HardwareBufferDescriptorGetDescriptor () const
 
const AHardwareBuffer_Desc & GetAndroidDescriptor () const
 
std::optional< uint64_t > GetSystemUniqueID () const
 Get the system wide unique ID of the hardware buffer if possible. This is only available on Android API 31 and above. Within the process, the handle are unique. More...
 
void * Lock (CPUAccessType type) const
 Lock the buffer for CPU access. This call may fail if the buffer was not created with one the usages that allow for CPU access. More...
 
bool Unlock () const
 Unlock a mapping previously locked for CPU access. More...
 

Static Public Member Functions

static bool IsAvailableOnPlatform ()
 
static std::optional< AHardwareBuffer_Desc > Describe (AHardwareBuffer *buffer)
 
static std::optional< uint64_t > GetSystemUniqueID (AHardwareBuffer *buffer)
 Get the system wide unique ID of the hardware buffer if possible. This is only available on Android API 31 and above. Within the process, the handle are unique. More...
 

Detailed Description

A wrapper for AHardwareBuffer https://developer.android.com/ndk/reference/group/a-hardware-buffer.

This wrapper creates and owns a handle to a managed hardware buffer. That is, there is no ability to take a reference to an externally created hardware buffer.

This wrapper is only available on Android API 29 and above.

Definition at line 95 of file hardware_buffer.h.

Member Enumeration Documentation

◆ CPUAccessType

Enumerator
kRead 
kWrite 

Definition at line 135 of file hardware_buffer.h.

135  {
136  kRead,
137  kWrite,
138  };

Constructor & Destructor Documentation

◆ HardwareBuffer() [1/2]

impeller::android::HardwareBuffer::HardwareBuffer ( HardwareBufferDescriptor  descriptor)
explicit

Definition at line 57 of file hardware_buffer.cc.

58  : descriptor_(descriptor),
59  android_descriptor_(ToAHardwareBufferDesc(descriptor_)) {
60  if (!descriptor_.IsAllocatable()) {
61  VALIDATION_LOG << "The hardware buffer descriptor is not allocatable.";
62  return;
63  }
64  const auto& proc_table = GetProcTable();
65 
66  AHardwareBuffer* buffer = nullptr;
67  if (auto result =
68  proc_table.AHardwareBuffer_allocate(&android_descriptor_, &buffer);
69  result != 0 || buffer == nullptr) {
70  VALIDATION_LOG << "Could not allocate hardware buffer. Error: " << result;
71  return;
72  }
73  buffer_.reset(buffer);
74  is_valid_ = true;
75 }
const ProcTable & GetProcTable()
Definition: proc_table.cc:12
static AHardwareBuffer_Desc ToAHardwareBufferDesc(const HardwareBufferDescriptor &desc)
bool IsAllocatable() const
If hardware buffers can be created using this descriptor. Allocatable descriptors may still cause fai...
#define VALIDATION_LOG
Definition: validation.h:91

References impeller::android::GetProcTable(), impeller::android::HardwareBufferDescriptor::IsAllocatable(), and VALIDATION_LOG.

◆ ~HardwareBuffer()

impeller::android::HardwareBuffer::~HardwareBuffer ( )
default

◆ HardwareBuffer() [2/2]

impeller::android::HardwareBuffer::HardwareBuffer ( const HardwareBuffer )
delete

Member Function Documentation

◆ Describe()

std::optional< AHardwareBuffer_Desc > impeller::android::HardwareBuffer::Describe ( AHardwareBuffer *  buffer)
static

Definition at line 127 of file hardware_buffer.cc.

128  {
129  if (!buffer || !GetProcTable().AHardwareBuffer_describe) {
130  return std::nullopt;
131  }
132  AHardwareBuffer_Desc desc = {};
133  GetProcTable().AHardwareBuffer_describe(buffer, &desc);
134  return desc;
135 }

References impeller::android::GetProcTable().

Referenced by impeller::android::testing::TEST().

◆ GetAndroidDescriptor()

const AHardwareBuffer_Desc & impeller::android::HardwareBuffer::GetAndroidDescriptor ( ) const

Definition at line 103 of file hardware_buffer.cc.

103  {
104  return android_descriptor_;
105 }

◆ GetDescriptor()

const HardwareBufferDescriptor & impeller::android::HardwareBuffer::GetDescriptor ( ) const

Definition at line 99 of file hardware_buffer.cc.

99  {
100  return descriptor_;
101 }

◆ GetHandle()

AHardwareBuffer * impeller::android::HardwareBuffer::GetHandle ( ) const

◆ GetSystemUniqueID() [1/2]

std::optional< uint64_t > impeller::android::HardwareBuffer::GetSystemUniqueID ( ) const

Get the system wide unique ID of the hardware buffer if possible. This is only available on Android API 31 and above. Within the process, the handle are unique.

Returns
The system unique id if one can be obtained.

Definition at line 111 of file hardware_buffer.cc.

111  {
112  return GetSystemUniqueID(GetHandle());
113 }
AHardwareBuffer * GetHandle() const
std::optional< uint64_t > GetSystemUniqueID() const
Get the system wide unique ID of the hardware buffer if possible. This is only available on Android A...

References GetHandle().

Referenced by impeller::android::testing::TEST().

◆ GetSystemUniqueID() [2/2]

std::optional< uint64_t > impeller::android::HardwareBuffer::GetSystemUniqueID ( AHardwareBuffer *  buffer)
static

Get the system wide unique ID of the hardware buffer if possible. This is only available on Android API 31 and above. Within the process, the handle are unique.

Returns
The system unique id if one can be obtained.

Definition at line 115 of file hardware_buffer.cc.

116  {
117  if (!GetProcTable().AHardwareBuffer_getId) {
118  return std::nullopt;
119  }
120  uint64_t out_id = 0u;
121  if (GetProcTable().AHardwareBuffer_getId(buffer, &out_id) != 0) {
122  return std::nullopt;
123  }
124  return out_id;
125 }

References impeller::android::GetProcTable().

◆ IsAvailableOnPlatform()

bool impeller::android::HardwareBuffer::IsAvailableOnPlatform ( )
static

Definition at line 107 of file hardware_buffer.cc.

107  {
108  return GetProcTable().IsValid() && GetProcTable().AHardwareBuffer_isSupported;
109 }
bool IsValid() const
If a valid proc table could be setup. This may fail in case of setup on non-Android platforms.
Definition: proc_table.cc:65

References impeller::android::GetProcTable(), and impeller::android::ProcTable::IsValid().

Referenced by impeller::AHBSwapchainVK::IsAvailableOnPlatform(), and impeller::android::testing::TEST().

◆ IsValid()

bool impeller::android::HardwareBuffer::IsValid ( ) const

Definition at line 79 of file hardware_buffer.cc.

79  {
80  return is_valid_;
81 }

Referenced by impeller::android::testing::TEST().

◆ Lock()

void * impeller::android::HardwareBuffer::Lock ( CPUAccessType  type) const

Lock the buffer for CPU access. This call may fail if the buffer was not created with one the usages that allow for CPU access.

Parameters
[in]typeThe type
Returns
A host-accessible buffer if there was no error related to usage or buffer validity.

Definition at line 137 of file hardware_buffer.cc.

137  {
138  if (!is_valid_ || !GetProcTable().AHardwareBuffer_lock) {
139  return nullptr;
140  }
141  uint64_t usage = 0;
142  switch (type) {
144  usage |= AHARDWAREBUFFER_USAGE_CPU_READ_MASK;
145  break;
147  usage |= AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK;
148  break;
149  }
150  void* buffer = nullptr;
151  const auto result = GetProcTable().AHardwareBuffer_lock( //
152  buffer_.get(), // buffer
153  usage, // usage
154  -1, // fence
155  nullptr, // rect
156  &buffer // out-addr
157  );
158  return result == 0 ? buffer : nullptr;
159 }
GLenum type

References impeller::android::GetProcTable(), kRead, kWrite, and type.

◆ operator=()

HardwareBuffer& impeller::android::HardwareBuffer::operator= ( const HardwareBuffer )
delete

◆ Unlock()

bool impeller::android::HardwareBuffer::Unlock ( ) const

Unlock a mapping previously locked for CPU access.

Returns
If the unlock was successful.

Definition at line 161 of file hardware_buffer.cc.

161  {
162  if (!is_valid_ || !GetProcTable().AHardwareBuffer_unlock) {
163  return false;
164  }
165  const auto result =
166  GetProcTable().AHardwareBuffer_unlock(buffer_.get(), nullptr);
167  return result == 0;
168 }

References impeller::android::GetProcTable().


The documentation for this class was generated from the following files: