1 /*
2 // Copyright (c) 2014 Intel Corporation 
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 #ifndef VIRTUAL_DEVICE_H
17 #define VIRTUAL_DEVICE_H
18 
19 #include <IDisplayDevice.h>
20 #include <SimpleThread.h>
21 #include <IVideoPayloadManager.h>
22 #include <utils/Condition.h>
23 #include <utils/Mutex.h>
24 #include <utils/Vector.h>
25 #include <utils/List.h>
26 #ifdef INTEL_WIDI
27 #include "IFrameServer.h"
28 #endif
29 #include <va/va.h>
30 #include <va/va_vpp.h>
31 
32 namespace android {
33 namespace intel {
34 
35 class Hwcomposer;
36 class DisplayPlaneManager;
37 class IVideoPayloadManager;
38 class SoftVsyncObserver;
39 
40 #ifdef INTEL_WIDI
41 class VirtualDevice : public IDisplayDevice, public BnFrameServer {
42 #else
43 class VirtualDevice : public IDisplayDevice, public RefBase{
44 #endif
45 protected:
46     class VAMappedHandle;
47     class VAMappedHandleObject;
48     struct CachedBuffer : public android::RefBase {
49         CachedBuffer(BufferManager *mgr, buffer_handle_t handle);
50         ~CachedBuffer();
51         BufferManager *manager;
52         BufferMapper *mapper;
53         VAMappedHandle *vaMappedHandle;
54         buffer_handle_t cachedKhandle;
55     };
56     struct HeldDecoderBuffer : public android::RefBase {
57         HeldDecoderBuffer(const sp<VirtualDevice>& vd, const android::sp<CachedBuffer>& cachedBuffer);
58         virtual ~HeldDecoderBuffer();
59         android::sp<VirtualDevice> vd;
60         android::sp<CachedBuffer> cachedBuffer;
61     };
62 #ifdef INTEL_WIDI
63     struct Configuration {
64         sp<IFrameTypeChangeListener> typeChangeListener;
65         sp<IFrameListener> frameListener;
66         FrameProcessingPolicy policy;
67         bool frameServerActive;
68         bool extendedModeEnabled;
69         bool forceNotifyFrameType;
70         bool forceNotifyBufferInfo;
71     };
72 #endif
73     class BufferList {
74     public:
75         BufferList(VirtualDevice& vd, const char* name, uint32_t limit, uint32_t format, uint32_t usage);
76         buffer_handle_t get(uint32_t width, uint32_t height, sp<RefBase>* heldBuffer);
77         void clear();
78     private:
79         struct HeldBuffer;
80         VirtualDevice& mVd;
81         const char* mName;
82         android::List<buffer_handle_t> mAvailableBuffers;
83         const uint32_t mLimit;
84         const uint32_t mFormat;
85         const uint32_t mUsage;
86         uint32_t mBuffersToCreate;
87         uint32_t mWidth;
88         uint32_t mHeight;
89     };
90     struct Task;
91     struct RenderTask;
92     struct ComposeTask;
93     struct EnableVspTask;
94     struct DisableVspTask;
95     struct BlitTask;
96     struct FrameTypeChangedTask;
97     struct BufferInfoChangedTask;
98     struct OnFrameReadyTask;
99 
100     Mutex mConfigLock;
101 #ifdef INTEL_WIDI
102     Configuration mCurrentConfig;
103     Configuration mNextConfig;
104 #endif
105     ssize_t mRgbLayer;
106     ssize_t mYuvLayer;
107     bool mProtectedMode;
108 
109     buffer_handle_t mExtLastKhandle;
110     int64_t mExtLastTimestamp;
111 
112     int64_t mRenderTimestamp;
113 
114     Mutex mTaskLock; // for task queue and buffer lists
115     BufferList mCscBuffers;
116     BufferList mRgbUpscaleBuffers;
117     DECLARE_THREAD(WidiBlitThread, VirtualDevice);
118     Condition mRequestQueued;
119     Condition mRequestDequeued;
120     Vector< sp<Task> > mTasks;
121 
122     // fence info
123     int mSyncTimelineFd;
124     unsigned mNextSyncPoint;
125     bool mExpectAcquireFences;
126 #ifdef INTEL_WIDI
127     FrameInfo mLastInputFrameInfo;
128     FrameInfo mLastOutputFrameInfo;
129 #endif
130     int32_t mVideoFramerate;
131 
132     android::KeyedVector<buffer_handle_t, android::sp<CachedBuffer> > mMappedBufferCache;
133     android::Mutex mHeldBuffersLock;
134     android::KeyedVector<buffer_handle_t, android::sp<android::RefBase> > mHeldBuffers;
135 
136     // VSP
137     bool mVspInUse;
138     bool mVspEnabled;
139     uint32_t mVspWidth;
140     uint32_t mVspHeight;
141     VADisplay va_dpy;
142     VAConfigID va_config;
143     VAContextID va_context;
144     VASurfaceID va_blank_yuv_in;
145     VASurfaceID va_blank_rgb_in;
146     android::KeyedVector<buffer_handle_t, android::sp<VAMappedHandleObject> > mVaMapCache;
147 
148     bool mVspUpscale;
149     bool mDebugVspClear;
150     bool mDebugVspDump;
151     uint32_t mDebugCounter;
152 
153 private:
154     android::sp<CachedBuffer> getMappedBuffer(buffer_handle_t handle);
155 
156     bool sendToWidi(hwc_display_contents_1_t *display);
157     bool queueCompose(hwc_display_contents_1_t *display);
158     bool queueColorConvert(hwc_display_contents_1_t *display);
159 #ifdef INTEL_WIDI
160     bool handleExtendedMode(hwc_display_contents_1_t *display);
161 
162     void queueFrameTypeInfo(const FrameInfo& inputFrameInfo);
163     void queueBufferInfo(const FrameInfo& outputFrameInfo);
164 #endif
165     void colorSwap(buffer_handle_t src, buffer_handle_t dest, uint32_t pixelCount);
166     void vspPrepare(uint32_t width, uint32_t height);
167     void vspEnable(uint32_t width, uint32_t height);
168     void vspDisable();
169     void vspCompose(VASurfaceID videoIn, VASurfaceID rgbIn, VASurfaceID videoOut,
170                     const VARectangle* surface_region, const VARectangle* output_region);
171 
172     bool getFrameOfSize(uint32_t width, uint32_t height, const IVideoPayloadManager::MetaData& metadata, IVideoPayloadManager::Buffer& info);
173     void setMaxDecodeResolution(uint32_t width, uint32_t height);
174 
175 public:
176     VirtualDevice(Hwcomposer& hwc);
177     virtual ~VirtualDevice();
178     bool isFrameServerActive() const;
179 
180 public:
181     virtual bool prePrepare(hwc_display_contents_1_t *display);
182     virtual bool prepare(hwc_display_contents_1_t *display);
183     virtual bool commit(hwc_display_contents_1_t *display,
184                           IDisplayContext *context);
185 
186     virtual bool vsyncControl(bool enabled);
187     virtual bool blank(bool blank);
188     virtual bool getDisplaySize(int *width, int *height);
189     virtual bool getDisplayConfigs(uint32_t *configs,
190                                        size_t *numConfigs);
191     virtual bool getDisplayAttributes(uint32_t config,
192                                           const uint32_t *attributes,
193                                           int32_t *values);
194     virtual bool compositionComplete();
195     virtual bool initialize();
196     virtual void deinitialize();
197     virtual bool isConnected() const;
198     virtual const char* getName() const;
199     virtual int getType() const;
200     virtual void onVsync(int64_t timestamp);
201     virtual void dump(Dump& d);
202 #ifdef INTEL_WIDI
203     // IFrameServer methods
204     virtual android::status_t start(sp<IFrameTypeChangeListener> frameTypeChangeListener);
205     virtual android::status_t stop(bool isConnected);
206 	/* TODO: 64-bit - this handle of size 32-bit is a problem for 64-bit */
207     virtual android::status_t notifyBufferReturned(int handle);
208     virtual android::status_t setResolution(const FrameProcessingPolicy& policy, android::sp<IFrameListener> listener);
209 #endif
210     virtual bool setPowerMode(int mode);
211     virtual int  getActiveConfig();
212     virtual bool setActiveConfig(int index);
213 
214 protected:
215     bool mInitialized;
216     Hwcomposer& mHwc;
217     IVideoPayloadManager *mPayloadManager;
218     SoftVsyncObserver *mVsyncObserver;
219     uint32_t mOrigContentWidth;
220     uint32_t mOrigContentHeight;
221     bool mFirstVideoFrame;
222     bool mLastConnectionStatus;
223     uint32_t mCachedBufferCapcity;
224     uint32_t mDecWidth;
225     uint32_t mDecHeight;
226     bool mIsForceCloneMode;
227 };
228 
229 }
230 }
231 
232 #endif /* VIRTUAL_DEVICE_H */
233