Flutter Engine
The Flutter Engine
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
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
 
AHardwareBufferGetHandle () 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}
struct AHardwareBuffer AHardwareBuffer
void reset(const T &value=Traits::InvalidValue())
Definition: unique_object.h:62
GAsyncResult * result
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
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:73

◆ ~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}

◆ 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

Definition at line 83 of file hardware_buffer.cc.

83 {
84 return buffer_.get();
85}
const T & get() const
Definition: unique_object.h:87

◆ 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 {
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...

◆ 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}

◆ 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

◆ IsValid()

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

Definition at line 79 of file hardware_buffer.cc.

79 {
80 return is_valid_;
81}

◆ 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
static void usage(char *argv0)

◆ 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}

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