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