| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 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 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "svgalibpaintengine.h" | 
|---|
| 43 |  | 
|---|
| 44 | #include <QColor> | 
|---|
| 45 | #include <vga.h> | 
|---|
| 46 | #include <vgagl.h> | 
|---|
| 47 |  | 
|---|
| 48 | SvgalibPaintEngine::SvgalibPaintEngine() | 
|---|
| 49 | { | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | SvgalibPaintEngine::~SvgalibPaintEngine() | 
|---|
| 53 | { | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | //! [0] | 
|---|
| 57 | bool SvgalibPaintEngine::begin(QPaintDevice *dev) | 
|---|
| 58 | { | 
|---|
| 59 | device = dev; | 
|---|
| 60 | pen = Qt::NoPen; | 
|---|
| 61 | simplePen = true; | 
|---|
| 62 | brush = Qt::NoBrush; | 
|---|
| 63 | simpleBrush = true; | 
|---|
| 64 | matrix = QMatrix(); | 
|---|
| 65 | simpleMatrix = true; | 
|---|
| 66 | setClip(QRect(0, 0, device->width(), device->height())); | 
|---|
| 67 | opaque = true; | 
|---|
| 68 | aliased = true; | 
|---|
| 69 | sourceOver = true; | 
|---|
| 70 |  | 
|---|
| 71 | return QRasterPaintEngine::begin(dev); | 
|---|
| 72 | } | 
|---|
| 73 | //! [0] | 
|---|
| 74 |  | 
|---|
| 75 | //! [1] | 
|---|
| 76 | bool SvgalibPaintEngine::end() | 
|---|
| 77 | { | 
|---|
| 78 | gl_setclippingwindow(0, 0, device->width() - 1, device->height() - 1); | 
|---|
| 79 | return QRasterPaintEngine::end(); | 
|---|
| 80 | } | 
|---|
| 81 | //! [1] | 
|---|
| 82 |  | 
|---|
| 83 | //! [2] | 
|---|
| 84 | void SvgalibPaintEngine::updateState(const QPaintEngineState &state) | 
|---|
| 85 | { | 
|---|
| 86 | QPaintEngine::DirtyFlags flags = state.state(); | 
|---|
| 87 |  | 
|---|
| 88 | if (flags & DirtyTransform) { | 
|---|
| 89 | matrix = state.matrix(); | 
|---|
| 90 | simpleMatrix = (matrix.m12() == 0 && matrix.m21() == 0); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | if (flags & DirtyPen) { | 
|---|
| 94 | pen = state.pen(); | 
|---|
| 95 | simplePen = (pen.width() == 0 || pen.widthF() <= 1) | 
|---|
| 96 | && (pen.style() == Qt::NoPen || pen.style() == Qt::SolidLine) | 
|---|
| 97 | && (pen.color().alpha() == 255); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | if (flags & DirtyBrush) { | 
|---|
| 101 | brush = state.brush(); | 
|---|
| 102 | simpleBrush = (brush.style() == Qt::SolidPattern | 
|---|
| 103 | || brush.style() == Qt::NoBrush) | 
|---|
| 104 | && (brush.color().alpha() == 255); | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | if (flags & DirtyClipRegion) | 
|---|
| 108 | setClip(state.clipRegion()); | 
|---|
| 109 |  | 
|---|
| 110 | if (flags & DirtyClipEnabled) { | 
|---|
| 111 | clipEnabled = state.isClipEnabled(); | 
|---|
| 112 | updateClip(); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | if (flags & DirtyClipPath) { | 
|---|
| 116 | setClip(QRegion()); | 
|---|
| 117 | simpleClip = false; | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | if (flags & DirtyCompositionMode) { | 
|---|
| 121 | const QPainter::CompositionMode m = state.compositionMode(); | 
|---|
| 122 | sourceOver = (m == QPainter::CompositionMode_SourceOver); | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | if (flags & DirtyOpacity) | 
|---|
| 126 | opaque = (state.opacity() == 256); | 
|---|
| 127 |  | 
|---|
| 128 | if (flags & DirtyHints) | 
|---|
| 129 | aliased = !(state.renderHints() & QPainter::Antialiasing); | 
|---|
| 130 |  | 
|---|
| 131 | QRasterPaintEngine::updateState(state); | 
|---|
| 132 | } | 
|---|
| 133 | //! [2] | 
|---|
| 134 |  | 
|---|
| 135 | //! [3] | 
|---|
| 136 | void SvgalibPaintEngine::setClip(const QRegion ®ion) | 
|---|
| 137 | { | 
|---|
| 138 | if (region.isEmpty()) | 
|---|
| 139 | clip = QRect(0, 0, device->width(), device->height()); | 
|---|
| 140 | else | 
|---|
| 141 | clip = matrix.map(region) & QRect(0, 0, device->width(), device->height()); | 
|---|
| 142 | clipEnabled = true; | 
|---|
| 143 | updateClip(); | 
|---|
| 144 | } | 
|---|
| 145 | //! [3] | 
|---|
| 146 |  | 
|---|
| 147 | //! [4] | 
|---|
| 148 | void SvgalibPaintEngine::updateClip() | 
|---|
| 149 | { | 
|---|
| 150 | QRegion clipRegion = QRect(0, 0, device->width(), device->height()); | 
|---|
| 151 |  | 
|---|
| 152 | if (!systemClip().isEmpty()) | 
|---|
| 153 | clipRegion &= systemClip(); | 
|---|
| 154 | if (clipEnabled) | 
|---|
| 155 | clipRegion &= clip; | 
|---|
| 156 |  | 
|---|
| 157 | simpleClip = (clipRegion.rects().size() <= 1); | 
|---|
| 158 |  | 
|---|
| 159 | const QRect r = clipRegion.boundingRect(); | 
|---|
| 160 | gl_setclippingwindow(r.left(), r.top(), | 
|---|
| 161 | r.x() + r.width(), | 
|---|
| 162 | r.y() + r.height()); | 
|---|
| 163 | } | 
|---|
| 164 | //! [4] | 
|---|
| 165 |  | 
|---|
| 166 | //! [5] | 
|---|
| 167 | void SvgalibPaintEngine::drawRects(const QRect *rects, int rectCount) | 
|---|
| 168 | { | 
|---|
| 169 | const bool canAccelerate = simplePen && simpleBrush && simpleMatrix | 
|---|
| 170 | && simpleClip && opaque && aliased | 
|---|
| 171 | && sourceOver; | 
|---|
| 172 | if (!canAccelerate) { | 
|---|
| 173 | QRasterPaintEngine::drawRects(rects, rectCount); | 
|---|
| 174 | return; | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | for (int i = 0; i < rectCount; ++i) { | 
|---|
| 178 | const QRect r = matrix.mapRect(rects[i]); | 
|---|
| 179 | if (brush != Qt::NoBrush) { | 
|---|
| 180 | gl_fillbox(r.left(), r.top(), r.width(), r.height(), | 
|---|
| 181 | brush.color().rgba()); | 
|---|
| 182 | } | 
|---|
| 183 | if (pen != Qt::NoPen) { | 
|---|
| 184 | const int c = pen.color().rgba(); | 
|---|
| 185 | gl_hline(r.left(), r.top(), r.right(), c); | 
|---|
| 186 | gl_hline(r.left(), r.bottom(), r.right(), c); | 
|---|
| 187 | gl_line(r.left(), r.top(), r.left(), r.bottom(), c); | 
|---|
| 188 | gl_line(r.right(), r.top(), r.right(), r.bottom(), c); | 
|---|
| 189 | } | 
|---|
| 190 | } | 
|---|
| 191 | } | 
|---|
| 192 | //! [5] | 
|---|