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 "ChunkedUpdateDrawingAreaProxy.h"
28
29#include "UpdateChunk.h"
30#include "WebProcessProxy.h"
31#include "WebView.h"
32#include <WebCore/BitmapInfo.h>
33
34using namespace WebCore;
35
36namespace WebKit {
37
38WebPageProxy* ChunkedUpdateDrawingAreaProxy::page()
39{
40    return m_webView->page();
41}
42
43void ChunkedUpdateDrawingAreaProxy::ensureBackingStore()
44{
45    if (m_backingStoreBitmap)
46        return;
47
48    BitmapInfo bitmapInfo = BitmapInfo::createBottomUp(size());
49
50    void* pixels = 0;
51    m_backingStoreBitmap.set(::CreateDIBSection(0, &bitmapInfo, DIB_RGB_COLORS, &pixels, 0, 0));
52
53    if (!m_backingStoreDC) {
54        // Create a DC for the backing store.
55        HDC screenDC = ::GetDC(0);
56        m_backingStoreDC.set(::CreateCompatibleDC(screenDC));
57        ::ReleaseDC(0, screenDC);
58    }
59
60    ::SelectObject(m_backingStoreDC.get(), m_backingStoreBitmap.get());
61}
62
63void ChunkedUpdateDrawingAreaProxy::invalidateBackingStore()
64{
65    m_backingStoreBitmap.clear();
66}
67
68bool ChunkedUpdateDrawingAreaProxy::platformPaint(const IntRect& rect, HDC hdc)
69{
70    if (!m_backingStoreBitmap)
71        return false;
72
73    // BitBlt from the backing-store to the passed in hdc.
74    ::BitBlt(hdc, rect.x(), rect.y(), rect.width(), rect.height(), m_backingStoreDC.get(), rect.x(), rect.y(), SRCCOPY);
75    return true;
76}
77
78void ChunkedUpdateDrawingAreaProxy::drawUpdateChunkIntoBackingStore(UpdateChunk* updateChunk)
79{
80    ensureBackingStore();
81
82    OwnPtr<HDC> updateChunkBitmapDC(::CreateCompatibleDC(m_backingStoreDC.get()));
83
84    // Create a bitmap.
85    BitmapInfo bitmapInfo = BitmapInfo::createBottomUp(updateChunk->rect().size());
86
87    // Duplicate the update chunk handle.
88    HANDLE updateChunkHandle;
89    BOOL result = ::DuplicateHandle(m_webView->page()->process()->processIdentifier(), updateChunk->memory(),
90                                    ::GetCurrentProcess(), &updateChunkHandle, STANDARD_RIGHTS_REQUIRED | FILE_MAP_READ | FILE_MAP_WRITE, false, DUPLICATE_CLOSE_SOURCE);
91
92    void* pixels = 0;
93    OwnPtr<HBITMAP> hBitmap(::CreateDIBSection(0, &bitmapInfo, DIB_RGB_COLORS, &pixels, updateChunkHandle, 0));
94    ::SelectObject(updateChunkBitmapDC.get(), hBitmap.get());
95
96    // BitBlt from the UpdateChunk to the backing store.
97    ::BitBlt(m_backingStoreDC.get(), updateChunk->rect().x(), updateChunk->rect().y(), updateChunk->rect().width(), updateChunk->rect().height(), updateChunkBitmapDC.get(), 0, 0, SRCCOPY);
98
99    // FIXME: We should not do this here.
100    ::CloseHandle(updateChunkHandle);
101
102    // Invalidate the WebView's HWND.
103    RECT rect = updateChunk->rect();
104    ::InvalidateRect(m_webView->window(), &rect, false);
105}
106
107} // namespace WebKit
108