1 /*
2  * Copyright (C) 2011 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 GOLDFISH_VPX_H_
18 
19 #define GOLDFISH_VPX_H_
20 
21 #include "GoldfishVideoDecoderOMXComponent.h"
22 #include "goldfish_vpx_defs.h"
23 
24 #include <sys/time.h>
25 
26 #include <map>
27 #include <vector>
28 
29 #include <ui/GraphicBuffer.h>
30 #include <utils/List.h>
31 #include <utils/RefBase.h>
32 #include <utils/Vector.h>
33 #include <utils/threads.h>
34 #include <gralloc_cb_bp.h>
35 
36 namespace android {
37 
38 struct ABuffer;
39 
40 struct GoldfishVPX : public GoldfishVideoDecoderOMXComponent {
41     GoldfishVPX(const char* name,
42                 const char* componentRole,
43                 OMX_VIDEO_CODINGTYPE codingType,
44                 const OMX_CALLBACKTYPE* callbacks,
45                 OMX_PTR appData,
46                 OMX_COMPONENTTYPE** component,
47                 RenderMode renderMode);
48 
49 protected:
50     virtual ~GoldfishVPX();
51 
52     virtual void onQueueFilled(OMX_U32 portIndex);
53     virtual void onPortFlushCompleted(OMX_U32 portIndex);
54     virtual void onReset();
55     virtual bool supportDescribeHdrStaticInfo();
56     virtual bool supportDescribeHdr10PlusInfo();
57 
58     virtual OMX_ERRORTYPE internalGetParameter(OMX_INDEXTYPE index,
59                                                OMX_PTR params);
60 
61     virtual OMX_ERRORTYPE internalSetParameter(OMX_INDEXTYPE index,
62                                                const OMX_PTR params);
63 
64     virtual OMX_ERRORTYPE getExtensionIndex(const char* name,
65                                             OMX_INDEXTYPE* index);
66 
67 private:
68     enum {
69         kNumBuffers = 10
70     };
71 
72     enum {
73         MODE_VP8,
74         MODE_VP9
75     } mMode;
76 
77     RenderMode mRenderMode = RenderMode::RENDER_BY_GUEST_CPU;
78     bool mEnableAndroidNativeBuffers = false;
79     std::map<void*, sp<ANativeWindowBuffer>> mNWBuffers;
80 
81     int getHostColorBufferId(void* header);
82 
83     enum {
84         INPUT_DATA_AVAILABLE,  // VPX component is ready to decode data.
85         INPUT_EOS_SEEN,        // VPX component saw EOS and is flushing On2 decoder.
86         OUTPUT_FRAMES_FLUSHED  // VPX component finished flushing On2 decoder.
87     } mEOSStatus;
88 
89     vpx_codec_ctx_t *mCtx;
90     bool mFrameParallelMode;  // Frame parallel is only supported by VP9 decoder.
91     struct PrivInfo {
92         OMX_TICKS mTimeStamp;
93         sp<ABuffer> mHdr10PlusInfo;
94     };
95     PrivInfo mPrivInfo[kNumBuffers];
96     uint8_t mTimeStampIdx;
97     vpx_image_t *mImg;
98 
99     status_t initDecoder();
100     status_t destroyDecoder();
101     bool outputBuffers(bool flushDecoder, bool display, bool eos, bool *portWillReset);
102     bool outputBufferSafe(OMX_BUFFERHEADERTYPE *outHeader);
103 
104     void setup_ctx_parameters(vpx_codec_ctx_t*, int hostColorBufferId = -1);
105 
106     DISALLOW_EVIL_CONSTRUCTORS(GoldfishVPX);
107 };
108 
109 }  // namespace android
110 
111 #endif  // GOLDFISH_VPX_H_
112