1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _OMX_FENCE_PARCELABLE_
18#define _OMX_FENCE_PARCELABLE_
19
20#include <binder/Parcel.h>
21
22namespace android {
23
24struct OMXFenceParcelable;
25
26// This is needed temporarily for the OMX HIDL transition.
27namespace hardware {
28    struct hidl_handle;
29namespace media { namespace omx { namespace V1_0 {
30namespace implementation {
31    void wrapAs(::android::OMXFenceParcelable* l,
32            ::android::hardware::hidl_handle const& t);
33    bool convertTo(::android::OMXFenceParcelable* l,
34            ::android::hardware::hidl_handle const& t);
35}
36namespace utils {
37    void wrapAs(::android::OMXFenceParcelable* l,
38            ::android::hardware::hidl_handle const& t);
39    bool convertTo(::android::OMXFenceParcelable* l,
40            ::android::hardware::hidl_handle const& t);
41}
42}}}}
43
44struct OMXFenceParcelable : public Parcelable {
45    OMXFenceParcelable() : mFenceFd(-1) {}
46    OMXFenceParcelable(int fenceFd) : mFenceFd(fenceFd) {}
47
48    int get() const { return mFenceFd; }
49
50    status_t readFromParcel(const Parcel* parcel) override;
51    status_t writeToParcel(Parcel* parcel) const override;
52
53private:
54    // Disable copy ctor and operator=
55    OMXFenceParcelable(const OMXFenceParcelable &);
56    OMXFenceParcelable &operator=(const OMXFenceParcelable &);
57
58    int mFenceFd;
59
60    // This is needed temporarily for OMX HIDL transition.
61    friend void (::android::hardware::media::omx::V1_0::implementation::
62            wrapAs)(OMXFenceParcelable* l,
63            ::android::hardware::hidl_handle const& t);
64    friend bool (::android::hardware::media::omx::V1_0::implementation::
65            convertTo)(OMXFenceParcelable* l,
66            ::android::hardware::hidl_handle const& t);
67    friend void (::android::hardware::media::omx::V1_0::utils::
68            wrapAs)(OMXFenceParcelable* l,
69            ::android::hardware::hidl_handle const& t);
70    friend bool (::android::hardware::media::omx::V1_0::utils::
71            convertTo)(OMXFenceParcelable* l,
72            ::android::hardware::hidl_handle const& t);
73};
74
75inline status_t OMXFenceParcelable::readFromParcel(const Parcel* parcel) {
76    int32_t haveFence;
77    status_t err = parcel->readInt32(&haveFence);
78    if (err == OK && haveFence) {
79        int fd = ::dup(parcel->readFileDescriptor());
80        if (fd < 0) {
81            return fd;
82        }
83        mFenceFd = fd;
84    }
85    return err;
86}
87
88inline status_t OMXFenceParcelable::writeToParcel(Parcel* parcel) const {
89    status_t err = parcel->writeInt32(mFenceFd >= 0);
90    if (err == OK && mFenceFd >= 0) {
91        err = parcel->writeFileDescriptor(mFenceFd, true /* takeOwnership */);
92    }
93    return err;
94}
95
96} // namespace android
97
98#endif // _OMX_FENCE_PARCELABLE_
99