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/stagefright/foundation/ADebug.h>
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
OMXBuffer()33 OMXBuffer::OMXBuffer()
34 : mBufferType(kBufferTypeInvalid) {
35 }
36
OMXBuffer(const sp<MediaCodecBuffer> & codecBuffer)37 OMXBuffer::OMXBuffer(const sp<MediaCodecBuffer>& codecBuffer)
38 : mBufferType(kBufferTypePreset),
39 mRangeOffset(codecBuffer != NULL ? codecBuffer->offset() : 0),
40 mRangeLength(codecBuffer != NULL ? codecBuffer->size() : 0) {
41 }
42
OMXBuffer(OMX_U32 rangeOffset,OMX_U32 rangeLength)43 OMXBuffer::OMXBuffer(OMX_U32 rangeOffset, OMX_U32 rangeLength)
44 : mBufferType(kBufferTypePreset),
45 mRangeOffset(rangeOffset),
46 mRangeLength(rangeLength) {
47 }
48
OMXBuffer(const sp<IMemory> & mem)49 OMXBuffer::OMXBuffer(const sp<IMemory> &mem)
50 : mBufferType(kBufferTypeSharedMem),
51 mMem(mem) {
52 }
53
OMXBuffer(const sp<GraphicBuffer> & gbuf)54 OMXBuffer::OMXBuffer(const sp<GraphicBuffer> &gbuf)
55 : mBufferType(kBufferTypeANWBuffer),
56 mGraphicBuffer(gbuf) {
57 }
58
OMXBuffer(const sp<NativeHandle> & handle)59 OMXBuffer::OMXBuffer(const sp<NativeHandle> &handle)
60 : mBufferType(kBufferTypeNativeHandle),
61 mNativeHandle(handle) {
62 }
63
OMXBuffer(const hidl_memory & hidlMemory)64 OMXBuffer::OMXBuffer(const hidl_memory &hidlMemory)
65 : mBufferType(kBufferTypeHidlMemory),
66 mHidlMemory(hidlMemory) {
67 }
68
~OMXBuffer()69 OMXBuffer::~OMXBuffer() {
70 }
71
writeToParcel(Parcel * parcel) const72 status_t OMXBuffer::writeToParcel(Parcel *parcel) const {
73 CHECK(mBufferType != kBufferTypeHidlMemory);
74 parcel->writeInt32(mBufferType);
75
76 switch(mBufferType) {
77 case kBufferTypePreset:
78 {
79 status_t err = parcel->writeUint32(mRangeOffset);
80 if (err != OK) {
81 return err;
82 }
83 return parcel->writeUint32(mRangeLength);
84 }
85
86 case kBufferTypeSharedMem:
87 {
88 return parcel->writeStrongBinder(IInterface::asBinder(mMem));
89 }
90
91 case kBufferTypeANWBuffer:
92 {
93 if (mGraphicBuffer == NULL) {
94 return parcel->writeBool(false);
95 }
96 status_t err = parcel->writeBool(true);
97 if (err != OK) {
98 return err;
99 }
100 return parcel->write(*mGraphicBuffer);
101 }
102
103 case kBufferTypeNativeHandle:
104 {
105 return parcel->writeNativeHandle(mNativeHandle->handle());
106 }
107
108 default:
109 return BAD_VALUE;
110 }
111 return BAD_VALUE;
112 }
113
readFromParcel(const Parcel * parcel)114 status_t OMXBuffer::readFromParcel(const Parcel *parcel) {
115 BufferType bufferType = (BufferType) parcel->readInt32();
116 CHECK(bufferType != kBufferTypeHidlMemory);
117
118 switch(bufferType) {
119 case kBufferTypePreset:
120 {
121 status_t err = parcel->readUint32(&mRangeOffset);
122 if (err != OK) {
123 return err;
124 }
125 err = parcel->readUint32(&mRangeLength);
126 if (err != OK) {
127 return err;
128 }
129 break;
130 }
131
132 case kBufferTypeSharedMem:
133 {
134 mMem = interface_cast<IMemory>(parcel->readStrongBinder());
135 break;
136 }
137
138 case kBufferTypeANWBuffer:
139 {
140 bool notNull;
141 status_t err = parcel->readBool(¬Null);
142 if (err != OK) {
143 return err;
144 }
145 if (notNull) {
146 sp<GraphicBuffer> buffer = new GraphicBuffer();
147 status_t err = parcel->read(*buffer);
148 if (err != OK) {
149 return err;
150 }
151 mGraphicBuffer = buffer;
152 } else {
153 mGraphicBuffer = nullptr;
154 }
155 break;
156 }
157
158 case kBufferTypeNativeHandle:
159 {
160 sp<NativeHandle> handle = NativeHandle::create(
161 parcel->readNativeHandle(), true /* ownsHandle */);
162
163 mNativeHandle = handle;
164 break;
165 }
166
167 default:
168 return BAD_VALUE;
169 }
170
171 mBufferType = bufferType;
172 return OK;
173 }
174
operator =(OMXBuffer && source)175 OMXBuffer& OMXBuffer::operator=(OMXBuffer&& source) noexcept {
176 mBufferType = std::move(source.mBufferType);
177 mRangeOffset = std::move(source.mRangeOffset);
178 mRangeLength = std::move(source.mRangeLength);
179 mMem = std::move(source.mMem);
180 mGraphicBuffer = std::move(source.mGraphicBuffer);
181 mNativeHandle = std::move(source.mNativeHandle);
182 mHidlMemory = std::move(source.mHidlMemory);
183 return *this;
184 }
185
186 } // namespace android
187
188
189
190
191