initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / win / MediaPlayerPrivateFullscreenWindow.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  * 
4  * Redistribution and use in source and binary forms, with or without 
5  * modification, are permitted provided that the following conditions 
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright 
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the 
11  *    documentation and/or other materials provided with the distribution.
12  * 
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "MediaPlayerPrivateFullscreenWindow.h"
28
29 #include "IntRect.h"
30 #include "WebCoreInstanceHandle.h"
31 #include <windows.h>
32
33 #if USE(CG)
34 #include <CoreGraphics/CGColor.h>
35 #endif
36
37 #if USE(ACCELERATED_COMPOSITING)
38 #include "CACFLayerTreeHost.h"
39 #include "PlatformCALayer.h"
40 #endif
41
42 namespace WebCore {
43
44 MediaPlayerPrivateFullscreenWindow::MediaPlayerPrivateFullscreenWindow(MediaPlayerPrivateFullscreenClient* client)
45     : m_client(client)
46     , m_hwnd(0)
47 {
48 }
49
50 MediaPlayerPrivateFullscreenWindow::~MediaPlayerPrivateFullscreenWindow()
51 {
52     if (!m_hwnd)
53         return;
54
55     ::DestroyWindow(m_hwnd);
56     ASSERT(!m_hwnd);
57 }
58
59 void MediaPlayerPrivateFullscreenWindow::createWindow(HWND parentHwnd)
60 {
61     static ATOM windowAtom;
62     static LPCWSTR windowClassName = L"MediaPlayerPrivateFullscreenWindowClass";
63     if (!windowAtom) {
64         WNDCLASSEX wcex = {0};
65         wcex.cbSize = sizeof(WNDCLASSEX);
66         wcex.style = CS_HREDRAW | CS_VREDRAW;
67         wcex.lpfnWndProc = staticWndProc;
68         wcex.hInstance = instanceHandle();
69         wcex.lpszClassName = windowClassName;
70         windowAtom = ::RegisterClassEx(&wcex);
71     }
72
73     ASSERT(!m_hwnd);
74
75     MONITORINFO mi = {0};
76     mi.cbSize = sizeof(MONITORINFO);
77     if (!GetMonitorInfo(MonitorFromWindow(parentHwnd, MONITOR_DEFAULTTONEAREST), &mi))
78         return;
79
80     IntRect monitorRect = mi.rcMonitor;
81     
82     ::CreateWindowExW(WS_EX_TOOLWINDOW, windowClassName, L"", WS_POPUP, 
83         monitorRect.x(), monitorRect.y(), monitorRect.width(), monitorRect.height(),
84         parentHwnd, 0, WebCore::instanceHandle(), this);
85     ASSERT(IsWindow(m_hwnd));
86
87 #if USE(ACCELERATED_COMPOSITING)
88     if (m_layerTreeHost)
89         m_layerTreeHost->setWindow(m_hwnd);
90 #endif
91
92     ::SetFocus(m_hwnd);
93 }
94
95 #if USE(ACCELERATED_COMPOSITING)
96 void MediaPlayerPrivateFullscreenWindow::setRootChildLayer(PassRefPtr<PlatformCALayer> rootChild)
97 {
98     if (m_rootChild == rootChild)
99         return;
100
101     if (m_rootChild)
102         m_rootChild->removeFromSuperlayer();
103
104     m_rootChild = rootChild;
105
106     if (!m_rootChild) {
107         m_layerTreeHost = nullptr;
108         return;
109     }
110
111     if (!m_layerTreeHost) {
112         m_layerTreeHost = CACFLayerTreeHost::create();
113         if (m_hwnd)
114             m_layerTreeHost->setWindow(m_hwnd);
115     }
116
117     m_layerTreeHost->setRootChildLayer(m_rootChild.get());
118     PlatformCALayer* rootLayer = m_rootChild->rootLayer();
119     CGRect rootBounds = m_rootChild->rootLayer()->bounds();
120     m_rootChild->setFrame(rootBounds);
121     m_rootChild->setBackgroundColor(CGColorGetConstantColor(kCGColorBlack));
122 #ifndef NDEBUG
123     RetainPtr<CGColorRef> redColor(AdoptCF, CGColorCreateGenericRGB(1, 0, 0, 1));
124     rootLayer->setBackgroundColor(redColor.get());
125 #else
126     rootLayer->setBackgroundColor(CGColorGetConstantColor(kCGColorBlack));
127 #endif
128 }
129 #endif
130
131 LRESULT MediaPlayerPrivateFullscreenWindow::staticWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
132 {
133     LONG_PTR longPtr = GetWindowLongPtr(hWnd, GWLP_USERDATA);
134
135     if (!longPtr && message == WM_CREATE) {
136         LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
137         longPtr = reinterpret_cast<LONG_PTR>(lpcs->lpCreateParams);
138         ::SetWindowLongPtr(hWnd, GWLP_USERDATA, longPtr);
139     }
140
141     if (MediaPlayerPrivateFullscreenWindow* window = reinterpret_cast<MediaPlayerPrivateFullscreenWindow*>(longPtr))
142         return window->wndProc(hWnd, message, wParam, lParam);
143
144     return ::DefWindowProc(hWnd, message, wParam, lParam);    
145 }
146
147 LRESULT MediaPlayerPrivateFullscreenWindow::wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
148 {
149     LRESULT lResult = 0;
150     switch (message) {
151     case WM_CREATE:
152         m_hwnd = hWnd;
153         break;
154     case WM_DESTROY:
155         m_hwnd = 0;
156 #if USE(ACCELERATED_COMPOSITING)
157         if (m_layerTreeHost)
158             m_layerTreeHost->setWindow(0);
159 #endif
160         break;
161     case WM_WINDOWPOSCHANGED:
162         {
163             LPWINDOWPOS wp = reinterpret_cast<LPWINDOWPOS>(lParam);
164             if (wp->flags & SWP_NOSIZE)
165                 break;
166 #if USE(ACCELERATED_COMPOSITING)
167             if (m_layerTreeHost) {
168                 m_layerTreeHost->resize();
169                 PlatformCALayer* rootLayer = m_rootChild->rootLayer();
170                 CGRect rootBounds = m_rootChild->rootLayer()->bounds();
171                 m_rootChild->setFrame(rootBounds);
172                 m_rootChild->setNeedsLayout();
173             }
174 #endif
175         }
176         break;
177     case WM_PAINT:
178 #if USE(ACCELERATED_COMPOSITING)
179         if (m_layerTreeHost) {
180             m_layerTreeHost->paint();
181             ::ValidateRect(m_hwnd, 0);
182         } else
183 #endif
184         {
185             PAINTSTRUCT ps;
186             HDC hdc = ::BeginPaint(m_hwnd, &ps);
187             ::FillRect(hdc, &ps.rcPaint, (HBRUSH)::GetStockObject(BLACK_BRUSH));
188             ::EndPaint(m_hwnd, &ps);
189         }
190         break;
191     case WM_PRINTCLIENT:
192         {
193             RECT clientRect;
194             HDC context = (HDC)wParam;
195             ::GetClientRect(m_hwnd, &clientRect);
196             ::FillRect(context, &clientRect, (HBRUSH)::GetStockObject(BLACK_BRUSH));
197         }
198     }
199     if (m_client)
200         lResult = m_client->fullscreenClientWndProc(hWnd, message, wParam, lParam);
201
202     return lResult;
203 }
204
205 }