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#ifndef _OMXBUFFER_H_
18#define _OMXBUFFER_H_
19
20#include <cutils/native_handle.h>
21#include <media/IOMX.h>
22#include <utils/StrongPointer.h>
23#include <hidl/HidlSupport.h>
24
25namespace android {
26
27class OMXBuffer;
28
29// This is needed temporarily for the OMX HIDL transition.
30namespace hardware { namespace media { namespace omx {
31namespace V1_0 {
32    struct CodecBuffer;
33namespace implementation {
34    inline bool wrapAs(::android::hardware::media::omx::V1_0::CodecBuffer* t,
35            ::android::OMXBuffer const& l);
36    inline bool convertTo(::android::OMXBuffer* l,
37            ::android::hardware::media::omx::V1_0::CodecBuffer const& t);
38}
39namespace utils {
40    inline bool wrapAs(::android::hardware::media::omx::V1_0::CodecBuffer* t,
41            ::android::OMXBuffer const& l);
42    inline bool convertTo(::android::OMXBuffer* l,
43            ::android::hardware::media::omx::V1_0::CodecBuffer const& t);
44}
45}}}}
46
47class GraphicBuffer;
48class IMemory;
49class MediaCodecBuffer;
50class NativeHandle;
51struct OMXNodeInstance;
52using hardware::hidl_memory;
53
54// TODO: After complete HIDL transition, this class would be replaced by
55// CodecBuffer.
56class OMXBuffer {
57public:
58    // sPreset is used in places where we are referring to a pre-registered
59    // buffer on a port. It has type kBufferTypePreset and mRangeLength of 0.
60    static OMXBuffer sPreset;
61
62    // Default constructor, constructs a buffer of type kBufferTypeInvalid.
63    OMXBuffer();
64
65    // Constructs a buffer of type kBufferTypePreset with mRangeOffset set to
66    // |codecBuffer|'s offset and mRangeLength set to |codecBuffer|'s size (or 0
67    // if |codecBuffer| is NULL).
68    OMXBuffer(const sp<MediaCodecBuffer> &codecBuffer);
69
70    // Constructs a buffer of type kBufferTypePreset with specified mRangeOffset
71    // and mRangeLength.
72    OMXBuffer(OMX_U32 rangeOffset, OMX_U32 rangeLength);
73
74    // Constructs a buffer of type kBufferTypeSharedMem.
75    OMXBuffer(const sp<IMemory> &mem);
76
77    // Constructs a buffer of type kBufferTypeANWBuffer.
78    OMXBuffer(const sp<GraphicBuffer> &gbuf);
79
80    // Constructs a buffer of type kBufferTypeNativeHandle.
81    OMXBuffer(const sp<NativeHandle> &handle);
82
83    // Constructs a buffer of type kBufferTypeHidlMemory.
84    OMXBuffer(const hidl_memory &hidlMemory);
85
86    // Parcelling/Un-parcelling.
87    status_t writeToParcel(Parcel *parcel) const;
88    status_t readFromParcel(const Parcel *parcel);
89
90    ~OMXBuffer();
91
92private:
93    friend struct OMXNodeInstance;
94
95    // This is needed temporarily for OMX HIDL transition.
96    friend inline bool (::android::hardware::media::omx::V1_0::implementation::
97            wrapAs)(::android::hardware::media::omx::V1_0::CodecBuffer* t,
98            OMXBuffer const& l);
99    friend inline bool (::android::hardware::media::omx::V1_0::implementation::
100            convertTo)(OMXBuffer* l,
101            ::android::hardware::media::omx::V1_0::CodecBuffer const& t);
102    friend inline bool (::android::hardware::media::omx::V1_0::utils::
103            wrapAs)(::android::hardware::media::omx::V1_0::CodecBuffer* t,
104            OMXBuffer const& l);
105    friend inline bool (::android::hardware::media::omx::V1_0::utils::
106            convertTo)(OMXBuffer* l,
107            ::android::hardware::media::omx::V1_0::CodecBuffer const& t);
108
109    enum BufferType {
110        kBufferTypeInvalid = 0,
111        kBufferTypePreset,
112        kBufferTypeSharedMem,
113        kBufferTypeANWBuffer, // Use only for non-Treble
114        kBufferTypeNativeHandle,
115        kBufferTypeHidlMemory // Mapped to CodecBuffer::Type::SHARED_MEM.
116    };
117
118    BufferType mBufferType;
119
120    // kBufferTypePreset
121    // If the port is operating in byte buffer mode, mRangeLength is the valid
122    // range length. Otherwise the range info should also be ignored.
123    OMX_U32 mRangeOffset;
124    OMX_U32 mRangeLength;
125
126    // kBufferTypeSharedMem
127    sp<IMemory> mMem;
128
129    // kBufferTypeANWBuffer
130    sp<GraphicBuffer> mGraphicBuffer;
131
132    // kBufferTypeNativeHandle
133    sp<NativeHandle> mNativeHandle;
134
135    // kBufferTypeHidlMemory
136    hidl_memory mHidlMemory;
137
138    // Move assignment
139    OMXBuffer &operator=(OMXBuffer&&);
140
141    // Deleted copy constructor and assignment.
142    OMXBuffer(const OMXBuffer&) = delete;
143    OMXBuffer& operator=(const OMXBuffer&) = delete;
144};
145
146}  // namespace android
147
148#endif  // _OMXBUFFER_H_
149