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 
25 namespace android {
26 
27 class OMXBuffer;
28 
29 // This is needed temporarily for the OMX HIDL transition.
30 namespace hardware { namespace media { namespace omx {
31 namespace V1_0 {
32     struct CodecBuffer;
33 namespace 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 }
39 namespace 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 
47 class GraphicBuffer;
48 class IMemory;
49 class MediaCodecBuffer;
50 class NativeHandle;
51 struct OMXNodeInstance;
52 using hardware::hidl_memory;
53 
54 // TODO: After complete HIDL transition, this class would be replaced by
55 // CodecBuffer.
56 class OMXBuffer {
57 public:
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 
92 private:
93     friend struct OMXNodeInstance;
94     friend struct C2OMXNode;
95 
96     // This is needed temporarily for OMX HIDL transition.
97     friend inline bool (::android::hardware::media::omx::V1_0::implementation::
98             wrapAs)(::android::hardware::media::omx::V1_0::CodecBuffer* t,
99             OMXBuffer const& l);
100     friend inline bool (::android::hardware::media::omx::V1_0::implementation::
101             convertTo)(OMXBuffer* l,
102             ::android::hardware::media::omx::V1_0::CodecBuffer const& t);
103     friend inline bool (::android::hardware::media::omx::V1_0::utils::
104             wrapAs)(::android::hardware::media::omx::V1_0::CodecBuffer* t,
105             OMXBuffer const& l);
106     friend inline bool (::android::hardware::media::omx::V1_0::utils::
107             convertTo)(OMXBuffer* l,
108             ::android::hardware::media::omx::V1_0::CodecBuffer const& t);
109 
110     enum BufferType {
111         kBufferTypeInvalid = 0,
112         kBufferTypePreset,
113         kBufferTypeSharedMem,
114         kBufferTypeANWBuffer, // Use only for non-Treble
115         kBufferTypeNativeHandle,
116         kBufferTypeHidlMemory // Mapped to CodecBuffer::Type::SHARED_MEM.
117     };
118 
119     BufferType mBufferType;
120 
121     // kBufferTypePreset
122     // If the port is operating in byte buffer mode, mRangeLength is the valid
123     // range length. Otherwise the range info should also be ignored.
124     OMX_U32 mRangeOffset;
125     OMX_U32 mRangeLength;
126 
127     // kBufferTypeSharedMem
128     sp<IMemory> mMem;
129 
130     // kBufferTypeANWBuffer
131     sp<GraphicBuffer> mGraphicBuffer;
132 
133     // kBufferTypeNativeHandle
134     sp<NativeHandle> mNativeHandle;
135 
136     // kBufferTypeHidlMemory
137     hidl_memory mHidlMemory;
138 
139     // Move assignment
140     OMXBuffer &operator=(OMXBuffer&&) noexcept;
141 
142     // Deleted copy constructor and assignment.
143     OMXBuffer(const OMXBuffer&) = delete;
144     OMXBuffer& operator=(const OMXBuffer&) = delete;
145 };
146 
147 }  // namespace android
148 
149 #endif  // _OMXBUFFER_H_
150