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 <ddraw.h>
18 
19 #include <Map>
20 
21 // Added
22 #include "webrtc/base/platform_thread.h"
23 #include "webrtc/modules/video_render/video_render_defines.h"
24 
25 #pragma comment(lib, "d3d9.lib")       // located in DirectX SDK
26 
27 namespace webrtc {
28 class CriticalSectionWrapper;
29 class EventTimerWrapper;
30 class Trace;
31 
32 class D3D9Channel: public VideoRenderCallback
33 {
34 public:
35     D3D9Channel(LPDIRECT3DDEVICE9 pd3DDevice,
36                     CriticalSectionWrapper* critSect, Trace* trace);
37 
38     virtual ~D3D9Channel();
39 
40     // Inherited from VideoRencerCallback, called from VideoAPI class.
41     // Called when the incomming frame size and/or number of streams in mix changes
42     virtual int FrameSizeChange(int width, int height, int numberOfStreams);
43 
44     // A new frame is delivered.
45     virtual int DeliverFrame(const VideoFrame& videoFrame);
46     virtual int32_t RenderFrame(const uint32_t streamId,
47                                 const VideoFrame& videoFrame);
48 
49     // Called to check if the video frame is updated.
50     int IsUpdated(bool& isUpdated);
51     // Called after the video frame has been render to the screen
52     int RenderOffFrame();
53     // Called to get the texture that contains the video frame
54     LPDIRECT3DTEXTURE9 GetTexture();
55     // Called to get the texture(video frame) size
56     int GetTextureWidth();
57     int GetTextureHeight();
58     //
59     void SetStreamSettings(uint16_t streamId,
60                            uint32_t zOrder,
61                            float startWidth,
62                            float startHeight,
63                            float stopWidth,
64                            float stopHeight);
65     int GetStreamSettings(uint16_t streamId,
66                           uint32_t& zOrder,
67                           float& startWidth,
68                           float& startHeight,
69                           float& stopWidth,
70                           float& stopHeight);
71 
72     int ReleaseTexture();
73     int RecreateTexture(LPDIRECT3DDEVICE9 pd3DDevice);
74 
75 protected:
76 
77 private:
78     //critical section passed from the owner
79     CriticalSectionWrapper* _critSect;
80     LPDIRECT3DDEVICE9 _pd3dDevice;
81     LPDIRECT3DTEXTURE9 _pTexture;
82 
83     bool _bufferIsUpdated;
84     // the frame size
85     int _width;
86     int _height;
87     //sream settings
88     //TODO support multiple streams in one channel
89     uint16_t _streamId;
90     uint32_t _zOrder;
91     float _startWidth;
92     float _startHeight;
93     float _stopWidth;
94     float _stopHeight;
95 };
96 
97 class VideoRenderDirect3D9: IVideoRenderWin
98 {
99 public:
100     VideoRenderDirect3D9(Trace* trace, HWND hWnd, bool fullScreen);
101     ~VideoRenderDirect3D9();
102 
103 public:
104     //IVideoRenderWin
105 
106     /**************************************************************************
107      *
108      *   Init
109      *
110      ***************************************************************************/
111     virtual int32_t Init();
112 
113     /**************************************************************************
114      *
115      *   Incoming Streams
116      *
117      ***************************************************************************/
118     virtual VideoRenderCallback
119             * CreateChannel(const uint32_t streamId,
120                             const uint32_t zOrder,
121                             const float left,
122                             const float top,
123                             const float right,
124                             const float bottom);
125 
126     virtual int32_t DeleteChannel(const uint32_t streamId);
127 
128     virtual int32_t GetStreamSettings(const uint32_t channel,
129                                       const uint16_t streamId,
130                                       uint32_t& zOrder,
131                                       float& left, float& top,
132                                       float& right, float& bottom);
133 
134     /**************************************************************************
135      *
136      *   Start/Stop
137      *
138      ***************************************************************************/
139 
140     virtual int32_t StartRender();
141     virtual int32_t StopRender();
142 
143     /**************************************************************************
144      *
145      *   Properties
146      *
147      ***************************************************************************/
148 
149     virtual bool IsFullScreen();
150 
151     virtual int32_t SetCropping(const uint32_t channel,
152                                 const uint16_t streamId,
153                                 const float left, const float top,
154                                 const float right, const float bottom);
155 
156     virtual int32_t ConfigureRenderer(const uint32_t channel,
157                                       const uint16_t streamId,
158                                       const unsigned int zOrder,
159                                       const float left, const float top,
160                                       const float right, const float bottom);
161 
162     virtual int32_t SetTransparentBackground(const bool enable);
163 
164     virtual int32_t ChangeWindow(void* window);
165 
166     virtual int32_t GetGraphicsMemory(uint64_t& totalMemory,
167                                       uint64_t& availableMemory);
168 
169     virtual int32_t SetText(const uint8_t textId,
170                             const uint8_t* text,
171                             const int32_t textLength,
172                             const uint32_t colorText,
173                             const uint32_t colorBg,
174                             const float left, const float top,
175                             const float rigth, const float bottom);
176 
177     virtual int32_t SetBitmap(const void* bitMap,
178                               const uint8_t pictureId,
179                               const void* colorKey,
180                               const float left, const float top,
181                               const float right, const float bottom);
182 
183 public:
184     // Get a channel by channel id
185     D3D9Channel* GetD3DChannel(int channel);
186     int UpdateRenderSurface();
187 
188 protected:
189     // The thread rendering the screen
190     static bool ScreenUpdateThreadProc(void* obj);
191     bool ScreenUpdateProcess();
192 
193 private:
194     // Init/close the d3d device
195     int InitDevice();
196     int CloseDevice();
197 
198     // Transparent related functions
199     int SetTransparentColor(LPDIRECT3DTEXTURE9 pTexture,
200                             DDCOLORKEY* transparentColorKey,
201                             DWORD width,
202                             DWORD height);
203 
204     CriticalSectionWrapper& _refD3DCritsect;
205     Trace* _trace;
206     // TODO(pbos): Remove scoped_ptr and use PlatformThread directly.
207     rtc::scoped_ptr<rtc::PlatformThread> _screenUpdateThread;
208     EventTimerWrapper* _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