summaryrefslogtreecommitdiffstats
path: root/src/gui/painting/qwindowsurface_d3d.cpp
blob: 8f9403f392a2a6a6230900c4b404aa969fbdbb30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights.  These rights are described in the Nokia Qt LGPL
** Exception version 1.1, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

//#define D3D_DEBUG_BACKBUFFER

#include <QtGui/QPaintDevice>
#include <QtGui/QWidget>
#include "qdebug.h"

#include "qpaintengine_d3d_p.h"
#include "qwindowsurface_d3d_p.h"
#include "private/qwidget_p.h"
#include "private/qbackingstore_p.h"

#include <d3d9.h>

QT_BEGIN_NAMESPACE

extern QDirect3DPaintEngine *qt_d3dEngine();

struct QD3DWindowSurfacePrivate
{
    QSize m_lastSize;
    QWidget *m_widget;
};

QD3DWindowSurface::QD3DWindowSurface(QWidget *window)
    : QWindowSurface(window), d_ptr(new QD3DWindowSurfacePrivate)
{
    Q_ASSERT(window->isTopLevel());
    d_ptr->m_widget = window;
}


QD3DWindowSurface::~QD3DWindowSurface()
{
    delete d_ptr;
}

QPaintDevice *QD3DWindowSurface::paintDevice()
{
    return d_ptr->m_widget;
}


void QD3DWindowSurface::flush(QWidget *widget, const QRegion &rgn, const QPoint &offset)
{
    QPoint wOffset = qt_qwidget_data(widget)->wrect.topLeft();

    QDirect3DPaintEngine *engine = qt_d3dEngine();
    LPDIRECT3DSWAPCHAIN9 swapchain = engine->swapChain(d_ptr->m_widget);

    if (swapchain) {
        QRect br = rgn.boundingRect();
        QRect wbr = br.translated(-wOffset);

        RECT destrect;
        destrect.left   = wbr.x();
        destrect.top    = wbr.y();
        destrect.right  = destrect.left + wbr.width();
        destrect.bottom = destrect.top  + wbr.height();

        RECT srcrect;
        srcrect.left    = br.x() + offset.x();
        srcrect.top     = br.y() + offset.y();
        srcrect.right   = wbr.width() + srcrect.left;
        srcrect.bottom  = wbr.height() + srcrect.top;
        int devwidth = d_ptr->m_lastSize.width();
        int devheight = d_ptr->m_lastSize.height();

        if (devwidth <= srcrect.right) {
            int diff = srcrect.right - devwidth;
            srcrect.right -= diff;
            destrect.right -= diff;
            if (srcrect.right <= srcrect.left)
                return;
        }
        if (devheight <= srcrect.bottom) {
            int diff = srcrect.bottom - devheight;
            srcrect.bottom -= diff;
            destrect.bottom -= diff;
            if (srcrect.bottom <= srcrect.top)
                return;
        }

        if (FAILED(swapchain->Present(&srcrect, &destrect, widget->winId(), 0, 0)))
            qWarning("QDirect3DPaintEngine: failed to present back buffer.");

#ifdef D3D_DEBUG_BACKBUFFER
        qDebug() << widget << srcrect.left << srcrect.top << wbr.width() << wbr.height() << "Dest: " << destrect.left << destrect.top;
        IDirect3DSurface9 *surface;
        swapchain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &surface);
        QString filename("C:\\test.bmp");
        D3DXSaveSurfaceToFile(filename.utf16(), D3DXIFF_BMP, surface, 0, 0);
        surface->Release();
#endif
    }
}

void QD3DWindowSurface::setGeometry(const QRect &rect)
{
    if (rect.isEmpty())
        qt_d3dEngine()->releaseSwapChain(d_ptr->m_widget);

    d_ptr->m_lastSize = rect.size();
    QWindowSurface::setGeometry(rect);
}


bool QD3DWindowSurface::scroll(const QRegion &area, int dx, int dy)
{
    QDirect3DPaintEngine *engine = qt_d3dEngine();
    QRect rect = area.boundingRect();

    RECT destrect;
    destrect.left   = rect.x()+dx;
    destrect.top    = rect.y()+dy;
    destrect.right  = rect.width() + destrect.left;
    destrect.bottom = rect.height() + destrect.top;

    RECT srcrect;
    srcrect.left    = rect.x();
    srcrect.top     = rect.y();
    srcrect.right   = rect.width() + srcrect.left;
    srcrect.bottom  = rect.height() + srcrect.top;

    engine->scroll(d_ptr->m_widget, srcrect, destrect);
    return true;
}

QT_END_NAMESPACE