1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <private/qgraphicssystem_runtime_p.h>
43 #include <private/qgraphicssystem_raster_p.h>
44 #include <private/qgraphicssystemfactory_p.h>
45 #include <private/qapplication_p.h>
46 #include <private/qwidget_p.h>
47 #include <QtCore/QDebug>
48 #include <QtCore/QTimer>
49 #include <QtGui/QBitmap>
53 static int qt_pixmap_serial = 0;
60 class QDeferredGraphicsSystemChange : public QObject
65 QDeferredGraphicsSystemChange(QRuntimeGraphicsSystem *gs, const QString& graphicsSystemName)
66 : m_graphicsSystem(gs), m_graphicsSystemName(graphicsSystemName)
72 QTimer::singleShot(0, this, SLOT(doChange()));
79 m_graphicsSystem->setGraphicsSystem(m_graphicsSystemName);
85 QRuntimeGraphicsSystem *m_graphicsSystem;
86 QString m_graphicsSystemName;
89 QRuntimePixmapData::QRuntimePixmapData(const QRuntimeGraphicsSystem *gs, PixelType type)
90 : QPixmapData(type, RuntimeClass), m_graphicsSystem(gs)
92 setSerialNumber(++qt_pixmap_serial);
95 QRuntimePixmapData::~QRuntimePixmapData()
97 if (QApplicationPrivate::graphics_system)
98 m_graphicsSystem->removePixmapData(this);
102 void QRuntimePixmapData::readBackInfo()
105 h = m_data->height();
107 is_null = m_data->isNull();
111 QPixmapData *QRuntimePixmapData::createCompatiblePixmapData() const
113 QRuntimePixmapData *rtData = new QRuntimePixmapData(m_graphicsSystem, pixelType());
114 rtData->m_data = m_data->createCompatiblePixmapData();
119 void QRuntimePixmapData::resize(int width, int height)
122 m_data->resize(width, height);
127 void QRuntimePixmapData::fromImage(const QImage &image,
128 Qt::ImageConversionFlags flags)
131 m_data->fromImage(image, flags);
136 bool QRuntimePixmapData::fromFile(const QString &filename, const char *format,
137 Qt::ImageConversionFlags flags)
141 success = m_data->fromFile(filename, format, flags);
146 bool QRuntimePixmapData::fromData(const uchar *buffer, uint len, const char *format,
147 Qt::ImageConversionFlags flags)
151 success = m_data->fromData(buffer, len, format, flags);
157 void QRuntimePixmapData::copy(const QPixmapData *data, const QRect &rect)
159 if (data->runtimeData()) {
161 m_data->copy(data->runtimeData(), rect);
165 m_data->copy(data, rect);
170 bool QRuntimePixmapData::scroll(int dx, int dy, const QRect &rect)
172 return m_data->scroll(dx, dy, rect);
176 int QRuntimePixmapData::metric(QPaintDevice::PaintDeviceMetric metric) const
178 return m_data->metric(metric);
181 void QRuntimePixmapData::fill(const QColor &color)
183 return m_data->fill(color);
186 QBitmap QRuntimePixmapData::mask() const
188 return m_data->mask();
191 void QRuntimePixmapData::setMask(const QBitmap &mask)
194 m_data->setMask(mask);
198 bool QRuntimePixmapData::hasAlphaChannel() const
200 return m_data->hasAlphaChannel();
203 QPixmap QRuntimePixmapData::transformed(const QTransform &matrix,
204 Qt::TransformationMode mode) const
206 return m_data->transformed(matrix, mode);
209 void QRuntimePixmapData::setAlphaChannel(const QPixmap &alphaChannel)
212 m_data->setAlphaChannel(alphaChannel);
216 QPixmap QRuntimePixmapData::alphaChannel() const
218 return m_data->alphaChannel();
221 QImage QRuntimePixmapData::toImage() const
223 return m_data->toImage();
226 QPaintEngine* QRuntimePixmapData::paintEngine() const
228 return m_data->paintEngine();
231 QImage* QRuntimePixmapData::buffer()
233 return m_data->buffer();
236 #if defined(Q_OS_SYMBIAN)
237 void* QRuntimePixmapData::toNativeType(NativeType type)
239 return m_data->toNativeType(type);
242 void QRuntimePixmapData::fromNativeType(void *pixmap, NativeType type)
244 m_data->fromNativeType(pixmap, type);
249 QPixmapData* QRuntimePixmapData::runtimeData() const
254 QRuntimeWindowSurface::QRuntimeWindowSurface(const QRuntimeGraphicsSystem *gs, QWidget *window)
255 : QWindowSurface(window), m_graphicsSystem(gs)
260 QRuntimeWindowSurface::~QRuntimeWindowSurface()
262 if (QApplicationPrivate::graphics_system)
263 m_graphicsSystem->removeWindowSurface(this);
266 QPaintDevice *QRuntimeWindowSurface::paintDevice()
268 return m_windowSurface->paintDevice();
271 void QRuntimeWindowSurface::flush(QWidget *widget, const QRegion ®ion,
272 const QPoint &offset)
274 m_windowSurface->flush(widget, region, offset);
276 int destroyPolicy = m_graphicsSystem->windowSurfaceDestroyPolicy();
277 if(m_pendingWindowSurface &&
278 destroyPolicy == QRuntimeGraphicsSystem::DestroyAfterFirstFlush) {
280 qDebug() << "QRuntimeWindowSurface::flush() - destroy pending window surface";
282 m_pendingWindowSurface.reset();
286 void QRuntimeWindowSurface::setGeometry(const QRect &rect)
288 QWindowSurface::setGeometry(rect);
289 m_windowSurface->setGeometry(rect);
292 bool QRuntimeWindowSurface::scroll(const QRegion &area, int dx, int dy)
294 return m_windowSurface->scroll(area, dx, dy);
297 void QRuntimeWindowSurface::beginPaint(const QRegion &rgn)
299 m_windowSurface->beginPaint(rgn);
302 void QRuntimeWindowSurface::endPaint(const QRegion &rgn)
304 m_windowSurface->endPaint(rgn);
307 QImage* QRuntimeWindowSurface::buffer(const QWidget *widget)
309 return m_windowSurface->buffer(widget);
312 QPixmap QRuntimeWindowSurface::grabWidget(const QWidget *widget, const QRect& rectangle) const
314 return m_windowSurface->grabWidget(widget, rectangle);
317 QPoint QRuntimeWindowSurface::offset(const QWidget *widget) const
319 return m_windowSurface->offset(widget);
322 QWindowSurface::WindowSurfaceFeatures QRuntimeWindowSurface::features() const
324 return m_windowSurface->features();
327 QRuntimeGraphicsSystem::QRuntimeGraphicsSystem()
328 : m_windowSurfaceDestroyPolicy(DestroyImmediately),
331 QApplicationPrivate::runtime_graphics_system = true;
333 #ifdef QT_DEFAULT_RUNTIME_SYSTEM
334 m_graphicsSystemName = QLatin1String(QT_DEFAULT_RUNTIME_SYSTEM);
335 if (m_graphicsSystemName.isNull())
337 m_graphicsSystemName = QLatin1String("raster");
340 m_windowSurfaceDestroyPolicy = DestroyAfterFirstFlush;
343 m_graphicsSystem = QGraphicsSystemFactory::create(m_graphicsSystemName);
345 QApplicationPrivate::graphics_system_name = QLatin1String("runtime");
349 QPixmapData *QRuntimeGraphicsSystem::createPixmapData(QPixmapData::PixelType type) const
351 Q_ASSERT(m_graphicsSystem);
352 QPixmapData *data = m_graphicsSystem->createPixmapData(type);
354 QRuntimePixmapData *rtData = new QRuntimePixmapData(this, type);
355 rtData->m_data = data;
356 m_pixmapDatas << rtData;
361 QWindowSurface *QRuntimeGraphicsSystem::createWindowSurface(QWidget *widget) const
363 Q_ASSERT(m_graphicsSystem);
364 QRuntimeWindowSurface *rtSurface = new QRuntimeWindowSurface(this, widget);
365 rtSurface->m_windowSurface.reset(m_graphicsSystem->createWindowSurface(widget));
366 widget->setWindowSurface(rtSurface);
367 m_windowSurfaces << rtSurface;
371 void QRuntimeGraphicsSystem::setGraphicsSystem(const QString &name)
373 if (m_graphicsSystemName == name)
376 qDebug() << "QRuntimeGraphicsSystem::setGraphicsSystem( " << name << " )";
378 QGraphicsSystem *oldSystem = m_graphicsSystem;
379 m_graphicsSystem = QGraphicsSystemFactory::create(name);
380 m_graphicsSystemName = name;
382 Q_ASSERT(m_graphicsSystem);
384 m_pendingGraphicsSystemName = QString();
386 for (int i = 0; i < m_pixmapDatas.size(); ++i) {
387 QRuntimePixmapData *proxy = m_pixmapDatas.at(i);
388 QPixmapData *newData = m_graphicsSystem->createPixmapData(proxy->m_data);
389 newData->fromImage(proxy->m_data->toImage(), Qt::NoOpaqueDetection);
390 delete proxy->m_data;
391 proxy->m_data = newData;
392 proxy->readBackInfo();
395 for (int i = 0; i < m_windowSurfaces.size(); ++i) {
396 QRuntimeWindowSurface *proxy = m_windowSurfaces.at(i);
397 QWidget *widget = proxy->m_windowSurface->window();
399 if(m_windowSurfaceDestroyPolicy == DestroyAfterFirstFlush)
400 proxy->m_pendingWindowSurface.reset(proxy->m_windowSurface.take());
402 QWindowSurface *newWindowSurface = m_graphicsSystem->createWindowSurface(widget);
403 newWindowSurface->setGeometry(proxy->geometry());
405 proxy->m_windowSurface.reset(newWindowSurface);
406 qt_widget_private(widget)->invalidateBuffer(widget->rect());
412 void QRuntimeGraphicsSystem::removePixmapData(QRuntimePixmapData *pixmapData) const
414 int index = m_pixmapDatas.lastIndexOf(pixmapData);
415 m_pixmapDatas.removeAt(index);
418 void QRuntimeGraphicsSystem::removeWindowSurface(QRuntimeWindowSurface *windowSurface) const
420 int index = m_windowSurfaces.lastIndexOf(windowSurface);
421 m_windowSurfaces.removeAt(index);
424 #include "qgraphicssystem_runtime.moc"