Chong Zhang | 6d332d2 | 2016-09-07 12:06:50 -0700 | [diff] [blame] | 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 | |
| 22 | namespace android { |
| 23 | |
| 24 | struct OMXFenceParcelable : public Parcelable { |
| 25 | OMXFenceParcelable() : mFenceFd(-1) {} |
| 26 | OMXFenceParcelable(int fenceFd) : mFenceFd(fenceFd) {} |
| 27 | |
| 28 | int get() const { return mFenceFd; } |
| 29 | |
| 30 | status_t readFromParcel(const Parcel* parcel) override; |
| 31 | status_t writeToParcel(Parcel* parcel) const override; |
| 32 | |
| 33 | private: |
| 34 | // Disable copy ctor and operator= |
| 35 | OMXFenceParcelable(const OMXFenceParcelable &); |
| 36 | OMXFenceParcelable &operator=(const OMXFenceParcelable &); |
| 37 | |
| 38 | int mFenceFd; |
| 39 | }; |
| 40 | |
| 41 | inline status_t OMXFenceParcelable::readFromParcel(const Parcel* parcel) { |
| 42 | int32_t haveFence; |
| 43 | status_t err = parcel->readInt32(&haveFence); |
| 44 | if (err == OK && haveFence) { |
| 45 | int fd = ::dup(parcel->readFileDescriptor()); |
| 46 | if (fd < 0) { |
| 47 | return fd; |
| 48 | } |
| 49 | mFenceFd = fd; |
| 50 | } |
| 51 | return err; |
| 52 | } |
| 53 | |
| 54 | inline status_t OMXFenceParcelable::writeToParcel(Parcel* parcel) const { |
| 55 | status_t err = parcel->writeInt32(mFenceFd >= 0); |
| 56 | if (err == OK && mFenceFd >= 0) { |
| 57 | err = parcel->writeFileDescriptor(mFenceFd, true /* takeOwnership */); |
| 58 | } |
| 59 | return err; |
| 60 | } |
| 61 | |
| 62 | } // namespace android |
| 63 | |
| 64 | #endif // _OMX_FENCE_PARCELABLE_ |