Chong Zhang | 3fd200f | 2016-10-07 17:25:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "OMXBuffer" |
| 19 | |
| 20 | #include <media/MediaCodecBuffer.h> |
| 21 | #include <media/OMXBuffer.h> |
| 22 | #include <binder/IMemory.h> |
| 23 | #include <binder/Parcel.h> |
| 24 | #include <ui/GraphicBuffer.h> |
| 25 | #include <utils/NativeHandle.h> |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | //static |
| 30 | OMXBuffer OMXBuffer::sPreset(static_cast<sp<MediaCodecBuffer> >(NULL)); |
| 31 | |
| 32 | OMXBuffer::OMXBuffer() |
| 33 | : mBufferType(kBufferTypeInvalid) { |
| 34 | } |
| 35 | |
| 36 | OMXBuffer::OMXBuffer(const sp<MediaCodecBuffer>& codecBuffer) |
| 37 | : mBufferType(kBufferTypePreset), |
Chong Zhang | 49b2b4d | 2017-01-11 17:19:42 -0800 | [diff] [blame^] | 38 | mRangeOffset(codecBuffer != NULL ? codecBuffer->offset() : 0), |
Chong Zhang | 3fd200f | 2016-10-07 17:25:58 -0700 | [diff] [blame] | 39 | mRangeLength(codecBuffer != NULL ? codecBuffer->size() : 0) { |
| 40 | } |
| 41 | |
Chong Zhang | d02c086 | 2016-10-13 14:32:32 -0700 | [diff] [blame] | 42 | OMXBuffer::OMXBuffer(const sp<IMemory> &mem) |
Chong Zhang | 3fd200f | 2016-10-07 17:25:58 -0700 | [diff] [blame] | 43 | : mBufferType(kBufferTypeSharedMem), |
Chong Zhang | d02c086 | 2016-10-13 14:32:32 -0700 | [diff] [blame] | 44 | mMem(mem) { |
Chong Zhang | 3fd200f | 2016-10-07 17:25:58 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | OMXBuffer::OMXBuffer(const sp<GraphicBuffer> &gbuf) |
| 48 | : mBufferType(kBufferTypeANWBuffer), |
| 49 | mGraphicBuffer(gbuf) { |
| 50 | } |
| 51 | |
| 52 | OMXBuffer::OMXBuffer(const sp<NativeHandle> &handle) |
| 53 | : mBufferType(kBufferTypeNativeHandle), |
| 54 | mNativeHandle(handle) { |
| 55 | } |
| 56 | |
| 57 | OMXBuffer::~OMXBuffer() { |
| 58 | } |
| 59 | |
| 60 | status_t OMXBuffer::writeToParcel(Parcel *parcel) const { |
| 61 | parcel->writeInt32(mBufferType); |
| 62 | |
| 63 | switch(mBufferType) { |
| 64 | case kBufferTypePreset: |
| 65 | { |
Chong Zhang | 49b2b4d | 2017-01-11 17:19:42 -0800 | [diff] [blame^] | 66 | status_t err = parcel->writeUint32(mRangeOffset); |
| 67 | if (err != OK) { |
| 68 | return err; |
| 69 | } |
Chong Zhang | 3fd200f | 2016-10-07 17:25:58 -0700 | [diff] [blame] | 70 | return parcel->writeUint32(mRangeLength); |
| 71 | } |
| 72 | |
| 73 | case kBufferTypeSharedMem: |
| 74 | { |
Chong Zhang | d02c086 | 2016-10-13 14:32:32 -0700 | [diff] [blame] | 75 | return parcel->writeStrongBinder(IInterface::asBinder(mMem)); |
Chong Zhang | 3fd200f | 2016-10-07 17:25:58 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | case kBufferTypeANWBuffer: |
| 79 | { |
| 80 | return parcel->write(*mGraphicBuffer); |
| 81 | } |
| 82 | |
| 83 | case kBufferTypeNativeHandle: |
| 84 | { |
| 85 | return parcel->writeNativeHandle(mNativeHandle->handle()); |
| 86 | } |
| 87 | |
| 88 | default: |
| 89 | return BAD_VALUE; |
| 90 | } |
| 91 | return BAD_VALUE; |
| 92 | } |
| 93 | |
| 94 | status_t OMXBuffer::readFromParcel(const Parcel *parcel) { |
| 95 | BufferType bufferType = (BufferType) parcel->readInt32(); |
| 96 | |
| 97 | switch(bufferType) { |
| 98 | case kBufferTypePreset: |
| 99 | { |
Chong Zhang | 49b2b4d | 2017-01-11 17:19:42 -0800 | [diff] [blame^] | 100 | status_t err = parcel->readUint32(&mRangeOffset); |
| 101 | if (err != OK) { |
| 102 | return err; |
| 103 | } |
| 104 | err = parcel->readUint32(&mRangeLength); |
| 105 | if (err != OK) { |
| 106 | return err; |
| 107 | } |
Chong Zhang | 3fd200f | 2016-10-07 17:25:58 -0700 | [diff] [blame] | 108 | break; |
| 109 | } |
| 110 | |
| 111 | case kBufferTypeSharedMem: |
| 112 | { |
Chong Zhang | d02c086 | 2016-10-13 14:32:32 -0700 | [diff] [blame] | 113 | mMem = interface_cast<IMemory>(parcel->readStrongBinder()); |
Chong Zhang | 3fd200f | 2016-10-07 17:25:58 -0700 | [diff] [blame] | 114 | break; |
| 115 | } |
| 116 | |
| 117 | case kBufferTypeANWBuffer: |
| 118 | { |
| 119 | sp<GraphicBuffer> buffer = new GraphicBuffer(); |
| 120 | |
| 121 | status_t err = parcel->read(*buffer); |
| 122 | |
| 123 | if (err != OK) { |
| 124 | return err; |
| 125 | } |
| 126 | |
| 127 | mGraphicBuffer = buffer; |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | case kBufferTypeNativeHandle: |
| 132 | { |
| 133 | sp<NativeHandle> handle = NativeHandle::create( |
| 134 | parcel->readNativeHandle(), true /* ownsHandle */); |
| 135 | |
| 136 | mNativeHandle = handle; |
| 137 | break; |
| 138 | } |
| 139 | |
| 140 | default: |
| 141 | return BAD_VALUE; |
| 142 | } |
| 143 | |
| 144 | mBufferType = bufferType; |
| 145 | return OK; |
| 146 | } |
| 147 | |
Pawin Vongmasa | 517b0e0 | 2016-12-02 05:15:17 -0800 | [diff] [blame] | 148 | OMXBuffer& OMXBuffer::operator=(OMXBuffer&& source) { |
| 149 | mBufferType = std::move(source.mBufferType); |
| 150 | mRangeLength = std::move(source.mRangeLength); |
| 151 | mMem = std::move(source.mMem); |
| 152 | mGraphicBuffer = std::move(source.mGraphicBuffer); |
| 153 | mNativeHandle = std::move(source.mNativeHandle); |
| 154 | return *this; |
| 155 | } |
| 156 | |
Chong Zhang | 3fd200f | 2016-10-07 17:25:58 -0700 | [diff] [blame] | 157 | } // namespace android |
| 158 | |
| 159 | |
| 160 | |
| 161 | |