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