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#if USE(ACCELERATED_COMPOSITING)
48    , m_layerTreeHost(CACFLayerTreeHost::create())
49#endif
50{
51}
52
53MediaPlayerPrivateFullscreenWindow::~MediaPlayerPrivateFullscreenWindow()
54{
55    if (!m_hwnd)
56        return;
57
58    ::DestroyWindow(m_hwnd);
59    ASSERT(!m_hwnd);
60}
61
62void MediaPlayerPrivateFullscreenWindow::createWindow(HWND parentHwnd)
63{
64    static ATOM windowAtom;
65    static LPCWSTR windowClassName = L"MediaPlayerPrivateFullscreenWindowClass";
66    if (!windowAtom) {
67        WNDCLASSEX wcex = {0};
68        wcex.cbSize = sizeof(WNDCLASSEX);
69        wcex.style = CS_HREDRAW | CS_VREDRAW;
70        wcex.lpfnWndProc = staticWndProc;
71        wcex.hInstance = instanceHandle();
72        wcex.lpszClassName = windowClassName;
73        windowAtom = ::RegisterClassEx(&wcex);
74    }
75
76    ASSERT(!m_hwnd);
77
78    MONITORINFO mi = {0};
79    mi.cbSize = sizeof(MONITORINFO);
80    if (!GetMonitorInfo(MonitorFromWindow(parentHwnd, MONITOR_DEFAULTTONEAREST), &mi))
81        return;
82
83    IntRect monitorRect = mi.rcMonitor;
84
85    ::CreateWindowExW(WS_EX_TOOLWINDOW, windowClassName, L"", WS_POPUP | WS_VISIBLE,
86        monitorRect.x(), monitorRect.y(), monitorRect.width(), monitorRect.height(),
87        parentHwnd, 0, WebCore::instanceHandle(), this);
88    ASSERT(IsWindow(m_hwnd));
89
90#if USE(ACCELERATED_COMPOSITING)
91    m_layerTreeHost->setWindow(m_hwnd);
92#endif
93
94    ::SetFocus(m_hwnd);
95}
96
97#if USE(ACCELERATED_COMPOSITING)
98void MediaPlayerPrivateFullscreenWindow::setRootChildLayer(PassRefPtr<PlatformCALayer> rootChild)
99{
100    if (m_rootChild == rootChild)
101        return;
102
103    if (m_rootChild)
104        m_rootChild->removeFromSuperlayer();
105
106    m_rootChild = rootChild;
107
108    if (!m_rootChild)
109        return;
110
111    m_layerTreeHost->setRootChildLayer(m_rootChild.get());
112    PlatformCALayer* rootLayer = m_rootChild->rootLayer();
113    CGRect rootBounds = m_rootChild->rootLayer()->bounds();
114    m_rootChild->setFrame(rootBounds);
115    m_rootChild->setBackgroundColor(CGColorGetConstantColor(kCGColorBlack));
116#ifndef NDEBUG
117    RetainPtr<CGColorRef> redColor(AdoptCF, CGColorCreateGenericRGB(1, 0, 0, 1));
118    rootLayer->setBackgroundColor(redColor.get());
119#else
120    rootLayer->setBackgroundColor(CGColorGetConstantColor(kCGColorBlack));
121#endif
122}
123#endif
124
125LRESULT MediaPlayerPrivateFullscreenWindow::staticWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
126{
127    LONG_PTR longPtr = GetWindowLongPtr(hWnd, GWLP_USERDATA);
128
129    if (!longPtr && message == WM_CREATE) {
130        LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
131        longPtr = reinterpret_cast<LONG_PTR>(lpcs->lpCreateParams);
132        ::SetWindowLongPtr(hWnd, GWLP_USERDATA, longPtr);
133    }
134
135    if (MediaPlayerPrivateFullscreenWindow* window = reinterpret_cast<MediaPlayerPrivateFullscreenWindow*>(longPtr))
136        return window->wndProc(hWnd, message, wParam, lParam);
137
138    return ::DefWindowProc(hWnd, message, wParam, lParam);
139}
140
141LRESULT MediaPlayerPrivateFullscreenWindow::wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
142{
143    LRESULT lResult = 0;
144    switch (message) {
145    case WM_CREATE:
146        m_hwnd = hWnd;
147        break;
148    case WM_DESTROY:
149        m_hwnd = 0;
150#if USE(ACCELERATED_COMPOSITING)
151        m_layerTreeHost->setWindow(0);
152#endif
153        break;
154    case WM_WINDOWPOSCHANGED:
155        {
156            LPWINDOWPOS wp = reinterpret_cast<LPWINDOWPOS>(lParam);
157            if (wp->flags & SWP_NOSIZE)
158                break;
159#if USE(ACCELERATED_COMPOSITING)
160            m_layerTreeHost->resize();
161            PlatformCALayer* rootLayer = m_rootChild->rootLayer();
162            CGRect rootBounds = m_rootChild->rootLayer()->bounds();
163            m_rootChild->setFrame(rootBounds);
164            m_rootChild->setNeedsLayout();
165#endif
166        }
167        break;
168    case WM_PAINT:
169#if USE(ACCELERATED_COMPOSITING)
170        m_layerTreeHost->paint();
171        ::ValidateRect(m_hwnd, 0);
172#endif
173        break;
174    }
175
176    if (m_client)
177        lResult = m_client->fullscreenClientWndProc(hWnd, message, wParam, lParam);
178
179    return lResult;
180}
181
182}
183