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), |
| 38 | mRangeLength(codecBuffer != NULL ? codecBuffer->size() : 0) { |
| 39 | } |
| 40 | |
| 41 | OMXBuffer::OMXBuffer(const sp<IMemory> &mem, size_t allottedSize) |
| 42 | : mBufferType(kBufferTypeSharedMem), |
| 43 | mMem(mem), |
| 44 | mAllottedSize(allottedSize ? : mem->size()) { |
| 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 | { |
| 66 | return parcel->writeUint32(mRangeLength); |
| 67 | } |
| 68 | |
| 69 | case kBufferTypeSharedMem: |
| 70 | { |
| 71 | status_t err = parcel->writeStrongBinder(IInterface::asBinder(mMem)); |
| 72 | if (err != NO_ERROR) { |
| 73 | return err; |
| 74 | } |
| 75 | return parcel->writeUint32(mAllottedSize); |
| 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 | { |
| 100 | mRangeLength = parcel->readUint32(); |
| 101 | break; |
| 102 | } |
| 103 | |
| 104 | case kBufferTypeSharedMem: |
| 105 | { |
| 106 | sp<IMemory> params = interface_cast<IMemory>(parcel->readStrongBinder()); |
| 107 | |
| 108 | mMem = params; |
| 109 | mAllottedSize = parcel->readUint32(); |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | case kBufferTypeANWBuffer: |
| 114 | { |
| 115 | sp<GraphicBuffer> buffer = new GraphicBuffer(); |
| 116 | |
| 117 | status_t err = parcel->read(*buffer); |
| 118 | |
| 119 | if (err != OK) { |
| 120 | return err; |
| 121 | } |
| 122 | |
| 123 | mGraphicBuffer = buffer; |
| 124 | break; |
| 125 | } |
| 126 | |
| 127 | case kBufferTypeNativeHandle: |
| 128 | { |
| 129 | sp<NativeHandle> handle = NativeHandle::create( |
| 130 | parcel->readNativeHandle(), true /* ownsHandle */); |
| 131 | |
| 132 | mNativeHandle = handle; |
| 133 | break; |
| 134 | } |
| 135 | |
| 136 | default: |
| 137 | return BAD_VALUE; |
| 138 | } |
| 139 | |
| 140 | mBufferType = bufferType; |
| 141 | return OK; |
| 142 | } |
| 143 | |
| 144 | } // namespace android |
| 145 | |
| 146 | |
| 147 | |
| 148 | |