summaryrefslogtreecommitdiff
path: root/platform/sysroot/usr/include/android/native_window_aidl.h
blob: a252245a10aecb94fd38353f8dd6f00b213cfb5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file native_window_aidl.h
 * @brief NativeWindow NDK AIDL glue code
 */

/**
 * @addtogroup ANativeWindow
 *
 * Parcelable support for ANativeWindow. Can be used with libbinder_ndk
 *
 * @{
 */

#ifndef ANDROID_NATIVE_WINDOW_AIDL_H
#define ANDROID_NATIVE_WINDOW_AIDL_H

#include <android/binder_parcel.h>
#include <android/native_window.h>
#include <sys/cdefs.h>

__BEGIN_DECLS

/**
 * Read an ANativeWindow from a AParcel. The output buffer will have an
 * initial reference acquired and will need to be released with
 * ANativeWindow_release.
 *
 * Available since API level 34.
 *
 * \return STATUS_OK on success
 *         STATUS_BAD_VALUE if the parcel or outBuffer is null, or if there's an
 *                          issue deserializing (eg, corrupted parcel)
 *         STATUS_BAD_TYPE if the parcel's current data position is not that of
 *                         an ANativeWindow type
 *         STATUS_NO_MEMORY if an allocation fails
 */
binder_status_t ANativeWindow_readFromParcel(const AParcel* _Nonnull parcel,
        ANativeWindow* _Nullable* _Nonnull outWindow) __INTRODUCED_IN(__ANDROID_API_U__);

/**
 * Write an ANativeWindow to an AParcel.
 *
 * Available since API level 34.
 *
 * \return STATUS_OK on success.
 *         STATUS_BAD_VALUE if either buffer or parcel is null, or if the ANativeWindow*
 *                          fails to serialize (eg, internally corrupted)
 *         STATUS_NO_MEMORY if the parcel runs out of space to store the buffer & is
 *                          unable to allocate more
 *         STATUS_FDS_NOT_ALLOWED if the parcel does not allow storing FDs
 */
binder_status_t ANativeWindow_writeToParcel(ANativeWindow* _Nonnull window,
        AParcel* _Nonnull parcel) __INTRODUCED_IN(__ANDROID_API_U__);

__END_DECLS

// Only enable the AIDL glue helper if this is C++
#ifdef __cplusplus

namespace aidl::android::hardware {

/**
 * Wrapper class that enables interop with AIDL NDK generation
 * 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 {
public:
    NativeWindow() noexcept {}
    explicit NativeWindow(ANativeWindow* _Nullable window) {
        reset(window);
    }

    explicit NativeWindow(NativeWindow&& other) noexcept {
        mWindow = other.release(); // steal ownership from r-value
    }

    ~NativeWindow() {
        reset();
    }

    binder_status_t readFromParcel(const AParcel* _Nonnull parcel) {
        reset();
        return ANativeWindow_readFromParcel(parcel, &mWindow);
    }

    binder_status_t writeToParcel(AParcel* _Nonnull parcel) const {
        if (!mWindow) {
            return STATUS_BAD_VALUE;
        }
        return ANativeWindow_writeToParcel(mWindow, parcel);
    }

    /**
     * Destroys any currently owned ANativeWindow* and takes ownership of the given
     * ANativeWindow*
     *
     * @param buffer The buffer to take ownership of
     */
    void reset(ANativeWindow* _Nullable window = nullptr) noexcept {
        if (mWindow) {
            ANativeWindow_release(mWindow);
            mWindow = nullptr;
        }
        if (window != nullptr) {
            ANativeWindow_acquire(window);
        }
        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;
    }

    /**
     * Stops managing any contained ANativeWindow*, returning it to the caller. Ownership
     * is released.
     * @return ANativeWindow* or null if this was empty
     */
    [[nodiscard]] ANativeWindow* _Nullable release() noexcept {
        ANativeWindow* _Nullable ret = mWindow;
        mWindow = nullptr;
        return ret;
    }
private:
    ANativeWindow* _Nullable mWindow = nullptr;
    NativeWindow(const NativeWindow &other) = delete;
    NativeWindow& operator=(const NativeWindow &other) = delete;
};

} // aidl::android::hardware
  //
namespace aidl::android::view {
    using Surface = aidl::android::hardware::NativeWindow;
}

#endif // __cplusplus

#endif // ANDROID_NATIVE_WINDOW_AIDL_H

/** @} */