author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Wed, 18 Aug 2010 10:37:55 +0300 | |
changeset 33 | 3e2da88830cd |
parent 18 | 2f34d5167611 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtGui module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include <qdebug.h> |
|
43 |
||
44 |
#include <qglobal.h> // for Q_WS_WIN define (non-PCH) |
|
45 |
#ifdef Q_WS_WIN |
|
46 |
#include <qlibrary.h> |
|
47 |
#include <qt_windows.h> |
|
48 |
#endif |
|
49 |
||
50 |
#include <QtGui/qpaintdevice.h> |
|
51 |
#include <QtGui/qwidget.h> |
|
52 |
||
53 |
#include "private/qwindowsurface_raster_p.h" |
|
54 |
#include "private/qnativeimage_p.h" |
|
55 |
#include "private/qwidget_p.h" |
|
56 |
||
57 |
#ifdef Q_WS_X11 |
|
58 |
#include "private/qpixmap_x11_p.h" |
|
59 |
#include "private/qt_x11_p.h" |
|
60 |
#include "private/qwidget_p.h" |
|
61 |
#include "qx11info_x11.h" |
|
62 |
#endif |
|
63 |
#include "private/qdrawhelper_p.h" |
|
64 |
||
65 |
#ifdef Q_WS_MAC |
|
66 |
#include <private/qt_cocoa_helpers_mac_p.h> |
|
67 |
#endif |
|
68 |
||
69 |
QT_BEGIN_NAMESPACE |
|
70 |
||
71 |
class QRasterWindowSurfacePrivate |
|
72 |
{ |
|
73 |
public: |
|
74 |
QNativeImage *image; |
|
75 |
||
76 |
#ifdef Q_WS_X11 |
|
77 |
GC gc; |
|
78 |
#ifndef QT_NO_XRENDER |
|
79 |
uint translucentBackground : 1; |
|
80 |
#endif |
|
81 |
#endif |
|
82 |
uint inSetGeometry : 1; |
|
83 |
}; |
|
84 |
||
85 |
QRasterWindowSurface::QRasterWindowSurface(QWidget *window) |
|
86 |
: QWindowSurface(window), d_ptr(new QRasterWindowSurfacePrivate) |
|
87 |
{ |
|
88 |
#ifdef Q_WS_X11 |
|
89 |
d_ptr->gc = XCreateGC(X11->display, window->handle(), 0, 0); |
|
90 |
#ifndef QT_NO_XRENDER |
|
91 |
d_ptr->translucentBackground = X11->use_xrender |
|
92 |
&& window->x11Info().depth() == 32; |
|
93 |
#endif |
|
94 |
#endif |
|
95 |
d_ptr->image = 0; |
|
96 |
d_ptr->inSetGeometry = false; |
|
97 |
setStaticContentsSupport(true); |
|
98 |
} |
|
99 |
||
100 |
||
101 |
QRasterWindowSurface::~QRasterWindowSurface() |
|
102 |
{ |
|
103 |
#ifdef Q_WS_X11 |
|
104 |
XFreeGC(X11->display, d_ptr->gc); |
|
105 |
#endif |
|
106 |
if (d_ptr->image) |
|
107 |
delete d_ptr->image; |
|
108 |
} |
|
109 |
||
110 |
||
111 |
QPaintDevice *QRasterWindowSurface::paintDevice() |
|
112 |
{ |
|
113 |
return &d_ptr->image->image; |
|
114 |
} |
|
115 |
||
116 |
void QRasterWindowSurface::beginPaint(const QRegion &rgn) |
|
117 |
{ |
|
118 |
#if (defined(Q_WS_X11) && !defined(QT_NO_XRENDER)) || (defined(Q_WS_WIN) && !defined(Q_WS_WINCE)) |
|
119 |
if (!qt_widget_private(window())->isOpaque && window()->testAttribute(Qt::WA_TranslucentBackground)) { |
|
120 |
#if defined(Q_WS_WIN) && !defined(Q_WS_WINCE) |
|
121 |
if (d_ptr->image->image.format() != QImage::Format_ARGB32_Premultiplied) |
|
122 |
prepareBuffer(QImage::Format_ARGB32_Premultiplied, window()); |
|
123 |
#endif |
|
124 |
QPainter p(&d_ptr->image->image); |
|
125 |
p.setCompositionMode(QPainter::CompositionMode_Source); |
|
126 |
const QVector<QRect> rects = rgn.rects(); |
|
127 |
const QColor blank = Qt::transparent; |
|
128 |
for (QVector<QRect>::const_iterator it = rects.begin(); it != rects.end(); ++it) { |
|
129 |
p.fillRect(*it, blank); |
|
130 |
} |
|
131 |
} |
|
132 |
#else |
|
133 |
Q_UNUSED(rgn); |
|
134 |
#endif |
|
135 |
} |
|
136 |
||
137 |
void QRasterWindowSurface::flush(QWidget *widget, const QRegion &rgn, const QPoint &offset) |
|
138 |
{ |
|
139 |
Q_D(QRasterWindowSurface); |
|
140 |
||
141 |
// Not ready for painting yet, bail out. This can happen in |
|
142 |
// QWidget::create_sys() |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
143 |
if (!d->image || rgn.rectCount() == 0) |
0 | 144 |
return; |
145 |
||
146 |
#ifdef Q_WS_WIN |
|
147 |
QRect br = rgn.boundingRect(); |
|
148 |
||
149 |
#ifndef Q_WS_WINCE |
|
150 |
if (!qt_widget_private(window())->isOpaque |
|
151 |
&& window()->testAttribute(Qt::WA_TranslucentBackground) |
|
152 |
&& (qt_widget_private(window())->data.window_flags & Qt::FramelessWindowHint)) |
|
153 |
{ |
|
154 |
QRect r = window()->frameGeometry(); |
|
155 |
QPoint frameOffset = qt_widget_private(window())->frameStrut().topLeft(); |
|
156 |
QRect dirtyRect = br.translated(offset + frameOffset); |
|
157 |
||
158 |
SIZE size = {r.width(), r.height()}; |
|
159 |
POINT ptDst = {r.x(), r.y()}; |
|
160 |
POINT ptSrc = {0, 0}; |
|
161 |
BLENDFUNCTION blend = {AC_SRC_OVER, 0, (int)(255.0 * window()->windowOpacity()), Q_AC_SRC_ALPHA}; |
|
162 |
RECT dirty = {dirtyRect.x(), dirtyRect.y(), |
|
163 |
dirtyRect.x() + dirtyRect.width(), dirtyRect.y() + dirtyRect.height()}; |
|
164 |
Q_UPDATELAYEREDWINDOWINFO info = {sizeof(info), NULL, &ptDst, &size, d->image->hdc, &ptSrc, 0, &blend, Q_ULW_ALPHA, &dirty}; |
|
165 |
ptrUpdateLayeredWindowIndirect(window()->internalWinId(), &info); |
|
166 |
} else |
|
167 |
#endif |
|
168 |
{ |
|
169 |
QPoint wOffset = qt_qwidget_data(widget)->wrect.topLeft(); |
|
170 |
||
171 |
HDC widget_dc = widget->getDC(); |
|
172 |
||
173 |
QRect wbr = br.translated(-wOffset); |
|
174 |
BitBlt(widget_dc, wbr.x(), wbr.y(), wbr.width(), wbr.height(), |
|
175 |
d->image->hdc, br.x() + offset.x(), br.y() + offset.y(), SRCCOPY); |
|
176 |
widget->releaseDC(widget_dc); |
|
177 |
} |
|
178 |
||
179 |
#ifndef QT_NO_DEBUG |
|
180 |
static bool flush = !qgetenv("QT_FLUSH_WINDOWSURFACE").isEmpty(); |
|
181 |
if (flush) { |
|
182 |
SelectObject(qt_win_display_dc(), GetStockObject(BLACK_BRUSH)); |
|
183 |
Rectangle(qt_win_display_dc(), 0, 0, d->image->width() + 2, d->image->height() + 2); |
|
184 |
BitBlt(qt_win_display_dc(), 1, 1, d->image->width(), d->image->height(), |
|
185 |
d->image->hdc, 0, 0, SRCCOPY); |
|
186 |
} |
|
187 |
#endif |
|
188 |
||
189 |
#endif |
|
190 |
||
191 |
#ifdef Q_WS_X11 |
|
192 |
extern void *qt_getClipRects(const QRegion &r, int &num); // in qpaintengine_x11.cpp |
|
193 |
extern QWidgetData* qt_widget_data(QWidget *); |
|
194 |
QPoint wOffset = qt_qwidget_data(widget)->wrect.topLeft(); |
|
195 |
||
196 |
if (widget->window() != window()) { |
|
197 |
XFreeGC(X11->display, d_ptr->gc); |
|
198 |
d_ptr->gc = XCreateGC(X11->display, widget->handle(), 0, 0); |
|
199 |
} |
|
200 |
||
201 |
QRegion wrgn(rgn); |
|
202 |
if (!wOffset.isNull()) |
|
203 |
wrgn.translate(-wOffset); |
|
204 |
QRect wbr = wrgn.boundingRect(); |
|
205 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
206 |
if (wrgn.rectCount() != 1) { |
0 | 207 |
int num; |
208 |
XRectangle *rects = (XRectangle *)qt_getClipRects(wrgn, num); |
|
209 |
XSetClipRectangles(X11->display, d_ptr->gc, 0, 0, rects, num, YXBanded); |
|
210 |
} |
|
211 |
||
212 |
QRect br = rgn.boundingRect().translated(offset); |
|
213 |
#ifndef QT_NO_MITSHM |
|
214 |
if (d_ptr->image->xshmpm) { |
|
215 |
XCopyArea(X11->display, d_ptr->image->xshmpm, widget->handle(), d_ptr->gc, |
|
216 |
br.x(), br.y(), br.width(), br.height(), wbr.x(), wbr.y()); |
|
217 |
XSync(X11->display, False); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
218 |
} else if (d_ptr->image->xshmimg) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
219 |
const QImage &src = d->image->image; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
220 |
br = br.intersected(src.rect()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
221 |
XShmPutImage(X11->display, widget->handle(), d_ptr->gc, d_ptr->image->xshmimg, |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
222 |
br.x(), br.y(), wbr.x(), wbr.y(), br.width(), br.height(), False); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
223 |
XSync(X11->display, False); |
0 | 224 |
} else |
225 |
#endif |
|
226 |
{ |
|
227 |
const QImage &src = d->image->image; |
|
228 |
br = br.intersected(src.rect()); |
|
229 |
if (src.format() != QImage::Format_RGB32 || widget->x11Info().depth() < 24) { |
|
230 |
Q_ASSERT(src.depth() >= 16); |
|
231 |
const QImage sub_src(src.scanLine(br.y()) + br.x() * (uint(src.depth()) / 8), |
|
232 |
br.width(), br.height(), src.bytesPerLine(), src.format()); |
|
233 |
QX11PixmapData *data = new QX11PixmapData(QPixmapData::PixmapType); |
|
234 |
data->xinfo = widget->x11Info(); |
|
235 |
data->fromImage(sub_src, Qt::NoOpaqueDetection); |
|
236 |
QPixmap pm = QPixmap(data); |
|
237 |
XCopyArea(X11->display, pm.handle(), widget->handle(), d_ptr->gc, 0 , 0 , br.width(), br.height(), wbr.x(), wbr.y()); |
|
238 |
} else { |
|
239 |
// qpaintengine_x11.cpp |
|
240 |
extern void qt_x11_drawImage(const QRect &rect, const QPoint &pos, const QImage &image, Drawable hd, GC gc, Display *dpy, Visual *visual, int depth); |
|
241 |
qt_x11_drawImage(br, wbr.topLeft(), src, widget->handle(), d_ptr->gc, X11->display, (Visual *)widget->x11Info().visual(), widget->x11Info().depth()); |
|
242 |
} |
|
243 |
} |
|
244 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
245 |
if (wrgn.rectCount() != 1) |
0 | 246 |
XSetClipMask(X11->display, d_ptr->gc, XNone); |
247 |
#endif // FALCON |
|
248 |
||
249 |
#ifdef Q_WS_MAC |
|
250 |
||
251 |
// qDebug() << "Flushing" << widget << rgn << offset; |
|
252 |
||
253 |
// d->image->image.save("flush.png"); |
|
254 |
||
255 |
Q_UNUSED(offset); |
|
256 |
// Get a context for the widget. |
|
257 |
#ifndef QT_MAC_USE_COCOA |
|
258 |
CGContextRef context; |
|
259 |
CGrafPtr port = GetWindowPort(qt_mac_window_for(widget)); |
|
260 |
QDBeginCGContext(port, &context); |
|
261 |
#else |
|
262 |
extern CGContextRef qt_mac_graphicsContextFor(QWidget *); |
|
263 |
CGContextRef context = qt_mac_graphicsContextFor(widget); |
|
264 |
#endif |
|
265 |
CGContextSaveGState(context); |
|
266 |
||
267 |
// Flip context. |
|
268 |
CGContextTranslateCTM(context, 0, widget->height()); |
|
269 |
CGContextScaleCTM(context, 1, -1); |
|
270 |
||
271 |
// Clip to region. |
|
272 |
const QVector<QRect> &rects = rgn.rects(); |
|
273 |
for (int i = 0; i < rects.size(); ++i) { |
|
274 |
const QRect &rect = rects.at(i); |
|
275 |
CGContextAddRect(context, CGRectMake(rect.x(), rect.y(), rect.width(), rect.height())); |
|
276 |
} |
|
277 |
CGContextClip(context); |
|
278 |
||
279 |
QRect r = rgn.boundingRect(); |
|
280 |
const CGRect area = CGRectMake(r.x(), r.y(), r.width(), r.height()); |
|
281 |
CGImageRef image = CGBitmapContextCreateImage(d->image->cg); |
|
282 |
CGImageRef subImage = CGImageCreateWithImageInRect(image, area); |
|
283 |
||
284 |
qt_mac_drawCGImage(context, &area, subImage); |
|
285 |
CGImageRelease(subImage); |
|
286 |
CGImageRelease(image); |
|
287 |
||
288 |
// CGSize size = { d->image->image.width(), d->image->image.height() }; |
|
289 |
// CGLayerRef layer = CGLayerCreateWithContext(d->image->cg, size, 0); |
|
290 |
// CGPoint pt = { 0, 0 }; |
|
291 |
// CGContextDrawLayerAtPoint(context, pt, layer); |
|
292 |
// CGLayerRelease(layer); |
|
293 |
||
294 |
// Restore context. |
|
295 |
CGContextRestoreGState(context); |
|
296 |
#ifndef QT_MAC_USE_COCOA |
|
297 |
QDEndCGContext(port, &context); |
|
298 |
#endif |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
299 |
#endif // Q_WS_MAC |
0 | 300 |
|
301 |
#ifdef Q_OS_SYMBIAN |
|
302 |
Q_UNUSED(widget); |
|
303 |
Q_UNUSED(rgn); |
|
304 |
Q_UNUSED(offset); |
|
305 |
#endif |
|
306 |
} |
|
307 |
||
308 |
void QRasterWindowSurface::setGeometry(const QRect &rect) |
|
309 |
{ |
|
310 |
QWindowSurface::setGeometry(rect); |
|
311 |
Q_D(QRasterWindowSurface); |
|
312 |
d->inSetGeometry = true; |
|
313 |
if (d->image == 0 || d->image->width() < rect.width() || d->image->height() < rect.height()) { |
|
314 |
#if (defined(Q_WS_X11) && !defined(QT_NO_XRENDER)) || (defined(Q_WS_WIN) && !defined(Q_WS_WINCE)) |
|
315 |
#ifndef Q_WS_WIN |
|
316 |
if (d_ptr->translucentBackground) |
|
317 |
#else |
|
318 |
if (!qt_widget_private(window())->isOpaque) |
|
319 |
#endif |
|
320 |
prepareBuffer(QImage::Format_ARGB32_Premultiplied, window()); |
|
321 |
else |
|
322 |
#endif |
|
323 |
prepareBuffer(QNativeImage::systemFormat(), window()); |
|
324 |
} |
|
325 |
d->inSetGeometry = false; |
|
326 |
} |
|
327 |
||
328 |
// from qwindowsurface.cpp |
|
329 |
extern void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset); |
|
330 |
||
331 |
bool QRasterWindowSurface::scroll(const QRegion &area, int dx, int dy) |
|
332 |
{ |
|
333 |
#ifdef Q_WS_WIN |
|
334 |
Q_D(QRasterWindowSurface); |
|
335 |
||
336 |
if (!d->image || !d->image->hdc) |
|
337 |
return false; |
|
338 |
||
339 |
QRect rect = area.boundingRect(); |
|
340 |
BitBlt(d->image->hdc, rect.x()+dx, rect.y()+dy, rect.width(), rect.height(), |
|
341 |
d->image->hdc, rect.x(), rect.y(), SRCCOPY); |
|
342 |
||
343 |
return true; |
|
344 |
#else |
|
345 |
Q_D(QRasterWindowSurface); |
|
346 |
||
347 |
if (!d->image || d->image->image.isNull()) |
|
348 |
return false; |
|
349 |
||
350 |
const QVector<QRect> rects = area.rects(); |
|
351 |
for (int i = 0; i < rects.size(); ++i) |
|
352 |
qt_scrollRectInImage(d->image->image, rects.at(i), QPoint(dx, dy)); |
|
353 |
||
354 |
return true; |
|
355 |
#endif |
|
356 |
} |
|
357 |
||
358 |
||
359 |
void QRasterWindowSurface::prepareBuffer(QImage::Format format, QWidget *widget) |
|
360 |
{ |
|
361 |
Q_D(QRasterWindowSurface); |
|
362 |
||
363 |
int width = window()->width(); |
|
364 |
int height = window()->height(); |
|
365 |
if (d->image) { |
|
366 |
width = qMax(d->image->width(), width); |
|
367 |
height = qMax(d->image->height(), height); |
|
368 |
} |
|
369 |
||
370 |
if (width == 0 || height == 0) { |
|
371 |
delete d->image; |
|
372 |
d->image = 0; |
|
373 |
return; |
|
374 |
} |
|
375 |
||
376 |
QNativeImage *oldImage = d->image; |
|
377 |
||
378 |
d->image = new QNativeImage(width, height, format, false, widget); |
|
379 |
||
380 |
if (oldImage && d->inSetGeometry && hasStaticContents()) { |
|
381 |
// Make sure we use the const version of bits() (no detach). |
|
382 |
const uchar *src = const_cast<const QImage &>(oldImage->image).bits(); |
|
383 |
uchar *dst = d->image->image.bits(); |
|
384 |
||
385 |
const int srcBytesPerLine = oldImage->image.bytesPerLine(); |
|
386 |
const int dstBytesPerLine = d->image->image.bytesPerLine(); |
|
387 |
const int bytesPerPixel = oldImage->image.depth() >> 3; |
|
388 |
||
389 |
QRegion staticRegion(staticContents()); |
|
390 |
// Make sure we're inside the boundaries of the old image. |
|
391 |
staticRegion &= QRect(0, 0, oldImage->image.width(), oldImage->image.height()); |
|
392 |
const QVector<QRect> &rects = staticRegion.rects(); |
|
393 |
const QRect *srcRect = rects.constData(); |
|
394 |
||
395 |
// Copy the static content of the old image into the new one. |
|
396 |
int numRectsLeft = rects.size(); |
|
397 |
do { |
|
398 |
const int bytesOffset = srcRect->x() * bytesPerPixel; |
|
399 |
const int dy = srcRect->y(); |
|
400 |
||
401 |
// Adjust src and dst to point to the right offset. |
|
402 |
const uchar *s = src + dy * srcBytesPerLine + bytesOffset; |
|
403 |
uchar *d = dst + dy * dstBytesPerLine + bytesOffset; |
|
404 |
const int numBytes = srcRect->width() * bytesPerPixel; |
|
405 |
||
406 |
int numScanLinesLeft = srcRect->height(); |
|
407 |
do { |
|
408 |
::memcpy(d, s, numBytes); |
|
409 |
d += dstBytesPerLine; |
|
410 |
s += srcBytesPerLine; |
|
411 |
} while (--numScanLinesLeft); |
|
412 |
||
413 |
++srcRect; |
|
414 |
} while (--numRectsLeft); |
|
415 |
} |
|
416 |
||
417 |
delete oldImage; |
|
418 |
} |
|
419 |
||
420 |
QT_END_NAMESPACE |