NativeWindow: implement functions expected by AIDL
ANativeWindow is now parcelable over stable interfaces by being
wrapped in NativeWindow or Surface. The readFromParcel and
writeToParcel functions are implemented for this class, but
AIDL also expects some comparators and a toString function
to be implemented for custom parcelables.
This CL implements the comparison operators, and the toString
function to allow NativeWindow/Surface to be used with AIDL
interfaces and Parcelables. The operations simply
compare/print the ANativeWindow pointer that is being managed
by the object.
Bug: 283283111
Test: Verified that Surface can be used in an AIDL file.
Change-Id: I28ceded9ee1358884a6b1214269bb04a90749fc0
diff --git a/libs/nativewindow/include/android/native_window_aidl.h b/libs/nativewindow/include/android/native_window_aidl.h
index a252245..adc1bf1 100644
--- a/libs/nativewindow/include/android/native_window_aidl.h
+++ b/libs/nativewindow/include/android/native_window_aidl.h
@@ -34,6 +34,9 @@
#include <android/native_window.h>
#include <sys/cdefs.h>
+#include <sstream>
+#include <string>
+
__BEGIN_DECLS
/**
@@ -80,7 +83,7 @@
* Takes ownership of the ANativeWindow* given to it in reset() and will automatically
* destroy it in the destructor, similar to a smart pointer container
*/
-class NativeWindow {
+class NativeWindow final {
public:
NativeWindow() noexcept {}
explicit NativeWindow(ANativeWindow* _Nullable window) {
@@ -123,15 +126,29 @@
}
mWindow = window;
}
- inline ANativeWindow* _Nullable operator-> () const { return mWindow; }
+
inline ANativeWindow* _Nullable get() const { return mWindow; }
- inline explicit operator bool () const { return mWindow != nullptr; }
NativeWindow& operator=(NativeWindow&& other) noexcept {
mWindow = other.release(); // steal ownership from r-value
return *this;
}
+ inline ANativeWindow* _Nullable operator->() const { return mWindow; }
+ inline explicit operator bool() const { return mWindow != nullptr; }
+ inline bool operator==(const NativeWindow& rhs) const { return mWindow == rhs.mWindow; }
+ inline bool operator!=(const NativeWindow& rhs) const { return !(*this == rhs); }
+ inline bool operator<(const NativeWindow& rhs) const { return mWindow < rhs.mWindow; }
+ inline bool operator>(const NativeWindow& rhs) const { return rhs < *this; }
+ inline bool operator>=(const NativeWindow& rhs) const { return !(*this < rhs); }
+ inline bool operator<=(const NativeWindow& rhs) const { return !(*this > rhs); }
+
+ std::string toString() const {
+ std::ostringstream ss;
+ ss << "NativeWindow: " << mWindow;
+ return ss.str();
+ }
+
/**
* Stops managing any contained ANativeWindow*, returning it to the caller. Ownership
* is released.