Support Alpha8 and Grayscale8 natively in the OpenGL paint engine

Adds special shaders for the Alpha8 and Grayscale8 formats so that
they do not need to rely on the support of GL_ALPHA and GL_LUMINANCE
that has been removed from core in recent OpenGL versions.

Change-Id: Ie370379b458abf2a50e252bc5099aefc1b11fb1d
Reviewed-by: Laszlo Agocs <laszlo.agocs@digia.com>
This commit is contained in:
Allan Sandfeld Jensen 2014-08-04 11:47:11 +02:00 committed by Allan Sandfeld Jensen
parent da72e5538e
commit 5adbb9cc57
9 changed files with 139 additions and 7 deletions

View File

@ -154,6 +154,8 @@ QOpenGLEngineSharedShaders::QOpenGLEngineSharedShaders(QOpenGLContext* context)
code[ImageSrcFragmentShader] = qopenglslImageSrcFragmentShader;
code[ImageSrcWithPatternFragmentShader] = qopenglslImageSrcWithPatternFragmentShader;
code[NonPremultipliedImageSrcFragmentShader] = qopenglslNonPremultipliedImageSrcFragmentShader;
code[GrayscaleImageSrcFragmentShader] = qopenglslGrayscaleImageSrcFragmentShader;
code[AlphaImageSrcFragmentShader] = qopenglslAlphaImageSrcFragmentShader;
code[CustomImageSrcFragmentShader] = qopenglslCustomSrcFragmentShader; // Calls "customShader", which must be appended
code[SolidBrushSrcFragmentShader] = qopenglslSolidBrushSrcFragmentShader;
if (context->isOpenGLES())
@ -700,6 +702,16 @@ bool QOpenGLEngineShaderManager::useCorrectShaderProg()
requiredProgram.positionVertexShader = QOpenGLEngineSharedShaders::PositionOnlyVertexShader;
texCoords = true;
break;
case QOpenGLEngineShaderManager::GrayscaleImageSrc:
requiredProgram.srcPixelFragShader = QOpenGLEngineSharedShaders::GrayscaleImageSrcFragmentShader;
requiredProgram.positionVertexShader = QOpenGLEngineSharedShaders::PositionOnlyVertexShader;
texCoords = true;
break;
case QOpenGLEngineShaderManager::AlphaImageSrc:
requiredProgram.srcPixelFragShader = QOpenGLEngineSharedShaders::AlphaImageSrcFragmentShader;
requiredProgram.positionVertexShader = QOpenGLEngineSharedShaders::PositionOnlyVertexShader;
texCoords = true;
break;
case QOpenGLEngineShaderManager::PatternSrc:
requiredProgram.srcPixelFragShader = QOpenGLEngineSharedShaders::ImageSrcWithPatternFragmentShader;
requiredProgram.positionVertexShader = QOpenGLEngineSharedShaders::PositionOnlyVertexShader;

View File

@ -288,6 +288,8 @@ public:
ImageSrcFragmentShader,
ImageSrcWithPatternFragmentShader,
NonPremultipliedImageSrcFragmentShader,
GrayscaleImageSrcFragmentShader,
AlphaImageSrcFragmentShader,
CustomImageSrcFragmentShader,
SolidBrushSrcFragmentShader,
TextureBrushSrcFragmentShader,
@ -414,7 +416,9 @@ public:
ImageSrc = Qt::TexturePattern+1,
NonPremultipliedImageSrc = Qt::TexturePattern+2,
PatternSrc = Qt::TexturePattern+3,
TextureSrcWithPattern = Qt::TexturePattern+4
TextureSrcWithPattern = Qt::TexturePattern+4,
GrayscaleImageSrc = Qt::TexturePattern+5,
AlphaImageSrc = Qt::TexturePattern+6,
};
enum Uniform {

View File

@ -367,6 +367,22 @@ static const char* const qopenglslNonPremultipliedImageSrcFragmentShader = "\n\
return sample; \n\
}\n";
static const char* const qopenglslGrayscaleImageSrcFragmentShader = "\n\
varying highp vec2 textureCoords; \n\
uniform sampler2D imageTexture; \n\
lowp vec4 srcPixel() \n\
{ \n\
return texture2D(imageTexture, textureCoords).rrra; \n\
}\n";
static const char* const qopenglslAlphaImageSrcFragmentShader = "\n\
varying highp vec2 textureCoords; \n\
uniform sampler2D imageTexture; \n\
lowp vec4 srcPixel() \n\
{ \n\
return vec4(0, 0, 0, texture2D(imageTexture, textureCoords).r); \n\
}\n";
static const char* const qopenglslShockingPinkSrcFragmentShader = "\n\
lowp vec4 srcPixel() \n\
{ \n\

View File

@ -173,6 +173,7 @@ QT_BEGIN_NAMESPACE
\value NPOTTextures Non power of two textures are available.
\value NPOTTextureRepeat Non power of two textures can use GL_REPEAT as wrap parameter.
\value FixedFunctionPipeline The fixed function pipeline is available.
\value TextureRGFormats The GL_RED and GL_RG texture formats are available.
*/
// Hidden private fields for additional extension data.
@ -284,10 +285,12 @@ static int qt_gl_resolve_features()
if (extensions.match("GL_OES_texture_npot"))
features |= QOpenGLFunctions::NPOTTextures |
QOpenGLFunctions::NPOTTextureRepeat;
if (ctx->format().majorVersion() >= 3 || extensions.match("GL_EXT_texture_rg"))
features |= QOpenGLFunctions::TextureRGFormats;
return features;
} else {
// OpenGL
int features = 0;
int features = QOpenGLFunctions::TextureRGFormats;
QSurfaceFormat format = QOpenGLContext::currentContext()->format();
QOpenGLExtensionMatcher extensions;

View File

@ -240,7 +240,8 @@ public:
StencilSeparate = 0x0800,
NPOTTextures = 0x1000,
NPOTTextureRepeat = 0x2000,
FixedFunctionPipeline = 0x4000
FixedFunctionPipeline = 0x4000,
TextureRGFormats = 0x8000
};
Q_DECLARE_FLAGS(OpenGLFeatures, OpenGLFeature)

View File

@ -1421,6 +1421,20 @@ void QOpenGL2PaintEngineEx::drawImage(const QRectF& dest, const QImage& image, c
d->shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::NonPremultipliedImageSrc);
bindOption = 0;
break;
case QImage::Format_Alpha8:
if (ctx->functions()->hasOpenGLFeature(QOpenGLFunctions::TextureRGFormats)) {
d->shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::AlphaImageSrc);
bindOption = QOpenGLTextureCache::UseRedFor8BitBindOption;
} else
d->shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::ImageSrc);
break;
case QImage::Format_Grayscale8:
if (ctx->functions()->hasOpenGLFeature(QOpenGLFunctions::TextureRGFormats)) {
d->shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::GrayscaleImageSrc);
bindOption = QOpenGLTextureCache::UseRedFor8BitBindOption;
} else
d->shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::ImageSrc);
break;
default:
d->shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::ImageSrc);
break;

View File

@ -41,6 +41,10 @@
QT_BEGIN_NAMESPACE
#ifndef GL_RED
#define GL_RED 0x1903
#endif
#ifndef GL_RGB10_A2
#define GL_RGB10_A2 0x8059
#endif
@ -273,21 +277,34 @@ GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, qint64 key, con
pixelType = GL_UNSIGNED_BYTE;
targetFormat = image.format();
break;
case QImage::Format_Indexed8:
if (options & UseRedFor8BitBindOption) {
externalFormat = internalFormat = GL_RED;
pixelType = GL_UNSIGNED_BYTE;
targetFormat = image.format();
}
break;
case QImage::Format_Alpha8:
if (context->isOpenGLES() || context->format().profile() != QSurfaceFormat::CoreProfile) {
if (options & UseRedFor8BitBindOption) {
externalFormat = internalFormat = GL_RED;
pixelType = GL_UNSIGNED_BYTE;
targetFormat = image.format();
} else if (context->isOpenGLES() || context->format().profile() != QSurfaceFormat::CoreProfile) {
externalFormat = internalFormat = GL_ALPHA;
pixelType = GL_UNSIGNED_BYTE;
targetFormat = image.format();
}
// ### add support for core profiles.
break;
case QImage::Format_Grayscale8:
if (context->isOpenGLES() || context->format().profile() != QSurfaceFormat::CoreProfile) {
if (options & UseRedFor8BitBindOption) {
externalFormat = internalFormat = GL_RED;
pixelType = GL_UNSIGNED_BYTE;
targetFormat = image.format();
} else if (context->isOpenGLES() || context->format().profile() != QSurfaceFormat::CoreProfile) {
externalFormat = internalFormat = GL_LUMINANCE;
pixelType = GL_UNSIGNED_BYTE;
targetFormat = image.format();
}
// ### add support for core profiles.
break;
default:
break;

View File

@ -78,6 +78,7 @@ public:
enum BindOption {
NoBindOption = 0x0000,
PremultipliedAlphaBindOption = 0x0001,
UseRedFor8BitBindOption = 0x0002,
};
Q_DECLARE_FLAGS(BindOptions, BindOption)

View File

@ -98,6 +98,8 @@ private slots:
void textureblitterPartTargetRectTransform();
void defaultSurfaceFormat();
void imageFormatPainting();
#ifdef USE_GLX
void glxContextWrap();
#endif
@ -718,6 +720,68 @@ void tst_QOpenGL::fboHandleNulledAfterContextDestroyed()
QCOMPARE(fbo->handle(), 0U);
}
void tst_QOpenGL::imageFormatPainting()
{
QScopedPointer<QSurface> surface(createSurface(QSurface::Window));
QOpenGLContext ctx;
QVERIFY(ctx.create());
QVERIFY(ctx.makeCurrent(surface.data()));
if (!QOpenGLFramebufferObject::hasOpenGLFramebufferObjects())
QSKIP("QOpenGLFramebufferObject not supported on this platform");
QOpenGLFramebufferObjectFormat fboFormat;
fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
const QSize size(128, 128);
QOpenGLFramebufferObject fbo(size, fboFormat);
if (fbo.attachment() != QOpenGLFramebufferObject::CombinedDepthStencil)
QSKIP("FBOs missing combined depth~stencil support");
QVERIFY(fbo.bind());
QImage alpha(128, 128, QImage::Format_Alpha8);
alpha.fill(127);
QPainter fboPainter;
QOpenGLPaintDevice device(fbo.width(), fbo.height());
QVERIFY(fboPainter.begin(&device));
fboPainter.fillRect(0, 0, 128, 128, qRgb(255, 0, 255));
fboPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
fboPainter.drawImage(0, 0, alpha);
fboPainter.end();
QImage fb = fbo.toImage();
QCOMPARE(fb.pixel(0, 0), qRgba(127, 0, 127, 127));
QImage grayscale(128, 128, QImage::Format_Grayscale8);
grayscale.fill(128);
QVERIFY(fboPainter.begin(&device));
fboPainter.setCompositionMode(QPainter::CompositionMode_Plus);
fboPainter.drawImage(0, 0, grayscale);
fboPainter.end();
fb = fbo.toImage();
QCOMPARE(fb.pixel(0, 0), qRgb(255, 128, 255));
QImage argb(128, 128, QImage::Format_ARGB32);
argb.fill(qRgba(255, 255, 255, 128));
QVERIFY(fboPainter.begin(&device));
fboPainter.setCompositionMode(QPainter::CompositionMode_SourceOver);
fboPainter.drawImage(0, 0, argb);
fboPainter.end();
fb = fbo.toImage();
QCOMPARE(fb.pixel(0, 0), qRgb(255, 192, 255));
}
void tst_QOpenGL::openGLPaintDevice_data()
{
QTest::addColumn<int>("surfaceClass");