1 /*
2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef WEBRTC_MODULES_VIDEO_RENDER_MAIN_SOURCE_WINDOWS_VIDEO_RENDER_DIRECT3D9_H_
12 #define WEBRTC_MODULES_VIDEO_RENDER_MAIN_SOURCE_WINDOWS_VIDEO_RENDER_DIRECT3D9_H_
13 
14 #include "webrtc/modules/video_render/windows/i_video_render_win.h"
15 
16 #include <d3d9.h>
17 #include <d3dx9.h>
18 #include <ddraw.h>
19 
20 #include <Map>
21 
22 // Added
23 #include "webrtc/modules/video_render/include/video_render_defines.h"
24 #include "webrtc/system_wrappers/interface/thread_wrapper.h"
25 
26 #pragma comment(lib, "d3d9.lib")       // located in DirectX SDK
27 
28 namespace webrtc {
29 class CriticalSectionWrapper;
30 class EventWrapper;
31 class Trace;
32 
33 class D3D9Channel: public VideoRenderCallback
34 {
35 public:
36     D3D9Channel(LPDIRECT3DDEVICE9 pd3DDevice,
37                     CriticalSectionWrapper* critSect, Trace* trace);
38 
39     virtual ~D3D9Channel();
40 
41     // Inherited from VideoRencerCallback, called from VideoAPI class.
42     // Called when the incomming frame size and/or number of streams in mix changes
43     virtual int FrameSizeChange(int width, int height, int numberOfStreams);
44 
45     // A new frame is delivered.
46     virtual int DeliverFrame(const I420VideoFrame& videoFrame);
47     virtual int32_t RenderFrame(const uint32_t streamId,
48                                 const I420VideoFrame& videoFrame);
49 
50     // Called to check if the video frame is updated.
51     int IsUpdated(bool& isUpdated);
52     // Called after the video frame has been render to the screen
53     int RenderOffFrame();
54     // Called to get the texture that contains the video frame
55     LPDIRECT3DTEXTURE9 GetTexture();
56     // Called to get the texture(video frame) size
57     int GetTextureWidth();
58     int GetTextureHeight();
59     //
60     void SetStreamSettings(uint16_t streamId,
61                            uint32_t zOrder,
62                            float startWidth,
63                            float startHeight,
64                            float stopWidth,
65                            float stopHeight);
66     int GetStreamSettings(uint16_t streamId,
67                           uint32_t& zOrder,
68                           float& startWidth,
69                           float& startHeight,
70                           float& stopWidth,
71                           float& stopHeight);
72 
73     int ReleaseTexture();
74     int RecreateTexture(LPDIRECT3DDEVICE9 pd3DDevice);
75 
76 protected:
77 
78 private:
79     //critical section passed from the owner
80     CriticalSectionWrapper* _critSect;
81     LPDIRECT3DDEVICE9 _pd3dDevice;
82     LPDIRECT3DTEXTURE9 _pTexture;
83 
84     bool _bufferIsUpdated;
85     // the frame size
86     int _width;
87     int _height;
88     //sream settings
89     //TODO support multiple streams in one channel
90     uint16_t _streamId;
91     uint32_t _zOrder;
92     float _startWidth;
93     float _startHeight;
94     float _stopWidth;
95     float _stopHeight;
96 };
97 
98 class VideoRenderDirect3D9: IVideoRenderWin
99 {
100 public:
101     VideoRenderDirect3D9(Trace* trace, HWND hWnd, bool fullScreen);
102     ~VideoRenderDirect3D9();
103 
104 public:
105     //IVideoRenderWin
106 
107     /**************************************************************************
108      *
109      *   Init
110      *
111      ***************************************************************************/
112     virtual int32_t Init();
113 
114     /**************************************************************************
115      *
116      *   Incoming Streams
117      *
118      ***************************************************************************/
119     virtual VideoRenderCallback
120             * CreateChannel(const uint32_t streamId,
121                             const uint32_t zOrder,
122                             const float left,
123                             const float top,
124                             const float right,
125                             const float bottom);
126 
127     virtual int32_t DeleteChannel(const uint32_t streamId);
128 
129     virtual int32_t GetStreamSettings(const uint32_t channel,
130                                       const uint16_t streamId,
131                                       uint32_t& zOrder,
132                                       float& left, float& top,
133                                       float& right, float& bottom);
134 
135     /**************************************************************************
136      *
137      *   Start/Stop
138      *
139      ***************************************************************************/
140 
141     virtual int32_t StartRender();
142     virtual int32_t StopRender();
143 
144     /**************************************************************************
145      *
146      *   Properties
147      *
148      ***************************************************************************/
149 
150     virtual bool IsFullScreen();
151 
152     virtual int32_t SetCropping(const uint32_t channel,
153                                 const uint16_t streamId,
154                                 const float left, const float top,
155                                 const float right, const float bottom);
156 
157     virtual int32_t ConfigureRenderer(const uint32_t channel,
158                                       const uint16_t streamId,
159                                       const unsigned int zOrder,
160                                       const float left, const float top,
161                                       const float right, const float bottom);
162 
163     virtual int32_t SetTransparentBackground(const bool enable);
164 
165     virtual int32_t ChangeWindow(void* window);
166 
167     virtual int32_t GetGraphicsMemory(uint64_t& totalMemory,
168                                       uint64_t& availableMemory);
169 
170     virtual int32_t SetText(const uint8_t textId,
171                             const uint8_t* text,
172                             const int32_t textLength,
173                             const uint32_t colorText,
174                             const uint32_t colorBg,
175                             const float left, const float top,
176                             const float rigth, const float bottom);
177 
178     virtual int32_t SetBitmap(const void* bitMap,
179                               const uint8_t pictureId,
180                               const void* colorKey,
181                               const float left, const float top,
182                               const float right, const float bottom);
183 
184 public:
185     // Get a channel by channel id
186     D3D9Channel* GetD3DChannel(int channel);
187     int UpdateRenderSurface();
188 
189 protected:
190     // The thread rendering the screen
191     static bool ScreenUpdateThreadProc(void* obj);
192     bool ScreenUpdateProcess();
193 
194 private:
195     // Init/close the d3d device
196     int InitDevice();
197     int CloseDevice();
198 
199     // Transparent related functions
200     int SetTransparentColor(LPDIRECT3DTEXTURE9 pTexture,
201                             DDCOLORKEY* transparentColorKey,
202                             DWORD width,
203                             DWORD height);
204 
205     CriticalSectionWrapper& _refD3DCritsect;
206     Trace* _trace;
207     rtc::scoped_ptr<ThreadWrapper> _screenUpdateThread;
208     EventWrapper* _screenUpdateEvent;
209 
210     HWND _hWnd;
211     bool _fullScreen;
212     RECT _originalHwndRect;
213     //FIXME we probably don't need this since all the information can be get from _d3dChannels
214     int _channel;
215     //Window size
216     UINT _winWidth;
217     UINT _winHeight;
218 
219     // Device
220     LPDIRECT3D9 _pD3D; // Used to create the D3DDevice
221     LPDIRECT3DDEVICE9 _pd3dDevice; // Our rendering device
222     LPDIRECT3DVERTEXBUFFER9 _pVB; // Buffer to hold Vertices
223     LPDIRECT3DTEXTURE9 _pTextureLogo;
224 
225     std::map<int, D3D9Channel*> _d3dChannels;
226     std::multimap<int, unsigned int> _d3dZorder;
227 
228     // The position where the logo will be placed
229     float _logoLeft;
230     float _logoTop;
231     float _logoRight;
232     float _logoBottom;
233 
234     typedef HRESULT (WINAPI *DIRECT3DCREATE9EX)(UINT SDKVersion, IDirect3D9Ex**);
235     LPDIRECT3DSURFACE9 _pd3dSurface;
236 
237     DWORD GetVertexProcessingCaps();
238     int InitializeD3D(HWND hWnd, D3DPRESENT_PARAMETERS* pd3dpp);
239 
240     D3DPRESENT_PARAMETERS _d3dpp;
241     int ResetDevice();
242 
243     int UpdateVerticeBuffer(LPDIRECT3DVERTEXBUFFER9 pVB, int offset,
244                             float startWidth, float startHeight,
245                             float stopWidth, float stopHeight);
246 
247     //code for providing graphics settings
248     DWORD _totalMemory;
249     DWORD _availableMemory;
250 };
251 
252 }  // namespace webrtc
253 
254 #endif // WEBRTC_MODULES_VIDEO_RENDER_MAIN_SOURCE_WINDOWS_VIDEO_RENDER_DIRECT3D9_H_
255