|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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 QtOpenGL 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 "qtextureglyphcache_gl_p.h" |
|
43 #include "qpaintengineex_opengl2_p.h" |
|
44 |
|
45 #ifdef Q_WS_WIN |
|
46 extern Q_GUI_EXPORT bool qt_cleartype_enabled; |
|
47 #endif |
|
48 |
|
49 QGLTextureGlyphCache::QGLTextureGlyphCache(QGLContext *context, QFontEngineGlyphCache::Type type, const QTransform &matrix) |
|
50 : QTextureGlyphCache(type, matrix) |
|
51 , ctx(context) |
|
52 , m_width(0) |
|
53 , m_height(0) |
|
54 { |
|
55 glGenFramebuffers(1, &m_fbo); |
|
56 connect(QGLSignalProxy::instance(), SIGNAL(aboutToDestroyContext(const QGLContext*)), |
|
57 SLOT(contextDestroyed(const QGLContext*))); |
|
58 } |
|
59 |
|
60 QGLTextureGlyphCache::~QGLTextureGlyphCache() |
|
61 { |
|
62 if (ctx) { |
|
63 QGLShareContextScope scope(ctx); |
|
64 glDeleteFramebuffers(1, &m_fbo); |
|
65 |
|
66 if (m_width || m_height) |
|
67 glDeleteTextures(1, &m_texture); |
|
68 } |
|
69 } |
|
70 |
|
71 void QGLTextureGlyphCache::createTextureData(int width, int height) |
|
72 { |
|
73 glGenTextures(1, &m_texture); |
|
74 glBindTexture(GL_TEXTURE_2D, m_texture); |
|
75 |
|
76 m_width = width; |
|
77 m_height = height; |
|
78 |
|
79 QVarLengthArray<uchar> data(width * height); |
|
80 for (int i = 0; i < data.size(); ++i) |
|
81 data[i] = 0; |
|
82 |
|
83 if (m_type == QFontEngineGlyphCache::Raster_RGBMask) |
|
84 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, &data[0]); |
|
85 else |
|
86 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, &data[0]); |
|
87 |
|
88 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
|
89 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
|
90 } |
|
91 |
|
92 void QGLTextureGlyphCache::resizeTextureData(int width, int height) |
|
93 { |
|
94 // ### the QTextureGlyphCache API needs to be reworked to allow |
|
95 // ### resizeTextureData to fail |
|
96 |
|
97 int oldWidth = m_width; |
|
98 int oldHeight = m_height; |
|
99 |
|
100 GLuint oldTexture = m_texture; |
|
101 createTextureData(width, height); |
|
102 |
|
103 glBindFramebuffer(GL_FRAMEBUFFER_EXT, m_fbo); |
|
104 |
|
105 GLuint tmp_texture; |
|
106 glGenTextures(1, &tmp_texture); |
|
107 glBindTexture(GL_TEXTURE_2D, tmp_texture); |
|
108 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, oldWidth, oldHeight, 0, |
|
109 GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
|
110 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
|
111 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
|
112 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
|
113 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
|
114 glBindTexture(GL_TEXTURE_2D, 0); |
|
115 glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, |
|
116 GL_TEXTURE_2D, tmp_texture, 0); |
|
117 |
|
118 glActiveTexture(GL_TEXTURE0 + QT_IMAGE_TEXTURE_UNIT); |
|
119 glBindTexture(GL_TEXTURE_2D, oldTexture); |
|
120 |
|
121 pex->transferMode(BrushDrawingMode); |
|
122 |
|
123 glDisable(GL_STENCIL_TEST); |
|
124 glDisable(GL_DEPTH_TEST); |
|
125 glDisable(GL_SCISSOR_TEST); |
|
126 glDisable(GL_BLEND); |
|
127 |
|
128 glViewport(0, 0, oldWidth, oldHeight); |
|
129 |
|
130 GLfloat* vertexCoordinateArray = pex->staticVertexCoordinateArray; |
|
131 vertexCoordinateArray[0] = -1.0f; |
|
132 vertexCoordinateArray[1] = -1.0f; |
|
133 vertexCoordinateArray[2] = 1.0f; |
|
134 vertexCoordinateArray[3] = -1.0f; |
|
135 vertexCoordinateArray[4] = 1.0f; |
|
136 vertexCoordinateArray[5] = 1.0f; |
|
137 vertexCoordinateArray[6] = -1.0f; |
|
138 vertexCoordinateArray[7] = 1.0f; |
|
139 |
|
140 GLfloat* textureCoordinateArray = pex->staticTextureCoordinateArray; |
|
141 textureCoordinateArray[0] = 0.0f; |
|
142 textureCoordinateArray[1] = 0.0f; |
|
143 textureCoordinateArray[2] = 1.0f; |
|
144 textureCoordinateArray[3] = 0.0f; |
|
145 textureCoordinateArray[4] = 1.0f; |
|
146 textureCoordinateArray[5] = 1.0f; |
|
147 textureCoordinateArray[6] = 0.0f; |
|
148 textureCoordinateArray[7] = 1.0f; |
|
149 |
|
150 pex->setVertexAttributePointer(QT_VERTEX_COORDS_ATTR, vertexCoordinateArray); |
|
151 pex->setVertexAttributePointer(QT_TEXTURE_COORDS_ATTR, textureCoordinateArray); |
|
152 |
|
153 pex->shaderManager->useBlitProgram(); |
|
154 pex->shaderManager->blitProgram()->setUniformValue("imageTexture", QT_IMAGE_TEXTURE_UNIT); |
|
155 |
|
156 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
|
157 |
|
158 glBindTexture(GL_TEXTURE_2D, m_texture); |
|
159 |
|
160 #ifdef QT_OPENGL_ES_2 |
|
161 QDataBuffer<uchar> buffer(4*oldWidth*oldHeight); |
|
162 buffer.resize(4*oldWidth*oldHeight); |
|
163 glReadPixels(0, 0, oldWidth, oldHeight, GL_RGBA, GL_UNSIGNED_BYTE, buffer.data()); |
|
164 |
|
165 // do an in-place conversion from GL_RGBA to GL_ALPHA |
|
166 for (int i=0; i<oldWidth*oldHeight; ++i) |
|
167 buffer.data()[i] = buffer.at(4*i + 3); |
|
168 |
|
169 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, oldWidth, oldHeight, |
|
170 GL_ALPHA, GL_UNSIGNED_BYTE, buffer.data()); |
|
171 #else |
|
172 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, oldWidth, oldHeight); |
|
173 #endif |
|
174 |
|
175 glFramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, |
|
176 GL_RENDERBUFFER_EXT, 0); |
|
177 glDeleteTextures(1, &tmp_texture); |
|
178 glDeleteTextures(1, &oldTexture); |
|
179 |
|
180 glBindFramebuffer(GL_FRAMEBUFFER_EXT, ctx->d_ptr->current_fbo); |
|
181 |
|
182 glViewport(0, 0, pex->width, pex->height); |
|
183 pex->updateClipScissorTest(); |
|
184 } |
|
185 |
|
186 void QGLTextureGlyphCache::fillTexture(const Coord &c, glyph_t glyph) |
|
187 { |
|
188 QImage mask = textureMapForGlyph(glyph); |
|
189 const int maskWidth = mask.width(); |
|
190 const int maskHeight = mask.height(); |
|
191 |
|
192 if (mask.format() == QImage::Format_Mono) { |
|
193 mask = mask.convertToFormat(QImage::Format_Indexed8); |
|
194 for (int y = 0; y < maskHeight; ++y) { |
|
195 uchar *src = (uchar *) mask.scanLine(y); |
|
196 for (int x = 0; x < maskWidth; ++x) |
|
197 src[x] = -src[x]; // convert 0 and 1 into 0 and 255 |
|
198 } |
|
199 } else if (mask.format() == QImage::Format_RGB32) { |
|
200 // Make the alpha component equal to the average of the RGB values. |
|
201 // This is needed when drawing sub-pixel antialiased text on translucent targets. |
|
202 for (int y = 0; y < maskHeight; ++y) { |
|
203 quint32 *src = (quint32 *) mask.scanLine(y); |
|
204 for (int x = 0; x < maskWidth; ++x) { |
|
205 uchar r = src[x] >> 16; |
|
206 uchar g = src[x] >> 8; |
|
207 uchar b = src[x]; |
|
208 quint32 avg = (quint32(r) + quint32(g) + quint32(b) + 1) / 3; // "+1" for rounding. |
|
209 src[x] = (src[x] & 0x00ffffff) | (avg << 24); |
|
210 } |
|
211 } |
|
212 } |
|
213 |
|
214 glBindTexture(GL_TEXTURE_2D, m_texture); |
|
215 if (mask.format() == QImage::Format_RGB32) { |
|
216 glTexSubImage2D(GL_TEXTURE_2D, 0, c.x, c.y, maskWidth, maskHeight, GL_BGRA, GL_UNSIGNED_BYTE, mask.bits()); |
|
217 } else { |
|
218 #ifdef QT_OPENGL_ES2 |
|
219 glTexSubImage2D(GL_TEXTURE_2D, 0, c.x, c.y, maskWidth, maskHeight, GL_ALPHA, GL_UNSIGNED_BYTE, mask.bits()); |
|
220 #else |
|
221 // glTexSubImage2D() might cause some garbage to appear in the texture if the mask width is |
|
222 // not a multiple of four bytes. The bug appeared on a computer with 32-bit Windows Vista |
|
223 // and nVidia GeForce 8500GT. GL_UNPACK_ALIGNMENT is set to four bytes, 'mask' has a |
|
224 // multiple of four bytes per line, and most of the glyph shows up correctly in the |
|
225 // texture, which makes me think that this is a driver bug. |
|
226 // One workaround is to make sure the mask width is a multiple of four bytes, for instance |
|
227 // by converting it to a format with four bytes per pixel. Another is to copy one line at a |
|
228 // time. |
|
229 |
|
230 for (int i = 0; i < maskHeight; ++i) |
|
231 glTexSubImage2D(GL_TEXTURE_2D, 0, c.x, c.y + i, maskWidth, 1, GL_ALPHA, GL_UNSIGNED_BYTE, mask.scanLine(i)); |
|
232 #endif |
|
233 } |
|
234 } |
|
235 |
|
236 int QGLTextureGlyphCache::glyphMargin() const |
|
237 { |
|
238 #if defined(Q_WS_MAC) |
|
239 return 2; |
|
240 #elif defined (Q_WS_X11) |
|
241 return 0; |
|
242 #else |
|
243 return m_type == QFontEngineGlyphCache::Raster_RGBMask ? 2 : 0; |
|
244 #endif |
|
245 } |