BackingStoreWin.cpp [plain text]
#include "config.h"
#include "BackingStore.h"
#include "ShareableBitmap.h"
#include "UpdateInfo.h"
#include <WebCore/BitmapInfo.h>
#include <WebCore/GraphicsContext.h>
#include <WebCore/IntRect.h>
using namespace WebCore;
namespace WebKit {
class BitmapDC {
WTF_MAKE_NONCOPYABLE(BitmapDC);
public:
BitmapDC(HBITMAP, HDC destinationDC);
~BitmapDC();
operator HDC() const { return m_dc.get(); }
private:
OwnPtr<HDC> m_dc;
HBITMAP m_originalBitmap;
};
BitmapDC::BitmapDC(HBITMAP bitmap, HDC destinationDC)
: m_dc(adoptPtr(::CreateCompatibleDC(destinationDC)))
, m_originalBitmap(static_cast<HBITMAP>(::SelectObject(m_dc.get(), bitmap)))
{
}
BitmapDC::~BitmapDC()
{
::SelectObject(m_dc.get(), m_originalBitmap);
}
void BackingStore::paint(HDC dc, const IntRect& rect)
{
ASSERT(m_bitmap);
::BitBlt(dc, rect.x(), rect.y(), rect.width(), rect.height(), BitmapDC(m_bitmap.get(), dc), rect.x(), rect.y(), SRCCOPY);
}
static PassOwnPtr<HBITMAP> createBitmap(const IntSize& size)
{
BitmapInfo info = BitmapInfo::createBottomUp(size);
void* bits;
return adoptPtr(::CreateDIBSection(0, &info, DIB_RGB_COLORS, &bits, 0, 0));
}
void BackingStore::incorporateUpdate(ShareableBitmap* bitmap, const UpdateInfo& updateInfo)
{
if (!m_bitmap)
m_bitmap = createBitmap(m_size);
scroll(updateInfo.scrollRect, updateInfo.scrollOffset);
IntPoint updateRectLocation = updateInfo.updateRectBounds.location();
BitmapDC backingStoreDC(m_bitmap.get(), 0);
HDC bitmapDC = bitmap->windowsContext();
for (size_t i = 0; i < updateInfo.updateRects.size(); ++i) {
IntRect updateRect = updateInfo.updateRects[i];
IntRect srcRect = updateRect;
srcRect.move(-updateRectLocation.x(), -updateRectLocation.y());
::BitBlt(backingStoreDC, updateRect.location().x(), updateRect.location().y(), updateRect.size().width(), updateRect.size().height(),
bitmapDC, srcRect.x(), srcRect.y(), SRCCOPY);
}
}
void BackingStore::scroll(const IntRect& scrollRect, const IntSize& scrollOffset)
{
if (scrollOffset.isZero())
return;
RECT winScrollRect = scrollRect;
::ScrollDC(BitmapDC(m_bitmap.get(), 0), scrollOffset.width(), scrollOffset.height(), &winScrollRect, &winScrollRect, 0, 0);
}
}