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