| Flutter Impeller |

proc_table.h
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
5 #ifndef FLUTTER_IMPELLER_TOOLKIT_ANDROID_PROC_TABLE_H_
6 #define FLUTTER_IMPELLER_TOOLKIT_ANDROID_PROC_TABLE_H_
7
8 #include <EGL/egl.h>
9 #define EGL_EGLEXT_PROTOTYPES
10 #include <EGL/eglext.h>
11 #include <android/api-level.h>
12 #include <android/hardware_buffer.h>
13 #include <android/hardware_buffer_jni.h>
14 #include <android/surface_control.h>
15 #include <android/trace.h>
16
17 #include <functional>
18
19 #include "flutter/fml/logging.h"
20 #include "flutter/fml/native_library.h"
21
22 namespace impeller::android {
23
24 //------------------------------------------------------------------------------
25 /// @brief The Android procs along with the device API level on which these
26 /// will be available. There is no checking of the actual API level
27 /// however (because getting the API level is itself only possible
28 /// on API levels 29 and above).
29 ///
30 /// Take care to explicitly check for the availability of these APIs
31 /// at runtime before invoking them.
32 ///
33 /// Typically, you'll never have to deal with the proc. table
34 /// directly. Instead, rely on the handle wrappers (`Choreographer`,
35 /// `HardwareBuffer`, etc..).
36 ///
37 #define FOR_EACH_ANDROID_PROC(INVOKE) \
38 INVOKE(AChoreographer_getInstance, 24) \
39 INVOKE(AChoreographer_postFrameCallback, 24) \
40 INVOKE(AChoreographer_postFrameCallback64, 29) \
41 INVOKE(AHardwareBuffer_acquire, 26) \
42 INVOKE(AHardwareBuffer_allocate, 26) \
43 INVOKE(AHardwareBuffer_describe, 26) \
44 INVOKE(AHardwareBuffer_fromHardwareBuffer, 26) \
45 INVOKE(AHardwareBuffer_getId, 31) \
46 INVOKE(AHardwareBuffer_isSupported, 29) \
47 INVOKE(AHardwareBuffer_lock, 26) \
48 INVOKE(AHardwareBuffer_release, 26) \
49 INVOKE(AHardwareBuffer_unlock, 26) \
50 INVOKE(ANativeWindow_acquire, 0) \
51 INVOKE(ANativeWindow_getHeight, 0) \
52 INVOKE(ANativeWindow_getWidth, 0) \
53 INVOKE(ANativeWindow_release, 0) \
54 INVOKE(ASurfaceControl_createFromWindow, 29) \
55 INVOKE(ASurfaceControl_release, 29) \
56 INVOKE(ASurfaceTransaction_apply, 29) \
57 INVOKE(ASurfaceTransaction_create, 29) \
58 INVOKE(ASurfaceTransaction_delete, 29) \
59 INVOKE(ASurfaceTransaction_reparent, 29) \
60 INVOKE(ASurfaceTransaction_setBuffer, 29) \
61 INVOKE(ASurfaceTransaction_setColor, 29) \
62 INVOKE(ASurfaceTransaction_setOnComplete, 29) \
63 INVOKE(ASurfaceTransactionStats_getPreviousReleaseFenceFd, 29) \
64 INVOKE(ATrace_isEnabled, 23) \
65 INVOKE(eglGetNativeClientBufferANDROID, 0)
66
67 template <class T>
68 struct AndroidProc {
69 using AndroidProcType = T;
70
71 const char* proc_name = nullptr;
72
73 AndroidProcType* proc = nullptr;
74
75 constexpr bool IsAvailable() const { return proc != nullptr; }
76
77 explicit constexpr operator bool() const { return IsAvailable(); }
78
79 template <class... Args>
80 auto operator()(Args&&... args) const {
81 FML_DCHECK(IsAvailable())
82 << "Android method " << proc_name
83 << " is not available on this device. Missing check.";
84 return proc(std::forward<Args>(args)...);
85 }
86
87 void Reset() { proc = nullptr; }
88 };
89
90 //------------------------------------------------------------------------------
91 /// @brief The table of Android procs that are resolved dynamically.
92 ///
93 struct ProcTable {
94 ProcTable();
95
96 ~ProcTable();
97
98 ProcTable(const ProcTable&) = delete;
99
100 ProcTable& operator=(const ProcTable&) = delete;
101
102 //----------------------------------------------------------------------------
103 /// @brief If a valid proc table could be setup. This may fail in case of
104 /// setup on non-Android platforms.
105 ///
106 /// @return `true` if valid.
107 ///
108 bool IsValid() const;
109
110 //----------------------------------------------------------------------------
111 /// @brief Check if tracing in enabled in the process. This call can be
112 /// made at any API level.
113 ///
114 /// @return If tracing is enabled.
115 ///
116 bool TraceIsEnabled() const;
117
118 #define DEFINE_PROC(name, api) \
119 AndroidProc<decltype(name)> name = {.proc_name = #name};
120 FOR_EACH_ANDROID_PROC(DEFINE_PROC);
121 #undef DEFINE_PROC
122
123 private:
124 std::vector<fml::RefPtr<fml::NativeLibrary>> libraries_;
125 bool is_valid_ = false;
126 };
127
128 const ProcTable& GetProcTable();
129
130 #ifdef TESTING
131 ProcTable& GetMutableProcTable();
132 #endif
133
134 } // namespace impeller::android
135
136 #endif // FLUTTER_IMPELLER_TOOLKIT_ANDROID_PROC_TABLE_H_
DEFINE_PROC
#define DEFINE_PROC(name, api)
Definition: proc_table.h:118
hardware_buffer.h
impeller::android
Definition: choreographer.cc:9
impeller::android::GetProcTable
const ProcTable & GetProcTable()
Definition: proc_table.cc:12
impeller::android::GetMutableProcTable
ProcTable & GetMutableProcTable()
Definition: proc_table.cc:18
impeller::android::AndroidProc
Definition: proc_table.h:68
impeller::android::AndroidProc::Reset
void Reset()
Definition: proc_table.h:87
impeller::android::AndroidProc::proc_name
const char * proc_name
Definition: proc_table.h:71
impeller::android::AndroidProc::AndroidProcType
T AndroidProcType
Definition: proc_table.h:69
impeller::android::AndroidProc::operator()
auto operator()(Args &&... args) const
Definition: proc_table.h:80
impeller::android::AndroidProc::IsAvailable
constexpr bool IsAvailable() const
Definition: proc_table.h:75
impeller::android::AndroidProc::proc
AndroidProcType * proc
Definition: proc_table.h:73
impeller::android::ProcTable
The table of Android procs that are resolved dynamically.
Definition: proc_table.h:93
impeller::android::ProcTable::~ProcTable
~ProcTable()
impeller::android::ProcTable::ProcTable
ProcTable(const ProcTable &)=delete
impeller::android::ProcTable::FOR_EACH_ANDROID_PROC
FOR_EACH_ANDROID_PROC(DEFINE_PROC)
impeller::android::ProcTable::ProcTable
ProcTable()
Definition: proc_table.cc:34
impeller::android::ProcTable::IsValid
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
impeller::android::ProcTable::TraceIsEnabled
bool TraceIsEnabled() const
Check if tracing in enabled in the process. This call can be made at any API level.
Definition: proc_table.cc:69
impeller::android::ProcTable::operator=
ProcTable & operator=(const ProcTable &)=delete
surface_control.h