2 Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
21 #include "TextureMapperQt.h"
23 #include <QtCore/qdebug.h>
24 #include <QtGui/qpaintengine.h>
25 #include <QtGui/qpixmap.h>
27 #if USE(TEXTURE_MAPPER_GL)
28 # include "opengl/TextureMapperGL.h"
33 void BitmapTextureQt::destroy()
35 if (m_pixmap.paintingActive())
36 qFatal("Destroying an active pixmap");
40 void BitmapTextureQt::reset(const IntSize& size, bool isOpaque)
42 BitmapTexture::reset(size, isOpaque);
44 if (size.width() > m_pixmap.size().width() || size.height() > m_pixmap.size().height() || m_pixmap.isNull())
45 m_pixmap = QPixmap(size.width(), size.height());
47 m_pixmap.fill(Qt::transparent);
50 PlatformGraphicsContext* BitmapTextureQt::beginPaint(const IntRect& dirtyRect)
52 m_painter.begin(&m_pixmap);
53 TextureMapperQt::initialize(&m_painter);
54 m_painter.setCompositionMode(QPainter::CompositionMode_Clear);
55 m_painter.fillRect(QRect(dirtyRect), Qt::transparent);
56 m_painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
60 void BitmapTextureQt::endPaint()
65 bool BitmapTextureQt::save(const String& path)
67 return m_pixmap.save(path, "PNG");
70 void BitmapTextureQt::setContentsToImage(Image* image)
74 const QPixmap* pixmap = image->nativeImageForCurrentFrame();
77 BitmapTexture::reset(pixmap->size(), !pixmap->hasAlphaChannel());
81 void BitmapTextureQt::pack()
83 if (m_pixmap.isNull())
86 m_image = m_pixmap.toImage();
91 void BitmapTextureQt::unpack()
97 m_pixmap = QPixmap::fromImage(m_image);
101 void TextureMapperQt::beginClip(const TransformationMatrix& matrix, const FloatRect& rect)
103 QPainter* painter = currentPainter();
105 QTransform prevTransform = painter->transform();
106 painter->setTransform(matrix, false);
107 painter->setClipRect(rect);
108 painter->setTransform(prevTransform, false);
111 void TextureMapperQt::endClip()
113 currentPainter()->restore();
116 IntSize TextureMapperQt::viewportSize() const
118 return IntSize(m_painter->device()->width(), m_painter->device()->height());
122 TextureMapperQt::TextureMapperQt()
123 : m_currentSurface(0)
127 void TextureMapperQt::setGraphicsContext(GraphicsContext* context)
130 m_painter = context->platformContext();
131 initialize(m_painter);
134 GraphicsContext* TextureMapperQt::graphicsContext()
139 void TextureMapperQt::bindSurface(BitmapTexture* surface)
141 if (m_currentSurface == surface)
143 if (m_currentSurface)
144 m_currentSurface->m_painter.end();
146 m_currentSurface = 0;
149 BitmapTextureQt* surfaceQt = static_cast<BitmapTextureQt*>(surface);
150 if (!surfaceQt->m_painter.isActive())
151 surfaceQt->m_painter.begin(&surfaceQt->m_pixmap);
152 m_currentSurface = surfaceQt;
156 void TextureMapperQt::drawTexture(const BitmapTexture& texture, const FloatRect& targetRect, const TransformationMatrix& matrix, float opacity, const BitmapTexture* maskTexture)
158 const BitmapTextureQt& textureQt = static_cast<const BitmapTextureQt&>(texture);
159 QPainter* painter = m_painter;
160 QPixmap pixmap = textureQt.m_pixmap;
161 if (m_currentSurface)
162 painter = &m_currentSurface->m_painter;
164 if (maskTexture && maskTexture->isValid()) {
165 const BitmapTextureQt* mask = static_cast<const BitmapTextureQt*>(maskTexture);
166 QPixmap intermediatePixmap(pixmap.size());
167 intermediatePixmap.fill(Qt::transparent);
168 QPainter maskPainter(&intermediatePixmap);
169 maskPainter.setCompositionMode(QPainter::CompositionMode_Source);
170 maskPainter.drawPixmap(0, 0, pixmap);
171 maskPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
172 maskPainter.drawPixmap(QRect(0, 0, pixmap.width(), pixmap.height()), mask->m_pixmap, mask->sourceRect());
174 pixmap = intermediatePixmap;
177 const qreal prevOpacity = painter->opacity();
178 const QTransform prevTransform = painter->transform();
179 painter->setOpacity(opacity);
180 painter->setTransform(matrix, true);
181 painter->drawPixmap(targetRect, pixmap, FloatRect(textureQt.sourceRect()));
182 painter->setTransform(prevTransform);
183 painter->setOpacity(prevOpacity);
186 PassOwnPtr<TextureMapper> TextureMapper::create(GraphicsContext* context)
188 #if USE(TEXTURE_MAPPER_GL)
189 if (context && context->platformContext()->paintEngine()->type() == QPaintEngine::OpenGL2)
190 return adoptPtr(new TextureMapperGL);
192 return adoptPtr(new TextureMapperQt);
195 PassRefPtr<BitmapTexture> TextureMapperQt::createTexture()
197 return adoptRef(new BitmapTextureQt());
200 BitmapTextureQt::BitmapTextureQt()
206 void TextureMapperQt::beginPainting()
211 void TextureMapperQt::endPainting()
213 m_painter->restore();
216 #if USE(TEXTURE_MAPPER_GL)
217 class RGBA32PremultimpliedBufferQt : public RGBA32PremultimpliedBuffer {
219 virtual PlatformGraphicsContext* beginPaint(const IntRect& rect, bool opaque)
221 // m_image is only using during paint, it's safe to override it.
222 m_image = QImage(rect.size().width(), rect.size().height(), opaque ? QImage::Format_RGB32 : QImage::Format_ARGB32_Premultiplied);
225 m_painter.begin(&m_image);
226 TextureMapperQt::initialize(&m_painter);
227 m_painter.translate(-rect.x(), -rect.y());
231 virtual void endPaint() { m_painter.end(); }
232 virtual const void* data() const { return m_image.constBits(); }
239 PassRefPtr<RGBA32PremultimpliedBuffer> RGBA32PremultimpliedBuffer::create()
241 return adoptRef(new RGBA32PremultimpliedBufferQt());
244 uint64_t uidForImage(Image* image)
246 return image->nativeImageForCurrentFrame()->serialNumber();