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
42namespace WebCore {
43
44MediaPlayerPrivateFullscreenWindow::MediaPlayerPrivateFullscreenWindow(MediaPlayerPrivateFullscreenClient* client)
45    : m_client(client)
46    , m_hwnd(0)
47{
48}
49
50MediaPlayerPrivateFullscreenWindow::~MediaPlayerPrivateFullscreenWindow()
51{
52    if (!m_hwnd)
53        return;
54
55    ::DestroyWindow(m_hwnd);
56    ASSERT(!m_hwnd);
57}
58
59void 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(wcex);
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, 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)
96void 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            m_layerTreeHost->createRenderer();
116        }
117    }
118
119    m_layerTreeHost->setRootChildLayer(m_rootChild.get());
120    PlatformCALayer* rootLayer = m_rootChild->rootLayer();
121    CGRect rootBounds = m_rootChild->rootLayer()->bounds();
122    m_rootChild->setFrame(rootBounds);
123    m_rootChild->setBackgroundColor(CGColorGetConstantColor(kCGColorBlack));
124#ifndef NDEBUG
125    RetainPtr<CGColorRef> redColor = adoptCF(CGColorCreateGenericRGB(1, 0, 0, 1));
126    rootLayer->setBackgroundColor(redColor.get());
127#else
128    rootLayer->setBackgroundColor(CGColorGetConstantColor(kCGColorBlack));
129#endif
130}
131#endif
132
133LRESULT MediaPlayerPrivateFullscreenWindow::staticWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
134{
135    LONG_PTR longPtr = GetWindowLongPtr(hWnd, GWLP_USERDATA);
136
137    if (!longPtr && message == WM_CREATE) {
138        LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
139        longPtr = reinterpret_cast<LONG_PTR>(lpcs->lpCreateParams);
140        ::SetWindowLongPtr(hWnd, GWLP_USERDATA, longPtr);
141    }
142
143    if (MediaPlayerPrivateFullscreenWindow* window = reinterpret_cast<MediaPlayerPrivateFullscreenWindow*>(longPtr))
144        return window->wndProc(hWnd, message, wParam, lParam);
145
146    return ::DefWindowProc(hWnd, message, wParam, lParam);
147}
148
149LRESULT MediaPlayerPrivateFullscreenWindow::wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
150{
151    LRESULT lResult = 0;
152    switch (message) {
153    case WM_CREATE:
154        m_hwnd = hWnd;
155        break;
156    case WM_DESTROY:
157        m_hwnd = 0;
158#if USE(ACCELERATED_COMPOSITING)
159        if (m_layerTreeHost)
160            m_layerTreeHost->setWindow(0);
161#endif
162        break;
163    case WM_WINDOWPOSCHANGED:
164        {
165            LPWINDOWPOS wp = reinterpret_cast<LPWINDOWPOS>(lParam);
166            if (wp->flags & SWP_NOSIZE)
167                break;
168#if USE(ACCELERATED_COMPOSITING)
169            if (m_layerTreeHost) {
170                m_layerTreeHost->resize();
171                PlatformCALayer* rootLayer = m_rootChild->rootLayer();
172                CGRect rootBounds = m_rootChild->rootLayer()->bounds();
173                m_rootChild->setFrame(rootBounds);
174                m_rootChild->setNeedsLayout();
175            }
176#endif
177        }
178        break;
179    case WM_PAINT:
180#if USE(ACCELERATED_COMPOSITING)
181        if (m_layerTreeHost) {
182            m_layerTreeHost->paint();
183            ::ValidateRect(m_hwnd, 0);
184        } else
185#endif
186        {
187            PAINTSTRUCT ps;
188            HDC hdc = ::BeginPaint(m_hwnd, &ps);
189            ::FillRect(hdc, &ps.rcPaint, (HBRUSH)::GetStockObject(BLACK_BRUSH));
190            ::EndPaint(m_hwnd, &ps);
191        }
192        break;
193    case WM_PRINTCLIENT:
194        {
195            RECT clientRect;
196            HDC context = (HDC)wParam;
197            ::GetClientRect(m_hwnd, &clientRect);
198            ::FillRect(context, &clientRect, (HBRUSH)::GetStockObject(BLACK_BRUSH));
199        }
200    }
201    if (m_client)
202        lResult = m_client->fullscreenClientWndProc(hWnd, message, wParam, lParam);
203
204    return lResult;
205}
206
207}
208