source: trunk/examples/qws/svgalib/svgalibscreen.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 10.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include "svgalibscreen.h"
42#include "svgalibsurface.h"
43
44#include <QVector>
45#include <QApplication>
46#include <QColor>
47#include <QWidget>
48
49#include <math.h>
50
51static int getModeDepth(vga_modeinfo *mode)
52{
53 const int bits = int(log2(mode->colors));
54 if (bits == 24 && mode->bytesperpixel == 4)
55 return 32;
56 return bits;
57}
58
59//! [0]
60bool SvgalibScreen::connect(const QString &displaySpec)
61{
62 int mode = vga_getdefaultmode();
63 if (mode <= 0) {
64 qCritical("SvgalibScreen::connect(): invalid vga mode");
65 return false;
66 }
67
68 vga_modeinfo *modeinfo = vga_getmodeinfo(mode);
69
70 QScreen::lstep = modeinfo->linewidth;
71 QScreen::dw = QScreen::w = modeinfo->width;
72 QScreen::dh = QScreen::h = modeinfo->height;
73 QScreen::d = getModeDepth(modeinfo);
74 QScreen::size = QScreen::lstep * dh;
75 QScreen::data = 0;
76
77 switch (depth()) {
78 case 32:
79 setPixelFormat(QImage::Format_ARGB32_Premultiplied);
80 break;
81 case 24:
82 setPixelFormat(QImage::Format_RGB888);
83 break;
84 case 16:
85 setPixelFormat(QImage::Format_RGB16);
86 break;
87 case 15:
88 setPixelFormat(QImage::Format_RGB555);
89 break;
90 default:
91 break;
92 }
93
94 const int dpi = 72;
95 QScreen::physWidth = qRound(QScreen::dw * 25.4 / dpi);
96 QScreen::physHeight = qRound(QScreen::dh * 25.4 / dpi);
97
98 const QStringList args = displaySpec.split(QLatin1Char(':'),
99 QString::SkipEmptyParts);
100 grayscale = args.contains(QLatin1String("grayscale"), Qt::CaseInsensitive);
101
102 return true;
103}
104//! [0]
105
106void SvgalibScreen::initColorMap()
107{
108 const int numColors = vga_getcolors();
109 if (numColors == 2 || numColors > 256) {
110 screencols = 0;
111 return; // not a palette based mode
112 }
113
114 if (numColors == 16) {
115 if (grayscale) {
116 for (int i = 0; i < 256; ++i) {
117 const int c = i * 16 / 256;
118 vga_setpalette(i, c, c, c);
119 }
120 screencols = 256; // XXX: takes advantage of optimization in alloc()
121 } else { // read in EGA palette
122 int r, g, b;
123 for (int i = 0; i < 16; ++i) {
124 vga_getpalette(i, &r, &g, &b);
125 screenclut[i] = qRgb(r, g, b);
126 }
127 screencols = 16;
128 }
129
130 return;
131 }
132
133 Q_ASSERT(numColors == 256);
134
135 if (grayscale) {
136 for (int i = 0; i < 256; ++i) {
137 const int c = i * 64 / 256;
138 vga_setpalette(i, c, c, c);
139 }
140 } else {
141 int i = 0;
142
143#if 0
144 // read in EGA palette
145 while (i < 16) {
146 int r, g, b;
147 vga_getpalette(i, &r, &g, &b);
148 screenclut[i] = qRgb(r, g, b);
149 ++i;
150 }
151 screencols = 16;
152#endif
153
154 // 6 * 6 * 6 color cube
155 for (int r = 0; r < 6; ++r) {
156 for (int g = 0; g < 6; ++g) {
157 for (int b = 0; b < 6; ++b) {
158 vga_setpalette(i, r * 64/6, g * 64/6, b * 64/6);
159 screenclut[i] = qRgb(r * 256/6, g * 256/6, b * 256/6);
160 ++i;
161 }
162 }
163 }
164 screencols = i;
165
166 while (i < 256) {
167 screenclut[i] = qRgb(0, 0, 0);
168 ++i;
169 }
170 }
171}
172
173//! [1]
174bool SvgalibScreen::initDevice()
175{
176 if (vga_init() != 0) {
177 qCritical("SvgalibScreen::initDevice(): unable to initialize svgalib");
178 return false;
179 }
180
181 int mode = vga_getdefaultmode();
182 if (vga_setmode(mode) == -1) {
183 qCritical("SvgalibScreen::initialize(): unable to set graphics mode");
184 return false;
185 }
186
187 if (gl_setcontextvga(mode) != 0) {
188 qCritical("SvgalibScreen::initDevice(): unable to set vga context");
189 return false;
190 }
191 context = gl_allocatecontext();
192 gl_getcontext(context);
193
194 vga_modeinfo *modeinfo = vga_getmodeinfo(mode);
195 if (modeinfo->flags & IS_LINEAR)
196 QScreen::data = vga_getgraphmem();
197
198 initColorMap();
199
200 QScreenCursor::initSoftwareCursor();
201 return true;
202}
203//! [1]
204
205//! [2]
206void SvgalibScreen::shutdownDevice()
207{
208 gl_freecontext(context);
209 vga_setmode(TEXT);
210}
211//! [2]
212
213//! [3]
214void SvgalibScreen::disconnect()
215{
216}
217//! [3]
218
219//! [4]
220void SvgalibScreen::solidFill(const QColor &color, const QRegion &reg)
221{
222 int c;
223 if (depth() == 4 || depth() == 8)
224 c = alloc(color.red(), color.green(), color.blue());
225 else
226 c = gl_rgbcolor(color.red(), color.green(), color.blue());
227
228 const QVector<QRect> rects = (reg & region()).rects();
229 for (int i = 0; i < rects.size(); ++i) {
230 const QRect r = rects.at(i);
231 gl_fillbox(r.left(), r.top(), r.width(), r.height(), c);
232 }
233}
234//! [4]
235
236void SvgalibScreen::blit16To8(const QImage &image,
237 const QPoint &topLeft, const QRegion &region)
238{
239 const int imageStride = image.bytesPerLine() / 2;
240 const QVector<QRect> rects = region.rects();
241
242 for (int i = 0; i < rects.size(); ++i) {
243 const QRect r = rects.at(i).translated(-topLeft);
244 int y = r.y();
245 const quint16 *s = reinterpret_cast<const quint16*>(image.scanLine(y));
246
247 while (y <= r.bottom()) {
248 int x1 = r.x();
249 while (x1 <= r.right()) {
250 const quint16 c = s[x1];
251 int x2 = x1;
252 // find span length
253 while ((x2+1 < r.right()) && (s[x2+1] == c))
254 ++x2;
255 gl_hline(x1 + topLeft.x(), y + topLeft.y(), x2 + topLeft.x(),
256 qt_colorConvert<quint8, quint16>(c, 0));
257 x1 = x2 + 1;
258 }
259 s += imageStride;
260 ++y;
261 }
262 }
263}
264
265void SvgalibScreen::blit32To8(const QImage &image,
266 const QPoint &topLeft, const QRegion &region)
267{
268 const int imageStride = image.bytesPerLine() / 4;
269 const QVector<QRect> rects = region.rects();
270
271 for (int i = 0; i < rects.size(); ++i) {
272 const QRect r = rects.at(i).translated(-topLeft);
273 int y = r.y();
274 const quint32 *s = reinterpret_cast<const quint32*>(image.scanLine(y));
275
276 while (y <= r.bottom()) {
277 int x1 = r.x();
278 while (x1 <= r.right()) {
279 const quint32 c = s[x1];
280 int x2 = x1;
281 // find span length
282 while ((x2+1 < r.right()) && (s[x2+1] == c))
283 ++x2;
284 gl_hline(x1 + topLeft.x(), y + topLeft.y(), x2 + topLeft.x(),
285 qt_colorConvert<quint8, quint32>(c, 0));
286 x1 = x2 + 1;
287 }
288 s += imageStride;
289 ++y;
290 }
291 }
292}
293
294//! [5]
295void SvgalibScreen::blit(const QImage &img, const QPoint &topLeft,
296 const QRegion &reg)
297{
298 if (depth() == 8) {
299 switch (img.format()) {
300 case QImage::Format_RGB16:
301 blit16To8(img, topLeft, reg);
302 return;
303 case QImage::Format_RGB32:
304 case QImage::Format_ARGB32:
305 case QImage::Format_ARGB32_Premultiplied:
306 blit32To8(img, topLeft, reg);
307 return;
308 default:
309 break;
310 }
311 }
312
313 if (img.format() != pixelFormat()) {
314 if (base())
315 QScreen::blit(img, topLeft, reg);
316 return;
317 }
318
319 const QVector<QRect> rects = (reg & region()).rects();
320
321 for (int i = 0; i < rects.size(); ++i) {
322 const QRect r = rects.at(i);
323 gl_putboxpart(r.x(), r.y(), r.width(), r.height(),
324 img.width(), img.height(),
325 static_cast<void*>(const_cast<uchar*>(img.bits())),
326 r.x() - topLeft.x(), r.y() - topLeft.y());
327 }
328}
329//! [5]
330
331//! [7]
332QWSWindowSurface* SvgalibScreen::createSurface(QWidget *widget) const
333{
334 if (base()) {
335 static int onScreenPaint = -1;
336 if (onScreenPaint == -1)
337 onScreenPaint = qgetenv("QT_ONSCREEN_PAINT").toInt();
338
339 if (onScreenPaint > 0 || widget->testAttribute(Qt::WA_PaintOnScreen))
340 return new SvgalibSurface(widget);
341 }
342 return QScreen::createSurface(widget);
343}
344//! [7]
345
346//! [8]
347QWSWindowSurface* SvgalibScreen::createSurface(const QString &key) const
348{
349 if (key == QLatin1String("svgalib"))
350 return new SvgalibSurface;
351 return QScreen::createSurface(key);
352}
353//! [8]
Note: See TracBrowser for help on using the repository browser.