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