1/*
2 * Copyright (C) 2011 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 "BackingStore.h"
28
29#include "ShareableBitmap.h"
30#include "UpdateInfo.h"
31#include <WebCore/BitmapInfo.h>
32#include <WebCore/GraphicsContext.h>
33#include <WebCore/IntRect.h>
34
35using namespace WebCore;
36
37namespace WebKit {
38
39class BitmapDC {
40    WTF_MAKE_NONCOPYABLE(BitmapDC);
41
42public:
43    BitmapDC(HBITMAP, HDC destinationDC);
44    ~BitmapDC();
45
46    operator HDC() const { return m_dc.get(); }
47
48private:
49    OwnPtr<HDC> m_dc;
50    HBITMAP m_originalBitmap;
51};
52
53BitmapDC::BitmapDC(HBITMAP bitmap, HDC destinationDC)
54    : m_dc(adoptPtr(::CreateCompatibleDC(destinationDC)))
55    , m_originalBitmap(static_cast<HBITMAP>(::SelectObject(m_dc.get(), bitmap)))
56{
57}
58
59BitmapDC::~BitmapDC()
60{
61    ::SelectObject(m_dc.get(), m_originalBitmap);
62}
63
64void BackingStore::paint(HDC dc, const IntRect& rect)
65{
66    ASSERT(m_bitmap);
67    ::BitBlt(dc, rect.x(), rect.y(), rect.width(), rect.height(), BitmapDC(m_bitmap.get(), dc), rect.x(), rect.y(), SRCCOPY);
68}
69
70static PassOwnPtr<HBITMAP> createBitmap(const IntSize& size)
71{
72    // FIXME: Maybe it would be better for performance to create a device-dependent bitmap here?
73    BitmapInfo info = BitmapInfo::createBottomUp(size);
74    void* bits;
75    return adoptPtr(::CreateDIBSection(0, &info, DIB_RGB_COLORS, &bits, 0, 0));
76}
77
78void BackingStore::incorporateUpdate(ShareableBitmap* bitmap, const UpdateInfo& updateInfo)
79{
80    if (!m_bitmap)
81        m_bitmap = createBitmap(m_size);
82
83    scroll(updateInfo.scrollRect, updateInfo.scrollOffset);
84
85    IntPoint updateRectLocation = updateInfo.updateRectBounds.location();
86
87    BitmapDC dc(m_bitmap.get(), 0);
88    GraphicsContext graphicsContext(dc);
89
90    // Paint all update rects.
91    for (size_t i = 0; i < updateInfo.updateRects.size(); ++i) {
92        IntRect updateRect = updateInfo.updateRects[i];
93        IntRect srcRect = updateRect;
94        srcRect.move(-updateRectLocation.x(), -updateRectLocation.y());
95
96        bitmap->paint(graphicsContext, updateRect.location(), srcRect);
97    }
98}
99
100void BackingStore::scroll(const IntRect& scrollRect, const IntSize& scrollOffset)
101{
102    if (scrollOffset.isZero())
103        return;
104
105    RECT winScrollRect = scrollRect;
106    ::ScrollDC(BitmapDC(m_bitmap.get(), 0), scrollOffset.width(), scrollOffset.height(), &winScrollRect, &winScrollRect, 0, 0);
107}
108
109} // namespace WebKit
110