Flutter Engine
The Flutter Engine
hardware_buffer.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
8
9namespace impeller::android {
10
11static AHardwareBuffer_Format ToAHardwareBufferFormat(
13 switch (format) {
15 return AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
16 }
18}
19
20static AHardwareBuffer_Desc ToAHardwareBufferDesc(
22 AHardwareBuffer_Desc ahb_desc = {};
23 ahb_desc.width = desc.size.width;
24 ahb_desc.height = desc.size.height;
25 ahb_desc.format = ToAHardwareBufferFormat(desc.format);
26 ahb_desc.layers = 1u;
28 ahb_desc.usage |= (AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER |
29 AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT);
30 }
32 ahb_desc.usage |= AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY;
33 }
35 ahb_desc.usage |= AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
36 }
38 ahb_desc.usage |= AHARDWAREBUFFER_USAGE_CPU_READ_RARELY;
39 }
41 ahb_desc.usage |= AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN;
42 }
44 ahb_desc.usage |= AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY;
45 }
47 ahb_desc.usage |= AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
48 }
49 return ahb_desc;
50}
51
53 const auto desc = ToAHardwareBufferDesc(*this);
54 return GetProcTable().AHardwareBuffer_isSupported(&desc) != 0u;
55}
56
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}
76
78
80 return is_valid_;
81}
82
84 return buffer_.get();
85}
86
88 const ISize& size) {
91 // Zero sized hardware buffers cannot be allocated.
92 desc.size = size.Max(ISize{1u, 1u});
96 return desc;
97}
98
100 return descriptor_;
101}
102
103const AHardwareBuffer_Desc& HardwareBuffer::GetAndroidDescriptor() const {
104 return android_descriptor_;
105}
106
108 return GetProcTable().IsValid() && GetProcTable().AHardwareBuffer_isSupported;
109}
110
111std::optional<uint64_t> HardwareBuffer::GetSystemUniqueID() const {
113}
114
115std::optional<uint64_t> HardwareBuffer::GetSystemUniqueID(
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}
126
127std::optional<AHardwareBuffer_Desc> HardwareBuffer::Describe(
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}
136
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}
160
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}
169
170} // namespace impeller::android
struct AHardwareBuffer AHardwareBuffer
GLenum type
void reset(const T &value=Traits::InvalidValue())
Definition: unique_object.h:62
const T & get() const
Definition: unique_object.h:87
HardwareBuffer(HardwareBufferDescriptor descriptor)
const HardwareBufferDescriptor & GetDescriptor() const
AHardwareBuffer * GetHandle() const
const AHardwareBuffer_Desc & GetAndroidDescriptor() const
bool Unlock() const
Unlock a mapping previously locked for CPU access.
static std::optional< AHardwareBuffer_Desc > Describe(AHardwareBuffer *buffer)
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 ...
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...
GAsyncResult * result
uint32_t uint32_t * format
#define FML_UNREACHABLE()
Definition: logging.h:109
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
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
const ProcTable & GetProcTable()
Definition: proc_table.cc:12
static AHardwareBuffer_Desc ToAHardwareBufferDesc(const HardwareBufferDescriptor &desc)
static AHardwareBuffer_Format ToAHardwareBufferFormat(HardwareBufferFormat format)
static void usage(char *argv0)
constexpr TSize Max(const TSize &o) const
Definition: size.h:81
A descriptor use to specify hardware buffer allocations.
static HardwareBufferDescriptor MakeForSwapchainImage(const ISize &size)
Create a descriptor of the given size that is suitable for use as a swapchain image.
bool IsAllocatable() const
If hardware buffers can be created using this descriptor. Allocatable descriptors may still cause fai...
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
#define VALIDATION_LOG
Definition: validation.h:73